smp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. * This does not handle HOTPLUG_CPU yet.
  16. */
  17. #include <linux/sched.h>
  18. #include <linux/err.h>
  19. #include <linux/smp.h>
  20. #include <asm/paravirt.h>
  21. #include <asm/desc.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/cpu.h>
  24. #include <xen/interface/xen.h>
  25. #include <xen/interface/vcpu.h>
  26. #include <asm/xen/interface.h>
  27. #include <asm/xen/hypercall.h>
  28. #include <xen/page.h>
  29. #include <xen/events.h>
  30. #include "xen-ops.h"
  31. #include "mmu.h"
  32. cpumask_t xen_cpu_initialized_map;
  33. static DEFINE_PER_CPU(int, resched_irq);
  34. static DEFINE_PER_CPU(int, callfunc_irq);
  35. static DEFINE_PER_CPU(int, callfuncsingle_irq);
  36. static DEFINE_PER_CPU(int, 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. Nothing to do,
  41. * all the work is done automatically when
  42. * we return from the interrupt.
  43. */
  44. static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
  45. {
  46. #ifdef CONFIG_X86_32
  47. __get_cpu_var(irq_stat).irq_resched_count++;
  48. #else
  49. add_pda(irq_resched_count, 1);
  50. #endif
  51. return IRQ_HANDLED;
  52. }
  53. static __cpuinit void cpu_bringup_and_idle(void)
  54. {
  55. int cpu = smp_processor_id();
  56. cpu_init();
  57. preempt_disable();
  58. xen_enable_sysenter();
  59. xen_enable_syscall();
  60. cpu = smp_processor_id();
  61. smp_store_cpu_info(cpu);
  62. cpu_data(cpu).x86_max_cores = 1;
  63. set_cpu_sibling_map(cpu);
  64. xen_setup_cpu_clockevents();
  65. cpu_set(cpu, cpu_online_map);
  66. x86_write_percpu(cpu_state, CPU_ONLINE);
  67. wmb();
  68. /* We can take interrupts now: we're officially "up". */
  69. local_irq_enable();
  70. wmb(); /* make sure everything is out */
  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(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(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(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(callfuncsingle_irq, cpu) = rc;
  114. return 0;
  115. fail:
  116. if (per_cpu(resched_irq, cpu) >= 0)
  117. unbind_from_irqhandler(per_cpu(resched_irq, cpu), NULL);
  118. if (per_cpu(callfunc_irq, cpu) >= 0)
  119. unbind_from_irqhandler(per_cpu(callfunc_irq, cpu), NULL);
  120. if (per_cpu(debug_irq, cpu) >= 0)
  121. unbind_from_irqhandler(per_cpu(debug_irq, cpu), NULL);
  122. if (per_cpu(callfuncsingle_irq, cpu) >= 0)
  123. unbind_from_irqhandler(per_cpu(callfuncsingle_irq, cpu), NULL);
  124. return rc;
  125. }
  126. static void __init xen_fill_possible_map(void)
  127. {
  128. int i, rc;
  129. for (i = 0; i < NR_CPUS; i++) {
  130. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  131. if (rc >= 0) {
  132. num_processors++;
  133. cpu_set(i, cpu_possible_map);
  134. }
  135. }
  136. }
  137. static void __init xen_smp_prepare_boot_cpu(void)
  138. {
  139. BUG_ON(smp_processor_id() != 0);
  140. native_smp_prepare_boot_cpu();
  141. /* We've switched to the "real" per-cpu gdt, so make sure the
  142. old memory can be recycled */
  143. make_lowmem_page_readwrite(&per_cpu_var(gdt_page));
  144. xen_setup_vcpu_info_placement();
  145. }
  146. static void __init xen_smp_prepare_cpus(unsigned int max_cpus)
  147. {
  148. unsigned cpu;
  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. xen_cpu_initialized_map = cpumask_of_cpu(0);
  155. /* Restrict the possible_map according to max_cpus. */
  156. while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
  157. for (cpu = NR_CPUS - 1; !cpu_possible(cpu); cpu--)
  158. continue;
  159. cpu_clear(cpu, cpu_possible_map);
  160. }
  161. for_each_possible_cpu (cpu) {
  162. struct task_struct *idle;
  163. if (cpu == 0)
  164. continue;
  165. idle = fork_idle(cpu);
  166. if (IS_ERR(idle))
  167. panic("failed fork for CPU %d", cpu);
  168. cpu_set(cpu, cpu_present_map);
  169. }
  170. //init_xenbus_allowed_cpumask();
  171. }
  172. static __cpuinit int
  173. cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
  174. {
  175. struct vcpu_guest_context *ctxt;
  176. struct desc_struct *gdt;
  177. if (cpu_test_and_set(cpu, xen_cpu_initialized_map))
  178. return 0;
  179. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  180. if (ctxt == NULL)
  181. return -ENOMEM;
  182. gdt = get_cpu_gdt_table(cpu);
  183. ctxt->flags = VGCF_IN_KERNEL;
  184. ctxt->user_regs.ds = __USER_DS;
  185. ctxt->user_regs.es = __USER_DS;
  186. ctxt->user_regs.ss = __KERNEL_DS;
  187. #ifdef CONFIG_X86_32
  188. ctxt->user_regs.fs = __KERNEL_PERCPU;
  189. #endif
  190. ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
  191. ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
  192. memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
  193. xen_copy_trap_info(ctxt->trap_ctxt);
  194. ctxt->ldt_ents = 0;
  195. BUG_ON((unsigned long)gdt & ~PAGE_MASK);
  196. make_lowmem_page_readonly(gdt);
  197. ctxt->gdt_frames[0] = virt_to_mfn(gdt);
  198. ctxt->gdt_ents = GDT_ENTRIES;
  199. ctxt->user_regs.cs = __KERNEL_CS;
  200. ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
  201. ctxt->kernel_ss = __KERNEL_DS;
  202. ctxt->kernel_sp = idle->thread.sp0;
  203. #ifdef CONFIG_X86_32
  204. ctxt->event_callback_cs = __KERNEL_CS;
  205. ctxt->failsafe_callback_cs = __KERNEL_CS;
  206. #endif
  207. ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
  208. ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
  209. per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
  210. ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
  211. if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
  212. BUG();
  213. kfree(ctxt);
  214. return 0;
  215. }
  216. static int __cpuinit xen_cpu_up(unsigned int cpu)
  217. {
  218. struct task_struct *idle = idle_task(cpu);
  219. int rc;
  220. #if 0
  221. rc = cpu_up_check(cpu);
  222. if (rc)
  223. return rc;
  224. #endif
  225. #ifdef CONFIG_X86_64
  226. /* Allocate node local memory for AP pdas */
  227. WARN_ON(cpu == 0);
  228. if (cpu > 0) {
  229. rc = get_local_pda(cpu);
  230. if (rc)
  231. return rc;
  232. }
  233. #endif
  234. #ifdef CONFIG_X86_32
  235. init_gdt(cpu);
  236. per_cpu(current_task, cpu) = idle;
  237. irq_ctx_init(cpu);
  238. #else
  239. cpu_pda(cpu)->pcurrent = idle;
  240. clear_tsk_thread_flag(idle, TIF_FORK);
  241. #endif
  242. xen_setup_timer(cpu);
  243. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  244. /* make sure interrupts start blocked */
  245. per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
  246. rc = cpu_initialize_context(cpu, idle);
  247. if (rc)
  248. return rc;
  249. if (num_online_cpus() == 1)
  250. alternatives_smp_switch(1);
  251. rc = xen_smp_intr_init(cpu);
  252. if (rc)
  253. return rc;
  254. rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
  255. BUG_ON(rc);
  256. while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
  257. HYPERVISOR_sched_op(SCHEDOP_yield, 0);
  258. barrier();
  259. }
  260. return 0;
  261. }
  262. static void xen_smp_cpus_done(unsigned int max_cpus)
  263. {
  264. }
  265. static void stop_self(void *v)
  266. {
  267. int cpu = smp_processor_id();
  268. /* make sure we're not pinning something down */
  269. load_cr3(swapper_pg_dir);
  270. /* should set up a minimal gdt */
  271. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
  272. BUG();
  273. }
  274. static void xen_smp_send_stop(void)
  275. {
  276. smp_call_function(stop_self, NULL, 0);
  277. }
  278. static void xen_smp_send_reschedule(int cpu)
  279. {
  280. xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
  281. }
  282. static void xen_send_IPI_mask(cpumask_t mask, enum ipi_vector vector)
  283. {
  284. unsigned cpu;
  285. cpus_and(mask, mask, cpu_online_map);
  286. for_each_cpu_mask(cpu, mask)
  287. xen_send_IPI_one(cpu, vector);
  288. }
  289. static void xen_smp_send_call_function_ipi(cpumask_t mask)
  290. {
  291. int cpu;
  292. xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
  293. /* Make sure other vcpus get a chance to run if they need to. */
  294. for_each_cpu_mask(cpu, mask) {
  295. if (xen_vcpu_stolen(cpu)) {
  296. HYPERVISOR_sched_op(SCHEDOP_yield, 0);
  297. break;
  298. }
  299. }
  300. }
  301. static void xen_smp_send_call_function_single_ipi(int cpu)
  302. {
  303. xen_send_IPI_mask(cpumask_of_cpu(cpu), XEN_CALL_FUNCTION_SINGLE_VECTOR);
  304. }
  305. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
  306. {
  307. irq_enter();
  308. generic_smp_call_function_interrupt();
  309. #ifdef CONFIG_X86_32
  310. __get_cpu_var(irq_stat).irq_call_count++;
  311. #else
  312. add_pda(irq_call_count, 1);
  313. #endif
  314. irq_exit();
  315. return IRQ_HANDLED;
  316. }
  317. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
  318. {
  319. irq_enter();
  320. generic_smp_call_function_single_interrupt();
  321. #ifdef CONFIG_X86_32
  322. __get_cpu_var(irq_stat).irq_call_count++;
  323. #else
  324. add_pda(irq_call_count, 1);
  325. #endif
  326. irq_exit();
  327. return IRQ_HANDLED;
  328. }
  329. static const struct smp_ops xen_smp_ops __initdata = {
  330. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  331. .smp_prepare_cpus = xen_smp_prepare_cpus,
  332. .cpu_up = xen_cpu_up,
  333. .smp_cpus_done = xen_smp_cpus_done,
  334. .smp_send_stop = xen_smp_send_stop,
  335. .smp_send_reschedule = xen_smp_send_reschedule,
  336. .send_call_func_ipi = xen_smp_send_call_function_ipi,
  337. .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
  338. };
  339. void __init xen_smp_init(void)
  340. {
  341. smp_ops = xen_smp_ops;
  342. xen_fill_possible_map();
  343. }