Skip to content

Commit

Permalink
selftest: KVM: Add open sev dev helper
Browse files Browse the repository at this point in the history
Refactors out open path support from open_kvm_dev_path_or_exit() and
adds new helper for SEV device path.

Signed-off-by: Peter Gonda <[email protected]>
Suggested-by: Sean Christopherson <[email protected]>
Cc: Marc Orr <[email protected]>
Cc: Sean Christopherson <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Brijesh Singh <[email protected]>
Cc: Tom Lendacky <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: [email protected]
Cc: [email protected]
Message-Id: <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
pgonda authored and bonzini committed Nov 11, 2021
1 parent 0b020f5 commit 7a6ab3c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions tools/testing/selftests/kvm/include/kvm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct vm_guest_mode_params {
};
extern const struct vm_guest_mode_params vm_guest_mode_params[];

int open_path_or_exit(const char *path, int flags);
int open_kvm_dev_path_or_exit(void);
int kvm_check_cap(long cap);
int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap);
Expand Down
2 changes: 2 additions & 0 deletions tools/testing/selftests/kvm/include/x86_64/svm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ static inline bool cpu_has_svm(void)
return ecx & CPUID_SVM;
}

int open_sev_dev_path_or_exit(void);

#endif /* SELFTEST_KVM_SVM_UTILS_H */
24 changes: 14 additions & 10 deletions tools/testing/selftests/kvm/lib/kvm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ static void *align(void *x, size_t size)
return (void *) (((size_t) x + mask) & ~mask);
}

int open_path_or_exit(const char *path, int flags)
{
int fd;

fd = open(path, flags);
if (fd < 0) {
print_skip("%s not available (errno: %d)", path, errno);
exit(KSFT_SKIP);
}

return fd;
}

/*
* Open KVM_DEV_PATH if available, otherwise exit the entire program.
*
Expand All @@ -42,16 +55,7 @@ static void *align(void *x, size_t size)
*/
static int _open_kvm_dev_path_or_exit(int flags)
{
int fd;

fd = open(KVM_DEV_PATH, flags);
if (fd < 0) {
print_skip("%s not available, is KVM loaded? (errno: %d)",
KVM_DEV_PATH, errno);
exit(KSFT_SKIP);
}

return fd;
return open_path_or_exit(KVM_DEV_PATH, flags);
}

int open_kvm_dev_path_or_exit(void)
Expand Down
13 changes: 13 additions & 0 deletions tools/testing/selftests/kvm/lib/x86_64/svm.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "processor.h"
#include "svm_util.h"

#define SEV_DEV_PATH "/dev/sev"

struct gpr64_regs guest_regs;
u64 rflags;

Expand Down Expand Up @@ -172,3 +174,14 @@ void nested_svm_check_supported(void)
exit(KSFT_SKIP);
}
}

/*
* Open SEV_DEV_PATH if available, otherwise exit the entire program.
*
* Return:
* The opened file descriptor of /dev/sev.
*/
int open_sev_dev_path_or_exit(void)
{
return open_path_or_exit(SEV_DEV_PATH, 0);
}

0 comments on commit 7a6ab3c

Please sign in to comment.