linux_sched.c revision 1.41 1 1.41 ad /* $NetBSD: linux_sched.c,v 1.41 2007/02/09 23:51:20 ad 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 * 3. All advertising materials mentioning features or use of this software
20 1.1 tron * must display the following acknowledgement:
21 1.1 tron * This product includes software developed by the NetBSD
22 1.1 tron * Foundation, Inc. and its contributors.
23 1.1 tron * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 tron * contributors may be used to endorse or promote products derived
25 1.1 tron * from this software without specific prior written permission.
26 1.1 tron *
27 1.1 tron * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 tron * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 tron * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 tron * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 tron * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 tron * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 tron * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 tron * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 tron * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 tron * POSSIBILITY OF SUCH DAMAGE.
38 1.1 tron */
39 1.1 tron
40 1.1 tron /*
41 1.1 tron * Linux compatibility module. Try to deal with scheduler related syscalls.
42 1.1 tron */
43 1.8 lukem
44 1.8 lukem #include <sys/cdefs.h>
45 1.41 ad __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.41 2007/02/09 23:51:20 ad Exp $");
46 1.1 tron
47 1.1 tron #include <sys/param.h>
48 1.1 tron #include <sys/mount.h>
49 1.1 tron #include <sys/proc.h>
50 1.1 tron #include <sys/systm.h>
51 1.22 manu #include <sys/sysctl.h>
52 1.22 manu #include <sys/malloc.h>
53 1.1 tron #include <sys/syscallargs.h>
54 1.14 jdolecek #include <sys/wait.h>
55 1.30 elad #include <sys/kauth.h>
56 1.34 manu #include <sys/ptrace.h>
57 1.3 itohy
58 1.3 itohy #include <machine/cpu.h>
59 1.1 tron
60 1.1 tron #include <compat/linux/common/linux_types.h>
61 1.1 tron #include <compat/linux/common/linux_signal.h>
62 1.21 manu #include <compat/linux/common/linux_machdep.h> /* For LINUX_NPTL */
63 1.19 manu #include <compat/linux/common/linux_emuldata.h>
64 1.1 tron
65 1.1 tron #include <compat/linux/linux_syscallargs.h>
66 1.1 tron
67 1.1 tron #include <compat/linux/common/linux_sched.h>
68 1.1 tron
69 1.1 tron int
70 1.12 thorpej linux_sys_clone(l, v, retval)
71 1.12 thorpej struct lwp *l;
72 1.1 tron void *v;
73 1.1 tron register_t *retval;
74 1.1 tron {
75 1.1 tron struct linux_sys_clone_args /* {
76 1.1 tron syscallarg(int) flags;
77 1.1 tron syscallarg(void *) stack;
78 1.21 manu #ifdef LINUX_NPTL
79 1.19 manu syscallarg(void *) parent_tidptr;
80 1.19 manu syscallarg(void *) child_tidptr;
81 1.19 manu #endif
82 1.1 tron } */ *uap = v;
83 1.1 tron int flags, sig;
84 1.19 manu int error;
85 1.21 manu #ifdef LINUX_NPTL
86 1.19 manu struct linux_emuldata *led;
87 1.19 manu #endif
88 1.1 tron
89 1.1 tron /*
90 1.1 tron * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
91 1.1 tron */
92 1.1 tron if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
93 1.1 tron return (EINVAL);
94 1.1 tron
95 1.13 jdolecek /*
96 1.13 jdolecek * Thread group implies shared signals. Shared signals
97 1.13 jdolecek * imply shared VM. This matches what Linux kernel does.
98 1.13 jdolecek */
99 1.13 jdolecek if (SCARG(uap, flags) & LINUX_CLONE_THREAD
100 1.13 jdolecek && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
101 1.13 jdolecek return (EINVAL);
102 1.13 jdolecek if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
103 1.13 jdolecek && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
104 1.13 jdolecek return (EINVAL);
105 1.13 jdolecek
106 1.1 tron flags = 0;
107 1.1 tron
108 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_VM)
109 1.1 tron flags |= FORK_SHAREVM;
110 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_FS)
111 1.1 tron flags |= FORK_SHARECWD;
112 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_FILES)
113 1.1 tron flags |= FORK_SHAREFILES;
114 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
115 1.1 tron flags |= FORK_SHARESIGS;
116 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
117 1.1 tron flags |= FORK_PPWAIT;
118 1.1 tron
119 1.34 manu sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
120 1.34 manu if (sig < 0 || sig >= LINUX__NSIG)
121 1.34 manu return (EINVAL);
122 1.34 manu sig = linux_to_native_signo[sig];
123 1.1 tron
124 1.21 manu #ifdef LINUX_NPTL
125 1.19 manu led = (struct linux_emuldata *)l->l_proc->p_emuldata;
126 1.19 manu
127 1.34 manu led->parent_tidptr = SCARG(uap, parent_tidptr);
128 1.34 manu led->child_tidptr = SCARG(uap, child_tidptr);
129 1.34 manu led->clone_flags = SCARG(uap, flags);
130 1.34 manu #endif /* LINUX_NPTL */
131 1.19 manu
132 1.1 tron /*
133 1.1 tron * Note that Linux does not provide a portable way of specifying
134 1.1 tron * the stack area; the caller must know if the stack grows up
135 1.1 tron * or down. So, we pass a stack size of 0, so that the code
136 1.1 tron * that makes this adjustment is a noop.
137 1.1 tron */
138 1.19 manu if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
139 1.19 manu NULL, NULL, retval, NULL)) != 0)
140 1.19 manu return error;
141 1.19 manu
142 1.19 manu return 0;
143 1.1 tron }
144 1.1 tron
145 1.1 tron int
146 1.37 christos linux_sys_sched_setparam(struct lwp *cl, void *v, register_t *retval)
147 1.1 tron {
148 1.1 tron struct linux_sys_sched_setparam_args /* {
149 1.1 tron syscallarg(linux_pid_t) pid;
150 1.1 tron syscallarg(const struct linux_sched_param *) sp;
151 1.1 tron } */ *uap = v;
152 1.1 tron int error;
153 1.1 tron struct linux_sched_param lp;
154 1.5 augustss struct proc *p;
155 1.1 tron
156 1.1 tron /*
157 1.1 tron * We only check for valid parameters and return afterwards.
158 1.1 tron */
159 1.1 tron
160 1.1 tron if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
161 1.1 tron return EINVAL;
162 1.1 tron
163 1.1 tron error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
164 1.1 tron if (error)
165 1.1 tron return error;
166 1.1 tron
167 1.1 tron if (SCARG(uap, pid) != 0) {
168 1.33 ad kauth_cred_t pc = cl->l_cred;
169 1.1 tron
170 1.1 tron if ((p = pfind(SCARG(uap, pid))) == NULL)
171 1.1 tron return ESRCH;
172 1.33 ad if (!(cl->l_proc == p ||
173 1.38 elad kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER, NULL) == 0 ||
174 1.30 elad kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
175 1.30 elad kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
176 1.30 elad kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
177 1.30 elad kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
178 1.1 tron return EPERM;
179 1.1 tron }
180 1.1 tron
181 1.1 tron return 0;
182 1.1 tron }
183 1.1 tron
184 1.1 tron int
185 1.37 christos linux_sys_sched_getparam(struct lwp *cl, void *v, register_t *retval)
186 1.1 tron {
187 1.1 tron struct linux_sys_sched_getparam_args /* {
188 1.1 tron syscallarg(linux_pid_t) pid;
189 1.1 tron syscallarg(struct linux_sched_param *) sp;
190 1.1 tron } */ *uap = v;
191 1.5 augustss struct proc *p;
192 1.1 tron struct linux_sched_param lp;
193 1.1 tron
194 1.1 tron /*
195 1.1 tron * We only check for valid parameters and return a dummy priority afterwards.
196 1.1 tron */
197 1.1 tron if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
198 1.1 tron return EINVAL;
199 1.1 tron
200 1.1 tron if (SCARG(uap, pid) != 0) {
201 1.33 ad kauth_cred_t pc = cl->l_cred;
202 1.1 tron
203 1.1 tron if ((p = pfind(SCARG(uap, pid))) == NULL)
204 1.1 tron return ESRCH;
205 1.33 ad if (!(cl->l_proc == p ||
206 1.38 elad kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER, NULL) == 0 ||
207 1.30 elad kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
208 1.30 elad kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
209 1.30 elad kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
210 1.30 elad kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
211 1.1 tron return EPERM;
212 1.1 tron }
213 1.1 tron
214 1.1 tron lp.sched_priority = 0;
215 1.1 tron return copyout(&lp, SCARG(uap, sp), sizeof(lp));
216 1.1 tron }
217 1.1 tron
218 1.1 tron int
219 1.36 christos linux_sys_sched_setscheduler(struct lwp *cl, void *v,
220 1.37 christos register_t *retval)
221 1.1 tron {
222 1.1 tron struct linux_sys_sched_setscheduler_args /* {
223 1.1 tron syscallarg(linux_pid_t) pid;
224 1.1 tron syscallarg(int) policy;
225 1.1 tron syscallarg(cont struct linux_sched_scheduler *) sp;
226 1.1 tron } */ *uap = v;
227 1.1 tron int error;
228 1.1 tron struct linux_sched_param lp;
229 1.5 augustss struct proc *p;
230 1.1 tron
231 1.1 tron /*
232 1.1 tron * We only check for valid parameters and return afterwards.
233 1.1 tron */
234 1.1 tron
235 1.1 tron if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
236 1.1 tron return EINVAL;
237 1.1 tron
238 1.1 tron error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
239 1.1 tron if (error)
240 1.1 tron return error;
241 1.1 tron
242 1.1 tron if (SCARG(uap, pid) != 0) {
243 1.33 ad kauth_cred_t pc = cl->l_cred;
244 1.1 tron
245 1.1 tron if ((p = pfind(SCARG(uap, pid))) == NULL)
246 1.1 tron return ESRCH;
247 1.33 ad if (!(cl->l_proc == p ||
248 1.38 elad kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER, NULL) == 0 ||
249 1.30 elad kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
250 1.30 elad kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
251 1.30 elad kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
252 1.30 elad kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
253 1.1 tron return EPERM;
254 1.1 tron }
255 1.1 tron
256 1.34 manu return 0;
257 1.1 tron /*
258 1.1 tron * We can't emulate anything put the default scheduling policy.
259 1.1 tron */
260 1.1 tron if (SCARG(uap, policy) != LINUX_SCHED_OTHER || lp.sched_priority != 0)
261 1.1 tron return EINVAL;
262 1.1 tron
263 1.1 tron return 0;
264 1.1 tron }
265 1.1 tron
266 1.1 tron int
267 1.12 thorpej linux_sys_sched_getscheduler(cl, v, retval)
268 1.12 thorpej struct lwp *cl;
269 1.1 tron void *v;
270 1.1 tron register_t *retval;
271 1.1 tron {
272 1.1 tron struct linux_sys_sched_getscheduler_args /* {
273 1.1 tron syscallarg(linux_pid_t) pid;
274 1.1 tron } */ *uap = v;
275 1.5 augustss struct proc *p;
276 1.1 tron
277 1.1 tron *retval = -1;
278 1.1 tron /*
279 1.1 tron * We only check for valid parameters and return afterwards.
280 1.1 tron */
281 1.1 tron
282 1.1 tron if (SCARG(uap, pid) != 0) {
283 1.33 ad kauth_cred_t pc = cl->l_cred;
284 1.1 tron
285 1.1 tron if ((p = pfind(SCARG(uap, pid))) == NULL)
286 1.1 tron return ESRCH;
287 1.33 ad if (!(cl->l_proc == p ||
288 1.38 elad kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER, NULL) == 0 ||
289 1.30 elad kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
290 1.30 elad kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
291 1.30 elad kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
292 1.30 elad kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
293 1.1 tron return EPERM;
294 1.1 tron }
295 1.1 tron
296 1.1 tron /*
297 1.1 tron * We can't emulate anything put the default scheduling policy.
298 1.1 tron */
299 1.1 tron *retval = LINUX_SCHED_OTHER;
300 1.1 tron return 0;
301 1.1 tron }
302 1.1 tron
303 1.1 tron int
304 1.37 christos linux_sys_sched_yield(struct lwp *cl, void *v,
305 1.37 christos register_t *retval)
306 1.1 tron {
307 1.11 gmcgarry
308 1.11 gmcgarry yield();
309 1.1 tron return 0;
310 1.1 tron }
311 1.1 tron
312 1.1 tron int
313 1.37 christos linux_sys_sched_get_priority_max(struct lwp *cl, void *v,
314 1.36 christos register_t *retval)
315 1.1 tron {
316 1.1 tron struct linux_sys_sched_get_priority_max_args /* {
317 1.1 tron syscallarg(int) policy;
318 1.1 tron } */ *uap = v;
319 1.1 tron
320 1.1 tron /*
321 1.1 tron * We can't emulate anything put the default scheduling policy.
322 1.1 tron */
323 1.1 tron if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
324 1.1 tron *retval = -1;
325 1.1 tron return EINVAL;
326 1.1 tron }
327 1.1 tron
328 1.1 tron *retval = 0;
329 1.1 tron return 0;
330 1.1 tron }
331 1.1 tron
332 1.1 tron int
333 1.37 christos linux_sys_sched_get_priority_min(struct lwp *cl, void *v,
334 1.36 christos register_t *retval)
335 1.1 tron {
336 1.1 tron struct linux_sys_sched_get_priority_min_args /* {
337 1.1 tron syscallarg(int) policy;
338 1.1 tron } */ *uap = v;
339 1.1 tron
340 1.1 tron /*
341 1.1 tron * We can't emulate anything put the default scheduling policy.
342 1.1 tron */
343 1.1 tron if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
344 1.1 tron *retval = -1;
345 1.1 tron return EINVAL;
346 1.1 tron }
347 1.1 tron
348 1.1 tron *retval = 0;
349 1.1 tron return 0;
350 1.1 tron }
351 1.14 jdolecek
352 1.14 jdolecek #ifndef __m68k__
353 1.14 jdolecek /* Present on everything but m68k */
354 1.14 jdolecek int
355 1.14 jdolecek linux_sys_exit_group(l, v, retval)
356 1.14 jdolecek struct lwp *l;
357 1.14 jdolecek void *v;
358 1.14 jdolecek register_t *retval;
359 1.14 jdolecek {
360 1.35 dogcow #ifdef LINUX_NPTL
361 1.14 jdolecek struct linux_sys_exit_group_args /* {
362 1.14 jdolecek syscallarg(int) error_code;
363 1.14 jdolecek } */ *uap = v;
364 1.31 manu struct proc *p = l->l_proc;
365 1.31 manu struct linux_emuldata *led = p->p_emuldata;
366 1.31 manu struct linux_emuldata *e;
367 1.14 jdolecek
368 1.39 njoly if (led->s->flags & LINUX_LES_USE_NPTL) {
369 1.39 njoly
370 1.34 manu #ifdef DEBUG_LINUX
371 1.39 njoly printf("%s:%d, led->s->refs = %d\n", __func__, __LINE__,
372 1.39 njoly led->s->refs);
373 1.34 manu #endif
374 1.39 njoly
375 1.39 njoly /*
376 1.39 njoly * The calling thread is supposed to kill all threads
377 1.39 njoly * in the same thread group (i.e. all threads created
378 1.39 njoly * via clone(2) with CLONE_THREAD flag set).
379 1.39 njoly *
380 1.39 njoly * If there is only one thread, things are quite simple
381 1.39 njoly */
382 1.39 njoly if (led->s->refs == 1)
383 1.39 njoly return sys_exit(l, v, retval);
384 1.31 manu
385 1.31 manu #ifdef DEBUG_LINUX
386 1.39 njoly printf("%s:%d\n", __func__, __LINE__);
387 1.31 manu #endif
388 1.34 manu
389 1.39 njoly led->s->flags |= LINUX_LES_INEXITGROUP;
390 1.39 njoly led->s->xstat = W_EXITCODE(SCARG(uap, error_code), 0);
391 1.34 manu
392 1.39 njoly /*
393 1.39 njoly * Kill all threads in the group. The emulation exit hook takes
394 1.39 njoly * care of hiding the zombies and reporting the exit code
395 1.39 njoly * properly.
396 1.39 njoly */
397 1.41 ad mutex_enter(&proclist_mutex);
398 1.39 njoly LIST_FOREACH(e, &led->s->threads, threads) {
399 1.39 njoly if (e->proc == p)
400 1.39 njoly continue;
401 1.31 manu
402 1.34 manu #ifdef DEBUG_LINUX
403 1.39 njoly printf("%s: kill PID %d\n", __func__, e->proc->p_pid);
404 1.34 manu #endif
405 1.39 njoly psignal(e->proc, SIGKILL);
406 1.39 njoly }
407 1.39 njoly
408 1.39 njoly /* Now, kill ourselves */
409 1.39 njoly psignal(p, SIGKILL);
410 1.41 ad mutex_exit(&proclist_mutex);
411 1.41 ad
412 1.39 njoly return 0;
413 1.39 njoly
414 1.31 manu }
415 1.39 njoly #endif /* LINUX_NPTL */
416 1.14 jdolecek
417 1.34 manu return sys_exit(l, v, retval);
418 1.14 jdolecek }
419 1.14 jdolecek #endif /* !__m68k__ */
420 1.19 manu
421 1.21 manu #ifdef LINUX_NPTL
422 1.19 manu int
423 1.19 manu linux_sys_set_tid_address(l, v, retval)
424 1.19 manu struct lwp *l;
425 1.19 manu void *v;
426 1.19 manu register_t *retval;
427 1.19 manu {
428 1.19 manu struct linux_sys_set_tid_address_args /* {
429 1.19 manu syscallarg(int *) tidptr;
430 1.19 manu } */ *uap = v;
431 1.19 manu struct linux_emuldata *led;
432 1.19 manu
433 1.19 manu led = (struct linux_emuldata *)l->l_proc->p_emuldata;
434 1.19 manu led->clear_tid = SCARG(uap, tid);
435 1.19 manu
436 1.39 njoly led->s->flags |= LINUX_LES_USE_NPTL;
437 1.39 njoly
438 1.19 manu *retval = l->l_proc->p_pid;
439 1.19 manu
440 1.19 manu return 0;
441 1.19 manu }
442 1.20 manu
443 1.20 manu /* ARGUSED1 */
444 1.20 manu int
445 1.20 manu linux_sys_gettid(l, v, retval)
446 1.20 manu struct lwp *l;
447 1.20 manu void *v;
448 1.20 manu register_t *retval;
449 1.20 manu {
450 1.31 manu /* The Linux kernel does it exactly that way */
451 1.20 manu *retval = l->l_proc->p_pid;
452 1.20 manu return 0;
453 1.20 manu }
454 1.22 manu
455 1.31 manu #ifdef LINUX_NPTL
456 1.31 manu /* ARGUSED1 */
457 1.31 manu int
458 1.31 manu linux_sys_getpid(l, v, retval)
459 1.31 manu struct lwp *l;
460 1.31 manu void *v;
461 1.31 manu register_t *retval;
462 1.31 manu {
463 1.39 njoly struct linux_emuldata *led = l->l_proc->p_emuldata;
464 1.31 manu
465 1.39 njoly if (led->s->flags & LINUX_LES_USE_NPTL) {
466 1.39 njoly /* The Linux kernel does it exactly that way */
467 1.39 njoly *retval = led->s->group_pid;
468 1.39 njoly } else {
469 1.39 njoly *retval = l->l_proc->p_pid;
470 1.39 njoly }
471 1.31 manu
472 1.31 manu return 0;
473 1.31 manu }
474 1.31 manu
475 1.31 manu /* ARGUSED1 */
476 1.31 manu int
477 1.31 manu linux_sys_getppid(l, v, retval)
478 1.31 manu struct lwp *l;
479 1.31 manu void *v;
480 1.31 manu register_t *retval;
481 1.31 manu {
482 1.31 manu struct proc *p = l->l_proc;
483 1.31 manu struct linux_emuldata *led = p->p_emuldata;
484 1.31 manu struct proc *glp;
485 1.31 manu struct proc *pp;
486 1.31 manu
487 1.39 njoly if (led->s->flags & LINUX_LES_USE_NPTL) {
488 1.31 manu
489 1.39 njoly /* Find the thread group leader's parent */
490 1.39 njoly if ((glp = pfind(led->s->group_pid)) == NULL) {
491 1.39 njoly /* Maybe panic... */
492 1.39 njoly printf("linux_sys_getppid: missing group leader PID"
493 1.39 njoly " %d\n", led->s->group_pid);
494 1.39 njoly return -1;
495 1.39 njoly }
496 1.39 njoly pp = glp->p_pptr;
497 1.39 njoly
498 1.39 njoly /* If this is a Linux process too, return thread group PID */
499 1.39 njoly if (pp->p_emul == p->p_emul) {
500 1.39 njoly struct linux_emuldata *pled;
501 1.39 njoly
502 1.39 njoly pled = pp->p_emuldata;
503 1.39 njoly *retval = pled->s->group_pid;
504 1.39 njoly } else {
505 1.39 njoly *retval = pp->p_pid;
506 1.39 njoly }
507 1.31 manu
508 1.31 manu } else {
509 1.39 njoly *retval = p->p_pptr->p_pid;
510 1.31 manu }
511 1.31 manu
512 1.31 manu return 0;
513 1.31 manu }
514 1.31 manu #endif /* LINUX_NPTL */
515 1.31 manu
516 1.22 manu int
517 1.22 manu linux_sys_sched_getaffinity(l, v, retval)
518 1.22 manu struct lwp *l;
519 1.22 manu void *v;
520 1.22 manu register_t *retval;
521 1.22 manu {
522 1.22 manu struct linux_sys_sched_getaffinity_args /* {
523 1.22 manu syscallarg(pid_t) pid;
524 1.22 manu syscallarg(unsigned int) len;
525 1.22 manu syscallarg(unsigned long *) mask;
526 1.22 manu } */ *uap = v;
527 1.22 manu int error;
528 1.22 manu int ret;
529 1.22 manu int ncpu;
530 1.22 manu int name[2];
531 1.22 manu size_t sz;
532 1.22 manu char *data;
533 1.22 manu int *retp;
534 1.22 manu
535 1.22 manu if (SCARG(uap, mask) == NULL)
536 1.22 manu return EINVAL;
537 1.22 manu
538 1.22 manu if (SCARG(uap, len) < sizeof(int))
539 1.22 manu return EINVAL;
540 1.22 manu
541 1.22 manu if (pfind(SCARG(uap, pid)) == NULL)
542 1.22 manu return ESRCH;
543 1.22 manu
544 1.22 manu /*
545 1.22 manu * return the actual number of CPU, tag all of them as available
546 1.22 manu * The result is a mask, the first CPU being in the least significant
547 1.22 manu * bit.
548 1.22 manu */
549 1.22 manu name[0] = CTL_HW;
550 1.22 manu name[1] = HW_NCPU;
551 1.22 manu sz = sizeof(ncpu);
552 1.22 manu
553 1.22 manu if ((error = old_sysctl(&name[0], 2, &ncpu, &sz, NULL, 0, NULL)) != 0)
554 1.22 manu return error;
555 1.22 manu
556 1.22 manu ret = (1 << ncpu) - 1;
557 1.22 manu
558 1.22 manu data = malloc(SCARG(uap, len), M_TEMP, M_WAITOK|M_ZERO);
559 1.22 manu retp = (int *)&data[SCARG(uap, len) - sizeof(ret)];
560 1.22 manu *retp = ret;
561 1.22 manu
562 1.22 manu if ((error = copyout(data, SCARG(uap, mask), SCARG(uap, len))) != 0)
563 1.22 manu return error;
564 1.22 manu
565 1.22 manu free(data, M_TEMP);
566 1.22 manu
567 1.22 manu return 0;
568 1.22 manu
569 1.22 manu }
570 1.22 manu
571 1.22 manu int
572 1.22 manu linux_sys_sched_setaffinity(l, v, retval)
573 1.22 manu struct lwp *l;
574 1.22 manu void *v;
575 1.22 manu register_t *retval;
576 1.22 manu {
577 1.22 manu struct linux_sys_sched_setaffinity_args /* {
578 1.22 manu syscallarg(pid_t) pid;
579 1.22 manu syscallarg(unsigned int) len;
580 1.22 manu syscallarg(unsigned long *) mask;
581 1.22 manu } */ *uap = v;
582 1.22 manu
583 1.22 manu if (pfind(SCARG(uap, pid)) == NULL)
584 1.22 manu return ESRCH;
585 1.22 manu
586 1.22 manu /* Let's ignore it */
587 1.22 manu #ifdef DEBUG_LINUX
588 1.22 manu printf("linux_sys_sched_setaffinity\n");
589 1.22 manu #endif
590 1.22 manu return 0;
591 1.22 manu };
592 1.23 manu #endif /* LINUX_NPTL */
593