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