Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <xen/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <xen/features.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <xen/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <asm/xen/hypercall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/xen/hypervisor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/e820/api.h> 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "xen-ops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "smp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "pmu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) EXPORT_SYMBOL_GPL(hypercall_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * Pointer to the xen_vcpu_info structure or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * acknowledge pending events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * Also more subtly it is used by the patched version of irq enable/disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * e.g. xen_irq_enable_direct and xen_iret in PV mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * The desire to be able to do those mask/unmask operations as a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * instruction by using the per-cpu offset held in %gs is the real reason
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * vcpu info is in a per-cpu pointer and the original reason for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * hypercall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * hypercall. This can be used both in PV and PVHVM mode. The structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * overrides the default per_cpu(xen_vcpu, cpu) value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) /* Linux <-> Xen vCPU id mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) EXPORT_SYMBOL(machine_to_phys_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) unsigned long  machine_to_phys_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) EXPORT_SYMBOL(machine_to_phys_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) struct start_info *xen_start_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) EXPORT_SYMBOL_GPL(xen_start_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) struct shared_info xen_dummy_shared_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) __read_mostly int xen_have_vector_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) EXPORT_SYMBOL_GPL(xen_have_vector_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * NB: These need to live in .data or alike because they're used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * xen_prepare_pvh() which runs before clearing the bss.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) enum xen_domain_type __ro_after_init xen_domain_type = XEN_NATIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) EXPORT_SYMBOL_GPL(xen_domain_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) uint32_t xen_start_flags __section(".data") = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) EXPORT_SYMBOL(xen_start_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * Point at some empty memory to start with. We map the real shared_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * page as soon as fixmap is up and running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * Flag to determine whether vcpu info placement is available on all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * VCPUs.  We assume it is to start with, and then set it to zero on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * the first failure.  This is because it can succeed on some VCPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * and not others, since it can involve hypervisor memory allocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * or because the guest failed to guarantee all the appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * constraints on all VCPUs (ie buffer can't cross a page boundary).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * Note that any particular CPU may be using a placed vcpu structure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * but we can only optimise if the all are.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * 0: not available, 1: available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) int xen_have_vcpu_info_placement = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static int xen_cpu_up_online(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	xen_init_lock_cpu(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		    int (*cpu_dead_cb)(unsigned int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				       "x86/xen/guest:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				       cpu_up_prepare_cb, cpu_dead_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (rc >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 					       "x86/xen/guest:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 					       xen_cpu_up_online, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return rc >= 0 ? 0 : rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int xen_vcpu_setup_restore(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* Any per_cpu(xen_vcpu) is stale, so reset it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	xen_vcpu_info_reset(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	 * For PVH and PVHVM, setup online VCPUs only. The rest will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 * be handled by hotplug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (xen_pv_domain() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	    (xen_hvm_domain() && cpu_online(cpu))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		rc = xen_vcpu_setup(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  * On restore, set the vcpu placement up again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * If it fails, then we're in a bad state, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * we can't back out from using it...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) void xen_vcpu_restore(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int cpu, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		bool other_cpu = (cpu != smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		bool is_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		/* Only Xen 4.5 and higher support this. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 					   xen_vcpu_nr(cpu), NULL) > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		if (other_cpu && is_up &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		    HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			xen_setup_runstate_info(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		rc = xen_vcpu_setup_restore(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			pr_emerg_once("vcpu restore failed for cpu=%d err=%d. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 					"System will hang.\n", cpu, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		 * In case xen_vcpu_setup_restore() fails, do not bring up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		 * VCPU. This helps us avoid the resulting OOPS when the VCPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		 * accesses pvclock_vcpu_time via xen_vcpu (which is NULL.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		 * Note that this does not improve the situation much -- now the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		 * VM hangs instead of OOPSing -- with the VCPUs that did not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		 * fail, spinning in stop_machine(), waiting for the failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		 * VCPUs to come up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (other_cpu && is_up && (rc == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		    HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) void xen_vcpu_info_reset(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		per_cpu(xen_vcpu, cpu) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			&HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		/* Set to NULL so that if somebody accesses it we get an OOPS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		per_cpu(xen_vcpu, cpu) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int xen_vcpu_setup(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct vcpu_register_vcpu_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct vcpu_info *vcpup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * and at restore (xen_vcpu_restore). Also called for hotplugged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * However, the hypercall can only be done once (see below) so if a VCPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * is offlined and comes back online then let's not redo the hypercall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	 * For PV it is called during restore (xen_vcpu_restore) and bootup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	 * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	 * use this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (xen_hvm_domain()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (xen_have_vcpu_info_placement) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		vcpup = &per_cpu(xen_vcpu_info, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		info.mfn = arbitrary_virt_to_mfn(vcpup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		info.offset = offset_in_page(vcpup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		 * Check to see if the hypervisor will put the vcpu_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		 * structure where we want it, which allows direct access via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		 * a percpu-variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		 * N.B. This hypercall can _only_ be called once per CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		 * Subsequent calls will error out with -EINVAL. This is due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		 * the fact that hypervisor has no unregister variant and this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		 * hypercall does not allow to over-write info.mfn and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		 * info.offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 					 xen_vcpu_nr(cpu), &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 				     cpu, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			xen_have_vcpu_info_placement = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			 * This cpu is using the registered vcpu info, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			 * later ones fail to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			per_cpu(xen_vcpu, cpu) = vcpup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (!xen_have_vcpu_info_placement)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		xen_vcpu_info_reset(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return ((per_cpu(xen_vcpu, cpu) == NULL) ? -ENODEV : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) void xen_reboot(int reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct sched_shutdown r = { .reason = reason };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	for_each_online_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		xen_pmu_finish(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int reboot_reason = SHUTDOWN_reboot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static bool xen_legacy_crash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) void xen_emergency_restart(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	xen_reboot(reboot_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (!kexec_crash_loaded()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		if (xen_legacy_crash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			xen_reboot(SHUTDOWN_crash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		reboot_reason = SHUTDOWN_crash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		 * If panic_timeout==0 then we are supposed to wait forever.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		 * However, to preserve original dom0 behavior we have to drop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		 * into hypervisor. (domU behavior is controlled by its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		 * config file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (panic_timeout == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			panic_timeout = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int __init parse_xen_legacy_crash(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	xen_legacy_crash = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) early_param("xen_legacy_crash", parse_xen_legacy_crash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static struct notifier_block xen_panic_block = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	.notifier_call = xen_panic_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	.priority = INT_MIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int xen_panic_handler_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) void xen_pin_vcpu(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	static bool disable_pinning;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	struct sched_pin_override pin_override;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (disable_pinning)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	pin_override.pcpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	/* Ignore errors when removing override. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (cpu < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	case -ENOSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		disable_pinning = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	case -EPERM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		WARN(1, "Trying to pin vcpu without having privilege to do so\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		disable_pinning = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	case -EINVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	case -EBUSY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		WARN(1, "rc %d while trying to pin vcpu\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		disable_pinning = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) #ifdef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) void xen_arch_register_cpu(int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	arch_register_cpu(num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) EXPORT_SYMBOL(xen_arch_register_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) void xen_arch_unregister_cpu(int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	arch_unregister_cpu(num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) EXPORT_SYMBOL(xen_arch_unregister_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) #endif