smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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/smp.h>
  18. #include <asm/paravirt.h>
  19. #include <asm/desc.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/cpu.h>
  22. #include <xen/interface/xen.h>
  23. #include <xen/interface/vcpu.h>
  24. #include <asm/xen/interface.h>
  25. #include <asm/xen/hypercall.h>
  26. #include <xen/page.h>
  27. #include <xen/events.h>
  28. #include "xen-ops.h"
  29. #include "mmu.h"
  30. cpumask_var_t xen_cpu_initialized_map;
  31. static DEFINE_PER_CPU(int, resched_irq);
  32. static DEFINE_PER_CPU(int, callfunc_irq);
  33. static DEFINE_PER_CPU(int, callfuncsingle_irq);
  34. static DEFINE_PER_CPU(int, debug_irq) = -1;
  35. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
  36. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
  37. /*
  38. * Reschedule call back. Nothing to do,
  39. * all the work is done automatically when
  40. * we return from the interrupt.
  41. */
  42. static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
  43. {
  44. inc_irq_stat(irq_resched_count);
  45. return IRQ_HANDLED;
  46. }
  47. static __cpuinit void cpu_bringup(void)
  48. {
  49. int cpu = smp_processor_id();
  50. cpu_init();
  51. touch_softlockup_watchdog();
  52. preempt_disable();
  53. xen_enable_sysenter();
  54. xen_enable_syscall();
  55. cpu = smp_processor_id();
  56. smp_store_cpu_info(cpu);
  57. cpu_data(cpu).x86_max_cores = 1;
  58. set_cpu_sibling_map(cpu);
  59. xen_setup_cpu_clockevents();
  60. cpu_set(cpu, cpu_online_map);
  61. percpu_write(cpu_state, CPU_ONLINE);
  62. wmb();
  63. /* We can take interrupts now: we're officially "up". */
  64. local_irq_enable();
  65. wmb(); /* make sure everything is out */
  66. }
  67. static __cpuinit void cpu_bringup_and_idle(void)
  68. {
  69. cpu_bringup();
  70. cpu_idle();
  71. }
  72. static int xen_smp_intr_init(unsigned int cpu)
  73. {
  74. int rc;
  75. const char *resched_name, *callfunc_name, *debug_name;
  76. resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
  77. rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
  78. cpu,
  79. xen_reschedule_interrupt,
  80. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  81. resched_name,
  82. NULL);
  83. if (rc < 0)
  84. goto fail;
  85. per_cpu(resched_irq, cpu) = rc;
  86. callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
  87. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
  88. cpu,
  89. xen_call_function_interrupt,
  90. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  91. callfunc_name,
  92. NULL);
  93. if (rc < 0)
  94. goto fail;
  95. per_cpu(callfunc_irq, cpu) = rc;
  96. debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
  97. rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
  98. IRQF_DISABLED | IRQF_PERCPU | IRQF_NOBALANCING,
  99. debug_name, NULL);
  100. if (rc < 0)
  101. goto fail;
  102. per_cpu(debug_irq, cpu) = rc;
  103. callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
  104. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
  105. cpu,
  106. xen_call_function_single_interrupt,
  107. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  108. callfunc_name,
  109. NULL);
  110. if (rc < 0)
  111. goto fail;
  112. per_cpu(callfuncsingle_irq, cpu) = rc;
  113. return 0;
  114. fail:
  115. if (per_cpu(resched_irq, cpu) >= 0)
  116. unbind_from_irqhandler(per_cpu(resched_irq, cpu), NULL);
  117. if (per_cpu(callfunc_irq, cpu) >= 0)
  118. unbind_from_irqhandler(per_cpu(callfunc_irq, cpu), NULL);
  119. if (per_cpu(debug_irq, cpu) >= 0)
  120. unbind_from_irqhandler(per_cpu(debug_irq, cpu), NULL);
  121. if (per_cpu(callfuncsingle_irq, cpu) >= 0)
  122. unbind_from_irqhandler(per_cpu(callfuncsingle_irq, cpu), NULL);
  123. return rc;
  124. }
  125. static void __init xen_fill_possible_map(void)
  126. {
  127. int i, rc;
  128. for (i = 0; i < nr_cpu_ids; i++) {
  129. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  130. if (rc >= 0) {
  131. num_processors++;
  132. set_cpu_possible(i, true);
  133. }
  134. }
  135. }
  136. static void __init xen_smp_prepare_boot_cpu(void)
  137. {
  138. BUG_ON(smp_processor_id() != 0);
  139. native_smp_prepare_boot_cpu();
  140. /* We've switched to the "real" per-cpu gdt, so make sure the
  141. old memory can be recycled */
  142. make_lowmem_page_readwrite(xen_initial_gdt);
  143. xen_setup_vcpu_info_placement();
  144. }
  145. static void __init xen_smp_prepare_cpus(unsigned int max_cpus)
  146. {
  147. unsigned cpu;
  148. xen_init_lock_cpu(0);
  149. smp_store_cpu_info(0);
  150. cpu_data(0).x86_max_cores = 1;
  151. set_cpu_sibling_map(0);
  152. if (xen_smp_intr_init(0))
  153. BUG();
  154. if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
  155. panic("could not allocate xen_cpu_initialized_map\n");
  156. cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
  157. /* Restrict the possible_map according to max_cpus. */
  158. while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
  159. for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
  160. continue;
  161. set_cpu_possible(cpu, false);
  162. }
  163. for_each_possible_cpu (cpu) {
  164. struct task_struct *idle;
  165. if (cpu == 0)
  166. continue;
  167. idle = fork_idle(cpu);
  168. if (IS_ERR(idle))
  169. panic("failed fork for CPU %d", cpu);
  170. set_cpu_present(cpu, true);
  171. }
  172. }
  173. static __cpuinit int
  174. cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
  175. {
  176. struct vcpu_guest_context *ctxt;
  177. struct desc_struct *gdt;
  178. unsigned long gdt_mfn;
  179. if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
  180. return 0;
  181. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  182. if (ctxt == NULL)
  183. return -ENOMEM;
  184. gdt = get_cpu_gdt_table(cpu);
  185. ctxt->flags = VGCF_IN_KERNEL;
  186. ctxt->user_regs.ds = __USER_DS;
  187. ctxt->user_regs.es = __USER_DS;
  188. ctxt->user_regs.ss = __KERNEL_DS;
  189. #ifdef CONFIG_X86_32
  190. ctxt->user_regs.fs = __KERNEL_PERCPU;
  191. #else
  192. ctxt->gs_base_kernel = per_cpu_offset(cpu);
  193. #endif
  194. ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
  195. ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
  196. memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
  197. xen_copy_trap_info(ctxt->trap_ctxt);
  198. ctxt->ldt_ents = 0;
  199. BUG_ON((unsigned long)gdt & ~PAGE_MASK);
  200. gdt_mfn = arbitrary_virt_to_mfn(gdt);
  201. make_lowmem_page_readonly(gdt);
  202. make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
  203. ctxt->gdt_frames[0] = gdt_mfn;
  204. ctxt->gdt_ents = GDT_ENTRIES;
  205. ctxt->user_regs.cs = __KERNEL_CS;
  206. ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
  207. ctxt->kernel_ss = __KERNEL_DS;
  208. ctxt->kernel_sp = idle->thread.sp0;
  209. #ifdef CONFIG_X86_32
  210. ctxt->event_callback_cs = __KERNEL_CS;
  211. ctxt->failsafe_callback_cs = __KERNEL_CS;
  212. #endif
  213. ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
  214. ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
  215. per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
  216. ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
  217. if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
  218. BUG();
  219. kfree(ctxt);
  220. return 0;
  221. }
  222. static int __cpuinit xen_cpu_up(unsigned int cpu)
  223. {
  224. struct task_struct *idle = idle_task(cpu);
  225. int rc;
  226. per_cpu(current_task, cpu) = idle;
  227. #ifdef CONFIG_X86_32
  228. irq_ctx_init(cpu);
  229. #else
  230. clear_tsk_thread_flag(idle, TIF_FORK);
  231. per_cpu(kernel_stack, cpu) =
  232. (unsigned long)task_stack_page(idle) -
  233. KERNEL_STACK_OFFSET + THREAD_SIZE;
  234. #endif
  235. xen_setup_timer(cpu);
  236. xen_init_lock_cpu(cpu);
  237. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  238. /* make sure interrupts start blocked */
  239. per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
  240. rc = cpu_initialize_context(cpu, idle);
  241. if (rc)
  242. return rc;
  243. if (num_online_cpus() == 1)
  244. alternatives_smp_switch(1);
  245. rc = xen_smp_intr_init(cpu);
  246. if (rc)
  247. return rc;
  248. rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
  249. BUG_ON(rc);
  250. while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
  251. HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
  252. barrier();
  253. }
  254. return 0;
  255. }
  256. static void xen_smp_cpus_done(unsigned int max_cpus)
  257. {
  258. }
  259. #ifdef CONFIG_HOTPLUG_CPU
  260. static int xen_cpu_disable(void)
  261. {
  262. unsigned int cpu = smp_processor_id();
  263. if (cpu == 0)
  264. return -EBUSY;
  265. cpu_disable_common();
  266. load_cr3(swapper_pg_dir);
  267. return 0;
  268. }
  269. static void xen_cpu_die(unsigned int cpu)
  270. {
  271. while (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) {
  272. current->state = TASK_UNINTERRUPTIBLE;
  273. schedule_timeout(HZ/10);
  274. }
  275. unbind_from_irqhandler(per_cpu(resched_irq, cpu), NULL);
  276. unbind_from_irqhandler(per_cpu(callfunc_irq, cpu), NULL);
  277. unbind_from_irqhandler(per_cpu(debug_irq, cpu), NULL);
  278. unbind_from_irqhandler(per_cpu(callfuncsingle_irq, cpu), NULL);
  279. xen_uninit_lock_cpu(cpu);
  280. xen_teardown_timer(cpu);
  281. if (num_online_cpus() == 1)
  282. alternatives_smp_switch(0);
  283. }
  284. static void __cpuinit xen_play_dead(void) /* used only with CPU_HOTPLUG */
  285. {
  286. play_dead_common();
  287. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  288. cpu_bringup();
  289. }
  290. #else /* !CONFIG_HOTPLUG_CPU */
  291. static int xen_cpu_disable(void)
  292. {
  293. return -ENOSYS;
  294. }
  295. static void xen_cpu_die(unsigned int cpu)
  296. {
  297. BUG();
  298. }
  299. static void xen_play_dead(void)
  300. {
  301. BUG();
  302. }
  303. #endif
  304. static void stop_self(void *v)
  305. {
  306. int cpu = smp_processor_id();
  307. /* make sure we're not pinning something down */
  308. load_cr3(swapper_pg_dir);
  309. /* should set up a minimal gdt */
  310. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
  311. BUG();
  312. }
  313. static void xen_smp_send_stop(void)
  314. {
  315. smp_call_function(stop_self, NULL, 0);
  316. }
  317. static void xen_smp_send_reschedule(int cpu)
  318. {
  319. xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
  320. }
  321. static void xen_send_IPI_mask(const struct cpumask *mask,
  322. enum ipi_vector vector)
  323. {
  324. unsigned cpu;
  325. for_each_cpu_and(cpu, mask, cpu_online_mask)
  326. xen_send_IPI_one(cpu, vector);
  327. }
  328. static void xen_smp_send_call_function_ipi(const struct cpumask *mask)
  329. {
  330. int cpu;
  331. xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
  332. /* Make sure other vcpus get a chance to run if they need to. */
  333. for_each_cpu(cpu, mask) {
  334. if (xen_vcpu_stolen(cpu)) {
  335. HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
  336. break;
  337. }
  338. }
  339. }
  340. static void xen_smp_send_call_function_single_ipi(int cpu)
  341. {
  342. xen_send_IPI_mask(cpumask_of(cpu),
  343. XEN_CALL_FUNCTION_SINGLE_VECTOR);
  344. }
  345. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
  346. {
  347. irq_enter();
  348. generic_smp_call_function_interrupt();
  349. inc_irq_stat(irq_call_count);
  350. irq_exit();
  351. return IRQ_HANDLED;
  352. }
  353. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
  354. {
  355. irq_enter();
  356. generic_smp_call_function_single_interrupt();
  357. inc_irq_stat(irq_call_count);
  358. irq_exit();
  359. return IRQ_HANDLED;
  360. }
  361. static const struct smp_ops xen_smp_ops __initdata = {
  362. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  363. .smp_prepare_cpus = xen_smp_prepare_cpus,
  364. .smp_cpus_done = xen_smp_cpus_done,
  365. .cpu_up = xen_cpu_up,
  366. .cpu_die = xen_cpu_die,
  367. .cpu_disable = xen_cpu_disable,
  368. .play_dead = xen_play_dead,
  369. .smp_send_stop = xen_smp_send_stop,
  370. .smp_send_reschedule = xen_smp_send_reschedule,
  371. .send_call_func_ipi = xen_smp_send_call_function_ipi,
  372. .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
  373. };
  374. void __init xen_smp_init(void)
  375. {
  376. smp_ops = xen_smp_ops;
  377. xen_fill_possible_map();
  378. xen_init_spinlocks();
  379. }