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>
20 #include <asm/paravirt.h>
22 #include <asm/pgtable.h>
25 #include <xen/interface/xen.h>
26 #include <xen/interface/vcpu.h>
28 #include <asm/xen/interface.h>
29 #include <asm/xen/hypercall.h>
32 #include <xen/events.h>
37 cpumask_var_t xen_cpu_initialized_map
;
39 static DEFINE_PER_CPU(int, xen_resched_irq
);
40 static DEFINE_PER_CPU(int, xen_callfunc_irq
);
41 static DEFINE_PER_CPU(int, xen_callfuncsingle_irq
);
42 static DEFINE_PER_CPU(int, xen_debug_irq
) = -1;
44 static irqreturn_t
xen_call_function_interrupt(int irq
, void *dev_id
);
45 static irqreturn_t
xen_call_function_single_interrupt(int irq
, void *dev_id
);
48 * Reschedule call back. Nothing to do,
49 * all the work is done automatically when
50 * we return from the interrupt.
52 static irqreturn_t
xen_reschedule_interrupt(int irq
, void *dev_id
)
54 inc_irq_stat(irq_resched_count
);
59 static __cpuinit
void cpu_bringup(void)
61 int cpu
= smp_processor_id();
64 touch_softlockup_watchdog();
67 xen_enable_sysenter();
70 cpu
= smp_processor_id();
71 smp_store_cpu_info(cpu
);
72 cpu_data(cpu
).x86_max_cores
= 1;
73 set_cpu_sibling_map(cpu
);
75 xen_setup_cpu_clockevents();
77 set_cpu_online(cpu
, true);
78 percpu_write(cpu_state
, CPU_ONLINE
);
81 /* We can take interrupts now: we're officially "up". */
84 wmb(); /* make sure everything is out */
87 static __cpuinit
void cpu_bringup_and_idle(void)
93 static int xen_smp_intr_init(unsigned int cpu
)
96 const char *resched_name
, *callfunc_name
, *debug_name
;
98 resched_name
= kasprintf(GFP_KERNEL
, "resched%d", cpu
);
99 rc
= bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR
,
101 xen_reschedule_interrupt
,
102 IRQF_DISABLED
|IRQF_PERCPU
|IRQF_NOBALANCING
,
107 per_cpu(xen_resched_irq
, cpu
) = rc
;
109 callfunc_name
= kasprintf(GFP_KERNEL
, "callfunc%d", cpu
);
110 rc
= bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR
,
112 xen_call_function_interrupt
,
113 IRQF_DISABLED
|IRQF_PERCPU
|IRQF_NOBALANCING
,
118 per_cpu(xen_callfunc_irq
, cpu
) = rc
;
120 debug_name
= kasprintf(GFP_KERNEL
, "debug%d", cpu
);
121 rc
= bind_virq_to_irqhandler(VIRQ_DEBUG
, cpu
, xen_debug_interrupt
,
122 IRQF_DISABLED
| IRQF_PERCPU
| IRQF_NOBALANCING
,
126 per_cpu(xen_debug_irq
, cpu
) = rc
;
128 callfunc_name
= kasprintf(GFP_KERNEL
, "callfuncsingle%d", cpu
);
129 rc
= bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR
,
131 xen_call_function_single_interrupt
,
132 IRQF_DISABLED
|IRQF_PERCPU
|IRQF_NOBALANCING
,
137 per_cpu(xen_callfuncsingle_irq
, cpu
) = rc
;
142 if (per_cpu(xen_resched_irq
, cpu
) >= 0)
143 unbind_from_irqhandler(per_cpu(xen_resched_irq
, cpu
), NULL
);
144 if (per_cpu(xen_callfunc_irq
, cpu
) >= 0)
145 unbind_from_irqhandler(per_cpu(xen_callfunc_irq
, cpu
), NULL
);
146 if (per_cpu(xen_debug_irq
, cpu
) >= 0)
147 unbind_from_irqhandler(per_cpu(xen_debug_irq
, cpu
), NULL
);
148 if (per_cpu(xen_callfuncsingle_irq
, cpu
) >= 0)
149 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq
, cpu
),
155 static void __init
xen_fill_possible_map(void)
159 for (i
= 0; i
< nr_cpu_ids
; i
++) {
160 rc
= HYPERVISOR_vcpu_op(VCPUOP_is_up
, i
, NULL
);
163 set_cpu_possible(i
, true);
168 static void __init
xen_smp_prepare_boot_cpu(void)
170 BUG_ON(smp_processor_id() != 0);
171 native_smp_prepare_boot_cpu();
173 /* We've switched to the "real" per-cpu gdt, so make sure the
174 old memory can be recycled */
175 make_lowmem_page_readwrite(xen_initial_gdt
);
177 xen_setup_vcpu_info_placement();
180 static void __init
xen_smp_prepare_cpus(unsigned int max_cpus
)
184 xen_init_lock_cpu(0);
186 smp_store_cpu_info(0);
187 cpu_data(0).x86_max_cores
= 1;
188 set_cpu_sibling_map(0);
190 if (xen_smp_intr_init(0))
193 if (!alloc_cpumask_var(&xen_cpu_initialized_map
, GFP_KERNEL
))
194 panic("could not allocate xen_cpu_initialized_map\n");
196 cpumask_copy(xen_cpu_initialized_map
, cpumask_of(0));
198 /* Restrict the possible_map according to max_cpus. */
199 while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus
)) {
200 for (cpu
= nr_cpu_ids
- 1; !cpu_possible(cpu
); cpu
--)
202 set_cpu_possible(cpu
, false);
205 for_each_possible_cpu (cpu
) {
206 struct task_struct
*idle
;
211 idle
= fork_idle(cpu
);
213 panic("failed fork for CPU %d", cpu
);
215 set_cpu_present(cpu
, true);
220 cpu_initialize_context(unsigned int cpu
, struct task_struct
*idle
)
222 struct vcpu_guest_context
*ctxt
;
223 struct desc_struct
*gdt
;
224 unsigned long gdt_mfn
;
226 if (cpumask_test_and_set_cpu(cpu
, xen_cpu_initialized_map
))
229 ctxt
= kzalloc(sizeof(*ctxt
), GFP_KERNEL
);
233 gdt
= get_cpu_gdt_table(cpu
);
235 ctxt
->flags
= VGCF_IN_KERNEL
;
236 ctxt
->user_regs
.ds
= __USER_DS
;
237 ctxt
->user_regs
.es
= __USER_DS
;
238 ctxt
->user_regs
.ss
= __KERNEL_DS
;
240 ctxt
->user_regs
.fs
= __KERNEL_PERCPU
;
241 ctxt
->user_regs
.gs
= __KERNEL_STACK_CANARY
;
243 ctxt
->gs_base_kernel
= per_cpu_offset(cpu
);
245 ctxt
->user_regs
.eip
= (unsigned long)cpu_bringup_and_idle
;
246 ctxt
->user_regs
.eflags
= 0x1000; /* IOPL_RING1 */
248 memset(&ctxt
->fpu_ctxt
, 0, sizeof(ctxt
->fpu_ctxt
));
250 xen_copy_trap_info(ctxt
->trap_ctxt
);
254 BUG_ON((unsigned long)gdt
& ~PAGE_MASK
);
256 gdt_mfn
= arbitrary_virt_to_mfn(gdt
);
257 make_lowmem_page_readonly(gdt
);
258 make_lowmem_page_readonly(mfn_to_virt(gdt_mfn
));
260 ctxt
->gdt_frames
[0] = gdt_mfn
;
261 ctxt
->gdt_ents
= GDT_ENTRIES
;
263 ctxt
->user_regs
.cs
= __KERNEL_CS
;
264 ctxt
->user_regs
.esp
= idle
->thread
.sp0
- sizeof(struct pt_regs
);
266 ctxt
->kernel_ss
= __KERNEL_DS
;
267 ctxt
->kernel_sp
= idle
->thread
.sp0
;
270 ctxt
->event_callback_cs
= __KERNEL_CS
;
271 ctxt
->failsafe_callback_cs
= __KERNEL_CS
;
273 ctxt
->event_callback_eip
= (unsigned long)xen_hypervisor_callback
;
274 ctxt
->failsafe_callback_eip
= (unsigned long)xen_failsafe_callback
;
276 per_cpu(xen_cr3
, cpu
) = __pa(swapper_pg_dir
);
277 ctxt
->ctrlreg
[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir
));
279 if (HYPERVISOR_vcpu_op(VCPUOP_initialise
, cpu
, ctxt
))
286 static int __cpuinit
xen_cpu_up(unsigned int cpu
)
288 struct task_struct
*idle
= idle_task(cpu
);
291 per_cpu(current_task
, cpu
) = idle
;
295 clear_tsk_thread_flag(idle
, TIF_FORK
);
296 per_cpu(kernel_stack
, cpu
) =
297 (unsigned long)task_stack_page(idle
) -
298 KERNEL_STACK_OFFSET
+ THREAD_SIZE
;
300 xen_setup_runstate_info(cpu
);
301 xen_setup_timer(cpu
);
302 xen_init_lock_cpu(cpu
);
304 per_cpu(cpu_state
, cpu
) = CPU_UP_PREPARE
;
306 /* make sure interrupts start blocked */
307 per_cpu(xen_vcpu
, cpu
)->evtchn_upcall_mask
= 1;
309 rc
= cpu_initialize_context(cpu
, idle
);
313 if (num_online_cpus() == 1)
314 alternatives_smp_switch(1);
316 rc
= xen_smp_intr_init(cpu
);
320 rc
= HYPERVISOR_vcpu_op(VCPUOP_up
, cpu
, NULL
);
323 while(per_cpu(cpu_state
, cpu
) != CPU_ONLINE
) {
324 HYPERVISOR_sched_op(SCHEDOP_yield
, NULL
);
331 static void xen_smp_cpus_done(unsigned int max_cpus
)
335 #ifdef CONFIG_HOTPLUG_CPU
336 static int xen_cpu_disable(void)
338 unsigned int cpu
= smp_processor_id();
342 cpu_disable_common();
344 load_cr3(swapper_pg_dir
);
348 static void xen_cpu_die(unsigned int cpu
)
350 while (HYPERVISOR_vcpu_op(VCPUOP_is_up
, cpu
, NULL
)) {
351 current
->state
= TASK_UNINTERRUPTIBLE
;
352 schedule_timeout(HZ
/10);
354 unbind_from_irqhandler(per_cpu(xen_resched_irq
, cpu
), NULL
);
355 unbind_from_irqhandler(per_cpu(xen_callfunc_irq
, cpu
), NULL
);
356 unbind_from_irqhandler(per_cpu(xen_debug_irq
, cpu
), NULL
);
357 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq
, cpu
), NULL
);
358 xen_uninit_lock_cpu(cpu
);
359 xen_teardown_timer(cpu
);
361 if (num_online_cpus() == 1)
362 alternatives_smp_switch(0);
365 static void __cpuinit
xen_play_dead(void) /* used only with HOTPLUG_CPU */
368 HYPERVISOR_vcpu_op(VCPUOP_down
, smp_processor_id(), NULL
);
372 #else /* !CONFIG_HOTPLUG_CPU */
373 static int xen_cpu_disable(void)
378 static void xen_cpu_die(unsigned int cpu
)
383 static void xen_play_dead(void)
389 static void stop_self(void *v
)
391 int cpu
= smp_processor_id();
393 /* make sure we're not pinning something down */
394 load_cr3(swapper_pg_dir
);
395 /* should set up a minimal gdt */
397 HYPERVISOR_vcpu_op(VCPUOP_down
, cpu
, NULL
);
401 static void xen_smp_send_stop(void)
403 smp_call_function(stop_self
, NULL
, 0);
406 static void xen_smp_send_reschedule(int cpu
)
408 xen_send_IPI_one(cpu
, XEN_RESCHEDULE_VECTOR
);
411 static void xen_send_IPI_mask(const struct cpumask
*mask
,
412 enum ipi_vector vector
)
416 for_each_cpu_and(cpu
, mask
, cpu_online_mask
)
417 xen_send_IPI_one(cpu
, vector
);
420 static void xen_smp_send_call_function_ipi(const struct cpumask
*mask
)
424 xen_send_IPI_mask(mask
, XEN_CALL_FUNCTION_VECTOR
);
426 /* Make sure other vcpus get a chance to run if they need to. */
427 for_each_cpu(cpu
, mask
) {
428 if (xen_vcpu_stolen(cpu
)) {
429 HYPERVISOR_sched_op(SCHEDOP_yield
, NULL
);
435 static void xen_smp_send_call_function_single_ipi(int cpu
)
437 xen_send_IPI_mask(cpumask_of(cpu
),
438 XEN_CALL_FUNCTION_SINGLE_VECTOR
);
441 static irqreturn_t
xen_call_function_interrupt(int irq
, void *dev_id
)
444 generic_smp_call_function_interrupt();
445 inc_irq_stat(irq_call_count
);
451 static irqreturn_t
xen_call_function_single_interrupt(int irq
, void *dev_id
)
454 generic_smp_call_function_single_interrupt();
455 inc_irq_stat(irq_call_count
);
461 static const struct smp_ops xen_smp_ops __initdata
= {
462 .smp_prepare_boot_cpu
= xen_smp_prepare_boot_cpu
,
463 .smp_prepare_cpus
= xen_smp_prepare_cpus
,
464 .smp_cpus_done
= xen_smp_cpus_done
,
466 .cpu_up
= xen_cpu_up
,
467 .cpu_die
= xen_cpu_die
,
468 .cpu_disable
= xen_cpu_disable
,
469 .play_dead
= xen_play_dead
,
471 .smp_send_stop
= xen_smp_send_stop
,
472 .smp_send_reschedule
= xen_smp_send_reschedule
,
474 .send_call_func_ipi
= xen_smp_send_call_function_ipi
,
475 .send_call_func_single_ipi
= xen_smp_send_call_function_single_ipi
,
478 void __init
xen_smp_init(void)
480 smp_ops
= xen_smp_ops
;
481 xen_fill_possible_map();
482 xen_init_spinlocks();