xref: /linux/arch/x86/xen/enlighten.c (revision 3088d26962e802efa3aa5188f88f82a957f50b22)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/console.h>
4 #include <linux/cpu.h>
5 #include <linux/instrumentation.h>
6 #include <linux/kexec.h>
7 #include <linux/memblock.h>
8 #include <linux/slab.h>
9 #include <linux/panic_notifier.h>
10 
11 #include <xen/xen.h>
12 #include <xen/features.h>
13 #include <xen/interface/sched.h>
14 #include <xen/interface/version.h>
15 #include <xen/page.h>
16 
17 #include <asm/xen/hypercall.h>
18 #include <asm/xen/hypervisor.h>
19 #include <asm/cpu.h>
20 #include <asm/e820/api.h>
21 #include <asm/setup.h>
22 
23 #include "xen-ops.h"
24 
25 DEFINE_STATIC_CALL(xen_hypercall, xen_hypercall_hvm);
26 EXPORT_STATIC_CALL_TRAMP(xen_hypercall);
27 
28 /*
29  * Pointer to the xen_vcpu_info structure or
30  * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
31  * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
32  * but during boot it is switched to point to xen_vcpu_info.
33  * The pointer is used in xen_evtchn_do_upcall to acknowledge pending events.
34  * Make sure that xen_vcpu_info doesn't cross a page boundary by making it
35  * cache-line aligned (the struct is guaranteed to have a size of 64 bytes,
36  * which matches the cache line size of 64-bit x86 processors).
37  */
38 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
39 DEFINE_PER_CPU_ALIGNED(struct vcpu_info, xen_vcpu_info);
40 
41 /* Linux <-> Xen vCPU id mapping */
42 DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
43 EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
44 
45 unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
46 EXPORT_SYMBOL(machine_to_phys_mapping);
47 unsigned long  machine_to_phys_nr;
48 EXPORT_SYMBOL(machine_to_phys_nr);
49 
50 struct start_info *xen_start_info;
51 EXPORT_SYMBOL_GPL(xen_start_info);
52 
53 struct shared_info xen_dummy_shared_info;
54 
55 __read_mostly bool xen_have_vector_callback = true;
56 EXPORT_SYMBOL_GPL(xen_have_vector_callback);
57 
58 /*
59  * NB: These need to live in .data or alike because they're used by
60  * xen_prepare_pvh() which runs before clearing the bss.
61  */
62 enum xen_domain_type __ro_after_init xen_domain_type = XEN_NATIVE;
63 EXPORT_SYMBOL_GPL(xen_domain_type);
64 uint32_t __ro_after_init xen_start_flags;
65 EXPORT_SYMBOL(xen_start_flags);
66 
67 /*
68  * Point at some empty memory to start with. We map the real shared_info
69  * page as soon as fixmap is up and running.
70  */
71 struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
72 
73 /* Number of pages released from the initial allocation. */
74 unsigned long xen_released_pages;
75 
xen_get_vendor(void)76 static __ref void xen_get_vendor(void)
77 {
78 	init_cpu_devs();
79 	cpu_detect(&boot_cpu_data);
80 	get_cpu_vendor(&boot_cpu_data);
81 }
82 
xen_hypercall_setfunc(void)83 void xen_hypercall_setfunc(void)
84 {
85 	if (static_call_query(xen_hypercall) != xen_hypercall_hvm)
86 		return;
87 
88 	if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
89 	     boot_cpu_data.x86_vendor == X86_VENDOR_HYGON))
90 		static_call_update(xen_hypercall, xen_hypercall_amd);
91 	else
92 		static_call_update(xen_hypercall, xen_hypercall_intel);
93 }
94 
95 /*
96  * Evaluate processor vendor in order to select the correct hypercall
97  * function for HVM/PVH guests.
98  * Might be called very early in boot before vendor has been set by
99  * early_cpu_init().
100  */
__xen_hypercall_setfunc(void)101 noinstr void *__xen_hypercall_setfunc(void)
102 {
103 	void (*func)(void);
104 
105 	/*
106 	 * Note that __xen_hypercall_setfunc() is noinstr only due to a nasty
107 	 * dependency chain: it is being called via the xen_hypercall static
108 	 * call when running as a PVH or HVM guest. Hypercalls need to be
109 	 * noinstr due to PV guests using hypercalls in noinstr code. So we
110 	 * can safely tag the function body as "instrumentation ok", since
111 	 * the PV guest requirement is not of interest here (xen_get_vendor()
112 	 * calls noinstr functions, and static_call_update_early() might do
113 	 * so, too).
114 	 */
115 	instrumentation_begin();
116 
117 	xen_get_vendor();
118 
119 	if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
120 	     boot_cpu_data.x86_vendor == X86_VENDOR_HYGON))
121 		func = xen_hypercall_amd;
122 	else
123 		func = xen_hypercall_intel;
124 
125 	static_call_update_early(xen_hypercall, func);
126 
127 	instrumentation_end();
128 
129 	return func;
130 }
131 
xen_cpu_up_online(unsigned int cpu)132 static int xen_cpu_up_online(unsigned int cpu)
133 {
134 	xen_init_lock_cpu(cpu);
135 	return 0;
136 }
137 
xen_cpuhp_setup(int (* cpu_up_prepare_cb)(unsigned int),int (* cpu_dead_cb)(unsigned int))138 int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
139 		    int (*cpu_dead_cb)(unsigned int))
140 {
141 	int rc;
142 
143 	rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
144 				       "x86/xen/guest:prepare",
145 				       cpu_up_prepare_cb, cpu_dead_cb);
146 	if (rc >= 0) {
147 		rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
148 					       "x86/xen/guest:online",
149 					       xen_cpu_up_online, NULL);
150 		if (rc < 0)
151 			cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
152 	}
153 
154 	return rc >= 0 ? 0 : rc;
155 }
156 
xen_vcpu_setup_restore(int cpu)157 static void xen_vcpu_setup_restore(int cpu)
158 {
159 	/* Any per_cpu(xen_vcpu) is stale, so reset it */
160 	xen_vcpu_info_reset(cpu);
161 
162 	/*
163 	 * For PVH and PVHVM, setup online VCPUs only. The rest will
164 	 * be handled by hotplug.
165 	 */
166 	if (xen_pv_domain() ||
167 	    (xen_hvm_domain() && cpu_online(cpu)))
168 		xen_vcpu_setup(cpu);
169 }
170 
171 /*
172  * On restore, set the vcpu placement up again.
173  * If it fails, then we're in a bad state, since
174  * we can't back out from using it...
175  */
xen_vcpu_restore(void)176 void xen_vcpu_restore(void)
177 {
178 	int cpu;
179 
180 	for_each_possible_cpu(cpu) {
181 		bool other_cpu = (cpu != smp_processor_id());
182 		bool is_up;
183 
184 		if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
185 			continue;
186 
187 		/* Only Xen 4.5 and higher support this. */
188 		is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
189 					   xen_vcpu_nr(cpu), NULL) > 0;
190 
191 		if (other_cpu && is_up &&
192 		    HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
193 			BUG();
194 
195 		if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
196 			xen_setup_runstate_info(cpu);
197 
198 		xen_vcpu_setup_restore(cpu);
199 
200 		if (other_cpu && is_up &&
201 		    HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
202 			BUG();
203 	}
204 }
205 
xen_vcpu_info_reset(int cpu)206 void xen_vcpu_info_reset(int cpu)
207 {
208 	if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
209 		per_cpu(xen_vcpu, cpu) =
210 			&HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
211 	} else {
212 		/* Set to NULL so that if somebody accesses it we get an OOPS */
213 		per_cpu(xen_vcpu, cpu) = NULL;
214 	}
215 }
216 
xen_vcpu_setup(int cpu)217 void xen_vcpu_setup(int cpu)
218 {
219 	struct vcpu_register_vcpu_info info;
220 	int err;
221 	struct vcpu_info *vcpup;
222 
223 	BUILD_BUG_ON(sizeof(*vcpup) > SMP_CACHE_BYTES);
224 	BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
225 
226 	/*
227 	 * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu)
228 	 * and at restore (xen_vcpu_restore). Also called for hotplugged
229 	 * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm).
230 	 * However, the hypercall can only be done once (see below) so if a VCPU
231 	 * is offlined and comes back online then let's not redo the hypercall.
232 	 *
233 	 * For PV it is called during restore (xen_vcpu_restore) and bootup
234 	 * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
235 	 * use this function.
236 	 */
237 	if (xen_hvm_domain()) {
238 		if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
239 			return;
240 	}
241 
242 	vcpup = &per_cpu(xen_vcpu_info, cpu);
243 	info.mfn = arbitrary_virt_to_mfn(vcpup);
244 	info.offset = offset_in_page(vcpup);
245 
246 	/*
247 	 * N.B. This hypercall can _only_ be called once per CPU.
248 	 * Subsequent calls will error out with -EINVAL. This is due to
249 	 * the fact that hypervisor has no unregister variant and this
250 	 * hypercall does not allow to over-write info.mfn and
251 	 * info.offset.
252 	 */
253 	err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, xen_vcpu_nr(cpu),
254 				 &info);
255 	if (err)
256 		panic("register_vcpu_info failed: cpu=%d err=%d\n", cpu, err);
257 
258 	per_cpu(xen_vcpu, cpu) = vcpup;
259 }
260 
xen_banner(void)261 void __init xen_banner(void)
262 {
263 	unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
264 	struct xen_extraversion extra;
265 
266 	HYPERVISOR_xen_version(XENVER_extraversion, &extra);
267 
268 	pr_info("Booting kernel on %s\n", pv_info.name);
269 	pr_info("Xen version: %u.%u%s%s\n",
270 		version >> 16, version & 0xffff, extra.extraversion,
271 		xen_feature(XENFEAT_mmu_pt_update_preserve_ad)
272 		? " (preserve-AD)" : "");
273 }
274 
275 /* Check if running on Xen version (major, minor) or later */
xen_running_on_version_or_later(unsigned int major,unsigned int minor)276 bool xen_running_on_version_or_later(unsigned int major, unsigned int minor)
277 {
278 	unsigned int version;
279 
280 	if (!xen_domain())
281 		return false;
282 
283 	version = HYPERVISOR_xen_version(XENVER_version, NULL);
284 	if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
285 		((version >> 16) > major))
286 		return true;
287 	return false;
288 }
289 
xen_add_preferred_consoles(void)290 void __init xen_add_preferred_consoles(void)
291 {
292 	add_preferred_console("xenboot", 0, NULL);
293 	if (!boot_params.screen_info.orig_video_isVGA)
294 		add_preferred_console("tty", 0, NULL);
295 	add_preferred_console("hvc", 0, NULL);
296 	if (boot_params.screen_info.orig_video_isVGA)
297 		add_preferred_console("tty", 0, NULL);
298 }
299 
xen_reboot(int reason)300 void xen_reboot(int reason)
301 {
302 	struct sched_shutdown r = { .reason = reason };
303 	int cpu;
304 
305 	for_each_online_cpu(cpu)
306 		xen_pmu_finish(cpu);
307 
308 	if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
309 		BUG();
310 }
311 
312 static int reboot_reason = SHUTDOWN_reboot;
313 static bool xen_legacy_crash;
xen_emergency_restart(void)314 void xen_emergency_restart(void)
315 {
316 	xen_reboot(reboot_reason);
317 }
318 
319 static int
xen_panic_event(struct notifier_block * this,unsigned long event,void * ptr)320 xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
321 {
322 	if (!kexec_crash_loaded()) {
323 		if (xen_legacy_crash)
324 			xen_reboot(SHUTDOWN_crash);
325 
326 		reboot_reason = SHUTDOWN_crash;
327 
328 		/*
329 		 * If panic_timeout==0 then we are supposed to wait forever.
330 		 * However, to preserve original dom0 behavior we have to drop
331 		 * into hypervisor. (domU behavior is controlled by its
332 		 * config file)
333 		 */
334 		if (panic_timeout == 0)
335 			panic_timeout = -1;
336 	}
337 	return NOTIFY_DONE;
338 }
339 
parse_xen_legacy_crash(char * arg)340 static int __init parse_xen_legacy_crash(char *arg)
341 {
342 	xen_legacy_crash = true;
343 	return 0;
344 }
345 early_param("xen_legacy_crash", parse_xen_legacy_crash);
346 
347 static struct notifier_block xen_panic_block = {
348 	.notifier_call = xen_panic_event,
349 	.priority = INT_MIN
350 };
351 
xen_panic_handler_init(void)352 int xen_panic_handler_init(void)
353 {
354 	atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
355 	return 0;
356 }
357 
xen_pin_vcpu(int cpu)358 void xen_pin_vcpu(int cpu)
359 {
360 	static bool disable_pinning;
361 	struct sched_pin_override pin_override;
362 	int ret;
363 
364 	if (disable_pinning)
365 		return;
366 
367 	pin_override.pcpu = cpu;
368 	ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
369 
370 	/* Ignore errors when removing override. */
371 	if (cpu < 0)
372 		return;
373 
374 	switch (ret) {
375 	case -ENOSYS:
376 		pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
377 			cpu);
378 		disable_pinning = true;
379 		break;
380 	case -EPERM:
381 		WARN(1, "Trying to pin vcpu without having privilege to do so\n");
382 		disable_pinning = true;
383 		break;
384 	case -EINVAL:
385 	case -EBUSY:
386 		pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
387 			cpu);
388 		break;
389 	case 0:
390 		break;
391 	default:
392 		WARN(1, "rc %d while trying to pin vcpu\n", ret);
393 		disable_pinning = true;
394 	}
395 }
396 
397 #ifdef CONFIG_HOTPLUG_CPU
xen_arch_register_cpu(int num)398 void xen_arch_register_cpu(int num)
399 {
400 	arch_register_cpu(num);
401 }
402 EXPORT_SYMBOL(xen_arch_register_cpu);
403 
xen_arch_unregister_cpu(int num)404 void xen_arch_unregister_cpu(int num)
405 {
406 	arch_unregister_cpu(num);
407 }
408 EXPORT_SYMBOL(xen_arch_unregister_cpu);
409 #endif
410 
411 /* Amount of extra memory space we add to the e820 ranges */
412 struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
413 
xen_add_extra_mem(unsigned long start_pfn,unsigned long n_pfns)414 void __init xen_add_extra_mem(unsigned long start_pfn, unsigned long n_pfns)
415 {
416 	unsigned int i;
417 
418 	/*
419 	 * No need to check for zero size, should happen rarely and will only
420 	 * write a new entry regarded to be unused due to zero size.
421 	 */
422 	for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
423 		/* Add new region. */
424 		if (xen_extra_mem[i].n_pfns == 0) {
425 			xen_extra_mem[i].start_pfn = start_pfn;
426 			xen_extra_mem[i].n_pfns = n_pfns;
427 			break;
428 		}
429 		/* Append to existing region. */
430 		if (xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns ==
431 		    start_pfn) {
432 			xen_extra_mem[i].n_pfns += n_pfns;
433 			break;
434 		}
435 	}
436 	if (i == XEN_EXTRA_MEM_MAX_REGIONS)
437 		printk(KERN_WARNING "Warning: not enough extra memory regions\n");
438 
439 	memblock_reserve(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns));
440 }
441 
442 #ifdef CONFIG_XEN_UNPOPULATED_ALLOC
arch_xen_unpopulated_init(struct resource ** res)443 int __init arch_xen_unpopulated_init(struct resource **res)
444 {
445 	unsigned int i;
446 
447 	if (!xen_domain())
448 		return -ENODEV;
449 
450 	/* Must be set strictly before calling xen_free_unpopulated_pages(). */
451 	*res = &iomem_resource;
452 
453 	/*
454 	 * Initialize with pages from the extra memory regions (see
455 	 * arch/x86/xen/setup.c).
456 	 */
457 	for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
458 		unsigned int j;
459 
460 		for (j = 0; j < xen_extra_mem[i].n_pfns; j++) {
461 			struct page *pg =
462 				pfn_to_page(xen_extra_mem[i].start_pfn + j);
463 
464 			xen_free_unpopulated_pages(1, &pg);
465 		}
466 
467 		/*
468 		 * Account for the region being in the physmap but unpopulated.
469 		 * The value in xen_released_pages is used by the balloon
470 		 * driver to know how much of the physmap is unpopulated and
471 		 * set an accurate initial memory target.
472 		 */
473 		xen_released_pages += xen_extra_mem[i].n_pfns;
474 		/* Zero so region is not also added to the balloon driver. */
475 		xen_extra_mem[i].n_pfns = 0;
476 	}
477 
478 	return 0;
479 }
480 #endif
481