Re: [PATCH] KVM: x86/mmu: Refresh CR0.WP prior to checking for emulated permission faults

Sean Christopherson
2023-04-05T14:36:56Z

On Wed, Apr 05, 2023, Mathias Krause wrote:
> On 05.04.23 02:26, Sean Christopherson wrote:
> > +void __kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,
> > + struct kvm_mmu *mmu)
> > +{
> > + const bool cr0_wp = kvm_is_cr0_bit_set(vcpu, X86_CR0_WP);
> > +
> > + BUILD_BUG_ON((KVM_MMU_CR0_ROLE_BITS & KVM_POSSIBLE_CR0_GUEST_BITS) != X86_CR0_WP);
>
> > + BUILD_BUG_ON((KVM_MMU_CR4_ROLE_BITS & KVM_POSSIBLE_CR4_GUEST_BITS));
>
> Just curious, this should assert that we don't run into similar issues
> if we make more bits of CR4 guest owned?

Yes? I'm not sure what you're asking. BUILD_BUG_ON() is a just more flexible
version of stiatic_assert(); it only requires that the inputs be compile-time
constants, not purely "static".

he above throws an error at compile-time if there is new overlap between the
CR{0,4} MMU role bits and the possible guest-owned bits. E.g. adding SMEP to the
possible guest-owned CR4 bits yields:

arch/x86/kvm/mmu/mmu.c: In function ‘__kvm_mmu_refresh_passthrough_bits’:
include/linux/compiler_types.h:397:45: error: call to ‘__compiletime_assert_1564’
declared with attribute error: BUILD_BUG_ON failed: (KVM_MMU_CR4_ROLE_BITS & KVM_POSSIBLE_CR4_GUEST_BITS)
397 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^
include/linux/compiler_types.h:378:25: note: in definition of macro ‘__compiletime_assert’
378 | prefix ## suffix(); \
| ^~~~~~
include/linux/compiler_types.h:397:9: note: in expansion of macro ‘_compiletime_assert’
397 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:39:37: note: in expansion of macro ‘compiletime_assert’
39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
| ^~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:50:9: note: in expansion of macro ‘BUILD_BUG_ON_MSG’
50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
| ^~~~~~~~~~~~~~~~
arch/x86/kvm/mmu/mmu.c:5191:9: note: in expansion of macro ‘BUILD_BUG_ON’
5191 | BUILD_BUG_ON((KVM_MMU_CR4_ROLE_BITS & KVM_POSSIBLE_CR4_GUEST_BITS));
| ^~~~~~~~~~~~
scripts/Makefile.build:252: arch/x86/kvm/mmu/mmu.o] Error 1