smp.c 11 KB

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