x86.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #ifndef ARCH_X86_KVM_X86_H
  2. #define ARCH_X86_KVM_X86_H
  3. #include <linux/kvm_host.h>
  4. #include <asm/pvclock.h>
  5. #include "kvm_cache_regs.h"
  6. #define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
  7. static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
  8. {
  9. vcpu->arch.exception.pending = false;
  10. }
  11. static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
  12. bool soft)
  13. {
  14. vcpu->arch.interrupt.pending = true;
  15. vcpu->arch.interrupt.soft = soft;
  16. vcpu->arch.interrupt.nr = vector;
  17. }
  18. static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
  19. {
  20. vcpu->arch.interrupt.pending = false;
  21. }
  22. static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
  23. {
  24. return vcpu->arch.exception.pending || vcpu->arch.interrupt.pending ||
  25. vcpu->arch.nmi_injected;
  26. }
  27. static inline bool kvm_exception_is_soft(unsigned int nr)
  28. {
  29. return (nr == BP_VECTOR) || (nr == OF_VECTOR);
  30. }
  31. static inline bool is_protmode(struct kvm_vcpu *vcpu)
  32. {
  33. return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
  34. }
  35. static inline int is_long_mode(struct kvm_vcpu *vcpu)
  36. {
  37. #ifdef CONFIG_X86_64
  38. return vcpu->arch.efer & EFER_LMA;
  39. #else
  40. return 0;
  41. #endif
  42. }
  43. static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
  44. {
  45. int cs_db, cs_l;
  46. if (!is_long_mode(vcpu))
  47. return false;
  48. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  49. return cs_l;
  50. }
  51. static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
  52. {
  53. return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
  54. }
  55. static inline int is_pae(struct kvm_vcpu *vcpu)
  56. {
  57. return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
  58. }
  59. static inline int is_pse(struct kvm_vcpu *vcpu)
  60. {
  61. return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
  62. }
  63. static inline int is_paging(struct kvm_vcpu *vcpu)
  64. {
  65. return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
  66. }
  67. static inline u32 bit(int bitno)
  68. {
  69. return 1 << (bitno & 31);
  70. }
  71. static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
  72. gva_t gva, gfn_t gfn, unsigned access)
  73. {
  74. vcpu->arch.mmio_gva = gva & PAGE_MASK;
  75. vcpu->arch.access = access;
  76. vcpu->arch.mmio_gfn = gfn;
  77. vcpu->arch.mmio_gen = kvm_memslots(vcpu->kvm)->generation;
  78. }
  79. static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
  80. {
  81. return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
  82. }
  83. /*
  84. * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
  85. * clear all mmio cache info.
  86. */
  87. #define MMIO_GVA_ANY (~(gva_t)0)
  88. static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
  89. {
  90. if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
  91. return;
  92. vcpu->arch.mmio_gva = 0;
  93. }
  94. static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
  95. {
  96. if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
  97. vcpu->arch.mmio_gva == (gva & PAGE_MASK))
  98. return true;
  99. return false;
  100. }
  101. static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
  102. {
  103. if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
  104. vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
  105. return true;
  106. return false;
  107. }
  108. static inline unsigned long kvm_register_readl(struct kvm_vcpu *vcpu,
  109. enum kvm_reg reg)
  110. {
  111. unsigned long val = kvm_register_read(vcpu, reg);
  112. return is_64_bit_mode(vcpu) ? val : (u32)val;
  113. }
  114. static inline void kvm_register_writel(struct kvm_vcpu *vcpu,
  115. enum kvm_reg reg,
  116. unsigned long val)
  117. {
  118. if (!is_64_bit_mode(vcpu))
  119. val = (u32)val;
  120. return kvm_register_write(vcpu, reg, val);
  121. }
  122. static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
  123. {
  124. return !(kvm->arch.disabled_quirks & quirk);
  125. }
  126. void kvm_before_handle_nmi(struct kvm_vcpu *vcpu);
  127. void kvm_after_handle_nmi(struct kvm_vcpu *vcpu);
  128. void kvm_set_pending_timer(struct kvm_vcpu *vcpu);
  129. int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
  130. void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr);
  131. u64 get_kvmclock_ns(struct kvm *kvm);
  132. int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
  133. gva_t addr, void *val, unsigned int bytes,
  134. struct x86_exception *exception);
  135. int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
  136. gva_t addr, void *val, unsigned int bytes,
  137. struct x86_exception *exception);
  138. void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
  139. u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
  140. bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  141. int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  142. int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  143. bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
  144. int page_num);
  145. bool kvm_vector_hashing_enabled(void);
  146. #define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
  147. | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
  148. | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
  149. | XFEATURE_MASK_PKRU)
  150. extern u64 host_xcr0;
  151. extern u64 kvm_supported_xcr0(void);
  152. extern unsigned int min_timer_period_us;
  153. extern unsigned int lapic_timer_advance_ns;
  154. extern struct static_key kvm_no_apic_vcpu;
  155. static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
  156. {
  157. return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
  158. vcpu->arch.virtual_tsc_shift);
  159. }
  160. /* Same "calling convention" as do_div:
  161. * - divide (n << 32) by base
  162. * - put result in n
  163. * - return remainder
  164. */
  165. #define do_shl32_div32(n, base) \
  166. ({ \
  167. u32 __quot, __rem; \
  168. asm("divl %2" : "=a" (__quot), "=d" (__rem) \
  169. : "rm" (base), "0" (0), "1" ((u32) n)); \
  170. n = __quot; \
  171. __rem; \
  172. })
  173. #endif