Home | History | Annotate | Line # | Download | only in kern
kern_lwp.c revision 1.1.2.4
      1 /*	$NetBSD: kern_lwp.c,v 1.1.2.4 2001/08/24 04:20:07 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 
    254 int
    255 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    256 {
    257 	struct sys__lwp_wait_args /* {
    258 	syscallarg(lwpid_t) wait_for;
    259 	syscallarg(lwpid_t *) departed;
    260 	} */ *uap = v;
    261 	int error;
    262 	lwpid_t dep;
    263 
    264 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    265 	if (error)
    266 		return (error);
    267 
    268 	if (SCARG(uap, departed)) {
    269 		error = copyout(&dep, SCARG(uap, departed),
    270 		    sizeof(dep));
    271 		if (error)
    272 			return (error);
    273 	}
    274 
    275 	return (0);
    276 }
    277 
    278 
    279 int
    280 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
    281 {
    282 
    283 	struct proc *p = l->l_proc;
    284 	struct lwp *l2, *l3;
    285 	int nfound, error, s, wpri;
    286 	static char waitstr1[] = "lwpwait";
    287 	static char waitstr2[] = "lwpwait2";
    288 
    289 	DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
    290 	    p->p_pid, l->l_lid, lid));
    291 
    292 	if (lid == l->l_lid)
    293 		return (EDEADLK); /* Waiting for ourselves makes no sense. */
    294 
    295 	wpri = PWAIT | PCATCH |
    296 	    ((flags & LWPWAIT_EXITCONTROL) ? PNOEXITERR : 0);
    297  loop:
    298 	nfound = 0;
    299 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    300 		if ((l2 == l) || (l2->l_flag & L_DETACHED) ||
    301 		    ((lid != 0) && (lid != l2->l_lid)))
    302 			continue;
    303 
    304 		nfound++;
    305 		if (l2->l_stat == LSZOMB) {
    306 			if (departed)
    307 				*departed = l2->l_lid;
    308 
    309 			s = proclist_lock_write();
    310 			LIST_REMOVE(l2, l_zlist); /* off zomblwp */
    311 			proclist_unlock_write(s);
    312 
    313 			simple_lock(&p->p_lwplock);
    314 			LIST_REMOVE(l2, l_sibling);
    315 			p->p_nlwps--;
    316 			p->p_nzlwps--;
    317 			simple_unlock(&p->p_lwplock);
    318 			/* XXX decrement limits */
    319 
    320 			pool_put(&lwp_pool, l2);
    321 
    322 			return (0);
    323 		} else if (l2->l_stat == LSSLEEP ||
    324 		           l2->l_stat == LSSUSPENDED) {
    325 			/* Deadlock checks.
    326 			 * 1. If all other LWPs are waiting for exits
    327 			 *    or suspended, we would deadlock.
    328 			 */
    329 
    330 			LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
    331 				if (l3 != l && (l3->l_stat != LSSUSPENDED) &&
    332 				    !(l3->l_stat == LSSLEEP &&
    333 					l3->l_wchan == (caddr_t) &p->p_nlwps))
    334 					break;
    335 			}
    336 			if (l3 == NULL) /* Everyone else is waiting. */
    337 				return (EDEADLK);
    338 
    339 			/* XXX we'd like to check for a cycle of waiting
    340 			 * LWPs (specific LID waits, not any-LWP waits)
    341 			 * and detect that sort of deadlock, but we don't
    342 			 * have a good place to store the lwp that is
    343 			 * being waited for. wchan is already filled with
    344 			 * &p->p_nlwps, and putting the lwp address in
    345 			 * there for deadlock tracing would require
    346 			 * exiting LWPs to call wakeup on both their
    347 			 * own address and &p->p_nlwps, to get threads
    348 			 * sleeping on any LWP exiting.
    349 			 *
    350 			 * Revisit later. Maybe another auxillary
    351 			 * storage location associated with sleeping
    352 			 * is in order.
    353 			 */
    354 		}
    355 	}
    356 
    357 	if (nfound == 0)
    358 		return (ESRCH);
    359 
    360 	if ((error = tsleep((caddr_t) &p->p_nlwps, wpri,
    361 	    (lid != 0) ? waitstr1 : waitstr2, 0)) != 0)
    362 		return (error);
    363 
    364 	goto loop;
    365 }
    366 
    367 
    368 int
    369 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr,
    370     int flags, void *stack, size_t stacksize,
    371     void (*func)(void *), void *arg, struct lwp **rnewlwpp)
    372 {
    373 	struct lwp *l2;
    374 	int s;
    375 
    376 	l2 = pool_get(&lwp_pool, PR_WAITOK);
    377 
    378 	l2->l_stat = LSIDL;
    379 	l2->l_forw = l2->l_back = NULL;
    380 	l2->l_proc = p2;
    381 
    382 
    383 	memset(&l2->l_startzero, 0,
    384 	       (unsigned) ((caddr_t)&l2->l_endzero -
    385 			   (caddr_t)&l2->l_startzero));
    386 	memcpy(&l2->l_startcopy, &l1->l_startcopy,
    387 	       (unsigned) ((caddr_t)&l2->l_endcopy -
    388 			   (caddr_t)&l2->l_startcopy));
    389 
    390 #if !defined(MULTIPROCESSOR)
    391 	/*
    392 	 * In the single-processor case, all processes will always run
    393 	 * on the same CPU.  So, initialize the child's CPU to the parent's
    394 	 * now.  In the multiprocessor case, the child's CPU will be
    395 	 * initialized in the low-level context switch code when the
    396 	 * process runs.
    397 	 */
    398 	l2->l_cpu = l1->l_cpu;
    399 #else
    400 	/*
    401 	 * zero child's cpu pointer so we don't get trash.
    402 	 */
    403 	l2->l_cpu = NULL;
    404 #endif /* ! MULTIPROCESSOR */
    405 
    406 	l2->l_flag = L_INMEM;
    407 	l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0;
    408 
    409 	callout_init(&l2->l_tsleep_ch);
    410 
    411 	if (rnewlwpp != NULL)
    412 		*rnewlwpp = l2;
    413 
    414 	l2->l_addr = (struct user *)uaddr;
    415 	uvm_lwp_fork(l1, l2, stack, stacksize, func,
    416 	    (arg != NULL) ? arg : l2);
    417 
    418 
    419 	simple_lock(&p2->p_lwplock);
    420 	l2->l_lid = ++p2->p_nlwpid;
    421 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
    422 	p2->p_nlwps++;
    423 	simple_unlock(&p2->p_lwplock);
    424 
    425 	/* XXX should be locked differently... */
    426 	s = proclist_lock_write();
    427 	LIST_INSERT_HEAD(&alllwp, l2, l_list);
    428 	proclist_unlock_write(s);
    429 
    430 	return (0);
    431 }
    432 
    433 
    434 /*
    435  * Quit the process. This will call cpu_exit, which will call cpu_switch,
    436  * so this can only be used meaningfully if you're willing to switch away.
    437  * Calling with l!=curproc would be weird.
    438  */
    439 void
    440 lwp_exit(struct lwp *l)
    441 {
    442 	struct proc *p = l->l_proc;
    443 	int s;
    444 
    445 	DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
    446 	DPRINTF((" nlwps: %d nrlwps %d nzlwps: %d\n",
    447 	    p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
    448 	/*
    449 	 * If we are the last live LWP in a process, we need to exit
    450 	 * the entire process (if that's not already going on). We do
    451 	 * so with an exit status of zero, because it's a "controlled"
    452 	 * exit, and because that's what Solaris does.
    453 	 */
    454 	if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) {
    455 		DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
    456 		    p->p_pid, l->l_lid));
    457 		exit1(l, 0);
    458 	}
    459 
    460 	s = proclist_lock_write();
    461 	LIST_REMOVE(l, l_list);
    462 	if ((l->l_flag & L_DETACHED) == 0) {
    463 		DPRINTF(("lwp_exit: %d.%d going on zombie list\n", p->p_pid,
    464 		    l->l_lid));
    465 		LIST_INSERT_HEAD(&zomblwp, l, l_zlist);
    466 	}
    467 	proclist_unlock_write(s);
    468 
    469 	simple_lock(&p->p_lwplock);
    470 	p->p_nrlwps--;
    471 	simple_unlock(&p->p_lwplock);
    472 
    473 	l->l_stat = LSDEAD;
    474 
    475 	/* cpu_exit() will not return */
    476 	cpu_exit(l, 0);
    477 
    478 }
    479 
    480 
    481 void
    482 lwp_exit2(struct lwp *l)
    483 {
    484 
    485 	simple_lock(&deadproc_slock);
    486 	LIST_INSERT_HEAD(&deadlwp, l, l_list);
    487 	simple_unlock(&deadproc_slock);
    488 
    489 	wakeup(&deadproc);
    490 }
    491