]> git.karo-electronics.de Git - mv-sheeva.git/blob - arch/x86/xen/enlighten.c
Clean up duplicate includes in arch/i386/xen/
[mv-sheeva.git] / arch / x86 / 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
29 #include <xen/interface/xen.h>
30 #include <xen/interface/physdev.h>
31 #include <xen/interface/vcpu.h>
32 #include <xen/interface/sched.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 #include <asm/reboot.h>
47
48 #include "xen-ops.h"
49 #include "mmu.h"
50 #include "multicalls.h"
51
52 EXPORT_SYMBOL_GPL(hypercall_page);
53
54 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
55 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
56 DEFINE_PER_CPU(unsigned long, xen_cr3);
57
58 struct start_info *xen_start_info;
59 EXPORT_SYMBOL_GPL(xen_start_info);
60
61 static /* __initdata */ struct shared_info dummy_shared_info;
62
63 /*
64  * Point at some empty memory to start with. We map the real shared_info
65  * page as soon as fixmap is up and running.
66  */
67 struct shared_info *HYPERVISOR_shared_info = (void *)&dummy_shared_info;
68
69 /*
70  * Flag to determine whether vcpu info placement is available on all
71  * VCPUs.  We assume it is to start with, and then set it to zero on
72  * the first failure.  This is because it can succeed on some VCPUs
73  * and not others, since it can involve hypervisor memory allocation,
74  * or because the guest failed to guarantee all the appropriate
75  * constraints on all VCPUs (ie buffer can't cross a page boundary).
76  *
77  * Note that any particular CPU may be using a placed vcpu structure,
78  * but we can only optimise if the all are.
79  *
80  * 0: not available, 1: available
81  */
82 static int have_vcpu_info_placement = 1;
83
84 static void __init xen_vcpu_setup(int cpu)
85 {
86         struct vcpu_register_vcpu_info info;
87         int err;
88         struct vcpu_info *vcpup;
89
90         per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
91
92         if (!have_vcpu_info_placement)
93                 return;         /* already tested, not available */
94
95         vcpup = &per_cpu(xen_vcpu_info, cpu);
96
97         info.mfn = virt_to_mfn(vcpup);
98         info.offset = offset_in_page(vcpup);
99
100         printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %x, offset %d\n",
101                cpu, vcpup, info.mfn, info.offset);
102
103         /* Check to see if the hypervisor will put the vcpu_info
104            structure where we want it, which allows direct access via
105            a percpu-variable. */
106         err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
107
108         if (err) {
109                 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
110                 have_vcpu_info_placement = 0;
111         } else {
112                 /* This cpu is using the registered vcpu info, even if
113                    later ones fail to. */
114                 per_cpu(xen_vcpu, cpu) = vcpup;
115
116                 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
117                        cpu, vcpup);
118         }
119 }
120
121 static void __init xen_banner(void)
122 {
123         printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
124                pv_info.name);
125         printk(KERN_INFO "Hypervisor signature: %s\n", xen_start_info->magic);
126 }
127
128 static void xen_cpuid(unsigned int *eax, unsigned int *ebx,
129                       unsigned int *ecx, unsigned int *edx)
130 {
131         unsigned maskedx = ~0;
132
133         /*
134          * Mask out inconvenient features, to try and disable as many
135          * unsupported kernel subsystems as possible.
136          */
137         if (*eax == 1)
138                 maskedx = ~((1 << X86_FEATURE_APIC) |  /* disable APIC */
139                             (1 << X86_FEATURE_ACPI) |  /* disable ACPI */
140                             (1 << X86_FEATURE_ACC));   /* thermal monitoring */
141
142         asm(XEN_EMULATE_PREFIX "cpuid"
143                 : "=a" (*eax),
144                   "=b" (*ebx),
145                   "=c" (*ecx),
146                   "=d" (*edx)
147                 : "0" (*eax), "2" (*ecx));
148         *edx &= maskedx;
149 }
150
151 static void xen_set_debugreg(int reg, unsigned long val)
152 {
153         HYPERVISOR_set_debugreg(reg, val);
154 }
155
156 static unsigned long xen_get_debugreg(int reg)
157 {
158         return HYPERVISOR_get_debugreg(reg);
159 }
160
161 static unsigned long xen_save_fl(void)
162 {
163         struct vcpu_info *vcpu;
164         unsigned long flags;
165
166         vcpu = x86_read_percpu(xen_vcpu);
167
168         /* flag has opposite sense of mask */
169         flags = !vcpu->evtchn_upcall_mask;
170
171         /* convert to IF type flag
172            -0 -> 0x00000000
173            -1 -> 0xffffffff
174         */
175         return (-flags) & X86_EFLAGS_IF;
176 }
177
178 static void xen_restore_fl(unsigned long flags)
179 {
180         struct vcpu_info *vcpu;
181
182         /* convert from IF type flag */
183         flags = !(flags & X86_EFLAGS_IF);
184
185         /* There's a one instruction preempt window here.  We need to
186            make sure we're don't switch CPUs between getting the vcpu
187            pointer and updating the mask. */
188         preempt_disable();
189         vcpu = x86_read_percpu(xen_vcpu);
190         vcpu->evtchn_upcall_mask = flags;
191         preempt_enable_no_resched();
192
193         /* Doesn't matter if we get preempted here, because any
194            pending event will get dealt with anyway. */
195
196         if (flags == 0) {
197                 preempt_check_resched();
198                 barrier(); /* unmask then check (avoid races) */
199                 if (unlikely(vcpu->evtchn_upcall_pending))
200                         force_evtchn_callback();
201         }
202 }
203
204 static void xen_irq_disable(void)
205 {
206         /* There's a one instruction preempt window here.  We need to
207            make sure we're don't switch CPUs between getting the vcpu
208            pointer and updating the mask. */
209         preempt_disable();
210         x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
211         preempt_enable_no_resched();
212 }
213
214 static void xen_irq_enable(void)
215 {
216         struct vcpu_info *vcpu;
217
218         /* There's a one instruction preempt window here.  We need to
219            make sure we're don't switch CPUs between getting the vcpu
220            pointer and updating the mask. */
221         preempt_disable();
222         vcpu = x86_read_percpu(xen_vcpu);
223         vcpu->evtchn_upcall_mask = 0;
224         preempt_enable_no_resched();
225
226         /* Doesn't matter if we get preempted here, because any
227            pending event will get dealt with anyway. */
228
229         barrier(); /* unmask then check (avoid races) */
230         if (unlikely(vcpu->evtchn_upcall_pending))
231                 force_evtchn_callback();
232 }
233
234 static void xen_safe_halt(void)
235 {
236         /* Blocking includes an implicit local_irq_enable(). */
237         if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0)
238                 BUG();
239 }
240
241 static void xen_halt(void)
242 {
243         if (irqs_disabled())
244                 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
245         else
246                 xen_safe_halt();
247 }
248
249 static void xen_leave_lazy(void)
250 {
251         paravirt_leave_lazy(paravirt_get_lazy_mode());
252         xen_mc_flush();
253 }
254
255 static unsigned long xen_store_tr(void)
256 {
257         return 0;
258 }
259
260 static void xen_set_ldt(const void *addr, unsigned entries)
261 {
262         unsigned long linear_addr = (unsigned long)addr;
263         struct mmuext_op *op;
264         struct multicall_space mcs = xen_mc_entry(sizeof(*op));
265
266         op = mcs.args;
267         op->cmd = MMUEXT_SET_LDT;
268         if (linear_addr) {
269                 /* ldt my be vmalloced, use arbitrary_virt_to_machine */
270                 xmaddr_t maddr;
271                 maddr = arbitrary_virt_to_machine((unsigned long)addr);
272                 linear_addr = (unsigned long)maddr.maddr;
273         }
274         op->arg1.linear_addr = linear_addr;
275         op->arg2.nr_ents = entries;
276
277         MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
278
279         xen_mc_issue(PARAVIRT_LAZY_CPU);
280 }
281
282 static void xen_load_gdt(const struct Xgt_desc_struct *dtr)
283 {
284         unsigned long *frames;
285         unsigned long va = dtr->address;
286         unsigned int size = dtr->size + 1;
287         unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
288         int f;
289         struct multicall_space mcs;
290
291         /* A GDT can be up to 64k in size, which corresponds to 8192
292            8-byte entries, or 16 4k pages.. */
293
294         BUG_ON(size > 65536);
295         BUG_ON(va & ~PAGE_MASK);
296
297         mcs = xen_mc_entry(sizeof(*frames) * pages);
298         frames = mcs.args;
299
300         for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
301                 frames[f] = virt_to_mfn(va);
302                 make_lowmem_page_readonly((void *)va);
303         }
304
305         MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
306
307         xen_mc_issue(PARAVIRT_LAZY_CPU);
308 }
309
310 static void load_TLS_descriptor(struct thread_struct *t,
311                                 unsigned int cpu, unsigned int i)
312 {
313         struct desc_struct *gdt = get_cpu_gdt_table(cpu);
314         xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
315         struct multicall_space mc = __xen_mc_entry(0);
316
317         MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
318 }
319
320 static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
321 {
322         xen_mc_batch();
323
324         load_TLS_descriptor(t, cpu, 0);
325         load_TLS_descriptor(t, cpu, 1);
326         load_TLS_descriptor(t, cpu, 2);
327
328         xen_mc_issue(PARAVIRT_LAZY_CPU);
329
330         /*
331          * XXX sleazy hack: If we're being called in a lazy-cpu zone,
332          * it means we're in a context switch, and %gs has just been
333          * saved.  This means we can zero it out to prevent faults on
334          * exit from the hypervisor if the next process has no %gs.
335          * Either way, it has been saved, and the new value will get
336          * loaded properly.  This will go away as soon as Xen has been
337          * modified to not save/restore %gs for normal hypercalls.
338          */
339         if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)
340                 loadsegment(gs, 0);
341 }
342
343 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
344                                 u32 low, u32 high)
345 {
346         unsigned long lp = (unsigned long)&dt[entrynum];
347         xmaddr_t mach_lp = virt_to_machine(lp);
348         u64 entry = (u64)high << 32 | low;
349
350         preempt_disable();
351
352         xen_mc_flush();
353         if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
354                 BUG();
355
356         preempt_enable();
357 }
358
359 static int cvt_gate_to_trap(int vector, u32 low, u32 high,
360                             struct trap_info *info)
361 {
362         u8 type, dpl;
363
364         type = (high >> 8) & 0x1f;
365         dpl = (high >> 13) & 3;
366
367         if (type != 0xf && type != 0xe)
368                 return 0;
369
370         info->vector = vector;
371         info->address = (high & 0xffff0000) | (low & 0x0000ffff);
372         info->cs = low >> 16;
373         info->flags = dpl;
374         /* interrupt gates clear IF */
375         if (type == 0xe)
376                 info->flags |= 4;
377
378         return 1;
379 }
380
381 /* Locations of each CPU's IDT */
382 static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc);
383
384 /* Set an IDT entry.  If the entry is part of the current IDT, then
385    also update Xen. */
386 static void xen_write_idt_entry(struct desc_struct *dt, int entrynum,
387                                 u32 low, u32 high)
388 {
389         unsigned long p = (unsigned long)&dt[entrynum];
390         unsigned long start, end;
391
392         preempt_disable();
393
394         start = __get_cpu_var(idt_desc).address;
395         end = start + __get_cpu_var(idt_desc).size + 1;
396
397         xen_mc_flush();
398
399         write_dt_entry(dt, entrynum, low, high);
400
401         if (p >= start && (p + 8) <= end) {
402                 struct trap_info info[2];
403
404                 info[1].address = 0;
405
406                 if (cvt_gate_to_trap(entrynum, low, high, &info[0]))
407                         if (HYPERVISOR_set_trap_table(info))
408                                 BUG();
409         }
410
411         preempt_enable();
412 }
413
414 static void xen_convert_trap_info(const struct Xgt_desc_struct *desc,
415                                   struct trap_info *traps)
416 {
417         unsigned in, out, count;
418
419         count = (desc->size+1) / 8;
420         BUG_ON(count > 256);
421
422         for (in = out = 0; in < count; in++) {
423                 const u32 *entry = (u32 *)(desc->address + in * 8);
424
425                 if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
426                         out++;
427         }
428         traps[out].address = 0;
429 }
430
431 void xen_copy_trap_info(struct trap_info *traps)
432 {
433         const struct Xgt_desc_struct *desc = &__get_cpu_var(idt_desc);
434
435         xen_convert_trap_info(desc, traps);
436 }
437
438 /* Load a new IDT into Xen.  In principle this can be per-CPU, so we
439    hold a spinlock to protect the static traps[] array (static because
440    it avoids allocation, and saves stack space). */
441 static void xen_load_idt(const struct Xgt_desc_struct *desc)
442 {
443         static DEFINE_SPINLOCK(lock);
444         static struct trap_info traps[257];
445
446         spin_lock(&lock);
447
448         __get_cpu_var(idt_desc) = *desc;
449
450         xen_convert_trap_info(desc, traps);
451
452         xen_mc_flush();
453         if (HYPERVISOR_set_trap_table(traps))
454                 BUG();
455
456         spin_unlock(&lock);
457 }
458
459 /* Write a GDT descriptor entry.  Ignore LDT descriptors, since
460    they're handled differently. */
461 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
462                                 u32 low, u32 high)
463 {
464         preempt_disable();
465
466         switch ((high >> 8) & 0xff) {
467         case DESCTYPE_LDT:
468         case DESCTYPE_TSS:
469                 /* ignore */
470                 break;
471
472         default: {
473                 xmaddr_t maddr = virt_to_machine(&dt[entry]);
474                 u64 desc = (u64)high << 32 | low;
475
476                 xen_mc_flush();
477                 if (HYPERVISOR_update_descriptor(maddr.maddr, desc))
478                         BUG();
479         }
480
481         }
482
483         preempt_enable();
484 }
485
486 static void xen_load_esp0(struct tss_struct *tss,
487                           struct thread_struct *thread)
488 {
489         struct multicall_space mcs = xen_mc_entry(0);
490         MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0);
491         xen_mc_issue(PARAVIRT_LAZY_CPU);
492 }
493
494 static void xen_set_iopl_mask(unsigned mask)
495 {
496         struct physdev_set_iopl set_iopl;
497
498         /* Force the change at ring 0. */
499         set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
500         HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
501 }
502
503 static void xen_io_delay(void)
504 {
505 }
506
507 #ifdef CONFIG_X86_LOCAL_APIC
508 static unsigned long xen_apic_read(unsigned long reg)
509 {
510         return 0;
511 }
512
513 static void xen_apic_write(unsigned long reg, unsigned long val)
514 {
515         /* Warn to see if there's any stray references */
516         WARN_ON(1);
517 }
518 #endif
519
520 static void xen_flush_tlb(void)
521 {
522         struct mmuext_op *op;
523         struct multicall_space mcs = xen_mc_entry(sizeof(*op));
524
525         op = mcs.args;
526         op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
527         MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
528
529         xen_mc_issue(PARAVIRT_LAZY_MMU);
530 }
531
532 static void xen_flush_tlb_single(unsigned long addr)
533 {
534         struct mmuext_op *op;
535         struct multicall_space mcs = xen_mc_entry(sizeof(*op));
536
537         op = mcs.args;
538         op->cmd = MMUEXT_INVLPG_LOCAL;
539         op->arg1.linear_addr = addr & PAGE_MASK;
540         MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
541
542         xen_mc_issue(PARAVIRT_LAZY_MMU);
543 }
544
545 static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
546                                  unsigned long va)
547 {
548         struct {
549                 struct mmuext_op op;
550                 cpumask_t mask;
551         } *args;
552         cpumask_t cpumask = *cpus;
553         struct multicall_space mcs;
554
555         /*
556          * A couple of (to be removed) sanity checks:
557          *
558          * - current CPU must not be in mask
559          * - mask must exist :)
560          */
561         BUG_ON(cpus_empty(cpumask));
562         BUG_ON(cpu_isset(smp_processor_id(), cpumask));
563         BUG_ON(!mm);
564
565         /* If a CPU which we ran on has gone down, OK. */
566         cpus_and(cpumask, cpumask, cpu_online_map);
567         if (cpus_empty(cpumask))
568                 return;
569
570         mcs = xen_mc_entry(sizeof(*args));
571         args = mcs.args;
572         args->mask = cpumask;
573         args->op.arg2.vcpumask = &args->mask;
574
575         if (va == TLB_FLUSH_ALL) {
576                 args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
577         } else {
578                 args->op.cmd = MMUEXT_INVLPG_MULTI;
579                 args->op.arg1.linear_addr = va;
580         }
581
582         MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
583
584         xen_mc_issue(PARAVIRT_LAZY_MMU);
585 }
586
587 static void xen_write_cr2(unsigned long cr2)
588 {
589         x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
590 }
591
592 static unsigned long xen_read_cr2(void)
593 {
594         return x86_read_percpu(xen_vcpu)->arch.cr2;
595 }
596
597 static unsigned long xen_read_cr2_direct(void)
598 {
599         return x86_read_percpu(xen_vcpu_info.arch.cr2);
600 }
601
602 static void xen_write_cr4(unsigned long cr4)
603 {
604         /* Just ignore cr4 changes; Xen doesn't allow us to do
605            anything anyway. */
606 }
607
608 static unsigned long xen_read_cr3(void)
609 {
610         return x86_read_percpu(xen_cr3);
611 }
612
613 static void xen_write_cr3(unsigned long cr3)
614 {
615         BUG_ON(preemptible());
616
617         if (cr3 == x86_read_percpu(xen_cr3)) {
618                 /* just a simple tlb flush */
619                 xen_flush_tlb();
620                 return;
621         }
622
623         x86_write_percpu(xen_cr3, cr3);
624
625
626         {
627                 struct mmuext_op *op;
628                 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
629                 unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
630
631                 op = mcs.args;
632                 op->cmd = MMUEXT_NEW_BASEPTR;
633                 op->arg1.mfn = mfn;
634
635                 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
636
637                 xen_mc_issue(PARAVIRT_LAZY_CPU);
638         }
639 }
640
641 /* Early in boot, while setting up the initial pagetable, assume
642    everything is pinned. */
643 static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn)
644 {
645         BUG_ON(mem_map);        /* should only be used early */
646         make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
647 }
648
649 /* This needs to make sure the new pte page is pinned iff its being
650    attached to a pinned pagetable. */
651 static void xen_alloc_pt(struct mm_struct *mm, u32 pfn)
652 {
653         struct page *page = pfn_to_page(pfn);
654
655         if (PagePinned(virt_to_page(mm->pgd))) {
656                 SetPagePinned(page);
657
658                 if (!PageHighMem(page))
659                         make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
660                 else
661                         /* make sure there are no stray mappings of
662                            this page */
663                         kmap_flush_unused();
664         }
665 }
666
667 /* This should never happen until we're OK to use struct page */
668 static void xen_release_pt(u32 pfn)
669 {
670         struct page *page = pfn_to_page(pfn);
671
672         if (PagePinned(page)) {
673                 if (!PageHighMem(page))
674                         make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
675         }
676 }
677
678 #ifdef CONFIG_HIGHPTE
679 static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
680 {
681         pgprot_t prot = PAGE_KERNEL;
682
683         if (PagePinned(page))
684                 prot = PAGE_KERNEL_RO;
685
686         if (0 && PageHighMem(page))
687                 printk("mapping highpte %lx type %d prot %s\n",
688                        page_to_pfn(page), type,
689                        (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
690
691         return kmap_atomic_prot(page, type, prot);
692 }
693 #endif
694
695 static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
696 {
697         /* If there's an existing pte, then don't allow _PAGE_RW to be set */
698         if (pte_val_ma(*ptep) & _PAGE_PRESENT)
699                 pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
700                                pte_val_ma(pte));
701
702         return pte;
703 }
704
705 /* Init-time set_pte while constructing initial pagetables, which
706    doesn't allow RO pagetable pages to be remapped RW */
707 static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
708 {
709         pte = mask_rw_pte(ptep, pte);
710
711         xen_set_pte(ptep, pte);
712 }
713
714 static __init void xen_pagetable_setup_start(pgd_t *base)
715 {
716         pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
717
718         /* special set_pte for pagetable initialization */
719         pv_mmu_ops.set_pte = xen_set_pte_init;
720
721         init_mm.pgd = base;
722         /*
723          * copy top-level of Xen-supplied pagetable into place.  For
724          * !PAE we can use this as-is, but for PAE it is a stand-in
725          * while we copy the pmd pages.
726          */
727         memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
728
729         if (PTRS_PER_PMD > 1) {
730                 int i;
731                 /*
732                  * For PAE, need to allocate new pmds, rather than
733                  * share Xen's, since Xen doesn't like pmd's being
734                  * shared between address spaces.
735                  */
736                 for (i = 0; i < PTRS_PER_PGD; i++) {
737                         if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
738                                 pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
739
740                                 memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
741                                        PAGE_SIZE);
742
743                                 make_lowmem_page_readonly(pmd);
744
745                                 set_pgd(&base[i], __pgd(1 + __pa(pmd)));
746                         } else
747                                 pgd_clear(&base[i]);
748                 }
749         }
750
751         /* make sure zero_page is mapped RO so we can use it in pagetables */
752         make_lowmem_page_readonly(empty_zero_page);
753         make_lowmem_page_readonly(base);
754         /*
755          * Switch to new pagetable.  This is done before
756          * pagetable_init has done anything so that the new pages
757          * added to the table can be prepared properly for Xen.
758          */
759         xen_write_cr3(__pa(base));
760 }
761
762 static __init void xen_pagetable_setup_done(pgd_t *base)
763 {
764         /* This will work as long as patching hasn't happened yet
765            (which it hasn't) */
766         pv_mmu_ops.alloc_pt = xen_alloc_pt;
767         pv_mmu_ops.set_pte = xen_set_pte;
768
769         if (!xen_feature(XENFEAT_auto_translated_physmap)) {
770                 /*
771                  * Create a mapping for the shared info page.
772                  * Should be set_fixmap(), but shared_info is a machine
773                  * address with no corresponding pseudo-phys address.
774                  */
775                 set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
776                             PFN_DOWN(xen_start_info->shared_info),
777                             PAGE_KERNEL);
778
779                 HYPERVISOR_shared_info =
780                         (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
781
782         } else
783                 HYPERVISOR_shared_info =
784                         (struct shared_info *)__va(xen_start_info->shared_info);
785
786         /* Actually pin the pagetable down, but we can't set PG_pinned
787            yet because the page structures don't exist yet. */
788         {
789                 struct mmuext_op op;
790 #ifdef CONFIG_X86_PAE
791                 op.cmd = MMUEXT_PIN_L3_TABLE;
792 #else
793                 op.cmd = MMUEXT_PIN_L3_TABLE;
794 #endif
795                 op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base)));
796                 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
797                         BUG();
798         }
799 }
800
801 /* This is called once we have the cpu_possible_map */
802 void __init xen_setup_vcpu_info_placement(void)
803 {
804         int cpu;
805
806         for_each_possible_cpu(cpu)
807                 xen_vcpu_setup(cpu);
808
809         /* xen_vcpu_setup managed to place the vcpu_info within the
810            percpu area for all cpus, so make use of it */
811         if (have_vcpu_info_placement) {
812                 printk(KERN_INFO "Xen: using vcpu_info placement\n");
813
814                 pv_irq_ops.save_fl = xen_save_fl_direct;
815                 pv_irq_ops.restore_fl = xen_restore_fl_direct;
816                 pv_irq_ops.irq_disable = xen_irq_disable_direct;
817                 pv_irq_ops.irq_enable = xen_irq_enable_direct;
818                 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
819                 pv_cpu_ops.iret = xen_iret_direct;
820         }
821 }
822
823 static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
824                           unsigned long addr, unsigned len)
825 {
826         char *start, *end, *reloc;
827         unsigned ret;
828
829         start = end = reloc = NULL;
830
831 #define SITE(op, x)                                                     \
832         case PARAVIRT_PATCH(op.x):                                      \
833         if (have_vcpu_info_placement) {                                 \
834                 start = (char *)xen_##x##_direct;                       \
835                 end = xen_##x##_direct_end;                             \
836                 reloc = xen_##x##_direct_reloc;                         \
837         }                                                               \
838         goto patch_site
839
840         switch (type) {
841                 SITE(pv_irq_ops, irq_enable);
842                 SITE(pv_irq_ops, irq_disable);
843                 SITE(pv_irq_ops, save_fl);
844                 SITE(pv_irq_ops, restore_fl);
845 #undef SITE
846
847         patch_site:
848                 if (start == NULL || (end-start) > len)
849                         goto default_patch;
850
851                 ret = paravirt_patch_insns(insnbuf, len, start, end);
852
853                 /* Note: because reloc is assigned from something that
854                    appears to be an array, gcc assumes it's non-null,
855                    but doesn't know its relationship with start and
856                    end. */
857                 if (reloc > start && reloc < end) {
858                         int reloc_off = reloc - start;
859                         long *relocp = (long *)(insnbuf + reloc_off);
860                         long delta = start - (char *)addr;
861
862                         *relocp += delta;
863                 }
864                 break;
865
866         default_patch:
867         default:
868                 ret = paravirt_patch_default(type, clobbers, insnbuf,
869                                              addr, len);
870                 break;
871         }
872
873         return ret;
874 }
875
876 static const struct pv_info xen_info __initdata = {
877         .paravirt_enabled = 1,
878         .shared_kernel_pmd = 0,
879
880         .name = "Xen",
881 };
882
883 static const struct pv_init_ops xen_init_ops __initdata = {
884         .patch = xen_patch,
885
886         .banner = xen_banner,
887         .memory_setup = xen_memory_setup,
888         .arch_setup = xen_arch_setup,
889         .post_allocator_init = xen_mark_init_mm_pinned,
890 };
891
892 static const struct pv_time_ops xen_time_ops __initdata = {
893         .time_init = xen_time_init,
894
895         .set_wallclock = xen_set_wallclock,
896         .get_wallclock = xen_get_wallclock,
897         .get_cpu_khz = xen_cpu_khz,
898         .sched_clock = xen_sched_clock,
899 };
900
901 static const struct pv_cpu_ops xen_cpu_ops __initdata = {
902         .cpuid = xen_cpuid,
903
904         .set_debugreg = xen_set_debugreg,
905         .get_debugreg = xen_get_debugreg,
906
907         .clts = native_clts,
908
909         .read_cr0 = native_read_cr0,
910         .write_cr0 = native_write_cr0,
911
912         .read_cr4 = native_read_cr4,
913         .read_cr4_safe = native_read_cr4_safe,
914         .write_cr4 = xen_write_cr4,
915
916         .wbinvd = native_wbinvd,
917
918         .read_msr = native_read_msr_safe,
919         .write_msr = native_write_msr_safe,
920         .read_tsc = native_read_tsc,
921         .read_pmc = native_read_pmc,
922
923         .iret = (void *)&hypercall_page[__HYPERVISOR_iret],
924         .irq_enable_sysexit = NULL,  /* never called */
925
926         .load_tr_desc = paravirt_nop,
927         .set_ldt = xen_set_ldt,
928         .load_gdt = xen_load_gdt,
929         .load_idt = xen_load_idt,
930         .load_tls = xen_load_tls,
931
932         .store_gdt = native_store_gdt,
933         .store_idt = native_store_idt,
934         .store_tr = xen_store_tr,
935
936         .write_ldt_entry = xen_write_ldt_entry,
937         .write_gdt_entry = xen_write_gdt_entry,
938         .write_idt_entry = xen_write_idt_entry,
939         .load_esp0 = xen_load_esp0,
940
941         .set_iopl_mask = xen_set_iopl_mask,
942         .io_delay = xen_io_delay,
943
944         .lazy_mode = {
945                 .enter = paravirt_enter_lazy_cpu,
946                 .leave = xen_leave_lazy,
947         },
948 };
949
950 static const struct pv_irq_ops xen_irq_ops __initdata = {
951         .init_IRQ = xen_init_IRQ,
952         .save_fl = xen_save_fl,
953         .restore_fl = xen_restore_fl,
954         .irq_disable = xen_irq_disable,
955         .irq_enable = xen_irq_enable,
956         .safe_halt = xen_safe_halt,
957         .halt = xen_halt,
958 };
959
960 static const struct pv_apic_ops xen_apic_ops __initdata = {
961 #ifdef CONFIG_X86_LOCAL_APIC
962         .apic_write = xen_apic_write,
963         .apic_write_atomic = xen_apic_write,
964         .apic_read = xen_apic_read,
965         .setup_boot_clock = paravirt_nop,
966         .setup_secondary_clock = paravirt_nop,
967         .startup_ipi_hook = paravirt_nop,
968 #endif
969 };
970
971 static const struct pv_mmu_ops xen_mmu_ops __initdata = {
972         .pagetable_setup_start = xen_pagetable_setup_start,
973         .pagetable_setup_done = xen_pagetable_setup_done,
974
975         .read_cr2 = xen_read_cr2,
976         .write_cr2 = xen_write_cr2,
977
978         .read_cr3 = xen_read_cr3,
979         .write_cr3 = xen_write_cr3,
980
981         .flush_tlb_user = xen_flush_tlb,
982         .flush_tlb_kernel = xen_flush_tlb,
983         .flush_tlb_single = xen_flush_tlb_single,
984         .flush_tlb_others = xen_flush_tlb_others,
985
986         .pte_update = paravirt_nop,
987         .pte_update_defer = paravirt_nop,
988
989         .alloc_pt = xen_alloc_pt_init,
990         .release_pt = xen_release_pt,
991         .alloc_pd = paravirt_nop,
992         .alloc_pd_clone = paravirt_nop,
993         .release_pd = paravirt_nop,
994
995 #ifdef CONFIG_HIGHPTE
996         .kmap_atomic_pte = xen_kmap_atomic_pte,
997 #endif
998
999         .set_pte = NULL,        /* see xen_pagetable_setup_* */
1000         .set_pte_at = xen_set_pte_at,
1001         .set_pmd = xen_set_pmd,
1002
1003         .pte_val = xen_pte_val,
1004         .pgd_val = xen_pgd_val,
1005
1006         .make_pte = xen_make_pte,
1007         .make_pgd = xen_make_pgd,
1008
1009 #ifdef CONFIG_X86_PAE
1010         .set_pte_atomic = xen_set_pte_atomic,
1011         .set_pte_present = xen_set_pte_at,
1012         .set_pud = xen_set_pud,
1013         .pte_clear = xen_pte_clear,
1014         .pmd_clear = xen_pmd_clear,
1015
1016         .make_pmd = xen_make_pmd,
1017         .pmd_val = xen_pmd_val,
1018 #endif  /* PAE */
1019
1020         .activate_mm = xen_activate_mm,
1021         .dup_mmap = xen_dup_mmap,
1022         .exit_mmap = xen_exit_mmap,
1023
1024         .lazy_mode = {
1025                 .enter = paravirt_enter_lazy_mmu,
1026                 .leave = xen_leave_lazy,
1027         },
1028 };
1029
1030 #ifdef CONFIG_SMP
1031 static const struct smp_ops xen_smp_ops __initdata = {
1032         .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
1033         .smp_prepare_cpus = xen_smp_prepare_cpus,
1034         .cpu_up = xen_cpu_up,
1035         .smp_cpus_done = xen_smp_cpus_done,
1036
1037         .smp_send_stop = xen_smp_send_stop,
1038         .smp_send_reschedule = xen_smp_send_reschedule,
1039         .smp_call_function_mask = xen_smp_call_function_mask,
1040 };
1041 #endif  /* CONFIG_SMP */
1042
1043 static void xen_reboot(int reason)
1044 {
1045 #ifdef CONFIG_SMP
1046         smp_send_stop();
1047 #endif
1048
1049         if (HYPERVISOR_sched_op(SCHEDOP_shutdown, reason))
1050                 BUG();
1051 }
1052
1053 static void xen_restart(char *msg)
1054 {
1055         xen_reboot(SHUTDOWN_reboot);
1056 }
1057
1058 static void xen_emergency_restart(void)
1059 {
1060         xen_reboot(SHUTDOWN_reboot);
1061 }
1062
1063 static void xen_machine_halt(void)
1064 {
1065         xen_reboot(SHUTDOWN_poweroff);
1066 }
1067
1068 static void xen_crash_shutdown(struct pt_regs *regs)
1069 {
1070         xen_reboot(SHUTDOWN_crash);
1071 }
1072
1073 static const struct machine_ops __initdata xen_machine_ops = {
1074         .restart = xen_restart,
1075         .halt = xen_machine_halt,
1076         .power_off = xen_machine_halt,
1077         .shutdown = xen_machine_halt,
1078         .crash_shutdown = xen_crash_shutdown,
1079         .emergency_restart = xen_emergency_restart,
1080 };
1081
1082
1083 /* First C function to be called on Xen boot */
1084 asmlinkage void __init xen_start_kernel(void)
1085 {
1086         pgd_t *pgd;
1087
1088         if (!xen_start_info)
1089                 return;
1090
1091         BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0);
1092
1093         /* Install Xen paravirt ops */
1094         pv_info = xen_info;
1095         pv_init_ops = xen_init_ops;
1096         pv_time_ops = xen_time_ops;
1097         pv_cpu_ops = xen_cpu_ops;
1098         pv_irq_ops = xen_irq_ops;
1099         pv_apic_ops = xen_apic_ops;
1100         pv_mmu_ops = xen_mmu_ops;
1101
1102         machine_ops = xen_machine_ops;
1103
1104 #ifdef CONFIG_SMP
1105         smp_ops = xen_smp_ops;
1106 #endif
1107
1108         xen_setup_features();
1109
1110         /* Get mfn list */
1111         if (!xen_feature(XENFEAT_auto_translated_physmap))
1112                 phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
1113
1114         pgd = (pgd_t *)xen_start_info->pt_base;
1115
1116         init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
1117
1118         init_mm.pgd = pgd; /* use the Xen pagetables to start */
1119
1120         /* keep using Xen gdt for now; no urgent need to change it */
1121
1122         x86_write_percpu(xen_cr3, __pa(pgd));
1123
1124 #ifdef CONFIG_SMP
1125         /* Don't do the full vcpu_info placement stuff until we have a
1126            possible map. */
1127         per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1128 #else
1129         /* May as well do it now, since there's no good time to call
1130            it later on UP. */
1131         xen_setup_vcpu_info_placement();
1132 #endif
1133
1134         pv_info.kernel_rpl = 1;
1135         if (xen_feature(XENFEAT_supervisor_mode_kernel))
1136                 pv_info.kernel_rpl = 0;
1137
1138         /* set the limit of our address space */
1139         reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE);
1140
1141         /* set up basic CPUID stuff */
1142         cpu_detect(&new_cpu_data);
1143         new_cpu_data.hard_math = 1;
1144         new_cpu_data.x86_capability[0] = cpuid_edx(1);
1145
1146         /* Poke various useful things into boot_params */
1147         LOADER_TYPE = (9 << 4) | 0;
1148         INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0;
1149         INITRD_SIZE = xen_start_info->mod_len;
1150
1151         /* Start the world */
1152         start_kernel();
1153 }