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