sys_lwp.c revision 1.1.2.3 1 /* $NetBSD: sys_lwp.c,v 1.1.2.3 2006/11/17 16:34:37 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams, and Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Lightweight process (LWP) system calls. See kern_lwp.c for a description
41 * of LWPs.
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.1.2.3 2006/11/17 16:34:37 ad Exp $");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/pool.h>
50 #include <sys/proc.h>
51 #include <sys/sa.h>
52 #include <sys/savar.h>
53 #include <sys/types.h>
54 #include <sys/syscallargs.h>
55 #include <sys/kauth.h>
56
57 #include <uvm/uvm_extern.h>
58
59 /* ARGSUSED */
60 int
61 sys__lwp_create(struct lwp *l, void *v, register_t *retval)
62 {
63 struct sys__lwp_create_args /* {
64 syscallarg(const ucontext_t *) ucp;
65 syscallarg(u_long) flags;
66 syscallarg(lwpid_t *) new_lwp;
67 } */ *uap = v;
68 struct proc *p = l->l_proc;
69 struct lwp *l2;
70 vaddr_t uaddr;
71 boolean_t inmem;
72 ucontext_t *newuc;
73 int error, lid;
74
75 mutex_enter(&p->p_smutex);
76 if ((p->p_sflag & (PS_SA | PS_WEXIT)) != 0 || p->p_sa != NULL) {
77 mutex_exit(&p->p_smutex);
78 return EINVAL;
79 }
80 p->p_sflag |= PS_NOSA;
81 mutex_exit(&p->p_smutex);
82
83 newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
84
85 error = copyin(SCARG(uap, ucp), newuc,
86 l->l_proc->p_emul->e_sa->sae_ucsize);
87 if (error) {
88 pool_put(&lwp_uc_pool, newuc);
89 return (error);
90 }
91
92 /* XXX check against resource limits */
93
94 inmem = uvm_uarea_alloc(&uaddr);
95 if (__predict_false(uaddr == 0)) {
96 pool_put(&lwp_uc_pool, newuc);
97 return (ENOMEM);
98 }
99
100 /* XXX flags:
101 * __LWP_ASLWP is probably needed for Solaris compat.
102 */
103
104 newlwp(l, p, uaddr, inmem,
105 SCARG(uap, flags) & LWP_DETACHED,
106 NULL, 0, startlwp, newuc, &l2);
107
108 mutex_enter(&p->p_smutex);
109 lwp_lock(l2);
110 lid = l2->l_lid;
111 if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
112 (l->l_flag & L_WREBOOT) == 0) {
113 p->p_nrlwps++;
114 lwp_relock(l2, &sched_mutex);
115 l2->l_stat = LSRUN;
116 setrunqueue(l2);
117 } else
118 l2->l_stat = LSSUSPENDED;
119 lwp_unlock(l2);
120 mutex_exit(&p->p_smutex);
121
122 error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
123 if (error) {
124 /* XXX We should destroy the LWP. */
125 return (error);
126 }
127
128 return (0);
129 }
130
131 int
132 sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
133 {
134
135 return lwp_exit(l, 1);
136 }
137
138 int
139 sys__lwp_self(struct lwp *l, void *v, register_t *retval)
140 {
141
142 *retval = l->l_lid;
143
144 return (0);
145 }
146
147 int
148 sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
149 {
150
151 mb_read();
152 *retval = (uintptr_t) l->l_private;
153
154 return (0);
155 }
156
157 int
158 sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
159 {
160 struct sys__lwp_setprivate_args /* {
161 syscallarg(void *) ptr;
162 } */ *uap = v;
163
164 l->l_private = SCARG(uap, ptr);
165 mb_write();
166
167 return (0);
168 }
169
170 int
171 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
172 {
173 struct sys__lwp_suspend_args /* {
174 syscallarg(lwpid_t) target;
175 } */ *uap = v;
176 struct proc *p = l->l_proc;
177 struct lwp *t;
178 int error;
179
180 mutex_enter(&p->p_smutex);
181
182 if ((p->p_flag & P_SA) != 0 || p->p_sa != NULL) {
183 mutex_exit(&p->p_smutex);
184 return EINVAL;
185 }
186
187 if ((t = lwp_byid(p, SCARG(uap, target))) == NULL) {
188 mutex_exit(&p->p_smutex);
189 return (ESRCH);
190 }
191
192 /*
193 * Check for deadlock, which is only possible when we're suspending
194 * ourself.
195 */
196 if (t == l && l->l_stat == LSONPROC && p->p_nrlwps == 1) {
197 lwp_unlock(l);
198 mutex_exit(&p->p_smutex);
199 return (EDEADLK);
200 }
201
202 /*
203 * Suspend the LWP. If it's on a different CPU, we need to wait for
204 * it to be preempted, where it will put itself to sleep. If not
205 * suspending ourselves, the LWP will be returned unlocked.
206 */
207 error = lwp_halt(l, t, LSSUSPENDED);
208 mutex_exit(&p->p_smutex);
209
210 /*
211 * If we suspended ourself, we need to sleep now.
212 */
213 if (t == l && !error) {
214 lwp_lock(l);
215 l->l_nvcsw++;
216 mi_switch(t, NULL);
217 }
218
219 return (error);
220 }
221
222 int
223 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
224 {
225 struct sys__lwp_continue_args /* {
226 syscallarg(lwpid_t) target;
227 } */ *uap = v;
228 int error;
229 struct proc *p = l->l_proc;
230 struct lwp *t;
231
232 error = 0;
233
234 mutex_enter(&p->p_smutex);
235
236 if ((p->p_flag & P_SA) != 0 || p->p_sa != NULL)
237 error = EINVAL;
238 else if ((t = lwp_byid(p, SCARG(uap, target))) == NULL)
239 error = ESRCH;
240 else if (t == l || t->l_stat != LSSUSPENDED)
241 lwp_unlock(t);
242 else
243 lwp_continue(t);
244
245 mutex_exit(&p->p_smutex);
246
247 return (error);
248 }
249
250 int
251 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
252 {
253 struct sys__lwp_wakeup_args /* {
254 syscallarg(lwpid_t) target;
255 } */ *uap = v;
256 struct lwp *t;
257 struct proc *p;
258 int error;
259
260 p = l->l_proc;
261 mutex_enter(&p->p_smutex);
262
263 if ((t = lwp_byid(p, SCARG(uap, target))) == NULL) {
264 mutex_exit(&p->p_smutex);
265 return ESRCH;
266 }
267
268 if (t->l_stat != LSSLEEP) {
269 error = ENODEV;
270 goto bad;
271 }
272
273 if ((t->l_flag & L_SINTR) == 0) {
274 error = EBUSY;
275 goto bad;
276 }
277
278 /* wake it up setrunnable() will release the LWP lock. */
279 t->l_flag |= L_CANCELLED;
280 setrunnable(t);
281 mutex_exit(&p->p_smutex);
282 return 0;
283
284 bad:
285 lwp_unlock(l);
286 mutex_exit(&p->p_smutex);
287 return error;
288 }
289
290 int
291 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
292 {
293 struct sys__lwp_wait_args /* {
294 syscallarg(lwpid_t) wait_for;
295 syscallarg(lwpid_t *) departed;
296 } */ *uap = v;
297 struct proc *p = l->l_proc;
298 int error;
299 lwpid_t dep;
300
301 mutex_enter(&p->p_smutex);
302 error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
303 mutex_exit(&p->p_smutex);
304 if (error)
305 return (error);
306
307 if (SCARG(uap, departed)) {
308 error = copyout(&dep, SCARG(uap, departed),
309 sizeof(dep));
310 if (error)
311 return (error);
312 }
313
314 return (0);
315 }
316
317 /* ARGSUSED */
318 int
319 sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
320 {
321 struct sys__lwp_kill_args /* {
322 syscallarg(lwpid_t) target;
323 syscallarg(int) signo;
324 } */ *uap = v;
325 struct proc *p = l->l_proc;
326 struct lwp *t;
327 ksiginfo_t ksi;
328 int signo = SCARG(uap, signo);
329 int error;
330
331 if ((u_int)signo >= NSIG)
332 return (EINVAL);
333
334 KSI_INIT(&ksi);
335 ksi.ksi_signo = signo;
336 ksi.ksi_code = SI_USER;
337 ksi.ksi_pid = p->p_pid;
338 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
339 ksi.ksi_lid = SCARG(uap, target);
340
341 rw_enter(&proclist_lock, RW_READER);
342 mutex_enter(&p->p_smutex);
343 if ((t = lwp_byid(p, ksi.ksi_lid)) == NULL)
344 error = ESRCH;
345 else {
346 lwp_unlock(t);
347 kpsignal2(p, &ksi);
348 error = 0;
349 }
350 mutex_exit(&p->p_smutex);
351 rw_exit(&proclist_lock);
352
353 return (error);
354 }
355