> + direct_bits = 0;
> if (kvm_arch_has_private_mem(vcpu->kvm) &&
> kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(range->gpa)))
> error_code |= PFERR_PRIVATE_ACCESS;
> + else
> + direct_bits = gfn_to_gpa(kvm_gfn_direct_bits(vcpu->kvm));
Eww. It's bad enough that TDX bleeds it's mirror needs into common MMU code,
but stuffing vendor specific GPA bits in common code goes too far. Actually,
all of this goes too far. There's zero reason any code outside of TDX needs to
*explicitly* care whether mirrors or "direct" MMUs have mandatory gfn bits.
Back to the main topic, KVM needs to have a single source of truth when it comes
to whether a fault is private and thus mirrored (or not). Common KVM needs to be
aware of aliased GFN bits, but absolute nothing outside of TDX (including common
VMX code) should be aware the mirror vs. "direct" (I hate that terminology; KVM
has far, far too much history and baggage with "direct") is tied to the existence
and polarity of aliased GFN bits.
To detect a mirror fault:
static inline bool kvm_is_mirror_fault(struct kvm *kvm, u64 error_code)
{
return kvm_has_mirrored_tdp(kvm) &&
error_code & PFERR_PRIVATE_ACCESS;
}
And for TDX, it should darn well explicitly track the shared GPA mask:
static bool tdx_is_private_gpa(struct kvm *kvm, gpa_t gpa)
{
/* For TDX the direct mask is the shared mask. */
return !(gpa & to_kvm_tdx(kvm)->shared_gpa_mask);
}
Outside of TDX, detecting mirrors, and anti-aliasing logic, the only use of
kvm_gfn_direct_bits() is to constrain TDP MMU walks to the appropriate gfn range.
And for that, we can simply use kvm_mmu_page.gfn, with a kvm_x86_ops hook to get
the TDP MMU root GFN (root allocation is a slow path, the CALL+RET is a non-issue).
Compile tested only, and obviously needs to be split into multiple patches.