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