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