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