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