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