smp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. xen_init_lock_cpu(0);
  150. smp_store_cpu_info(0);
  151. cpu_data(0).x86_max_cores = 1;
  152. set_cpu_sibling_map(0);
  153. if (xen_smp_intr_init(0))
  154. BUG();
  155. xen_cpu_initialized_map = cpumask_of_cpu(0);
  156. /* Restrict the possible_map according to max_cpus. */
  157. while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
  158. for (cpu = NR_CPUS - 1; !cpu_possible(cpu); cpu--)
  159. continue;
  160. cpu_clear(cpu, cpu_possible_map);
  161. }
  162. for_each_possible_cpu (cpu) {
  163. struct task_struct *idle;
  164. if (cpu == 0)
  165. continue;
  166. idle = fork_idle(cpu);
  167. if (IS_ERR(idle))
  168. panic("failed fork for CPU %d", cpu);
  169. cpu_set(cpu, cpu_present_map);
  170. }
  171. //init_xenbus_allowed_cpumask();
  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. if (cpu_test_and_set(cpu, xen_cpu_initialized_map))
  179. return 0;
  180. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  181. if (ctxt == NULL)
  182. return -ENOMEM;
  183. gdt = get_cpu_gdt_table(cpu);
  184. ctxt->flags = VGCF_IN_KERNEL;
  185. ctxt->user_regs.ds = __USER_DS;
  186. ctxt->user_regs.es = __USER_DS;
  187. ctxt->user_regs.ss = __KERNEL_DS;
  188. #ifdef CONFIG_X86_32
  189. ctxt->user_regs.fs = __KERNEL_PERCPU;
  190. #endif
  191. ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
  192. ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
  193. memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
  194. xen_copy_trap_info(ctxt->trap_ctxt);
  195. ctxt->ldt_ents = 0;
  196. BUG_ON((unsigned long)gdt & ~PAGE_MASK);
  197. make_lowmem_page_readonly(gdt);
  198. ctxt->gdt_frames[0] = virt_to_mfn(gdt);
  199. ctxt->gdt_ents = GDT_ENTRIES;
  200. ctxt->user_regs.cs = __KERNEL_CS;
  201. ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
  202. ctxt->kernel_ss = __KERNEL_DS;
  203. ctxt->kernel_sp = idle->thread.sp0;
  204. #ifdef CONFIG_X86_32
  205. ctxt->event_callback_cs = __KERNEL_CS;
  206. ctxt->failsafe_callback_cs = __KERNEL_CS;
  207. #endif
  208. ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
  209. ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
  210. per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
  211. ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
  212. if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
  213. BUG();
  214. kfree(ctxt);
  215. return 0;
  216. }
  217. static int __cpuinit xen_cpu_up(unsigned int cpu)
  218. {
  219. struct task_struct *idle = idle_task(cpu);
  220. int rc;
  221. #if 0
  222. rc = cpu_up_check(cpu);
  223. if (rc)
  224. return rc;
  225. #endif
  226. #ifdef CONFIG_X86_64
  227. /* Allocate node local memory for AP pdas */
  228. WARN_ON(cpu == 0);
  229. if (cpu > 0) {
  230. rc = get_local_pda(cpu);
  231. if (rc)
  232. return rc;
  233. }
  234. #endif
  235. #ifdef CONFIG_X86_32
  236. init_gdt(cpu);
  237. per_cpu(current_task, cpu) = idle;
  238. irq_ctx_init(cpu);
  239. #else
  240. cpu_pda(cpu)->pcurrent = idle;
  241. clear_tsk_thread_flag(idle, TIF_FORK);
  242. #endif
  243. xen_setup_timer(cpu);
  244. xen_init_lock_cpu(cpu);
  245. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  246. /* make sure interrupts start blocked */
  247. per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
  248. rc = cpu_initialize_context(cpu, idle);
  249. if (rc)
  250. return rc;
  251. if (num_online_cpus() == 1)
  252. alternatives_smp_switch(1);
  253. rc = xen_smp_intr_init(cpu);
  254. if (rc)
  255. return rc;
  256. rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
  257. BUG_ON(rc);
  258. while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
  259. HYPERVISOR_sched_op(SCHEDOP_yield, 0);
  260. barrier();
  261. }
  262. return 0;
  263. }
  264. static void xen_smp_cpus_done(unsigned int max_cpus)
  265. {
  266. }
  267. static void stop_self(void *v)
  268. {
  269. int cpu = smp_processor_id();
  270. /* make sure we're not pinning something down */
  271. load_cr3(swapper_pg_dir);
  272. /* should set up a minimal gdt */
  273. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
  274. BUG();
  275. }
  276. static void xen_smp_send_stop(void)
  277. {
  278. smp_call_function(stop_self, NULL, 0);
  279. }
  280. static void xen_smp_send_reschedule(int cpu)
  281. {
  282. xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
  283. }
  284. static void xen_send_IPI_mask(cpumask_t mask, enum ipi_vector vector)
  285. {
  286. unsigned cpu;
  287. cpus_and(mask, mask, cpu_online_map);
  288. for_each_cpu_mask_nr(cpu, mask)
  289. xen_send_IPI_one(cpu, vector);
  290. }
  291. static void xen_smp_send_call_function_ipi(cpumask_t mask)
  292. {
  293. int cpu;
  294. xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
  295. /* Make sure other vcpus get a chance to run if they need to. */
  296. for_each_cpu_mask_nr(cpu, mask) {
  297. if (xen_vcpu_stolen(cpu)) {
  298. HYPERVISOR_sched_op(SCHEDOP_yield, 0);
  299. break;
  300. }
  301. }
  302. }
  303. static void xen_smp_send_call_function_single_ipi(int cpu)
  304. {
  305. xen_send_IPI_mask(cpumask_of_cpu(cpu), XEN_CALL_FUNCTION_SINGLE_VECTOR);
  306. }
  307. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
  308. {
  309. irq_enter();
  310. generic_smp_call_function_interrupt();
  311. #ifdef CONFIG_X86_32
  312. __get_cpu_var(irq_stat).irq_call_count++;
  313. #else
  314. add_pda(irq_call_count, 1);
  315. #endif
  316. irq_exit();
  317. return IRQ_HANDLED;
  318. }
  319. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
  320. {
  321. irq_enter();
  322. generic_smp_call_function_single_interrupt();
  323. #ifdef CONFIG_X86_32
  324. __get_cpu_var(irq_stat).irq_call_count++;
  325. #else
  326. add_pda(irq_call_count, 1);
  327. #endif
  328. irq_exit();
  329. return IRQ_HANDLED;
  330. }
  331. static const struct smp_ops xen_smp_ops __initdata = {
  332. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  333. .smp_prepare_cpus = xen_smp_prepare_cpus,
  334. .cpu_up = xen_cpu_up,
  335. .smp_cpus_done = xen_smp_cpus_done,
  336. .smp_send_stop = xen_smp_send_stop,
  337. .smp_send_reschedule = xen_smp_send_reschedule,
  338. .send_call_func_ipi = xen_smp_send_call_function_ipi,
  339. .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
  340. };
  341. void __init xen_smp_init(void)
  342. {
  343. smp_ops = xen_smp_ops;
  344. xen_fill_possible_map();
  345. xen_init_spinlocks();
  346. }