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