smp.c 19 KB

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