smp.c 8.8 KB

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