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