blob: 42756771b8ebdcf930e53bd490e85f1b58de5c57 [file] [log] [blame]
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001/*
2 * Core of Xen paravirt_ops implementation.
3 *
4 * This file contains the xen_paravirt_ops structure itself, and the
5 * implementations for:
6 * - privileged instructions
7 * - interrupt flags
8 * - segment operations
9 * - booting and setup
10 *
11 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
12 */
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/smp.h>
17#include <linux/preempt.h>
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070018#include <linux/hardirq.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070019#include <linux/percpu.h>
20#include <linux/delay.h>
21#include <linux/start_kernel.h>
22#include <linux/sched.h>
23#include <linux/bootmem.h>
24#include <linux/module.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070025#include <linux/mm.h>
26#include <linux/page-flags.h>
27#include <linux/highmem.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070028#include <linux/smp.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070029
30#include <xen/interface/xen.h>
31#include <xen/interface/physdev.h>
32#include <xen/interface/vcpu.h>
33#include <xen/features.h>
34#include <xen/page.h>
35
36#include <asm/paravirt.h>
37#include <asm/page.h>
38#include <asm/xen/hypercall.h>
39#include <asm/xen/hypervisor.h>
40#include <asm/fixmap.h>
41#include <asm/processor.h>
42#include <asm/setup.h>
43#include <asm/desc.h>
44#include <asm/pgtable.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070045#include <asm/tlbflush.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070046
47#include "xen-ops.h"
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070048#include "mmu.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070049#include "multicalls.h"
50
51EXPORT_SYMBOL_GPL(hypercall_page);
52
53DEFINE_PER_CPU(enum paravirt_lazy_mode, xen_lazy_mode);
54
55DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
56DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
57DEFINE_PER_CPU(unsigned long, xen_cr3);
58
59struct start_info *xen_start_info;
60EXPORT_SYMBOL_GPL(xen_start_info);
61
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070062void xen_vcpu_setup(int cpu)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070063{
64 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
65}
66
67static void __init xen_banner(void)
68{
69 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
70 paravirt_ops.name);
71 printk(KERN_INFO "Hypervisor signature: %s\n", xen_start_info->magic);
72}
73
74static void xen_cpuid(unsigned int *eax, unsigned int *ebx,
75 unsigned int *ecx, unsigned int *edx)
76{
77 unsigned maskedx = ~0;
78
79 /*
80 * Mask out inconvenient features, to try and disable as many
81 * unsupported kernel subsystems as possible.
82 */
83 if (*eax == 1)
84 maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
85 (1 << X86_FEATURE_ACPI) | /* disable ACPI */
86 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
87
88 asm(XEN_EMULATE_PREFIX "cpuid"
89 : "=a" (*eax),
90 "=b" (*ebx),
91 "=c" (*ecx),
92 "=d" (*edx)
93 : "0" (*eax), "2" (*ecx));
94 *edx &= maskedx;
95}
96
97static void xen_set_debugreg(int reg, unsigned long val)
98{
99 HYPERVISOR_set_debugreg(reg, val);
100}
101
102static unsigned long xen_get_debugreg(int reg)
103{
104 return HYPERVISOR_get_debugreg(reg);
105}
106
107static unsigned long xen_save_fl(void)
108{
109 struct vcpu_info *vcpu;
110 unsigned long flags;
111
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700112 vcpu = x86_read_percpu(xen_vcpu);
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700113
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700114 /* flag has opposite sense of mask */
115 flags = !vcpu->evtchn_upcall_mask;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700116
117 /* convert to IF type flag
118 -0 -> 0x00000000
119 -1 -> 0xffffffff
120 */
121 return (-flags) & X86_EFLAGS_IF;
122}
123
124static void xen_restore_fl(unsigned long flags)
125{
126 struct vcpu_info *vcpu;
127
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700128 /* convert from IF type flag */
129 flags = !(flags & X86_EFLAGS_IF);
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700130
131 /* There's a one instruction preempt window here. We need to
132 make sure we're don't switch CPUs between getting the vcpu
133 pointer and updating the mask. */
134 preempt_disable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700135 vcpu = x86_read_percpu(xen_vcpu);
136 vcpu->evtchn_upcall_mask = flags;
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700137 preempt_enable_no_resched();
138
139 /* Doesn't matter if we get preempted here, because any
140 pending event will get dealt with anyway. */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700141
142 if (flags == 0) {
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700143 preempt_check_resched();
144 barrier(); /* unmask then check (avoid races) */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700145 if (unlikely(vcpu->evtchn_upcall_pending))
146 force_evtchn_callback();
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700147 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700148}
149
150static void xen_irq_disable(void)
151{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700152 /* There's a one instruction preempt window here. We need to
153 make sure we're don't switch CPUs between getting the vcpu
154 pointer and updating the mask. */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700155 preempt_disable();
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700156 x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700157 preempt_enable_no_resched();
158}
159
160static void xen_irq_enable(void)
161{
162 struct vcpu_info *vcpu;
163
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700164 /* There's a one instruction preempt window here. We need to
165 make sure we're don't switch CPUs between getting the vcpu
166 pointer and updating the mask. */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700167 preempt_disable();
168 vcpu = x86_read_percpu(xen_vcpu);
169 vcpu->evtchn_upcall_mask = 0;
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700170 preempt_enable_no_resched();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700171
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700172 /* Doesn't matter if we get preempted here, because any
173 pending event will get dealt with anyway. */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700174
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700175 barrier(); /* unmask then check (avoid races) */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700176 if (unlikely(vcpu->evtchn_upcall_pending))
177 force_evtchn_callback();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700178}
179
180static void xen_safe_halt(void)
181{
182 /* Blocking includes an implicit local_irq_enable(). */
183 if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0)
184 BUG();
185}
186
187static void xen_halt(void)
188{
189 if (irqs_disabled())
190 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
191 else
192 xen_safe_halt();
193}
194
195static void xen_set_lazy_mode(enum paravirt_lazy_mode mode)
196{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700197 BUG_ON(preemptible());
198
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700199 switch (mode) {
200 case PARAVIRT_LAZY_NONE:
201 BUG_ON(x86_read_percpu(xen_lazy_mode) == PARAVIRT_LAZY_NONE);
202 break;
203
204 case PARAVIRT_LAZY_MMU:
205 case PARAVIRT_LAZY_CPU:
206 BUG_ON(x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE);
207 break;
208
209 case PARAVIRT_LAZY_FLUSH:
210 /* flush if necessary, but don't change state */
211 if (x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE)
212 xen_mc_flush();
213 return;
214 }
215
216 xen_mc_flush();
217 x86_write_percpu(xen_lazy_mode, mode);
218}
219
220static unsigned long xen_store_tr(void)
221{
222 return 0;
223}
224
225static void xen_set_ldt(const void *addr, unsigned entries)
226{
227 unsigned long linear_addr = (unsigned long)addr;
228 struct mmuext_op *op;
229 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
230
231 op = mcs.args;
232 op->cmd = MMUEXT_SET_LDT;
233 if (linear_addr) {
234 /* ldt my be vmalloced, use arbitrary_virt_to_machine */
235 xmaddr_t maddr;
236 maddr = arbitrary_virt_to_machine((unsigned long)addr);
237 linear_addr = (unsigned long)maddr.maddr;
238 }
239 op->arg1.linear_addr = linear_addr;
240 op->arg2.nr_ents = entries;
241
242 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
243
244 xen_mc_issue(PARAVIRT_LAZY_CPU);
245}
246
247static void xen_load_gdt(const struct Xgt_desc_struct *dtr)
248{
249 unsigned long *frames;
250 unsigned long va = dtr->address;
251 unsigned int size = dtr->size + 1;
252 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
253 int f;
254 struct multicall_space mcs;
255
256 /* A GDT can be up to 64k in size, which corresponds to 8192
257 8-byte entries, or 16 4k pages.. */
258
259 BUG_ON(size > 65536);
260 BUG_ON(va & ~PAGE_MASK);
261
262 mcs = xen_mc_entry(sizeof(*frames) * pages);
263 frames = mcs.args;
264
265 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
266 frames[f] = virt_to_mfn(va);
267 make_lowmem_page_readonly((void *)va);
268 }
269
270 MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
271
272 xen_mc_issue(PARAVIRT_LAZY_CPU);
273}
274
275static void load_TLS_descriptor(struct thread_struct *t,
276 unsigned int cpu, unsigned int i)
277{
278 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
279 xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
280 struct multicall_space mc = __xen_mc_entry(0);
281
282 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
283}
284
285static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
286{
287 xen_mc_batch();
288
289 load_TLS_descriptor(t, cpu, 0);
290 load_TLS_descriptor(t, cpu, 1);
291 load_TLS_descriptor(t, cpu, 2);
292
293 xen_mc_issue(PARAVIRT_LAZY_CPU);
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700294
295 /*
296 * XXX sleazy hack: If we're being called in a lazy-cpu zone,
297 * it means we're in a context switch, and %gs has just been
298 * saved. This means we can zero it out to prevent faults on
299 * exit from the hypervisor if the next process has no %gs.
300 * Either way, it has been saved, and the new value will get
301 * loaded properly. This will go away as soon as Xen has been
302 * modified to not save/restore %gs for normal hypercalls.
303 */
304 if (xen_get_lazy_mode() == PARAVIRT_LAZY_CPU)
305 loadsegment(gs, 0);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700306}
307
308static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
309 u32 low, u32 high)
310{
311 unsigned long lp = (unsigned long)&dt[entrynum];
312 xmaddr_t mach_lp = virt_to_machine(lp);
313 u64 entry = (u64)high << 32 | low;
314
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700315 preempt_disable();
316
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700317 xen_mc_flush();
318 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
319 BUG();
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700320
321 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700322}
323
324static int cvt_gate_to_trap(int vector, u32 low, u32 high,
325 struct trap_info *info)
326{
327 u8 type, dpl;
328
329 type = (high >> 8) & 0x1f;
330 dpl = (high >> 13) & 3;
331
332 if (type != 0xf && type != 0xe)
333 return 0;
334
335 info->vector = vector;
336 info->address = (high & 0xffff0000) | (low & 0x0000ffff);
337 info->cs = low >> 16;
338 info->flags = dpl;
339 /* interrupt gates clear IF */
340 if (type == 0xe)
341 info->flags |= 4;
342
343 return 1;
344}
345
346/* Locations of each CPU's IDT */
347static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc);
348
349/* Set an IDT entry. If the entry is part of the current IDT, then
350 also update Xen. */
351static void xen_write_idt_entry(struct desc_struct *dt, int entrynum,
352 u32 low, u32 high)
353{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700354 unsigned long p = (unsigned long)&dt[entrynum];
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700355 unsigned long start, end;
356
357 preempt_disable();
358
359 start = __get_cpu_var(idt_desc).address;
360 end = start + __get_cpu_var(idt_desc).size + 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700361
362 xen_mc_flush();
363
364 write_dt_entry(dt, entrynum, low, high);
365
366 if (p >= start && (p + 8) <= end) {
367 struct trap_info info[2];
368
369 info[1].address = 0;
370
371 if (cvt_gate_to_trap(entrynum, low, high, &info[0]))
372 if (HYPERVISOR_set_trap_table(info))
373 BUG();
374 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700375
376 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700377}
378
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700379static void xen_convert_trap_info(const struct Xgt_desc_struct *desc,
380 struct trap_info *traps)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700381{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700382 unsigned in, out, count;
383
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700384 count = (desc->size+1) / 8;
385 BUG_ON(count > 256);
386
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700387 for (in = out = 0; in < count; in++) {
388 const u32 *entry = (u32 *)(desc->address + in * 8);
389
390 if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
391 out++;
392 }
393 traps[out].address = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700394}
395
396void xen_copy_trap_info(struct trap_info *traps)
397{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700398 const struct Xgt_desc_struct *desc = &__get_cpu_var(idt_desc);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700399
400 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700401}
402
403/* Load a new IDT into Xen. In principle this can be per-CPU, so we
404 hold a spinlock to protect the static traps[] array (static because
405 it avoids allocation, and saves stack space). */
406static void xen_load_idt(const struct Xgt_desc_struct *desc)
407{
408 static DEFINE_SPINLOCK(lock);
409 static struct trap_info traps[257];
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700410
411 spin_lock(&lock);
412
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700413 __get_cpu_var(idt_desc) = *desc;
414
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700415 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700416
417 xen_mc_flush();
418 if (HYPERVISOR_set_trap_table(traps))
419 BUG();
420
421 spin_unlock(&lock);
422}
423
424/* Write a GDT descriptor entry. Ignore LDT descriptors, since
425 they're handled differently. */
426static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
427 u32 low, u32 high)
428{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700429 preempt_disable();
430
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700431 switch ((high >> 8) & 0xff) {
432 case DESCTYPE_LDT:
433 case DESCTYPE_TSS:
434 /* ignore */
435 break;
436
437 default: {
438 xmaddr_t maddr = virt_to_machine(&dt[entry]);
439 u64 desc = (u64)high << 32 | low;
440
441 xen_mc_flush();
442 if (HYPERVISOR_update_descriptor(maddr.maddr, desc))
443 BUG();
444 }
445
446 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700447
448 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700449}
450
451static void xen_load_esp0(struct tss_struct *tss,
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700452 struct thread_struct *thread)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700453{
454 struct multicall_space mcs = xen_mc_entry(0);
455 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0);
456 xen_mc_issue(PARAVIRT_LAZY_CPU);
457}
458
459static void xen_set_iopl_mask(unsigned mask)
460{
461 struct physdev_set_iopl set_iopl;
462
463 /* Force the change at ring 0. */
464 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
465 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
466}
467
468static void xen_io_delay(void)
469{
470}
471
472#ifdef CONFIG_X86_LOCAL_APIC
473static unsigned long xen_apic_read(unsigned long reg)
474{
475 return 0;
476}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700477
478static void xen_apic_write(unsigned long reg, unsigned long val)
479{
480 /* Warn to see if there's any stray references */
481 WARN_ON(1);
482}
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700483#endif
484
485static void xen_flush_tlb(void)
486{
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700487 struct mmuext_op *op;
488 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700489
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700490 op = mcs.args;
491 op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
492 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
493
494 xen_mc_issue(PARAVIRT_LAZY_MMU);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700495}
496
497static void xen_flush_tlb_single(unsigned long addr)
498{
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700499 struct mmuext_op *op;
500 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700501
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700502 op = mcs.args;
503 op->cmd = MMUEXT_INVLPG_LOCAL;
504 op->arg1.linear_addr = addr & PAGE_MASK;
505 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
506
507 xen_mc_issue(PARAVIRT_LAZY_MMU);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700508}
509
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700510static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
511 unsigned long va)
512{
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700513 struct {
514 struct mmuext_op op;
515 cpumask_t mask;
516 } *args;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700517 cpumask_t cpumask = *cpus;
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700518 struct multicall_space mcs;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700519
520 /*
521 * A couple of (to be removed) sanity checks:
522 *
523 * - current CPU must not be in mask
524 * - mask must exist :)
525 */
526 BUG_ON(cpus_empty(cpumask));
527 BUG_ON(cpu_isset(smp_processor_id(), cpumask));
528 BUG_ON(!mm);
529
530 /* If a CPU which we ran on has gone down, OK. */
531 cpus_and(cpumask, cpumask, cpu_online_map);
532 if (cpus_empty(cpumask))
533 return;
534
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700535 mcs = xen_mc_entry(sizeof(*args));
536 args = mcs.args;
537 args->mask = cpumask;
538 args->op.arg2.vcpumask = &args->mask;
539
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700540 if (va == TLB_FLUSH_ALL) {
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700541 args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700542 } else {
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700543 args->op.cmd = MMUEXT_INVLPG_MULTI;
544 args->op.arg1.linear_addr = va;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700545 }
546
Jeremy Fitzhardinged66bf8f2007-07-17 18:37:06 -0700547 MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
548
549 xen_mc_issue(PARAVIRT_LAZY_MMU);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700550}
551
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700552static unsigned long xen_read_cr2(void)
553{
554 return x86_read_percpu(xen_vcpu)->arch.cr2;
555}
556
557static void xen_write_cr4(unsigned long cr4)
558{
559 /* never allow TSC to be disabled */
560 native_write_cr4(cr4 & ~X86_CR4_TSD);
561}
562
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700563static unsigned long xen_read_cr3(void)
564{
565 return x86_read_percpu(xen_cr3);
566}
567
568static void xen_write_cr3(unsigned long cr3)
569{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700570 BUG_ON(preemptible());
571
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700572 if (cr3 == x86_read_percpu(xen_cr3)) {
573 /* just a simple tlb flush */
574 xen_flush_tlb();
575 return;
576 }
577
578 x86_write_percpu(xen_cr3, cr3);
579
580
581 {
582 struct mmuext_op *op;
583 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
584 unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
585
586 op = mcs.args;
587 op->cmd = MMUEXT_NEW_BASEPTR;
588 op->arg1.mfn = mfn;
589
590 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
591
592 xen_mc_issue(PARAVIRT_LAZY_CPU);
593 }
594}
595
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700596/* Early in boot, while setting up the initial pagetable, assume
597 everything is pinned. */
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700598static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn)
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700599{
600 BUG_ON(mem_map); /* should only be used early */
601 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
602}
603
604/* This needs to make sure the new pte page is pinned iff its being
605 attached to a pinned pagetable. */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700606static void xen_alloc_pt(struct mm_struct *mm, u32 pfn)
607{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700608 struct page *page = pfn_to_page(pfn);
609
610 if (PagePinned(virt_to_page(mm->pgd))) {
611 SetPagePinned(page);
612
613 if (!PageHighMem(page))
614 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
615 else
616 /* make sure there are no stray mappings of
617 this page */
618 kmap_flush_unused();
619 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700620}
621
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700622/* This should never happen until we're OK to use struct page */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700623static void xen_release_pt(u32 pfn)
624{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700625 struct page *page = pfn_to_page(pfn);
626
627 if (PagePinned(page)) {
628 if (!PageHighMem(page))
629 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
630 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700631}
632
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700633#ifdef CONFIG_HIGHPTE
634static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700635{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700636 pgprot_t prot = PAGE_KERNEL;
637
638 if (PagePinned(page))
639 prot = PAGE_KERNEL_RO;
640
641 if (0 && PageHighMem(page))
642 printk("mapping highpte %lx type %d prot %s\n",
643 page_to_pfn(page), type,
644 (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
645
646 return kmap_atomic_prot(page, type, prot);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700647}
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700648#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700649
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700650static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
651{
652 /* If there's an existing pte, then don't allow _PAGE_RW to be set */
653 if (pte_val_ma(*ptep) & _PAGE_PRESENT)
654 pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
655 pte_val_ma(pte));
656
657 return pte;
658}
659
660/* Init-time set_pte while constructing initial pagetables, which
661 doesn't allow RO pagetable pages to be remapped RW */
662static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
663{
664 pte = mask_rw_pte(ptep, pte);
665
666 xen_set_pte(ptep, pte);
667}
668
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700669static __init void xen_pagetable_setup_start(pgd_t *base)
670{
671 pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
672
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700673 /* special set_pte for pagetable initialization */
674 paravirt_ops.set_pte = xen_set_pte_init;
675
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700676 init_mm.pgd = base;
677 /*
678 * copy top-level of Xen-supplied pagetable into place. For
679 * !PAE we can use this as-is, but for PAE it is a stand-in
680 * while we copy the pmd pages.
681 */
682 memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
683
684 if (PTRS_PER_PMD > 1) {
685 int i;
686 /*
687 * For PAE, need to allocate new pmds, rather than
688 * share Xen's, since Xen doesn't like pmd's being
689 * shared between address spaces.
690 */
691 for (i = 0; i < PTRS_PER_PGD; i++) {
692 if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
693 pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
694
695 memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
696 PAGE_SIZE);
697
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700698 make_lowmem_page_readonly(pmd);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700699
700 set_pgd(&base[i], __pgd(1 + __pa(pmd)));
701 } else
702 pgd_clear(&base[i]);
703 }
704 }
705
706 /* make sure zero_page is mapped RO so we can use it in pagetables */
707 make_lowmem_page_readonly(empty_zero_page);
708 make_lowmem_page_readonly(base);
709 /*
710 * Switch to new pagetable. This is done before
711 * pagetable_init has done anything so that the new pages
712 * added to the table can be prepared properly for Xen.
713 */
714 xen_write_cr3(__pa(base));
715}
716
717static __init void xen_pagetable_setup_done(pgd_t *base)
718{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700719 /* This will work as long as patching hasn't happened yet
720 (which it hasn't) */
721 paravirt_ops.alloc_pt = xen_alloc_pt;
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700722 paravirt_ops.set_pte = xen_set_pte;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700723
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700724 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
725 /*
726 * Create a mapping for the shared info page.
727 * Should be set_fixmap(), but shared_info is a machine
728 * address with no corresponding pseudo-phys address.
729 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700730 set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
731 PFN_DOWN(xen_start_info->shared_info),
732 PAGE_KERNEL);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700733
734 HYPERVISOR_shared_info =
735 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
736
737 } else
738 HYPERVISOR_shared_info =
739 (struct shared_info *)__va(xen_start_info->shared_info);
740
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700741 /* Actually pin the pagetable down, but we can't set PG_pinned
742 yet because the page structures don't exist yet. */
743 {
744 struct mmuext_op op;
745#ifdef CONFIG_X86_PAE
746 op.cmd = MMUEXT_PIN_L3_TABLE;
747#else
748 op.cmd = MMUEXT_PIN_L3_TABLE;
749#endif
750 op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base)));
751 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
752 BUG();
753 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700754
755 xen_vcpu_setup(smp_processor_id());
756}
757
758static const struct paravirt_ops xen_paravirt_ops __initdata = {
759 .paravirt_enabled = 1,
760 .shared_kernel_pmd = 0,
761
762 .name = "Xen",
763 .banner = xen_banner,
764
765 .patch = paravirt_patch_default,
766
767 .memory_setup = xen_memory_setup,
768 .arch_setup = xen_arch_setup,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700769 .init_IRQ = xen_init_IRQ,
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700770 .post_allocator_init = xen_mark_init_mm_pinned,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700771
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700772 .time_init = xen_time_init,
773 .set_wallclock = xen_set_wallclock,
774 .get_wallclock = xen_get_wallclock,
775 .get_cpu_khz = xen_cpu_khz,
Jeremy Fitzhardingeab550282007-07-17 18:37:05 -0700776 .sched_clock = xen_sched_clock,
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700777
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700778 .cpuid = xen_cpuid,
779
780 .set_debugreg = xen_set_debugreg,
781 .get_debugreg = xen_get_debugreg,
782
783 .clts = native_clts,
784
785 .read_cr0 = native_read_cr0,
786 .write_cr0 = native_write_cr0,
787
788 .read_cr2 = xen_read_cr2,
789 .write_cr2 = native_write_cr2,
790
791 .read_cr3 = xen_read_cr3,
792 .write_cr3 = xen_write_cr3,
793
794 .read_cr4 = native_read_cr4,
795 .read_cr4_safe = native_read_cr4_safe,
796 .write_cr4 = xen_write_cr4,
797
798 .save_fl = xen_save_fl,
799 .restore_fl = xen_restore_fl,
800 .irq_disable = xen_irq_disable,
801 .irq_enable = xen_irq_enable,
802 .safe_halt = xen_safe_halt,
803 .halt = xen_halt,
804 .wbinvd = native_wbinvd,
805
806 .read_msr = native_read_msr_safe,
807 .write_msr = native_write_msr_safe,
808 .read_tsc = native_read_tsc,
809 .read_pmc = native_read_pmc,
810
811 .iret = (void *)&hypercall_page[__HYPERVISOR_iret],
812 .irq_enable_sysexit = NULL, /* never called */
813
814 .load_tr_desc = paravirt_nop,
815 .set_ldt = xen_set_ldt,
816 .load_gdt = xen_load_gdt,
817 .load_idt = xen_load_idt,
818 .load_tls = xen_load_tls,
819
820 .store_gdt = native_store_gdt,
821 .store_idt = native_store_idt,
822 .store_tr = xen_store_tr,
823
824 .write_ldt_entry = xen_write_ldt_entry,
825 .write_gdt_entry = xen_write_gdt_entry,
826 .write_idt_entry = xen_write_idt_entry,
827 .load_esp0 = xen_load_esp0,
828
829 .set_iopl_mask = xen_set_iopl_mask,
830 .io_delay = xen_io_delay,
831
832#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700833 .apic_write = xen_apic_write,
834 .apic_write_atomic = xen_apic_write,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700835 .apic_read = xen_apic_read,
836 .setup_boot_clock = paravirt_nop,
837 .setup_secondary_clock = paravirt_nop,
838 .startup_ipi_hook = paravirt_nop,
839#endif
840
841 .flush_tlb_user = xen_flush_tlb,
842 .flush_tlb_kernel = xen_flush_tlb,
843 .flush_tlb_single = xen_flush_tlb_single,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700844 .flush_tlb_others = xen_flush_tlb_others,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700845
846 .pte_update = paravirt_nop,
847 .pte_update_defer = paravirt_nop,
848
849 .pagetable_setup_start = xen_pagetable_setup_start,
850 .pagetable_setup_done = xen_pagetable_setup_done,
851
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700852 .alloc_pt = xen_alloc_pt_init,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700853 .release_pt = xen_release_pt,
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700854 .alloc_pd = paravirt_nop,
855 .alloc_pd_clone = paravirt_nop,
856 .release_pd = paravirt_nop,
857
858#ifdef CONFIG_HIGHPTE
859 .kmap_atomic_pte = xen_kmap_atomic_pte,
860#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700861
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700862 .set_pte = NULL, /* see xen_pagetable_setup_* */
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700863 .set_pte_at = xen_set_pte_at,
864 .set_pmd = xen_set_pmd,
865
866 .pte_val = xen_pte_val,
867 .pgd_val = xen_pgd_val,
868
869 .make_pte = xen_make_pte,
870 .make_pgd = xen_make_pgd,
871
872#ifdef CONFIG_X86_PAE
873 .set_pte_atomic = xen_set_pte_atomic,
874 .set_pte_present = xen_set_pte_at,
875 .set_pud = xen_set_pud,
876 .pte_clear = xen_pte_clear,
877 .pmd_clear = xen_pmd_clear,
878
879 .make_pmd = xen_make_pmd,
880 .pmd_val = xen_pmd_val,
881#endif /* PAE */
882
883 .activate_mm = xen_activate_mm,
884 .dup_mmap = xen_dup_mmap,
885 .exit_mmap = xen_exit_mmap,
886
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700887 .set_lazy_mode = xen_set_lazy_mode,
888};
889
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700890#ifdef CONFIG_SMP
891static const struct smp_ops xen_smp_ops __initdata = {
892 .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
893 .smp_prepare_cpus = xen_smp_prepare_cpus,
894 .cpu_up = xen_cpu_up,
895 .smp_cpus_done = xen_smp_cpus_done,
896
897 .smp_send_stop = xen_smp_send_stop,
898 .smp_send_reschedule = xen_smp_send_reschedule,
899 .smp_call_function_mask = xen_smp_call_function_mask,
900};
901#endif /* CONFIG_SMP */
902
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700903/* First C function to be called on Xen boot */
904asmlinkage void __init xen_start_kernel(void)
905{
906 pgd_t *pgd;
907
908 if (!xen_start_info)
909 return;
910
911 BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0);
912
913 /* Install Xen paravirt ops */
914 paravirt_ops = xen_paravirt_ops;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700915#ifdef CONFIG_SMP
916 smp_ops = xen_smp_ops;
917#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700918
919 xen_setup_features();
920
921 /* Get mfn list */
922 if (!xen_feature(XENFEAT_auto_translated_physmap))
923 phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
924
925 pgd = (pgd_t *)xen_start_info->pt_base;
926
927 init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
928
929 init_mm.pgd = pgd; /* use the Xen pagetables to start */
930
931 /* keep using Xen gdt for now; no urgent need to change it */
932
933 x86_write_percpu(xen_cr3, __pa(pgd));
934 xen_vcpu_setup(0);
935
936 paravirt_ops.kernel_rpl = 1;
937 if (xen_feature(XENFEAT_supervisor_mode_kernel))
938 paravirt_ops.kernel_rpl = 0;
939
940 /* set the limit of our address space */
941 reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE);
942
943 /* set up basic CPUID stuff */
944 cpu_detect(&new_cpu_data);
945 new_cpu_data.hard_math = 1;
946 new_cpu_data.x86_capability[0] = cpuid_edx(1);
947
948 /* Poke various useful things into boot_params */
949 LOADER_TYPE = (9 << 4) | 0;
950 INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0;
951 INITRD_SIZE = xen_start_info->mod_len;
952
953 /* Start the world */
954 start_kernel();
955}