smp.c 19 KB

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