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