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