enlighten.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  3. #include <linux/memblock.h>
  4. #endif
  5. #include <linux/console.h>
  6. #include <linux/cpu.h>
  7. #include <linux/kexec.h>
  8. #include <linux/slab.h>
  9. #include <linux/panic_notifier.h>
  10. #include <xen/xen.h>
  11. #include <xen/features.h>
  12. #include <xen/interface/sched.h>
  13. #include <xen/interface/version.h>
  14. #include <xen/page.h>
  15. #include <asm/xen/hypercall.h>
  16. #include <asm/xen/hypervisor.h>
  17. #include <asm/cpu.h>
  18. #include <asm/e820/api.h>
  19. #include <asm/setup.h>
  20. #include "xen-ops.h"
  21. #include "smp.h"
  22. #include "pmu.h"
  23. EXPORT_SYMBOL_GPL(hypercall_page);
  24. /*
  25. * Pointer to the xen_vcpu_info structure or
  26. * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
  27. * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
  28. * but during boot it is switched to point to xen_vcpu_info.
  29. * The pointer is used in xen_evtchn_do_upcall to acknowledge pending events.
  30. * Make sure that xen_vcpu_info doesn't cross a page boundary by making it
  31. * cache-line aligned (the struct is guaranteed to have a size of 64 bytes,
  32. * which matches the cache line size of 64-bit x86 processors).
  33. */
  34. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  35. DEFINE_PER_CPU_ALIGNED(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. unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
  40. EXPORT_SYMBOL(machine_to_phys_mapping);
  41. unsigned long machine_to_phys_nr;
  42. EXPORT_SYMBOL(machine_to_phys_nr);
  43. struct start_info *xen_start_info;
  44. EXPORT_SYMBOL_GPL(xen_start_info);
  45. struct shared_info xen_dummy_shared_info;
  46. __read_mostly bool xen_have_vector_callback = true;
  47. EXPORT_SYMBOL_GPL(xen_have_vector_callback);
  48. /*
  49. * NB: These need to live in .data or alike because they're used by
  50. * xen_prepare_pvh() which runs before clearing the bss.
  51. */
  52. enum xen_domain_type __ro_after_init xen_domain_type = XEN_NATIVE;
  53. EXPORT_SYMBOL_GPL(xen_domain_type);
  54. uint32_t __ro_after_init xen_start_flags;
  55. EXPORT_SYMBOL(xen_start_flags);
  56. /*
  57. * Point at some empty memory to start with. We map the real shared_info
  58. * page as soon as fixmap is up and running.
  59. */
  60. struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
  61. static int xen_cpu_up_online(unsigned int cpu)
  62. {
  63. xen_init_lock_cpu(cpu);
  64. return 0;
  65. }
  66. int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
  67. int (*cpu_dead_cb)(unsigned int))
  68. {
  69. int rc;
  70. rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
  71. "x86/xen/guest:prepare",
  72. cpu_up_prepare_cb, cpu_dead_cb);
  73. if (rc >= 0) {
  74. rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  75. "x86/xen/guest:online",
  76. xen_cpu_up_online, NULL);
  77. if (rc < 0)
  78. cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
  79. }
  80. return rc >= 0 ? 0 : rc;
  81. }
  82. static void xen_vcpu_setup_restore(int cpu)
  83. {
  84. /* Any per_cpu(xen_vcpu) is stale, so reset it */
  85. xen_vcpu_info_reset(cpu);
  86. /*
  87. * For PVH and PVHVM, setup online VCPUs only. The rest will
  88. * be handled by hotplug.
  89. */
  90. if (xen_pv_domain() ||
  91. (xen_hvm_domain() && cpu_online(cpu)))
  92. xen_vcpu_setup(cpu);
  93. }
  94. /*
  95. * On restore, set the vcpu placement up again.
  96. * If it fails, then we're in a bad state, since
  97. * we can't back out from using it...
  98. */
  99. void xen_vcpu_restore(void)
  100. {
  101. int cpu;
  102. for_each_possible_cpu(cpu) {
  103. bool other_cpu = (cpu != smp_processor_id());
  104. bool is_up;
  105. if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
  106. continue;
  107. /* Only Xen 4.5 and higher support this. */
  108. is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
  109. xen_vcpu_nr(cpu), NULL) > 0;
  110. if (other_cpu && is_up &&
  111. HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
  112. BUG();
  113. if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
  114. xen_setup_runstate_info(cpu);
  115. xen_vcpu_setup_restore(cpu);
  116. if (other_cpu && is_up &&
  117. HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
  118. BUG();
  119. }
  120. }
  121. void xen_vcpu_info_reset(int cpu)
  122. {
  123. if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
  124. per_cpu(xen_vcpu, cpu) =
  125. &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
  126. } else {
  127. /* Set to NULL so that if somebody accesses it we get an OOPS */
  128. per_cpu(xen_vcpu, cpu) = NULL;
  129. }
  130. }
  131. void xen_vcpu_setup(int cpu)
  132. {
  133. struct vcpu_register_vcpu_info info;
  134. int err;
  135. struct vcpu_info *vcpup;
  136. BUILD_BUG_ON(sizeof(*vcpup) > SMP_CACHE_BYTES);
  137. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  138. /*
  139. * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu)
  140. * and at restore (xen_vcpu_restore). Also called for hotplugged
  141. * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm).
  142. * However, the hypercall can only be done once (see below) so if a VCPU
  143. * is offlined and comes back online then let's not redo the hypercall.
  144. *
  145. * For PV it is called during restore (xen_vcpu_restore) and bootup
  146. * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
  147. * use this function.
  148. */
  149. if (xen_hvm_domain()) {
  150. if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
  151. return;
  152. }
  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. * N.B. This hypercall can _only_ be called once per CPU.
  158. * Subsequent calls will error out with -EINVAL. This is due to
  159. * the fact that hypervisor has no unregister variant and this
  160. * hypercall does not allow to over-write info.mfn and
  161. * info.offset.
  162. */
  163. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, xen_vcpu_nr(cpu),
  164. &info);
  165. if (err)
  166. panic("register_vcpu_info failed: cpu=%d err=%d\n", cpu, err);
  167. per_cpu(xen_vcpu, cpu) = vcpup;
  168. }
  169. void __init xen_banner(void)
  170. {
  171. unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
  172. struct xen_extraversion extra;
  173. HYPERVISOR_xen_version(XENVER_extraversion, &extra);
  174. pr_info("Booting kernel on %s\n", pv_info.name);
  175. pr_info("Xen version: %u.%u%s%s\n",
  176. version >> 16, version & 0xffff, extra.extraversion,
  177. xen_feature(XENFEAT_mmu_pt_update_preserve_ad)
  178. ? " (preserve-AD)" : "");
  179. }
  180. /* Check if running on Xen version (major, minor) or later */
  181. bool xen_running_on_version_or_later(unsigned int major, unsigned int minor)
  182. {
  183. unsigned int version;
  184. if (!xen_domain())
  185. return false;
  186. version = HYPERVISOR_xen_version(XENVER_version, NULL);
  187. if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
  188. ((version >> 16) > major))
  189. return true;
  190. return false;
  191. }
  192. void __init xen_add_preferred_consoles(void)
  193. {
  194. add_preferred_console("xenboot", 0, NULL);
  195. if (!boot_params.screen_info.orig_video_isVGA)
  196. add_preferred_console("tty", 0, NULL);
  197. add_preferred_console("hvc", 0, NULL);
  198. if (boot_params.screen_info.orig_video_isVGA)
  199. add_preferred_console("tty", 0, NULL);
  200. }
  201. void xen_reboot(int reason)
  202. {
  203. struct sched_shutdown r = { .reason = reason };
  204. int cpu;
  205. for_each_online_cpu(cpu)
  206. xen_pmu_finish(cpu);
  207. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  208. BUG();
  209. }
  210. static int reboot_reason = SHUTDOWN_reboot;
  211. static bool xen_legacy_crash;
  212. void xen_emergency_restart(void)
  213. {
  214. xen_reboot(reboot_reason);
  215. }
  216. static int
  217. xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
  218. {
  219. if (!kexec_crash_loaded()) {
  220. if (xen_legacy_crash)
  221. xen_reboot(SHUTDOWN_crash);
  222. reboot_reason = SHUTDOWN_crash;
  223. /*
  224. * If panic_timeout==0 then we are supposed to wait forever.
  225. * However, to preserve original dom0 behavior we have to drop
  226. * into hypervisor. (domU behavior is controlled by its
  227. * config file)
  228. */
  229. if (panic_timeout == 0)
  230. panic_timeout = -1;
  231. }
  232. return NOTIFY_DONE;
  233. }
  234. static int __init parse_xen_legacy_crash(char *arg)
  235. {
  236. xen_legacy_crash = true;
  237. return 0;
  238. }
  239. early_param("xen_legacy_crash", parse_xen_legacy_crash);
  240. static struct notifier_block xen_panic_block = {
  241. .notifier_call = xen_panic_event,
  242. .priority = INT_MIN
  243. };
  244. int xen_panic_handler_init(void)
  245. {
  246. atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
  247. return 0;
  248. }
  249. void xen_pin_vcpu(int cpu)
  250. {
  251. static bool disable_pinning;
  252. struct sched_pin_override pin_override;
  253. int ret;
  254. if (disable_pinning)
  255. return;
  256. pin_override.pcpu = cpu;
  257. ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
  258. /* Ignore errors when removing override. */
  259. if (cpu < 0)
  260. return;
  261. switch (ret) {
  262. case -ENOSYS:
  263. pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
  264. cpu);
  265. disable_pinning = true;
  266. break;
  267. case -EPERM:
  268. WARN(1, "Trying to pin vcpu without having privilege to do so\n");
  269. disable_pinning = true;
  270. break;
  271. case -EINVAL:
  272. case -EBUSY:
  273. pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
  274. cpu);
  275. break;
  276. case 0:
  277. break;
  278. default:
  279. WARN(1, "rc %d while trying to pin vcpu\n", ret);
  280. disable_pinning = true;
  281. }
  282. }
  283. #ifdef CONFIG_HOTPLUG_CPU
  284. void xen_arch_register_cpu(int num)
  285. {
  286. arch_register_cpu(num);
  287. }
  288. EXPORT_SYMBOL(xen_arch_register_cpu);
  289. void xen_arch_unregister_cpu(int num)
  290. {
  291. arch_unregister_cpu(num);
  292. }
  293. EXPORT_SYMBOL(xen_arch_unregister_cpu);
  294. #endif