smp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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/kernel_stat.h>
  19. #include <linux/err.h>
  20. #include <linux/smp.h>
  21. #include <asm/paravirt.h>
  22. #include <asm/desc.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/cpu.h>
  25. #include <xen/interface/xen.h>
  26. #include <xen/interface/vcpu.h>
  27. #include <asm/xen/interface.h>
  28. #include <asm/xen/hypercall.h>
  29. #include <xen/page.h>
  30. #include <xen/events.h>
  31. #include "xen-ops.h"
  32. #include "mmu.h"
  33. static void __cpuinit xen_init_lock_cpu(int cpu);
  34. cpumask_t xen_cpu_initialized_map;
  35. static DEFINE_PER_CPU(int, resched_irq);
  36. static DEFINE_PER_CPU(int, callfunc_irq);
  37. static DEFINE_PER_CPU(int, callfuncsingle_irq);
  38. static DEFINE_PER_CPU(int, debug_irq) = -1;
  39. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
  40. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
  41. /*
  42. * Reschedule call back. Nothing to do,
  43. * all the work is done automatically when
  44. * we return from the interrupt.
  45. */
  46. static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
  47. {
  48. #ifdef CONFIG_X86_32
  49. __get_cpu_var(irq_stat).irq_resched_count++;
  50. #else
  51. add_pda(irq_resched_count, 1);
  52. #endif
  53. return IRQ_HANDLED;
  54. }
  55. static __cpuinit void cpu_bringup_and_idle(void)
  56. {
  57. int cpu = smp_processor_id();
  58. cpu_init();
  59. preempt_disable();
  60. xen_enable_sysenter();
  61. xen_enable_syscall();
  62. cpu = smp_processor_id();
  63. smp_store_cpu_info(cpu);
  64. cpu_data(cpu).x86_max_cores = 1;
  65. set_cpu_sibling_map(cpu);
  66. xen_setup_cpu_clockevents();
  67. cpu_set(cpu, cpu_online_map);
  68. x86_write_percpu(cpu_state, CPU_ONLINE);
  69. wmb();
  70. /* We can take interrupts now: we're officially "up". */
  71. local_irq_enable();
  72. wmb(); /* make sure everything is out */
  73. cpu_idle();
  74. }
  75. static int xen_smp_intr_init(unsigned int cpu)
  76. {
  77. int rc;
  78. const char *resched_name, *callfunc_name, *debug_name;
  79. resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
  80. rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
  81. cpu,
  82. xen_reschedule_interrupt,
  83. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  84. resched_name,
  85. NULL);
  86. if (rc < 0)
  87. goto fail;
  88. per_cpu(resched_irq, cpu) = rc;
  89. callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
  90. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
  91. cpu,
  92. xen_call_function_interrupt,
  93. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  94. callfunc_name,
  95. NULL);
  96. if (rc < 0)
  97. goto fail;
  98. per_cpu(callfunc_irq, cpu) = rc;
  99. debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
  100. rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
  101. IRQF_DISABLED | IRQF_PERCPU | IRQF_NOBALANCING,
  102. debug_name, NULL);
  103. if (rc < 0)
  104. goto fail;
  105. per_cpu(debug_irq, cpu) = rc;
  106. callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
  107. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
  108. cpu,
  109. xen_call_function_single_interrupt,
  110. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  111. callfunc_name,
  112. NULL);
  113. if (rc < 0)
  114. goto fail;
  115. per_cpu(callfuncsingle_irq, cpu) = rc;
  116. return 0;
  117. fail:
  118. if (per_cpu(resched_irq, cpu) >= 0)
  119. unbind_from_irqhandler(per_cpu(resched_irq, cpu), NULL);
  120. if (per_cpu(callfunc_irq, cpu) >= 0)
  121. unbind_from_irqhandler(per_cpu(callfunc_irq, cpu), NULL);
  122. if (per_cpu(debug_irq, cpu) >= 0)
  123. unbind_from_irqhandler(per_cpu(debug_irq, cpu), NULL);
  124. if (per_cpu(callfuncsingle_irq, cpu) >= 0)
  125. unbind_from_irqhandler(per_cpu(callfuncsingle_irq, cpu), NULL);
  126. return rc;
  127. }
  128. static void __init xen_fill_possible_map(void)
  129. {
  130. int i, rc;
  131. for (i = 0; i < NR_CPUS; i++) {
  132. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  133. if (rc >= 0) {
  134. num_processors++;
  135. cpu_set(i, cpu_possible_map);
  136. }
  137. }
  138. }
  139. static void __init xen_smp_prepare_boot_cpu(void)
  140. {
  141. BUG_ON(smp_processor_id() != 0);
  142. native_smp_prepare_boot_cpu();
  143. /* We've switched to the "real" per-cpu gdt, so make sure the
  144. old memory can be recycled */
  145. make_lowmem_page_readwrite(&per_cpu_var(gdt_page));
  146. xen_setup_vcpu_info_placement();
  147. }
  148. static void __init xen_smp_prepare_cpus(unsigned int max_cpus)
  149. {
  150. unsigned cpu;
  151. xen_init_lock_cpu(0);
  152. smp_store_cpu_info(0);
  153. cpu_data(0).x86_max_cores = 1;
  154. set_cpu_sibling_map(0);
  155. if (xen_smp_intr_init(0))
  156. BUG();
  157. xen_cpu_initialized_map = cpumask_of_cpu(0);
  158. /* Restrict the possible_map according to max_cpus. */
  159. while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
  160. for (cpu = NR_CPUS - 1; !cpu_possible(cpu); cpu--)
  161. continue;
  162. cpu_clear(cpu, cpu_possible_map);
  163. }
  164. for_each_possible_cpu (cpu) {
  165. struct task_struct *idle;
  166. if (cpu == 0)
  167. continue;
  168. idle = fork_idle(cpu);
  169. if (IS_ERR(idle))
  170. panic("failed fork for CPU %d", cpu);
  171. cpu_set(cpu, cpu_present_map);
  172. }
  173. //init_xenbus_allowed_cpumask();
  174. }
  175. static __cpuinit int
  176. cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
  177. {
  178. struct vcpu_guest_context *ctxt;
  179. struct desc_struct *gdt;
  180. if (cpu_test_and_set(cpu, xen_cpu_initialized_map))
  181. return 0;
  182. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  183. if (ctxt == NULL)
  184. return -ENOMEM;
  185. gdt = get_cpu_gdt_table(cpu);
  186. ctxt->flags = VGCF_IN_KERNEL;
  187. ctxt->user_regs.ds = __USER_DS;
  188. ctxt->user_regs.es = __USER_DS;
  189. ctxt->user_regs.ss = __KERNEL_DS;
  190. #ifdef CONFIG_X86_32
  191. ctxt->user_regs.fs = __KERNEL_PERCPU;
  192. #endif
  193. ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
  194. ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
  195. memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
  196. xen_copy_trap_info(ctxt->trap_ctxt);
  197. ctxt->ldt_ents = 0;
  198. BUG_ON((unsigned long)gdt & ~PAGE_MASK);
  199. make_lowmem_page_readonly(gdt);
  200. ctxt->gdt_frames[0] = virt_to_mfn(gdt);
  201. ctxt->gdt_ents = GDT_ENTRIES;
  202. ctxt->user_regs.cs = __KERNEL_CS;
  203. ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
  204. ctxt->kernel_ss = __KERNEL_DS;
  205. ctxt->kernel_sp = idle->thread.sp0;
  206. #ifdef CONFIG_X86_32
  207. ctxt->event_callback_cs = __KERNEL_CS;
  208. ctxt->failsafe_callback_cs = __KERNEL_CS;
  209. #endif
  210. ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
  211. ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
  212. per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
  213. ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
  214. if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
  215. BUG();
  216. kfree(ctxt);
  217. return 0;
  218. }
  219. static int __cpuinit xen_cpu_up(unsigned int cpu)
  220. {
  221. struct task_struct *idle = idle_task(cpu);
  222. int rc;
  223. #if 0
  224. rc = cpu_up_check(cpu);
  225. if (rc)
  226. return rc;
  227. #endif
  228. #ifdef CONFIG_X86_64
  229. /* Allocate node local memory for AP pdas */
  230. WARN_ON(cpu == 0);
  231. if (cpu > 0) {
  232. rc = get_local_pda(cpu);
  233. if (rc)
  234. return rc;
  235. }
  236. #endif
  237. #ifdef CONFIG_X86_32
  238. init_gdt(cpu);
  239. per_cpu(current_task, cpu) = idle;
  240. irq_ctx_init(cpu);
  241. #else
  242. cpu_pda(cpu)->pcurrent = idle;
  243. clear_tsk_thread_flag(idle, TIF_FORK);
  244. #endif
  245. xen_setup_timer(cpu);
  246. xen_init_lock_cpu(cpu);
  247. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  248. /* make sure interrupts start blocked */
  249. per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
  250. rc = cpu_initialize_context(cpu, idle);
  251. if (rc)
  252. return rc;
  253. if (num_online_cpus() == 1)
  254. alternatives_smp_switch(1);
  255. rc = xen_smp_intr_init(cpu);
  256. if (rc)
  257. return rc;
  258. rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
  259. BUG_ON(rc);
  260. while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
  261. HYPERVISOR_sched_op(SCHEDOP_yield, 0);
  262. barrier();
  263. }
  264. return 0;
  265. }
  266. static void xen_smp_cpus_done(unsigned int max_cpus)
  267. {
  268. }
  269. static void stop_self(void *v)
  270. {
  271. int cpu = smp_processor_id();
  272. /* make sure we're not pinning something down */
  273. load_cr3(swapper_pg_dir);
  274. /* should set up a minimal gdt */
  275. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
  276. BUG();
  277. }
  278. static void xen_smp_send_stop(void)
  279. {
  280. smp_call_function(stop_self, NULL, 0);
  281. }
  282. static void xen_smp_send_reschedule(int cpu)
  283. {
  284. xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
  285. }
  286. static void xen_send_IPI_mask(cpumask_t mask, enum ipi_vector vector)
  287. {
  288. unsigned cpu;
  289. cpus_and(mask, mask, cpu_online_map);
  290. for_each_cpu_mask_nr(cpu, mask)
  291. xen_send_IPI_one(cpu, vector);
  292. }
  293. static void xen_smp_send_call_function_ipi(cpumask_t mask)
  294. {
  295. int cpu;
  296. xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
  297. /* Make sure other vcpus get a chance to run if they need to. */
  298. for_each_cpu_mask_nr(cpu, mask) {
  299. if (xen_vcpu_stolen(cpu)) {
  300. HYPERVISOR_sched_op(SCHEDOP_yield, 0);
  301. break;
  302. }
  303. }
  304. }
  305. static void xen_smp_send_call_function_single_ipi(int cpu)
  306. {
  307. xen_send_IPI_mask(cpumask_of_cpu(cpu), XEN_CALL_FUNCTION_SINGLE_VECTOR);
  308. }
  309. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
  310. {
  311. irq_enter();
  312. generic_smp_call_function_interrupt();
  313. #ifdef CONFIG_X86_32
  314. __get_cpu_var(irq_stat).irq_call_count++;
  315. #else
  316. add_pda(irq_call_count, 1);
  317. #endif
  318. irq_exit();
  319. return IRQ_HANDLED;
  320. }
  321. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
  322. {
  323. irq_enter();
  324. generic_smp_call_function_single_interrupt();
  325. #ifdef CONFIG_X86_32
  326. __get_cpu_var(irq_stat).irq_call_count++;
  327. #else
  328. add_pda(irq_call_count, 1);
  329. #endif
  330. irq_exit();
  331. return IRQ_HANDLED;
  332. }
  333. struct xen_spinlock {
  334. unsigned char lock; /* 0 -> free; 1 -> locked */
  335. unsigned short spinners; /* count of waiting cpus */
  336. };
  337. static int xen_spin_is_locked(struct raw_spinlock *lock)
  338. {
  339. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  340. return xl->lock != 0;
  341. }
  342. static int xen_spin_is_contended(struct raw_spinlock *lock)
  343. {
  344. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  345. /* Not strictly true; this is only the count of contended
  346. lock-takers entering the slow path. */
  347. return xl->spinners != 0;
  348. }
  349. static int xen_spin_trylock(struct raw_spinlock *lock)
  350. {
  351. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  352. u8 old = 1;
  353. asm("xchgb %b0,%1"
  354. : "+q" (old), "+m" (xl->lock) : : "memory");
  355. return old == 0;
  356. }
  357. static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
  358. static DEFINE_PER_CPU(struct xen_spinlock *, lock_spinners);
  359. static inline void spinning_lock(struct xen_spinlock *xl)
  360. {
  361. __get_cpu_var(lock_spinners) = xl;
  362. wmb(); /* set lock of interest before count */
  363. asm(LOCK_PREFIX " incw %0"
  364. : "+m" (xl->spinners) : : "memory");
  365. }
  366. static inline void unspinning_lock(struct xen_spinlock *xl)
  367. {
  368. asm(LOCK_PREFIX " decw %0"
  369. : "+m" (xl->spinners) : : "memory");
  370. wmb(); /* decrement count before clearing lock */
  371. __get_cpu_var(lock_spinners) = NULL;
  372. }
  373. static noinline int xen_spin_lock_slow(struct raw_spinlock *lock)
  374. {
  375. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  376. int irq = __get_cpu_var(lock_kicker_irq);
  377. int ret;
  378. /* If kicker interrupts not initialized yet, just spin */
  379. if (irq == -1)
  380. return 0;
  381. /* announce we're spinning */
  382. spinning_lock(xl);
  383. /* clear pending */
  384. xen_clear_irq_pending(irq);
  385. /* check again make sure it didn't become free while
  386. we weren't looking */
  387. ret = xen_spin_trylock(lock);
  388. if (ret)
  389. goto out;
  390. /* block until irq becomes pending */
  391. xen_poll_irq(irq);
  392. kstat_this_cpu.irqs[irq]++;
  393. out:
  394. unspinning_lock(xl);
  395. return ret;
  396. }
  397. static void xen_spin_lock(struct raw_spinlock *lock)
  398. {
  399. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  400. int timeout;
  401. u8 oldval;
  402. do {
  403. timeout = 1 << 10;
  404. asm("1: xchgb %1,%0\n"
  405. " testb %1,%1\n"
  406. " jz 3f\n"
  407. "2: rep;nop\n"
  408. " cmpb $0,%0\n"
  409. " je 1b\n"
  410. " dec %2\n"
  411. " jnz 2b\n"
  412. "3:\n"
  413. : "+m" (xl->lock), "=q" (oldval), "+r" (timeout)
  414. : "1" (1)
  415. : "memory");
  416. } while (unlikely(oldval != 0 && !xen_spin_lock_slow(lock)));
  417. }
  418. static noinline void xen_spin_unlock_slow(struct xen_spinlock *xl)
  419. {
  420. int cpu;
  421. for_each_online_cpu(cpu) {
  422. /* XXX should mix up next cpu selection */
  423. if (per_cpu(lock_spinners, cpu) == xl) {
  424. xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
  425. break;
  426. }
  427. }
  428. }
  429. static void xen_spin_unlock(struct raw_spinlock *lock)
  430. {
  431. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  432. smp_wmb(); /* make sure no writes get moved after unlock */
  433. xl->lock = 0; /* release lock */
  434. /* make sure unlock happens before kick */
  435. barrier();
  436. if (unlikely(xl->spinners))
  437. xen_spin_unlock_slow(xl);
  438. }
  439. static __cpuinit void xen_init_lock_cpu(int cpu)
  440. {
  441. int irq;
  442. const char *name;
  443. name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
  444. irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
  445. cpu,
  446. xen_reschedule_interrupt,
  447. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  448. name,
  449. NULL);
  450. if (irq >= 0) {
  451. disable_irq(irq); /* make sure it's never delivered */
  452. per_cpu(lock_kicker_irq, cpu) = irq;
  453. }
  454. printk("cpu %d spinlock event irq %d\n", cpu, irq);
  455. }
  456. static void __init xen_init_spinlocks(void)
  457. {
  458. pv_lock_ops.spin_is_locked = xen_spin_is_locked;
  459. pv_lock_ops.spin_is_contended = xen_spin_is_contended;
  460. pv_lock_ops.spin_lock = xen_spin_lock;
  461. pv_lock_ops.spin_trylock = xen_spin_trylock;
  462. pv_lock_ops.spin_unlock = xen_spin_unlock;
  463. }
  464. static const struct smp_ops xen_smp_ops __initdata = {
  465. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  466. .smp_prepare_cpus = xen_smp_prepare_cpus,
  467. .cpu_up = xen_cpu_up,
  468. .smp_cpus_done = xen_smp_cpus_done,
  469. .smp_send_stop = xen_smp_send_stop,
  470. .smp_send_reschedule = xen_smp_send_reschedule,
  471. .send_call_func_ipi = xen_smp_send_call_function_ipi,
  472. .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
  473. };
  474. void __init xen_smp_init(void)
  475. {
  476. smp_ops = xen_smp_ops;
  477. xen_fill_possible_map();
  478. xen_init_spinlocks();
  479. }