]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/x86/xen/enlighten_pv.c
x86/xen: use capabilities instead of fake cpuid values for acc
[linux.git] / arch / x86 / xen / enlighten_pv.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/cpu.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/smp.h>
18 #include <linux/preempt.h>
19 #include <linux/hardirq.h>
20 #include <linux/percpu.h>
21 #include <linux/delay.h>
22 #include <linux/start_kernel.h>
23 #include <linux/sched.h>
24 #include <linux/kprobes.h>
25 #include <linux/bootmem.h>
26 #include <linux/export.h>
27 #include <linux/mm.h>
28 #include <linux/page-flags.h>
29 #include <linux/highmem.h>
30 #include <linux/console.h>
31 #include <linux/pci.h>
32 #include <linux/gfp.h>
33 #include <linux/memblock.h>
34 #include <linux/edd.h>
35 #include <linux/frame.h>
36
37 #include <xen/xen.h>
38 #include <xen/events.h>
39 #include <xen/interface/xen.h>
40 #include <xen/interface/version.h>
41 #include <xen/interface/physdev.h>
42 #include <xen/interface/vcpu.h>
43 #include <xen/interface/memory.h>
44 #include <xen/interface/nmi.h>
45 #include <xen/interface/xen-mca.h>
46 #include <xen/features.h>
47 #include <xen/page.h>
48 #include <xen/hvc-console.h>
49 #include <xen/acpi.h>
50
51 #include <asm/paravirt.h>
52 #include <asm/apic.h>
53 #include <asm/page.h>
54 #include <asm/xen/pci.h>
55 #include <asm/xen/hypercall.h>
56 #include <asm/xen/hypervisor.h>
57 #include <asm/xen/cpuid.h>
58 #include <asm/fixmap.h>
59 #include <asm/processor.h>
60 #include <asm/proto.h>
61 #include <asm/msr-index.h>
62 #include <asm/traps.h>
63 #include <asm/setup.h>
64 #include <asm/desc.h>
65 #include <asm/pgalloc.h>
66 #include <asm/pgtable.h>
67 #include <asm/tlbflush.h>
68 #include <asm/reboot.h>
69 #include <asm/stackprotector.h>
70 #include <asm/hypervisor.h>
71 #include <asm/mach_traps.h>
72 #include <asm/mwait.h>
73 #include <asm/pci_x86.h>
74 #include <asm/cpu.h>
75
76 #ifdef CONFIG_ACPI
77 #include <linux/acpi.h>
78 #include <asm/acpi.h>
79 #include <acpi/pdc_intel.h>
80 #include <acpi/processor.h>
81 #include <xen/interface/platform.h>
82 #endif
83
84 #include "xen-ops.h"
85 #include "mmu.h"
86 #include "smp.h"
87 #include "multicalls.h"
88 #include "pmu.h"
89
90 void *xen_initial_gdt;
91
92 RESERVE_BRK(shared_info_page_brk, PAGE_SIZE);
93
94 static int xen_cpu_up_prepare_pv(unsigned int cpu);
95 static int xen_cpu_dead_pv(unsigned int cpu);
96
97 struct tls_descs {
98         struct desc_struct desc[3];
99 };
100
101 /*
102  * Updating the 3 TLS descriptors in the GDT on every task switch is
103  * surprisingly expensive so we avoid updating them if they haven't
104  * changed.  Since Xen writes different descriptors than the one
105  * passed in the update_descriptor hypercall we keep shadow copies to
106  * compare against.
107  */
108 static DEFINE_PER_CPU(struct tls_descs, shadow_tls_desc);
109
110 /*
111  * On restore, set the vcpu placement up again.
112  * If it fails, then we're in a bad state, since
113  * we can't back out from using it...
114  */
115 void xen_vcpu_restore(void)
116 {
117         int cpu;
118
119         for_each_possible_cpu(cpu) {
120                 bool other_cpu = (cpu != smp_processor_id());
121                 bool is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up, xen_vcpu_nr(cpu),
122                                                 NULL);
123
124                 if (other_cpu && is_up &&
125                     HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
126                         BUG();
127
128                 xen_setup_runstate_info(cpu);
129
130                 if (xen_have_vcpu_info_placement)
131                         xen_vcpu_setup(cpu);
132
133                 if (other_cpu && is_up &&
134                     HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
135                         BUG();
136         }
137 }
138
139 static void __init xen_banner(void)
140 {
141         unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
142         struct xen_extraversion extra;
143         HYPERVISOR_xen_version(XENVER_extraversion, &extra);
144
145         pr_info("Booting paravirtualized kernel %son %s\n",
146                 xen_feature(XENFEAT_auto_translated_physmap) ?
147                         "with PVH extensions " : "", pv_info.name);
148         printk(KERN_INFO "Xen version: %d.%d%s%s\n",
149                version >> 16, version & 0xffff, extra.extraversion,
150                xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
151 }
152 /* Check if running on Xen version (major, minor) or later */
153 bool
154 xen_running_on_version_or_later(unsigned int major, unsigned int minor)
155 {
156         unsigned int version;
157
158         if (!xen_domain())
159                 return false;
160
161         version = HYPERVISOR_xen_version(XENVER_version, NULL);
162         if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
163                 ((version >> 16) > major))
164                 return true;
165         return false;
166 }
167
168 static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
169 static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
170
171 static __read_mostly unsigned int cpuid_leaf1_ecx_set_mask;
172 static __read_mostly unsigned int cpuid_leaf5_ecx_val;
173 static __read_mostly unsigned int cpuid_leaf5_edx_val;
174
175 static void xen_cpuid(unsigned int *ax, unsigned int *bx,
176                       unsigned int *cx, unsigned int *dx)
177 {
178         unsigned maskebx = ~0;
179         unsigned maskecx = ~0;
180         unsigned maskedx = ~0;
181         unsigned setecx = 0;
182         /*
183          * Mask out inconvenient features, to try and disable as many
184          * unsupported kernel subsystems as possible.
185          */
186         switch (*ax) {
187         case 1:
188                 maskecx = cpuid_leaf1_ecx_mask;
189                 setecx = cpuid_leaf1_ecx_set_mask;
190                 maskedx = cpuid_leaf1_edx_mask;
191                 break;
192
193         case CPUID_MWAIT_LEAF:
194                 /* Synthesize the values.. */
195                 *ax = 0;
196                 *bx = 0;
197                 *cx = cpuid_leaf5_ecx_val;
198                 *dx = cpuid_leaf5_edx_val;
199                 return;
200
201         case 0xb:
202                 /* Suppress extended topology stuff */
203                 maskebx = 0;
204                 break;
205         }
206
207         asm(XEN_EMULATE_PREFIX "cpuid"
208                 : "=a" (*ax),
209                   "=b" (*bx),
210                   "=c" (*cx),
211                   "=d" (*dx)
212                 : "0" (*ax), "2" (*cx));
213
214         *bx &= maskebx;
215         *cx &= maskecx;
216         *cx |= setecx;
217         *dx &= maskedx;
218 }
219 STACK_FRAME_NON_STANDARD(xen_cpuid); /* XEN_EMULATE_PREFIX */
220
221 static bool __init xen_check_mwait(void)
222 {
223 #ifdef CONFIG_ACPI
224         struct xen_platform_op op = {
225                 .cmd                    = XENPF_set_processor_pminfo,
226                 .u.set_pminfo.id        = -1,
227                 .u.set_pminfo.type      = XEN_PM_PDC,
228         };
229         uint32_t buf[3];
230         unsigned int ax, bx, cx, dx;
231         unsigned int mwait_mask;
232
233         /* We need to determine whether it is OK to expose the MWAIT
234          * capability to the kernel to harvest deeper than C3 states from ACPI
235          * _CST using the processor_harvest_xen.c module. For this to work, we
236          * need to gather the MWAIT_LEAF values (which the cstate.c code
237          * checks against). The hypervisor won't expose the MWAIT flag because
238          * it would break backwards compatibility; so we will find out directly
239          * from the hardware and hypercall.
240          */
241         if (!xen_initial_domain())
242                 return false;
243
244         /*
245          * When running under platform earlier than Xen4.2, do not expose
246          * mwait, to avoid the risk of loading native acpi pad driver
247          */
248         if (!xen_running_on_version_or_later(4, 2))
249                 return false;
250
251         ax = 1;
252         cx = 0;
253
254         native_cpuid(&ax, &bx, &cx, &dx);
255
256         mwait_mask = (1 << (X86_FEATURE_EST % 32)) |
257                      (1 << (X86_FEATURE_MWAIT % 32));
258
259         if ((cx & mwait_mask) != mwait_mask)
260                 return false;
261
262         /* We need to emulate the MWAIT_LEAF and for that we need both
263          * ecx and edx. The hypercall provides only partial information.
264          */
265
266         ax = CPUID_MWAIT_LEAF;
267         bx = 0;
268         cx = 0;
269         dx = 0;
270
271         native_cpuid(&ax, &bx, &cx, &dx);
272
273         /* Ask the Hypervisor whether to clear ACPI_PDC_C_C2C3_FFH. If so,
274          * don't expose MWAIT_LEAF and let ACPI pick the IOPORT version of C3.
275          */
276         buf[0] = ACPI_PDC_REVISION_ID;
277         buf[1] = 1;
278         buf[2] = (ACPI_PDC_C_CAPABILITY_SMP | ACPI_PDC_EST_CAPABILITY_SWSMP);
279
280         set_xen_guest_handle(op.u.set_pminfo.pdc, buf);
281
282         if ((HYPERVISOR_platform_op(&op) == 0) &&
283             (buf[2] & (ACPI_PDC_C_C1_FFH | ACPI_PDC_C_C2C3_FFH))) {
284                 cpuid_leaf5_ecx_val = cx;
285                 cpuid_leaf5_edx_val = dx;
286         }
287         return true;
288 #else
289         return false;
290 #endif
291 }
292 static void __init xen_init_cpuid_mask(void)
293 {
294         unsigned int ax, bx, cx, dx;
295         unsigned int xsave_mask;
296
297         if (!xen_initial_domain())
298                 cpuid_leaf1_edx_mask &=
299                         ~((1 << X86_FEATURE_ACPI));  /* disable ACPI */
300
301         cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_X2APIC % 32));
302
303         ax = 1;
304         cx = 0;
305         cpuid(1, &ax, &bx, &cx, &dx);
306
307         xsave_mask =
308                 (1 << (X86_FEATURE_XSAVE % 32)) |
309                 (1 << (X86_FEATURE_OSXSAVE % 32));
310
311         /* Xen will set CR4.OSXSAVE if supported and not disabled by force */
312         if ((cx & xsave_mask) != xsave_mask)
313                 cpuid_leaf1_ecx_mask &= ~xsave_mask; /* disable XSAVE & OSXSAVE */
314         if (xen_check_mwait())
315                 cpuid_leaf1_ecx_set_mask = (1 << (X86_FEATURE_MWAIT % 32));
316 }
317
318 static void __init xen_init_capabilities(void)
319 {
320         setup_clear_cpu_cap(X86_BUG_SYSRET_SS_ATTRS);
321         setup_force_cpu_cap(X86_FEATURE_XENPV);
322         setup_clear_cpu_cap(X86_FEATURE_DCA);
323         setup_clear_cpu_cap(X86_FEATURE_APERFMPERF);
324         setup_clear_cpu_cap(X86_FEATURE_MTRR);
325         setup_clear_cpu_cap(X86_FEATURE_ACC);
326 }
327
328 static void xen_set_debugreg(int reg, unsigned long val)
329 {
330         HYPERVISOR_set_debugreg(reg, val);
331 }
332
333 static unsigned long xen_get_debugreg(int reg)
334 {
335         return HYPERVISOR_get_debugreg(reg);
336 }
337
338 static void xen_end_context_switch(struct task_struct *next)
339 {
340         xen_mc_flush();
341         paravirt_end_context_switch(next);
342 }
343
344 static unsigned long xen_store_tr(void)
345 {
346         return 0;
347 }
348
349 /*
350  * Set the page permissions for a particular virtual address.  If the
351  * address is a vmalloc mapping (or other non-linear mapping), then
352  * find the linear mapping of the page and also set its protections to
353  * match.
354  */
355 static void set_aliased_prot(void *v, pgprot_t prot)
356 {
357         int level;
358         pte_t *ptep;
359         pte_t pte;
360         unsigned long pfn;
361         struct page *page;
362         unsigned char dummy;
363
364         ptep = lookup_address((unsigned long)v, &level);
365         BUG_ON(ptep == NULL);
366
367         pfn = pte_pfn(*ptep);
368         page = pfn_to_page(pfn);
369
370         pte = pfn_pte(pfn, prot);
371
372         /*
373          * Careful: update_va_mapping() will fail if the virtual address
374          * we're poking isn't populated in the page tables.  We don't
375          * need to worry about the direct map (that's always in the page
376          * tables), but we need to be careful about vmap space.  In
377          * particular, the top level page table can lazily propagate
378          * entries between processes, so if we've switched mms since we
379          * vmapped the target in the first place, we might not have the
380          * top-level page table entry populated.
381          *
382          * We disable preemption because we want the same mm active when
383          * we probe the target and when we issue the hypercall.  We'll
384          * have the same nominal mm, but if we're a kernel thread, lazy
385          * mm dropping could change our pgd.
386          *
387          * Out of an abundance of caution, this uses __get_user() to fault
388          * in the target address just in case there's some obscure case
389          * in which the target address isn't readable.
390          */
391
392         preempt_disable();
393
394         probe_kernel_read(&dummy, v, 1);
395
396         if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
397                 BUG();
398
399         if (!PageHighMem(page)) {
400                 void *av = __va(PFN_PHYS(pfn));
401
402                 if (av != v)
403                         if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
404                                 BUG();
405         } else
406                 kmap_flush_unused();
407
408         preempt_enable();
409 }
410
411 static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
412 {
413         const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
414         int i;
415
416         /*
417          * We need to mark the all aliases of the LDT pages RO.  We
418          * don't need to call vm_flush_aliases(), though, since that's
419          * only responsible for flushing aliases out the TLBs, not the
420          * page tables, and Xen will flush the TLB for us if needed.
421          *
422          * To avoid confusing future readers: none of this is necessary
423          * to load the LDT.  The hypervisor only checks this when the
424          * LDT is faulted in due to subsequent descriptor access.
425          */
426
427         for (i = 0; i < entries; i += entries_per_page)
428                 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
429 }
430
431 static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
432 {
433         const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
434         int i;
435
436         for (i = 0; i < entries; i += entries_per_page)
437                 set_aliased_prot(ldt + i, PAGE_KERNEL);
438 }
439
440 static void xen_set_ldt(const void *addr, unsigned entries)
441 {
442         struct mmuext_op *op;
443         struct multicall_space mcs = xen_mc_entry(sizeof(*op));
444
445         trace_xen_cpu_set_ldt(addr, entries);
446
447         op = mcs.args;
448         op->cmd = MMUEXT_SET_LDT;
449         op->arg1.linear_addr = (unsigned long)addr;
450         op->arg2.nr_ents = entries;
451
452         MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
453
454         xen_mc_issue(PARAVIRT_LAZY_CPU);
455 }
456
457 static void xen_load_gdt(const struct desc_ptr *dtr)
458 {
459         unsigned long va = dtr->address;
460         unsigned int size = dtr->size + 1;
461         unsigned pages = DIV_ROUND_UP(size, PAGE_SIZE);
462         unsigned long frames[pages];
463         int f;
464
465         /*
466          * A GDT can be up to 64k in size, which corresponds to 8192
467          * 8-byte entries, or 16 4k pages..
468          */
469
470         BUG_ON(size > 65536);
471         BUG_ON(va & ~PAGE_MASK);
472
473         for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
474                 int level;
475                 pte_t *ptep;
476                 unsigned long pfn, mfn;
477                 void *virt;
478
479                 /*
480                  * The GDT is per-cpu and is in the percpu data area.
481                  * That can be virtually mapped, so we need to do a
482                  * page-walk to get the underlying MFN for the
483                  * hypercall.  The page can also be in the kernel's
484                  * linear range, so we need to RO that mapping too.
485                  */
486                 ptep = lookup_address(va, &level);
487                 BUG_ON(ptep == NULL);
488
489                 pfn = pte_pfn(*ptep);
490                 mfn = pfn_to_mfn(pfn);
491                 virt = __va(PFN_PHYS(pfn));
492
493                 frames[f] = mfn;
494
495                 make_lowmem_page_readonly((void *)va);
496                 make_lowmem_page_readonly(virt);
497         }
498
499         if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
500                 BUG();
501 }
502
503 /*
504  * load_gdt for early boot, when the gdt is only mapped once
505  */
506 static void __init xen_load_gdt_boot(const struct desc_ptr *dtr)
507 {
508         unsigned long va = dtr->address;
509         unsigned int size = dtr->size + 1;
510         unsigned pages = DIV_ROUND_UP(size, PAGE_SIZE);
511         unsigned long frames[pages];
512         int f;
513
514         /*
515          * A GDT can be up to 64k in size, which corresponds to 8192
516          * 8-byte entries, or 16 4k pages..
517          */
518
519         BUG_ON(size > 65536);
520         BUG_ON(va & ~PAGE_MASK);
521
522         for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
523                 pte_t pte;
524                 unsigned long pfn, mfn;
525
526                 pfn = virt_to_pfn(va);
527                 mfn = pfn_to_mfn(pfn);
528
529                 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
530
531                 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
532                         BUG();
533
534                 frames[f] = mfn;
535         }
536
537         if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
538                 BUG();
539 }
540
541 static inline bool desc_equal(const struct desc_struct *d1,
542                               const struct desc_struct *d2)
543 {
544         return d1->a == d2->a && d1->b == d2->b;
545 }
546
547 static void load_TLS_descriptor(struct thread_struct *t,
548                                 unsigned int cpu, unsigned int i)
549 {
550         struct desc_struct *shadow = &per_cpu(shadow_tls_desc, cpu).desc[i];
551         struct desc_struct *gdt;
552         xmaddr_t maddr;
553         struct multicall_space mc;
554
555         if (desc_equal(shadow, &t->tls_array[i]))
556                 return;
557
558         *shadow = t->tls_array[i];
559
560         gdt = get_cpu_gdt_rw(cpu);
561         maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
562         mc = __xen_mc_entry(0);
563
564         MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
565 }
566
567 static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
568 {
569         /*
570          * XXX sleazy hack: If we're being called in a lazy-cpu zone
571          * and lazy gs handling is enabled, it means we're in a
572          * context switch, and %gs has just been saved.  This means we
573          * can zero it out to prevent faults on exit from the
574          * hypervisor if the next process has no %gs.  Either way, it
575          * has been saved, and the new value will get loaded properly.
576          * This will go away as soon as Xen has been modified to not
577          * save/restore %gs for normal hypercalls.
578          *
579          * On x86_64, this hack is not used for %gs, because gs points
580          * to KERNEL_GS_BASE (and uses it for PDA references), so we
581          * must not zero %gs on x86_64
582          *
583          * For x86_64, we need to zero %fs, otherwise we may get an
584          * exception between the new %fs descriptor being loaded and
585          * %fs being effectively cleared at __switch_to().
586          */
587         if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
588 #ifdef CONFIG_X86_32
589                 lazy_load_gs(0);
590 #else
591                 loadsegment(fs, 0);
592 #endif
593         }
594
595         xen_mc_batch();
596
597         load_TLS_descriptor(t, cpu, 0);
598         load_TLS_descriptor(t, cpu, 1);
599         load_TLS_descriptor(t, cpu, 2);
600
601         xen_mc_issue(PARAVIRT_LAZY_CPU);
602 }
603
604 #ifdef CONFIG_X86_64
605 static void xen_load_gs_index(unsigned int idx)
606 {
607         if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
608                 BUG();
609 }
610 #endif
611
612 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
613                                 const void *ptr)
614 {
615         xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
616         u64 entry = *(u64 *)ptr;
617
618         trace_xen_cpu_write_ldt_entry(dt, entrynum, entry);
619
620         preempt_disable();
621
622         xen_mc_flush();
623         if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
624                 BUG();
625
626         preempt_enable();
627 }
628
629 static int cvt_gate_to_trap(int vector, const gate_desc *val,
630                             struct trap_info *info)
631 {
632         unsigned long addr;
633
634         if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
635                 return 0;
636
637         info->vector = vector;
638
639         addr = gate_offset(*val);
640 #ifdef CONFIG_X86_64
641         /*
642          * Look for known traps using IST, and substitute them
643          * appropriately.  The debugger ones are the only ones we care
644          * about.  Xen will handle faults like double_fault,
645          * so we should never see them.  Warn if
646          * there's an unexpected IST-using fault handler.
647          */
648         if (addr == (unsigned long)debug)
649                 addr = (unsigned long)xen_debug;
650         else if (addr == (unsigned long)int3)
651                 addr = (unsigned long)xen_int3;
652         else if (addr == (unsigned long)stack_segment)
653                 addr = (unsigned long)xen_stack_segment;
654         else if (addr == (unsigned long)double_fault) {
655                 /* Don't need to handle these */
656                 return 0;
657 #ifdef CONFIG_X86_MCE
658         } else if (addr == (unsigned long)machine_check) {
659                 /*
660                  * when xen hypervisor inject vMCE to guest,
661                  * use native mce handler to handle it
662                  */
663                 ;
664 #endif
665         } else if (addr == (unsigned long)nmi)
666                 /*
667                  * Use the native version as well.
668                  */
669                 ;
670         else {
671                 /* Some other trap using IST? */
672                 if (WARN_ON(val->ist != 0))
673                         return 0;
674         }
675 #endif  /* CONFIG_X86_64 */
676         info->address = addr;
677
678         info->cs = gate_segment(*val);
679         info->flags = val->dpl;
680         /* interrupt gates clear IF */
681         if (val->type == GATE_INTERRUPT)
682                 info->flags |= 1 << 2;
683
684         return 1;
685 }
686
687 /* Locations of each CPU's IDT */
688 static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
689
690 /* Set an IDT entry.  If the entry is part of the current IDT, then
691    also update Xen. */
692 static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
693 {
694         unsigned long p = (unsigned long)&dt[entrynum];
695         unsigned long start, end;
696
697         trace_xen_cpu_write_idt_entry(dt, entrynum, g);
698
699         preempt_disable();
700
701         start = __this_cpu_read(idt_desc.address);
702         end = start + __this_cpu_read(idt_desc.size) + 1;
703
704         xen_mc_flush();
705
706         native_write_idt_entry(dt, entrynum, g);
707
708         if (p >= start && (p + 8) <= end) {
709                 struct trap_info info[2];
710
711                 info[1].address = 0;
712
713                 if (cvt_gate_to_trap(entrynum, g, &info[0]))
714                         if (HYPERVISOR_set_trap_table(info))
715                                 BUG();
716         }
717
718         preempt_enable();
719 }
720
721 static void xen_convert_trap_info(const struct desc_ptr *desc,
722                                   struct trap_info *traps)
723 {
724         unsigned in, out, count;
725
726         count = (desc->size+1) / sizeof(gate_desc);
727         BUG_ON(count > 256);
728
729         for (in = out = 0; in < count; in++) {
730                 gate_desc *entry = (gate_desc *)(desc->address) + in;
731
732                 if (cvt_gate_to_trap(in, entry, &traps[out]))
733                         out++;
734         }
735         traps[out].address = 0;
736 }
737
738 void xen_copy_trap_info(struct trap_info *traps)
739 {
740         const struct desc_ptr *desc = this_cpu_ptr(&idt_desc);
741
742         xen_convert_trap_info(desc, traps);
743 }
744
745 /* Load a new IDT into Xen.  In principle this can be per-CPU, so we
746    hold a spinlock to protect the static traps[] array (static because
747    it avoids allocation, and saves stack space). */
748 static void xen_load_idt(const struct desc_ptr *desc)
749 {
750         static DEFINE_SPINLOCK(lock);
751         static struct trap_info traps[257];
752
753         trace_xen_cpu_load_idt(desc);
754
755         spin_lock(&lock);
756
757         memcpy(this_cpu_ptr(&idt_desc), desc, sizeof(idt_desc));
758
759         xen_convert_trap_info(desc, traps);
760
761         xen_mc_flush();
762         if (HYPERVISOR_set_trap_table(traps))
763                 BUG();
764
765         spin_unlock(&lock);
766 }
767
768 /* Write a GDT descriptor entry.  Ignore LDT descriptors, since
769    they're handled differently. */
770 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
771                                 const void *desc, int type)
772 {
773         trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
774
775         preempt_disable();
776
777         switch (type) {
778         case DESC_LDT:
779         case DESC_TSS:
780                 /* ignore */
781                 break;
782
783         default: {
784                 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
785
786                 xen_mc_flush();
787                 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
788                         BUG();
789         }
790
791         }
792
793         preempt_enable();
794 }
795
796 /*
797  * Version of write_gdt_entry for use at early boot-time needed to
798  * update an entry as simply as possible.
799  */
800 static void __init xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
801                                             const void *desc, int type)
802 {
803         trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
804
805         switch (type) {
806         case DESC_LDT:
807         case DESC_TSS:
808                 /* ignore */
809                 break;
810
811         default: {
812                 xmaddr_t maddr = virt_to_machine(&dt[entry]);
813
814                 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
815                         dt[entry] = *(struct desc_struct *)desc;
816         }
817
818         }
819 }
820
821 static void xen_load_sp0(struct tss_struct *tss,
822                          struct thread_struct *thread)
823 {
824         struct multicall_space mcs;
825
826         mcs = xen_mc_entry(0);
827         MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
828         xen_mc_issue(PARAVIRT_LAZY_CPU);
829         tss->x86_tss.sp0 = thread->sp0;
830 }
831
832 void xen_set_iopl_mask(unsigned mask)
833 {
834         struct physdev_set_iopl set_iopl;
835
836         /* Force the change at ring 0. */
837         set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
838         HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
839 }
840
841 static void xen_io_delay(void)
842 {
843 }
844
845 static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
846
847 static unsigned long xen_read_cr0(void)
848 {
849         unsigned long cr0 = this_cpu_read(xen_cr0_value);
850
851         if (unlikely(cr0 == 0)) {
852                 cr0 = native_read_cr0();
853                 this_cpu_write(xen_cr0_value, cr0);
854         }
855
856         return cr0;
857 }
858
859 static void xen_write_cr0(unsigned long cr0)
860 {
861         struct multicall_space mcs;
862
863         this_cpu_write(xen_cr0_value, cr0);
864
865         /* Only pay attention to cr0.TS; everything else is
866            ignored. */
867         mcs = xen_mc_entry(0);
868
869         MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
870
871         xen_mc_issue(PARAVIRT_LAZY_CPU);
872 }
873
874 static void xen_write_cr4(unsigned long cr4)
875 {
876         cr4 &= ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PCE);
877
878         native_write_cr4(cr4);
879 }
880 #ifdef CONFIG_X86_64
881 static inline unsigned long xen_read_cr8(void)
882 {
883         return 0;
884 }
885 static inline void xen_write_cr8(unsigned long val)
886 {
887         BUG_ON(val);
888 }
889 #endif
890
891 static u64 xen_read_msr_safe(unsigned int msr, int *err)
892 {
893         u64 val;
894
895         if (pmu_msr_read(msr, &val, err))
896                 return val;
897
898         val = native_read_msr_safe(msr, err);
899         switch (msr) {
900         case MSR_IA32_APICBASE:
901 #ifdef CONFIG_X86_X2APIC
902                 if (!(cpuid_ecx(1) & (1 << (X86_FEATURE_X2APIC & 31))))
903 #endif
904                         val &= ~X2APIC_ENABLE;
905                 break;
906         }
907         return val;
908 }
909
910 static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
911 {
912         int ret;
913
914         ret = 0;
915
916         switch (msr) {
917 #ifdef CONFIG_X86_64
918                 unsigned which;
919                 u64 base;
920
921         case MSR_FS_BASE:               which = SEGBASE_FS; goto set;
922         case MSR_KERNEL_GS_BASE:        which = SEGBASE_GS_USER; goto set;
923         case MSR_GS_BASE:               which = SEGBASE_GS_KERNEL; goto set;
924
925         set:
926                 base = ((u64)high << 32) | low;
927                 if (HYPERVISOR_set_segment_base(which, base) != 0)
928                         ret = -EIO;
929                 break;
930 #endif
931
932         case MSR_STAR:
933         case MSR_CSTAR:
934         case MSR_LSTAR:
935         case MSR_SYSCALL_MASK:
936         case MSR_IA32_SYSENTER_CS:
937         case MSR_IA32_SYSENTER_ESP:
938         case MSR_IA32_SYSENTER_EIP:
939                 /* Fast syscall setup is all done in hypercalls, so
940                    these are all ignored.  Stub them out here to stop
941                    Xen console noise. */
942                 break;
943
944         default:
945                 if (!pmu_msr_write(msr, low, high, &ret))
946                         ret = native_write_msr_safe(msr, low, high);
947         }
948
949         return ret;
950 }
951
952 static u64 xen_read_msr(unsigned int msr)
953 {
954         /*
955          * This will silently swallow a #GP from RDMSR.  It may be worth
956          * changing that.
957          */
958         int err;
959
960         return xen_read_msr_safe(msr, &err);
961 }
962
963 static void xen_write_msr(unsigned int msr, unsigned low, unsigned high)
964 {
965         /*
966          * This will silently swallow a #GP from WRMSR.  It may be worth
967          * changing that.
968          */
969         xen_write_msr_safe(msr, low, high);
970 }
971
972 void xen_setup_shared_info(void)
973 {
974         if (!xen_feature(XENFEAT_auto_translated_physmap)) {
975                 set_fixmap(FIX_PARAVIRT_BOOTMAP,
976                            xen_start_info->shared_info);
977
978                 HYPERVISOR_shared_info =
979                         (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
980         } else
981                 HYPERVISOR_shared_info =
982                         (struct shared_info *)__va(xen_start_info->shared_info);
983
984 #ifndef CONFIG_SMP
985         /* In UP this is as good a place as any to set up shared info */
986         xen_setup_vcpu_info_placement();
987 #endif
988
989         xen_setup_mfn_list_list();
990 }
991
992 /* This is called once we have the cpu_possible_mask */
993 void xen_setup_vcpu_info_placement(void)
994 {
995         int cpu;
996
997         for_each_possible_cpu(cpu) {
998                 /* Set up direct vCPU id mapping for PV guests. */
999                 per_cpu(xen_vcpu_id, cpu) = cpu;
1000                 xen_vcpu_setup(cpu);
1001         }
1002
1003         /*
1004          * xen_vcpu_setup managed to place the vcpu_info within the
1005          * percpu area for all cpus, so make use of it.
1006          */
1007         if (xen_have_vcpu_info_placement) {
1008                 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
1009                 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
1010                 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
1011                 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
1012                 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
1013         }
1014 }
1015
1016 static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
1017                           unsigned long addr, unsigned len)
1018 {
1019         char *start, *end, *reloc;
1020         unsigned ret;
1021
1022         start = end = reloc = NULL;
1023
1024 #define SITE(op, x)                                                     \
1025         case PARAVIRT_PATCH(op.x):                                      \
1026         if (xen_have_vcpu_info_placement) {                             \
1027                 start = (char *)xen_##x##_direct;                       \
1028                 end = xen_##x##_direct_end;                             \
1029                 reloc = xen_##x##_direct_reloc;                         \
1030         }                                                               \
1031         goto patch_site
1032
1033         switch (type) {
1034                 SITE(pv_irq_ops, irq_enable);
1035                 SITE(pv_irq_ops, irq_disable);
1036                 SITE(pv_irq_ops, save_fl);
1037                 SITE(pv_irq_ops, restore_fl);
1038 #undef SITE
1039
1040         patch_site:
1041                 if (start == NULL || (end-start) > len)
1042                         goto default_patch;
1043
1044                 ret = paravirt_patch_insns(insnbuf, len, start, end);
1045
1046                 /* Note: because reloc is assigned from something that
1047                    appears to be an array, gcc assumes it's non-null,
1048                    but doesn't know its relationship with start and
1049                    end. */
1050                 if (reloc > start && reloc < end) {
1051                         int reloc_off = reloc - start;
1052                         long *relocp = (long *)(insnbuf + reloc_off);
1053                         long delta = start - (char *)addr;
1054
1055                         *relocp += delta;
1056                 }
1057                 break;
1058
1059         default_patch:
1060         default:
1061                 ret = paravirt_patch_default(type, clobbers, insnbuf,
1062                                              addr, len);
1063                 break;
1064         }
1065
1066         return ret;
1067 }
1068
1069 static const struct pv_info xen_info __initconst = {
1070         .shared_kernel_pmd = 0,
1071
1072 #ifdef CONFIG_X86_64
1073         .extra_user_64bit_cs = FLAT_USER_CS64,
1074 #endif
1075         .name = "Xen",
1076 };
1077
1078 static const struct pv_init_ops xen_init_ops __initconst = {
1079         .patch = xen_patch,
1080 };
1081
1082 static const struct pv_cpu_ops xen_cpu_ops __initconst = {
1083         .cpuid = xen_cpuid,
1084
1085         .set_debugreg = xen_set_debugreg,
1086         .get_debugreg = xen_get_debugreg,
1087
1088         .read_cr0 = xen_read_cr0,
1089         .write_cr0 = xen_write_cr0,
1090
1091         .read_cr4 = native_read_cr4,
1092         .write_cr4 = xen_write_cr4,
1093
1094 #ifdef CONFIG_X86_64
1095         .read_cr8 = xen_read_cr8,
1096         .write_cr8 = xen_write_cr8,
1097 #endif
1098
1099         .wbinvd = native_wbinvd,
1100
1101         .read_msr = xen_read_msr,
1102         .write_msr = xen_write_msr,
1103
1104         .read_msr_safe = xen_read_msr_safe,
1105         .write_msr_safe = xen_write_msr_safe,
1106
1107         .read_pmc = xen_read_pmc,
1108
1109         .iret = xen_iret,
1110 #ifdef CONFIG_X86_64
1111         .usergs_sysret64 = xen_sysret64,
1112 #endif
1113
1114         .load_tr_desc = paravirt_nop,
1115         .set_ldt = xen_set_ldt,
1116         .load_gdt = xen_load_gdt,
1117         .load_idt = xen_load_idt,
1118         .load_tls = xen_load_tls,
1119 #ifdef CONFIG_X86_64
1120         .load_gs_index = xen_load_gs_index,
1121 #endif
1122
1123         .alloc_ldt = xen_alloc_ldt,
1124         .free_ldt = xen_free_ldt,
1125
1126         .store_idt = native_store_idt,
1127         .store_tr = xen_store_tr,
1128
1129         .write_ldt_entry = xen_write_ldt_entry,
1130         .write_gdt_entry = xen_write_gdt_entry,
1131         .write_idt_entry = xen_write_idt_entry,
1132         .load_sp0 = xen_load_sp0,
1133
1134         .set_iopl_mask = xen_set_iopl_mask,
1135         .io_delay = xen_io_delay,
1136
1137         /* Xen takes care of %gs when switching to usermode for us */
1138         .swapgs = paravirt_nop,
1139
1140         .start_context_switch = paravirt_start_context_switch,
1141         .end_context_switch = xen_end_context_switch,
1142 };
1143
1144 static void xen_restart(char *msg)
1145 {
1146         xen_reboot(SHUTDOWN_reboot);
1147 }
1148
1149 static void xen_machine_halt(void)
1150 {
1151         xen_reboot(SHUTDOWN_poweroff);
1152 }
1153
1154 static void xen_machine_power_off(void)
1155 {
1156         if (pm_power_off)
1157                 pm_power_off();
1158         xen_reboot(SHUTDOWN_poweroff);
1159 }
1160
1161 static void xen_crash_shutdown(struct pt_regs *regs)
1162 {
1163         xen_reboot(SHUTDOWN_crash);
1164 }
1165
1166 static const struct machine_ops xen_machine_ops __initconst = {
1167         .restart = xen_restart,
1168         .halt = xen_machine_halt,
1169         .power_off = xen_machine_power_off,
1170         .shutdown = xen_machine_halt,
1171         .crash_shutdown = xen_crash_shutdown,
1172         .emergency_restart = xen_emergency_restart,
1173 };
1174
1175 static unsigned char xen_get_nmi_reason(void)
1176 {
1177         unsigned char reason = 0;
1178
1179         /* Construct a value which looks like it came from port 0x61. */
1180         if (test_bit(_XEN_NMIREASON_io_error,
1181                      &HYPERVISOR_shared_info->arch.nmi_reason))
1182                 reason |= NMI_REASON_IOCHK;
1183         if (test_bit(_XEN_NMIREASON_pci_serr,
1184                      &HYPERVISOR_shared_info->arch.nmi_reason))
1185                 reason |= NMI_REASON_SERR;
1186
1187         return reason;
1188 }
1189
1190 static void __init xen_boot_params_init_edd(void)
1191 {
1192 #if IS_ENABLED(CONFIG_EDD)
1193         struct xen_platform_op op;
1194         struct edd_info *edd_info;
1195         u32 *mbr_signature;
1196         unsigned nr;
1197         int ret;
1198
1199         edd_info = boot_params.eddbuf;
1200         mbr_signature = boot_params.edd_mbr_sig_buffer;
1201
1202         op.cmd = XENPF_firmware_info;
1203
1204         op.u.firmware_info.type = XEN_FW_DISK_INFO;
1205         for (nr = 0; nr < EDDMAXNR; nr++) {
1206                 struct edd_info *info = edd_info + nr;
1207
1208                 op.u.firmware_info.index = nr;
1209                 info->params.length = sizeof(info->params);
1210                 set_xen_guest_handle(op.u.firmware_info.u.disk_info.edd_params,
1211                                      &info->params);
1212                 ret = HYPERVISOR_platform_op(&op);
1213                 if (ret)
1214                         break;
1215
1216 #define C(x) info->x = op.u.firmware_info.u.disk_info.x
1217                 C(device);
1218                 C(version);
1219                 C(interface_support);
1220                 C(legacy_max_cylinder);
1221                 C(legacy_max_head);
1222                 C(legacy_sectors_per_track);
1223 #undef C
1224         }
1225         boot_params.eddbuf_entries = nr;
1226
1227         op.u.firmware_info.type = XEN_FW_DISK_MBR_SIGNATURE;
1228         for (nr = 0; nr < EDD_MBR_SIG_MAX; nr++) {
1229                 op.u.firmware_info.index = nr;
1230                 ret = HYPERVISOR_platform_op(&op);
1231                 if (ret)
1232                         break;
1233                 mbr_signature[nr] = op.u.firmware_info.u.disk_mbr_signature.mbr_signature;
1234         }
1235         boot_params.edd_mbr_sig_buf_entries = nr;
1236 #endif
1237 }
1238
1239 /*
1240  * Set up the GDT and segment registers for -fstack-protector.  Until
1241  * we do this, we have to be careful not to call any stack-protected
1242  * function, which is most of the kernel.
1243  */
1244 static void xen_setup_gdt(int cpu)
1245 {
1246         pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1247         pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1248
1249         setup_stack_canary_segment(0);
1250         switch_to_new_gdt(0);
1251
1252         pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1253         pv_cpu_ops.load_gdt = xen_load_gdt;
1254 }
1255
1256 static void __init xen_dom0_set_legacy_features(void)
1257 {
1258         x86_platform.legacy.rtc = 1;
1259 }
1260
1261 /* First C function to be called on Xen boot */
1262 asmlinkage __visible void __init xen_start_kernel(void)
1263 {
1264         struct physdev_set_iopl set_iopl;
1265         unsigned long initrd_start = 0;
1266         int rc;
1267
1268         if (!xen_start_info)
1269                 return;
1270
1271         xen_domain_type = XEN_PV_DOMAIN;
1272
1273         xen_setup_features();
1274
1275         xen_setup_machphys_mapping();
1276
1277         /* Install Xen paravirt ops */
1278         pv_info = xen_info;
1279         pv_init_ops = xen_init_ops;
1280         pv_cpu_ops = xen_cpu_ops;
1281
1282         x86_platform.get_nmi_reason = xen_get_nmi_reason;
1283
1284         x86_init.resources.memory_setup = xen_memory_setup;
1285         x86_init.oem.arch_setup = xen_arch_setup;
1286         x86_init.oem.banner = xen_banner;
1287
1288         xen_init_time_ops();
1289
1290         /*
1291          * Set up some pagetable state before starting to set any ptes.
1292          */
1293
1294         xen_init_mmu_ops();
1295
1296         /* Prevent unwanted bits from being set in PTEs. */
1297         __supported_pte_mask &= ~_PAGE_GLOBAL;
1298
1299         /*
1300          * Prevent page tables from being allocated in highmem, even
1301          * if CONFIG_HIGHPTE is enabled.
1302          */
1303         __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
1304
1305         /* Work out if we support NX */
1306         x86_configure_nx();
1307
1308         /* Get mfn list */
1309         xen_build_dynamic_phys_to_machine();
1310
1311         /*
1312          * Set up kernel GDT and segment registers, mainly so that
1313          * -fstack-protector code can be executed.
1314          */
1315         xen_setup_gdt(0);
1316
1317         xen_init_irq_ops();
1318         xen_init_cpuid_mask();
1319         xen_init_capabilities();
1320
1321 #ifdef CONFIG_X86_LOCAL_APIC
1322         /*
1323          * set up the basic apic ops.
1324          */
1325         xen_init_apic();
1326 #endif
1327
1328         if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1329                 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1330                 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1331         }
1332
1333         machine_ops = xen_machine_ops;
1334
1335         /*
1336          * The only reliable way to retain the initial address of the
1337          * percpu gdt_page is to remember it here, so we can go and
1338          * mark it RW later, when the initial percpu area is freed.
1339          */
1340         xen_initial_gdt = &per_cpu(gdt_page, 0);
1341
1342         xen_smp_init();
1343
1344 #ifdef CONFIG_ACPI_NUMA
1345         /*
1346          * The pages we from Xen are not related to machine pages, so
1347          * any NUMA information the kernel tries to get from ACPI will
1348          * be meaningless.  Prevent it from trying.
1349          */
1350         acpi_numa = -1;
1351 #endif
1352         /* Don't do the full vcpu_info placement stuff until we have a
1353            possible map and a non-dummy shared_info. */
1354         per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1355
1356         WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_pv, xen_cpu_dead_pv));
1357
1358         local_irq_disable();
1359         early_boot_irqs_disabled = true;
1360
1361         xen_raw_console_write("mapping kernel into physical memory\n");
1362         xen_setup_kernel_pagetable((pgd_t *)xen_start_info->pt_base,
1363                                    xen_start_info->nr_pages);
1364         xen_reserve_special_pages();
1365
1366         /* keep using Xen gdt for now; no urgent need to change it */
1367
1368 #ifdef CONFIG_X86_32
1369         pv_info.kernel_rpl = 1;
1370         if (xen_feature(XENFEAT_supervisor_mode_kernel))
1371                 pv_info.kernel_rpl = 0;
1372 #else
1373         pv_info.kernel_rpl = 0;
1374 #endif
1375         /* set the limit of our address space */
1376         xen_reserve_top();
1377
1378         /*
1379          * We used to do this in xen_arch_setup, but that is too late
1380          * on AMD were early_cpu_init (run before ->arch_setup()) calls
1381          * early_amd_init which pokes 0xcf8 port.
1382          */
1383         set_iopl.iopl = 1;
1384         rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
1385         if (rc != 0)
1386                 xen_raw_printk("physdev_op failed %d\n", rc);
1387
1388 #ifdef CONFIG_X86_32
1389         /* set up basic CPUID stuff */
1390         cpu_detect(&new_cpu_data);
1391         set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
1392         new_cpu_data.x86_capability[CPUID_1_EDX] = cpuid_edx(1);
1393 #endif
1394
1395         if (xen_start_info->mod_start) {
1396             if (xen_start_info->flags & SIF_MOD_START_PFN)
1397                 initrd_start = PFN_PHYS(xen_start_info->mod_start);
1398             else
1399                 initrd_start = __pa(xen_start_info->mod_start);
1400         }
1401
1402         /* Poke various useful things into boot_params */
1403         boot_params.hdr.type_of_loader = (9 << 4) | 0;
1404         boot_params.hdr.ramdisk_image = initrd_start;
1405         boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
1406         boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
1407         boot_params.hdr.hardware_subarch = X86_SUBARCH_XEN;
1408
1409         if (!xen_initial_domain()) {
1410                 add_preferred_console("xenboot", 0, NULL);
1411                 add_preferred_console("tty", 0, NULL);
1412                 add_preferred_console("hvc", 0, NULL);
1413                 if (pci_xen)
1414                         x86_init.pci.arch_init = pci_xen_init;
1415         } else {
1416                 const struct dom0_vga_console_info *info =
1417                         (void *)((char *)xen_start_info +
1418                                  xen_start_info->console.dom0.info_off);
1419                 struct xen_platform_op op = {
1420                         .cmd = XENPF_firmware_info,
1421                         .interface_version = XENPF_INTERFACE_VERSION,
1422                         .u.firmware_info.type = XEN_FW_KBD_SHIFT_FLAGS,
1423                 };
1424
1425                 x86_platform.set_legacy_features =
1426                                 xen_dom0_set_legacy_features;
1427                 xen_init_vga(info, xen_start_info->console.dom0.info_size);
1428                 xen_start_info->console.domU.mfn = 0;
1429                 xen_start_info->console.domU.evtchn = 0;
1430
1431                 if (HYPERVISOR_platform_op(&op) == 0)
1432                         boot_params.kbd_status = op.u.firmware_info.u.kbd_shift_flags;
1433
1434                 /* Make sure ACS will be enabled */
1435                 pci_request_acs();
1436
1437                 xen_acpi_sleep_register();
1438
1439                 /* Avoid searching for BIOS MP tables */
1440                 x86_init.mpparse.find_smp_config = x86_init_noop;
1441                 x86_init.mpparse.get_smp_config = x86_init_uint_noop;
1442
1443                 xen_boot_params_init_edd();
1444         }
1445 #ifdef CONFIG_PCI
1446         /* PCI BIOS service won't work from a PV guest. */
1447         pci_probe &= ~PCI_PROBE_BIOS;
1448 #endif
1449         xen_raw_console_write("about to get started...\n");
1450
1451         /* Let's presume PV guests always boot on vCPU with id 0. */
1452         per_cpu(xen_vcpu_id, 0) = 0;
1453
1454         xen_setup_runstate_info(0);
1455
1456         xen_efi_init();
1457
1458         /* Start the world */
1459 #ifdef CONFIG_X86_32
1460         i386_start_kernel();
1461 #else
1462         cr4_init_shadow(); /* 32b kernel does this in i386_start_kernel() */
1463         x86_64_start_reservations((char *)__pa_symbol(&boot_params));
1464 #endif
1465 }
1466
1467 static int xen_cpu_up_prepare_pv(unsigned int cpu)
1468 {
1469         int rc;
1470
1471         xen_setup_timer(cpu);
1472
1473         rc = xen_smp_intr_init(cpu);
1474         if (rc) {
1475                 WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
1476                      cpu, rc);
1477                 return rc;
1478         }
1479
1480         rc = xen_smp_intr_init_pv(cpu);
1481         if (rc) {
1482                 WARN(1, "xen_smp_intr_init_pv() for CPU %d failed: %d\n",
1483                      cpu, rc);
1484                 return rc;
1485         }
1486
1487         return 0;
1488 }
1489
1490 static int xen_cpu_dead_pv(unsigned int cpu)
1491 {
1492         xen_smp_intr_free(cpu);
1493         xen_smp_intr_free_pv(cpu);
1494
1495         xen_teardown_timer(cpu);
1496
1497         return 0;
1498 }
1499
1500 static uint32_t __init xen_platform_pv(void)
1501 {
1502         if (xen_pv_domain())
1503                 return xen_cpuid_base();
1504
1505         return 0;
1506 }
1507
1508 const struct hypervisor_x86 x86_hyper_xen_pv = {
1509         .name                   = "Xen PV",
1510         .detect                 = xen_platform_pv,
1511         .pin_vcpu               = xen_pin_vcpu,
1512 };
1513 EXPORT_SYMBOL(x86_hyper_xen_pv);