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