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