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