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