blob: de62d66e08934172658f8aa3ebbe4fc126e7d34d [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>
18#include <linux/percpu.h>
19#include <linux/delay.h>
20#include <linux/start_kernel.h>
21#include <linux/sched.h>
22#include <linux/bootmem.h>
23#include <linux/module.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070024#include <linux/mm.h>
25#include <linux/page-flags.h>
26#include <linux/highmem.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070027#include <linux/smp.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070028
29#include <xen/interface/xen.h>
30#include <xen/interface/physdev.h>
31#include <xen/interface/vcpu.h>
32#include <xen/features.h>
33#include <xen/page.h>
34
35#include <asm/paravirt.h>
36#include <asm/page.h>
37#include <asm/xen/hypercall.h>
38#include <asm/xen/hypervisor.h>
39#include <asm/fixmap.h>
40#include <asm/processor.h>
41#include <asm/setup.h>
42#include <asm/desc.h>
43#include <asm/pgtable.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070044#include <asm/tlbflush.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070045
46#include "xen-ops.h"
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070047#include "mmu.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070048#include "multicalls.h"
49
50EXPORT_SYMBOL_GPL(hypercall_page);
51
52DEFINE_PER_CPU(enum paravirt_lazy_mode, xen_lazy_mode);
53
54DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
55DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
56DEFINE_PER_CPU(unsigned long, xen_cr3);
57
58struct start_info *xen_start_info;
59EXPORT_SYMBOL_GPL(xen_start_info);
60
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070061void xen_vcpu_setup(int cpu)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070062{
63 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
64}
65
66static void __init xen_banner(void)
67{
68 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
69 paravirt_ops.name);
70 printk(KERN_INFO "Hypervisor signature: %s\n", xen_start_info->magic);
71}
72
73static void xen_cpuid(unsigned int *eax, unsigned int *ebx,
74 unsigned int *ecx, unsigned int *edx)
75{
76 unsigned maskedx = ~0;
77
78 /*
79 * Mask out inconvenient features, to try and disable as many
80 * unsupported kernel subsystems as possible.
81 */
82 if (*eax == 1)
83 maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
84 (1 << X86_FEATURE_ACPI) | /* disable ACPI */
85 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
86
87 asm(XEN_EMULATE_PREFIX "cpuid"
88 : "=a" (*eax),
89 "=b" (*ebx),
90 "=c" (*ecx),
91 "=d" (*edx)
92 : "0" (*eax), "2" (*ecx));
93 *edx &= maskedx;
94}
95
96static void xen_set_debugreg(int reg, unsigned long val)
97{
98 HYPERVISOR_set_debugreg(reg, val);
99}
100
101static unsigned long xen_get_debugreg(int reg)
102{
103 return HYPERVISOR_get_debugreg(reg);
104}
105
106static unsigned long xen_save_fl(void)
107{
108 struct vcpu_info *vcpu;
109 unsigned long flags;
110
111 preempt_disable();
112 vcpu = x86_read_percpu(xen_vcpu);
113 /* flag has opposite sense of mask */
114 flags = !vcpu->evtchn_upcall_mask;
115 preempt_enable();
116
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
128 preempt_disable();
129
130 /* convert from IF type flag */
131 flags = !(flags & X86_EFLAGS_IF);
132 vcpu = x86_read_percpu(xen_vcpu);
133 vcpu->evtchn_upcall_mask = flags;
134
135 if (flags == 0) {
136 /* Unmask then check (avoid races). We're only protecting
137 against updates by this CPU, so there's no need for
138 anything stronger. */
139 barrier();
140
141 if (unlikely(vcpu->evtchn_upcall_pending))
142 force_evtchn_callback();
143 preempt_enable();
144 } else
145 preempt_enable_no_resched();
146}
147
148static void xen_irq_disable(void)
149{
150 struct vcpu_info *vcpu;
151 preempt_disable();
152 vcpu = x86_read_percpu(xen_vcpu);
153 vcpu->evtchn_upcall_mask = 1;
154 preempt_enable_no_resched();
155}
156
157static void xen_irq_enable(void)
158{
159 struct vcpu_info *vcpu;
160
161 preempt_disable();
162 vcpu = x86_read_percpu(xen_vcpu);
163 vcpu->evtchn_upcall_mask = 0;
164
165 /* Unmask then check (avoid races). We're only protecting
166 against updates by this CPU, so there's no need for
167 anything stronger. */
168 barrier();
169
170 if (unlikely(vcpu->evtchn_upcall_pending))
171 force_evtchn_callback();
172 preempt_enable();
173}
174
175static void xen_safe_halt(void)
176{
177 /* Blocking includes an implicit local_irq_enable(). */
178 if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0)
179 BUG();
180}
181
182static void xen_halt(void)
183{
184 if (irqs_disabled())
185 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
186 else
187 xen_safe_halt();
188}
189
190static void xen_set_lazy_mode(enum paravirt_lazy_mode mode)
191{
192 switch (mode) {
193 case PARAVIRT_LAZY_NONE:
194 BUG_ON(x86_read_percpu(xen_lazy_mode) == PARAVIRT_LAZY_NONE);
195 break;
196
197 case PARAVIRT_LAZY_MMU:
198 case PARAVIRT_LAZY_CPU:
199 BUG_ON(x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE);
200 break;
201
202 case PARAVIRT_LAZY_FLUSH:
203 /* flush if necessary, but don't change state */
204 if (x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE)
205 xen_mc_flush();
206 return;
207 }
208
209 xen_mc_flush();
210 x86_write_percpu(xen_lazy_mode, mode);
211}
212
213static unsigned long xen_store_tr(void)
214{
215 return 0;
216}
217
218static void xen_set_ldt(const void *addr, unsigned entries)
219{
220 unsigned long linear_addr = (unsigned long)addr;
221 struct mmuext_op *op;
222 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
223
224 op = mcs.args;
225 op->cmd = MMUEXT_SET_LDT;
226 if (linear_addr) {
227 /* ldt my be vmalloced, use arbitrary_virt_to_machine */
228 xmaddr_t maddr;
229 maddr = arbitrary_virt_to_machine((unsigned long)addr);
230 linear_addr = (unsigned long)maddr.maddr;
231 }
232 op->arg1.linear_addr = linear_addr;
233 op->arg2.nr_ents = entries;
234
235 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
236
237 xen_mc_issue(PARAVIRT_LAZY_CPU);
238}
239
240static void xen_load_gdt(const struct Xgt_desc_struct *dtr)
241{
242 unsigned long *frames;
243 unsigned long va = dtr->address;
244 unsigned int size = dtr->size + 1;
245 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
246 int f;
247 struct multicall_space mcs;
248
249 /* A GDT can be up to 64k in size, which corresponds to 8192
250 8-byte entries, or 16 4k pages.. */
251
252 BUG_ON(size > 65536);
253 BUG_ON(va & ~PAGE_MASK);
254
255 mcs = xen_mc_entry(sizeof(*frames) * pages);
256 frames = mcs.args;
257
258 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
259 frames[f] = virt_to_mfn(va);
260 make_lowmem_page_readonly((void *)va);
261 }
262
263 MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
264
265 xen_mc_issue(PARAVIRT_LAZY_CPU);
266}
267
268static void load_TLS_descriptor(struct thread_struct *t,
269 unsigned int cpu, unsigned int i)
270{
271 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
272 xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
273 struct multicall_space mc = __xen_mc_entry(0);
274
275 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
276}
277
278static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
279{
280 xen_mc_batch();
281
282 load_TLS_descriptor(t, cpu, 0);
283 load_TLS_descriptor(t, cpu, 1);
284 load_TLS_descriptor(t, cpu, 2);
285
286 xen_mc_issue(PARAVIRT_LAZY_CPU);
287}
288
289static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
290 u32 low, u32 high)
291{
292 unsigned long lp = (unsigned long)&dt[entrynum];
293 xmaddr_t mach_lp = virt_to_machine(lp);
294 u64 entry = (u64)high << 32 | low;
295
296 xen_mc_flush();
297 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
298 BUG();
299}
300
301static int cvt_gate_to_trap(int vector, u32 low, u32 high,
302 struct trap_info *info)
303{
304 u8 type, dpl;
305
306 type = (high >> 8) & 0x1f;
307 dpl = (high >> 13) & 3;
308
309 if (type != 0xf && type != 0xe)
310 return 0;
311
312 info->vector = vector;
313 info->address = (high & 0xffff0000) | (low & 0x0000ffff);
314 info->cs = low >> 16;
315 info->flags = dpl;
316 /* interrupt gates clear IF */
317 if (type == 0xe)
318 info->flags |= 4;
319
320 return 1;
321}
322
323/* Locations of each CPU's IDT */
324static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc);
325
326/* Set an IDT entry. If the entry is part of the current IDT, then
327 also update Xen. */
328static void xen_write_idt_entry(struct desc_struct *dt, int entrynum,
329 u32 low, u32 high)
330{
331
332 int cpu = smp_processor_id();
333 unsigned long p = (unsigned long)&dt[entrynum];
334 unsigned long start = per_cpu(idt_desc, cpu).address;
335 unsigned long end = start + per_cpu(idt_desc, cpu).size + 1;
336
337 xen_mc_flush();
338
339 write_dt_entry(dt, entrynum, low, high);
340
341 if (p >= start && (p + 8) <= end) {
342 struct trap_info info[2];
343
344 info[1].address = 0;
345
346 if (cvt_gate_to_trap(entrynum, low, high, &info[0]))
347 if (HYPERVISOR_set_trap_table(info))
348 BUG();
349 }
350}
351
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700352static void xen_convert_trap_info(const struct Xgt_desc_struct *desc,
353 struct trap_info *traps)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700354{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700355 unsigned in, out, count;
356
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700357 count = (desc->size+1) / 8;
358 BUG_ON(count > 256);
359
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700360 for (in = out = 0; in < count; in++) {
361 const u32 *entry = (u32 *)(desc->address + in * 8);
362
363 if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
364 out++;
365 }
366 traps[out].address = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700367}
368
369void xen_copy_trap_info(struct trap_info *traps)
370{
371 const struct Xgt_desc_struct *desc = &get_cpu_var(idt_desc);
372
373 xen_convert_trap_info(desc, traps);
374
375 put_cpu_var(idt_desc);
376}
377
378/* Load a new IDT into Xen. In principle this can be per-CPU, so we
379 hold a spinlock to protect the static traps[] array (static because
380 it avoids allocation, and saves stack space). */
381static void xen_load_idt(const struct Xgt_desc_struct *desc)
382{
383 static DEFINE_SPINLOCK(lock);
384 static struct trap_info traps[257];
385 int cpu = smp_processor_id();
386
387 per_cpu(idt_desc, cpu) = *desc;
388
389 spin_lock(&lock);
390
391 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700392
393 xen_mc_flush();
394 if (HYPERVISOR_set_trap_table(traps))
395 BUG();
396
397 spin_unlock(&lock);
398}
399
400/* Write a GDT descriptor entry. Ignore LDT descriptors, since
401 they're handled differently. */
402static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
403 u32 low, u32 high)
404{
405 switch ((high >> 8) & 0xff) {
406 case DESCTYPE_LDT:
407 case DESCTYPE_TSS:
408 /* ignore */
409 break;
410
411 default: {
412 xmaddr_t maddr = virt_to_machine(&dt[entry]);
413 u64 desc = (u64)high << 32 | low;
414
415 xen_mc_flush();
416 if (HYPERVISOR_update_descriptor(maddr.maddr, desc))
417 BUG();
418 }
419
420 }
421}
422
423static void xen_load_esp0(struct tss_struct *tss,
424 struct thread_struct *thread)
425{
426 struct multicall_space mcs = xen_mc_entry(0);
427 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0);
428 xen_mc_issue(PARAVIRT_LAZY_CPU);
429}
430
431static void xen_set_iopl_mask(unsigned mask)
432{
433 struct physdev_set_iopl set_iopl;
434
435 /* Force the change at ring 0. */
436 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
437 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
438}
439
440static void xen_io_delay(void)
441{
442}
443
444#ifdef CONFIG_X86_LOCAL_APIC
445static unsigned long xen_apic_read(unsigned long reg)
446{
447 return 0;
448}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700449
450static void xen_apic_write(unsigned long reg, unsigned long val)
451{
452 /* Warn to see if there's any stray references */
453 WARN_ON(1);
454}
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700455#endif
456
457static void xen_flush_tlb(void)
458{
459 struct mmuext_op op;
460
461 op.cmd = MMUEXT_TLB_FLUSH_LOCAL;
462 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
463 BUG();
464}
465
466static void xen_flush_tlb_single(unsigned long addr)
467{
468 struct mmuext_op op;
469
470 op.cmd = MMUEXT_INVLPG_LOCAL;
471 op.arg1.linear_addr = addr & PAGE_MASK;
472 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
473 BUG();
474}
475
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700476static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
477 unsigned long va)
478{
479 struct mmuext_op op;
480 cpumask_t cpumask = *cpus;
481
482 /*
483 * A couple of (to be removed) sanity checks:
484 *
485 * - current CPU must not be in mask
486 * - mask must exist :)
487 */
488 BUG_ON(cpus_empty(cpumask));
489 BUG_ON(cpu_isset(smp_processor_id(), cpumask));
490 BUG_ON(!mm);
491
492 /* If a CPU which we ran on has gone down, OK. */
493 cpus_and(cpumask, cpumask, cpu_online_map);
494 if (cpus_empty(cpumask))
495 return;
496
497 if (va == TLB_FLUSH_ALL) {
498 op.cmd = MMUEXT_TLB_FLUSH_MULTI;
499 op.arg2.vcpumask = (void *)cpus;
500 } else {
501 op.cmd = MMUEXT_INVLPG_MULTI;
502 op.arg1.linear_addr = va;
503 op.arg2.vcpumask = (void *)cpus;
504 }
505
506 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
507 BUG();
508}
509
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700510static unsigned long xen_read_cr2(void)
511{
512 return x86_read_percpu(xen_vcpu)->arch.cr2;
513}
514
515static void xen_write_cr4(unsigned long cr4)
516{
517 /* never allow TSC to be disabled */
518 native_write_cr4(cr4 & ~X86_CR4_TSD);
519}
520
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700521static unsigned long xen_read_cr3(void)
522{
523 return x86_read_percpu(xen_cr3);
524}
525
526static void xen_write_cr3(unsigned long cr3)
527{
528 if (cr3 == x86_read_percpu(xen_cr3)) {
529 /* just a simple tlb flush */
530 xen_flush_tlb();
531 return;
532 }
533
534 x86_write_percpu(xen_cr3, cr3);
535
536
537 {
538 struct mmuext_op *op;
539 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
540 unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
541
542 op = mcs.args;
543 op->cmd = MMUEXT_NEW_BASEPTR;
544 op->arg1.mfn = mfn;
545
546 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
547
548 xen_mc_issue(PARAVIRT_LAZY_CPU);
549 }
550}
551
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700552/* Early in boot, while setting up the initial pagetable, assume
553 everything is pinned. */
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700554static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn)
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700555{
556 BUG_ON(mem_map); /* should only be used early */
557 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
558}
559
560/* This needs to make sure the new pte page is pinned iff its being
561 attached to a pinned pagetable. */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700562static void xen_alloc_pt(struct mm_struct *mm, u32 pfn)
563{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700564 struct page *page = pfn_to_page(pfn);
565
566 if (PagePinned(virt_to_page(mm->pgd))) {
567 SetPagePinned(page);
568
569 if (!PageHighMem(page))
570 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
571 else
572 /* make sure there are no stray mappings of
573 this page */
574 kmap_flush_unused();
575 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700576}
577
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700578/* This should never happen until we're OK to use struct page */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700579static void xen_release_pt(u32 pfn)
580{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700581 struct page *page = pfn_to_page(pfn);
582
583 if (PagePinned(page)) {
584 if (!PageHighMem(page))
585 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
586 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700587}
588
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700589#ifdef CONFIG_HIGHPTE
590static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700591{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700592 pgprot_t prot = PAGE_KERNEL;
593
594 if (PagePinned(page))
595 prot = PAGE_KERNEL_RO;
596
597 if (0 && PageHighMem(page))
598 printk("mapping highpte %lx type %d prot %s\n",
599 page_to_pfn(page), type,
600 (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
601
602 return kmap_atomic_prot(page, type, prot);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700603}
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700604#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700605
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700606static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
607{
608 /* If there's an existing pte, then don't allow _PAGE_RW to be set */
609 if (pte_val_ma(*ptep) & _PAGE_PRESENT)
610 pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
611 pte_val_ma(pte));
612
613 return pte;
614}
615
616/* Init-time set_pte while constructing initial pagetables, which
617 doesn't allow RO pagetable pages to be remapped RW */
618static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
619{
620 pte = mask_rw_pte(ptep, pte);
621
622 xen_set_pte(ptep, pte);
623}
624
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700625static __init void xen_pagetable_setup_start(pgd_t *base)
626{
627 pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
628
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700629 /* special set_pte for pagetable initialization */
630 paravirt_ops.set_pte = xen_set_pte_init;
631
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700632 init_mm.pgd = base;
633 /*
634 * copy top-level of Xen-supplied pagetable into place. For
635 * !PAE we can use this as-is, but for PAE it is a stand-in
636 * while we copy the pmd pages.
637 */
638 memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
639
640 if (PTRS_PER_PMD > 1) {
641 int i;
642 /*
643 * For PAE, need to allocate new pmds, rather than
644 * share Xen's, since Xen doesn't like pmd's being
645 * shared between address spaces.
646 */
647 for (i = 0; i < PTRS_PER_PGD; i++) {
648 if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
649 pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
650
651 memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
652 PAGE_SIZE);
653
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700654 make_lowmem_page_readonly(pmd);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700655
656 set_pgd(&base[i], __pgd(1 + __pa(pmd)));
657 } else
658 pgd_clear(&base[i]);
659 }
660 }
661
662 /* make sure zero_page is mapped RO so we can use it in pagetables */
663 make_lowmem_page_readonly(empty_zero_page);
664 make_lowmem_page_readonly(base);
665 /*
666 * Switch to new pagetable. This is done before
667 * pagetable_init has done anything so that the new pages
668 * added to the table can be prepared properly for Xen.
669 */
670 xen_write_cr3(__pa(base));
671}
672
673static __init void xen_pagetable_setup_done(pgd_t *base)
674{
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700675 /* This will work as long as patching hasn't happened yet
676 (which it hasn't) */
677 paravirt_ops.alloc_pt = xen_alloc_pt;
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700678 paravirt_ops.set_pte = xen_set_pte;
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700679
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700680 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
681 /*
682 * Create a mapping for the shared info page.
683 * Should be set_fixmap(), but shared_info is a machine
684 * address with no corresponding pseudo-phys address.
685 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700686 set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
687 PFN_DOWN(xen_start_info->shared_info),
688 PAGE_KERNEL);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700689
690 HYPERVISOR_shared_info =
691 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
692
693 } else
694 HYPERVISOR_shared_info =
695 (struct shared_info *)__va(xen_start_info->shared_info);
696
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700697 /* Actually pin the pagetable down, but we can't set PG_pinned
698 yet because the page structures don't exist yet. */
699 {
700 struct mmuext_op op;
701#ifdef CONFIG_X86_PAE
702 op.cmd = MMUEXT_PIN_L3_TABLE;
703#else
704 op.cmd = MMUEXT_PIN_L3_TABLE;
705#endif
706 op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base)));
707 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
708 BUG();
709 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700710
711 xen_vcpu_setup(smp_processor_id());
712}
713
714static const struct paravirt_ops xen_paravirt_ops __initdata = {
715 .paravirt_enabled = 1,
716 .shared_kernel_pmd = 0,
717
718 .name = "Xen",
719 .banner = xen_banner,
720
721 .patch = paravirt_patch_default,
722
723 .memory_setup = xen_memory_setup,
724 .arch_setup = xen_arch_setup,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700725 .init_IRQ = xen_init_IRQ,
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700726 .post_allocator_init = xen_mark_init_mm_pinned,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700727
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700728 .time_init = xen_time_init,
729 .set_wallclock = xen_set_wallclock,
730 .get_wallclock = xen_get_wallclock,
731 .get_cpu_khz = xen_cpu_khz,
Jeremy Fitzhardingeab550282007-07-17 18:37:05 -0700732 .sched_clock = xen_sched_clock,
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700733
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700734 .cpuid = xen_cpuid,
735
736 .set_debugreg = xen_set_debugreg,
737 .get_debugreg = xen_get_debugreg,
738
739 .clts = native_clts,
740
741 .read_cr0 = native_read_cr0,
742 .write_cr0 = native_write_cr0,
743
744 .read_cr2 = xen_read_cr2,
745 .write_cr2 = native_write_cr2,
746
747 .read_cr3 = xen_read_cr3,
748 .write_cr3 = xen_write_cr3,
749
750 .read_cr4 = native_read_cr4,
751 .read_cr4_safe = native_read_cr4_safe,
752 .write_cr4 = xen_write_cr4,
753
754 .save_fl = xen_save_fl,
755 .restore_fl = xen_restore_fl,
756 .irq_disable = xen_irq_disable,
757 .irq_enable = xen_irq_enable,
758 .safe_halt = xen_safe_halt,
759 .halt = xen_halt,
760 .wbinvd = native_wbinvd,
761
762 .read_msr = native_read_msr_safe,
763 .write_msr = native_write_msr_safe,
764 .read_tsc = native_read_tsc,
765 .read_pmc = native_read_pmc,
766
767 .iret = (void *)&hypercall_page[__HYPERVISOR_iret],
768 .irq_enable_sysexit = NULL, /* never called */
769
770 .load_tr_desc = paravirt_nop,
771 .set_ldt = xen_set_ldt,
772 .load_gdt = xen_load_gdt,
773 .load_idt = xen_load_idt,
774 .load_tls = xen_load_tls,
775
776 .store_gdt = native_store_gdt,
777 .store_idt = native_store_idt,
778 .store_tr = xen_store_tr,
779
780 .write_ldt_entry = xen_write_ldt_entry,
781 .write_gdt_entry = xen_write_gdt_entry,
782 .write_idt_entry = xen_write_idt_entry,
783 .load_esp0 = xen_load_esp0,
784
785 .set_iopl_mask = xen_set_iopl_mask,
786 .io_delay = xen_io_delay,
787
788#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700789 .apic_write = xen_apic_write,
790 .apic_write_atomic = xen_apic_write,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700791 .apic_read = xen_apic_read,
792 .setup_boot_clock = paravirt_nop,
793 .setup_secondary_clock = paravirt_nop,
794 .startup_ipi_hook = paravirt_nop,
795#endif
796
797 .flush_tlb_user = xen_flush_tlb,
798 .flush_tlb_kernel = xen_flush_tlb,
799 .flush_tlb_single = xen_flush_tlb_single,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700800 .flush_tlb_others = xen_flush_tlb_others,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700801
802 .pte_update = paravirt_nop,
803 .pte_update_defer = paravirt_nop,
804
805 .pagetable_setup_start = xen_pagetable_setup_start,
806 .pagetable_setup_done = xen_pagetable_setup_done,
807
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700808 .alloc_pt = xen_alloc_pt_init,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700809 .release_pt = xen_release_pt,
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -0700810 .alloc_pd = paravirt_nop,
811 .alloc_pd_clone = paravirt_nop,
812 .release_pd = paravirt_nop,
813
814#ifdef CONFIG_HIGHPTE
815 .kmap_atomic_pte = xen_kmap_atomic_pte,
816#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700817
Jeremy Fitzhardinge9a4029f2007-07-17 18:37:05 -0700818 .set_pte = NULL, /* see xen_pagetable_setup_* */
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -0700819 .set_pte_at = xen_set_pte_at,
820 .set_pmd = xen_set_pmd,
821
822 .pte_val = xen_pte_val,
823 .pgd_val = xen_pgd_val,
824
825 .make_pte = xen_make_pte,
826 .make_pgd = xen_make_pgd,
827
828#ifdef CONFIG_X86_PAE
829 .set_pte_atomic = xen_set_pte_atomic,
830 .set_pte_present = xen_set_pte_at,
831 .set_pud = xen_set_pud,
832 .pte_clear = xen_pte_clear,
833 .pmd_clear = xen_pmd_clear,
834
835 .make_pmd = xen_make_pmd,
836 .pmd_val = xen_pmd_val,
837#endif /* PAE */
838
839 .activate_mm = xen_activate_mm,
840 .dup_mmap = xen_dup_mmap,
841 .exit_mmap = xen_exit_mmap,
842
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700843 .set_lazy_mode = xen_set_lazy_mode,
844};
845
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700846#ifdef CONFIG_SMP
847static const struct smp_ops xen_smp_ops __initdata = {
848 .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
849 .smp_prepare_cpus = xen_smp_prepare_cpus,
850 .cpu_up = xen_cpu_up,
851 .smp_cpus_done = xen_smp_cpus_done,
852
853 .smp_send_stop = xen_smp_send_stop,
854 .smp_send_reschedule = xen_smp_send_reschedule,
855 .smp_call_function_mask = xen_smp_call_function_mask,
856};
857#endif /* CONFIG_SMP */
858
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700859/* First C function to be called on Xen boot */
860asmlinkage void __init xen_start_kernel(void)
861{
862 pgd_t *pgd;
863
864 if (!xen_start_info)
865 return;
866
867 BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0);
868
869 /* Install Xen paravirt ops */
870 paravirt_ops = xen_paravirt_ops;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700871#ifdef CONFIG_SMP
872 smp_ops = xen_smp_ops;
873#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700874
875 xen_setup_features();
876
877 /* Get mfn list */
878 if (!xen_feature(XENFEAT_auto_translated_physmap))
879 phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
880
881 pgd = (pgd_t *)xen_start_info->pt_base;
882
883 init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
884
885 init_mm.pgd = pgd; /* use the Xen pagetables to start */
886
887 /* keep using Xen gdt for now; no urgent need to change it */
888
889 x86_write_percpu(xen_cr3, __pa(pgd));
890 xen_vcpu_setup(0);
891
892 paravirt_ops.kernel_rpl = 1;
893 if (xen_feature(XENFEAT_supervisor_mode_kernel))
894 paravirt_ops.kernel_rpl = 0;
895
896 /* set the limit of our address space */
897 reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE);
898
899 /* set up basic CPUID stuff */
900 cpu_detect(&new_cpu_data);
901 new_cpu_data.hard_math = 1;
902 new_cpu_data.x86_capability[0] = cpuid_edx(1);
903
904 /* Poke various useful things into boot_params */
905 LOADER_TYPE = (9 << 4) | 0;
906 INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0;
907 INITRD_SIZE = xen_start_info->mod_len;
908
909 /* Start the world */
910 start_kernel();
911}