smp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 <linux/irq_work.h>
  20. #include <linux/tick.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/xen.h>
  30. #include <xen/page.h>
  31. #include <xen/events.h>
  32. #include <xen/hvc-console.h>
  33. #include "xen-ops.h"
  34. #include "mmu.h"
  35. cpumask_var_t xen_cpu_initialized_map;
  36. struct xen_common_irq {
  37. int irq;
  38. char *name;
  39. };
  40. static DEFINE_PER_CPU(struct xen_common_irq, xen_resched_irq) = { .irq = -1 };
  41. static DEFINE_PER_CPU(struct xen_common_irq, xen_callfunc_irq) = { .irq = -1 };
  42. static DEFINE_PER_CPU(struct xen_common_irq, xen_callfuncsingle_irq) = { .irq = -1 };
  43. static DEFINE_PER_CPU(struct xen_common_irq, xen_irq_work) = { .irq = -1 };
  44. static DEFINE_PER_CPU(struct xen_common_irq, xen_debug_irq) = { .irq = -1 };
  45. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
  46. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
  47. static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id);
  48. /*
  49. * Reschedule call back.
  50. */
  51. static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
  52. {
  53. inc_irq_stat(irq_resched_count);
  54. scheduler_ipi();
  55. return IRQ_HANDLED;
  56. }
  57. static void cpu_bringup(void)
  58. {
  59. int cpu;
  60. cpu_init();
  61. touch_softlockup_watchdog();
  62. preempt_disable();
  63. /* PVH runs in ring 0 and allows us to do native syscalls. Yay! */
  64. if (!xen_feature(XENFEAT_supervisor_mode_kernel)) {
  65. xen_enable_sysenter();
  66. xen_enable_syscall();
  67. }
  68. cpu = smp_processor_id();
  69. smp_store_cpu_info(cpu);
  70. cpu_data(cpu).x86_max_cores = 1;
  71. set_cpu_sibling_map(cpu);
  72. xen_setup_cpu_clockevents();
  73. notify_cpu_starting(cpu);
  74. set_cpu_online(cpu, true);
  75. this_cpu_write(cpu_state, CPU_ONLINE);
  76. wmb();
  77. /* We can take interrupts now: we're officially "up". */
  78. local_irq_enable();
  79. wmb(); /* make sure everything is out */
  80. }
  81. /* Note: cpu parameter is only relevant for PVH */
  82. static void cpu_bringup_and_idle(int cpu)
  83. {
  84. #ifdef CONFIG_X86_64
  85. if (xen_feature(XENFEAT_auto_translated_physmap) &&
  86. xen_feature(XENFEAT_supervisor_mode_kernel))
  87. xen_pvh_secondary_vcpu_init(cpu);
  88. #endif
  89. cpu_bringup();
  90. cpu_startup_entry(CPUHP_ONLINE);
  91. }
  92. static void xen_smp_intr_free(unsigned int cpu)
  93. {
  94. if (per_cpu(xen_resched_irq, cpu).irq >= 0) {
  95. unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu).irq, NULL);
  96. per_cpu(xen_resched_irq, cpu).irq = -1;
  97. kfree(per_cpu(xen_resched_irq, cpu).name);
  98. per_cpu(xen_resched_irq, cpu).name = NULL;
  99. }
  100. if (per_cpu(xen_callfunc_irq, cpu).irq >= 0) {
  101. unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu).irq, NULL);
  102. per_cpu(xen_callfunc_irq, cpu).irq = -1;
  103. kfree(per_cpu(xen_callfunc_irq, cpu).name);
  104. per_cpu(xen_callfunc_irq, cpu).name = NULL;
  105. }
  106. if (per_cpu(xen_debug_irq, cpu).irq >= 0) {
  107. unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu).irq, NULL);
  108. per_cpu(xen_debug_irq, cpu).irq = -1;
  109. kfree(per_cpu(xen_debug_irq, cpu).name);
  110. per_cpu(xen_debug_irq, cpu).name = NULL;
  111. }
  112. if (per_cpu(xen_callfuncsingle_irq, cpu).irq >= 0) {
  113. unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu).irq,
  114. NULL);
  115. per_cpu(xen_callfuncsingle_irq, cpu).irq = -1;
  116. kfree(per_cpu(xen_callfuncsingle_irq, cpu).name);
  117. per_cpu(xen_callfuncsingle_irq, cpu).name = NULL;
  118. }
  119. if (xen_hvm_domain())
  120. return;
  121. if (per_cpu(xen_irq_work, cpu).irq >= 0) {
  122. unbind_from_irqhandler(per_cpu(xen_irq_work, cpu).irq, NULL);
  123. per_cpu(xen_irq_work, cpu).irq = -1;
  124. kfree(per_cpu(xen_irq_work, cpu).name);
  125. per_cpu(xen_irq_work, cpu).name = NULL;
  126. }
  127. };
  128. static int xen_smp_intr_init(unsigned int cpu)
  129. {
  130. int rc;
  131. char *resched_name, *callfunc_name, *debug_name;
  132. resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
  133. rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
  134. cpu,
  135. xen_reschedule_interrupt,
  136. IRQF_PERCPU|IRQF_NOBALANCING,
  137. resched_name,
  138. NULL);
  139. if (rc < 0)
  140. goto fail;
  141. per_cpu(xen_resched_irq, cpu).irq = rc;
  142. per_cpu(xen_resched_irq, cpu).name = resched_name;
  143. callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
  144. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
  145. cpu,
  146. xen_call_function_interrupt,
  147. IRQF_PERCPU|IRQF_NOBALANCING,
  148. callfunc_name,
  149. NULL);
  150. if (rc < 0)
  151. goto fail;
  152. per_cpu(xen_callfunc_irq, cpu).irq = rc;
  153. per_cpu(xen_callfunc_irq, cpu).name = callfunc_name;
  154. debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
  155. rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
  156. IRQF_PERCPU | IRQF_NOBALANCING,
  157. debug_name, NULL);
  158. if (rc < 0)
  159. goto fail;
  160. per_cpu(xen_debug_irq, cpu).irq = rc;
  161. per_cpu(xen_debug_irq, cpu).name = debug_name;
  162. callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
  163. rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
  164. cpu,
  165. xen_call_function_single_interrupt,
  166. IRQF_PERCPU|IRQF_NOBALANCING,
  167. callfunc_name,
  168. NULL);
  169. if (rc < 0)
  170. goto fail;
  171. per_cpu(xen_callfuncsingle_irq, cpu).irq = rc;
  172. per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
  173. /*
  174. * The IRQ worker on PVHVM goes through the native path and uses the
  175. * IPI mechanism.
  176. */
  177. if (xen_hvm_domain())
  178. return 0;
  179. callfunc_name = kasprintf(GFP_KERNEL, "irqwork%d", cpu);
  180. rc = bind_ipi_to_irqhandler(XEN_IRQ_WORK_VECTOR,
  181. cpu,
  182. xen_irq_work_interrupt,
  183. IRQF_PERCPU|IRQF_NOBALANCING,
  184. callfunc_name,
  185. NULL);
  186. if (rc < 0)
  187. goto fail;
  188. per_cpu(xen_irq_work, cpu).irq = rc;
  189. per_cpu(xen_irq_work, cpu).name = callfunc_name;
  190. return 0;
  191. fail:
  192. xen_smp_intr_free(cpu);
  193. return rc;
  194. }
  195. static void __init xen_fill_possible_map(void)
  196. {
  197. int i, rc;
  198. if (xen_initial_domain())
  199. return;
  200. for (i = 0; i < nr_cpu_ids; i++) {
  201. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  202. if (rc >= 0) {
  203. num_processors++;
  204. set_cpu_possible(i, true);
  205. }
  206. }
  207. }
  208. static void __init xen_filter_cpu_maps(void)
  209. {
  210. int i, rc;
  211. unsigned int subtract = 0;
  212. if (!xen_initial_domain())
  213. return;
  214. num_processors = 0;
  215. disabled_cpus = 0;
  216. for (i = 0; i < nr_cpu_ids; i++) {
  217. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  218. if (rc >= 0) {
  219. num_processors++;
  220. set_cpu_possible(i, true);
  221. } else {
  222. set_cpu_possible(i, false);
  223. set_cpu_present(i, false);
  224. subtract++;
  225. }
  226. }
  227. #ifdef CONFIG_HOTPLUG_CPU
  228. /* This is akin to using 'nr_cpus' on the Linux command line.
  229. * Which is OK as when we use 'dom0_max_vcpus=X' we can only
  230. * have up to X, while nr_cpu_ids is greater than X. This
  231. * normally is not a problem, except when CPU hotplugging
  232. * is involved and then there might be more than X CPUs
  233. * in the guest - which will not work as there is no
  234. * hypercall to expand the max number of VCPUs an already
  235. * running guest has. So cap it up to X. */
  236. if (subtract)
  237. nr_cpu_ids = nr_cpu_ids - subtract;
  238. #endif
  239. }
  240. static void __init xen_smp_prepare_boot_cpu(void)
  241. {
  242. BUG_ON(smp_processor_id() != 0);
  243. native_smp_prepare_boot_cpu();
  244. if (xen_pv_domain()) {
  245. if (!xen_feature(XENFEAT_writable_page_tables))
  246. /* We've switched to the "real" per-cpu gdt, so make
  247. * sure the old memory can be recycled. */
  248. make_lowmem_page_readwrite(xen_initial_gdt);
  249. #ifdef CONFIG_X86_32
  250. /*
  251. * Xen starts us with XEN_FLAT_RING1_DS, but linux code
  252. * expects __USER_DS
  253. */
  254. loadsegment(ds, __USER_DS);
  255. loadsegment(es, __USER_DS);
  256. #endif
  257. xen_filter_cpu_maps();
  258. xen_setup_vcpu_info_placement();
  259. }
  260. /*
  261. * The alternative logic (which patches the unlock/lock) runs before
  262. * the smp bootup up code is activated. Hence we need to set this up
  263. * the core kernel is being patched. Otherwise we will have only
  264. * modules patched but not core code.
  265. */
  266. xen_init_spinlocks();
  267. }
  268. static void __init xen_smp_prepare_cpus(unsigned int max_cpus)
  269. {
  270. unsigned cpu;
  271. unsigned int i;
  272. if (skip_ioapic_setup) {
  273. char *m = (max_cpus == 0) ?
  274. "The nosmp parameter is incompatible with Xen; " \
  275. "use Xen dom0_max_vcpus=1 parameter" :
  276. "The noapic parameter is incompatible with Xen";
  277. xen_raw_printk(m);
  278. panic(m);
  279. }
  280. xen_init_lock_cpu(0);
  281. smp_store_boot_cpu_info();
  282. cpu_data(0).x86_max_cores = 1;
  283. for_each_possible_cpu(i) {
  284. zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
  285. zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
  286. zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
  287. }
  288. set_cpu_sibling_map(0);
  289. if (xen_smp_intr_init(0))
  290. BUG();
  291. if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
  292. panic("could not allocate xen_cpu_initialized_map\n");
  293. cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
  294. /* Restrict the possible_map according to max_cpus. */
  295. while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
  296. for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
  297. continue;
  298. set_cpu_possible(cpu, false);
  299. }
  300. for_each_possible_cpu(cpu)
  301. set_cpu_present(cpu, true);
  302. }
  303. static int
  304. cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
  305. {
  306. struct vcpu_guest_context *ctxt;
  307. struct desc_struct *gdt;
  308. unsigned long gdt_mfn;
  309. if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
  310. return 0;
  311. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  312. if (ctxt == NULL)
  313. return -ENOMEM;
  314. gdt = get_cpu_gdt_table(cpu);
  315. #ifdef CONFIG_X86_32
  316. /* Note: PVH is not yet supported on x86_32. */
  317. ctxt->user_regs.fs = __KERNEL_PERCPU;
  318. ctxt->user_regs.gs = __KERNEL_STACK_CANARY;
  319. #endif
  320. ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
  321. memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
  322. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  323. ctxt->flags = VGCF_IN_KERNEL;
  324. ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
  325. ctxt->user_regs.ds = __USER_DS;
  326. ctxt->user_regs.es = __USER_DS;
  327. ctxt->user_regs.ss = __KERNEL_DS;
  328. xen_copy_trap_info(ctxt->trap_ctxt);
  329. ctxt->ldt_ents = 0;
  330. BUG_ON((unsigned long)gdt & ~PAGE_MASK);
  331. gdt_mfn = arbitrary_virt_to_mfn(gdt);
  332. make_lowmem_page_readonly(gdt);
  333. make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
  334. ctxt->gdt_frames[0] = gdt_mfn;
  335. ctxt->gdt_ents = GDT_ENTRIES;
  336. ctxt->kernel_ss = __KERNEL_DS;
  337. ctxt->kernel_sp = idle->thread.sp0;
  338. #ifdef CONFIG_X86_32
  339. ctxt->event_callback_cs = __KERNEL_CS;
  340. ctxt->failsafe_callback_cs = __KERNEL_CS;
  341. #else
  342. ctxt->gs_base_kernel = per_cpu_offset(cpu);
  343. #endif
  344. ctxt->event_callback_eip =
  345. (unsigned long)xen_hypervisor_callback;
  346. ctxt->failsafe_callback_eip =
  347. (unsigned long)xen_failsafe_callback;
  348. ctxt->user_regs.cs = __KERNEL_CS;
  349. per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
  350. #ifdef CONFIG_X86_32
  351. }
  352. #else
  353. } else
  354. /* N.B. The user_regs.eip (cpu_bringup_and_idle) is called with
  355. * %rdi having the cpu number - which means are passing in
  356. * as the first parameter the cpu. Subtle!
  357. */
  358. ctxt->user_regs.rdi = cpu;
  359. #endif
  360. ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
  361. ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
  362. if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
  363. BUG();
  364. kfree(ctxt);
  365. return 0;
  366. }
  367. static int xen_cpu_up(unsigned int cpu, struct task_struct *idle)
  368. {
  369. int rc;
  370. per_cpu(current_task, cpu) = idle;
  371. #ifdef CONFIG_X86_32
  372. irq_ctx_init(cpu);
  373. #else
  374. clear_tsk_thread_flag(idle, TIF_FORK);
  375. #endif
  376. per_cpu(kernel_stack, cpu) =
  377. (unsigned long)task_stack_page(idle) -
  378. KERNEL_STACK_OFFSET + THREAD_SIZE;
  379. xen_setup_runstate_info(cpu);
  380. xen_setup_timer(cpu);
  381. xen_init_lock_cpu(cpu);
  382. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  383. /* make sure interrupts start blocked */
  384. per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
  385. rc = cpu_initialize_context(cpu, idle);
  386. if (rc)
  387. return rc;
  388. if (num_online_cpus() == 1)
  389. /* Just in case we booted with a single CPU. */
  390. alternatives_enable_smp();
  391. rc = xen_smp_intr_init(cpu);
  392. if (rc)
  393. return rc;
  394. rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
  395. BUG_ON(rc);
  396. while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
  397. HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
  398. barrier();
  399. }
  400. return 0;
  401. }
  402. static void xen_smp_cpus_done(unsigned int max_cpus)
  403. {
  404. }
  405. #ifdef CONFIG_HOTPLUG_CPU
  406. static int xen_cpu_disable(void)
  407. {
  408. unsigned int cpu = smp_processor_id();
  409. if (cpu == 0)
  410. return -EBUSY;
  411. cpu_disable_common();
  412. load_cr3(swapper_pg_dir);
  413. return 0;
  414. }
  415. static void xen_cpu_die(unsigned int cpu)
  416. {
  417. while (xen_pv_domain() && HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) {
  418. current->state = TASK_UNINTERRUPTIBLE;
  419. schedule_timeout(HZ/10);
  420. }
  421. xen_smp_intr_free(cpu);
  422. xen_uninit_lock_cpu(cpu);
  423. xen_teardown_timer(cpu);
  424. }
  425. static void xen_play_dead(void) /* used only with HOTPLUG_CPU */
  426. {
  427. play_dead_common();
  428. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  429. cpu_bringup();
  430. /*
  431. * commit 4b0c0f294 (tick: Cleanup NOHZ per cpu data on cpu down)
  432. * clears certain data that the cpu_idle loop (which called us
  433. * and that we return from) expects. The only way to get that
  434. * data back is to call:
  435. */
  436. tick_nohz_idle_enter();
  437. }
  438. #else /* !CONFIG_HOTPLUG_CPU */
  439. static int xen_cpu_disable(void)
  440. {
  441. return -ENOSYS;
  442. }
  443. static void xen_cpu_die(unsigned int cpu)
  444. {
  445. BUG();
  446. }
  447. static void xen_play_dead(void)
  448. {
  449. BUG();
  450. }
  451. #endif
  452. static void stop_self(void *v)
  453. {
  454. int cpu = smp_processor_id();
  455. /* make sure we're not pinning something down */
  456. load_cr3(swapper_pg_dir);
  457. /* should set up a minimal gdt */
  458. set_cpu_online(cpu, false);
  459. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
  460. BUG();
  461. }
  462. static void xen_stop_other_cpus(int wait)
  463. {
  464. smp_call_function(stop_self, NULL, wait);
  465. }
  466. static void xen_smp_send_reschedule(int cpu)
  467. {
  468. xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
  469. }
  470. static void __xen_send_IPI_mask(const struct cpumask *mask,
  471. int vector)
  472. {
  473. unsigned cpu;
  474. for_each_cpu_and(cpu, mask, cpu_online_mask)
  475. xen_send_IPI_one(cpu, vector);
  476. }
  477. static void xen_smp_send_call_function_ipi(const struct cpumask *mask)
  478. {
  479. int cpu;
  480. __xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
  481. /* Make sure other vcpus get a chance to run if they need to. */
  482. for_each_cpu(cpu, mask) {
  483. if (xen_vcpu_stolen(cpu)) {
  484. HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
  485. break;
  486. }
  487. }
  488. }
  489. static void xen_smp_send_call_function_single_ipi(int cpu)
  490. {
  491. __xen_send_IPI_mask(cpumask_of(cpu),
  492. XEN_CALL_FUNCTION_SINGLE_VECTOR);
  493. }
  494. static inline int xen_map_vector(int vector)
  495. {
  496. int xen_vector;
  497. switch (vector) {
  498. case RESCHEDULE_VECTOR:
  499. xen_vector = XEN_RESCHEDULE_VECTOR;
  500. break;
  501. case CALL_FUNCTION_VECTOR:
  502. xen_vector = XEN_CALL_FUNCTION_VECTOR;
  503. break;
  504. case CALL_FUNCTION_SINGLE_VECTOR:
  505. xen_vector = XEN_CALL_FUNCTION_SINGLE_VECTOR;
  506. break;
  507. case IRQ_WORK_VECTOR:
  508. xen_vector = XEN_IRQ_WORK_VECTOR;
  509. break;
  510. #ifdef CONFIG_X86_64
  511. case NMI_VECTOR:
  512. case APIC_DM_NMI: /* Some use that instead of NMI_VECTOR */
  513. xen_vector = XEN_NMI_VECTOR;
  514. break;
  515. #endif
  516. default:
  517. xen_vector = -1;
  518. printk(KERN_ERR "xen: vector 0x%x is not implemented\n",
  519. vector);
  520. }
  521. return xen_vector;
  522. }
  523. void xen_send_IPI_mask(const struct cpumask *mask,
  524. int vector)
  525. {
  526. int xen_vector = xen_map_vector(vector);
  527. if (xen_vector >= 0)
  528. __xen_send_IPI_mask(mask, xen_vector);
  529. }
  530. void xen_send_IPI_all(int vector)
  531. {
  532. int xen_vector = xen_map_vector(vector);
  533. if (xen_vector >= 0)
  534. __xen_send_IPI_mask(cpu_online_mask, xen_vector);
  535. }
  536. void xen_send_IPI_self(int vector)
  537. {
  538. int xen_vector = xen_map_vector(vector);
  539. if (xen_vector >= 0)
  540. xen_send_IPI_one(smp_processor_id(), xen_vector);
  541. }
  542. void xen_send_IPI_mask_allbutself(const struct cpumask *mask,
  543. int vector)
  544. {
  545. unsigned cpu;
  546. unsigned int this_cpu = smp_processor_id();
  547. int xen_vector = xen_map_vector(vector);
  548. if (!(num_online_cpus() > 1) || (xen_vector < 0))
  549. return;
  550. for_each_cpu_and(cpu, mask, cpu_online_mask) {
  551. if (this_cpu == cpu)
  552. continue;
  553. xen_send_IPI_one(cpu, xen_vector);
  554. }
  555. }
  556. void xen_send_IPI_allbutself(int vector)
  557. {
  558. xen_send_IPI_mask_allbutself(cpu_online_mask, vector);
  559. }
  560. static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
  561. {
  562. irq_enter();
  563. generic_smp_call_function_interrupt();
  564. inc_irq_stat(irq_call_count);
  565. irq_exit();
  566. return IRQ_HANDLED;
  567. }
  568. static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
  569. {
  570. irq_enter();
  571. generic_smp_call_function_single_interrupt();
  572. inc_irq_stat(irq_call_count);
  573. irq_exit();
  574. return IRQ_HANDLED;
  575. }
  576. static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id)
  577. {
  578. irq_enter();
  579. irq_work_run();
  580. inc_irq_stat(apic_irq_work_irqs);
  581. irq_exit();
  582. return IRQ_HANDLED;
  583. }
  584. static const struct smp_ops xen_smp_ops __initconst = {
  585. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  586. .smp_prepare_cpus = xen_smp_prepare_cpus,
  587. .smp_cpus_done = xen_smp_cpus_done,
  588. .cpu_up = xen_cpu_up,
  589. .cpu_die = xen_cpu_die,
  590. .cpu_disable = xen_cpu_disable,
  591. .play_dead = xen_play_dead,
  592. .stop_other_cpus = xen_stop_other_cpus,
  593. .smp_send_reschedule = xen_smp_send_reschedule,
  594. .send_call_func_ipi = xen_smp_send_call_function_ipi,
  595. .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
  596. };
  597. void __init xen_smp_init(void)
  598. {
  599. smp_ops = xen_smp_ops;
  600. xen_fill_possible_map();
  601. }
  602. static void __init xen_hvm_smp_prepare_cpus(unsigned int max_cpus)
  603. {
  604. native_smp_prepare_cpus(max_cpus);
  605. WARN_ON(xen_smp_intr_init(0));
  606. xen_init_lock_cpu(0);
  607. }
  608. static int xen_hvm_cpu_up(unsigned int cpu, struct task_struct *tidle)
  609. {
  610. int rc;
  611. /*
  612. * xen_smp_intr_init() needs to run before native_cpu_up()
  613. * so that IPI vectors are set up on the booting CPU before
  614. * it is marked online in native_cpu_up().
  615. */
  616. rc = xen_smp_intr_init(cpu);
  617. WARN_ON(rc);
  618. if (!rc)
  619. rc = native_cpu_up(cpu, tidle);
  620. /*
  621. * We must initialize the slowpath CPU kicker _after_ the native
  622. * path has executed. If we initialized it before none of the
  623. * unlocker IPI kicks would reach the booting CPU as the booting
  624. * CPU had not set itself 'online' in cpu_online_mask. That mask
  625. * is checked when IPIs are sent (on HVM at least).
  626. */
  627. xen_init_lock_cpu(cpu);
  628. return rc;
  629. }
  630. static void xen_hvm_cpu_die(unsigned int cpu)
  631. {
  632. xen_cpu_die(cpu);
  633. native_cpu_die(cpu);
  634. }
  635. void __init xen_hvm_smp_init(void)
  636. {
  637. if (!xen_have_vector_callback)
  638. return;
  639. smp_ops.smp_prepare_cpus = xen_hvm_smp_prepare_cpus;
  640. smp_ops.smp_send_reschedule = xen_smp_send_reschedule;
  641. smp_ops.cpu_up = xen_hvm_cpu_up;
  642. smp_ops.cpu_die = xen_hvm_cpu_die;
  643. smp_ops.send_call_func_ipi = xen_smp_send_call_function_ipi;
  644. smp_ops.send_call_func_single_ipi = xen_smp_send_call_function_single_ipi;
  645. smp_ops.smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu;
  646. }