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