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