smp.c 12 KB

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