Commit 7a6ab3cf authored by Peter Gonda's avatar Peter Gonda Committed by Paolo Bonzini

selftest: KVM: Add open sev dev helper

Refactors out open path support from open_kvm_dev_path_or_exit() and
adds new helper for SEV device path.
Signed-off-by: default avatarPeter Gonda <pgonda@google.com>
Suggested-by: default avatarSean Christopherson <seanjc@google.com>
Cc: Marc Orr <marcorr@google.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Message-Id: <20211021174303.385706-5-pgonda@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 0b020f5a
...@@ -82,6 +82,7 @@ struct vm_guest_mode_params { ...@@ -82,6 +82,7 @@ struct vm_guest_mode_params {
}; };
extern const struct vm_guest_mode_params 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 open_kvm_dev_path_or_exit(void);
int kvm_check_cap(long cap); int kvm_check_cap(long cap);
int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap); int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap);
......
...@@ -46,4 +46,6 @@ static inline bool cpu_has_svm(void) ...@@ -46,4 +46,6 @@ static inline bool cpu_has_svm(void)
return ecx & CPUID_SVM; return ecx & CPUID_SVM;
} }
int open_sev_dev_path_or_exit(void);
#endif /* SELFTEST_KVM_SVM_UTILS_H */ #endif /* SELFTEST_KVM_SVM_UTILS_H */
...@@ -31,6 +31,19 @@ static void *align(void *x, size_t size) ...@@ -31,6 +31,19 @@ static void *align(void *x, size_t size)
return (void *) (((size_t) x + mask) & ~mask); 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. * Open KVM_DEV_PATH if available, otherwise exit the entire program.
* *
...@@ -42,16 +55,7 @@ static void *align(void *x, size_t size) ...@@ -42,16 +55,7 @@ static void *align(void *x, size_t size)
*/ */
static int _open_kvm_dev_path_or_exit(int flags) static int _open_kvm_dev_path_or_exit(int flags)
{ {
int fd; return open_path_or_exit(KVM_DEV_PATH, flags);
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;
} }
int open_kvm_dev_path_or_exit(void) int open_kvm_dev_path_or_exit(void)
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#include "processor.h" #include "processor.h"
#include "svm_util.h" #include "svm_util.h"
#define SEV_DEV_PATH "/dev/sev"
struct gpr64_regs guest_regs; struct gpr64_regs guest_regs;
u64 rflags; u64 rflags;
...@@ -172,3 +174,14 @@ void nested_svm_check_supported(void) ...@@ -172,3 +174,14 @@ void nested_svm_check_supported(void)
exit(KSFT_SKIP); 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);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment