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>
This commit is contained in:
parent
b213812d3f
commit
7040e54fdd
tools/testing/selftests/kvm
@ -1098,10 +1098,10 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
|
||||
uint64_t __xen_hypercall(uint64_t nr, uint64_t a0, void *a1);
|
||||
void xen_hypercall(uint64_t nr, uint64_t a0, void *a1);
|
||||
|
||||
void __vm_xsave_require_permission(int bit, const char *name);
|
||||
void __vm_xsave_require_permission(uint64_t xfeature, const char *name);
|
||||
|
||||
#define vm_xsave_require_permission(perm) \
|
||||
__vm_xsave_require_permission(perm, #perm)
|
||||
#define vm_xsave_require_permission(xfeature) \
|
||||
__vm_xsave_require_permission(xfeature, #xfeature)
|
||||
|
||||
enum pg_level {
|
||||
PG_LEVEL_NONE,
|
||||
|
@ -697,7 +697,7 @@ uint64_t kvm_get_feature_msr(uint64_t msr_index)
|
||||
return buffer.entry.data;
|
||||
}
|
||||
|
||||
void __vm_xsave_require_permission(int bit, const char *name)
|
||||
void __vm_xsave_require_permission(uint64_t xfeature, const char *name)
|
||||
{
|
||||
int kvm_fd;
|
||||
u64 bitmask;
|
||||
@ -705,12 +705,15 @@ void __vm_xsave_require_permission(int bit, const char *name)
|
||||
struct kvm_device_attr attr = {
|
||||
.group = 0,
|
||||
.attr = KVM_X86_XCOMP_GUEST_SUPP,
|
||||
.addr = (unsigned long) &bitmask
|
||||
.addr = (unsigned long) &bitmask,
|
||||
};
|
||||
|
||||
TEST_ASSERT(!kvm_supported_cpuid,
|
||||
"kvm_get_supported_cpuid() cannot be used before ARCH_REQ_XCOMP_GUEST_PERM");
|
||||
|
||||
TEST_ASSERT(is_power_of_2(xfeature),
|
||||
"Dynamic XFeatures must be enabled one at a time");
|
||||
|
||||
kvm_fd = open_kvm_dev_path_or_exit();
|
||||
rc = __kvm_ioctl(kvm_fd, KVM_GET_DEVICE_ATTR, &attr);
|
||||
close(kvm_fd);
|
||||
@ -720,16 +723,16 @@ void __vm_xsave_require_permission(int bit, const char *name)
|
||||
|
||||
TEST_ASSERT(rc == 0, "KVM_GET_DEVICE_ATTR(0, KVM_X86_XCOMP_GUEST_SUPP) error: %ld", rc);
|
||||
|
||||
__TEST_REQUIRE(bitmask & (1ULL << bit),
|
||||
__TEST_REQUIRE(bitmask & xfeature,
|
||||
"Required XSAVE feature '%s' not supported", name);
|
||||
|
||||
TEST_REQUIRE(!syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_GUEST_PERM, bit));
|
||||
TEST_REQUIRE(!syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_GUEST_PERM, ilog2(xfeature)));
|
||||
|
||||
rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_GUEST_PERM, &bitmask);
|
||||
TEST_ASSERT(rc == 0, "prctl(ARCH_GET_XCOMP_GUEST_PERM) error: %ld", rc);
|
||||
TEST_ASSERT(bitmask & (1ULL << bit),
|
||||
"prctl(ARCH_REQ_XCOMP_GUEST_PERM) failure bitmask=0x%lx",
|
||||
bitmask);
|
||||
TEST_ASSERT(bitmask & xfeature,
|
||||
"'%s' (0x%lx) not permitted after prctl(ARCH_REQ_XCOMP_GUEST_PERM) perrmited=0x%lx",
|
||||
name, xfeature, bitmask);
|
||||
}
|
||||
|
||||
void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid)
|
||||
|
@ -233,7 +233,7 @@ int main(int argc, char *argv[])
|
||||
* Note, all off-by-default features must be enabled before anything
|
||||
* caches KVM_GET_SUPPORTED_CPUID, e.g. before using kvm_cpu_has().
|
||||
*/
|
||||
vm_xsave_require_permission(XSTATE_XTILE_DATA_BIT);
|
||||
vm_xsave_require_permission(XFEATURE_MASK_XTILEDATA);
|
||||
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XFD));
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVE));
|
||||
|
Loading…
x
Reference in New Issue
Block a user