1 // SPDX-License-Identifier: GPL-2.0
2
3 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
4 #include <linux/memblock.h>
5 #endif
6 #include <linux/console.h>
7 #include <linux/cpu.h>
8 #include <linux/kexec.h>
9 #include <linux/slab.h>
10 #include <linux/panic_notifier.h>
11
12 #include <xen/xen.h>
13 #include <xen/features.h>
14 #include <xen/interface/sched.h>
15 #include <xen/interface/version.h>
16 #include <xen/page.h>
17
18 #include <asm/xen/hypercall.h>
19 #include <asm/xen/hypervisor.h>
20 #include <asm/cpu.h>
21 #include <asm/e820/api.h>
22 #include <asm/setup.h>
23
24 #include "xen-ops.h"
25 #include "smp.h"
26 #include "pmu.h"
27
28 EXPORT_SYMBOL_GPL(hypercall_page);
29
30 /*
31 * Pointer to the xen_vcpu_info structure or
32 * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
33 * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
34 * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point
35 * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to
36 * acknowledge pending events.
37 * Also more subtly it is used by the patched version of irq enable/disable
38 * e.g. xen_irq_enable_direct and xen_iret in PV mode.
39 *
40 * The desire to be able to do those mask/unmask operations as a single
41 * instruction by using the per-cpu offset held in %gs is the real reason
42 * vcpu info is in a per-cpu pointer and the original reason for this
43 * hypercall.
44 *
45 */
46 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
47
48 /*
49 * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info
50 * hypercall. This can be used both in PV and PVHVM mode. The structure
51 * overrides the default per_cpu(xen_vcpu, cpu) value.
52 */
53 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
54
55 /* Linux <-> Xen vCPU id mapping */
56 DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
57 EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
58
59 unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
60 EXPORT_SYMBOL(machine_to_phys_mapping);
61 unsigned long machine_to_phys_nr;
62 EXPORT_SYMBOL(machine_to_phys_nr);
63
64 struct start_info *xen_start_info;
65 EXPORT_SYMBOL_GPL(xen_start_info);
66
67 struct shared_info xen_dummy_shared_info;
68
69 __read_mostly int xen_have_vector_callback;
70 EXPORT_SYMBOL_GPL(xen_have_vector_callback);
71
72 /*
73 * NB: These need to live in .data or alike because they're used by
74 * xen_prepare_pvh() which runs before clearing the bss.
75 */
76 enum xen_domain_type __ro_after_init xen_domain_type = XEN_NATIVE;
77 EXPORT_SYMBOL_GPL(xen_domain_type);
78 uint32_t __ro_after_init xen_start_flags;
79 EXPORT_SYMBOL(xen_start_flags);
80
81 /*
82 * Point at some empty memory to start with. We map the real shared_info
83 * page as soon as fixmap is up and running.
84 */
85 struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
86
87 /*
88 * Flag to determine whether vcpu info placement is available on all
89 * VCPUs. We assume it is to start with, and then set it to zero on
90 * the first failure. This is because it can succeed on some VCPUs
91 * and not others, since it can involve hypervisor memory allocation,
92 * or because the guest failed to guarantee all the appropriate
93 * constraints on all VCPUs (ie buffer can't cross a page boundary).
94 *
95 * Note that any particular CPU may be using a placed vcpu structure,
96 * but we can only optimise if the all are.
97 *
98 * 0: not available, 1: available
99 */
100 int xen_have_vcpu_info_placement = 1;
101
xen_cpu_up_online(unsigned int cpu)102 static int xen_cpu_up_online(unsigned int cpu)
103 {
104 xen_init_lock_cpu(cpu);
105 return 0;
106 }
107
xen_cpuhp_setup(int (* cpu_up_prepare_cb)(unsigned int),int (* cpu_dead_cb)(unsigned int))108 int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
109 int (*cpu_dead_cb)(unsigned int))
110 {
111 int rc;
112
113 rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
114 "x86/xen/guest:prepare",
115 cpu_up_prepare_cb, cpu_dead_cb);
116 if (rc >= 0) {
117 rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
118 "x86/xen/guest:online",
119 xen_cpu_up_online, NULL);
120 if (rc < 0)
121 cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
122 }
123
124 return rc >= 0 ? 0 : rc;
125 }
126
xen_vcpu_setup_restore(int cpu)127 static int xen_vcpu_setup_restore(int cpu)
128 {
129 int rc = 0;
130
131 /* Any per_cpu(xen_vcpu) is stale, so reset it */
132 xen_vcpu_info_reset(cpu);
133
134 /*
135 * For PVH and PVHVM, setup online VCPUs only. The rest will
136 * be handled by hotplug.
137 */
138 if (xen_pv_domain() ||
139 (xen_hvm_domain() && cpu_online(cpu))) {
140 rc = xen_vcpu_setup(cpu);
141 }
142
143 return rc;
144 }
145
146 /*
147 * On restore, set the vcpu placement up again.
148 * If it fails, then we're in a bad state, since
149 * we can't back out from using it...
150 */
xen_vcpu_restore(void)151 void xen_vcpu_restore(void)
152 {
153 int cpu, rc;
154
155 for_each_possible_cpu(cpu) {
156 bool other_cpu = (cpu != smp_processor_id());
157 bool is_up;
158
159 if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
160 continue;
161
162 /* Only Xen 4.5 and higher support this. */
163 is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
164 xen_vcpu_nr(cpu), NULL) > 0;
165
166 if (other_cpu && is_up &&
167 HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
168 BUG();
169
170 if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
171 xen_setup_runstate_info(cpu);
172
173 rc = xen_vcpu_setup_restore(cpu);
174 if (rc)
175 pr_emerg_once("vcpu restore failed for cpu=%d err=%d. "
176 "System will hang.\n", cpu, rc);
177 /*
178 * In case xen_vcpu_setup_restore() fails, do not bring up the
179 * VCPU. This helps us avoid the resulting OOPS when the VCPU
180 * accesses pvclock_vcpu_time via xen_vcpu (which is NULL.)
181 * Note that this does not improve the situation much -- now the
182 * VM hangs instead of OOPSing -- with the VCPUs that did not
183 * fail, spinning in stop_machine(), waiting for the failed
184 * VCPUs to come up.
185 */
186 if (other_cpu && is_up && (rc == 0) &&
187 HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
188 BUG();
189 }
190 }
191
xen_vcpu_info_reset(int cpu)192 void xen_vcpu_info_reset(int cpu)
193 {
194 if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
195 per_cpu(xen_vcpu, cpu) =
196 &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
197 } else {
198 /* Set to NULL so that if somebody accesses it we get an OOPS */
199 per_cpu(xen_vcpu, cpu) = NULL;
200 }
201 }
202
xen_vcpu_setup(int cpu)203 int xen_vcpu_setup(int cpu)
204 {
205 struct vcpu_register_vcpu_info info;
206 int err;
207 struct vcpu_info *vcpup;
208
209 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
210
211 /*
212 * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu)
213 * and at restore (xen_vcpu_restore). Also called for hotplugged
214 * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm).
215 * However, the hypercall can only be done once (see below) so if a VCPU
216 * is offlined and comes back online then let's not redo the hypercall.
217 *
218 * For PV it is called during restore (xen_vcpu_restore) and bootup
219 * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
220 * use this function.
221 */
222 if (xen_hvm_domain()) {
223 if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
224 return 0;
225 }
226
227 if (xen_have_vcpu_info_placement) {
228 vcpup = &per_cpu(xen_vcpu_info, cpu);
229 info.mfn = arbitrary_virt_to_mfn(vcpup);
230 info.offset = offset_in_page(vcpup);
231
232 /*
233 * Check to see if the hypervisor will put the vcpu_info
234 * structure where we want it, which allows direct access via
235 * a percpu-variable.
236 * N.B. This hypercall can _only_ be called once per CPU.
237 * Subsequent calls will error out with -EINVAL. This is due to
238 * the fact that hypervisor has no unregister variant and this
239 * hypercall does not allow to over-write info.mfn and
240 * info.offset.
241 */
242 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
243 xen_vcpu_nr(cpu), &info);
244
245 if (err) {
246 pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n",
247 cpu, err);
248 xen_have_vcpu_info_placement = 0;
249 } else {
250 /*
251 * This cpu is using the registered vcpu info, even if
252 * later ones fail to.
253 */
254 per_cpu(xen_vcpu, cpu) = vcpup;
255 }
256 }
257
258 if (!xen_have_vcpu_info_placement)
259 xen_vcpu_info_reset(cpu);
260
261 return ((per_cpu(xen_vcpu, cpu) == NULL) ? -ENODEV : 0);
262 }
263
xen_banner(void)264 void __init xen_banner(void)
265 {
266 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
267 struct xen_extraversion extra;
268
269 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
270
271 pr_info("Booting kernel on %s\n", pv_info.name);
272 pr_info("Xen version: %u.%u%s%s\n",
273 version >> 16, version & 0xffff, extra.extraversion,
274 xen_feature(XENFEAT_mmu_pt_update_preserve_ad)
275 ? " (preserve-AD)" : "");
276 }
277
278 /* Check if running on Xen version (major, minor) or later */
xen_running_on_version_or_later(unsigned int major,unsigned int minor)279 bool xen_running_on_version_or_later(unsigned int major, unsigned int minor)
280 {
281 unsigned int version;
282
283 if (!xen_domain())
284 return false;
285
286 version = HYPERVISOR_xen_version(XENVER_version, NULL);
287 if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
288 ((version >> 16) > major))
289 return true;
290 return false;
291 }
292
xen_add_preferred_consoles(void)293 void __init xen_add_preferred_consoles(void)
294 {
295 add_preferred_console("xenboot", 0, NULL);
296 if (!boot_params.screen_info.orig_video_isVGA)
297 add_preferred_console("tty", 0, NULL);
298 add_preferred_console("hvc", 0, NULL);
299 if (boot_params.screen_info.orig_video_isVGA)
300 add_preferred_console("tty", 0, NULL);
301 }
302
xen_reboot(int reason)303 void xen_reboot(int reason)
304 {
305 struct sched_shutdown r = { .reason = reason };
306 int cpu;
307
308 for_each_online_cpu(cpu)
309 xen_pmu_finish(cpu);
310
311 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
312 BUG();
313 }
314
315 static int reboot_reason = SHUTDOWN_reboot;
316 static bool xen_legacy_crash;
xen_emergency_restart(void)317 void xen_emergency_restart(void)
318 {
319 xen_reboot(reboot_reason);
320 }
321
322 static int
xen_panic_event(struct notifier_block * this,unsigned long event,void * ptr)323 xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
324 {
325 if (!kexec_crash_loaded()) {
326 if (xen_legacy_crash)
327 xen_reboot(SHUTDOWN_crash);
328
329 reboot_reason = SHUTDOWN_crash;
330
331 /*
332 * If panic_timeout==0 then we are supposed to wait forever.
333 * However, to preserve original dom0 behavior we have to drop
334 * into hypervisor. (domU behavior is controlled by its
335 * config file)
336 */
337 if (panic_timeout == 0)
338 panic_timeout = -1;
339 }
340 return NOTIFY_DONE;
341 }
342
parse_xen_legacy_crash(char * arg)343 static int __init parse_xen_legacy_crash(char *arg)
344 {
345 xen_legacy_crash = true;
346 return 0;
347 }
348 early_param("xen_legacy_crash", parse_xen_legacy_crash);
349
350 static struct notifier_block xen_panic_block = {
351 .notifier_call = xen_panic_event,
352 .priority = INT_MIN
353 };
354
xen_panic_handler_init(void)355 int xen_panic_handler_init(void)
356 {
357 atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
358 return 0;
359 }
360
xen_pin_vcpu(int cpu)361 void xen_pin_vcpu(int cpu)
362 {
363 static bool disable_pinning;
364 struct sched_pin_override pin_override;
365 int ret;
366
367 if (disable_pinning)
368 return;
369
370 pin_override.pcpu = cpu;
371 ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
372
373 /* Ignore errors when removing override. */
374 if (cpu < 0)
375 return;
376
377 switch (ret) {
378 case -ENOSYS:
379 pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
380 cpu);
381 disable_pinning = true;
382 break;
383 case -EPERM:
384 WARN(1, "Trying to pin vcpu without having privilege to do so\n");
385 disable_pinning = true;
386 break;
387 case -EINVAL:
388 case -EBUSY:
389 pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
390 cpu);
391 break;
392 case 0:
393 break;
394 default:
395 WARN(1, "rc %d while trying to pin vcpu\n", ret);
396 disable_pinning = true;
397 }
398 }
399
400 #ifdef CONFIG_HOTPLUG_CPU
xen_arch_register_cpu(int num)401 void xen_arch_register_cpu(int num)
402 {
403 arch_register_cpu(num);
404 }
405 EXPORT_SYMBOL(xen_arch_register_cpu);
406
xen_arch_unregister_cpu(int num)407 void xen_arch_unregister_cpu(int num)
408 {
409 arch_unregister_cpu(num);
410 }
411 EXPORT_SYMBOL(xen_arch_unregister_cpu);
412 #endif
413