e2511bccbc8d9b581b2e9367413c40ee58199861
[platform/adaptation/renesas_rcar/renesas_kernel.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/kprobes.h>
24 #include <linux/bootmem.h>
25 #include <linux/module.h>
26 #include <linux/mm.h>
27 #include <linux/page-flags.h>
28 #include <linux/highmem.h>
29 #include <linux/console.h>
30 #include <linux/pci.h>
31
32 #include <xen/xen.h>
33 #include <xen/interface/xen.h>
34 #include <xen/interface/version.h>
35 #include <xen/interface/physdev.h>
36 #include <xen/interface/vcpu.h>
37 #include <xen/features.h>
38 #include <xen/page.h>
39 #include <xen/hvc-console.h>
40
41 #include <asm/paravirt.h>
42 #include <asm/apic.h>
43 #include <asm/page.h>
44 #include <asm/xen/hypercall.h>
45 #include <asm/xen/hypervisor.h>
46 #include <asm/fixmap.h>
47 #include <asm/processor.h>
48 #include <asm/proto.h>
49 #include <asm/msr-index.h>
50 #include <asm/traps.h>
51 #include <asm/setup.h>
52 #include <asm/desc.h>
53 #include <asm/pgtable.h>
54 #include <asm/tlbflush.h>
55 #include <asm/reboot.h>
56 #include <asm/stackprotector.h>
57
58 #include "xen-ops.h"
59 #include "mmu.h"
60 #include "multicalls.h"
61
62 EXPORT_SYMBOL_GPL(hypercall_page);
63
64 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
65 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
66
67 enum xen_domain_type xen_domain_type = XEN_NATIVE;
68 EXPORT_SYMBOL_GPL(xen_domain_type);
69
70 struct start_info *xen_start_info;
71 EXPORT_SYMBOL_GPL(xen_start_info);
72
73 struct shared_info xen_dummy_shared_info;
74
75 void *xen_initial_gdt;
76
77 /*
78  * Point at some empty memory to start with. We map the real shared_info
79  * page as soon as fixmap is up and running.
80  */
81 struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
82
83 /*
84  * Flag to determine whether vcpu info placement is available on all
85  * VCPUs.  We assume it is to start with, and then set it to zero on
86  * the first failure.  This is because it can succeed on some VCPUs
87  * and not others, since it can involve hypervisor memory allocation,
88  * or because the guest failed to guarantee all the appropriate
89  * constraints on all VCPUs (ie buffer can't cross a page boundary).
90  *
91  * Note that any particular CPU may be using a placed vcpu structure,
92  * but we can only optimise if the all are.
93  *
94  * 0: not available, 1: available
95  */
96 static int have_vcpu_info_placement = 1;
97
98 static void xen_vcpu_setup(int cpu)
99 {
100         struct vcpu_register_vcpu_info info;
101         int err;
102         struct vcpu_info *vcpup;
103
104         BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
105         per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
106
107         if (!have_vcpu_info_placement)
108                 return;         /* already tested, not available */
109
110         vcpup = &per_cpu(xen_vcpu_info, cpu);
111
112         info.mfn = arbitrary_virt_to_mfn(vcpup);
113         info.offset = offset_in_page(vcpup);
114
115         printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
116                cpu, vcpup, info.mfn, info.offset);
117
118         /* Check to see if the hypervisor will put the vcpu_info
119            structure where we want it, which allows direct access via
120            a percpu-variable. */
121         err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
122
123         if (err) {
124                 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
125                 have_vcpu_info_placement = 0;
126         } else {
127                 /* This cpu is using the registered vcpu info, even if
128                    later ones fail to. */
129                 per_cpu(xen_vcpu, cpu) = vcpup;
130
131                 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
132                        cpu, vcpup);
133         }
134 }
135
136 /*
137  * On restore, set the vcpu placement up again.
138  * If it fails, then we're in a bad state, since
139  * we can't back out from using it...
140  */
141 void xen_vcpu_restore(void)
142 {
143         if (have_vcpu_info_placement) {
144                 int cpu;
145
146                 for_each_online_cpu(cpu) {
147                         bool other_cpu = (cpu != smp_processor_id());
148
149                         if (other_cpu &&
150                             HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
151                                 BUG();
152
153                         xen_vcpu_setup(cpu);
154
155                         if (other_cpu &&
156                             HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
157                                 BUG();
158                 }
159
160                 BUG_ON(!have_vcpu_info_placement);
161         }
162 }
163
164 static void __init xen_banner(void)
165 {
166         unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
167         struct xen_extraversion extra;
168         HYPERVISOR_xen_version(XENVER_extraversion, &extra);
169
170         printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
171                pv_info.name);
172         printk(KERN_INFO "Xen version: %d.%d%s%s\n",
173                version >> 16, version & 0xffff, extra.extraversion,
174                xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
175 }
176
177 static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
178 static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
179
180 static void xen_cpuid(unsigned int *ax, unsigned int *bx,
181                       unsigned int *cx, unsigned int *dx)
182 {
183         unsigned maskecx = ~0;
184         unsigned maskedx = ~0;
185
186         /*
187          * Mask out inconvenient features, to try and disable as many
188          * unsupported kernel subsystems as possible.
189          */
190         if (*ax == 1) {
191                 maskecx = cpuid_leaf1_ecx_mask;
192                 maskedx = cpuid_leaf1_edx_mask;
193         }
194
195         asm(XEN_EMULATE_PREFIX "cpuid"
196                 : "=a" (*ax),
197                   "=b" (*bx),
198                   "=c" (*cx),
199                   "=d" (*dx)
200                 : "0" (*ax), "2" (*cx));
201
202         *cx &= maskecx;
203         *dx &= maskedx;
204 }
205
206 static __init void xen_init_cpuid_mask(void)
207 {
208         unsigned int ax, bx, cx, dx;
209
210         cpuid_leaf1_edx_mask =
211                 ~((1 << X86_FEATURE_MCE)  |  /* disable MCE */
212                   (1 << X86_FEATURE_MCA)  |  /* disable MCA */
213                   (1 << X86_FEATURE_ACC));   /* thermal monitoring */
214
215         if (!xen_initial_domain())
216                 cpuid_leaf1_edx_mask &=
217                         ~((1 << X86_FEATURE_APIC) |  /* disable local APIC */
218                           (1 << X86_FEATURE_ACPI));  /* disable ACPI */
219
220         ax = 1;
221         cx = 0;
222         xen_cpuid(&ax, &bx, &cx, &dx);
223
224         /* cpuid claims we support xsave; try enabling it to see what happens */
225         if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
226                 unsigned long cr4;
227
228                 set_in_cr4(X86_CR4_OSXSAVE);
229                 
230                 cr4 = read_cr4();
231
232                 if ((cr4 & X86_CR4_OSXSAVE) == 0)
233                         cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
234
235                 clear_in_cr4(X86_CR4_OSXSAVE);
236         }
237 }
238
239 static void xen_set_debugreg(int reg, unsigned long val)
240 {
241         HYPERVISOR_set_debugreg(reg, val);
242 }
243
244 static unsigned long xen_get_debugreg(int reg)
245 {
246         return HYPERVISOR_get_debugreg(reg);
247 }
248
249 static void xen_end_context_switch(struct task_struct *next)
250 {
251         xen_mc_flush();
252         paravirt_end_context_switch(next);
253 }
254
255 static unsigned long xen_store_tr(void)
256 {
257         return 0;
258 }
259
260 /*
261  * Set the page permissions for a particular virtual address.  If the
262  * address is a vmalloc mapping (or other non-linear mapping), then
263  * find the linear mapping of the page and also set its protections to
264  * match.
265  */
266 static void set_aliased_prot(void *v, pgprot_t prot)
267 {
268         int level;
269         pte_t *ptep;
270         pte_t pte;
271         unsigned long pfn;
272         struct page *page;
273
274         ptep = lookup_address((unsigned long)v, &level);
275         BUG_ON(ptep == NULL);
276
277         pfn = pte_pfn(*ptep);
278         page = pfn_to_page(pfn);
279
280         pte = pfn_pte(pfn, prot);
281
282         if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
283                 BUG();
284
285         if (!PageHighMem(page)) {
286                 void *av = __va(PFN_PHYS(pfn));
287
288                 if (av != v)
289                         if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
290                                 BUG();
291         } else
292                 kmap_flush_unused();
293 }
294
295 static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
296 {
297         const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
298         int i;
299
300         for(i = 0; i < entries; i += entries_per_page)
301                 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
302 }
303
304 static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
305 {
306         const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
307         int i;
308
309         for(i = 0; i < entries; i += entries_per_page)
310                 set_aliased_prot(ldt + i, PAGE_KERNEL);
311 }
312
313 static void xen_set_ldt(const void *addr, unsigned entries)
314 {
315         struct mmuext_op *op;
316         struct multicall_space mcs = xen_mc_entry(sizeof(*op));
317
318         op = mcs.args;
319         op->cmd = MMUEXT_SET_LDT;
320         op->arg1.linear_addr = (unsigned long)addr;
321         op->arg2.nr_ents = entries;
322
323         MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
324
325         xen_mc_issue(PARAVIRT_LAZY_CPU);
326 }
327
328 static void xen_load_gdt(const struct desc_ptr *dtr)
329 {
330         unsigned long va = dtr->address;
331         unsigned int size = dtr->size + 1;
332         unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
333         unsigned long frames[pages];
334         int f;
335
336         /*
337          * A GDT can be up to 64k in size, which corresponds to 8192
338          * 8-byte entries, or 16 4k pages..
339          */
340
341         BUG_ON(size > 65536);
342         BUG_ON(va & ~PAGE_MASK);
343
344         for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
345                 int level;
346                 pte_t *ptep;
347                 unsigned long pfn, mfn;
348                 void *virt;
349
350                 /*
351                  * The GDT is per-cpu and is in the percpu data area.
352                  * That can be virtually mapped, so we need to do a
353                  * page-walk to get the underlying MFN for the
354                  * hypercall.  The page can also be in the kernel's
355                  * linear range, so we need to RO that mapping too.
356                  */
357                 ptep = lookup_address(va, &level);
358                 BUG_ON(ptep == NULL);
359
360                 pfn = pte_pfn(*ptep);
361                 mfn = pfn_to_mfn(pfn);
362                 virt = __va(PFN_PHYS(pfn));
363
364                 frames[f] = mfn;
365
366                 make_lowmem_page_readonly((void *)va);
367                 make_lowmem_page_readonly(virt);
368         }
369
370         if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
371                 BUG();
372 }
373
374 /*
375  * load_gdt for early boot, when the gdt is only mapped once
376  */
377 static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
378 {
379         unsigned long va = dtr->address;
380         unsigned int size = dtr->size + 1;
381         unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
382         unsigned long frames[pages];
383         int f;
384
385         /*
386          * A GDT can be up to 64k in size, which corresponds to 8192
387          * 8-byte entries, or 16 4k pages..
388          */
389
390         BUG_ON(size > 65536);
391         BUG_ON(va & ~PAGE_MASK);
392
393         for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
394                 pte_t pte;
395                 unsigned long pfn, mfn;
396
397                 pfn = virt_to_pfn(va);
398                 mfn = pfn_to_mfn(pfn);
399
400                 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
401
402                 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
403                         BUG();
404
405                 frames[f] = mfn;
406         }
407
408         if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
409                 BUG();
410 }
411
412 static void load_TLS_descriptor(struct thread_struct *t,
413                                 unsigned int cpu, unsigned int i)
414 {
415         struct desc_struct *gdt = get_cpu_gdt_table(cpu);
416         xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
417         struct multicall_space mc = __xen_mc_entry(0);
418
419         MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
420 }
421
422 static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
423 {
424         /*
425          * XXX sleazy hack: If we're being called in a lazy-cpu zone
426          * and lazy gs handling is enabled, it means we're in a
427          * context switch, and %gs has just been saved.  This means we
428          * can zero it out to prevent faults on exit from the
429          * hypervisor if the next process has no %gs.  Either way, it
430          * has been saved, and the new value will get loaded properly.
431          * This will go away as soon as Xen has been modified to not
432          * save/restore %gs for normal hypercalls.
433          *
434          * On x86_64, this hack is not used for %gs, because gs points
435          * to KERNEL_GS_BASE (and uses it for PDA references), so we
436          * must not zero %gs on x86_64
437          *
438          * For x86_64, we need to zero %fs, otherwise we may get an
439          * exception between the new %fs descriptor being loaded and
440          * %fs being effectively cleared at __switch_to().
441          */
442         if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
443 #ifdef CONFIG_X86_32
444                 lazy_load_gs(0);
445 #else
446                 loadsegment(fs, 0);
447 #endif
448         }
449
450         xen_mc_batch();
451
452         load_TLS_descriptor(t, cpu, 0);
453         load_TLS_descriptor(t, cpu, 1);
454         load_TLS_descriptor(t, cpu, 2);
455
456         xen_mc_issue(PARAVIRT_LAZY_CPU);
457 }
458
459 #ifdef CONFIG_X86_64
460 static void xen_load_gs_index(unsigned int idx)
461 {
462         if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
463                 BUG();
464 }
465 #endif
466
467 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
468                                 const void *ptr)
469 {
470         xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
471         u64 entry = *(u64 *)ptr;
472
473         preempt_disable();
474
475         xen_mc_flush();
476         if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
477                 BUG();
478
479         preempt_enable();
480 }
481
482 static int cvt_gate_to_trap(int vector, const gate_desc *val,
483                             struct trap_info *info)
484 {
485         unsigned long addr;
486
487         if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
488                 return 0;
489
490         info->vector = vector;
491
492         addr = gate_offset(*val);
493 #ifdef CONFIG_X86_64
494         /*
495          * Look for known traps using IST, and substitute them
496          * appropriately.  The debugger ones are the only ones we care
497          * about.  Xen will handle faults like double_fault and
498          * machine_check, so we should never see them.  Warn if
499          * there's an unexpected IST-using fault handler.
500          */
501         if (addr == (unsigned long)debug)
502                 addr = (unsigned long)xen_debug;
503         else if (addr == (unsigned long)int3)
504                 addr = (unsigned long)xen_int3;
505         else if (addr == (unsigned long)stack_segment)
506                 addr = (unsigned long)xen_stack_segment;
507         else if (addr == (unsigned long)double_fault ||
508                  addr == (unsigned long)nmi) {
509                 /* Don't need to handle these */
510                 return 0;
511 #ifdef CONFIG_X86_MCE
512         } else if (addr == (unsigned long)machine_check) {
513                 return 0;
514 #endif
515         } else {
516                 /* Some other trap using IST? */
517                 if (WARN_ON(val->ist != 0))
518                         return 0;
519         }
520 #endif  /* CONFIG_X86_64 */
521         info->address = addr;
522
523         info->cs = gate_segment(*val);
524         info->flags = val->dpl;
525         /* interrupt gates clear IF */
526         if (val->type == GATE_INTERRUPT)
527                 info->flags |= 1 << 2;
528
529         return 1;
530 }
531
532 /* Locations of each CPU's IDT */
533 static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
534
535 /* Set an IDT entry.  If the entry is part of the current IDT, then
536    also update Xen. */
537 static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
538 {
539         unsigned long p = (unsigned long)&dt[entrynum];
540         unsigned long start, end;
541
542         preempt_disable();
543
544         start = __get_cpu_var(idt_desc).address;
545         end = start + __get_cpu_var(idt_desc).size + 1;
546
547         xen_mc_flush();
548
549         native_write_idt_entry(dt, entrynum, g);
550
551         if (p >= start && (p + 8) <= end) {
552                 struct trap_info info[2];
553
554                 info[1].address = 0;
555
556                 if (cvt_gate_to_trap(entrynum, g, &info[0]))
557                         if (HYPERVISOR_set_trap_table(info))
558                                 BUG();
559         }
560
561         preempt_enable();
562 }
563
564 static void xen_convert_trap_info(const struct desc_ptr *desc,
565                                   struct trap_info *traps)
566 {
567         unsigned in, out, count;
568
569         count = (desc->size+1) / sizeof(gate_desc);
570         BUG_ON(count > 256);
571
572         for (in = out = 0; in < count; in++) {
573                 gate_desc *entry = (gate_desc*)(desc->address) + in;
574
575                 if (cvt_gate_to_trap(in, entry, &traps[out]))
576                         out++;
577         }
578         traps[out].address = 0;
579 }
580
581 void xen_copy_trap_info(struct trap_info *traps)
582 {
583         const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
584
585         xen_convert_trap_info(desc, traps);
586 }
587
588 /* Load a new IDT into Xen.  In principle this can be per-CPU, so we
589    hold a spinlock to protect the static traps[] array (static because
590    it avoids allocation, and saves stack space). */
591 static void xen_load_idt(const struct desc_ptr *desc)
592 {
593         static DEFINE_SPINLOCK(lock);
594         static struct trap_info traps[257];
595
596         spin_lock(&lock);
597
598         __get_cpu_var(idt_desc) = *desc;
599
600         xen_convert_trap_info(desc, traps);
601
602         xen_mc_flush();
603         if (HYPERVISOR_set_trap_table(traps))
604                 BUG();
605
606         spin_unlock(&lock);
607 }
608
609 /* Write a GDT descriptor entry.  Ignore LDT descriptors, since
610    they're handled differently. */
611 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
612                                 const void *desc, int type)
613 {
614         preempt_disable();
615
616         switch (type) {
617         case DESC_LDT:
618         case DESC_TSS:
619                 /* ignore */
620                 break;
621
622         default: {
623                 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
624
625                 xen_mc_flush();
626                 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
627                         BUG();
628         }
629
630         }
631
632         preempt_enable();
633 }
634
635 /*
636  * Version of write_gdt_entry for use at early boot-time needed to
637  * update an entry as simply as possible.
638  */
639 static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
640                                             const void *desc, int type)
641 {
642         switch (type) {
643         case DESC_LDT:
644         case DESC_TSS:
645                 /* ignore */
646                 break;
647
648         default: {
649                 xmaddr_t maddr = virt_to_machine(&dt[entry]);
650
651                 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
652                         dt[entry] = *(struct desc_struct *)desc;
653         }
654
655         }
656 }
657
658 static void xen_load_sp0(struct tss_struct *tss,
659                          struct thread_struct *thread)
660 {
661         struct multicall_space mcs = xen_mc_entry(0);
662         MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
663         xen_mc_issue(PARAVIRT_LAZY_CPU);
664 }
665
666 static void xen_set_iopl_mask(unsigned mask)
667 {
668         struct physdev_set_iopl set_iopl;
669
670         /* Force the change at ring 0. */
671         set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
672         HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
673 }
674
675 static void xen_io_delay(void)
676 {
677 }
678
679 #ifdef CONFIG_X86_LOCAL_APIC
680 static u32 xen_apic_read(u32 reg)
681 {
682         return 0;
683 }
684
685 static void xen_apic_write(u32 reg, u32 val)
686 {
687         /* Warn to see if there's any stray references */
688         WARN_ON(1);
689 }
690
691 static u64 xen_apic_icr_read(void)
692 {
693         return 0;
694 }
695
696 static void xen_apic_icr_write(u32 low, u32 id)
697 {
698         /* Warn to see if there's any stray references */
699         WARN_ON(1);
700 }
701
702 static void xen_apic_wait_icr_idle(void)
703 {
704         return;
705 }
706
707 static u32 xen_safe_apic_wait_icr_idle(void)
708 {
709         return 0;
710 }
711
712 static void set_xen_basic_apic_ops(void)
713 {
714         apic->read = xen_apic_read;
715         apic->write = xen_apic_write;
716         apic->icr_read = xen_apic_icr_read;
717         apic->icr_write = xen_apic_icr_write;
718         apic->wait_icr_idle = xen_apic_wait_icr_idle;
719         apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
720 }
721
722 #endif
723
724
725 static void xen_clts(void)
726 {
727         struct multicall_space mcs;
728
729         mcs = xen_mc_entry(0);
730
731         MULTI_fpu_taskswitch(mcs.mc, 0);
732
733         xen_mc_issue(PARAVIRT_LAZY_CPU);
734 }
735
736 static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
737
738 static unsigned long xen_read_cr0(void)
739 {
740         unsigned long cr0 = percpu_read(xen_cr0_value);
741
742         if (unlikely(cr0 == 0)) {
743                 cr0 = native_read_cr0();
744                 percpu_write(xen_cr0_value, cr0);
745         }
746
747         return cr0;
748 }
749
750 static void xen_write_cr0(unsigned long cr0)
751 {
752         struct multicall_space mcs;
753
754         percpu_write(xen_cr0_value, cr0);
755
756         /* Only pay attention to cr0.TS; everything else is
757            ignored. */
758         mcs = xen_mc_entry(0);
759
760         MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
761
762         xen_mc_issue(PARAVIRT_LAZY_CPU);
763 }
764
765 static void xen_write_cr4(unsigned long cr4)
766 {
767         cr4 &= ~X86_CR4_PGE;
768         cr4 &= ~X86_CR4_PSE;
769
770         native_write_cr4(cr4);
771 }
772
773 static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
774 {
775         int ret;
776
777         ret = 0;
778
779         switch (msr) {
780 #ifdef CONFIG_X86_64
781                 unsigned which;
782                 u64 base;
783
784         case MSR_FS_BASE:               which = SEGBASE_FS; goto set;
785         case MSR_KERNEL_GS_BASE:        which = SEGBASE_GS_USER; goto set;
786         case MSR_GS_BASE:               which = SEGBASE_GS_KERNEL; goto set;
787
788         set:
789                 base = ((u64)high << 32) | low;
790                 if (HYPERVISOR_set_segment_base(which, base) != 0)
791                         ret = -EIO;
792                 break;
793 #endif
794
795         case MSR_STAR:
796         case MSR_CSTAR:
797         case MSR_LSTAR:
798         case MSR_SYSCALL_MASK:
799         case MSR_IA32_SYSENTER_CS:
800         case MSR_IA32_SYSENTER_ESP:
801         case MSR_IA32_SYSENTER_EIP:
802                 /* Fast syscall setup is all done in hypercalls, so
803                    these are all ignored.  Stub them out here to stop
804                    Xen console noise. */
805                 break;
806
807         default:
808                 ret = native_write_msr_safe(msr, low, high);
809         }
810
811         return ret;
812 }
813
814 void xen_setup_shared_info(void)
815 {
816         if (!xen_feature(XENFEAT_auto_translated_physmap)) {
817                 set_fixmap(FIX_PARAVIRT_BOOTMAP,
818                            xen_start_info->shared_info);
819
820                 HYPERVISOR_shared_info =
821                         (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
822         } else
823                 HYPERVISOR_shared_info =
824                         (struct shared_info *)__va(xen_start_info->shared_info);
825
826 #ifndef CONFIG_SMP
827         /* In UP this is as good a place as any to set up shared info */
828         xen_setup_vcpu_info_placement();
829 #endif
830
831         xen_setup_mfn_list_list();
832 }
833
834 /* This is called once we have the cpu_possible_map */
835 void xen_setup_vcpu_info_placement(void)
836 {
837         int cpu;
838
839         for_each_possible_cpu(cpu)
840                 xen_vcpu_setup(cpu);
841
842         /* xen_vcpu_setup managed to place the vcpu_info within the
843            percpu area for all cpus, so make use of it */
844         if (have_vcpu_info_placement) {
845                 printk(KERN_INFO "Xen: using vcpu_info placement\n");
846
847                 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
848                 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
849                 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
850                 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
851                 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
852         }
853 }
854
855 static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
856                           unsigned long addr, unsigned len)
857 {
858         char *start, *end, *reloc;
859         unsigned ret;
860
861         start = end = reloc = NULL;
862
863 #define SITE(op, x)                                                     \
864         case PARAVIRT_PATCH(op.x):                                      \
865         if (have_vcpu_info_placement) {                                 \
866                 start = (char *)xen_##x##_direct;                       \
867                 end = xen_##x##_direct_end;                             \
868                 reloc = xen_##x##_direct_reloc;                         \
869         }                                                               \
870         goto patch_site
871
872         switch (type) {
873                 SITE(pv_irq_ops, irq_enable);
874                 SITE(pv_irq_ops, irq_disable);
875                 SITE(pv_irq_ops, save_fl);
876                 SITE(pv_irq_ops, restore_fl);
877 #undef SITE
878
879         patch_site:
880                 if (start == NULL || (end-start) > len)
881                         goto default_patch;
882
883                 ret = paravirt_patch_insns(insnbuf, len, start, end);
884
885                 /* Note: because reloc is assigned from something that
886                    appears to be an array, gcc assumes it's non-null,
887                    but doesn't know its relationship with start and
888                    end. */
889                 if (reloc > start && reloc < end) {
890                         int reloc_off = reloc - start;
891                         long *relocp = (long *)(insnbuf + reloc_off);
892                         long delta = start - (char *)addr;
893
894                         *relocp += delta;
895                 }
896                 break;
897
898         default_patch:
899         default:
900                 ret = paravirt_patch_default(type, clobbers, insnbuf,
901                                              addr, len);
902                 break;
903         }
904
905         return ret;
906 }
907
908 static const struct pv_info xen_info __initdata = {
909         .paravirt_enabled = 1,
910         .shared_kernel_pmd = 0,
911
912         .name = "Xen",
913 };
914
915 static const struct pv_init_ops xen_init_ops __initdata = {
916         .patch = xen_patch,
917 };
918
919 static const struct pv_time_ops xen_time_ops __initdata = {
920         .sched_clock = xen_sched_clock,
921 };
922
923 static const struct pv_cpu_ops xen_cpu_ops __initdata = {
924         .cpuid = xen_cpuid,
925
926         .set_debugreg = xen_set_debugreg,
927         .get_debugreg = xen_get_debugreg,
928
929         .clts = xen_clts,
930
931         .read_cr0 = xen_read_cr0,
932         .write_cr0 = xen_write_cr0,
933
934         .read_cr4 = native_read_cr4,
935         .read_cr4_safe = native_read_cr4_safe,
936         .write_cr4 = xen_write_cr4,
937
938         .wbinvd = native_wbinvd,
939
940         .read_msr = native_read_msr_safe,
941         .write_msr = xen_write_msr_safe,
942         .read_tsc = native_read_tsc,
943         .read_pmc = native_read_pmc,
944
945         .iret = xen_iret,
946         .irq_enable_sysexit = xen_sysexit,
947 #ifdef CONFIG_X86_64
948         .usergs_sysret32 = xen_sysret32,
949         .usergs_sysret64 = xen_sysret64,
950 #endif
951
952         .load_tr_desc = paravirt_nop,
953         .set_ldt = xen_set_ldt,
954         .load_gdt = xen_load_gdt,
955         .load_idt = xen_load_idt,
956         .load_tls = xen_load_tls,
957 #ifdef CONFIG_X86_64
958         .load_gs_index = xen_load_gs_index,
959 #endif
960
961         .alloc_ldt = xen_alloc_ldt,
962         .free_ldt = xen_free_ldt,
963
964         .store_gdt = native_store_gdt,
965         .store_idt = native_store_idt,
966         .store_tr = xen_store_tr,
967
968         .write_ldt_entry = xen_write_ldt_entry,
969         .write_gdt_entry = xen_write_gdt_entry,
970         .write_idt_entry = xen_write_idt_entry,
971         .load_sp0 = xen_load_sp0,
972
973         .set_iopl_mask = xen_set_iopl_mask,
974         .io_delay = xen_io_delay,
975
976         /* Xen takes care of %gs when switching to usermode for us */
977         .swapgs = paravirt_nop,
978
979         .start_context_switch = paravirt_start_context_switch,
980         .end_context_switch = xen_end_context_switch,
981 };
982
983 static const struct pv_apic_ops xen_apic_ops __initdata = {
984 #ifdef CONFIG_X86_LOCAL_APIC
985         .startup_ipi_hook = paravirt_nop,
986 #endif
987 };
988
989 static void xen_reboot(int reason)
990 {
991         struct sched_shutdown r = { .reason = reason };
992
993 #ifdef CONFIG_SMP
994         smp_send_stop();
995 #endif
996
997         if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
998                 BUG();
999 }
1000
1001 static void xen_restart(char *msg)
1002 {
1003         xen_reboot(SHUTDOWN_reboot);
1004 }
1005
1006 static void xen_emergency_restart(void)
1007 {
1008         xen_reboot(SHUTDOWN_reboot);
1009 }
1010
1011 static void xen_machine_halt(void)
1012 {
1013         xen_reboot(SHUTDOWN_poweroff);
1014 }
1015
1016 static void xen_crash_shutdown(struct pt_regs *regs)
1017 {
1018         xen_reboot(SHUTDOWN_crash);
1019 }
1020
1021 static const struct machine_ops __initdata xen_machine_ops = {
1022         .restart = xen_restart,
1023         .halt = xen_machine_halt,
1024         .power_off = xen_machine_halt,
1025         .shutdown = xen_machine_halt,
1026         .crash_shutdown = xen_crash_shutdown,
1027         .emergency_restart = xen_emergency_restart,
1028 };
1029
1030 /*
1031  * Set up the GDT and segment registers for -fstack-protector.  Until
1032  * we do this, we have to be careful not to call any stack-protected
1033  * function, which is most of the kernel.
1034  */
1035 static void __init xen_setup_stackprotector(void)
1036 {
1037         pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1038         pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1039
1040         setup_stack_canary_segment(0);
1041         switch_to_new_gdt(0);
1042
1043         pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1044         pv_cpu_ops.load_gdt = xen_load_gdt;
1045 }
1046
1047 /* First C function to be called on Xen boot */
1048 asmlinkage void __init xen_start_kernel(void)
1049 {
1050         pgd_t *pgd;
1051
1052         if (!xen_start_info)
1053                 return;
1054
1055         xen_domain_type = XEN_PV_DOMAIN;
1056
1057         /* Install Xen paravirt ops */
1058         pv_info = xen_info;
1059         pv_init_ops = xen_init_ops;
1060         pv_time_ops = xen_time_ops;
1061         pv_cpu_ops = xen_cpu_ops;
1062         pv_apic_ops = xen_apic_ops;
1063
1064         x86_init.resources.memory_setup = xen_memory_setup;
1065         x86_init.oem.arch_setup = xen_arch_setup;
1066         x86_init.oem.banner = xen_banner;
1067
1068         x86_init.timers.timer_init = xen_time_init;
1069         x86_init.timers.setup_percpu_clockev = x86_init_noop;
1070         x86_cpuinit.setup_percpu_clockev = x86_init_noop;
1071
1072         x86_platform.calibrate_tsc = xen_tsc_khz;
1073         x86_platform.get_wallclock = xen_get_wallclock;
1074         x86_platform.set_wallclock = xen_set_wallclock;
1075
1076         /*
1077          * Set up some pagetable state before starting to set any ptes.
1078          */
1079
1080         xen_init_mmu_ops();
1081
1082         /* Prevent unwanted bits from being set in PTEs. */
1083         __supported_pte_mask &= ~_PAGE_GLOBAL;
1084         if (!xen_initial_domain())
1085                 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1086
1087         __supported_pte_mask |= _PAGE_IOMAP;
1088
1089 #ifdef CONFIG_X86_64
1090         /* Work out if we support NX */
1091         check_efer();
1092 #endif
1093
1094         xen_setup_features();
1095
1096         /* Get mfn list */
1097         if (!xen_feature(XENFEAT_auto_translated_physmap))
1098                 xen_build_dynamic_phys_to_machine();
1099
1100         /*
1101          * Set up kernel GDT and segment registers, mainly so that
1102          * -fstack-protector code can be executed.
1103          */
1104         xen_setup_stackprotector();
1105
1106         xen_init_irq_ops();
1107         xen_init_cpuid_mask();
1108
1109 #ifdef CONFIG_X86_LOCAL_APIC
1110         /*
1111          * set up the basic apic ops.
1112          */
1113         set_xen_basic_apic_ops();
1114 #endif
1115
1116         if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1117                 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1118                 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1119         }
1120
1121         machine_ops = xen_machine_ops;
1122
1123         /*
1124          * The only reliable way to retain the initial address of the
1125          * percpu gdt_page is to remember it here, so we can go and
1126          * mark it RW later, when the initial percpu area is freed.
1127          */
1128         xen_initial_gdt = &per_cpu(gdt_page, 0);
1129
1130         xen_smp_init();
1131
1132         pgd = (pgd_t *)xen_start_info->pt_base;
1133
1134         /* Don't do the full vcpu_info placement stuff until we have a
1135            possible map and a non-dummy shared_info. */
1136         per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1137
1138         local_irq_disable();
1139         early_boot_irqs_off();
1140
1141         xen_raw_console_write("mapping kernel into physical memory\n");
1142         pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
1143
1144         init_mm.pgd = pgd;
1145
1146         /* keep using Xen gdt for now; no urgent need to change it */
1147
1148         pv_info.kernel_rpl = 1;
1149         if (xen_feature(XENFEAT_supervisor_mode_kernel))
1150                 pv_info.kernel_rpl = 0;
1151
1152         /* set the limit of our address space */
1153         xen_reserve_top();
1154
1155 #ifdef CONFIG_X86_32
1156         /* set up basic CPUID stuff */
1157         cpu_detect(&new_cpu_data);
1158         new_cpu_data.hard_math = 1;
1159         new_cpu_data.wp_works_ok = 1;
1160         new_cpu_data.x86_capability[0] = cpuid_edx(1);
1161 #endif
1162
1163         /* Poke various useful things into boot_params */
1164         boot_params.hdr.type_of_loader = (9 << 4) | 0;
1165         boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1166                 ? __pa(xen_start_info->mod_start) : 0;
1167         boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
1168         boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
1169
1170         if (!xen_initial_domain()) {
1171                 add_preferred_console("xenboot", 0, NULL);
1172                 add_preferred_console("tty", 0, NULL);
1173                 add_preferred_console("hvc", 0, NULL);
1174         } else {
1175                 /* Make sure ACS will be enabled */
1176                 pci_request_acs();
1177         }
1178                 
1179
1180         xen_raw_console_write("about to get started...\n");
1181
1182         /* Start the world */
1183 #ifdef CONFIG_X86_32
1184         i386_start_kernel();
1185 #else
1186         x86_64_start_reservations((char *)__pa_symbol(&boot_params));
1187 #endif
1188 }