Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.100.2.1
      1 /*	$NetBSD: kern_proc.c,v 1.100.2.1 2007/02/17 10:30:57 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2006, 2007 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, and by Andrew Doran.
     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.100.2.1 2007/02/17 10:30:57 yamt Exp $");
     73 
     74 #include "opt_kstack.h"
     75 #include "opt_maxuprc.h"
     76 #include "opt_multiprocessor.h"
     77 #include "opt_lockdebug.h"
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/proc.h>
     83 #include <sys/resourcevar.h>
     84 #include <sys/buf.h>
     85 #include <sys/acct.h>
     86 #include <sys/wait.h>
     87 #include <sys/file.h>
     88 #include <ufs/ufs/quota.h>
     89 #include <sys/uio.h>
     90 #include <sys/malloc.h>
     91 #include <sys/pool.h>
     92 #include <sys/mbuf.h>
     93 #include <sys/ioctl.h>
     94 #include <sys/tty.h>
     95 #include <sys/signalvar.h>
     96 #include <sys/ras.h>
     97 #include <sys/filedesc.h>
     98 #include <sys/kauth.h>
     99 #include <sys/sleepq.h>
    100 
    101 #include <uvm/uvm.h>
    102 #include <uvm/uvm_extern.h>
    103 
    104 /*
    105  * Other process lists
    106  */
    107 
    108 struct proclist allproc;
    109 struct proclist zombproc;	/* resources have been freed */
    110 
    111 /*
    112  * There are two locks on global process state.
    113  *
    114  * 1. proclist_lock is a reader/writer lock and is used when modifying or
    115  * examining process state from a process context.  It protects our internal
    116  * tables, all of the process lists, and a number of members of struct lwp
    117  * and struct proc.
    118 
    119  * 2. proclist_mutex is used when allproc must be traversed from an
    120  * interrupt context, or when we must signal processes from an interrupt
    121  * context.  The proclist_lock should always be used in preference.
    122  *
    123  *	proclist_lock	proclist_mutex	structure
    124  *	--------------- --------------- -----------------
    125  *	x				zombproc
    126  *	x		x		pid_table
    127  *	x				proc::p_pptr
    128  *	x				proc::p_sibling
    129  *	x				proc::p_children
    130  *	x		x		allproc
    131  *	x		x		proc::p_pgrp
    132  *	x		x		proc::p_pglist
    133  *	x		x		proc::p_session
    134  *	x		x		proc::p_list
    135  *			x		alllwp
    136  *			x		lwp::l_list
    137  *
    138  * The lock order for processes and LWPs is approximately as following:
    139  *
    140  * kernel_mutex
    141  * -> proclist_lock
    142  *    -> proclist_mutex
    143  *	-> proc::p_mutex
    144  *         -> proc::p_smutex
    145  */
    146 krwlock_t	proclist_lock;
    147 kmutex_t	proclist_mutex;
    148 
    149 /*
    150  * pid to proc lookup is done by indexing the pid_table array.
    151  * Since pid numbers are only allocated when an empty slot
    152  * has been found, there is no need to search any lists ever.
    153  * (an orphaned pgrp will lock the slot, a session will lock
    154  * the pgrp with the same number.)
    155  * If the table is too small it is reallocated with twice the
    156  * previous size and the entries 'unzipped' into the two halves.
    157  * A linked list of free entries is passed through the pt_proc
    158  * field of 'free' items - set odd to be an invalid ptr.
    159  */
    160 
    161 struct pid_table {
    162 	struct proc	*pt_proc;
    163 	struct pgrp	*pt_pgrp;
    164 };
    165 #if 1	/* strongly typed cast - should be a noop */
    166 static inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; }
    167 #else
    168 #define p2u(p) ((uint)p)
    169 #endif
    170 #define P_VALID(p) (!(p2u(p) & 1))
    171 #define P_NEXT(p) (p2u(p) >> 1)
    172 #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1))
    173 
    174 #define INITIAL_PID_TABLE_SIZE	(1 << 5)
    175 static struct pid_table *pid_table;
    176 static uint pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
    177 static uint pid_alloc_lim;	/* max we allocate before growing table */
    178 static uint pid_alloc_cnt;	/* number of allocated pids */
    179 
    180 /* links through free slots - never empty! */
    181 static uint next_free_pt, last_free_pt;
    182 static pid_t pid_max = PID_MAX;		/* largest value we allocate */
    183 
    184 /* Components of the first process -- never freed. */
    185 struct session session0;
    186 struct pgrp pgrp0;
    187 struct proc proc0;
    188 struct lwp lwp0 __aligned(MIN_LWP_ALIGNMENT);
    189 kauth_cred_t cred0;
    190 struct filedesc0 filedesc0;
    191 struct cwdinfo cwdi0;
    192 struct plimit limit0;
    193 struct pstats pstat0;
    194 struct vmspace vmspace0;
    195 struct sigacts sigacts0;
    196 struct turnstile turnstile0;
    197 
    198 extern struct user *proc0paddr;
    199 
    200 extern const struct emul emul_netbsd;	/* defined in kern_exec.c */
    201 
    202 int nofile = NOFILE;
    203 int maxuprc = MAXUPRC;
    204 int cmask = CMASK;
    205 
    206 POOL_INIT(proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    207     &pool_allocator_nointr);
    208 POOL_INIT(pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    209     &pool_allocator_nointr);
    210 POOL_INIT(plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    211     &pool_allocator_nointr);
    212 POOL_INIT(pstats_pool, sizeof(struct pstats), 0, 0, 0, "pstatspl",
    213     &pool_allocator_nointr);
    214 POOL_INIT(rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    215     &pool_allocator_nointr);
    216 POOL_INIT(session_pool, sizeof(struct session), 0, 0, 0, "sessionpl",
    217     &pool_allocator_nointr);
    218 
    219 MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data");
    220 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
    221 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
    222 
    223 /*
    224  * The process list descriptors, used during pid allocation and
    225  * by sysctl.  No locking on this data structure is needed since
    226  * it is completely static.
    227  */
    228 const struct proclist_desc proclists[] = {
    229 	{ &allproc	},
    230 	{ &zombproc	},
    231 	{ NULL		},
    232 };
    233 
    234 static void orphanpg(struct pgrp *);
    235 static void pg_delete(pid_t);
    236 
    237 static specificdata_domain_t proc_specificdata_domain;
    238 
    239 /*
    240  * Initialize global process hashing structures.
    241  */
    242 void
    243 procinit(void)
    244 {
    245 	const struct proclist_desc *pd;
    246 	int i;
    247 #define	LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
    248 
    249 	for (pd = proclists; pd->pd_list != NULL; pd++)
    250 		LIST_INIT(pd->pd_list);
    251 
    252 	/*
    253 	 * XXX p_smutex can be IPL_VM except for audio drivers
    254 	 * XXX proclist_lock must die
    255 	 */
    256 	rw_init(&proclist_lock);
    257 	mutex_init(&proclist_mutex, MUTEX_SPIN, IPL_SCHED);
    258 
    259 	pid_table = malloc(INITIAL_PID_TABLE_SIZE * sizeof *pid_table,
    260 			    M_PROC, M_WAITOK);
    261 	/* Set free list running through table...
    262 	   Preset 'use count' above PID_MAX so we allocate pid 1 next. */
    263 	for (i = 0; i <= pid_tbl_mask; i++) {
    264 		pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1);
    265 		pid_table[i].pt_pgrp = 0;
    266 	}
    267 	/* slot 0 is just grabbed */
    268 	next_free_pt = 1;
    269 	/* Need to fix last entry. */
    270 	last_free_pt = pid_tbl_mask;
    271 	pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY);
    272 	/* point at which we grow table - to avoid reusing pids too often */
    273 	pid_alloc_lim = pid_tbl_mask - 1;
    274 #undef LINK_EMPTY
    275 
    276 	LIST_INIT(&alllwp);
    277 
    278 	uihashtbl =
    279 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
    280 
    281 	proc_specificdata_domain = specificdata_domain_create();
    282 	KASSERT(proc_specificdata_domain != NULL);
    283 }
    284 
    285 /*
    286  * Initialize process 0.
    287  */
    288 void
    289 proc0_init(void)
    290 {
    291 	struct proc *p;
    292 	struct pgrp *pg;
    293 	struct session *sess;
    294 	struct lwp *l;
    295 	u_int i;
    296 	rlim_t lim;
    297 
    298 	p = &proc0;
    299 	pg = &pgrp0;
    300 	sess = &session0;
    301 	l = &lwp0;
    302 
    303 	/* XXX p_smutex can be IPL_VM except for audio drivers */
    304 	mutex_init(&p->p_smutex, MUTEX_SPIN, IPL_SCHED);
    305 	mutex_init(&p->p_stmutex, MUTEX_SPIN, IPL_STATCLOCK);
    306 	mutex_init(&p->p_rasmutex, MUTEX_SPIN, IPL_NONE);
    307 	mutex_init(&p->p_mutex, MUTEX_DEFAULT, IPL_NONE);
    308 	cv_init(&p->p_refcv, "drainref");
    309 	cv_init(&p->p_waitcv, "wait");
    310 	cv_init(&p->p_lwpcv, "lwpwait");
    311 
    312 	LIST_INIT(&p->p_lwps);
    313 	LIST_INIT(&p->p_sigwaiters);
    314 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    315 
    316 	p->p_nlwps = 1;
    317 	p->p_nrlwps = 1;
    318 	p->p_nlwpid = l->l_lid;
    319 	p->p_refcnt = 1;
    320 
    321 	pid_table[0].pt_proc = p;
    322 	LIST_INSERT_HEAD(&allproc, p, p_list);
    323 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    324 
    325 	p->p_pgrp = pg;
    326 	pid_table[0].pt_pgrp = pg;
    327 	LIST_INIT(&pg->pg_members);
    328 	LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
    329 
    330 	pg->pg_session = sess;
    331 	sess->s_count = 1;
    332 	sess->s_sid = 0;
    333 	sess->s_leader = p;
    334 
    335 	/*
    336 	 * Set P_NOCLDWAIT so that kernel threads are reparented to
    337 	 * init(8) when they exit.  init(8) can easily wait them out
    338 	 * for us.
    339 	 */
    340 	p->p_flag = P_SYSTEM | P_NOCLDWAIT;
    341 	p->p_stat = SACTIVE;
    342 	p->p_nice = NZERO;
    343 	p->p_emul = &emul_netbsd;
    344 #ifdef __HAVE_SYSCALL_INTERN
    345 	(*p->p_emul->e_syscall_intern)(p);
    346 #endif
    347 	strncpy(p->p_comm, "swapper", MAXCOMLEN);
    348 
    349 	l->l_mutex = &sched_mutex;
    350 	l->l_flag = L_INMEM | L_SYSTEM;
    351 	l->l_stat = LSONPROC;
    352 	l->l_ts = &turnstile0;
    353 	l->l_syncobj = &sched_syncobj;
    354 	l->l_refcnt = 1;
    355 	l->l_cpu = curcpu();
    356 	l->l_priority = PRIBIO;
    357 	l->l_usrpri = PRIBIO;
    358 
    359 	callout_init(&l->l_tsleep_ch);
    360 	cv_init(&l->l_sigcv, "sigwait");
    361 
    362 	/* Create credentials. */
    363 	cred0 = kauth_cred_alloc();
    364 	p->p_cred = cred0;
    365 	kauth_cred_hold(cred0);
    366 	l->l_cred = cred0;
    367 
    368 	/* Create the CWD info. */
    369 	p->p_cwdi = &cwdi0;
    370 	cwdi0.cwdi_cmask = cmask;
    371 	cwdi0.cwdi_refcnt = 1;
    372 	simple_lock_init(&cwdi0.cwdi_slock);
    373 
    374 	/* Create the limits structures. */
    375 	p->p_limit = &limit0;
    376 	simple_lock_init(&limit0.p_slock);
    377 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
    378 		limit0.pl_rlimit[i].rlim_cur =
    379 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
    380 
    381 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
    382 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
    383 	    maxfiles < nofile ? maxfiles : nofile;
    384 
    385 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
    386 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
    387 	    maxproc < maxuprc ? maxproc : maxuprc;
    388 
    389 	lim = ptoa(uvmexp.free);
    390 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
    391 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
    392 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
    393 	limit0.pl_corename = defcorename;
    394 	limit0.p_refcnt = 1;
    395 
    396 	/* Configure virtual memory system, set vm rlimits. */
    397 	uvm_init_limits(p);
    398 
    399 	/* Initialize file descriptor table for proc0. */
    400 	p->p_fd = &filedesc0.fd_fd;
    401 	fdinit1(&filedesc0);
    402 
    403 	/*
    404 	 * Initialize proc0's vmspace, which uses the kernel pmap.
    405 	 * All kernel processes (which never have user space mappings)
    406 	 * share proc0's vmspace, and thus, the kernel pmap.
    407 	 */
    408 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    409 	    trunc_page(VM_MAX_ADDRESS));
    410 	p->p_vmspace = &vmspace0;
    411 
    412 	l->l_addr = proc0paddr;				/* XXX */
    413 
    414 	p->p_stats = &pstat0;
    415 
    416 	/* Initialize signal state for proc0. */
    417 	p->p_sigacts = &sigacts0;
    418 	mutex_init(&p->p_sigacts->sa_mutex, MUTEX_SPIN, IPL_NONE);
    419 	siginit(p);
    420 
    421 	proc_initspecific(p);
    422 	lwp_initspecific(l);
    423 }
    424 
    425 /*
    426  * Check that the specified process group is in the session of the
    427  * specified process.
    428  * Treats -ve ids as process ids.
    429  * Used to validate TIOCSPGRP requests.
    430  */
    431 int
    432 pgid_in_session(struct proc *p, pid_t pg_id)
    433 {
    434 	struct pgrp *pgrp;
    435 
    436 	if (pg_id < 0) {
    437 		struct proc *p1 = pfind(-pg_id);
    438 		if (p1 == NULL)
    439 			return EINVAL;
    440 		pgrp = p1->p_pgrp;
    441 	} else {
    442 		pgrp = pgfind(pg_id);
    443 		if (pgrp == NULL)
    444 			return EINVAL;
    445 	}
    446 	if (pgrp->pg_session != p->p_pgrp->pg_session)
    447 		return EPERM;
    448 	return 0;
    449 }
    450 
    451 /*
    452  * Is p an inferior of q?
    453  *
    454  * Call with the proclist_lock held.
    455  */
    456 int
    457 inferior(struct proc *p, struct proc *q)
    458 {
    459 
    460 	for (; p != q; p = p->p_pptr)
    461 		if (p->p_pid == 0)
    462 			return 0;
    463 	return 1;
    464 }
    465 
    466 /*
    467  * Locate a process by number
    468  */
    469 struct proc *
    470 p_find(pid_t pid, uint flags)
    471 {
    472 	struct proc *p;
    473 	char stat;
    474 
    475 	if (!(flags & PFIND_LOCKED))
    476 		rw_enter(&proclist_lock, RW_READER);
    477 
    478 	p = pid_table[pid & pid_tbl_mask].pt_proc;
    479 
    480 	/* Only allow live processes to be found by pid. */
    481 	/* XXXSMP p_stat */
    482 	if (P_VALID(p) && p->p_pid == pid && ((stat = p->p_stat) == SACTIVE ||
    483 	    stat == SSTOP || ((flags & PFIND_ZOMBIE) &&
    484 	    (stat == SZOMB || stat == SDEAD || stat == SDYING)))) {
    485 		if (flags & PFIND_UNLOCK_OK)
    486 			 rw_exit(&proclist_lock);
    487 		return p;
    488 	}
    489 	if (flags & PFIND_UNLOCK_FAIL)
    490 		 rw_exit(&proclist_lock);
    491 	return NULL;
    492 }
    493 
    494 
    495 /*
    496  * Locate a process group by number
    497  */
    498 struct pgrp *
    499 pg_find(pid_t pgid, uint flags)
    500 {
    501 	struct pgrp *pg;
    502 
    503 	if (!(flags & PFIND_LOCKED))
    504 		rw_enter(&proclist_lock, RW_READER);
    505 	pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    506 	/*
    507 	 * Can't look up a pgrp that only exists because the session
    508 	 * hasn't died yet (traditional)
    509 	 */
    510 	if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
    511 		if (flags & PFIND_UNLOCK_FAIL)
    512 			 rw_exit(&proclist_lock);
    513 		return NULL;
    514 	}
    515 
    516 	if (flags & PFIND_UNLOCK_OK)
    517 		rw_exit(&proclist_lock);
    518 	return pg;
    519 }
    520 
    521 static void
    522 expand_pid_table(void)
    523 {
    524 	uint pt_size = pid_tbl_mask + 1;
    525 	struct pid_table *n_pt, *new_pt;
    526 	struct proc *proc;
    527 	struct pgrp *pgrp;
    528 	int i;
    529 	pid_t pid;
    530 
    531 	new_pt = malloc(pt_size * 2 * sizeof *new_pt, M_PROC, M_WAITOK);
    532 
    533 	rw_enter(&proclist_lock, RW_WRITER);
    534 	if (pt_size != pid_tbl_mask + 1) {
    535 		/* Another process beat us to it... */
    536 		rw_exit(&proclist_lock);
    537 		FREE(new_pt, M_PROC);
    538 		return;
    539 	}
    540 
    541 	/*
    542 	 * Copy entries from old table into new one.
    543 	 * If 'pid' is 'odd' we need to place in the upper half,
    544 	 * even pid's to the lower half.
    545 	 * Free items stay in the low half so we don't have to
    546 	 * fixup the reference to them.
    547 	 * We stuff free items on the front of the freelist
    548 	 * because we can't write to unmodified entries.
    549 	 * Processing the table backwards maintains a semblance
    550 	 * of issueing pid numbers that increase with time.
    551 	 */
    552 	i = pt_size - 1;
    553 	n_pt = new_pt + i;
    554 	for (; ; i--, n_pt--) {
    555 		proc = pid_table[i].pt_proc;
    556 		pgrp = pid_table[i].pt_pgrp;
    557 		if (!P_VALID(proc)) {
    558 			/* Up 'use count' so that link is valid */
    559 			pid = (P_NEXT(proc) + pt_size) & ~pt_size;
    560 			proc = P_FREE(pid);
    561 			if (pgrp)
    562 				pid = pgrp->pg_id;
    563 		} else
    564 			pid = proc->p_pid;
    565 
    566 		/* Save entry in appropriate half of table */
    567 		n_pt[pid & pt_size].pt_proc = proc;
    568 		n_pt[pid & pt_size].pt_pgrp = pgrp;
    569 
    570 		/* Put other piece on start of free list */
    571 		pid = (pid ^ pt_size) & ~pid_tbl_mask;
    572 		n_pt[pid & pt_size].pt_proc =
    573 				    P_FREE((pid & ~pt_size) | next_free_pt);
    574 		n_pt[pid & pt_size].pt_pgrp = 0;
    575 		next_free_pt = i | (pid & pt_size);
    576 		if (i == 0)
    577 			break;
    578 	}
    579 
    580 	/* Switch tables */
    581 	mutex_enter(&proclist_mutex);
    582 	n_pt = pid_table;
    583 	pid_table = new_pt;
    584 	mutex_exit(&proclist_mutex);
    585 	pid_tbl_mask = pt_size * 2 - 1;
    586 
    587 	/*
    588 	 * pid_max starts as PID_MAX (= 30000), once we have 16384
    589 	 * allocated pids we need it to be larger!
    590 	 */
    591 	if (pid_tbl_mask > PID_MAX) {
    592 		pid_max = pid_tbl_mask * 2 + 1;
    593 		pid_alloc_lim |= pid_alloc_lim << 1;
    594 	} else
    595 		pid_alloc_lim <<= 1;	/* doubles number of free slots... */
    596 
    597 	rw_exit(&proclist_lock);
    598 	FREE(n_pt, M_PROC);
    599 }
    600 
    601 struct proc *
    602 proc_alloc(void)
    603 {
    604 	struct proc *p;
    605 	int nxt;
    606 	pid_t pid;
    607 	struct pid_table *pt;
    608 
    609 	p = pool_get(&proc_pool, PR_WAITOK);
    610 	p->p_stat = SIDL;			/* protect against others */
    611 
    612 	proc_initspecific(p);
    613 	/* allocate next free pid */
    614 
    615 	for (;;expand_pid_table()) {
    616 		if (__predict_false(pid_alloc_cnt >= pid_alloc_lim))
    617 			/* ensure pids cycle through 2000+ values */
    618 			continue;
    619 		rw_enter(&proclist_lock, RW_WRITER);
    620 		pt = &pid_table[next_free_pt];
    621 #ifdef DIAGNOSTIC
    622 		if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp))
    623 			panic("proc_alloc: slot busy");
    624 #endif
    625 		nxt = P_NEXT(pt->pt_proc);
    626 		if (nxt & pid_tbl_mask)
    627 			break;
    628 		/* Table full - expand (NB last entry not used....) */
    629 		rw_exit(&proclist_lock);
    630 	}
    631 
    632 	/* pid is 'saved use count' + 'size' + entry */
    633 	pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
    634 	if ((uint)pid > (uint)pid_max)
    635 		pid &= pid_tbl_mask;
    636 	p->p_pid = pid;
    637 	next_free_pt = nxt & pid_tbl_mask;
    638 
    639 	/* Grab table slot */
    640 	mutex_enter(&proclist_mutex);
    641 	pt->pt_proc = p;
    642 	mutex_exit(&proclist_mutex);
    643 	pid_alloc_cnt++;
    644 
    645 	rw_exit(&proclist_lock);
    646 
    647 	return p;
    648 }
    649 
    650 /*
    651  * Free last resources of a process - called from proc_free (in kern_exit.c)
    652  *
    653  * Called with the proclist_lock write held, and releases upon exit.
    654  */
    655 void
    656 proc_free_mem(struct proc *p)
    657 {
    658 	pid_t pid = p->p_pid;
    659 	struct pid_table *pt;
    660 
    661 	LOCK_ASSERT(rw_write_held(&proclist_lock));
    662 
    663 	pt = &pid_table[pid & pid_tbl_mask];
    664 #ifdef DIAGNOSTIC
    665 	if (__predict_false(pt->pt_proc != p))
    666 		panic("proc_free: pid_table mismatch, pid %x, proc %p",
    667 			pid, p);
    668 #endif
    669 	mutex_enter(&proclist_mutex);
    670 	/* save pid use count in slot */
    671 	pt->pt_proc = P_FREE(pid & ~pid_tbl_mask);
    672 
    673 	if (pt->pt_pgrp == NULL) {
    674 		/* link last freed entry onto ours */
    675 		pid &= pid_tbl_mask;
    676 		pt = &pid_table[last_free_pt];
    677 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid);
    678 		last_free_pt = pid;
    679 		pid_alloc_cnt--;
    680 	}
    681 	mutex_exit(&proclist_mutex);
    682 
    683 	nprocs--;
    684 	rw_exit(&proclist_lock);
    685 
    686 	pool_put(&proc_pool, p);
    687 }
    688 
    689 /*
    690  * Move p to a new or existing process group (and session)
    691  *
    692  * If we are creating a new pgrp, the pgid should equal
    693  * the calling process' pid.
    694  * If is only valid to enter a process group that is in the session
    695  * of the process.
    696  * Also mksess should only be set if we are creating a process group
    697  *
    698  * Only called from sys_setsid, sys_setpgid/sys_setpgrp and the
    699  * SYSV setpgrp support for hpux.
    700  */
    701 int
    702 enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, int mksess)
    703 {
    704 	struct pgrp *new_pgrp, *pgrp;
    705 	struct session *sess;
    706 	struct proc *p;
    707 	int rval;
    708 	pid_t pg_id = NO_PGID;
    709 
    710 	/* Allocate data areas we might need before doing any validity checks */
    711 	rw_enter(&proclist_lock, RW_READER);		/* Because pid_table might change */
    712 	if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
    713 		rw_exit(&proclist_lock);
    714 		new_pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    715 	} else {
    716 		rw_exit(&proclist_lock);
    717 		new_pgrp = NULL;
    718 	}
    719 	if (mksess)
    720 		sess = pool_get(&session_pool, PR_WAITOK);
    721 	else
    722 		sess = NULL;
    723 
    724 	rw_enter(&proclist_lock, RW_WRITER);
    725 	rval = EPERM;	/* most common error (to save typing) */
    726 
    727 	/* Check pgrp exists or can be created */
    728 	pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    729 	if (pgrp != NULL && pgrp->pg_id != pgid)
    730 		goto done;
    731 
    732 	/* Can only set another process under restricted circumstances. */
    733 	if (pid != curp->p_pid) {
    734 		/* must exist and be one of our children... */
    735 		if ((p = p_find(pid, PFIND_LOCKED)) == NULL ||
    736 		    !inferior(p, curp)) {
    737 			rval = ESRCH;
    738 			goto done;
    739 		}
    740 		/* ... in the same session... */
    741 		if (sess != NULL || p->p_session != curp->p_session)
    742 			goto done;
    743 		/* ... existing pgid must be in same session ... */
    744 		if (pgrp != NULL && pgrp->pg_session != p->p_session)
    745 			goto done;
    746 		/* ... and not done an exec. */
    747 		if (p->p_flag & P_EXEC) {
    748 			rval = EACCES;
    749 			goto done;
    750 		}
    751 	} else {
    752 		/* ... setsid() cannot re-enter a pgrp */
    753 		if (mksess && (curp->p_pgid == curp->p_pid ||
    754 		    pg_find(curp->p_pid, PFIND_LOCKED)))
    755 			goto done;
    756 		p = curp;
    757 	}
    758 
    759 	/* Changing the process group/session of a session
    760 	   leader is definitely off limits. */
    761 	if (SESS_LEADER(p)) {
    762 		if (sess == NULL && p->p_pgrp == pgrp)
    763 			/* unless it's a definite noop */
    764 			rval = 0;
    765 		goto done;
    766 	}
    767 
    768 	/* Can only create a process group with id of process */
    769 	if (pgrp == NULL && pgid != pid)
    770 		goto done;
    771 
    772 	/* Can only create a session if creating pgrp */
    773 	if (sess != NULL && pgrp != NULL)
    774 		goto done;
    775 
    776 	/* Check we allocated memory for a pgrp... */
    777 	if (pgrp == NULL && new_pgrp == NULL)
    778 		goto done;
    779 
    780 	/* Don't attach to 'zombie' pgrp */
    781 	if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
    782 		goto done;
    783 
    784 	/* Expect to succeed now */
    785 	rval = 0;
    786 
    787 	if (pgrp == p->p_pgrp)
    788 		/* nothing to do */
    789 		goto done;
    790 
    791 	/* Ok all setup, link up required structures */
    792 
    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_lflag &= ~PL_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 		mutex_enter(&proclist_mutex);
    822 		pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
    823 		pgrp->pg_jobc = 0;
    824 	} else
    825 		mutex_enter(&proclist_mutex);
    826 
    827 #ifdef notyet
    828 	/*
    829 	 * If there's a controlling terminal for the current session, we
    830 	 * have to interlock with it.  See ttread().
    831 	 */
    832 	if (p->p_session->s_ttyvp != NULL) {
    833 		tp = p->p_session->s_ttyp;
    834 		mutex_enter(&tp->t_mutex);
    835 	} else
    836 		tp = NULL;
    837 #endif
    838 
    839 	/*
    840 	 * Adjust eligibility of affected pgrps to participate in job control.
    841 	 * Increment eligibility counts before decrementing, otherwise we
    842 	 * could reach 0 spuriously during the first call.
    843 	 */
    844 	fixjobc(p, pgrp, 1);
    845 	fixjobc(p, p->p_pgrp, 0);
    846 
    847 	/* Move process to requested group. */
    848 	LIST_REMOVE(p, p_pglist);
    849 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
    850 		/* defer delete until we've dumped the lock */
    851 		pg_id = p->p_pgrp->pg_id;
    852 	p->p_pgrp = pgrp;
    853 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    854 	mutex_exit(&proclist_mutex);
    855 
    856 #ifdef notyet
    857 	/* Done with the swap; we can release the tty mutex. */
    858 	if (tp != NULL)
    859 		mutex_exit(&tp->t_mutex);
    860 #endif
    861 
    862     done:
    863 	if (pg_id != NO_PGID)
    864 		pg_delete(pg_id);
    865 	rw_exit(&proclist_lock);
    866 	if (sess != NULL)
    867 		pool_put(&session_pool, sess);
    868 	if (new_pgrp != NULL)
    869 		pool_put(&pgrp_pool, new_pgrp);
    870 #ifdef DEBUG_PGRP
    871 	if (__predict_false(rval))
    872 		printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
    873 			pid, pgid, mksess, curp->p_pid, rval);
    874 #endif
    875 	return rval;
    876 }
    877 
    878 /*
    879  * Remove a process from its process group.  Must be called with the
    880  * proclist_lock write held.
    881  */
    882 void
    883 leavepgrp(struct proc *p)
    884 {
    885 	struct pgrp *pgrp;
    886 
    887 	LOCK_ASSERT(rw_write_held(&proclist_lock));
    888 
    889 	/*
    890 	 * If there's a controlling terminal for the session, we have to
    891 	 * interlock with it.  See ttread().
    892 	 */
    893 	mutex_enter(&proclist_mutex);
    894 #ifdef notyet
    895 	if (p_>p_session->s_ttyvp != NULL) {
    896 		tp = p->p_session->s_ttyp;
    897 		mutex_enter(&tp->t_mutex);
    898 	} else
    899 		tp = NULL;
    900 #endif
    901 
    902 	pgrp = p->p_pgrp;
    903 	LIST_REMOVE(p, p_pglist);
    904 	p->p_pgrp = NULL;
    905 
    906 #ifdef notyet
    907 	if (tp != NULL)
    908 		mutex_exit(&tp->t_mutex);
    909 #endif
    910 	mutex_exit(&proclist_mutex);
    911 
    912 	if (LIST_EMPTY(&pgrp->pg_members))
    913 		pg_delete(pgrp->pg_id);
    914 }
    915 
    916 /*
    917  * Free a process group.  Must be called with the proclist_lock write held.
    918  */
    919 static void
    920 pg_free(pid_t pg_id)
    921 {
    922 	struct pgrp *pgrp;
    923 	struct pid_table *pt;
    924 
    925 	LOCK_ASSERT(rw_write_held(&proclist_lock));
    926 
    927 	pt = &pid_table[pg_id & pid_tbl_mask];
    928 	pgrp = pt->pt_pgrp;
    929 #ifdef DIAGNOSTIC
    930 	if (__predict_false(!pgrp || pgrp->pg_id != pg_id
    931 	    || !LIST_EMPTY(&pgrp->pg_members)))
    932 		panic("pg_free: process group absent or has members");
    933 #endif
    934 	pt->pt_pgrp = 0;
    935 
    936 	if (!P_VALID(pt->pt_proc)) {
    937 		/* orphaned pgrp, put slot onto free list */
    938 #ifdef DIAGNOSTIC
    939 		if (__predict_false(P_NEXT(pt->pt_proc) & pid_tbl_mask))
    940 			panic("pg_free: process slot on free list");
    941 #endif
    942 		mutex_enter(&proclist_mutex);
    943 		pg_id &= pid_tbl_mask;
    944 		pt = &pid_table[last_free_pt];
    945 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
    946 		mutex_exit(&proclist_mutex);
    947 		last_free_pt = pg_id;
    948 		pid_alloc_cnt--;
    949 	}
    950 	pool_put(&pgrp_pool, pgrp);
    951 }
    952 
    953 /*
    954  * Delete a process group.  Must be called with the proclist_lock write
    955  * held.
    956  */
    957 static void
    958 pg_delete(pid_t pg_id)
    959 {
    960 	struct pgrp *pgrp;
    961 	struct tty *ttyp;
    962 	struct session *ss;
    963 	int is_pgrp_leader;
    964 
    965 	LOCK_ASSERT(rw_write_held(&proclist_lock));
    966 
    967 	pgrp = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
    968 	if (pgrp == NULL || pgrp->pg_id != pg_id ||
    969 	    !LIST_EMPTY(&pgrp->pg_members))
    970 		return;
    971 
    972 	ss = pgrp->pg_session;
    973 
    974 	/* Remove reference (if any) from tty to this process group */
    975 	ttyp = ss->s_ttyp;
    976 	if (ttyp != NULL && ttyp->t_pgrp == pgrp) {
    977 		ttyp->t_pgrp = NULL;
    978 #ifdef DIAGNOSTIC
    979 		if (ttyp->t_session != ss)
    980 			panic("pg_delete: wrong session on terminal");
    981 #endif
    982 	}
    983 
    984 	/*
    985 	 * The leading process group in a session is freed
    986 	 * by sessdelete() if last reference.
    987 	 */
    988 	is_pgrp_leader = (ss->s_sid == pgrp->pg_id);
    989 	SESSRELE(ss);
    990 
    991 	if (is_pgrp_leader)
    992 		return;
    993 
    994 	pg_free(pg_id);
    995 }
    996 
    997 /*
    998  * Delete session - called from SESSRELE when s_count becomes zero.
    999  * Must be called with the proclist_lock write held.
   1000  */
   1001 void
   1002 sessdelete(struct session *ss)
   1003 {
   1004 
   1005 	LOCK_ASSERT(rw_write_held(&proclist_lock));
   1006 
   1007 	/*
   1008 	 * We keep the pgrp with the same id as the session in
   1009 	 * order to stop a process being given the same pid.
   1010 	 * Since the pgrp holds a reference to the session, it
   1011 	 * must be a 'zombie' pgrp by now.
   1012 	 */
   1013 	pg_free(ss->s_sid);
   1014 	pool_put(&session_pool, ss);
   1015 }
   1016 
   1017 /*
   1018  * Adjust pgrp jobc counters when specified process changes process group.
   1019  * We count the number of processes in each process group that "qualify"
   1020  * the group for terminal job control (those with a parent in a different
   1021  * process group of the same session).  If that count reaches zero, the
   1022  * process group becomes orphaned.  Check both the specified process'
   1023  * process group and that of its children.
   1024  * entering == 0 => p is leaving specified group.
   1025  * entering == 1 => p is entering specified group.
   1026  *
   1027  * Call with proclist_lock write held.
   1028  */
   1029 void
   1030 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
   1031 {
   1032 	struct pgrp *hispgrp;
   1033 	struct session *mysession = pgrp->pg_session;
   1034 	struct proc *child;
   1035 
   1036 	LOCK_ASSERT(rw_write_held(&proclist_lock));
   1037 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
   1038 
   1039 	/*
   1040 	 * Check p's parent to see whether p qualifies its own process
   1041 	 * group; if so, adjust count for p's process group.
   1042 	 */
   1043 	hispgrp = p->p_pptr->p_pgrp;
   1044 	if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
   1045 		if (entering) {
   1046 			mutex_enter(&p->p_smutex);
   1047 			p->p_sflag &= ~PS_ORPHANPG;
   1048 			mutex_exit(&p->p_smutex);
   1049 			pgrp->pg_jobc++;
   1050 		} else if (--pgrp->pg_jobc == 0)
   1051 			orphanpg(pgrp);
   1052 	}
   1053 
   1054 	/*
   1055 	 * Check this process' children to see whether they qualify
   1056 	 * their process groups; if so, adjust counts for children's
   1057 	 * process groups.
   1058 	 */
   1059 	LIST_FOREACH(child, &p->p_children, p_sibling) {
   1060 		hispgrp = child->p_pgrp;
   1061 		if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
   1062 		    !P_ZOMBIE(child)) {
   1063 			if (entering) {
   1064 				mutex_enter(&child->p_smutex);
   1065 				child->p_sflag &= ~PS_ORPHANPG;
   1066 				mutex_exit(&child->p_smutex);
   1067 				hispgrp->pg_jobc++;
   1068 			} else if (--hispgrp->pg_jobc == 0)
   1069 				orphanpg(hispgrp);
   1070 		}
   1071 	}
   1072 }
   1073 
   1074 /*
   1075  * A process group has become orphaned;
   1076  * if there are any stopped processes in the group,
   1077  * hang-up all process in that group.
   1078  *
   1079  * Call with proclist_lock write held.
   1080  */
   1081 static void
   1082 orphanpg(struct pgrp *pg)
   1083 {
   1084 	struct proc *p;
   1085 	int doit;
   1086 
   1087 	LOCK_ASSERT(rw_write_held(&proclist_lock));
   1088 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
   1089 
   1090 	doit = 0;
   1091 
   1092 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1093 		mutex_enter(&p->p_smutex);
   1094 		if (p->p_stat == SSTOP) {
   1095 			doit = 1;
   1096 			p->p_sflag |= PS_ORPHANPG;
   1097 		}
   1098 		mutex_exit(&p->p_smutex);
   1099 	}
   1100 
   1101 	if (doit) {
   1102 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1103 			psignal(p, SIGHUP);
   1104 			psignal(p, SIGCONT);
   1105 		}
   1106 	}
   1107 }
   1108 
   1109 #ifdef DDB
   1110 #include <ddb/db_output.h>
   1111 void pidtbl_dump(void);
   1112 void
   1113 pidtbl_dump(void)
   1114 {
   1115 	struct pid_table *pt;
   1116 	struct proc *p;
   1117 	struct pgrp *pgrp;
   1118 	int id;
   1119 
   1120 	db_printf("pid table %p size %x, next %x, last %x\n",
   1121 		pid_table, pid_tbl_mask+1,
   1122 		next_free_pt, last_free_pt);
   1123 	for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
   1124 		p = pt->pt_proc;
   1125 		if (!P_VALID(p) && !pt->pt_pgrp)
   1126 			continue;
   1127 		db_printf("  id %x: ", id);
   1128 		if (P_VALID(p))
   1129 			db_printf("proc %p id %d (0x%x) %s\n",
   1130 				p, p->p_pid, p->p_pid, p->p_comm);
   1131 		else
   1132 			db_printf("next %x use %x\n",
   1133 				P_NEXT(p) & pid_tbl_mask,
   1134 				P_NEXT(p) & ~pid_tbl_mask);
   1135 		if ((pgrp = pt->pt_pgrp)) {
   1136 			db_printf("\tsession %p, sid %d, count %d, login %s\n",
   1137 			    pgrp->pg_session, pgrp->pg_session->s_sid,
   1138 			    pgrp->pg_session->s_count,
   1139 			    pgrp->pg_session->s_login);
   1140 			db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
   1141 			    pgrp, pgrp->pg_id, pgrp->pg_jobc,
   1142 			    pgrp->pg_members.lh_first);
   1143 			for (p = pgrp->pg_members.lh_first; p != 0;
   1144 			    p = p->p_pglist.le_next) {
   1145 				db_printf("\t\tpid %d addr %p pgrp %p %s\n",
   1146 				    p->p_pid, p, p->p_pgrp, p->p_comm);
   1147 			}
   1148 		}
   1149 	}
   1150 }
   1151 #endif /* DDB */
   1152 
   1153 #ifdef KSTACK_CHECK_MAGIC
   1154 #include <sys/user.h>
   1155 
   1156 #define	KSTACK_MAGIC	0xdeadbeaf
   1157 
   1158 /* XXX should be per process basis? */
   1159 int kstackleftmin = KSTACK_SIZE;
   1160 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
   1161 					  less than this */
   1162 
   1163 void
   1164 kstack_setup_magic(const struct lwp *l)
   1165 {
   1166 	uint32_t *ip;
   1167 	uint32_t const *end;
   1168 
   1169 	KASSERT(l != NULL);
   1170 	KASSERT(l != &lwp0);
   1171 
   1172 	/*
   1173 	 * fill all the stack with magic number
   1174 	 * so that later modification on it can be detected.
   1175 	 */
   1176 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1177 	end = (uint32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1178 	for (; ip < end; ip++) {
   1179 		*ip = KSTACK_MAGIC;
   1180 	}
   1181 }
   1182 
   1183 void
   1184 kstack_check_magic(const struct lwp *l)
   1185 {
   1186 	uint32_t const *ip, *end;
   1187 	int stackleft;
   1188 
   1189 	KASSERT(l != NULL);
   1190 
   1191 	/* don't check proc0 */ /*XXX*/
   1192 	if (l == &lwp0)
   1193 		return;
   1194 
   1195 #ifdef __MACHINE_STACK_GROWS_UP
   1196 	/* stack grows upwards (eg. hppa) */
   1197 	ip = (uint32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1198 	end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1199 	for (ip--; ip >= end; ip--)
   1200 		if (*ip != KSTACK_MAGIC)
   1201 			break;
   1202 
   1203 	stackleft = (caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (caddr_t)ip;
   1204 #else /* __MACHINE_STACK_GROWS_UP */
   1205 	/* stack grows downwards (eg. i386) */
   1206 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1207 	end = (uint32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1208 	for (; ip < end; ip++)
   1209 		if (*ip != KSTACK_MAGIC)
   1210 			break;
   1211 
   1212 	stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l);
   1213 #endif /* __MACHINE_STACK_GROWS_UP */
   1214 
   1215 	if (kstackleftmin > stackleft) {
   1216 		kstackleftmin = stackleft;
   1217 		if (stackleft < kstackleftthres)
   1218 			printf("warning: kernel stack left %d bytes"
   1219 			    "(pid %u:lid %u)\n", stackleft,
   1220 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1221 	}
   1222 
   1223 	if (stackleft <= 0) {
   1224 		panic("magic on the top of kernel stack changed for "
   1225 		    "pid %u, lid %u: maybe kernel stack overflow",
   1226 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1227 	}
   1228 }
   1229 #endif /* KSTACK_CHECK_MAGIC */
   1230 
   1231 /*
   1232  * XXXSMP this is bust, it grabs a read lock and then messes about
   1233  * with allproc.
   1234  */
   1235 int
   1236 proclist_foreach_call(struct proclist *list,
   1237     int (*callback)(struct proc *, void *arg), void *arg)
   1238 {
   1239 	struct proc marker;
   1240 	struct proc *p;
   1241 	struct lwp * const l = curlwp;
   1242 	int ret = 0;
   1243 
   1244 	marker.p_flag = P_MARKER;
   1245 	PHOLD(l);
   1246 	rw_enter(&proclist_lock, RW_READER);
   1247 	for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
   1248 		if (p->p_flag & P_MARKER) {
   1249 			p = LIST_NEXT(p, p_list);
   1250 			continue;
   1251 		}
   1252 		LIST_INSERT_AFTER(p, &marker, p_list);
   1253 		ret = (*callback)(p, arg);
   1254 		KASSERT(rw_read_held(&proclist_lock));
   1255 		p = LIST_NEXT(&marker, p_list);
   1256 		LIST_REMOVE(&marker, p_list);
   1257 	}
   1258 	rw_exit(&proclist_lock);
   1259 	PRELE(l);
   1260 
   1261 	return ret;
   1262 }
   1263 
   1264 int
   1265 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
   1266 {
   1267 
   1268 	/* XXXCDC: how should locking work here? */
   1269 
   1270 	/* curproc exception is for coredump. */
   1271 
   1272 	if ((p != curproc && (p->p_sflag & PS_WEXIT) != 0) ||
   1273 	    (p->p_vmspace->vm_refcnt < 1)) { /* XXX */
   1274 		return EFAULT;
   1275 	}
   1276 
   1277 	uvmspace_addref(p->p_vmspace);
   1278 	*vm = p->p_vmspace;
   1279 
   1280 	return 0;
   1281 }
   1282 
   1283 /*
   1284  * Acquire a write lock on the process credential.
   1285  */
   1286 void
   1287 proc_crmod_enter(void)
   1288 {
   1289 	struct lwp *l = curlwp;
   1290 	struct proc *p = l->l_proc;
   1291 	struct plimit *lim;
   1292 	kauth_cred_t oc;
   1293 	char *cn;
   1294 
   1295 	mutex_enter(&p->p_mutex);
   1296 
   1297 	/* Ensure the LWP cached credentials are up to date. */
   1298 	if ((oc = l->l_cred) != p->p_cred) {
   1299 		kauth_cred_hold(p->p_cred);
   1300 		l->l_cred = p->p_cred;
   1301 		kauth_cred_free(oc);
   1302 	}
   1303 
   1304 	/* Reset what needs to be reset in plimit. */
   1305 	lim = p->p_limit;
   1306 	if (lim->pl_corename != defcorename) {
   1307 		if (lim->p_refcnt > 1 &&
   1308 		    (lim->p_lflags & PL_SHAREMOD) == 0) {
   1309 			p->p_limit = limcopy(p);
   1310 			limfree(lim);
   1311 			lim = p->p_limit;
   1312 		}
   1313 		simple_lock(&lim->p_slock);
   1314 		cn = lim->pl_corename;
   1315 		lim->pl_corename = defcorename;
   1316 		simple_unlock(&lim->p_slock);
   1317 		if (cn != defcorename)
   1318 			free(cn, M_TEMP);
   1319 	}
   1320 }
   1321 
   1322 /*
   1323  * Set in a new process credential, and drop the write lock.  The credential
   1324  * must have a reference already.  Optionally, free a no-longer required
   1325  * credential.  The scheduler also needs to inspect p_cred, so we also
   1326  * briefly acquire the sched state mutex.
   1327  */
   1328 void
   1329 proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, boolean_t sugid)
   1330 {
   1331 	struct lwp *l = curlwp;
   1332 	struct proc *p = l->l_proc;
   1333 	kauth_cred_t oc;
   1334 
   1335 	/* Is there a new credential to set in? */
   1336 	if (scred != NULL) {
   1337 		mutex_enter(&p->p_smutex);
   1338 		p->p_cred = scred;
   1339 		mutex_exit(&p->p_smutex);
   1340 
   1341 		/* Ensure the LWP cached credentials are up to date. */
   1342 		if ((oc = l->l_cred) != scred) {
   1343 			kauth_cred_hold(scred);
   1344 			l->l_cred = scred;
   1345 		}
   1346 	} else
   1347 		oc = NULL;	/* XXXgcc */
   1348 
   1349 	if (sugid) {
   1350 		/*
   1351 		 * Mark process as having changed credentials, stops
   1352 		 * tracing etc.
   1353 		 */
   1354 		p->p_flag |= P_SUGID;
   1355 	}
   1356 
   1357 	mutex_exit(&p->p_mutex);
   1358 
   1359 	/* If there is a credential to be released, free it now. */
   1360 	if (fcred != NULL) {
   1361 		KASSERT(scred != NULL);
   1362 		kauth_cred_free(fcred);
   1363 		if (oc != scred)
   1364 			kauth_cred_free(oc);
   1365 	}
   1366 }
   1367 
   1368 /*
   1369  * Acquire a reference on a process, to prevent it from exiting or execing.
   1370  */
   1371 int
   1372 proc_addref(struct proc *p)
   1373 {
   1374 
   1375 	LOCK_ASSERT(mutex_owned(&p->p_mutex));
   1376 
   1377 	if (p->p_refcnt <= 0)
   1378 		return EAGAIN;
   1379 	p->p_refcnt++;
   1380 
   1381 	return 0;
   1382 }
   1383 
   1384 /*
   1385  * Release a reference on a process.
   1386  */
   1387 void
   1388 proc_delref(struct proc *p)
   1389 {
   1390 
   1391 	LOCK_ASSERT(mutex_owned(&p->p_mutex));
   1392 
   1393 	if (p->p_refcnt < 0) {
   1394 		if (++p->p_refcnt == 0)
   1395 			cv_broadcast(&p->p_refcv);
   1396 	} else {
   1397 		p->p_refcnt--;
   1398 		KASSERT(p->p_refcnt != 0);
   1399 	}
   1400 }
   1401 
   1402 /*
   1403  * Wait for all references on the process to drain, and prevent new
   1404  * references from being acquired.
   1405  */
   1406 void
   1407 proc_drainrefs(struct proc *p)
   1408 {
   1409 
   1410 	LOCK_ASSERT(mutex_owned(&p->p_mutex));
   1411 	KASSERT(p->p_refcnt > 0);
   1412 
   1413 	/*
   1414 	 * The process itself holds the last reference.  Once it's released,
   1415 	 * no new references will be granted.  If we have already locked out
   1416 	 * new references (refcnt <= 0), potentially due to a failed exec,
   1417 	 * there is nothing more to do.
   1418 	 */
   1419 	p->p_refcnt = 1 - p->p_refcnt;
   1420 	while (p->p_refcnt != 0)
   1421 		cv_wait(&p->p_refcv, &p->p_mutex);
   1422 }
   1423 
   1424 /*
   1425  * proc_specific_key_create --
   1426  *	Create a key for subsystem proc-specific data.
   1427  */
   1428 int
   1429 proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1430 {
   1431 
   1432 	return (specificdata_key_create(proc_specificdata_domain, keyp, dtor));
   1433 }
   1434 
   1435 /*
   1436  * proc_specific_key_delete --
   1437  *	Delete a key for subsystem proc-specific data.
   1438  */
   1439 void
   1440 proc_specific_key_delete(specificdata_key_t key)
   1441 {
   1442 
   1443 	specificdata_key_delete(proc_specificdata_domain, key);
   1444 }
   1445 
   1446 /*
   1447  * proc_initspecific --
   1448  *	Initialize a proc's specificdata container.
   1449  */
   1450 void
   1451 proc_initspecific(struct proc *p)
   1452 {
   1453 	int error;
   1454 
   1455 	error = specificdata_init(proc_specificdata_domain, &p->p_specdataref);
   1456 	KASSERT(error == 0);
   1457 }
   1458 
   1459 /*
   1460  * proc_finispecific --
   1461  *	Finalize a proc's specificdata container.
   1462  */
   1463 void
   1464 proc_finispecific(struct proc *p)
   1465 {
   1466 
   1467 	specificdata_fini(proc_specificdata_domain, &p->p_specdataref);
   1468 }
   1469 
   1470 /*
   1471  * proc_getspecific --
   1472  *	Return proc-specific data corresponding to the specified key.
   1473  */
   1474 void *
   1475 proc_getspecific(struct proc *p, specificdata_key_t key)
   1476 {
   1477 
   1478 	return (specificdata_getspecific(proc_specificdata_domain,
   1479 					 &p->p_specdataref, key));
   1480 }
   1481 
   1482 /*
   1483  * proc_setspecific --
   1484  *	Set proc-specific data corresponding to the specified key.
   1485  */
   1486 void
   1487 proc_setspecific(struct proc *p, specificdata_key_t key, void *data)
   1488 {
   1489 
   1490 	specificdata_setspecific(proc_specificdata_domain,
   1491 				 &p->p_specdataref, key, data);
   1492 }
   1493