blob: 8c0e047d0b804450884b5e7964fdab6fc181a091 [file] [log] [blame]
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +01001/*
2 * Xen SMP support
3 *
4 * This file implements the Xen versions of smp_ops. SMP under Xen is
5 * very straightforward. Bringing a CPU up is simply a matter of
6 * loading its initial context and setting it running.
7 *
8 * IPIs are handled through the Xen event mechanism.
9 *
10 * Because virtual CPUs can be scheduled onto any real CPU, there's no
11 * useful topology information for the kernel to make use of. As a
12 * result, all CPUs are treated as if they're single-core and
13 * single-threaded.
14 */
15#include <linux/sched.h>
Andy Lutomirskif16b3da2017-11-02 00:59:12 -070016#include <linux/sched/task_stack.h>
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +010017#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>
Juergen Grossc185dde2017-07-05 16:05:20 +020023#include <linux/cpuhotplug.h>
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +010024
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
47cpumask_var_t xen_cpu_initialized_map;
48
49static DEFINE_PER_CPU(struct xen_common_irq, xen_irq_work) = { .irq = -1 };
50static DEFINE_PER_CPU(struct xen_common_irq, xen_pmu_irq) = { .irq = -1 };
51
52static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id);
53
54static 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
84asmlinkage __visible void cpu_bringup_and_idle(void)
85{
86 cpu_bringup();
87 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
88}
89
90void 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
107int 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
143static 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
159static 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
195static 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
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100226static void __init xen_pv_smp_prepare_cpus(unsigned int max_cpus)
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100227{
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
Boris Ostrovskyf31b9692017-04-26 09:42:48 -0400254 if (xen_smp_intr_init(0) || xen_smp_intr_init_pv(0))
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100255 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
273static int
274cpu_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
Andy Lutomirskif16b3da2017-11-02 00:59:12 -0700297 /*
298 * Bring up the CPU in cpu_bringup_and_idle() with the stack
299 * pointing just below where pt_regs would be if it were a normal
300 * kernel entry.
301 */
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100302 ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
303 ctxt->flags = VGCF_IN_KERNEL;
304 ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
305 ctxt->user_regs.ds = __USER_DS;
306 ctxt->user_regs.es = __USER_DS;
307 ctxt->user_regs.ss = __KERNEL_DS;
Andy Lutomirskif16b3da2017-11-02 00:59:12 -0700308 ctxt->user_regs.cs = __KERNEL_CS;
309 ctxt->user_regs.esp = (unsigned long)task_pt_regs(idle);
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100310
311 xen_copy_trap_info(ctxt->trap_ctxt);
312
313 ctxt->ldt_ents = 0;
314
315 BUG_ON((unsigned long)gdt & ~PAGE_MASK);
316
317 gdt_mfn = arbitrary_virt_to_mfn(gdt);
318 make_lowmem_page_readonly(gdt);
319 make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
320
321 ctxt->gdt_frames[0] = gdt_mfn;
322 ctxt->gdt_ents = GDT_ENTRIES;
323
Andy Lutomirskif16b3da2017-11-02 00:59:12 -0700324 /*
325 * Set SS:SP that Xen will use when entering guest kernel mode
326 * from guest user mode. Subsequent calls to load_sp0() can
327 * change this value.
328 */
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100329 ctxt->kernel_ss = __KERNEL_DS;
Andy Lutomirskif16b3da2017-11-02 00:59:12 -0700330 ctxt->kernel_sp = task_top_of_stack(idle);
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100331
332#ifdef CONFIG_X86_32
333 ctxt->event_callback_cs = __KERNEL_CS;
334 ctxt->failsafe_callback_cs = __KERNEL_CS;
335#else
336 ctxt->gs_base_kernel = per_cpu_offset(cpu);
337#endif
338 ctxt->event_callback_eip =
339 (unsigned long)xen_hypervisor_callback;
340 ctxt->failsafe_callback_eip =
341 (unsigned long)xen_failsafe_callback;
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100342 per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
343
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100344 ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_gfn(swapper_pg_dir));
345 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, xen_vcpu_nr(cpu), ctxt))
346 BUG();
347
348 kfree(ctxt);
349 return 0;
350}
351
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100352static int xen_pv_cpu_up(unsigned int cpu, struct task_struct *idle)
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100353{
354 int rc;
355
356 common_cpu_up(cpu, idle);
357
358 xen_setup_runstate_info(cpu);
359
360 /*
361 * PV VCPUs are always successfully taken down (see 'while' loop
362 * in xen_cpu_die()), so -EBUSY is an error.
363 */
364 rc = cpu_check_up_prepare(cpu);
365 if (rc)
366 return rc;
367
368 /* make sure interrupts start blocked */
369 per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
370
371 rc = cpu_initialize_context(cpu, idle);
372 if (rc)
373 return rc;
374
375 xen_pmu_init(cpu);
376
377 rc = HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL);
378 BUG_ON(rc);
379
380 while (cpu_report_state(cpu) != CPU_ONLINE)
381 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
382
383 return 0;
384}
385
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100386#ifdef CONFIG_HOTPLUG_CPU
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100387static int xen_pv_cpu_disable(void)
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100388{
389 unsigned int cpu = smp_processor_id();
390 if (cpu == 0)
391 return -EBUSY;
392
393 cpu_disable_common();
394
395 load_cr3(swapper_pg_dir);
396 return 0;
397}
398
399static void xen_pv_cpu_die(unsigned int cpu)
400{
401 while (HYPERVISOR_vcpu_op(VCPUOP_is_up,
402 xen_vcpu_nr(cpu), NULL)) {
403 __set_current_state(TASK_UNINTERRUPTIBLE);
404 schedule_timeout(HZ/10);
405 }
406
407 if (common_cpu_die(cpu) == 0) {
408 xen_smp_intr_free(cpu);
409 xen_uninit_lock_cpu(cpu);
410 xen_teardown_timer(cpu);
411 xen_pmu_finish(cpu);
412 }
413}
414
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100415static void xen_pv_play_dead(void) /* used only with HOTPLUG_CPU */
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100416{
417 play_dead_common();
418 HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(smp_processor_id()), NULL);
419 cpu_bringup();
420 /*
421 * commit 4b0c0f294 (tick: Cleanup NOHZ per cpu data on cpu down)
422 * clears certain data that the cpu_idle loop (which called us
423 * and that we return from) expects. The only way to get that
424 * data back is to call:
425 */
426 tick_nohz_idle_enter();
427
Juergen Grossc185dde2017-07-05 16:05:20 +0200428 cpuhp_online_idle(CPUHP_AP_ONLINE_IDLE);
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100429}
430
431#else /* !CONFIG_HOTPLUG_CPU */
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100432static int xen_pv_cpu_disable(void)
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100433{
434 return -ENOSYS;
435}
436
437static void xen_pv_cpu_die(unsigned int cpu)
438{
439 BUG();
440}
441
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100442static void xen_pv_play_dead(void)
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100443{
444 BUG();
445}
446
447#endif
448static void stop_self(void *v)
449{
450 int cpu = smp_processor_id();
451
452 /* make sure we're not pinning something down */
453 load_cr3(swapper_pg_dir);
454 /* should set up a minimal gdt */
455
456 set_cpu_online(cpu, false);
457
458 HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL);
459 BUG();
460}
461
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100462static void xen_pv_stop_other_cpus(int wait)
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100463{
464 smp_call_function(stop_self, NULL, wait);
465}
466
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100467static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id)
468{
469 irq_enter();
470 irq_work_run();
471 inc_irq_stat(apic_irq_work_irqs);
472 irq_exit();
473
474 return IRQ_HANDLED;
475}
476
477static const struct smp_ops xen_smp_ops __initconst = {
478 .smp_prepare_boot_cpu = xen_pv_smp_prepare_boot_cpu,
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100479 .smp_prepare_cpus = xen_pv_smp_prepare_cpus,
Ankur Aroraae039002017-06-02 17:06:02 -0700480 .smp_cpus_done = xen_smp_cpus_done,
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100481
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100482 .cpu_up = xen_pv_cpu_up,
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100483 .cpu_die = xen_pv_cpu_die,
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100484 .cpu_disable = xen_pv_cpu_disable,
485 .play_dead = xen_pv_play_dead,
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100486
Vitaly Kuznetsov8cb6de32017-03-14 18:35:56 +0100487 .stop_other_cpus = xen_pv_stop_other_cpus,
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +0100488 .smp_send_reschedule = xen_smp_send_reschedule,
489
490 .send_call_func_ipi = xen_smp_send_call_function_ipi,
491 .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
492};
493
494void __init xen_smp_init(void)
495{
496 smp_ops = xen_smp_ops;
497 xen_fill_possible_map();
498}