Skip to content

Commit 7a6ab3c

Browse files
pgondabonzini
authored andcommitted
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: 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]>
1 parent 0b020f5 commit 7a6ab3c

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

tools/testing/selftests/kvm/include/kvm_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ struct vm_guest_mode_params {
8282
};
8383
extern const struct vm_guest_mode_params vm_guest_mode_params[];
8484

85+
int open_path_or_exit(const char *path, int flags);
8586
int open_kvm_dev_path_or_exit(void);
8687
int kvm_check_cap(long cap);
8788
int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ static inline bool cpu_has_svm(void)
4646
return ecx & CPUID_SVM;
4747
}
4848

49+
int open_sev_dev_path_or_exit(void);
50+
4951
#endif /* SELFTEST_KVM_SVM_UTILS_H */

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ static void *align(void *x, size_t size)
3131
return (void *) (((size_t) x + mask) & ~mask);
3232
}
3333

34+
int open_path_or_exit(const char *path, int flags)
35+
{
36+
int fd;
37+
38+
fd = open(path, flags);
39+
if (fd < 0) {
40+
print_skip("%s not available (errno: %d)", path, errno);
41+
exit(KSFT_SKIP);
42+
}
43+
44+
return fd;
45+
}
46+
3447
/*
3548
* Open KVM_DEV_PATH if available, otherwise exit the entire program.
3649
*
@@ -42,16 +55,7 @@ static void *align(void *x, size_t size)
4255
*/
4356
static int _open_kvm_dev_path_or_exit(int flags)
4457
{
45-
int fd;
46-
47-
fd = open(KVM_DEV_PATH, flags);
48-
if (fd < 0) {
49-
print_skip("%s not available, is KVM loaded? (errno: %d)",
50-
KVM_DEV_PATH, errno);
51-
exit(KSFT_SKIP);
52-
}
53-
54-
return fd;
58+
return open_path_or_exit(KVM_DEV_PATH, flags);
5559
}
5660

5761
int open_kvm_dev_path_or_exit(void)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "processor.h"
1414
#include "svm_util.h"
1515

16+
#define SEV_DEV_PATH "/dev/sev"
17+
1618
struct gpr64_regs guest_regs;
1719
u64 rflags;
1820

@@ -172,3 +174,14 @@ void nested_svm_check_supported(void)
172174
exit(KSFT_SKIP);
173175
}
174176
}
177+
178+
/*
179+
* Open SEV_DEV_PATH if available, otherwise exit the entire program.
180+
*
181+
* Return:
182+
* The opened file descriptor of /dev/sev.
183+
*/
184+
int open_sev_dev_path_or_exit(void)
185+
{
186+
return open_path_or_exit(SEV_DEV_PATH, 0);
187+
}

0 commit comments

Comments
 (0)