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