1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 226eef70cSAvi Kivity #ifndef ARCH_X86_KVM_X86_H 326eef70cSAvi Kivity #define ARCH_X86_KVM_X86_H 426eef70cSAvi Kivity 526eef70cSAvi Kivity #include <linux/kvm_host.h> 63f1a18b9SUros Bizjak #include <asm/mce.h> 78d93c874SMarcelo Tosatti #include <asm/pvclock.h> 83eeb3288SAvi Kivity #include "kvm_cache_regs.h" 92f728d66SSean Christopherson #include "kvm_emulate.h" 1026eef70cSAvi Kivity 11c8e88717SBabu Moger #define KVM_DEFAULT_PLE_GAP 128 12c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 13c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_GROW 2 14c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_SHRINK 0 15c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX UINT_MAX 168566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX USHRT_MAX 178566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW 3000 18c8e88717SBabu Moger 19c8e88717SBabu Moger static inline unsigned int __grow_ple_window(unsigned int val, 20c8e88717SBabu Moger unsigned int base, unsigned int modifier, unsigned int max) 21c8e88717SBabu Moger { 22c8e88717SBabu Moger u64 ret = val; 23c8e88717SBabu Moger 24c8e88717SBabu Moger if (modifier < 1) 25c8e88717SBabu Moger return base; 26c8e88717SBabu Moger 27c8e88717SBabu Moger if (modifier < base) 28c8e88717SBabu Moger ret *= modifier; 29c8e88717SBabu Moger else 30c8e88717SBabu Moger ret += modifier; 31c8e88717SBabu Moger 32c8e88717SBabu Moger return min(ret, (u64)max); 33c8e88717SBabu Moger } 34c8e88717SBabu Moger 35c8e88717SBabu Moger static inline unsigned int __shrink_ple_window(unsigned int val, 36c8e88717SBabu Moger unsigned int base, unsigned int modifier, unsigned int min) 37c8e88717SBabu Moger { 38c8e88717SBabu Moger if (modifier < 1) 39c8e88717SBabu Moger return base; 40c8e88717SBabu Moger 41c8e88717SBabu Moger if (modifier < base) 42c8e88717SBabu Moger val /= modifier; 43c8e88717SBabu Moger else 44c8e88717SBabu Moger val -= modifier; 45c8e88717SBabu Moger 46c8e88717SBabu Moger return max(val, min); 47c8e88717SBabu Moger } 48c8e88717SBabu Moger 4974545705SRadim Krčmář #define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL 5074545705SRadim Krčmář 5126eef70cSAvi Kivity static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu) 5226eef70cSAvi Kivity { 535c7d4f9aSLiran Alon vcpu->arch.exception.pending = false; 54664f8e26SWanpeng Li vcpu->arch.exception.injected = false; 5526eef70cSAvi Kivity } 5626eef70cSAvi Kivity 5766fd3f7fSGleb Natapov static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector, 5866fd3f7fSGleb Natapov bool soft) 59937a7eaeSAvi Kivity { 6004140b41SLiran Alon vcpu->arch.interrupt.injected = true; 6166fd3f7fSGleb Natapov vcpu->arch.interrupt.soft = soft; 62937a7eaeSAvi Kivity vcpu->arch.interrupt.nr = vector; 63937a7eaeSAvi Kivity } 64937a7eaeSAvi Kivity 65937a7eaeSAvi Kivity static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu) 66937a7eaeSAvi Kivity { 6704140b41SLiran Alon vcpu->arch.interrupt.injected = false; 68937a7eaeSAvi Kivity } 69937a7eaeSAvi Kivity 703298b75cSGleb Natapov static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu) 713298b75cSGleb Natapov { 7204140b41SLiran Alon return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected || 733298b75cSGleb Natapov vcpu->arch.nmi_injected; 743298b75cSGleb Natapov } 7566fd3f7fSGleb Natapov 7666fd3f7fSGleb Natapov static inline bool kvm_exception_is_soft(unsigned int nr) 7766fd3f7fSGleb Natapov { 7866fd3f7fSGleb Natapov return (nr == BP_VECTOR) || (nr == OF_VECTOR); 7966fd3f7fSGleb Natapov } 80fc61b800SGleb Natapov 813eeb3288SAvi Kivity static inline bool is_protmode(struct kvm_vcpu *vcpu) 823eeb3288SAvi Kivity { 833eeb3288SAvi Kivity return kvm_read_cr0_bits(vcpu, X86_CR0_PE); 843eeb3288SAvi Kivity } 853eeb3288SAvi Kivity 86836a1b3cSAvi Kivity static inline int is_long_mode(struct kvm_vcpu *vcpu) 87836a1b3cSAvi Kivity { 88836a1b3cSAvi Kivity #ifdef CONFIG_X86_64 89f6801dffSAvi Kivity return vcpu->arch.efer & EFER_LMA; 90836a1b3cSAvi Kivity #else 91836a1b3cSAvi Kivity return 0; 92836a1b3cSAvi Kivity #endif 93836a1b3cSAvi Kivity } 94836a1b3cSAvi Kivity 955777392eSNadav Amit static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu) 965777392eSNadav Amit { 975777392eSNadav Amit int cs_db, cs_l; 985777392eSNadav Amit 995777392eSNadav Amit if (!is_long_mode(vcpu)) 1005777392eSNadav Amit return false; 101afaf0b2fSSean Christopherson kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l); 1025777392eSNadav Amit return cs_l; 1035777392eSNadav Amit } 1045777392eSNadav Amit 105855feb67SYu Zhang static inline bool is_la57_mode(struct kvm_vcpu *vcpu) 106855feb67SYu Zhang { 107855feb67SYu Zhang #ifdef CONFIG_X86_64 108855feb67SYu Zhang return (vcpu->arch.efer & EFER_LMA) && 109855feb67SYu Zhang kvm_read_cr4_bits(vcpu, X86_CR4_LA57); 110855feb67SYu Zhang #else 111855feb67SYu Zhang return 0; 112855feb67SYu Zhang #endif 113855feb67SYu Zhang } 114855feb67SYu Zhang 1150447378aSMarc Orr static inline bool x86_exception_has_error_code(unsigned int vector) 1160447378aSMarc Orr { 1170447378aSMarc Orr static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) | 1180447378aSMarc Orr BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) | 1190447378aSMarc Orr BIT(PF_VECTOR) | BIT(AC_VECTOR); 1200447378aSMarc Orr 1210447378aSMarc Orr return (1U << vector) & exception_has_error_code; 1220447378aSMarc Orr } 1230447378aSMarc Orr 1246539e738SJoerg Roedel static inline bool mmu_is_nested(struct kvm_vcpu *vcpu) 1256539e738SJoerg Roedel { 1266539e738SJoerg Roedel return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu; 1276539e738SJoerg Roedel } 1286539e738SJoerg Roedel 129eeeb4f67SSean Christopherson static inline void kvm_vcpu_flush_tlb_current(struct kvm_vcpu *vcpu) 130eeeb4f67SSean Christopherson { 131eeeb4f67SSean Christopherson ++vcpu->stat.tlb_flush; 132eeeb4f67SSean Christopherson kvm_x86_ops.tlb_flush_current(vcpu); 133eeeb4f67SSean Christopherson } 134eeeb4f67SSean Christopherson 135836a1b3cSAvi Kivity static inline int is_pae(struct kvm_vcpu *vcpu) 136836a1b3cSAvi Kivity { 137836a1b3cSAvi Kivity return kvm_read_cr4_bits(vcpu, X86_CR4_PAE); 138836a1b3cSAvi Kivity } 139836a1b3cSAvi Kivity 140836a1b3cSAvi Kivity static inline int is_pse(struct kvm_vcpu *vcpu) 141836a1b3cSAvi Kivity { 142836a1b3cSAvi Kivity return kvm_read_cr4_bits(vcpu, X86_CR4_PSE); 143836a1b3cSAvi Kivity } 144836a1b3cSAvi Kivity 145836a1b3cSAvi Kivity static inline int is_paging(struct kvm_vcpu *vcpu) 146836a1b3cSAvi Kivity { 147c36fc04eSDavidlohr Bueso return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG)); 148836a1b3cSAvi Kivity } 149836a1b3cSAvi Kivity 150bf03d4f9SPaolo Bonzini static inline bool is_pae_paging(struct kvm_vcpu *vcpu) 151bf03d4f9SPaolo Bonzini { 152bf03d4f9SPaolo Bonzini return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu); 153bf03d4f9SPaolo Bonzini } 154bf03d4f9SPaolo Bonzini 155fd8cb433SYu Zhang static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu) 156fd8cb433SYu Zhang { 157fd8cb433SYu Zhang return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48; 158fd8cb433SYu Zhang } 159fd8cb433SYu Zhang 160fd8cb433SYu Zhang static inline u64 get_canonical(u64 la, u8 vaddr_bits) 161fd8cb433SYu Zhang { 162fd8cb433SYu Zhang return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits); 163fd8cb433SYu Zhang } 164fd8cb433SYu Zhang 165fd8cb433SYu Zhang static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu) 166fd8cb433SYu Zhang { 167fd8cb433SYu Zhang return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la; 168fd8cb433SYu Zhang } 169fd8cb433SYu Zhang 170bebb106aSXiao Guangrong static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu, 171bebb106aSXiao Guangrong gva_t gva, gfn_t gfn, unsigned access) 172bebb106aSXiao Guangrong { 173ddfd1730SSean Christopherson u64 gen = kvm_memslots(vcpu->kvm)->generation; 174ddfd1730SSean Christopherson 175361209e0SSean Christopherson if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS)) 176ddfd1730SSean Christopherson return; 177ddfd1730SSean Christopherson 1789034e6e8SPaolo Bonzini /* 1799034e6e8SPaolo Bonzini * If this is a shadow nested page table, the "GVA" is 1809034e6e8SPaolo Bonzini * actually a nGPA. 1819034e6e8SPaolo Bonzini */ 1829034e6e8SPaolo Bonzini vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK; 183871bd034SSean Christopherson vcpu->arch.mmio_access = access; 184bebb106aSXiao Guangrong vcpu->arch.mmio_gfn = gfn; 185ddfd1730SSean Christopherson vcpu->arch.mmio_gen = gen; 18656f17dd3SDavid Matlack } 18756f17dd3SDavid Matlack 18856f17dd3SDavid Matlack static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu) 18956f17dd3SDavid Matlack { 19056f17dd3SDavid Matlack return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation; 191bebb106aSXiao Guangrong } 192bebb106aSXiao Guangrong 193bebb106aSXiao Guangrong /* 19456f17dd3SDavid Matlack * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we 19556f17dd3SDavid Matlack * clear all mmio cache info. 196bebb106aSXiao Guangrong */ 19756f17dd3SDavid Matlack #define MMIO_GVA_ANY (~(gva_t)0) 19856f17dd3SDavid Matlack 199bebb106aSXiao Guangrong static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva) 200bebb106aSXiao Guangrong { 20156f17dd3SDavid Matlack if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK)) 202bebb106aSXiao Guangrong return; 203bebb106aSXiao Guangrong 204bebb106aSXiao Guangrong vcpu->arch.mmio_gva = 0; 205bebb106aSXiao Guangrong } 206bebb106aSXiao Guangrong 207bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva) 208bebb106aSXiao Guangrong { 20956f17dd3SDavid Matlack if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva && 21056f17dd3SDavid Matlack vcpu->arch.mmio_gva == (gva & PAGE_MASK)) 211bebb106aSXiao Guangrong return true; 212bebb106aSXiao Guangrong 213bebb106aSXiao Guangrong return false; 214bebb106aSXiao Guangrong } 215bebb106aSXiao Guangrong 216bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa) 217bebb106aSXiao Guangrong { 21856f17dd3SDavid Matlack if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn && 21956f17dd3SDavid Matlack vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT) 220bebb106aSXiao Guangrong return true; 221bebb106aSXiao Guangrong 222bebb106aSXiao Guangrong return false; 223bebb106aSXiao Guangrong } 224bebb106aSXiao Guangrong 225489cbcf0SSean Christopherson static inline unsigned long kvm_register_readl(struct kvm_vcpu *vcpu, int reg) 2265777392eSNadav Amit { 2275777392eSNadav Amit unsigned long val = kvm_register_read(vcpu, reg); 2285777392eSNadav Amit 2295777392eSNadav Amit return is_64_bit_mode(vcpu) ? val : (u32)val; 2305777392eSNadav Amit } 2315777392eSNadav Amit 23227e6fb5dSNadav Amit static inline void kvm_register_writel(struct kvm_vcpu *vcpu, 233489cbcf0SSean Christopherson int reg, unsigned long val) 23427e6fb5dSNadav Amit { 23527e6fb5dSNadav Amit if (!is_64_bit_mode(vcpu)) 23627e6fb5dSNadav Amit val = (u32)val; 23727e6fb5dSNadav Amit return kvm_register_write(vcpu, reg, val); 23827e6fb5dSNadav Amit } 23927e6fb5dSNadav Amit 24041dbc6bcSPaolo Bonzini static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk) 24141dbc6bcSPaolo Bonzini { 24241dbc6bcSPaolo Bonzini return !(kvm->arch.disabled_quirks & quirk); 24341dbc6bcSPaolo Bonzini } 24441dbc6bcSPaolo Bonzini 24527cbe7d6SLiran Alon static inline bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu) 24627cbe7d6SLiran Alon { 247afaf0b2fSSean Christopherson return is_smm(vcpu) || kvm_x86_ops.apic_init_signal_blocked(vcpu); 24827cbe7d6SLiran Alon } 24927cbe7d6SLiran Alon 2509497e1f2SSean Christopherson void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip); 251ff9d07a0SZhang, Yanmin 2528fe8ab46SWill Auld void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr); 253108b249cSPaolo Bonzini u64 get_kvmclock_ns(struct kvm *kvm); 25499e3e30aSZachary Amsden 255ce14e868SPaolo Bonzini int kvm_read_guest_virt(struct kvm_vcpu *vcpu, 256064aea77SNadav Har'El gva_t addr, void *val, unsigned int bytes, 257064aea77SNadav Har'El struct x86_exception *exception); 258064aea77SNadav Har'El 259ce14e868SPaolo Bonzini int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, 2606a4d7550SNadav Har'El gva_t addr, void *val, unsigned int bytes, 2616a4d7550SNadav Har'El struct x86_exception *exception); 2626a4d7550SNadav Har'El 263082d06edSWanpeng Li int handle_ud(struct kvm_vcpu *vcpu); 264082d06edSWanpeng Li 265da998b46SJim Mattson void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu); 266da998b46SJim Mattson 26719efffa2SXiao Guangrong void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu); 268ff53604bSXiao Guangrong u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn); 2694566654bSNadav Amit bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data); 270ff53604bSXiao Guangrong int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data); 271ff53604bSXiao Guangrong int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata); 2726a39bbc5SXiao Guangrong bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, 2736a39bbc5SXiao Guangrong int page_num); 27452004014SFeng Wu bool kvm_vector_hashing_enabled(void); 27589786147SMohammed Gamal void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code); 276736c291cSSean Christopherson int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, 277c60658d1SSean Christopherson int emulation_type, void *insn, int insn_len); 278404d5d7bSWanpeng Li fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu); 2794566654bSNadav Amit 28000b27a3eSAvi Kivity extern u64 host_xcr0; 281cfc48181SSean Christopherson extern u64 supported_xcr0; 28286137773STom Lendacky extern u64 host_xss; 283408e9a31SPaolo Bonzini extern u64 supported_xss; 2844ff41732SPaolo Bonzini 285615a4ae1SSean Christopherson static inline bool kvm_mpx_supported(void) 286615a4ae1SSean Christopherson { 287615a4ae1SSean Christopherson return (supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR)) 288615a4ae1SSean Christopherson == (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR); 289615a4ae1SSean Christopherson } 290615a4ae1SSean Christopherson 2919ed96e87SMarcelo Tosatti extern unsigned int min_timer_period_us; 2929ed96e87SMarcelo Tosatti 293c4ae60e4SLiran Alon extern bool enable_vmware_backdoor; 294c4ae60e4SLiran Alon 2950c5f81daSWanpeng Li extern int pi_inject_timer; 2960c5f81daSWanpeng Li 29754e9818fSGleb Natapov extern struct static_key kvm_no_apic_vcpu; 298b51012deSPaolo Bonzini 2998d93c874SMarcelo Tosatti static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) 3008d93c874SMarcelo Tosatti { 3018d93c874SMarcelo Tosatti return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult, 3028d93c874SMarcelo Tosatti vcpu->arch.virtual_tsc_shift); 3038d93c874SMarcelo Tosatti } 3048d93c874SMarcelo Tosatti 305b51012deSPaolo Bonzini /* Same "calling convention" as do_div: 306b51012deSPaolo Bonzini * - divide (n << 32) by base 307b51012deSPaolo Bonzini * - put result in n 308b51012deSPaolo Bonzini * - return remainder 309b51012deSPaolo Bonzini */ 310b51012deSPaolo Bonzini #define do_shl32_div32(n, base) \ 311b51012deSPaolo Bonzini ({ \ 312b51012deSPaolo Bonzini u32 __quot, __rem; \ 313b51012deSPaolo Bonzini asm("divl %2" : "=a" (__quot), "=d" (__rem) \ 314b51012deSPaolo Bonzini : "rm" (base), "0" (0), "1" ((u32) n)); \ 315b51012deSPaolo Bonzini n = __quot; \ 316b51012deSPaolo Bonzini __rem; \ 317b51012deSPaolo Bonzini }) 318b51012deSPaolo Bonzini 3194d5422ceSWanpeng Li static inline bool kvm_mwait_in_guest(struct kvm *kvm) 320668fffa3SMichael S. Tsirkin { 3214d5422ceSWanpeng Li return kvm->arch.mwait_in_guest; 322668fffa3SMichael S. Tsirkin } 323668fffa3SMichael S. Tsirkin 324caa057a2SWanpeng Li static inline bool kvm_hlt_in_guest(struct kvm *kvm) 325caa057a2SWanpeng Li { 326caa057a2SWanpeng Li return kvm->arch.hlt_in_guest; 327caa057a2SWanpeng Li } 328caa057a2SWanpeng Li 329b31c114bSWanpeng Li static inline bool kvm_pause_in_guest(struct kvm *kvm) 330b31c114bSWanpeng Li { 331b31c114bSWanpeng Li return kvm->arch.pause_in_guest; 332b31c114bSWanpeng Li } 333b31c114bSWanpeng Li 334b5170063SWanpeng Li static inline bool kvm_cstate_in_guest(struct kvm *kvm) 335b5170063SWanpeng Li { 336b5170063SWanpeng Li return kvm->arch.cstate_in_guest; 337b5170063SWanpeng Li } 338b5170063SWanpeng Li 339dd60d217SAndi Kleen DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu); 340dd60d217SAndi Kleen 341dd60d217SAndi Kleen static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu) 342dd60d217SAndi Kleen { 343dd60d217SAndi Kleen __this_cpu_write(current_vcpu, vcpu); 344dd60d217SAndi Kleen } 345dd60d217SAndi Kleen 346dd60d217SAndi Kleen static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu) 347dd60d217SAndi Kleen { 348dd60d217SAndi Kleen __this_cpu_write(current_vcpu, NULL); 349dd60d217SAndi Kleen } 350dd60d217SAndi Kleen 351674ea351SPaolo Bonzini 352674ea351SPaolo Bonzini static inline bool kvm_pat_valid(u64 data) 353674ea351SPaolo Bonzini { 354674ea351SPaolo Bonzini if (data & 0xF8F8F8F8F8F8F8F8ull) 355674ea351SPaolo Bonzini return false; 356674ea351SPaolo Bonzini /* 0, 1, 4, 5, 6, 7 are valid values. */ 357674ea351SPaolo Bonzini return (data | ((data & 0x0202020202020202ull) << 1)) == data; 358674ea351SPaolo Bonzini } 359674ea351SPaolo Bonzini 3609b5e8532SSean Christopherson static inline bool kvm_dr7_valid(u64 data) 361b91991bfSKrish Sadhukhan { 362b91991bfSKrish Sadhukhan /* Bits [63:32] are reserved */ 363b91991bfSKrish Sadhukhan return !(data >> 32); 364b91991bfSKrish Sadhukhan } 365f5f6145eSKrish Sadhukhan static inline bool kvm_dr6_valid(u64 data) 366f5f6145eSKrish Sadhukhan { 367f5f6145eSKrish Sadhukhan /* Bits [63:32] are reserved */ 368f5f6145eSKrish Sadhukhan return !(data >> 32); 369f5f6145eSKrish Sadhukhan } 370b91991bfSKrish Sadhukhan 3713f1a18b9SUros Bizjak /* 3723f1a18b9SUros Bizjak * Trigger machine check on the host. We assume all the MSRs are already set up 3733f1a18b9SUros Bizjak * by the CPU and that we still run on the same CPU as the MCE occurred on. 3743f1a18b9SUros Bizjak * We pass a fake environment to the machine check handler because we want 3753f1a18b9SUros Bizjak * the guest to be always treated like user space, no matter what context 3763f1a18b9SUros Bizjak * it used internally. 3773f1a18b9SUros Bizjak */ 3783f1a18b9SUros Bizjak static inline void kvm_machine_check(void) 3793f1a18b9SUros Bizjak { 3803f1a18b9SUros Bizjak #if defined(CONFIG_X86_MCE) 3813f1a18b9SUros Bizjak struct pt_regs regs = { 3823f1a18b9SUros Bizjak .cs = 3, /* Fake ring 3 no matter what the guest ran on */ 3833f1a18b9SUros Bizjak .flags = X86_EFLAGS_IF, 3843f1a18b9SUros Bizjak }; 3853f1a18b9SUros Bizjak 3863f1a18b9SUros Bizjak do_machine_check(®s); 3873f1a18b9SUros Bizjak #endif 3883f1a18b9SUros Bizjak } 3893f1a18b9SUros Bizjak 390139a12cfSAaron Lewis void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu); 391139a12cfSAaron Lewis void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu); 392841c2be0SMaxim Levitsky int kvm_spec_ctrl_test_value(u64 value); 393ee69c92bSSean Christopherson bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); 3945a9f5443SWanpeng Li bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu); 3953f3393b3SBabu Moger int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r, 3963f3393b3SBabu Moger struct x86_exception *e); 3979715092fSBabu Moger int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva); 39851de8151SAlexander Graf bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type); 399674ea351SPaolo Bonzini 400cc4cb017SMaxim Levitsky /* 401cc4cb017SMaxim Levitsky * Internal error codes that are used to indicate that MSR emulation encountered 402cc4cb017SMaxim Levitsky * an error that should result in #GP in the guest, unless userspace 403cc4cb017SMaxim Levitsky * handles it. 404cc4cb017SMaxim Levitsky */ 405cc4cb017SMaxim Levitsky #define KVM_MSR_RET_INVALID 2 /* in-kernel MSR emulation #GP condition */ 406cc4cb017SMaxim Levitsky #define KVM_MSR_RET_FILTERED 3 /* #GP due to userspace MSR filter */ 4076abe9c13SPeter Xu 408b899c132SKrish Sadhukhan #define __cr4_reserved_bits(__cpu_has, __c) \ 409b899c132SKrish Sadhukhan ({ \ 410b899c132SKrish Sadhukhan u64 __reserved_bits = CR4_RESERVED_BITS; \ 411b899c132SKrish Sadhukhan \ 412b899c132SKrish Sadhukhan if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \ 413b899c132SKrish Sadhukhan __reserved_bits |= X86_CR4_OSXSAVE; \ 414b899c132SKrish Sadhukhan if (!__cpu_has(__c, X86_FEATURE_SMEP)) \ 415b899c132SKrish Sadhukhan __reserved_bits |= X86_CR4_SMEP; \ 416b899c132SKrish Sadhukhan if (!__cpu_has(__c, X86_FEATURE_SMAP)) \ 417b899c132SKrish Sadhukhan __reserved_bits |= X86_CR4_SMAP; \ 418b899c132SKrish Sadhukhan if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \ 419b899c132SKrish Sadhukhan __reserved_bits |= X86_CR4_FSGSBASE; \ 420b899c132SKrish Sadhukhan if (!__cpu_has(__c, X86_FEATURE_PKU)) \ 421b899c132SKrish Sadhukhan __reserved_bits |= X86_CR4_PKE; \ 422b899c132SKrish Sadhukhan if (!__cpu_has(__c, X86_FEATURE_LA57)) \ 423b899c132SKrish Sadhukhan __reserved_bits |= X86_CR4_LA57; \ 424b899c132SKrish Sadhukhan if (!__cpu_has(__c, X86_FEATURE_UMIP)) \ 425b899c132SKrish Sadhukhan __reserved_bits |= X86_CR4_UMIP; \ 42653efe527SPaolo Bonzini if (!__cpu_has(__c, X86_FEATURE_VMX)) \ 42753efe527SPaolo Bonzini __reserved_bits |= X86_CR4_VMXE; \ 428*4683d758SVitaly Kuznetsov if (!__cpu_has(__c, X86_FEATURE_PCID)) \ 429*4683d758SVitaly Kuznetsov __reserved_bits |= X86_CR4_PCIDE; \ 430b899c132SKrish Sadhukhan __reserved_bits; \ 431b899c132SKrish Sadhukhan }) 432b899c132SKrish Sadhukhan 4338f423a80STom Lendacky int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes, 4348f423a80STom Lendacky void *dst); 4358f423a80STom Lendacky int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes, 4368f423a80STom Lendacky void *dst); 4377ed9abfeSTom Lendacky int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, 4387ed9abfeSTom Lendacky unsigned int port, void *data, unsigned int count, 4397ed9abfeSTom Lendacky int in); 4408f423a80STom Lendacky 44126eef70cSAvi Kivity #endif 442