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