Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.50
      1 /*	$NetBSD: kern_proc.c,v 1.50 2002/07/26 06:04:57 enami 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.50 2002/07/26 06:04:57 enami Exp $");
     77 
     78 #include "opt_kstack.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/map.h>
     83 #include <sys/kernel.h>
     84 #include <sys/proc.h>
     85 #include <sys/resourcevar.h>
     86 #include <sys/buf.h>
     87 #include <sys/acct.h>
     88 #include <sys/wait.h>
     89 #include <sys/file.h>
     90 #include <ufs/ufs/quota.h>
     91 #include <sys/uio.h>
     92 #include <sys/malloc.h>
     93 #include <sys/pool.h>
     94 #include <sys/mbuf.h>
     95 #include <sys/ioctl.h>
     96 #include <sys/tty.h>
     97 #include <sys/signalvar.h>
     98 
     99 /*
    100  * Structure associated with user cacheing.
    101  */
    102 struct uidinfo {
    103 	LIST_ENTRY(uidinfo) ui_hash;
    104 	uid_t	ui_uid;
    105 	long	ui_proccnt;
    106 };
    107 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
    108 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
    109 u_long uihash;		/* size of hash table - 1 */
    110 
    111 /*
    112  * Other process lists
    113  */
    114 struct pidhashhead *pidhashtbl;
    115 u_long pidhash;
    116 struct pgrphashhead *pgrphashtbl;
    117 u_long pgrphash;
    118 
    119 struct proclist allproc;
    120 struct proclist zombproc;	/* resources have been freed */
    121 
    122 /*
    123  * Process list locking:
    124  *
    125  * We have two types of locks on the proclists: read locks and write
    126  * locks.  Read locks can be used in interrupt context, so while we
    127  * hold the write lock, we must also block clock interrupts to
    128  * lock out any scheduling changes that may happen in interrupt
    129  * context.
    130  *
    131  * The proclist lock locks the following structures:
    132  *
    133  *	allproc
    134  *	zombproc
    135  *	pidhashtbl
    136  */
    137 struct lock proclist_lock;
    138 
    139 /*
    140  * Locking of this proclist is special; it's accessed in a
    141  * critical section of process exit, and thus locking it can't
    142  * modify interrupt state.  We use a simple spin lock for this
    143  * proclist.  Processes on this proclist are also on zombproc;
    144  * we use the p_hash member to linkup to deadproc.
    145  */
    146 struct simplelock deadproc_slock;
    147 struct proclist deadproc;	/* dead, but not yet undead */
    148 
    149 struct pool proc_pool;
    150 struct pool pcred_pool;
    151 struct pool plimit_pool;
    152 struct pool pgrp_pool;
    153 struct pool rusage_pool;
    154 
    155 /*
    156  * The process list descriptors, used during pid allocation and
    157  * by sysctl.  No locking on this data structure is needed since
    158  * it is completely static.
    159  */
    160 const struct proclist_desc proclists[] = {
    161 	{ &allproc	},
    162 	{ &zombproc	},
    163 	{ NULL		},
    164 };
    165 
    166 static void orphanpg __P((struct pgrp *));
    167 #ifdef DEBUG
    168 void pgrpdump __P((void));
    169 #endif
    170 
    171 /*
    172  * Initialize global process hashing structures.
    173  */
    174 void
    175 procinit()
    176 {
    177 	const struct proclist_desc *pd;
    178 
    179 	for (pd = proclists; pd->pd_list != NULL; pd++)
    180 		LIST_INIT(pd->pd_list);
    181 
    182 	spinlockinit(&proclist_lock, "proclk", 0);
    183 
    184 	LIST_INIT(&deadproc);
    185 	simple_lock_init(&deadproc_slock);
    186 
    187 	pidhashtbl =
    188 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pidhash);
    189 	pgrphashtbl =
    190 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pgrphash);
    191 	uihashtbl =
    192 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
    193 
    194 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    195 	    &pool_allocator_nointr);
    196 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    197 	    &pool_allocator_nointr);
    198 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
    199 	    &pool_allocator_nointr);
    200 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    201 	    &pool_allocator_nointr);
    202 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    203 	    &pool_allocator_nointr);
    204 }
    205 
    206 /*
    207  * Acquire a read lock on the proclist.
    208  */
    209 void
    210 proclist_lock_read()
    211 {
    212 	int error;
    213 
    214 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
    215 #ifdef DIAGNOSTIC
    216 	if (__predict_false(error != 0))
    217 		panic("proclist_lock_read: failed to acquire lock");
    218 #endif
    219 }
    220 
    221 /*
    222  * Release a read lock on the proclist.
    223  */
    224 void
    225 proclist_unlock_read()
    226 {
    227 
    228 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    229 }
    230 
    231 /*
    232  * Acquire a write lock on the proclist.
    233  */
    234 int
    235 proclist_lock_write()
    236 {
    237 	int s, error;
    238 
    239 	s = splclock();
    240 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
    241 #ifdef DIAGNOSTIC
    242 	if (__predict_false(error != 0))
    243 		panic("proclist_lock: failed to acquire lock");
    244 #endif
    245 	return (s);
    246 }
    247 
    248 /*
    249  * Release a write lock on the proclist.
    250  */
    251 void
    252 proclist_unlock_write(s)
    253 	int s;
    254 {
    255 
    256 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    257 	splx(s);
    258 }
    259 
    260 /*
    261  * Change the count associated with number of processes
    262  * a given user is using.
    263  */
    264 int
    265 chgproccnt(uid, diff)
    266 	uid_t	uid;
    267 	int	diff;
    268 {
    269 	struct uidinfo *uip;
    270 	struct uihashhead *uipp;
    271 
    272 	uipp = UIHASH(uid);
    273 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
    274 		if (uip->ui_uid == uid)
    275 			break;
    276 	if (uip) {
    277 		uip->ui_proccnt += diff;
    278 		if (uip->ui_proccnt > 0)
    279 			return (uip->ui_proccnt);
    280 		if (uip->ui_proccnt < 0)
    281 			panic("chgproccnt: procs < 0");
    282 		LIST_REMOVE(uip, ui_hash);
    283 		FREE(uip, M_PROC);
    284 		return (0);
    285 	}
    286 	if (diff <= 0) {
    287 		if (diff == 0)
    288 			return(0);
    289 		panic("chgproccnt: lost user");
    290 	}
    291 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    292 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    293 	uip->ui_uid = uid;
    294 	uip->ui_proccnt = diff;
    295 	return (diff);
    296 }
    297 
    298 /*
    299  * Is p an inferior of q?
    300  */
    301 int
    302 inferior(p, q)
    303 	struct proc *p;
    304 	struct proc *q;
    305 {
    306 
    307 	for (; p != q; p = p->p_pptr)
    308 		if (p->p_pid == 0)
    309 			return (0);
    310 	return (1);
    311 }
    312 
    313 /*
    314  * Locate a process by number
    315  */
    316 struct proc *
    317 pfind(pid)
    318 	pid_t pid;
    319 {
    320 	struct proc *p;
    321 
    322 	proclist_lock_read();
    323 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
    324 		if (p->p_pid == pid)
    325 			goto out;
    326  out:
    327 	proclist_unlock_read();
    328 	return (p);
    329 }
    330 
    331 /*
    332  * Locate a process group by number
    333  */
    334 struct pgrp *
    335 pgfind(pgid)
    336 	pid_t pgid;
    337 {
    338 	struct pgrp *pgrp;
    339 
    340 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != NULL;
    341 	    pgrp = pgrp->pg_hash.le_next)
    342 		if (pgrp->pg_id == pgid)
    343 			return (pgrp);
    344 	return (NULL);
    345 }
    346 
    347 /*
    348  * Move p to a new or existing process group (and session)
    349  */
    350 int
    351 enterpgrp(p, pgid, mksess)
    352 	struct proc *p;
    353 	pid_t pgid;
    354 	int mksess;
    355 {
    356 	struct pgrp *pgrp = pgfind(pgid);
    357 
    358 #ifdef DIAGNOSTIC
    359 	if (__predict_false(pgrp != NULL && mksess))	/* firewalls */
    360 		panic("enterpgrp: setsid into non-empty pgrp");
    361 	if (__predict_false(SESS_LEADER(p)))
    362 		panic("enterpgrp: session leader attempted setpgrp");
    363 #endif
    364 	if (pgrp == NULL) {
    365 		pid_t savepid = p->p_pid;
    366 		struct proc *np;
    367 		/*
    368 		 * new process group
    369 		 */
    370 #ifdef DIAGNOSTIC
    371 		if (__predict_false(p->p_pid != pgid))
    372 			panic("enterpgrp: new pgrp and pid != pgid");
    373 #endif
    374 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    375 		if ((np = pfind(savepid)) == NULL || np != p) {
    376 			pool_put(&pgrp_pool, pgrp);
    377 			return (ESRCH);
    378 		}
    379 		if (mksess) {
    380 			struct session *sess;
    381 
    382 			/*
    383 			 * new session
    384 			 */
    385 			MALLOC(sess, struct session *, sizeof(struct session),
    386 			    M_SESSION, M_WAITOK);
    387 			if ((np = pfind(savepid)) == NULL || np != p) {
    388 				FREE(sess, M_SESSION);
    389 				pool_put(&pgrp_pool, pgrp);
    390 				return (ESRCH);
    391 			}
    392 			sess->s_sid = p->p_pid;
    393 			sess->s_leader = p;
    394 			sess->s_count = 1;
    395 			sess->s_ttyvp = NULL;
    396 			sess->s_ttyp = NULL;
    397 			memcpy(sess->s_login, p->p_session->s_login,
    398 			    sizeof(sess->s_login));
    399 			p->p_flag &= ~P_CONTROLT;
    400 			pgrp->pg_session = sess;
    401 #ifdef DIAGNOSTIC
    402 			if (__predict_false(p != curproc))
    403 				panic("enterpgrp: mksession and p != curproc");
    404 #endif
    405 		} else {
    406 			SESSHOLD(p->p_session);
    407 			pgrp->pg_session = p->p_session;
    408 		}
    409 		pgrp->pg_id = pgid;
    410 		LIST_INIT(&pgrp->pg_members);
    411 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    412 		pgrp->pg_jobc = 0;
    413 	} else if (pgrp == p->p_pgrp)
    414 		return (0);
    415 
    416 	/*
    417 	 * Adjust eligibility of affected pgrps to participate in job control.
    418 	 * Increment eligibility counts before decrementing, otherwise we
    419 	 * could reach 0 spuriously during the first call.
    420 	 */
    421 	fixjobc(p, pgrp, 1);
    422 	fixjobc(p, p->p_pgrp, 0);
    423 
    424 	LIST_REMOVE(p, p_pglist);
    425 	if (p->p_pgrp->pg_members.lh_first == 0)
    426 		pgdelete(p->p_pgrp);
    427 	p->p_pgrp = pgrp;
    428 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    429 	return (0);
    430 }
    431 
    432 /*
    433  * remove process from process group
    434  */
    435 int
    436 leavepgrp(p)
    437 	struct proc *p;
    438 {
    439 
    440 	LIST_REMOVE(p, p_pglist);
    441 	if (p->p_pgrp->pg_members.lh_first == 0)
    442 		pgdelete(p->p_pgrp);
    443 	p->p_pgrp = 0;
    444 	return (0);
    445 }
    446 
    447 /*
    448  * delete a process group
    449  */
    450 void
    451 pgdelete(pgrp)
    452 	struct pgrp *pgrp;
    453 {
    454 
    455 	/* Remove reference (if any) from tty to this process group */
    456 	if (pgrp->pg_session->s_ttyp != NULL &&
    457 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    458 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    459 	LIST_REMOVE(pgrp, pg_hash);
    460 	SESSRELE(pgrp->pg_session);
    461 	pool_put(&pgrp_pool, pgrp);
    462 }
    463 
    464 /*
    465  * Adjust pgrp jobc counters when specified process changes process group.
    466  * We count the number of processes in each process group that "qualify"
    467  * the group for terminal job control (those with a parent in a different
    468  * process group of the same session).  If that count reaches zero, the
    469  * process group becomes orphaned.  Check both the specified process'
    470  * process group and that of its children.
    471  * entering == 0 => p is leaving specified group.
    472  * entering == 1 => p is entering specified group.
    473  */
    474 void
    475 fixjobc(p, pgrp, entering)
    476 	struct proc *p;
    477 	struct pgrp *pgrp;
    478 	int entering;
    479 {
    480 	struct pgrp *hispgrp;
    481 	struct session *mysession = pgrp->pg_session;
    482 
    483 	/*
    484 	 * Check p's parent to see whether p qualifies its own process
    485 	 * group; if so, adjust count for p's process group.
    486 	 */
    487 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    488 	    hispgrp->pg_session == mysession) {
    489 		if (entering)
    490 			pgrp->pg_jobc++;
    491 		else if (--pgrp->pg_jobc == 0)
    492 			orphanpg(pgrp);
    493 	}
    494 
    495 	/*
    496 	 * Check this process' children to see whether they qualify
    497 	 * their process groups; if so, adjust counts for children's
    498 	 * process groups.
    499 	 */
    500 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    501 		if ((hispgrp = p->p_pgrp) != pgrp &&
    502 		    hispgrp->pg_session == mysession &&
    503 		    P_ZOMBIE(p) == 0) {
    504 			if (entering)
    505 				hispgrp->pg_jobc++;
    506 			else if (--hispgrp->pg_jobc == 0)
    507 				orphanpg(hispgrp);
    508 		}
    509 	}
    510 }
    511 
    512 /*
    513  * A process group has become orphaned;
    514  * if there are any stopped processes in the group,
    515  * hang-up all process in that group.
    516  */
    517 static void
    518 orphanpg(pg)
    519 	struct pgrp *pg;
    520 {
    521 	struct proc *p;
    522 
    523 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    524 		if (p->p_stat == SSTOP) {
    525 			for (p = pg->pg_members.lh_first; p != 0;
    526 			    p = p->p_pglist.le_next) {
    527 				psignal(p, SIGHUP);
    528 				psignal(p, SIGCONT);
    529 			}
    530 			return;
    531 		}
    532 	}
    533 }
    534 
    535 /* mark process as suid/sgid, reset some values do defaults */
    536 void
    537 p_sugid(p)
    538 	struct proc *p;
    539 {
    540 	struct plimit *newlim;
    541 
    542 	p->p_flag |= P_SUGID;
    543 	/* reset what needs to be reset in plimit */
    544 	if (p->p_limit->pl_corename != defcorename) {
    545 		if (p->p_limit->p_refcnt > 1 &&
    546 		    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    547 			newlim = limcopy(p->p_limit);
    548 			limfree(p->p_limit);
    549 			p->p_limit = newlim;
    550 		}
    551 		free(p->p_limit->pl_corename, M_TEMP);
    552 		p->p_limit->pl_corename = defcorename;
    553 	}
    554 }
    555 
    556 #ifdef DEBUG
    557 void
    558 pgrpdump()
    559 {
    560 	struct pgrp *pgrp;
    561 	struct proc *p;
    562 	int i;
    563 
    564 	for (i = 0; i <= pgrphash; i++) {
    565 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
    566 			printf("\tindx %d\n", i);
    567 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    568 				printf("\tpgrp %p, pgid %d, sess %p, "
    569 				    "sesscnt %d, mem %p\n",
    570 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    571 				    pgrp->pg_session->s_count,
    572 				    pgrp->pg_members.lh_first);
    573 				for (p = pgrp->pg_members.lh_first; p != 0;
    574 				    p = p->p_pglist.le_next) {
    575 					printf("\t\tpid %d addr %p pgrp %p\n",
    576 					    p->p_pid, p, p->p_pgrp);
    577 				}
    578 			}
    579 		}
    580 	}
    581 }
    582 #endif /* DEBUG */
    583 
    584 #ifdef KSTACK_CHECK_MAGIC
    585 #include <sys/user.h>
    586 
    587 #define	KSTACK_MAGIC	0xdeadbeaf
    588 
    589 /* XXX should be per process basis? */
    590 int kstackleftmin = KSTACK_SIZE;
    591 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
    592 					  less than this */
    593 
    594 void
    595 kstack_setup_magic(const struct proc *p)
    596 {
    597 	u_int32_t *ip;
    598 	u_int32_t const *end;
    599 
    600 	KASSERT(p != 0);
    601 	KASSERT(p != &proc0);
    602 
    603 	/*
    604 	 * fill all the stack with magic number
    605 	 * so that later modification on it can be detected.
    606 	 */
    607 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
    608 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
    609 	for (; ip < end; ip++) {
    610 		*ip = KSTACK_MAGIC;
    611 	}
    612 }
    613 
    614 void
    615 kstack_check_magic(const struct proc *p)
    616 {
    617 	u_int32_t const *ip, *end;
    618 	int stackleft;
    619 
    620 	KASSERT(p != 0);
    621 
    622 	/* don't check proc0 */ /*XXX*/
    623 	if (p == &proc0)
    624 		return;
    625 
    626 #ifdef __MACHINE_STACK_GROWS_UP
    627 	/* stack grows upwards (eg. hppa) */
    628 	ip = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
    629 	end = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
    630 	for (ip--; ip >= end; ip--)
    631 		if (*ip != KSTACK_MAGIC)
    632 			break;
    633 
    634 	stackleft = (caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE - (caddr_t)ip;
    635 #else /* __MACHINE_STACK_GROWS_UP */
    636 	/* stack grows downwards (eg. i386) */
    637 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
    638 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
    639 	for (; ip < end; ip++)
    640 		if (*ip != KSTACK_MAGIC)
    641 			break;
    642 
    643 	stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(p);
    644 #endif /* __MACHINE_STACK_GROWS_UP */
    645 
    646 	if (kstackleftmin > stackleft) {
    647 		kstackleftmin = stackleft;
    648 		if (stackleft < kstackleftthres)
    649 			printf("warning: kernel stack left %d bytes(pid %u)\n",
    650 			    stackleft, p->p_pid);
    651 	}
    652 
    653 	if (stackleft <= 0) {
    654 		panic("magic on the top of kernel stack changed for pid %u: "
    655 		    "maybe kernel stack overflow\n", p->p_pid);
    656 	}
    657 }
    658 #endif /* KSTACK_CHECK_MAGIC */
    659