Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.44.2.9
      1 /*	$NetBSD: kern_proc.c,v 1.44.2.9 2002/08/01 02:46:20 nathanw 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.44.2.9 2002/08/01 02:46:20 nathanw 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 #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 sadata_pool;
    161 struct pool saupcall_pool;
    162 struct pool ptimer_pool;
    163 
    164 /*
    165  * The process list descriptors, used during pid allocation and
    166  * by sysctl.  No locking on this data structure is needed since
    167  * it is completely static.
    168  */
    169 const struct proclist_desc proclists[] = {
    170 	{ &allproc	},
    171 	{ &zombproc	},
    172 	{ NULL		},
    173 };
    174 
    175 static void orphanpg __P((struct pgrp *));
    176 #ifdef DEBUG
    177 void pgrpdump __P((void));
    178 #endif
    179 
    180 /*
    181  * Initialize global process hashing structures.
    182  */
    183 void
    184 procinit()
    185 {
    186 	const struct proclist_desc *pd;
    187 
    188 	for (pd = proclists; pd->pd_list != NULL; pd++)
    189 		LIST_INIT(pd->pd_list);
    190 
    191 	spinlockinit(&proclist_lock, "proclk", 0);
    192 
    193 	LIST_INIT(&deadproc);
    194 	simple_lock_init(&deadproc_slock);
    195 
    196 	LIST_INIT(&alllwp);
    197 	LIST_INIT(&deadlwp);
    198 	LIST_INIT(&zomblwp);
    199 
    200 	pidhashtbl =
    201 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pidhash);
    202 	pgrphashtbl =
    203 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pgrphash);
    204 	uihashtbl =
    205 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
    206 
    207 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    208 	    &pool_allocator_nointr);
    209 	pool_init(&lwp_pool, sizeof(struct lwp), 0, 0, 0, "lwppl",
    210 	    &pool_allocator_nointr);
    211 	pool_init(&lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
    212 	    &pool_allocator_nointr);
    213 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    214 	    &pool_allocator_nointr);
    215 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
    216 	    &pool_allocator_nointr);
    217 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    218 	    &pool_allocator_nointr);
    219 	pool_init(&pstats_pool, sizeof(struct pstats), 0, 0, 0, "pstatspl",
    220 	    &pool_allocator_nointr);
    221 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    222 	    &pool_allocator_nointr);
    223 	pool_init(&sadata_pool, sizeof(struct sadata), 0, 0, 0, "sadatapl",
    224 	    &pool_allocator_nointr);
    225 	pool_init(&saupcall_pool, sizeof(struct sadata_upcall), 0, 0, 0,
    226 	    "saupcpl",
    227 	    &pool_allocator_nointr);
    228 	pool_init(&ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
    229 	    &pool_allocator_nointr);
    230 }
    231 
    232 /*
    233  * Acquire a read lock on the proclist.
    234  */
    235 void
    236 proclist_lock_read()
    237 {
    238 	int error;
    239 
    240 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
    241 #ifdef DIAGNOSTIC
    242 	if (__predict_false(error != 0))
    243 		panic("proclist_lock_read: failed to acquire lock");
    244 #endif
    245 }
    246 
    247 /*
    248  * Release a read lock on the proclist.
    249  */
    250 void
    251 proclist_unlock_read()
    252 {
    253 
    254 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    255 }
    256 
    257 /*
    258  * Acquire a write lock on the proclist.
    259  */
    260 int
    261 proclist_lock_write()
    262 {
    263 	int s, error;
    264 
    265 	s = splclock();
    266 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
    267 #ifdef DIAGNOSTIC
    268 	if (__predict_false(error != 0))
    269 		panic("proclist_lock: failed to acquire lock");
    270 #endif
    271 	return (s);
    272 }
    273 
    274 /*
    275  * Release a write lock on the proclist.
    276  */
    277 void
    278 proclist_unlock_write(s)
    279 	int s;
    280 {
    281 
    282 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    283 	splx(s);
    284 }
    285 
    286 /*
    287  * Change the count associated with number of processes
    288  * a given user is using.
    289  */
    290 int
    291 chgproccnt(uid, diff)
    292 	uid_t	uid;
    293 	int	diff;
    294 {
    295 	struct uidinfo *uip;
    296 	struct uihashhead *uipp;
    297 
    298 	uipp = UIHASH(uid);
    299 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
    300 		if (uip->ui_uid == uid)
    301 			break;
    302 	if (uip) {
    303 		uip->ui_proccnt += diff;
    304 		if (uip->ui_proccnt > 0)
    305 			return (uip->ui_proccnt);
    306 		if (uip->ui_proccnt < 0)
    307 			panic("chgproccnt: procs < 0");
    308 		LIST_REMOVE(uip, ui_hash);
    309 		FREE(uip, M_PROC);
    310 		return (0);
    311 	}
    312 	if (diff <= 0) {
    313 		if (diff == 0)
    314 			return(0);
    315 		panic("chgproccnt: lost user");
    316 	}
    317 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    318 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    319 	uip->ui_uid = uid;
    320 	uip->ui_proccnt = diff;
    321 	return (diff);
    322 }
    323 
    324 /*
    325  * Is p an inferior of q?
    326  */
    327 int
    328 inferior(p, q)
    329 	struct proc *p;
    330 	struct proc *q;
    331 {
    332 
    333 	for (; p != q; p = p->p_pptr)
    334 		if (p->p_pid == 0)
    335 			return (0);
    336 	return (1);
    337 }
    338 
    339 /*
    340  * Locate a process by number
    341  */
    342 struct proc *
    343 pfind(pid)
    344 	pid_t pid;
    345 {
    346 	struct proc *p;
    347 
    348 	proclist_lock_read();
    349 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
    350 		if (p->p_pid == pid)
    351 			goto out;
    352  out:
    353 	proclist_unlock_read();
    354 	return (p);
    355 }
    356 
    357 /*
    358  * Locate a process group by number
    359  */
    360 struct pgrp *
    361 pgfind(pgid)
    362 	pid_t pgid;
    363 {
    364 	struct pgrp *pgrp;
    365 
    366 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != NULL;
    367 	    pgrp = pgrp->pg_hash.le_next)
    368 		if (pgrp->pg_id == pgid)
    369 			return (pgrp);
    370 	return (NULL);
    371 }
    372 
    373 /*
    374  * Move p to a new or existing process group (and session)
    375  */
    376 int
    377 enterpgrp(p, pgid, mksess)
    378 	struct proc *p;
    379 	pid_t pgid;
    380 	int mksess;
    381 {
    382 	struct pgrp *pgrp = pgfind(pgid);
    383 
    384 #ifdef DIAGNOSTIC
    385 	if (__predict_false(pgrp != NULL && mksess))	/* firewalls */
    386 		panic("enterpgrp: setsid into non-empty pgrp");
    387 	if (__predict_false(SESS_LEADER(p)))
    388 		panic("enterpgrp: session leader attempted setpgrp");
    389 #endif
    390 	if (pgrp == NULL) {
    391 		pid_t savepid = p->p_pid;
    392 		struct proc *np;
    393 		/*
    394 		 * new process group
    395 		 */
    396 #ifdef DIAGNOSTIC
    397 		if (__predict_false(p->p_pid != pgid))
    398 			panic("enterpgrp: new pgrp and pid != pgid");
    399 #endif
    400 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    401 		if ((np = pfind(savepid)) == NULL || np != p) {
    402 			pool_put(&pgrp_pool, pgrp);
    403 			return (ESRCH);
    404 		}
    405 		if (mksess) {
    406 			struct session *sess;
    407 
    408 			/*
    409 			 * new session
    410 			 */
    411 			MALLOC(sess, struct session *, sizeof(struct session),
    412 			    M_SESSION, M_WAITOK);
    413 			if ((np = pfind(savepid)) == NULL || np != p) {
    414 				FREE(sess, M_SESSION);
    415 				pool_put(&pgrp_pool, pgrp);
    416 				return (ESRCH);
    417 			}
    418 			sess->s_sid = p->p_pid;
    419 			sess->s_leader = p;
    420 			sess->s_count = 1;
    421 			sess->s_ttyvp = NULL;
    422 			sess->s_ttyp = NULL;
    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 (p->p_pgrp->pg_members.lh_first == 0)
    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(p)
    463 	struct proc *p;
    464 {
    465 
    466 	LIST_REMOVE(p, p_pglist);
    467 	if (p->p_pgrp->pg_members.lh_first == 0)
    468 		pgdelete(p->p_pgrp);
    469 	p->p_pgrp = 0;
    470 	return (0);
    471 }
    472 
    473 /*
    474  * delete a process group
    475  */
    476 void
    477 pgdelete(pgrp)
    478 	struct pgrp *pgrp;
    479 {
    480 
    481 	/* Remove reference (if any) from tty to this process group */
    482 	if (pgrp->pg_session->s_ttyp != NULL &&
    483 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    484 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    485 	LIST_REMOVE(pgrp, pg_hash);
    486 	SESSRELE(pgrp->pg_session);
    487 	pool_put(&pgrp_pool, pgrp);
    488 }
    489 
    490 /*
    491  * Adjust pgrp jobc counters when specified process changes process group.
    492  * We count the number of processes in each process group that "qualify"
    493  * the group for terminal job control (those with a parent in a different
    494  * process group of the same session).  If that count reaches zero, the
    495  * process group becomes orphaned.  Check both the specified process'
    496  * process group and that of its children.
    497  * entering == 0 => p is leaving specified group.
    498  * entering == 1 => p is entering specified group.
    499  */
    500 void
    501 fixjobc(p, pgrp, entering)
    502 	struct proc *p;
    503 	struct pgrp *pgrp;
    504 	int entering;
    505 {
    506 	struct pgrp *hispgrp;
    507 	struct session *mysession = pgrp->pg_session;
    508 
    509 	/*
    510 	 * Check p's parent to see whether p qualifies its own process
    511 	 * group; if so, adjust count for p's process group.
    512 	 */
    513 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    514 	    hispgrp->pg_session == mysession) {
    515 		if (entering)
    516 			pgrp->pg_jobc++;
    517 		else if (--pgrp->pg_jobc == 0)
    518 			orphanpg(pgrp);
    519 	}
    520 
    521 	/*
    522 	 * Check this process' children to see whether they qualify
    523 	 * their process groups; if so, adjust counts for children's
    524 	 * process groups.
    525 	 */
    526 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    527 		if ((hispgrp = p->p_pgrp) != pgrp &&
    528 		    hispgrp->pg_session == mysession &&
    529 		    P_ZOMBIE(p) == 0) {
    530 			if (entering)
    531 				hispgrp->pg_jobc++;
    532 			else if (--hispgrp->pg_jobc == 0)
    533 				orphanpg(hispgrp);
    534 		}
    535 	}
    536 }
    537 
    538 /*
    539  * A process group has become orphaned;
    540  * if there are any stopped processes in the group,
    541  * hang-up all process in that group.
    542  */
    543 static void
    544 orphanpg(pg)
    545 	struct pgrp *pg;
    546 {
    547 	struct proc *p;
    548 
    549 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    550 		if (p->p_stat == SSTOP) {
    551 			for (p = pg->pg_members.lh_first; p != 0;
    552 			    p = p->p_pglist.le_next) {
    553 				psignal(p, SIGHUP);
    554 				psignal(p, SIGCONT);
    555 			}
    556 			return;
    557 		}
    558 	}
    559 }
    560 
    561 /* mark process as suid/sgid, reset some values do defaults */
    562 void
    563 p_sugid(p)
    564 	struct proc *p;
    565 {
    566 	struct plimit *newlim;
    567 
    568 	p->p_flag |= P_SUGID;
    569 	/* reset what needs to be reset in plimit */
    570 	if (p->p_limit->pl_corename != defcorename) {
    571 		if (p->p_limit->p_refcnt > 1 &&
    572 		    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    573 			newlim = limcopy(p->p_limit);
    574 			limfree(p->p_limit);
    575 			p->p_limit = newlim;
    576 		}
    577 		free(p->p_limit->pl_corename, M_TEMP);
    578 		p->p_limit->pl_corename = defcorename;
    579 	}
    580 }
    581 
    582 #ifdef DEBUG
    583 void
    584 pgrpdump()
    585 {
    586 	struct pgrp *pgrp;
    587 	struct proc *p;
    588 	int i;
    589 
    590 	for (i = 0; i <= pgrphash; i++) {
    591 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
    592 			printf("\tindx %d\n", i);
    593 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    594 				printf("\tpgrp %p, pgid %d, sess %p, "
    595 				    "sesscnt %d, mem %p\n",
    596 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    597 				    pgrp->pg_session->s_count,
    598 				    pgrp->pg_members.lh_first);
    599 				for (p = pgrp->pg_members.lh_first; p != 0;
    600 				    p = p->p_pglist.le_next) {
    601 					printf("\t\tpid %d addr %p pgrp %p\n",
    602 					    p->p_pid, p, p->p_pgrp);
    603 				}
    604 			}
    605 		}
    606 	}
    607 }
    608 #endif /* DEBUG */
    609 
    610 #ifdef KSTACK_CHECK_MAGIC
    611 #include <sys/user.h>
    612 
    613 #define	KSTACK_MAGIC	0xdeadbeaf
    614 
    615 /* XXX should be per process basis? */
    616 int kstackleftmin = KSTACK_SIZE;
    617 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
    618 					  less than this */
    619 
    620 void
    621 kstack_setup_magic(const struct proc *p)
    622 {
    623 	u_int32_t *ip;
    624 	u_int32_t const *end;
    625 
    626 	KASSERT(p != 0);
    627 	KASSERT(p != &proc0);
    628 
    629 	/*
    630 	 * fill all the stack with magic number
    631 	 * so that later modification on it can be detected.
    632 	 */
    633 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
    634 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
    635 	for (; ip < end; ip++) {
    636 		*ip = KSTACK_MAGIC;
    637 	}
    638 }
    639 
    640 void
    641 kstack_check_magic(const struct proc *p)
    642 {
    643 	u_int32_t const *ip, *end;
    644 	int stackleft;
    645 
    646 	KASSERT(p != 0);
    647 
    648 	/* don't check proc0 */ /*XXX*/
    649 	if (p == &proc0)
    650 		return;
    651 
    652 #ifdef __MACHINE_STACK_GROWS_UP
    653 	/* stack grows upwards (eg. hppa) */
    654 	ip = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
    655 	end = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
    656 	for (ip--; ip >= end; ip--)
    657 		if (*ip != KSTACK_MAGIC)
    658 			break;
    659 
    660 	stackleft = (caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE - (caddr_t)ip;
    661 #else /* __MACHINE_STACK_GROWS_UP */
    662 	/* stack grows downwards (eg. i386) */
    663 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
    664 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
    665 	for (; ip < end; ip++)
    666 		if (*ip != KSTACK_MAGIC)
    667 			break;
    668 
    669 	stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(p);
    670 #endif /* __MACHINE_STACK_GROWS_UP */
    671 
    672 	if (kstackleftmin > stackleft) {
    673 		kstackleftmin = stackleft;
    674 		if (stackleft < kstackleftthres)
    675 			printf("warning: kernel stack left %d bytes(pid %u)\n",
    676 			    stackleft, p->p_pid);
    677 	}
    678 
    679 	if (stackleft <= 0) {
    680 		panic("magic on the top of kernel stack changed for pid %u: "
    681 		    "maybe kernel stack overflow\n", p->p_pid);
    682 	}
    683 }
    684 #endif /* KSTACK_CHECK_MAGIC */
    685