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