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