Skip to content

Commit 7040e54

Browse files
committed
KVM: selftests: Rework dynamic XFeature helper to take mask, not bit
Take the XFeature mask in __vm_xsave_require_permission() instead of the bit so that there's no need to define macros for both the bit and the mask. Asserting that only a single bit is set and retrieving said bit is easy enough via log2 helpers. Opportunistically clean up the error message for the ARCH_REQ_XCOMP_GUEST_PERM sanity check. Reviewed-by: Aaron Lewis <aaronlewis@google.com> Tested-by: Aaron Lewis <aaronlewis@google.com> Link: https://lore.kernel.org/r/20230405004520.421768-5-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent b213812 commit 7040e54

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,10 +1098,10 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
10981098
uint64_t __xen_hypercall(uint64_t nr, uint64_t a0, void *a1);
10991099
void xen_hypercall(uint64_t nr, uint64_t a0, void *a1);
11001100

1101-
void __vm_xsave_require_permission(int bit, const char *name);
1101+
void __vm_xsave_require_permission(uint64_t xfeature, const char *name);
11021102

1103-
#define vm_xsave_require_permission(perm) \
1104-
__vm_xsave_require_permission(perm, #perm)
1103+
#define vm_xsave_require_permission(xfeature) \
1104+
__vm_xsave_require_permission(xfeature, #xfeature)
11051105

11061106
enum pg_level {
11071107
PG_LEVEL_NONE,

tools/testing/selftests/kvm/lib/x86_64/processor.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -697,20 +697,23 @@ uint64_t kvm_get_feature_msr(uint64_t msr_index)
697697
return buffer.entry.data;
698698
}
699699

700-
void __vm_xsave_require_permission(int bit, const char *name)
700+
void __vm_xsave_require_permission(uint64_t xfeature, const char *name)
701701
{
702702
int kvm_fd;
703703
u64 bitmask;
704704
long rc;
705705
struct kvm_device_attr attr = {
706706
.group = 0,
707707
.attr = KVM_X86_XCOMP_GUEST_SUPP,
708-
.addr = (unsigned long) &bitmask
708+
.addr = (unsigned long) &bitmask,
709709
};
710710

711711
TEST_ASSERT(!kvm_supported_cpuid,
712712
"kvm_get_supported_cpuid() cannot be used before ARCH_REQ_XCOMP_GUEST_PERM");
713713

714+
TEST_ASSERT(is_power_of_2(xfeature),
715+
"Dynamic XFeatures must be enabled one at a time");
716+
714717
kvm_fd = open_kvm_dev_path_or_exit();
715718
rc = __kvm_ioctl(kvm_fd, KVM_GET_DEVICE_ATTR, &attr);
716719
close(kvm_fd);
@@ -720,16 +723,16 @@ void __vm_xsave_require_permission(int bit, const char *name)
720723

721724
TEST_ASSERT(rc == 0, "KVM_GET_DEVICE_ATTR(0, KVM_X86_XCOMP_GUEST_SUPP) error: %ld", rc);
722725

723-
__TEST_REQUIRE(bitmask & (1ULL << bit),
726+
__TEST_REQUIRE(bitmask & xfeature,
724727
"Required XSAVE feature '%s' not supported", name);
725728

726-
TEST_REQUIRE(!syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_GUEST_PERM, bit));
729+
TEST_REQUIRE(!syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_GUEST_PERM, ilog2(xfeature)));
727730

728731
rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_GUEST_PERM, &bitmask);
729732
TEST_ASSERT(rc == 0, "prctl(ARCH_GET_XCOMP_GUEST_PERM) error: %ld", rc);
730-
TEST_ASSERT(bitmask & (1ULL << bit),
731-
"prctl(ARCH_REQ_XCOMP_GUEST_PERM) failure bitmask=0x%lx",
732-
bitmask);
733+
TEST_ASSERT(bitmask & xfeature,
734+
"'%s' (0x%lx) not permitted after prctl(ARCH_REQ_XCOMP_GUEST_PERM) perrmited=0x%lx",
735+
name, xfeature, bitmask);
733736
}
734737

735738
void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid)

tools/testing/selftests/kvm/x86_64/amx_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ int main(int argc, char *argv[])
233233
* Note, all off-by-default features must be enabled before anything
234234
* caches KVM_GET_SUPPORTED_CPUID, e.g. before using kvm_cpu_has().
235235
*/
236-
vm_xsave_require_permission(XSTATE_XTILE_DATA_BIT);
236+
vm_xsave_require_permission(XFEATURE_MASK_XTILEDATA);
237237

238238
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XFD));
239239
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVE));

0 commit comments

Comments
 (0)