1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/console.h>
5 #include <linux/kexec.h>
6 #include <linux/memblock.h>
7 #include <linux/slab.h>
8 #include <linux/panic_notifier.h>
11 #include <xen/features.h>
12 #include <xen/interface/sched.h>
13 #include <xen/interface/version.h>
16 #include <asm/xen/hypercall.h>
17 #include <asm/xen/hypervisor.h>
19 #include <asm/e820/api.h>
20 #include <asm/setup.h>
24 EXPORT_SYMBOL_GPL(hypercall_page
);
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).
36 DEFINE_PER_CPU(struct vcpu_info
*, xen_vcpu
);
37 DEFINE_PER_CPU_ALIGNED(struct vcpu_info
, xen_vcpu_info
);
39 /* Linux <-> Xen vCPU id mapping */
40 DEFINE_PER_CPU(uint32_t, xen_vcpu_id
);
41 EXPORT_PER_CPU_SYMBOL(xen_vcpu_id
);
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
);
48 struct start_info
*xen_start_info
;
49 EXPORT_SYMBOL_GPL(xen_start_info
);
51 struct shared_info xen_dummy_shared_info
;
53 __read_mostly
bool xen_have_vector_callback
= true;
54 EXPORT_SYMBOL_GPL(xen_have_vector_callback
);
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.
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
);
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.
69 struct shared_info
*HYPERVISOR_shared_info
= &xen_dummy_shared_info
;
71 static int xen_cpu_up_online(unsigned int cpu
)
73 xen_init_lock_cpu(cpu
);
77 int xen_cpuhp_setup(int (*cpu_up_prepare_cb
)(unsigned int),
78 int (*cpu_dead_cb
)(unsigned int))
82 rc
= cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE
,
83 "x86/xen/guest:prepare",
84 cpu_up_prepare_cb
, cpu_dead_cb
);
86 rc
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
87 "x86/xen/guest:online",
88 xen_cpu_up_online
, NULL
);
90 cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE
);
93 return rc
>= 0 ? 0 : rc
;
96 static void xen_vcpu_setup_restore(int cpu
)
98 /* Any per_cpu(xen_vcpu) is stale, so reset it */
99 xen_vcpu_info_reset(cpu
);
102 * For PVH and PVHVM, setup online VCPUs only. The rest will
103 * be handled by hotplug.
105 if (xen_pv_domain() ||
106 (xen_hvm_domain() && cpu_online(cpu
)))
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...
115 void xen_vcpu_restore(void)
119 for_each_possible_cpu(cpu
) {
120 bool other_cpu
= (cpu
!= smp_processor_id());
123 if (xen_vcpu_nr(cpu
) == XEN_VCPU_ID_INVALID
)
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;
130 if (other_cpu
&& is_up
&&
131 HYPERVISOR_vcpu_op(VCPUOP_down
, xen_vcpu_nr(cpu
), NULL
))
134 if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock
))
135 xen_setup_runstate_info(cpu
);
137 xen_vcpu_setup_restore(cpu
);
139 if (other_cpu
&& is_up
&&
140 HYPERVISOR_vcpu_op(VCPUOP_up
, xen_vcpu_nr(cpu
), NULL
))
145 void xen_vcpu_info_reset(int cpu
)
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
)];
151 /* Set to NULL so that if somebody accesses it we get an OOPS */
152 per_cpu(xen_vcpu
, cpu
) = NULL
;
156 void xen_vcpu_setup(int cpu
)
158 struct vcpu_register_vcpu_info info
;
160 struct vcpu_info
*vcpup
;
162 BUILD_BUG_ON(sizeof(*vcpup
) > SMP_CACHE_BYTES
);
163 BUG_ON(HYPERVISOR_shared_info
== &xen_dummy_shared_info
);
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.
172 * For PV it is called during restore (xen_vcpu_restore) and bootup
173 * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
176 if (xen_hvm_domain()) {
177 if (per_cpu(xen_vcpu
, cpu
) == &per_cpu(xen_vcpu_info
, cpu
))
181 vcpup
= &per_cpu(xen_vcpu_info
, cpu
);
182 info
.mfn
= arbitrary_virt_to_mfn(vcpup
);
183 info
.offset
= offset_in_page(vcpup
);
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
192 err
= HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info
, xen_vcpu_nr(cpu
),
195 panic("register_vcpu_info failed: cpu=%d err=%d\n", cpu
, err
);
197 per_cpu(xen_vcpu
, cpu
) = vcpup
;
200 void __init
xen_banner(void)
202 unsigned version
= HYPERVISOR_xen_version(XENVER_version
, NULL
);
203 struct xen_extraversion extra
;
205 HYPERVISOR_xen_version(XENVER_extraversion
, &extra
);
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)" : "");
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
)
217 unsigned int version
;
222 version
= HYPERVISOR_xen_version(XENVER_version
, NULL
);
223 if ((((version
>> 16) == major
) && ((version
& 0xffff) >= minor
)) ||
224 ((version
>> 16) > major
))
229 void __init
xen_add_preferred_consoles(void)
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
);
239 void xen_reboot(int reason
)
241 struct sched_shutdown r
= { .reason
= reason
};
244 for_each_online_cpu(cpu
)
247 if (HYPERVISOR_sched_op(SCHEDOP_shutdown
, &r
))
251 static int reboot_reason
= SHUTDOWN_reboot
;
252 static bool xen_legacy_crash
;
253 void xen_emergency_restart(void)
255 xen_reboot(reboot_reason
);
259 xen_panic_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
261 if (!kexec_crash_loaded()) {
262 if (xen_legacy_crash
)
263 xen_reboot(SHUTDOWN_crash
);
265 reboot_reason
= SHUTDOWN_crash
;
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
273 if (panic_timeout
== 0)
279 static int __init
parse_xen_legacy_crash(char *arg
)
281 xen_legacy_crash
= true;
284 early_param("xen_legacy_crash", parse_xen_legacy_crash
);
286 static struct notifier_block xen_panic_block
= {
287 .notifier_call
= xen_panic_event
,
291 int xen_panic_handler_init(void)
293 atomic_notifier_chain_register(&panic_notifier_list
, &xen_panic_block
);
297 void xen_pin_vcpu(int cpu
)
299 static bool disable_pinning
;
300 struct sched_pin_override pin_override
;
306 pin_override
.pcpu
= cpu
;
307 ret
= HYPERVISOR_sched_op(SCHEDOP_pin_override
, &pin_override
);
309 /* Ignore errors when removing override. */
315 pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
317 disable_pinning
= true;
320 WARN(1, "Trying to pin vcpu without having privilege to do so\n");
321 disable_pinning
= true;
325 pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
331 WARN(1, "rc %d while trying to pin vcpu\n", ret
);
332 disable_pinning
= true;
336 #ifdef CONFIG_HOTPLUG_CPU
337 void xen_arch_register_cpu(int num
)
339 arch_register_cpu(num
);
341 EXPORT_SYMBOL(xen_arch_register_cpu
);
343 void xen_arch_unregister_cpu(int num
)
345 arch_unregister_cpu(num
);
347 EXPORT_SYMBOL(xen_arch_unregister_cpu
);
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
;
353 void __init
xen_add_extra_mem(unsigned long start_pfn
, unsigned long n_pfns
)
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.
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
;
368 /* Append to existing region. */
369 if (xen_extra_mem
[i
].start_pfn
+ xen_extra_mem
[i
].n_pfns
==
371 xen_extra_mem
[i
].n_pfns
+= n_pfns
;
375 if (i
== XEN_EXTRA_MEM_MAX_REGIONS
)
376 printk(KERN_WARNING
"Warning: not enough extra memory regions\n");
378 memblock_reserve(PFN_PHYS(start_pfn
), PFN_PHYS(n_pfns
));
381 #ifdef CONFIG_XEN_UNPOPULATED_ALLOC
382 int __init
arch_xen_unpopulated_init(struct resource
**res
)
389 /* Must be set strictly before calling xen_free_unpopulated_pages(). */
390 *res
= &iomem_resource
;
393 * Initialize with pages from the extra memory regions (see
394 * arch/x86/xen/setup.c).
396 for (i
= 0; i
< XEN_EXTRA_MEM_MAX_REGIONS
; i
++) {
399 for (j
= 0; j
< xen_extra_mem
[i
].n_pfns
; j
++) {
401 pfn_to_page(xen_extra_mem
[i
].start_pfn
+ j
);
403 xen_free_unpopulated_pages(1, &pg
);
406 /* Zero so region is not also added to the balloon driver. */
407 xen_extra_mem
[i
].n_pfns
= 0;