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>
15 #include <asm/early_ioremap.h>
17 #include <asm/xen/cpuid.h>
18 #include <asm/xen/hypervisor.h>
19 #include <asm/xen/page.h>
25 static unsigned long shared_info_pfn
;
27 void xen_hvm_init_shared_info(void)
29 struct xen_add_to_physmap xatp
;
31 xatp
.domid
= DOMID_SELF
;
33 xatp
.space
= XENMAPSPACE_shared_info
;
34 xatp
.gpfn
= shared_info_pfn
;
35 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap
, &xatp
))
39 static void __init
reserve_shared_info(void)
44 * Search for a free page starting at 4kB physical address.
45 * Low memory is preferred to avoid an EPT large page split up
47 * Starting below X86_RESERVE_LOW (usually 64kB) is fine as
48 * the BIOS used for HVM guests is well behaved and won't
49 * clobber memory other than the first 4kB.
52 !e820__mapped_all(pa
, pa
+ PAGE_SIZE
, E820_TYPE_RAM
) ||
53 memblock_is_reserved(pa
);
57 shared_info_pfn
= PHYS_PFN(pa
);
59 memblock_reserve(pa
, PAGE_SIZE
);
60 HYPERVISOR_shared_info
= early_memremap(pa
, PAGE_SIZE
);
63 static void __init
xen_hvm_init_mem_mapping(void)
65 early_memunmap(HYPERVISOR_shared_info
, PAGE_SIZE
);
66 HYPERVISOR_shared_info
= __va(PFN_PHYS(shared_info_pfn
));
69 static void __init
init_hvm_pv_info(void)
72 uint32_t eax
, ebx
, ecx
, edx
, base
;
74 base
= xen_cpuid_base();
75 eax
= cpuid_eax(base
+ 1);
79 printk(KERN_INFO
"Xen version %d.%d.\n", major
, minor
);
81 xen_domain_type
= XEN_HVM_DOMAIN
;
83 /* PVH set up hypercall page in xen_prepare_pvh(). */
85 pv_info
.name
= "Xen PVH";
90 pv_info
.name
= "Xen HVM";
91 msr
= cpuid_ebx(base
+ 2);
92 pfn
= __pa(hypercall_page
);
93 wrmsr_safe(msr
, (u32
)pfn
, (u32
)(pfn
>> 32));
98 cpuid(base
+ 4, &eax
, &ebx
, &ecx
, &edx
);
99 if (eax
& XEN_HVM_CPUID_VCPU_ID_PRESENT
)
100 this_cpu_write(xen_vcpu_id
, ebx
);
102 this_cpu_write(xen_vcpu_id
, smp_processor_id());
105 #ifdef CONFIG_KEXEC_CORE
106 static void xen_hvm_shutdown(void)
108 native_machine_shutdown();
109 if (kexec_in_progress
)
110 xen_reboot(SHUTDOWN_soft_reset
);
113 static void xen_hvm_crash_shutdown(struct pt_regs
*regs
)
115 native_machine_crash_shutdown(regs
);
116 xen_reboot(SHUTDOWN_soft_reset
);
120 static int xen_cpu_up_prepare_hvm(unsigned int cpu
)
125 * This can happen if CPU was offlined earlier and
126 * offlining timed out in common_cpu_die().
128 if (cpu_report_state(cpu
) == CPU_DEAD_FROZEN
) {
129 xen_smp_intr_free(cpu
);
130 xen_uninit_lock_cpu(cpu
);
133 if (cpu_acpi_id(cpu
) != U32_MAX
)
134 per_cpu(xen_vcpu_id
, cpu
) = cpu_acpi_id(cpu
);
136 per_cpu(xen_vcpu_id
, cpu
) = cpu
;
137 rc
= xen_vcpu_setup(cpu
);
141 if (xen_have_vector_callback
&& xen_feature(XENFEAT_hvm_safe_pvclock
))
142 xen_setup_timer(cpu
);
144 rc
= xen_smp_intr_init(cpu
);
146 WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
152 static int xen_cpu_dead_hvm(unsigned int cpu
)
154 xen_smp_intr_free(cpu
);
156 if (xen_have_vector_callback
&& xen_feature(XENFEAT_hvm_safe_pvclock
))
157 xen_teardown_timer(cpu
);
162 static void __init
xen_hvm_guest_init(void)
169 reserve_shared_info();
170 xen_hvm_init_shared_info();
173 * xen_vcpu is a pointer to the vcpu_info struct in the shared_info
174 * page, we use it in the event channel upcall and in some pvclock
177 xen_vcpu_info_reset(0);
179 xen_panic_handler_init();
181 if (xen_feature(XENFEAT_hvm_callback_vector
))
182 xen_have_vector_callback
= 1;
185 WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_hvm
, xen_cpu_dead_hvm
));
186 xen_unplug_emulated_devices();
187 x86_init
.irqs
.intr_init
= xen_init_IRQ
;
188 xen_hvm_init_time_ops();
189 xen_hvm_init_mmu_ops();
191 if (xen_pvh_domain())
192 machine_ops
.emergency_restart
= xen_emergency_restart
;
193 #ifdef CONFIG_KEXEC_CORE
194 machine_ops
.shutdown
= xen_hvm_shutdown
;
195 machine_ops
.crash_shutdown
= xen_hvm_crash_shutdown
;
199 static bool xen_nopv
;
200 static __init
int xen_parse_nopv(char *arg
)
205 early_param("xen_nopv", xen_parse_nopv
);
207 bool xen_hvm_need_lapic(void)
213 if (!xen_hvm_domain())
215 if (xen_feature(XENFEAT_hvm_pirqs
) && xen_have_vector_callback
)
219 EXPORT_SYMBOL_GPL(xen_hvm_need_lapic
);
221 static uint32_t __init
xen_platform_hvm(void)
223 if (xen_pv_domain() || xen_nopv
)
226 return xen_cpuid_base();
229 const struct hypervisor_x86 x86_hyper_xen_hvm
= {
231 .detect
= xen_platform_hvm
,
232 .init_platform
= xen_hvm_guest_init
,
233 .pin_vcpu
= xen_pin_vcpu
,
234 .x2apic_available
= xen_x2apic_para_available
,
235 .init_mem_mapping
= xen_hvm_init_mem_mapping
,
237 EXPORT_SYMBOL(x86_hyper_xen_hvm
);