smp_pv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xen SMP support
  4. *
  5. * This file implements the Xen versions of smp_ops. SMP under Xen is
  6. * very straightforward. Bringing a CPU up is simply a matter of
  7. * loading its initial context and setting it running.
  8. *
  9. * IPIs are handled through the Xen event mechanism.
  10. *
  11. * Because virtual CPUs can be scheduled onto any real CPU, there's no
  12. * useful topology information for the kernel to make use of. As a
  13. * result, all CPUs are treated as if they're single-core and
  14. * single-threaded.
  15. */
  16. #include <linux/sched.h>
  17. #include <linux/sched/task_stack.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/smp.h>
  21. #include <linux/irq_work.h>
  22. #include <linux/tick.h>
  23. #include <linux/nmi.h>
  24. #include <linux/cpuhotplug.h>
  25. #include <linux/stackprotector.h>
  26. #include <asm/paravirt.h>
  27. #include <asm/desc.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/cpu.h>
  30. #include <xen/interface/xen.h>
  31. #include <xen/interface/vcpu.h>
  32. #include <xen/interface/xenpmu.h>
  33. #include <asm/spec-ctrl.h>
  34. #include <asm/xen/interface.h>
  35. #include <asm/xen/hypercall.h>
  36. #include <xen/xen.h>
  37. #include <xen/page.h>
  38. #include <xen/events.h>
  39. #include <xen/hvc-console.h>
  40. #include "xen-ops.h"
  41. #include "mmu.h"
  42. #include "smp.h"
  43. #include "pmu.h"
  44. cpumask_var_t xen_cpu_initialized_map;
  45. static DEFINE_PER_CPU(struct xen_common_irq, xen_irq_work) = { .irq = -1 };
  46. static DEFINE_PER_CPU(struct xen_common_irq, xen_pmu_irq) = { .irq = -1 };
  47. static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id);
  48. static void cpu_bringup(void)
  49. {
  50. int cpu;
  51. cpu_init();
  52. touch_softlockup_watchdog();
  53. preempt_disable();
  54. /* PVH runs in ring 0 and allows us to do native syscalls. Yay! */
  55. if (!xen_feature(XENFEAT_supervisor_mode_kernel)) {
  56. xen_enable_sysenter();
  57. xen_enable_syscall();
  58. }
  59. cpu = smp_processor_id();
  60. smp_store_cpu_info(cpu);
  61. cpu_data(cpu).x86_max_cores = 1;
  62. set_cpu_sibling_map(cpu);
  63. speculative_store_bypass_ht_init();
  64. xen_setup_cpu_clockevents();
  65. notify_cpu_starting(cpu);
  66. set_cpu_online(cpu, true);
  67. cpu_set_state_online(cpu); /* Implies full memory barrier. */
  68. /* We can take interrupts now: we're officially "up". */
  69. local_irq_enable();
  70. }
  71. asmlinkage __visible void cpu_bringup_and_idle(void)
  72. {
  73. cpu_bringup();
  74. boot_init_stack_canary();
  75. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  76. }
  77. void xen_smp_intr_free_pv(unsigned int cpu)
  78. {
  79. if (per_cpu(xen_irq_work, cpu).irq >= 0) {
  80. unbind_from_irqhandler(per_cpu(xen_irq_work, cpu).irq, NULL);
  81. per_cpu(xen_irq_work, cpu).irq = -1;
  82. kfree(per_cpu(xen_irq_work, cpu).name);
  83. per_cpu(xen_irq_work, cpu).name = NULL;
  84. }
  85. if (per_cpu(xen_pmu_irq, cpu).irq >= 0) {
  86. unbind_from_irqhandler(per_cpu(xen_pmu_irq, cpu).irq, NULL);
  87. per_cpu(xen_pmu_irq, cpu).irq = -1;
  88. kfree(per_cpu(xen_pmu_irq, cpu).name);
  89. per_cpu(xen_pmu_irq, cpu).name = NULL;
  90. }
  91. }
  92. int xen_smp_intr_init_pv(unsigned int cpu)
  93. {
  94. int rc;
  95. char *callfunc_name, *pmu_name;
  96. callfunc_name = kasprintf(GFP_KERNEL, "irqwork%d", cpu);
  97. rc = bind_ipi_to_irqhandler(XEN_IRQ_WORK_VECTOR,
  98. cpu,
  99. xen_irq_work_interrupt,
  100. IRQF_PERCPU|IRQF_NOBALANCING,
  101. callfunc_name,
  102. NULL);
  103. if (rc < 0)
  104. goto fail;
  105. per_cpu(xen_irq_work, cpu).irq = rc;
  106. per_cpu(xen_irq_work, cpu).name = callfunc_name;
  107. if (is_xen_pmu(cpu)) {
  108. pmu_name = kasprintf(GFP_KERNEL, "pmu%d", cpu);
  109. rc = bind_virq_to_irqhandler(VIRQ_XENPMU, cpu,
  110. xen_pmu_irq_handler,
  111. IRQF_PERCPU|IRQF_NOBALANCING,
  112. pmu_name, NULL);
  113. if (rc < 0)
  114. goto fail;
  115. per_cpu(xen_pmu_irq, cpu).irq = rc;
  116. per_cpu(xen_pmu_irq, cpu).name = pmu_name;
  117. }
  118. return 0;
  119. fail:
  120. xen_smp_intr_free_pv(cpu);
  121. return rc;
  122. }
  123. static void __init xen_fill_possible_map(void)
  124. {
  125. int i, rc;
  126. if (xen_initial_domain())
  127. return;
  128. for (i = 0; i < nr_cpu_ids; i++) {
  129. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  130. if (rc >= 0) {
  131. num_processors++;
  132. set_cpu_possible(i, true);
  133. }
  134. }
  135. }
  136. static void __init xen_filter_cpu_maps(void)
  137. {
  138. int i, rc;
  139. unsigned int subtract = 0;
  140. if (!xen_initial_domain())
  141. return;
  142. num_processors = 0;
  143. disabled_cpus = 0;
  144. for (i = 0; i < nr_cpu_ids; i++) {
  145. rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
  146. if (rc >= 0) {
  147. num_processors++;
  148. set_cpu_possible(i, true);
  149. } else {
  150. set_cpu_possible(i, false);
  151. set_cpu_present(i, false);
  152. subtract++;
  153. }
  154. }
  155. #ifdef CONFIG_HOTPLUG_CPU
  156. /* This is akin to using 'nr_cpus' on the Linux command line.
  157. * Which is OK as when we use 'dom0_max_vcpus=X' we can only
  158. * have up to X, while nr_cpu_ids is greater than X. This
  159. * normally is not a problem, except when CPU hotplugging
  160. * is involved and then there might be more than X CPUs
  161. * in the guest - which will not work as there is no
  162. * hypercall to expand the max number of VCPUs an already
  163. * running guest has. So cap it up to X. */
  164. if (subtract)
  165. nr_cpu_ids = nr_cpu_ids - subtract;
  166. #endif
  167. }
  168. static void __init xen_pv_smp_prepare_boot_cpu(void)
  169. {
  170. BUG_ON(smp_processor_id() != 0);
  171. native_smp_prepare_boot_cpu();
  172. if (!xen_feature(XENFEAT_writable_page_tables))
  173. /* We've switched to the "real" per-cpu gdt, so make
  174. * sure the old memory can be recycled. */
  175. make_lowmem_page_readwrite(xen_initial_gdt);
  176. #ifdef CONFIG_X86_32
  177. /*
  178. * Xen starts us with XEN_FLAT_RING1_DS, but linux code
  179. * expects __USER_DS
  180. */
  181. loadsegment(ds, __USER_DS);
  182. loadsegment(es, __USER_DS);
  183. #endif
  184. xen_filter_cpu_maps();
  185. xen_setup_vcpu_info_placement();
  186. /*
  187. * The alternative logic (which patches the unlock/lock) runs before
  188. * the smp bootup up code is activated. Hence we need to set this up
  189. * the core kernel is being patched. Otherwise we will have only
  190. * modules patched but not core code.
  191. */
  192. xen_init_spinlocks();
  193. }
  194. static void __init xen_pv_smp_prepare_cpus(unsigned int max_cpus)
  195. {
  196. unsigned cpu;
  197. unsigned int i;
  198. if (skip_ioapic_setup) {
  199. char *m = (max_cpus == 0) ?
  200. "The nosmp parameter is incompatible with Xen; " \
  201. "use Xen dom0_max_vcpus=1 parameter" :
  202. "The noapic parameter is incompatible with Xen";
  203. xen_raw_printk(m);
  204. panic(m);
  205. }
  206. xen_init_lock_cpu(0);
  207. smp_store_boot_cpu_info();
  208. cpu_data(0).x86_max_cores = 1;
  209. for_each_possible_cpu(i) {
  210. zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
  211. zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
  212. zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
  213. }
  214. set_cpu_sibling_map(0);
  215. speculative_store_bypass_ht_init();
  216. xen_pmu_init(0);
  217. if (xen_smp_intr_init(0) || xen_smp_intr_init_pv(0))
  218. BUG();
  219. if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
  220. panic("could not allocate xen_cpu_initialized_map\n");
  221. cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
  222. /* Restrict the possible_map according to max_cpus. */
  223. while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
  224. for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
  225. continue;
  226. set_cpu_possible(cpu, false);
  227. }
  228. for_each_possible_cpu(cpu)
  229. set_cpu_present(cpu, true);
  230. }
  231. static int
  232. cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
  233. {
  234. struct vcpu_guest_context *ctxt;
  235. struct desc_struct *gdt;
  236. unsigned long gdt_mfn;
  237. /* used to tell cpu_init() that it can proceed with initialization */
  238. cpumask_set_cpu(cpu, cpu_callout_mask);
  239. if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
  240. return 0;
  241. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  242. if (ctxt == NULL)
  243. return -ENOMEM;
  244. gdt = get_cpu_gdt_rw(cpu);
  245. #ifdef CONFIG_X86_32
  246. ctxt->user_regs.fs = __KERNEL_PERCPU;
  247. ctxt->user_regs.gs = __KERNEL_STACK_CANARY;
  248. #endif
  249. memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
  250. /*
  251. * Bring up the CPU in cpu_bringup_and_idle() with the stack
  252. * pointing just below where pt_regs would be if it were a normal
  253. * kernel entry.
  254. */
  255. ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
  256. ctxt->flags = VGCF_IN_KERNEL;
  257. ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
  258. ctxt->user_regs.ds = __USER_DS;
  259. ctxt->user_regs.es = __USER_DS;
  260. ctxt->user_regs.ss = __KERNEL_DS;
  261. ctxt->user_regs.cs = __KERNEL_CS;
  262. ctxt->user_regs.esp = (unsigned long)task_pt_regs(idle);
  263. xen_copy_trap_info(ctxt->trap_ctxt);
  264. ctxt->ldt_ents = 0;
  265. BUG_ON((unsigned long)gdt & ~PAGE_MASK);
  266. gdt_mfn = arbitrary_virt_to_mfn(gdt);
  267. make_lowmem_page_readonly(gdt);
  268. make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
  269. ctxt->gdt_frames[0] = gdt_mfn;
  270. ctxt->gdt_ents = GDT_ENTRIES;
  271. /*
  272. * Set SS:SP that Xen will use when entering guest kernel mode
  273. * from guest user mode. Subsequent calls to load_sp0() can
  274. * change this value.
  275. */
  276. ctxt->kernel_ss = __KERNEL_DS;
  277. ctxt->kernel_sp = task_top_of_stack(idle);
  278. #ifdef CONFIG_X86_32
  279. ctxt->event_callback_cs = __KERNEL_CS;
  280. ctxt->failsafe_callback_cs = __KERNEL_CS;
  281. #else
  282. ctxt->gs_base_kernel = per_cpu_offset(cpu);
  283. #endif
  284. ctxt->event_callback_eip =
  285. (unsigned long)xen_hypervisor_callback;
  286. ctxt->failsafe_callback_eip =
  287. (unsigned long)xen_failsafe_callback;
  288. per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
  289. ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_gfn(swapper_pg_dir));
  290. if (HYPERVISOR_vcpu_op(VCPUOP_initialise, xen_vcpu_nr(cpu), ctxt))
  291. BUG();
  292. kfree(ctxt);
  293. return 0;
  294. }
  295. static int xen_pv_cpu_up(unsigned int cpu, struct task_struct *idle)
  296. {
  297. int rc;
  298. common_cpu_up(cpu, idle);
  299. xen_setup_runstate_info(cpu);
  300. /*
  301. * PV VCPUs are always successfully taken down (see 'while' loop
  302. * in xen_cpu_die()), so -EBUSY is an error.
  303. */
  304. rc = cpu_check_up_prepare(cpu);
  305. if (rc)
  306. return rc;
  307. /* make sure interrupts start blocked */
  308. per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
  309. rc = cpu_initialize_context(cpu, idle);
  310. if (rc)
  311. return rc;
  312. xen_pmu_init(cpu);
  313. rc = HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL);
  314. BUG_ON(rc);
  315. while (cpu_report_state(cpu) != CPU_ONLINE)
  316. HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
  317. return 0;
  318. }
  319. #ifdef CONFIG_HOTPLUG_CPU
  320. static int xen_pv_cpu_disable(void)
  321. {
  322. unsigned int cpu = smp_processor_id();
  323. if (cpu == 0)
  324. return -EBUSY;
  325. cpu_disable_common();
  326. load_cr3(swapper_pg_dir);
  327. return 0;
  328. }
  329. static void xen_pv_cpu_die(unsigned int cpu)
  330. {
  331. while (HYPERVISOR_vcpu_op(VCPUOP_is_up,
  332. xen_vcpu_nr(cpu), NULL)) {
  333. __set_current_state(TASK_UNINTERRUPTIBLE);
  334. schedule_timeout(HZ/10);
  335. }
  336. if (common_cpu_die(cpu) == 0) {
  337. xen_smp_intr_free(cpu);
  338. xen_uninit_lock_cpu(cpu);
  339. xen_teardown_timer(cpu);
  340. xen_pmu_finish(cpu);
  341. }
  342. }
  343. static void xen_pv_play_dead(void) /* used only with HOTPLUG_CPU */
  344. {
  345. play_dead_common();
  346. HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(smp_processor_id()), NULL);
  347. cpu_bringup();
  348. /*
  349. * commit 4b0c0f294 (tick: Cleanup NOHZ per cpu data on cpu down)
  350. * clears certain data that the cpu_idle loop (which called us
  351. * and that we return from) expects. The only way to get that
  352. * data back is to call:
  353. */
  354. tick_nohz_idle_enter();
  355. tick_nohz_idle_stop_tick_protected();
  356. cpuhp_online_idle(CPUHP_AP_ONLINE_IDLE);
  357. }
  358. #else /* !CONFIG_HOTPLUG_CPU */
  359. static int xen_pv_cpu_disable(void)
  360. {
  361. return -ENOSYS;
  362. }
  363. static void xen_pv_cpu_die(unsigned int cpu)
  364. {
  365. BUG();
  366. }
  367. static void xen_pv_play_dead(void)
  368. {
  369. BUG();
  370. }
  371. #endif
  372. static void stop_self(void *v)
  373. {
  374. int cpu = smp_processor_id();
  375. /* make sure we're not pinning something down */
  376. load_cr3(swapper_pg_dir);
  377. /* should set up a minimal gdt */
  378. set_cpu_online(cpu, false);
  379. HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL);
  380. BUG();
  381. }
  382. static void xen_pv_stop_other_cpus(int wait)
  383. {
  384. smp_call_function(stop_self, NULL, wait);
  385. }
  386. static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id)
  387. {
  388. irq_enter();
  389. irq_work_run();
  390. inc_irq_stat(apic_irq_work_irqs);
  391. irq_exit();
  392. return IRQ_HANDLED;
  393. }
  394. static const struct smp_ops xen_smp_ops __initconst = {
  395. .smp_prepare_boot_cpu = xen_pv_smp_prepare_boot_cpu,
  396. .smp_prepare_cpus = xen_pv_smp_prepare_cpus,
  397. .smp_cpus_done = xen_smp_cpus_done,
  398. .cpu_up = xen_pv_cpu_up,
  399. .cpu_die = xen_pv_cpu_die,
  400. .cpu_disable = xen_pv_cpu_disable,
  401. .play_dead = xen_pv_play_dead,
  402. .stop_other_cpus = xen_pv_stop_other_cpus,
  403. .smp_send_reschedule = xen_smp_send_reschedule,
  404. .send_call_func_ipi = xen_smp_send_call_function_ipi,
  405. .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
  406. };
  407. void __init xen_smp_init(void)
  408. {
  409. smp_ops = xen_smp_ops;
  410. xen_fill_possible_map();
  411. }