blob: 7b1622089f96595d3fd082bfca6ed5ab02918e4a [file] [log] [blame]
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +01001#include <linux/cpu.h>
2#include <linux/kexec.h>
Juergen Grossa5d5f322017-06-12 13:53:56 +02003#include <linux/memblock.h>
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +01004
5#include <xen/features.h>
6#include <xen/events.h>
7#include <xen/interface/memory.h>
8
9#include <asm/cpu.h>
10#include <asm/smp.h>
11#include <asm/reboot.h>
12#include <asm/setup.h>
13#include <asm/hypervisor.h>
Juergen Grossa5d5f322017-06-12 13:53:56 +020014#include <asm/e820/api.h>
Juergen Gross4ca83dc2017-07-28 12:23:14 +020015#include <asm/early_ioremap.h>
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010016
17#include <asm/xen/cpuid.h>
18#include <asm/xen/hypervisor.h>
Juergen Grossa5d5f322017-06-12 13:53:56 +020019#include <asm/xen/page.h>
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010020
21#include "xen-ops.h"
22#include "mmu.h"
23#include "smp.h"
24
Juergen Gross4ca83dc2017-07-28 12:23:14 +020025static unsigned long shared_info_pfn;
26
Juergen Gross10231f62017-07-28 12:23:13 +020027void xen_hvm_init_shared_info(void)
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010028{
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010029 struct xen_add_to_physmap xatp;
Juergen Grossa5d5f322017-06-12 13:53:56 +020030
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010031 xatp.domid = DOMID_SELF;
32 xatp.idx = 0;
33 xatp.space = XENMAPSPACE_shared_info;
Juergen Gross4ca83dc2017-07-28 12:23:14 +020034 xatp.gpfn = shared_info_pfn;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010035 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
36 BUG();
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010037}
38
Juergen Gross10231f62017-07-28 12:23:13 +020039static void __init reserve_shared_info(void)
40{
41 u64 pa;
42
43 /*
44 * Search for a free page starting at 4kB physical address.
45 * Low memory is preferred to avoid an EPT large page split up
46 * by the mapping.
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.
50 */
51 for (pa = PAGE_SIZE;
52 !e820__mapped_all(pa, pa + PAGE_SIZE, E820_TYPE_RAM) ||
53 memblock_is_reserved(pa);
54 pa += PAGE_SIZE)
55 ;
56
Juergen Gross4ca83dc2017-07-28 12:23:14 +020057 shared_info_pfn = PHYS_PFN(pa);
58
Juergen Gross10231f62017-07-28 12:23:13 +020059 memblock_reserve(pa, PAGE_SIZE);
Juergen Gross4ca83dc2017-07-28 12:23:14 +020060 HYPERVISOR_shared_info = early_memremap(pa, PAGE_SIZE);
61}
62
63static void __init xen_hvm_init_mem_mapping(void)
64{
65 early_memunmap(HYPERVISOR_shared_info, PAGE_SIZE);
66 HYPERVISOR_shared_info = __va(PFN_PHYS(shared_info_pfn));
Juergen Gross10231f62017-07-28 12:23:13 +020067}
68
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010069static void __init init_hvm_pv_info(void)
70{
71 int major, minor;
72 uint32_t eax, ebx, ecx, edx, base;
73
74 base = xen_cpuid_base();
75 eax = cpuid_eax(base + 1);
76
77 major = eax >> 16;
78 minor = eax & 0xffff;
79 printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
80
81 xen_domain_type = XEN_HVM_DOMAIN;
82
83 /* PVH set up hypercall page in xen_prepare_pvh(). */
84 if (xen_pvh_domain())
85 pv_info.name = "Xen PVH";
86 else {
87 u64 pfn;
88 uint32_t msr;
89
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));
94 }
95
96 xen_setup_features();
97
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);
101 else
102 this_cpu_write(xen_vcpu_id, smp_processor_id());
103}
104
105#ifdef CONFIG_KEXEC_CORE
106static void xen_hvm_shutdown(void)
107{
108 native_machine_shutdown();
109 if (kexec_in_progress)
110 xen_reboot(SHUTDOWN_soft_reset);
111}
112
113static void xen_hvm_crash_shutdown(struct pt_regs *regs)
114{
115 native_machine_crash_shutdown(regs);
116 xen_reboot(SHUTDOWN_soft_reset);
117}
118#endif
119
120static int xen_cpu_up_prepare_hvm(unsigned int cpu)
121{
Ankur Arorac9b5d982017-06-02 17:06:01 -0700122 int rc = 0;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100123
124 /*
125 * This can happen if CPU was offlined earlier and
126 * offlining timed out in common_cpu_die().
127 */
128 if (cpu_report_state(cpu) == CPU_DEAD_FROZEN) {
129 xen_smp_intr_free(cpu);
130 xen_uninit_lock_cpu(cpu);
131 }
132
133 if (cpu_acpi_id(cpu) != U32_MAX)
134 per_cpu(xen_vcpu_id, cpu) = cpu_acpi_id(cpu);
135 else
136 per_cpu(xen_vcpu_id, cpu) = cpu;
Ankur Arorac9b5d982017-06-02 17:06:01 -0700137 rc = xen_vcpu_setup(cpu);
138 if (rc)
139 return rc;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100140
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400141 if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100142 xen_setup_timer(cpu);
143
144 rc = xen_smp_intr_init(cpu);
145 if (rc) {
146 WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
147 cpu, rc);
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100148 }
Ankur Arorac9b5d982017-06-02 17:06:01 -0700149 return rc;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100150}
151
152static int xen_cpu_dead_hvm(unsigned int cpu)
153{
154 xen_smp_intr_free(cpu);
155
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400156 if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100157 xen_teardown_timer(cpu);
158
159 return 0;
160}
161
162static void __init xen_hvm_guest_init(void)
163{
164 if (xen_pv_domain())
165 return;
166
167 init_hvm_pv_info();
168
Juergen Gross10231f62017-07-28 12:23:13 +0200169 reserve_shared_info();
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100170 xen_hvm_init_shared_info();
171
Ankur Arora0b64ffb2017-06-02 17:05:59 -0700172 /*
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
175 * related functions.
176 */
177 xen_vcpu_info_reset(0);
178
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100179 xen_panic_handler_init();
180
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400181 if (xen_feature(XENFEAT_hvm_callback_vector))
182 xen_have_vector_callback = 1;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100183
184 xen_hvm_smp_init();
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();
190
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;
196#endif
197}
198
199static bool xen_nopv;
200static __init int xen_parse_nopv(char *arg)
201{
202 xen_nopv = true;
203 return 0;
204}
205early_param("xen_nopv", xen_parse_nopv);
206
207bool xen_hvm_need_lapic(void)
208{
209 if (xen_nopv)
210 return false;
211 if (xen_pv_domain())
212 return false;
213 if (!xen_hvm_domain())
214 return false;
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400215 if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100216 return false;
217 return true;
218}
219EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
220
221static uint32_t __init xen_platform_hvm(void)
222{
223 if (xen_pv_domain() || xen_nopv)
224 return 0;
225
226 return xen_cpuid_base();
227}
228
229const struct hypervisor_x86 x86_hyper_xen_hvm = {
230 .name = "Xen HVM",
231 .detect = xen_platform_hvm,
Juergen Grossf72e38e2017-11-09 14:27:35 +0100232 .init.init_platform = xen_hvm_guest_init,
233 .init.x2apic_available = xen_x2apic_para_available,
234 .init.init_mem_mapping = xen_hvm_init_mem_mapping,
235 .runtime.pin_vcpu = xen_pin_vcpu,
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100236};
237EXPORT_SYMBOL(x86_hyper_xen_hvm);