smp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. #include <linux/sched.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/smp.h>
  19. #include <asm/paravirt.h>
  20. #include <asm/desc.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/cpu.h>
  23. #include <xen/interface/xen.h>
  24. #include <xen/interface/vcpu.h>
  25. #include <asm/xen/interface.h>
  26. #include <asm/xen/hypercall.h>
  27. #include <xen/xen.h>
  28. #include <xen/page.h>
  29. #include <xen/events.h>
  30. #include <xen/hvc-console.h>
  31. #include "xen-ops.h"
  32. #include "mmu.h"
  33. cpumask_var_t xen_cpu_initialized_map;
  34. static DEFINE_PER_CPU(int, xen_resched_irq);
  35. static DEFINE_PER_CPU(int, xen_callfunc_irq);
  36. static DEFINE_PER_CPU(int, xen_callfuncsingle_irq);
  37. static DEFINE_PER_CPU(int, xen_debug_irq) = -1;
  38. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
  39. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
  40. /*
  41. * Reschedule call back.
  42. */
  43. static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
  44. {
  45. inc_irq_stat(irq_resched_count);
  46. scheduler_ipi();
  47. return IRQ_HANDLED;
  48. }
  49. static void __cpuinit cpu_bringup(void)
  50. {
  51. int cpu;
  52. cpu_init();
  53. touch_softlockup_watchdog();
  54. preempt_disable();
  55. xen_enable_sysenter();
  56. xen_enable_syscall();
  57. cpu = smp_processor_id();
  58. smp_store_cpu_info(cpu);
  59. cpu_data(cpu).x86_max_cores = 1;
  60. set_cpu_sibling_map(cpu);
  61. xen_setup_cpu_clockevents();
  62. notify_cpu_starting(cpu);
  63. ipi_call_lock();
  64. set_cpu_online(cpu, true);
  65. ipi_call_unlock();
  66. this_cpu_write(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. }
  72. static void __cpuinit cpu_bringup_and_idle(void)
  73. {
  74. cpu_bringup();
  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(xen_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(xen_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(xen_debug_irq, cpu) = rc;
  108. callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
  109. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
  110. cpu,
  111. xen_call_function_single_interrupt,
  112. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  113. callfunc_name,
  114. NULL);
  115. if (rc < 0)
  116. goto fail;
  117. per_cpu(xen_callfuncsingle_irq, cpu) = rc;
  118. return 0;
  119. fail:
  120. if (per_cpu(xen_resched_irq, cpu) >= 0)
  121. unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL);
  122. if (per_cpu(xen_callfunc_irq, cpu) >= 0)
  123. unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL);
  124. if (per_cpu(xen_debug_irq, cpu) >= 0)
  125. unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL);
  126. if (per_cpu(xen_callfuncsingle_irq, cpu) >= 0)
  127. unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu),
  128. NULL);
  129. return rc;
  130. }
  131. static void __init xen_fill_possible_map(void)
  132. {
  133. int i, rc;
  134. if (xen_initial_domain())
  135. return;
  136. for (i = 0; i < nr_cpu_ids; i++) {
  137. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  138. if (rc >= 0) {
  139. num_processors++;
  140. set_cpu_possible(i, true);
  141. }
  142. }
  143. }
  144. static void __init xen_filter_cpu_maps(void)
  145. {
  146. int i, rc;
  147. unsigned int subtract = 0;
  148. if (!xen_initial_domain())
  149. return;
  150. num_processors = 0;
  151. disabled_cpus = 0;
  152. for (i = 0; i < nr_cpu_ids; i++) {
  153. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  154. if (rc >= 0) {
  155. num_processors++;
  156. set_cpu_possible(i, true);
  157. } else {
  158. set_cpu_possible(i, false);
  159. set_cpu_present(i, false);
  160. subtract++;
  161. }
  162. }
  163. #ifdef CONFIG_HOTPLUG_CPU
  164. /* This is akin to using 'nr_cpus' on the Linux command line.
  165. * Which is OK as when we use 'dom0_max_vcpus=X' we can only
  166. * have up to X, while nr_cpu_ids is greater than X. This
  167. * normally is not a problem, except when CPU hotplugging
  168. * is involved and then there might be more than X CPUs
  169. * in the guest - which will not work as there is no
  170. * hypercall to expand the max number of VCPUs an already
  171. * running guest has. So cap it up to X. */
  172. if (subtract)
  173. nr_cpu_ids = nr_cpu_ids - subtract;
  174. #endif
  175. }
  176. static void __init xen_smp_prepare_boot_cpu(void)
  177. {
  178. BUG_ON(smp_processor_id() != 0);
  179. native_smp_prepare_boot_cpu();
  180. /* We've switched to the "real" per-cpu gdt, so make sure the
  181. old memory can be recycled */
  182. make_lowmem_page_readwrite(xen_initial_gdt);
  183. xen_filter_cpu_maps();
  184. xen_setup_vcpu_info_placement();
  185. }
  186. static void __init xen_smp_prepare_cpus(unsigned int max_cpus)
  187. {
  188. unsigned cpu;
  189. unsigned int i;
  190. if (skip_ioapic_setup) {
  191. char *m = (max_cpus == 0) ?
  192. "The nosmp parameter is incompatible with Xen; " \
  193. "use Xen dom0_max_vcpus=1 parameter" :
  194. "The noapic parameter is incompatible with Xen";
  195. xen_raw_printk(m);
  196. panic(m);
  197. }
  198. xen_init_lock_cpu(0);
  199. smp_store_cpu_info(0);
  200. cpu_data(0).x86_max_cores = 1;
  201. for_each_possible_cpu(i) {
  202. zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
  203. zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
  204. zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
  205. }
  206. set_cpu_sibling_map(0);
  207. if (xen_smp_intr_init(0))
  208. BUG();
  209. if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
  210. panic("could not allocate xen_cpu_initialized_map\n");
  211. cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
  212. /* Restrict the possible_map according to max_cpus. */
  213. while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
  214. for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
  215. continue;
  216. set_cpu_possible(cpu, false);
  217. }
  218. for_each_possible_cpu (cpu) {
  219. struct task_struct *idle;
  220. if (cpu == 0)
  221. continue;
  222. idle = fork_idle(cpu);
  223. if (IS_ERR(idle))
  224. panic("failed fork for CPU %d", cpu);
  225. set_cpu_present(cpu, true);
  226. }
  227. }
  228. static int __cpuinit
  229. cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
  230. {
  231. struct vcpu_guest_context *ctxt;
  232. struct desc_struct *gdt;
  233. unsigned long gdt_mfn;
  234. if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
  235. return 0;
  236. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  237. if (ctxt == NULL)
  238. return -ENOMEM;
  239. gdt = get_cpu_gdt_table(cpu);
  240. ctxt->flags = VGCF_IN_KERNEL;
  241. ctxt->user_regs.ds = __USER_DS;
  242. ctxt->user_regs.es = __USER_DS;
  243. ctxt->user_regs.ss = __KERNEL_DS;
  244. #ifdef CONFIG_X86_32
  245. ctxt->user_regs.fs = __KERNEL_PERCPU;
  246. ctxt->user_regs.gs = __KERNEL_STACK_CANARY;
  247. #else
  248. ctxt->gs_base_kernel = per_cpu_offset(cpu);
  249. #endif
  250. ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
  251. ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
  252. memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
  253. xen_copy_trap_info(ctxt->trap_ctxt);
  254. ctxt->ldt_ents = 0;
  255. BUG_ON((unsigned long)gdt & ~PAGE_MASK);
  256. gdt_mfn = arbitrary_virt_to_mfn(gdt);
  257. make_lowmem_page_readonly(gdt);
  258. make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
  259. ctxt->gdt_frames[0] = gdt_mfn;
  260. ctxt->gdt_ents = GDT_ENTRIES;
  261. ctxt->user_regs.cs = __KERNEL_CS;
  262. ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
  263. ctxt->kernel_ss = __KERNEL_DS;
  264. ctxt->kernel_sp = idle->thread.sp0;
  265. #ifdef CONFIG_X86_32
  266. ctxt->event_callback_cs = __KERNEL_CS;
  267. ctxt->failsafe_callback_cs = __KERNEL_CS;
  268. #endif
  269. ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
  270. ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
  271. per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
  272. ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
  273. if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
  274. BUG();
  275. kfree(ctxt);
  276. return 0;
  277. }
  278. static int __cpuinit xen_cpu_up(unsigned int cpu)
  279. {
  280. struct task_struct *idle = idle_task(cpu);
  281. int rc;
  282. per_cpu(current_task, cpu) = idle;
  283. #ifdef CONFIG_X86_32
  284. irq_ctx_init(cpu);
  285. #else
  286. clear_tsk_thread_flag(idle, TIF_FORK);
  287. per_cpu(kernel_stack, cpu) =
  288. (unsigned long)task_stack_page(idle) -
  289. KERNEL_STACK_OFFSET + THREAD_SIZE;
  290. #endif
  291. xen_setup_runstate_info(cpu);
  292. xen_setup_timer(cpu);
  293. xen_init_lock_cpu(cpu);
  294. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  295. /* make sure interrupts start blocked */
  296. per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
  297. rc = cpu_initialize_context(cpu, idle);
  298. if (rc)
  299. return rc;
  300. if (num_online_cpus() == 1)
  301. alternatives_smp_switch(1);
  302. rc = xen_smp_intr_init(cpu);
  303. if (rc)
  304. return rc;
  305. rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
  306. BUG_ON(rc);
  307. while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
  308. HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
  309. barrier();
  310. }
  311. return 0;
  312. }
  313. static void xen_smp_cpus_done(unsigned int max_cpus)
  314. {
  315. }
  316. #ifdef CONFIG_HOTPLUG_CPU
  317. static int xen_cpu_disable(void)
  318. {
  319. unsigned int cpu = smp_processor_id();
  320. if (cpu == 0)
  321. return -EBUSY;
  322. cpu_disable_common();
  323. load_cr3(swapper_pg_dir);
  324. return 0;
  325. }
  326. static void xen_cpu_die(unsigned int cpu)
  327. {
  328. while (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) {
  329. current->state = TASK_UNINTERRUPTIBLE;
  330. schedule_timeout(HZ/10);
  331. }
  332. unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL);
  333. unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL);
  334. unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL);
  335. unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu), NULL);
  336. xen_uninit_lock_cpu(cpu);
  337. xen_teardown_timer(cpu);
  338. if (num_online_cpus() == 1)
  339. alternatives_smp_switch(0);
  340. }
  341. static void __cpuinit xen_play_dead(void) /* used only with HOTPLUG_CPU */
  342. {
  343. play_dead_common();
  344. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  345. cpu_bringup();
  346. /*
  347. * Balance out the preempt calls - as we are running in cpu_idle
  348. * loop which has been called at bootup from cpu_bringup_and_idle.
  349. * The cpucpu_bringup_and_idle called cpu_bringup which made a
  350. * preempt_disable() So this preempt_enable will balance it out.
  351. */
  352. preempt_enable();
  353. }
  354. #else /* !CONFIG_HOTPLUG_CPU */
  355. static int xen_cpu_disable(void)
  356. {
  357. return -ENOSYS;
  358. }
  359. static void xen_cpu_die(unsigned int cpu)
  360. {
  361. BUG();
  362. }
  363. static void xen_play_dead(void)
  364. {
  365. BUG();
  366. }
  367. #endif
  368. static void stop_self(void *v)
  369. {
  370. int cpu = smp_processor_id();
  371. /* make sure we're not pinning something down */
  372. load_cr3(swapper_pg_dir);
  373. /* should set up a minimal gdt */
  374. set_cpu_online(cpu, false);
  375. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
  376. BUG();
  377. }
  378. static void xen_stop_other_cpus(int wait)
  379. {
  380. smp_call_function(stop_self, NULL, wait);
  381. }
  382. static void xen_smp_send_reschedule(int cpu)
  383. {
  384. xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
  385. }
  386. static void xen_send_IPI_mask(const struct cpumask *mask,
  387. enum ipi_vector vector)
  388. {
  389. unsigned cpu;
  390. for_each_cpu_and(cpu, mask, cpu_online_mask)
  391. xen_send_IPI_one(cpu, vector);
  392. }
  393. static void xen_smp_send_call_function_ipi(const struct cpumask *mask)
  394. {
  395. int cpu;
  396. xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
  397. /* Make sure other vcpus get a chance to run if they need to. */
  398. for_each_cpu(cpu, mask) {
  399. if (xen_vcpu_stolen(cpu)) {
  400. HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
  401. break;
  402. }
  403. }
  404. }
  405. static void xen_smp_send_call_function_single_ipi(int cpu)
  406. {
  407. xen_send_IPI_mask(cpumask_of(cpu),
  408. XEN_CALL_FUNCTION_SINGLE_VECTOR);
  409. }
  410. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
  411. {
  412. irq_enter();
  413. generic_smp_call_function_interrupt();
  414. inc_irq_stat(irq_call_count);
  415. irq_exit();
  416. return IRQ_HANDLED;
  417. }
  418. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
  419. {
  420. irq_enter();
  421. generic_smp_call_function_single_interrupt();
  422. inc_irq_stat(irq_call_count);
  423. irq_exit();
  424. return IRQ_HANDLED;
  425. }
  426. static const struct smp_ops xen_smp_ops __initconst = {
  427. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  428. .smp_prepare_cpus = xen_smp_prepare_cpus,
  429. .smp_cpus_done = xen_smp_cpus_done,
  430. .cpu_up = xen_cpu_up,
  431. .cpu_die = xen_cpu_die,
  432. .cpu_disable = xen_cpu_disable,
  433. .play_dead = xen_play_dead,
  434. .stop_other_cpus = xen_stop_other_cpus,
  435. .smp_send_reschedule = xen_smp_send_reschedule,
  436. .send_call_func_ipi = xen_smp_send_call_function_ipi,
  437. .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
  438. };
  439. void __init xen_smp_init(void)
  440. {
  441. smp_ops = xen_smp_ops;
  442. xen_fill_possible_map();
  443. xen_init_spinlocks();
  444. }
  445. static void __init xen_hvm_smp_prepare_cpus(unsigned int max_cpus)
  446. {
  447. native_smp_prepare_cpus(max_cpus);
  448. WARN_ON(xen_smp_intr_init(0));
  449. xen_init_lock_cpu(0);
  450. }
  451. static int __cpuinit xen_hvm_cpu_up(unsigned int cpu)
  452. {
  453. int rc;
  454. rc = native_cpu_up(cpu);
  455. WARN_ON (xen_smp_intr_init(cpu));
  456. return rc;
  457. }
  458. static void xen_hvm_cpu_die(unsigned int cpu)
  459. {
  460. unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL);
  461. unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL);
  462. unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL);
  463. unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu), NULL);
  464. xen_uninit_lock_cpu(cpu);
  465. xen_teardown_timer(cpu);
  466. native_cpu_die(cpu);
  467. }
  468. void __init xen_hvm_smp_init(void)
  469. {
  470. if (!xen_have_vector_callback)
  471. return;
  472. smp_ops.smp_prepare_cpus = xen_hvm_smp_prepare_cpus;
  473. smp_ops.smp_send_reschedule = xen_smp_send_reschedule;
  474. smp_ops.cpu_up = xen_hvm_cpu_up;
  475. smp_ops.cpu_die = xen_hvm_cpu_die;
  476. smp_ops.send_call_func_ipi = xen_smp_send_call_function_ipi;
  477. smp_ops.send_call_func_single_ipi = xen_smp_send_call_function_single_ipi;
  478. }