Skip to content

Commit 63b6206

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: Remove separate "bit" defines for page fault error code masks
Open code the bit number directly in the PFERR_* masks and drop the intermediate PFERR_*_BIT defines, as having to bounce through two macros just to see which flag corresponds to which bit is quite annoying, as is having to define two macros just to add recognition of a new flag. Use ternary operator to derive the bit in permission_fault(), the one function that actually needs the bit number as part of clever shifting to avoid conditional branches. Generally the compiler is able to turn it into a conditional move, and if not it's not really a big deal. No functional change intended. Signed-off-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Message-ID: <20240228024147.41573-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent d0bf8e6 commit 63b6206

2 files changed

Lines changed: 12 additions & 25 deletions

File tree

arch/x86/include/asm/kvm_host.h

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -254,28 +254,16 @@ enum x86_intercept_stage;
254254
KVM_GUESTDBG_INJECT_DB | \
255255
KVM_GUESTDBG_BLOCKIRQ)
256256

257-
258-
#define PFERR_PRESENT_BIT 0
259-
#define PFERR_WRITE_BIT 1
260-
#define PFERR_USER_BIT 2
261-
#define PFERR_RSVD_BIT 3
262-
#define PFERR_FETCH_BIT 4
263-
#define PFERR_PK_BIT 5
264-
#define PFERR_SGX_BIT 15
265-
#define PFERR_GUEST_FINAL_BIT 32
266-
#define PFERR_GUEST_PAGE_BIT 33
267-
#define PFERR_IMPLICIT_ACCESS_BIT 48
268-
269-
#define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT)
270-
#define PFERR_WRITE_MASK BIT(PFERR_WRITE_BIT)
271-
#define PFERR_USER_MASK BIT(PFERR_USER_BIT)
272-
#define PFERR_RSVD_MASK BIT(PFERR_RSVD_BIT)
273-
#define PFERR_FETCH_MASK BIT(PFERR_FETCH_BIT)
274-
#define PFERR_PK_MASK BIT(PFERR_PK_BIT)
275-
#define PFERR_SGX_MASK BIT(PFERR_SGX_BIT)
276-
#define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT)
277-
#define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT)
278-
#define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT)
257+
#define PFERR_PRESENT_MASK BIT(0)
258+
#define PFERR_WRITE_MASK BIT(1)
259+
#define PFERR_USER_MASK BIT(2)
260+
#define PFERR_RSVD_MASK BIT(3)
261+
#define PFERR_FETCH_MASK BIT(4)
262+
#define PFERR_PK_MASK BIT(5)
263+
#define PFERR_SGX_MASK BIT(15)
264+
#define PFERR_GUEST_FINAL_MASK BIT_ULL(32)
265+
#define PFERR_GUEST_PAGE_MASK BIT_ULL(33)
266+
#define PFERR_IMPLICIT_ACCESS BIT_ULL(48)
279267

280268
#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \
281269
PFERR_WRITE_MASK | \

arch/x86/kvm/mmu.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
213213
*/
214214
u64 implicit_access = access & PFERR_IMPLICIT_ACCESS;
215215
bool not_smap = ((rflags & X86_EFLAGS_AC) | implicit_access) == X86_EFLAGS_AC;
216-
int index = (pfec + (not_smap << PFERR_RSVD_BIT)) >> 1;
216+
int index = (pfec | (not_smap ? PFERR_RSVD_MASK : 0)) >> 1;
217217
u32 errcode = PFERR_PRESENT_MASK;
218218
bool fault;
219219

@@ -234,8 +234,7 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
234234
pkru_bits = (vcpu->arch.pkru >> (pte_pkey * 2)) & 3;
235235

236236
/* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */
237-
offset = (pfec & ~1) +
238-
((pte_access & PT_USER_MASK) << (PFERR_RSVD_BIT - PT_USER_SHIFT));
237+
offset = (pfec & ~1) | ((pte_access & PT_USER_MASK) ? PFERR_RSVD_MASK : 0);
239238

240239
pkru_bits &= mmu->pkru_mask >> offset;
241240
errcode |= -pkru_bits & PFERR_PK_MASK;

0 commit comments

Comments
 (0)