xen: lazy-mmu operations
[powerpc.git] / arch / i386 / xen / enlighten.c
1 /*
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/hardirq.h>
19 #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>
25 #include <linux/mm.h>
26 #include <linux/page-flags.h>
27 #include <linux/highmem.h>
28 #include <linux/smp.h>
29
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>
45 #include <asm/tlbflush.h>
46
47 #include "xen-ops.h"
48 #include "mmu.h"
49 #include "multicalls.h"
50
51 EXPORT_SYMBOL_GPL(hypercall_page);
52
53 DEFINE_PER_CPU(enum paravirt_lazy_mode, xen_lazy_mode);
54
55 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
56 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
57 DEFINE_PER_CPU(unsigned long, xen_cr3);
58
59 struct start_info *xen_start_info;
60 EXPORT_SYMBOL_GPL(xen_start_info);
61
62 void xen_vcpu_setup(int cpu)
63 {
64         per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
65 }
66
67 static 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
74 static 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
97 static void xen_set_debugreg(int reg, unsigned long val)
98 {
99         HYPERVISOR_set_debugreg(reg, val);
100 }
101
102 static unsigned long xen_get_debugreg(int reg)
103 {
104         return HYPERVISOR_get_debugreg(reg);
105 }
106
107 static unsigned long xen_save_fl(void)
108 {
109         struct vcpu_info *vcpu;
110         unsigned long flags;
111
112         vcpu = x86_read_percpu(xen_vcpu);
113
114         /* flag has opposite sense of mask */
115         flags = !vcpu->evtchn_upcall_mask;
116
117         /* convert to IF type flag
118            -0 -> 0x00000000
119            -1 -> 0xffffffff
120         */
121         return (-flags) & X86_EFLAGS_IF;
122 }
123
124 static void xen_restore_fl(unsigned long flags)
125 {
126         struct vcpu_info *vcpu;
127
128         /* convert from IF type flag */
129         flags = !(flags & X86_EFLAGS_IF);
130
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();
135         vcpu = x86_read_percpu(xen_vcpu);
136         vcpu->evtchn_upcall_mask = flags;
137         preempt_enable_no_resched();
138
139         /* Doesn't matter if we get preempted here, because any
140            pending event will get dealt with anyway. */
141
142         if (flags == 0) {
143                 preempt_check_resched();
144                 barrier(); /* unmask then check (avoid races) */
145                 if (unlikely(vcpu->evtchn_upcall_pending))
146                         force_evtchn_callback();
147         }
148 }
149
150 static void xen_irq_disable(void)
151 {
152         /* 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. */
155         preempt_disable();
156         x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
157         preempt_enable_no_resched();
158 }
159
160 static void xen_irq_enable(void)
161 {
162         struct vcpu_info *vcpu;
163
164         /* 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. */
167         preempt_disable();
168         vcpu = x86_read_percpu(xen_vcpu);
169         vcpu->evtchn_upcall_mask = 0;
170         preempt_enable_no_resched();
171
172         /* Doesn't matter if we get preempted here, because any
173            pending event will get dealt with anyway. */
174
175         barrier(); /* unmask then check (avoid races) */
176         if (unlikely(vcpu->evtchn_upcall_pending))
177                 force_evtchn_callback();
178 }
179
180 static 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
187 static 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
195 static void xen_set_lazy_mode(enum paravirt_lazy_mode mode)
196 {
197         BUG_ON(preemptible());
198
199         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
220 static unsigned long xen_store_tr(void)
221 {
222         return 0;
223 }
224
225 static 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
247 static 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
275 static 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
285 static 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);
294 }
295
296 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
297                                 u32 low, u32 high)
298 {
299         unsigned long lp = (unsigned long)&dt[entrynum];
300         xmaddr_t mach_lp = virt_to_machine(lp);
301         u64 entry = (u64)high << 32 | low;
302
303         preempt_disable();
304
305         xen_mc_flush();
306         if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
307                 BUG();
308
309         preempt_enable();
310 }
311
312 static int cvt_gate_to_trap(int vector, u32 low, u32 high,
313                             struct trap_info *info)
314 {
315         u8 type, dpl;
316
317         type = (high >> 8) & 0x1f;
318         dpl = (high >> 13) & 3;
319
320         if (type != 0xf && type != 0xe)
321                 return 0;
322
323         info->vector = vector;
324         info->address = (high & 0xffff0000) | (low & 0x0000ffff);
325         info->cs = low >> 16;
326         info->flags = dpl;
327         /* interrupt gates clear IF */
328         if (type == 0xe)
329                 info->flags |= 4;
330
331         return 1;
332 }
333
334 /* Locations of each CPU's IDT */
335 static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc);
336
337 /* Set an IDT entry.  If the entry is part of the current IDT, then
338    also update Xen. */
339 static void xen_write_idt_entry(struct desc_struct *dt, int entrynum,
340                                 u32 low, u32 high)
341 {
342         unsigned long p = (unsigned long)&dt[entrynum];
343         unsigned long start, end;
344
345         preempt_disable();
346
347         start = __get_cpu_var(idt_desc).address;
348         end = start + __get_cpu_var(idt_desc).size + 1;
349
350         xen_mc_flush();
351
352         write_dt_entry(dt, entrynum, low, high);
353
354         if (p >= start && (p + 8) <= end) {
355                 struct trap_info info[2];
356
357                 info[1].address = 0;
358
359                 if (cvt_gate_to_trap(entrynum, low, high, &info[0]))
360                         if (HYPERVISOR_set_trap_table(info))
361                                 BUG();
362         }
363
364         preempt_enable();
365 }
366
367 static void xen_convert_trap_info(const struct Xgt_desc_struct *desc,
368                                   struct trap_info *traps)
369 {
370         unsigned in, out, count;
371
372         count = (desc->size+1) / 8;
373         BUG_ON(count > 256);
374
375         for (in = out = 0; in < count; in++) {
376                 const u32 *entry = (u32 *)(desc->address + in * 8);
377
378                 if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
379                         out++;
380         }
381         traps[out].address = 0;
382 }
383
384 void xen_copy_trap_info(struct trap_info *traps)
385 {
386         const struct Xgt_desc_struct *desc = &__get_cpu_var(idt_desc);
387
388         xen_convert_trap_info(desc, traps);
389 }
390
391 /* Load a new IDT into Xen.  In principle this can be per-CPU, so we
392    hold a spinlock to protect the static traps[] array (static because
393    it avoids allocation, and saves stack space). */
394 static void xen_load_idt(const struct Xgt_desc_struct *desc)
395 {
396         static DEFINE_SPINLOCK(lock);
397         static struct trap_info traps[257];
398
399         spin_lock(&lock);
400
401         __get_cpu_var(idt_desc) = *desc;
402
403         xen_convert_trap_info(desc, traps);
404
405         xen_mc_flush();
406         if (HYPERVISOR_set_trap_table(traps))
407                 BUG();
408
409         spin_unlock(&lock);
410 }
411
412 /* Write a GDT descriptor entry.  Ignore LDT descriptors, since
413    they're handled differently. */
414 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
415                                 u32 low, u32 high)
416 {
417         preempt_disable();
418
419         switch ((high >> 8) & 0xff) {
420         case DESCTYPE_LDT:
421         case DESCTYPE_TSS:
422                 /* ignore */
423                 break;
424
425         default: {
426                 xmaddr_t maddr = virt_to_machine(&dt[entry]);
427                 u64 desc = (u64)high << 32 | low;
428
429                 xen_mc_flush();
430                 if (HYPERVISOR_update_descriptor(maddr.maddr, desc))
431                         BUG();
432         }
433
434         }
435
436         preempt_enable();
437 }
438
439 static void xen_load_esp0(struct tss_struct *tss,
440                           struct thread_struct *thread)
441 {
442         struct multicall_space mcs = xen_mc_entry(0);
443         MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0);
444         xen_mc_issue(PARAVIRT_LAZY_CPU);
445 }
446
447 static void xen_set_iopl_mask(unsigned mask)
448 {
449         struct physdev_set_iopl set_iopl;
450
451         /* Force the change at ring 0. */
452         set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
453         HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
454 }
455
456 static void xen_io_delay(void)
457 {
458 }
459
460 #ifdef CONFIG_X86_LOCAL_APIC
461 static unsigned long xen_apic_read(unsigned long reg)
462 {
463         return 0;
464 }
465
466 static void xen_apic_write(unsigned long reg, unsigned long val)
467 {
468         /* Warn to see if there's any stray references */
469         WARN_ON(1);
470 }
471 #endif
472
473 static void xen_flush_tlb(void)
474 {
475         struct mmuext_op *op;
476         struct multicall_space mcs = xen_mc_entry(sizeof(*op));
477
478         op = mcs.args;
479         op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
480         MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
481
482         xen_mc_issue(PARAVIRT_LAZY_MMU);
483 }
484
485 static void xen_flush_tlb_single(unsigned long addr)
486 {
487         struct mmuext_op *op;
488         struct multicall_space mcs = xen_mc_entry(sizeof(*op));
489
490         op = mcs.args;
491         op->cmd = MMUEXT_INVLPG_LOCAL;
492         op->arg1.linear_addr = addr & PAGE_MASK;
493         MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
494
495         xen_mc_issue(PARAVIRT_LAZY_MMU);
496 }
497
498 static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
499                                  unsigned long va)
500 {
501         struct {
502                 struct mmuext_op op;
503                 cpumask_t mask;
504         } *args;
505         cpumask_t cpumask = *cpus;
506         struct multicall_space mcs;
507
508         /*
509          * A couple of (to be removed) sanity checks:
510          *
511          * - current CPU must not be in mask
512          * - mask must exist :)
513          */
514         BUG_ON(cpus_empty(cpumask));
515         BUG_ON(cpu_isset(smp_processor_id(), cpumask));
516         BUG_ON(!mm);
517
518         /* If a CPU which we ran on has gone down, OK. */
519         cpus_and(cpumask, cpumask, cpu_online_map);
520         if (cpus_empty(cpumask))
521                 return;
522
523         mcs = xen_mc_entry(sizeof(*args));
524         args = mcs.args;
525         args->mask = cpumask;
526         args->op.arg2.vcpumask = &args->mask;
527
528         if (va == TLB_FLUSH_ALL) {
529                 args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
530         } else {
531                 args->op.cmd = MMUEXT_INVLPG_MULTI;
532                 args->op.arg1.linear_addr = va;
533         }
534
535         MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
536
537         xen_mc_issue(PARAVIRT_LAZY_MMU);
538 }
539
540 static unsigned long xen_read_cr2(void)
541 {
542         return x86_read_percpu(xen_vcpu)->arch.cr2;
543 }
544
545 static void xen_write_cr4(unsigned long cr4)
546 {
547         /* never allow TSC to be disabled */
548         native_write_cr4(cr4 & ~X86_CR4_TSD);
549 }
550
551 static unsigned long xen_read_cr3(void)
552 {
553         return x86_read_percpu(xen_cr3);
554 }
555
556 static void xen_write_cr3(unsigned long cr3)
557 {
558         BUG_ON(preemptible());
559
560         if (cr3 == x86_read_percpu(xen_cr3)) {
561                 /* just a simple tlb flush */
562                 xen_flush_tlb();
563                 return;
564         }
565
566         x86_write_percpu(xen_cr3, cr3);
567
568
569         {
570                 struct mmuext_op *op;
571                 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
572                 unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
573
574                 op = mcs.args;
575                 op->cmd = MMUEXT_NEW_BASEPTR;
576                 op->arg1.mfn = mfn;
577
578                 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
579
580                 xen_mc_issue(PARAVIRT_LAZY_CPU);
581         }
582 }
583
584 /* Early in boot, while setting up the initial pagetable, assume
585    everything is pinned. */
586 static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn)
587 {
588         BUG_ON(mem_map);        /* should only be used early */
589         make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
590 }
591
592 /* This needs to make sure the new pte page is pinned iff its being
593    attached to a pinned pagetable. */
594 static void xen_alloc_pt(struct mm_struct *mm, u32 pfn)
595 {
596         struct page *page = pfn_to_page(pfn);
597
598         if (PagePinned(virt_to_page(mm->pgd))) {
599                 SetPagePinned(page);
600
601                 if (!PageHighMem(page))
602                         make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
603                 else
604                         /* make sure there are no stray mappings of
605                            this page */
606                         kmap_flush_unused();
607         }
608 }
609
610 /* This should never happen until we're OK to use struct page */
611 static void xen_release_pt(u32 pfn)
612 {
613         struct page *page = pfn_to_page(pfn);
614
615         if (PagePinned(page)) {
616                 if (!PageHighMem(page))
617                         make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
618         }
619 }
620
621 #ifdef CONFIG_HIGHPTE
622 static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
623 {
624         pgprot_t prot = PAGE_KERNEL;
625
626         if (PagePinned(page))
627                 prot = PAGE_KERNEL_RO;
628
629         if (0 && PageHighMem(page))
630                 printk("mapping highpte %lx type %d prot %s\n",
631                        page_to_pfn(page), type,
632                        (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
633
634         return kmap_atomic_prot(page, type, prot);
635 }
636 #endif
637
638 static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
639 {
640         /* If there's an existing pte, then don't allow _PAGE_RW to be set */
641         if (pte_val_ma(*ptep) & _PAGE_PRESENT)
642                 pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
643                                pte_val_ma(pte));
644
645         return pte;
646 }
647
648 /* Init-time set_pte while constructing initial pagetables, which
649    doesn't allow RO pagetable pages to be remapped RW */
650 static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
651 {
652         pte = mask_rw_pte(ptep, pte);
653
654         xen_set_pte(ptep, pte);
655 }
656
657 static __init void xen_pagetable_setup_start(pgd_t *base)
658 {
659         pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
660
661         /* special set_pte for pagetable initialization */
662         paravirt_ops.set_pte = xen_set_pte_init;
663
664         init_mm.pgd = base;
665         /*
666          * copy top-level of Xen-supplied pagetable into place.  For
667          * !PAE we can use this as-is, but for PAE it is a stand-in
668          * while we copy the pmd pages.
669          */
670         memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
671
672         if (PTRS_PER_PMD > 1) {
673                 int i;
674                 /*
675                  * For PAE, need to allocate new pmds, rather than
676                  * share Xen's, since Xen doesn't like pmd's being
677                  * shared between address spaces.
678                  */
679                 for (i = 0; i < PTRS_PER_PGD; i++) {
680                         if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
681                                 pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
682
683                                 memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
684                                        PAGE_SIZE);
685
686                                 make_lowmem_page_readonly(pmd);
687
688                                 set_pgd(&base[i], __pgd(1 + __pa(pmd)));
689                         } else
690                                 pgd_clear(&base[i]);
691                 }
692         }
693
694         /* make sure zero_page is mapped RO so we can use it in pagetables */
695         make_lowmem_page_readonly(empty_zero_page);
696         make_lowmem_page_readonly(base);
697         /*
698          * Switch to new pagetable.  This is done before
699          * pagetable_init has done anything so that the new pages
700          * added to the table can be prepared properly for Xen.
701          */
702         xen_write_cr3(__pa(base));
703 }
704
705 static __init void xen_pagetable_setup_done(pgd_t *base)
706 {
707         /* This will work as long as patching hasn't happened yet
708            (which it hasn't) */
709         paravirt_ops.alloc_pt = xen_alloc_pt;
710         paravirt_ops.set_pte = xen_set_pte;
711
712         if (!xen_feature(XENFEAT_auto_translated_physmap)) {
713                 /*
714                  * Create a mapping for the shared info page.
715                  * Should be set_fixmap(), but shared_info is a machine
716                  * address with no corresponding pseudo-phys address.
717                  */
718                 set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
719                             PFN_DOWN(xen_start_info->shared_info),
720                             PAGE_KERNEL);
721
722                 HYPERVISOR_shared_info =
723                         (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
724
725         } else
726                 HYPERVISOR_shared_info =
727                         (struct shared_info *)__va(xen_start_info->shared_info);
728
729         /* Actually pin the pagetable down, but we can't set PG_pinned
730            yet because the page structures don't exist yet. */
731         {
732                 struct mmuext_op op;
733 #ifdef CONFIG_X86_PAE
734                 op.cmd = MMUEXT_PIN_L3_TABLE;
735 #else
736                 op.cmd = MMUEXT_PIN_L3_TABLE;
737 #endif
738                 op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base)));
739                 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
740                         BUG();
741         }
742
743         xen_vcpu_setup(smp_processor_id());
744 }
745
746 static const struct paravirt_ops xen_paravirt_ops __initdata = {
747         .paravirt_enabled = 1,
748         .shared_kernel_pmd = 0,
749
750         .name = "Xen",
751         .banner = xen_banner,
752
753         .patch = paravirt_patch_default,
754
755         .memory_setup = xen_memory_setup,
756         .arch_setup = xen_arch_setup,
757         .init_IRQ = xen_init_IRQ,
758         .post_allocator_init = xen_mark_init_mm_pinned,
759
760         .time_init = xen_time_init,
761         .set_wallclock = xen_set_wallclock,
762         .get_wallclock = xen_get_wallclock,
763         .get_cpu_khz = xen_cpu_khz,
764         .sched_clock = xen_sched_clock,
765
766         .cpuid = xen_cpuid,
767
768         .set_debugreg = xen_set_debugreg,
769         .get_debugreg = xen_get_debugreg,
770
771         .clts = native_clts,
772
773         .read_cr0 = native_read_cr0,
774         .write_cr0 = native_write_cr0,
775
776         .read_cr2 = xen_read_cr2,
777         .write_cr2 = native_write_cr2,
778
779         .read_cr3 = xen_read_cr3,
780         .write_cr3 = xen_write_cr3,
781
782         .read_cr4 = native_read_cr4,
783         .read_cr4_safe = native_read_cr4_safe,
784         .write_cr4 = xen_write_cr4,
785
786         .save_fl = xen_save_fl,
787         .restore_fl = xen_restore_fl,
788         .irq_disable = xen_irq_disable,
789         .irq_enable = xen_irq_enable,
790         .safe_halt = xen_safe_halt,
791         .halt = xen_halt,
792         .wbinvd = native_wbinvd,
793
794         .read_msr = native_read_msr_safe,
795         .write_msr = native_write_msr_safe,
796         .read_tsc = native_read_tsc,
797         .read_pmc = native_read_pmc,
798
799         .iret = (void *)&hypercall_page[__HYPERVISOR_iret],
800         .irq_enable_sysexit = NULL,  /* never called */
801
802         .load_tr_desc = paravirt_nop,
803         .set_ldt = xen_set_ldt,
804         .load_gdt = xen_load_gdt,
805         .load_idt = xen_load_idt,
806         .load_tls = xen_load_tls,
807
808         .store_gdt = native_store_gdt,
809         .store_idt = native_store_idt,
810         .store_tr = xen_store_tr,
811
812         .write_ldt_entry = xen_write_ldt_entry,
813         .write_gdt_entry = xen_write_gdt_entry,
814         .write_idt_entry = xen_write_idt_entry,
815         .load_esp0 = xen_load_esp0,
816
817         .set_iopl_mask = xen_set_iopl_mask,
818         .io_delay = xen_io_delay,
819
820 #ifdef CONFIG_X86_LOCAL_APIC
821         .apic_write = xen_apic_write,
822         .apic_write_atomic = xen_apic_write,
823         .apic_read = xen_apic_read,
824         .setup_boot_clock = paravirt_nop,
825         .setup_secondary_clock = paravirt_nop,
826         .startup_ipi_hook = paravirt_nop,
827 #endif
828
829         .flush_tlb_user = xen_flush_tlb,
830         .flush_tlb_kernel = xen_flush_tlb,
831         .flush_tlb_single = xen_flush_tlb_single,
832         .flush_tlb_others = xen_flush_tlb_others,
833
834         .pte_update = paravirt_nop,
835         .pte_update_defer = paravirt_nop,
836
837         .pagetable_setup_start = xen_pagetable_setup_start,
838         .pagetable_setup_done = xen_pagetable_setup_done,
839
840         .alloc_pt = xen_alloc_pt_init,
841         .release_pt = xen_release_pt,
842         .alloc_pd = paravirt_nop,
843         .alloc_pd_clone = paravirt_nop,
844         .release_pd = paravirt_nop,
845
846 #ifdef CONFIG_HIGHPTE
847         .kmap_atomic_pte = xen_kmap_atomic_pte,
848 #endif
849
850         .set_pte = NULL,        /* see xen_pagetable_setup_* */
851         .set_pte_at = xen_set_pte_at,
852         .set_pmd = xen_set_pmd,
853
854         .pte_val = xen_pte_val,
855         .pgd_val = xen_pgd_val,
856
857         .make_pte = xen_make_pte,
858         .make_pgd = xen_make_pgd,
859
860 #ifdef CONFIG_X86_PAE
861         .set_pte_atomic = xen_set_pte_atomic,
862         .set_pte_present = xen_set_pte_at,
863         .set_pud = xen_set_pud,
864         .pte_clear = xen_pte_clear,
865         .pmd_clear = xen_pmd_clear,
866
867         .make_pmd = xen_make_pmd,
868         .pmd_val = xen_pmd_val,
869 #endif  /* PAE */
870
871         .activate_mm = xen_activate_mm,
872         .dup_mmap = xen_dup_mmap,
873         .exit_mmap = xen_exit_mmap,
874
875         .set_lazy_mode = xen_set_lazy_mode,
876 };
877
878 #ifdef CONFIG_SMP
879 static const struct smp_ops xen_smp_ops __initdata = {
880         .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
881         .smp_prepare_cpus = xen_smp_prepare_cpus,
882         .cpu_up = xen_cpu_up,
883         .smp_cpus_done = xen_smp_cpus_done,
884
885         .smp_send_stop = xen_smp_send_stop,
886         .smp_send_reschedule = xen_smp_send_reschedule,
887         .smp_call_function_mask = xen_smp_call_function_mask,
888 };
889 #endif  /* CONFIG_SMP */
890
891 /* First C function to be called on Xen boot */
892 asmlinkage void __init xen_start_kernel(void)
893 {
894         pgd_t *pgd;
895
896         if (!xen_start_info)
897                 return;
898
899         BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0);
900
901         /* Install Xen paravirt ops */
902         paravirt_ops = xen_paravirt_ops;
903 #ifdef CONFIG_SMP
904         smp_ops = xen_smp_ops;
905 #endif
906
907         xen_setup_features();
908
909         /* Get mfn list */
910         if (!xen_feature(XENFEAT_auto_translated_physmap))
911                 phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
912
913         pgd = (pgd_t *)xen_start_info->pt_base;
914
915         init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
916
917         init_mm.pgd = pgd; /* use the Xen pagetables to start */
918
919         /* keep using Xen gdt for now; no urgent need to change it */
920
921         x86_write_percpu(xen_cr3, __pa(pgd));
922         xen_vcpu_setup(0);
923
924         paravirt_ops.kernel_rpl = 1;
925         if (xen_feature(XENFEAT_supervisor_mode_kernel))
926                 paravirt_ops.kernel_rpl = 0;
927
928         /* set the limit of our address space */
929         reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE);
930
931         /* set up basic CPUID stuff */
932         cpu_detect(&new_cpu_data);
933         new_cpu_data.hard_math = 1;
934         new_cpu_data.x86_capability[0] = cpuid_edx(1);
935
936         /* Poke various useful things into boot_params */
937         LOADER_TYPE = (9 << 4) | 0;
938         INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0;
939         INITRD_SIZE = xen_start_info->mod_len;
940
941         /* Start the world */
942         start_kernel();
943 }