smp_pv.c 11 KB

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