Home | History | Annotate | Line # | Download | only in kern
kern_lwp.c revision 1.1.2.24
      1 /*	$NetBSD: kern_lwp.c,v 1.1.2.24 2003/01/17 03:07:03 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 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.
      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 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/pool.h>
     42 #include <sys/lock.h>
     43 #include <sys/proc.h>
     44 #include <sys/sa.h>
     45 #include <sys/savar.h>
     46 #include <sys/types.h>
     47 #include <sys/ucontext.h>
     48 #include <sys/resourcevar.h>
     49 #include <sys/mount.h>
     50 #include <sys/syscallargs.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 struct lwplist alllwp;
     55 struct lwplist deadlwp;
     56 struct lwplist zomblwp;
     57 
     58 #define LWP_DEBUG
     59 
     60 #ifdef LWP_DEBUG
     61 int lwp_debug = 0;
     62 #define DPRINTF(x) if (lwp_debug) printf x
     63 #else
     64 #define DPRINTF(x)
     65 #endif
     66 /* ARGSUSED */
     67 int
     68 sys__lwp_create(struct lwp *l, void *v, register_t *retval)
     69 {
     70 	struct sys__lwp_create_args /* {
     71 		syscallarg(const ucontext_t *) ucp;
     72 		syscallarg(u_long) flags;
     73 		syscallarg(lwpid_t *) new_lwp;
     74 	} */ *uap = v;
     75 	struct proc *p = l->l_proc;
     76 	struct lwp *l2;
     77 	vaddr_t uaddr;
     78 	boolean_t inmem;
     79 	ucontext_t *newuc;
     80 	int s, error;
     81 
     82 	newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
     83 
     84 	error = copyin(SCARG(uap, ucp), newuc, sizeof(*newuc));
     85 	if (error)
     86 		return (error);
     87 
     88 	/* XXX check against resource limits */
     89 
     90 	inmem = uvm_uarea_alloc(&uaddr);
     91 	if (__predict_false(uaddr == 0)) {
     92 		return (ENOMEM);
     93 	}
     94 
     95 	/* XXX flags:
     96 	 * __LWP_ASLWP is probably needed for Solaris compat.
     97 	 */
     98 
     99 	newlwp(l, p, uaddr, inmem,
    100 	    SCARG(uap, flags) & LWP_DETACHED,
    101 	    NULL, NULL, startlwp, newuc, &l2);
    102 
    103 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0) {
    104 		SCHED_LOCK(s);
    105 		l2->l_stat = LSRUN;
    106 		setrunqueue(l2);
    107 		SCHED_UNLOCK(s);
    108 		simple_lock(&p->p_lwplock);
    109 		p->p_nrlwps++;
    110 		simple_unlock(&p->p_lwplock);
    111 	} else {
    112 		l2->l_stat = LSSUSPENDED;
    113 	}
    114 
    115 	error = copyout(&l2->l_lid, SCARG(uap, new_lwp),
    116 	    sizeof(l2->l_lid));
    117 	if (error)
    118 		return (error);
    119 
    120 	return (0);
    121 }
    122 
    123 
    124 int
    125 sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
    126 {
    127 
    128 	lwp_exit(l);
    129 	/* NOTREACHED */
    130 	return (0);
    131 }
    132 
    133 
    134 int
    135 sys__lwp_self(struct lwp *l, void *v, register_t *retval)
    136 {
    137 
    138 	*retval = l->l_lid;
    139 
    140 	return (0);
    141 }
    142 
    143 
    144 int
    145 sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
    146 {
    147 
    148 	*retval = (uintptr_t) l->l_private;
    149 
    150 	return (0);
    151 }
    152 
    153 
    154 int
    155 sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
    156 {
    157 	struct sys__lwp_setprivate_args /* {
    158 		syscallarg(void *) ptr;
    159 	} */ *uap = v;
    160 
    161 	l->l_private = SCARG(uap, ptr);
    162 
    163 	return (0);
    164 }
    165 
    166 
    167 int
    168 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
    169 {
    170 	struct sys__lwp_suspend_args /* {
    171 		syscallarg(lwpid_t) target;
    172 	} */ *uap = v;
    173 	int target_lid;
    174 	struct proc *p = l->l_proc;
    175 	struct lwp *t, *t2;
    176 	int s;
    177 
    178 	target_lid = SCARG(uap, target);
    179 
    180 	LIST_FOREACH(t, &p->p_lwps, l_sibling)
    181 		if (t->l_lid == target_lid)
    182 			break;
    183 
    184 	if (t == NULL)
    185 		return (ESRCH);
    186 
    187 	if (t == l) {
    188 		/*
    189 		 * Check for deadlock, which is only possible
    190 		 * when we're suspending ourself.
    191 		 */
    192 		LIST_FOREACH(t2, &p->p_lwps, l_sibling) {
    193 			if ((t2 != l) && (t2->l_stat != LSSUSPENDED))
    194 				break;
    195 		}
    196 
    197 		if (t2 == NULL) /* All other LWPs are suspended */
    198 			return (EDEADLK);
    199 
    200 		SCHED_LOCK(s);
    201 		l->l_stat = LSSUSPENDED;
    202 		/* XXX NJWLWP check if this makes sense here: */
    203 		l->l_proc->p_stats->p_ru.ru_nvcsw++;
    204 		mi_switch(l, NULL);
    205 		SCHED_ASSERT_UNLOCKED();
    206 		splx(s);
    207 	} else {
    208 		switch (t->l_stat) {
    209 		case LSSUSPENDED:
    210 			return (0); /* _lwp_suspend() is idempotent */
    211 		case LSRUN:
    212 			SCHED_LOCK(s);
    213 			remrunqueue(t);
    214 			t->l_stat = LSSUSPENDED;
    215 			SCHED_UNLOCK(s);
    216 			simple_lock(&p->p_lwplock);
    217 			p->p_nrlwps--;
    218 			simple_unlock(&p->p_lwplock);
    219 			break;
    220 		case LSSLEEP:
    221 			t->l_stat = LSSUSPENDED;
    222 			break;
    223 		case LSIDL:
    224 		case LSDEAD:
    225 		case LSZOMB:
    226 			return (EINTR); /* It's what Solaris does..... */
    227 		case LSSTOP:
    228 			panic("_lwp_suspend: Stopped LWP in running process!");
    229 			break;
    230 		case LSONPROC:
    231 			panic("XXX multiprocessor LWPs? Implement me!");
    232 			break;
    233 		}
    234 	}
    235 
    236 	return (0);
    237 }
    238 
    239 
    240 int
    241 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    242 {
    243 	struct sys__lwp_continue_args /* {
    244 		syscallarg(lwpid_t) target;
    245 	} */ *uap = v;
    246 	int target_lid;
    247 	struct proc *p = l->l_proc;
    248 	struct lwp *t;
    249 
    250 	target_lid = SCARG(uap, target);
    251 
    252 	LIST_FOREACH(t, &p->p_lwps, l_sibling)
    253 		if (t->l_lid == target_lid)
    254 			break;
    255 
    256 	if (t == NULL)
    257 		return (ESRCH);
    258 
    259 	lwp_continue(t);
    260 
    261 	return (0);
    262 }
    263 
    264 void
    265 lwp_continue(struct lwp *l)
    266 {
    267 	int s;
    268 
    269 	DPRINTF(("lwp_continue of %d.%d (%s), state %d, wchan %p\n",
    270 	    l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, l->l_stat,
    271 	    l->l_wchan));
    272 
    273 	if (l->l_stat != LSSUSPENDED)
    274 		return;
    275 
    276 	if (l->l_wchan == 0) {
    277 		/* LWP was runnable before being suspended. */
    278 		SCHED_LOCK(s);
    279 		setrunnable(l);
    280 		SCHED_UNLOCK(s);
    281 	} else {
    282 		/* LWP was sleeping before being suspended. */
    283 		l->l_stat = LSSLEEP;
    284 	}
    285 }
    286 
    287 int
    288 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
    289 {
    290 	struct sys__lwp_wakeup_args /* {
    291 		syscallarg(lwpid_t) wakeup;
    292 	} */ *uap = v;
    293 	lwpid_t target_lid;
    294 	struct lwp *t;
    295 	struct proc *p;
    296 
    297 	p = l->l_proc;
    298 	target_lid = SCARG(uap, target);
    299 
    300 	LIST_FOREACH(t, &p->p_lwps, l_sibling)
    301 		if (t->l_lid == target_lid)
    302 			break;
    303 
    304 	if (t == NULL)
    305 		return (ESRCH);
    306 
    307 	if (t->l_stat != LSSLEEP)
    308 		return (ENODEV);
    309 
    310 	if ((l->l_flag & L_SINTR) == 0)
    311 		return (EBUSY);
    312 
    313 	setrunnable(l);
    314 
    315 	return 0;
    316 }
    317 
    318 int
    319 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    320 {
    321 	struct sys__lwp_wait_args /* {
    322 		syscallarg(lwpid_t) wait_for;
    323 		syscallarg(lwpid_t *) departed;
    324 	} */ *uap = v;
    325 	int error;
    326 	lwpid_t dep;
    327 
    328 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    329 	if (error)
    330 		return (error);
    331 
    332 	if (SCARG(uap, departed)) {
    333 		error = copyout(&dep, SCARG(uap, departed),
    334 		    sizeof(dep));
    335 		if (error)
    336 			return (error);
    337 	}
    338 
    339 	return (0);
    340 }
    341 
    342 
    343 int
    344 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
    345 {
    346 
    347 	struct proc *p = l->l_proc;
    348 	struct lwp *l2, *l3;
    349 	int nfound, error, s, wpri;
    350 	static char waitstr1[] = "lwpwait";
    351 	static char waitstr2[] = "lwpwait2";
    352 
    353 	DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
    354 	    p->p_pid, l->l_lid, lid));
    355 
    356 	if (lid == l->l_lid)
    357 		return (EDEADLK); /* Waiting for ourselves makes no sense. */
    358 
    359 	wpri = PWAIT |
    360 	    ((flags & LWPWAIT_EXITCONTROL) ? PNOEXITERR : PCATCH);
    361  loop:
    362 	nfound = 0;
    363 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    364 		if ((l2 == l) || (l2->l_flag & L_DETACHED) ||
    365 		    ((lid != 0) && (lid != l2->l_lid)))
    366 			continue;
    367 
    368 		nfound++;
    369 		if (l2->l_stat == LSZOMB) {
    370 			if (departed)
    371 				*departed = l2->l_lid;
    372 
    373 			s = proclist_lock_write();
    374 			LIST_REMOVE(l2, l_zlist); /* off zomblwp */
    375 			proclist_unlock_write(s);
    376 
    377 			simple_lock(&p->p_lwplock);
    378 			LIST_REMOVE(l2, l_sibling);
    379 			p->p_nlwps--;
    380 			p->p_nzlwps--;
    381 			simple_unlock(&p->p_lwplock);
    382 			/* XXX decrement limits */
    383 
    384 			pool_put(&lwp_pool, l2);
    385 
    386 			return (0);
    387 		} else if (l2->l_stat == LSSLEEP ||
    388 		           l2->l_stat == LSSUSPENDED) {
    389 			/* Deadlock checks.
    390 			 * 1. If all other LWPs are waiting for exits
    391 			 *    or suspended, we would deadlock.
    392 			 */
    393 
    394 			LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
    395 				if (l3 != l && (l3->l_stat != LSSUSPENDED) &&
    396 				    !(l3->l_stat == LSSLEEP &&
    397 					l3->l_wchan == (caddr_t) &p->p_nlwps))
    398 					break;
    399 			}
    400 			if (l3 == NULL) /* Everyone else is waiting. */
    401 				return (EDEADLK);
    402 
    403 			/* XXX we'd like to check for a cycle of waiting
    404 			 * LWPs (specific LID waits, not any-LWP waits)
    405 			 * and detect that sort of deadlock, but we don't
    406 			 * have a good place to store the lwp that is
    407 			 * being waited for. wchan is already filled with
    408 			 * &p->p_nlwps, and putting the lwp address in
    409 			 * there for deadlock tracing would require
    410 			 * exiting LWPs to call wakeup on both their
    411 			 * own address and &p->p_nlwps, to get threads
    412 			 * sleeping on any LWP exiting.
    413 			 *
    414 			 * Revisit later. Maybe another auxillary
    415 			 * storage location associated with sleeping
    416 			 * is in order.
    417 			 */
    418 		}
    419 	}
    420 
    421 	if (nfound == 0)
    422 		return (ESRCH);
    423 
    424 	if ((error = tsleep((caddr_t) &p->p_nlwps, wpri,
    425 	    (lid != 0) ? waitstr1 : waitstr2, 0)) != 0)
    426 		return (error);
    427 
    428 	goto loop;
    429 }
    430 
    431 
    432 int
    433 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr, boolean_t inmem,
    434     int flags, void *stack, size_t stacksize,
    435     void (*func)(void *), void *arg, struct lwp **rnewlwpp)
    436 {
    437 	struct lwp *l2;
    438 	int s;
    439 
    440 	l2 = pool_get(&lwp_pool, PR_WAITOK);
    441 
    442 	l2->l_stat = LSIDL;
    443 	l2->l_forw = l2->l_back = NULL;
    444 	l2->l_proc = p2;
    445 
    446 
    447 	memset(&l2->l_startzero, 0,
    448 	       (unsigned) ((caddr_t)&l2->l_endzero -
    449 			   (caddr_t)&l2->l_startzero));
    450 	memcpy(&l2->l_startcopy, &l1->l_startcopy,
    451 	       (unsigned) ((caddr_t)&l2->l_endcopy -
    452 			   (caddr_t)&l2->l_startcopy));
    453 
    454 #if !defined(MULTIPROCESSOR)
    455 	/*
    456 	 * In the single-processor case, all processes will always run
    457 	 * on the same CPU.  So, initialize the child's CPU to the parent's
    458 	 * now.  In the multiprocessor case, the child's CPU will be
    459 	 * initialized in the low-level context switch code when the
    460 	 * process runs.
    461 	 */
    462 	l2->l_cpu = l1->l_cpu;
    463 #else
    464 	/*
    465 	 * zero child's cpu pointer so we don't get trash.
    466 	 */
    467 	l2->l_cpu = NULL;
    468 #endif /* ! MULTIPROCESSOR */
    469 
    470 	l2->l_flag = inmem ? L_INMEM : 0;
    471 	l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0;
    472 
    473 	callout_init(&l2->l_tsleep_ch);
    474 
    475 	if (rnewlwpp != NULL)
    476 		*rnewlwpp = l2;
    477 
    478 	l2->l_addr = (struct user *)uaddr;
    479 	uvm_lwp_fork(l1, l2, stack, stacksize, func,
    480 	    (arg != NULL) ? arg : l2);
    481 
    482 
    483 	simple_lock(&p2->p_lwplock);
    484 	l2->l_lid = ++p2->p_nlwpid;
    485 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
    486 	p2->p_nlwps++;
    487 	simple_unlock(&p2->p_lwplock);
    488 
    489 	/* XXX should be locked differently... */
    490 	s = proclist_lock_write();
    491 	LIST_INSERT_HEAD(&alllwp, l2, l_list);
    492 	proclist_unlock_write(s);
    493 
    494 	return (0);
    495 }
    496 
    497 
    498 /*
    499  * Quit the process. This will call cpu_exit, which will call cpu_switch,
    500  * so this can only be used meaningfully if you're willing to switch away.
    501  * Calling with l!=curlwp would be weird.
    502  */
    503 void
    504 lwp_exit(struct lwp *l)
    505 {
    506 	struct proc *p = l->l_proc;
    507 	int s;
    508 
    509 	DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
    510 	DPRINTF((" nlwps: %d nrlwps %d nzlwps: %d\n",
    511 	    p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
    512 
    513 	/*
    514 	 * If we are the last live LWP in a process, we need to exit
    515 	 * the entire process (if that's not already going on). We do
    516 	 * so with an exit status of zero, because it's a "controlled"
    517 	 * exit, and because that's what Solaris does.
    518 	 */
    519 	if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) {
    520 		DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
    521 		    p->p_pid, l->l_lid));
    522 		exit1(l, 0);
    523 	}
    524 
    525 	s = proclist_lock_write();
    526 	LIST_REMOVE(l, l_list);
    527 	if ((l->l_flag & L_DETACHED) == 0) {
    528 		DPRINTF(("lwp_exit: %d.%d going on zombie list\n", p->p_pid,
    529 		    l->l_lid));
    530 		LIST_INSERT_HEAD(&zomblwp, l, l_zlist);
    531 	}
    532 	proclist_unlock_write(s);
    533 
    534 	simple_lock(&p->p_lwplock);
    535 	p->p_nrlwps--;
    536 	simple_unlock(&p->p_lwplock);
    537 
    538 	l->l_stat = LSDEAD;
    539 
    540 	/* This LWP no longer needs to hold the kernel lock. */
    541 	KERNEL_PROC_UNLOCK(l);
    542 
    543 	/* cpu_exit() will not return */
    544 	cpu_exit(l, 0);
    545 
    546 }
    547 
    548 
    549 void
    550 lwp_exit2(struct lwp *l)
    551 {
    552 
    553 	simple_lock(&deadproc_slock);
    554 	LIST_INSERT_HEAD(&deadlwp, l, l_list);
    555 	simple_unlock(&deadproc_slock);
    556 
    557 	wakeup(&deadproc);
    558 }
    559 
    560 /*
    561  * Pick a LWP to represent the process for those operations which
    562  * want information about a "process" that is actually associated
    563  * with a LWP.
    564  */
    565 struct lwp *
    566 proc_representative_lwp(p)
    567 	struct proc *p;
    568 {
    569 	struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended;
    570 
    571 	/* Trivial case: only one LWP */
    572 	if (p->p_nlwps == 1)
    573 		return (LIST_FIRST(&p->p_lwps));
    574 
    575 	switch (p->p_stat) {
    576 	case SSTOP:
    577 	case SACTIVE:
    578 		/* Pick the most live LWP */
    579 		onproc = running = sleeping = stopped = suspended = NULL;
    580 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    581 			switch (l->l_stat) {
    582 			case LSONPROC:
    583 				onproc = l;
    584 				break;
    585 			case LSRUN:
    586 				running = l;
    587 				break;
    588 			case LSSLEEP:
    589 				sleeping = l;
    590 				break;
    591 			case LSSTOP:
    592 				stopped = l;
    593 				break;
    594 			case LSSUSPENDED:
    595 				suspended = l;
    596 				break;
    597 			}
    598 			if (onproc)
    599 				return onproc;
    600 			if (running)
    601 				return running;
    602 			if (sleeping)
    603 				return sleeping;
    604 			if (stopped)
    605 				return stopped;
    606 			if (suspended)
    607 				return suspended;
    608 		}
    609 		break;
    610 	case SDEAD:
    611 	case SZOMB:
    612 		/* Doesn't really matter... */
    613 		return (LIST_FIRST(&p->p_lwps));
    614 		break;
    615 #ifdef DIAGNOSTIC
    616 	case SIDL:
    617 		/* We have more than one LWP and we're in SIDL?
    618 		 * How'd that happen?
    619 		 */
    620 		panic("Too many LWPs (%d) in SIDL process %d (%s)",
    621 		    p->p_nrlwps, p->p_pid, p->p_comm);
    622 	default:
    623 		panic("Process %d (%s) in unknown state %d",
    624 		    p->p_pid, p->p_comm, p->p_stat);
    625 #endif
    626 	}
    627 
    628 	panic("proc_representative_lwp: couldn't find a lwp for process"
    629 		" %d (%s)", p->p_pid, p->p_comm);
    630 	/* NOTREACHED */
    631 	return NULL;
    632 }
    633