drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / x86 / xen / enlighten.c
CommitLineData
5ead97c8
JF
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
38e20b07 14#include <linux/cpu.h>
5ead97c8
JF
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/smp.h>
18#include <linux/preempt.h>
f120f13e 19#include <linux/hardirq.h>
5ead97c8
JF
20#include <linux/percpu.h>
21#include <linux/delay.h>
22#include <linux/start_kernel.h>
23#include <linux/sched.h>
6cac5a92 24#include <linux/kprobes.h>
5ead97c8
JF
25#include <linux/bootmem.h>
26#include <linux/module.h>
f4f97b3e
JF
27#include <linux/mm.h>
28#include <linux/page-flags.h>
29#include <linux/highmem.h>
b8c2d3df 30#include <linux/console.h>
5d990b62 31#include <linux/pci.h>
5a0e3ad6 32#include <linux/gfp.h>
236260b9 33#include <linux/memblock.h>
96f28bc6 34#include <linux/edd.h>
5ead97c8 35
9754f73b 36#ifdef CONFIG_KEXEC
919845cb
VK
37#include <linux/kexec.h>
38#endif
39
1ccbf534 40#include <xen/xen.h>
0ec53ecf 41#include <xen/events.h>
5ead97c8 42#include <xen/interface/xen.h>
ecbf29cd 43#include <xen/interface/version.h>
5ead97c8
JF
44#include <xen/interface/physdev.h>
45#include <xen/interface/vcpu.h>
bee6ab53 46#include <xen/interface/memory.h>
cef12ee5 47#include <xen/interface/xen-mca.h>
5ead97c8
JF
48#include <xen/features.h>
49#include <xen/page.h>
38e20b07 50#include <xen/hvm.h>
084a2a4e 51#include <xen/hvc-console.h>
211063dc 52#include <xen/acpi.h>
5ead97c8
JF
53
54#include <asm/paravirt.h>
7b6aa335 55#include <asm/apic.h>
5ead97c8 56#include <asm/page.h>
b5401a96 57#include <asm/xen/pci.h>
5ead97c8
JF
58#include <asm/xen/hypercall.h>
59#include <asm/xen/hypervisor.h>
60#include <asm/fixmap.h>
61#include <asm/processor.h>
707ebbc8 62#include <asm/proto.h>
1153968a 63#include <asm/msr-index.h>
6cac5a92 64#include <asm/traps.h>
5ead97c8
JF
65#include <asm/setup.h>
66#include <asm/desc.h>
817a824b 67#include <asm/pgalloc.h>
5ead97c8 68#include <asm/pgtable.h>
f87e4cac 69#include <asm/tlbflush.h>
fefa629a 70#include <asm/reboot.h>
577eebea 71#include <asm/stackprotector.h>
bee6ab53 72#include <asm/hypervisor.h>
73c154c6 73#include <asm/mwait.h>
76a8df7b 74#include <asm/pci_x86.h>
c79c4982 75#include <asm/pat.h>
73c154c6
KRW
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
5ead97c8
JF
84
85#include "xen-ops.h"
3b827c1b 86#include "mmu.h"
f447d56d 87#include "smp.h"
5ead97c8
JF
88#include "multicalls.h"
89
90EXPORT_SYMBOL_GPL(hypercall_page);
91
a520996a
KRW
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 */
5ead97c8 108DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
a520996a
KRW
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 */
5ead97c8 115DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
9f79991d 116
6e833587
JF
117enum xen_domain_type xen_domain_type = XEN_NATIVE;
118EXPORT_SYMBOL_GPL(xen_domain_type);
119
7e77506a
IC
120unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
121EXPORT_SYMBOL(machine_to_phys_mapping);
ccbcdf7c
JB
122unsigned long machine_to_phys_nr;
123EXPORT_SYMBOL(machine_to_phys_nr);
7e77506a 124
5ead97c8
JF
125struct start_info *xen_start_info;
126EXPORT_SYMBOL_GPL(xen_start_info);
127
a0d695c8 128struct shared_info xen_dummy_shared_info;
60223a32 129
38341432
JF
130void *xen_initial_gdt;
131
bee6ab53 132RESERVE_BRK(shared_info_page_brk, PAGE_SIZE);
38e20b07
SY
133__read_mostly int xen_have_vector_callback;
134EXPORT_SYMBOL_GPL(xen_have_vector_callback);
bee6ab53 135
60223a32
JF
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 */
4648da7c 140struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
60223a32
JF
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 */
e4d04071 155static int have_vcpu_info_placement = 1;
60223a32 156
1c32cdc6
DV
157struct 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 */
168static DEFINE_PER_CPU(struct tls_descs, shadow_tls_desc);
169
c06ee78d
MR
170static 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
9c7a7942 178static void xen_vcpu_setup(int cpu)
5ead97c8 179{
60223a32
JF
180 struct vcpu_register_vcpu_info info;
181 int err;
182 struct vcpu_info *vcpup;
183
a0d695c8 184 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
60223a32 185
7f1fc268
KRW
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 }
c06ee78d
MR
201 if (cpu < MAX_VIRT_CPUS)
202 per_cpu(xen_vcpu,cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
60223a32 203
c06ee78d
MR
204 if (!have_vcpu_info_placement) {
205 if (cpu >= MAX_VIRT_CPUS)
206 clamp_max_cpus();
207 return;
208 }
60223a32 209
c06ee78d 210 vcpup = &per_cpu(xen_vcpu_info, cpu);
9976b39b 211 info.mfn = arbitrary_virt_to_mfn(vcpup);
60223a32
JF
212 info.offset = offset_in_page(vcpup);
213
60223a32
JF
214 /* Check to see if the hypervisor will put the vcpu_info
215 structure where we want it, which allows direct access via
a520996a
KRW
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 */
60223a32
JF
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;
c06ee78d 227 clamp_max_cpus();
60223a32
JF
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;
60223a32 232 }
5ead97c8
JF
233}
234
9c7a7942
JF
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 */
240void xen_vcpu_restore(void)
241{
3905bb2a 242 int cpu;
9c7a7942 243
9d328a94 244 for_each_possible_cpu(cpu) {
3905bb2a 245 bool other_cpu = (cpu != smp_processor_id());
9d328a94 246 bool is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL);
9c7a7942 247
9d328a94 248 if (other_cpu && is_up &&
3905bb2a
JF
249 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
250 BUG();
9c7a7942 251
3905bb2a 252 xen_setup_runstate_info(cpu);
9c7a7942 253
3905bb2a 254 if (have_vcpu_info_placement)
9c7a7942 255 xen_vcpu_setup(cpu);
9c7a7942 256
9d328a94 257 if (other_cpu && is_up &&
3905bb2a
JF
258 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
259 BUG();
9c7a7942
JF
260 }
261}
262
5ead97c8
JF
263static void __init xen_banner(void)
264{
95c7c23b
JF
265 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
266 struct xen_extraversion extra;
267 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
268
5ead97c8 269 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
93b1eab3 270 pv_info.name);
95c7c23b
JF
271 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
272 version >> 16, version & 0xffff, extra.extraversion,
e57778a1 273 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
5ead97c8 274}
394b40f6
KRW
275/* Check if running on Xen version (major, minor) or later */
276bool
277xen_running_on_version_or_later(unsigned int major, unsigned int minor)
278{
279 unsigned int version;
280
281 if (!xen_domain())
282 return false;
283
284 version = HYPERVISOR_xen_version(XENVER_version, NULL);
285 if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
286 ((version >> 16) > major))
287 return true;
288 return false;
289}
5ead97c8 290
5e626254
AP
291#define CPUID_THERM_POWER_LEAF 6
292#define APERFMPERF_PRESENT 0
293
e826fe1b
JF
294static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
295static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
296
73c154c6
KRW
297static __read_mostly unsigned int cpuid_leaf1_ecx_set_mask;
298static __read_mostly unsigned int cpuid_leaf5_ecx_val;
299static __read_mostly unsigned int cpuid_leaf5_edx_val;
300
65ea5b03
PA
301static void xen_cpuid(unsigned int *ax, unsigned int *bx,
302 unsigned int *cx, unsigned int *dx)
5ead97c8 303{
82d64699 304 unsigned maskebx = ~0;
e826fe1b 305 unsigned maskecx = ~0;
5ead97c8 306 unsigned maskedx = ~0;
73c154c6 307 unsigned setecx = 0;
5ead97c8
JF
308 /*
309 * Mask out inconvenient features, to try and disable as many
310 * unsupported kernel subsystems as possible.
311 */
82d64699
JF
312 switch (*ax) {
313 case 1:
e826fe1b 314 maskecx = cpuid_leaf1_ecx_mask;
73c154c6 315 setecx = cpuid_leaf1_ecx_set_mask;
e826fe1b 316 maskedx = cpuid_leaf1_edx_mask;
82d64699
JF
317 break;
318
73c154c6
KRW
319 case CPUID_MWAIT_LEAF:
320 /* Synthesize the values.. */
321 *ax = 0;
322 *bx = 0;
323 *cx = cpuid_leaf5_ecx_val;
324 *dx = cpuid_leaf5_edx_val;
325 return;
326
5e626254
AP
327 case CPUID_THERM_POWER_LEAF:
328 /* Disabling APERFMPERF for kernel usage */
329 maskecx = ~(1 << APERFMPERF_PRESENT);
330 break;
331
82d64699
JF
332 case 0xb:
333 /* Suppress extended topology stuff */
334 maskebx = 0;
335 break;
e826fe1b 336 }
5ead97c8
JF
337
338 asm(XEN_EMULATE_PREFIX "cpuid"
65ea5b03
PA
339 : "=a" (*ax),
340 "=b" (*bx),
341 "=c" (*cx),
342 "=d" (*dx)
343 : "0" (*ax), "2" (*cx));
e826fe1b 344
82d64699 345 *bx &= maskebx;
e826fe1b 346 *cx &= maskecx;
73c154c6 347 *cx |= setecx;
65ea5b03 348 *dx &= maskedx;
73c154c6 349
5ead97c8
JF
350}
351
73c154c6
KRW
352static bool __init xen_check_mwait(void)
353{
e3aa4e61 354#ifdef CONFIG_ACPI
73c154c6
KRW
355 struct xen_platform_op op = {
356 .cmd = XENPF_set_processor_pminfo,
357 .u.set_pminfo.id = -1,
358 .u.set_pminfo.type = XEN_PM_PDC,
359 };
360 uint32_t buf[3];
361 unsigned int ax, bx, cx, dx;
362 unsigned int mwait_mask;
363
364 /* We need to determine whether it is OK to expose the MWAIT
365 * capability to the kernel to harvest deeper than C3 states from ACPI
366 * _CST using the processor_harvest_xen.c module. For this to work, we
367 * need to gather the MWAIT_LEAF values (which the cstate.c code
368 * checks against). The hypervisor won't expose the MWAIT flag because
369 * it would break backwards compatibility; so we will find out directly
370 * from the hardware and hypercall.
371 */
372 if (!xen_initial_domain())
373 return false;
374
e3aa4e61
LJ
375 /*
376 * When running under platform earlier than Xen4.2, do not expose
377 * mwait, to avoid the risk of loading native acpi pad driver
378 */
379 if (!xen_running_on_version_or_later(4, 2))
380 return false;
381
73c154c6
KRW
382 ax = 1;
383 cx = 0;
384
385 native_cpuid(&ax, &bx, &cx, &dx);
386
387 mwait_mask = (1 << (X86_FEATURE_EST % 32)) |
388 (1 << (X86_FEATURE_MWAIT % 32));
389
390 if ((cx & mwait_mask) != mwait_mask)
391 return false;
392
393 /* We need to emulate the MWAIT_LEAF and for that we need both
394 * ecx and edx. The hypercall provides only partial information.
395 */
396
397 ax = CPUID_MWAIT_LEAF;
398 bx = 0;
399 cx = 0;
400 dx = 0;
401
402 native_cpuid(&ax, &bx, &cx, &dx);
403
404 /* Ask the Hypervisor whether to clear ACPI_PDC_C_C2C3_FFH. If so,
405 * don't expose MWAIT_LEAF and let ACPI pick the IOPORT version of C3.
406 */
407 buf[0] = ACPI_PDC_REVISION_ID;
408 buf[1] = 1;
409 buf[2] = (ACPI_PDC_C_CAPABILITY_SMP | ACPI_PDC_EST_CAPABILITY_SWSMP);
410
411 set_xen_guest_handle(op.u.set_pminfo.pdc, buf);
412
413 if ((HYPERVISOR_dom0_op(&op) == 0) &&
414 (buf[2] & (ACPI_PDC_C_C1_FFH | ACPI_PDC_C_C2C3_FFH))) {
415 cpuid_leaf5_ecx_val = cx;
416 cpuid_leaf5_edx_val = dx;
417 }
418 return true;
419#else
420 return false;
421#endif
422}
ad3062a0 423static void __init xen_init_cpuid_mask(void)
e826fe1b
JF
424{
425 unsigned int ax, bx, cx, dx;
947ccf9c 426 unsigned int xsave_mask;
e826fe1b
JF
427
428 cpuid_leaf1_edx_mask =
cef12ee5 429 ~((1 << X86_FEATURE_MTRR) | /* disable MTRR */
e826fe1b
JF
430 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
431
432 if (!xen_initial_domain())
433 cpuid_leaf1_edx_mask &=
434 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
435 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
4ea9b9ac
ZD
436
437 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_X2APIC % 32));
438
947ccf9c 439 ax = 1;
5e287830 440 cx = 0;
947ccf9c 441 xen_cpuid(&ax, &bx, &cx, &dx);
e826fe1b 442
947ccf9c
SH
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 */
73c154c6
KRW
450 if (xen_check_mwait())
451 cpuid_leaf1_ecx_set_mask = (1 << (X86_FEATURE_MWAIT % 32));
e826fe1b
JF
452}
453
5ead97c8
JF
454static void xen_set_debugreg(int reg, unsigned long val)
455{
456 HYPERVISOR_set_debugreg(reg, val);
457}
458
459static unsigned long xen_get_debugreg(int reg)
460{
461 return HYPERVISOR_get_debugreg(reg);
462}
463
224101ed 464static void xen_end_context_switch(struct task_struct *next)
5ead97c8 465{
5ead97c8 466 xen_mc_flush();
224101ed 467 paravirt_end_context_switch(next);
5ead97c8
JF
468}
469
470static unsigned long xen_store_tr(void)
471{
472 return 0;
473}
474
a05d2eba 475/*
cef43bf6
JF
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.
a05d2eba
JF
480 */
481static 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;
3f2c206a 488 unsigned char dummy;
a05d2eba
JF
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
3f2c206a
AL
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
a05d2eba
JF
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();
3f2c206a
AL
535
536 preempt_enable();
a05d2eba
JF
537}
538
38ffbe66
JF
539static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
540{
a05d2eba 541 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
38ffbe66
JF
542 int i;
543
3f2c206a
AL
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
a05d2eba
JF
555 for(i = 0; i < entries; i += entries_per_page)
556 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
38ffbe66
JF
557}
558
559static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
560{
a05d2eba 561 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
38ffbe66
JF
562 int i;
563
a05d2eba
JF
564 for(i = 0; i < entries; i += entries_per_page)
565 set_aliased_prot(ldt + i, PAGE_KERNEL);
38ffbe66
JF
566}
567
5ead97c8
JF
568static void xen_set_ldt(const void *addr, unsigned entries)
569{
5ead97c8
JF
570 struct mmuext_op *op;
571 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
572
ab78f7ad
JF
573 trace_xen_cpu_set_ldt(addr, entries);
574
5ead97c8
JF
575 op = mcs.args;
576 op->cmd = MMUEXT_SET_LDT;
4dbf7af6 577 op->arg1.linear_addr = (unsigned long)addr;
5ead97c8
JF
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
6b68f01b 585static void xen_load_gdt(const struct desc_ptr *dtr)
5ead97c8 586{
5ead97c8
JF
587 unsigned long va = dtr->address;
588 unsigned int size = dtr->size + 1;
589 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
3ce5fa7e 590 unsigned long frames[pages];
5ead97c8 591 int f;
5ead97c8 592
577eebea
JF
593 /*
594 * A GDT can be up to 64k in size, which corresponds to 8192
595 * 8-byte entries, or 16 4k pages..
596 */
5ead97c8
JF
597
598 BUG_ON(size > 65536);
599 BUG_ON(va & ~PAGE_MASK);
600
5ead97c8 601 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
6ed6bf42 602 int level;
577eebea 603 pte_t *ptep;
6ed6bf42
JF
604 unsigned long pfn, mfn;
605 void *virt;
606
577eebea
JF
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);
6ed6bf42
JF
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;
9976b39b 622
5ead97c8 623 make_lowmem_page_readonly((void *)va);
6ed6bf42 624 make_lowmem_page_readonly(virt);
5ead97c8
JF
625 }
626
3ce5fa7e
JF
627 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
628 BUG();
5ead97c8
JF
629}
630
577eebea
JF
631/*
632 * load_gdt for early boot, when the gdt is only mapped once
633 */
ad3062a0 634static void __init xen_load_gdt_boot(const struct desc_ptr *dtr)
577eebea
JF
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
59290362
DV
669static 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
5ead97c8
JF
675static void load_TLS_descriptor(struct thread_struct *t,
676 unsigned int cpu, unsigned int i)
677{
1c32cdc6
DV
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);
5ead97c8
JF
691
692 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
693}
694
695static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
696{
8b84ad94 697 /*
ccbeed3a
TH
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.
8a95408e
EH
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().
8b84ad94 714 */
8a95408e
EH
715 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
716#ifdef CONFIG_X86_32
ccbeed3a 717 lazy_load_gs(0);
8a95408e
EH
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);
5ead97c8
JF
730}
731
a8fc1089
EH
732#ifdef CONFIG_X86_64
733static void xen_load_gs_index(unsigned int idx)
734{
735 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
736 BUG();
5ead97c8 737}
a8fc1089 738#endif
5ead97c8
JF
739
740static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
75b8bb3e 741 const void *ptr)
5ead97c8 742{
cef43bf6 743 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
75b8bb3e 744 u64 entry = *(u64 *)ptr;
5ead97c8 745
ab78f7ad
JF
746 trace_xen_cpu_write_ldt_entry(dt, entrynum, entry);
747
f120f13e
JF
748 preempt_disable();
749
5ead97c8
JF
750 xen_mc_flush();
751 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
752 BUG();
f120f13e
JF
753
754 preempt_enable();
5ead97c8
JF
755}
756
e176d367 757static int cvt_gate_to_trap(int vector, const gate_desc *val,
5ead97c8
JF
758 struct trap_info *info)
759{
6cac5a92
JF
760 unsigned long addr;
761
6d02c426 762 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
5ead97c8
JF
763 return 0;
764
765 info->vector = vector;
6cac5a92
JF
766
767 addr = gate_offset(*val);
768#ifdef CONFIG_X86_64
b80119bb
JF
769 /*
770 * Look for known traps using IST, and substitute them
771 * appropriately. The debugger ones are the only ones we care
05e36006
LJ
772 * about. Xen will handle faults like double_fault,
773 * so we should never see them. Warn if
b80119bb
JF
774 * there's an unexpected IST-using fault handler.
775 */
6cac5a92
JF
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;
b80119bb
JF
782 else if (addr == (unsigned long)double_fault ||
783 addr == (unsigned long)nmi) {
784 /* Don't need to handle these */
785 return 0;
786#ifdef CONFIG_X86_MCE
787 } else if (addr == (unsigned long)machine_check) {
05e36006
LJ
788 /*
789 * when xen hypervisor inject vMCE to guest,
790 * use native mce handler to handle it
791 */
792 ;
b80119bb
JF
793#endif
794 } else {
795 /* Some other trap using IST? */
796 if (WARN_ON(val->ist != 0))
797 return 0;
798 }
6cac5a92
JF
799#endif /* CONFIG_X86_64 */
800 info->address = addr;
801
e176d367
EH
802 info->cs = gate_segment(*val);
803 info->flags = val->dpl;
5ead97c8 804 /* interrupt gates clear IF */
6d02c426
JF
805 if (val->type == GATE_INTERRUPT)
806 info->flags |= 1 << 2;
5ead97c8
JF
807
808 return 1;
809}
810
811/* Locations of each CPU's IDT */
6b68f01b 812static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
5ead97c8
JF
813
814/* Set an IDT entry. If the entry is part of the current IDT, then
815 also update Xen. */
8d947344 816static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
5ead97c8 817{
5ead97c8 818 unsigned long p = (unsigned long)&dt[entrynum];
f120f13e
JF
819 unsigned long start, end;
820
ab78f7ad
JF
821 trace_xen_cpu_write_idt_entry(dt, entrynum, g);
822
f120f13e
JF
823 preempt_disable();
824
780f36d8
CL
825 start = __this_cpu_read(idt_desc.address);
826 end = start + __this_cpu_read(idt_desc.size) + 1;
5ead97c8
JF
827
828 xen_mc_flush();
829
8d947344 830 native_write_idt_entry(dt, entrynum, g);
5ead97c8
JF
831
832 if (p >= start && (p + 8) <= end) {
833 struct trap_info info[2];
834
835 info[1].address = 0;
836
e176d367 837 if (cvt_gate_to_trap(entrynum, g, &info[0]))
5ead97c8
JF
838 if (HYPERVISOR_set_trap_table(info))
839 BUG();
840 }
f120f13e
JF
841
842 preempt_enable();
5ead97c8
JF
843}
844
6b68f01b 845static void xen_convert_trap_info(const struct desc_ptr *desc,
f87e4cac 846 struct trap_info *traps)
5ead97c8 847{
5ead97c8
JF
848 unsigned in, out, count;
849
e176d367 850 count = (desc->size+1) / sizeof(gate_desc);
5ead97c8
JF
851 BUG_ON(count > 256);
852
5ead97c8 853 for (in = out = 0; in < count; in++) {
e176d367 854 gate_desc *entry = (gate_desc*)(desc->address) + in;
5ead97c8 855
e176d367 856 if (cvt_gate_to_trap(in, entry, &traps[out]))
5ead97c8
JF
857 out++;
858 }
859 traps[out].address = 0;
f87e4cac
JF
860}
861
862void xen_copy_trap_info(struct trap_info *traps)
863{
6b68f01b 864 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
f87e4cac
JF
865
866 xen_convert_trap_info(desc, traps);
f87e4cac
JF
867}
868
869/* Load a new IDT into Xen. In principle this can be per-CPU, so we
870 hold a spinlock to protect the static traps[] array (static because
871 it avoids allocation, and saves stack space). */
6b68f01b 872static void xen_load_idt(const struct desc_ptr *desc)
f87e4cac
JF
873{
874 static DEFINE_SPINLOCK(lock);
875 static struct trap_info traps[257];
f87e4cac 876
ab78f7ad
JF
877 trace_xen_cpu_load_idt(desc);
878
f87e4cac
JF
879 spin_lock(&lock);
880
f120f13e
JF
881 __get_cpu_var(idt_desc) = *desc;
882
f87e4cac 883 xen_convert_trap_info(desc, traps);
5ead97c8
JF
884
885 xen_mc_flush();
886 if (HYPERVISOR_set_trap_table(traps))
887 BUG();
888
889 spin_unlock(&lock);
890}
891
892/* Write a GDT descriptor entry. Ignore LDT descriptors, since
893 they're handled differently. */
894static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
014b15be 895 const void *desc, int type)
5ead97c8 896{
ab78f7ad
JF
897 trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
898
f120f13e
JF
899 preempt_disable();
900
014b15be
GOC
901 switch (type) {
902 case DESC_LDT:
903 case DESC_TSS:
5ead97c8
JF
904 /* ignore */
905 break;
906
907 default: {
9976b39b 908 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
5ead97c8
JF
909
910 xen_mc_flush();
014b15be 911 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
5ead97c8
JF
912 BUG();
913 }
914
915 }
f120f13e
JF
916
917 preempt_enable();
5ead97c8
JF
918}
919
577eebea
JF
920/*
921 * Version of write_gdt_entry for use at early boot-time needed to
922 * update an entry as simply as possible.
923 */
ad3062a0 924static void __init xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
577eebea
JF
925 const void *desc, int type)
926{
ab78f7ad
JF
927 trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
928
577eebea
JF
929 switch (type) {
930 case DESC_LDT:
931 case DESC_TSS:
932 /* ignore */
933 break;
934
935 default: {
936 xmaddr_t maddr = virt_to_machine(&dt[entry]);
937
938 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
939 dt[entry] = *(struct desc_struct *)desc;
940 }
941
942 }
943}
944
faca6227 945static void xen_load_sp0(struct tss_struct *tss,
a05d2eba 946 struct thread_struct *thread)
5ead97c8 947{
ab78f7ad
JF
948 struct multicall_space mcs;
949
950 mcs = xen_mc_entry(0);
faca6227 951 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
5ead97c8
JF
952 xen_mc_issue(PARAVIRT_LAZY_CPU);
953}
954
c9950bcb 955void xen_set_iopl_mask(unsigned mask)
5ead97c8
JF
956{
957 struct physdev_set_iopl set_iopl;
958
959 /* Force the change at ring 0. */
960 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
961 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
962}
963
964static void xen_io_delay(void)
965{
966}
967
968#ifdef CONFIG_X86_LOCAL_APIC
558daa28
KRW
969static unsigned long xen_set_apic_id(unsigned int x)
970{
971 WARN_ON(1);
972 return x;
973}
974static unsigned int xen_get_apic_id(unsigned long x)
975{
976 return ((x)>>24) & 0xFFu;
977}
ad66dd34 978static u32 xen_apic_read(u32 reg)
5ead97c8 979{
558daa28
KRW
980 struct xen_platform_op op = {
981 .cmd = XENPF_get_cpuinfo,
982 .interface_version = XENPF_INTERFACE_VERSION,
983 .u.pcpu_info.xen_cpuid = 0,
984 };
985 int ret = 0;
986
987 /* Shouldn't need this as APIC is turned off for PV, and we only
988 * get called on the bootup processor. But just in case. */
989 if (!xen_initial_domain() || smp_processor_id())
990 return 0;
991
992 if (reg == APIC_LVR)
993 return 0x10;
994
995 if (reg != APIC_ID)
996 return 0;
997
998 ret = HYPERVISOR_dom0_op(&op);
999 if (ret)
1000 return 0;
1001
1002 return op.u.pcpu_info.apic_id << 24;
5ead97c8 1003}
f87e4cac 1004
ad66dd34 1005static void xen_apic_write(u32 reg, u32 val)
f87e4cac
JF
1006{
1007 /* Warn to see if there's any stray references */
1008 WARN_ON(1);
1009}
ad66dd34 1010
ad66dd34
SS
1011static u64 xen_apic_icr_read(void)
1012{
1013 return 0;
1014}
1015
1016static void xen_apic_icr_write(u32 low, u32 id)
1017{
1018 /* Warn to see if there's any stray references */
1019 WARN_ON(1);
1020}
1021
1022static void xen_apic_wait_icr_idle(void)
1023{
1024 return;
1025}
1026
94a8c3c2
YL
1027static u32 xen_safe_apic_wait_icr_idle(void)
1028{
1029 return 0;
1030}
1031
c1eeb2de
YL
1032static void set_xen_basic_apic_ops(void)
1033{
1034 apic->read = xen_apic_read;
1035 apic->write = xen_apic_write;
1036 apic->icr_read = xen_apic_icr_read;
1037 apic->icr_write = xen_apic_icr_write;
1038 apic->wait_icr_idle = xen_apic_wait_icr_idle;
1039 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
558daa28
KRW
1040 apic->set_apic_id = xen_set_apic_id;
1041 apic->get_apic_id = xen_get_apic_id;
f447d56d
BG
1042
1043#ifdef CONFIG_SMP
1044 apic->send_IPI_allbutself = xen_send_IPI_allbutself;
1045 apic->send_IPI_mask_allbutself = xen_send_IPI_mask_allbutself;
1046 apic->send_IPI_mask = xen_send_IPI_mask;
1047 apic->send_IPI_all = xen_send_IPI_all;
1048 apic->send_IPI_self = xen_send_IPI_self;
1049#endif
c1eeb2de 1050}
ad66dd34 1051
5ead97c8
JF
1052#endif
1053
7b1333aa
JF
1054static void xen_clts(void)
1055{
1056 struct multicall_space mcs;
1057
1058 mcs = xen_mc_entry(0);
1059
1060 MULTI_fpu_taskswitch(mcs.mc, 0);
1061
1062 xen_mc_issue(PARAVIRT_LAZY_CPU);
1063}
1064
a789ed5f
JF
1065static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
1066
1067static unsigned long xen_read_cr0(void)
1068{
2113f469 1069 unsigned long cr0 = this_cpu_read(xen_cr0_value);
a789ed5f
JF
1070
1071 if (unlikely(cr0 == 0)) {
1072 cr0 = native_read_cr0();
2113f469 1073 this_cpu_write(xen_cr0_value, cr0);
a789ed5f
JF
1074 }
1075
1076 return cr0;
1077}
1078
7b1333aa
JF
1079static void xen_write_cr0(unsigned long cr0)
1080{
1081 struct multicall_space mcs;
1082
2113f469 1083 this_cpu_write(xen_cr0_value, cr0);
a789ed5f 1084
7b1333aa
JF
1085 /* Only pay attention to cr0.TS; everything else is
1086 ignored. */
1087 mcs = xen_mc_entry(0);
1088
1089 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
1090
1091 xen_mc_issue(PARAVIRT_LAZY_CPU);
1092}
1093
5ead97c8
JF
1094static void xen_write_cr4(unsigned long cr4)
1095{
2956a351
JF
1096 cr4 &= ~X86_CR4_PGE;
1097 cr4 &= ~X86_CR4_PSE;
1098
1099 native_write_cr4(cr4);
5ead97c8 1100}
1a7bbda5
KRW
1101#ifdef CONFIG_X86_64
1102static inline unsigned long xen_read_cr8(void)
1103{
1104 return 0;
1105}
1106static inline void xen_write_cr8(unsigned long val)
1107{
1108 BUG_ON(val);
1109}
1110#endif
1153968a
JF
1111static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
1112{
1113 int ret;
1114
1115 ret = 0;
1116
f63c2f24 1117 switch (msr) {
1153968a
JF
1118#ifdef CONFIG_X86_64
1119 unsigned which;
1120 u64 base;
1121
1122 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
1123 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
1124 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
1125
1126 set:
1127 base = ((u64)high << 32) | low;
1128 if (HYPERVISOR_set_segment_base(which, base) != 0)
0cc0213e 1129 ret = -EIO;
1153968a
JF
1130 break;
1131#endif
d89961e2
JF
1132
1133 case MSR_STAR:
1134 case MSR_CSTAR:
1135 case MSR_LSTAR:
1136 case MSR_SYSCALL_MASK:
1137 case MSR_IA32_SYSENTER_CS:
1138 case MSR_IA32_SYSENTER_ESP:
1139 case MSR_IA32_SYSENTER_EIP:
1140 /* Fast syscall setup is all done in hypercalls, so
1141 these are all ignored. Stub them out here to stop
1142 Xen console noise. */
1143 break;
1144
41f2e477
JF
1145 case MSR_IA32_CR_PAT:
1146 if (smp_processor_id() == 0)
1147 xen_set_pat(((u64)high << 32) | low);
1148 break;
1149
1153968a
JF
1150 default:
1151 ret = native_write_msr_safe(msr, low, high);
1152 }
1153
1154 return ret;
1155}
1156
0e91398f 1157void xen_setup_shared_info(void)
5ead97c8
JF
1158{
1159 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
15664f96
JF
1160 set_fixmap(FIX_PARAVIRT_BOOTMAP,
1161 xen_start_info->shared_info);
1162
1163 HYPERVISOR_shared_info =
1164 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
5ead97c8
JF
1165 } else
1166 HYPERVISOR_shared_info =
1167 (struct shared_info *)__va(xen_start_info->shared_info);
1168
2e8fe719
JF
1169#ifndef CONFIG_SMP
1170 /* In UP this is as good a place as any to set up shared info */
1171 xen_setup_vcpu_info_placement();
1172#endif
d5edbc1f
JF
1173
1174 xen_setup_mfn_list_list();
2e8fe719
JF
1175}
1176
5f054e31 1177/* This is called once we have the cpu_possible_mask */
0e91398f 1178void xen_setup_vcpu_info_placement(void)
60223a32
JF
1179{
1180 int cpu;
1181
1182 for_each_possible_cpu(cpu)
1183 xen_vcpu_setup(cpu);
1184
1185 /* xen_vcpu_setup managed to place the vcpu_info within the
1186 percpu area for all cpus, so make use of it */
1187 if (have_vcpu_info_placement) {
ecb93d1c
JF
1188 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
1189 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
1190 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
1191 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
93b1eab3 1192 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
60223a32 1193 }
5ead97c8
JF
1194}
1195
ab144f5e
AK
1196static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
1197 unsigned long addr, unsigned len)
6487673b
JF
1198{
1199 char *start, *end, *reloc;
1200 unsigned ret;
1201
1202 start = end = reloc = NULL;
1203
93b1eab3
JF
1204#define SITE(op, x) \
1205 case PARAVIRT_PATCH(op.x): \
6487673b
JF
1206 if (have_vcpu_info_placement) { \
1207 start = (char *)xen_##x##_direct; \
1208 end = xen_##x##_direct_end; \
1209 reloc = xen_##x##_direct_reloc; \
1210 } \
1211 goto patch_site
1212
1213 switch (type) {
93b1eab3
JF
1214 SITE(pv_irq_ops, irq_enable);
1215 SITE(pv_irq_ops, irq_disable);
1216 SITE(pv_irq_ops, save_fl);
1217 SITE(pv_irq_ops, restore_fl);
6487673b
JF
1218#undef SITE
1219
1220 patch_site:
1221 if (start == NULL || (end-start) > len)
1222 goto default_patch;
1223
ab144f5e 1224 ret = paravirt_patch_insns(insnbuf, len, start, end);
6487673b
JF
1225
1226 /* Note: because reloc is assigned from something that
1227 appears to be an array, gcc assumes it's non-null,
1228 but doesn't know its relationship with start and
1229 end. */
1230 if (reloc > start && reloc < end) {
1231 int reloc_off = reloc - start;
ab144f5e
AK
1232 long *relocp = (long *)(insnbuf + reloc_off);
1233 long delta = start - (char *)addr;
6487673b
JF
1234
1235 *relocp += delta;
1236 }
1237 break;
1238
1239 default_patch:
1240 default:
ab144f5e
AK
1241 ret = paravirt_patch_default(type, clobbers, insnbuf,
1242 addr, len);
6487673b
JF
1243 break;
1244 }
1245
1246 return ret;
1247}
1248
ad3062a0 1249static const struct pv_info xen_info __initconst = {
5ead97c8
JF
1250 .paravirt_enabled = 1,
1251 .shared_kernel_pmd = 0,
1252
318f5a2a
AL
1253#ifdef CONFIG_X86_64
1254 .extra_user_64bit_cs = FLAT_USER_CS64,
1255#endif
1256
5ead97c8 1257 .name = "Xen",
93b1eab3 1258};
5ead97c8 1259
ad3062a0 1260static const struct pv_init_ops xen_init_ops __initconst = {
6487673b 1261 .patch = xen_patch,
93b1eab3 1262};
5ead97c8 1263
ad3062a0 1264static const struct pv_cpu_ops xen_cpu_ops __initconst = {
5ead97c8
JF
1265 .cpuid = xen_cpuid,
1266
1267 .set_debugreg = xen_set_debugreg,
1268 .get_debugreg = xen_get_debugreg,
1269
7b1333aa 1270 .clts = xen_clts,
5ead97c8 1271
a789ed5f 1272 .read_cr0 = xen_read_cr0,
7b1333aa 1273 .write_cr0 = xen_write_cr0,
5ead97c8 1274
5ead97c8
JF
1275 .read_cr4 = native_read_cr4,
1276 .read_cr4_safe = native_read_cr4_safe,
1277 .write_cr4 = xen_write_cr4,
1278
1a7bbda5
KRW
1279#ifdef CONFIG_X86_64
1280 .read_cr8 = xen_read_cr8,
1281 .write_cr8 = xen_write_cr8,
1282#endif
1283
5ead97c8
JF
1284 .wbinvd = native_wbinvd,
1285
1286 .read_msr = native_read_msr_safe,
1153968a 1287 .write_msr = xen_write_msr_safe,
1ab46fd3 1288
5ead97c8
JF
1289 .read_tsc = native_read_tsc,
1290 .read_pmc = native_read_pmc,
1291
cd0608e7
KRW
1292 .read_tscp = native_read_tscp,
1293
81e103f1 1294 .iret = xen_iret,
d75cd22f 1295 .irq_enable_sysexit = xen_sysexit,
6fcac6d3
JF
1296#ifdef CONFIG_X86_64
1297 .usergs_sysret32 = xen_sysret32,
1298 .usergs_sysret64 = xen_sysret64,
1299#endif
5ead97c8
JF
1300
1301 .load_tr_desc = paravirt_nop,
1302 .set_ldt = xen_set_ldt,
1303 .load_gdt = xen_load_gdt,
1304 .load_idt = xen_load_idt,
1305 .load_tls = xen_load_tls,
a8fc1089
EH
1306#ifdef CONFIG_X86_64
1307 .load_gs_index = xen_load_gs_index,
1308#endif
5ead97c8 1309
38ffbe66
JF
1310 .alloc_ldt = xen_alloc_ldt,
1311 .free_ldt = xen_free_ldt,
1312
5ead97c8
JF
1313 .store_idt = native_store_idt,
1314 .store_tr = xen_store_tr,
1315
1316 .write_ldt_entry = xen_write_ldt_entry,
1317 .write_gdt_entry = xen_write_gdt_entry,
1318 .write_idt_entry = xen_write_idt_entry,
faca6227 1319 .load_sp0 = xen_load_sp0,
5ead97c8
JF
1320
1321 .set_iopl_mask = xen_set_iopl_mask,
1322 .io_delay = xen_io_delay,
1323
952d1d70
JF
1324 /* Xen takes care of %gs when switching to usermode for us */
1325 .swapgs = paravirt_nop,
1326
224101ed
JF
1327 .start_context_switch = paravirt_start_context_switch,
1328 .end_context_switch = xen_end_context_switch,
93b1eab3
JF
1329};
1330
ad3062a0 1331static const struct pv_apic_ops xen_apic_ops __initconst = {
5ead97c8 1332#ifdef CONFIG_X86_LOCAL_APIC
5ead97c8
JF
1333 .startup_ipi_hook = paravirt_nop,
1334#endif
93b1eab3
JF
1335};
1336
fefa629a
JF
1337static void xen_reboot(int reason)
1338{
349c709f
JF
1339 struct sched_shutdown r = { .reason = reason };
1340
349c709f 1341 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
fefa629a
JF
1342 BUG();
1343}
1344
1345static void xen_restart(char *msg)
1346{
1347 xen_reboot(SHUTDOWN_reboot);
1348}
1349
1350static void xen_emergency_restart(void)
1351{
1352 xen_reboot(SHUTDOWN_reboot);
1353}
1354
1355static void xen_machine_halt(void)
1356{
1357 xen_reboot(SHUTDOWN_poweroff);
1358}
1359
b2abe506
TG
1360static void xen_machine_power_off(void)
1361{
1362 if (pm_power_off)
1363 pm_power_off();
1364 xen_reboot(SHUTDOWN_poweroff);
1365}
1366
fefa629a
JF
1367static void xen_crash_shutdown(struct pt_regs *regs)
1368{
1369 xen_reboot(SHUTDOWN_crash);
1370}
1371
f09f6d19
DD
1372static int
1373xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
1374{
086748e5 1375 xen_reboot(SHUTDOWN_crash);
f09f6d19
DD
1376 return NOTIFY_DONE;
1377}
1378
1379static struct notifier_block xen_panic_block = {
1380 .notifier_call= xen_panic_event,
1381};
1382
1383int xen_panic_handler_init(void)
1384{
1385 atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
1386 return 0;
1387}
1388
ad3062a0 1389static const struct machine_ops xen_machine_ops __initconst = {
fefa629a
JF
1390 .restart = xen_restart,
1391 .halt = xen_machine_halt,
b2abe506 1392 .power_off = xen_machine_power_off,
fefa629a
JF
1393 .shutdown = xen_machine_halt,
1394 .crash_shutdown = xen_crash_shutdown,
1395 .emergency_restart = xen_emergency_restart,
1396};
1397
96f28bc6
DV
1398static void __init xen_boot_params_init_edd(void)
1399{
1400#if IS_ENABLED(CONFIG_EDD)
1401 struct xen_platform_op op;
1402 struct edd_info *edd_info;
1403 u32 *mbr_signature;
1404 unsigned nr;
1405 int ret;
1406
1407 edd_info = boot_params.eddbuf;
1408 mbr_signature = boot_params.edd_mbr_sig_buffer;
1409
1410 op.cmd = XENPF_firmware_info;
1411
1412 op.u.firmware_info.type = XEN_FW_DISK_INFO;
1413 for (nr = 0; nr < EDDMAXNR; nr++) {
1414 struct edd_info *info = edd_info + nr;
1415
1416 op.u.firmware_info.index = nr;
1417 info->params.length = sizeof(info->params);
1418 set_xen_guest_handle(op.u.firmware_info.u.disk_info.edd_params,
1419 &info->params);
1420 ret = HYPERVISOR_dom0_op(&op);
1421 if (ret)
1422 break;
1423
1424#define C(x) info->x = op.u.firmware_info.u.disk_info.x
1425 C(device);
1426 C(version);
1427 C(interface_support);
1428 C(legacy_max_cylinder);
1429 C(legacy_max_head);
1430 C(legacy_sectors_per_track);
1431#undef C
1432 }
1433 boot_params.eddbuf_entries = nr;
1434
1435 op.u.firmware_info.type = XEN_FW_DISK_MBR_SIGNATURE;
1436 for (nr = 0; nr < EDD_MBR_SIG_MAX; nr++) {
1437 op.u.firmware_info.index = nr;
1438 ret = HYPERVISOR_dom0_op(&op);
1439 if (ret)
1440 break;
1441 mbr_signature[nr] = op.u.firmware_info.u.disk_mbr_signature.mbr_signature;
1442 }
1443 boot_params.edd_mbr_sig_buf_entries = nr;
1444#endif
1445}
1446
577eebea
JF
1447/*
1448 * Set up the GDT and segment registers for -fstack-protector. Until
1449 * we do this, we have to be careful not to call any stack-protected
1450 * function, which is most of the kernel.
1451 */
1452static void __init xen_setup_stackprotector(void)
1453{
1454 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1455 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1456
1457 setup_stack_canary_segment(0);
1458 switch_to_new_gdt(0);
1459
1460 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1461 pv_cpu_ops.load_gdt = xen_load_gdt;
1462}
1463
5ead97c8
JF
1464/* First C function to be called on Xen boot */
1465asmlinkage void __init xen_start_kernel(void)
1466{
ec35a69c
KRW
1467 struct physdev_set_iopl set_iopl;
1468 int rc;
5ead97c8
JF
1469
1470 if (!xen_start_info)
1471 return;
1472
6e833587
JF
1473 xen_domain_type = XEN_PV_DOMAIN;
1474
7e77506a
IC
1475 xen_setup_machphys_mapping();
1476
5ead97c8 1477 /* Install Xen paravirt ops */
93b1eab3
JF
1478 pv_info = xen_info;
1479 pv_init_ops = xen_init_ops;
93b1eab3 1480 pv_cpu_ops = xen_cpu_ops;
93b1eab3 1481 pv_apic_ops = xen_apic_ops;
93b1eab3 1482
6b18ae3e 1483 x86_init.resources.memory_setup = xen_memory_setup;
42bbdb43 1484 x86_init.oem.arch_setup = xen_arch_setup;
6f30c1ac 1485 x86_init.oem.banner = xen_banner;
845b3944 1486
409771d2 1487 xen_init_time_ops();
93b1eab3 1488
ce2eef33 1489 /*
577eebea 1490 * Set up some pagetable state before starting to set any ptes.
ce2eef33 1491 */
577eebea 1492
973df35e
JF
1493 xen_init_mmu_ops();
1494
577eebea
JF
1495 /* Prevent unwanted bits from being set in PTEs. */
1496 __supported_pte_mask &= ~_PAGE_GLOBAL;
8eaffa67 1497#if 0
577eebea 1498 if (!xen_initial_domain())
8eaffa67 1499#endif
577eebea
JF
1500 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1501
1502 __supported_pte_mask |= _PAGE_IOMAP;
1503
817a824b
IC
1504 /*
1505 * Prevent page tables from being allocated in highmem, even
1506 * if CONFIG_HIGHPTE is enabled.
1507 */
1508 __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
1509
b75fe4e5 1510 /* Work out if we support NX */
4763ed4d 1511 x86_configure_nx();
b75fe4e5 1512
577eebea
JF
1513 xen_setup_features();
1514
1515 /* Get mfn list */
1516 if (!xen_feature(XENFEAT_auto_translated_physmap))
1517 xen_build_dynamic_phys_to_machine();
1518
1519 /*
1520 * Set up kernel GDT and segment registers, mainly so that
1521 * -fstack-protector code can be executed.
1522 */
1523 xen_setup_stackprotector();
0d1edf46 1524
ce2eef33 1525 xen_init_irq_ops();
e826fe1b
JF
1526 xen_init_cpuid_mask();
1527
94a8c3c2 1528#ifdef CONFIG_X86_LOCAL_APIC
ad66dd34 1529 /*
94a8c3c2 1530 * set up the basic apic ops.
ad66dd34 1531 */
c1eeb2de 1532 set_xen_basic_apic_ops();
ad66dd34 1533#endif
93b1eab3 1534
e57778a1
JF
1535 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1536 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1537 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1538 }
1539
fefa629a
JF
1540 machine_ops = xen_machine_ops;
1541
38341432
JF
1542 /*
1543 * The only reliable way to retain the initial address of the
1544 * percpu gdt_page is to remember it here, so we can go and
1545 * mark it RW later, when the initial percpu area is freed.
1546 */
1547 xen_initial_gdt = &per_cpu(gdt_page, 0);
795f99b6 1548
a9e7062d 1549 xen_smp_init();
5ead97c8 1550
c1f5db1a
IC
1551#ifdef CONFIG_ACPI_NUMA
1552 /*
1553 * The pages we from Xen are not related to machine pages, so
1554 * any NUMA information the kernel tries to get from ACPI will
1555 * be meaningless. Prevent it from trying.
1556 */
1557 acpi_numa = -1;
1558#endif
c79c4982
KRW
1559#ifdef CONFIG_X86_PAT
1560 /*
1561 * For right now disable the PAT. We should remove this once
1562 * git commit 8eaffa67b43e99ae581622c5133e20b0f48bcef1
1563 * (xen/pat: Disable PAT support for now) is reverted.
1564 */
1565 pat_enabled = 0;
1566#endif
60223a32 1567 /* Don't do the full vcpu_info placement stuff until we have a
2e8fe719 1568 possible map and a non-dummy shared_info. */
60223a32 1569 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
5ead97c8 1570
55d80856 1571 local_irq_disable();
2ce802f6 1572 early_boot_irqs_disabled = true;
55d80856 1573
084a2a4e 1574 xen_raw_console_write("mapping kernel into physical memory\n");
3699aad0 1575 xen_setup_kernel_pagetable((pgd_t *)xen_start_info->pt_base, xen_start_info->nr_pages);
5ead97c8 1576
33a84750
JF
1577 /* Allocate and initialize top and mid mfn levels for p2m structure */
1578 xen_build_mfn_list_list();
1579
5ead97c8
JF
1580 /* keep using Xen gdt for now; no urgent need to change it */
1581
e68266b7 1582#ifdef CONFIG_X86_32
93b1eab3 1583 pv_info.kernel_rpl = 1;
5ead97c8 1584 if (xen_feature(XENFEAT_supervisor_mode_kernel))
93b1eab3 1585 pv_info.kernel_rpl = 0;
e68266b7
IC
1586#else
1587 pv_info.kernel_rpl = 0;
1588#endif
5ead97c8 1589 /* set the limit of our address space */
fb1d8404 1590 xen_reserve_top();
5ead97c8 1591
ec35a69c
KRW
1592 /* We used to do this in xen_arch_setup, but that is too late on AMD
1593 * were early_cpu_init (run before ->arch_setup()) calls early_amd_init
1594 * which pokes 0xcf8 port.
1595 */
1596 set_iopl.iopl = 1;
1597 rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
1598 if (rc != 0)
1599 xen_raw_printk("physdev_op failed %d\n", rc);
1600
7d087b68 1601#ifdef CONFIG_X86_32
5ead97c8
JF
1602 /* set up basic CPUID stuff */
1603 cpu_detect(&new_cpu_data);
1604 new_cpu_data.hard_math = 1;
d560bc61 1605 new_cpu_data.wp_works_ok = 1;
5ead97c8 1606 new_cpu_data.x86_capability[0] = cpuid_edx(1);
7d087b68 1607#endif
5ead97c8
JF
1608
1609 /* Poke various useful things into boot_params */
30c82645
PA
1610 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1611 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1612 ? __pa(xen_start_info->mod_start) : 0;
1613 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
b7c3c5c1 1614 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
5ead97c8 1615
6e833587 1616 if (!xen_initial_domain()) {
83abc70a 1617 add_preferred_console("xenboot", 0, NULL);
9e124fe1 1618 add_preferred_console("tty", 0, NULL);
b8c2d3df 1619 add_preferred_console("hvc", 0, NULL);
b5401a96
AN
1620 if (pci_xen)
1621 x86_init.pci.arch_init = pci_xen_init;
5d990b62 1622 } else {
c2419b4a
JF
1623 const struct dom0_vga_console_info *info =
1624 (void *)((char *)xen_start_info +
1625 xen_start_info->console.dom0.info_off);
ffb8b233
KRW
1626 struct xen_platform_op op = {
1627 .cmd = XENPF_firmware_info,
1628 .interface_version = XENPF_INTERFACE_VERSION,
1629 .u.firmware_info.type = XEN_FW_KBD_SHIFT_FLAGS,
1630 };
c2419b4a
JF
1631
1632 xen_init_vga(info, xen_start_info->console.dom0.info_size);
1633 xen_start_info->console.domU.mfn = 0;
1634 xen_start_info->console.domU.evtchn = 0;
1635
ffb8b233
KRW
1636 if (HYPERVISOR_dom0_op(&op) == 0)
1637 boot_params.kbd_status = op.u.firmware_info.u.kbd_shift_flags;
1638
31b3c9d7
KRW
1639 xen_init_apic();
1640
5d990b62
CW
1641 /* Make sure ACS will be enabled */
1642 pci_request_acs();
211063dc
KRW
1643
1644 xen_acpi_sleep_register();
bd49940a
KRW
1645
1646 /* Avoid searching for BIOS MP tables */
1647 x86_init.mpparse.find_smp_config = x86_init_noop;
1648 x86_init.mpparse.get_smp_config = x86_init_uint_noop;
96f28bc6
DV
1649
1650 xen_boot_params_init_edd();
9e124fe1 1651 }
76a8df7b
DV
1652#ifdef CONFIG_PCI
1653 /* PCI BIOS service won't work from a PV guest. */
1654 pci_probe &= ~PCI_PROBE_BIOS;
1655#endif
084a2a4e
JF
1656 xen_raw_console_write("about to get started...\n");
1657
499d19b8
JF
1658 xen_setup_runstate_info(0);
1659
5ead97c8 1660 /* Start the world */
f5d36de0 1661#ifdef CONFIG_X86_32
f0d43100 1662 i386_start_kernel();
f5d36de0 1663#else
084a2a4e 1664 x86_64_start_reservations((char *)__pa_symbol(&boot_params));
f5d36de0 1665#endif
5ead97c8 1666}
bee6ab53 1667
e9daff24 1668void __ref xen_hvm_init_shared_info(void)
bee6ab53 1669{
e9daff24 1670 int cpu;
bee6ab53 1671 struct xen_add_to_physmap xatp;
e9daff24 1672 static struct shared_info *shared_info_page = 0;
bee6ab53 1673
e9daff24
KRW
1674 if (!shared_info_page)
1675 shared_info_page = (struct shared_info *)
1676 extend_brk(PAGE_SIZE, PAGE_SIZE);
bee6ab53
SY
1677 xatp.domid = DOMID_SELF;
1678 xatp.idx = 0;
1679 xatp.space = XENMAPSPACE_shared_info;
e9daff24 1680 xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
bee6ab53
SY
1681 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
1682 BUG();
1683
e9daff24 1684 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
bee6ab53 1685
016b6f5f
SS
1686 /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
1687 * page, we use it in the event channel upcall and in some pvclock
1688 * related functions. We don't need the vcpu_info placement
1689 * optimizations because we don't use any pv_mmu or pv_irq op on
e9daff24
KRW
1690 * HVM.
1691 * When xen_hvm_init_shared_info is run at boot time only vcpu 0 is
1692 * online but xen_hvm_init_shared_info is run at resume time too and
1693 * in that case multiple vcpus might be online. */
1694 for_each_online_cpu(cpu) {
d5b17dbf
KRW
1695 /* Leave it to be NULL. */
1696 if (cpu >= MAX_VIRT_CPUS)
1697 continue;
016b6f5f
SS
1698 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
1699 }
bee6ab53
SY
1700}
1701
e9daff24 1702#ifdef CONFIG_XEN_PVHVM
4ff2d062
OH
1703static void __init init_hvm_pv_info(void)
1704{
e9daff24 1705 int major, minor;
5eb65be2 1706 uint32_t eax, ebx, ecx, edx, pages, msr, base;
4ff2d062
OH
1707 u64 pfn;
1708
1709 base = xen_cpuid_base();
e9daff24
KRW
1710 cpuid(base + 1, &eax, &ebx, &ecx, &edx);
1711
1712 major = eax >> 16;
1713 minor = eax & 0xffff;
1714 printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
1715
4ff2d062
OH
1716 cpuid(base + 2, &pages, &msr, &ecx, &edx);
1717
1718 pfn = __pa(hypercall_page);
1719 wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
1720
1721 xen_setup_features();
1722
1723 pv_info.name = "Xen HVM";
1724
1725 xen_domain_type = XEN_HVM_DOMAIN;
1726}
1727
38e20b07
SY
1728static int __cpuinit xen_hvm_cpu_notify(struct notifier_block *self,
1729 unsigned long action, void *hcpu)
1730{
1731 int cpu = (long)hcpu;
1732 switch (action) {
1733 case CPU_UP_PREPARE:
90d4f553 1734 xen_vcpu_setup(cpu);
7918c92a 1735 if (xen_have_vector_callback) {
99bbb3a8 1736 xen_init_lock_cpu(cpu);
7918c92a
KRW
1737 if (xen_feature(XENFEAT_hvm_safe_pvclock))
1738 xen_setup_timer(cpu);
1739 }
38e20b07
SY
1740 break;
1741 default:
1742 break;
1743 }
1744 return NOTIFY_OK;
1745}
1746
ad3062a0 1747static struct notifier_block xen_hvm_cpu_notifier __cpuinitdata = {
38e20b07
SY
1748 .notifier_call = xen_hvm_cpu_notify,
1749};
1750
9754f73b 1751#ifdef CONFIG_KEXEC
919845cb
VK
1752static void xen_hvm_shutdown(void)
1753{
1754 native_machine_shutdown();
1755 if (kexec_in_progress)
1756 xen_reboot(SHUTDOWN_soft_reset);
1757}
1758
1759static void xen_hvm_crash_shutdown(struct pt_regs *regs)
1760{
1761 native_machine_crash_shutdown(regs);
1762 xen_reboot(SHUTDOWN_soft_reset);
1763}
1764#endif
1765
bee6ab53
SY
1766static void __init xen_hvm_guest_init(void)
1767{
4ff2d062 1768 init_hvm_pv_info();
bee6ab53 1769
016b6f5f 1770 xen_hvm_init_shared_info();
38e20b07
SY
1771
1772 if (xen_feature(XENFEAT_hvm_callback_vector))
1773 xen_have_vector_callback = 1;
99bbb3a8 1774 xen_hvm_smp_init();
38e20b07 1775 register_cpu_notifier(&xen_hvm_cpu_notifier);
c1c5413a 1776 xen_unplug_emulated_devices();
38e20b07 1777 x86_init.irqs.intr_init = xen_init_IRQ;
409771d2 1778 xen_hvm_init_time_ops();
59151001 1779 xen_hvm_init_mmu_ops();
9754f73b 1780#ifdef CONFIG_KEXEC
919845cb
VK
1781 machine_ops.shutdown = xen_hvm_shutdown;
1782 machine_ops.crash_shutdown = xen_hvm_crash_shutdown;
1783#endif
bee6ab53
SY
1784}
1785
1786static bool __init xen_hvm_platform(void)
1787{
1788 if (xen_pv_domain())
1789 return false;
1790
e9daff24 1791 if (!xen_cpuid_base())
bee6ab53
SY
1792 return false;
1793
1794 return true;
1795}
1796
d9b8ca84
SY
1797bool xen_hvm_need_lapic(void)
1798{
1799 if (xen_pv_domain())
1800 return false;
1801 if (!xen_hvm_domain())
1802 return false;
1803 if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
1804 return false;
1805 return true;
1806}
1807EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
1808
ad3062a0 1809const struct hypervisor_x86 x86_hyper_xen_hvm __refconst = {
bee6ab53
SY
1810 .name = "Xen HVM",
1811 .detect = xen_hvm_platform,
1812 .init_platform = xen_hvm_guest_init,
4cca6ea0 1813 .x2apic_available = xen_x2apic_para_available,
bee6ab53
SY
1814};
1815EXPORT_SYMBOL(x86_hyper_xen_hvm);
ca65f9fc 1816#endif