smp.c 9.6 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) = -1;
  34. static DEFINE_PER_CPU(int, callfunc_irq) = -1;
  35. static DEFINE_PER_CPU(int, debug_irq) = -1;
  36. /*
  37. * Structure and data for smp_call_function(). This is designed to minimise
  38. * static memory requirements. It also looks cleaner.
  39. */
  40. static DEFINE_SPINLOCK(call_lock);
  41. struct call_data_struct {
  42. void (*func) (void *info);
  43. void *info;
  44. atomic_t started;
  45. atomic_t finished;
  46. int wait;
  47. };
  48. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
  49. static struct call_data_struct *call_data;
  50. /*
  51. * Reschedule call back. Nothing to do,
  52. * all the work is done automatically when
  53. * we return from the interrupt.
  54. */
  55. static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
  56. {
  57. #ifdef CONFIG_X86_32
  58. __get_cpu_var(irq_stat).irq_resched_count++;
  59. #else
  60. add_pda(irq_resched_count, 1);
  61. #endif
  62. return IRQ_HANDLED;
  63. }
  64. static __cpuinit void cpu_bringup_and_idle(void)
  65. {
  66. int cpu = smp_processor_id();
  67. cpu_init();
  68. xen_enable_sysenter();
  69. preempt_disable();
  70. per_cpu(cpu_state, cpu) = CPU_ONLINE;
  71. xen_setup_cpu_clockevents();
  72. /* We can take interrupts now: we're officially "up". */
  73. local_irq_enable();
  74. wmb(); /* make sure everything is out */
  75. cpu_idle();
  76. }
  77. static int xen_smp_intr_init(unsigned int cpu)
  78. {
  79. int rc;
  80. const char *resched_name, *callfunc_name, *debug_name;
  81. resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
  82. rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
  83. cpu,
  84. xen_reschedule_interrupt,
  85. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  86. resched_name,
  87. NULL);
  88. if (rc < 0)
  89. goto fail;
  90. per_cpu(resched_irq, cpu) = rc;
  91. callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
  92. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
  93. cpu,
  94. xen_call_function_interrupt,
  95. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  96. callfunc_name,
  97. NULL);
  98. if (rc < 0)
  99. goto fail;
  100. per_cpu(callfunc_irq, cpu) = rc;
  101. debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
  102. rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
  103. IRQF_DISABLED | IRQF_PERCPU | IRQF_NOBALANCING,
  104. debug_name, NULL);
  105. if (rc < 0)
  106. goto fail;
  107. per_cpu(debug_irq, cpu) = rc;
  108. return 0;
  109. fail:
  110. if (per_cpu(resched_irq, cpu) >= 0)
  111. unbind_from_irqhandler(per_cpu(resched_irq, cpu), NULL);
  112. if (per_cpu(callfunc_irq, cpu) >= 0)
  113. unbind_from_irqhandler(per_cpu(callfunc_irq, cpu), NULL);
  114. if (per_cpu(debug_irq, cpu) >= 0)
  115. unbind_from_irqhandler(per_cpu(debug_irq, cpu), NULL);
  116. return rc;
  117. }
  118. void __init xen_fill_possible_map(void)
  119. {
  120. int i, rc;
  121. for (i = 0; i < NR_CPUS; i++) {
  122. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  123. if (rc >= 0)
  124. cpu_set(i, cpu_possible_map);
  125. }
  126. }
  127. void __init xen_smp_prepare_boot_cpu(void)
  128. {
  129. int cpu;
  130. BUG_ON(smp_processor_id() != 0);
  131. native_smp_prepare_boot_cpu();
  132. /* We've switched to the "real" per-cpu gdt, so make sure the
  133. old memory can be recycled */
  134. make_lowmem_page_readwrite(&per_cpu__gdt_page);
  135. for_each_possible_cpu(cpu) {
  136. cpus_clear(per_cpu(cpu_sibling_map, cpu));
  137. /*
  138. * cpu_core_map lives in a per cpu area that is cleared
  139. * when the per cpu array is allocated.
  140. *
  141. * cpus_clear(per_cpu(cpu_core_map, cpu));
  142. */
  143. }
  144. xen_setup_vcpu_info_placement();
  145. }
  146. void __init xen_smp_prepare_cpus(unsigned int max_cpus)
  147. {
  148. unsigned cpu;
  149. for_each_possible_cpu(cpu) {
  150. cpus_clear(per_cpu(cpu_sibling_map, cpu));
  151. /*
  152. * cpu_core_ map will be zeroed when the per
  153. * cpu area is allocated.
  154. *
  155. * cpus_clear(per_cpu(cpu_core_map, cpu));
  156. */
  157. }
  158. smp_store_cpu_info(0);
  159. set_cpu_sibling_map(0);
  160. if (xen_smp_intr_init(0))
  161. BUG();
  162. xen_cpu_initialized_map = cpumask_of_cpu(0);
  163. /* Restrict the possible_map according to max_cpus. */
  164. while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
  165. for (cpu = NR_CPUS - 1; !cpu_possible(cpu); cpu--)
  166. continue;
  167. cpu_clear(cpu, cpu_possible_map);
  168. }
  169. for_each_possible_cpu (cpu) {
  170. struct task_struct *idle;
  171. if (cpu == 0)
  172. continue;
  173. idle = fork_idle(cpu);
  174. if (IS_ERR(idle))
  175. panic("failed fork for CPU %d", cpu);
  176. cpu_set(cpu, cpu_present_map);
  177. }
  178. //init_xenbus_allowed_cpumask();
  179. }
  180. static __cpuinit int
  181. cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
  182. {
  183. struct vcpu_guest_context *ctxt;
  184. struct gdt_page *gdt = &per_cpu(gdt_page, cpu);
  185. if (cpu_test_and_set(cpu, xen_cpu_initialized_map))
  186. return 0;
  187. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  188. if (ctxt == NULL)
  189. return -ENOMEM;
  190. ctxt->flags = VGCF_IN_KERNEL;
  191. ctxt->user_regs.ds = __USER_DS;
  192. ctxt->user_regs.es = __USER_DS;
  193. ctxt->user_regs.fs = __KERNEL_PERCPU;
  194. ctxt->user_regs.gs = 0;
  195. ctxt->user_regs.ss = __KERNEL_DS;
  196. ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
  197. ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
  198. memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
  199. xen_copy_trap_info(ctxt->trap_ctxt);
  200. ctxt->ldt_ents = 0;
  201. BUG_ON((unsigned long)gdt->gdt & ~PAGE_MASK);
  202. make_lowmem_page_readonly(gdt->gdt);
  203. ctxt->gdt_frames[0] = virt_to_mfn(gdt->gdt);
  204. ctxt->gdt_ents = ARRAY_SIZE(gdt->gdt);
  205. ctxt->user_regs.cs = __KERNEL_CS;
  206. ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
  207. ctxt->kernel_ss = __KERNEL_DS;
  208. ctxt->kernel_sp = idle->thread.sp0;
  209. ctxt->event_callback_cs = __KERNEL_CS;
  210. ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
  211. ctxt->failsafe_callback_cs = __KERNEL_CS;
  212. ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
  213. per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
  214. ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
  215. if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
  216. BUG();
  217. kfree(ctxt);
  218. return 0;
  219. }
  220. int __cpuinit xen_cpu_up(unsigned int cpu)
  221. {
  222. struct task_struct *idle = idle_task(cpu);
  223. int rc;
  224. #if 0
  225. rc = cpu_up_check(cpu);
  226. if (rc)
  227. return rc;
  228. #endif
  229. init_gdt(cpu);
  230. per_cpu(current_task, cpu) = idle;
  231. irq_ctx_init(cpu);
  232. xen_setup_timer(cpu);
  233. /* make sure interrupts start blocked */
  234. per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
  235. rc = cpu_initialize_context(cpu, idle);
  236. if (rc)
  237. return rc;
  238. if (num_online_cpus() == 1)
  239. alternatives_smp_switch(1);
  240. rc = xen_smp_intr_init(cpu);
  241. if (rc)
  242. return rc;
  243. smp_store_cpu_info(cpu);
  244. set_cpu_sibling_map(cpu);
  245. /* This must be done before setting cpu_online_map */
  246. wmb();
  247. cpu_set(cpu, cpu_online_map);
  248. rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
  249. BUG_ON(rc);
  250. return 0;
  251. }
  252. void xen_smp_cpus_done(unsigned int max_cpus)
  253. {
  254. }
  255. static void stop_self(void *v)
  256. {
  257. int cpu = smp_processor_id();
  258. /* make sure we're not pinning something down */
  259. load_cr3(swapper_pg_dir);
  260. /* should set up a minimal gdt */
  261. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
  262. BUG();
  263. }
  264. void xen_smp_send_stop(void)
  265. {
  266. smp_call_function(stop_self, NULL, 0, 0);
  267. }
  268. void xen_smp_send_reschedule(int cpu)
  269. {
  270. xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
  271. }
  272. static void xen_send_IPI_mask(cpumask_t mask, enum ipi_vector vector)
  273. {
  274. unsigned cpu;
  275. cpus_and(mask, mask, cpu_online_map);
  276. for_each_cpu_mask(cpu, mask)
  277. xen_send_IPI_one(cpu, vector);
  278. }
  279. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
  280. {
  281. void (*func) (void *info) = call_data->func;
  282. void *info = call_data->info;
  283. int wait = call_data->wait;
  284. /*
  285. * Notify initiating CPU that I've grabbed the data and am
  286. * about to execute the function
  287. */
  288. mb();
  289. atomic_inc(&call_data->started);
  290. /*
  291. * At this point the info structure may be out of scope unless wait==1
  292. */
  293. irq_enter();
  294. (*func)(info);
  295. __get_cpu_var(irq_stat).irq_call_count++;
  296. irq_exit();
  297. if (wait) {
  298. mb(); /* commit everything before setting finished */
  299. atomic_inc(&call_data->finished);
  300. }
  301. return IRQ_HANDLED;
  302. }
  303. int xen_smp_call_function_mask(cpumask_t mask, void (*func)(void *),
  304. void *info, int wait)
  305. {
  306. struct call_data_struct data;
  307. int cpus, cpu;
  308. bool yield;
  309. /* Holding any lock stops cpus from going down. */
  310. spin_lock(&call_lock);
  311. cpu_clear(smp_processor_id(), mask);
  312. cpus = cpus_weight(mask);
  313. if (!cpus) {
  314. spin_unlock(&call_lock);
  315. return 0;
  316. }
  317. /* Can deadlock when called with interrupts disabled */
  318. WARN_ON(irqs_disabled());
  319. data.func = func;
  320. data.info = info;
  321. atomic_set(&data.started, 0);
  322. data.wait = wait;
  323. if (wait)
  324. atomic_set(&data.finished, 0);
  325. call_data = &data;
  326. mb(); /* write everything before IPI */
  327. /* Send a message to other CPUs and wait for them to respond */
  328. xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
  329. /* Make sure other vcpus get a chance to run if they need to. */
  330. yield = false;
  331. for_each_cpu_mask(cpu, mask)
  332. if (xen_vcpu_stolen(cpu))
  333. yield = true;
  334. if (yield)
  335. HYPERVISOR_sched_op(SCHEDOP_yield, 0);
  336. /* Wait for response */
  337. while (atomic_read(&data.started) != cpus ||
  338. (wait && atomic_read(&data.finished) != cpus))
  339. cpu_relax();
  340. spin_unlock(&call_lock);
  341. return 0;
  342. }