linux_sched.c revision 1.81 1 1.81 kre /* $NetBSD: linux_sched.c,v 1.81 2024/09/30 01:26:47 kre Exp $ */
2 1.1 tron
3 1.1 tron /*-
4 1.73 ad * Copyright (c) 1999, 2019 The NetBSD Foundation, Inc.
5 1.1 tron * All rights reserved.
6 1.1 tron *
7 1.1 tron * This code is derived from software contributed to The NetBSD Foundation
8 1.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 tron * NASA Ames Research Center; by Matthias Scheler.
10 1.1 tron *
11 1.1 tron * Redistribution and use in source and binary forms, with or without
12 1.1 tron * modification, are permitted provided that the following conditions
13 1.1 tron * are met:
14 1.1 tron * 1. Redistributions of source code must retain the above copyright
15 1.1 tron * notice, this list of conditions and the following disclaimer.
16 1.1 tron * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 tron * notice, this list of conditions and the following disclaimer in the
18 1.1 tron * documentation and/or other materials provided with the distribution.
19 1.1 tron *
20 1.1 tron * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 tron * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 tron * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 tron * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 tron * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 tron * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 tron * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 tron * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 tron * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 tron * POSSIBILITY OF SUCH DAMAGE.
31 1.1 tron */
32 1.1 tron
33 1.1 tron /*
34 1.1 tron * Linux compatibility module. Try to deal with scheduler related syscalls.
35 1.1 tron */
36 1.8 lukem
37 1.8 lukem #include <sys/cdefs.h>
38 1.81 kre __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.81 2024/09/30 01:26:47 kre Exp $");
39 1.1 tron
40 1.1 tron #include <sys/param.h>
41 1.1 tron #include <sys/mount.h>
42 1.1 tron #include <sys/proc.h>
43 1.1 tron #include <sys/systm.h>
44 1.22 manu #include <sys/sysctl.h>
45 1.1 tron #include <sys/syscallargs.h>
46 1.14 jdolecek #include <sys/wait.h>
47 1.30 elad #include <sys/kauth.h>
48 1.34 manu #include <sys/ptrace.h>
49 1.63 chs #include <sys/atomic.h>
50 1.3 itohy
51 1.43 ad #include <sys/cpu.h>
52 1.1 tron
53 1.1 tron #include <compat/linux/common/linux_types.h>
54 1.1 tron #include <compat/linux/common/linux_signal.h>
55 1.19 manu #include <compat/linux/common/linux_emuldata.h>
56 1.44 njoly #include <compat/linux/common/linux_ipc.h>
57 1.44 njoly #include <compat/linux/common/linux_sem.h>
58 1.58 christos #include <compat/linux/common/linux_exec.h>
59 1.63 chs #include <compat/linux/common/linux_machdep.h>
60 1.1 tron
61 1.1 tron #include <compat/linux/linux_syscallargs.h>
62 1.1 tron
63 1.1 tron #include <compat/linux/common/linux_sched.h>
64 1.1 tron
65 1.65 christos static int linux_clone_nptl(struct lwp *, const struct linux_sys_clone_args *,
66 1.65 christos register_t *);
67 1.65 christos
68 1.68 christos /* Unlike Linux, dynamically calculate CPU mask size */
69 1.68 christos #define LINUX_CPU_MASK_SIZE (sizeof(long) * ((ncpu + LONG_BIT - 1) / LONG_BIT))
70 1.68 christos
71 1.65 christos #if DEBUG_LINUX
72 1.80 christos #define DPRINTF(x, ...) uprintf(x, __VA_ARGS__)
73 1.65 christos #else
74 1.80 christos #define DPRINTF(x, ...)
75 1.65 christos #endif
76 1.63 chs
77 1.63 chs static void
78 1.63 chs linux_child_return(void *arg)
79 1.63 chs {
80 1.63 chs struct lwp *l = arg;
81 1.63 chs struct proc *p = l->l_proc;
82 1.63 chs struct linux_emuldata *led = l->l_emuldata;
83 1.63 chs void *ctp = led->led_child_tidptr;
84 1.65 christos int error;
85 1.63 chs
86 1.63 chs if (ctp) {
87 1.65 christos if ((error = copyout(&p->p_pid, ctp, sizeof(p->p_pid))) != 0)
88 1.63 chs printf("%s: LINUX_CLONE_CHILD_SETTID "
89 1.65 christos "failed (child_tidptr = %p, tid = %d error =%d)\n",
90 1.65 christos __func__, ctp, p->p_pid, error);
91 1.63 chs }
92 1.63 chs child_return(arg);
93 1.63 chs }
94 1.63 chs
95 1.1 tron int
96 1.65 christos linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap,
97 1.65 christos register_t *retval)
98 1.1 tron {
99 1.46 dsl /* {
100 1.1 tron syscallarg(int) flags;
101 1.1 tron syscallarg(void *) stack;
102 1.19 manu syscallarg(void *) parent_tidptr;
103 1.63 chs syscallarg(void *) tls;
104 1.19 manu syscallarg(void *) child_tidptr;
105 1.46 dsl } */
106 1.19 manu struct linux_emuldata *led;
107 1.63 chs int flags, sig, error;
108 1.1 tron
109 1.1 tron /*
110 1.1 tron * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
111 1.1 tron */
112 1.1 tron if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
113 1.65 christos return EINVAL;
114 1.1 tron
115 1.13 jdolecek /*
116 1.13 jdolecek * Thread group implies shared signals. Shared signals
117 1.13 jdolecek * imply shared VM. This matches what Linux kernel does.
118 1.13 jdolecek */
119 1.13 jdolecek if (SCARG(uap, flags) & LINUX_CLONE_THREAD
120 1.13 jdolecek && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
121 1.65 christos return EINVAL;
122 1.13 jdolecek if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
123 1.13 jdolecek && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
124 1.65 christos return EINVAL;
125 1.13 jdolecek
126 1.63 chs /*
127 1.63 chs * The thread group flavor is implemented totally differently.
128 1.63 chs */
129 1.65 christos if (SCARG(uap, flags) & LINUX_CLONE_THREAD)
130 1.63 chs return linux_clone_nptl(l, uap, retval);
131 1.63 chs
132 1.1 tron flags = 0;
133 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_VM)
134 1.1 tron flags |= FORK_SHAREVM;
135 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_FS)
136 1.1 tron flags |= FORK_SHARECWD;
137 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_FILES)
138 1.1 tron flags |= FORK_SHAREFILES;
139 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
140 1.1 tron flags |= FORK_SHARESIGS;
141 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
142 1.1 tron flags |= FORK_PPWAIT;
143 1.1 tron
144 1.34 manu sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
145 1.34 manu if (sig < 0 || sig >= LINUX__NSIG)
146 1.65 christos return EINVAL;
147 1.34 manu sig = linux_to_native_signo[sig];
148 1.1 tron
149 1.63 chs if (SCARG(uap, flags) & LINUX_CLONE_CHILD_SETTID) {
150 1.63 chs led = l->l_emuldata;
151 1.63 chs led->led_child_tidptr = SCARG(uap, child_tidptr);
152 1.63 chs }
153 1.19 manu
154 1.1 tron /*
155 1.1 tron * Note that Linux does not provide a portable way of specifying
156 1.1 tron * the stack area; the caller must know if the stack grows up
157 1.1 tron * or down. So, we pass a stack size of 0, so that the code
158 1.1 tron * that makes this adjustment is a noop.
159 1.1 tron */
160 1.19 manu if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
161 1.71 kamil linux_child_return, NULL, retval)) != 0) {
162 1.80 christos DPRINTF("%s: fork1: error %d\n", __func__, error);
163 1.63 chs return error;
164 1.65 christos }
165 1.63 chs
166 1.63 chs return 0;
167 1.63 chs }
168 1.63 chs
169 1.80 christos
170 1.80 christos int
171 1.80 christos linux_sys_clone3(struct lwp *l, const struct linux_sys_clone3_args *uap, register_t *retval)
172 1.80 christos {
173 1.80 christos struct linux_user_clone3_args cl_args;
174 1.80 christos struct linux_sys_clone_args clone_args;
175 1.80 christos int error;
176 1.80 christos
177 1.80 christos if (SCARG(uap, size) != sizeof(cl_args)) {
178 1.80 christos DPRINTF("%s: Invalid size less or more\n", __func__);
179 1.80 christos return EINVAL;
180 1.80 christos }
181 1.80 christos
182 1.80 christos error = copyin(SCARG(uap, cl_args), &cl_args, SCARG(uap, size));
183 1.80 christos if (error) {
184 1.80 christos DPRINTF("%s: Copyin failed: %d\n", __func__, error);
185 1.80 christos return error;
186 1.80 christos }
187 1.80 christos
188 1.80 christos DPRINTF("%s: Flags: %#jx\n", __func__, (intmax_t)cl_args.flags);
189 1.80 christos
190 1.80 christos /* Define allowed flags */
191 1.80 christos if (cl_args.flags & LINUX_CLONE_UNIMPLEMENTED_FLAGS) {
192 1.80 christos DPRINTF("%s: Unsupported flags for clone3: %#x\n", __func__,
193 1.80 christos cl_args.flags & LINUX_CLONE_UNIMPLEMENTED_FLAGS);
194 1.80 christos return EOPNOTSUPP;
195 1.80 christos }
196 1.80 christos if (cl_args.flags & ~LINUX_CLONE_ALLOWED_FLAGS) {
197 1.80 christos DPRINTF("%s: Disallowed flags for clone3: %#x\n", __func__,
198 1.80 christos cl_args.flags & ~LINUX_CLONE_ALLOWED_FLAGS);
199 1.80 christos return EINVAL;
200 1.80 christos }
201 1.80 christos
202 1.80 christos #if 0
203 1.80 christos // XXX: this is wrong, exit_signal is the signal to deliver to the
204 1.80 christos // process upon exit.
205 1.80 christos if ((cl_args.exit_signal & ~(uint64_t)LINUX_CLONE_CSIGNAL) != 0){
206 1.80 christos DPRINTF("%s: Disallowed flags for clone3: %#x\n", __func__,
207 1.80 christos cl_args.exit_signal & ~(uint64_t)LINUX_CLONE_CSIGNAL);
208 1.80 christos return EINVAL;
209 1.80 christos }
210 1.80 christos #endif
211 1.80 christos
212 1.80 christos if (cl_args.stack == 0 && cl_args.stack_size != 0) {
213 1.80 christos DPRINTF("%s: Stack is NULL but stack size is not 0\n",
214 1.80 christos __func__);
215 1.80 christos return EINVAL;
216 1.80 christos }
217 1.80 christos if (cl_args.stack != 0 && cl_args.stack_size == 0) {
218 1.80 christos DPRINTF("%s: Stack is not NULL but stack size is 0\n",
219 1.80 christos __func__);
220 1.80 christos return EINVAL;
221 1.80 christos }
222 1.80 christos
223 1.80 christos int flags = cl_args.flags & LINUX_CLONE_ALLOWED_FLAGS;
224 1.80 christos #if 0
225 1.80 christos int sig = cl_args.exit_signal & LINUX_CLONE_CSIGNAL;
226 1.80 christos #endif
227 1.80 christos // XXX: Pidfd member handling
228 1.80 christos // XXX: we don't have cgroups
229 1.80 christos // XXX: what to do with tid_set and tid_set_size
230 1.80 christos // XXX: clone3 has stacksize, instead implement clone as a clone3
231 1.80 christos // wrapper.
232 1.80 christos SCARG(&clone_args, flags) = flags;
233 1.81 kre SCARG(&clone_args, stack) = (void *)(uintptr_t)cl_args.stack;
234 1.80 christos SCARG(&clone_args, parent_tidptr) =
235 1.80 christos (void *)(intptr_t)cl_args.parent_tid;
236 1.80 christos SCARG(&clone_args, tls) =
237 1.80 christos (void *)(intptr_t)cl_args.tls;
238 1.80 christos SCARG(&clone_args, child_tidptr) =
239 1.80 christos (void *)(intptr_t)cl_args.child_tid;
240 1.80 christos
241 1.80 christos return linux_sys_clone(l, &clone_args, retval);
242 1.80 christos }
243 1.80 christos
244 1.63 chs static int
245 1.63 chs linux_clone_nptl(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
246 1.63 chs {
247 1.63 chs /* {
248 1.63 chs syscallarg(int) flags;
249 1.63 chs syscallarg(void *) stack;
250 1.63 chs syscallarg(void *) parent_tidptr;
251 1.63 chs syscallarg(void *) tls;
252 1.63 chs syscallarg(void *) child_tidptr;
253 1.63 chs } */
254 1.63 chs struct proc *p;
255 1.63 chs struct lwp *l2;
256 1.63 chs struct linux_emuldata *led;
257 1.63 chs void *parent_tidptr, *tls, *child_tidptr;
258 1.63 chs vaddr_t uaddr;
259 1.63 chs lwpid_t lid;
260 1.74 thorpej int flags, error;
261 1.63 chs
262 1.63 chs p = l->l_proc;
263 1.63 chs flags = SCARG(uap, flags);
264 1.63 chs parent_tidptr = SCARG(uap, parent_tidptr);
265 1.63 chs tls = SCARG(uap, tls);
266 1.63 chs child_tidptr = SCARG(uap, child_tidptr);
267 1.63 chs
268 1.63 chs uaddr = uvm_uarea_alloc();
269 1.63 chs if (__predict_false(uaddr == 0)) {
270 1.63 chs return ENOMEM;
271 1.63 chs }
272 1.63 chs
273 1.75 thorpej error = lwp_create(l, p, uaddr, LWP_DETACHED,
274 1.69 christos SCARG(uap, stack), 0, child_return, NULL, &l2, l->l_class,
275 1.69 christos &l->l_sigmask, &l->l_sigstk);
276 1.63 chs if (__predict_false(error)) {
277 1.65 christos DPRINTF(("%s: lwp_create error=%d\n", __func__, error));
278 1.63 chs uvm_uarea_free(uaddr);
279 1.19 manu return error;
280 1.63 chs }
281 1.63 chs lid = l2->l_lid;
282 1.19 manu
283 1.63 chs /* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */
284 1.63 chs if (flags & LINUX_CLONE_CHILD_CLEARTID) {
285 1.63 chs led = l2->l_emuldata;
286 1.63 chs led->led_clear_tid = child_tidptr;
287 1.63 chs }
288 1.63 chs
289 1.63 chs /* LINUX_CLONE_PARENT_SETTID: store child's TID in parent's memory */
290 1.63 chs if (flags & LINUX_CLONE_PARENT_SETTID) {
291 1.65 christos if ((error = copyout(&lid, parent_tidptr, sizeof(lid))) != 0)
292 1.63 chs printf("%s: LINUX_CLONE_PARENT_SETTID "
293 1.65 christos "failed (parent_tidptr = %p tid = %d error=%d)\n",
294 1.65 christos __func__, parent_tidptr, lid, error);
295 1.63 chs }
296 1.63 chs
297 1.63 chs /* LINUX_CLONE_CHILD_SETTID: store child's TID in child's memory */
298 1.63 chs if (flags & LINUX_CLONE_CHILD_SETTID) {
299 1.65 christos if ((error = copyout(&lid, child_tidptr, sizeof(lid))) != 0)
300 1.63 chs printf("%s: LINUX_CLONE_CHILD_SETTID "
301 1.65 christos "failed (child_tidptr = %p, tid = %d error=%d)\n",
302 1.65 christos __func__, child_tidptr, lid, error);
303 1.63 chs }
304 1.63 chs
305 1.63 chs if (flags & LINUX_CLONE_SETTLS) {
306 1.63 chs error = LINUX_LWP_SETPRIVATE(l2, tls);
307 1.63 chs if (error) {
308 1.65 christos DPRINTF(("%s: LINUX_LWP_SETPRIVATE %d\n", __func__,
309 1.65 christos error));
310 1.63 chs lwp_exit(l2);
311 1.63 chs return error;
312 1.63 chs }
313 1.63 chs }
314 1.63 chs
315 1.73 ad /* Set the new LWP running. */
316 1.73 ad lwp_start(l2, 0);
317 1.58 christos
318 1.63 chs retval[0] = lid;
319 1.63 chs retval[1] = 0;
320 1.19 manu return 0;
321 1.1 tron }
322 1.1 tron
323 1.49 elad /*
324 1.49 elad * linux realtime priority
325 1.49 elad *
326 1.49 elad * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
327 1.49 elad *
328 1.49 elad * - SCHED_OTHER tasks don't have realtime priorities.
329 1.49 elad * in particular, sched_param::sched_priority is always 0.
330 1.49 elad */
331 1.49 elad
332 1.49 elad #define LINUX_SCHED_RTPRIO_MIN 1
333 1.49 elad #define LINUX_SCHED_RTPRIO_MAX 99
334 1.49 elad
335 1.49 elad static int
336 1.49 elad sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
337 1.49 elad int *native_policy, struct sched_param *native_params)
338 1.49 elad {
339 1.49 elad
340 1.49 elad switch (linux_policy) {
341 1.49 elad case LINUX_SCHED_OTHER:
342 1.49 elad if (native_policy != NULL) {
343 1.49 elad *native_policy = SCHED_OTHER;
344 1.49 elad }
345 1.49 elad break;
346 1.49 elad
347 1.49 elad case LINUX_SCHED_FIFO:
348 1.49 elad if (native_policy != NULL) {
349 1.49 elad *native_policy = SCHED_FIFO;
350 1.49 elad }
351 1.49 elad break;
352 1.49 elad
353 1.49 elad case LINUX_SCHED_RR:
354 1.49 elad if (native_policy != NULL) {
355 1.49 elad *native_policy = SCHED_RR;
356 1.49 elad }
357 1.49 elad break;
358 1.49 elad
359 1.49 elad default:
360 1.49 elad return EINVAL;
361 1.49 elad }
362 1.49 elad
363 1.49 elad if (linux_params != NULL) {
364 1.49 elad int prio = linux_params->sched_priority;
365 1.49 elad
366 1.49 elad KASSERT(native_params != NULL);
367 1.49 elad
368 1.49 elad if (linux_policy == LINUX_SCHED_OTHER) {
369 1.49 elad if (prio != 0) {
370 1.49 elad return EINVAL;
371 1.49 elad }
372 1.49 elad native_params->sched_priority = PRI_NONE; /* XXX */
373 1.49 elad } else {
374 1.49 elad if (prio < LINUX_SCHED_RTPRIO_MIN ||
375 1.49 elad prio > LINUX_SCHED_RTPRIO_MAX) {
376 1.49 elad return EINVAL;
377 1.49 elad }
378 1.49 elad native_params->sched_priority =
379 1.49 elad (prio - LINUX_SCHED_RTPRIO_MIN)
380 1.49 elad * (SCHED_PRI_MAX - SCHED_PRI_MIN)
381 1.49 elad / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
382 1.49 elad + SCHED_PRI_MIN;
383 1.49 elad }
384 1.49 elad }
385 1.49 elad
386 1.49 elad return 0;
387 1.49 elad }
388 1.49 elad
389 1.49 elad static int
390 1.49 elad sched_native2linux(int native_policy, struct sched_param *native_params,
391 1.49 elad int *linux_policy, struct linux_sched_param *linux_params)
392 1.49 elad {
393 1.49 elad
394 1.49 elad switch (native_policy) {
395 1.49 elad case SCHED_OTHER:
396 1.49 elad if (linux_policy != NULL) {
397 1.49 elad *linux_policy = LINUX_SCHED_OTHER;
398 1.49 elad }
399 1.49 elad break;
400 1.49 elad
401 1.49 elad case SCHED_FIFO:
402 1.49 elad if (linux_policy != NULL) {
403 1.49 elad *linux_policy = LINUX_SCHED_FIFO;
404 1.49 elad }
405 1.49 elad break;
406 1.49 elad
407 1.49 elad case SCHED_RR:
408 1.49 elad if (linux_policy != NULL) {
409 1.49 elad *linux_policy = LINUX_SCHED_RR;
410 1.49 elad }
411 1.49 elad break;
412 1.49 elad
413 1.49 elad default:
414 1.49 elad panic("%s: unknown policy %d\n", __func__, native_policy);
415 1.49 elad }
416 1.49 elad
417 1.49 elad if (native_params != NULL) {
418 1.49 elad int prio = native_params->sched_priority;
419 1.49 elad
420 1.49 elad KASSERT(prio >= SCHED_PRI_MIN);
421 1.49 elad KASSERT(prio <= SCHED_PRI_MAX);
422 1.49 elad KASSERT(linux_params != NULL);
423 1.56 jmcneill
424 1.79 riastrad memset(linux_params, 0, sizeof(*linux_params));
425 1.79 riastrad
426 1.80 christos DPRINTF("%s: native: policy %d, priority %d\n",
427 1.80 christos __func__, native_policy, prio);
428 1.49 elad
429 1.49 elad if (native_policy == SCHED_OTHER) {
430 1.49 elad linux_params->sched_priority = 0;
431 1.49 elad } else {
432 1.49 elad linux_params->sched_priority =
433 1.49 elad (prio - SCHED_PRI_MIN)
434 1.49 elad * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
435 1.49 elad / (SCHED_PRI_MAX - SCHED_PRI_MIN)
436 1.49 elad + LINUX_SCHED_RTPRIO_MIN;
437 1.49 elad }
438 1.80 christos DPRINTF("%s: linux: policy %d, priority %d\n",
439 1.80 christos __func__, -1, linux_params->sched_priority);
440 1.49 elad }
441 1.49 elad
442 1.49 elad return 0;
443 1.49 elad }
444 1.49 elad
445 1.1 tron int
446 1.46 dsl linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
447 1.1 tron {
448 1.46 dsl /* {
449 1.1 tron syscallarg(linux_pid_t) pid;
450 1.1 tron syscallarg(const struct linux_sched_param *) sp;
451 1.46 dsl } */
452 1.49 elad int error, policy;
453 1.1 tron struct linux_sched_param lp;
454 1.49 elad struct sched_param sp;
455 1.49 elad
456 1.49 elad if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
457 1.49 elad error = EINVAL;
458 1.49 elad goto out;
459 1.49 elad }
460 1.1 tron
461 1.49 elad error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
462 1.49 elad if (error)
463 1.49 elad goto out;
464 1.1 tron
465 1.49 elad /* We need the current policy in Linux terms. */
466 1.66 njoly error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
467 1.49 elad if (error)
468 1.49 elad goto out;
469 1.49 elad error = sched_native2linux(policy, NULL, &policy, NULL);
470 1.49 elad if (error)
471 1.49 elad goto out;
472 1.1 tron
473 1.49 elad error = sched_linux2native(policy, &lp, &policy, &sp);
474 1.1 tron if (error)
475 1.49 elad goto out;
476 1.1 tron
477 1.66 njoly error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
478 1.49 elad if (error)
479 1.49 elad goto out;
480 1.1 tron
481 1.49 elad out:
482 1.49 elad return error;
483 1.1 tron }
484 1.1 tron
485 1.1 tron int
486 1.46 dsl linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
487 1.1 tron {
488 1.46 dsl /* {
489 1.1 tron syscallarg(linux_pid_t) pid;
490 1.1 tron syscallarg(struct linux_sched_param *) sp;
491 1.46 dsl } */
492 1.1 tron struct linux_sched_param lp;
493 1.49 elad struct sched_param sp;
494 1.50 elad int error, policy;
495 1.49 elad
496 1.49 elad if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
497 1.49 elad error = EINVAL;
498 1.49 elad goto out;
499 1.49 elad }
500 1.1 tron
501 1.66 njoly error = do_sched_getparam(SCARG(uap, pid), 0, &policy, &sp);
502 1.49 elad if (error)
503 1.49 elad goto out;
504 1.80 christos DPRINTF("%s: native: policy %d, priority %d\n",
505 1.80 christos __func__, policy, sp.sched_priority);
506 1.1 tron
507 1.50 elad error = sched_native2linux(policy, &sp, NULL, &lp);
508 1.49 elad if (error)
509 1.49 elad goto out;
510 1.80 christos DPRINTF("%s: linux: policy %d, priority %d\n",
511 1.80 christos __func__, policy, lp.sched_priority);
512 1.47 elad
513 1.49 elad error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
514 1.49 elad if (error)
515 1.49 elad goto out;
516 1.1 tron
517 1.49 elad out:
518 1.49 elad return error;
519 1.1 tron }
520 1.1 tron
521 1.1 tron int
522 1.46 dsl linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
523 1.1 tron {
524 1.46 dsl /* {
525 1.1 tron syscallarg(linux_pid_t) pid;
526 1.1 tron syscallarg(int) policy;
527 1.61 njoly syscallarg(cont struct linux_sched_param *) sp;
528 1.46 dsl } */
529 1.49 elad int error, policy;
530 1.1 tron struct linux_sched_param lp;
531 1.49 elad struct sched_param sp;
532 1.1 tron
533 1.49 elad if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
534 1.49 elad error = EINVAL;
535 1.49 elad goto out;
536 1.49 elad }
537 1.1 tron
538 1.1 tron error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
539 1.1 tron if (error)
540 1.49 elad goto out;
541 1.80 christos DPRINTF("%s: linux: policy %d, priority %d\n",
542 1.80 christos __func__, SCARG(uap, policy), lp.sched_priority);
543 1.1 tron
544 1.49 elad error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
545 1.49 elad if (error)
546 1.49 elad goto out;
547 1.80 christos DPRINTF("%s: native: policy %d, priority %d\n",
548 1.80 christos __func__, policy, sp.sched_priority);
549 1.1 tron
550 1.66 njoly error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
551 1.49 elad if (error)
552 1.49 elad goto out;
553 1.1 tron
554 1.49 elad out:
555 1.49 elad return error;
556 1.1 tron }
557 1.1 tron
558 1.1 tron int
559 1.46 dsl linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval)
560 1.1 tron {
561 1.46 dsl /* {
562 1.1 tron syscallarg(linux_pid_t) pid;
563 1.46 dsl } */
564 1.49 elad int error, policy;
565 1.1 tron
566 1.1 tron *retval = -1;
567 1.1 tron
568 1.66 njoly error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
569 1.49 elad if (error)
570 1.49 elad goto out;
571 1.49 elad
572 1.49 elad error = sched_native2linux(policy, NULL, &policy, NULL);
573 1.49 elad if (error)
574 1.49 elad goto out;
575 1.49 elad
576 1.49 elad *retval = policy;
577 1.1 tron
578 1.49 elad out:
579 1.49 elad return error;
580 1.1 tron }
581 1.1 tron
582 1.1 tron int
583 1.46 dsl linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
584 1.1 tron {
585 1.11 gmcgarry
586 1.11 gmcgarry yield();
587 1.1 tron return 0;
588 1.1 tron }
589 1.1 tron
590 1.1 tron int
591 1.46 dsl linux_sys_sched_get_priority_max(struct lwp *l, const struct linux_sys_sched_get_priority_max_args *uap, register_t *retval)
592 1.1 tron {
593 1.46 dsl /* {
594 1.1 tron syscallarg(int) policy;
595 1.46 dsl } */
596 1.1 tron
597 1.55 njoly switch (SCARG(uap, policy)) {
598 1.55 njoly case LINUX_SCHED_OTHER:
599 1.55 njoly *retval = 0;
600 1.55 njoly break;
601 1.55 njoly case LINUX_SCHED_FIFO:
602 1.55 njoly case LINUX_SCHED_RR:
603 1.55 njoly *retval = LINUX_SCHED_RTPRIO_MAX;
604 1.55 njoly break;
605 1.55 njoly default:
606 1.1 tron return EINVAL;
607 1.1 tron }
608 1.1 tron
609 1.1 tron return 0;
610 1.1 tron }
611 1.1 tron
612 1.1 tron int
613 1.46 dsl linux_sys_sched_get_priority_min(struct lwp *l, const struct linux_sys_sched_get_priority_min_args *uap, register_t *retval)
614 1.1 tron {
615 1.46 dsl /* {
616 1.1 tron syscallarg(int) policy;
617 1.46 dsl } */
618 1.1 tron
619 1.55 njoly switch (SCARG(uap, policy)) {
620 1.55 njoly case LINUX_SCHED_OTHER:
621 1.55 njoly *retval = 0;
622 1.55 njoly break;
623 1.55 njoly case LINUX_SCHED_FIFO:
624 1.55 njoly case LINUX_SCHED_RR:
625 1.55 njoly *retval = LINUX_SCHED_RTPRIO_MIN;
626 1.55 njoly break;
627 1.55 njoly default:
628 1.1 tron return EINVAL;
629 1.1 tron }
630 1.1 tron
631 1.1 tron return 0;
632 1.1 tron }
633 1.14 jdolecek
634 1.63 chs int
635 1.63 chs linux_sys_exit(struct lwp *l, const struct linux_sys_exit_args *uap, register_t *retval)
636 1.63 chs {
637 1.63 chs
638 1.63 chs lwp_exit(l);
639 1.63 chs return 0;
640 1.63 chs }
641 1.63 chs
642 1.14 jdolecek #ifndef __m68k__
643 1.14 jdolecek /* Present on everything but m68k */
644 1.14 jdolecek int
645 1.46 dsl linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval)
646 1.14 jdolecek {
647 1.14 jdolecek
648 1.46 dsl return sys_exit(l, (const void *)uap, retval);
649 1.14 jdolecek }
650 1.14 jdolecek #endif /* !__m68k__ */
651 1.19 manu
652 1.19 manu int
653 1.46 dsl linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval)
654 1.19 manu {
655 1.46 dsl /* {
656 1.19 manu syscallarg(int *) tidptr;
657 1.46 dsl } */
658 1.19 manu struct linux_emuldata *led;
659 1.19 manu
660 1.63 chs led = (struct linux_emuldata *)l->l_emuldata;
661 1.63 chs led->led_clear_tid = SCARG(uap, tid);
662 1.63 chs *retval = l->l_lid;
663 1.19 manu
664 1.19 manu return 0;
665 1.19 manu }
666 1.20 manu
667 1.20 manu /* ARGUSED1 */
668 1.20 manu int
669 1.46 dsl linux_sys_gettid(struct lwp *l, const void *v, register_t *retval)
670 1.20 manu {
671 1.31 manu
672 1.63 chs *retval = l->l_lid;
673 1.31 manu return 0;
674 1.31 manu }
675 1.31 manu
676 1.68 christos /*
677 1.68 christos * The affinity syscalls assume that the layout of our cpu kcpuset is
678 1.68 christos * the same as linux's: a linear bitmask.
679 1.68 christos */
680 1.22 manu int
681 1.46 dsl linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval)
682 1.22 manu {
683 1.46 dsl /* {
684 1.63 chs syscallarg(linux_pid_t) pid;
685 1.22 manu syscallarg(unsigned int) len;
686 1.22 manu syscallarg(unsigned long *) mask;
687 1.46 dsl } */
688 1.76 thorpej struct proc *p;
689 1.68 christos struct lwp *t;
690 1.68 christos kcpuset_t *kcset;
691 1.68 christos size_t size;
692 1.68 christos cpuid_t i;
693 1.68 christos int error;
694 1.22 manu
695 1.68 christos size = LINUX_CPU_MASK_SIZE;
696 1.60 njoly if (SCARG(uap, len) < size)
697 1.22 manu return EINVAL;
698 1.22 manu
699 1.76 thorpej if (SCARG(uap, pid) == 0) {
700 1.76 thorpej p = curproc;
701 1.76 thorpej mutex_enter(p->p_lock);
702 1.76 thorpej t = curlwp;
703 1.76 thorpej } else {
704 1.76 thorpej t = lwp_find2(-1, SCARG(uap, pid));
705 1.76 thorpej if (__predict_false(t == NULL)) {
706 1.76 thorpej return ESRCH;
707 1.76 thorpej }
708 1.76 thorpej p = t->l_proc;
709 1.76 thorpej KASSERT(mutex_owned(p->p_lock));
710 1.76 thorpej }
711 1.22 manu
712 1.68 christos /* Check the permission */
713 1.68 christos if (kauth_authorize_process(l->l_cred,
714 1.76 thorpej KAUTH_PROCESS_SCHEDULER_GETAFFINITY, p, NULL, NULL, NULL)) {
715 1.76 thorpej mutex_exit(p->p_lock);
716 1.68 christos return EPERM;
717 1.68 christos }
718 1.68 christos
719 1.68 christos kcpuset_create(&kcset, true);
720 1.68 christos lwp_lock(t);
721 1.68 christos if (t->l_affinity != NULL)
722 1.68 christos kcpuset_copy(kcset, t->l_affinity);
723 1.68 christos else {
724 1.68 christos /*
725 1.68 christos * All available CPUs should be masked when affinity has not
726 1.68 christos * been set.
727 1.68 christos */
728 1.68 christos kcpuset_zero(kcset);
729 1.68 christos for (i = 0; i < ncpu; i++)
730 1.68 christos kcpuset_set(kcset, i);
731 1.68 christos }
732 1.68 christos lwp_unlock(t);
733 1.76 thorpej mutex_exit(p->p_lock);
734 1.68 christos error = kcpuset_copyout(kcset, (cpuset_t *)SCARG(uap, mask), size);
735 1.68 christos kcpuset_unuse(kcset, NULL);
736 1.60 njoly *retval = size;
737 1.59 njoly return error;
738 1.22 manu }
739 1.22 manu
740 1.22 manu int
741 1.46 dsl linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval)
742 1.22 manu {
743 1.46 dsl /* {
744 1.63 chs syscallarg(linux_pid_t) pid;
745 1.22 manu syscallarg(unsigned int) len;
746 1.22 manu syscallarg(unsigned long *) mask;
747 1.46 dsl } */
748 1.68 christos struct sys__sched_setaffinity_args ssa;
749 1.68 christos size_t size;
750 1.76 thorpej pid_t pid;
751 1.77 rin lwpid_t lid;
752 1.68 christos
753 1.68 christos size = LINUX_CPU_MASK_SIZE;
754 1.68 christos if (SCARG(uap, len) < size)
755 1.68 christos return EINVAL;
756 1.22 manu
757 1.77 rin lid = SCARG(uap, pid);
758 1.77 rin if (lid != 0) {
759 1.76 thorpej /* Get the canonical PID for the process. */
760 1.78 ad mutex_enter(&proc_lock);
761 1.76 thorpej struct proc *p = proc_find_lwpid(SCARG(uap, pid));
762 1.76 thorpej if (p == NULL) {
763 1.78 ad mutex_exit(&proc_lock);
764 1.76 thorpej return ESRCH;
765 1.76 thorpej }
766 1.76 thorpej pid = p->p_pid;
767 1.78 ad mutex_exit(&proc_lock);
768 1.76 thorpej } else {
769 1.77 rin pid = curproc->p_pid;
770 1.77 rin lid = curlwp->l_lid;
771 1.76 thorpej }
772 1.76 thorpej
773 1.76 thorpej SCARG(&ssa, pid) = pid;
774 1.77 rin SCARG(&ssa, lid) = lid;
775 1.68 christos SCARG(&ssa, size) = size;
776 1.68 christos SCARG(&ssa, cpuset) = (cpuset_t *)SCARG(uap, mask);
777 1.22 manu
778 1.68 christos return sys__sched_setaffinity(l, &ssa, retval);
779 1.64 dsl }
780