xref: /linux/arch/x86/xen/smp_pv.c (revision b2d0f5d5dc53532e6f07bc546a476a55ebdfe0f3)
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/err.h>
18 #include <linux/slab.h>
19 #include <linux/smp.h>
20 #include <linux/irq_work.h>
21 #include <linux/tick.h>
22 #include <linux/nmi.h>
23 #include <linux/cpuhotplug.h>
24 
25 #include <asm/paravirt.h>
26 #include <asm/desc.h>
27 #include <asm/pgtable.h>
28 #include <asm/cpu.h>
29 
30 #include <xen/interface/xen.h>
31 #include <xen/interface/vcpu.h>
32 #include <xen/interface/xenpmu.h>
33 
34 #include <asm/xen/interface.h>
35 #include <asm/xen/hypercall.h>
36 
37 #include <xen/xen.h>
38 #include <xen/page.h>
39 #include <xen/events.h>
40 
41 #include <xen/hvc-console.h>
42 #include "xen-ops.h"
43 #include "mmu.h"
44 #include "smp.h"
45 #include "pmu.h"
46 
47 cpumask_var_t xen_cpu_initialized_map;
48 
49 static DEFINE_PER_CPU(struct xen_common_irq, xen_irq_work) = { .irq = -1 };
50 static DEFINE_PER_CPU(struct xen_common_irq, xen_pmu_irq) = { .irq = -1 };
51 
52 static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id);
53 
54 static void cpu_bringup(void)
55 {
56 	int cpu;
57 
58 	cpu_init();
59 	touch_softlockup_watchdog();
60 	preempt_disable();
61 
62 	/* PVH runs in ring 0 and allows us to do native syscalls. Yay! */
63 	if (!xen_feature(XENFEAT_supervisor_mode_kernel)) {
64 		xen_enable_sysenter();
65 		xen_enable_syscall();
66 	}
67 	cpu = smp_processor_id();
68 	smp_store_cpu_info(cpu);
69 	cpu_data(cpu).x86_max_cores = 1;
70 	set_cpu_sibling_map(cpu);
71 
72 	xen_setup_cpu_clockevents();
73 
74 	notify_cpu_starting(cpu);
75 
76 	set_cpu_online(cpu, true);
77 
78 	cpu_set_state_online(cpu);  /* Implies full memory barrier. */
79 
80 	/* We can take interrupts now: we're officially "up". */
81 	local_irq_enable();
82 }
83 
84 asmlinkage __visible void cpu_bringup_and_idle(void)
85 {
86 	cpu_bringup();
87 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
88 }
89 
90 void xen_smp_intr_free_pv(unsigned int cpu)
91 {
92 	if (per_cpu(xen_irq_work, cpu).irq >= 0) {
93 		unbind_from_irqhandler(per_cpu(xen_irq_work, cpu).irq, NULL);
94 		per_cpu(xen_irq_work, cpu).irq = -1;
95 		kfree(per_cpu(xen_irq_work, cpu).name);
96 		per_cpu(xen_irq_work, cpu).name = NULL;
97 	}
98 
99 	if (per_cpu(xen_pmu_irq, cpu).irq >= 0) {
100 		unbind_from_irqhandler(per_cpu(xen_pmu_irq, cpu).irq, NULL);
101 		per_cpu(xen_pmu_irq, cpu).irq = -1;
102 		kfree(per_cpu(xen_pmu_irq, cpu).name);
103 		per_cpu(xen_pmu_irq, cpu).name = NULL;
104 	}
105 }
106 
107 int xen_smp_intr_init_pv(unsigned int cpu)
108 {
109 	int rc;
110 	char *callfunc_name, *pmu_name;
111 
112 	callfunc_name = kasprintf(GFP_KERNEL, "irqwork%d", cpu);
113 	rc = bind_ipi_to_irqhandler(XEN_IRQ_WORK_VECTOR,
114 				    cpu,
115 				    xen_irq_work_interrupt,
116 				    IRQF_PERCPU|IRQF_NOBALANCING,
117 				    callfunc_name,
118 				    NULL);
119 	if (rc < 0)
120 		goto fail;
121 	per_cpu(xen_irq_work, cpu).irq = rc;
122 	per_cpu(xen_irq_work, cpu).name = callfunc_name;
123 
124 	if (is_xen_pmu(cpu)) {
125 		pmu_name = kasprintf(GFP_KERNEL, "pmu%d", cpu);
126 		rc = bind_virq_to_irqhandler(VIRQ_XENPMU, cpu,
127 					     xen_pmu_irq_handler,
128 					     IRQF_PERCPU|IRQF_NOBALANCING,
129 					     pmu_name, NULL);
130 		if (rc < 0)
131 			goto fail;
132 		per_cpu(xen_pmu_irq, cpu).irq = rc;
133 		per_cpu(xen_pmu_irq, cpu).name = pmu_name;
134 	}
135 
136 	return 0;
137 
138  fail:
139 	xen_smp_intr_free_pv(cpu);
140 	return rc;
141 }
142 
143 static void __init xen_fill_possible_map(void)
144 {
145 	int i, rc;
146 
147 	if (xen_initial_domain())
148 		return;
149 
150 	for (i = 0; i < nr_cpu_ids; i++) {
151 		rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
152 		if (rc >= 0) {
153 			num_processors++;
154 			set_cpu_possible(i, true);
155 		}
156 	}
157 }
158 
159 static void __init xen_filter_cpu_maps(void)
160 {
161 	int i, rc;
162 	unsigned int subtract = 0;
163 
164 	if (!xen_initial_domain())
165 		return;
166 
167 	num_processors = 0;
168 	disabled_cpus = 0;
169 	for (i = 0; i < nr_cpu_ids; i++) {
170 		rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
171 		if (rc >= 0) {
172 			num_processors++;
173 			set_cpu_possible(i, true);
174 		} else {
175 			set_cpu_possible(i, false);
176 			set_cpu_present(i, false);
177 			subtract++;
178 		}
179 	}
180 #ifdef CONFIG_HOTPLUG_CPU
181 	/* This is akin to using 'nr_cpus' on the Linux command line.
182 	 * Which is OK as when we use 'dom0_max_vcpus=X' we can only
183 	 * have up to X, while nr_cpu_ids is greater than X. This
184 	 * normally is not a problem, except when CPU hotplugging
185 	 * is involved and then there might be more than X CPUs
186 	 * in the guest - which will not work as there is no
187 	 * hypercall to expand the max number of VCPUs an already
188 	 * running guest has. So cap it up to X. */
189 	if (subtract)
190 		nr_cpu_ids = nr_cpu_ids - subtract;
191 #endif
192 
193 }
194 
195 static void __init xen_pv_smp_prepare_boot_cpu(void)
196 {
197 	BUG_ON(smp_processor_id() != 0);
198 	native_smp_prepare_boot_cpu();
199 
200 	if (!xen_feature(XENFEAT_writable_page_tables))
201 		/* We've switched to the "real" per-cpu gdt, so make
202 		 * sure the old memory can be recycled. */
203 		make_lowmem_page_readwrite(xen_initial_gdt);
204 
205 #ifdef CONFIG_X86_32
206 	/*
207 	 * Xen starts us with XEN_FLAT_RING1_DS, but linux code
208 	 * expects __USER_DS
209 	 */
210 	loadsegment(ds, __USER_DS);
211 	loadsegment(es, __USER_DS);
212 #endif
213 
214 	xen_filter_cpu_maps();
215 	xen_setup_vcpu_info_placement();
216 
217 	/*
218 	 * The alternative logic (which patches the unlock/lock) runs before
219 	 * the smp bootup up code is activated. Hence we need to set this up
220 	 * the core kernel is being patched. Otherwise we will have only
221 	 * modules patched but not core code.
222 	 */
223 	xen_init_spinlocks();
224 }
225 
226 static void __init xen_pv_smp_prepare_cpus(unsigned int max_cpus)
227 {
228 	unsigned cpu;
229 	unsigned int i;
230 
231 	if (skip_ioapic_setup) {
232 		char *m = (max_cpus == 0) ?
233 			"The nosmp parameter is incompatible with Xen; " \
234 			"use Xen dom0_max_vcpus=1 parameter" :
235 			"The noapic parameter is incompatible with Xen";
236 
237 		xen_raw_printk(m);
238 		panic(m);
239 	}
240 	xen_init_lock_cpu(0);
241 
242 	smp_store_boot_cpu_info();
243 	cpu_data(0).x86_max_cores = 1;
244 
245 	for_each_possible_cpu(i) {
246 		zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
247 		zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
248 		zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
249 	}
250 	set_cpu_sibling_map(0);
251 
252 	xen_pmu_init(0);
253 
254 	if (xen_smp_intr_init(0) || xen_smp_intr_init_pv(0))
255 		BUG();
256 
257 	if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
258 		panic("could not allocate xen_cpu_initialized_map\n");
259 
260 	cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
261 
262 	/* Restrict the possible_map according to max_cpus. */
263 	while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
264 		for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
265 			continue;
266 		set_cpu_possible(cpu, false);
267 	}
268 
269 	for_each_possible_cpu(cpu)
270 		set_cpu_present(cpu, true);
271 }
272 
273 static int
274 cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
275 {
276 	struct vcpu_guest_context *ctxt;
277 	struct desc_struct *gdt;
278 	unsigned long gdt_mfn;
279 
280 	/* used to tell cpu_init() that it can proceed with initialization */
281 	cpumask_set_cpu(cpu, cpu_callout_mask);
282 	if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
283 		return 0;
284 
285 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
286 	if (ctxt == NULL)
287 		return -ENOMEM;
288 
289 	gdt = get_cpu_gdt_rw(cpu);
290 
291 #ifdef CONFIG_X86_32
292 	ctxt->user_regs.fs = __KERNEL_PERCPU;
293 	ctxt->user_regs.gs = __KERNEL_STACK_CANARY;
294 #endif
295 	memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
296 
297 	ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
298 	ctxt->flags = VGCF_IN_KERNEL;
299 	ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
300 	ctxt->user_regs.ds = __USER_DS;
301 	ctxt->user_regs.es = __USER_DS;
302 	ctxt->user_regs.ss = __KERNEL_DS;
303 
304 	xen_copy_trap_info(ctxt->trap_ctxt);
305 
306 	ctxt->ldt_ents = 0;
307 
308 	BUG_ON((unsigned long)gdt & ~PAGE_MASK);
309 
310 	gdt_mfn = arbitrary_virt_to_mfn(gdt);
311 	make_lowmem_page_readonly(gdt);
312 	make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
313 
314 	ctxt->gdt_frames[0] = gdt_mfn;
315 	ctxt->gdt_ents      = GDT_ENTRIES;
316 
317 	ctxt->kernel_ss = __KERNEL_DS;
318 	ctxt->kernel_sp = idle->thread.sp0;
319 
320 #ifdef CONFIG_X86_32
321 	ctxt->event_callback_cs     = __KERNEL_CS;
322 	ctxt->failsafe_callback_cs  = __KERNEL_CS;
323 #else
324 	ctxt->gs_base_kernel = per_cpu_offset(cpu);
325 #endif
326 	ctxt->event_callback_eip    =
327 		(unsigned long)xen_hypervisor_callback;
328 	ctxt->failsafe_callback_eip =
329 		(unsigned long)xen_failsafe_callback;
330 	ctxt->user_regs.cs = __KERNEL_CS;
331 	per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
332 
333 	ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
334 	ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_gfn(swapper_pg_dir));
335 	if (HYPERVISOR_vcpu_op(VCPUOP_initialise, xen_vcpu_nr(cpu), ctxt))
336 		BUG();
337 
338 	kfree(ctxt);
339 	return 0;
340 }
341 
342 static int xen_pv_cpu_up(unsigned int cpu, struct task_struct *idle)
343 {
344 	int rc;
345 
346 	common_cpu_up(cpu, idle);
347 
348 	xen_setup_runstate_info(cpu);
349 
350 	/*
351 	 * PV VCPUs are always successfully taken down (see 'while' loop
352 	 * in xen_cpu_die()), so -EBUSY is an error.
353 	 */
354 	rc = cpu_check_up_prepare(cpu);
355 	if (rc)
356 		return rc;
357 
358 	/* make sure interrupts start blocked */
359 	per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
360 
361 	rc = cpu_initialize_context(cpu, idle);
362 	if (rc)
363 		return rc;
364 
365 	xen_pmu_init(cpu);
366 
367 	rc = HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL);
368 	BUG_ON(rc);
369 
370 	while (cpu_report_state(cpu) != CPU_ONLINE)
371 		HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
372 
373 	return 0;
374 }
375 
376 #ifdef CONFIG_HOTPLUG_CPU
377 static int xen_pv_cpu_disable(void)
378 {
379 	unsigned int cpu = smp_processor_id();
380 	if (cpu == 0)
381 		return -EBUSY;
382 
383 	cpu_disable_common();
384 
385 	load_cr3(swapper_pg_dir);
386 	return 0;
387 }
388 
389 static void xen_pv_cpu_die(unsigned int cpu)
390 {
391 	while (HYPERVISOR_vcpu_op(VCPUOP_is_up,
392 				  xen_vcpu_nr(cpu), NULL)) {
393 		__set_current_state(TASK_UNINTERRUPTIBLE);
394 		schedule_timeout(HZ/10);
395 	}
396 
397 	if (common_cpu_die(cpu) == 0) {
398 		xen_smp_intr_free(cpu);
399 		xen_uninit_lock_cpu(cpu);
400 		xen_teardown_timer(cpu);
401 		xen_pmu_finish(cpu);
402 	}
403 }
404 
405 static void xen_pv_play_dead(void) /* used only with HOTPLUG_CPU */
406 {
407 	play_dead_common();
408 	HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(smp_processor_id()), NULL);
409 	cpu_bringup();
410 	/*
411 	 * commit 4b0c0f294 (tick: Cleanup NOHZ per cpu data on cpu down)
412 	 * clears certain data that the cpu_idle loop (which called us
413 	 * and that we return from) expects. The only way to get that
414 	 * data back is to call:
415 	 */
416 	tick_nohz_idle_enter();
417 
418 	cpuhp_online_idle(CPUHP_AP_ONLINE_IDLE);
419 }
420 
421 #else /* !CONFIG_HOTPLUG_CPU */
422 static int xen_pv_cpu_disable(void)
423 {
424 	return -ENOSYS;
425 }
426 
427 static void xen_pv_cpu_die(unsigned int cpu)
428 {
429 	BUG();
430 }
431 
432 static void xen_pv_play_dead(void)
433 {
434 	BUG();
435 }
436 
437 #endif
438 static void stop_self(void *v)
439 {
440 	int cpu = smp_processor_id();
441 
442 	/* make sure we're not pinning something down */
443 	load_cr3(swapper_pg_dir);
444 	/* should set up a minimal gdt */
445 
446 	set_cpu_online(cpu, false);
447 
448 	HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL);
449 	BUG();
450 }
451 
452 static void xen_pv_stop_other_cpus(int wait)
453 {
454 	smp_call_function(stop_self, NULL, wait);
455 }
456 
457 static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id)
458 {
459 	irq_enter();
460 	irq_work_run();
461 	inc_irq_stat(apic_irq_work_irqs);
462 	irq_exit();
463 
464 	return IRQ_HANDLED;
465 }
466 
467 static const struct smp_ops xen_smp_ops __initconst = {
468 	.smp_prepare_boot_cpu = xen_pv_smp_prepare_boot_cpu,
469 	.smp_prepare_cpus = xen_pv_smp_prepare_cpus,
470 	.smp_cpus_done = xen_smp_cpus_done,
471 
472 	.cpu_up = xen_pv_cpu_up,
473 	.cpu_die = xen_pv_cpu_die,
474 	.cpu_disable = xen_pv_cpu_disable,
475 	.play_dead = xen_pv_play_dead,
476 
477 	.stop_other_cpus = xen_pv_stop_other_cpus,
478 	.smp_send_reschedule = xen_smp_send_reschedule,
479 
480 	.send_call_func_ipi = xen_smp_send_call_function_ipi,
481 	.send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
482 };
483 
484 void __init xen_smp_init(void)
485 {
486 	smp_ops = xen_smp_ops;
487 	xen_fill_possible_map();
488 }
489