enlighten_hvm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/acpi.h>
  3. #include <linux/cpu.h>
  4. #include <linux/kexec.h>
  5. #include <linux/memblock.h>
  6. #include <linux/virtio_anchor.h>
  7. #include <xen/features.h>
  8. #include <xen/events.h>
  9. #include <xen/hvm.h>
  10. #include <xen/interface/hvm/hvm_op.h>
  11. #include <xen/interface/memory.h>
  12. #include <asm/apic.h>
  13. #include <asm/cpu.h>
  14. #include <asm/smp.h>
  15. #include <asm/io_apic.h>
  16. #include <asm/reboot.h>
  17. #include <asm/setup.h>
  18. #include <asm/idtentry.h>
  19. #include <asm/hypervisor.h>
  20. #include <asm/e820/api.h>
  21. #include <asm/early_ioremap.h>
  22. #include <asm/xen/cpuid.h>
  23. #include <asm/xen/hypervisor.h>
  24. #include <asm/xen/page.h>
  25. #include "xen-ops.h"
  26. #include "mmu.h"
  27. #include "smp.h"
  28. static unsigned long shared_info_pfn;
  29. __ro_after_init bool xen_percpu_upcall;
  30. EXPORT_SYMBOL_GPL(xen_percpu_upcall);
  31. void xen_hvm_init_shared_info(void)
  32. {
  33. struct xen_add_to_physmap xatp;
  34. xatp.domid = DOMID_SELF;
  35. xatp.idx = 0;
  36. xatp.space = XENMAPSPACE_shared_info;
  37. xatp.gpfn = shared_info_pfn;
  38. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  39. BUG();
  40. }
  41. static void __init reserve_shared_info(void)
  42. {
  43. u64 pa;
  44. /*
  45. * Search for a free page starting at 4kB physical address.
  46. * Low memory is preferred to avoid an EPT large page split up
  47. * by the mapping.
  48. * Starting below X86_RESERVE_LOW (usually 64kB) is fine as
  49. * the BIOS used for HVM guests is well behaved and won't
  50. * clobber memory other than the first 4kB.
  51. */
  52. for (pa = PAGE_SIZE;
  53. !e820__mapped_all(pa, pa + PAGE_SIZE, E820_TYPE_RAM) ||
  54. memblock_is_reserved(pa);
  55. pa += PAGE_SIZE)
  56. ;
  57. shared_info_pfn = PHYS_PFN(pa);
  58. memblock_reserve(pa, PAGE_SIZE);
  59. HYPERVISOR_shared_info = early_memremap(pa, PAGE_SIZE);
  60. }
  61. static void __init xen_hvm_init_mem_mapping(void)
  62. {
  63. early_memunmap(HYPERVISOR_shared_info, PAGE_SIZE);
  64. HYPERVISOR_shared_info = __va(PFN_PHYS(shared_info_pfn));
  65. /*
  66. * The virtual address of the shared_info page has changed, so
  67. * the vcpu_info pointer for VCPU 0 is now stale.
  68. *
  69. * The prepare_boot_cpu callback will re-initialize it via
  70. * xen_vcpu_setup, but we can't rely on that to be called for
  71. * old Xen versions (xen_have_vector_callback == 0).
  72. *
  73. * It is, in any case, bad to have a stale vcpu_info pointer
  74. * so reset it now.
  75. */
  76. xen_vcpu_info_reset(0);
  77. }
  78. static void __init init_hvm_pv_info(void)
  79. {
  80. int major, minor;
  81. uint32_t eax, ebx, ecx, edx, base;
  82. base = xen_cpuid_base();
  83. eax = cpuid_eax(base + 1);
  84. major = eax >> 16;
  85. minor = eax & 0xffff;
  86. printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
  87. xen_domain_type = XEN_HVM_DOMAIN;
  88. /* PVH set up hypercall page in xen_prepare_pvh(). */
  89. if (xen_pvh_domain())
  90. pv_info.name = "Xen PVH";
  91. else {
  92. u64 pfn;
  93. uint32_t msr;
  94. pv_info.name = "Xen HVM";
  95. msr = cpuid_ebx(base + 2);
  96. pfn = __pa(hypercall_page);
  97. wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
  98. }
  99. xen_setup_features();
  100. cpuid(base + 4, &eax, &ebx, &ecx, &edx);
  101. if (eax & XEN_HVM_CPUID_VCPU_ID_PRESENT)
  102. this_cpu_write(xen_vcpu_id, ebx);
  103. else
  104. this_cpu_write(xen_vcpu_id, smp_processor_id());
  105. }
  106. DEFINE_IDTENTRY_SYSVEC(sysvec_xen_hvm_callback)
  107. {
  108. struct pt_regs *old_regs = set_irq_regs(regs);
  109. if (xen_percpu_upcall)
  110. ack_APIC_irq();
  111. inc_irq_stat(irq_hv_callback_count);
  112. xen_evtchn_do_upcall();
  113. set_irq_regs(old_regs);
  114. }
  115. #ifdef CONFIG_KEXEC_CORE
  116. static void xen_hvm_shutdown(void)
  117. {
  118. native_machine_shutdown();
  119. if (kexec_in_progress)
  120. xen_reboot(SHUTDOWN_soft_reset);
  121. }
  122. static void xen_hvm_crash_shutdown(struct pt_regs *regs)
  123. {
  124. native_machine_crash_shutdown(regs);
  125. xen_reboot(SHUTDOWN_soft_reset);
  126. }
  127. #endif
  128. static int xen_cpu_up_prepare_hvm(unsigned int cpu)
  129. {
  130. int rc = 0;
  131. /*
  132. * This can happen if CPU was offlined earlier and
  133. * offlining timed out in common_cpu_die().
  134. */
  135. if (cpu_report_state(cpu) == CPU_DEAD_FROZEN) {
  136. xen_smp_intr_free(cpu);
  137. xen_uninit_lock_cpu(cpu);
  138. }
  139. if (cpu_acpi_id(cpu) != U32_MAX)
  140. per_cpu(xen_vcpu_id, cpu) = cpu_acpi_id(cpu);
  141. else
  142. per_cpu(xen_vcpu_id, cpu) = cpu;
  143. xen_vcpu_setup(cpu);
  144. if (!xen_have_vector_callback)
  145. return 0;
  146. if (xen_percpu_upcall) {
  147. rc = xen_set_upcall_vector(cpu);
  148. if (rc) {
  149. WARN(1, "HVMOP_set_evtchn_upcall_vector"
  150. " for CPU %d failed: %d\n", cpu, rc);
  151. return rc;
  152. }
  153. }
  154. if (xen_feature(XENFEAT_hvm_safe_pvclock))
  155. xen_setup_timer(cpu);
  156. rc = xen_smp_intr_init(cpu);
  157. if (rc) {
  158. WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
  159. cpu, rc);
  160. }
  161. return rc;
  162. }
  163. static int xen_cpu_dead_hvm(unsigned int cpu)
  164. {
  165. xen_smp_intr_free(cpu);
  166. if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
  167. xen_teardown_timer(cpu);
  168. return 0;
  169. }
  170. static void __init xen_hvm_guest_init(void)
  171. {
  172. if (xen_pv_domain())
  173. return;
  174. if (IS_ENABLED(CONFIG_XEN_VIRTIO_FORCE_GRANT))
  175. virtio_set_mem_acc_cb(xen_virtio_restricted_mem_acc);
  176. init_hvm_pv_info();
  177. reserve_shared_info();
  178. xen_hvm_init_shared_info();
  179. /*
  180. * xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  181. * page, we use it in the event channel upcall and in some pvclock
  182. * related functions.
  183. */
  184. xen_vcpu_info_reset(0);
  185. xen_panic_handler_init();
  186. xen_hvm_smp_init();
  187. WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_hvm, xen_cpu_dead_hvm));
  188. xen_unplug_emulated_devices();
  189. x86_init.irqs.intr_init = xen_init_IRQ;
  190. xen_hvm_init_time_ops();
  191. xen_hvm_init_mmu_ops();
  192. #ifdef CONFIG_KEXEC_CORE
  193. machine_ops.shutdown = xen_hvm_shutdown;
  194. machine_ops.crash_shutdown = xen_hvm_crash_shutdown;
  195. #endif
  196. }
  197. static __init int xen_parse_nopv(char *arg)
  198. {
  199. pr_notice("\"xen_nopv\" is deprecated, please use \"nopv\" instead\n");
  200. if (xen_cpuid_base())
  201. nopv = true;
  202. return 0;
  203. }
  204. early_param("xen_nopv", xen_parse_nopv);
  205. static __init int xen_parse_no_vector_callback(char *arg)
  206. {
  207. xen_have_vector_callback = false;
  208. return 0;
  209. }
  210. early_param("xen_no_vector_callback", xen_parse_no_vector_callback);
  211. static __init bool xen_x2apic_available(void)
  212. {
  213. return x2apic_supported();
  214. }
  215. static bool __init msi_ext_dest_id(void)
  216. {
  217. return cpuid_eax(xen_cpuid_base() + 4) & XEN_HVM_CPUID_EXT_DEST_ID;
  218. }
  219. static __init void xen_hvm_guest_late_init(void)
  220. {
  221. #ifdef CONFIG_XEN_PVH
  222. /* Test for PVH domain (PVH boot path taken overrides ACPI flags). */
  223. if (!xen_pvh &&
  224. (x86_platform.legacy.rtc || !x86_platform.legacy.no_vga))
  225. return;
  226. /* PVH detected. */
  227. xen_pvh = true;
  228. if (nopv)
  229. panic("\"nopv\" and \"xen_nopv\" parameters are unsupported in PVH guest.");
  230. /* Make sure we don't fall back to (default) ACPI_IRQ_MODEL_PIC. */
  231. if (!nr_ioapics && acpi_irq_model == ACPI_IRQ_MODEL_PIC)
  232. acpi_irq_model = ACPI_IRQ_MODEL_PLATFORM;
  233. machine_ops.emergency_restart = xen_emergency_restart;
  234. pv_info.name = "Xen PVH";
  235. #endif
  236. }
  237. static uint32_t __init xen_platform_hvm(void)
  238. {
  239. uint32_t xen_domain = xen_cpuid_base();
  240. struct x86_hyper_init *h = &x86_hyper_xen_hvm.init;
  241. if (xen_pv_domain())
  242. return 0;
  243. if (xen_pvh_domain() && nopv) {
  244. /* Guest booting via the Xen-PVH boot entry goes here */
  245. pr_info("\"nopv\" parameter is ignored in PVH guest\n");
  246. nopv = false;
  247. } else if (nopv && xen_domain) {
  248. /*
  249. * Guest booting via normal boot entry (like via grub2) goes
  250. * here.
  251. *
  252. * Use interface functions for bare hardware if nopv,
  253. * xen_hvm_guest_late_init is an exception as we need to
  254. * detect PVH and panic there.
  255. */
  256. h->init_platform = x86_init_noop;
  257. h->x2apic_available = bool_x86_init_noop;
  258. h->init_mem_mapping = x86_init_noop;
  259. h->init_after_bootmem = x86_init_noop;
  260. h->guest_late_init = xen_hvm_guest_late_init;
  261. x86_hyper_xen_hvm.runtime.pin_vcpu = x86_op_int_noop;
  262. }
  263. return xen_domain;
  264. }
  265. struct hypervisor_x86 x86_hyper_xen_hvm __initdata = {
  266. .name = "Xen HVM",
  267. .detect = xen_platform_hvm,
  268. .type = X86_HYPER_XEN_HVM,
  269. .init.init_platform = xen_hvm_guest_init,
  270. .init.x2apic_available = xen_x2apic_available,
  271. .init.init_mem_mapping = xen_hvm_init_mem_mapping,
  272. .init.guest_late_init = xen_hvm_guest_late_init,
  273. .init.msi_ext_dest_id = msi_ext_dest_id,
  274. .runtime.pin_vcpu = xen_pin_vcpu,
  275. .ignore_nopv = true,
  276. };