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