enlighten.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  3. #include <linux/bootmem.h>
  4. #endif
  5. #include <linux/cpu.h>
  6. #include <linux/kexec.h>
  7. #include <linux/slab.h>
  8. #include <xen/xen.h>
  9. #include <xen/features.h>
  10. #include <xen/page.h>
  11. #include <asm/xen/hypercall.h>
  12. #include <asm/xen/hypervisor.h>
  13. #include <asm/cpu.h>
  14. #include <asm/e820/api.h>
  15. #include "xen-ops.h"
  16. #include "smp.h"
  17. #include "pmu.h"
  18. EXPORT_SYMBOL_GPL(hypercall_page);
  19. /*
  20. * Pointer to the xen_vcpu_info structure or
  21. * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
  22. * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
  23. * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point
  24. * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to
  25. * acknowledge pending events.
  26. * Also more subtly it is used by the patched version of irq enable/disable
  27. * e.g. xen_irq_enable_direct and xen_iret in PV mode.
  28. *
  29. * The desire to be able to do those mask/unmask operations as a single
  30. * instruction by using the per-cpu offset held in %gs is the real reason
  31. * vcpu info is in a per-cpu pointer and the original reason for this
  32. * hypercall.
  33. *
  34. */
  35. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  36. /*
  37. * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info
  38. * hypercall. This can be used both in PV and PVHVM mode. The structure
  39. * overrides the default per_cpu(xen_vcpu, cpu) value.
  40. */
  41. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  42. /* Linux <-> Xen vCPU id mapping */
  43. DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
  44. EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
  45. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  46. EXPORT_SYMBOL_GPL(xen_domain_type);
  47. unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
  48. EXPORT_SYMBOL(machine_to_phys_mapping);
  49. unsigned long machine_to_phys_nr;
  50. EXPORT_SYMBOL(machine_to_phys_nr);
  51. struct start_info *xen_start_info;
  52. EXPORT_SYMBOL_GPL(xen_start_info);
  53. struct shared_info xen_dummy_shared_info;
  54. __read_mostly int xen_have_vector_callback;
  55. EXPORT_SYMBOL_GPL(xen_have_vector_callback);
  56. /*
  57. * NB: needs to live in .data because it's used by xen_prepare_pvh which runs
  58. * before clearing the bss.
  59. */
  60. uint32_t xen_start_flags __attribute__((section(".data"))) = 0;
  61. EXPORT_SYMBOL(xen_start_flags);
  62. /*
  63. * Point at some empty memory to start with. We map the real shared_info
  64. * page as soon as fixmap is up and running.
  65. */
  66. struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
  67. /*
  68. * Flag to determine whether vcpu info placement is available on all
  69. * VCPUs. We assume it is to start with, and then set it to zero on
  70. * the first failure. This is because it can succeed on some VCPUs
  71. * and not others, since it can involve hypervisor memory allocation,
  72. * or because the guest failed to guarantee all the appropriate
  73. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  74. *
  75. * Note that any particular CPU may be using a placed vcpu structure,
  76. * but we can only optimise if the all are.
  77. *
  78. * 0: not available, 1: available
  79. */
  80. int xen_have_vcpu_info_placement = 1;
  81. static int xen_cpu_up_online(unsigned int cpu)
  82. {
  83. xen_init_lock_cpu(cpu);
  84. return 0;
  85. }
  86. int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
  87. int (*cpu_dead_cb)(unsigned int))
  88. {
  89. int rc;
  90. rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
  91. "x86/xen/guest:prepare",
  92. cpu_up_prepare_cb, cpu_dead_cb);
  93. if (rc >= 0) {
  94. rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  95. "x86/xen/guest:online",
  96. xen_cpu_up_online, NULL);
  97. if (rc < 0)
  98. cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
  99. }
  100. return rc >= 0 ? 0 : rc;
  101. }
  102. static int xen_vcpu_setup_restore(int cpu)
  103. {
  104. int rc = 0;
  105. /* Any per_cpu(xen_vcpu) is stale, so reset it */
  106. xen_vcpu_info_reset(cpu);
  107. /*
  108. * For PVH and PVHVM, setup online VCPUs only. The rest will
  109. * be handled by hotplug.
  110. */
  111. if (xen_pv_domain() ||
  112. (xen_hvm_domain() && cpu_online(cpu))) {
  113. rc = xen_vcpu_setup(cpu);
  114. }
  115. return rc;
  116. }
  117. /*
  118. * On restore, set the vcpu placement up again.
  119. * If it fails, then we're in a bad state, since
  120. * we can't back out from using it...
  121. */
  122. void xen_vcpu_restore(void)
  123. {
  124. int cpu, rc;
  125. for_each_possible_cpu(cpu) {
  126. bool other_cpu = (cpu != smp_processor_id());
  127. bool is_up;
  128. if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
  129. continue;
  130. /* Only Xen 4.5 and higher support this. */
  131. is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
  132. xen_vcpu_nr(cpu), NULL) > 0;
  133. if (other_cpu && is_up &&
  134. HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
  135. BUG();
  136. if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
  137. xen_setup_runstate_info(cpu);
  138. rc = xen_vcpu_setup_restore(cpu);
  139. if (rc)
  140. pr_emerg_once("vcpu restore failed for cpu=%d err=%d. "
  141. "System will hang.\n", cpu, rc);
  142. /*
  143. * In case xen_vcpu_setup_restore() fails, do not bring up the
  144. * VCPU. This helps us avoid the resulting OOPS when the VCPU
  145. * accesses pvclock_vcpu_time via xen_vcpu (which is NULL.)
  146. * Note that this does not improve the situation much -- now the
  147. * VM hangs instead of OOPSing -- with the VCPUs that did not
  148. * fail, spinning in stop_machine(), waiting for the failed
  149. * VCPUs to come up.
  150. */
  151. if (other_cpu && is_up && (rc == 0) &&
  152. HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
  153. BUG();
  154. }
  155. }
  156. void xen_vcpu_info_reset(int cpu)
  157. {
  158. if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
  159. per_cpu(xen_vcpu, cpu) =
  160. &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
  161. } else {
  162. /* Set to NULL so that if somebody accesses it we get an OOPS */
  163. per_cpu(xen_vcpu, cpu) = NULL;
  164. }
  165. }
  166. int xen_vcpu_setup(int cpu)
  167. {
  168. struct vcpu_register_vcpu_info info;
  169. int err;
  170. struct vcpu_info *vcpup;
  171. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  172. /*
  173. * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu)
  174. * and at restore (xen_vcpu_restore). Also called for hotplugged
  175. * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm).
  176. * However, the hypercall can only be done once (see below) so if a VCPU
  177. * is offlined and comes back online then let's not redo the hypercall.
  178. *
  179. * For PV it is called during restore (xen_vcpu_restore) and bootup
  180. * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
  181. * use this function.
  182. */
  183. if (xen_hvm_domain()) {
  184. if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
  185. return 0;
  186. }
  187. if (xen_have_vcpu_info_placement) {
  188. vcpup = &per_cpu(xen_vcpu_info, cpu);
  189. info.mfn = arbitrary_virt_to_mfn(vcpup);
  190. info.offset = offset_in_page(vcpup);
  191. /*
  192. * Check to see if the hypervisor will put the vcpu_info
  193. * structure where we want it, which allows direct access via
  194. * a percpu-variable.
  195. * N.B. This hypercall can _only_ be called once per CPU.
  196. * Subsequent calls will error out with -EINVAL. This is due to
  197. * the fact that hypervisor has no unregister variant and this
  198. * hypercall does not allow to over-write info.mfn and
  199. * info.offset.
  200. */
  201. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
  202. xen_vcpu_nr(cpu), &info);
  203. if (err) {
  204. pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n",
  205. cpu, err);
  206. xen_have_vcpu_info_placement = 0;
  207. } else {
  208. /*
  209. * This cpu is using the registered vcpu info, even if
  210. * later ones fail to.
  211. */
  212. per_cpu(xen_vcpu, cpu) = vcpup;
  213. }
  214. }
  215. if (!xen_have_vcpu_info_placement)
  216. xen_vcpu_info_reset(cpu);
  217. return ((per_cpu(xen_vcpu, cpu) == NULL) ? -ENODEV : 0);
  218. }
  219. void xen_reboot(int reason)
  220. {
  221. struct sched_shutdown r = { .reason = reason };
  222. int cpu;
  223. for_each_online_cpu(cpu)
  224. xen_pmu_finish(cpu);
  225. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  226. BUG();
  227. }
  228. void xen_emergency_restart(void)
  229. {
  230. xen_reboot(SHUTDOWN_reboot);
  231. }
  232. static int
  233. xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
  234. {
  235. if (!kexec_crash_loaded())
  236. xen_reboot(SHUTDOWN_crash);
  237. return NOTIFY_DONE;
  238. }
  239. static struct notifier_block xen_panic_block = {
  240. .notifier_call = xen_panic_event,
  241. .priority = INT_MIN
  242. };
  243. int xen_panic_handler_init(void)
  244. {
  245. atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
  246. return 0;
  247. }
  248. void xen_pin_vcpu(int cpu)
  249. {
  250. static bool disable_pinning;
  251. struct sched_pin_override pin_override;
  252. int ret;
  253. if (disable_pinning)
  254. return;
  255. pin_override.pcpu = cpu;
  256. ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
  257. /* Ignore errors when removing override. */
  258. if (cpu < 0)
  259. return;
  260. switch (ret) {
  261. case -ENOSYS:
  262. pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
  263. cpu);
  264. disable_pinning = true;
  265. break;
  266. case -EPERM:
  267. WARN(1, "Trying to pin vcpu without having privilege to do so\n");
  268. disable_pinning = true;
  269. break;
  270. case -EINVAL:
  271. case -EBUSY:
  272. pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
  273. cpu);
  274. break;
  275. case 0:
  276. break;
  277. default:
  278. WARN(1, "rc %d while trying to pin vcpu\n", ret);
  279. disable_pinning = true;
  280. }
  281. }
  282. #ifdef CONFIG_HOTPLUG_CPU
  283. void xen_arch_register_cpu(int num)
  284. {
  285. arch_register_cpu(num);
  286. }
  287. EXPORT_SYMBOL(xen_arch_register_cpu);
  288. void xen_arch_unregister_cpu(int num)
  289. {
  290. arch_unregister_cpu(num);
  291. }
  292. EXPORT_SYMBOL(xen_arch_unregister_cpu);
  293. #endif