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