enlighten.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <xen/features.h>
  7. #include <xen/page.h>
  8. #include <xen/interface/memory.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. * Point at some empty memory to start with. We map the real shared_info
  56. * page as soon as fixmap is up and running.
  57. */
  58. struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
  59. /*
  60. * Flag to determine whether vcpu info placement is available on all
  61. * VCPUs. We assume it is to start with, and then set it to zero on
  62. * the first failure. This is because it can succeed on some VCPUs
  63. * and not others, since it can involve hypervisor memory allocation,
  64. * or because the guest failed to guarantee all the appropriate
  65. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  66. *
  67. * Note that any particular CPU may be using a placed vcpu structure,
  68. * but we can only optimise if the all are.
  69. *
  70. * 0: not available, 1: available
  71. */
  72. int xen_have_vcpu_info_placement = 1;
  73. static int xen_cpu_up_online(unsigned int cpu)
  74. {
  75. xen_init_lock_cpu(cpu);
  76. return 0;
  77. }
  78. int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
  79. int (*cpu_dead_cb)(unsigned int))
  80. {
  81. int rc;
  82. rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
  83. "x86/xen/guest:prepare",
  84. cpu_up_prepare_cb, cpu_dead_cb);
  85. if (rc >= 0) {
  86. rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  87. "x86/xen/guest:online",
  88. xen_cpu_up_online, NULL);
  89. if (rc < 0)
  90. cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
  91. }
  92. return rc >= 0 ? 0 : rc;
  93. }
  94. static int xen_vcpu_setup_restore(int cpu)
  95. {
  96. int rc = 0;
  97. /* Any per_cpu(xen_vcpu) is stale, so reset it */
  98. xen_vcpu_info_reset(cpu);
  99. /*
  100. * For PVH and PVHVM, setup online VCPUs only. The rest will
  101. * be handled by hotplug.
  102. */
  103. if (xen_pv_domain() ||
  104. (xen_hvm_domain() && cpu_online(cpu))) {
  105. rc = xen_vcpu_setup(cpu);
  106. }
  107. return rc;
  108. }
  109. /*
  110. * On restore, set the vcpu placement up again.
  111. * If it fails, then we're in a bad state, since
  112. * we can't back out from using it...
  113. */
  114. void xen_vcpu_restore(void)
  115. {
  116. int cpu, rc;
  117. for_each_possible_cpu(cpu) {
  118. bool other_cpu = (cpu != smp_processor_id());
  119. bool is_up;
  120. if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
  121. continue;
  122. /* Only Xen 4.5 and higher support this. */
  123. is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
  124. xen_vcpu_nr(cpu), NULL) > 0;
  125. if (other_cpu && is_up &&
  126. HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
  127. BUG();
  128. if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
  129. xen_setup_runstate_info(cpu);
  130. rc = xen_vcpu_setup_restore(cpu);
  131. if (rc)
  132. pr_emerg_once("vcpu restore failed for cpu=%d err=%d. "
  133. "System will hang.\n", cpu, rc);
  134. /*
  135. * In case xen_vcpu_setup_restore() fails, do not bring up the
  136. * VCPU. This helps us avoid the resulting OOPS when the VCPU
  137. * accesses pvclock_vcpu_time via xen_vcpu (which is NULL.)
  138. * Note that this does not improve the situation much -- now the
  139. * VM hangs instead of OOPSing -- with the VCPUs that did not
  140. * fail, spinning in stop_machine(), waiting for the failed
  141. * VCPUs to come up.
  142. */
  143. if (other_cpu && is_up && (rc == 0) &&
  144. HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
  145. BUG();
  146. }
  147. }
  148. void xen_vcpu_info_reset(int cpu)
  149. {
  150. if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
  151. per_cpu(xen_vcpu, cpu) =
  152. &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
  153. } else {
  154. /* Set to NULL so that if somebody accesses it we get an OOPS */
  155. per_cpu(xen_vcpu, cpu) = NULL;
  156. }
  157. }
  158. int xen_vcpu_setup(int cpu)
  159. {
  160. struct vcpu_register_vcpu_info info;
  161. int err;
  162. struct vcpu_info *vcpup;
  163. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  164. /*
  165. * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu)
  166. * and at restore (xen_vcpu_restore). Also called for hotplugged
  167. * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm).
  168. * However, the hypercall can only be done once (see below) so if a VCPU
  169. * is offlined and comes back online then let's not redo the hypercall.
  170. *
  171. * For PV it is called during restore (xen_vcpu_restore) and bootup
  172. * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
  173. * use this function.
  174. */
  175. if (xen_hvm_domain()) {
  176. if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
  177. return 0;
  178. }
  179. if (xen_have_vcpu_info_placement) {
  180. vcpup = &per_cpu(xen_vcpu_info, cpu);
  181. info.mfn = arbitrary_virt_to_mfn(vcpup);
  182. info.offset = offset_in_page(vcpup);
  183. /*
  184. * Check to see if the hypervisor will put the vcpu_info
  185. * structure where we want it, which allows direct access via
  186. * a percpu-variable.
  187. * N.B. This hypercall can _only_ be called once per CPU.
  188. * Subsequent calls will error out with -EINVAL. This is due to
  189. * the fact that hypervisor has no unregister variant and this
  190. * hypercall does not allow to over-write info.mfn and
  191. * info.offset.
  192. */
  193. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
  194. xen_vcpu_nr(cpu), &info);
  195. if (err) {
  196. pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n",
  197. cpu, err);
  198. xen_have_vcpu_info_placement = 0;
  199. } else {
  200. /*
  201. * This cpu is using the registered vcpu info, even if
  202. * later ones fail to.
  203. */
  204. per_cpu(xen_vcpu, cpu) = vcpup;
  205. }
  206. }
  207. if (!xen_have_vcpu_info_placement)
  208. xen_vcpu_info_reset(cpu);
  209. return ((per_cpu(xen_vcpu, cpu) == NULL) ? -ENODEV : 0);
  210. }
  211. void xen_reboot(int reason)
  212. {
  213. struct sched_shutdown r = { .reason = reason };
  214. int cpu;
  215. for_each_online_cpu(cpu)
  216. xen_pmu_finish(cpu);
  217. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  218. BUG();
  219. }
  220. void xen_emergency_restart(void)
  221. {
  222. xen_reboot(SHUTDOWN_reboot);
  223. }
  224. static int
  225. xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
  226. {
  227. if (!kexec_crash_loaded())
  228. xen_reboot(SHUTDOWN_crash);
  229. return NOTIFY_DONE;
  230. }
  231. static struct notifier_block xen_panic_block = {
  232. .notifier_call = xen_panic_event,
  233. .priority = INT_MIN
  234. };
  235. int xen_panic_handler_init(void)
  236. {
  237. atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
  238. return 0;
  239. }
  240. void xen_pin_vcpu(int cpu)
  241. {
  242. static bool disable_pinning;
  243. struct sched_pin_override pin_override;
  244. int ret;
  245. if (disable_pinning)
  246. return;
  247. pin_override.pcpu = cpu;
  248. ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
  249. /* Ignore errors when removing override. */
  250. if (cpu < 0)
  251. return;
  252. switch (ret) {
  253. case -ENOSYS:
  254. pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
  255. cpu);
  256. disable_pinning = true;
  257. break;
  258. case -EPERM:
  259. WARN(1, "Trying to pin vcpu without having privilege to do so\n");
  260. disable_pinning = true;
  261. break;
  262. case -EINVAL:
  263. case -EBUSY:
  264. pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
  265. cpu);
  266. break;
  267. case 0:
  268. break;
  269. default:
  270. WARN(1, "rc %d while trying to pin vcpu\n", ret);
  271. disable_pinning = true;
  272. }
  273. }
  274. #ifdef CONFIG_HOTPLUG_CPU
  275. void xen_arch_register_cpu(int num)
  276. {
  277. arch_register_cpu(num);
  278. }
  279. EXPORT_SYMBOL(xen_arch_register_cpu);
  280. void xen_arch_unregister_cpu(int num)
  281. {
  282. arch_unregister_cpu(num);
  283. }
  284. EXPORT_SYMBOL(xen_arch_unregister_cpu);
  285. #endif
  286. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  287. void __init arch_xen_balloon_init(struct resource *hostmem_resource)
  288. {
  289. struct xen_memory_map memmap;
  290. int rc;
  291. unsigned int i, last_guest_ram;
  292. phys_addr_t max_addr = PFN_PHYS(max_pfn);
  293. struct e820_table *xen_e820_table;
  294. const struct e820_entry *entry;
  295. struct resource *res;
  296. if (!xen_initial_domain())
  297. return;
  298. xen_e820_table = kmalloc(sizeof(*xen_e820_table), GFP_KERNEL);
  299. if (!xen_e820_table)
  300. return;
  301. memmap.nr_entries = ARRAY_SIZE(xen_e820_table->entries);
  302. set_xen_guest_handle(memmap.buffer, xen_e820_table->entries);
  303. rc = HYPERVISOR_memory_op(XENMEM_machine_memory_map, &memmap);
  304. if (rc) {
  305. pr_warn("%s: Can't read host e820 (%d)\n", __func__, rc);
  306. goto out;
  307. }
  308. last_guest_ram = 0;
  309. for (i = 0; i < memmap.nr_entries; i++) {
  310. if (xen_e820_table->entries[i].addr >= max_addr)
  311. break;
  312. if (xen_e820_table->entries[i].type == E820_TYPE_RAM)
  313. last_guest_ram = i;
  314. }
  315. entry = &xen_e820_table->entries[last_guest_ram];
  316. if (max_addr >= entry->addr + entry->size)
  317. goto out; /* No unallocated host RAM. */
  318. hostmem_resource->start = max_addr;
  319. hostmem_resource->end = entry->addr + entry->size;
  320. /*
  321. * Mark non-RAM regions between the end of dom0 RAM and end of host RAM
  322. * as unavailable. The rest of that region can be used for hotplug-based
  323. * ballooning.
  324. */
  325. for (; i < memmap.nr_entries; i++) {
  326. entry = &xen_e820_table->entries[i];
  327. if (entry->type == E820_TYPE_RAM)
  328. continue;
  329. if (entry->addr >= hostmem_resource->end)
  330. break;
  331. res = kzalloc(sizeof(*res), GFP_KERNEL);
  332. if (!res)
  333. goto out;
  334. res->name = "Unavailable host RAM";
  335. res->start = entry->addr;
  336. res->end = (entry->addr + entry->size < hostmem_resource->end) ?
  337. entry->addr + entry->size : hostmem_resource->end;
  338. rc = insert_resource(hostmem_resource, res);
  339. if (rc) {
  340. pr_warn("%s: Can't insert [%llx - %llx) (%d)\n",
  341. __func__, res->start, res->end, rc);
  342. kfree(res);
  343. goto out;
  344. }
  345. }
  346. out:
  347. kfree(xen_e820_table);
  348. }
  349. #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */