enlighten_hvm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <linux/cpu.h>
  2. #include <linux/kexec.h>
  3. #include <xen/features.h>
  4. #include <xen/events.h>
  5. #include <xen/interface/memory.h>
  6. #include <asm/cpu.h>
  7. #include <asm/smp.h>
  8. #include <asm/reboot.h>
  9. #include <asm/setup.h>
  10. #include <asm/hypervisor.h>
  11. #include <asm/xen/cpuid.h>
  12. #include <asm/xen/hypervisor.h>
  13. #include "xen-ops.h"
  14. #include "mmu.h"
  15. #include "smp.h"
  16. void __ref xen_hvm_init_shared_info(void)
  17. {
  18. int cpu;
  19. struct xen_add_to_physmap xatp;
  20. static struct shared_info *shared_info_page;
  21. if (!shared_info_page)
  22. shared_info_page = (struct shared_info *)
  23. extend_brk(PAGE_SIZE, PAGE_SIZE);
  24. xatp.domid = DOMID_SELF;
  25. xatp.idx = 0;
  26. xatp.space = XENMAPSPACE_shared_info;
  27. xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
  28. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  29. BUG();
  30. HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
  31. /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  32. * page, we use it in the event channel upcall and in some pvclock
  33. * related functions. We don't need the vcpu_info placement
  34. * optimizations because we don't use any pv_mmu or pv_irq op on
  35. * HVM.
  36. * When xen_hvm_init_shared_info is run at boot time only vcpu 0 is
  37. * online but xen_hvm_init_shared_info is run at resume time too and
  38. * in that case multiple vcpus might be online. */
  39. for_each_online_cpu(cpu) {
  40. /* Leave it to be NULL. */
  41. if (xen_vcpu_nr(cpu) >= MAX_VIRT_CPUS)
  42. continue;
  43. per_cpu(xen_vcpu, cpu) =
  44. &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
  45. }
  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;
  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. xen_vcpu_setup(cpu);
  104. if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
  105. xen_setup_timer(cpu);
  106. rc = xen_smp_intr_init(cpu);
  107. if (rc) {
  108. WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
  109. cpu, rc);
  110. return rc;
  111. }
  112. return 0;
  113. }
  114. static int xen_cpu_dead_hvm(unsigned int cpu)
  115. {
  116. xen_smp_intr_free(cpu);
  117. if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
  118. xen_teardown_timer(cpu);
  119. return 0;
  120. }
  121. static void __init xen_hvm_guest_init(void)
  122. {
  123. if (xen_pv_domain())
  124. return;
  125. init_hvm_pv_info();
  126. xen_hvm_init_shared_info();
  127. xen_panic_handler_init();
  128. if (xen_feature(XENFEAT_hvm_callback_vector))
  129. xen_have_vector_callback = 1;
  130. xen_hvm_smp_init();
  131. WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_hvm, xen_cpu_dead_hvm));
  132. xen_unplug_emulated_devices();
  133. x86_init.irqs.intr_init = xen_init_IRQ;
  134. xen_hvm_init_time_ops();
  135. xen_hvm_init_mmu_ops();
  136. if (xen_pvh_domain())
  137. machine_ops.emergency_restart = xen_emergency_restart;
  138. #ifdef CONFIG_KEXEC_CORE
  139. machine_ops.shutdown = xen_hvm_shutdown;
  140. machine_ops.crash_shutdown = xen_hvm_crash_shutdown;
  141. #endif
  142. }
  143. static bool xen_nopv;
  144. static __init int xen_parse_nopv(char *arg)
  145. {
  146. xen_nopv = true;
  147. return 0;
  148. }
  149. early_param("xen_nopv", xen_parse_nopv);
  150. bool xen_hvm_need_lapic(void)
  151. {
  152. if (xen_nopv)
  153. return false;
  154. if (xen_pv_domain())
  155. return false;
  156. if (!xen_hvm_domain())
  157. return false;
  158. if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
  159. return false;
  160. return true;
  161. }
  162. EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
  163. static uint32_t __init xen_platform_hvm(void)
  164. {
  165. if (xen_pv_domain() || xen_nopv)
  166. return 0;
  167. return xen_cpuid_base();
  168. }
  169. const struct hypervisor_x86 x86_hyper_xen_hvm = {
  170. .name = "Xen HVM",
  171. .detect = xen_platform_hvm,
  172. .init_platform = xen_hvm_guest_init,
  173. .pin_vcpu = xen_pin_vcpu,
  174. .x2apic_available = xen_x2apic_para_available,
  175. };
  176. EXPORT_SYMBOL(x86_hyper_xen_hvm);