linux_sched.c revision 1.84 1 1.84 christos /* $NetBSD: linux_sched.c,v 1.84 2025/09/19 18:41:58 christos 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.84 christos __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.84 2025/09/19 18:41:58 christos 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.83 hannken DPRINTF("%s: Unsupported flags for clone3: %#" PRIx64 "\n",
193 1.83 hannken __func__, 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.83 hannken DPRINTF("%s: Disallowed flags for clone3: %#" PRIx64 "\n",
198 1.83 hannken __func__, 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.82 riastrad
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.82 riastrad // 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.84 christos #ifdef __MACHINE_STACK_GROWS_UP
234 1.81 kre SCARG(&clone_args, stack) = (void *)(uintptr_t)cl_args.stack;
235 1.84 christos #else
236 1.84 christos SCARG(&clone_args, stack) =
237 1.84 christos (void *)((uintptr_t)cl_args.stack + cl_args.stack_size);
238 1.84 christos #endif
239 1.80 christos SCARG(&clone_args, parent_tidptr) =
240 1.80 christos (void *)(intptr_t)cl_args.parent_tid;
241 1.80 christos SCARG(&clone_args, tls) =
242 1.80 christos (void *)(intptr_t)cl_args.tls;
243 1.80 christos SCARG(&clone_args, child_tidptr) =
244 1.80 christos (void *)(intptr_t)cl_args.child_tid;
245 1.80 christos
246 1.80 christos return linux_sys_clone(l, &clone_args, retval);
247 1.80 christos }
248 1.80 christos
249 1.63 chs static int
250 1.63 chs linux_clone_nptl(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
251 1.63 chs {
252 1.63 chs /* {
253 1.63 chs syscallarg(int) flags;
254 1.63 chs syscallarg(void *) stack;
255 1.63 chs syscallarg(void *) parent_tidptr;
256 1.63 chs syscallarg(void *) tls;
257 1.63 chs syscallarg(void *) child_tidptr;
258 1.63 chs } */
259 1.63 chs struct proc *p;
260 1.63 chs struct lwp *l2;
261 1.63 chs struct linux_emuldata *led;
262 1.63 chs void *parent_tidptr, *tls, *child_tidptr;
263 1.63 chs vaddr_t uaddr;
264 1.63 chs lwpid_t lid;
265 1.74 thorpej int flags, error;
266 1.63 chs
267 1.63 chs p = l->l_proc;
268 1.63 chs flags = SCARG(uap, flags);
269 1.63 chs parent_tidptr = SCARG(uap, parent_tidptr);
270 1.63 chs tls = SCARG(uap, tls);
271 1.63 chs child_tidptr = SCARG(uap, child_tidptr);
272 1.63 chs
273 1.63 chs uaddr = uvm_uarea_alloc();
274 1.63 chs if (__predict_false(uaddr == 0)) {
275 1.63 chs return ENOMEM;
276 1.63 chs }
277 1.63 chs
278 1.75 thorpej error = lwp_create(l, p, uaddr, LWP_DETACHED,
279 1.69 christos SCARG(uap, stack), 0, child_return, NULL, &l2, l->l_class,
280 1.69 christos &l->l_sigmask, &l->l_sigstk);
281 1.63 chs if (__predict_false(error)) {
282 1.83 hannken DPRINTF("%s: lwp_create error=%d\n", __func__, error);
283 1.63 chs uvm_uarea_free(uaddr);
284 1.19 manu return error;
285 1.63 chs }
286 1.63 chs lid = l2->l_lid;
287 1.19 manu
288 1.63 chs /* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */
289 1.63 chs if (flags & LINUX_CLONE_CHILD_CLEARTID) {
290 1.63 chs led = l2->l_emuldata;
291 1.63 chs led->led_clear_tid = child_tidptr;
292 1.63 chs }
293 1.63 chs
294 1.63 chs /* LINUX_CLONE_PARENT_SETTID: store child's TID in parent's memory */
295 1.63 chs if (flags & LINUX_CLONE_PARENT_SETTID) {
296 1.65 christos if ((error = copyout(&lid, parent_tidptr, sizeof(lid))) != 0)
297 1.63 chs printf("%s: LINUX_CLONE_PARENT_SETTID "
298 1.65 christos "failed (parent_tidptr = %p tid = %d error=%d)\n",
299 1.65 christos __func__, parent_tidptr, lid, error);
300 1.63 chs }
301 1.63 chs
302 1.63 chs /* LINUX_CLONE_CHILD_SETTID: store child's TID in child's memory */
303 1.63 chs if (flags & LINUX_CLONE_CHILD_SETTID) {
304 1.65 christos if ((error = copyout(&lid, child_tidptr, sizeof(lid))) != 0)
305 1.63 chs printf("%s: LINUX_CLONE_CHILD_SETTID "
306 1.65 christos "failed (child_tidptr = %p, tid = %d error=%d)\n",
307 1.65 christos __func__, child_tidptr, lid, error);
308 1.63 chs }
309 1.63 chs
310 1.63 chs if (flags & LINUX_CLONE_SETTLS) {
311 1.63 chs error = LINUX_LWP_SETPRIVATE(l2, tls);
312 1.63 chs if (error) {
313 1.83 hannken DPRINTF("%s: LINUX_LWP_SETPRIVATE %d\n", __func__,
314 1.83 hannken error);
315 1.63 chs lwp_exit(l2);
316 1.63 chs return error;
317 1.63 chs }
318 1.63 chs }
319 1.63 chs
320 1.73 ad /* Set the new LWP running. */
321 1.73 ad lwp_start(l2, 0);
322 1.58 christos
323 1.63 chs retval[0] = lid;
324 1.63 chs retval[1] = 0;
325 1.19 manu return 0;
326 1.1 tron }
327 1.1 tron
328 1.49 elad /*
329 1.49 elad * linux realtime priority
330 1.49 elad *
331 1.49 elad * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
332 1.49 elad *
333 1.49 elad * - SCHED_OTHER tasks don't have realtime priorities.
334 1.49 elad * in particular, sched_param::sched_priority is always 0.
335 1.49 elad */
336 1.49 elad
337 1.49 elad #define LINUX_SCHED_RTPRIO_MIN 1
338 1.49 elad #define LINUX_SCHED_RTPRIO_MAX 99
339 1.49 elad
340 1.49 elad static int
341 1.49 elad sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
342 1.49 elad int *native_policy, struct sched_param *native_params)
343 1.49 elad {
344 1.49 elad
345 1.49 elad switch (linux_policy) {
346 1.49 elad case LINUX_SCHED_OTHER:
347 1.49 elad if (native_policy != NULL) {
348 1.49 elad *native_policy = SCHED_OTHER;
349 1.49 elad }
350 1.49 elad break;
351 1.49 elad
352 1.49 elad case LINUX_SCHED_FIFO:
353 1.49 elad if (native_policy != NULL) {
354 1.49 elad *native_policy = SCHED_FIFO;
355 1.49 elad }
356 1.49 elad break;
357 1.49 elad
358 1.49 elad case LINUX_SCHED_RR:
359 1.49 elad if (native_policy != NULL) {
360 1.49 elad *native_policy = SCHED_RR;
361 1.49 elad }
362 1.49 elad break;
363 1.49 elad
364 1.49 elad default:
365 1.49 elad return EINVAL;
366 1.49 elad }
367 1.49 elad
368 1.49 elad if (linux_params != NULL) {
369 1.49 elad int prio = linux_params->sched_priority;
370 1.82 riastrad
371 1.49 elad KASSERT(native_params != NULL);
372 1.49 elad
373 1.49 elad if (linux_policy == LINUX_SCHED_OTHER) {
374 1.49 elad if (prio != 0) {
375 1.49 elad return EINVAL;
376 1.49 elad }
377 1.49 elad native_params->sched_priority = PRI_NONE; /* XXX */
378 1.49 elad } else {
379 1.49 elad if (prio < LINUX_SCHED_RTPRIO_MIN ||
380 1.49 elad prio > LINUX_SCHED_RTPRIO_MAX) {
381 1.49 elad return EINVAL;
382 1.49 elad }
383 1.49 elad native_params->sched_priority =
384 1.49 elad (prio - LINUX_SCHED_RTPRIO_MIN)
385 1.49 elad * (SCHED_PRI_MAX - SCHED_PRI_MIN)
386 1.49 elad / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
387 1.49 elad + SCHED_PRI_MIN;
388 1.49 elad }
389 1.49 elad }
390 1.49 elad
391 1.49 elad return 0;
392 1.49 elad }
393 1.49 elad
394 1.49 elad static int
395 1.49 elad sched_native2linux(int native_policy, struct sched_param *native_params,
396 1.49 elad int *linux_policy, struct linux_sched_param *linux_params)
397 1.49 elad {
398 1.49 elad
399 1.49 elad switch (native_policy) {
400 1.49 elad case SCHED_OTHER:
401 1.49 elad if (linux_policy != NULL) {
402 1.49 elad *linux_policy = LINUX_SCHED_OTHER;
403 1.49 elad }
404 1.49 elad break;
405 1.49 elad
406 1.49 elad case SCHED_FIFO:
407 1.49 elad if (linux_policy != NULL) {
408 1.49 elad *linux_policy = LINUX_SCHED_FIFO;
409 1.49 elad }
410 1.49 elad break;
411 1.49 elad
412 1.49 elad case SCHED_RR:
413 1.49 elad if (linux_policy != NULL) {
414 1.49 elad *linux_policy = LINUX_SCHED_RR;
415 1.49 elad }
416 1.49 elad break;
417 1.49 elad
418 1.49 elad default:
419 1.49 elad panic("%s: unknown policy %d\n", __func__, native_policy);
420 1.49 elad }
421 1.49 elad
422 1.49 elad if (native_params != NULL) {
423 1.49 elad int prio = native_params->sched_priority;
424 1.49 elad
425 1.49 elad KASSERT(prio >= SCHED_PRI_MIN);
426 1.49 elad KASSERT(prio <= SCHED_PRI_MAX);
427 1.49 elad KASSERT(linux_params != NULL);
428 1.56 jmcneill
429 1.79 riastrad memset(linux_params, 0, sizeof(*linux_params));
430 1.79 riastrad
431 1.80 christos DPRINTF("%s: native: policy %d, priority %d\n",
432 1.80 christos __func__, native_policy, prio);
433 1.49 elad
434 1.49 elad if (native_policy == SCHED_OTHER) {
435 1.49 elad linux_params->sched_priority = 0;
436 1.49 elad } else {
437 1.49 elad linux_params->sched_priority =
438 1.49 elad (prio - SCHED_PRI_MIN)
439 1.49 elad * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
440 1.49 elad / (SCHED_PRI_MAX - SCHED_PRI_MIN)
441 1.49 elad + LINUX_SCHED_RTPRIO_MIN;
442 1.49 elad }
443 1.80 christos DPRINTF("%s: linux: policy %d, priority %d\n",
444 1.80 christos __func__, -1, linux_params->sched_priority);
445 1.49 elad }
446 1.49 elad
447 1.49 elad return 0;
448 1.49 elad }
449 1.49 elad
450 1.1 tron int
451 1.46 dsl linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
452 1.1 tron {
453 1.46 dsl /* {
454 1.1 tron syscallarg(linux_pid_t) pid;
455 1.1 tron syscallarg(const struct linux_sched_param *) sp;
456 1.46 dsl } */
457 1.49 elad int error, policy;
458 1.1 tron struct linux_sched_param lp;
459 1.49 elad struct sched_param sp;
460 1.49 elad
461 1.49 elad if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
462 1.49 elad error = EINVAL;
463 1.49 elad goto out;
464 1.49 elad }
465 1.1 tron
466 1.49 elad error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
467 1.49 elad if (error)
468 1.49 elad goto out;
469 1.1 tron
470 1.49 elad /* We need the current policy in Linux terms. */
471 1.66 njoly error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
472 1.49 elad if (error)
473 1.49 elad goto out;
474 1.49 elad error = sched_native2linux(policy, NULL, &policy, NULL);
475 1.49 elad if (error)
476 1.49 elad goto out;
477 1.1 tron
478 1.49 elad error = sched_linux2native(policy, &lp, &policy, &sp);
479 1.1 tron if (error)
480 1.49 elad goto out;
481 1.1 tron
482 1.66 njoly error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
483 1.49 elad if (error)
484 1.49 elad goto out;
485 1.1 tron
486 1.49 elad out:
487 1.49 elad return error;
488 1.1 tron }
489 1.1 tron
490 1.1 tron int
491 1.46 dsl linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
492 1.1 tron {
493 1.46 dsl /* {
494 1.1 tron syscallarg(linux_pid_t) pid;
495 1.1 tron syscallarg(struct linux_sched_param *) sp;
496 1.46 dsl } */
497 1.1 tron struct linux_sched_param lp;
498 1.49 elad struct sched_param sp;
499 1.50 elad int error, policy;
500 1.49 elad
501 1.49 elad if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
502 1.49 elad error = EINVAL;
503 1.49 elad goto out;
504 1.49 elad }
505 1.1 tron
506 1.66 njoly error = do_sched_getparam(SCARG(uap, pid), 0, &policy, &sp);
507 1.49 elad if (error)
508 1.49 elad goto out;
509 1.80 christos DPRINTF("%s: native: policy %d, priority %d\n",
510 1.80 christos __func__, policy, sp.sched_priority);
511 1.1 tron
512 1.50 elad error = sched_native2linux(policy, &sp, NULL, &lp);
513 1.49 elad if (error)
514 1.49 elad goto out;
515 1.80 christos DPRINTF("%s: linux: policy %d, priority %d\n",
516 1.80 christos __func__, policy, lp.sched_priority);
517 1.47 elad
518 1.49 elad error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
519 1.49 elad if (error)
520 1.49 elad goto out;
521 1.1 tron
522 1.49 elad out:
523 1.49 elad return error;
524 1.1 tron }
525 1.1 tron
526 1.1 tron int
527 1.46 dsl linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
528 1.1 tron {
529 1.46 dsl /* {
530 1.1 tron syscallarg(linux_pid_t) pid;
531 1.1 tron syscallarg(int) policy;
532 1.61 njoly syscallarg(cont struct linux_sched_param *) sp;
533 1.46 dsl } */
534 1.49 elad int error, policy;
535 1.1 tron struct linux_sched_param lp;
536 1.49 elad struct sched_param sp;
537 1.1 tron
538 1.49 elad if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
539 1.49 elad error = EINVAL;
540 1.49 elad goto out;
541 1.49 elad }
542 1.1 tron
543 1.1 tron error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
544 1.1 tron if (error)
545 1.49 elad goto out;
546 1.80 christos DPRINTF("%s: linux: policy %d, priority %d\n",
547 1.80 christos __func__, SCARG(uap, policy), lp.sched_priority);
548 1.1 tron
549 1.49 elad error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
550 1.49 elad if (error)
551 1.49 elad goto out;
552 1.80 christos DPRINTF("%s: native: policy %d, priority %d\n",
553 1.80 christos __func__, policy, sp.sched_priority);
554 1.1 tron
555 1.66 njoly error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
556 1.49 elad if (error)
557 1.49 elad goto out;
558 1.1 tron
559 1.49 elad out:
560 1.49 elad return error;
561 1.1 tron }
562 1.1 tron
563 1.1 tron int
564 1.46 dsl linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval)
565 1.1 tron {
566 1.46 dsl /* {
567 1.1 tron syscallarg(linux_pid_t) pid;
568 1.46 dsl } */
569 1.49 elad int error, policy;
570 1.1 tron
571 1.1 tron *retval = -1;
572 1.1 tron
573 1.66 njoly error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
574 1.49 elad if (error)
575 1.49 elad goto out;
576 1.49 elad
577 1.49 elad error = sched_native2linux(policy, NULL, &policy, NULL);
578 1.49 elad if (error)
579 1.49 elad goto out;
580 1.49 elad
581 1.49 elad *retval = policy;
582 1.1 tron
583 1.49 elad out:
584 1.49 elad return error;
585 1.1 tron }
586 1.1 tron
587 1.1 tron int
588 1.46 dsl linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
589 1.1 tron {
590 1.11 gmcgarry
591 1.11 gmcgarry yield();
592 1.1 tron return 0;
593 1.1 tron }
594 1.1 tron
595 1.1 tron int
596 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)
597 1.1 tron {
598 1.46 dsl /* {
599 1.1 tron syscallarg(int) policy;
600 1.46 dsl } */
601 1.1 tron
602 1.55 njoly switch (SCARG(uap, policy)) {
603 1.55 njoly case LINUX_SCHED_OTHER:
604 1.55 njoly *retval = 0;
605 1.55 njoly break;
606 1.55 njoly case LINUX_SCHED_FIFO:
607 1.55 njoly case LINUX_SCHED_RR:
608 1.55 njoly *retval = LINUX_SCHED_RTPRIO_MAX;
609 1.55 njoly break;
610 1.55 njoly default:
611 1.1 tron return EINVAL;
612 1.1 tron }
613 1.1 tron
614 1.1 tron return 0;
615 1.1 tron }
616 1.1 tron
617 1.1 tron int
618 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)
619 1.1 tron {
620 1.46 dsl /* {
621 1.1 tron syscallarg(int) policy;
622 1.46 dsl } */
623 1.1 tron
624 1.55 njoly switch (SCARG(uap, policy)) {
625 1.55 njoly case LINUX_SCHED_OTHER:
626 1.55 njoly *retval = 0;
627 1.55 njoly break;
628 1.55 njoly case LINUX_SCHED_FIFO:
629 1.55 njoly case LINUX_SCHED_RR:
630 1.55 njoly *retval = LINUX_SCHED_RTPRIO_MIN;
631 1.55 njoly break;
632 1.55 njoly default:
633 1.1 tron return EINVAL;
634 1.1 tron }
635 1.1 tron
636 1.1 tron return 0;
637 1.1 tron }
638 1.14 jdolecek
639 1.63 chs int
640 1.63 chs linux_sys_exit(struct lwp *l, const struct linux_sys_exit_args *uap, register_t *retval)
641 1.63 chs {
642 1.63 chs
643 1.63 chs lwp_exit(l);
644 1.63 chs return 0;
645 1.63 chs }
646 1.63 chs
647 1.14 jdolecek #ifndef __m68k__
648 1.14 jdolecek /* Present on everything but m68k */
649 1.14 jdolecek int
650 1.46 dsl linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval)
651 1.14 jdolecek {
652 1.14 jdolecek
653 1.46 dsl return sys_exit(l, (const void *)uap, retval);
654 1.14 jdolecek }
655 1.14 jdolecek #endif /* !__m68k__ */
656 1.19 manu
657 1.19 manu int
658 1.46 dsl linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval)
659 1.19 manu {
660 1.46 dsl /* {
661 1.19 manu syscallarg(int *) tidptr;
662 1.46 dsl } */
663 1.19 manu struct linux_emuldata *led;
664 1.19 manu
665 1.63 chs led = (struct linux_emuldata *)l->l_emuldata;
666 1.63 chs led->led_clear_tid = SCARG(uap, tid);
667 1.63 chs *retval = l->l_lid;
668 1.19 manu
669 1.19 manu return 0;
670 1.19 manu }
671 1.20 manu
672 1.20 manu /* ARGUSED1 */
673 1.20 manu int
674 1.46 dsl linux_sys_gettid(struct lwp *l, const void *v, register_t *retval)
675 1.20 manu {
676 1.31 manu
677 1.63 chs *retval = l->l_lid;
678 1.31 manu return 0;
679 1.31 manu }
680 1.31 manu
681 1.68 christos /*
682 1.68 christos * The affinity syscalls assume that the layout of our cpu kcpuset is
683 1.68 christos * the same as linux's: a linear bitmask.
684 1.68 christos */
685 1.22 manu int
686 1.46 dsl linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval)
687 1.22 manu {
688 1.46 dsl /* {
689 1.63 chs syscallarg(linux_pid_t) pid;
690 1.22 manu syscallarg(unsigned int) len;
691 1.22 manu syscallarg(unsigned long *) mask;
692 1.46 dsl } */
693 1.76 thorpej struct proc *p;
694 1.68 christos struct lwp *t;
695 1.68 christos kcpuset_t *kcset;
696 1.68 christos size_t size;
697 1.68 christos cpuid_t i;
698 1.68 christos int error;
699 1.22 manu
700 1.68 christos size = LINUX_CPU_MASK_SIZE;
701 1.60 njoly if (SCARG(uap, len) < size)
702 1.22 manu return EINVAL;
703 1.22 manu
704 1.76 thorpej if (SCARG(uap, pid) == 0) {
705 1.76 thorpej p = curproc;
706 1.76 thorpej mutex_enter(p->p_lock);
707 1.76 thorpej t = curlwp;
708 1.76 thorpej } else {
709 1.76 thorpej t = lwp_find2(-1, SCARG(uap, pid));
710 1.76 thorpej if (__predict_false(t == NULL)) {
711 1.76 thorpej return ESRCH;
712 1.76 thorpej }
713 1.76 thorpej p = t->l_proc;
714 1.76 thorpej KASSERT(mutex_owned(p->p_lock));
715 1.76 thorpej }
716 1.22 manu
717 1.68 christos /* Check the permission */
718 1.68 christos if (kauth_authorize_process(l->l_cred,
719 1.76 thorpej KAUTH_PROCESS_SCHEDULER_GETAFFINITY, p, NULL, NULL, NULL)) {
720 1.76 thorpej mutex_exit(p->p_lock);
721 1.68 christos return EPERM;
722 1.68 christos }
723 1.68 christos
724 1.68 christos kcpuset_create(&kcset, true);
725 1.68 christos lwp_lock(t);
726 1.68 christos if (t->l_affinity != NULL)
727 1.68 christos kcpuset_copy(kcset, t->l_affinity);
728 1.68 christos else {
729 1.68 christos /*
730 1.68 christos * All available CPUs should be masked when affinity has not
731 1.68 christos * been set.
732 1.68 christos */
733 1.68 christos kcpuset_zero(kcset);
734 1.68 christos for (i = 0; i < ncpu; i++)
735 1.68 christos kcpuset_set(kcset, i);
736 1.68 christos }
737 1.68 christos lwp_unlock(t);
738 1.76 thorpej mutex_exit(p->p_lock);
739 1.68 christos error = kcpuset_copyout(kcset, (cpuset_t *)SCARG(uap, mask), size);
740 1.68 christos kcpuset_unuse(kcset, NULL);
741 1.60 njoly *retval = size;
742 1.59 njoly return error;
743 1.22 manu }
744 1.22 manu
745 1.22 manu int
746 1.46 dsl linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval)
747 1.22 manu {
748 1.46 dsl /* {
749 1.63 chs syscallarg(linux_pid_t) pid;
750 1.22 manu syscallarg(unsigned int) len;
751 1.22 manu syscallarg(unsigned long *) mask;
752 1.46 dsl } */
753 1.68 christos struct sys__sched_setaffinity_args ssa;
754 1.68 christos size_t size;
755 1.76 thorpej pid_t pid;
756 1.77 rin lwpid_t lid;
757 1.68 christos
758 1.68 christos size = LINUX_CPU_MASK_SIZE;
759 1.68 christos if (SCARG(uap, len) < size)
760 1.68 christos return EINVAL;
761 1.22 manu
762 1.77 rin lid = SCARG(uap, pid);
763 1.77 rin if (lid != 0) {
764 1.76 thorpej /* Get the canonical PID for the process. */
765 1.78 ad mutex_enter(&proc_lock);
766 1.76 thorpej struct proc *p = proc_find_lwpid(SCARG(uap, pid));
767 1.76 thorpej if (p == NULL) {
768 1.78 ad mutex_exit(&proc_lock);
769 1.76 thorpej return ESRCH;
770 1.76 thorpej }
771 1.76 thorpej pid = p->p_pid;
772 1.78 ad mutex_exit(&proc_lock);
773 1.76 thorpej } else {
774 1.77 rin pid = curproc->p_pid;
775 1.77 rin lid = curlwp->l_lid;
776 1.76 thorpej }
777 1.76 thorpej
778 1.76 thorpej SCARG(&ssa, pid) = pid;
779 1.77 rin SCARG(&ssa, lid) = lid;
780 1.68 christos SCARG(&ssa, size) = size;
781 1.68 christos SCARG(&ssa, cpuset) = (cpuset_t *)SCARG(uap, mask);
782 1.22 manu
783 1.68 christos return sys__sched_setaffinity(l, &ssa, retval);
784 1.64 dsl }
785