1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Xen SMP support 4 * 5 * This file implements the Xen versions of smp_ops. SMP under Xen is 6 * very straightforward. Bringing a CPU up is simply a matter of 7 * loading its initial context and setting it running. 8 * 9 * IPIs are handled through the Xen event mechanism. 10 * 11 * Because virtual CPUs can be scheduled onto any real CPU, there's no 12 * useful topology information for the kernel to make use of. As a 13 * result, all CPUs are treated as if they're single-core and 14 * single-threaded. 15 */ 16 #include <linux/sched.h> 17 #include <linux/sched/task_stack.h> 18 #include <linux/err.h> 19 #include <linux/slab.h> 20 #include <linux/smp.h> 21 #include <linux/irq_work.h> 22 #include <linux/tick.h> 23 #include <linux/nmi.h> 24 #include <linux/cpuhotplug.h> 25 #include <linux/stackprotector.h> 26 #include <linux/pgtable.h> 27 28 #include <asm/paravirt.h> 29 #include <asm/idtentry.h> 30 #include <asm/desc.h> 31 #include <asm/cpu.h> 32 #include <asm/io_apic.h> 33 34 #include <xen/interface/xen.h> 35 #include <xen/interface/vcpu.h> 36 #include <xen/interface/xenpmu.h> 37 38 #include <asm/spec-ctrl.h> 39 #include <asm/xen/interface.h> 40 #include <asm/xen/hypercall.h> 41 42 #include <xen/xen.h> 43 #include <xen/page.h> 44 #include <xen/events.h> 45 46 #include <xen/hvc-console.h> 47 #include "xen-ops.h" 48 #include "mmu.h" 49 #include "smp.h" 50 #include "pmu.h" 51 52 cpumask_var_t xen_cpu_initialized_map; 53 54 static DEFINE_PER_CPU(struct xen_common_irq, xen_irq_work) = { .irq = -1 }; 55 static DEFINE_PER_CPU(struct xen_common_irq, xen_pmu_irq) = { .irq = -1 }; 56 57 static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id); 58 59 static void cpu_bringup(void) 60 { 61 int cpu; 62 63 cr4_init(); 64 cpuhp_ap_sync_alive(); 65 cpu_init(); 66 fpu__init_cpu(); 67 touch_softlockup_watchdog(); 68 69 /* PVH runs in ring 0 and allows us to do native syscalls. Yay! */ 70 if (!xen_feature(XENFEAT_supervisor_mode_kernel)) { 71 xen_enable_sysenter(); 72 xen_enable_syscall(); 73 } 74 cpu = smp_processor_id(); 75 smp_store_cpu_info(cpu); 76 set_cpu_sibling_map(cpu); 77 78 speculative_store_bypass_ht_init(); 79 80 xen_setup_cpu_clockevents(); 81 82 notify_cpu_starting(cpu); 83 84 set_cpu_online(cpu, true); 85 86 smp_mb(); 87 88 /* We can take interrupts now: we're officially "up". */ 89 local_irq_enable(); 90 } 91 92 asmlinkage __visible void cpu_bringup_and_idle(void) 93 { 94 cpu_bringup(); 95 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 96 } 97 98 void xen_smp_intr_free_pv(unsigned int cpu) 99 { 100 kfree(per_cpu(xen_irq_work, cpu).name); 101 per_cpu(xen_irq_work, cpu).name = NULL; 102 if (per_cpu(xen_irq_work, cpu).irq >= 0) { 103 unbind_from_irqhandler(per_cpu(xen_irq_work, cpu).irq, NULL); 104 per_cpu(xen_irq_work, cpu).irq = -1; 105 } 106 107 kfree(per_cpu(xen_pmu_irq, cpu).name); 108 per_cpu(xen_pmu_irq, cpu).name = NULL; 109 if (per_cpu(xen_pmu_irq, cpu).irq >= 0) { 110 unbind_from_irqhandler(per_cpu(xen_pmu_irq, cpu).irq, NULL); 111 per_cpu(xen_pmu_irq, cpu).irq = -1; 112 } 113 } 114 115 int xen_smp_intr_init_pv(unsigned int cpu) 116 { 117 int rc; 118 char *callfunc_name, *pmu_name; 119 120 callfunc_name = kasprintf(GFP_KERNEL, "irqwork%d", cpu); 121 per_cpu(xen_irq_work, cpu).name = callfunc_name; 122 rc = bind_ipi_to_irqhandler(XEN_IRQ_WORK_VECTOR, 123 cpu, 124 xen_irq_work_interrupt, 125 IRQF_PERCPU|IRQF_NOBALANCING, 126 callfunc_name, 127 NULL); 128 if (rc < 0) 129 goto fail; 130 per_cpu(xen_irq_work, cpu).irq = rc; 131 132 if (is_xen_pmu) { 133 pmu_name = kasprintf(GFP_KERNEL, "pmu%d", cpu); 134 per_cpu(xen_pmu_irq, cpu).name = pmu_name; 135 rc = bind_virq_to_irqhandler(VIRQ_XENPMU, cpu, 136 xen_pmu_irq_handler, 137 IRQF_PERCPU|IRQF_NOBALANCING, 138 pmu_name, NULL); 139 if (rc < 0) 140 goto fail; 141 per_cpu(xen_pmu_irq, cpu).irq = rc; 142 } 143 144 return 0; 145 146 fail: 147 xen_smp_intr_free_pv(cpu); 148 return rc; 149 } 150 151 static void __init _get_smp_config(unsigned int early) 152 { 153 int i, rc; 154 unsigned int subtract = 0; 155 156 if (early) 157 return; 158 159 num_processors = 0; 160 disabled_cpus = 0; 161 for (i = 0; i < nr_cpu_ids; i++) { 162 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL); 163 if (rc >= 0) { 164 num_processors++; 165 set_cpu_possible(i, true); 166 } else { 167 set_cpu_possible(i, false); 168 set_cpu_present(i, false); 169 subtract++; 170 } 171 } 172 #ifdef CONFIG_HOTPLUG_CPU 173 /* This is akin to using 'nr_cpus' on the Linux command line. 174 * Which is OK as when we use 'dom0_max_vcpus=X' we can only 175 * have up to X, while nr_cpu_ids is greater than X. This 176 * normally is not a problem, except when CPU hotplugging 177 * is involved and then there might be more than X CPUs 178 * in the guest - which will not work as there is no 179 * hypercall to expand the max number of VCPUs an already 180 * running guest has. So cap it up to X. */ 181 if (subtract) 182 set_nr_cpu_ids(nr_cpu_ids - subtract); 183 #endif 184 /* Pretend to be a proper enumerated system */ 185 smp_found_config = 1; 186 } 187 188 static void __init xen_pv_smp_prepare_boot_cpu(void) 189 { 190 BUG_ON(smp_processor_id() != 0); 191 native_smp_prepare_boot_cpu(); 192 193 if (!xen_feature(XENFEAT_writable_page_tables)) 194 /* We've switched to the "real" per-cpu gdt, so make 195 * sure the old memory can be recycled. */ 196 make_lowmem_page_readwrite(xen_initial_gdt); 197 198 xen_setup_vcpu_info_placement(); 199 200 /* 201 * The alternative logic (which patches the unlock/lock) runs before 202 * the smp bootup up code is activated. Hence we need to set this up 203 * the core kernel is being patched. Otherwise we will have only 204 * modules patched but not core code. 205 */ 206 xen_init_spinlocks(); 207 } 208 209 static void __init xen_pv_smp_prepare_cpus(unsigned int max_cpus) 210 { 211 unsigned cpu; 212 213 if (ioapic_is_disabled) { 214 char *m = (max_cpus == 0) ? 215 "The nosmp parameter is incompatible with Xen; " \ 216 "use Xen dom0_max_vcpus=1 parameter" : 217 "The noapic parameter is incompatible with Xen"; 218 219 xen_raw_printk(m); 220 panic(m); 221 } 222 xen_init_lock_cpu(0); 223 224 smp_prepare_cpus_common(); 225 226 speculative_store_bypass_ht_init(); 227 228 xen_pmu_init(0); 229 230 if (xen_smp_intr_init(0) || xen_smp_intr_init_pv(0)) 231 BUG(); 232 233 if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL)) 234 panic("could not allocate xen_cpu_initialized_map\n"); 235 236 cpumask_copy(xen_cpu_initialized_map, cpumask_of(0)); 237 238 /* Restrict the possible_map according to max_cpus. */ 239 while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) { 240 for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--) 241 continue; 242 set_cpu_possible(cpu, false); 243 } 244 245 for_each_possible_cpu(cpu) 246 set_cpu_present(cpu, true); 247 } 248 249 static int 250 cpu_initialize_context(unsigned int cpu, struct task_struct *idle) 251 { 252 struct vcpu_guest_context *ctxt; 253 struct desc_struct *gdt; 254 unsigned long gdt_mfn; 255 256 if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map)) 257 return 0; 258 259 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL); 260 if (ctxt == NULL) { 261 cpumask_clear_cpu(cpu, xen_cpu_initialized_map); 262 return -ENOMEM; 263 } 264 265 gdt = get_cpu_gdt_rw(cpu); 266 267 /* 268 * Bring up the CPU in cpu_bringup_and_idle() with the stack 269 * pointing just below where pt_regs would be if it were a normal 270 * kernel entry. 271 */ 272 ctxt->user_regs.eip = (unsigned long)asm_cpu_bringup_and_idle; 273 ctxt->flags = VGCF_IN_KERNEL; 274 ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */ 275 ctxt->user_regs.ds = __USER_DS; 276 ctxt->user_regs.es = __USER_DS; 277 ctxt->user_regs.ss = __KERNEL_DS; 278 ctxt->user_regs.cs = __KERNEL_CS; 279 ctxt->user_regs.esp = (unsigned long)task_pt_regs(idle); 280 281 xen_copy_trap_info(ctxt->trap_ctxt); 282 283 BUG_ON((unsigned long)gdt & ~PAGE_MASK); 284 285 gdt_mfn = arbitrary_virt_to_mfn(gdt); 286 make_lowmem_page_readonly(gdt); 287 make_lowmem_page_readonly(mfn_to_virt(gdt_mfn)); 288 289 ctxt->gdt_frames[0] = gdt_mfn; 290 ctxt->gdt_ents = GDT_ENTRIES; 291 292 /* 293 * Set SS:SP that Xen will use when entering guest kernel mode 294 * from guest user mode. Subsequent calls to load_sp0() can 295 * change this value. 296 */ 297 ctxt->kernel_ss = __KERNEL_DS; 298 ctxt->kernel_sp = task_top_of_stack(idle); 299 300 ctxt->gs_base_kernel = per_cpu_offset(cpu); 301 ctxt->event_callback_eip = 302 (unsigned long)xen_asm_exc_xen_hypervisor_callback; 303 ctxt->failsafe_callback_eip = 304 (unsigned long)xen_failsafe_callback; 305 per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir); 306 307 ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_gfn(swapper_pg_dir)); 308 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, xen_vcpu_nr(cpu), ctxt)) 309 BUG(); 310 311 kfree(ctxt); 312 return 0; 313 } 314 315 static int xen_pv_kick_ap(unsigned int cpu, struct task_struct *idle) 316 { 317 int rc; 318 319 rc = common_cpu_up(cpu, idle); 320 if (rc) 321 return rc; 322 323 xen_setup_runstate_info(cpu); 324 325 /* make sure interrupts start blocked */ 326 per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1; 327 328 rc = cpu_initialize_context(cpu, idle); 329 if (rc) 330 return rc; 331 332 xen_pmu_init(cpu); 333 334 /* 335 * Why is this a BUG? If the hypercall fails then everything can be 336 * rolled back, no? 337 */ 338 BUG_ON(HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL)); 339 340 return 0; 341 } 342 343 static void xen_pv_poll_sync_state(void) 344 { 345 HYPERVISOR_sched_op(SCHEDOP_yield, NULL); 346 } 347 348 #ifdef CONFIG_HOTPLUG_CPU 349 static int xen_pv_cpu_disable(void) 350 { 351 unsigned int cpu = smp_processor_id(); 352 if (cpu == 0) 353 return -EBUSY; 354 355 cpu_disable_common(); 356 357 load_cr3(swapper_pg_dir); 358 return 0; 359 } 360 361 static void xen_pv_cpu_die(unsigned int cpu) 362 { 363 while (HYPERVISOR_vcpu_op(VCPUOP_is_up, xen_vcpu_nr(cpu), NULL)) { 364 __set_current_state(TASK_UNINTERRUPTIBLE); 365 schedule_timeout(HZ/10); 366 } 367 } 368 369 static void xen_pv_cleanup_dead_cpu(unsigned int cpu) 370 { 371 xen_smp_intr_free(cpu); 372 xen_uninit_lock_cpu(cpu); 373 xen_teardown_timer(cpu); 374 xen_pmu_finish(cpu); 375 } 376 377 static void __noreturn xen_pv_play_dead(void) /* used only with HOTPLUG_CPU */ 378 { 379 play_dead_common(); 380 HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(smp_processor_id()), NULL); 381 xen_cpu_bringup_again((unsigned long)task_pt_regs(current)); 382 BUG(); 383 } 384 385 #else /* !CONFIG_HOTPLUG_CPU */ 386 static int xen_pv_cpu_disable(void) 387 { 388 return -ENOSYS; 389 } 390 391 static void xen_pv_cpu_die(unsigned int cpu) 392 { 393 BUG(); 394 } 395 396 static void xen_pv_cleanup_dead_cpu(unsigned int cpu) 397 { 398 BUG(); 399 } 400 401 static void __noreturn xen_pv_play_dead(void) 402 { 403 BUG(); 404 } 405 406 #endif 407 static void stop_self(void *v) 408 { 409 int cpu = smp_processor_id(); 410 411 /* make sure we're not pinning something down */ 412 load_cr3(swapper_pg_dir); 413 /* should set up a minimal gdt */ 414 415 set_cpu_online(cpu, false); 416 417 HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL); 418 BUG(); 419 } 420 421 static void xen_pv_stop_other_cpus(int wait) 422 { 423 smp_call_function(stop_self, NULL, wait); 424 } 425 426 static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id) 427 { 428 irq_work_run(); 429 inc_irq_stat(apic_irq_work_irqs); 430 431 return IRQ_HANDLED; 432 } 433 434 static const struct smp_ops xen_smp_ops __initconst = { 435 .smp_prepare_boot_cpu = xen_pv_smp_prepare_boot_cpu, 436 .smp_prepare_cpus = xen_pv_smp_prepare_cpus, 437 .smp_cpus_done = xen_smp_cpus_done, 438 439 .kick_ap_alive = xen_pv_kick_ap, 440 .cpu_die = xen_pv_cpu_die, 441 .cleanup_dead_cpu = xen_pv_cleanup_dead_cpu, 442 .poll_sync_state = xen_pv_poll_sync_state, 443 .cpu_disable = xen_pv_cpu_disable, 444 .play_dead = xen_pv_play_dead, 445 446 .stop_other_cpus = xen_pv_stop_other_cpus, 447 .smp_send_reschedule = xen_smp_send_reschedule, 448 449 .send_call_func_ipi = xen_smp_send_call_function_ipi, 450 .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi, 451 }; 452 453 void __init xen_smp_init(void) 454 { 455 smp_ops = xen_smp_ops; 456 457 /* Avoid searching for BIOS MP tables */ 458 x86_init.mpparse.find_mptable = x86_init_noop; 459 x86_init.mpparse.get_smp_config = _get_smp_config; 460 } 461