xref: /freebsd/sys/kern/kern_fork.c (revision ee41f1b1cf5e3d4f586cb85b46123b416275862c)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_fork.c	8.6 (Berkeley) 4/8/94
39  * $FreeBSD$
40  */
41 
42 #include "opt_ktrace.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/malloc.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.h>
53 #include <sys/resourcevar.h>
54 #include <sys/syscall.h>
55 #include <sys/vnode.h>
56 #include <sys/acct.h>
57 #include <sys/ktr.h>
58 #include <sys/ktrace.h>
59 #include <sys/kthread.h>
60 #include <sys/unistd.h>
61 #include <sys/jail.h>
62 
63 #include <vm/vm.h>
64 #include <sys/lock.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_zone.h>
69 
70 #include <sys/vmmeter.h>
71 #include <sys/user.h>
72 
73 static MALLOC_DEFINE(M_ATFORK, "atfork", "atfork callback");
74 
75 static int	fast_vfork = 1;
76 SYSCTL_INT(_kern, OID_AUTO, fast_vfork, CTLFLAG_RW, &fast_vfork, 0,
77     "flag to indicate whether we have a fast vfork()");
78 
79 /*
80  * These are the stuctures used to create a callout list for things to do
81  * when forking a process
82  */
83 struct forklist {
84 	forklist_fn function;
85 	TAILQ_ENTRY(forklist) next;
86 };
87 
88 TAILQ_HEAD(forklist_head, forklist);
89 static struct forklist_head fork_list = TAILQ_HEAD_INITIALIZER(fork_list);
90 
91 #ifndef _SYS_SYSPROTO_H_
92 struct fork_args {
93 	int     dummy;
94 };
95 #endif
96 
97 /* ARGSUSED */
98 int
99 fork(p, uap)
100 	struct proc *p;
101 	struct fork_args *uap;
102 {
103 	int error;
104 	struct proc *p2;
105 
106 	error = fork1(p, RFFDG | RFPROC, &p2);
107 	if (error == 0) {
108 		p->p_retval[0] = p2->p_pid;
109 		p->p_retval[1] = 0;
110 	}
111 	return error;
112 }
113 
114 /* ARGSUSED */
115 int
116 vfork(p, uap)
117 	struct proc *p;
118 	struct vfork_args *uap;
119 {
120 	int error;
121 	struct proc *p2;
122 
123 	error = fork1(p, RFFDG | RFPROC | RFPPWAIT | RFMEM, &p2);
124 	if (error == 0) {
125 		p->p_retval[0] = p2->p_pid;
126 		p->p_retval[1] = 0;
127 	}
128 	return error;
129 }
130 
131 int
132 rfork(p, uap)
133 	struct proc *p;
134 	struct rfork_args *uap;
135 {
136 	int error;
137 	struct proc *p2;
138 
139 	/* mask kernel only flags out of the user flags */
140 	error = fork1(p, uap->flags & ~RFKERNELONLY, &p2);
141 	if (error == 0) {
142 		p->p_retval[0] = p2 ? p2->p_pid : 0;
143 		p->p_retval[1] = 0;
144 	}
145 	return error;
146 }
147 
148 
149 int	nprocs = 1;				/* process 0 */
150 static int nextpid = 0;
151 SYSCTL_INT(_kern, OID_AUTO, lastpid, CTLFLAG_RD, &nextpid, 0,
152     "Last used PID");
153 
154 /*
155  * Random component to nextpid generation.  We mix in a random factor to make
156  * it a little harder to predict.  We sanity check the modulus value to avoid
157  * doing it in critical paths.  Don't let it be too small or we pointlessly
158  * waste randomness entropy, and don't let it be impossibly large.  Using a
159  * modulus that is too big causes a LOT more process table scans and slows
160  * down fork processing as the pidchecked caching is defeated.
161  */
162 static int randompid = 0;
163 
164 static int
165 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
166 {
167 	int error, pid;
168 
169 	pid = randompid;
170 	error = sysctl_handle_int(oidp, &pid, 0, req);
171 	if (error || !req->newptr)
172 		return (error);
173 	if (pid < 0 || pid > PID_MAX - 100)	/* out of range */
174 		pid = PID_MAX - 100;
175 	else if (pid < 2)			/* NOP */
176 		pid = 0;
177 	else if (pid < 100)			/* Make it reasonable */
178 		pid = 100;
179 	randompid = pid;
180 	return (error);
181 }
182 
183 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
184     0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
185 
186 int
187 fork1(p1, flags, procp)
188 	struct proc *p1;			/* parent proc */
189 	int flags;
190 	struct proc **procp;			/* child proc */
191 {
192 	struct proc *p2, *pptr;
193 	uid_t uid;
194 	struct proc *newproc;
195 	int trypid;
196 	int ok;
197 	static int pidchecked = 0;
198 	struct forklist *ep;
199 
200 	/* Can't copy and clear */
201 	if ((flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG))
202 		return (EINVAL);
203 
204 	/*
205 	 * Here we don't create a new process, but we divorce
206 	 * certain parts of a process from itself.
207 	 */
208 	if ((flags & RFPROC) == 0) {
209 
210 		vm_fork(p1, 0, flags);
211 
212 		/*
213 		 * Close all file descriptors.
214 		 */
215 		if (flags & RFCFDG) {
216 			struct filedesc *fdtmp;
217 			fdtmp = fdinit(p1);
218 			fdfree(p1);
219 			p1->p_fd = fdtmp;
220 		}
221 
222 		/*
223 		 * Unshare file descriptors (from parent.)
224 		 */
225 		if (flags & RFFDG) {
226 			if (p1->p_fd->fd_refcnt > 1) {
227 				struct filedesc *newfd;
228 				newfd = fdcopy(p1);
229 				fdfree(p1);
230 				p1->p_fd = newfd;
231 			}
232 		}
233 		*procp = NULL;
234 		return (0);
235 	}
236 
237 	/*
238 	 * Although process entries are dynamically created, we still keep
239 	 * a global limit on the maximum number we will create.  Don't allow
240 	 * a nonprivileged user to use the last process; don't let root
241 	 * exceed the limit. The variable nprocs is the current number of
242 	 * processes, maxproc is the limit.
243 	 */
244 	uid = p1->p_cred->p_ruid;
245 	if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
246 		tablefull("proc");
247 		return (EAGAIN);
248 	}
249 	/*
250 	 * Increment the nprocs resource before blocking can occur.  There
251 	 * are hard-limits as to the number of processes that can run.
252 	 */
253 	nprocs++;
254 
255 	/*
256 	 * Increment the count of procs running with this uid. Don't allow
257 	 * a nonprivileged user to exceed their current limit.
258 	 */
259 	ok = chgproccnt(p1->p_cred->p_uidinfo, 1,
260 		(uid != 0) ? p1->p_rlimit[RLIMIT_NPROC].rlim_cur : 0);
261 	if (!ok) {
262 		/*
263 		 * Back out the process count
264 		 */
265 		nprocs--;
266 		return (EAGAIN);
267 	}
268 
269 	/* Allocate new proc. */
270 	newproc = zalloc(proc_zone);
271 
272 	/*
273 	 * Setup linkage for kernel based threading
274 	 */
275 	if((flags & RFTHREAD) != 0) {
276 		newproc->p_peers = p1->p_peers;
277 		p1->p_peers = newproc;
278 		newproc->p_leader = p1->p_leader;
279 	} else {
280 		newproc->p_peers = NULL;
281 		newproc->p_leader = newproc;
282 	}
283 
284 	newproc->p_vmspace = NULL;
285 
286 	/*
287 	 * Find an unused process ID.  We remember a range of unused IDs
288 	 * ready to use (from nextpid+1 through pidchecked-1).
289 	 *
290 	 * If RFHIGHPID is set (used during system boot), do not allocate
291 	 * low-numbered pids.
292 	 */
293 	ALLPROC_LOCK(AP_EXCLUSIVE);
294 	trypid = nextpid + 1;
295 	if (flags & RFHIGHPID) {
296 		if (trypid < 10) {
297 			trypid = 10;
298 		}
299 	} else {
300 		if (randompid)
301 			trypid += arc4random() % randompid;
302 	}
303 retry:
304 	/*
305 	 * If the process ID prototype has wrapped around,
306 	 * restart somewhat above 0, as the low-numbered procs
307 	 * tend to include daemons that don't exit.
308 	 */
309 	if (trypid >= PID_MAX) {
310 		trypid = trypid % PID_MAX;
311 		if (trypid < 100)
312 			trypid += 100;
313 		pidchecked = 0;
314 	}
315 	if (trypid >= pidchecked) {
316 		int doingzomb = 0;
317 
318 		pidchecked = PID_MAX;
319 		/*
320 		 * Scan the active and zombie procs to check whether this pid
321 		 * is in use.  Remember the lowest pid that's greater
322 		 * than trypid, so we can avoid checking for a while.
323 		 */
324 		p2 = LIST_FIRST(&allproc);
325 again:
326 		for (; p2 != NULL; p2 = LIST_NEXT(p2, p_list)) {
327 			while (p2->p_pid == trypid ||
328 			    p2->p_pgrp->pg_id == trypid ||
329 			    p2->p_session->s_sid == trypid) {
330 				trypid++;
331 				if (trypid >= pidchecked)
332 					goto retry;
333 			}
334 			if (p2->p_pid > trypid && pidchecked > p2->p_pid)
335 				pidchecked = p2->p_pid;
336 			if (p2->p_pgrp->pg_id > trypid &&
337 			    pidchecked > p2->p_pgrp->pg_id)
338 				pidchecked = p2->p_pgrp->pg_id;
339 			if (p2->p_session->s_sid > trypid &&
340 			    pidchecked > p2->p_session->s_sid)
341 				pidchecked = p2->p_session->s_sid;
342 		}
343 		if (!doingzomb) {
344 			doingzomb = 1;
345 			p2 = LIST_FIRST(&zombproc);
346 			goto again;
347 		}
348 	}
349 
350 	/*
351 	 * RFHIGHPID does not mess with the nextpid counter during boot.
352 	 */
353 	if (flags & RFHIGHPID)
354 		pidchecked = 0;
355 	else
356 		nextpid = trypid;
357 
358 	p2 = newproc;
359 	p2->p_intr_nesting_level = 0;
360 	p2->p_stat = SIDL;			/* protect against others */
361 	p2->p_pid = trypid;
362 	LIST_INSERT_HEAD(&allproc, p2, p_list);
363 	LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
364 	ALLPROC_LOCK(AP_RELEASE);
365 
366 	/*
367 	 * Make a proc table entry for the new process.
368 	 * Start by zeroing the section of proc that is zero-initialized,
369 	 * then copy the section that is copied directly from the parent.
370 	 */
371 	bzero(&p2->p_startzero,
372 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
373 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
374 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
375 
376 	mtx_init(&p2->p_mtx, "process lock", MTX_DEF);
377 	p2->p_aioinfo = NULL;
378 
379 	/*
380 	 * Duplicate sub-structures as needed.
381 	 * Increase reference counts on shared objects.
382 	 * The p_stats and p_sigacts substructs are set in vm_fork.
383 	 */
384 	p2->p_flag = 0;
385 	mtx_lock_spin(&sched_lock);
386 	p2->p_sflag = PS_INMEM;
387 	if (p1->p_sflag & PS_PROFIL)
388 		startprofclock(p2);
389 	mtx_unlock_spin(&sched_lock);
390 	MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
391 	    M_SUBPROC, M_WAITOK);
392 	bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
393 	p2->p_cred->p_refcnt = 1;
394 	crhold(p1->p_ucred);
395 	uihold(p1->p_cred->p_uidinfo);
396 
397 	if (p2->p_prison) {
398 		p2->p_prison->pr_ref++;
399 		p2->p_flag |= P_JAILED;
400 	}
401 
402 	if (p2->p_args)
403 		p2->p_args->ar_ref++;
404 
405 	if (flags & RFSIGSHARE) {
406 		p2->p_procsig = p1->p_procsig;
407 		p2->p_procsig->ps_refcnt++;
408 		if (p1->p_sigacts == &p1->p_addr->u_sigacts) {
409 			struct sigacts *newsigacts;
410 			int s;
411 
412 			/* Create the shared sigacts structure */
413 			MALLOC(newsigacts, struct sigacts *,
414 			    sizeof(struct sigacts), M_SUBPROC, M_WAITOK);
415 			s = splhigh();
416 			/*
417 			 * Set p_sigacts to the new shared structure.
418 			 * Note that this is updating p1->p_sigacts at the
419 			 * same time, since p_sigacts is just a pointer to
420 			 * the shared p_procsig->ps_sigacts.
421 			 */
422 			p2->p_sigacts  = newsigacts;
423 			bcopy(&p1->p_addr->u_sigacts, p2->p_sigacts,
424 			    sizeof(*p2->p_sigacts));
425 			*p2->p_sigacts = p1->p_addr->u_sigacts;
426 			splx(s);
427 		}
428 	} else {
429 		MALLOC(p2->p_procsig, struct procsig *, sizeof(struct procsig),
430 		    M_SUBPROC, M_WAITOK);
431 		bcopy(p1->p_procsig, p2->p_procsig, sizeof(*p2->p_procsig));
432 		p2->p_procsig->ps_refcnt = 1;
433 		p2->p_sigacts = NULL;	/* finished in vm_fork() */
434 	}
435 	if (flags & RFLINUXTHPN)
436 	        p2->p_sigparent = SIGUSR1;
437 	else
438 	        p2->p_sigparent = SIGCHLD;
439 
440 	/* bump references to the text vnode (for procfs) */
441 	p2->p_textvp = p1->p_textvp;
442 	if (p2->p_textvp)
443 		VREF(p2->p_textvp);
444 
445 	if (flags & RFCFDG)
446 		p2->p_fd = fdinit(p1);
447 	else if (flags & RFFDG)
448 		p2->p_fd = fdcopy(p1);
449 	else
450 		p2->p_fd = fdshare(p1);
451 
452 	/*
453 	 * If p_limit is still copy-on-write, bump refcnt,
454 	 * otherwise get a copy that won't be modified.
455 	 * (If PL_SHAREMOD is clear, the structure is shared
456 	 * copy-on-write.)
457 	 */
458 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
459 		p2->p_limit = limcopy(p1->p_limit);
460 	else {
461 		p2->p_limit = p1->p_limit;
462 		p2->p_limit->p_refcnt++;
463 	}
464 
465 	/*
466 	 * Preserve some more flags in subprocess.  P_PROFIL has already
467 	 * been preserved.
468 	 */
469 	p2->p_flag |= p1->p_flag & P_SUGID;
470 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
471 		p2->p_flag |= P_CONTROLT;
472 	if (flags & RFPPWAIT)
473 		p2->p_flag |= P_PPWAIT;
474 
475 	LIST_INSERT_AFTER(p1, p2, p_pglist);
476 
477 	/*
478 	 * Attach the new process to its parent.
479 	 *
480 	 * If RFNOWAIT is set, the newly created process becomes a child
481 	 * of init.  This effectively disassociates the child from the
482 	 * parent.
483 	 */
484 	if (flags & RFNOWAIT)
485 		pptr = initproc;
486 	else
487 		pptr = p1;
488 	PROCTREE_LOCK(PT_EXCLUSIVE);
489 	p2->p_pptr = pptr;
490 	LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling);
491 	PROCTREE_LOCK(PT_RELEASE);
492 	LIST_INIT(&p2->p_children);
493 	LIST_INIT(&p2->p_heldmtx);
494 	LIST_INIT(&p2->p_contested);
495 
496 	callout_init(&p2->p_itcallout, 0);
497 	callout_init(&p2->p_slpcallout, 1);
498 
499 #ifdef KTRACE
500 	/*
501 	 * Copy traceflag and tracefile if enabled.
502 	 * If not inherited, these were zeroed above.
503 	 */
504 	if (p1->p_traceflag&KTRFAC_INHERIT) {
505 		p2->p_traceflag = p1->p_traceflag;
506 		if ((p2->p_tracep = p1->p_tracep) != NULL)
507 			VREF(p2->p_tracep);
508 	}
509 #endif
510 
511 	/*
512 	 * set priority of child to be that of parent
513 	 */
514 	p2->p_estcpu = p1->p_estcpu;
515 
516 	/*
517 	 * This begins the section where we must prevent the parent
518 	 * from being swapped.
519 	 */
520 	PHOLD(p1);
521 
522 	/*
523 	 * Finish creating the child process.  It will return via a different
524 	 * execution path later.  (ie: directly into user mode)
525 	 */
526 	vm_fork(p1, p2, flags);
527 
528 	if (flags == (RFFDG | RFPROC)) {
529 		cnt.v_forks++;
530 		cnt.v_forkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
531 	} else if (flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM)) {
532 		cnt.v_vforks++;
533 		cnt.v_vforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
534 	} else if (p1 == &proc0) {
535 		cnt.v_kthreads++;
536 		cnt.v_kthreadpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
537 	} else {
538 		cnt.v_rforks++;
539 		cnt.v_rforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
540 	}
541 
542 	/*
543 	 * Both processes are set up, now check if any loadable modules want
544 	 * to adjust anything.
545 	 *   What if they have an error? XXX
546 	 */
547 	TAILQ_FOREACH(ep, &fork_list, next) {
548 		(*ep->function)(p1, p2, flags);
549 	}
550 
551 	/*
552 	 * If RFSTOPPED not requested, make child runnable and add to
553 	 * run queue.
554 	 */
555 	microtime(&(p2->p_stats->p_start));
556 	p2->p_acflag = AFORK;
557 	if ((flags & RFSTOPPED) == 0) {
558 		splhigh();
559 		mtx_lock_spin(&sched_lock);
560 		p2->p_stat = SRUN;
561 		setrunqueue(p2);
562 		mtx_unlock_spin(&sched_lock);
563 		spl0();
564 	}
565 
566 	/*
567 	 * Now can be swapped.
568 	 */
569 	PRELE(p1);
570 
571 	/*
572 	 * tell any interested parties about the new process
573 	 */
574 	KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid);
575 
576 	/*
577 	 * Preserve synchronization semantics of vfork.  If waiting for
578 	 * child to exec or exit, set P_PPWAIT on child, and sleep on our
579 	 * proc (in case of exit).
580 	 */
581 	while (p2->p_flag & P_PPWAIT)
582 		tsleep(p1, PWAIT, "ppwait", 0);
583 
584 	/*
585 	 * Return child proc pointer to parent.
586 	 */
587 	*procp = p2;
588 	return (0);
589 }
590 
591 /*
592  * The next two functionms are general routines to handle adding/deleting
593  * items on the fork callout list.
594  *
595  * at_fork():
596  * Take the arguments given and put them onto the fork callout list,
597  * However first make sure that it's not already there.
598  * Returns 0 on success or a standard error number.
599  */
600 
601 int
602 at_fork(function)
603 	forklist_fn function;
604 {
605 	struct forklist *ep;
606 
607 #ifdef INVARIANTS
608 	/* let the programmer know if he's been stupid */
609 	if (rm_at_fork(function))
610 		printf("WARNING: fork callout entry (%p) already present\n",
611 		    function);
612 #endif
613 	ep = malloc(sizeof(*ep), M_ATFORK, M_NOWAIT);
614 	if (ep == NULL)
615 		return (ENOMEM);
616 	ep->function = function;
617 	TAILQ_INSERT_TAIL(&fork_list, ep, next);
618 	return (0);
619 }
620 
621 /*
622  * Scan the exit callout list for the given item and remove it..
623  * Returns the number of items removed (0 or 1)
624  */
625 
626 int
627 rm_at_fork(function)
628 	forklist_fn function;
629 {
630 	struct forklist *ep;
631 
632 	TAILQ_FOREACH(ep, &fork_list, next) {
633 		if (ep->function == function) {
634 			TAILQ_REMOVE(&fork_list, ep, next);
635 			free(ep, M_ATFORK);
636 			return(1);
637 		}
638 	}
639 	return (0);
640 }
641 
642 /*
643  * Handle the return of a child process from fork1().  This function
644  * is called from the MD fork_trampoline() entry point.
645  */
646 void
647 fork_exit(callout, arg, frame)
648 	void (*callout)(void *, struct trapframe *);
649 	void *arg;
650 	struct trapframe *frame;
651 {
652 	struct proc *p;
653 
654 	mtx_unlock_spin(&sched_lock);
655 	/*
656 	 * XXX: We really shouldn't have to do this.
657 	 */
658 	enable_intr();
659 
660 #ifdef SMP
661 	if (PCPU_GET(switchtime.tv_sec) == 0)
662 		microuptime(PCPU_PTR(switchtime));
663 	PCPU_SET(switchticks, ticks);
664 #endif
665 
666 	/*
667 	 * cpu_set_fork_handler intercepts this function call to
668          * have this call a non-return function to stay in kernel mode.
669          * initproc has its own fork handler, but it does return.
670          */
671 	callout(arg, frame);
672 
673 	/*
674 	 * Check if a kernel thread misbehaved and returned from its main
675 	 * function.
676 	 */
677 	p = CURPROC;
678 	if (p->p_flag & P_KTHREAD) {
679 		mtx_lock(&Giant);
680 		printf("Kernel thread \"%s\" (pid %d) exited prematurely.\n",
681 		    p->p_comm, p->p_pid);
682 		kthread_exit(0);
683 	}
684 	mtx_assert(&Giant, MA_NOTOWNED);
685 }
686 
687 /*
688  * Simplified back end of syscall(), used when returning from fork()
689  * directly into user mode.  Giant is not held on entry, and must not
690  * be held on return.  This function is passed in to fork_exit() as the
691  * first parameter and is called when returning to a new userland process.
692  */
693 void
694 fork_return(p, frame)
695 	struct proc *p;
696 	struct trapframe *frame;
697 {
698 
699 	userret(p, frame, 0);
700 #ifdef KTRACE
701 	if (KTRPOINT(p, KTR_SYSRET)) {
702 		if (!mtx_owned(&Giant))
703 			mtx_lock(&Giant);
704 		ktrsysret(p->p_tracep, SYS_fork, 0, 0);
705 	}
706 #endif
707 	if (mtx_owned(&Giant))
708 		mtx_unlock(&Giant);
709 	mtx_assert(&Giant, MA_NOTOWNED);
710 }
711