• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	 * Xen is supported only on CPUs with CPUID, so testing for
107 	 * X86_FEATURE_CPUID is a test for early_cpu_init() having been
108 	 * run.
109 	 *
110 	 * Note that __xen_hypercall_setfunc() is noinstr only due to a nasty
111 	 * dependency chain: it is being called via the xen_hypercall static
112 	 * call when running as a PVH or HVM guest. Hypercalls need to be
113 	 * noinstr due to PV guests using hypercalls in noinstr code. So we
114 	 * the PV guest requirement is not of interest here (xen_get_vendor()
115 	 * calls noinstr functions, and static_call_update_early() might do
116 	 * so, too).
117 	 */
118 	instrumentation_begin();
119 
120 	if (!boot_cpu_has(X86_FEATURE_CPUID))
121 		xen_get_vendor();
122 
123 	if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
124 	     boot_cpu_data.x86_vendor == X86_VENDOR_HYGON))
125 		func = xen_hypercall_amd;
126 	else
127 		func = xen_hypercall_intel;
128 
129 	static_call_update_early(xen_hypercall, func);
130 
131 	instrumentation_end();
132 
133 	return func;
134 }
135 
xen_cpu_up_online(unsigned int cpu)136 static int xen_cpu_up_online(unsigned int cpu)
137 {
138 	xen_init_lock_cpu(cpu);
139 	return 0;
140 }
141 
xen_cpuhp_setup(int (* cpu_up_prepare_cb)(unsigned int),int (* cpu_dead_cb)(unsigned int))142 int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
143 		    int (*cpu_dead_cb)(unsigned int))
144 {
145 	int rc;
146 
147 	rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
148 				       "x86/xen/guest:prepare",
149 				       cpu_up_prepare_cb, cpu_dead_cb);
150 	if (rc >= 0) {
151 		rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
152 					       "x86/xen/guest:online",
153 					       xen_cpu_up_online, NULL);
154 		if (rc < 0)
155 			cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
156 	}
157 
158 	return rc >= 0 ? 0 : rc;
159 }
160 
xen_vcpu_setup_restore(int cpu)161 static void xen_vcpu_setup_restore(int cpu)
162 {
163 	/* Any per_cpu(xen_vcpu) is stale, so reset it */
164 	xen_vcpu_info_reset(cpu);
165 
166 	/*
167 	 * For PVH and PVHVM, setup online VCPUs only. The rest will
168 	 * be handled by hotplug.
169 	 */
170 	if (xen_pv_domain() ||
171 	    (xen_hvm_domain() && cpu_online(cpu)))
172 		xen_vcpu_setup(cpu);
173 }
174 
175 /*
176  * On restore, set the vcpu placement up again.
177  * If it fails, then we're in a bad state, since
178  * we can't back out from using it...
179  */
xen_vcpu_restore(void)180 void xen_vcpu_restore(void)
181 {
182 	int cpu;
183 
184 	for_each_possible_cpu(cpu) {
185 		bool other_cpu = (cpu != smp_processor_id());
186 		bool is_up;
187 
188 		if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
189 			continue;
190 
191 		/* Only Xen 4.5 and higher support this. */
192 		is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
193 					   xen_vcpu_nr(cpu), NULL) > 0;
194 
195 		if (other_cpu && is_up &&
196 		    HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
197 			BUG();
198 
199 		if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
200 			xen_setup_runstate_info(cpu);
201 
202 		xen_vcpu_setup_restore(cpu);
203 
204 		if (other_cpu && is_up &&
205 		    HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
206 			BUG();
207 	}
208 }
209 
xen_vcpu_info_reset(int cpu)210 void xen_vcpu_info_reset(int cpu)
211 {
212 	if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
213 		per_cpu(xen_vcpu, cpu) =
214 			&HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
215 	} else {
216 		/* Set to NULL so that if somebody accesses it we get an OOPS */
217 		per_cpu(xen_vcpu, cpu) = NULL;
218 	}
219 }
220 
xen_vcpu_setup(int cpu)221 void xen_vcpu_setup(int cpu)
222 {
223 	struct vcpu_register_vcpu_info info;
224 	int err;
225 	struct vcpu_info *vcpup;
226 
227 	BUILD_BUG_ON(sizeof(*vcpup) > SMP_CACHE_BYTES);
228 	BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
229 
230 	/*
231 	 * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu)
232 	 * and at restore (xen_vcpu_restore). Also called for hotplugged
233 	 * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm).
234 	 * However, the hypercall can only be done once (see below) so if a VCPU
235 	 * is offlined and comes back online then let's not redo the hypercall.
236 	 *
237 	 * For PV it is called during restore (xen_vcpu_restore) and bootup
238 	 * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
239 	 * use this function.
240 	 */
241 	if (xen_hvm_domain()) {
242 		if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
243 			return;
244 	}
245 
246 	vcpup = &per_cpu(xen_vcpu_info, cpu);
247 	info.mfn = arbitrary_virt_to_mfn(vcpup);
248 	info.offset = offset_in_page(vcpup);
249 
250 	/*
251 	 * N.B. This hypercall can _only_ be called once per CPU.
252 	 * Subsequent calls will error out with -EINVAL. This is due to
253 	 * the fact that hypervisor has no unregister variant and this
254 	 * hypercall does not allow to over-write info.mfn and
255 	 * info.offset.
256 	 */
257 	err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, xen_vcpu_nr(cpu),
258 				 &info);
259 	if (err)
260 		panic("register_vcpu_info failed: cpu=%d err=%d\n", cpu, err);
261 
262 	per_cpu(xen_vcpu, cpu) = vcpup;
263 }
264 
xen_banner(void)265 void __init xen_banner(void)
266 {
267 	unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
268 	struct xen_extraversion extra;
269 
270 	HYPERVISOR_xen_version(XENVER_extraversion, &extra);
271 
272 	pr_info("Booting kernel on %s\n", pv_info.name);
273 	pr_info("Xen version: %u.%u%s%s\n",
274 		version >> 16, version & 0xffff, extra.extraversion,
275 		xen_feature(XENFEAT_mmu_pt_update_preserve_ad)
276 		? " (preserve-AD)" : "");
277 }
278 
279 /* Check if running on Xen version (major, minor) or later */
xen_running_on_version_or_later(unsigned int major,unsigned int minor)280 bool xen_running_on_version_or_later(unsigned int major, unsigned int minor)
281 {
282 	unsigned int version;
283 
284 	if (!xen_domain())
285 		return false;
286 
287 	version = HYPERVISOR_xen_version(XENVER_version, NULL);
288 	if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
289 		((version >> 16) > major))
290 		return true;
291 	return false;
292 }
293 
xen_add_preferred_consoles(void)294 void __init xen_add_preferred_consoles(void)
295 {
296 	add_preferred_console("xenboot", 0, NULL);
297 	if (!boot_params.screen_info.orig_video_isVGA)
298 		add_preferred_console("tty", 0, NULL);
299 	add_preferred_console("hvc", 0, NULL);
300 	if (boot_params.screen_info.orig_video_isVGA)
301 		add_preferred_console("tty", 0, NULL);
302 }
303 
xen_reboot(int reason)304 void xen_reboot(int reason)
305 {
306 	struct sched_shutdown r = { .reason = reason };
307 	int cpu;
308 
309 	for_each_online_cpu(cpu)
310 		xen_pmu_finish(cpu);
311 
312 	if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
313 		BUG();
314 }
315 
316 static int reboot_reason = SHUTDOWN_reboot;
317 static bool xen_legacy_crash;
xen_emergency_restart(void)318 void xen_emergency_restart(void)
319 {
320 	xen_reboot(reboot_reason);
321 }
322 
323 static int
xen_panic_event(struct notifier_block * this,unsigned long event,void * ptr)324 xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
325 {
326 	if (!kexec_crash_loaded()) {
327 		if (xen_legacy_crash)
328 			xen_reboot(SHUTDOWN_crash);
329 
330 		reboot_reason = SHUTDOWN_crash;
331 
332 		/*
333 		 * If panic_timeout==0 then we are supposed to wait forever.
334 		 * However, to preserve original dom0 behavior we have to drop
335 		 * into hypervisor. (domU behavior is controlled by its
336 		 * config file)
337 		 */
338 		if (panic_timeout == 0)
339 			panic_timeout = -1;
340 	}
341 	return NOTIFY_DONE;
342 }
343 
parse_xen_legacy_crash(char * arg)344 static int __init parse_xen_legacy_crash(char *arg)
345 {
346 	xen_legacy_crash = true;
347 	return 0;
348 }
349 early_param("xen_legacy_crash", parse_xen_legacy_crash);
350 
351 static struct notifier_block xen_panic_block = {
352 	.notifier_call = xen_panic_event,
353 	.priority = INT_MIN
354 };
355 
xen_panic_handler_init(void)356 int xen_panic_handler_init(void)
357 {
358 	atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
359 	return 0;
360 }
361 
xen_pin_vcpu(int cpu)362 void xen_pin_vcpu(int cpu)
363 {
364 	static bool disable_pinning;
365 	struct sched_pin_override pin_override;
366 	int ret;
367 
368 	if (disable_pinning)
369 		return;
370 
371 	pin_override.pcpu = cpu;
372 	ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
373 
374 	/* Ignore errors when removing override. */
375 	if (cpu < 0)
376 		return;
377 
378 	switch (ret) {
379 	case -ENOSYS:
380 		pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
381 			cpu);
382 		disable_pinning = true;
383 		break;
384 	case -EPERM:
385 		WARN(1, "Trying to pin vcpu without having privilege to do so\n");
386 		disable_pinning = true;
387 		break;
388 	case -EINVAL:
389 	case -EBUSY:
390 		pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
391 			cpu);
392 		break;
393 	case 0:
394 		break;
395 	default:
396 		WARN(1, "rc %d while trying to pin vcpu\n", ret);
397 		disable_pinning = true;
398 	}
399 }
400 
401 #ifdef CONFIG_HOTPLUG_CPU
xen_arch_register_cpu(int num)402 void xen_arch_register_cpu(int num)
403 {
404 	arch_register_cpu(num);
405 }
406 EXPORT_SYMBOL(xen_arch_register_cpu);
407 
xen_arch_unregister_cpu(int num)408 void xen_arch_unregister_cpu(int num)
409 {
410 	arch_unregister_cpu(num);
411 }
412 EXPORT_SYMBOL(xen_arch_unregister_cpu);
413 #endif
414 
415 /* Amount of extra memory space we add to the e820 ranges */
416 struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
417 
xen_add_extra_mem(unsigned long start_pfn,unsigned long n_pfns)418 void __init xen_add_extra_mem(unsigned long start_pfn, unsigned long n_pfns)
419 {
420 	unsigned int i;
421 
422 	/*
423 	 * No need to check for zero size, should happen rarely and will only
424 	 * write a new entry regarded to be unused due to zero size.
425 	 */
426 	for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
427 		/* Add new region. */
428 		if (xen_extra_mem[i].n_pfns == 0) {
429 			xen_extra_mem[i].start_pfn = start_pfn;
430 			xen_extra_mem[i].n_pfns = n_pfns;
431 			break;
432 		}
433 		/* Append to existing region. */
434 		if (xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns ==
435 		    start_pfn) {
436 			xen_extra_mem[i].n_pfns += n_pfns;
437 			break;
438 		}
439 	}
440 	if (i == XEN_EXTRA_MEM_MAX_REGIONS)
441 		printk(KERN_WARNING "Warning: not enough extra memory regions\n");
442 
443 	memblock_reserve(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns));
444 }
445 
446 #ifdef CONFIG_XEN_UNPOPULATED_ALLOC
arch_xen_unpopulated_init(struct resource ** res)447 int __init arch_xen_unpopulated_init(struct resource **res)
448 {
449 	unsigned int i;
450 
451 	if (!xen_domain())
452 		return -ENODEV;
453 
454 	/* Must be set strictly before calling xen_free_unpopulated_pages(). */
455 	*res = &iomem_resource;
456 
457 	/*
458 	 * Initialize with pages from the extra memory regions (see
459 	 * arch/x86/xen/setup.c).
460 	 */
461 	for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
462 		unsigned int j;
463 
464 		for (j = 0; j < xen_extra_mem[i].n_pfns; j++) {
465 			struct page *pg =
466 				pfn_to_page(xen_extra_mem[i].start_pfn + j);
467 
468 			xen_free_unpopulated_pages(1, &pg);
469 		}
470 
471 		/*
472 		 * Account for the region being in the physmap but unpopulated.
473 		 * The value in xen_released_pages is used by the balloon
474 		 * driver to know how much of the physmap is unpopulated and
475 		 * set an accurate initial memory target.
476 		 */
477 		xen_released_pages += xen_extra_mem[i].n_pfns;
478 		/* Zero so region is not also added to the balloon driver. */
479 		xen_extra_mem[i].n_pfns = 0;
480 	}
481 
482 	return 0;
483 }
484 #endif
485