enlighten.c 8.7 KB

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