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