enlighten_hvm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <linux/cpu.h>
  2. #include <linux/kexec.h>
  3. #include <linux/memblock.h>
  4. #include <xen/features.h>
  5. #include <xen/events.h>
  6. #include <xen/interface/memory.h>
  7. #include <asm/cpu.h>
  8. #include <asm/smp.h>
  9. #include <asm/reboot.h>
  10. #include <asm/setup.h>
  11. #include <asm/hypervisor.h>
  12. #include <asm/e820/api.h>
  13. #include <asm/xen/cpuid.h>
  14. #include <asm/xen/hypervisor.h>
  15. #include <asm/xen/page.h>
  16. #include "xen-ops.h"
  17. #include "mmu.h"
  18. #include "smp.h"
  19. void __ref xen_hvm_init_shared_info(void)
  20. {
  21. struct xen_add_to_physmap xatp;
  22. u64 pa;
  23. if (HYPERVISOR_shared_info == &xen_dummy_shared_info) {
  24. /*
  25. * Search for a free page starting at 4kB physical address.
  26. * Low memory is preferred to avoid an EPT large page split up
  27. * by the mapping.
  28. * Starting below X86_RESERVE_LOW (usually 64kB) is fine as
  29. * the BIOS used for HVM guests is well behaved and won't
  30. * clobber memory other than the first 4kB.
  31. */
  32. for (pa = PAGE_SIZE;
  33. !e820__mapped_all(pa, pa + PAGE_SIZE, E820_TYPE_RAM) ||
  34. memblock_is_reserved(pa);
  35. pa += PAGE_SIZE)
  36. ;
  37. memblock_reserve(pa, PAGE_SIZE);
  38. HYPERVISOR_shared_info = __va(pa);
  39. }
  40. xatp.domid = DOMID_SELF;
  41. xatp.idx = 0;
  42. xatp.space = XENMAPSPACE_shared_info;
  43. xatp.gpfn = virt_to_pfn(HYPERVISOR_shared_info);
  44. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  45. BUG();
  46. }
  47. static void __init init_hvm_pv_info(void)
  48. {
  49. int major, minor;
  50. uint32_t eax, ebx, ecx, edx, base;
  51. base = xen_cpuid_base();
  52. eax = cpuid_eax(base + 1);
  53. major = eax >> 16;
  54. minor = eax & 0xffff;
  55. printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
  56. xen_domain_type = XEN_HVM_DOMAIN;
  57. /* PVH set up hypercall page in xen_prepare_pvh(). */
  58. if (xen_pvh_domain())
  59. pv_info.name = "Xen PVH";
  60. else {
  61. u64 pfn;
  62. uint32_t msr;
  63. pv_info.name = "Xen HVM";
  64. msr = cpuid_ebx(base + 2);
  65. pfn = __pa(hypercall_page);
  66. wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
  67. }
  68. xen_setup_features();
  69. cpuid(base + 4, &eax, &ebx, &ecx, &edx);
  70. if (eax & XEN_HVM_CPUID_VCPU_ID_PRESENT)
  71. this_cpu_write(xen_vcpu_id, ebx);
  72. else
  73. this_cpu_write(xen_vcpu_id, smp_processor_id());
  74. }
  75. #ifdef CONFIG_KEXEC_CORE
  76. static void xen_hvm_shutdown(void)
  77. {
  78. native_machine_shutdown();
  79. if (kexec_in_progress)
  80. xen_reboot(SHUTDOWN_soft_reset);
  81. }
  82. static void xen_hvm_crash_shutdown(struct pt_regs *regs)
  83. {
  84. native_machine_crash_shutdown(regs);
  85. xen_reboot(SHUTDOWN_soft_reset);
  86. }
  87. #endif
  88. static int xen_cpu_up_prepare_hvm(unsigned int cpu)
  89. {
  90. int rc = 0;
  91. /*
  92. * This can happen if CPU was offlined earlier and
  93. * offlining timed out in common_cpu_die().
  94. */
  95. if (cpu_report_state(cpu) == CPU_DEAD_FROZEN) {
  96. xen_smp_intr_free(cpu);
  97. xen_uninit_lock_cpu(cpu);
  98. }
  99. if (cpu_acpi_id(cpu) != U32_MAX)
  100. per_cpu(xen_vcpu_id, cpu) = cpu_acpi_id(cpu);
  101. else
  102. per_cpu(xen_vcpu_id, cpu) = cpu;
  103. rc = xen_vcpu_setup(cpu);
  104. if (rc)
  105. return rc;
  106. if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
  107. xen_setup_timer(cpu);
  108. rc = xen_smp_intr_init(cpu);
  109. if (rc) {
  110. WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
  111. cpu, rc);
  112. }
  113. return rc;
  114. }
  115. static int xen_cpu_dead_hvm(unsigned int cpu)
  116. {
  117. xen_smp_intr_free(cpu);
  118. if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
  119. xen_teardown_timer(cpu);
  120. return 0;
  121. }
  122. static void __init xen_hvm_guest_init(void)
  123. {
  124. if (xen_pv_domain())
  125. return;
  126. init_hvm_pv_info();
  127. xen_hvm_init_shared_info();
  128. /*
  129. * xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  130. * page, we use it in the event channel upcall and in some pvclock
  131. * related functions.
  132. */
  133. xen_vcpu_info_reset(0);
  134. xen_panic_handler_init();
  135. if (xen_feature(XENFEAT_hvm_callback_vector))
  136. xen_have_vector_callback = 1;
  137. xen_hvm_smp_init();
  138. WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_hvm, xen_cpu_dead_hvm));
  139. xen_unplug_emulated_devices();
  140. x86_init.irqs.intr_init = xen_init_IRQ;
  141. xen_hvm_init_time_ops();
  142. xen_hvm_init_mmu_ops();
  143. if (xen_pvh_domain())
  144. machine_ops.emergency_restart = xen_emergency_restart;
  145. #ifdef CONFIG_KEXEC_CORE
  146. machine_ops.shutdown = xen_hvm_shutdown;
  147. machine_ops.crash_shutdown = xen_hvm_crash_shutdown;
  148. #endif
  149. }
  150. static bool xen_nopv;
  151. static __init int xen_parse_nopv(char *arg)
  152. {
  153. xen_nopv = true;
  154. return 0;
  155. }
  156. early_param("xen_nopv", xen_parse_nopv);
  157. bool xen_hvm_need_lapic(void)
  158. {
  159. if (xen_nopv)
  160. return false;
  161. if (xen_pv_domain())
  162. return false;
  163. if (!xen_hvm_domain())
  164. return false;
  165. if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
  166. return false;
  167. return true;
  168. }
  169. EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
  170. static uint32_t __init xen_platform_hvm(void)
  171. {
  172. if (xen_pv_domain() || xen_nopv)
  173. return 0;
  174. return xen_cpuid_base();
  175. }
  176. const struct hypervisor_x86 x86_hyper_xen_hvm = {
  177. .name = "Xen HVM",
  178. .detect = xen_platform_hvm,
  179. .init_platform = xen_hvm_guest_init,
  180. .pin_vcpu = xen_pin_vcpu,
  181. .x2apic_available = xen_x2apic_para_available,
  182. };
  183. EXPORT_SYMBOL(x86_hyper_xen_hvm);