blob: f5b29ecdf18d2942d810b2fcff3f6e3f7c7624a2 [file] [log] [blame]
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001/*
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.
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070014 */
15#include <linux/sched.h>
16#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070018#include <linux/smp.h>
Lin Ming1ff2b0c2012-04-21 00:11:05 +080019#include <linux/irq_work.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070020
21#include <asm/paravirt.h>
22#include <asm/desc.h>
23#include <asm/pgtable.h>
24#include <asm/cpu.h>
25
26#include <xen/interface/xen.h>
27#include <xen/interface/vcpu.h>
28
29#include <asm/xen/interface.h>
30#include <asm/xen/hypercall.h>
31
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +010032#include <xen/xen.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070033#include <xen/page.h>
34#include <xen/events.h>
35
Konrad Rzeszutek Wilked467e62011-09-01 09:48:27 -040036#include <xen/hvc-console.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070037#include "xen-ops.h"
38#include "mmu.h"
39
Mike Travisb78936e2008-12-16 17:33:57 -080040cpumask_var_t xen_cpu_initialized_map;
Jens Axboe3b16cf82008-06-26 11:21:54 +020041
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040042struct xen_common_irq {
43 int irq;
44 char *name;
45};
46static DEFINE_PER_CPU(struct xen_common_irq, xen_resched_irq);
47static DEFINE_PER_CPU(struct xen_common_irq, xen_callfunc_irq);
48static DEFINE_PER_CPU(struct xen_common_irq, xen_callfuncsingle_irq);
49static DEFINE_PER_CPU(struct xen_common_irq, xen_irq_work);
50static DEFINE_PER_CPU(struct xen_common_irq, xen_debug_irq) = { .irq = -1 };
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070051
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070052static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
Jens Axboe3b16cf82008-06-26 11:21:54 +020053static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
Lin Ming1ff2b0c2012-04-21 00:11:05 +080054static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070055
56/*
Peter Zijlstra184748c2011-04-05 17:23:39 +020057 * Reschedule call back.
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070058 */
59static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
60{
Brian Gerst1b437c82009-01-19 00:38:57 +090061 inc_irq_stat(irq_resched_count);
Peter Zijlstra184748c2011-04-05 17:23:39 +020062 scheduler_ipi();
Jeremy Fitzhardinge38bb5ab2008-05-26 23:31:16 +010063
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070064 return IRQ_HANDLED;
65}
66
Daniel Kiperb53cede2011-05-04 20:18:05 +020067static void __cpuinit cpu_bringup(void)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070068{
Srivatsa S. Bhate8c9e782012-03-22 18:29:24 +053069 int cpu;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070070
71 cpu_init();
Alex Nixond68d82a2008-08-22 11:52:15 +010072 touch_softlockup_watchdog();
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -070073 preempt_disable();
74
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -070075 xen_enable_sysenter();
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -070076 xen_enable_syscall();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070077
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -070078 cpu = smp_processor_id();
79 smp_store_cpu_info(cpu);
80 cpu_data(cpu).x86_max_cores = 1;
81 set_cpu_sibling_map(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070082
83 xen_setup_cpu_clockevents();
84
Konrad Rzeszutek Wilk106b4432012-03-21 13:03:45 -040085 notify_cpu_starting(cpu);
86
Rusty Russelld7d37562009-11-03 14:58:38 +103087 set_cpu_online(cpu, true);
Konrad Rzeszutek Wilk106b4432012-03-21 13:03:45 -040088
Alex Shi2113f462012-01-13 23:53:35 +080089 this_cpu_write(cpu_state, CPU_ONLINE);
Konrad Rzeszutek Wilk106b4432012-03-21 13:03:45 -040090
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -070091 wmb();
92
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070093 /* We can take interrupts now: we're officially "up". */
94 local_irq_enable();
95
96 wmb(); /* make sure everything is out */
Alex Nixond68d82a2008-08-22 11:52:15 +010097}
98
Daniel Kiperb53cede2011-05-04 20:18:05 +020099static void __cpuinit cpu_bringup_and_idle(void)
Alex Nixond68d82a2008-08-22 11:52:15 +0100100{
101 cpu_bringup();
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100102 cpu_startup_entry(CPUHP_ONLINE);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700103}
104
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -0400105static void xen_smp_intr_free(unsigned int cpu)
106{
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -0400107 if (per_cpu(xen_resched_irq, cpu).irq >= 0)
108 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu).irq, NULL);
109 if (per_cpu(xen_callfunc_irq, cpu).irq >= 0)
110 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu).irq, NULL);
111 if (per_cpu(xen_debug_irq, cpu).irq >= 0)
112 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu).irq, NULL);
113 if (per_cpu(xen_callfuncsingle_irq, cpu).irq >= 0)
114 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu).irq,
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -0400115 NULL);
116 if (xen_hvm_domain())
117 return;
118
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -0400119 if (per_cpu(xen_irq_work, cpu).irq >= 0)
120 unbind_from_irqhandler(per_cpu(xen_irq_work, cpu).irq, NULL);
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -0400121};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700122static int xen_smp_intr_init(unsigned int cpu)
123{
124 int rc;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700125 const char *resched_name, *callfunc_name, *debug_name;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700126
127 resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
128 rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
129 cpu,
130 xen_reschedule_interrupt,
131 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
132 resched_name,
133 NULL);
134 if (rc < 0)
135 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -0400136 per_cpu(xen_resched_irq, cpu).irq = rc;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700137
138 callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
139 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
140 cpu,
141 xen_call_function_interrupt,
142 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
143 callfunc_name,
144 NULL);
145 if (rc < 0)
146 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -0400147 per_cpu(xen_callfunc_irq, cpu).irq = rc;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700148
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700149 debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
150 rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
151 IRQF_DISABLED | IRQF_PERCPU | IRQF_NOBALANCING,
152 debug_name, NULL);
153 if (rc < 0)
154 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -0400155 per_cpu(xen_debug_irq, cpu).irq = rc;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700156
Jens Axboe3b16cf82008-06-26 11:21:54 +0200157 callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
158 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
159 cpu,
160 xen_call_function_single_interrupt,
161 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
162 callfunc_name,
163 NULL);
164 if (rc < 0)
165 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -0400166 per_cpu(xen_callfuncsingle_irq, cpu).irq = rc;
Jens Axboe3b16cf82008-06-26 11:21:54 +0200167
Konrad Rzeszutek Wilk27d8b202013-04-16 14:37:04 -0400168 /*
169 * The IRQ worker on PVHVM goes through the native path and uses the
170 * IPI mechanism.
171 */
172 if (xen_hvm_domain())
173 return 0;
174
Lin Ming1ff2b0c2012-04-21 00:11:05 +0800175 callfunc_name = kasprintf(GFP_KERNEL, "irqwork%d", cpu);
176 rc = bind_ipi_to_irqhandler(XEN_IRQ_WORK_VECTOR,
177 cpu,
178 xen_irq_work_interrupt,
179 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
180 callfunc_name,
181 NULL);
182 if (rc < 0)
183 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -0400184 per_cpu(xen_irq_work, cpu).irq = rc;
Lin Ming1ff2b0c2012-04-21 00:11:05 +0800185
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700186 return 0;
187
188 fail:
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -0400189 xen_smp_intr_free(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700190 return rc;
191}
192
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700193static void __init xen_fill_possible_map(void)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700194{
195 int i, rc;
196
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100197 if (xen_initial_domain())
198 return;
199
Mike Travise7986732008-12-16 17:33:52 -0800200 for (i = 0; i < nr_cpu_ids; i++) {
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700201 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
Jeremy Fitzhardinge4560a292008-07-08 15:06:56 -0700202 if (rc >= 0) {
203 num_processors++;
Rusty Russell4f062892009-03-13 14:49:54 +1030204 set_cpu_possible(i, true);
Jeremy Fitzhardinge4560a292008-07-08 15:06:56 -0700205 }
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700206 }
207}
208
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100209static void __init xen_filter_cpu_maps(void)
210{
211 int i, rc;
Konrad Rzeszutek Wilkcf405ae2012-04-26 13:50:03 -0400212 unsigned int subtract = 0;
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100213
214 if (!xen_initial_domain())
215 return;
216
Stefano Stabellini801fd142010-09-23 12:06:25 +0100217 num_processors = 0;
218 disabled_cpus = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700219 for (i = 0; i < nr_cpu_ids; i++) {
220 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
221 if (rc >= 0) {
222 num_processors++;
223 set_cpu_possible(i, true);
Stefano Stabellini801fd142010-09-23 12:06:25 +0100224 } else {
225 set_cpu_possible(i, false);
226 set_cpu_present(i, false);
Konrad Rzeszutek Wilkcf405ae2012-04-26 13:50:03 -0400227 subtract++;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700228 }
229 }
Konrad Rzeszutek Wilkcf405ae2012-04-26 13:50:03 -0400230#ifdef CONFIG_HOTPLUG_CPU
231 /* This is akin to using 'nr_cpus' on the Linux command line.
232 * Which is OK as when we use 'dom0_max_vcpus=X' we can only
233 * have up to X, while nr_cpu_ids is greater than X. This
234 * normally is not a problem, except when CPU hotplugging
235 * is involved and then there might be more than X CPUs
236 * in the guest - which will not work as there is no
237 * hypercall to expand the max number of VCPUs an already
238 * running guest has. So cap it up to X. */
239 if (subtract)
240 nr_cpu_ids = nr_cpu_ids - subtract;
241#endif
242
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700243}
244
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700245static void __init xen_smp_prepare_boot_cpu(void)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700246{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700247 BUG_ON(smp_processor_id() != 0);
248 native_smp_prepare_boot_cpu();
249
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700250 /* We've switched to the "real" per-cpu gdt, so make sure the
251 old memory can be recycled */
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -0800252 make_lowmem_page_readwrite(xen_initial_gdt);
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700253
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100254 xen_filter_cpu_maps();
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700255 xen_setup_vcpu_info_placement();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700256}
257
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700258static void __init xen_smp_prepare_cpus(unsigned int max_cpus)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700259{
260 unsigned cpu;
Andrew Jones900cba82009-12-18 10:31:31 +0100261 unsigned int i;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700262
Konrad Rzeszutek Wilked467e62011-09-01 09:48:27 -0400263 if (skip_ioapic_setup) {
264 char *m = (max_cpus == 0) ?
265 "The nosmp parameter is incompatible with Xen; " \
266 "use Xen dom0_max_vcpus=1 parameter" :
267 "The noapic parameter is incompatible with Xen";
268
269 xen_raw_printk(m);
270 panic(m);
271 }
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700272 xen_init_lock_cpu(0);
273
Konrad Rzeszutek Wilk06d0b5d2012-12-17 20:29:32 -0500274 smp_store_boot_cpu_info();
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700275 cpu_data(0).x86_max_cores = 1;
Andrew Jones900cba82009-12-18 10:31:31 +0100276
277 for_each_possible_cpu(i) {
278 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
279 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
280 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
281 }
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700282 set_cpu_sibling_map(0);
283
284 if (xen_smp_intr_init(0))
285 BUG();
286
Mike Travisb78936e2008-12-16 17:33:57 -0800287 if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
288 panic("could not allocate xen_cpu_initialized_map\n");
289
290 cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700291
292 /* Restrict the possible_map according to max_cpus. */
293 while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
Mike Travise7986732008-12-16 17:33:52 -0800294 for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700295 continue;
Rusty Russell4f062892009-03-13 14:49:54 +1030296 set_cpu_possible(cpu, false);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700297 }
298
Thomas Gleixner7eb43a62012-04-20 13:05:48 +0000299 for_each_possible_cpu(cpu)
Rusty Russell4f062892009-03-13 14:49:54 +1030300 set_cpu_present(cpu, true);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700301}
302
Daniel Kiperb53cede2011-05-04 20:18:05 +0200303static int __cpuinit
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700304cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
305{
306 struct vcpu_guest_context *ctxt;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700307 struct desc_struct *gdt;
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800308 unsigned long gdt_mfn;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700309
Mike Travisb78936e2008-12-16 17:33:57 -0800310 if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700311 return 0;
312
313 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
314 if (ctxt == NULL)
315 return -ENOMEM;
316
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700317 gdt = get_cpu_gdt_table(cpu);
318
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700319 ctxt->flags = VGCF_IN_KERNEL;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700320 ctxt->user_regs.ss = __KERNEL_DS;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700321#ifdef CONFIG_X86_32
322 ctxt->user_regs.fs = __KERNEL_PERCPU;
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700323 ctxt->user_regs.gs = __KERNEL_STACK_CANARY;
Jeremy Fitzhardinge795f99b2009-01-30 17:47:54 +0900324#else
325 ctxt->gs_base_kernel = per_cpu_offset(cpu);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700326#endif
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700327 ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700328
329 memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
330
Konrad Rzeszutek Wilkdacd45f2012-10-22 11:35:16 -0400331 {
332 ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
333 ctxt->user_regs.ds = __USER_DS;
334 ctxt->user_regs.es = __USER_DS;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700335
Konrad Rzeszutek Wilkdacd45f2012-10-22 11:35:16 -0400336 xen_copy_trap_info(ctxt->trap_ctxt);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700337
Konrad Rzeszutek Wilkdacd45f2012-10-22 11:35:16 -0400338 ctxt->ldt_ents = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700339
Konrad Rzeszutek Wilkdacd45f2012-10-22 11:35:16 -0400340 BUG_ON((unsigned long)gdt & ~PAGE_MASK);
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800341
Konrad Rzeszutek Wilkdacd45f2012-10-22 11:35:16 -0400342 gdt_mfn = arbitrary_virt_to_mfn(gdt);
343 make_lowmem_page_readonly(gdt);
344 make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700345
Konrad Rzeszutek Wilkdacd45f2012-10-22 11:35:16 -0400346 ctxt->gdt_frames[0] = gdt_mfn;
347 ctxt->gdt_ents = GDT_ENTRIES;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700348
Konrad Rzeszutek Wilkdacd45f2012-10-22 11:35:16 -0400349 ctxt->kernel_ss = __KERNEL_DS;
350 ctxt->kernel_sp = idle->thread.sp0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700351
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700352#ifdef CONFIG_X86_32
Konrad Rzeszutek Wilkdacd45f2012-10-22 11:35:16 -0400353 ctxt->event_callback_cs = __KERNEL_CS;
354 ctxt->failsafe_callback_cs = __KERNEL_CS;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700355#endif
Konrad Rzeszutek Wilkdacd45f2012-10-22 11:35:16 -0400356 ctxt->event_callback_eip =
357 (unsigned long)xen_hypervisor_callback;
358 ctxt->failsafe_callback_eip =
359 (unsigned long)xen_failsafe_callback;
360 }
361 ctxt->user_regs.cs = __KERNEL_CS;
362 ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700363
364 per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
365 ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
366
367 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
368 BUG();
369
370 kfree(ctxt);
371 return 0;
372}
373
Thomas Gleixner7eb43a62012-04-20 13:05:48 +0000374static int __cpuinit xen_cpu_up(unsigned int cpu, struct task_struct *idle)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700375{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700376 int rc;
377
Brian Gerstc6f5e0a2009-01-19 00:38:58 +0900378 per_cpu(current_task, cpu) = idle;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700379#ifdef CONFIG_X86_32
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700380 irq_ctx_init(cpu);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700381#else
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700382 clear_tsk_thread_flag(idle, TIF_FORK);
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -0800383 per_cpu(kernel_stack, cpu) =
384 (unsigned long)task_stack_page(idle) -
385 KERNEL_STACK_OFFSET + THREAD_SIZE;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700386#endif
Ian Campbell02889672009-11-24 09:32:48 -0800387 xen_setup_runstate_info(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700388 xen_setup_timer(cpu);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700389 xen_init_lock_cpu(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700390
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700391 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
392
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700393 /* make sure interrupts start blocked */
394 per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
395
396 rc = cpu_initialize_context(cpu, idle);
397 if (rc)
398 return rc;
399
400 if (num_online_cpus() == 1)
Rusty Russell816afe42012-08-06 17:29:49 +0930401 /* Just in case we booted with a single CPU. */
402 alternatives_enable_smp();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700403
404 rc = xen_smp_intr_init(cpu);
405 if (rc)
406 return rc;
407
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700408 rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
409 BUG_ON(rc);
410
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700411 while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
Hannes Eder1207cf8e2009-03-05 20:13:57 +0100412 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700413 barrier();
414 }
415
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700416 return 0;
417}
418
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700419static void xen_smp_cpus_done(unsigned int max_cpus)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700420{
421}
422
Alex Nixon27371462008-09-08 13:43:33 +0100423#ifdef CONFIG_HOTPLUG_CPU
Alex Nixon26fd1052008-09-08 13:43:34 +0100424static int xen_cpu_disable(void)
Alex Nixond68d82a2008-08-22 11:52:15 +0100425{
426 unsigned int cpu = smp_processor_id();
427 if (cpu == 0)
428 return -EBUSY;
429
430 cpu_disable_common();
431
432 load_cr3(swapper_pg_dir);
433 return 0;
434}
435
Alex Nixon26fd1052008-09-08 13:43:34 +0100436static void xen_cpu_die(unsigned int cpu)
Alex Nixond68d82a2008-08-22 11:52:15 +0100437{
Konrad Rzeszutek Wilkb12abaa2013-04-08 20:56:35 -0400438 while (xen_pv_domain() && HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) {
Alex Nixond68d82a2008-08-22 11:52:15 +0100439 current->state = TASK_UNINTERRUPTIBLE;
440 schedule_timeout(HZ/10);
441 }
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -0400442 xen_smp_intr_free(cpu);
Alex Nixond68d82a2008-08-22 11:52:15 +0100443 xen_uninit_lock_cpu(cpu);
444 xen_teardown_timer(cpu);
Alex Nixond68d82a2008-08-22 11:52:15 +0100445}
446
Robert P. J. Day71709242009-12-28 11:50:29 -0500447static void __cpuinit xen_play_dead(void) /* used only with HOTPLUG_CPU */
Alex Nixond68d82a2008-08-22 11:52:15 +0100448{
449 play_dead_common();
450 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
451 cpu_bringup();
452}
453
Alex Nixon27371462008-09-08 13:43:33 +0100454#else /* !CONFIG_HOTPLUG_CPU */
Alex Nixon26fd1052008-09-08 13:43:34 +0100455static int xen_cpu_disable(void)
Alex Nixon27371462008-09-08 13:43:33 +0100456{
457 return -ENOSYS;
458}
459
Alex Nixon26fd1052008-09-08 13:43:34 +0100460static void xen_cpu_die(unsigned int cpu)
Alex Nixon27371462008-09-08 13:43:33 +0100461{
462 BUG();
463}
464
Alex Nixon26fd1052008-09-08 13:43:34 +0100465static void xen_play_dead(void)
Alex Nixon27371462008-09-08 13:43:33 +0100466{
467 BUG();
468}
469
470#endif
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700471static void stop_self(void *v)
472{
473 int cpu = smp_processor_id();
474
475 /* make sure we're not pinning something down */
476 load_cr3(swapper_pg_dir);
477 /* should set up a minimal gdt */
478
Ian Campbell086748e2010-08-03 14:55:14 -0700479 set_cpu_online(cpu, false);
480
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700481 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
482 BUG();
483}
484
Alok Kataria76fac072010-10-11 14:37:08 -0700485static void xen_stop_other_cpus(int wait)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700486{
Alok Kataria76fac072010-10-11 14:37:08 -0700487 smp_call_function(stop_self, NULL, wait);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700488}
489
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700490static void xen_smp_send_reschedule(int cpu)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700491{
492 xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
493}
494
Ben Guthrof447d562012-04-21 00:11:04 +0800495static void __xen_send_IPI_mask(const struct cpumask *mask,
496 int vector)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700497{
498 unsigned cpu;
499
Mike Travisbcda0162008-12-16 17:33:59 -0800500 for_each_cpu_and(cpu, mask, cpu_online_mask)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700501 xen_send_IPI_one(cpu, vector);
502}
503
Mike Travisbcda0162008-12-16 17:33:59 -0800504static void xen_smp_send_call_function_ipi(const struct cpumask *mask)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200505{
506 int cpu;
507
Ben Guthrof447d562012-04-21 00:11:04 +0800508 __xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200509
510 /* Make sure other vcpus get a chance to run if they need to. */
Mike Travisbcda0162008-12-16 17:33:59 -0800511 for_each_cpu(cpu, mask) {
Jens Axboe3b16cf82008-06-26 11:21:54 +0200512 if (xen_vcpu_stolen(cpu)) {
Hannes Eder1207cf8e2009-03-05 20:13:57 +0100513 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200514 break;
515 }
516 }
517}
518
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700519static void xen_smp_send_call_function_single_ipi(int cpu)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200520{
Ben Guthrof447d562012-04-21 00:11:04 +0800521 __xen_send_IPI_mask(cpumask_of(cpu),
Mike Travise7986732008-12-16 17:33:52 -0800522 XEN_CALL_FUNCTION_SINGLE_VECTOR);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200523}
524
Ben Guthrof447d562012-04-21 00:11:04 +0800525static inline int xen_map_vector(int vector)
526{
527 int xen_vector;
528
529 switch (vector) {
530 case RESCHEDULE_VECTOR:
531 xen_vector = XEN_RESCHEDULE_VECTOR;
532 break;
533 case CALL_FUNCTION_VECTOR:
534 xen_vector = XEN_CALL_FUNCTION_VECTOR;
535 break;
536 case CALL_FUNCTION_SINGLE_VECTOR:
537 xen_vector = XEN_CALL_FUNCTION_SINGLE_VECTOR;
538 break;
Lin Ming1ff2b0c2012-04-21 00:11:05 +0800539 case IRQ_WORK_VECTOR:
540 xen_vector = XEN_IRQ_WORK_VECTOR;
541 break;
Ben Guthrof447d562012-04-21 00:11:04 +0800542 default:
543 xen_vector = -1;
544 printk(KERN_ERR "xen: vector 0x%x is not implemented\n",
545 vector);
546 }
547
548 return xen_vector;
549}
550
551void xen_send_IPI_mask(const struct cpumask *mask,
552 int vector)
553{
554 int xen_vector = xen_map_vector(vector);
555
556 if (xen_vector >= 0)
557 __xen_send_IPI_mask(mask, xen_vector);
558}
559
560void xen_send_IPI_all(int vector)
561{
562 int xen_vector = xen_map_vector(vector);
563
564 if (xen_vector >= 0)
565 __xen_send_IPI_mask(cpu_online_mask, xen_vector);
566}
567
568void xen_send_IPI_self(int vector)
569{
570 int xen_vector = xen_map_vector(vector);
571
572 if (xen_vector >= 0)
573 xen_send_IPI_one(smp_processor_id(), xen_vector);
574}
575
576void xen_send_IPI_mask_allbutself(const struct cpumask *mask,
577 int vector)
578{
579 unsigned cpu;
580 unsigned int this_cpu = smp_processor_id();
Stefan Bader1db01b42013-05-08 16:37:35 +0200581 int xen_vector = xen_map_vector(vector);
Ben Guthrof447d562012-04-21 00:11:04 +0800582
Stefan Bader1db01b42013-05-08 16:37:35 +0200583 if (!(num_online_cpus() > 1) || (xen_vector < 0))
Ben Guthrof447d562012-04-21 00:11:04 +0800584 return;
585
586 for_each_cpu_and(cpu, mask, cpu_online_mask) {
587 if (this_cpu == cpu)
588 continue;
589
Stefan Bader1db01b42013-05-08 16:37:35 +0200590 xen_send_IPI_one(cpu, xen_vector);
Ben Guthrof447d562012-04-21 00:11:04 +0800591 }
592}
593
594void xen_send_IPI_allbutself(int vector)
595{
Stefan Bader1db01b42013-05-08 16:37:35 +0200596 xen_send_IPI_mask_allbutself(cpu_online_mask, vector);
Ben Guthrof447d562012-04-21 00:11:04 +0800597}
598
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700599static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
600{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700601 irq_enter();
Jens Axboe3b16cf82008-06-26 11:21:54 +0200602 generic_smp_call_function_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900603 inc_irq_stat(irq_call_count);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700604 irq_exit();
605
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700606 return IRQ_HANDLED;
607}
608
Jens Axboe3b16cf82008-06-26 11:21:54 +0200609static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700610{
Jens Axboe3b16cf82008-06-26 11:21:54 +0200611 irq_enter();
612 generic_smp_call_function_single_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900613 inc_irq_stat(irq_call_count);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200614 irq_exit();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700615
Jens Axboe3b16cf82008-06-26 11:21:54 +0200616 return IRQ_HANDLED;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700617}
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700618
Lin Ming1ff2b0c2012-04-21 00:11:05 +0800619static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id)
620{
621 irq_enter();
622 irq_work_run();
623 inc_irq_stat(apic_irq_work_irqs);
624 irq_exit();
625
626 return IRQ_HANDLED;
627}
628
Daniel Kiperb53cede2011-05-04 20:18:05 +0200629static const struct smp_ops xen_smp_ops __initconst = {
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700630 .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
631 .smp_prepare_cpus = xen_smp_prepare_cpus,
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700632 .smp_cpus_done = xen_smp_cpus_done,
633
Alex Nixond68d82a2008-08-22 11:52:15 +0100634 .cpu_up = xen_cpu_up,
635 .cpu_die = xen_cpu_die,
636 .cpu_disable = xen_cpu_disable,
637 .play_dead = xen_play_dead,
638
Alok Kataria76fac072010-10-11 14:37:08 -0700639 .stop_other_cpus = xen_stop_other_cpus,
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700640 .smp_send_reschedule = xen_smp_send_reschedule,
641
642 .send_call_func_ipi = xen_smp_send_call_function_ipi,
643 .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
644};
645
646void __init xen_smp_init(void)
647{
648 smp_ops = xen_smp_ops;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700649 xen_fill_possible_map();
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700650 xen_init_spinlocks();
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700651}
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000652
653static void __init xen_hvm_smp_prepare_cpus(unsigned int max_cpus)
654{
655 native_smp_prepare_cpus(max_cpus);
656 WARN_ON(xen_smp_intr_init(0));
657
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000658 xen_init_lock_cpu(0);
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000659}
660
Thomas Gleixner5cdaf182012-04-20 13:05:47 +0000661static int __cpuinit xen_hvm_cpu_up(unsigned int cpu, struct task_struct *tidle)
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000662{
663 int rc;
Thomas Gleixner5cdaf182012-04-20 13:05:47 +0000664 rc = native_cpu_up(cpu, tidle);
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000665 WARN_ON (xen_smp_intr_init(cpu));
666 return rc;
667}
668
669static void xen_hvm_cpu_die(unsigned int cpu)
670{
Konrad Rzeszutek Wilkb12abaa2013-04-08 20:56:35 -0400671 xen_cpu_die(cpu);
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000672 native_cpu_die(cpu);
673}
674
675void __init xen_hvm_smp_init(void)
676{
Stefano Stabellini3c05c4b2011-08-17 15:15:00 +0200677 if (!xen_have_vector_callback)
678 return;
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000679 smp_ops.smp_prepare_cpus = xen_hvm_smp_prepare_cpus;
680 smp_ops.smp_send_reschedule = xen_smp_send_reschedule;
681 smp_ops.cpu_up = xen_hvm_cpu_up;
682 smp_ops.cpu_die = xen_hvm_cpu_die;
683 smp_ops.send_call_func_ipi = xen_smp_send_call_function_ipi;
684 smp_ops.send_call_func_single_ipi = xen_smp_send_call_function_single_ipi;
685}