smp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. xen_enable_sysenter();
  58. preempt_disable();
  59. per_cpu(cpu_state, cpu) = CPU_ONLINE;
  60. xen_setup_cpu_clockevents();
  61. /* We can take interrupts now: we're officially "up". */
  62. local_irq_enable();
  63. wmb(); /* make sure everything is out */
  64. cpu_idle();
  65. }
  66. static int xen_smp_intr_init(unsigned int cpu)
  67. {
  68. int rc;
  69. const char *resched_name, *callfunc_name, *debug_name;
  70. resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
  71. rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
  72. cpu,
  73. xen_reschedule_interrupt,
  74. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  75. resched_name,
  76. NULL);
  77. if (rc < 0)
  78. goto fail;
  79. per_cpu(resched_irq, cpu) = rc;
  80. callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
  81. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
  82. cpu,
  83. xen_call_function_interrupt,
  84. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  85. callfunc_name,
  86. NULL);
  87. if (rc < 0)
  88. goto fail;
  89. per_cpu(callfunc_irq, cpu) = rc;
  90. debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
  91. rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
  92. IRQF_DISABLED | IRQF_PERCPU | IRQF_NOBALANCING,
  93. debug_name, NULL);
  94. if (rc < 0)
  95. goto fail;
  96. per_cpu(debug_irq, cpu) = rc;
  97. callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
  98. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
  99. cpu,
  100. xen_call_function_single_interrupt,
  101. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  102. callfunc_name,
  103. NULL);
  104. if (rc < 0)
  105. goto fail;
  106. per_cpu(callfuncsingle_irq, cpu) = rc;
  107. return 0;
  108. fail:
  109. if (per_cpu(resched_irq, cpu) >= 0)
  110. unbind_from_irqhandler(per_cpu(resched_irq, cpu), NULL);
  111. if (per_cpu(callfunc_irq, cpu) >= 0)
  112. unbind_from_irqhandler(per_cpu(callfunc_irq, cpu), NULL);
  113. if (per_cpu(debug_irq, cpu) >= 0)
  114. unbind_from_irqhandler(per_cpu(debug_irq, cpu), NULL);
  115. if (per_cpu(callfuncsingle_irq, cpu) >= 0)
  116. unbind_from_irqhandler(per_cpu(callfuncsingle_irq, cpu), NULL);
  117. return rc;
  118. }
  119. void __init xen_fill_possible_map(void)
  120. {
  121. int i, rc;
  122. for (i = 0; i < NR_CPUS; i++) {
  123. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  124. if (rc >= 0)
  125. cpu_set(i, cpu_possible_map);
  126. }
  127. }
  128. void __init xen_smp_prepare_boot_cpu(void)
  129. {
  130. int cpu;
  131. BUG_ON(smp_processor_id() != 0);
  132. native_smp_prepare_boot_cpu();
  133. /* We've switched to the "real" per-cpu gdt, so make sure the
  134. old memory can be recycled */
  135. make_lowmem_page_readwrite(&per_cpu__gdt_page);
  136. for_each_possible_cpu(cpu) {
  137. cpus_clear(per_cpu(cpu_sibling_map, cpu));
  138. /*
  139. * cpu_core_map lives in a per cpu area that is cleared
  140. * when the per cpu array is allocated.
  141. *
  142. * cpus_clear(per_cpu(cpu_core_map, cpu));
  143. */
  144. }
  145. xen_setup_vcpu_info_placement();
  146. }
  147. void __init xen_smp_prepare_cpus(unsigned int max_cpus)
  148. {
  149. unsigned cpu;
  150. for_each_possible_cpu(cpu) {
  151. cpus_clear(per_cpu(cpu_sibling_map, cpu));
  152. /*
  153. * cpu_core_ map will be zeroed when the per
  154. * cpu area is allocated.
  155. *
  156. * cpus_clear(per_cpu(cpu_core_map, cpu));
  157. */
  158. }
  159. smp_store_cpu_info(0);
  160. set_cpu_sibling_map(0);
  161. if (xen_smp_intr_init(0))
  162. BUG();
  163. xen_cpu_initialized_map = cpumask_of_cpu(0);
  164. /* Restrict the possible_map according to max_cpus. */
  165. while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
  166. for (cpu = NR_CPUS - 1; !cpu_possible(cpu); cpu--)
  167. continue;
  168. cpu_clear(cpu, cpu_possible_map);
  169. }
  170. for_each_possible_cpu (cpu) {
  171. struct task_struct *idle;
  172. if (cpu == 0)
  173. continue;
  174. idle = fork_idle(cpu);
  175. if (IS_ERR(idle))
  176. panic("failed fork for CPU %d", cpu);
  177. cpu_set(cpu, cpu_present_map);
  178. }
  179. //init_xenbus_allowed_cpumask();
  180. }
  181. static __cpuinit int
  182. cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
  183. {
  184. struct vcpu_guest_context *ctxt;
  185. struct gdt_page *gdt = &per_cpu(gdt_page, cpu);
  186. if (cpu_test_and_set(cpu, xen_cpu_initialized_map))
  187. return 0;
  188. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  189. if (ctxt == NULL)
  190. return -ENOMEM;
  191. ctxt->flags = VGCF_IN_KERNEL;
  192. ctxt->user_regs.ds = __USER_DS;
  193. ctxt->user_regs.es = __USER_DS;
  194. ctxt->user_regs.fs = __KERNEL_PERCPU;
  195. ctxt->user_regs.gs = 0;
  196. ctxt->user_regs.ss = __KERNEL_DS;
  197. ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
  198. ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
  199. memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
  200. xen_copy_trap_info(ctxt->trap_ctxt);
  201. ctxt->ldt_ents = 0;
  202. BUG_ON((unsigned long)gdt->gdt & ~PAGE_MASK);
  203. make_lowmem_page_readonly(gdt->gdt);
  204. ctxt->gdt_frames[0] = virt_to_mfn(gdt->gdt);
  205. ctxt->gdt_ents = ARRAY_SIZE(gdt->gdt);
  206. ctxt->user_regs.cs = __KERNEL_CS;
  207. ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
  208. ctxt->kernel_ss = __KERNEL_DS;
  209. ctxt->kernel_sp = idle->thread.sp0;
  210. ctxt->event_callback_cs = __KERNEL_CS;
  211. ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
  212. ctxt->failsafe_callback_cs = __KERNEL_CS;
  213. ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
  214. per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
  215. ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
  216. if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
  217. BUG();
  218. kfree(ctxt);
  219. return 0;
  220. }
  221. int __cpuinit xen_cpu_up(unsigned int cpu)
  222. {
  223. struct task_struct *idle = idle_task(cpu);
  224. int rc;
  225. #if 0
  226. rc = cpu_up_check(cpu);
  227. if (rc)
  228. return rc;
  229. #endif
  230. init_gdt(cpu);
  231. per_cpu(current_task, cpu) = idle;
  232. irq_ctx_init(cpu);
  233. xen_setup_timer(cpu);
  234. /* make sure interrupts start blocked */
  235. per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
  236. rc = cpu_initialize_context(cpu, idle);
  237. if (rc)
  238. return rc;
  239. if (num_online_cpus() == 1)
  240. alternatives_smp_switch(1);
  241. rc = xen_smp_intr_init(cpu);
  242. if (rc)
  243. return rc;
  244. smp_store_cpu_info(cpu);
  245. set_cpu_sibling_map(cpu);
  246. /* This must be done before setting cpu_online_map */
  247. wmb();
  248. cpu_set(cpu, cpu_online_map);
  249. rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
  250. BUG_ON(rc);
  251. return 0;
  252. }
  253. void xen_smp_cpus_done(unsigned int max_cpus)
  254. {
  255. }
  256. static void stop_self(void *v)
  257. {
  258. int cpu = smp_processor_id();
  259. /* make sure we're not pinning something down */
  260. load_cr3(swapper_pg_dir);
  261. /* should set up a minimal gdt */
  262. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
  263. BUG();
  264. }
  265. void xen_smp_send_stop(void)
  266. {
  267. smp_call_function(stop_self, NULL, 0);
  268. }
  269. void xen_smp_send_reschedule(int cpu)
  270. {
  271. xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
  272. }
  273. static void xen_send_IPI_mask(cpumask_t mask, enum ipi_vector vector)
  274. {
  275. unsigned cpu;
  276. cpus_and(mask, mask, cpu_online_map);
  277. for_each_cpu_mask(cpu, mask)
  278. xen_send_IPI_one(cpu, vector);
  279. }
  280. void xen_smp_send_call_function_ipi(cpumask_t mask)
  281. {
  282. int cpu;
  283. xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
  284. /* Make sure other vcpus get a chance to run if they need to. */
  285. for_each_cpu_mask(cpu, mask) {
  286. if (xen_vcpu_stolen(cpu)) {
  287. HYPERVISOR_sched_op(SCHEDOP_yield, 0);
  288. break;
  289. }
  290. }
  291. }
  292. void xen_smp_send_call_function_single_ipi(int cpu)
  293. {
  294. xen_send_IPI_mask(cpumask_of_cpu(cpu), XEN_CALL_FUNCTION_SINGLE_VECTOR);
  295. }
  296. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
  297. {
  298. irq_enter();
  299. generic_smp_call_function_interrupt();
  300. __get_cpu_var(irq_stat).irq_call_count++;
  301. irq_exit();
  302. return IRQ_HANDLED;
  303. }
  304. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
  305. {
  306. irq_enter();
  307. generic_smp_call_function_single_interrupt();
  308. __get_cpu_var(irq_stat).irq_call_count++;
  309. irq_exit();
  310. return IRQ_HANDLED;
  311. }