smp_pv.c 11 KB

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