hyperv.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * KVM Microsoft Hyper-V emulation
  4. *
  5. * derived from arch/x86/kvm/x86.c
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright (C) 2008 Qumranet, Inc.
  9. * Copyright IBM Corporation, 2008
  10. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  11. * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
  12. *
  13. * Authors:
  14. * Avi Kivity <avi@qumranet.com>
  15. * Yaniv Kamay <yaniv@qumranet.com>
  16. * Amit Shah <amit.shah@qumranet.com>
  17. * Ben-Ami Yassour <benami@il.ibm.com>
  18. * Andrey Smetanin <asmetanin@virtuozzo.com>
  19. */
  20. #ifndef __ARCH_X86_KVM_HYPERV_H__
  21. #define __ARCH_X86_KVM_HYPERV_H__
  22. #include <linux/kvm_host.h>
  23. #include "x86.h"
  24. #ifdef CONFIG_KVM_HYPERV
  25. /* "Hv#1" signature */
  26. #define HYPERV_CPUID_SIGNATURE_EAX 0x31237648
  27. /*
  28. * The #defines related to the synthetic debugger are required by KDNet, but
  29. * they are not documented in the Hyper-V TLFS because the synthetic debugger
  30. * functionality has been deprecated and is subject to removal in future
  31. * versions of Windows.
  32. */
  33. #define HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS 0x40000080
  34. #define HYPERV_CPUID_SYNDBG_INTERFACE 0x40000081
  35. #define HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES 0x40000082
  36. /*
  37. * Hyper-V synthetic debugger platform capabilities
  38. * These are HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES.EAX bits.
  39. */
  40. #define HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING BIT(1)
  41. /* Hyper-V Synthetic debug options MSR */
  42. #define HV_X64_MSR_SYNDBG_CONTROL 0x400000F1
  43. #define HV_X64_MSR_SYNDBG_STATUS 0x400000F2
  44. #define HV_X64_MSR_SYNDBG_SEND_BUFFER 0x400000F3
  45. #define HV_X64_MSR_SYNDBG_RECV_BUFFER 0x400000F4
  46. #define HV_X64_MSR_SYNDBG_PENDING_BUFFER 0x400000F5
  47. #define HV_X64_MSR_SYNDBG_OPTIONS 0x400000FF
  48. /* Hyper-V HV_X64_MSR_SYNDBG_OPTIONS bits */
  49. #define HV_X64_SYNDBG_OPTION_USE_HCALLS BIT(2)
  50. static inline struct kvm_hv *to_kvm_hv(struct kvm *kvm)
  51. {
  52. return &kvm->arch.hyperv;
  53. }
  54. static inline struct kvm_vcpu_hv *to_hv_vcpu(struct kvm_vcpu *vcpu)
  55. {
  56. return vcpu->arch.hyperv;
  57. }
  58. static inline struct kvm_vcpu_hv_synic *to_hv_synic(struct kvm_vcpu *vcpu)
  59. {
  60. struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
  61. return &hv_vcpu->synic;
  62. }
  63. static inline struct kvm_vcpu *hv_synic_to_vcpu(struct kvm_vcpu_hv_synic *synic)
  64. {
  65. struct kvm_vcpu_hv *hv_vcpu = container_of(synic, struct kvm_vcpu_hv, synic);
  66. return hv_vcpu->vcpu;
  67. }
  68. static inline struct kvm_hv_syndbg *to_hv_syndbg(struct kvm_vcpu *vcpu)
  69. {
  70. return &vcpu->kvm->arch.hyperv.hv_syndbg;
  71. }
  72. static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu)
  73. {
  74. struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
  75. return hv_vcpu ? hv_vcpu->vp_index : vcpu->vcpu_idx;
  76. }
  77. int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host);
  78. int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host);
  79. static inline bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu)
  80. {
  81. return vcpu->arch.hyperv_enabled && to_kvm_hv(vcpu->kvm)->hv_guest_os_id;
  82. }
  83. int kvm_hv_hypercall(struct kvm_vcpu *vcpu);
  84. void kvm_hv_irq_routing_update(struct kvm *kvm);
  85. int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint);
  86. void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector);
  87. int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages);
  88. static inline bool kvm_hv_synic_has_vector(struct kvm_vcpu *vcpu, int vector)
  89. {
  90. return to_hv_vcpu(vcpu) && test_bit(vector, to_hv_synic(vcpu)->vec_bitmap);
  91. }
  92. static inline bool kvm_hv_synic_auto_eoi_set(struct kvm_vcpu *vcpu, int vector)
  93. {
  94. return to_hv_vcpu(vcpu) &&
  95. test_bit(vector, to_hv_synic(vcpu)->auto_eoi_bitmap);
  96. }
  97. void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu);
  98. bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu);
  99. int kvm_hv_get_assist_page(struct kvm_vcpu *vcpu);
  100. static inline struct kvm_vcpu_hv_stimer *to_hv_stimer(struct kvm_vcpu *vcpu,
  101. int timer_index)
  102. {
  103. return &to_hv_vcpu(vcpu)->stimer[timer_index];
  104. }
  105. static inline struct kvm_vcpu *hv_stimer_to_vcpu(struct kvm_vcpu_hv_stimer *stimer)
  106. {
  107. struct kvm_vcpu_hv *hv_vcpu;
  108. hv_vcpu = container_of(stimer - stimer->index, struct kvm_vcpu_hv,
  109. stimer[0]);
  110. return hv_vcpu->vcpu;
  111. }
  112. static inline bool kvm_hv_has_stimer_pending(struct kvm_vcpu *vcpu)
  113. {
  114. struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
  115. if (!hv_vcpu)
  116. return false;
  117. return !bitmap_empty(hv_vcpu->stimer_pending_bitmap,
  118. HV_SYNIC_STIMER_COUNT);
  119. }
  120. /*
  121. * With HV_ACCESS_TSC_INVARIANT feature, invariant TSC (CPUID.80000007H:EDX[8])
  122. * is only observed after HV_X64_MSR_TSC_INVARIANT_CONTROL was written to.
  123. */
  124. static inline bool kvm_hv_invtsc_suppressed(struct kvm_vcpu *vcpu)
  125. {
  126. struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
  127. /*
  128. * If Hyper-V's invariant TSC control is not exposed to the guest,
  129. * the invariant TSC CPUID flag is not suppressed, Windows guests were
  130. * observed to be able to handle it correctly. Going forward, VMMs are
  131. * encouraged to enable Hyper-V's invariant TSC control when invariant
  132. * TSC CPUID flag is set to make KVM's behavior match genuine Hyper-V.
  133. */
  134. if (!hv_vcpu ||
  135. !(hv_vcpu->cpuid_cache.features_eax & HV_ACCESS_TSC_INVARIANT))
  136. return false;
  137. /*
  138. * If Hyper-V's invariant TSC control is exposed to the guest, KVM is
  139. * responsible for suppressing the invariant TSC CPUID flag if the
  140. * Hyper-V control is not enabled.
  141. */
  142. return !(to_kvm_hv(vcpu->kvm)->hv_invtsc_control & HV_EXPOSE_INVARIANT_TSC);
  143. }
  144. void kvm_hv_process_stimers(struct kvm_vcpu *vcpu);
  145. void kvm_hv_setup_tsc_page(struct kvm *kvm,
  146. struct pvclock_vcpu_time_info *hv_clock);
  147. void kvm_hv_request_tsc_page_update(struct kvm *kvm);
  148. void kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu);
  149. void kvm_hv_init_vm(struct kvm *kvm);
  150. void kvm_hv_destroy_vm(struct kvm *kvm);
  151. int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu);
  152. void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu, bool hyperv_enabled);
  153. int kvm_hv_set_enforce_cpuid(struct kvm_vcpu *vcpu, bool enforce);
  154. int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args);
  155. int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
  156. struct kvm_cpuid_entry2 __user *entries);
  157. static inline struct kvm_vcpu_hv_tlb_flush_fifo *kvm_hv_get_tlb_flush_fifo(struct kvm_vcpu *vcpu,
  158. bool is_guest_mode)
  159. {
  160. struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
  161. int i = is_guest_mode ? HV_L2_TLB_FLUSH_FIFO :
  162. HV_L1_TLB_FLUSH_FIFO;
  163. return &hv_vcpu->tlb_flush_fifo[i];
  164. }
  165. static inline void kvm_hv_vcpu_purge_flush_tlb(struct kvm_vcpu *vcpu)
  166. {
  167. struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
  168. if (!to_hv_vcpu(vcpu) || !kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
  169. return;
  170. tlb_flush_fifo = kvm_hv_get_tlb_flush_fifo(vcpu, is_guest_mode(vcpu));
  171. kfifo_reset_out(&tlb_flush_fifo->entries);
  172. }
  173. static inline bool guest_hv_cpuid_has_l2_tlb_flush(struct kvm_vcpu *vcpu)
  174. {
  175. struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
  176. return hv_vcpu &&
  177. (hv_vcpu->cpuid_cache.nested_eax & HV_X64_NESTED_DIRECT_FLUSH);
  178. }
  179. static inline bool kvm_hv_is_tlb_flush_hcall(struct kvm_vcpu *vcpu)
  180. {
  181. struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
  182. u16 code;
  183. if (!hv_vcpu)
  184. return false;
  185. code = is_64_bit_hypercall(vcpu) ? kvm_rcx_read(vcpu) :
  186. kvm_rax_read(vcpu);
  187. return (code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE ||
  188. code == HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST ||
  189. code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX ||
  190. code == HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX);
  191. }
  192. static inline int kvm_hv_verify_vp_assist(struct kvm_vcpu *vcpu)
  193. {
  194. if (!to_hv_vcpu(vcpu))
  195. return 0;
  196. if (!kvm_hv_assist_page_enabled(vcpu))
  197. return 0;
  198. return kvm_hv_get_assist_page(vcpu);
  199. }
  200. static inline void kvm_hv_nested_transtion_tlb_flush(struct kvm_vcpu *vcpu,
  201. bool tdp_enabled)
  202. {
  203. /*
  204. * KVM_REQ_HV_TLB_FLUSH flushes entries from either L1's VP_ID or
  205. * L2's VP_ID upon request from the guest. Make sure we check for
  206. * pending entries in the right FIFO upon L1/L2 transition as these
  207. * requests are put by other vCPUs asynchronously.
  208. */
  209. if (to_hv_vcpu(vcpu) && tdp_enabled)
  210. kvm_make_request(KVM_REQ_HV_TLB_FLUSH, vcpu);
  211. }
  212. int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu);
  213. #else /* CONFIG_KVM_HYPERV */
  214. static inline void kvm_hv_setup_tsc_page(struct kvm *kvm,
  215. struct pvclock_vcpu_time_info *hv_clock) {}
  216. static inline void kvm_hv_request_tsc_page_update(struct kvm *kvm) {}
  217. static inline void kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu) {}
  218. static inline void kvm_hv_init_vm(struct kvm *kvm) {}
  219. static inline void kvm_hv_destroy_vm(struct kvm *kvm) {}
  220. static inline int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
  221. {
  222. return 0;
  223. }
  224. static inline void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) {}
  225. static inline bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu)
  226. {
  227. return false;
  228. }
  229. static inline int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
  230. {
  231. return HV_STATUS_ACCESS_DENIED;
  232. }
  233. static inline void kvm_hv_vcpu_purge_flush_tlb(struct kvm_vcpu *vcpu) {}
  234. static inline bool kvm_hv_synic_has_vector(struct kvm_vcpu *vcpu, int vector)
  235. {
  236. return false;
  237. }
  238. static inline bool kvm_hv_synic_auto_eoi_set(struct kvm_vcpu *vcpu, int vector)
  239. {
  240. return false;
  241. }
  242. static inline void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) {}
  243. static inline bool kvm_hv_invtsc_suppressed(struct kvm_vcpu *vcpu)
  244. {
  245. return false;
  246. }
  247. static inline void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu, bool hyperv_enabled) {}
  248. static inline bool kvm_hv_has_stimer_pending(struct kvm_vcpu *vcpu)
  249. {
  250. return false;
  251. }
  252. static inline bool kvm_hv_is_tlb_flush_hcall(struct kvm_vcpu *vcpu)
  253. {
  254. return false;
  255. }
  256. static inline bool guest_hv_cpuid_has_l2_tlb_flush(struct kvm_vcpu *vcpu)
  257. {
  258. return false;
  259. }
  260. static inline int kvm_hv_verify_vp_assist(struct kvm_vcpu *vcpu)
  261. {
  262. return 0;
  263. }
  264. static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu)
  265. {
  266. return vcpu->vcpu_idx;
  267. }
  268. static inline void kvm_hv_nested_transtion_tlb_flush(struct kvm_vcpu *vcpu, bool tdp_enabled) {}
  269. #endif /* CONFIG_KVM_HYPERV */
  270. #endif /* __ARCH_X86_KVM_HYPERV_H__ */