blob: d23531f5f17ea414eb85bb2cbbee7f447713e6c0 [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>
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010015
16#include <asm/xen/cpuid.h>
17#include <asm/xen/hypervisor.h>
Juergen Grossa5d5f322017-06-12 13:53:56 +020018#include <asm/xen/page.h>
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010019
20#include "xen-ops.h"
21#include "mmu.h"
22#include "smp.h"
23
Juergen Gross10231f62017-07-28 12:23:13 +020024void xen_hvm_init_shared_info(void)
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010025{
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010026 struct xen_add_to_physmap xatp;
Juergen Grossa5d5f322017-06-12 13:53:56 +020027
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010028 xatp.domid = DOMID_SELF;
29 xatp.idx = 0;
30 xatp.space = XENMAPSPACE_shared_info;
Juergen Grossa5d5f322017-06-12 13:53:56 +020031 xatp.gpfn = virt_to_pfn(HYPERVISOR_shared_info);
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010032 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
33 BUG();
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010034}
35
Juergen Gross10231f62017-07-28 12:23:13 +020036static void __init reserve_shared_info(void)
37{
38 u64 pa;
39
40 /*
41 * Search for a free page starting at 4kB physical address.
42 * Low memory is preferred to avoid an EPT large page split up
43 * by the mapping.
44 * Starting below X86_RESERVE_LOW (usually 64kB) is fine as
45 * the BIOS used for HVM guests is well behaved and won't
46 * clobber memory other than the first 4kB.
47 */
48 for (pa = PAGE_SIZE;
49 !e820__mapped_all(pa, pa + PAGE_SIZE, E820_TYPE_RAM) ||
50 memblock_is_reserved(pa);
51 pa += PAGE_SIZE)
52 ;
53
54 memblock_reserve(pa, PAGE_SIZE);
55 HYPERVISOR_shared_info = __va(pa);
56}
57
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010058static void __init init_hvm_pv_info(void)
59{
60 int major, minor;
61 uint32_t eax, ebx, ecx, edx, base;
62
63 base = xen_cpuid_base();
64 eax = cpuid_eax(base + 1);
65
66 major = eax >> 16;
67 minor = eax & 0xffff;
68 printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
69
70 xen_domain_type = XEN_HVM_DOMAIN;
71
72 /* PVH set up hypercall page in xen_prepare_pvh(). */
73 if (xen_pvh_domain())
74 pv_info.name = "Xen PVH";
75 else {
76 u64 pfn;
77 uint32_t msr;
78
79 pv_info.name = "Xen HVM";
80 msr = cpuid_ebx(base + 2);
81 pfn = __pa(hypercall_page);
82 wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
83 }
84
85 xen_setup_features();
86
87 cpuid(base + 4, &eax, &ebx, &ecx, &edx);
88 if (eax & XEN_HVM_CPUID_VCPU_ID_PRESENT)
89 this_cpu_write(xen_vcpu_id, ebx);
90 else
91 this_cpu_write(xen_vcpu_id, smp_processor_id());
92}
93
94#ifdef CONFIG_KEXEC_CORE
95static void xen_hvm_shutdown(void)
96{
97 native_machine_shutdown();
98 if (kexec_in_progress)
99 xen_reboot(SHUTDOWN_soft_reset);
100}
101
102static void xen_hvm_crash_shutdown(struct pt_regs *regs)
103{
104 native_machine_crash_shutdown(regs);
105 xen_reboot(SHUTDOWN_soft_reset);
106}
107#endif
108
109static int xen_cpu_up_prepare_hvm(unsigned int cpu)
110{
Ankur Arorac9b5d982017-06-02 17:06:01 -0700111 int rc = 0;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100112
113 /*
114 * This can happen if CPU was offlined earlier and
115 * offlining timed out in common_cpu_die().
116 */
117 if (cpu_report_state(cpu) == CPU_DEAD_FROZEN) {
118 xen_smp_intr_free(cpu);
119 xen_uninit_lock_cpu(cpu);
120 }
121
122 if (cpu_acpi_id(cpu) != U32_MAX)
123 per_cpu(xen_vcpu_id, cpu) = cpu_acpi_id(cpu);
124 else
125 per_cpu(xen_vcpu_id, cpu) = cpu;
Ankur Arorac9b5d982017-06-02 17:06:01 -0700126 rc = xen_vcpu_setup(cpu);
127 if (rc)
128 return rc;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100129
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400130 if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100131 xen_setup_timer(cpu);
132
133 rc = xen_smp_intr_init(cpu);
134 if (rc) {
135 WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
136 cpu, rc);
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100137 }
Ankur Arorac9b5d982017-06-02 17:06:01 -0700138 return rc;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100139}
140
141static int xen_cpu_dead_hvm(unsigned int cpu)
142{
143 xen_smp_intr_free(cpu);
144
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400145 if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100146 xen_teardown_timer(cpu);
147
148 return 0;
149}
150
151static void __init xen_hvm_guest_init(void)
152{
153 if (xen_pv_domain())
154 return;
155
156 init_hvm_pv_info();
157
Juergen Gross10231f62017-07-28 12:23:13 +0200158 reserve_shared_info();
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100159 xen_hvm_init_shared_info();
160
Ankur Arora0b64ffb2017-06-02 17:05:59 -0700161 /*
162 * xen_vcpu is a pointer to the vcpu_info struct in the shared_info
163 * page, we use it in the event channel upcall and in some pvclock
164 * related functions.
165 */
166 xen_vcpu_info_reset(0);
167
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100168 xen_panic_handler_init();
169
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400170 if (xen_feature(XENFEAT_hvm_callback_vector))
171 xen_have_vector_callback = 1;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100172
173 xen_hvm_smp_init();
174 WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_hvm, xen_cpu_dead_hvm));
175 xen_unplug_emulated_devices();
176 x86_init.irqs.intr_init = xen_init_IRQ;
177 xen_hvm_init_time_ops();
178 xen_hvm_init_mmu_ops();
179
180 if (xen_pvh_domain())
181 machine_ops.emergency_restart = xen_emergency_restart;
182#ifdef CONFIG_KEXEC_CORE
183 machine_ops.shutdown = xen_hvm_shutdown;
184 machine_ops.crash_shutdown = xen_hvm_crash_shutdown;
185#endif
186}
187
188static bool xen_nopv;
189static __init int xen_parse_nopv(char *arg)
190{
191 xen_nopv = true;
192 return 0;
193}
194early_param("xen_nopv", xen_parse_nopv);
195
196bool xen_hvm_need_lapic(void)
197{
198 if (xen_nopv)
199 return false;
200 if (xen_pv_domain())
201 return false;
202 if (!xen_hvm_domain())
203 return false;
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400204 if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100205 return false;
206 return true;
207}
208EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
209
210static uint32_t __init xen_platform_hvm(void)
211{
212 if (xen_pv_domain() || xen_nopv)
213 return 0;
214
215 return xen_cpuid_base();
216}
217
218const struct hypervisor_x86 x86_hyper_xen_hvm = {
219 .name = "Xen HVM",
220 .detect = xen_platform_hvm,
221 .init_platform = xen_hvm_guest_init,
222 .pin_vcpu = xen_pin_vcpu,
223 .x2apic_available = xen_x2apic_para_available,
224};
225EXPORT_SYMBOL(x86_hyper_xen_hvm);