projects/linux·blob master · refs

153 lines · 4.7 KiB

1/* SPDX-License-Identifier: GPL-2.0-only */
2#ifndef SELFTEST_KVM_SYSCALLS_H
3#define SELFTEST_KVM_SYSCALLS_H
4
5/*
6 * Include both the kernel and libc versions of mman.h. The kernel provides
7 * the most up-to-date flags and definitions, while libc provides the syscall
8 * wrappers tests expect.
9 */
10#include <linux/mman.h>
11
12#include <sys/mman.h>
13#include <sys/syscall.h>
14
15#include <pthread.h>
16#include <sched.h>
17#include <test_util.h>
18
19#define MAP_ARGS0(m,...)
20#define MAP_ARGS1(m,t,a,...) m(t,a)
21#define MAP_ARGS2(m,t,a,...) m(t,a), MAP_ARGS1(m,__VA_ARGS__)
22#define MAP_ARGS3(m,t,a,...) m(t,a), MAP_ARGS2(m,__VA_ARGS__)
23#define MAP_ARGS4(m,t,a,...) m(t,a), MAP_ARGS3(m,__VA_ARGS__)
24#define MAP_ARGS5(m,t,a,...) m(t,a), MAP_ARGS4(m,__VA_ARGS__)
25#define MAP_ARGS6(m,t,a,...) m(t,a), MAP_ARGS5(m,__VA_ARGS__)
26#define MAP_ARGS(n,...) MAP_ARGS##n(__VA_ARGS__)
27
28#define __DECLARE_ARGS(t, a) t a
29#define __UNPACK_ARGS(t, a) a
30
31#define DECLARE_ARGS(nr_args, args...) MAP_ARGS(nr_args, __DECLARE_ARGS, args)
32#define UNPACK_ARGS(nr_args, args...) MAP_ARGS(nr_args, __UNPACK_ARGS, args)
33
34#define __KVM_SYSCALL_ERROR(_name, _ret) \
35 "%s failed, rc: %i errno: %i (%s)", (_name), (_ret), errno, strerror(errno)
36
37/* Define a kvm_<syscall>() API to assert success. */
38#define __KVM_SYSCALL_DEFINE(name, nr_args, args...) \
39static inline void kvm_##name(DECLARE_ARGS(nr_args, args)) \
40{ \
41 int r; \
42 \
43 r = name(UNPACK_ARGS(nr_args, args)); \
44 TEST_ASSERT(!r, __KVM_SYSCALL_ERROR(#name, r)); \
45}
46
47/*
48 * Macro to define syscall APIs, either because KVM selftests doesn't link to
49 * the standard library, e.g. libnuma, or because there is no library that yet
50 * provides the syscall. These
51 */
52#define KVM_SYSCALL_DEFINE(name, nr_args, args...) \
53static inline long name(DECLARE_ARGS(nr_args, args)) \
54{ \
55 return syscall(__NR_##name, UNPACK_ARGS(nr_args, args)); \
56} \
57__KVM_SYSCALL_DEFINE(name, nr_args, args)
58
59/*
60 * Special case mmap(), as KVM selftest rarely/never specific an address,
61 * rarely specify an offset, and because the unique return code requires
62 * special handling anyways.
63 */
64static inline void *__kvm_mmap(size_t size, int prot, int flags, int fd,
65 off_t offset)
66{
67 void *mem;
68
69 mem = mmap(NULL, size, prot, flags, fd, offset);
70 TEST_ASSERT(mem != MAP_FAILED, __KVM_SYSCALL_ERROR("mmap()",
71 (int)(unsigned long)MAP_FAILED));
72 return mem;
73}
74
75static inline void *kvm_mmap(size_t size, int prot, int flags, int fd)
76{
77 return __kvm_mmap(size, prot, flags, fd, 0);
78}
79
80static inline int kvm_dup(int fd)
81{
82 int new_fd = dup(fd);
83
84 TEST_ASSERT(new_fd >= 0, __KVM_SYSCALL_ERROR("dup()", new_fd));
85 return new_fd;
86}
87
88static inline pid_t kvm_gettid(void)
89{
90 return syscall(__NR_gettid);
91}
92
93__KVM_SYSCALL_DEFINE(munmap, 2, void *, mem, size_t, size);
94__KVM_SYSCALL_DEFINE(close, 1, int, fd);
95__KVM_SYSCALL_DEFINE(fallocate, 4, int, fd, int, mode, loff_t, offset, loff_t, len);
96__KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length);
97__KVM_SYSCALL_DEFINE(madvise, 3, void *, addr, size_t, length, int, advice);
98__KVM_SYSCALL_DEFINE(sched_getaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_set_t *, mask);
99__KVM_SYSCALL_DEFINE(sched_setaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_set_t *, mask);
100
101__KVM_SYSCALL_DEFINE(pthread_getaffinity_np, 3, pthread_t, thread,
102 size_t, cpusetsize, cpu_set_t *, cpuset);
103__KVM_SYSCALL_DEFINE(pthread_setaffinity_np, 3, pthread_t, thread,
104 size_t, cpusetsize, const cpu_set_t *, cpuset);
105
106static inline void kvm_pthread_getaffinity(pthread_t thread, cpu_set_t *cpuset)
107{
108 kvm_pthread_getaffinity_np(thread, sizeof(cpu_set_t), cpuset);
109}
110
111static inline void kvm_pthread_setaffinity(pthread_t thread,
112 const cpu_set_t *cpuset)
113{
114 kvm_pthread_setaffinity_np(thread, sizeof(cpu_set_t), cpuset);
115}
116
117typedef void *(*pthread_fn_t)(void *);
118__KVM_SYSCALL_DEFINE(pthread_create, 4, pthread_t *, thread,
119 const pthread_attr_t *, attr, pthread_fn_t, fn, void *, arg);
120__KVM_SYSCALL_DEFINE(pthread_join, 2, pthread_t, thread, void **, thread_return);
121__KVM_SYSCALL_DEFINE(pthread_cancel, 1, pthread_t, thread);
122
123static inline void __kvm_pthread_cancel_join(pthread_t thread, void **r)
124{
125 kvm_pthread_cancel(thread);
126 kvm_pthread_join(thread, r);
127}
128
129static inline void kvm_pthread_cancel_join(pthread_t thread)
130{
131 __kvm_pthread_cancel_join(thread, NULL);
132}
133
134/*
135 * Cancel+Join a pthread that was configured with PTHREAD_CANCEL_ASYNCHRONOUS
136 * and is expected to exit only in response to cancellation.
137 */
138static inline void kvm_pthread_cancel_join_async(pthread_t thread)
139{
140 void *r;
141
142 __kvm_pthread_cancel_join(thread, &r);
143 TEST_ASSERT(r == PTHREAD_CANCELED,
144 "expected retval=%p, got %p", PTHREAD_CANCELED, r);
145}
146
147#define kvm_free_fd(fd) \
148do { \
149 kvm_close(fd); \
150 (fd) = -1; \
151} while (0)
152
153#endif /* SELFTEST_KVM_SYSCALLS_H */