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