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