enlighten.c 9.5 KB

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