1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2015 Cui Bixuan <[email protected]>
4 * Copyright (c) Linux Test Project, 2016-2022
5 */
6
7 #ifndef LAPI_SCHED_H__
8 #define LAPI_SCHED_H__
9
10 #include <sched.h>
11 #include <unistd.h>
12 #include <stdint.h>
13 #include <inttypes.h>
14 #include "config.h"
15 #include "lapi/syscalls.h"
16 #include "lapi/sched.h"
17
18 struct sched_attr {
19 uint32_t size;
20
21 uint32_t sched_policy;
22 uint64_t sched_flags;
23
24 /* SCHED_NORMAL, SCHED_BATCH */
25 int32_t sched_nice;
26
27 /* SCHED_FIFO, SCHED_RR */
28 uint32_t sched_priority;
29
30 /* SCHED_DEADLINE (nsec) */
31 uint64_t sched_runtime;
32 uint64_t sched_deadline;
33 uint64_t sched_period;
34 };
35
sched_setattr(pid_t pid,const struct sched_attr * attr,unsigned int flags)36 static inline int sched_setattr(pid_t pid, const struct sched_attr *attr,
37 unsigned int flags)
38 {
39 return syscall(__NR_sched_setattr, pid, attr, flags);
40 }
41
sched_getattr(pid_t pid,struct sched_attr * attr,unsigned int size,unsigned int flags)42 static inline int sched_getattr(pid_t pid, struct sched_attr *attr,
43 unsigned int size, unsigned int flags)
44 {
45 return syscall(__NR_sched_getattr, pid, attr, size, flags);
46 }
47
48 #ifndef HAVE_STRUCT_CLONE_ARGS
49 struct clone_args {
50 uint64_t __attribute__((aligned(8))) flags;
51 uint64_t __attribute__((aligned(8))) pidfd;
52 uint64_t __attribute__((aligned(8))) child_tid;
53 uint64_t __attribute__((aligned(8))) parent_tid;
54 uint64_t __attribute__((aligned(8))) exit_signal;
55 uint64_t __attribute__((aligned(8))) stack;
56 uint64_t __attribute__((aligned(8))) stack_size;
57 uint64_t __attribute__((aligned(8))) tls;
58 uint64_t __attribute__((aligned(8))) set_tid;
59 uint64_t __attribute__((aligned(8))) set_tid_size;
60 uint64_t __attribute__((aligned(8))) cgroup;
61 };
62 #endif
63
64 struct clone_args_minimal {
65 uint64_t __attribute__((aligned(8))) flags;
66 uint64_t __attribute__((aligned(8))) pidfd;
67 uint64_t __attribute__((aligned(8))) child_tid;
68 uint64_t __attribute__((aligned(8))) parent_tid;
69 uint64_t __attribute__((aligned(8))) exit_signal;
70 uint64_t __attribute__((aligned(8))) stack;
71 uint64_t __attribute__((aligned(8))) stack_size;
72 uint64_t __attribute__((aligned(8))) tls;
73 };
74
75 #ifndef HAVE_CLONE3
clone3(struct clone_args * args,size_t size)76 static inline int clone3(struct clone_args *args, size_t size)
77 {
78 return tst_syscall(__NR_clone3, args, size);
79 }
80 #endif
81
clone3_supported_by_kernel(void)82 static inline void clone3_supported_by_kernel(void)
83 {
84 if ((tst_kvercmp(5, 3, 0)) < 0) {
85 /* Check if the syscall is backported on an older kernel */
86 tst_syscall(__NR_clone3, NULL, 0);
87 }
88 }
89
90 #ifndef HAVE_GETCPU
getcpu(unsigned * cpu,unsigned * node)91 static inline int getcpu(unsigned *cpu, unsigned *node)
92 {
93 return tst_syscall(__NR_getcpu, cpu, node, NULL);
94 }
95 #endif
96
97 #ifndef SCHED_DEADLINE
98 # define SCHED_DEADLINE 6
99 #endif
100
101 #ifndef CLONE_VM
102 # define CLONE_VM 0x00000100
103 #endif
104
105 #ifndef CLONE_FS
106 # define CLONE_FS 0x00000200
107 #endif
108
109 #ifndef CLONE_PIDFD
110 # define CLONE_PIDFD 0x00001000
111 #endif
112
113 #ifndef CLONE_NEWNS
114 # define CLONE_NEWNS 0x00020000
115 #endif
116
117 #ifndef CLONE_SYSVSEM
118 # define CLONE_SYSVSEM 0x00040000
119 #endif
120
121 #ifndef CLONE_NEWCGROUP
122 # define CLONE_NEWCGROUP 0x02000000
123 #endif
124
125 #ifndef CLONE_NEWUTS
126 # define CLONE_NEWUTS 0x04000000
127 #endif
128
129 #ifndef CLONE_NEWIPC
130 # define CLONE_NEWIPC 0x08000000
131 #endif
132
133 #ifndef CLONE_NEWUSER
134 # define CLONE_NEWUSER 0x10000000
135 #endif
136
137 #ifndef CLONE_NEWPID
138 # define CLONE_NEWPID 0x20000000
139 #endif
140
141 #ifndef CLONE_NEWNET
142 # define CLONE_NEWNET 0x40000000
143 #endif
144
145 #ifndef CLONE_IO
146 # define CLONE_IO 0x80000000
147 #endif
148
149 #ifndef CLONE_NEWTIME
150 # define CLONE_NEWTIME 0x00000080
151 #endif
152
153 #ifndef CLONE_INTO_CGROUP
154 # define CLONE_INTO_CGROUP 0x200000000ULL
155 #endif
156
157 #endif /* LAPI_SCHED_H__ */
158