linux_sched.c revision 1.67 1 1.67 maxv /* $NetBSD: linux_sched.c,v 1.67 2014/11/09 17:48:08 maxv Exp $ */
2 1.1 tron
3 1.1 tron /*-
4 1.1 tron * Copyright (c) 1999 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.67 maxv __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.67 2014/11/09 17:48:08 maxv 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.65 christos #if DEBUG_LINUX
69 1.65 christos #define DPRINTF(x) uprintf x
70 1.65 christos #else
71 1.65 christos #define DPRINTF(x)
72 1.65 christos #endif
73 1.63 chs
74 1.63 chs static void
75 1.63 chs linux_child_return(void *arg)
76 1.63 chs {
77 1.63 chs struct lwp *l = arg;
78 1.63 chs struct proc *p = l->l_proc;
79 1.63 chs struct linux_emuldata *led = l->l_emuldata;
80 1.63 chs void *ctp = led->led_child_tidptr;
81 1.65 christos int error;
82 1.63 chs
83 1.63 chs if (ctp) {
84 1.65 christos if ((error = copyout(&p->p_pid, ctp, sizeof(p->p_pid))) != 0)
85 1.63 chs printf("%s: LINUX_CLONE_CHILD_SETTID "
86 1.65 christos "failed (child_tidptr = %p, tid = %d error =%d)\n",
87 1.65 christos __func__, ctp, p->p_pid, error);
88 1.63 chs }
89 1.63 chs child_return(arg);
90 1.63 chs }
91 1.63 chs
92 1.1 tron int
93 1.65 christos linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap,
94 1.65 christos register_t *retval)
95 1.1 tron {
96 1.46 dsl /* {
97 1.1 tron syscallarg(int) flags;
98 1.1 tron syscallarg(void *) stack;
99 1.19 manu syscallarg(void *) parent_tidptr;
100 1.63 chs syscallarg(void *) tls;
101 1.19 manu syscallarg(void *) child_tidptr;
102 1.46 dsl } */
103 1.58 christos struct proc *p;
104 1.19 manu struct linux_emuldata *led;
105 1.63 chs int flags, sig, error;
106 1.1 tron
107 1.1 tron /*
108 1.1 tron * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
109 1.1 tron */
110 1.1 tron if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
111 1.65 christos return EINVAL;
112 1.1 tron
113 1.13 jdolecek /*
114 1.13 jdolecek * Thread group implies shared signals. Shared signals
115 1.13 jdolecek * imply shared VM. This matches what Linux kernel does.
116 1.13 jdolecek */
117 1.13 jdolecek if (SCARG(uap, flags) & LINUX_CLONE_THREAD
118 1.13 jdolecek && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
119 1.65 christos return EINVAL;
120 1.13 jdolecek if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
121 1.13 jdolecek && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
122 1.65 christos return EINVAL;
123 1.13 jdolecek
124 1.63 chs /*
125 1.63 chs * The thread group flavor is implemented totally differently.
126 1.63 chs */
127 1.65 christos if (SCARG(uap, flags) & LINUX_CLONE_THREAD)
128 1.63 chs return linux_clone_nptl(l, uap, retval);
129 1.63 chs
130 1.1 tron flags = 0;
131 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_VM)
132 1.1 tron flags |= FORK_SHAREVM;
133 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_FS)
134 1.1 tron flags |= FORK_SHARECWD;
135 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_FILES)
136 1.1 tron flags |= FORK_SHAREFILES;
137 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
138 1.1 tron flags |= FORK_SHARESIGS;
139 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
140 1.1 tron flags |= FORK_PPWAIT;
141 1.1 tron
142 1.34 manu sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
143 1.34 manu if (sig < 0 || sig >= LINUX__NSIG)
144 1.65 christos return EINVAL;
145 1.34 manu sig = linux_to_native_signo[sig];
146 1.1 tron
147 1.63 chs if (SCARG(uap, flags) & LINUX_CLONE_CHILD_SETTID) {
148 1.63 chs led = l->l_emuldata;
149 1.63 chs led->led_child_tidptr = SCARG(uap, child_tidptr);
150 1.63 chs }
151 1.19 manu
152 1.1 tron /*
153 1.1 tron * Note that Linux does not provide a portable way of specifying
154 1.1 tron * the stack area; the caller must know if the stack grows up
155 1.1 tron * or down. So, we pass a stack size of 0, so that the code
156 1.1 tron * that makes this adjustment is a noop.
157 1.1 tron */
158 1.19 manu if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
159 1.65 christos linux_child_return, NULL, retval, &p)) != 0) {
160 1.65 christos DPRINTF(("%s: fork1: error %d\n", __func__, error));
161 1.63 chs return error;
162 1.65 christos }
163 1.63 chs
164 1.63 chs return 0;
165 1.63 chs }
166 1.63 chs
167 1.63 chs static int
168 1.63 chs linux_clone_nptl(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
169 1.63 chs {
170 1.63 chs /* {
171 1.63 chs syscallarg(int) flags;
172 1.63 chs syscallarg(void *) stack;
173 1.63 chs syscallarg(void *) parent_tidptr;
174 1.63 chs syscallarg(void *) tls;
175 1.63 chs syscallarg(void *) child_tidptr;
176 1.63 chs } */
177 1.63 chs struct proc *p;
178 1.63 chs struct lwp *l2;
179 1.63 chs struct linux_emuldata *led;
180 1.63 chs void *parent_tidptr, *tls, *child_tidptr;
181 1.63 chs struct schedstate_percpu *spc;
182 1.63 chs vaddr_t uaddr;
183 1.63 chs lwpid_t lid;
184 1.63 chs int flags, tnprocs, error;
185 1.63 chs
186 1.63 chs p = l->l_proc;
187 1.63 chs flags = SCARG(uap, flags);
188 1.63 chs parent_tidptr = SCARG(uap, parent_tidptr);
189 1.63 chs tls = SCARG(uap, tls);
190 1.63 chs child_tidptr = SCARG(uap, child_tidptr);
191 1.63 chs
192 1.63 chs tnprocs = atomic_inc_uint_nv(&nprocs);
193 1.63 chs if (__predict_false(tnprocs >= maxproc) ||
194 1.63 chs kauth_authorize_process(l->l_cred, KAUTH_PROCESS_FORK, p,
195 1.65 christos KAUTH_ARG(tnprocs), NULL, NULL) != 0) {
196 1.63 chs atomic_dec_uint(&nprocs);
197 1.63 chs return EAGAIN;
198 1.63 chs }
199 1.63 chs
200 1.63 chs uaddr = uvm_uarea_alloc();
201 1.63 chs if (__predict_false(uaddr == 0)) {
202 1.63 chs atomic_dec_uint(&nprocs);
203 1.63 chs return ENOMEM;
204 1.63 chs }
205 1.63 chs
206 1.63 chs error = lwp_create(l, p, uaddr, LWP_DETACHED | LWP_PIDLID,
207 1.65 christos SCARG(uap, stack), 0, child_return, NULL, &l2, l->l_class);
208 1.63 chs if (__predict_false(error)) {
209 1.65 christos DPRINTF(("%s: lwp_create error=%d\n", __func__, error));
210 1.63 chs atomic_dec_uint(&nprocs);
211 1.63 chs uvm_uarea_free(uaddr);
212 1.19 manu return error;
213 1.63 chs }
214 1.63 chs lid = l2->l_lid;
215 1.19 manu
216 1.63 chs /* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */
217 1.63 chs if (flags & LINUX_CLONE_CHILD_CLEARTID) {
218 1.63 chs led = l2->l_emuldata;
219 1.63 chs led->led_clear_tid = child_tidptr;
220 1.63 chs }
221 1.63 chs
222 1.63 chs /* LINUX_CLONE_PARENT_SETTID: store child's TID in parent's memory */
223 1.63 chs if (flags & LINUX_CLONE_PARENT_SETTID) {
224 1.65 christos if ((error = copyout(&lid, parent_tidptr, sizeof(lid))) != 0)
225 1.63 chs printf("%s: LINUX_CLONE_PARENT_SETTID "
226 1.65 christos "failed (parent_tidptr = %p tid = %d error=%d)\n",
227 1.65 christos __func__, parent_tidptr, lid, error);
228 1.63 chs }
229 1.63 chs
230 1.63 chs /* LINUX_CLONE_CHILD_SETTID: store child's TID in child's memory */
231 1.63 chs if (flags & LINUX_CLONE_CHILD_SETTID) {
232 1.65 christos if ((error = copyout(&lid, child_tidptr, sizeof(lid))) != 0)
233 1.63 chs printf("%s: LINUX_CLONE_CHILD_SETTID "
234 1.65 christos "failed (child_tidptr = %p, tid = %d error=%d)\n",
235 1.65 christos __func__, child_tidptr, lid, error);
236 1.63 chs }
237 1.63 chs
238 1.63 chs if (flags & LINUX_CLONE_SETTLS) {
239 1.63 chs error = LINUX_LWP_SETPRIVATE(l2, tls);
240 1.63 chs if (error) {
241 1.65 christos DPRINTF(("%s: LINUX_LWP_SETPRIVATE %d\n", __func__,
242 1.65 christos error));
243 1.63 chs lwp_exit(l2);
244 1.63 chs return error;
245 1.63 chs }
246 1.63 chs }
247 1.63 chs
248 1.63 chs /*
249 1.63 chs * Set the new LWP running, unless the process is stopping,
250 1.63 chs * then the LWP is created stopped.
251 1.63 chs */
252 1.63 chs mutex_enter(p->p_lock);
253 1.63 chs lwp_lock(l2);
254 1.63 chs spc = &l2->l_cpu->ci_schedstate;
255 1.63 chs if ((l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
256 1.63 chs if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
257 1.63 chs KASSERT(l2->l_wchan == NULL);
258 1.63 chs l2->l_stat = LSSTOP;
259 1.63 chs p->p_nrlwps--;
260 1.63 chs lwp_unlock_to(l2, spc->spc_lwplock);
261 1.63 chs } else {
262 1.63 chs KASSERT(lwp_locked(l2, spc->spc_mutex));
263 1.63 chs l2->l_stat = LSRUN;
264 1.63 chs sched_enqueue(l2, false);
265 1.63 chs lwp_unlock(l2);
266 1.63 chs }
267 1.63 chs } else {
268 1.63 chs l2->l_stat = LSSUSPENDED;
269 1.63 chs p->p_nrlwps--;
270 1.63 chs lwp_unlock_to(l2, spc->spc_lwplock);
271 1.63 chs }
272 1.63 chs mutex_exit(p->p_lock);
273 1.58 christos
274 1.63 chs retval[0] = lid;
275 1.63 chs retval[1] = 0;
276 1.19 manu return 0;
277 1.1 tron }
278 1.1 tron
279 1.49 elad /*
280 1.49 elad * linux realtime priority
281 1.49 elad *
282 1.49 elad * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
283 1.49 elad *
284 1.49 elad * - SCHED_OTHER tasks don't have realtime priorities.
285 1.49 elad * in particular, sched_param::sched_priority is always 0.
286 1.49 elad */
287 1.49 elad
288 1.49 elad #define LINUX_SCHED_RTPRIO_MIN 1
289 1.49 elad #define LINUX_SCHED_RTPRIO_MAX 99
290 1.49 elad
291 1.49 elad static int
292 1.49 elad sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
293 1.49 elad int *native_policy, struct sched_param *native_params)
294 1.49 elad {
295 1.49 elad
296 1.49 elad switch (linux_policy) {
297 1.49 elad case LINUX_SCHED_OTHER:
298 1.49 elad if (native_policy != NULL) {
299 1.49 elad *native_policy = SCHED_OTHER;
300 1.49 elad }
301 1.49 elad break;
302 1.49 elad
303 1.49 elad case LINUX_SCHED_FIFO:
304 1.49 elad if (native_policy != NULL) {
305 1.49 elad *native_policy = SCHED_FIFO;
306 1.49 elad }
307 1.49 elad break;
308 1.49 elad
309 1.49 elad case LINUX_SCHED_RR:
310 1.49 elad if (native_policy != NULL) {
311 1.49 elad *native_policy = SCHED_RR;
312 1.49 elad }
313 1.49 elad break;
314 1.49 elad
315 1.49 elad default:
316 1.49 elad return EINVAL;
317 1.49 elad }
318 1.49 elad
319 1.49 elad if (linux_params != NULL) {
320 1.49 elad int prio = linux_params->sched_priority;
321 1.49 elad
322 1.49 elad KASSERT(native_params != NULL);
323 1.49 elad
324 1.49 elad if (linux_policy == LINUX_SCHED_OTHER) {
325 1.49 elad if (prio != 0) {
326 1.49 elad return EINVAL;
327 1.49 elad }
328 1.49 elad native_params->sched_priority = PRI_NONE; /* XXX */
329 1.49 elad } else {
330 1.49 elad if (prio < LINUX_SCHED_RTPRIO_MIN ||
331 1.49 elad prio > LINUX_SCHED_RTPRIO_MAX) {
332 1.49 elad return EINVAL;
333 1.49 elad }
334 1.49 elad native_params->sched_priority =
335 1.49 elad (prio - LINUX_SCHED_RTPRIO_MIN)
336 1.49 elad * (SCHED_PRI_MAX - SCHED_PRI_MIN)
337 1.49 elad / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
338 1.49 elad + SCHED_PRI_MIN;
339 1.49 elad }
340 1.49 elad }
341 1.49 elad
342 1.49 elad return 0;
343 1.49 elad }
344 1.49 elad
345 1.49 elad static int
346 1.49 elad sched_native2linux(int native_policy, struct sched_param *native_params,
347 1.49 elad int *linux_policy, struct linux_sched_param *linux_params)
348 1.49 elad {
349 1.49 elad
350 1.49 elad switch (native_policy) {
351 1.49 elad case SCHED_OTHER:
352 1.49 elad if (linux_policy != NULL) {
353 1.49 elad *linux_policy = LINUX_SCHED_OTHER;
354 1.49 elad }
355 1.49 elad break;
356 1.49 elad
357 1.49 elad case SCHED_FIFO:
358 1.49 elad if (linux_policy != NULL) {
359 1.49 elad *linux_policy = LINUX_SCHED_FIFO;
360 1.49 elad }
361 1.49 elad break;
362 1.49 elad
363 1.49 elad case SCHED_RR:
364 1.49 elad if (linux_policy != NULL) {
365 1.49 elad *linux_policy = LINUX_SCHED_RR;
366 1.49 elad }
367 1.49 elad break;
368 1.49 elad
369 1.49 elad default:
370 1.49 elad panic("%s: unknown policy %d\n", __func__, native_policy);
371 1.49 elad }
372 1.49 elad
373 1.49 elad if (native_params != NULL) {
374 1.49 elad int prio = native_params->sched_priority;
375 1.49 elad
376 1.49 elad KASSERT(prio >= SCHED_PRI_MIN);
377 1.49 elad KASSERT(prio <= SCHED_PRI_MAX);
378 1.49 elad KASSERT(linux_params != NULL);
379 1.56 jmcneill
380 1.65 christos DPRINTF(("%s: native: policy %d, priority %d\n",
381 1.65 christos __func__, native_policy, prio));
382 1.49 elad
383 1.49 elad if (native_policy == SCHED_OTHER) {
384 1.49 elad linux_params->sched_priority = 0;
385 1.49 elad } else {
386 1.49 elad linux_params->sched_priority =
387 1.49 elad (prio - SCHED_PRI_MIN)
388 1.49 elad * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
389 1.49 elad / (SCHED_PRI_MAX - SCHED_PRI_MIN)
390 1.49 elad + LINUX_SCHED_RTPRIO_MIN;
391 1.49 elad }
392 1.65 christos DPRINTF(("%s: linux: policy %d, priority %d\n",
393 1.65 christos __func__, -1, linux_params->sched_priority));
394 1.49 elad }
395 1.49 elad
396 1.49 elad return 0;
397 1.49 elad }
398 1.49 elad
399 1.1 tron int
400 1.46 dsl linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
401 1.1 tron {
402 1.46 dsl /* {
403 1.1 tron syscallarg(linux_pid_t) pid;
404 1.1 tron syscallarg(const struct linux_sched_param *) sp;
405 1.46 dsl } */
406 1.49 elad int error, policy;
407 1.1 tron struct linux_sched_param lp;
408 1.49 elad struct sched_param sp;
409 1.49 elad
410 1.49 elad if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
411 1.49 elad error = EINVAL;
412 1.49 elad goto out;
413 1.49 elad }
414 1.1 tron
415 1.49 elad error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
416 1.49 elad if (error)
417 1.49 elad goto out;
418 1.1 tron
419 1.49 elad /* We need the current policy in Linux terms. */
420 1.66 njoly error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
421 1.49 elad if (error)
422 1.49 elad goto out;
423 1.49 elad error = sched_native2linux(policy, NULL, &policy, NULL);
424 1.49 elad if (error)
425 1.49 elad goto out;
426 1.1 tron
427 1.49 elad error = sched_linux2native(policy, &lp, &policy, &sp);
428 1.1 tron if (error)
429 1.49 elad goto out;
430 1.1 tron
431 1.66 njoly error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
432 1.49 elad if (error)
433 1.49 elad goto out;
434 1.1 tron
435 1.49 elad out:
436 1.49 elad return error;
437 1.1 tron }
438 1.1 tron
439 1.1 tron int
440 1.46 dsl linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
441 1.1 tron {
442 1.46 dsl /* {
443 1.1 tron syscallarg(linux_pid_t) pid;
444 1.1 tron syscallarg(struct linux_sched_param *) sp;
445 1.46 dsl } */
446 1.1 tron struct linux_sched_param lp;
447 1.49 elad struct sched_param sp;
448 1.50 elad int error, policy;
449 1.49 elad
450 1.49 elad if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
451 1.49 elad error = EINVAL;
452 1.49 elad goto out;
453 1.49 elad }
454 1.1 tron
455 1.66 njoly error = do_sched_getparam(SCARG(uap, pid), 0, &policy, &sp);
456 1.49 elad if (error)
457 1.49 elad goto out;
458 1.65 christos DPRINTF(("%s: native: policy %d, priority %d\n",
459 1.65 christos __func__, policy, sp.sched_priority));
460 1.1 tron
461 1.50 elad error = sched_native2linux(policy, &sp, NULL, &lp);
462 1.49 elad if (error)
463 1.49 elad goto out;
464 1.65 christos DPRINTF(("%s: linux: policy %d, priority %d\n",
465 1.65 christos __func__, policy, lp.sched_priority));
466 1.47 elad
467 1.49 elad error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
468 1.49 elad if (error)
469 1.49 elad goto out;
470 1.1 tron
471 1.49 elad out:
472 1.49 elad return error;
473 1.1 tron }
474 1.1 tron
475 1.1 tron int
476 1.46 dsl linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
477 1.1 tron {
478 1.46 dsl /* {
479 1.1 tron syscallarg(linux_pid_t) pid;
480 1.1 tron syscallarg(int) policy;
481 1.61 njoly syscallarg(cont struct linux_sched_param *) sp;
482 1.46 dsl } */
483 1.49 elad int error, policy;
484 1.1 tron struct linux_sched_param lp;
485 1.49 elad struct sched_param sp;
486 1.1 tron
487 1.49 elad if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
488 1.49 elad error = EINVAL;
489 1.49 elad goto out;
490 1.49 elad }
491 1.1 tron
492 1.1 tron error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
493 1.1 tron if (error)
494 1.49 elad goto out;
495 1.65 christos DPRINTF(("%s: linux: policy %d, priority %d\n",
496 1.65 christos __func__, SCARG(uap, policy), lp.sched_priority));
497 1.1 tron
498 1.49 elad error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
499 1.49 elad if (error)
500 1.49 elad goto out;
501 1.65 christos DPRINTF(("%s: native: policy %d, priority %d\n",
502 1.65 christos __func__, policy, sp.sched_priority));
503 1.1 tron
504 1.66 njoly error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
505 1.49 elad if (error)
506 1.49 elad goto out;
507 1.1 tron
508 1.49 elad out:
509 1.49 elad return error;
510 1.1 tron }
511 1.1 tron
512 1.1 tron int
513 1.46 dsl linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval)
514 1.1 tron {
515 1.46 dsl /* {
516 1.1 tron syscallarg(linux_pid_t) pid;
517 1.46 dsl } */
518 1.49 elad int error, policy;
519 1.1 tron
520 1.1 tron *retval = -1;
521 1.1 tron
522 1.66 njoly error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
523 1.49 elad if (error)
524 1.49 elad goto out;
525 1.49 elad
526 1.49 elad error = sched_native2linux(policy, NULL, &policy, NULL);
527 1.49 elad if (error)
528 1.49 elad goto out;
529 1.49 elad
530 1.49 elad *retval = policy;
531 1.1 tron
532 1.49 elad out:
533 1.49 elad return error;
534 1.1 tron }
535 1.1 tron
536 1.1 tron int
537 1.46 dsl linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
538 1.1 tron {
539 1.11 gmcgarry
540 1.11 gmcgarry yield();
541 1.1 tron return 0;
542 1.1 tron }
543 1.1 tron
544 1.1 tron int
545 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)
546 1.1 tron {
547 1.46 dsl /* {
548 1.1 tron syscallarg(int) policy;
549 1.46 dsl } */
550 1.1 tron
551 1.55 njoly switch (SCARG(uap, policy)) {
552 1.55 njoly case LINUX_SCHED_OTHER:
553 1.55 njoly *retval = 0;
554 1.55 njoly break;
555 1.55 njoly case LINUX_SCHED_FIFO:
556 1.55 njoly case LINUX_SCHED_RR:
557 1.55 njoly *retval = LINUX_SCHED_RTPRIO_MAX;
558 1.55 njoly break;
559 1.55 njoly default:
560 1.1 tron return EINVAL;
561 1.1 tron }
562 1.1 tron
563 1.1 tron return 0;
564 1.1 tron }
565 1.1 tron
566 1.1 tron int
567 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)
568 1.1 tron {
569 1.46 dsl /* {
570 1.1 tron syscallarg(int) policy;
571 1.46 dsl } */
572 1.1 tron
573 1.55 njoly switch (SCARG(uap, policy)) {
574 1.55 njoly case LINUX_SCHED_OTHER:
575 1.55 njoly *retval = 0;
576 1.55 njoly break;
577 1.55 njoly case LINUX_SCHED_FIFO:
578 1.55 njoly case LINUX_SCHED_RR:
579 1.55 njoly *retval = LINUX_SCHED_RTPRIO_MIN;
580 1.55 njoly break;
581 1.55 njoly default:
582 1.1 tron return EINVAL;
583 1.1 tron }
584 1.1 tron
585 1.1 tron return 0;
586 1.1 tron }
587 1.14 jdolecek
588 1.63 chs int
589 1.63 chs linux_sys_exit(struct lwp *l, const struct linux_sys_exit_args *uap, register_t *retval)
590 1.63 chs {
591 1.63 chs
592 1.63 chs lwp_exit(l);
593 1.63 chs return 0;
594 1.63 chs }
595 1.63 chs
596 1.14 jdolecek #ifndef __m68k__
597 1.14 jdolecek /* Present on everything but m68k */
598 1.14 jdolecek int
599 1.46 dsl linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval)
600 1.14 jdolecek {
601 1.14 jdolecek
602 1.46 dsl return sys_exit(l, (const void *)uap, retval);
603 1.14 jdolecek }
604 1.14 jdolecek #endif /* !__m68k__ */
605 1.19 manu
606 1.19 manu int
607 1.46 dsl linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval)
608 1.19 manu {
609 1.46 dsl /* {
610 1.19 manu syscallarg(int *) tidptr;
611 1.46 dsl } */
612 1.19 manu struct linux_emuldata *led;
613 1.19 manu
614 1.63 chs led = (struct linux_emuldata *)l->l_emuldata;
615 1.63 chs led->led_clear_tid = SCARG(uap, tid);
616 1.63 chs *retval = l->l_lid;
617 1.19 manu
618 1.19 manu return 0;
619 1.19 manu }
620 1.20 manu
621 1.20 manu /* ARGUSED1 */
622 1.20 manu int
623 1.46 dsl linux_sys_gettid(struct lwp *l, const void *v, register_t *retval)
624 1.20 manu {
625 1.31 manu
626 1.63 chs *retval = l->l_lid;
627 1.31 manu return 0;
628 1.31 manu }
629 1.31 manu
630 1.22 manu int
631 1.46 dsl linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval)
632 1.22 manu {
633 1.46 dsl /* {
634 1.63 chs syscallarg(linux_pid_t) pid;
635 1.22 manu syscallarg(unsigned int) len;
636 1.22 manu syscallarg(unsigned long *) mask;
637 1.46 dsl } */
638 1.63 chs proc_t *p;
639 1.63 chs unsigned long *lp, *data;
640 1.60 njoly int error, size, nb = ncpu;
641 1.22 manu
642 1.60 njoly /* Unlike Linux, dynamically calculate cpu mask size */
643 1.60 njoly size = sizeof(long) * ((ncpu + LONG_BIT - 1) / LONG_BIT);
644 1.60 njoly if (SCARG(uap, len) < size)
645 1.22 manu return EINVAL;
646 1.22 manu
647 1.62 rmind /* XXX: Pointless check. TODO: Actually implement this. */
648 1.62 rmind mutex_enter(proc_lock);
649 1.62 rmind p = proc_find(SCARG(uap, pid));
650 1.62 rmind mutex_exit(proc_lock);
651 1.62 rmind if (p == NULL) {
652 1.22 manu return ESRCH;
653 1.62 rmind }
654 1.22 manu
655 1.22 manu /*
656 1.22 manu * return the actual number of CPU, tag all of them as available
657 1.22 manu * The result is a mask, the first CPU being in the least significant
658 1.22 manu * bit.
659 1.22 manu */
660 1.62 rmind data = kmem_zalloc(size, KM_SLEEP);
661 1.63 chs lp = data;
662 1.60 njoly while (nb > LONG_BIT) {
663 1.63 chs *lp++ = ~0UL;
664 1.60 njoly nb -= LONG_BIT;
665 1.60 njoly }
666 1.60 njoly if (nb)
667 1.63 chs *lp = (1 << ncpu) - 1;
668 1.22 manu
669 1.60 njoly error = copyout(data, SCARG(uap, mask), size);
670 1.62 rmind kmem_free(data, size);
671 1.60 njoly *retval = size;
672 1.59 njoly return error;
673 1.22 manu }
674 1.22 manu
675 1.22 manu int
676 1.46 dsl linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval)
677 1.22 manu {
678 1.46 dsl /* {
679 1.63 chs syscallarg(linux_pid_t) pid;
680 1.22 manu syscallarg(unsigned int) len;
681 1.22 manu syscallarg(unsigned long *) mask;
682 1.46 dsl } */
683 1.62 rmind proc_t *p;
684 1.22 manu
685 1.62 rmind /* XXX: Pointless check. TODO: Actually implement this. */
686 1.62 rmind mutex_enter(proc_lock);
687 1.62 rmind p = proc_find(SCARG(uap, pid));
688 1.62 rmind mutex_exit(proc_lock);
689 1.62 rmind if (p == NULL) {
690 1.22 manu return ESRCH;
691 1.62 rmind }
692 1.22 manu
693 1.22 manu /* Let's ignore it */
694 1.65 christos DPRINTF(("%s\n", __func__));
695 1.22 manu return 0;
696 1.64 dsl }
697