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 #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_CLONE3
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
63 struct clone_args_minimal {
64 uint64_t __attribute__((aligned(8))) flags;
65 uint64_t __attribute__((aligned(8))) pidfd;
66 uint64_t __attribute__((aligned(8))) child_tid;
67 uint64_t __attribute__((aligned(8))) parent_tid;
68 uint64_t __attribute__((aligned(8))) exit_signal;
69 uint64_t __attribute__((aligned(8))) stack;
70 uint64_t __attribute__((aligned(8))) stack_size;
71 uint64_t __attribute__((aligned(8))) tls;
72 };
73
clone3(struct clone_args * args,size_t size)74 static inline int clone3(struct clone_args *args, size_t size)
75 {
76 return tst_syscall(__NR_clone3, args, size);
77 }
78 #endif
79
clone3_supported_by_kernel(void)80 static inline void clone3_supported_by_kernel(void)
81 {
82 if ((tst_kvercmp(5, 3, 0)) < 0) {
83 /* Check if the syscall is backported on an older kernel */
84 tst_syscall(__NR_clone3, NULL, 0);
85 }
86 }
87
88 #ifndef HAVE_GETCPU
getcpu(unsigned * cpu,unsigned * node)89 static inline int getcpu(unsigned *cpu, unsigned *node)
90 {
91 return tst_syscall(__NR_getcpu, cpu, node, NULL);
92 }
93 #endif
94
95 #ifndef SCHED_DEADLINE
96 # define SCHED_DEADLINE 6
97 #endif
98
99 #ifndef CLONE_VM
100 # define CLONE_VM 0x00000100
101 #endif
102
103 #ifndef CLONE_FS
104 # define CLONE_FS 0x00000200
105 #endif
106
107 #ifndef CLONE_PIDFD
108 # define CLONE_PIDFD 0x00001000
109 #endif
110
111 #ifndef CLONE_NEWNS
112 # define CLONE_NEWNS 0x00020000
113 #endif
114
115 #ifndef CLONE_SYSVSEM
116 # define CLONE_SYSVSEM 0x00040000
117 #endif
118
119 #ifndef CLONE_NEWCGROUP
120 # define CLONE_NEWCGROUP 0x02000000
121 #endif
122
123 #ifndef CLONE_NEWUTS
124 # define CLONE_NEWUTS 0x04000000
125 #endif
126
127 #ifndef CLONE_NEWIPC
128 # define CLONE_NEWIPC 0x08000000
129 #endif
130
131 #ifndef CLONE_NEWUSER
132 # define CLONE_NEWUSER 0x10000000
133 #endif
134
135 #ifndef CLONE_NEWPID
136 # define CLONE_NEWPID 0x20000000
137 #endif
138
139 #ifndef CLONE_NEWNET
140 # define CLONE_NEWNET 0x40000000
141 #endif
142
143 #ifndef CLONE_IO
144 # define CLONE_IO 0x80000000
145 #endif
146
147 #ifndef CLONE_NEWTIME
148 # define CLONE_NEWTIME 0x00000080
149 #endif
150
151 #ifndef CLONE_INTO_CGROUP
152 # define CLONE_INTO_CGROUP 0x200000000ULL
153 #endif
154
155 #endif /* LAPI_SCHED_H__ */
156