Fri Jun 19 21:41:34 2009 UTC ()
Pull up following revision(s) (requested by njoly in ticket #822):
	sys/compat/linux/common/linux_sched.c: revision 1.59
In linux_sys_sched_getaffinity(), do not leak memory on error.


(snj)
diff -r1.58 -r1.58.10.1 src/sys/compat/linux/common/linux_sched.c

cvs diff -r1.58 -r1.58.10.1 src/sys/compat/linux/common/linux_sched.c (switch to unified diff)

--- src/sys/compat/linux/common/linux_sched.c 2008/10/25 23:38:28 1.58
+++ src/sys/compat/linux/common/linux_sched.c 2009/06/19 21:41:33 1.58.10.1
@@ -1,676 +1,675 @@ @@ -1,676 +1,675 @@
1/* $NetBSD: linux_sched.c,v 1.58 2008/10/25 23:38:28 christos Exp $ */ 1/* $NetBSD: linux_sched.c,v 1.58.10.1 2009/06/19 21:41:33 snj Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center; by Matthias Scheler. 9 * NASA Ames Research Center; by Matthias Scheler.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33/* 33/*
34 * Linux compatibility module. Try to deal with scheduler related syscalls. 34 * Linux compatibility module. Try to deal with scheduler related syscalls.
35 */ 35 */
36 36
37#include <sys/cdefs.h> 37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.58 2008/10/25 23:38:28 christos Exp $"); 38__KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.58.10.1 2009/06/19 21:41:33 snj Exp $");
39 39
40#include <sys/param.h> 40#include <sys/param.h>
41#include <sys/mount.h> 41#include <sys/mount.h>
42#include <sys/proc.h> 42#include <sys/proc.h>
43#include <sys/systm.h> 43#include <sys/systm.h>
44#include <sys/sysctl.h> 44#include <sys/sysctl.h>
45#include <sys/malloc.h> 45#include <sys/malloc.h>
46#include <sys/syscallargs.h> 46#include <sys/syscallargs.h>
47#include <sys/wait.h> 47#include <sys/wait.h>
48#include <sys/kauth.h> 48#include <sys/kauth.h>
49#include <sys/ptrace.h> 49#include <sys/ptrace.h>
50 50
51#include <sys/cpu.h> 51#include <sys/cpu.h>
52 52
53#include <compat/linux/common/linux_types.h> 53#include <compat/linux/common/linux_types.h>
54#include <compat/linux/common/linux_signal.h> 54#include <compat/linux/common/linux_signal.h>
55#include <compat/linux/common/linux_machdep.h> /* For LINUX_NPTL */ 55#include <compat/linux/common/linux_machdep.h> /* For LINUX_NPTL */
56#include <compat/linux/common/linux_emuldata.h> 56#include <compat/linux/common/linux_emuldata.h>
57#include <compat/linux/common/linux_ipc.h> 57#include <compat/linux/common/linux_ipc.h>
58#include <compat/linux/common/linux_sem.h> 58#include <compat/linux/common/linux_sem.h>
59#include <compat/linux/common/linux_exec.h> 59#include <compat/linux/common/linux_exec.h>
60 60
61#include <compat/linux/linux_syscallargs.h> 61#include <compat/linux/linux_syscallargs.h>
62 62
63#include <compat/linux/common/linux_sched.h> 63#include <compat/linux/common/linux_sched.h>
64 64
65int 65int
66linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval) 66linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
67{ 67{
68 /* { 68 /* {
69 syscallarg(int) flags; 69 syscallarg(int) flags;
70 syscallarg(void *) stack; 70 syscallarg(void *) stack;
71#ifdef LINUX_NPTL 71#ifdef LINUX_NPTL
72 syscallarg(void *) parent_tidptr; 72 syscallarg(void *) parent_tidptr;
73 syscallarg(void *) child_tidptr; 73 syscallarg(void *) child_tidptr;
74#endif 74#endif
75 } */ 75 } */
76 int flags, sig; 76 int flags, sig;
77 int error; 77 int error;
78 struct proc *p; 78 struct proc *p;
79#ifdef LINUX_NPTL 79#ifdef LINUX_NPTL
80 struct linux_emuldata *led; 80 struct linux_emuldata *led;
81#endif 81#endif
82 82
83 /* 83 /*
84 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags. 84 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
85 */ 85 */
86 if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE)) 86 if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
87 return (EINVAL); 87 return (EINVAL);
88 88
89 /* 89 /*
90 * Thread group implies shared signals. Shared signals 90 * Thread group implies shared signals. Shared signals
91 * imply shared VM. This matches what Linux kernel does. 91 * imply shared VM. This matches what Linux kernel does.
92 */ 92 */
93 if (SCARG(uap, flags) & LINUX_CLONE_THREAD 93 if (SCARG(uap, flags) & LINUX_CLONE_THREAD
94 && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0) 94 && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
95 return (EINVAL); 95 return (EINVAL);
96 if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND 96 if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
97 && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0) 97 && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
98 return (EINVAL); 98 return (EINVAL);
99 99
100 flags = 0; 100 flags = 0;
101 101
102 if (SCARG(uap, flags) & LINUX_CLONE_VM) 102 if (SCARG(uap, flags) & LINUX_CLONE_VM)
103 flags |= FORK_SHAREVM; 103 flags |= FORK_SHAREVM;
104 if (SCARG(uap, flags) & LINUX_CLONE_FS) 104 if (SCARG(uap, flags) & LINUX_CLONE_FS)
105 flags |= FORK_SHARECWD; 105 flags |= FORK_SHARECWD;
106 if (SCARG(uap, flags) & LINUX_CLONE_FILES) 106 if (SCARG(uap, flags) & LINUX_CLONE_FILES)
107 flags |= FORK_SHAREFILES; 107 flags |= FORK_SHAREFILES;
108 if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) 108 if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
109 flags |= FORK_SHARESIGS; 109 flags |= FORK_SHARESIGS;
110 if (SCARG(uap, flags) & LINUX_CLONE_VFORK) 110 if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
111 flags |= FORK_PPWAIT; 111 flags |= FORK_PPWAIT;
112 112
113 sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL; 113 sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
114 if (sig < 0 || sig >= LINUX__NSIG) 114 if (sig < 0 || sig >= LINUX__NSIG)
115 return (EINVAL); 115 return (EINVAL);
116 sig = linux_to_native_signo[sig]; 116 sig = linux_to_native_signo[sig];
117 117
118#ifdef LINUX_NPTL 118#ifdef LINUX_NPTL
119 led = (struct linux_emuldata *)l->l_proc->p_emuldata; 119 led = (struct linux_emuldata *)l->l_proc->p_emuldata;
120 120
121 led->parent_tidptr = SCARG(uap, parent_tidptr); 121 led->parent_tidptr = SCARG(uap, parent_tidptr);
122 led->child_tidptr = SCARG(uap, child_tidptr); 122 led->child_tidptr = SCARG(uap, child_tidptr);
123 led->clone_flags = SCARG(uap, flags); 123 led->clone_flags = SCARG(uap, flags);
124#endif /* LINUX_NPTL */ 124#endif /* LINUX_NPTL */
125 125
126 /* 126 /*
127 * Note that Linux does not provide a portable way of specifying 127 * Note that Linux does not provide a portable way of specifying
128 * the stack area; the caller must know if the stack grows up 128 * the stack area; the caller must know if the stack grows up
129 * or down. So, we pass a stack size of 0, so that the code 129 * or down. So, we pass a stack size of 0, so that the code
130 * that makes this adjustment is a noop. 130 * that makes this adjustment is a noop.
131 */ 131 */
132 if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0, 132 if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
133 NULL, NULL, retval, &p)) != 0) 133 NULL, NULL, retval, &p)) != 0)
134 return error; 134 return error;
135 135
136#ifdef LINUX_NPTL 136#ifdef LINUX_NPTL
137 if ((SCARG(uap, flags) & LINUX_CLONE_SETTLS) != 0) 137 if ((SCARG(uap, flags) & LINUX_CLONE_SETTLS) != 0)
138 return linux_init_thread_area(l, LIST_FIRST(&p->p_lwps)); 138 return linux_init_thread_area(l, LIST_FIRST(&p->p_lwps));
139#endif /* LINUX_NPTL */ 139#endif /* LINUX_NPTL */
140 140
141 return 0; 141 return 0;
142} 142}
143 143
144/* 144/*
145 * linux realtime priority 145 * linux realtime priority
146 * 146 *
147 * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99]. 147 * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
148 * 148 *
149 * - SCHED_OTHER tasks don't have realtime priorities. 149 * - SCHED_OTHER tasks don't have realtime priorities.
150 * in particular, sched_param::sched_priority is always 0. 150 * in particular, sched_param::sched_priority is always 0.
151 */ 151 */
152 152
153#define LINUX_SCHED_RTPRIO_MIN 1 153#define LINUX_SCHED_RTPRIO_MIN 1
154#define LINUX_SCHED_RTPRIO_MAX 99 154#define LINUX_SCHED_RTPRIO_MAX 99
155 155
156static int 156static int
157sched_linux2native(int linux_policy, struct linux_sched_param *linux_params, 157sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
158 int *native_policy, struct sched_param *native_params) 158 int *native_policy, struct sched_param *native_params)
159{ 159{
160 160
161 switch (linux_policy) { 161 switch (linux_policy) {
162 case LINUX_SCHED_OTHER: 162 case LINUX_SCHED_OTHER:
163 if (native_policy != NULL) { 163 if (native_policy != NULL) {
164 *native_policy = SCHED_OTHER; 164 *native_policy = SCHED_OTHER;
165 } 165 }
166 break; 166 break;
167 167
168 case LINUX_SCHED_FIFO: 168 case LINUX_SCHED_FIFO:
169 if (native_policy != NULL) { 169 if (native_policy != NULL) {
170 *native_policy = SCHED_FIFO; 170 *native_policy = SCHED_FIFO;
171 } 171 }
172 break; 172 break;
173 173
174 case LINUX_SCHED_RR: 174 case LINUX_SCHED_RR:
175 if (native_policy != NULL) { 175 if (native_policy != NULL) {
176 *native_policy = SCHED_RR; 176 *native_policy = SCHED_RR;
177 } 177 }
178 break; 178 break;
179 179
180 default: 180 default:
181 return EINVAL; 181 return EINVAL;
182 } 182 }
183 183
184 if (linux_params != NULL) { 184 if (linux_params != NULL) {
185 int prio = linux_params->sched_priority; 185 int prio = linux_params->sched_priority;
186  186
187 KASSERT(native_params != NULL); 187 KASSERT(native_params != NULL);
188 188
189 if (linux_policy == LINUX_SCHED_OTHER) { 189 if (linux_policy == LINUX_SCHED_OTHER) {
190 if (prio != 0) { 190 if (prio != 0) {
191 return EINVAL; 191 return EINVAL;
192 } 192 }
193 native_params->sched_priority = PRI_NONE; /* XXX */ 193 native_params->sched_priority = PRI_NONE; /* XXX */
194 } else { 194 } else {
195 if (prio < LINUX_SCHED_RTPRIO_MIN || 195 if (prio < LINUX_SCHED_RTPRIO_MIN ||
196 prio > LINUX_SCHED_RTPRIO_MAX) { 196 prio > LINUX_SCHED_RTPRIO_MAX) {
197 return EINVAL; 197 return EINVAL;
198 } 198 }
199 native_params->sched_priority = 199 native_params->sched_priority =
200 (prio - LINUX_SCHED_RTPRIO_MIN) 200 (prio - LINUX_SCHED_RTPRIO_MIN)
201 * (SCHED_PRI_MAX - SCHED_PRI_MIN) 201 * (SCHED_PRI_MAX - SCHED_PRI_MIN)
202 / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN) 202 / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
203 + SCHED_PRI_MIN; 203 + SCHED_PRI_MIN;
204 } 204 }
205 } 205 }
206 206
207 return 0; 207 return 0;
208} 208}
209 209
210static int 210static int
211sched_native2linux(int native_policy, struct sched_param *native_params, 211sched_native2linux(int native_policy, struct sched_param *native_params,
212 int *linux_policy, struct linux_sched_param *linux_params) 212 int *linux_policy, struct linux_sched_param *linux_params)
213{ 213{
214 214
215 switch (native_policy) { 215 switch (native_policy) {
216 case SCHED_OTHER: 216 case SCHED_OTHER:
217 if (linux_policy != NULL) { 217 if (linux_policy != NULL) {
218 *linux_policy = LINUX_SCHED_OTHER; 218 *linux_policy = LINUX_SCHED_OTHER;
219 } 219 }
220 break; 220 break;
221 221
222 case SCHED_FIFO: 222 case SCHED_FIFO:
223 if (linux_policy != NULL) { 223 if (linux_policy != NULL) {
224 *linux_policy = LINUX_SCHED_FIFO; 224 *linux_policy = LINUX_SCHED_FIFO;
225 } 225 }
226 break; 226 break;
227 227
228 case SCHED_RR: 228 case SCHED_RR:
229 if (linux_policy != NULL) { 229 if (linux_policy != NULL) {
230 *linux_policy = LINUX_SCHED_RR; 230 *linux_policy = LINUX_SCHED_RR;
231 } 231 }
232 break; 232 break;
233 233
234 default: 234 default:
235 panic("%s: unknown policy %d\n", __func__, native_policy); 235 panic("%s: unknown policy %d\n", __func__, native_policy);
236 } 236 }
237 237
238 if (native_params != NULL) { 238 if (native_params != NULL) {
239 int prio = native_params->sched_priority; 239 int prio = native_params->sched_priority;
240 240
241 KASSERT(prio >= SCHED_PRI_MIN); 241 KASSERT(prio >= SCHED_PRI_MIN);
242 KASSERT(prio <= SCHED_PRI_MAX); 242 KASSERT(prio <= SCHED_PRI_MAX);
243 KASSERT(linux_params != NULL); 243 KASSERT(linux_params != NULL);
244 244
245#ifdef DEBUG_LINUX 245#ifdef DEBUG_LINUX
246 printf("native2linux: native: policy %d, priority %d\n", 246 printf("native2linux: native: policy %d, priority %d\n",
247 native_policy, prio); 247 native_policy, prio);
248#endif 248#endif
249 249
250 if (native_policy == SCHED_OTHER) { 250 if (native_policy == SCHED_OTHER) {
251 linux_params->sched_priority = 0; 251 linux_params->sched_priority = 0;
252 } else { 252 } else {
253 linux_params->sched_priority = 253 linux_params->sched_priority =
254 (prio - SCHED_PRI_MIN) 254 (prio - SCHED_PRI_MIN)
255 * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN) 255 * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
256 / (SCHED_PRI_MAX - SCHED_PRI_MIN) 256 / (SCHED_PRI_MAX - SCHED_PRI_MIN)
257 + LINUX_SCHED_RTPRIO_MIN; 257 + LINUX_SCHED_RTPRIO_MIN;
258 } 258 }
259#ifdef DEBUG_LINUX 259#ifdef DEBUG_LINUX
260 printf("native2linux: linux: policy %d, priority %d\n", 260 printf("native2linux: linux: policy %d, priority %d\n",
261 -1, linux_params->sched_priority); 261 -1, linux_params->sched_priority);
262#endif 262#endif
263 } 263 }
264 264
265 return 0; 265 return 0;
266} 266}
267 267
268int 268int
269linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval) 269linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
270{ 270{
271 /* { 271 /* {
272 syscallarg(linux_pid_t) pid; 272 syscallarg(linux_pid_t) pid;
273 syscallarg(const struct linux_sched_param *) sp; 273 syscallarg(const struct linux_sched_param *) sp;
274 } */ 274 } */
275 int error, policy; 275 int error, policy;
276 struct linux_sched_param lp; 276 struct linux_sched_param lp;
277 struct sched_param sp; 277 struct sched_param sp;
278 278
279 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) { 279 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
280 error = EINVAL; 280 error = EINVAL;
281 goto out; 281 goto out;
282 } 282 }
283 283
284 error = copyin(SCARG(uap, sp), &lp, sizeof(lp)); 284 error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
285 if (error) 285 if (error)
286 goto out; 286 goto out;
287 287
288 /* We need the current policy in Linux terms. */ 288 /* We need the current policy in Linux terms. */
289 error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL); 289 error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
290 if (error) 290 if (error)
291 goto out; 291 goto out;
292 error = sched_native2linux(policy, NULL, &policy, NULL); 292 error = sched_native2linux(policy, NULL, &policy, NULL);
293 if (error) 293 if (error)
294 goto out; 294 goto out;
295 295
296 error = sched_linux2native(policy, &lp, &policy, &sp); 296 error = sched_linux2native(policy, &lp, &policy, &sp);
297 if (error) 297 if (error)
298 goto out; 298 goto out;
299 299
300 error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp); 300 error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
301 if (error) 301 if (error)
302 goto out; 302 goto out;
303 303
304 out: 304 out:
305 return error; 305 return error;
306} 306}
307 307
308int 308int
309linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval) 309linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
310{ 310{
311 /* { 311 /* {
312 syscallarg(linux_pid_t) pid; 312 syscallarg(linux_pid_t) pid;
313 syscallarg(struct linux_sched_param *) sp; 313 syscallarg(struct linux_sched_param *) sp;
314 } */ 314 } */
315 struct linux_sched_param lp; 315 struct linux_sched_param lp;
316 struct sched_param sp; 316 struct sched_param sp;
317 int error, policy; 317 int error, policy;
318 318
319 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) { 319 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
320 error = EINVAL; 320 error = EINVAL;
321 goto out; 321 goto out;
322 } 322 }
323 323
324 error = do_sched_getparam(SCARG(uap, pid), 0, &policy, &sp); 324 error = do_sched_getparam(SCARG(uap, pid), 0, &policy, &sp);
325 if (error) 325 if (error)
326 goto out; 326 goto out;
327#ifdef DEBUG_LINUX 327#ifdef DEBUG_LINUX
328 printf("getparam: native: policy %d, priority %d\n", 328 printf("getparam: native: policy %d, priority %d\n",
329 policy, sp.sched_priority); 329 policy, sp.sched_priority);
330#endif 330#endif
331 331
332 error = sched_native2linux(policy, &sp, NULL, &lp); 332 error = sched_native2linux(policy, &sp, NULL, &lp);
333 if (error) 333 if (error)
334 goto out; 334 goto out;
335#ifdef DEBUG_LINUX 335#ifdef DEBUG_LINUX
336 printf("getparam: linux: policy %d, priority %d\n", 336 printf("getparam: linux: policy %d, priority %d\n",
337 policy, lp.sched_priority); 337 policy, lp.sched_priority);
338#endif 338#endif
339 339
340 error = copyout(&lp, SCARG(uap, sp), sizeof(lp)); 340 error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
341 if (error) 341 if (error)
342 goto out; 342 goto out;
343 343
344 out: 344 out:
345 return error; 345 return error;
346} 346}
347 347
348int 348int
349linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval) 349linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
350{ 350{
351 /* { 351 /* {
352 syscallarg(linux_pid_t) pid; 352 syscallarg(linux_pid_t) pid;
353 syscallarg(int) policy; 353 syscallarg(int) policy;
354 syscallarg(cont struct linux_sched_scheduler *) sp; 354 syscallarg(cont struct linux_sched_scheduler *) sp;
355 } */ 355 } */
356 int error, policy; 356 int error, policy;
357 struct linux_sched_param lp; 357 struct linux_sched_param lp;
358 struct sched_param sp; 358 struct sched_param sp;
359 359
360 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) { 360 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
361 error = EINVAL; 361 error = EINVAL;
362 goto out; 362 goto out;
363 } 363 }
364 364
365 error = copyin(SCARG(uap, sp), &lp, sizeof(lp)); 365 error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
366 if (error) 366 if (error)
367 goto out; 367 goto out;
368#ifdef DEBUG_LINUX 368#ifdef DEBUG_LINUX
369 printf("setscheduler: linux: policy %d, priority %d\n", 369 printf("setscheduler: linux: policy %d, priority %d\n",
370 SCARG(uap, policy), lp.sched_priority); 370 SCARG(uap, policy), lp.sched_priority);
371#endif 371#endif
372 372
373 error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp); 373 error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
374 if (error) 374 if (error)
375 goto out; 375 goto out;
376#ifdef DEBUG_LINUX 376#ifdef DEBUG_LINUX
377 printf("setscheduler: native: policy %d, priority %d\n", 377 printf("setscheduler: native: policy %d, priority %d\n",
378 policy, sp.sched_priority); 378 policy, sp.sched_priority);
379#endif 379#endif
380 380
381 error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp); 381 error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
382 if (error) 382 if (error)
383 goto out; 383 goto out;
384 384
385 out: 385 out:
386 return error; 386 return error;
387} 387}
388 388
389int 389int
390linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval) 390linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval)
391{ 391{
392 /* { 392 /* {
393 syscallarg(linux_pid_t) pid; 393 syscallarg(linux_pid_t) pid;
394 } */ 394 } */
395 int error, policy; 395 int error, policy;
396 396
397 *retval = -1; 397 *retval = -1;
398 398
399 error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL); 399 error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
400 if (error) 400 if (error)
401 goto out; 401 goto out;
402 402
403 error = sched_native2linux(policy, NULL, &policy, NULL); 403 error = sched_native2linux(policy, NULL, &policy, NULL);
404 if (error) 404 if (error)
405 goto out; 405 goto out;
406 406
407 *retval = policy; 407 *retval = policy;
408 408
409 out: 409 out:
410 return error; 410 return error;
411} 411}
412 412
413int 413int
414linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval) 414linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
415{ 415{
416 416
417 yield(); 417 yield();
418 return 0; 418 return 0;
419} 419}
420 420
421int 421int
422linux_sys_sched_get_priority_max(struct lwp *l, const struct linux_sys_sched_get_priority_max_args *uap, register_t *retval) 422linux_sys_sched_get_priority_max(struct lwp *l, const struct linux_sys_sched_get_priority_max_args *uap, register_t *retval)
423{ 423{
424 /* { 424 /* {
425 syscallarg(int) policy; 425 syscallarg(int) policy;
426 } */ 426 } */
427 427
428 switch (SCARG(uap, policy)) { 428 switch (SCARG(uap, policy)) {
429 case LINUX_SCHED_OTHER: 429 case LINUX_SCHED_OTHER:
430 *retval = 0; 430 *retval = 0;
431 break; 431 break;
432 case LINUX_SCHED_FIFO: 432 case LINUX_SCHED_FIFO:
433 case LINUX_SCHED_RR: 433 case LINUX_SCHED_RR:
434 *retval = LINUX_SCHED_RTPRIO_MAX; 434 *retval = LINUX_SCHED_RTPRIO_MAX;
435 break; 435 break;
436 default: 436 default:
437 return EINVAL; 437 return EINVAL;
438 } 438 }
439 439
440 return 0; 440 return 0;
441} 441}
442 442
443int 443int
444linux_sys_sched_get_priority_min(struct lwp *l, const struct linux_sys_sched_get_priority_min_args *uap, register_t *retval) 444linux_sys_sched_get_priority_min(struct lwp *l, const struct linux_sys_sched_get_priority_min_args *uap, register_t *retval)
445{ 445{
446 /* { 446 /* {
447 syscallarg(int) policy; 447 syscallarg(int) policy;
448 } */ 448 } */
449 449
450 switch (SCARG(uap, policy)) { 450 switch (SCARG(uap, policy)) {
451 case LINUX_SCHED_OTHER: 451 case LINUX_SCHED_OTHER:
452 *retval = 0; 452 *retval = 0;
453 break; 453 break;
454 case LINUX_SCHED_FIFO: 454 case LINUX_SCHED_FIFO:
455 case LINUX_SCHED_RR: 455 case LINUX_SCHED_RR:
456 *retval = LINUX_SCHED_RTPRIO_MIN; 456 *retval = LINUX_SCHED_RTPRIO_MIN;
457 break; 457 break;
458 default: 458 default:
459 return EINVAL; 459 return EINVAL;
460 } 460 }
461 461
462 return 0; 462 return 0;
463} 463}
464 464
465#ifndef __m68k__ 465#ifndef __m68k__
466/* Present on everything but m68k */ 466/* Present on everything but m68k */
467int 467int
468linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval) 468linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval)
469{ 469{
470#ifdef LINUX_NPTL 470#ifdef LINUX_NPTL
471 /* { 471 /* {
472 syscallarg(int) error_code; 472 syscallarg(int) error_code;
473 } */ 473 } */
474 struct proc *p = l->l_proc; 474 struct proc *p = l->l_proc;
475 struct linux_emuldata *led = p->p_emuldata; 475 struct linux_emuldata *led = p->p_emuldata;
476 struct linux_emuldata *e; 476 struct linux_emuldata *e;
477 477
478 if (led->s->flags & LINUX_LES_USE_NPTL) { 478 if (led->s->flags & LINUX_LES_USE_NPTL) {
479 479
480#ifdef DEBUG_LINUX 480#ifdef DEBUG_LINUX
481 printf("%s:%d, led->s->refs = %d\n", __func__, __LINE__, 481 printf("%s:%d, led->s->refs = %d\n", __func__, __LINE__,
482 led->s->refs); 482 led->s->refs);
483#endif 483#endif
484 484
485 /* 485 /*
486 * The calling thread is supposed to kill all threads 486 * The calling thread is supposed to kill all threads
487 * in the same thread group (i.e. all threads created 487 * in the same thread group (i.e. all threads created
488 * via clone(2) with CLONE_THREAD flag set). 488 * via clone(2) with CLONE_THREAD flag set).
489 * 489 *
490 * If there is only one thread, things are quite simple 490 * If there is only one thread, things are quite simple
491 */ 491 */
492 if (led->s->refs == 1) 492 if (led->s->refs == 1)
493 return sys_exit(l, (const void *)uap, retval); 493 return sys_exit(l, (const void *)uap, retval);
494 494
495#ifdef DEBUG_LINUX 495#ifdef DEBUG_LINUX
496 printf("%s:%d\n", __func__, __LINE__); 496 printf("%s:%d\n", __func__, __LINE__);
497#endif 497#endif
498 498
499 mutex_enter(proc_lock); 499 mutex_enter(proc_lock);
500 led->s->flags |= LINUX_LES_INEXITGROUP; 500 led->s->flags |= LINUX_LES_INEXITGROUP;
501 led->s->xstat = W_EXITCODE(SCARG(uap, error_code), 0); 501 led->s->xstat = W_EXITCODE(SCARG(uap, error_code), 0);
502 502
503 /* 503 /*
504 * Kill all threads in the group. The emulation exit hook takes 504 * Kill all threads in the group. The emulation exit hook takes
505 * care of hiding the zombies and reporting the exit code 505 * care of hiding the zombies and reporting the exit code
506 * properly. 506 * properly.
507 */ 507 */
508 LIST_FOREACH(e, &led->s->threads, threads) { 508 LIST_FOREACH(e, &led->s->threads, threads) {
509 if (e->proc == p) 509 if (e->proc == p)
510 continue; 510 continue;
511 511
512#ifdef DEBUG_LINUX 512#ifdef DEBUG_LINUX
513 printf("%s: kill PID %d\n", __func__, e->proc->p_pid); 513 printf("%s: kill PID %d\n", __func__, e->proc->p_pid);
514#endif 514#endif
515 psignal(e->proc, SIGKILL); 515 psignal(e->proc, SIGKILL);
516 } 516 }
517 517
518 /* Now, kill ourselves */ 518 /* Now, kill ourselves */
519 psignal(p, SIGKILL); 519 psignal(p, SIGKILL);
520 mutex_exit(proc_lock); 520 mutex_exit(proc_lock);
521 521
522 return 0; 522 return 0;
523 523
524 } 524 }
525#endif /* LINUX_NPTL */ 525#endif /* LINUX_NPTL */
526 526
527 return sys_exit(l, (const void *)uap, retval); 527 return sys_exit(l, (const void *)uap, retval);
528} 528}
529#endif /* !__m68k__ */ 529#endif /* !__m68k__ */
530 530
531#ifdef LINUX_NPTL 531#ifdef LINUX_NPTL
532int 532int
533linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval) 533linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval)
534{ 534{
535 /* { 535 /* {
536 syscallarg(int *) tidptr; 536 syscallarg(int *) tidptr;
537 } */ 537 } */
538 struct linux_emuldata *led; 538 struct linux_emuldata *led;
539 539
540 led = (struct linux_emuldata *)l->l_proc->p_emuldata; 540 led = (struct linux_emuldata *)l->l_proc->p_emuldata;
541 led->clear_tid = SCARG(uap, tid); 541 led->clear_tid = SCARG(uap, tid);
542 542
543 led->s->flags |= LINUX_LES_USE_NPTL; 543 led->s->flags |= LINUX_LES_USE_NPTL;
544 544
545 *retval = l->l_proc->p_pid; 545 *retval = l->l_proc->p_pid;
546 546
547 return 0; 547 return 0;
548} 548}
549 549
550/* ARGUSED1 */ 550/* ARGUSED1 */
551int 551int
552linux_sys_gettid(struct lwp *l, const void *v, register_t *retval) 552linux_sys_gettid(struct lwp *l, const void *v, register_t *retval)
553{ 553{
554 /* The Linux kernel does it exactly that way */ 554 /* The Linux kernel does it exactly that way */
555 *retval = l->l_proc->p_pid; 555 *retval = l->l_proc->p_pid;
556 return 0; 556 return 0;
557} 557}
558 558
559#ifdef LINUX_NPTL 559#ifdef LINUX_NPTL
560/* ARGUSED1 */ 560/* ARGUSED1 */
561int 561int
562linux_sys_getpid(struct lwp *l, const void *v, register_t *retval) 562linux_sys_getpid(struct lwp *l, const void *v, register_t *retval)
563{ 563{
564 struct linux_emuldata *led = l->l_proc->p_emuldata; 564 struct linux_emuldata *led = l->l_proc->p_emuldata;
565 565
566 if (led->s->flags & LINUX_LES_USE_NPTL) { 566 if (led->s->flags & LINUX_LES_USE_NPTL) {
567 /* The Linux kernel does it exactly that way */ 567 /* The Linux kernel does it exactly that way */
568 *retval = led->s->group_pid; 568 *retval = led->s->group_pid;
569 } else { 569 } else {
570 *retval = l->l_proc->p_pid; 570 *retval = l->l_proc->p_pid;
571 } 571 }
572 572
573 return 0; 573 return 0;
574} 574}
575 575
576/* ARGUSED1 */ 576/* ARGUSED1 */
577int 577int
578linux_sys_getppid(struct lwp *l, const void *v, register_t *retval) 578linux_sys_getppid(struct lwp *l, const void *v, register_t *retval)
579{ 579{
580 struct proc *p = l->l_proc; 580 struct proc *p = l->l_proc;
581 struct linux_emuldata *led = p->p_emuldata; 581 struct linux_emuldata *led = p->p_emuldata;
582 struct proc *glp; 582 struct proc *glp;
583 struct proc *pp; 583 struct proc *pp;
584 584
585 mutex_enter(proc_lock); 585 mutex_enter(proc_lock);
586 if (led->s->flags & LINUX_LES_USE_NPTL) { 586 if (led->s->flags & LINUX_LES_USE_NPTL) {
587 587
588 /* Find the thread group leader's parent */ 588 /* Find the thread group leader's parent */
589 if ((glp = p_find(led->s->group_pid, PFIND_LOCKED)) == NULL) { 589 if ((glp = p_find(led->s->group_pid, PFIND_LOCKED)) == NULL) {
590 /* Maybe panic... */ 590 /* Maybe panic... */
591 printf("linux_sys_getppid: missing group leader PID" 591 printf("linux_sys_getppid: missing group leader PID"
592 " %d\n", led->s->group_pid);  592 " %d\n", led->s->group_pid);
593 mutex_exit(proc_lock); 593 mutex_exit(proc_lock);
594 return -1; 594 return -1;
595 } 595 }
596 pp = glp->p_pptr; 596 pp = glp->p_pptr;
597 597
598 /* If this is a Linux process too, return thread group PID */ 598 /* If this is a Linux process too, return thread group PID */
599 if (pp->p_emul == p->p_emul) { 599 if (pp->p_emul == p->p_emul) {
600 struct linux_emuldata *pled; 600 struct linux_emuldata *pled;
601 601
602 pled = pp->p_emuldata; 602 pled = pp->p_emuldata;
603 *retval = pled->s->group_pid; 603 *retval = pled->s->group_pid;
604 } else { 604 } else {
605 *retval = pp->p_pid; 605 *retval = pp->p_pid;
606 } 606 }
607 607
608 } else { 608 } else {
609 *retval = p->p_pptr->p_pid; 609 *retval = p->p_pptr->p_pid;
610 } 610 }
611 mutex_exit(proc_lock); 611 mutex_exit(proc_lock);
612 612
613 return 0; 613 return 0;
614} 614}
615#endif /* LINUX_NPTL */ 615#endif /* LINUX_NPTL */
616 616
617int 617int
618linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval) 618linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval)
619{ 619{
620 /* { 620 /* {
621 syscallarg(pid_t) pid; 621 syscallarg(pid_t) pid;
622 syscallarg(unsigned int) len; 622 syscallarg(unsigned int) len;
623 syscallarg(unsigned long *) mask; 623 syscallarg(unsigned long *) mask;
624 } */ 624 } */
625 int error; 625 int error;
626 int ret; 626 int ret;
627 char *data; 627 char *data;
628 int *retp; 628 int *retp;
629 629
630 if (SCARG(uap, mask) == NULL) 630 if (SCARG(uap, mask) == NULL)
631 return EINVAL; 631 return EINVAL;
632 632
633 if (SCARG(uap, len) < sizeof(int)) 633 if (SCARG(uap, len) < sizeof(int))
634 return EINVAL; 634 return EINVAL;
635 635
636 if (pfind(SCARG(uap, pid)) == NULL) 636 if (pfind(SCARG(uap, pid)) == NULL)
637 return ESRCH; 637 return ESRCH;
638 638
639 /*  639 /*
640 * return the actual number of CPU, tag all of them as available  640 * return the actual number of CPU, tag all of them as available
641 * The result is a mask, the first CPU being in the least significant 641 * The result is a mask, the first CPU being in the least significant
642 * bit. 642 * bit.
643 */ 643 */
644 ret = (1 << ncpu) - 1; 644 ret = (1 << ncpu) - 1;
645 data = malloc(SCARG(uap, len), M_TEMP, M_WAITOK|M_ZERO); 645 data = malloc(SCARG(uap, len), M_TEMP, M_WAITOK|M_ZERO);
646 retp = (int *)&data[SCARG(uap, len) - sizeof(ret)]; 646 retp = (int *)&data[SCARG(uap, len) - sizeof(ret)];
647 *retp = ret; 647 *retp = ret;
648 648
649 if ((error = copyout(data, SCARG(uap, mask), SCARG(uap, len))) != 0) 649 error = copyout(data, SCARG(uap, mask), SCARG(uap, len));
650 return error; 
651 650
652 free(data, M_TEMP); 651 free(data, M_TEMP);
653 652
654 return 0; 653 return error;
655 654
656} 655}
657 656
658int 657int
659linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval) 658linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval)
660{ 659{
661 /* { 660 /* {
662 syscallarg(pid_t) pid; 661 syscallarg(pid_t) pid;
663 syscallarg(unsigned int) len; 662 syscallarg(unsigned int) len;
664 syscallarg(unsigned long *) mask; 663 syscallarg(unsigned long *) mask;
665 } */ 664 } */
666 665
667 if (pfind(SCARG(uap, pid)) == NULL) 666 if (pfind(SCARG(uap, pid)) == NULL)
668 return ESRCH; 667 return ESRCH;
669 668
670 /* Let's ignore it */ 669 /* Let's ignore it */
671#ifdef DEBUG_LINUX 670#ifdef DEBUG_LINUX
672 printf("linux_sys_sched_setaffinity\n"); 671 printf("linux_sys_sched_setaffinity\n");
673#endif 672#endif
674 return 0; 673 return 0;
675}; 674};
676#endif /* LINUX_NPTL */ 675#endif /* LINUX_NPTL */