xref: /openbsd-src/sys/kern/kern_fork.c (revision 79a514fcbea783e80e5f8763d0f8d6ad9a05557f)
1 /*	$OpenBSD: kern_fork.c,v 1.195 2017/02/12 04:55:08 guenther Exp $	*/
2 /*	$NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)kern_fork.c	8.6 (Berkeley) 4/8/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/filedesc.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/exec.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/vnode.h>
51 #include <sys/vmmeter.h>
52 #include <sys/file.h>
53 #include <sys/acct.h>
54 #include <sys/ktrace.h>
55 #include <sys/sched.h>
56 #include <sys/sysctl.h>
57 #include <sys/pool.h>
58 #include <sys/mman.h>
59 #include <sys/ptrace.h>
60 #include <sys/atomic.h>
61 #include <sys/pledge.h>
62 #include <sys/unistd.h>
63 
64 #include <sys/syscallargs.h>
65 
66 #include <uvm/uvm.h>
67 
68 #ifdef __HAVE_MD_TCB
69 # include <machine/tcb.h>
70 #endif
71 
72 int	nprocesses = 1;		/* process 0 */
73 int	nthreads = 1;		/* proc 0 */
74 int	randompid;		/* when set to 1, pid's go random */
75 struct	forkstat forkstat;
76 
77 void fork_return(void *);
78 pid_t alloctid(void);
79 pid_t allocpid(void);
80 int ispidtaken(pid_t);
81 
82 struct proc *thread_new(struct proc *_parent, vaddr_t _uaddr);
83 struct process *process_new(struct proc *, struct process *, int);
84 int fork_check_maxthread(uid_t _uid);
85 
86 void
87 fork_return(void *arg)
88 {
89 	struct proc *p = (struct proc *)arg;
90 
91 	if (p->p_p->ps_flags & PS_TRACED)
92 		psignal(p, SIGTRAP);
93 
94 	child_return(p);
95 }
96 
97 int
98 sys_fork(struct proc *p, void *v, register_t *retval)
99 {
100 	int flags;
101 
102 	flags = FORK_FORK;
103 	if (p->p_p->ps_ptmask & PTRACE_FORK)
104 		flags |= FORK_PTRACE;
105 	return fork1(p, flags, fork_return, NULL, retval, NULL);
106 }
107 
108 int
109 sys_vfork(struct proc *p, void *v, register_t *retval)
110 {
111 	return fork1(p, FORK_VFORK|FORK_PPWAIT, child_return, NULL,
112 	    retval, NULL);
113 }
114 
115 int
116 sys___tfork(struct proc *p, void *v, register_t *retval)
117 {
118 	struct sys___tfork_args /* {
119 		syscallarg(const struct __tfork) *param;
120 		syscallarg(size_t) psize;
121 	} */ *uap = v;
122 	size_t psize = SCARG(uap, psize);
123 	struct __tfork param = { 0 };
124 	int error;
125 
126 	if (psize == 0 || psize > sizeof(param))
127 		return EINVAL;
128 	if ((error = copyin(SCARG(uap, param), &param, psize)))
129 		return error;
130 #ifdef KTRACE
131 	if (KTRPOINT(p, KTR_STRUCT))
132 		ktrstruct(p, "tfork", &param, sizeof(param));
133 #endif
134 
135 	return thread_fork(p, param.tf_stack, param.tf_tcb, param.tf_tid,
136 	    retval);
137 }
138 
139 /*
140  * Allocate and initialize a thread (proc) structure, given the parent thread.
141  */
142 struct proc *
143 thread_new(struct proc *parent, vaddr_t uaddr)
144 {
145 	struct proc *p;
146 
147 	p = pool_get(&proc_pool, PR_WAITOK);
148 	p->p_stat = SIDL;			/* protect against others */
149 	p->p_flag = 0;
150 
151 	/*
152 	 * Make a proc table entry for the new process.
153 	 * Start by zeroing the section of proc that is zero-initialized,
154 	 * then copy the section that is copied directly from the parent.
155 	 */
156 	memset(&p->p_startzero, 0,
157 	    (caddr_t)&p->p_endzero - (caddr_t)&p->p_startzero);
158 	memcpy(&p->p_startcopy, &parent->p_startcopy,
159 	    (caddr_t)&p->p_endcopy - (caddr_t)&p->p_startcopy);
160 	crhold(p->p_ucred);
161 	p->p_addr = (struct user *)uaddr;
162 
163 	/*
164 	 * Initialize the timeouts.
165 	 */
166 	timeout_set(&p->p_sleep_to, endtsleep, p);
167 
168 	/*
169 	 * set priority of child to be that of parent
170 	 * XXX should move p_estcpu into the region of struct proc which gets
171 	 * copied.
172 	 */
173 	scheduler_fork_hook(parent, p);
174 
175 	return p;
176 }
177 
178 /*
179  * Initialize common bits of a process structure, given the initial thread.
180  */
181 void
182 process_initialize(struct process *pr, struct proc *p)
183 {
184 	/* initialize the thread links */
185 	pr->ps_mainproc = p;
186 	TAILQ_INIT(&pr->ps_threads);
187 	TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link);
188 	pr->ps_refcnt = 1;
189 	p->p_p = pr;
190 
191 	/* give the process the same creds as the initial thread */
192 	pr->ps_ucred = p->p_ucred;
193 	crhold(pr->ps_ucred);
194 	KASSERT(p->p_ucred->cr_ref >= 2);	/* new thread and new process */
195 
196 	LIST_INIT(&pr->ps_children);
197 
198 	timeout_set(&pr->ps_realit_to, realitexpire, pr);
199 }
200 
201 
202 /*
203  * Allocate and initialize a new process.
204  */
205 struct process *
206 process_new(struct proc *p, struct process *parent, int flags)
207 {
208 	struct process *pr;
209 
210 	pr = pool_get(&process_pool, PR_WAITOK);
211 
212 	/*
213 	 * Make a process structure for the new process.
214 	 * Start by zeroing the section of proc that is zero-initialized,
215 	 * then copy the section that is copied directly from the parent.
216 	 */
217 	memset(&pr->ps_startzero, 0,
218 	    (caddr_t)&pr->ps_endzero - (caddr_t)&pr->ps_startzero);
219 	memcpy(&pr->ps_startcopy, &parent->ps_startcopy,
220 	    (caddr_t)&pr->ps_endcopy - (caddr_t)&pr->ps_startcopy);
221 
222 	process_initialize(pr, p);
223 	pr->ps_pid = allocpid();
224 
225 	/* post-copy fixups */
226 	pr->ps_pptr = parent;
227 	pr->ps_limit->p_refcnt++;
228 
229 	/* bump references to the text vnode (for sysctl) */
230 	pr->ps_textvp = parent->ps_textvp;
231 	if (pr->ps_textvp)
232 		vref(pr->ps_textvp);
233 
234 	pr->ps_flags = parent->ps_flags &
235 	    (PS_SUGID | PS_SUGIDEXEC | PS_PLEDGE | PS_WXNEEDED);
236 	if (parent->ps_session->s_ttyvp != NULL)
237 		pr->ps_flags |= parent->ps_flags & PS_CONTROLT;
238 
239 	/*
240 	 * Duplicate sub-structures as needed.
241 	 * Increase reference counts on shared objects.
242 	 */
243 	if (flags & FORK_SHAREFILES)
244 		pr->ps_fd = fdshare(parent);
245 	else
246 		pr->ps_fd = fdcopy(parent);
247 	if (flags & FORK_SIGHAND)
248 		pr->ps_sigacts = sigactsshare(parent);
249 	else
250 		pr->ps_sigacts = sigactsinit(parent);
251 	if (flags & FORK_SHAREVM)
252 		pr->ps_vmspace = uvmspace_share(parent);
253 	else
254 		pr->ps_vmspace = uvmspace_fork(parent);
255 
256 	if (pr->ps_pledgepaths)
257 		pr->ps_pledgepaths->wl_ref++;
258 
259 	if (parent->ps_flags & PS_PROFIL)
260 		startprofclock(pr);
261 	if (flags & FORK_PTRACE)
262 		pr->ps_flags |= parent->ps_flags & PS_TRACED;
263 	if (flags & FORK_NOZOMBIE)
264 		pr->ps_flags |= PS_NOZOMBIE;
265 	if (flags & FORK_SYSTEM)
266 		pr->ps_flags |= PS_SYSTEM;
267 
268 	/* mark as embryo to protect against others */
269 	pr->ps_flags |= PS_EMBRYO;
270 
271 	/* Force visibility of all of the above changes */
272 	membar_producer();
273 
274 	/* it's sufficiently inited to be globally visible */
275 	LIST_INSERT_HEAD(&allprocess, pr, ps_list);
276 
277 	return pr;
278 }
279 
280 /* print the 'table full' message once per 10 seconds */
281 struct timeval fork_tfmrate = { 10, 0 };
282 
283 int
284 fork_check_maxthread(uid_t uid)
285 {
286 	/*
287 	 * Although process entries are dynamically created, we still keep
288 	 * a global limit on the maximum number we will create. We reserve
289 	 * the last 5 processes to root. The variable nprocesses is the
290 	 * current number of processes, maxprocess is the limit.  Similar
291 	 * rules for threads (struct proc): we reserve the last 5 to root;
292 	 * the variable nthreads is the current number of procs, maxthread is
293 	 * the limit.
294 	 */
295 	if ((nthreads >= maxthread - 5 && uid != 0) || nthreads >= maxthread) {
296 		static struct timeval lasttfm;
297 
298 		if (ratecheck(&lasttfm, &fork_tfmrate))
299 			tablefull("proc");
300 		return EAGAIN;
301 	}
302 	nthreads++;
303 
304 	return 0;
305 }
306 
307 static inline void
308 fork_thread_start(struct proc *p, struct proc *parent, int flags)
309 {
310 	int s;
311 
312 	SCHED_LOCK(s);
313 	p->p_stat = SRUN;
314 	p->p_cpu = sched_choosecpu_fork(parent, flags);
315 	setrunqueue(p);
316 	SCHED_UNLOCK(s);
317 }
318 
319 int
320 fork1(struct proc *curp, int flags, void (*func)(void *), void *arg,
321     register_t *retval, struct proc **rnewprocp)
322 {
323 	struct process *curpr = curp->p_p;
324 	struct process *pr;
325 	struct proc *p;
326 	uid_t uid = curp->p_ucred->cr_ruid;
327 	struct vmspace *vm;
328 	int count;
329 	vaddr_t uaddr;
330 	int error;
331 	struct  ptrace_state *newptstat = NULL;
332 
333 	KASSERT((flags & ~(FORK_FORK | FORK_VFORK | FORK_PPWAIT | FORK_PTRACE
334 	    | FORK_IDLE | FORK_SHAREVM | FORK_SHAREFILES | FORK_NOZOMBIE
335 	    | FORK_SYSTEM | FORK_SIGHAND)) == 0);
336 	KASSERT((flags & FORK_SIGHAND) == 0 || (flags & FORK_SHAREVM));
337 	KASSERT(func != NULL);
338 
339 	if ((error = fork_check_maxthread(uid)))
340 		return error;
341 
342 	if ((nprocesses >= maxprocess - 5 && uid != 0) ||
343 	    nprocesses >= maxprocess) {
344 		static struct timeval lasttfm;
345 
346 		if (ratecheck(&lasttfm, &fork_tfmrate))
347 			tablefull("process");
348 		nthreads--;
349 		return EAGAIN;
350 	}
351 	nprocesses++;
352 
353 	/*
354 	 * Increment the count of processes running with this uid.
355 	 * Don't allow a nonprivileged user to exceed their current limit.
356 	 */
357 	count = chgproccnt(uid, 1);
358 	if (uid != 0 && count > curp->p_rlimit[RLIMIT_NPROC].rlim_cur) {
359 		(void)chgproccnt(uid, -1);
360 		nprocesses--;
361 		nthreads--;
362 		return EAGAIN;
363 	}
364 
365 	uaddr = uvm_uarea_alloc();
366 	if (uaddr == 0) {
367 		(void)chgproccnt(uid, -1);
368 		nprocesses--;
369 		nthreads--;
370 		return (ENOMEM);
371 	}
372 
373 	/*
374 	 * From now on, we're committed to the fork and cannot fail.
375 	 */
376 	p = thread_new(curp, uaddr);
377 	pr = process_new(p, curpr, flags);
378 
379 	p->p_fd		= pr->ps_fd;
380 	p->p_vmspace	= pr->ps_vmspace;
381 	if (pr->ps_flags & PS_SYSTEM)
382 		atomic_setbits_int(&p->p_flag, P_SYSTEM);
383 
384 	if (flags & FORK_PPWAIT) {
385 		atomic_setbits_int(&pr->ps_flags, PS_PPWAIT);
386 		atomic_setbits_int(&curpr->ps_flags, PS_ISPWAIT);
387 	}
388 
389 #ifdef KTRACE
390 	/*
391 	 * Copy traceflag and tracefile if enabled.
392 	 * If not inherited, these were zeroed above.
393 	 */
394 	if (curpr->ps_traceflag & KTRFAC_INHERIT)
395 		ktrsettrace(pr, curpr->ps_traceflag, curpr->ps_tracevp,
396 		    curpr->ps_tracecred);
397 #endif
398 
399 	/*
400 	 * Finish creating the child thread.  cpu_fork() will copy
401 	 * and update the pcb and make the child ready to run.  If
402 	 * this is a normal user fork, the child will exit directly
403 	 * to user mode via child_return() on its first time slice
404 	 * and will not return here.  If this is a kernel thread,
405 	 * the specified entry point will be executed.
406 	 */
407 	cpu_fork(curp, p, NULL, NULL, func, arg ? arg : p);
408 
409 	vm = pr->ps_vmspace;
410 
411 	if (flags & FORK_FORK) {
412 		forkstat.cntfork++;
413 		forkstat.sizfork += vm->vm_dsize + vm->vm_ssize;
414 	} else if (flags & FORK_VFORK) {
415 		forkstat.cntvfork++;
416 		forkstat.sizvfork += vm->vm_dsize + vm->vm_ssize;
417 	} else {
418 		forkstat.cntkthread++;
419 	}
420 
421 	if (pr->ps_flags & PS_TRACED && flags & FORK_FORK)
422 		newptstat = malloc(sizeof(*newptstat), M_SUBPROC, M_WAITOK);
423 
424 	p->p_tid = alloctid();
425 
426 	LIST_INSERT_HEAD(&allproc, p, p_list);
427 	LIST_INSERT_HEAD(TIDHASH(p->p_tid), p, p_hash);
428 	LIST_INSERT_HEAD(PIDHASH(pr->ps_pid), pr, ps_hash);
429 	LIST_INSERT_AFTER(curpr, pr, ps_pglist);
430 	LIST_INSERT_HEAD(&curpr->ps_children, pr, ps_sibling);
431 
432 	if (pr->ps_flags & PS_TRACED) {
433 		pr->ps_oppid = curpr->ps_pid;
434 		if (pr->ps_pptr != curpr->ps_pptr)
435 			proc_reparent(pr, curpr->ps_pptr);
436 
437 		/*
438 		 * Set ptrace status.
439 		 */
440 		if (newptstat != NULL) {
441 			pr->ps_ptstat = newptstat;
442 			newptstat = NULL;
443 			curpr->ps_ptstat->pe_report_event = PTRACE_FORK;
444 			pr->ps_ptstat->pe_report_event = PTRACE_FORK;
445 			curpr->ps_ptstat->pe_other_pid = pr->ps_pid;
446 			pr->ps_ptstat->pe_other_pid = curpr->ps_pid;
447 		}
448 	}
449 
450 	/*
451 	 * For new processes, set accounting bits and mark as complete.
452 	 */
453 	getnanotime(&pr->ps_start);
454 	pr->ps_acflag = AFORK;
455 	atomic_clearbits_int(&pr->ps_flags, PS_EMBRYO);
456 
457 	if ((flags & FORK_IDLE) == 0)
458 		fork_thread_start(p, curp, flags);
459 	else
460 		p->p_cpu = arg;
461 
462 	free(newptstat, M_SUBPROC, sizeof(*newptstat));
463 
464 	/*
465 	 * Notify any interested parties about the new process.
466 	 */
467 	KNOTE(&curpr->ps_klist, NOTE_FORK | pr->ps_pid);
468 
469 	/*
470 	 * Update stats now that we know the fork was successful.
471 	 */
472 	uvmexp.forks++;
473 	if (flags & FORK_PPWAIT)
474 		uvmexp.forks_ppwait++;
475 	if (flags & FORK_SHAREVM)
476 		uvmexp.forks_sharevm++;
477 
478 	/*
479 	 * Pass a pointer to the new process to the caller.
480 	 */
481 	if (rnewprocp != NULL)
482 		*rnewprocp = p;
483 
484 	/*
485 	 * Preserve synchronization semantics of vfork.  If waiting for
486 	 * child to exec or exit, set PS_PPWAIT on child and PS_ISPWAIT
487 	 * on ourselves, and sleep on our process for the latter flag
488 	 * to go away.
489 	 * XXX Need to stop other rthreads in the parent
490 	 */
491 	if (flags & FORK_PPWAIT)
492 		while (curpr->ps_flags & PS_ISPWAIT)
493 			tsleep(curpr, PWAIT, "ppwait", 0);
494 
495 	/*
496 	 * If we're tracing the child, alert the parent too.
497 	 */
498 	if ((flags & FORK_PTRACE) && (curpr->ps_flags & PS_TRACED))
499 		psignal(curp, SIGTRAP);
500 
501 	/*
502 	 * Return child pid to parent process
503 	 */
504 	if (retval != NULL) {
505 		retval[0] = pr->ps_pid;
506 		retval[1] = 0;
507 	}
508 	return (0);
509 }
510 
511 int
512 thread_fork(struct proc *curp, void *stack, void *tcb, pid_t *tidptr,
513     register_t *retval)
514 {
515 	struct process *pr = curp->p_p;
516 	struct proc *p;
517 	pid_t tid;
518 	vaddr_t uaddr;
519 	int error;
520 
521 	if (stack == NULL)
522 		return EINVAL;
523 
524 	if ((error = fork_check_maxthread(curp->p_ucred->cr_ruid)))
525 		return error;
526 
527 	uaddr = uvm_uarea_alloc();
528 	if (uaddr == 0) {
529 		nthreads--;
530 		return ENOMEM;
531 	}
532 
533 	/*
534 	 * From now on, we're committed to the fork and cannot fail.
535 	 */
536 	p = thread_new(curp, uaddr);
537 	atomic_setbits_int(&p->p_flag, P_THREAD);
538 	sigstkinit(&p->p_sigstk);
539 
540 	/* other links */
541 	p->p_p = pr;
542 	pr->ps_refcnt++;
543 
544 	/* local copies */
545 	p->p_fd		= pr->ps_fd;
546 	p->p_vmspace	= pr->ps_vmspace;
547 
548 	/*
549 	 * Finish creating the child thread.  cpu_fork() will copy
550 	 * and update the pcb and make the child ready to run.  The
551 	 * child will exit directly to user mode via child_return()
552 	 * on its first time slice and will not return here.
553 	 */
554 	cpu_fork(curp, p, stack, tcb, child_return, p);
555 
556 	p->p_tid = alloctid();
557 
558 	LIST_INSERT_HEAD(&allproc, p, p_list);
559 	LIST_INSERT_HEAD(TIDHASH(p->p_tid), p, p_hash);
560 	TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link);
561 
562 	/*
563 	 * if somebody else wants to take us to single threaded mode,
564 	 * count ourselves in.
565 	 */
566 	if (pr->ps_single) {
567 		pr->ps_singlecount++;
568 		atomic_setbits_int(&p->p_flag, P_SUSPSINGLE);
569 	}
570 
571 	/*
572 	 * Return tid to parent thread and copy it out to userspace
573 	 */
574 	retval[0] = tid = p->p_tid + THREAD_PID_OFFSET;
575 	retval[1] = 0;
576 	if (tidptr != NULL) {
577 		if (copyout(&tid, tidptr, sizeof(tid)))
578 			psignal(curp, SIGSEGV);
579 	}
580 
581 	fork_thread_start(p, curp, 0);
582 
583 	/*
584 	 * Update stats now that we know the fork was successful.
585 	 */
586 	forkstat.cnttfork++;
587 	uvmexp.forks++;
588 	uvmexp.forks_sharevm++;
589 
590 	return 0;
591 }
592 
593 
594 /* Find an unused tid */
595 pid_t
596 alloctid(void)
597 {
598 	pid_t tid;
599 
600 	do {
601 		/* (0 .. TID_MASK+1] */
602 		tid = 1 + (arc4random() & TID_MASK);
603 	} while (tfind(tid) != NULL);
604 
605 	return (tid);
606 }
607 
608 /*
609  * Checks for current use of a pid, either as a pid or pgid.
610  */
611 pid_t oldpids[128];
612 int
613 ispidtaken(pid_t pid)
614 {
615 	uint32_t i;
616 
617 	for (i = 0; i < nitems(oldpids); i++)
618 		if (pid == oldpids[i])
619 			return (1);
620 
621 	if (prfind(pid) != NULL)
622 		return (1);
623 	if (pgfind(pid) != NULL)
624 		return (1);
625 	if (zombiefind(pid) != NULL)
626 		return (1);
627 	return (0);
628 }
629 
630 /* Find an unused pid */
631 pid_t
632 allocpid(void)
633 {
634 	static pid_t lastpid;
635 	pid_t pid;
636 
637 	if (!randompid) {
638 		/* only used early on for system processes */
639 		pid = ++lastpid;
640 	} else {
641 		/* Find an unused pid satisfying lastpid < pid <= PID_MAX */
642 		do {
643 			pid = arc4random_uniform(PID_MAX - lastpid) + 1 +
644 			    lastpid;
645 		} while (ispidtaken(pid));
646 	}
647 
648 	return pid;
649 }
650 
651 void
652 freepid(pid_t pid)
653 {
654 	static uint32_t idx;
655 
656 	oldpids[idx++ % nitems(oldpids)] = pid;
657 }
658 
659 #if defined(MULTIPROCESSOR)
660 /*
661  * XXX This is a slight hack to get newly-formed processes to
662  * XXX acquire the kernel lock as soon as they run.
663  */
664 void
665 proc_trampoline_mp(void)
666 {
667 	SCHED_ASSERT_LOCKED();
668 	__mp_unlock(&sched_lock);
669 	spl0();
670 	SCHED_ASSERT_UNLOCKED();
671 	KERNEL_ASSERT_UNLOCKED();
672 
673 	KERNEL_LOCK();
674 }
675 #endif
676