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