1 /*
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/module.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
36 #ifdef CONFIG_KEXEC_CORE
37 #include <linux/kexec.h>
38 #endif
39
40 #include <xen/xen.h>
41 #include <xen/events.h>
42 #include <xen/interface/xen.h>
43 #include <xen/interface/version.h>
44 #include <xen/interface/physdev.h>
45 #include <xen/interface/vcpu.h>
46 #include <xen/interface/memory.h>
47 #include <xen/interface/xen-mca.h>
48 #include <xen/features.h>
49 #include <xen/page.h>
50 #include <xen/hvm.h>
51 #include <xen/hvc-console.h>
52 #include <xen/acpi.h>
53
54 #include <asm/paravirt.h>
55 #include <asm/apic.h>
56 #include <asm/page.h>
57 #include <asm/xen/pci.h>
58 #include <asm/xen/hypercall.h>
59 #include <asm/xen/hypervisor.h>
60 #include <asm/fixmap.h>
61 #include <asm/processor.h>
62 #include <asm/proto.h>
63 #include <asm/msr-index.h>
64 #include <asm/traps.h>
65 #include <asm/setup.h>
66 #include <asm/desc.h>
67 #include <asm/pgalloc.h>
68 #include <asm/pgtable.h>
69 #include <asm/tlbflush.h>
70 #include <asm/reboot.h>
71 #include <asm/stackprotector.h>
72 #include <asm/hypervisor.h>
73 #include <asm/mwait.h>
74 #include <asm/pci_x86.h>
75 #include <asm/pat.h>
76
77 #ifdef CONFIG_ACPI
78 #include <linux/acpi.h>
79 #include <asm/acpi.h>
80 #include <acpi/pdc_intel.h>
81 #include <acpi/processor.h>
82 #include <xen/interface/platform.h>
83 #endif
84
85 #include "xen-ops.h"
86 #include "mmu.h"
87 #include "smp.h"
88 #include "multicalls.h"
89
90 EXPORT_SYMBOL_GPL(hypercall_page);
91
92 /*
93 * Pointer to the xen_vcpu_info structure or
94 * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
95 * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
96 * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point
97 * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to
98 * acknowledge pending events.
99 * Also more subtly it is used by the patched version of irq enable/disable
100 * e.g. xen_irq_enable_direct and xen_iret in PV mode.
101 *
102 * The desire to be able to do those mask/unmask operations as a single
103 * instruction by using the per-cpu offset held in %gs is the real reason
104 * vcpu info is in a per-cpu pointer and the original reason for this
105 * hypercall.
106 *
107 */
108 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
109
110 /*
111 * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info
112 * hypercall. This can be used both in PV and PVHVM mode. The structure
113 * overrides the default per_cpu(xen_vcpu, cpu) value.
114 */
115 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
116
117 enum xen_domain_type xen_domain_type = XEN_NATIVE;
118 EXPORT_SYMBOL_GPL(xen_domain_type);
119
120 unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
121 EXPORT_SYMBOL(machine_to_phys_mapping);
122 unsigned long machine_to_phys_nr;
123 EXPORT_SYMBOL(machine_to_phys_nr);
124
125 struct start_info *xen_start_info;
126 EXPORT_SYMBOL_GPL(xen_start_info);
127
128 struct shared_info xen_dummy_shared_info;
129
130 void *xen_initial_gdt;
131
132 RESERVE_BRK(shared_info_page_brk, PAGE_SIZE);
133 __read_mostly int xen_have_vector_callback;
134 EXPORT_SYMBOL_GPL(xen_have_vector_callback);
135
136 /*
137 * Point at some empty memory to start with. We map the real shared_info
138 * page as soon as fixmap is up and running.
139 */
140 struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
141
142 /*
143 * Flag to determine whether vcpu info placement is available on all
144 * VCPUs. We assume it is to start with, and then set it to zero on
145 * the first failure. This is because it can succeed on some VCPUs
146 * and not others, since it can involve hypervisor memory allocation,
147 * or because the guest failed to guarantee all the appropriate
148 * constraints on all VCPUs (ie buffer can't cross a page boundary).
149 *
150 * Note that any particular CPU may be using a placed vcpu structure,
151 * but we can only optimise if the all are.
152 *
153 * 0: not available, 1: available
154 */
155 static int have_vcpu_info_placement = 1;
156
157 struct tls_descs {
158 struct desc_struct desc[3];
159 };
160
161 /*
162 * Updating the 3 TLS descriptors in the GDT on every task switch is
163 * surprisingly expensive so we avoid updating them if they haven't
164 * changed. Since Xen writes different descriptors than the one
165 * passed in the update_descriptor hypercall we keep shadow copies to
166 * compare against.
167 */
168 static DEFINE_PER_CPU(struct tls_descs, shadow_tls_desc);
169
clamp_max_cpus(void)170 static void clamp_max_cpus(void)
171 {
172 #ifdef CONFIG_SMP
173 if (setup_max_cpus > MAX_VIRT_CPUS)
174 setup_max_cpus = MAX_VIRT_CPUS;
175 #endif
176 }
177
xen_vcpu_setup(int cpu)178 static void xen_vcpu_setup(int cpu)
179 {
180 struct vcpu_register_vcpu_info info;
181 int err;
182 struct vcpu_info *vcpup;
183
184 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
185
186 /*
187 * This path is called twice on PVHVM - first during bootup via
188 * smp_init -> xen_hvm_cpu_notify, and then if the VCPU is being
189 * hotplugged: cpu_up -> xen_hvm_cpu_notify.
190 * As we can only do the VCPUOP_register_vcpu_info once lets
191 * not over-write its result.
192 *
193 * For PV it is called during restore (xen_vcpu_restore) and bootup
194 * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
195 * use this function.
196 */
197 if (xen_hvm_domain()) {
198 if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
199 return;
200 }
201 if (cpu < MAX_VIRT_CPUS)
202 per_cpu(xen_vcpu,cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
203
204 if (!have_vcpu_info_placement) {
205 if (cpu >= MAX_VIRT_CPUS)
206 clamp_max_cpus();
207 return;
208 }
209
210 vcpup = &per_cpu(xen_vcpu_info, cpu);
211 info.mfn = arbitrary_virt_to_mfn(vcpup);
212 info.offset = offset_in_page(vcpup);
213
214 /* Check to see if the hypervisor will put the vcpu_info
215 structure where we want it, which allows direct access via
216 a percpu-variable.
217 N.B. This hypercall can _only_ be called once per CPU. Subsequent
218 calls will error out with -EINVAL. This is due to the fact that
219 hypervisor has no unregister variant and this hypercall does not
220 allow to over-write info.mfn and info.offset.
221 */
222 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
223
224 if (err) {
225 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
226 have_vcpu_info_placement = 0;
227 clamp_max_cpus();
228 } else {
229 /* This cpu is using the registered vcpu info, even if
230 later ones fail to. */
231 per_cpu(xen_vcpu, cpu) = vcpup;
232 }
233 }
234
235 /*
236 * On restore, set the vcpu placement up again.
237 * If it fails, then we're in a bad state, since
238 * we can't back out from using it...
239 */
xen_vcpu_restore(void)240 void xen_vcpu_restore(void)
241 {
242 int cpu;
243
244 for_each_possible_cpu(cpu) {
245 bool other_cpu = (cpu != smp_processor_id());
246 bool is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL);
247
248 if (other_cpu && is_up &&
249 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
250 BUG();
251
252 xen_setup_runstate_info(cpu);
253
254 if (have_vcpu_info_placement)
255 xen_vcpu_setup(cpu);
256
257 if (other_cpu && is_up &&
258 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
259 BUG();
260 }
261 }
262
xen_banner(void)263 static void __init xen_banner(void)
264 {
265 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
266 struct xen_extraversion extra;
267 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
268
269 pr_info("Booting paravirtualized kernel %son %s\n",
270 xen_feature(XENFEAT_auto_translated_physmap) ?
271 "with PVH extensions " : "", pv_info.name);
272 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
273 version >> 16, version & 0xffff, extra.extraversion,
274 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
275 }
276 /* Check if running on Xen version (major, minor) or later */
277 bool
xen_running_on_version_or_later(unsigned int major,unsigned int minor)278 xen_running_on_version_or_later(unsigned int major, unsigned int minor)
279 {
280 unsigned int version;
281
282 if (!xen_domain())
283 return false;
284
285 version = HYPERVISOR_xen_version(XENVER_version, NULL);
286 if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
287 ((version >> 16) > major))
288 return true;
289 return false;
290 }
291
292 #define CPUID_THERM_POWER_LEAF 6
293 #define APERFMPERF_PRESENT 0
294
295 static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
296 static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
297
298 static __read_mostly unsigned int cpuid_leaf1_ecx_set_mask;
299 static __read_mostly unsigned int cpuid_leaf5_ecx_val;
300 static __read_mostly unsigned int cpuid_leaf5_edx_val;
301
xen_cpuid(unsigned int * ax,unsigned int * bx,unsigned int * cx,unsigned int * dx)302 static void xen_cpuid(unsigned int *ax, unsigned int *bx,
303 unsigned int *cx, unsigned int *dx)
304 {
305 unsigned maskebx = ~0;
306 unsigned maskecx = ~0;
307 unsigned maskedx = ~0;
308 unsigned setecx = 0;
309 /*
310 * Mask out inconvenient features, to try and disable as many
311 * unsupported kernel subsystems as possible.
312 */
313 switch (*ax) {
314 case 1:
315 maskecx = cpuid_leaf1_ecx_mask;
316 setecx = cpuid_leaf1_ecx_set_mask;
317 maskedx = cpuid_leaf1_edx_mask;
318 break;
319
320 case CPUID_MWAIT_LEAF:
321 /* Synthesize the values.. */
322 *ax = 0;
323 *bx = 0;
324 *cx = cpuid_leaf5_ecx_val;
325 *dx = cpuid_leaf5_edx_val;
326 return;
327
328 case CPUID_THERM_POWER_LEAF:
329 /* Disabling APERFMPERF for kernel usage */
330 maskecx = ~(1 << APERFMPERF_PRESENT);
331 break;
332
333 case 0xb:
334 /* Suppress extended topology stuff */
335 maskebx = 0;
336 break;
337 }
338
339 asm(XEN_EMULATE_PREFIX "cpuid"
340 : "=a" (*ax),
341 "=b" (*bx),
342 "=c" (*cx),
343 "=d" (*dx)
344 : "0" (*ax), "2" (*cx));
345
346 *bx &= maskebx;
347 *cx &= maskecx;
348 *cx |= setecx;
349 *dx &= maskedx;
350
351 }
352
xen_check_mwait(void)353 static bool __init xen_check_mwait(void)
354 {
355 #ifdef CONFIG_ACPI
356 struct xen_platform_op op = {
357 .cmd = XENPF_set_processor_pminfo,
358 .u.set_pminfo.id = -1,
359 .u.set_pminfo.type = XEN_PM_PDC,
360 };
361 uint32_t buf[3];
362 unsigned int ax, bx, cx, dx;
363 unsigned int mwait_mask;
364
365 /* We need to determine whether it is OK to expose the MWAIT
366 * capability to the kernel to harvest deeper than C3 states from ACPI
367 * _CST using the processor_harvest_xen.c module. For this to work, we
368 * need to gather the MWAIT_LEAF values (which the cstate.c code
369 * checks against). The hypervisor won't expose the MWAIT flag because
370 * it would break backwards compatibility; so we will find out directly
371 * from the hardware and hypercall.
372 */
373 if (!xen_initial_domain())
374 return false;
375
376 /*
377 * When running under platform earlier than Xen4.2, do not expose
378 * mwait, to avoid the risk of loading native acpi pad driver
379 */
380 if (!xen_running_on_version_or_later(4, 2))
381 return false;
382
383 ax = 1;
384 cx = 0;
385
386 native_cpuid(&ax, &bx, &cx, &dx);
387
388 mwait_mask = (1 << (X86_FEATURE_EST % 32)) |
389 (1 << (X86_FEATURE_MWAIT % 32));
390
391 if ((cx & mwait_mask) != mwait_mask)
392 return false;
393
394 /* We need to emulate the MWAIT_LEAF and for that we need both
395 * ecx and edx. The hypercall provides only partial information.
396 */
397
398 ax = CPUID_MWAIT_LEAF;
399 bx = 0;
400 cx = 0;
401 dx = 0;
402
403 native_cpuid(&ax, &bx, &cx, &dx);
404
405 /* Ask the Hypervisor whether to clear ACPI_PDC_C_C2C3_FFH. If so,
406 * don't expose MWAIT_LEAF and let ACPI pick the IOPORT version of C3.
407 */
408 buf[0] = ACPI_PDC_REVISION_ID;
409 buf[1] = 1;
410 buf[2] = (ACPI_PDC_C_CAPABILITY_SMP | ACPI_PDC_EST_CAPABILITY_SWSMP);
411
412 set_xen_guest_handle(op.u.set_pminfo.pdc, buf);
413
414 if ((HYPERVISOR_dom0_op(&op) == 0) &&
415 (buf[2] & (ACPI_PDC_C_C1_FFH | ACPI_PDC_C_C2C3_FFH))) {
416 cpuid_leaf5_ecx_val = cx;
417 cpuid_leaf5_edx_val = dx;
418 }
419 return true;
420 #else
421 return false;
422 #endif
423 }
xen_init_cpuid_mask(void)424 static void __init xen_init_cpuid_mask(void)
425 {
426 unsigned int ax, bx, cx, dx;
427 unsigned int xsave_mask;
428
429 cpuid_leaf1_edx_mask =
430 ~((1 << X86_FEATURE_MTRR) | /* disable MTRR */
431 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
432
433 if (!xen_initial_domain())
434 cpuid_leaf1_edx_mask &=
435 ~((1 << X86_FEATURE_ACPI)); /* disable ACPI */
436
437 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_X2APIC % 32));
438
439 ax = 1;
440 cx = 0;
441 cpuid(1, &ax, &bx, &cx, &dx);
442
443 xsave_mask =
444 (1 << (X86_FEATURE_XSAVE % 32)) |
445 (1 << (X86_FEATURE_OSXSAVE % 32));
446
447 /* Xen will set CR4.OSXSAVE if supported and not disabled by force */
448 if ((cx & xsave_mask) != xsave_mask)
449 cpuid_leaf1_ecx_mask &= ~xsave_mask; /* disable XSAVE & OSXSAVE */
450 if (xen_check_mwait())
451 cpuid_leaf1_ecx_set_mask = (1 << (X86_FEATURE_MWAIT % 32));
452 }
453
xen_set_debugreg(int reg,unsigned long val)454 static void xen_set_debugreg(int reg, unsigned long val)
455 {
456 HYPERVISOR_set_debugreg(reg, val);
457 }
458
xen_get_debugreg(int reg)459 static unsigned long xen_get_debugreg(int reg)
460 {
461 return HYPERVISOR_get_debugreg(reg);
462 }
463
xen_end_context_switch(struct task_struct * next)464 static void xen_end_context_switch(struct task_struct *next)
465 {
466 xen_mc_flush();
467 paravirt_end_context_switch(next);
468 }
469
xen_store_tr(void)470 static unsigned long xen_store_tr(void)
471 {
472 return 0;
473 }
474
475 /*
476 * Set the page permissions for a particular virtual address. If the
477 * address is a vmalloc mapping (or other non-linear mapping), then
478 * find the linear mapping of the page and also set its protections to
479 * match.
480 */
set_aliased_prot(void * v,pgprot_t prot)481 static void set_aliased_prot(void *v, pgprot_t prot)
482 {
483 int level;
484 pte_t *ptep;
485 pte_t pte;
486 unsigned long pfn;
487 struct page *page;
488 unsigned char dummy;
489
490 ptep = lookup_address((unsigned long)v, &level);
491 BUG_ON(ptep == NULL);
492
493 pfn = pte_pfn(*ptep);
494 page = pfn_to_page(pfn);
495
496 pte = pfn_pte(pfn, prot);
497
498 /*
499 * Careful: update_va_mapping() will fail if the virtual address
500 * we're poking isn't populated in the page tables. We don't
501 * need to worry about the direct map (that's always in the page
502 * tables), but we need to be careful about vmap space. In
503 * particular, the top level page table can lazily propagate
504 * entries between processes, so if we've switched mms since we
505 * vmapped the target in the first place, we might not have the
506 * top-level page table entry populated.
507 *
508 * We disable preemption because we want the same mm active when
509 * we probe the target and when we issue the hypercall. We'll
510 * have the same nominal mm, but if we're a kernel thread, lazy
511 * mm dropping could change our pgd.
512 *
513 * Out of an abundance of caution, this uses __get_user() to fault
514 * in the target address just in case there's some obscure case
515 * in which the target address isn't readable.
516 */
517
518 preempt_disable();
519
520 pagefault_disable(); /* Avoid warnings due to being atomic. */
521 __get_user(dummy, (unsigned char __user __force *)v);
522 pagefault_enable();
523
524 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
525 BUG();
526
527 if (!PageHighMem(page)) {
528 void *av = __va(PFN_PHYS(pfn));
529
530 if (av != v)
531 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
532 BUG();
533 } else
534 kmap_flush_unused();
535
536 preempt_enable();
537 }
538
xen_alloc_ldt(struct desc_struct * ldt,unsigned entries)539 static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
540 {
541 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
542 int i;
543
544 /*
545 * We need to mark the all aliases of the LDT pages RO. We
546 * don't need to call vm_flush_aliases(), though, since that's
547 * only responsible for flushing aliases out the TLBs, not the
548 * page tables, and Xen will flush the TLB for us if needed.
549 *
550 * To avoid confusing future readers: none of this is necessary
551 * to load the LDT. The hypervisor only checks this when the
552 * LDT is faulted in due to subsequent descriptor access.
553 */
554
555 for(i = 0; i < entries; i += entries_per_page)
556 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
557 }
558
xen_free_ldt(struct desc_struct * ldt,unsigned entries)559 static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
560 {
561 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
562 int i;
563
564 for(i = 0; i < entries; i += entries_per_page)
565 set_aliased_prot(ldt + i, PAGE_KERNEL);
566 }
567
xen_set_ldt(const void * addr,unsigned entries)568 static void xen_set_ldt(const void *addr, unsigned entries)
569 {
570 struct mmuext_op *op;
571 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
572
573 trace_xen_cpu_set_ldt(addr, entries);
574
575 op = mcs.args;
576 op->cmd = MMUEXT_SET_LDT;
577 op->arg1.linear_addr = (unsigned long)addr;
578 op->arg2.nr_ents = entries;
579
580 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
581
582 xen_mc_issue(PARAVIRT_LAZY_CPU);
583 }
584
xen_load_gdt(const struct desc_ptr * dtr)585 static void xen_load_gdt(const struct desc_ptr *dtr)
586 {
587 unsigned long va = dtr->address;
588 unsigned int size = dtr->size + 1;
589 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
590 unsigned long frames[pages];
591 int f;
592
593 /*
594 * A GDT can be up to 64k in size, which corresponds to 8192
595 * 8-byte entries, or 16 4k pages..
596 */
597
598 BUG_ON(size > 65536);
599 BUG_ON(va & ~PAGE_MASK);
600
601 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
602 int level;
603 pte_t *ptep;
604 unsigned long pfn, mfn;
605 void *virt;
606
607 /*
608 * The GDT is per-cpu and is in the percpu data area.
609 * That can be virtually mapped, so we need to do a
610 * page-walk to get the underlying MFN for the
611 * hypercall. The page can also be in the kernel's
612 * linear range, so we need to RO that mapping too.
613 */
614 ptep = lookup_address(va, &level);
615 BUG_ON(ptep == NULL);
616
617 pfn = pte_pfn(*ptep);
618 mfn = pfn_to_mfn(pfn);
619 virt = __va(PFN_PHYS(pfn));
620
621 frames[f] = mfn;
622
623 make_lowmem_page_readonly((void *)va);
624 make_lowmem_page_readonly(virt);
625 }
626
627 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
628 BUG();
629 }
630
631 /*
632 * load_gdt for early boot, when the gdt is only mapped once
633 */
xen_load_gdt_boot(const struct desc_ptr * dtr)634 static void __init xen_load_gdt_boot(const struct desc_ptr *dtr)
635 {
636 unsigned long va = dtr->address;
637 unsigned int size = dtr->size + 1;
638 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
639 unsigned long frames[pages];
640 int f;
641
642 /*
643 * A GDT can be up to 64k in size, which corresponds to 8192
644 * 8-byte entries, or 16 4k pages..
645 */
646
647 BUG_ON(size > 65536);
648 BUG_ON(va & ~PAGE_MASK);
649
650 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
651 pte_t pte;
652 unsigned long pfn, mfn;
653
654 pfn = virt_to_pfn(va);
655 mfn = pfn_to_mfn(pfn);
656
657 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
658
659 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
660 BUG();
661
662 frames[f] = mfn;
663 }
664
665 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
666 BUG();
667 }
668
desc_equal(const struct desc_struct * d1,const struct desc_struct * d2)669 static inline bool desc_equal(const struct desc_struct *d1,
670 const struct desc_struct *d2)
671 {
672 return d1->a == d2->a && d1->b == d2->b;
673 }
674
load_TLS_descriptor(struct thread_struct * t,unsigned int cpu,unsigned int i)675 static void load_TLS_descriptor(struct thread_struct *t,
676 unsigned int cpu, unsigned int i)
677 {
678 struct desc_struct *shadow = &per_cpu(shadow_tls_desc, cpu).desc[i];
679 struct desc_struct *gdt;
680 xmaddr_t maddr;
681 struct multicall_space mc;
682
683 if (desc_equal(shadow, &t->tls_array[i]))
684 return;
685
686 *shadow = t->tls_array[i];
687
688 gdt = get_cpu_gdt_table(cpu);
689 maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
690 mc = __xen_mc_entry(0);
691
692 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
693 }
694
xen_load_tls(struct thread_struct * t,unsigned int cpu)695 static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
696 {
697 /*
698 * XXX sleazy hack: If we're being called in a lazy-cpu zone
699 * and lazy gs handling is enabled, it means we're in a
700 * context switch, and %gs has just been saved. This means we
701 * can zero it out to prevent faults on exit from the
702 * hypervisor if the next process has no %gs. Either way, it
703 * has been saved, and the new value will get loaded properly.
704 * This will go away as soon as Xen has been modified to not
705 * save/restore %gs for normal hypercalls.
706 *
707 * On x86_64, this hack is not used for %gs, because gs points
708 * to KERNEL_GS_BASE (and uses it for PDA references), so we
709 * must not zero %gs on x86_64
710 *
711 * For x86_64, we need to zero %fs, otherwise we may get an
712 * exception between the new %fs descriptor being loaded and
713 * %fs being effectively cleared at __switch_to().
714 */
715 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
716 #ifdef CONFIG_X86_32
717 lazy_load_gs(0);
718 #else
719 loadsegment(fs, 0);
720 #endif
721 }
722
723 xen_mc_batch();
724
725 load_TLS_descriptor(t, cpu, 0);
726 load_TLS_descriptor(t, cpu, 1);
727 load_TLS_descriptor(t, cpu, 2);
728
729 xen_mc_issue(PARAVIRT_LAZY_CPU);
730 }
731
732 #ifdef CONFIG_X86_64
xen_load_gs_index(unsigned int idx)733 static void xen_load_gs_index(unsigned int idx)
734 {
735 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
736 BUG();
737 }
738 #endif
739
xen_write_ldt_entry(struct desc_struct * dt,int entrynum,const void * ptr)740 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
741 const void *ptr)
742 {
743 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
744 u64 entry = *(u64 *)ptr;
745
746 trace_xen_cpu_write_ldt_entry(dt, entrynum, entry);
747
748 preempt_disable();
749
750 xen_mc_flush();
751 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
752 BUG();
753
754 preempt_enable();
755 }
756
cvt_gate_to_trap(int vector,const gate_desc * val,struct trap_info * info)757 static int cvt_gate_to_trap(int vector, const gate_desc *val,
758 struct trap_info *info)
759 {
760 unsigned long addr;
761
762 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
763 return 0;
764
765 info->vector = vector;
766
767 addr = gate_offset(*val);
768 #ifdef CONFIG_X86_64
769 /*
770 * Look for known traps using IST, and substitute them
771 * appropriately. The debugger ones are the only ones we care
772 * about. Xen will handle faults like double_fault,
773 * so we should never see them. Warn if
774 * there's an unexpected IST-using fault handler.
775 */
776 if (addr == (unsigned long)debug)
777 addr = (unsigned long)xen_debug;
778 else if (addr == (unsigned long)int3)
779 addr = (unsigned long)xen_int3;
780 else if (addr == (unsigned long)stack_segment)
781 addr = (unsigned long)xen_stack_segment;
782 else if (addr == (unsigned long)double_fault) {
783 /* Don't need to handle these */
784 return 0;
785 #ifdef CONFIG_X86_MCE
786 } else if (addr == (unsigned long)machine_check) {
787 /*
788 * when xen hypervisor inject vMCE to guest,
789 * use native mce handler to handle it
790 */
791 ;
792 #endif
793 } else if (addr == (unsigned long)nmi)
794 /*
795 * Use the native version as well.
796 */
797 ;
798 else {
799 /* Some other trap using IST? */
800 if (WARN_ON(val->ist != 0))
801 return 0;
802 }
803 #endif /* CONFIG_X86_64 */
804 info->address = addr;
805
806 info->cs = gate_segment(*val);
807 info->flags = val->dpl;
808 /* interrupt gates clear IF */
809 if (val->type == GATE_INTERRUPT)
810 info->flags |= 1 << 2;
811
812 return 1;
813 }
814
815 /* Locations of each CPU's IDT */
816 static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
817
818 /* Set an IDT entry. If the entry is part of the current IDT, then
819 also update Xen. */
xen_write_idt_entry(gate_desc * dt,int entrynum,const gate_desc * g)820 static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
821 {
822 unsigned long p = (unsigned long)&dt[entrynum];
823 unsigned long start, end;
824
825 trace_xen_cpu_write_idt_entry(dt, entrynum, g);
826
827 preempt_disable();
828
829 start = __this_cpu_read(idt_desc.address);
830 end = start + __this_cpu_read(idt_desc.size) + 1;
831
832 xen_mc_flush();
833
834 native_write_idt_entry(dt, entrynum, g);
835
836 if (p >= start && (p + 8) <= end) {
837 struct trap_info info[2];
838
839 info[1].address = 0;
840
841 if (cvt_gate_to_trap(entrynum, g, &info[0]))
842 if (HYPERVISOR_set_trap_table(info))
843 BUG();
844 }
845
846 preempt_enable();
847 }
848
xen_convert_trap_info(const struct desc_ptr * desc,struct trap_info * traps)849 static void xen_convert_trap_info(const struct desc_ptr *desc,
850 struct trap_info *traps)
851 {
852 unsigned in, out, count;
853
854 count = (desc->size+1) / sizeof(gate_desc);
855 BUG_ON(count > 256);
856
857 for (in = out = 0; in < count; in++) {
858 gate_desc *entry = (gate_desc*)(desc->address) + in;
859
860 if (cvt_gate_to_trap(in, entry, &traps[out]))
861 out++;
862 }
863 traps[out].address = 0;
864 }
865
xen_copy_trap_info(struct trap_info * traps)866 void xen_copy_trap_info(struct trap_info *traps)
867 {
868 const struct desc_ptr *desc = this_cpu_ptr(&idt_desc);
869
870 xen_convert_trap_info(desc, traps);
871 }
872
873 /* Load a new IDT into Xen. In principle this can be per-CPU, so we
874 hold a spinlock to protect the static traps[] array (static because
875 it avoids allocation, and saves stack space). */
xen_load_idt(const struct desc_ptr * desc)876 static void xen_load_idt(const struct desc_ptr *desc)
877 {
878 static DEFINE_SPINLOCK(lock);
879 static struct trap_info traps[257];
880
881 trace_xen_cpu_load_idt(desc);
882
883 spin_lock(&lock);
884
885 memcpy(this_cpu_ptr(&idt_desc), desc, sizeof(idt_desc));
886
887 xen_convert_trap_info(desc, traps);
888
889 xen_mc_flush();
890 if (HYPERVISOR_set_trap_table(traps))
891 BUG();
892
893 spin_unlock(&lock);
894 }
895
896 /* Write a GDT descriptor entry. Ignore LDT descriptors, since
897 they're handled differently. */
xen_write_gdt_entry(struct desc_struct * dt,int entry,const void * desc,int type)898 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
899 const void *desc, int type)
900 {
901 trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
902
903 preempt_disable();
904
905 switch (type) {
906 case DESC_LDT:
907 case DESC_TSS:
908 /* ignore */
909 break;
910
911 default: {
912 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
913
914 xen_mc_flush();
915 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
916 BUG();
917 }
918
919 }
920
921 preempt_enable();
922 }
923
924 /*
925 * Version of write_gdt_entry for use at early boot-time needed to
926 * update an entry as simply as possible.
927 */
xen_write_gdt_entry_boot(struct desc_struct * dt,int entry,const void * desc,int type)928 static void __init xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
929 const void *desc, int type)
930 {
931 trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
932
933 switch (type) {
934 case DESC_LDT:
935 case DESC_TSS:
936 /* ignore */
937 break;
938
939 default: {
940 xmaddr_t maddr = virt_to_machine(&dt[entry]);
941
942 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
943 dt[entry] = *(struct desc_struct *)desc;
944 }
945
946 }
947 }
948
xen_load_sp0(struct tss_struct * tss,struct thread_struct * thread)949 static void xen_load_sp0(struct tss_struct *tss,
950 struct thread_struct *thread)
951 {
952 struct multicall_space mcs;
953
954 mcs = xen_mc_entry(0);
955 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
956 xen_mc_issue(PARAVIRT_LAZY_CPU);
957 }
958
xen_set_iopl_mask(unsigned mask)959 void xen_set_iopl_mask(unsigned mask)
960 {
961 struct physdev_set_iopl set_iopl;
962
963 /* Force the change at ring 0. */
964 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
965 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
966 }
967
xen_io_delay(void)968 static void xen_io_delay(void)
969 {
970 }
971
972 #ifdef CONFIG_X86_LOCAL_APIC
xen_set_apic_id(unsigned int x)973 static unsigned long xen_set_apic_id(unsigned int x)
974 {
975 WARN_ON(1);
976 return x;
977 }
xen_get_apic_id(unsigned long x)978 static unsigned int xen_get_apic_id(unsigned long x)
979 {
980 return ((x)>>24) & 0xFFu;
981 }
xen_apic_read(u32 reg)982 static u32 xen_apic_read(u32 reg)
983 {
984 struct xen_platform_op op = {
985 .cmd = XENPF_get_cpuinfo,
986 .interface_version = XENPF_INTERFACE_VERSION,
987 .u.pcpu_info.xen_cpuid = 0,
988 };
989 int ret = 0;
990
991 /* Shouldn't need this as APIC is turned off for PV, and we only
992 * get called on the bootup processor. But just in case. */
993 if (!xen_initial_domain() || smp_processor_id())
994 return 0;
995
996 if (reg == APIC_LVR)
997 return 0x10;
998
999 if (reg != APIC_ID)
1000 return 0;
1001
1002 ret = HYPERVISOR_dom0_op(&op);
1003 if (ret)
1004 return 0;
1005
1006 return op.u.pcpu_info.apic_id << 24;
1007 }
1008
xen_apic_write(u32 reg,u32 val)1009 static void xen_apic_write(u32 reg, u32 val)
1010 {
1011 /* Warn to see if there's any stray references */
1012 WARN_ON(1);
1013 }
1014
xen_apic_icr_read(void)1015 static u64 xen_apic_icr_read(void)
1016 {
1017 return 0;
1018 }
1019
xen_apic_icr_write(u32 low,u32 id)1020 static void xen_apic_icr_write(u32 low, u32 id)
1021 {
1022 /* Warn to see if there's any stray references */
1023 WARN_ON(1);
1024 }
1025
xen_apic_wait_icr_idle(void)1026 static void xen_apic_wait_icr_idle(void)
1027 {
1028 return;
1029 }
1030
xen_safe_apic_wait_icr_idle(void)1031 static u32 xen_safe_apic_wait_icr_idle(void)
1032 {
1033 return 0;
1034 }
1035
set_xen_basic_apic_ops(void)1036 static void set_xen_basic_apic_ops(void)
1037 {
1038 apic->read = xen_apic_read;
1039 apic->write = xen_apic_write;
1040 apic->icr_read = xen_apic_icr_read;
1041 apic->icr_write = xen_apic_icr_write;
1042 apic->wait_icr_idle = xen_apic_wait_icr_idle;
1043 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
1044 apic->set_apic_id = xen_set_apic_id;
1045 apic->get_apic_id = xen_get_apic_id;
1046
1047 #ifdef CONFIG_SMP
1048 apic->send_IPI_allbutself = xen_send_IPI_allbutself;
1049 apic->send_IPI_mask_allbutself = xen_send_IPI_mask_allbutself;
1050 apic->send_IPI_mask = xen_send_IPI_mask;
1051 apic->send_IPI_all = xen_send_IPI_all;
1052 apic->send_IPI_self = xen_send_IPI_self;
1053 #endif
1054 }
1055
1056 #endif
1057
xen_clts(void)1058 static void xen_clts(void)
1059 {
1060 struct multicall_space mcs;
1061
1062 mcs = xen_mc_entry(0);
1063
1064 MULTI_fpu_taskswitch(mcs.mc, 0);
1065
1066 xen_mc_issue(PARAVIRT_LAZY_CPU);
1067 }
1068
1069 static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
1070
xen_read_cr0(void)1071 static unsigned long xen_read_cr0(void)
1072 {
1073 unsigned long cr0 = this_cpu_read(xen_cr0_value);
1074
1075 if (unlikely(cr0 == 0)) {
1076 cr0 = native_read_cr0();
1077 this_cpu_write(xen_cr0_value, cr0);
1078 }
1079
1080 return cr0;
1081 }
1082
xen_write_cr0(unsigned long cr0)1083 static void xen_write_cr0(unsigned long cr0)
1084 {
1085 struct multicall_space mcs;
1086
1087 this_cpu_write(xen_cr0_value, cr0);
1088
1089 /* Only pay attention to cr0.TS; everything else is
1090 ignored. */
1091 mcs = xen_mc_entry(0);
1092
1093 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
1094
1095 xen_mc_issue(PARAVIRT_LAZY_CPU);
1096 }
1097
xen_write_cr4(unsigned long cr4)1098 static void xen_write_cr4(unsigned long cr4)
1099 {
1100 cr4 &= ~X86_CR4_PGE;
1101 cr4 &= ~X86_CR4_PSE;
1102
1103 native_write_cr4(cr4);
1104 }
1105 #ifdef CONFIG_X86_64
xen_read_cr8(void)1106 static inline unsigned long xen_read_cr8(void)
1107 {
1108 return 0;
1109 }
xen_write_cr8(unsigned long val)1110 static inline void xen_write_cr8(unsigned long val)
1111 {
1112 BUG_ON(val);
1113 }
1114 #endif
xen_write_msr_safe(unsigned int msr,unsigned low,unsigned high)1115 static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
1116 {
1117 int ret;
1118
1119 ret = 0;
1120
1121 switch (msr) {
1122 #ifdef CONFIG_X86_64
1123 unsigned which;
1124 u64 base;
1125
1126 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
1127 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
1128 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
1129
1130 set:
1131 base = ((u64)high << 32) | low;
1132 if (HYPERVISOR_set_segment_base(which, base) != 0)
1133 ret = -EIO;
1134 break;
1135 #endif
1136
1137 case MSR_STAR:
1138 case MSR_CSTAR:
1139 case MSR_LSTAR:
1140 case MSR_SYSCALL_MASK:
1141 case MSR_IA32_SYSENTER_CS:
1142 case MSR_IA32_SYSENTER_ESP:
1143 case MSR_IA32_SYSENTER_EIP:
1144 /* Fast syscall setup is all done in hypercalls, so
1145 these are all ignored. Stub them out here to stop
1146 Xen console noise. */
1147 break;
1148
1149 case MSR_IA32_CR_PAT:
1150 if (smp_processor_id() == 0)
1151 xen_set_pat(((u64)high << 32) | low);
1152 break;
1153
1154 default:
1155 ret = native_write_msr_safe(msr, low, high);
1156 }
1157
1158 return ret;
1159 }
1160
xen_setup_shared_info(void)1161 void xen_setup_shared_info(void)
1162 {
1163 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1164 set_fixmap(FIX_PARAVIRT_BOOTMAP,
1165 xen_start_info->shared_info);
1166
1167 HYPERVISOR_shared_info =
1168 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
1169 } else
1170 HYPERVISOR_shared_info =
1171 (struct shared_info *)__va(xen_start_info->shared_info);
1172
1173 #ifndef CONFIG_SMP
1174 /* In UP this is as good a place as any to set up shared info */
1175 xen_setup_vcpu_info_placement();
1176 #endif
1177
1178 xen_setup_mfn_list_list();
1179 }
1180
1181 /* This is called once we have the cpu_possible_mask */
xen_setup_vcpu_info_placement(void)1182 void xen_setup_vcpu_info_placement(void)
1183 {
1184 int cpu;
1185
1186 for_each_possible_cpu(cpu)
1187 xen_vcpu_setup(cpu);
1188
1189 /* xen_vcpu_setup managed to place the vcpu_info within the
1190 * percpu area for all cpus, so make use of it. Note that for
1191 * PVH we want to use native IRQ mechanism. */
1192 if (have_vcpu_info_placement && !xen_pvh_domain()) {
1193 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
1194 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
1195 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
1196 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
1197 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
1198 }
1199 }
1200
xen_patch(u8 type,u16 clobbers,void * insnbuf,unsigned long addr,unsigned len)1201 static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
1202 unsigned long addr, unsigned len)
1203 {
1204 char *start, *end, *reloc;
1205 unsigned ret;
1206
1207 start = end = reloc = NULL;
1208
1209 #define SITE(op, x) \
1210 case PARAVIRT_PATCH(op.x): \
1211 if (have_vcpu_info_placement) { \
1212 start = (char *)xen_##x##_direct; \
1213 end = xen_##x##_direct_end; \
1214 reloc = xen_##x##_direct_reloc; \
1215 } \
1216 goto patch_site
1217
1218 switch (type) {
1219 SITE(pv_irq_ops, irq_enable);
1220 SITE(pv_irq_ops, irq_disable);
1221 SITE(pv_irq_ops, save_fl);
1222 SITE(pv_irq_ops, restore_fl);
1223 #undef SITE
1224
1225 patch_site:
1226 if (start == NULL || (end-start) > len)
1227 goto default_patch;
1228
1229 ret = paravirt_patch_insns(insnbuf, len, start, end);
1230
1231 /* Note: because reloc is assigned from something that
1232 appears to be an array, gcc assumes it's non-null,
1233 but doesn't know its relationship with start and
1234 end. */
1235 if (reloc > start && reloc < end) {
1236 int reloc_off = reloc - start;
1237 long *relocp = (long *)(insnbuf + reloc_off);
1238 long delta = start - (char *)addr;
1239
1240 *relocp += delta;
1241 }
1242 break;
1243
1244 default_patch:
1245 default:
1246 ret = paravirt_patch_default(type, clobbers, insnbuf,
1247 addr, len);
1248 break;
1249 }
1250
1251 return ret;
1252 }
1253
1254 static const struct pv_info xen_info __initconst = {
1255 .paravirt_enabled = 1,
1256 .shared_kernel_pmd = 0,
1257
1258 #ifdef CONFIG_X86_64
1259 .extra_user_64bit_cs = FLAT_USER_CS64,
1260 #endif
1261
1262 .name = "Xen",
1263 };
1264
1265 static const struct pv_init_ops xen_init_ops __initconst = {
1266 .patch = xen_patch,
1267 };
1268
1269 static const struct pv_cpu_ops xen_cpu_ops __initconst = {
1270 .cpuid = xen_cpuid,
1271
1272 .set_debugreg = xen_set_debugreg,
1273 .get_debugreg = xen_get_debugreg,
1274
1275 .clts = xen_clts,
1276
1277 .read_cr0 = xen_read_cr0,
1278 .write_cr0 = xen_write_cr0,
1279
1280 .read_cr4 = native_read_cr4,
1281 .read_cr4_safe = native_read_cr4_safe,
1282 .write_cr4 = xen_write_cr4,
1283
1284 #ifdef CONFIG_X86_64
1285 .read_cr8 = xen_read_cr8,
1286 .write_cr8 = xen_write_cr8,
1287 #endif
1288
1289 .wbinvd = native_wbinvd,
1290
1291 .read_msr = native_read_msr_safe,
1292 .write_msr = xen_write_msr_safe,
1293
1294 .read_tsc = native_read_tsc,
1295 .read_pmc = native_read_pmc,
1296
1297 .read_tscp = native_read_tscp,
1298
1299 .iret = xen_iret,
1300 .irq_enable_sysexit = xen_sysexit,
1301 #ifdef CONFIG_X86_64
1302 .usergs_sysret32 = xen_sysret32,
1303 .usergs_sysret64 = xen_sysret64,
1304 #endif
1305
1306 .load_tr_desc = paravirt_nop,
1307 .set_ldt = xen_set_ldt,
1308 .load_gdt = xen_load_gdt,
1309 .load_idt = xen_load_idt,
1310 .load_tls = xen_load_tls,
1311 #ifdef CONFIG_X86_64
1312 .load_gs_index = xen_load_gs_index,
1313 #endif
1314
1315 .alloc_ldt = xen_alloc_ldt,
1316 .free_ldt = xen_free_ldt,
1317
1318 .store_idt = native_store_idt,
1319 .store_tr = xen_store_tr,
1320
1321 .write_ldt_entry = xen_write_ldt_entry,
1322 .write_gdt_entry = xen_write_gdt_entry,
1323 .write_idt_entry = xen_write_idt_entry,
1324 .load_sp0 = xen_load_sp0,
1325
1326 .set_iopl_mask = xen_set_iopl_mask,
1327 .io_delay = xen_io_delay,
1328
1329 /* Xen takes care of %gs when switching to usermode for us */
1330 .swapgs = paravirt_nop,
1331
1332 .start_context_switch = paravirt_start_context_switch,
1333 .end_context_switch = xen_end_context_switch,
1334 };
1335
1336 static const struct pv_apic_ops xen_apic_ops __initconst = {
1337 #ifdef CONFIG_X86_LOCAL_APIC
1338 .startup_ipi_hook = paravirt_nop,
1339 #endif
1340 };
1341
xen_reboot(int reason)1342 static void xen_reboot(int reason)
1343 {
1344 struct sched_shutdown r = { .reason = reason };
1345
1346 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
1347 BUG();
1348 }
1349
xen_restart(char * msg)1350 static void xen_restart(char *msg)
1351 {
1352 xen_reboot(SHUTDOWN_reboot);
1353 }
1354
xen_emergency_restart(void)1355 static void xen_emergency_restart(void)
1356 {
1357 xen_reboot(SHUTDOWN_reboot);
1358 }
1359
xen_machine_halt(void)1360 static void xen_machine_halt(void)
1361 {
1362 xen_reboot(SHUTDOWN_poweroff);
1363 }
1364
xen_machine_power_off(void)1365 static void xen_machine_power_off(void)
1366 {
1367 if (pm_power_off)
1368 pm_power_off();
1369 xen_reboot(SHUTDOWN_poweroff);
1370 }
1371
xen_crash_shutdown(struct pt_regs * regs)1372 static void xen_crash_shutdown(struct pt_regs *regs)
1373 {
1374 xen_reboot(SHUTDOWN_crash);
1375 }
1376
1377 static int
xen_panic_event(struct notifier_block * this,unsigned long event,void * ptr)1378 xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
1379 {
1380 xen_reboot(SHUTDOWN_crash);
1381 return NOTIFY_DONE;
1382 }
1383
1384 static struct notifier_block xen_panic_block = {
1385 .notifier_call= xen_panic_event,
1386 .priority = INT_MIN
1387 };
1388
xen_panic_handler_init(void)1389 int xen_panic_handler_init(void)
1390 {
1391 atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
1392 return 0;
1393 }
1394
1395 static const struct machine_ops xen_machine_ops __initconst = {
1396 .restart = xen_restart,
1397 .halt = xen_machine_halt,
1398 .power_off = xen_machine_power_off,
1399 .shutdown = xen_machine_halt,
1400 .crash_shutdown = xen_crash_shutdown,
1401 .emergency_restart = xen_emergency_restart,
1402 };
1403
xen_boot_params_init_edd(void)1404 static void __init xen_boot_params_init_edd(void)
1405 {
1406 #if IS_ENABLED(CONFIG_EDD)
1407 struct xen_platform_op op;
1408 struct edd_info *edd_info;
1409 u32 *mbr_signature;
1410 unsigned nr;
1411 int ret;
1412
1413 edd_info = boot_params.eddbuf;
1414 mbr_signature = boot_params.edd_mbr_sig_buffer;
1415
1416 op.cmd = XENPF_firmware_info;
1417
1418 op.u.firmware_info.type = XEN_FW_DISK_INFO;
1419 for (nr = 0; nr < EDDMAXNR; nr++) {
1420 struct edd_info *info = edd_info + nr;
1421
1422 op.u.firmware_info.index = nr;
1423 info->params.length = sizeof(info->params);
1424 set_xen_guest_handle(op.u.firmware_info.u.disk_info.edd_params,
1425 &info->params);
1426 ret = HYPERVISOR_dom0_op(&op);
1427 if (ret)
1428 break;
1429
1430 #define C(x) info->x = op.u.firmware_info.u.disk_info.x
1431 C(device);
1432 C(version);
1433 C(interface_support);
1434 C(legacy_max_cylinder);
1435 C(legacy_max_head);
1436 C(legacy_sectors_per_track);
1437 #undef C
1438 }
1439 boot_params.eddbuf_entries = nr;
1440
1441 op.u.firmware_info.type = XEN_FW_DISK_MBR_SIGNATURE;
1442 for (nr = 0; nr < EDD_MBR_SIG_MAX; nr++) {
1443 op.u.firmware_info.index = nr;
1444 ret = HYPERVISOR_dom0_op(&op);
1445 if (ret)
1446 break;
1447 mbr_signature[nr] = op.u.firmware_info.u.disk_mbr_signature.mbr_signature;
1448 }
1449 boot_params.edd_mbr_sig_buf_entries = nr;
1450 #endif
1451 }
1452
1453 /*
1454 * Set up the GDT and segment registers for -fstack-protector. Until
1455 * we do this, we have to be careful not to call any stack-protected
1456 * function, which is most of the kernel.
1457 *
1458 * Note, that it is __ref because the only caller of this after init
1459 * is PVH which is not going to use xen_load_gdt_boot or other
1460 * __init functions.
1461 */
xen_setup_gdt(int cpu)1462 static void __ref xen_setup_gdt(int cpu)
1463 {
1464 if (xen_feature(XENFEAT_auto_translated_physmap)) {
1465 #ifdef CONFIG_X86_64
1466 unsigned long dummy;
1467
1468 load_percpu_segment(cpu); /* We need to access per-cpu area */
1469 switch_to_new_gdt(cpu); /* GDT and GS set */
1470
1471 /* We are switching of the Xen provided GDT to our HVM mode
1472 * GDT. The new GDT has __KERNEL_CS with CS.L = 1
1473 * and we are jumping to reload it.
1474 */
1475 asm volatile ("pushq %0\n"
1476 "leaq 1f(%%rip),%0\n"
1477 "pushq %0\n"
1478 "lretq\n"
1479 "1:\n"
1480 : "=&r" (dummy) : "0" (__KERNEL_CS));
1481
1482 /*
1483 * While not needed, we also set the %es, %ds, and %fs
1484 * to zero. We don't care about %ss as it is NULL.
1485 * Strictly speaking this is not needed as Xen zeros those
1486 * out (and also MSR_FS_BASE, MSR_GS_BASE, MSR_KERNEL_GS_BASE)
1487 *
1488 * Linux zeros them in cpu_init() and in secondary_startup_64
1489 * (for BSP).
1490 */
1491 loadsegment(es, 0);
1492 loadsegment(ds, 0);
1493 loadsegment(fs, 0);
1494 #else
1495 /* PVH: TODO Implement. */
1496 BUG();
1497 #endif
1498 return; /* PVH does not need any PV GDT ops. */
1499 }
1500 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1501 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1502
1503 setup_stack_canary_segment(0);
1504 switch_to_new_gdt(0);
1505
1506 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1507 pv_cpu_ops.load_gdt = xen_load_gdt;
1508 }
1509
1510 #ifdef CONFIG_XEN_PVH
1511 /*
1512 * A PV guest starts with default flags that are not set for PVH, set them
1513 * here asap.
1514 */
xen_pvh_set_cr_flags(int cpu)1515 static void xen_pvh_set_cr_flags(int cpu)
1516 {
1517
1518 /* Some of these are setup in 'secondary_startup_64'. The others:
1519 * X86_CR0_TS, X86_CR0_PE, X86_CR0_ET are set by Xen for HVM guests
1520 * (which PVH shared codepaths), while X86_CR0_PG is for PVH. */
1521 write_cr0(read_cr0() | X86_CR0_MP | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM);
1522
1523 if (!cpu)
1524 return;
1525 /*
1526 * For BSP, PSE PGE are set in probe_page_size_mask(), for APs
1527 * set them here. For all, OSFXSR OSXMMEXCPT are set in fpu_init.
1528 */
1529 if (cpu_has_pse)
1530 cr4_set_bits_and_update_boot(X86_CR4_PSE);
1531
1532 if (cpu_has_pge)
1533 cr4_set_bits_and_update_boot(X86_CR4_PGE);
1534 }
1535
1536 /*
1537 * Note, that it is ref - because the only caller of this after init
1538 * is PVH which is not going to use xen_load_gdt_boot or other
1539 * __init functions.
1540 */
xen_pvh_secondary_vcpu_init(int cpu)1541 void __ref xen_pvh_secondary_vcpu_init(int cpu)
1542 {
1543 xen_setup_gdt(cpu);
1544 xen_pvh_set_cr_flags(cpu);
1545 }
1546
xen_pvh_early_guest_init(void)1547 static void __init xen_pvh_early_guest_init(void)
1548 {
1549 if (!xen_feature(XENFEAT_auto_translated_physmap))
1550 return;
1551
1552 if (!xen_feature(XENFEAT_hvm_callback_vector))
1553 return;
1554
1555 xen_have_vector_callback = 1;
1556
1557 xen_pvh_early_cpu_init(0, false);
1558 xen_pvh_set_cr_flags(0);
1559
1560 #ifdef CONFIG_X86_32
1561 BUG(); /* PVH: Implement proper support. */
1562 #endif
1563 }
1564 #endif /* CONFIG_XEN_PVH */
1565
1566 /* First C function to be called on Xen boot */
xen_start_kernel(void)1567 asmlinkage __visible void __init xen_start_kernel(void)
1568 {
1569 struct physdev_set_iopl set_iopl;
1570 unsigned long initrd_start = 0;
1571 int rc;
1572
1573 if (!xen_start_info)
1574 return;
1575
1576 xen_domain_type = XEN_PV_DOMAIN;
1577
1578 xen_setup_features();
1579 #ifdef CONFIG_XEN_PVH
1580 xen_pvh_early_guest_init();
1581 #endif
1582 xen_setup_machphys_mapping();
1583
1584 /* Install Xen paravirt ops */
1585 pv_info = xen_info;
1586 pv_init_ops = xen_init_ops;
1587 pv_apic_ops = xen_apic_ops;
1588 if (!xen_pvh_domain())
1589 pv_cpu_ops = xen_cpu_ops;
1590
1591 if (xen_feature(XENFEAT_auto_translated_physmap))
1592 x86_init.resources.memory_setup = xen_auto_xlated_memory_setup;
1593 else
1594 x86_init.resources.memory_setup = xen_memory_setup;
1595 x86_init.oem.arch_setup = xen_arch_setup;
1596 x86_init.oem.banner = xen_banner;
1597
1598 xen_init_time_ops();
1599
1600 /*
1601 * Set up some pagetable state before starting to set any ptes.
1602 */
1603
1604 xen_init_mmu_ops();
1605
1606 /* Prevent unwanted bits from being set in PTEs. */
1607 __supported_pte_mask &= ~_PAGE_GLOBAL;
1608 #if 0
1609 if (!xen_initial_domain())
1610 #endif
1611 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1612
1613 /*
1614 * Prevent page tables from being allocated in highmem, even
1615 * if CONFIG_HIGHPTE is enabled.
1616 */
1617 __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
1618
1619 /* Work out if we support NX */
1620 x86_configure_nx();
1621
1622 /* Get mfn list */
1623 xen_build_dynamic_phys_to_machine();
1624
1625 /*
1626 * Set up kernel GDT and segment registers, mainly so that
1627 * -fstack-protector code can be executed.
1628 */
1629 xen_setup_gdt(0);
1630
1631 xen_init_irq_ops();
1632 xen_init_cpuid_mask();
1633
1634 #ifdef CONFIG_X86_LOCAL_APIC
1635 /*
1636 * set up the basic apic ops.
1637 */
1638 set_xen_basic_apic_ops();
1639 #endif
1640
1641 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1642 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1643 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1644 }
1645
1646 machine_ops = xen_machine_ops;
1647
1648 /*
1649 * The only reliable way to retain the initial address of the
1650 * percpu gdt_page is to remember it here, so we can go and
1651 * mark it RW later, when the initial percpu area is freed.
1652 */
1653 xen_initial_gdt = &per_cpu(gdt_page, 0);
1654
1655 xen_smp_init();
1656
1657 #ifdef CONFIG_ACPI_NUMA
1658 /*
1659 * The pages we from Xen are not related to machine pages, so
1660 * any NUMA information the kernel tries to get from ACPI will
1661 * be meaningless. Prevent it from trying.
1662 */
1663 acpi_numa = -1;
1664 #endif
1665 #ifdef CONFIG_X86_PAT
1666 /*
1667 * For right now disable the PAT. We should remove this once
1668 * git commit 8eaffa67b43e99ae581622c5133e20b0f48bcef1
1669 * (xen/pat: Disable PAT support for now) is reverted.
1670 */
1671 pat_enabled = 0;
1672 #endif
1673 /* Don't do the full vcpu_info placement stuff until we have a
1674 possible map and a non-dummy shared_info. */
1675 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1676
1677 local_irq_disable();
1678 early_boot_irqs_disabled = true;
1679
1680 xen_raw_console_write("mapping kernel into physical memory\n");
1681 xen_setup_kernel_pagetable((pgd_t *)xen_start_info->pt_base, xen_start_info->nr_pages);
1682
1683 /* keep using Xen gdt for now; no urgent need to change it */
1684
1685 #ifdef CONFIG_X86_32
1686 pv_info.kernel_rpl = 1;
1687 if (xen_feature(XENFEAT_supervisor_mode_kernel))
1688 pv_info.kernel_rpl = 0;
1689 #else
1690 pv_info.kernel_rpl = 0;
1691 #endif
1692 /* set the limit of our address space */
1693 xen_reserve_top();
1694
1695 /* PVH: runs at default kernel iopl of 0 */
1696 if (!xen_pvh_domain()) {
1697 /*
1698 * We used to do this in xen_arch_setup, but that is too late
1699 * on AMD were early_cpu_init (run before ->arch_setup()) calls
1700 * early_amd_init which pokes 0xcf8 port.
1701 */
1702 set_iopl.iopl = 1;
1703 rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
1704 if (rc != 0)
1705 xen_raw_printk("physdev_op failed %d\n", rc);
1706 }
1707
1708 #ifdef CONFIG_X86_32
1709 /* set up basic CPUID stuff */
1710 cpu_detect(&new_cpu_data);
1711 set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
1712 new_cpu_data.wp_works_ok = 1;
1713 new_cpu_data.x86_capability[0] = cpuid_edx(1);
1714 #endif
1715
1716 if (xen_start_info->mod_start) {
1717 if (xen_start_info->flags & SIF_MOD_START_PFN)
1718 initrd_start = PFN_PHYS(xen_start_info->mod_start);
1719 else
1720 initrd_start = __pa(xen_start_info->mod_start);
1721 }
1722
1723 /* Poke various useful things into boot_params */
1724 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1725 boot_params.hdr.ramdisk_image = initrd_start;
1726 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
1727 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
1728
1729 if (!xen_initial_domain()) {
1730 add_preferred_console("xenboot", 0, NULL);
1731 add_preferred_console("tty", 0, NULL);
1732 add_preferred_console("hvc", 0, NULL);
1733 if (pci_xen)
1734 x86_init.pci.arch_init = pci_xen_init;
1735 } else {
1736 const struct dom0_vga_console_info *info =
1737 (void *)((char *)xen_start_info +
1738 xen_start_info->console.dom0.info_off);
1739 struct xen_platform_op op = {
1740 .cmd = XENPF_firmware_info,
1741 .interface_version = XENPF_INTERFACE_VERSION,
1742 .u.firmware_info.type = XEN_FW_KBD_SHIFT_FLAGS,
1743 };
1744
1745 xen_init_vga(info, xen_start_info->console.dom0.info_size);
1746 xen_start_info->console.domU.mfn = 0;
1747 xen_start_info->console.domU.evtchn = 0;
1748
1749 if (HYPERVISOR_dom0_op(&op) == 0)
1750 boot_params.kbd_status = op.u.firmware_info.u.kbd_shift_flags;
1751
1752 xen_init_apic();
1753
1754 /* Make sure ACS will be enabled */
1755 pci_request_acs();
1756
1757 xen_acpi_sleep_register();
1758
1759 /* Avoid searching for BIOS MP tables */
1760 x86_init.mpparse.find_smp_config = x86_init_noop;
1761 x86_init.mpparse.get_smp_config = x86_init_uint_noop;
1762
1763 xen_boot_params_init_edd();
1764 }
1765 #ifdef CONFIG_PCI
1766 /* PCI BIOS service won't work from a PV guest. */
1767 pci_probe &= ~PCI_PROBE_BIOS;
1768 #endif
1769 xen_raw_console_write("about to get started...\n");
1770
1771 xen_setup_runstate_info(0);
1772
1773 xen_efi_init();
1774
1775 /* Start the world */
1776 #ifdef CONFIG_X86_32
1777 i386_start_kernel();
1778 #else
1779 cr4_init_shadow(); /* 32b kernel does this in i386_start_kernel() */
1780 x86_64_start_reservations((char *)__pa_symbol(&boot_params));
1781 #endif
1782 }
1783
xen_hvm_init_shared_info(void)1784 void __ref xen_hvm_init_shared_info(void)
1785 {
1786 int cpu;
1787 struct xen_add_to_physmap xatp;
1788 static struct shared_info *shared_info_page = 0;
1789
1790 if (!shared_info_page)
1791 shared_info_page = (struct shared_info *)
1792 extend_brk(PAGE_SIZE, PAGE_SIZE);
1793 xatp.domid = DOMID_SELF;
1794 xatp.idx = 0;
1795 xatp.space = XENMAPSPACE_shared_info;
1796 xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
1797 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
1798 BUG();
1799
1800 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
1801
1802 /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
1803 * page, we use it in the event channel upcall and in some pvclock
1804 * related functions. We don't need the vcpu_info placement
1805 * optimizations because we don't use any pv_mmu or pv_irq op on
1806 * HVM.
1807 * When xen_hvm_init_shared_info is run at boot time only vcpu 0 is
1808 * online but xen_hvm_init_shared_info is run at resume time too and
1809 * in that case multiple vcpus might be online. */
1810 for_each_online_cpu(cpu) {
1811 /* Leave it to be NULL. */
1812 if (cpu >= MAX_VIRT_CPUS)
1813 continue;
1814 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
1815 }
1816 }
1817
1818 #ifdef CONFIG_XEN_PVHVM
init_hvm_pv_info(void)1819 static void __init init_hvm_pv_info(void)
1820 {
1821 int major, minor;
1822 uint32_t eax, ebx, ecx, edx, pages, msr, base;
1823 u64 pfn;
1824
1825 base = xen_cpuid_base();
1826 cpuid(base + 1, &eax, &ebx, &ecx, &edx);
1827
1828 major = eax >> 16;
1829 minor = eax & 0xffff;
1830 printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
1831
1832 cpuid(base + 2, &pages, &msr, &ecx, &edx);
1833
1834 pfn = __pa(hypercall_page);
1835 wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
1836
1837 xen_setup_features();
1838
1839 pv_info.name = "Xen HVM";
1840
1841 xen_domain_type = XEN_HVM_DOMAIN;
1842 }
1843
xen_hvm_cpu_notify(struct notifier_block * self,unsigned long action,void * hcpu)1844 static int xen_hvm_cpu_notify(struct notifier_block *self, unsigned long action,
1845 void *hcpu)
1846 {
1847 int cpu = (long)hcpu;
1848 switch (action) {
1849 case CPU_UP_PREPARE:
1850 xen_vcpu_setup(cpu);
1851 if (xen_have_vector_callback) {
1852 if (xen_feature(XENFEAT_hvm_safe_pvclock))
1853 xen_setup_timer(cpu);
1854 }
1855 break;
1856 default:
1857 break;
1858 }
1859 return NOTIFY_OK;
1860 }
1861
1862 static struct notifier_block xen_hvm_cpu_notifier = {
1863 .notifier_call = xen_hvm_cpu_notify,
1864 };
1865
1866 #ifdef CONFIG_KEXEC_CORE
xen_hvm_shutdown(void)1867 static void xen_hvm_shutdown(void)
1868 {
1869 native_machine_shutdown();
1870 if (kexec_in_progress)
1871 xen_reboot(SHUTDOWN_soft_reset);
1872 }
1873
xen_hvm_crash_shutdown(struct pt_regs * regs)1874 static void xen_hvm_crash_shutdown(struct pt_regs *regs)
1875 {
1876 native_machine_crash_shutdown(regs);
1877 xen_reboot(SHUTDOWN_soft_reset);
1878 }
1879 #endif
1880
xen_hvm_guest_init(void)1881 static void __init xen_hvm_guest_init(void)
1882 {
1883 init_hvm_pv_info();
1884
1885 xen_hvm_init_shared_info();
1886
1887 xen_panic_handler_init();
1888
1889 if (xen_feature(XENFEAT_hvm_callback_vector))
1890 xen_have_vector_callback = 1;
1891 xen_hvm_smp_init();
1892 register_cpu_notifier(&xen_hvm_cpu_notifier);
1893 xen_unplug_emulated_devices();
1894 x86_init.irqs.intr_init = xen_init_IRQ;
1895 xen_hvm_init_time_ops();
1896 xen_hvm_init_mmu_ops();
1897 #ifdef CONFIG_KEXEC_CORE
1898 machine_ops.shutdown = xen_hvm_shutdown;
1899 machine_ops.crash_shutdown = xen_hvm_crash_shutdown;
1900 #endif
1901 }
1902
1903 static bool xen_nopv = false;
xen_parse_nopv(char * arg)1904 static __init int xen_parse_nopv(char *arg)
1905 {
1906 xen_nopv = true;
1907 return 0;
1908 }
1909 early_param("xen_nopv", xen_parse_nopv);
1910
xen_hvm_platform(void)1911 static uint32_t __init xen_hvm_platform(void)
1912 {
1913 if (xen_nopv)
1914 return 0;
1915
1916 if (xen_pv_domain())
1917 return 0;
1918
1919 return xen_cpuid_base();
1920 }
1921
xen_hvm_need_lapic(void)1922 bool xen_hvm_need_lapic(void)
1923 {
1924 if (xen_nopv)
1925 return false;
1926 if (xen_pv_domain())
1927 return false;
1928 if (!xen_hvm_domain())
1929 return false;
1930 if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
1931 return false;
1932 return true;
1933 }
1934 EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
1935
1936 const struct hypervisor_x86 x86_hyper_xen_hvm __refconst = {
1937 .name = "Xen HVM",
1938 .detect = xen_hvm_platform,
1939 .init_platform = xen_hvm_guest_init,
1940 .x2apic_available = xen_x2apic_para_available,
1941 };
1942 EXPORT_SYMBOL(x86_hyper_xen_hvm);
1943 #endif
1944