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.
8 * IPIs are handled through the Xen event mechanism.
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
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 #include <linux/cpuhotplug.h>
24 #include <asm/paravirt.h>
26 #include <asm/pgtable.h>
29 #include <xen/interface/xen.h>
30 #include <xen/interface/vcpu.h>
31 #include <xen/interface/xenpmu.h>
33 #include <asm/xen/interface.h>
34 #include <asm/xen/hypercall.h>
38 #include <xen/events.h>
40 #include <xen/hvc-console.h>
46 cpumask_var_t xen_cpu_initialized_map
;
48 static DEFINE_PER_CPU(struct xen_common_irq
, xen_irq_work
) = { .irq
= -1 };
49 static DEFINE_PER_CPU(struct xen_common_irq
, xen_pmu_irq
) = { .irq
= -1 };
51 static irqreturn_t
xen_irq_work_interrupt(int irq
, void *dev_id
);
53 static void cpu_bringup(void)
58 touch_softlockup_watchdog();
61 /* PVH runs in ring 0 and allows us to do native syscalls. Yay! */
62 if (!xen_feature(XENFEAT_supervisor_mode_kernel
)) {
63 xen_enable_sysenter();
66 cpu
= smp_processor_id();
67 smp_store_cpu_info(cpu
);
68 cpu_data(cpu
).x86_max_cores
= 1;
69 set_cpu_sibling_map(cpu
);
71 xen_setup_cpu_clockevents();
73 notify_cpu_starting(cpu
);
75 set_cpu_online(cpu
, true);
77 cpu_set_state_online(cpu
); /* Implies full memory barrier. */
79 /* We can take interrupts now: we're officially "up". */
83 asmlinkage __visible
void cpu_bringup_and_idle(void)
86 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE
);
89 void xen_smp_intr_free_pv(unsigned int cpu
)
91 if (per_cpu(xen_irq_work
, cpu
).irq
>= 0) {
92 unbind_from_irqhandler(per_cpu(xen_irq_work
, cpu
).irq
, NULL
);
93 per_cpu(xen_irq_work
, cpu
).irq
= -1;
94 kfree(per_cpu(xen_irq_work
, cpu
).name
);
95 per_cpu(xen_irq_work
, cpu
).name
= NULL
;
98 if (per_cpu(xen_pmu_irq
, cpu
).irq
>= 0) {
99 unbind_from_irqhandler(per_cpu(xen_pmu_irq
, cpu
).irq
, NULL
);
100 per_cpu(xen_pmu_irq
, cpu
).irq
= -1;
101 kfree(per_cpu(xen_pmu_irq
, cpu
).name
);
102 per_cpu(xen_pmu_irq
, cpu
).name
= NULL
;
106 int xen_smp_intr_init_pv(unsigned int cpu
)
109 char *callfunc_name
, *pmu_name
;
111 callfunc_name
= kasprintf(GFP_KERNEL
, "irqwork%d", cpu
);
112 rc
= bind_ipi_to_irqhandler(XEN_IRQ_WORK_VECTOR
,
114 xen_irq_work_interrupt
,
115 IRQF_PERCPU
|IRQF_NOBALANCING
,
120 per_cpu(xen_irq_work
, cpu
).irq
= rc
;
121 per_cpu(xen_irq_work
, cpu
).name
= callfunc_name
;
123 if (is_xen_pmu(cpu
)) {
124 pmu_name
= kasprintf(GFP_KERNEL
, "pmu%d", cpu
);
125 rc
= bind_virq_to_irqhandler(VIRQ_XENPMU
, cpu
,
127 IRQF_PERCPU
|IRQF_NOBALANCING
,
131 per_cpu(xen_pmu_irq
, cpu
).irq
= rc
;
132 per_cpu(xen_pmu_irq
, cpu
).name
= pmu_name
;
138 xen_smp_intr_free_pv(cpu
);
142 static void __init
xen_fill_possible_map(void)
146 if (xen_initial_domain())
149 for (i
= 0; i
< nr_cpu_ids
; i
++) {
150 rc
= HYPERVISOR_vcpu_op(VCPUOP_is_up
, i
, NULL
);
153 set_cpu_possible(i
, true);
158 static void __init
xen_filter_cpu_maps(void)
161 unsigned int subtract
= 0;
163 if (!xen_initial_domain())
168 for (i
= 0; i
< nr_cpu_ids
; i
++) {
169 rc
= HYPERVISOR_vcpu_op(VCPUOP_is_up
, i
, NULL
);
172 set_cpu_possible(i
, true);
174 set_cpu_possible(i
, false);
175 set_cpu_present(i
, false);
179 #ifdef CONFIG_HOTPLUG_CPU
180 /* This is akin to using 'nr_cpus' on the Linux command line.
181 * Which is OK as when we use 'dom0_max_vcpus=X' we can only
182 * have up to X, while nr_cpu_ids is greater than X. This
183 * normally is not a problem, except when CPU hotplugging
184 * is involved and then there might be more than X CPUs
185 * in the guest - which will not work as there is no
186 * hypercall to expand the max number of VCPUs an already
187 * running guest has. So cap it up to X. */
189 nr_cpu_ids
= nr_cpu_ids
- subtract
;
194 static void __init
xen_pv_smp_prepare_boot_cpu(void)
196 BUG_ON(smp_processor_id() != 0);
197 native_smp_prepare_boot_cpu();
199 if (!xen_feature(XENFEAT_writable_page_tables
))
200 /* We've switched to the "real" per-cpu gdt, so make
201 * sure the old memory can be recycled. */
202 make_lowmem_page_readwrite(xen_initial_gdt
);
206 * Xen starts us with XEN_FLAT_RING1_DS, but linux code
209 loadsegment(ds
, __USER_DS
);
210 loadsegment(es
, __USER_DS
);
213 xen_filter_cpu_maps();
214 xen_setup_vcpu_info_placement();
217 * The alternative logic (which patches the unlock/lock) runs before
218 * the smp bootup up code is activated. Hence we need to set this up
219 * the core kernel is being patched. Otherwise we will have only
220 * modules patched but not core code.
222 xen_init_spinlocks();
225 static void __init
xen_pv_smp_prepare_cpus(unsigned int max_cpus
)
230 if (skip_ioapic_setup
) {
231 char *m
= (max_cpus
== 0) ?
232 "The nosmp parameter is incompatible with Xen; " \
233 "use Xen dom0_max_vcpus=1 parameter" :
234 "The noapic parameter is incompatible with Xen";
239 xen_init_lock_cpu(0);
241 smp_store_boot_cpu_info();
242 cpu_data(0).x86_max_cores
= 1;
244 for_each_possible_cpu(i
) {
245 zalloc_cpumask_var(&per_cpu(cpu_sibling_map
, i
), GFP_KERNEL
);
246 zalloc_cpumask_var(&per_cpu(cpu_core_map
, i
), GFP_KERNEL
);
247 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map
, i
), GFP_KERNEL
);
249 set_cpu_sibling_map(0);
253 if (xen_smp_intr_init(0) || xen_smp_intr_init_pv(0))
256 if (!alloc_cpumask_var(&xen_cpu_initialized_map
, GFP_KERNEL
))
257 panic("could not allocate xen_cpu_initialized_map\n");
259 cpumask_copy(xen_cpu_initialized_map
, cpumask_of(0));
261 /* Restrict the possible_map according to max_cpus. */
262 while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus
)) {
263 for (cpu
= nr_cpu_ids
- 1; !cpu_possible(cpu
); cpu
--)
265 set_cpu_possible(cpu
, false);
268 for_each_possible_cpu(cpu
)
269 set_cpu_present(cpu
, true);
273 cpu_initialize_context(unsigned int cpu
, struct task_struct
*idle
)
275 struct vcpu_guest_context
*ctxt
;
276 struct desc_struct
*gdt
;
277 unsigned long gdt_mfn
;
279 /* used to tell cpu_init() that it can proceed with initialization */
280 cpumask_set_cpu(cpu
, cpu_callout_mask
);
281 if (cpumask_test_and_set_cpu(cpu
, xen_cpu_initialized_map
))
284 ctxt
= kzalloc(sizeof(*ctxt
), GFP_KERNEL
);
288 gdt
= get_cpu_gdt_rw(cpu
);
291 ctxt
->user_regs
.fs
= __KERNEL_PERCPU
;
292 ctxt
->user_regs
.gs
= __KERNEL_STACK_CANARY
;
294 memset(&ctxt
->fpu_ctxt
, 0, sizeof(ctxt
->fpu_ctxt
));
296 ctxt
->user_regs
.eip
= (unsigned long)cpu_bringup_and_idle
;
297 ctxt
->flags
= VGCF_IN_KERNEL
;
298 ctxt
->user_regs
.eflags
= 0x1000; /* IOPL_RING1 */
299 ctxt
->user_regs
.ds
= __USER_DS
;
300 ctxt
->user_regs
.es
= __USER_DS
;
301 ctxt
->user_regs
.ss
= __KERNEL_DS
;
303 xen_copy_trap_info(ctxt
->trap_ctxt
);
307 BUG_ON((unsigned long)gdt
& ~PAGE_MASK
);
309 gdt_mfn
= arbitrary_virt_to_mfn(gdt
);
310 make_lowmem_page_readonly(gdt
);
311 make_lowmem_page_readonly(mfn_to_virt(gdt_mfn
));
313 ctxt
->gdt_frames
[0] = gdt_mfn
;
314 ctxt
->gdt_ents
= GDT_ENTRIES
;
316 ctxt
->kernel_ss
= __KERNEL_DS
;
317 ctxt
->kernel_sp
= idle
->thread
.sp0
;
320 ctxt
->event_callback_cs
= __KERNEL_CS
;
321 ctxt
->failsafe_callback_cs
= __KERNEL_CS
;
323 ctxt
->gs_base_kernel
= per_cpu_offset(cpu
);
325 ctxt
->event_callback_eip
=
326 (unsigned long)xen_hypervisor_callback
;
327 ctxt
->failsafe_callback_eip
=
328 (unsigned long)xen_failsafe_callback
;
329 ctxt
->user_regs
.cs
= __KERNEL_CS
;
330 per_cpu(xen_cr3
, cpu
) = __pa(swapper_pg_dir
);
332 ctxt
->user_regs
.esp
= idle
->thread
.sp0
- sizeof(struct pt_regs
);
333 ctxt
->ctrlreg
[3] = xen_pfn_to_cr3(virt_to_gfn(swapper_pg_dir
));
334 if (HYPERVISOR_vcpu_op(VCPUOP_initialise
, xen_vcpu_nr(cpu
), ctxt
))
341 static int xen_pv_cpu_up(unsigned int cpu
, struct task_struct
*idle
)
345 common_cpu_up(cpu
, idle
);
347 xen_setup_runstate_info(cpu
);
350 * PV VCPUs are always successfully taken down (see 'while' loop
351 * in xen_cpu_die()), so -EBUSY is an error.
353 rc
= cpu_check_up_prepare(cpu
);
357 /* make sure interrupts start blocked */
358 per_cpu(xen_vcpu
, cpu
)->evtchn_upcall_mask
= 1;
360 rc
= cpu_initialize_context(cpu
, idle
);
366 rc
= HYPERVISOR_vcpu_op(VCPUOP_up
, xen_vcpu_nr(cpu
), NULL
);
369 while (cpu_report_state(cpu
) != CPU_ONLINE
)
370 HYPERVISOR_sched_op(SCHEDOP_yield
, NULL
);
375 #ifdef CONFIG_HOTPLUG_CPU
376 static int xen_pv_cpu_disable(void)
378 unsigned int cpu
= smp_processor_id();
382 cpu_disable_common();
384 load_cr3(swapper_pg_dir
);
388 static void xen_pv_cpu_die(unsigned int cpu
)
390 while (HYPERVISOR_vcpu_op(VCPUOP_is_up
,
391 xen_vcpu_nr(cpu
), NULL
)) {
392 __set_current_state(TASK_UNINTERRUPTIBLE
);
393 schedule_timeout(HZ
/10);
396 if (common_cpu_die(cpu
) == 0) {
397 xen_smp_intr_free(cpu
);
398 xen_uninit_lock_cpu(cpu
);
399 xen_teardown_timer(cpu
);
404 static void xen_pv_play_dead(void) /* used only with HOTPLUG_CPU */
407 HYPERVISOR_vcpu_op(VCPUOP_down
, xen_vcpu_nr(smp_processor_id()), NULL
);
410 * commit 4b0c0f294 (tick: Cleanup NOHZ per cpu data on cpu down)
411 * clears certain data that the cpu_idle loop (which called us
412 * and that we return from) expects. The only way to get that
413 * data back is to call:
415 tick_nohz_idle_enter();
417 cpuhp_online_idle(CPUHP_AP_ONLINE_IDLE
);
420 #else /* !CONFIG_HOTPLUG_CPU */
421 static int xen_pv_cpu_disable(void)
426 static void xen_pv_cpu_die(unsigned int cpu
)
431 static void xen_pv_play_dead(void)
437 static void stop_self(void *v
)
439 int cpu
= smp_processor_id();
441 /* make sure we're not pinning something down */
442 load_cr3(swapper_pg_dir
);
443 /* should set up a minimal gdt */
445 set_cpu_online(cpu
, false);
447 HYPERVISOR_vcpu_op(VCPUOP_down
, xen_vcpu_nr(cpu
), NULL
);
451 static void xen_pv_stop_other_cpus(int wait
)
453 smp_call_function(stop_self
, NULL
, wait
);
456 static irqreturn_t
xen_irq_work_interrupt(int irq
, void *dev_id
)
460 inc_irq_stat(apic_irq_work_irqs
);
466 static const struct smp_ops xen_smp_ops __initconst
= {
467 .smp_prepare_boot_cpu
= xen_pv_smp_prepare_boot_cpu
,
468 .smp_prepare_cpus
= xen_pv_smp_prepare_cpus
,
469 .smp_cpus_done
= xen_smp_cpus_done
,
471 .cpu_up
= xen_pv_cpu_up
,
472 .cpu_die
= xen_pv_cpu_die
,
473 .cpu_disable
= xen_pv_cpu_disable
,
474 .play_dead
= xen_pv_play_dead
,
476 .stop_other_cpus
= xen_pv_stop_other_cpus
,
477 .smp_send_reschedule
= xen_smp_send_reschedule
,
479 .send_call_func_ipi
= xen_smp_send_call_function_ipi
,
480 .send_call_func_single_ipi
= xen_smp_send_call_function_single_ipi
,
483 void __init
xen_smp_init(void)
485 smp_ops
= xen_smp_ops
;
486 xen_fill_possible_map();