sys_lwp.c revision 1.19 1 /* $NetBSD: sys_lwp.c,v 1.19 2007/05/17 14:51:41 yamt 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.19 2007/05/17 14:51:41 yamt 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, l2->l_cpu->ci_schedstate.spc_mutex));
132 p->p_nrlwps++;
133 l2->l_stat = LSRUN;
134 sched_enqueue(l2, false);
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 lwp_lock(l);
466 if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
467 l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
468 lwp_unlock(l);
469 sleepq_unlock(sq);
470 return EALREADY;
471 }
472 lwp_unlock_to(l, sq->sq_mutex);
473
474 KERNEL_UNLOCK_ALL(l, &l->l_biglocks); /* XXX for compat32 */
475 sleepq_enqueue(sq, sched_kpri(l), wchan, "parked", &lwp_park_sobj);
476 error = sleepq_block(timo, true);
477 switch (error) {
478 case EWOULDBLOCK:
479 error = ETIMEDOUT;
480 break;
481 case ERESTART:
482 error = EINTR;
483 break;
484 default:
485 /* nothing */
486 break;
487 }
488 return error;
489 }
490
491 int
492 sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
493 {
494 struct sys__lwp_unpark_args /* {
495 syscallarg(lwpid_t) target;
496 syscallarg(const void *) hint;
497 } */ *uap = v;
498 struct proc *p;
499 struct lwp *t;
500 sleepq_t *sq;
501 lwpid_t target;
502 wchan_t wchan;
503 int swapin;
504
505 p = l->l_proc;
506 target = SCARG(uap, target);
507
508 /*
509 * Easy case: search for the LWP on the sleep queue. If
510 * it's parked, remove it from the queue and set running.
511 */
512 wchan = lwp_park_wchan(p, SCARG(uap, hint));
513 sq = sleeptab_lookup(&lwp_park_tab, wchan);
514
515 TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
516 if (t->l_proc == p && t->l_lid == target)
517 break;
518
519 if (__predict_true(t != NULL)) {
520 swapin = sleepq_remove(sq, t);
521 sleepq_unlock(sq);
522 if (swapin)
523 uvm_kick_scheduler();
524 return 0;
525 }
526
527 /*
528 * The LWP hasn't parked yet. Take the hit and mark the
529 * operation as pending.
530 */
531 sleepq_unlock(sq);
532 mutex_enter(&p->p_smutex);
533 if ((t = lwp_find(p, target)) == NULL) {
534 mutex_exit(&p->p_smutex);
535 return ESRCH;
536 }
537 lwp_lock(t);
538
539 /*
540 * It may not have parked yet, we may have raced, or it
541 * is parked on a different user sync object.
542 */
543 if (t->l_syncobj == &lwp_park_sobj) {
544 /* Releases the LWP lock. */
545 lwp_unsleep(t);
546 } else {
547 /*
548 * Set the operation pending. The next call to _lwp_park
549 * will return early.
550 */
551 t->l_flag |= LW_UNPARKED;
552 lwp_unlock(t);
553 }
554
555 mutex_exit(&p->p_smutex);
556 return 0;
557 }
558
559 int
560 sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
561 {
562 struct sys__lwp_unpark_all_args /* {
563 syscallarg(const lwpid_t *) targets;
564 syscallarg(size_t) ntargets;
565 syscallarg(const void *) hint;
566 } */ *uap = v;
567 struct proc *p;
568 struct lwp *t;
569 sleepq_t *sq;
570 wchan_t wchan;
571 lwpid_t targets[32], *tp, *tpp, *tmax, target;
572 int swapin, error;
573 u_int ntargets;
574 size_t sz;
575
576 p = l->l_proc;
577 ntargets = SCARG(uap, ntargets);
578
579 if (SCARG(uap, targets) == NULL) {
580 /*
581 * Let the caller know how much we are willing to do, and
582 * let it unpark the LWPs in blocks.
583 */
584 *retval = LWP_UNPARK_MAX;
585 return 0;
586 }
587 if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
588 return EINVAL;
589
590 /*
591 * Copy in the target array. If it's a small number of LWPs, then
592 * place the numbers on the stack.
593 */
594 sz = sizeof(target) * ntargets;
595 if (sz <= sizeof(targets))
596 tp = targets;
597 else {
598 KERNEL_LOCK(1, l); /* XXXSMP */
599 tp = kmem_alloc(sz, KM_SLEEP);
600 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
601 if (tp == NULL)
602 return ENOMEM;
603 }
604 error = copyin(SCARG(uap, targets), tp, sz);
605 if (error != 0) {
606 if (tp != targets) {
607 KERNEL_LOCK(1, l); /* XXXSMP */
608 kmem_free(tp, sz);
609 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
610 }
611 return error;
612 }
613
614 swapin = 0;
615 wchan = lwp_park_wchan(p, SCARG(uap, hint));
616 sq = sleeptab_lookup(&lwp_park_tab, wchan);
617
618 for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
619 target = *tpp;
620
621 /*
622 * Easy case: search for the LWP on the sleep queue. If
623 * it's parked, remove it from the queue and set running.
624 */
625 TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
626 if (t->l_proc == p && t->l_lid == target)
627 break;
628
629 if (t != NULL) {
630 swapin |= sleepq_remove(sq, t);
631 continue;
632 }
633
634 /*
635 * The LWP hasn't parked yet. Take the hit and
636 * mark the operation as pending.
637 */
638 sleepq_unlock(sq);
639 mutex_enter(&p->p_smutex);
640 if ((t = lwp_find(p, target)) == NULL) {
641 mutex_exit(&p->p_smutex);
642 sleepq_lock(sq);
643 continue;
644 }
645 lwp_lock(t);
646
647 /*
648 * It may not have parked yet, we may have raced, or
649 * it is parked on a different user sync object.
650 */
651 if (t->l_syncobj == &lwp_park_sobj) {
652 /* Releases the LWP lock. */
653 lwp_unsleep(t);
654 } else {
655 /*
656 * Set the operation pending. The next call to
657 * _lwp_park will return early.
658 */
659 t->l_flag |= LW_UNPARKED;
660 lwp_unlock(t);
661 }
662
663 mutex_exit(&p->p_smutex);
664 sleepq_lock(sq);
665 }
666
667 sleepq_unlock(sq);
668 if (tp != targets) {
669 KERNEL_LOCK(1, l); /* XXXSMP */
670 kmem_free(tp, sz);
671 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
672 }
673 if (swapin)
674 uvm_kick_scheduler();
675
676 return 0;
677 }
678