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