enlighten.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 <xen/interface/memory.h>
  12. #include <asm/xen/hypercall.h>
  13. #include <asm/xen/hypervisor.h>
  14. #include <asm/cpu.h>
  15. #include <asm/e820/api.h>
  16. #include "xen-ops.h"
  17. #include "smp.h"
  18. #include "pmu.h"
  19. EXPORT_SYMBOL_GPL(hypercall_page);
  20. /*
  21. * Pointer to the xen_vcpu_info structure or
  22. * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
  23. * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
  24. * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point
  25. * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to
  26. * acknowledge pending events.
  27. * Also more subtly it is used by the patched version of irq enable/disable
  28. * e.g. xen_irq_enable_direct and xen_iret in PV mode.
  29. *
  30. * The desire to be able to do those mask/unmask operations as a single
  31. * instruction by using the per-cpu offset held in %gs is the real reason
  32. * vcpu info is in a per-cpu pointer and the original reason for this
  33. * hypercall.
  34. *
  35. */
  36. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  37. /*
  38. * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info
  39. * hypercall. This can be used both in PV and PVHVM mode. The structure
  40. * overrides the default per_cpu(xen_vcpu, cpu) value.
  41. */
  42. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  43. /* Linux <-> Xen vCPU id mapping */
  44. DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
  45. EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
  46. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  47. EXPORT_SYMBOL_GPL(xen_domain_type);
  48. unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
  49. EXPORT_SYMBOL(machine_to_phys_mapping);
  50. unsigned long machine_to_phys_nr;
  51. EXPORT_SYMBOL(machine_to_phys_nr);
  52. struct start_info *xen_start_info;
  53. EXPORT_SYMBOL_GPL(xen_start_info);
  54. struct shared_info xen_dummy_shared_info;
  55. __read_mostly int xen_have_vector_callback;
  56. EXPORT_SYMBOL_GPL(xen_have_vector_callback);
  57. /*
  58. * NB: needs to live in .data because it's used by xen_prepare_pvh which runs
  59. * before clearing the bss.
  60. */
  61. uint32_t xen_start_flags __attribute__((section(".data"))) = 0;
  62. EXPORT_SYMBOL(xen_start_flags);
  63. /*
  64. * Point at some empty memory to start with. We map the real shared_info
  65. * page as soon as fixmap is up and running.
  66. */
  67. struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
  68. /*
  69. * Flag to determine whether vcpu info placement is available on all
  70. * VCPUs. We assume it is to start with, and then set it to zero on
  71. * the first failure. This is because it can succeed on some VCPUs
  72. * and not others, since it can involve hypervisor memory allocation,
  73. * or because the guest failed to guarantee all the appropriate
  74. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  75. *
  76. * Note that any particular CPU may be using a placed vcpu structure,
  77. * but we can only optimise if the all are.
  78. *
  79. * 0: not available, 1: available
  80. */
  81. int xen_have_vcpu_info_placement = 1;
  82. static int xen_cpu_up_online(unsigned int cpu)
  83. {
  84. xen_init_lock_cpu(cpu);
  85. return 0;
  86. }
  87. int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
  88. int (*cpu_dead_cb)(unsigned int))
  89. {
  90. int rc;
  91. rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
  92. "x86/xen/guest:prepare",
  93. cpu_up_prepare_cb, cpu_dead_cb);
  94. if (rc >= 0) {
  95. rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  96. "x86/xen/guest:online",
  97. xen_cpu_up_online, NULL);
  98. if (rc < 0)
  99. cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
  100. }
  101. return rc >= 0 ? 0 : rc;
  102. }
  103. static int xen_vcpu_setup_restore(int cpu)
  104. {
  105. int rc = 0;
  106. /* Any per_cpu(xen_vcpu) is stale, so reset it */
  107. xen_vcpu_info_reset(cpu);
  108. /*
  109. * For PVH and PVHVM, setup online VCPUs only. The rest will
  110. * be handled by hotplug.
  111. */
  112. if (xen_pv_domain() ||
  113. (xen_hvm_domain() && cpu_online(cpu))) {
  114. rc = xen_vcpu_setup(cpu);
  115. }
  116. return rc;
  117. }
  118. /*
  119. * On restore, set the vcpu placement up again.
  120. * If it fails, then we're in a bad state, since
  121. * we can't back out from using it...
  122. */
  123. void xen_vcpu_restore(void)
  124. {
  125. int cpu, rc;
  126. for_each_possible_cpu(cpu) {
  127. bool other_cpu = (cpu != smp_processor_id());
  128. bool is_up;
  129. if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
  130. continue;
  131. /* Only Xen 4.5 and higher support this. */
  132. is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
  133. xen_vcpu_nr(cpu), NULL) > 0;
  134. if (other_cpu && is_up &&
  135. HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
  136. BUG();
  137. if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
  138. xen_setup_runstate_info(cpu);
  139. rc = xen_vcpu_setup_restore(cpu);
  140. if (rc)
  141. pr_emerg_once("vcpu restore failed for cpu=%d err=%d. "
  142. "System will hang.\n", cpu, rc);
  143. /*
  144. * In case xen_vcpu_setup_restore() fails, do not bring up the
  145. * VCPU. This helps us avoid the resulting OOPS when the VCPU
  146. * accesses pvclock_vcpu_time via xen_vcpu (which is NULL.)
  147. * Note that this does not improve the situation much -- now the
  148. * VM hangs instead of OOPSing -- with the VCPUs that did not
  149. * fail, spinning in stop_machine(), waiting for the failed
  150. * VCPUs to come up.
  151. */
  152. if (other_cpu && is_up && (rc == 0) &&
  153. HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
  154. BUG();
  155. }
  156. }
  157. void xen_vcpu_info_reset(int cpu)
  158. {
  159. if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
  160. per_cpu(xen_vcpu, cpu) =
  161. &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
  162. } else {
  163. /* Set to NULL so that if somebody accesses it we get an OOPS */
  164. per_cpu(xen_vcpu, cpu) = NULL;
  165. }
  166. }
  167. int xen_vcpu_setup(int cpu)
  168. {
  169. struct vcpu_register_vcpu_info info;
  170. int err;
  171. struct vcpu_info *vcpup;
  172. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  173. /*
  174. * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu)
  175. * and at restore (xen_vcpu_restore). Also called for hotplugged
  176. * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm).
  177. * However, the hypercall can only be done once (see below) so if a VCPU
  178. * is offlined and comes back online then let's not redo the hypercall.
  179. *
  180. * For PV it is called during restore (xen_vcpu_restore) and bootup
  181. * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
  182. * use this function.
  183. */
  184. if (xen_hvm_domain()) {
  185. if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
  186. return 0;
  187. }
  188. if (xen_have_vcpu_info_placement) {
  189. vcpup = &per_cpu(xen_vcpu_info, cpu);
  190. info.mfn = arbitrary_virt_to_mfn(vcpup);
  191. info.offset = offset_in_page(vcpup);
  192. /*
  193. * Check to see if the hypervisor will put the vcpu_info
  194. * structure where we want it, which allows direct access via
  195. * a percpu-variable.
  196. * N.B. This hypercall can _only_ be called once per CPU.
  197. * Subsequent calls will error out with -EINVAL. This is due to
  198. * the fact that hypervisor has no unregister variant and this
  199. * hypercall does not allow to over-write info.mfn and
  200. * info.offset.
  201. */
  202. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
  203. xen_vcpu_nr(cpu), &info);
  204. if (err) {
  205. pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n",
  206. cpu, err);
  207. xen_have_vcpu_info_placement = 0;
  208. } else {
  209. /*
  210. * This cpu is using the registered vcpu info, even if
  211. * later ones fail to.
  212. */
  213. per_cpu(xen_vcpu, cpu) = vcpup;
  214. }
  215. }
  216. if (!xen_have_vcpu_info_placement)
  217. xen_vcpu_info_reset(cpu);
  218. return ((per_cpu(xen_vcpu, cpu) == NULL) ? -ENODEV : 0);
  219. }
  220. void xen_reboot(int reason)
  221. {
  222. struct sched_shutdown r = { .reason = reason };
  223. int cpu;
  224. for_each_online_cpu(cpu)
  225. xen_pmu_finish(cpu);
  226. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  227. BUG();
  228. }
  229. void xen_emergency_restart(void)
  230. {
  231. xen_reboot(SHUTDOWN_reboot);
  232. }
  233. static int
  234. xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
  235. {
  236. if (!kexec_crash_loaded())
  237. xen_reboot(SHUTDOWN_crash);
  238. return NOTIFY_DONE;
  239. }
  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
  295. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  296. void __init arch_xen_balloon_init(struct resource *hostmem_resource)
  297. {
  298. struct xen_memory_map memmap;
  299. int rc;
  300. unsigned int i, last_guest_ram;
  301. phys_addr_t max_addr = PFN_PHYS(max_pfn);
  302. struct e820_table *xen_e820_table;
  303. const struct e820_entry *entry;
  304. struct resource *res;
  305. if (!xen_initial_domain())
  306. return;
  307. xen_e820_table = kmalloc(sizeof(*xen_e820_table), GFP_KERNEL);
  308. if (!xen_e820_table)
  309. return;
  310. memmap.nr_entries = ARRAY_SIZE(xen_e820_table->entries);
  311. set_xen_guest_handle(memmap.buffer, xen_e820_table->entries);
  312. rc = HYPERVISOR_memory_op(XENMEM_machine_memory_map, &memmap);
  313. if (rc) {
  314. pr_warn("%s: Can't read host e820 (%d)\n", __func__, rc);
  315. goto out;
  316. }
  317. last_guest_ram = 0;
  318. for (i = 0; i < memmap.nr_entries; i++) {
  319. if (xen_e820_table->entries[i].addr >= max_addr)
  320. break;
  321. if (xen_e820_table->entries[i].type == E820_TYPE_RAM)
  322. last_guest_ram = i;
  323. }
  324. entry = &xen_e820_table->entries[last_guest_ram];
  325. if (max_addr >= entry->addr + entry->size)
  326. goto out; /* No unallocated host RAM. */
  327. hostmem_resource->start = max_addr;
  328. hostmem_resource->end = entry->addr + entry->size;
  329. /*
  330. * Mark non-RAM regions between the end of dom0 RAM and end of host RAM
  331. * as unavailable. The rest of that region can be used for hotplug-based
  332. * ballooning.
  333. */
  334. for (; i < memmap.nr_entries; i++) {
  335. entry = &xen_e820_table->entries[i];
  336. if (entry->type == E820_TYPE_RAM)
  337. continue;
  338. if (entry->addr >= hostmem_resource->end)
  339. break;
  340. res = kzalloc(sizeof(*res), GFP_KERNEL);
  341. if (!res)
  342. goto out;
  343. res->name = "Unavailable host RAM";
  344. res->start = entry->addr;
  345. res->end = (entry->addr + entry->size < hostmem_resource->end) ?
  346. entry->addr + entry->size : hostmem_resource->end;
  347. rc = insert_resource(hostmem_resource, res);
  348. if (rc) {
  349. pr_warn("%s: Can't insert [%llx - %llx) (%d)\n",
  350. __func__, res->start, res->end, rc);
  351. kfree(res);
  352. goto out;
  353. }
  354. }
  355. out:
  356. kfree(xen_e820_table);
  357. }
  358. #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */