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