Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.59
      1 /*	$NetBSD: kern_proc.c,v 1.59 2003/03/12 16:39:01 dsl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.59 2003/03/12 16:39:01 dsl Exp $");
     77 
     78 #include "opt_kstack.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/kernel.h>
     83 #include <sys/proc.h>
     84 #include <sys/resourcevar.h>
     85 #include <sys/buf.h>
     86 #include <sys/acct.h>
     87 #include <sys/wait.h>
     88 #include <sys/file.h>
     89 #include <ufs/ufs/quota.h>
     90 #include <sys/uio.h>
     91 #include <sys/malloc.h>
     92 #include <sys/pool.h>
     93 #include <sys/mbuf.h>
     94 #include <sys/ioctl.h>
     95 #include <sys/tty.h>
     96 #include <sys/signalvar.h>
     97 #include <sys/ras.h>
     98 #include <sys/sa.h>
     99 #include <sys/savar.h>
    100 
    101 /*
    102  * Structure associated with user cacheing.
    103  */
    104 struct uidinfo {
    105 	LIST_ENTRY(uidinfo) ui_hash;
    106 	uid_t	ui_uid;
    107 	long	ui_proccnt;
    108 };
    109 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
    110 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
    111 u_long uihash;		/* size of hash table - 1 */
    112 
    113 /*
    114  * Other process lists
    115  */
    116 struct pidhashhead *pidhashtbl;
    117 u_long pidhash;
    118 struct pgrphashhead *pgrphashtbl;
    119 u_long pgrphash;
    120 
    121 struct proclist allproc;
    122 struct proclist zombproc;	/* resources have been freed */
    123 
    124 
    125 /*
    126  * Process list locking:
    127  *
    128  * We have two types of locks on the proclists: read locks and write
    129  * locks.  Read locks can be used in interrupt context, so while we
    130  * hold the write lock, we must also block clock interrupts to
    131  * lock out any scheduling changes that may happen in interrupt
    132  * context.
    133  *
    134  * The proclist lock locks the following structures:
    135  *
    136  *	allproc
    137  *	zombproc
    138  *	pidhashtbl
    139  */
    140 struct lock proclist_lock;
    141 
    142 /*
    143  * Locking of this proclist is special; it's accessed in a
    144  * critical section of process exit, and thus locking it can't
    145  * modify interrupt state.  We use a simple spin lock for this
    146  * proclist.  Processes on this proclist are also on zombproc;
    147  * we use the p_hash member to linkup to deadproc.
    148  */
    149 struct simplelock deadproc_slock;
    150 struct proclist deadproc;	/* dead, but not yet undead */
    151 
    152 struct pool proc_pool;
    153 struct pool lwp_pool;
    154 struct pool lwp_uc_pool;
    155 struct pool pcred_pool;
    156 struct pool plimit_pool;
    157 struct pool pstats_pool;
    158 struct pool pgrp_pool;
    159 struct pool rusage_pool;
    160 struct pool ras_pool;
    161 struct pool sadata_pool;
    162 struct pool saupcall_pool;
    163 struct pool ptimer_pool;
    164 
    165 MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data");
    166 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
    167 MALLOC_DEFINE(M_SESSION, "session", "session header");
    168 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
    169 
    170 /*
    171  * The process list descriptors, used during pid allocation and
    172  * by sysctl.  No locking on this data structure is needed since
    173  * it is completely static.
    174  */
    175 const struct proclist_desc proclists[] = {
    176 	{ &allproc	},
    177 	{ &zombproc	},
    178 	{ NULL		},
    179 };
    180 
    181 static void orphanpg __P((struct pgrp *));
    182 #ifdef DEBUG
    183 void pgrpdump __P((void));
    184 #endif
    185 
    186 /*
    187  * Initialize global process hashing structures.
    188  */
    189 void
    190 procinit(void)
    191 {
    192 	const struct proclist_desc *pd;
    193 
    194 	for (pd = proclists; pd->pd_list != NULL; pd++)
    195 		LIST_INIT(pd->pd_list);
    196 
    197 	spinlockinit(&proclist_lock, "proclk", 0);
    198 
    199 	LIST_INIT(&deadproc);
    200 	simple_lock_init(&deadproc_slock);
    201 
    202 	LIST_INIT(&alllwp);
    203 	LIST_INIT(&deadlwp);
    204 	LIST_INIT(&zomblwp);
    205 
    206 	pidhashtbl =
    207 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pidhash);
    208 	pgrphashtbl =
    209 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pgrphash);
    210 	uihashtbl =
    211 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
    212 
    213 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    214 	    &pool_allocator_nointr);
    215 	pool_init(&lwp_pool, sizeof(struct lwp), 0, 0, 0, "lwppl",
    216 	    &pool_allocator_nointr);
    217 	pool_init(&lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
    218 	    &pool_allocator_nointr);
    219 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    220 	    &pool_allocator_nointr);
    221 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
    222 	    &pool_allocator_nointr);
    223 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    224 	    &pool_allocator_nointr);
    225 	pool_init(&pstats_pool, sizeof(struct pstats), 0, 0, 0, "pstatspl",
    226 	    &pool_allocator_nointr);
    227 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    228 	    &pool_allocator_nointr);
    229 	pool_init(&ras_pool, sizeof(struct ras), 0, 0, 0, "raspl",
    230 	    &pool_allocator_nointr);
    231 	pool_init(&sadata_pool, sizeof(struct sadata), 0, 0, 0, "sadatapl",
    232 	    &pool_allocator_nointr);
    233 	pool_init(&saupcall_pool, sizeof(struct sadata_upcall), 0, 0, 0,
    234 	    "saupcpl",
    235 	    &pool_allocator_nointr);
    236 	pool_init(&ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
    237 	    &pool_allocator_nointr);
    238 }
    239 
    240 /*
    241  * Acquire a read lock on the proclist.
    242  */
    243 void
    244 proclist_lock_read(void)
    245 {
    246 	int error;
    247 
    248 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
    249 #ifdef DIAGNOSTIC
    250 	if (__predict_false(error != 0))
    251 		panic("proclist_lock_read: failed to acquire lock");
    252 #endif
    253 }
    254 
    255 /*
    256  * Release a read lock on the proclist.
    257  */
    258 void
    259 proclist_unlock_read(void)
    260 {
    261 
    262 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    263 }
    264 
    265 /*
    266  * Acquire a write lock on the proclist.
    267  */
    268 int
    269 proclist_lock_write(void)
    270 {
    271 	int s, error;
    272 
    273 	s = splclock();
    274 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
    275 #ifdef DIAGNOSTIC
    276 	if (__predict_false(error != 0))
    277 		panic("proclist_lock: failed to acquire lock");
    278 #endif
    279 	return (s);
    280 }
    281 
    282 /*
    283  * Release a write lock on the proclist.
    284  */
    285 void
    286 proclist_unlock_write(int s)
    287 {
    288 
    289 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    290 	splx(s);
    291 }
    292 
    293 /*
    294  * Change the count associated with number of processes
    295  * a given user is using.
    296  */
    297 int
    298 chgproccnt(uid_t uid, int diff)
    299 {
    300 	struct uidinfo *uip;
    301 	struct uihashhead *uipp;
    302 
    303 	uipp = UIHASH(uid);
    304 
    305 	LIST_FOREACH(uip, uipp, ui_hash)
    306 		if (uip->ui_uid == uid)
    307 			break;
    308 
    309 	if (uip) {
    310 		uip->ui_proccnt += diff;
    311 		if (uip->ui_proccnt > 0)
    312 			return (uip->ui_proccnt);
    313 		if (uip->ui_proccnt < 0)
    314 			panic("chgproccnt: procs < 0");
    315 		LIST_REMOVE(uip, ui_hash);
    316 		FREE(uip, M_PROC);
    317 		return (0);
    318 	}
    319 	if (diff <= 0) {
    320 		if (diff == 0)
    321 			return(0);
    322 		panic("chgproccnt: lost user");
    323 	}
    324 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    325 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    326 	uip->ui_uid = uid;
    327 	uip->ui_proccnt = diff;
    328 	return (diff);
    329 }
    330 
    331 /*
    332  * Is p an inferior of q?
    333  */
    334 int
    335 inferior(struct proc *p, struct proc *q)
    336 {
    337 
    338 	for (; p != q; p = p->p_pptr)
    339 		if (p->p_pid == 0)
    340 			return (0);
    341 	return (1);
    342 }
    343 
    344 /*
    345  * Locate a process by number
    346  */
    347 struct proc *
    348 pfind(pid_t pid)
    349 {
    350 	struct proc *p;
    351 
    352 	proclist_lock_read();
    353 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
    354 		if (p->p_pid == pid)
    355 			goto out;
    356  out:
    357 	proclist_unlock_read();
    358 	return (p);
    359 }
    360 
    361 /*
    362  * Locate a process group by number
    363  */
    364 struct pgrp *
    365 pgfind(pid_t pgid)
    366 {
    367 	struct pgrp *pgrp;
    368 
    369 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
    370 		if (pgrp->pg_id == pgid)
    371 			return (pgrp);
    372 	return (NULL);
    373 }
    374 
    375 /*
    376  * Move p to a new or existing process group (and session)
    377  */
    378 int
    379 enterpgrp(struct proc *p, pid_t pgid, int mksess)
    380 {
    381 	struct pgrp *pgrp = pgfind(pgid);
    382 
    383 #ifdef DIAGNOSTIC
    384 	if (__predict_false(pgrp != NULL && mksess))	/* firewalls */
    385 		panic("enterpgrp: setsid into non-empty pgrp");
    386 	if (__predict_false(SESS_LEADER(p)))
    387 		panic("enterpgrp: session leader attempted setpgrp");
    388 #endif
    389 	if (pgrp == NULL) {
    390 		pid_t savepid = p->p_pid;
    391 		struct proc *np;
    392 		/*
    393 		 * new process group
    394 		 */
    395 #ifdef DIAGNOSTIC
    396 		if (__predict_false(p->p_pid != pgid))
    397 			panic("enterpgrp: new pgrp and pid != pgid");
    398 #endif
    399 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    400 		if ((np = pfind(savepid)) == NULL || np != p) {
    401 			pool_put(&pgrp_pool, pgrp);
    402 			return (ESRCH);
    403 		}
    404 		if (mksess) {
    405 			struct session *sess;
    406 
    407 			/*
    408 			 * new session
    409 			 */
    410 			MALLOC(sess, struct session *, sizeof(struct session),
    411 			    M_SESSION, M_WAITOK);
    412 			if ((np = pfind(savepid)) == NULL || np != p) {
    413 				FREE(sess, M_SESSION);
    414 				pool_put(&pgrp_pool, pgrp);
    415 				return (ESRCH);
    416 			}
    417 			sess->s_sid = p->p_pid;
    418 			sess->s_leader = p;
    419 			sess->s_count = 1;
    420 			sess->s_ttyvp = NULL;
    421 			sess->s_ttyp = NULL;
    422 			sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
    423 			memcpy(sess->s_login, p->p_session->s_login,
    424 			    sizeof(sess->s_login));
    425 			p->p_flag &= ~P_CONTROLT;
    426 			pgrp->pg_session = sess;
    427 #ifdef DIAGNOSTIC
    428 			if (__predict_false(p != curproc))
    429 				panic("enterpgrp: mksession and p != curlwp");
    430 #endif
    431 		} else {
    432 			SESSHOLD(p->p_session);
    433 			pgrp->pg_session = p->p_session;
    434 		}
    435 		pgrp->pg_id = pgid;
    436 		LIST_INIT(&pgrp->pg_members);
    437 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    438 		pgrp->pg_jobc = 0;
    439 	} else if (pgrp == p->p_pgrp)
    440 		return (0);
    441 
    442 	/*
    443 	 * Adjust eligibility of affected pgrps to participate in job control.
    444 	 * Increment eligibility counts before decrementing, otherwise we
    445 	 * could reach 0 spuriously during the first call.
    446 	 */
    447 	fixjobc(p, pgrp, 1);
    448 	fixjobc(p, p->p_pgrp, 0);
    449 
    450 	LIST_REMOVE(p, p_pglist);
    451 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
    452 		pgdelete(p->p_pgrp);
    453 	p->p_pgrp = pgrp;
    454 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    455 	return (0);
    456 }
    457 
    458 /*
    459  * remove process from process group
    460  */
    461 int
    462 leavepgrp(struct proc *p)
    463 {
    464 
    465 	LIST_REMOVE(p, p_pglist);
    466 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
    467 		pgdelete(p->p_pgrp);
    468 	p->p_pgrp = 0;
    469 	return (0);
    470 }
    471 
    472 /*
    473  * delete a process group
    474  */
    475 void
    476 pgdelete(struct pgrp *pgrp)
    477 {
    478 
    479 	/* Remove reference (if any) from tty to this process group */
    480 	if (pgrp->pg_session->s_ttyp != NULL &&
    481 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    482 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    483 	LIST_REMOVE(pgrp, pg_hash);
    484 	SESSRELE(pgrp->pg_session);
    485 	pool_put(&pgrp_pool, pgrp);
    486 }
    487 
    488 /*
    489  * Adjust pgrp jobc counters when specified process changes process group.
    490  * We count the number of processes in each process group that "qualify"
    491  * the group for terminal job control (those with a parent in a different
    492  * process group of the same session).  If that count reaches zero, the
    493  * process group becomes orphaned.  Check both the specified process'
    494  * process group and that of its children.
    495  * entering == 0 => p is leaving specified group.
    496  * entering == 1 => p is entering specified group.
    497  */
    498 void
    499 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
    500 {
    501 	struct pgrp *hispgrp;
    502 	struct session *mysession = pgrp->pg_session;
    503 
    504 	/*
    505 	 * Check p's parent to see whether p qualifies its own process
    506 	 * group; if so, adjust count for p's process group.
    507 	 */
    508 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    509 	    hispgrp->pg_session == mysession) {
    510 		if (entering)
    511 			pgrp->pg_jobc++;
    512 		else if (--pgrp->pg_jobc == 0)
    513 			orphanpg(pgrp);
    514 	}
    515 
    516 	/*
    517 	 * Check this process' children to see whether they qualify
    518 	 * their process groups; if so, adjust counts for children's
    519 	 * process groups.
    520 	 */
    521 	LIST_FOREACH(p, &p->p_children, p_sibling) {
    522 		if ((hispgrp = p->p_pgrp) != pgrp &&
    523 		    hispgrp->pg_session == mysession &&
    524 		    P_ZOMBIE(p) == 0) {
    525 			if (entering)
    526 				hispgrp->pg_jobc++;
    527 			else if (--hispgrp->pg_jobc == 0)
    528 				orphanpg(hispgrp);
    529 		}
    530 	}
    531 }
    532 
    533 /*
    534  * A process group has become orphaned;
    535  * if there are any stopped processes in the group,
    536  * hang-up all process in that group.
    537  */
    538 static void
    539 orphanpg(struct pgrp *pg)
    540 {
    541 	struct proc *p;
    542 
    543 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    544 		if (p->p_stat == SSTOP) {
    545 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    546 				psignal(p, SIGHUP);
    547 				psignal(p, SIGCONT);
    548 			}
    549 			return;
    550 		}
    551 	}
    552 }
    553 
    554 /* mark process as suid/sgid, reset some values do defaults */
    555 void
    556 p_sugid(struct proc *p)
    557 {
    558 	struct plimit *newlim;
    559 
    560 	p->p_flag |= P_SUGID;
    561 	/* reset what needs to be reset in plimit */
    562 	if (p->p_limit->pl_corename != defcorename) {
    563 		if (p->p_limit->p_refcnt > 1 &&
    564 		    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    565 			newlim = limcopy(p->p_limit);
    566 			limfree(p->p_limit);
    567 			p->p_limit = newlim;
    568 		}
    569 		free(p->p_limit->pl_corename, M_TEMP);
    570 		p->p_limit->pl_corename = defcorename;
    571 	}
    572 }
    573 
    574 #ifdef DEBUG
    575 void
    576 pgrpdump(void)
    577 {
    578 	struct pgrp *pgrp;
    579 	struct proc *p;
    580 	int i;
    581 
    582 	for (i = 0; i <= pgrphash; i++) {
    583 		if ((pgrp = LIST_FIRST(&pgrphashtbl[i])) != NULL) {
    584 			printf("\tindx %d\n", i);
    585 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    586 				printf("\tpgrp %p, pgid %d, sess %p, "
    587 				    "sesscnt %d, mem %p\n",
    588 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    589 				    pgrp->pg_session->s_count,
    590 				    LIST_FIRST(&pgrp->pg_members));
    591 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
    592 					printf("\t\tpid %d addr %p pgrp %p\n",
    593 					    p->p_pid, p, p->p_pgrp);
    594 				}
    595 			}
    596 		}
    597 	}
    598 }
    599 #endif /* DEBUG */
    600 
    601 #ifdef KSTACK_CHECK_MAGIC
    602 #include <sys/user.h>
    603 
    604 #define	KSTACK_MAGIC	0xdeadbeaf
    605 
    606 /* XXX should be per process basis? */
    607 int kstackleftmin = KSTACK_SIZE;
    608 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
    609 					  less than this */
    610 
    611 void
    612 kstack_setup_magic(const struct lwp *l)
    613 {
    614 	u_int32_t *ip;
    615 	u_int32_t const *end;
    616 
    617 	KASSERT(l != NULL);
    618 	KASSERT(l != &lwp0);
    619 
    620 	/*
    621 	 * fill all the stack with magic number
    622 	 * so that later modification on it can be detected.
    623 	 */
    624 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
    625 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
    626 	for (; ip < end; ip++) {
    627 		*ip = KSTACK_MAGIC;
    628 	}
    629 }
    630 
    631 void
    632 kstack_check_magic(const struct lwp *l)
    633 {
    634 	u_int32_t const *ip, *end;
    635 	int stackleft;
    636 
    637 	KASSERT(l != NULL);
    638 
    639 	/* don't check proc0 */ /*XXX*/
    640 	if (l == &lwp0)
    641 		return;
    642 
    643 #ifdef __MACHINE_STACK_GROWS_UP
    644 	/* stack grows upwards (eg. hppa) */
    645 	ip = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
    646 	end = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
    647 	for (ip--; ip >= end; ip--)
    648 		if (*ip != KSTACK_MAGIC)
    649 			break;
    650 
    651 	stackleft = (caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (caddr_t)ip;
    652 #else /* __MACHINE_STACK_GROWS_UP */
    653 	/* stack grows downwards (eg. i386) */
    654 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
    655 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
    656 	for (; ip < end; ip++)
    657 		if (*ip != KSTACK_MAGIC)
    658 			break;
    659 
    660 	stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(l);
    661 #endif /* __MACHINE_STACK_GROWS_UP */
    662 
    663 	if (kstackleftmin > stackleft) {
    664 		kstackleftmin = stackleft;
    665 		if (stackleft < kstackleftthres)
    666 			printf("warning: kernel stack left %d bytes"
    667 			    "(pid %u:lid %u)\n", stackleft,
    668 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
    669 	}
    670 
    671 	if (stackleft <= 0) {
    672 		panic("magic on the top of kernel stack changed for "
    673 		    "pid %u, lid %u: maybe kernel stack overflow",
    674 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
    675 	}
    676 }
    677 #endif /* KSTACK_CHECK_MAGIC */
    678