sys_lwp.c revision 1.16 1 /* $NetBSD: sys_lwp.c,v 1.16 2007/03/20 23:25:17 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2006, 2007 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.16 2007/03/20 23:25:17 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/types.h>
52 #include <sys/syscallargs.h>
53 #include <sys/kauth.h>
54 #include <sys/kmem.h>
55 #include <sys/sleepq.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #define LWP_UNPARK_MAX 1024
60
61 syncobj_t lwp_park_sobj = {
62 SOBJ_SLEEPQ_FIFO,
63 sleepq_unsleep,
64 sleepq_changepri,
65 sleepq_lendpri,
66 syncobj_noowner,
67 };
68
69 sleeptab_t lwp_park_tab;
70
71 void
72 lwp_sys_init(void)
73 {
74 sleeptab_init(&lwp_park_tab);
75 }
76
77 /* ARGSUSED */
78 int
79 sys__lwp_create(struct lwp *l, void *v, register_t *retval)
80 {
81 struct sys__lwp_create_args /* {
82 syscallarg(const ucontext_t *) ucp;
83 syscallarg(u_long) flags;
84 syscallarg(lwpid_t *) new_lwp;
85 } */ *uap = v;
86 struct proc *p = l->l_proc;
87 struct lwp *l2;
88 vaddr_t uaddr;
89 bool inmem;
90 ucontext_t *newuc;
91 int error, lid;
92
93 newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
94
95 error = copyin(SCARG(uap, ucp), newuc, p->p_emul->e_ucsize);
96 if (error) {
97 pool_put(&lwp_uc_pool, newuc);
98 return error;
99 }
100
101 /* XXX check against resource limits */
102
103 inmem = uvm_uarea_alloc(&uaddr);
104 if (__predict_false(uaddr == 0)) {
105 pool_put(&lwp_uc_pool, newuc);
106 return ENOMEM;
107 }
108
109 newlwp(l, p, uaddr, inmem,
110 SCARG(uap, flags) & LWP_DETACHED,
111 NULL, 0, p->p_emul->e_startlwp, newuc, &l2);
112
113 /*
114 * Set the new LWP running, unless the caller has requested that
115 * it be created in suspended state. If the process is stopping,
116 * then the LWP is created stopped.
117 */
118 mutex_enter(&p->p_smutex);
119 lwp_lock(l2);
120 lid = l2->l_lid;
121 if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
122 (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
123 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0)
124 l2->l_stat = LSSTOP;
125 else {
126 KASSERT(lwp_locked(l2, &sched_mutex));
127 p->p_nrlwps++;
128 l2->l_stat = LSRUN;
129 setrunqueue(l2);
130 }
131 } else
132 l2->l_stat = LSSUSPENDED;
133 lwp_unlock(l2);
134 mutex_exit(&p->p_smutex);
135
136 error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
137 if (error)
138 return error;
139
140 return 0;
141 }
142
143 int
144 sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
145 {
146
147 lwp_exit(l);
148 return 0;
149 }
150
151 int
152 sys__lwp_self(struct lwp *l, void *v, register_t *retval)
153 {
154
155 *retval = l->l_lid;
156 return 0;
157 }
158
159 int
160 sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
161 {
162
163 *retval = (uintptr_t)l->l_private;
164 return 0;
165 }
166
167 int
168 sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
169 {
170 struct sys__lwp_setprivate_args /* {
171 syscallarg(void *) ptr;
172 } */ *uap = v;
173
174 l->l_private = SCARG(uap, ptr);
175 return 0;
176 }
177
178 int
179 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
180 {
181 struct sys__lwp_suspend_args /* {
182 syscallarg(lwpid_t) target;
183 } */ *uap = v;
184 struct proc *p = l->l_proc;
185 struct lwp *t;
186 int error;
187
188 mutex_enter(&p->p_smutex);
189 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
190 mutex_exit(&p->p_smutex);
191 return ESRCH;
192 }
193
194 /*
195 * Check for deadlock, which is only possible when we're suspending
196 * ourself. XXX There is a short race here, as p_nrlwps is only
197 * incremented when an LWP suspends itself on the kernel/user
198 * boundary. It's still possible to kill -9 the process so we
199 * don't bother checking further.
200 */
201 lwp_lock(t);
202 if ((t == l && p->p_nrlwps == 1) ||
203 (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
204 lwp_unlock(t);
205 mutex_exit(&p->p_smutex);
206 return EDEADLK;
207 }
208
209 /*
210 * Suspend the LWP. XXX If it's on a different CPU, we should wait
211 * for it to be preempted, where it will put itself to sleep.
212 *
213 * Suspension of the current LWP will happen on return to userspace.
214 */
215 error = lwp_suspend(l, t);
216 mutex_exit(&p->p_smutex);
217
218 return error;
219 }
220
221 int
222 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
223 {
224 struct sys__lwp_continue_args /* {
225 syscallarg(lwpid_t) target;
226 } */ *uap = v;
227 int error;
228 struct proc *p = l->l_proc;
229 struct lwp *t;
230
231 error = 0;
232
233 mutex_enter(&p->p_smutex);
234 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
235 mutex_exit(&p->p_smutex);
236 return ESRCH;
237 }
238
239 lwp_lock(t);
240 lwp_continue(t);
241 mutex_exit(&p->p_smutex);
242
243 return error;
244 }
245
246 int
247 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
248 {
249 struct sys__lwp_wakeup_args /* {
250 syscallarg(lwpid_t) target;
251 } */ *uap = v;
252 struct lwp *t;
253 struct proc *p;
254 int error;
255
256 p = l->l_proc;
257 mutex_enter(&p->p_smutex);
258
259 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
260 mutex_exit(&p->p_smutex);
261 return ESRCH;
262 }
263
264 lwp_lock(t);
265 t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
266
267 if (t->l_stat != LSSLEEP) {
268 lwp_unlock(t);
269 error = ENODEV;
270 } else if ((t->l_flag & LW_SINTR) == 0) {
271 lwp_unlock(t);
272 error = EBUSY;
273 } else {
274 /* Wake it up. lwp_unsleep() will release the LWP lock. */
275 lwp_unsleep(t);
276 error = 0;
277 }
278
279 mutex_exit(&p->p_smutex);
280
281 return error;
282 }
283
284 int
285 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
286 {
287 struct sys__lwp_wait_args /* {
288 syscallarg(lwpid_t) wait_for;
289 syscallarg(lwpid_t *) departed;
290 } */ *uap = v;
291 struct proc *p = l->l_proc;
292 int error;
293 lwpid_t dep;
294
295 mutex_enter(&p->p_smutex);
296 error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
297 mutex_exit(&p->p_smutex);
298
299 if (error)
300 return error;
301
302 if (SCARG(uap, departed)) {
303 error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
304 if (error)
305 return error;
306 }
307
308 return 0;
309 }
310
311 /* ARGSUSED */
312 int
313 sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
314 {
315 struct sys__lwp_kill_args /* {
316 syscallarg(lwpid_t) target;
317 syscallarg(int) signo;
318 } */ *uap = v;
319 struct proc *p = l->l_proc;
320 struct lwp *t;
321 ksiginfo_t ksi;
322 int signo = SCARG(uap, signo);
323 int error = 0;
324
325 if ((u_int)signo >= NSIG)
326 return EINVAL;
327
328 KSI_INIT(&ksi);
329 ksi.ksi_signo = signo;
330 ksi.ksi_code = SI_USER;
331 ksi.ksi_pid = p->p_pid;
332 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
333 ksi.ksi_lid = SCARG(uap, target);
334
335 mutex_enter(&proclist_mutex);
336 mutex_enter(&p->p_smutex);
337 if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
338 error = ESRCH;
339 else if (signo != 0)
340 kpsignal2(p, &ksi);
341 mutex_exit(&p->p_smutex);
342 mutex_exit(&proclist_mutex);
343
344 return error;
345 }
346
347 int
348 sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
349 {
350 struct sys__lwp_detach_args /* {
351 syscallarg(lwpid_t) target;
352 } */ *uap = v;
353 struct proc *p;
354 struct lwp *t;
355 lwpid_t target;
356 int error;
357
358 target = SCARG(uap, target);
359 p = l->l_proc;
360
361 mutex_enter(&p->p_smutex);
362
363 if (l->l_lid == target)
364 t = l;
365 else {
366 /*
367 * We can't use lwp_find() here because the target might
368 * be a zombie.
369 */
370 LIST_FOREACH(t, &p->p_lwps, l_sibling)
371 if (t->l_lid == target)
372 break;
373 }
374
375 /*
376 * If the LWP is already detached, there's nothing to do.
377 * If it's a zombie, we need to clean up after it. LSZOMB
378 * is visible with the proc mutex held.
379 *
380 * After we have detached or released the LWP, kick any
381 * other LWPs that may be sitting in _lwp_wait(), waiting
382 * for the target LWP to exit.
383 */
384 if (t != NULL && t->l_stat != LSIDL) {
385 if ((t->l_prflag & LPR_DETACHED) == 0) {
386 p->p_ndlwps++;
387 t->l_prflag |= LPR_DETACHED;
388 if (t->l_stat == LSZOMB) {
389 cv_broadcast(&p->p_lwpcv);
390 lwp_free(t, 0, 0); /* releases proc mutex */
391 return 0;
392 }
393 error = 0;
394 } else
395 error = EINVAL;
396 } else
397 error = ESRCH;
398
399 cv_broadcast(&p->p_lwpcv);
400 mutex_exit(&p->p_smutex);
401
402 return error;
403 }
404
405 static inline wchan_t
406 lwp_park_wchan(struct proc *p, const void *hint)
407 {
408 return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
409 }
410
411 /*
412 * 'park' an LWP waiting on a user-level synchronisation object. The LWP
413 * will remain parked until another LWP in the same process calls in and
414 * requests that it be unparked.
415 */
416 int
417 sys__lwp_park(struct lwp *l, void *v, register_t *retval)
418 {
419 struct sys__lwp_park_args /* {
420 syscallarg(const struct timespec *) ts;
421 syscallarg(ucontext_t *) uc;
422 syscallarg(const void *) hint;
423 } */ *uap = v;
424 const struct timespec *tsp;
425 struct timespec ts, tsx;
426 struct timeval tv;
427 sleepq_t *sq;
428 wchan_t wchan;
429 int timo, error;
430
431 /* Fix up the given timeout value. */
432 if ((tsp = SCARG(uap, ts)) != NULL) {
433 if ((error = copyin(tsp, &ts, sizeof(ts))) != 0)
434 return error;
435 getnanotime(&tsx);
436 timespecsub(&ts, &tsx, &ts);
437 tv.tv_sec = ts.tv_sec;
438 tv.tv_usec = ts.tv_nsec / 1000;
439 if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0))
440 return ETIMEDOUT;
441 if ((error = itimerfix(&tv)) != 0)
442 return error;
443 timo = tvtohz(&tv);
444 } else
445 timo = 0;
446
447 /* Find and lock the sleep queue. */
448 wchan = lwp_park_wchan(l->l_proc, SCARG(uap, hint));
449 sq = sleeptab_lookup(&lwp_park_tab, wchan);
450
451 /*
452 * Before going the full route and blocking, check to see if an
453 * unpark op is pending.
454 */
455 sleepq_lwp_lock(l);
456 if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
457 l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
458 sleepq_lwp_unlock(l);
459 sleepq_unlock(sq);
460 return EALREADY;
461 }
462 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
463 lwp_unlock_to(l, sq->sq_mutex);
464 #endif
465
466 /*
467 * For now we ignore the ucontext argument. In the future, we may
468 * put our stack up to be recycled. If it's binned, a trampoline
469 * function could call sleepq_unblock() on our behalf.
470 */
471 KERNEL_UNLOCK_ALL(l, &l->l_biglocks); /* XXX for compat32 */
472 sleepq_block(sq, sched_kpri(l), wchan, "parked", timo, 1,
473 &lwp_park_sobj);
474 error = sleepq_unblock(timo, 1);
475 switch (error) {
476 case EWOULDBLOCK:
477 error = ETIMEDOUT;
478 break;
479 case ERESTART:
480 error = EINTR;
481 break;
482 default:
483 /* nothing */
484 break;
485 }
486 return error;
487 }
488
489 int
490 sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
491 {
492 struct sys__lwp_unpark_args /* {
493 syscallarg(lwpid_t) target;
494 syscallarg(const void *) hint;
495 } */ *uap = v;
496 struct proc *p;
497 struct lwp *t;
498 sleepq_t *sq;
499 lwpid_t target;
500 wchan_t wchan;
501 int swapin;
502
503 p = l->l_proc;
504 target = SCARG(uap, target);
505
506 /*
507 * Easy case: search for the LWP on the sleep queue. If
508 * it's parked, remove it from the queue and set running.
509 */
510 wchan = lwp_park_wchan(p, SCARG(uap, hint));
511 sq = sleeptab_lookup(&lwp_park_tab, wchan);
512
513 TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
514 if (t->l_proc == p && t->l_lid == target)
515 break;
516
517 if (__predict_true(t != NULL)) {
518 swapin = sleepq_remove(sq, t);
519 sleepq_unlock(sq);
520 if (swapin)
521 uvm_kick_scheduler();
522 return 0;
523 }
524
525 /*
526 * The LWP hasn't parked yet. Take the hit and mark the
527 * operation as pending.
528 */
529 sleepq_unlock(sq);
530 mutex_enter(&p->p_smutex);
531 if ((t = lwp_find(p, target)) == NULL) {
532 mutex_exit(&p->p_smutex);
533 return ESRCH;
534 }
535 lwp_lock(t);
536
537 /*
538 * It may not have parked yet, we may have raced, or it
539 * is parked on a different user sync object.
540 */
541 if (t->l_syncobj == &lwp_park_sobj) {
542 /* Releases the LWP lock. */
543 lwp_unsleep(t);
544 } else {
545 /*
546 * Set the operation pending. The next call to _lwp_park
547 * will return early.
548 */
549 t->l_flag |= LW_UNPARKED;
550 lwp_unlock(t);
551 }
552
553 mutex_exit(&p->p_smutex);
554 return 0;
555 }
556
557 int
558 sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
559 {
560 struct sys__lwp_unpark_all_args /* {
561 syscallarg(const lwpid_t *) targets;
562 syscallarg(size_t) ntargets;
563 syscallarg(const void *) hint;
564 } */ *uap = v;
565 struct proc *p;
566 struct lwp *t;
567 sleepq_t *sq;
568 wchan_t wchan;
569 lwpid_t targets[32], *tp, *tpp, *tmax, target;
570 int swapin, error;
571 u_int ntargets;
572 size_t sz;
573
574 p = l->l_proc;
575 ntargets = SCARG(uap, ntargets);
576
577 if (SCARG(uap, targets) == NULL) {
578 /*
579 * Let the caller know how much we are willing to do, and
580 * let it unpark the LWPs in blocks.
581 */
582 *retval = LWP_UNPARK_MAX;
583 return 0;
584 }
585 if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
586 return EINVAL;
587
588 /*
589 * Copy in the target array. If it's a small number of LWPs, then
590 * place the numbers on the stack.
591 */
592 sz = sizeof(target) * ntargets;
593 if (sz <= sizeof(targets))
594 tp = targets;
595 else {
596 KERNEL_LOCK(1, l); /* XXXSMP */
597 tp = kmem_alloc(sz, KM_SLEEP);
598 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
599 if (tp == NULL)
600 return ENOMEM;
601 }
602 error = copyin(SCARG(uap, targets), tp, sz);
603 if (error != 0) {
604 if (tp != targets) {
605 KERNEL_LOCK(1, l); /* XXXSMP */
606 kmem_free(tp, sz);
607 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
608 }
609 return error;
610 }
611
612 swapin = 0;
613 wchan = lwp_park_wchan(p, SCARG(uap, hint));
614 sq = sleeptab_lookup(&lwp_park_tab, wchan);
615
616 for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
617 target = *tpp;
618
619 /*
620 * Easy case: search for the LWP on the sleep queue. If
621 * it's parked, remove it from the queue and set running.
622 */
623 TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
624 if (t->l_proc == p && t->l_lid == target)
625 break;
626
627 if (t != NULL) {
628 swapin |= sleepq_remove(sq, t);
629 continue;
630 }
631
632 /*
633 * The LWP hasn't parked yet. Take the hit and
634 * mark the operation as pending.
635 */
636 sleepq_unlock(sq);
637 mutex_enter(&p->p_smutex);
638 if ((t = lwp_find(p, target)) == NULL) {
639 mutex_exit(&p->p_smutex);
640 sleepq_lock(sq);
641 continue;
642 }
643 lwp_lock(t);
644
645 /*
646 * It may not have parked yet, we may have raced, or
647 * it is parked on a different user sync object.
648 */
649 if (t->l_syncobj == &lwp_park_sobj) {
650 /* Releases the LWP lock. */
651 lwp_unsleep(t);
652 } else {
653 /*
654 * Set the operation pending. The next call to
655 * _lwp_park will return early.
656 */
657 t->l_flag |= LW_UNPARKED;
658 lwp_unlock(t);
659 }
660
661 mutex_exit(&p->p_smutex);
662 sleepq_lock(sq);
663 }
664
665 sleepq_unlock(sq);
666 if (tp != targets) {
667 KERNEL_LOCK(1, l); /* XXXSMP */
668 kmem_free(tp, sz);
669 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
670 }
671 if (swapin)
672 uvm_kick_scheduler();
673
674 return 0;
675 }
676