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