Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.89
      1 /*	$NetBSD: kern_proc.c,v 1.89 2006/05/14 21:15:11 elad 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. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.89 2006/05/14 21:15:11 elad Exp $");
     73 
     74 #include "opt_kstack.h"
     75 #include "opt_maxuprc.h"
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/kernel.h>
     80 #include <sys/proc.h>
     81 #include <sys/resourcevar.h>
     82 #include <sys/buf.h>
     83 #include <sys/acct.h>
     84 #include <sys/wait.h>
     85 #include <sys/file.h>
     86 #include <ufs/ufs/quota.h>
     87 #include <sys/uio.h>
     88 #include <sys/malloc.h>
     89 #include <sys/pool.h>
     90 #include <sys/mbuf.h>
     91 #include <sys/ioctl.h>
     92 #include <sys/tty.h>
     93 #include <sys/signalvar.h>
     94 #include <sys/ras.h>
     95 #include <sys/sa.h>
     96 #include <sys/savar.h>
     97 #include <sys/filedesc.h>
     98 #include <sys/kauth.h>
     99 
    100 #include <uvm/uvm.h>
    101 #include <uvm/uvm_extern.h>
    102 
    103 /*
    104  * Other process lists
    105  */
    106 
    107 struct proclist allproc;
    108 struct proclist zombproc;	/* resources have been freed */
    109 
    110 
    111 /*
    112  * Process list locking:
    113  *
    114  * We have two types of locks on the proclists: read locks and write
    115  * locks.  Read locks can be used in interrupt context, so while we
    116  * hold the write lock, we must also block clock interrupts to
    117  * lock out any scheduling changes that may happen in interrupt
    118  * context.
    119  *
    120  * The proclist lock locks the following structures:
    121  *
    122  *	allproc
    123  *	zombproc
    124  *	pid_table
    125  */
    126 struct lock proclist_lock;
    127 
    128 /*
    129  * pid to proc lookup is done by indexing the pid_table array.
    130  * Since pid numbers are only allocated when an empty slot
    131  * has been found, there is no need to search any lists ever.
    132  * (an orphaned pgrp will lock the slot, a session will lock
    133  * the pgrp with the same number.)
    134  * If the table is too small it is reallocated with twice the
    135  * previous size and the entries 'unzipped' into the two halves.
    136  * A linked list of free entries is passed through the pt_proc
    137  * field of 'free' items - set odd to be an invalid ptr.
    138  */
    139 
    140 struct pid_table {
    141 	struct proc	*pt_proc;
    142 	struct pgrp	*pt_pgrp;
    143 };
    144 #if 1	/* strongly typed cast - should be a noop */
    145 static inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; }
    146 #else
    147 #define p2u(p) ((uint)p)
    148 #endif
    149 #define P_VALID(p) (!(p2u(p) & 1))
    150 #define P_NEXT(p) (p2u(p) >> 1)
    151 #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1))
    152 
    153 #define INITIAL_PID_TABLE_SIZE	(1 << 5)
    154 static struct pid_table *pid_table;
    155 static uint pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
    156 static uint pid_alloc_lim;	/* max we allocate before growing table */
    157 static uint pid_alloc_cnt;	/* number of allocated pids */
    158 
    159 /* links through free slots - never empty! */
    160 static uint next_free_pt, last_free_pt;
    161 static pid_t pid_max = PID_MAX;		/* largest value we allocate */
    162 
    163 /* Components of the first process -- never freed. */
    164 struct session session0;
    165 struct pgrp pgrp0;
    166 struct proc proc0;
    167 struct lwp lwp0;
    168 kauth_cred_t cred0;
    169 struct filedesc0 filedesc0;
    170 struct cwdinfo cwdi0;
    171 struct plimit limit0;
    172 struct pstats pstat0;
    173 struct vmspace vmspace0;
    174 struct sigacts sigacts0;
    175 
    176 extern struct user *proc0paddr;
    177 
    178 extern const struct emul emul_netbsd;	/* defined in kern_exec.c */
    179 
    180 int nofile = NOFILE;
    181 int maxuprc = MAXUPRC;
    182 int cmask = CMASK;
    183 
    184 POOL_INIT(proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    185     &pool_allocator_nointr);
    186 POOL_INIT(lwp_pool, sizeof(struct lwp), 0, 0, 0, "lwppl",
    187     &pool_allocator_nointr);
    188 POOL_INIT(lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
    189     &pool_allocator_nointr);
    190 POOL_INIT(pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    191     &pool_allocator_nointr);
    192 POOL_INIT(plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    193     &pool_allocator_nointr);
    194 POOL_INIT(pstats_pool, sizeof(struct pstats), 0, 0, 0, "pstatspl",
    195     &pool_allocator_nointr);
    196 POOL_INIT(rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    197     &pool_allocator_nointr);
    198 POOL_INIT(ras_pool, sizeof(struct ras), 0, 0, 0, "raspl",
    199     &pool_allocator_nointr);
    200 POOL_INIT(sadata_pool, sizeof(struct sadata), 0, 0, 0, "sadatapl",
    201     &pool_allocator_nointr);
    202 POOL_INIT(saupcall_pool, sizeof(struct sadata_upcall), 0, 0, 0, "saupcpl",
    203     &pool_allocator_nointr);
    204 POOL_INIT(sastack_pool, sizeof(struct sastack), 0, 0, 0, "sastackpl",
    205     &pool_allocator_nointr);
    206 POOL_INIT(savp_pool, sizeof(struct sadata_vp), 0, 0, 0, "savppl",
    207     &pool_allocator_nointr);
    208 POOL_INIT(session_pool, sizeof(struct session), 0, 0, 0, "sessionpl",
    209     &pool_allocator_nointr);
    210 
    211 MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data");
    212 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
    213 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
    214 
    215 /*
    216  * The process list descriptors, used during pid allocation and
    217  * by sysctl.  No locking on this data structure is needed since
    218  * it is completely static.
    219  */
    220 const struct proclist_desc proclists[] = {
    221 	{ &allproc	},
    222 	{ &zombproc	},
    223 	{ NULL		},
    224 };
    225 
    226 static void orphanpg(struct pgrp *);
    227 static void pg_delete(pid_t);
    228 
    229 /*
    230  * Initialize global process hashing structures.
    231  */
    232 void
    233 procinit(void)
    234 {
    235 	const struct proclist_desc *pd;
    236 	int i;
    237 #define	LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
    238 
    239 	for (pd = proclists; pd->pd_list != NULL; pd++)
    240 		LIST_INIT(pd->pd_list);
    241 
    242 	spinlockinit(&proclist_lock, "proclk", 0);
    243 
    244 	pid_table = malloc(INITIAL_PID_TABLE_SIZE * sizeof *pid_table,
    245 			    M_PROC, M_WAITOK);
    246 	/* Set free list running through table...
    247 	   Preset 'use count' above PID_MAX so we allocate pid 1 next. */
    248 	for (i = 0; i <= pid_tbl_mask; i++) {
    249 		pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1);
    250 		pid_table[i].pt_pgrp = 0;
    251 	}
    252 	/* slot 0 is just grabbed */
    253 	next_free_pt = 1;
    254 	/* Need to fix last entry. */
    255 	last_free_pt = pid_tbl_mask;
    256 	pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY);
    257 	/* point at which we grow table - to avoid reusing pids too often */
    258 	pid_alloc_lim = pid_tbl_mask - 1;
    259 #undef LINK_EMPTY
    260 
    261 	LIST_INIT(&alllwp);
    262 
    263 	uihashtbl =
    264 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
    265 }
    266 
    267 /*
    268  * Initialize process 0.
    269  */
    270 void
    271 proc0_init(void)
    272 {
    273 	struct proc *p;
    274 	struct pgrp *pg;
    275 	struct session *sess;
    276 	struct lwp *l;
    277 	int s;
    278 	u_int i;
    279 	rlim_t lim;
    280 
    281 	p = &proc0;
    282 	pg = &pgrp0;
    283 	sess = &session0;
    284 	l = &lwp0;
    285 
    286 	simple_lock_init(&p->p_lock);
    287 	LIST_INIT(&p->p_lwps);
    288 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    289 	p->p_nlwps = 1;
    290 	simple_lock_init(&p->p_sigctx.ps_silock);
    291 	CIRCLEQ_INIT(&p->p_sigctx.ps_siginfo);
    292 
    293 	s = proclist_lock_write();
    294 
    295 	pid_table[0].pt_proc = p;
    296 	LIST_INSERT_HEAD(&allproc, p, p_list);
    297 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    298 
    299 	p->p_pgrp = pg;
    300 	pid_table[0].pt_pgrp = pg;
    301 	LIST_INIT(&pg->pg_members);
    302 	LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
    303 
    304 	pg->pg_session = sess;
    305 	sess->s_count = 1;
    306 	sess->s_sid = 0;
    307 	sess->s_leader = p;
    308 
    309 	proclist_unlock_write(s);
    310 
    311 	/*
    312 	 * Set P_NOCLDWAIT so that kernel threads are reparented to
    313 	 * init(8) when they exit.  init(8) can easily wait them out
    314 	 * for us.
    315 	 */
    316 	p->p_flag = P_SYSTEM | P_NOCLDWAIT;
    317 	p->p_stat = SACTIVE;
    318 	p->p_nice = NZERO;
    319 	p->p_emul = &emul_netbsd;
    320 #ifdef __HAVE_SYSCALL_INTERN
    321 	(*p->p_emul->e_syscall_intern)(p);
    322 #endif
    323 	strncpy(p->p_comm, "swapper", MAXCOMLEN);
    324 
    325 	l->l_flag = L_INMEM;
    326 	l->l_stat = LSONPROC;
    327 	p->p_nrlwps = 1;
    328 
    329 	callout_init(&l->l_tsleep_ch);
    330 
    331 	/* Create credentials. */
    332 	cred0 = kauth_cred_alloc();
    333 	p->p_cred = cred0;
    334 
    335 	/* Create the CWD info. */
    336 	p->p_cwdi = &cwdi0;
    337 	cwdi0.cwdi_cmask = cmask;
    338 	cwdi0.cwdi_refcnt = 1;
    339 	simple_lock_init(&cwdi0.cwdi_slock);
    340 
    341 	/* Create the limits structures. */
    342 	p->p_limit = &limit0;
    343 	simple_lock_init(&limit0.p_slock);
    344 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
    345 		limit0.pl_rlimit[i].rlim_cur =
    346 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
    347 
    348 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
    349 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
    350 	    maxfiles < nofile ? maxfiles : nofile;
    351 
    352 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
    353 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
    354 	    maxproc < maxuprc ? maxproc : maxuprc;
    355 
    356 	lim = ptoa(uvmexp.free);
    357 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
    358 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
    359 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
    360 	limit0.pl_corename = defcorename;
    361 	limit0.p_refcnt = 1;
    362 
    363 	/* Configure virtual memory system, set vm rlimits. */
    364 	uvm_init_limits(p);
    365 
    366 	/* Initialize file descriptor table for proc0. */
    367 	p->p_fd = &filedesc0.fd_fd;
    368 	fdinit1(&filedesc0);
    369 
    370 	/*
    371 	 * Initialize proc0's vmspace, which uses the kernel pmap.
    372 	 * All kernel processes (which never have user space mappings)
    373 	 * share proc0's vmspace, and thus, the kernel pmap.
    374 	 */
    375 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    376 	    trunc_page(VM_MAX_ADDRESS));
    377 	p->p_vmspace = &vmspace0;
    378 
    379 	l->l_addr = proc0paddr;				/* XXX */
    380 
    381 	p->p_stats = &pstat0;
    382 
    383 	/* Initialize signal state for proc0. */
    384 	p->p_sigacts = &sigacts0;
    385 	siginit(p);
    386 }
    387 
    388 /*
    389  * Acquire a read lock on the proclist.
    390  */
    391 void
    392 proclist_lock_read(void)
    393 {
    394 	int error;
    395 
    396 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
    397 #ifdef DIAGNOSTIC
    398 	if (__predict_false(error != 0))
    399 		panic("proclist_lock_read: failed to acquire lock");
    400 #endif
    401 }
    402 
    403 /*
    404  * Release a read lock on the proclist.
    405  */
    406 void
    407 proclist_unlock_read(void)
    408 {
    409 
    410 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    411 }
    412 
    413 /*
    414  * Acquire a write lock on the proclist.
    415  */
    416 int
    417 proclist_lock_write(void)
    418 {
    419 	int s, error;
    420 
    421 	s = splclock();
    422 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
    423 #ifdef DIAGNOSTIC
    424 	if (__predict_false(error != 0))
    425 		panic("proclist_lock: failed to acquire lock");
    426 #endif
    427 	return s;
    428 }
    429 
    430 /*
    431  * Release a write lock on the proclist.
    432  */
    433 void
    434 proclist_unlock_write(int s)
    435 {
    436 
    437 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    438 	splx(s);
    439 }
    440 
    441 /*
    442  * Check that the specified process group is in the session of the
    443  * specified process.
    444  * Treats -ve ids as process ids.
    445  * Used to validate TIOCSPGRP requests.
    446  */
    447 int
    448 pgid_in_session(struct proc *p, pid_t pg_id)
    449 {
    450 	struct pgrp *pgrp;
    451 
    452 	if (pg_id < 0) {
    453 		struct proc *p1 = pfind(-pg_id);
    454 		if (p1 == NULL)
    455 			return EINVAL;
    456 		pgrp = p1->p_pgrp;
    457 	} else {
    458 		pgrp = pgfind(pg_id);
    459 		if (pgrp == NULL)
    460 			return EINVAL;
    461 	}
    462 	if (pgrp->pg_session != p->p_pgrp->pg_session)
    463 		return EPERM;
    464 	return 0;
    465 }
    466 
    467 /*
    468  * Is p an inferior of q?
    469  */
    470 int
    471 inferior(struct proc *p, struct proc *q)
    472 {
    473 
    474 	for (; p != q; p = p->p_pptr)
    475 		if (p->p_pid == 0)
    476 			return 0;
    477 	return 1;
    478 }
    479 
    480 /*
    481  * Locate a process by number
    482  */
    483 struct proc *
    484 p_find(pid_t pid, uint flags)
    485 {
    486 	struct proc *p;
    487 	char stat;
    488 
    489 	if (!(flags & PFIND_LOCKED))
    490 		proclist_lock_read();
    491 	p = pid_table[pid & pid_tbl_mask].pt_proc;
    492 	/* Only allow live processes to be found by pid. */
    493 	if (P_VALID(p) && p->p_pid == pid &&
    494 	    ((stat = p->p_stat) == SACTIVE || stat == SSTOP
    495 		    || (stat == SZOMB && (flags & PFIND_ZOMBIE)))) {
    496 		if (flags & PFIND_UNLOCK_OK)
    497 			 proclist_unlock_read();
    498 		return p;
    499 	}
    500 	if (flags & PFIND_UNLOCK_FAIL)
    501 		 proclist_unlock_read();
    502 	return NULL;
    503 }
    504 
    505 
    506 /*
    507  * Locate a process group by number
    508  */
    509 struct pgrp *
    510 pg_find(pid_t pgid, uint flags)
    511 {
    512 	struct pgrp *pg;
    513 
    514 	if (!(flags & PFIND_LOCKED))
    515 		proclist_lock_read();
    516 	pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    517 	/*
    518 	 * Can't look up a pgrp that only exists because the session
    519 	 * hasn't died yet (traditional)
    520 	 */
    521 	if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
    522 		if (flags & PFIND_UNLOCK_FAIL)
    523 			 proclist_unlock_read();
    524 		return NULL;
    525 	}
    526 
    527 	if (flags & PFIND_UNLOCK_OK)
    528 		proclist_unlock_read();
    529 	return pg;
    530 }
    531 
    532 static void
    533 expand_pid_table(void)
    534 {
    535 	uint pt_size = pid_tbl_mask + 1;
    536 	struct pid_table *n_pt, *new_pt;
    537 	struct proc *proc;
    538 	struct pgrp *pgrp;
    539 	int i;
    540 	int s;
    541 	pid_t pid;
    542 
    543 	new_pt = malloc(pt_size * 2 * sizeof *new_pt, M_PROC, M_WAITOK);
    544 
    545 	s = proclist_lock_write();
    546 	if (pt_size != pid_tbl_mask + 1) {
    547 		/* Another process beat us to it... */
    548 		proclist_unlock_write(s);
    549 		FREE(new_pt, M_PROC);
    550 		return;
    551 	}
    552 
    553 	/*
    554 	 * Copy entries from old table into new one.
    555 	 * If 'pid' is 'odd' we need to place in the upper half,
    556 	 * even pid's to the lower half.
    557 	 * Free items stay in the low half so we don't have to
    558 	 * fixup the reference to them.
    559 	 * We stuff free items on the front of the freelist
    560 	 * because we can't write to unmodified entries.
    561 	 * Processing the table backwards maintains a semblance
    562 	 * of issueing pid numbers that increase with time.
    563 	 */
    564 	i = pt_size - 1;
    565 	n_pt = new_pt + i;
    566 	for (; ; i--, n_pt--) {
    567 		proc = pid_table[i].pt_proc;
    568 		pgrp = pid_table[i].pt_pgrp;
    569 		if (!P_VALID(proc)) {
    570 			/* Up 'use count' so that link is valid */
    571 			pid = (P_NEXT(proc) + pt_size) & ~pt_size;
    572 			proc = P_FREE(pid);
    573 			if (pgrp)
    574 				pid = pgrp->pg_id;
    575 		} else
    576 			pid = proc->p_pid;
    577 
    578 		/* Save entry in appropriate half of table */
    579 		n_pt[pid & pt_size].pt_proc = proc;
    580 		n_pt[pid & pt_size].pt_pgrp = pgrp;
    581 
    582 		/* Put other piece on start of free list */
    583 		pid = (pid ^ pt_size) & ~pid_tbl_mask;
    584 		n_pt[pid & pt_size].pt_proc =
    585 				    P_FREE((pid & ~pt_size) | next_free_pt);
    586 		n_pt[pid & pt_size].pt_pgrp = 0;
    587 		next_free_pt = i | (pid & pt_size);
    588 		if (i == 0)
    589 			break;
    590 	}
    591 
    592 	/* Switch tables */
    593 	n_pt = pid_table;
    594 	pid_table = new_pt;
    595 	pid_tbl_mask = pt_size * 2 - 1;
    596 
    597 	/*
    598 	 * pid_max starts as PID_MAX (= 30000), once we have 16384
    599 	 * allocated pids we need it to be larger!
    600 	 */
    601 	if (pid_tbl_mask > PID_MAX) {
    602 		pid_max = pid_tbl_mask * 2 + 1;
    603 		pid_alloc_lim |= pid_alloc_lim << 1;
    604 	} else
    605 		pid_alloc_lim <<= 1;	/* doubles number of free slots... */
    606 
    607 	proclist_unlock_write(s);
    608 	FREE(n_pt, M_PROC);
    609 }
    610 
    611 struct proc *
    612 proc_alloc(void)
    613 {
    614 	struct proc *p;
    615 	int s;
    616 	int nxt;
    617 	pid_t pid;
    618 	struct pid_table *pt;
    619 
    620 	p = pool_get(&proc_pool, PR_WAITOK);
    621 	p->p_stat = SIDL;			/* protect against others */
    622 
    623 	/* allocate next free pid */
    624 
    625 	for (;;expand_pid_table()) {
    626 		if (__predict_false(pid_alloc_cnt >= pid_alloc_lim))
    627 			/* ensure pids cycle through 2000+ values */
    628 			continue;
    629 		s = proclist_lock_write();
    630 		pt = &pid_table[next_free_pt];
    631 #ifdef DIAGNOSTIC
    632 		if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp))
    633 			panic("proc_alloc: slot busy");
    634 #endif
    635 		nxt = P_NEXT(pt->pt_proc);
    636 		if (nxt & pid_tbl_mask)
    637 			break;
    638 		/* Table full - expand (NB last entry not used....) */
    639 		proclist_unlock_write(s);
    640 	}
    641 
    642 	/* pid is 'saved use count' + 'size' + entry */
    643 	pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
    644 	if ((uint)pid > (uint)pid_max)
    645 		pid &= pid_tbl_mask;
    646 	p->p_pid = pid;
    647 	next_free_pt = nxt & pid_tbl_mask;
    648 
    649 	/* Grab table slot */
    650 	pt->pt_proc = p;
    651 	pid_alloc_cnt++;
    652 
    653 	proclist_unlock_write(s);
    654 
    655 	return p;
    656 }
    657 
    658 /*
    659  * Free last resources of a process - called from proc_free (in kern_exit.c)
    660  */
    661 void
    662 proc_free_mem(struct proc *p)
    663 {
    664 	int s;
    665 	pid_t pid = p->p_pid;
    666 	struct pid_table *pt;
    667 
    668 	s = proclist_lock_write();
    669 
    670 	pt = &pid_table[pid & pid_tbl_mask];
    671 #ifdef DIAGNOSTIC
    672 	if (__predict_false(pt->pt_proc != p))
    673 		panic("proc_free: pid_table mismatch, pid %x, proc %p",
    674 			pid, p);
    675 #endif
    676 	/* save pid use count in slot */
    677 	pt->pt_proc = P_FREE(pid & ~pid_tbl_mask);
    678 
    679 	if (pt->pt_pgrp == NULL) {
    680 		/* link last freed entry onto ours */
    681 		pid &= pid_tbl_mask;
    682 		pt = &pid_table[last_free_pt];
    683 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid);
    684 		last_free_pt = pid;
    685 		pid_alloc_cnt--;
    686 	}
    687 
    688 	nprocs--;
    689 	proclist_unlock_write(s);
    690 
    691 	pool_put(&proc_pool, p);
    692 }
    693 
    694 /*
    695  * Move p to a new or existing process group (and session)
    696  *
    697  * If we are creating a new pgrp, the pgid should equal
    698  * the calling process' pid.
    699  * If is only valid to enter a process group that is in the session
    700  * of the process.
    701  * Also mksess should only be set if we are creating a process group
    702  *
    703  * Only called from sys_setsid, sys_setpgid/sys_setpgrp and the
    704  * SYSV setpgrp support for hpux == enterpgrp(curproc, curproc->p_pid)
    705  */
    706 int
    707 enterpgrp(struct proc *p, pid_t pgid, int mksess)
    708 {
    709 	struct pgrp *new_pgrp, *pgrp;
    710 	struct session *sess;
    711 	struct proc *curp = curproc;
    712 	pid_t pid = p->p_pid;
    713 	int rval;
    714 	int s;
    715 	pid_t pg_id = NO_PGID;
    716 
    717 	/* Allocate data areas we might need before doing any validity checks */
    718 	proclist_lock_read();		/* Because pid_table might change */
    719 	if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
    720 		proclist_unlock_read();
    721 		new_pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    722 	} else {
    723 		proclist_unlock_read();
    724 		new_pgrp = NULL;
    725 	}
    726 	if (mksess)
    727 		sess = pool_get(&session_pool, M_WAITOK);
    728 	else
    729 		sess = NULL;
    730 
    731 	s = proclist_lock_write();
    732 	rval = EPERM;	/* most common error (to save typing) */
    733 
    734 	/* Check pgrp exists or can be created */
    735 	pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    736 	if (pgrp != NULL && pgrp->pg_id != pgid)
    737 		goto done;
    738 
    739 	/* Can only set another process under restricted circumstances. */
    740 	if (p != curp) {
    741 		/* must exist and be one of our children... */
    742 		if (p != pid_table[pid & pid_tbl_mask].pt_proc
    743 		    || !inferior(p, curp)) {
    744 			rval = ESRCH;
    745 			goto done;
    746 		}
    747 		/* ... in the same session... */
    748 		if (sess != NULL || p->p_session != curp->p_session)
    749 			goto done;
    750 		/* ... existing pgid must be in same session ... */
    751 		if (pgrp != NULL && pgrp->pg_session != p->p_session)
    752 			goto done;
    753 		/* ... and not done an exec. */
    754 		if (p->p_flag & P_EXEC) {
    755 			rval = EACCES;
    756 			goto done;
    757 		}
    758 	}
    759 
    760 	/* Changing the process group/session of a session
    761 	   leader is definitely off limits. */
    762 	if (SESS_LEADER(p)) {
    763 		if (sess == NULL && p->p_pgrp == pgrp)
    764 			/* unless it's a definite noop */
    765 			rval = 0;
    766 		goto done;
    767 	}
    768 
    769 	/* Can only create a process group with id of process */
    770 	if (pgrp == NULL && pgid != pid)
    771 		goto done;
    772 
    773 	/* Can only create a session if creating pgrp */
    774 	if (sess != NULL && pgrp != NULL)
    775 		goto done;
    776 
    777 	/* Check we allocated memory for a pgrp... */
    778 	if (pgrp == NULL && new_pgrp == NULL)
    779 		goto done;
    780 
    781 	/* Don't attach to 'zombie' pgrp */
    782 	if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
    783 		goto done;
    784 
    785 	/* Expect to succeed now */
    786 	rval = 0;
    787 
    788 	if (pgrp == p->p_pgrp)
    789 		/* nothing to do */
    790 		goto done;
    791 
    792 	/* Ok all setup, link up required structures */
    793 	if (pgrp == NULL) {
    794 		pgrp = new_pgrp;
    795 		new_pgrp = 0;
    796 		if (sess != NULL) {
    797 			sess->s_sid = p->p_pid;
    798 			sess->s_leader = p;
    799 			sess->s_count = 1;
    800 			sess->s_ttyvp = NULL;
    801 			sess->s_ttyp = NULL;
    802 			sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
    803 			memcpy(sess->s_login, p->p_session->s_login,
    804 			    sizeof(sess->s_login));
    805 			p->p_flag &= ~P_CONTROLT;
    806 		} else {
    807 			sess = p->p_pgrp->pg_session;
    808 			SESSHOLD(sess);
    809 		}
    810 		pgrp->pg_session = sess;
    811 		sess = 0;
    812 
    813 		pgrp->pg_id = pgid;
    814 		LIST_INIT(&pgrp->pg_members);
    815 #ifdef DIAGNOSTIC
    816 		if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
    817 			panic("enterpgrp: pgrp table slot in use");
    818 		if (__predict_false(mksess && p != curp))
    819 			panic("enterpgrp: mksession and p != curproc");
    820 #endif
    821 		pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
    822 		pgrp->pg_jobc = 0;
    823 	}
    824 
    825 	/*
    826 	 * Adjust eligibility of affected pgrps to participate in job control.
    827 	 * Increment eligibility counts before decrementing, otherwise we
    828 	 * could reach 0 spuriously during the first call.
    829 	 */
    830 	fixjobc(p, pgrp, 1);
    831 	fixjobc(p, p->p_pgrp, 0);
    832 
    833 	/* Move process to requested group */
    834 	LIST_REMOVE(p, p_pglist);
    835 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
    836 		/* defer delete until we've dumped the lock */
    837 		pg_id = p->p_pgrp->pg_id;
    838 	p->p_pgrp = pgrp;
    839 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    840 
    841     done:
    842 	proclist_unlock_write(s);
    843 	if (sess != NULL)
    844 		pool_put(&session_pool, sess);
    845 	if (new_pgrp != NULL)
    846 		pool_put(&pgrp_pool, new_pgrp);
    847 	if (pg_id != NO_PGID)
    848 		pg_delete(pg_id);
    849 #ifdef DEBUG_PGRP
    850 	if (__predict_false(rval))
    851 		printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
    852 			pid, pgid, mksess, curp->p_pid, rval);
    853 #endif
    854 	return rval;
    855 }
    856 
    857 /*
    858  * remove process from process group
    859  */
    860 int
    861 leavepgrp(struct proc *p)
    862 {
    863 	int s;
    864 	struct pgrp *pgrp;
    865 	pid_t pg_id;
    866 
    867 	s = proclist_lock_write();
    868 	pgrp = p->p_pgrp;
    869 	LIST_REMOVE(p, p_pglist);
    870 	p->p_pgrp = 0;
    871 	pg_id = LIST_EMPTY(&pgrp->pg_members) ? pgrp->pg_id : NO_PGID;
    872 	proclist_unlock_write(s);
    873 
    874 	if (pg_id != NO_PGID)
    875 		pg_delete(pg_id);
    876 	return 0;
    877 }
    878 
    879 static void
    880 pg_free(pid_t pg_id)
    881 {
    882 	struct pgrp *pgrp;
    883 	struct pid_table *pt;
    884 	int s;
    885 
    886 	s = proclist_lock_write();
    887 	pt = &pid_table[pg_id & pid_tbl_mask];
    888 	pgrp = pt->pt_pgrp;
    889 #ifdef DIAGNOSTIC
    890 	if (__predict_false(!pgrp || pgrp->pg_id != pg_id
    891 	    || !LIST_EMPTY(&pgrp->pg_members)))
    892 		panic("pg_free: process group absent or has members");
    893 #endif
    894 	pt->pt_pgrp = 0;
    895 
    896 	if (!P_VALID(pt->pt_proc)) {
    897 		/* orphaned pgrp, put slot onto free list */
    898 #ifdef DIAGNOSTIC
    899 		if (__predict_false(P_NEXT(pt->pt_proc) & pid_tbl_mask))
    900 			panic("pg_free: process slot on free list");
    901 #endif
    902 
    903 		pg_id &= pid_tbl_mask;
    904 		pt = &pid_table[last_free_pt];
    905 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
    906 		last_free_pt = pg_id;
    907 		pid_alloc_cnt--;
    908 	}
    909 	proclist_unlock_write(s);
    910 
    911 	pool_put(&pgrp_pool, pgrp);
    912 }
    913 
    914 /*
    915  * delete a process group
    916  */
    917 static void
    918 pg_delete(pid_t pg_id)
    919 {
    920 	struct pgrp *pgrp;
    921 	struct tty *ttyp;
    922 	struct session *ss;
    923 	int s, is_pgrp_leader;
    924 
    925 	s = proclist_lock_write();
    926 	pgrp = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
    927 	if (pgrp == NULL || pgrp->pg_id != pg_id ||
    928 	    !LIST_EMPTY(&pgrp->pg_members)) {
    929 		proclist_unlock_write(s);
    930 		return;
    931 	}
    932 
    933 	ss = pgrp->pg_session;
    934 
    935 	/* Remove reference (if any) from tty to this process group */
    936 	ttyp = ss->s_ttyp;
    937 	if (ttyp != NULL && ttyp->t_pgrp == pgrp) {
    938 		ttyp->t_pgrp = NULL;
    939 #ifdef DIAGNOSTIC
    940 		if (ttyp->t_session != ss)
    941 			panic("pg_delete: wrong session on terminal");
    942 #endif
    943 	}
    944 
    945 	/*
    946 	 * The leading process group in a session is freed
    947 	 * by sessdelete() if last reference.
    948 	 */
    949 	is_pgrp_leader = (ss->s_sid == pgrp->pg_id);
    950 	proclist_unlock_write(s);
    951 	SESSRELE(ss);
    952 
    953 	if (is_pgrp_leader)
    954 		return;
    955 
    956 	pg_free(pg_id);
    957 }
    958 
    959 /*
    960  * Delete session - called from SESSRELE when s_count becomes zero.
    961  */
    962 void
    963 sessdelete(struct session *ss)
    964 {
    965 	/*
    966 	 * We keep the pgrp with the same id as the session in
    967 	 * order to stop a process being given the same pid.
    968 	 * Since the pgrp holds a reference to the session, it
    969 	 * must be a 'zombie' pgrp by now.
    970 	 */
    971 
    972 	pg_free(ss->s_sid);
    973 
    974 	pool_put(&session_pool, ss);
    975 }
    976 
    977 /*
    978  * Adjust pgrp jobc counters when specified process changes process group.
    979  * We count the number of processes in each process group that "qualify"
    980  * the group for terminal job control (those with a parent in a different
    981  * process group of the same session).  If that count reaches zero, the
    982  * process group becomes orphaned.  Check both the specified process'
    983  * process group and that of its children.
    984  * entering == 0 => p is leaving specified group.
    985  * entering == 1 => p is entering specified group.
    986  *
    987  * Call with proclist_lock held.
    988  */
    989 void
    990 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
    991 {
    992 	struct pgrp *hispgrp;
    993 	struct session *mysession = pgrp->pg_session;
    994 	struct proc *child;
    995 
    996 	/*
    997 	 * Check p's parent to see whether p qualifies its own process
    998 	 * group; if so, adjust count for p's process group.
    999 	 */
   1000 	hispgrp = p->p_pptr->p_pgrp;
   1001 	if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
   1002 		if (entering)
   1003 			pgrp->pg_jobc++;
   1004 		else if (--pgrp->pg_jobc == 0)
   1005 			orphanpg(pgrp);
   1006 	}
   1007 
   1008 	/*
   1009 	 * Check this process' children to see whether they qualify
   1010 	 * their process groups; if so, adjust counts for children's
   1011 	 * process groups.
   1012 	 */
   1013 	LIST_FOREACH(child, &p->p_children, p_sibling) {
   1014 		hispgrp = child->p_pgrp;
   1015 		if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
   1016 		    !P_ZOMBIE(child)) {
   1017 			if (entering)
   1018 				hispgrp->pg_jobc++;
   1019 			else if (--hispgrp->pg_jobc == 0)
   1020 				orphanpg(hispgrp);
   1021 		}
   1022 	}
   1023 }
   1024 
   1025 /*
   1026  * A process group has become orphaned;
   1027  * if there are any stopped processes in the group,
   1028  * hang-up all process in that group.
   1029  *
   1030  * Call with proclist_lock held.
   1031  */
   1032 static void
   1033 orphanpg(struct pgrp *pg)
   1034 {
   1035 	struct proc *p;
   1036 
   1037 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1038 		if (p->p_stat == SSTOP) {
   1039 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1040 				psignal(p, SIGHUP);
   1041 				psignal(p, SIGCONT);
   1042 			}
   1043 			return;
   1044 		}
   1045 	}
   1046 }
   1047 
   1048 /* mark process as suid/sgid, reset some values to defaults */
   1049 void
   1050 p_sugid(struct proc *p)
   1051 {
   1052 	struct plimit *lim;
   1053 	char *cn;
   1054 
   1055 	p->p_flag |= P_SUGID;
   1056 	/* reset what needs to be reset in plimit */
   1057 	lim = p->p_limit;
   1058 	if (lim->pl_corename != defcorename) {
   1059 		if (lim->p_refcnt > 1 &&
   1060 		    (lim->p_lflags & PL_SHAREMOD) == 0) {
   1061 			p->p_limit = limcopy(lim);
   1062 			limfree(lim);
   1063 			lim = p->p_limit;
   1064 		}
   1065 		simple_lock(&lim->p_slock);
   1066 		cn = lim->pl_corename;
   1067 		lim->pl_corename = defcorename;
   1068 		simple_unlock(&lim->p_slock);
   1069 		if (cn != defcorename)
   1070 			free(cn, M_TEMP);
   1071 	}
   1072 }
   1073 
   1074 #ifdef DDB
   1075 #include <ddb/db_output.h>
   1076 void pidtbl_dump(void);
   1077 void
   1078 pidtbl_dump(void)
   1079 {
   1080 	struct pid_table *pt;
   1081 	struct proc *p;
   1082 	struct pgrp *pgrp;
   1083 	int id;
   1084 
   1085 	db_printf("pid table %p size %x, next %x, last %x\n",
   1086 		pid_table, pid_tbl_mask+1,
   1087 		next_free_pt, last_free_pt);
   1088 	for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
   1089 		p = pt->pt_proc;
   1090 		if (!P_VALID(p) && !pt->pt_pgrp)
   1091 			continue;
   1092 		db_printf("  id %x: ", id);
   1093 		if (P_VALID(p))
   1094 			db_printf("proc %p id %d (0x%x) %s\n",
   1095 				p, p->p_pid, p->p_pid, p->p_comm);
   1096 		else
   1097 			db_printf("next %x use %x\n",
   1098 				P_NEXT(p) & pid_tbl_mask,
   1099 				P_NEXT(p) & ~pid_tbl_mask);
   1100 		if ((pgrp = pt->pt_pgrp)) {
   1101 			db_printf("\tsession %p, sid %d, count %d, login %s\n",
   1102 			    pgrp->pg_session, pgrp->pg_session->s_sid,
   1103 			    pgrp->pg_session->s_count,
   1104 			    pgrp->pg_session->s_login);
   1105 			db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
   1106 			    pgrp, pgrp->pg_id, pgrp->pg_jobc,
   1107 			    pgrp->pg_members.lh_first);
   1108 			for (p = pgrp->pg_members.lh_first; p != 0;
   1109 			    p = p->p_pglist.le_next) {
   1110 				db_printf("\t\tpid %d addr %p pgrp %p %s\n",
   1111 				    p->p_pid, p, p->p_pgrp, p->p_comm);
   1112 			}
   1113 		}
   1114 	}
   1115 }
   1116 #endif /* DDB */
   1117 
   1118 #ifdef KSTACK_CHECK_MAGIC
   1119 #include <sys/user.h>
   1120 
   1121 #define	KSTACK_MAGIC	0xdeadbeaf
   1122 
   1123 /* XXX should be per process basis? */
   1124 int kstackleftmin = KSTACK_SIZE;
   1125 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
   1126 					  less than this */
   1127 
   1128 void
   1129 kstack_setup_magic(const struct lwp *l)
   1130 {
   1131 	uint32_t *ip;
   1132 	uint32_t const *end;
   1133 
   1134 	KASSERT(l != NULL);
   1135 	KASSERT(l != &lwp0);
   1136 
   1137 	/*
   1138 	 * fill all the stack with magic number
   1139 	 * so that later modification on it can be detected.
   1140 	 */
   1141 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1142 	end = (uint32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1143 	for (; ip < end; ip++) {
   1144 		*ip = KSTACK_MAGIC;
   1145 	}
   1146 }
   1147 
   1148 void
   1149 kstack_check_magic(const struct lwp *l)
   1150 {
   1151 	uint32_t const *ip, *end;
   1152 	int stackleft;
   1153 
   1154 	KASSERT(l != NULL);
   1155 
   1156 	/* don't check proc0 */ /*XXX*/
   1157 	if (l == &lwp0)
   1158 		return;
   1159 
   1160 #ifdef __MACHINE_STACK_GROWS_UP
   1161 	/* stack grows upwards (eg. hppa) */
   1162 	ip = (uint32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1163 	end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1164 	for (ip--; ip >= end; ip--)
   1165 		if (*ip != KSTACK_MAGIC)
   1166 			break;
   1167 
   1168 	stackleft = (caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (caddr_t)ip;
   1169 #else /* __MACHINE_STACK_GROWS_UP */
   1170 	/* stack grows downwards (eg. i386) */
   1171 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1172 	end = (uint32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1173 	for (; ip < end; ip++)
   1174 		if (*ip != KSTACK_MAGIC)
   1175 			break;
   1176 
   1177 	stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(l);
   1178 #endif /* __MACHINE_STACK_GROWS_UP */
   1179 
   1180 	if (kstackleftmin > stackleft) {
   1181 		kstackleftmin = stackleft;
   1182 		if (stackleft < kstackleftthres)
   1183 			printf("warning: kernel stack left %d bytes"
   1184 			    "(pid %u:lid %u)\n", stackleft,
   1185 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1186 	}
   1187 
   1188 	if (stackleft <= 0) {
   1189 		panic("magic on the top of kernel stack changed for "
   1190 		    "pid %u, lid %u: maybe kernel stack overflow",
   1191 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1192 	}
   1193 }
   1194 #endif /* KSTACK_CHECK_MAGIC */
   1195 
   1196 /* XXX shouldn't be here */
   1197 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1198 #define	PROCLIST_ASSERT_LOCKED_READ()	\
   1199 	KASSERT(lockstatus(&proclist_lock) == LK_SHARED)
   1200 #else
   1201 #define	PROCLIST_ASSERT_LOCKED_READ()	/* nothing */
   1202 #endif
   1203 
   1204 int
   1205 proclist_foreach_call(struct proclist *list,
   1206     int (*callback)(struct proc *, void *arg), void *arg)
   1207 {
   1208 	struct proc marker;
   1209 	struct proc *p;
   1210 	struct lwp * const l = curlwp;
   1211 	int ret = 0;
   1212 
   1213 	marker.p_flag = P_MARKER;
   1214 	PHOLD(l);
   1215 	proclist_lock_read();
   1216 	for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
   1217 		if (p->p_flag & P_MARKER) {
   1218 			p = LIST_NEXT(p, p_list);
   1219 			continue;
   1220 		}
   1221 		LIST_INSERT_AFTER(p, &marker, p_list);
   1222 		ret = (*callback)(p, arg);
   1223 		PROCLIST_ASSERT_LOCKED_READ();
   1224 		p = LIST_NEXT(&marker, p_list);
   1225 		LIST_REMOVE(&marker, p_list);
   1226 	}
   1227 	proclist_unlock_read();
   1228 	PRELE(l);
   1229 
   1230 	return ret;
   1231 }
   1232 
   1233 int
   1234 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
   1235 {
   1236 
   1237 	/* XXXCDC: how should locking work here? */
   1238 
   1239 	/* curproc exception is for coredump. */
   1240 
   1241 	if ((p != curproc && (p->p_flag & P_WEXIT) != 0) ||
   1242 	    (p->p_vmspace->vm_refcnt < 1)) { /* XXX */
   1243 		return EFAULT;
   1244 	}
   1245 
   1246 	uvmspace_addref(p->p_vmspace);
   1247 	*vm = p->p_vmspace;
   1248 
   1249 	return 0;
   1250 }
   1251