enlighten.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /*
  91. * On restore, set the vcpu placement up again.
  92. * If it fails, then we're in a bad state, since
  93. * we can't back out from using it...
  94. */
  95. void xen_vcpu_restore(void)
  96. {
  97. int cpu;
  98. for_each_possible_cpu(cpu) {
  99. bool other_cpu = (cpu != smp_processor_id());
  100. bool is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up, xen_vcpu_nr(cpu),
  101. NULL);
  102. if (other_cpu && is_up &&
  103. HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
  104. BUG();
  105. xen_setup_runstate_info(cpu);
  106. if (xen_have_vcpu_info_placement)
  107. xen_vcpu_setup(cpu);
  108. if (other_cpu && is_up &&
  109. HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
  110. BUG();
  111. }
  112. }
  113. static void clamp_max_cpus(void)
  114. {
  115. #ifdef CONFIG_SMP
  116. if (setup_max_cpus > MAX_VIRT_CPUS)
  117. setup_max_cpus = MAX_VIRT_CPUS;
  118. #endif
  119. }
  120. void xen_vcpu_info_reset(int cpu)
  121. {
  122. if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
  123. per_cpu(xen_vcpu, cpu) =
  124. &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
  125. } else {
  126. /* Set to NULL so that if somebody accesses it we get an OOPS */
  127. per_cpu(xen_vcpu, cpu) = NULL;
  128. }
  129. }
  130. void xen_vcpu_setup(int cpu)
  131. {
  132. struct vcpu_register_vcpu_info info;
  133. int err;
  134. struct vcpu_info *vcpup;
  135. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  136. /*
  137. * This path is called twice on PVHVM - first during bootup via
  138. * smp_init -> xen_hvm_cpu_notify, and then if the VCPU is being
  139. * hotplugged: cpu_up -> xen_hvm_cpu_notify.
  140. * As we can only do the VCPUOP_register_vcpu_info once lets
  141. * not over-write its result.
  142. *
  143. * For PV it is called during restore (xen_vcpu_restore) and bootup
  144. * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
  145. * use this function.
  146. */
  147. if (xen_hvm_domain()) {
  148. if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
  149. return;
  150. }
  151. xen_vcpu_info_reset(cpu);
  152. if (xen_have_vcpu_info_placement) {
  153. vcpup = &per_cpu(xen_vcpu_info, cpu);
  154. info.mfn = arbitrary_virt_to_mfn(vcpup);
  155. info.offset = offset_in_page(vcpup);
  156. /*
  157. * Check to see if the hypervisor will put the vcpu_info
  158. * structure where we want it, which allows direct access via
  159. * a percpu-variable.
  160. * N.B. This hypercall can _only_ be called once per CPU.
  161. * Subsequent calls will error out with -EINVAL. This is due to
  162. * the fact that hypervisor has no unregister variant and this
  163. * hypercall does not allow to over-write info.mfn and
  164. * info.offset.
  165. */
  166. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
  167. xen_vcpu_nr(cpu), &info);
  168. if (err) {
  169. pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n",
  170. cpu, err);
  171. xen_have_vcpu_info_placement = 0;
  172. } else {
  173. /*
  174. * This cpu is using the registered vcpu info, even if
  175. * later ones fail to.
  176. */
  177. per_cpu(xen_vcpu, cpu) = vcpup;
  178. }
  179. }
  180. if (!xen_have_vcpu_info_placement) {
  181. if (cpu >= MAX_VIRT_CPUS)
  182. clamp_max_cpus();
  183. return;
  184. }
  185. }
  186. void xen_reboot(int reason)
  187. {
  188. struct sched_shutdown r = { .reason = reason };
  189. int cpu;
  190. for_each_online_cpu(cpu)
  191. xen_pmu_finish(cpu);
  192. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  193. BUG();
  194. }
  195. void xen_emergency_restart(void)
  196. {
  197. xen_reboot(SHUTDOWN_reboot);
  198. }
  199. static int
  200. xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
  201. {
  202. if (!kexec_crash_loaded())
  203. xen_reboot(SHUTDOWN_crash);
  204. return NOTIFY_DONE;
  205. }
  206. static struct notifier_block xen_panic_block = {
  207. .notifier_call = xen_panic_event,
  208. .priority = INT_MIN
  209. };
  210. int xen_panic_handler_init(void)
  211. {
  212. atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
  213. return 0;
  214. }
  215. void xen_pin_vcpu(int cpu)
  216. {
  217. static bool disable_pinning;
  218. struct sched_pin_override pin_override;
  219. int ret;
  220. if (disable_pinning)
  221. return;
  222. pin_override.pcpu = cpu;
  223. ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
  224. /* Ignore errors when removing override. */
  225. if (cpu < 0)
  226. return;
  227. switch (ret) {
  228. case -ENOSYS:
  229. pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
  230. cpu);
  231. disable_pinning = true;
  232. break;
  233. case -EPERM:
  234. WARN(1, "Trying to pin vcpu without having privilege to do so\n");
  235. disable_pinning = true;
  236. break;
  237. case -EINVAL:
  238. case -EBUSY:
  239. pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
  240. cpu);
  241. break;
  242. case 0:
  243. break;
  244. default:
  245. WARN(1, "rc %d while trying to pin vcpu\n", ret);
  246. disable_pinning = true;
  247. }
  248. }
  249. #ifdef CONFIG_HOTPLUG_CPU
  250. void xen_arch_register_cpu(int num)
  251. {
  252. arch_register_cpu(num);
  253. }
  254. EXPORT_SYMBOL(xen_arch_register_cpu);
  255. void xen_arch_unregister_cpu(int num)
  256. {
  257. arch_unregister_cpu(num);
  258. }
  259. EXPORT_SYMBOL(xen_arch_unregister_cpu);
  260. #endif