linux/tools/testing/selftests/kvm/include/kvm_syscalls.h
Paolo Bonzini b02a4f8c42 KVM selftests changes for 7.2
- Randomize the dirty log test's delay when reaping the bitmap on the first
    pass, as always waiting only 1ms hid a KVM RISC-V bug as the test reaped the
    bitmap before KVM could build up enough state to hit the bug.
 
  - A pile of one-off fixes and cleanups.
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEKTobbabEP7vbhhN9OlYIJqCjN/0FAmorQNUACgkQOlYIJqCj
 N/3E2RAAqSMg2+iYYbfkjFw5kFafSDb7IWFxK4SWNWeh5C8jkfCrrviWTMOewTR8
 C7YtKCMGK8iCZQfmB3jhenTEoQJEcICJn4JOUNK2RkNcC/BuNlKlM/C8dX0x29xk
 m1XWZdLgwjmZr7LydzVGMgdhJdcdxK/WV/71vvFYze4Jxim4lnnTM3VoMbjj8FuS
 6FdMvKbclD6Mbfx1/wvYNndl6J9Y0fhKZbsj6tpAxDmXH/Pw9zx8b49znioV4HsD
 k3GE76w4Xe/cIgHwbXWTUmpf1s2Ou8ZO8ju+02u3gUz6UpIj2gbcV0mhu327EhyT
 IPlepblusG6hzJAXwfmb6D8u/aXg2VfZdSsiLNNhgisNtLnakFjxdKg0ViSnKS+3
 UZ49TWmPwyZ92JEC7paluB1PKv7n+GiJBfyLAU9lV7x4rXHn+nseW/ZPJ2Rvdq5n
 HsLlG9smz5Q7ea8AI8yHaGYQpTbw48t52hnNqLdt0mU5Tj027nNNWVP72s+Jhc/j
 N+1juth6qszxh7gLMD00AgxRtTKRpGMMV9zbieuQTg+mhmUrk5lJsCuk+su9TCzT
 /UjGrmRmz2TKtA9MQ0xckA3ysR519WYdBn9EHhiQLLfLd5V01LTo0FERMovjfjyH
 JUYKcyFhiZqbVqCNsloIGTv3/N3jOwxKzMhuFrmuGT9+NCyC5Fk=
 =nMTy
 -----END PGP SIGNATURE-----

Merge tag 'kvm-x86-selftests-7.2' of https://github.com/kvm-x86/linux into HEAD

KVM selftests changes for 7.2

 - Randomize the dirty log test's delay when reaping the bitmap on the first
   pass, as always waiting only 1ms hid a KVM RISC-V bug as the test reaped the
   bitmap before KVM could build up enough state to hit the bug.

 - A pile of one-off fixes and cleanups.
2026-06-12 10:12:22 +02:00

99 lines
3.0 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef SELFTEST_KVM_SYSCALLS_H
#define SELFTEST_KVM_SYSCALLS_H
/*
* Include both the kernel and libc versions of mman.h. The kernel provides
* the most up-to-date flags and definitions, while libc provides the syscall
* wrappers tests expect.
*/
#include <linux/mman.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <test_util.h>
#define MAP_ARGS0(m,...)
#define MAP_ARGS1(m,t,a,...) m(t,a)
#define MAP_ARGS2(m,t,a,...) m(t,a), MAP_ARGS1(m,__VA_ARGS__)
#define MAP_ARGS3(m,t,a,...) m(t,a), MAP_ARGS2(m,__VA_ARGS__)
#define MAP_ARGS4(m,t,a,...) m(t,a), MAP_ARGS3(m,__VA_ARGS__)
#define MAP_ARGS5(m,t,a,...) m(t,a), MAP_ARGS4(m,__VA_ARGS__)
#define MAP_ARGS6(m,t,a,...) m(t,a), MAP_ARGS5(m,__VA_ARGS__)
#define MAP_ARGS(n,...) MAP_ARGS##n(__VA_ARGS__)
#define __DECLARE_ARGS(t, a) t a
#define __UNPACK_ARGS(t, a) a
#define DECLARE_ARGS(nr_args, args...) MAP_ARGS(nr_args, __DECLARE_ARGS, args)
#define UNPACK_ARGS(nr_args, args...) MAP_ARGS(nr_args, __UNPACK_ARGS, args)
#define __KVM_SYSCALL_ERROR(_name, _ret) \
"%s failed, rc: %i errno: %i (%s)", (_name), (_ret), errno, strerror(errno)
/* Define a kvm_<syscall>() API to assert success. */
#define __KVM_SYSCALL_DEFINE(name, nr_args, args...) \
static inline void kvm_##name(DECLARE_ARGS(nr_args, args)) \
{ \
int r; \
\
r = name(UNPACK_ARGS(nr_args, args)); \
TEST_ASSERT(!r, __KVM_SYSCALL_ERROR(#name, r)); \
}
/*
* Macro to define syscall APIs, either because KVM selftests doesn't link to
* the standard library, e.g. libnuma, or because there is no library that yet
* provides the syscall. These
*/
#define KVM_SYSCALL_DEFINE(name, nr_args, args...) \
static inline long name(DECLARE_ARGS(nr_args, args)) \
{ \
return syscall(__NR_##name, UNPACK_ARGS(nr_args, args)); \
} \
__KVM_SYSCALL_DEFINE(name, nr_args, args)
/*
* Special case mmap(), as KVM selftest rarely/never specific an address,
* rarely specify an offset, and because the unique return code requires
* special handling anyways.
*/
static inline void *__kvm_mmap(size_t size, int prot, int flags, int fd,
off_t offset)
{
void *mem;
mem = mmap(NULL, size, prot, flags, fd, offset);
TEST_ASSERT(mem != MAP_FAILED, __KVM_SYSCALL_ERROR("mmap()",
(int)(unsigned long)MAP_FAILED));
return mem;
}
static inline void *kvm_mmap(size_t size, int prot, int flags, int fd)
{
return __kvm_mmap(size, prot, flags, fd, 0);
}
static inline int kvm_dup(int fd)
{
int new_fd = dup(fd);
TEST_ASSERT(new_fd >= 0, __KVM_SYSCALL_ERROR("dup()", new_fd));
return new_fd;
}
__KVM_SYSCALL_DEFINE(munmap, 2, void *, mem, size_t, size);
__KVM_SYSCALL_DEFINE(close, 1, int, fd);
__KVM_SYSCALL_DEFINE(fallocate, 4, int, fd, int, mode, loff_t, offset, loff_t, len);
__KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length);
__KVM_SYSCALL_DEFINE(madvise, 3, void *, addr, size_t, length, int, advice);
#define kvm_free_fd(fd) \
do { \
kvm_close(fd); \
(fd) = -1; \
} while (0)
#endif /* SELFTEST_KVM_SYSCALLS_H */