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