2 #include <linux/kexec.h>
3 #include <linux/memblock.h>
5 #include <xen/features.h>
6 #include <xen/events.h>
7 #include <xen/interface/memory.h>
11 #include <asm/reboot.h>
12 #include <asm/setup.h>
13 #include <asm/hypervisor.h>
14 #include <asm/e820/api.h>
16 #include <asm/xen/cpuid.h>
17 #include <asm/xen/hypervisor.h>
18 #include <asm/xen/page.h>
24 void __ref
xen_hvm_init_shared_info(void)
26 struct xen_add_to_physmap xatp
;
29 if (HYPERVISOR_shared_info
== &xen_dummy_shared_info
) {
31 * Search for a free page starting at 4kB physical address.
32 * Low memory is preferred to avoid an EPT large page split up
34 * Starting below X86_RESERVE_LOW (usually 64kB) is fine as
35 * the BIOS used for HVM guests is well behaved and won't
36 * clobber memory other than the first 4kB.
39 !e820__mapped_all(pa
, pa
+ PAGE_SIZE
, E820_TYPE_RAM
) ||
40 memblock_is_reserved(pa
);
44 memblock_reserve(pa
, PAGE_SIZE
);
45 HYPERVISOR_shared_info
= __va(pa
);
48 xatp
.domid
= DOMID_SELF
;
50 xatp
.space
= XENMAPSPACE_shared_info
;
51 xatp
.gpfn
= virt_to_pfn(HYPERVISOR_shared_info
);
52 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap
, &xatp
))
56 static void __init
init_hvm_pv_info(void)
59 uint32_t eax
, ebx
, ecx
, edx
, base
;
61 base
= xen_cpuid_base();
62 eax
= cpuid_eax(base
+ 1);
66 printk(KERN_INFO
"Xen version %d.%d.\n", major
, minor
);
68 xen_domain_type
= XEN_HVM_DOMAIN
;
70 /* PVH set up hypercall page in xen_prepare_pvh(). */
72 pv_info
.name
= "Xen PVH";
77 pv_info
.name
= "Xen HVM";
78 msr
= cpuid_ebx(base
+ 2);
79 pfn
= __pa(hypercall_page
);
80 wrmsr_safe(msr
, (u32
)pfn
, (u32
)(pfn
>> 32));
85 cpuid(base
+ 4, &eax
, &ebx
, &ecx
, &edx
);
86 if (eax
& XEN_HVM_CPUID_VCPU_ID_PRESENT
)
87 this_cpu_write(xen_vcpu_id
, ebx
);
89 this_cpu_write(xen_vcpu_id
, smp_processor_id());
92 #ifdef CONFIG_KEXEC_CORE
93 static void xen_hvm_shutdown(void)
95 native_machine_shutdown();
96 if (kexec_in_progress
)
97 xen_reboot(SHUTDOWN_soft_reset
);
100 static void xen_hvm_crash_shutdown(struct pt_regs
*regs
)
102 native_machine_crash_shutdown(regs
);
103 xen_reboot(SHUTDOWN_soft_reset
);
107 static int xen_cpu_up_prepare_hvm(unsigned int cpu
)
112 * This can happen if CPU was offlined earlier and
113 * offlining timed out in common_cpu_die().
115 if (cpu_report_state(cpu
) == CPU_DEAD_FROZEN
) {
116 xen_smp_intr_free(cpu
);
117 xen_uninit_lock_cpu(cpu
);
120 if (cpu_acpi_id(cpu
) != U32_MAX
)
121 per_cpu(xen_vcpu_id
, cpu
) = cpu_acpi_id(cpu
);
123 per_cpu(xen_vcpu_id
, cpu
) = cpu
;
124 rc
= xen_vcpu_setup(cpu
);
128 if (xen_have_vector_callback
&& xen_feature(XENFEAT_hvm_safe_pvclock
))
129 xen_setup_timer(cpu
);
131 rc
= xen_smp_intr_init(cpu
);
133 WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
139 static int xen_cpu_dead_hvm(unsigned int cpu
)
141 xen_smp_intr_free(cpu
);
143 if (xen_have_vector_callback
&& xen_feature(XENFEAT_hvm_safe_pvclock
))
144 xen_teardown_timer(cpu
);
149 static void __init
xen_hvm_guest_init(void)
156 xen_hvm_init_shared_info();
159 * xen_vcpu is a pointer to the vcpu_info struct in the shared_info
160 * page, we use it in the event channel upcall and in some pvclock
163 xen_vcpu_info_reset(0);
165 xen_panic_handler_init();
167 if (xen_feature(XENFEAT_hvm_callback_vector
))
168 xen_have_vector_callback
= 1;
171 WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_hvm
, xen_cpu_dead_hvm
));
172 xen_unplug_emulated_devices();
173 x86_init
.irqs
.intr_init
= xen_init_IRQ
;
174 xen_hvm_init_time_ops();
175 xen_hvm_init_mmu_ops();
177 if (xen_pvh_domain())
178 machine_ops
.emergency_restart
= xen_emergency_restart
;
179 #ifdef CONFIG_KEXEC_CORE
180 machine_ops
.shutdown
= xen_hvm_shutdown
;
181 machine_ops
.crash_shutdown
= xen_hvm_crash_shutdown
;
185 static bool xen_nopv
;
186 static __init
int xen_parse_nopv(char *arg
)
191 early_param("xen_nopv", xen_parse_nopv
);
193 bool xen_hvm_need_lapic(void)
199 if (!xen_hvm_domain())
201 if (xen_feature(XENFEAT_hvm_pirqs
) && xen_have_vector_callback
)
205 EXPORT_SYMBOL_GPL(xen_hvm_need_lapic
);
207 static uint32_t __init
xen_platform_hvm(void)
209 if (xen_pv_domain() || xen_nopv
)
212 return xen_cpuid_base();
215 const struct hypervisor_x86 x86_hyper_xen_hvm
= {
217 .detect
= xen_platform_hvm
,
218 .init_platform
= xen_hvm_guest_init
,
219 .pin_vcpu
= xen_pin_vcpu
,
220 .x2apic_available
= xen_x2apic_para_available
,
222 EXPORT_SYMBOL(x86_hyper_xen_hvm
);