Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 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. |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 14 | */ |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 18 | #include <linux/smp.h> |
| 19 | |
| 20 | #include <asm/paravirt.h> |
| 21 | #include <asm/desc.h> |
| 22 | #include <asm/pgtable.h> |
| 23 | #include <asm/cpu.h> |
| 24 | |
| 25 | #include <xen/interface/xen.h> |
| 26 | #include <xen/interface/vcpu.h> |
| 27 | |
| 28 | #include <asm/xen/interface.h> |
| 29 | #include <asm/xen/hypercall.h> |
| 30 | |
| 31 | #include <xen/page.h> |
| 32 | #include <xen/events.h> |
| 33 | |
| 34 | #include "xen-ops.h" |
| 35 | #include "mmu.h" |
| 36 | |
Mike Travis | b78936e | 2008-12-16 17:33:57 -0800 | [diff] [blame] | 37 | cpumask_var_t xen_cpu_initialized_map; |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 38 | |
Tejun Heo | c6e22f9 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 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; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 43 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 44 | static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id); |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 45 | static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Reschedule call back. Nothing to do, |
| 49 | * all the work is done automatically when |
| 50 | * we return from the interrupt. |
| 51 | */ |
| 52 | static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id) |
| 53 | { |
Brian Gerst | 1b437c8 | 2009-01-19 00:38:57 +0900 | [diff] [blame] | 54 | inc_irq_stat(irq_resched_count); |
Jeremy Fitzhardinge | 38bb5ab | 2008-05-26 23:31:16 +0100 | [diff] [blame] | 55 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 56 | return IRQ_HANDLED; |
| 57 | } |
| 58 | |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 59 | static __cpuinit void cpu_bringup(void) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 60 | { |
| 61 | int cpu = smp_processor_id(); |
| 62 | |
| 63 | cpu_init(); |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 64 | touch_softlockup_watchdog(); |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 65 | preempt_disable(); |
| 66 | |
Jeremy Fitzhardinge | e2a81ba | 2008-03-17 16:37:17 -0700 | [diff] [blame] | 67 | xen_enable_sysenter(); |
Jeremy Fitzhardinge | 6fcac6d | 2008-07-08 15:07:14 -0700 | [diff] [blame] | 68 | xen_enable_syscall(); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 69 | |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 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); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 74 | |
| 75 | xen_setup_cpu_clockevents(); |
| 76 | |
Rusty Russell | d7d3756 | 2009-11-03 14:58:38 +1030 | [diff] [blame] | 77 | set_cpu_online(cpu, true); |
Ingo Molnar | 6dbde35 | 2009-01-15 22:15:53 +0900 | [diff] [blame] | 78 | percpu_write(cpu_state, CPU_ONLINE); |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 79 | wmb(); |
| 80 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 81 | /* We can take interrupts now: we're officially "up". */ |
| 82 | local_irq_enable(); |
| 83 | |
| 84 | wmb(); /* make sure everything is out */ |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static __cpuinit void cpu_bringup_and_idle(void) |
| 88 | { |
| 89 | cpu_bringup(); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 90 | cpu_idle(); |
| 91 | } |
| 92 | |
| 93 | static int xen_smp_intr_init(unsigned int cpu) |
| 94 | { |
| 95 | int rc; |
Jeremy Fitzhardinge | ee523ca | 2008-03-17 16:37:18 -0700 | [diff] [blame] | 96 | const char *resched_name, *callfunc_name, *debug_name; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 97 | |
| 98 | resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu); |
| 99 | rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR, |
| 100 | cpu, |
| 101 | xen_reschedule_interrupt, |
| 102 | IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING, |
| 103 | resched_name, |
| 104 | NULL); |
| 105 | if (rc < 0) |
| 106 | goto fail; |
Tejun Heo | c6e22f9 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 107 | per_cpu(xen_resched_irq, cpu) = rc; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 108 | |
| 109 | callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu); |
| 110 | rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR, |
| 111 | cpu, |
| 112 | xen_call_function_interrupt, |
| 113 | IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING, |
| 114 | callfunc_name, |
| 115 | NULL); |
| 116 | if (rc < 0) |
| 117 | goto fail; |
Tejun Heo | c6e22f9 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 118 | per_cpu(xen_callfunc_irq, cpu) = rc; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 119 | |
Jeremy Fitzhardinge | ee523ca | 2008-03-17 16:37:18 -0700 | [diff] [blame] | 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, |
| 123 | debug_name, NULL); |
| 124 | if (rc < 0) |
| 125 | goto fail; |
Tejun Heo | c6e22f9 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 126 | per_cpu(xen_debug_irq, cpu) = rc; |
Jeremy Fitzhardinge | ee523ca | 2008-03-17 16:37:18 -0700 | [diff] [blame] | 127 | |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 128 | callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu); |
| 129 | rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR, |
| 130 | cpu, |
| 131 | xen_call_function_single_interrupt, |
| 132 | IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING, |
| 133 | callfunc_name, |
| 134 | NULL); |
| 135 | if (rc < 0) |
| 136 | goto fail; |
Tejun Heo | c6e22f9 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 137 | per_cpu(xen_callfuncsingle_irq, cpu) = rc; |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 138 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 139 | return 0; |
| 140 | |
| 141 | fail: |
Tejun Heo | c6e22f9 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 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), |
| 150 | NULL); |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 151 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 152 | return rc; |
| 153 | } |
| 154 | |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 155 | static void __init xen_fill_possible_map(void) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 156 | { |
| 157 | int i, rc; |
| 158 | |
Stefano Stabellini | 801fd14 | 2010-09-23 12:06:25 +0100 | [diff] [blame^] | 159 | num_processors = 0; |
| 160 | disabled_cpus = 0; |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 161 | for (i = 0; i < nr_cpu_ids; i++) { |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 162 | rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL); |
Jeremy Fitzhardinge | 4560a29 | 2008-07-08 15:06:56 -0700 | [diff] [blame] | 163 | if (rc >= 0) { |
| 164 | num_processors++; |
Rusty Russell | 4f06289 | 2009-03-13 14:49:54 +1030 | [diff] [blame] | 165 | set_cpu_possible(i, true); |
Stefano Stabellini | 801fd14 | 2010-09-23 12:06:25 +0100 | [diff] [blame^] | 166 | } else { |
| 167 | set_cpu_possible(i, false); |
| 168 | set_cpu_present(i, false); |
Jeremy Fitzhardinge | 4560a29 | 2008-07-08 15:06:56 -0700 | [diff] [blame] | 169 | } |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 173 | static void __init xen_smp_prepare_boot_cpu(void) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 174 | { |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 175 | BUG_ON(smp_processor_id() != 0); |
| 176 | native_smp_prepare_boot_cpu(); |
| 177 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 178 | /* We've switched to the "real" per-cpu gdt, so make sure the |
| 179 | old memory can be recycled */ |
Jeremy Fitzhardinge | 3834143 | 2009-02-02 13:55:31 -0800 | [diff] [blame] | 180 | make_lowmem_page_readwrite(xen_initial_gdt); |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 181 | |
| 182 | xen_setup_vcpu_info_placement(); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 185 | static void __init xen_smp_prepare_cpus(unsigned int max_cpus) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 186 | { |
| 187 | unsigned cpu; |
| 188 | |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 189 | xen_init_lock_cpu(0); |
| 190 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 191 | smp_store_cpu_info(0); |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 192 | cpu_data(0).x86_max_cores = 1; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 193 | set_cpu_sibling_map(0); |
| 194 | |
| 195 | if (xen_smp_intr_init(0)) |
| 196 | BUG(); |
| 197 | |
Stefano Stabellini | 801fd14 | 2010-09-23 12:06:25 +0100 | [diff] [blame^] | 198 | xen_fill_possible_map(); |
| 199 | |
Mike Travis | b78936e | 2008-12-16 17:33:57 -0800 | [diff] [blame] | 200 | if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL)) |
| 201 | panic("could not allocate xen_cpu_initialized_map\n"); |
| 202 | |
| 203 | cpumask_copy(xen_cpu_initialized_map, cpumask_of(0)); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 204 | |
| 205 | /* Restrict the possible_map according to max_cpus. */ |
| 206 | while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) { |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 207 | for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 208 | continue; |
Rusty Russell | 4f06289 | 2009-03-13 14:49:54 +1030 | [diff] [blame] | 209 | set_cpu_possible(cpu, false); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | for_each_possible_cpu (cpu) { |
| 213 | struct task_struct *idle; |
| 214 | |
| 215 | if (cpu == 0) |
| 216 | continue; |
| 217 | |
| 218 | idle = fork_idle(cpu); |
| 219 | if (IS_ERR(idle)) |
| 220 | panic("failed fork for CPU %d", cpu); |
| 221 | |
Rusty Russell | 4f06289 | 2009-03-13 14:49:54 +1030 | [diff] [blame] | 222 | set_cpu_present(cpu, true); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 223 | } |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static __cpuinit int |
| 227 | cpu_initialize_context(unsigned int cpu, struct task_struct *idle) |
| 228 | { |
| 229 | struct vcpu_guest_context *ctxt; |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 230 | struct desc_struct *gdt; |
Jeremy Fitzhardinge | 9976b39 | 2009-02-27 09:19:26 -0800 | [diff] [blame] | 231 | unsigned long gdt_mfn; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 232 | |
Mike Travis | b78936e | 2008-12-16 17:33:57 -0800 | [diff] [blame] | 233 | if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map)) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | |
| 236 | ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL); |
| 237 | if (ctxt == NULL) |
| 238 | return -ENOMEM; |
| 239 | |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 240 | gdt = get_cpu_gdt_table(cpu); |
| 241 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 242 | ctxt->flags = VGCF_IN_KERNEL; |
| 243 | ctxt->user_regs.ds = __USER_DS; |
| 244 | ctxt->user_regs.es = __USER_DS; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 245 | ctxt->user_regs.ss = __KERNEL_DS; |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 246 | #ifdef CONFIG_X86_32 |
| 247 | ctxt->user_regs.fs = __KERNEL_PERCPU; |
Jeremy Fitzhardinge | 577eebe | 2009-08-27 12:46:35 -0700 | [diff] [blame] | 248 | ctxt->user_regs.gs = __KERNEL_STACK_CANARY; |
Jeremy Fitzhardinge | 795f99b | 2009-01-30 17:47:54 +0900 | [diff] [blame] | 249 | #else |
| 250 | ctxt->gs_base_kernel = per_cpu_offset(cpu); |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 251 | #endif |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 252 | ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle; |
| 253 | ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */ |
| 254 | |
| 255 | memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt)); |
| 256 | |
| 257 | xen_copy_trap_info(ctxt->trap_ctxt); |
| 258 | |
| 259 | ctxt->ldt_ents = 0; |
| 260 | |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 261 | BUG_ON((unsigned long)gdt & ~PAGE_MASK); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 262 | |
Jeremy Fitzhardinge | 9976b39 | 2009-02-27 09:19:26 -0800 | [diff] [blame] | 263 | gdt_mfn = arbitrary_virt_to_mfn(gdt); |
| 264 | make_lowmem_page_readonly(gdt); |
| 265 | make_lowmem_page_readonly(mfn_to_virt(gdt_mfn)); |
| 266 | |
| 267 | ctxt->gdt_frames[0] = gdt_mfn; |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 268 | ctxt->gdt_ents = GDT_ENTRIES; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 269 | |
| 270 | ctxt->user_regs.cs = __KERNEL_CS; |
H. Peter Anvin | faca622 | 2008-01-30 13:31:02 +0100 | [diff] [blame] | 271 | ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 272 | |
| 273 | ctxt->kernel_ss = __KERNEL_DS; |
H. Peter Anvin | faca622 | 2008-01-30 13:31:02 +0100 | [diff] [blame] | 274 | ctxt->kernel_sp = idle->thread.sp0; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 275 | |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 276 | #ifdef CONFIG_X86_32 |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 277 | ctxt->event_callback_cs = __KERNEL_CS; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 278 | ctxt->failsafe_callback_cs = __KERNEL_CS; |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 279 | #endif |
| 280 | ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 281 | ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback; |
| 282 | |
| 283 | per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir); |
| 284 | ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir)); |
| 285 | |
| 286 | if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt)) |
| 287 | BUG(); |
| 288 | |
| 289 | kfree(ctxt); |
| 290 | return 0; |
| 291 | } |
| 292 | |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 293 | static int __cpuinit xen_cpu_up(unsigned int cpu) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 294 | { |
| 295 | struct task_struct *idle = idle_task(cpu); |
| 296 | int rc; |
| 297 | |
Brian Gerst | c6f5e0a | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 298 | per_cpu(current_task, cpu) = idle; |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 299 | #ifdef CONFIG_X86_32 |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 300 | irq_ctx_init(cpu); |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 301 | #else |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 302 | clear_tsk_thread_flag(idle, TIF_FORK); |
Jeremy Fitzhardinge | 3834143 | 2009-02-02 13:55:31 -0800 | [diff] [blame] | 303 | per_cpu(kernel_stack, cpu) = |
| 304 | (unsigned long)task_stack_page(idle) - |
| 305 | KERNEL_STACK_OFFSET + THREAD_SIZE; |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 306 | #endif |
Ian Campbell | 0288967 | 2009-11-24 09:32:48 -0800 | [diff] [blame] | 307 | xen_setup_runstate_info(cpu); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 308 | xen_setup_timer(cpu); |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 309 | xen_init_lock_cpu(cpu); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 310 | |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 311 | per_cpu(cpu_state, cpu) = CPU_UP_PREPARE; |
| 312 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 313 | /* make sure interrupts start blocked */ |
| 314 | per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1; |
| 315 | |
| 316 | rc = cpu_initialize_context(cpu, idle); |
| 317 | if (rc) |
| 318 | return rc; |
| 319 | |
| 320 | if (num_online_cpus() == 1) |
| 321 | alternatives_smp_switch(1); |
| 322 | |
| 323 | rc = xen_smp_intr_init(cpu); |
| 324 | if (rc) |
| 325 | return rc; |
| 326 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 327 | rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL); |
| 328 | BUG_ON(rc); |
| 329 | |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 330 | while(per_cpu(cpu_state, cpu) != CPU_ONLINE) { |
Hannes Eder | 1207cf8e | 2009-03-05 20:13:57 +0100 | [diff] [blame] | 331 | HYPERVISOR_sched_op(SCHEDOP_yield, NULL); |
Jeremy Fitzhardinge | c7b7594 | 2008-07-08 15:06:43 -0700 | [diff] [blame] | 332 | barrier(); |
| 333 | } |
| 334 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 338 | static void xen_smp_cpus_done(unsigned int max_cpus) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 339 | { |
| 340 | } |
| 341 | |
Alex Nixon | 2737146 | 2008-09-08 13:43:33 +0100 | [diff] [blame] | 342 | #ifdef CONFIG_HOTPLUG_CPU |
Alex Nixon | 26fd105 | 2008-09-08 13:43:34 +0100 | [diff] [blame] | 343 | static int xen_cpu_disable(void) |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 344 | { |
| 345 | unsigned int cpu = smp_processor_id(); |
| 346 | if (cpu == 0) |
| 347 | return -EBUSY; |
| 348 | |
| 349 | cpu_disable_common(); |
| 350 | |
| 351 | load_cr3(swapper_pg_dir); |
| 352 | return 0; |
| 353 | } |
| 354 | |
Alex Nixon | 26fd105 | 2008-09-08 13:43:34 +0100 | [diff] [blame] | 355 | static void xen_cpu_die(unsigned int cpu) |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 356 | { |
| 357 | while (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) { |
| 358 | current->state = TASK_UNINTERRUPTIBLE; |
| 359 | schedule_timeout(HZ/10); |
| 360 | } |
Tejun Heo | c6e22f9 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 361 | unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL); |
| 362 | unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL); |
| 363 | unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL); |
| 364 | unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu), NULL); |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 365 | xen_uninit_lock_cpu(cpu); |
| 366 | xen_teardown_timer(cpu); |
| 367 | |
| 368 | if (num_online_cpus() == 1) |
| 369 | alternatives_smp_switch(0); |
| 370 | } |
| 371 | |
Robert P. J. Day | 7170924 | 2009-12-28 11:50:29 -0500 | [diff] [blame] | 372 | static void __cpuinit xen_play_dead(void) /* used only with HOTPLUG_CPU */ |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 373 | { |
| 374 | play_dead_common(); |
| 375 | HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL); |
| 376 | cpu_bringup(); |
| 377 | } |
| 378 | |
Alex Nixon | 2737146 | 2008-09-08 13:43:33 +0100 | [diff] [blame] | 379 | #else /* !CONFIG_HOTPLUG_CPU */ |
Alex Nixon | 26fd105 | 2008-09-08 13:43:34 +0100 | [diff] [blame] | 380 | static int xen_cpu_disable(void) |
Alex Nixon | 2737146 | 2008-09-08 13:43:33 +0100 | [diff] [blame] | 381 | { |
| 382 | return -ENOSYS; |
| 383 | } |
| 384 | |
Alex Nixon | 26fd105 | 2008-09-08 13:43:34 +0100 | [diff] [blame] | 385 | static void xen_cpu_die(unsigned int cpu) |
Alex Nixon | 2737146 | 2008-09-08 13:43:33 +0100 | [diff] [blame] | 386 | { |
| 387 | BUG(); |
| 388 | } |
| 389 | |
Alex Nixon | 26fd105 | 2008-09-08 13:43:34 +0100 | [diff] [blame] | 390 | static void xen_play_dead(void) |
Alex Nixon | 2737146 | 2008-09-08 13:43:33 +0100 | [diff] [blame] | 391 | { |
| 392 | BUG(); |
| 393 | } |
| 394 | |
| 395 | #endif |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 396 | static void stop_self(void *v) |
| 397 | { |
| 398 | int cpu = smp_processor_id(); |
| 399 | |
| 400 | /* make sure we're not pinning something down */ |
| 401 | load_cr3(swapper_pg_dir); |
| 402 | /* should set up a minimal gdt */ |
| 403 | |
Ian Campbell | 086748e | 2010-08-03 14:55:14 -0700 | [diff] [blame] | 404 | set_cpu_online(cpu, false); |
| 405 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 406 | HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL); |
| 407 | BUG(); |
| 408 | } |
| 409 | |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 410 | static void xen_smp_send_stop(void) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 411 | { |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 412 | smp_call_function(stop_self, NULL, 0); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 415 | static void xen_smp_send_reschedule(int cpu) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 416 | { |
| 417 | xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR); |
| 418 | } |
| 419 | |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 420 | static void xen_send_IPI_mask(const struct cpumask *mask, |
| 421 | enum ipi_vector vector) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 422 | { |
| 423 | unsigned cpu; |
| 424 | |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 425 | for_each_cpu_and(cpu, mask, cpu_online_mask) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 426 | xen_send_IPI_one(cpu, vector); |
| 427 | } |
| 428 | |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 429 | static void xen_smp_send_call_function_ipi(const struct cpumask *mask) |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 430 | { |
| 431 | int cpu; |
| 432 | |
| 433 | xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR); |
| 434 | |
| 435 | /* Make sure other vcpus get a chance to run if they need to. */ |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 436 | for_each_cpu(cpu, mask) { |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 437 | if (xen_vcpu_stolen(cpu)) { |
Hannes Eder | 1207cf8e | 2009-03-05 20:13:57 +0100 | [diff] [blame] | 438 | HYPERVISOR_sched_op(SCHEDOP_yield, NULL); |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 444 | static void xen_smp_send_call_function_single_ipi(int cpu) |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 445 | { |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 446 | xen_send_IPI_mask(cpumask_of(cpu), |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 447 | XEN_CALL_FUNCTION_SINGLE_VECTOR); |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 450 | static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id) |
| 451 | { |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 452 | irq_enter(); |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 453 | generic_smp_call_function_interrupt(); |
Brian Gerst | 1b437c8 | 2009-01-19 00:38:57 +0900 | [diff] [blame] | 454 | inc_irq_stat(irq_call_count); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 455 | irq_exit(); |
| 456 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 457 | return IRQ_HANDLED; |
| 458 | } |
| 459 | |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 460 | static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 461 | { |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 462 | irq_enter(); |
| 463 | generic_smp_call_function_single_interrupt(); |
Brian Gerst | 1b437c8 | 2009-01-19 00:38:57 +0900 | [diff] [blame] | 464 | inc_irq_stat(irq_call_count); |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 465 | irq_exit(); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 466 | |
Jens Axboe | 3b16cf8 | 2008-06-26 11:21:54 +0200 | [diff] [blame] | 467 | return IRQ_HANDLED; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 468 | } |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 469 | |
| 470 | static const struct smp_ops xen_smp_ops __initdata = { |
| 471 | .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu, |
| 472 | .smp_prepare_cpus = xen_smp_prepare_cpus, |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 473 | .smp_cpus_done = xen_smp_cpus_done, |
| 474 | |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 475 | .cpu_up = xen_cpu_up, |
| 476 | .cpu_die = xen_cpu_die, |
| 477 | .cpu_disable = xen_cpu_disable, |
| 478 | .play_dead = xen_play_dead, |
| 479 | |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 480 | .smp_send_stop = xen_smp_send_stop, |
| 481 | .smp_send_reschedule = xen_smp_send_reschedule, |
| 482 | |
| 483 | .send_call_func_ipi = xen_smp_send_call_function_ipi, |
| 484 | .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi, |
| 485 | }; |
| 486 | |
| 487 | void __init xen_smp_init(void) |
| 488 | { |
| 489 | smp_ops = xen_smp_ops; |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 490 | xen_init_spinlocks(); |
Jeremy Fitzhardinge | a9e7062 | 2008-07-08 15:06:41 -0700 | [diff] [blame] | 491 | } |