xref: /openbmc/linux/arch/x86/kvm/mmu.h (revision fc78f51938e1ea866daa2045851b2e5681371668)
1edf88417SAvi Kivity #ifndef __KVM_X86_MMU_H
2edf88417SAvi Kivity #define __KVM_X86_MMU_H
3edf88417SAvi Kivity 
4edf88417SAvi Kivity #include <linux/kvm_host.h>
5*fc78f519SAvi Kivity #include "kvm_cache_regs.h"
6edf88417SAvi Kivity 
78c6d6adcSSheng Yang #define PT64_PT_BITS 9
88c6d6adcSSheng Yang #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
98c6d6adcSSheng Yang #define PT32_PT_BITS 10
108c6d6adcSSheng Yang #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
118c6d6adcSSheng Yang 
128c6d6adcSSheng Yang #define PT_WRITABLE_SHIFT 1
138c6d6adcSSheng Yang 
148c6d6adcSSheng Yang #define PT_PRESENT_MASK (1ULL << 0)
158c6d6adcSSheng Yang #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
168c6d6adcSSheng Yang #define PT_USER_MASK (1ULL << 2)
178c6d6adcSSheng Yang #define PT_PWT_MASK (1ULL << 3)
188c6d6adcSSheng Yang #define PT_PCD_MASK (1ULL << 4)
191b7fcd32SAvi Kivity #define PT_ACCESSED_SHIFT 5
201b7fcd32SAvi Kivity #define PT_ACCESSED_MASK (1ULL << PT_ACCESSED_SHIFT)
218c6d6adcSSheng Yang #define PT_DIRTY_MASK (1ULL << 6)
228c6d6adcSSheng Yang #define PT_PAGE_SIZE_MASK (1ULL << 7)
238c6d6adcSSheng Yang #define PT_PAT_MASK (1ULL << 7)
248c6d6adcSSheng Yang #define PT_GLOBAL_MASK (1ULL << 8)
258c6d6adcSSheng Yang #define PT64_NX_SHIFT 63
268c6d6adcSSheng Yang #define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
278c6d6adcSSheng Yang 
288c6d6adcSSheng Yang #define PT_PAT_SHIFT 7
298c6d6adcSSheng Yang #define PT_DIR_PAT_SHIFT 12
308c6d6adcSSheng Yang #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
318c6d6adcSSheng Yang 
328c6d6adcSSheng Yang #define PT32_DIR_PSE36_SIZE 4
338c6d6adcSSheng Yang #define PT32_DIR_PSE36_SHIFT 13
348c6d6adcSSheng Yang #define PT32_DIR_PSE36_MASK \
358c6d6adcSSheng Yang 	(((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
368c6d6adcSSheng Yang 
378c6d6adcSSheng Yang #define PT64_ROOT_LEVEL 4
388c6d6adcSSheng Yang #define PT32_ROOT_LEVEL 2
398c6d6adcSSheng Yang #define PT32E_ROOT_LEVEL 3
408c6d6adcSSheng Yang 
4194d8b056SMarcelo Tosatti int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4]);
4294d8b056SMarcelo Tosatti 
43edf88417SAvi Kivity static inline void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
44edf88417SAvi Kivity {
45edf88417SAvi Kivity 	if (unlikely(vcpu->kvm->arch.n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
46edf88417SAvi Kivity 		__kvm_mmu_free_some_pages(vcpu);
47edf88417SAvi Kivity }
48edf88417SAvi Kivity 
49edf88417SAvi Kivity static inline int kvm_mmu_reload(struct kvm_vcpu *vcpu)
50edf88417SAvi Kivity {
51edf88417SAvi Kivity 	if (likely(vcpu->arch.mmu.root_hpa != INVALID_PAGE))
52edf88417SAvi Kivity 		return 0;
53edf88417SAvi Kivity 
54edf88417SAvi Kivity 	return kvm_mmu_load(vcpu);
55edf88417SAvi Kivity }
56edf88417SAvi Kivity 
57edf88417SAvi Kivity static inline int is_long_mode(struct kvm_vcpu *vcpu)
58edf88417SAvi Kivity {
59edf88417SAvi Kivity #ifdef CONFIG_X86_64
6041d6af11SAmit Shah 	return vcpu->arch.shadow_efer & EFER_LMA;
61edf88417SAvi Kivity #else
62edf88417SAvi Kivity 	return 0;
63edf88417SAvi Kivity #endif
64edf88417SAvi Kivity }
65edf88417SAvi Kivity 
66edf88417SAvi Kivity static inline int is_pae(struct kvm_vcpu *vcpu)
67edf88417SAvi Kivity {
68*fc78f519SAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
69edf88417SAvi Kivity }
70edf88417SAvi Kivity 
71edf88417SAvi Kivity static inline int is_pse(struct kvm_vcpu *vcpu)
72edf88417SAvi Kivity {
73*fc78f519SAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
74edf88417SAvi Kivity }
75edf88417SAvi Kivity 
76edf88417SAvi Kivity static inline int is_paging(struct kvm_vcpu *vcpu)
77edf88417SAvi Kivity {
78edf88417SAvi Kivity 	return vcpu->arch.cr0 & X86_CR0_PG;
79edf88417SAvi Kivity }
80edf88417SAvi Kivity 
8143a3795aSAvi Kivity static inline int is_present_gpte(unsigned long pte)
8220c466b5SDong, Eddie {
8320c466b5SDong, Eddie 	return pte & PT_PRESENT_MASK;
8420c466b5SDong, Eddie }
8520c466b5SDong, Eddie 
86edf88417SAvi Kivity #endif
87