xref: /freebsd/sys/kern/init_main.c (revision 983ed4f9f1cba96b078141fad58ef32005428c9b)
19454b2d8SWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
42b14f991SJulian Elischer  * Copyright (c) 1995 Terrence R. Lambert
52b14f991SJulian Elischer  * All rights reserved.
62b14f991SJulian Elischer  *
7df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
8df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
9df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
10df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
11df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
12df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
13df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
14df8bae1dSRodney W. Grimes  *
15df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
16df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
17df8bae1dSRodney W. Grimes  * are met:
18df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
20df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
21df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
22df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
23df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
24df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
25df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
26df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
27df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
28df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
29df8bae1dSRodney W. Grimes  *    without specific prior written permission.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  *	@(#)init_main.c	8.9 (Berkeley) 1/21/94
44df8bae1dSRodney W. Grimes  */
45df8bae1dSRodney W. Grimes 
46677b542eSDavid E. O'Brien #include <sys/cdefs.h>
47677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
48677b542eSDavid E. O'Brien 
4977fe4438SBenno Rice #include "opt_ddb.h"
5067481196SJohn Birrell #include "opt_init_path.h"
519541307fSJohn-Mark Gurney #include "opt_verbose_sysinit.h"
524bd49128SPeter Wemm 
53df8bae1dSRodney W. Grimes #include <sys/param.h>
549e209b12SAlfred Perlstein #include <sys/kernel.h>
55905837ebSGleb Smirnoff #include <sys/epoch.h>
56f36ba452SJake Burkholder #include <sys/exec.h>
57b3c5c18dSBruce Evans #include <sys/file.h>
58df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
593a325decSBrooks Davis #include <sys/imgact.h>
600304c731SJamie Gritton #include <sys/jail.h>
610384fff8SJason Evans #include <sys/ktr.h>
621005a129SJohn Baldwin #include <sys/lock.h>
632bfc50bcSEdward Tomasz Napierala #include <sys/loginclass.h>
64ac0ad63fSBruce Evans #include <sys/mount.h>
6535e0e5b3SJohn Baldwin #include <sys/mutex.h>
668f19eb88SIan Dowse #include <sys/syscallsubr.h>
67946bb7a2SPoul-Henning Kamp #include <sys/sysctl.h>
68df8bae1dSRodney W. Grimes #include <sys/proc.h>
69097055e2SEdward Tomasz Napierala #include <sys/racct.h>
70df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
71df8bae1dSRodney W. Grimes #include <sys/systm.h>
7236240ea5SDoug Rabson #include <sys/signalvar.h>
73df8bae1dSRodney W. Grimes #include <sys/vnode.h>
74f3f0ca60SSøren Schmidt #include <sys/sysent.h>
75df8bae1dSRodney W. Grimes #include <sys/reboot.h>
76de028f5aSJeff Roberson #include <sys/sched.h>
771005a129SJohn Baldwin #include <sys/sx.h>
78ad7507e2SSteven Wallace #include <sys/sysproto.h>
79efeaf95aSDavid Greenman #include <sys/vmmeter.h>
8074b2192aSJohn Dyson #include <sys/unistd.h>
8194e9d7c1SPeter Wemm #include <sys/malloc.h>
82db901281SPoul-Henning Kamp #include <sys/conf.h>
83d7f687fcSJeff Roberson #include <sys/cpuset.h>
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #include <machine/cpu.h>
86df8bae1dSRodney W. Grimes 
873683665bSRobert Watson #include <security/audit/audit.h>
88aed55708SRobert Watson #include <security/mac/mac_framework.h>
893683665bSRobert Watson 
90df8bae1dSRodney W. Grimes #include <vm/vm.h>
91efeaf95aSDavid Greenman #include <vm/vm_param.h>
92e2068d0bSJeff Roberson #include <vm/vm_extern.h>
93efeaf95aSDavid Greenman #include <vm/pmap.h>
94efeaf95aSDavid Greenman #include <vm/vm_map.h>
956faaa756SWolfram Schneider #include <sys/copyright.h>
96df8bae1dSRodney W. Grimes 
9726ab616fSBenno Rice #include <ddb/ddb.h>
9826ab616fSBenno Rice #include <ddb/db_sym.h>
9926ab616fSBenno Rice 
10037b087a6SPeter Wemm void mi_startup(void);				/* Should be elsewhere */
101df8bae1dSRodney W. Grimes 
102df8bae1dSRodney W. Grimes /* Components of the first process -- never freed. */
103154c04e5SPoul-Henning Kamp static struct session session0;
104154c04e5SPoul-Henning Kamp static struct pgrp pgrp0;
105df8bae1dSRodney W. Grimes struct	proc proc0;
106f743ea96SMateusz Guzik struct thread0_storage thread0_st __aligned(32);
10723734995SJake Burkholder struct	vmspace vmspace0;
1082b14f991SJulian Elischer struct	proc *initproc;
109df8bae1dSRodney W. Grimes 
110*983ed4f9SMatt Macy int
111*983ed4f9SMatt Macy linux_alloc_current_noop(struct thread *td __unused, int flags __unused)
112*983ed4f9SMatt Macy {
113*983ed4f9SMatt Macy 	return (0);
114*983ed4f9SMatt Macy }
115*983ed4f9SMatt Macy int (*lkpi_alloc_current)(struct thread *, int) = linux_alloc_current_noop;
116*983ed4f9SMatt Macy 
117*983ed4f9SMatt Macy 
11856fddc5dSBrooks Davis #ifndef BOOTHOWTO
11956fddc5dSBrooks Davis #define	BOOTHOWTO	0
12056fddc5dSBrooks Davis #endif
12156fddc5dSBrooks Davis int	boothowto = BOOTHOWTO;	/* initialized so that it can be patched */
122a0c87b74SGavin Atkinson SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0,
123a0c87b74SGavin Atkinson 	"Boot control flags, passed from loader");
12456fddc5dSBrooks Davis 
12556fddc5dSBrooks Davis #ifndef BOOTVERBOSE
12656fddc5dSBrooks Davis #define	BOOTVERBOSE	0
12756fddc5dSBrooks Davis #endif
12856fddc5dSBrooks Davis int	bootverbose = BOOTVERBOSE;
129a0c87b74SGavin Atkinson SYSCTL_INT(_debug, OID_AUTO, bootverbose, CTLFLAG_RW, &bootverbose, 0,
130a0c87b74SGavin Atkinson 	"Control the output of verbose kernel messages");
131946bb7a2SPoul-Henning Kamp 
132c7962400SKyle Evans #ifdef VERBOSE_SYSINIT
133c7962400SKyle Evans /*
134c7962400SKyle Evans  * We'll use the defined value of VERBOSE_SYSINIT from the kernel config to
135c7962400SKyle Evans  * dictate the default VERBOSE_SYSINIT behavior.  Significant values for this
136c7962400SKyle Evans  * option and associated tunable are:
137c7962400SKyle Evans  * - 0, 'compiled in but silent by default'
138c7962400SKyle Evans  * - 1, 'compiled in but verbose by default' (default)
139c7962400SKyle Evans  */
140c7962400SKyle Evans int	verbose_sysinit = VERBOSE_SYSINIT;
141c7962400SKyle Evans TUNABLE_INT("debug.verbose_sysinit", &verbose_sysinit);
142c7962400SKyle Evans #endif
143c7962400SKyle Evans 
14413534277SWarner Losh #ifdef INVARIANTS
1453f272816SWarner Losh FEATURE(invariants, "Kernel compiled with INVARIANTS, may affect performance");
14613534277SWarner Losh #endif
14713534277SWarner Losh 
1482b14f991SJulian Elischer /*
1492b14f991SJulian Elischer  * This ensures that there is at least one entry so that the sysinit_set
1502b14f991SJulian Elischer  * symbol is not undefined.  A sybsystem ID of SI_SUB_DUMMY is never
1512b14f991SJulian Elischer  * executed.
15226f9a767SRodney W. Grimes  */
153237fdd78SRobert Watson SYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL);
1548a129caeSDavid Greenman 
15594e9d7c1SPeter Wemm /*
15694e9d7c1SPeter Wemm  * The sysinit table itself.  Items are checked off as the are run.
15794e9d7c1SPeter Wemm  * If we want to register new sysinit types, add them to newsysinit.
15894e9d7c1SPeter Wemm  */
159f41325dbSPeter Wemm SET_DECLARE(sysinit_set, struct sysinit);
160f41325dbSPeter Wemm struct sysinit **sysinit, **sysinit_end;
161f41325dbSPeter Wemm struct sysinit **newsysinit, **newsysinit_end;
16294e9d7c1SPeter Wemm 
1632ca45184SMatt Joras EVENTHANDLER_LIST_DECLARE(process_init);
1642ca45184SMatt Joras EVENTHANDLER_LIST_DECLARE(thread_init);
1652ca45184SMatt Joras EVENTHANDLER_LIST_DECLARE(process_ctor);
1662ca45184SMatt Joras EVENTHANDLER_LIST_DECLARE(thread_ctor);
1672ca45184SMatt Joras 
16894e9d7c1SPeter Wemm /*
16994e9d7c1SPeter Wemm  * Merge a new sysinit set into the current set, reallocating it if
17094e9d7c1SPeter Wemm  * necessary.  This can only be called after malloc is running.
17194e9d7c1SPeter Wemm  */
17294e9d7c1SPeter Wemm void
173f41325dbSPeter Wemm sysinit_add(struct sysinit **set, struct sysinit **set_end)
17494e9d7c1SPeter Wemm {
17594e9d7c1SPeter Wemm 	struct sysinit **newset;
17694e9d7c1SPeter Wemm 	struct sysinit **sipp;
17794e9d7c1SPeter Wemm 	struct sysinit **xipp;
178f41325dbSPeter Wemm 	int count;
17994e9d7c1SPeter Wemm 
180f41325dbSPeter Wemm 	count = set_end - set;
181ddd62546SPeter Wemm 	if (newsysinit)
182f41325dbSPeter Wemm 		count += newsysinit_end - newsysinit;
183ddd62546SPeter Wemm 	else
184f41325dbSPeter Wemm 		count += sysinit_end - sysinit;
185ac2fffa4SPedro F. Giffuni 	newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT);
18694e9d7c1SPeter Wemm 	if (newset == NULL)
18794e9d7c1SPeter Wemm 		panic("cannot malloc for sysinit");
18894e9d7c1SPeter Wemm 	xipp = newset;
189ddd62546SPeter Wemm 	if (newsysinit)
190f41325dbSPeter Wemm 		for (sipp = newsysinit; sipp < newsysinit_end; sipp++)
191ddd62546SPeter Wemm 			*xipp++ = *sipp;
192ddd62546SPeter Wemm 	else
193f41325dbSPeter Wemm 		for (sipp = sysinit; sipp < sysinit_end; sipp++)
19494e9d7c1SPeter Wemm 			*xipp++ = *sipp;
195f41325dbSPeter Wemm 	for (sipp = set; sipp < set_end; sipp++)
19694e9d7c1SPeter Wemm 		*xipp++ = *sipp;
197ddd62546SPeter Wemm 	if (newsysinit)
198ddd62546SPeter Wemm 		free(newsysinit, M_TEMP);
19994e9d7c1SPeter Wemm 	newsysinit = newset;
200f41325dbSPeter Wemm 	newsysinit_end = newset + count;
20194e9d7c1SPeter Wemm }
20226f9a767SRodney W. Grimes 
203b871e661SJohn Baldwin #if defined (DDB) && defined(VERBOSE_SYSINIT)
204b871e661SJohn Baldwin static const char *
205b871e661SJohn Baldwin symbol_name(vm_offset_t va, db_strategy_t strategy)
206b871e661SJohn Baldwin {
207b871e661SJohn Baldwin 	const char *name;
208b871e661SJohn Baldwin 	c_db_sym_t sym;
209b871e661SJohn Baldwin 	db_expr_t  offset;
210b871e661SJohn Baldwin 
211b871e661SJohn Baldwin 	if (va == 0)
212b871e661SJohn Baldwin 		return (NULL);
213b871e661SJohn Baldwin 	sym = db_search_symbol(va, strategy, &offset);
214b871e661SJohn Baldwin 	if (offset != 0)
215b871e661SJohn Baldwin 		return (NULL);
216b871e661SJohn Baldwin 	db_symbol_values(sym, &name, NULL);
217b871e661SJohn Baldwin 	return (name);
218b871e661SJohn Baldwin }
219b871e661SJohn Baldwin #endif
220b871e661SJohn Baldwin 
221df8bae1dSRodney W. Grimes /*
222df8bae1dSRodney W. Grimes  * System startup; initialize the world, create process 0, mount root
223df8bae1dSRodney W. Grimes  * filesystem, and fork to create init and pagedaemon.  Most of the
224df8bae1dSRodney W. Grimes  * hard work is done in the lower-level initialization routines including
225df8bae1dSRodney W. Grimes  * startup(), which does memory initialization and autoconfiguration.
2262b14f991SJulian Elischer  *
2272b14f991SJulian Elischer  * This allows simple addition of new kernel subsystems that require
2282b14f991SJulian Elischer  * boot time initialization.  It also allows substitution of subsystem
2292b14f991SJulian Elischer  * (for instance, a scheduler, kernel profiler, or VM system) by object
230c5b193bfSPoul-Henning Kamp  * module.  Finally, it allows for optional "kernel threads".
231df8bae1dSRodney W. Grimes  */
23226f9a767SRodney W. Grimes void
23337b087a6SPeter Wemm mi_startup(void)
234df8bae1dSRodney W. Grimes {
2352b14f991SJulian Elischer 
23661f481fbSKevin Lo 	struct sysinit **sipp;	/* system initialization*/
23761f481fbSKevin Lo 	struct sysinit **xipp;	/* interior loop of sort*/
23861f481fbSKevin Lo 	struct sysinit *save;	/* bubble*/
2392b14f991SJulian Elischer 
24026ab616fSBenno Rice #if defined(VERBOSE_SYSINIT)
24126ab616fSBenno Rice 	int last;
24226ab616fSBenno Rice 	int verbose;
24326ab616fSBenno Rice #endif
24426ab616fSBenno Rice 
2456032e088SColin Percival 	TSENTER();
2466032e088SColin Percival 
247b94e6f0eSJohn Baldwin 	if (boothowto & RB_VERBOSE)
248b94e6f0eSJohn Baldwin 		bootverbose++;
249b94e6f0eSJohn Baldwin 
250f41325dbSPeter Wemm 	if (sysinit == NULL) {
251f41325dbSPeter Wemm 		sysinit = SET_BEGIN(sysinit_set);
252f41325dbSPeter Wemm 		sysinit_end = SET_LIMIT(sysinit_set);
253f41325dbSPeter Wemm 	}
254f41325dbSPeter Wemm 
25594e9d7c1SPeter Wemm restart:
2562b14f991SJulian Elischer 	/*
2572b14f991SJulian Elischer 	 * Perform a bubble sort of the system initialization objects by
2582b14f991SJulian Elischer 	 * their subsystem (primary key) and order (secondary key).
2592b14f991SJulian Elischer 	 */
260f41325dbSPeter Wemm 	for (sipp = sysinit; sipp < sysinit_end; sipp++) {
261f41325dbSPeter Wemm 		for (xipp = sipp + 1; xipp < sysinit_end; xipp++) {
2622b14f991SJulian Elischer 			if ((*sipp)->subsystem < (*xipp)->subsystem ||
2632b14f991SJulian Elischer 			     ((*sipp)->subsystem == (*xipp)->subsystem &&
264af4b2d2dSPeter Wemm 			      (*sipp)->order <= (*xipp)->order))
2652b14f991SJulian Elischer 				continue;	/* skip*/
2662b14f991SJulian Elischer 			save = *sipp;
2672b14f991SJulian Elischer 			*sipp = *xipp;
2682b14f991SJulian Elischer 			*xipp = save;
2692b14f991SJulian Elischer 		}
2702b14f991SJulian Elischer 	}
2712b14f991SJulian Elischer 
27226ab616fSBenno Rice #if defined(VERBOSE_SYSINIT)
27326ab616fSBenno Rice 	last = SI_SUB_COPYRIGHT;
27426ab616fSBenno Rice 	verbose = 0;
27526ab616fSBenno Rice #if !defined(DDB)
27626ab616fSBenno Rice 	printf("VERBOSE_SYSINIT: DDB not enabled, symbol lookups disabled.\n");
27726ab616fSBenno Rice #endif
27826ab616fSBenno Rice #endif
27926ab616fSBenno Rice 
2802b14f991SJulian Elischer 	/*
2812b14f991SJulian Elischer 	 * Traverse the (now) ordered list of system initialization tasks.
2822b14f991SJulian Elischer 	 * Perform each task, and continue on to the next task.
2832b14f991SJulian Elischer 	 */
284f41325dbSPeter Wemm 	for (sipp = sysinit; sipp < sysinit_end; sipp++) {
28574b2192aSJohn Dyson 
2862b14f991SJulian Elischer 		if ((*sipp)->subsystem == SI_SUB_DUMMY)
2872b14f991SJulian Elischer 			continue;	/* skip dummy task(s)*/
2882b14f991SJulian Elischer 
28994e9d7c1SPeter Wemm 		if ((*sipp)->subsystem == SI_SUB_DONE)
29094e9d7c1SPeter Wemm 			continue;
29194e9d7c1SPeter Wemm 
29226ab616fSBenno Rice #if defined(VERBOSE_SYSINIT)
293c7962400SKyle Evans 		if ((*sipp)->subsystem > last && verbose_sysinit != 0) {
29426ab616fSBenno Rice 			verbose = 1;
29526ab616fSBenno Rice 			last = (*sipp)->subsystem;
29626ab616fSBenno Rice 			printf("subsystem %x\n", last);
29726ab616fSBenno Rice 		}
29826ab616fSBenno Rice 		if (verbose) {
29926ab616fSBenno Rice #if defined(DDB)
300b871e661SJohn Baldwin 			const char *func, *data;
30126ab616fSBenno Rice 
302b871e661SJohn Baldwin 			func = symbol_name((vm_offset_t)(*sipp)->func,
303b871e661SJohn Baldwin 			    DB_STGY_PROC);
304b871e661SJohn Baldwin 			data = symbol_name((vm_offset_t)(*sipp)->udata,
305b871e661SJohn Baldwin 			    DB_STGY_ANY);
306b871e661SJohn Baldwin 			if (func != NULL && data != NULL)
307b871e661SJohn Baldwin 				printf("   %s(&%s)... ", func, data);
308b871e661SJohn Baldwin 			else if (func != NULL)
309b871e661SJohn Baldwin 				printf("   %s(%p)... ", func, (*sipp)->udata);
31026ab616fSBenno Rice 			else
31126ab616fSBenno Rice #endif
31226ab616fSBenno Rice 				printf("   %p(%p)... ", (*sipp)->func,
31326ab616fSBenno Rice 				    (*sipp)->udata);
31426ab616fSBenno Rice 		}
31526ab616fSBenno Rice #endif
31626ab616fSBenno Rice 
3179c8b8baaSPeter Wemm 		/* Call function */
3182b14f991SJulian Elischer 		(*((*sipp)->func))((*sipp)->udata);
31994e9d7c1SPeter Wemm 
32026ab616fSBenno Rice #if defined(VERBOSE_SYSINIT)
32126ab616fSBenno Rice 		if (verbose)
32226ab616fSBenno Rice 			printf("done.\n");
32326ab616fSBenno Rice #endif
32426ab616fSBenno Rice 
32594e9d7c1SPeter Wemm 		/* Check off the one we're just done */
32694e9d7c1SPeter Wemm 		(*sipp)->subsystem = SI_SUB_DONE;
32794e9d7c1SPeter Wemm 
32894e9d7c1SPeter Wemm 		/* Check if we've installed more sysinit items via KLD */
32994e9d7c1SPeter Wemm 		if (newsysinit != NULL) {
330f41325dbSPeter Wemm 			if (sysinit != SET_BEGIN(sysinit_set))
33194e9d7c1SPeter Wemm 				free(sysinit, M_TEMP);
33294e9d7c1SPeter Wemm 			sysinit = newsysinit;
333f41325dbSPeter Wemm 			sysinit_end = newsysinit_end;
33494e9d7c1SPeter Wemm 			newsysinit = NULL;
335f41325dbSPeter Wemm 			newsysinit_end = NULL;
33694e9d7c1SPeter Wemm 			goto restart;
33794e9d7c1SPeter Wemm 		}
3382b14f991SJulian Elischer 	}
3392b14f991SJulian Elischer 
3406032e088SColin Percival 	TSEXIT();	/* Here so we don't overlap with start_init. */
3416032e088SColin Percival 
342785797c3SAndriy Gapon 	mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED);
343785797c3SAndriy Gapon 	mtx_unlock(&Giant);
344785797c3SAndriy Gapon 
345785797c3SAndriy Gapon 	/*
346785797c3SAndriy Gapon 	 * Now hand over this thread to swapper.
347785797c3SAndriy Gapon 	 */
348785797c3SAndriy Gapon 	swapper();
3492b14f991SJulian Elischer 	/* NOTREACHED*/
3502b14f991SJulian Elischer }
3512b14f991SJulian Elischer 
3529ef6c28aSBruce Evans static void
35378edb09eSAndriy Gapon print_caddr_t(void *data)
3549ef6c28aSBruce Evans {
3559ef6c28aSBruce Evans 	printf("%s", (char *)data);
3569ef6c28aSBruce Evans }
3577e936cefSAndriy Gapon 
3587e936cefSAndriy Gapon static void
3597e936cefSAndriy Gapon print_version(void *data __unused)
3607e936cefSAndriy Gapon {
3617e936cefSAndriy Gapon 	int len;
3627e936cefSAndriy Gapon 
3637e936cefSAndriy Gapon 	/* Strip a trailing newline from version. */
3647e936cefSAndriy Gapon 	len = strlen(version);
3657e936cefSAndriy Gapon 	while (len > 0 && version[len - 1] == '\n')
3667e936cefSAndriy Gapon 		len--;
3677e936cefSAndriy Gapon 	printf("%.*s %s\n", len, version, machine);
368bfdcb3bcSAndriy Gapon 	printf("%s\n", compiler_version);
3697e936cefSAndriy Gapon }
3707e936cefSAndriy Gapon 
371237fdd78SRobert Watson SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t,
372237fdd78SRobert Watson     copyright);
373237fdd78SRobert Watson SYSINIT(trademark, SI_SUB_COPYRIGHT, SI_ORDER_SECOND, print_caddr_t,
374237fdd78SRobert Watson     trademark);
3757e936cefSAndriy Gapon SYSINIT(version, SI_SUB_COPYRIGHT, SI_ORDER_THIRD, print_version, NULL);
3767a08bae6SJohn Baldwin 
3772cf6bdacSPoul-Henning Kamp #ifdef WITNESS
3782cf6bdacSPoul-Henning Kamp static char wit_warn[] =
3792cf6bdacSPoul-Henning Kamp      "WARNING: WITNESS option enabled, expect reduced performance.\n";
3805add74b4SRobert Watson SYSINIT(witwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 1,
381237fdd78SRobert Watson    print_caddr_t, wit_warn);
382785797c3SAndriy Gapon SYSINIT(witwarn2, SI_SUB_LAST, SI_ORDER_THIRD + 1,
383237fdd78SRobert Watson    print_caddr_t, wit_warn);
3842cf6bdacSPoul-Henning Kamp #endif
3852cf6bdacSPoul-Henning Kamp 
3862cf6bdacSPoul-Henning Kamp #ifdef DIAGNOSTIC
3872cf6bdacSPoul-Henning Kamp static char diag_warn[] =
3882cf6bdacSPoul-Henning Kamp      "WARNING: DIAGNOSTIC option enabled, expect reduced performance.\n";
38988b85279SRobert Watson SYSINIT(diagwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 2,
390237fdd78SRobert Watson     print_caddr_t, diag_warn);
391785797c3SAndriy Gapon SYSINIT(diagwarn2, SI_SUB_LAST, SI_ORDER_THIRD + 2,
392237fdd78SRobert Watson     print_caddr_t, diag_warn);
3932cf6bdacSPoul-Henning Kamp #endif
3942cf6bdacSPoul-Henning Kamp 
395afe1a688SKonstantin Belousov static int
3962d88da2fSKonstantin Belousov null_fetch_syscall_args(struct thread *td __unused)
397afe1a688SKonstantin Belousov {
398afe1a688SKonstantin Belousov 
399afe1a688SKonstantin Belousov 	panic("null_fetch_syscall_args");
400afe1a688SKonstantin Belousov }
401afe1a688SKonstantin Belousov 
402afe1a688SKonstantin Belousov static void
403afe1a688SKonstantin Belousov null_set_syscall_retval(struct thread *td __unused, int error __unused)
404afe1a688SKonstantin Belousov {
405afe1a688SKonstantin Belousov 
406afe1a688SKonstantin Belousov 	panic("null_set_syscall_retval");
407afe1a688SKonstantin Belousov }
408afe1a688SKonstantin Belousov 
409f36ba452SJake Burkholder struct sysentvec null_sysvec = {
410a8d403e1SKonstantin Belousov 	.sv_size	= 0,
411a8d403e1SKonstantin Belousov 	.sv_table	= NULL,
412a8d403e1SKonstantin Belousov 	.sv_errsize	= 0,
413a8d403e1SKonstantin Belousov 	.sv_errtbl	= NULL,
414a8d403e1SKonstantin Belousov 	.sv_transtrap	= NULL,
415a8d403e1SKonstantin Belousov 	.sv_fixup	= NULL,
416a8d403e1SKonstantin Belousov 	.sv_sendsig	= NULL,
417a8d403e1SKonstantin Belousov 	.sv_sigcode	= NULL,
418a8d403e1SKonstantin Belousov 	.sv_szsigcode	= NULL,
419a8d403e1SKonstantin Belousov 	.sv_name	= "null",
420a8d403e1SKonstantin Belousov 	.sv_coredump	= NULL,
421a8d403e1SKonstantin Belousov 	.sv_imgact_try	= NULL,
422a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= 0,
423a8d403e1SKonstantin Belousov 	.sv_pagesize	= PAGE_SIZE,
424a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
425a8d403e1SKonstantin Belousov 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
426a8d403e1SKonstantin Belousov 	.sv_usrstack	= USRSTACK,
427a8d403e1SKonstantin Belousov 	.sv_psstrings	= PS_STRINGS,
428a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
429a8d403e1SKonstantin Belousov 	.sv_copyout_strings	= NULL,
430a8d403e1SKonstantin Belousov 	.sv_setregs	= NULL,
431a8d403e1SKonstantin Belousov 	.sv_fixlimit	= NULL,
432afe1a688SKonstantin Belousov 	.sv_maxssiz	= NULL,
433afe1a688SKonstantin Belousov 	.sv_flags	= 0,
434afe1a688SKonstantin Belousov 	.sv_set_syscall_retval = null_set_syscall_retval,
435afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = null_fetch_syscall_args,
436afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
437e5d81ef1SDmitry Chagin 	.sv_schedtail	= NULL,
43891d1786fSDmitry Chagin 	.sv_thread_detach = NULL,
439038c7205SDmitry Chagin 	.sv_trap	= NULL,
440f36ba452SJake Burkholder };
4413ebc1248SPeter Wemm 
4422b14f991SJulian Elischer /*
4437c686853SKevin Lo  * The two following SYSINIT's are proc0 specific glue code.  I am not
4447c686853SKevin Lo  * convinced that they can not be safely combined, but their order of
4457c686853SKevin Lo  * operation has been maintained as the same as the original init_main.c
4467c686853SKevin Lo  * for right now.
4472b14f991SJulian Elischer  */
4482b14f991SJulian Elischer /* ARGSUSED*/
449154c04e5SPoul-Henning Kamp static void
45037b087a6SPeter Wemm proc0_init(void *dummy __unused)
4512b14f991SJulian Elischer {
4528ec21e3aSPoul-Henning Kamp 	struct proc *p;
453b40ce416SJulian Elischer 	struct thread *td;
454daf63fd2SMateusz Guzik 	struct ucred *newcred;
4555949c7e5SMateusz Guzik 	struct uidinfo tmpuinfo;
456c0b5261bSMateusz Guzik 	struct loginclass tmplc = {
457c0b5261bSMateusz Guzik 		.lc_name = "",
458c0b5261bSMateusz Guzik 	};
459ac45ee97SAlan Cox 	vm_paddr_t pageablemem;
460ac45ee97SAlan Cox 	int i;
461df8bae1dSRodney W. Grimes 
4620cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
463df8bae1dSRodney W. Grimes 	p = &proc0;
464079b7badSJulian Elischer 	td = &thread0;
465df8bae1dSRodney W. Grimes 
466df8bae1dSRodney W. Grimes 	/*
467f231de47SKonstantin Belousov 	 * Initialize magic number and osrel.
4680384fff8SJason Evans 	 */
4690384fff8SJason Evans 	p->p_magic = P_MAGIC;
470f231de47SKonstantin Belousov 	p->p_osrel = osreldate;
4710384fff8SJason Evans 
4728460a577SJohn Birrell 	/*
4738460a577SJohn Birrell 	 * Initialize thread and process structures.
4748460a577SJohn Birrell 	 */
4758460a577SJohn Birrell 	procinit();	/* set up proc zone */
4768460a577SJohn Birrell 	threadinit();	/* set up UMA zones */
4778460a577SJohn Birrell 
4788460a577SJohn Birrell 	/*
4798460a577SJohn Birrell 	 * Initialise scheduler resources.
4808460a577SJohn Birrell 	 * Add scheduler specific parts to proc, thread as needed.
4818460a577SJohn Birrell 	 */
482ed062c8dSJulian Elischer 	schedinit();	/* scheduler gets its house in order */
483b1508c72SDavid Greenman 
484b1508c72SDavid Greenman 	/*
485df8bae1dSRodney W. Grimes 	 * Create process 0 (the swapper).
486df8bae1dSRodney W. Grimes 	 */
487a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&allproc, p, p_list);
4887b531e60SJohn Baldwin 	LIST_INSERT_HEAD(PIDHASH(0), p, p_hash);
4896008862bSJohn Baldwin 	mtx_init(&pgrp0.pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
490df8bae1dSRodney W. Grimes 	p->p_pgrp = &pgrp0;
491a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
492a3bfb996SJeffrey Hsu 	LIST_INIT(&pgrp0.pg_members);
493a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
494a3bfb996SJeffrey Hsu 
495df8bae1dSRodney W. Grimes 	pgrp0.pg_session = &session0;
4966008862bSJohn Baldwin 	mtx_init(&session0.s_mtx, "session", NULL, MTX_DEF);
497bc093719SEd Schouten 	refcount_init(&session0.s_count, 1);
498df8bae1dSRodney W. Grimes 	session0.s_leader = p;
499df8bae1dSRodney W. Grimes 
5003ebc1248SPeter Wemm 	p->p_sysent = &null_sysvec;
501db57c70aSKonstantin Belousov 	p->p_flag = P_SYSTEM | P_INMEM | P_KPROC;
50255648840SJohn Baldwin 	p->p_flag2 = 0;
503e602ba25SJulian Elischer 	p->p_state = PRS_NORMAL;
5049e590ff0SKonstantin Belousov 	p->p_klist = knlist_alloc(&p->p_mtx);
5052c255e9dSRobert Watson 	STAILQ_INIT(&p->p_ktr);
506fa885116SJulian Elischer 	p->p_nice = NZERO;
507abce621cSKonstantin Belousov 	/* pid_max cannot be greater than PID_MAX */
5086829a5c5SJulian Elischer 	td->td_tid = PID_MAX + 1;
50921ecd1e9SDavid Xu 	LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
510e602ba25SJulian Elischer 	td->td_state = TDS_RUNNING;
5118460a577SJohn Birrell 	td->td_pri_class = PRI_TIMESHARE;
5128460a577SJohn Birrell 	td->td_user_pri = PUSER;
51360d48235SDavid Xu 	td->td_base_user_pri = PUSER;
514acbe332aSDavid Xu 	td->td_lend_user_pri = PRI_MAX;
5152c100766SJulian Elischer 	td->td_priority = PVM;
516fd058078SJohn Baldwin 	td->td_base_pri = PVM;
5178c636a11SNathan Whitehorn 	td->td_oncpu = curcpu;
5189b9a0179SPawel Jakub Dawidek 	td->td_flags = TDF_INMEM;
5199b9a0179SPawel Jakub Dawidek 	td->td_pflags = TDP_KTHREAD;
520d7f687fcSJeff Roberson 	td->td_cpuset = cpuset_thread0();
5213f289c3fSJeff Roberson 	td->td_domain.dr_policy = td->td_cpuset->cs_domain;
522905837ebSGleb Smirnoff 	epoch_thread_init(td);
523b96bd95bSIan Lepore 	prison0_init();
5242c1011f7SJohn Dyson 	p->p_peers = 0;
5252c1011f7SJohn Dyson 	p->p_leader = p;
526237623b0SKonstantin Belousov 	p->p_reaper = p;
5270fb9f29bSBjoern A. Zeeb 	p->p_treeflag |= P_TREE_REAPER;
528237623b0SKonstantin Belousov 	LIST_INIT(&p->p_reaplist);
5292c1011f7SJohn Dyson 
5307ab24ea3SJulian Elischer 	strncpy(p->p_comm, "kernel", sizeof (p->p_comm));
5317ab24ea3SJulian Elischer 	strncpy(td->td_name, "swapper", sizeof (td->td_name));
532df8bae1dSRodney W. Grimes 
533f7e50ea7SKonstantin Belousov 	callout_init_mtx(&p->p_itcallout, &p->p_mtx, 0);
5341c4bcd05SJeff Roberson 	callout_init_mtx(&p->p_limco, &p->p_mtx, 0);
535fd90e2edSJung-uk Kim 	callout_init(&td->td_slpcallout, 1);
5364f559836SJake Burkholder 
537df8bae1dSRodney W. Grimes 	/* Create credentials. */
538daf63fd2SMateusz Guzik 	newcred = crget();
539daf63fd2SMateusz Guzik 	newcred->cr_ngroups = 1;	/* group 0 */
5405949c7e5SMateusz Guzik 	/* A hack to prevent uifind from tripping over NULL pointers. */
5415949c7e5SMateusz Guzik 	curthread->td_ucred = newcred;
5425949c7e5SMateusz Guzik 	tmpuinfo.ui_uid = 1;
5435949c7e5SMateusz Guzik 	newcred->cr_uidinfo = newcred->cr_ruidinfo = &tmpuinfo;
544daf63fd2SMateusz Guzik 	newcred->cr_uidinfo = uifind(0);
545daf63fd2SMateusz Guzik 	newcred->cr_ruidinfo = uifind(0);
546c0b5261bSMateusz Guzik 	newcred->cr_loginclass = &tmplc;
547c0b5261bSMateusz Guzik 	newcred->cr_loginclass = loginclass_find("default");
5485949c7e5SMateusz Guzik 	/* End hack. creds get properly set later with thread_cow_get_proc */
5495949c7e5SMateusz Guzik 	curthread->td_ucred = NULL;
550daf63fd2SMateusz Guzik 	newcred->cr_prison = &prison0;
551ffb34484SMateusz Guzik 	proc_set_cred_init(p, newcred);
5523683665bSRobert Watson #ifdef AUDIT
553daf63fd2SMateusz Guzik 	audit_cred_kproc0(newcred);
5543683665bSRobert Watson #endif
55540244964SRobert Watson #ifdef MAC
556daf63fd2SMateusz Guzik 	mac_cred_create_swapper(newcred);
55740244964SRobert Watson #endif
55890af4afaSJohn Baldwin 	/* Create sigacts. */
55990af4afaSJohn Baldwin 	p->p_sigacts = sigacts_alloc();
5606626c604SJulian Elischer 
56137b087a6SPeter Wemm 	/* Initialize signal state for process 0. */
56237b087a6SPeter Wemm 	siginit(&proc0);
56337b087a6SPeter Wemm 
564df8bae1dSRodney W. Grimes 	/* Create the file descriptor table. */
565eb48fbd9SMateusz Guzik 	p->p_fd = fdinit(NULL, false);
566ad05d580STor Egge 	p->p_fdtol = NULL;
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes 	/* Create the limits structures. */
56991d5354aSJohn Baldwin 	p->p_limit = lim_alloc();
57091d5354aSJohn Baldwin 	for (i = 0; i < RLIM_NLIMITS; i++)
57191d5354aSJohn Baldwin 		p->p_limit->pl_rlimit[i].rlim_cur =
57291d5354aSJohn Baldwin 		    p->p_limit->pl_rlimit[i].rlim_max = RLIM_INFINITY;
57391d5354aSJohn Baldwin 	p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur =
57491d5354aSJohn Baldwin 	    p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
57591d5354aSJohn Baldwin 	p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_cur =
57691d5354aSJohn Baldwin 	    p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
577ac45ee97SAlan Cox 	p->p_limit->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz;
578ac45ee97SAlan Cox 	p->p_limit->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz;
579ac45ee97SAlan Cox 	p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz;
580ac45ee97SAlan Cox 	p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz;
581ac45ee97SAlan Cox 	/* Cast to avoid overflow on i386/PAE. */
582e2068d0bSJeff Roberson 	pageablemem = ptoa((vm_paddr_t)vm_free_count());
583ac45ee97SAlan Cox 	p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_cur =
584ac45ee97SAlan Cox 	    p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_max = pageablemem;
585ac45ee97SAlan Cox 	p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = pageablemem / 3;
586ac45ee97SAlan Cox 	p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = pageablemem;
5875715307fSJohn Baldwin 	p->p_cpulimit = RLIM_INFINITY;
588df8bae1dSRodney W. Grimes 
5894ea6a9a2SMateusz Guzik 	PROC_LOCK(p);
5904ea6a9a2SMateusz Guzik 	thread_cow_get_proc(td, p);
5914ea6a9a2SMateusz Guzik 	PROC_UNLOCK(p);
5924ea6a9a2SMateusz Guzik 
593097055e2SEdward Tomasz Napierala 	/* Initialize resource accounting structures. */
594097055e2SEdward Tomasz Napierala 	racct_create(&p->p_racct);
595097055e2SEdward Tomasz Napierala 
5968b059651SDavid Schultz 	p->p_stats = pstats_alloc();
5978b059651SDavid Schultz 
598df8bae1dSRodney W. Grimes 	/* Allocate a prototype map so we have something to fork. */
599df8bae1dSRodney W. Grimes 	p->p_vmspace = &vmspace0;
600df8bae1dSRodney W. Grimes 	vmspace0.vm_refcnt = 1;
601100ac78bSKonstantin Belousov 	pmap_pinit0(vmspace_pmap(&vmspace0));
602878adb85SBjoern A. Zeeb 
603878adb85SBjoern A. Zeeb 	/*
604878adb85SBjoern A. Zeeb 	 * proc0 is not expected to enter usermode, so there is no special
605878adb85SBjoern A. Zeeb 	 * handling for sv_minuser here, like is done for exec_new_vmspace().
606878adb85SBjoern A. Zeeb 	 */
60792351f16SAlan Cox 	vm_map_init(&vmspace0.vm_map, vmspace_pmap(&vmspace0),
60892351f16SAlan Cox 	    p->p_sysent->sv_minuser, p->p_sysent->sv_maxuser);
6097c7454feSRandall Stewart 
610175389cfSEdward Tomasz Napierala 	/*
611175389cfSEdward Tomasz Napierala 	 * Call the init and ctor for the new thread and proc.  We wait
612175389cfSEdward Tomasz Napierala 	 * to do this until all other structures are fairly sane.
6137c7454feSRandall Stewart 	 */
6142ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(process_init, p);
6152ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_init, td);
6162ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
6172ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
618df8bae1dSRodney W. Grimes 
61939fb8e6bSJulian Elischer 	/*
620a3bfb996SJeffrey Hsu 	 * Charge root for one process.
621df8bae1dSRodney W. Grimes 	 */
622b1fc0ec1SRobert Watson 	(void)chgproccnt(p->p_ucred->cr_ruidinfo, 1, 0);
62358c77a9dSEdward Tomasz Napierala 	PROC_LOCK(p);
62458c77a9dSEdward Tomasz Napierala 	racct_add_force(p, RACCT_NPROC, 1);
62558c77a9dSEdward Tomasz Napierala 	PROC_UNLOCK(p);
62626f9a767SRodney W. Grimes }
627237fdd78SRobert Watson SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL);
6282b14f991SJulian Elischer 
6292b14f991SJulian Elischer /* ARGSUSED*/
630154c04e5SPoul-Henning Kamp static void
63137b087a6SPeter Wemm proc0_post(void *dummy __unused)
6322b14f991SJulian Elischer {
63300af9731SPoul-Henning Kamp 	struct timespec ts;
63437b087a6SPeter Wemm 	struct proc *p;
635a140976eSAttilio Rao 	struct rusage ru;
636d92909c1SRobert Watson 	struct thread *td;
637a6fc8288SPeter Wemm 
6382b14f991SJulian Elischer 	/*
6391b0b259eSBruce Evans 	 * Now we can look at the time, having had a chance to verify the
6401b0b259eSBruce Evans 	 * time from the filesystem.  Pretend that proc0 started now.
6412b14f991SJulian Elischer 	 */
6421005a129SJohn Baldwin 	sx_slock(&allproc_lock);
6434f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
64403bd1b69SBryan Drewery 		PROC_LOCK(p);
64503bd1b69SBryan Drewery 		if (p->p_state == PRS_NEW) {
64603bd1b69SBryan Drewery 			PROC_UNLOCK(p);
64703bd1b69SBryan Drewery 			continue;
64803bd1b69SBryan Drewery 		}
64987ccef7bSDag-Erling Smørgrav 		microuptime(&p->p_stats->p_start);
6505c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
651a140976eSAttilio Rao 		rufetch(p, &ru);	/* Clears thread stats */
6525b1a8eb3SPoul-Henning Kamp 		p->p_rux.rux_runtime = 0;
653a140976eSAttilio Rao 		p->p_rux.rux_uticks = 0;
654a140976eSAttilio Rao 		p->p_rux.rux_sticks = 0;
655a140976eSAttilio Rao 		p->p_rux.rux_iticks = 0;
65603bd1b69SBryan Drewery 		PROC_STATUNLOCK(p);
657d92909c1SRobert Watson 		FOREACH_THREAD_IN_PROC(p, td) {
658d92909c1SRobert Watson 			td->td_runtime = 0;
659d92909c1SRobert Watson 		}
66003bd1b69SBryan Drewery 		PROC_UNLOCK(p);
66137b087a6SPeter Wemm 	}
6621005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
6635b1a8eb3SPoul-Henning Kamp 	PCPU_SET(switchtime, cpu_ticks());
6640384fff8SJason Evans 	PCPU_SET(switchticks, ticks);
6652b14f991SJulian Elischer 
666a6fc8288SPeter Wemm 	/*
667a6fc8288SPeter Wemm 	 * Give the ``random'' number generator a thump.
668a6fc8288SPeter Wemm 	 */
66900af9731SPoul-Henning Kamp 	nanotime(&ts);
67000af9731SPoul-Henning Kamp 	srandom(ts.tv_sec ^ ts.tv_nsec);
6712b14f991SJulian Elischer }
672237fdd78SRobert Watson SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL);
6732b14f991SJulian Elischer 
674e64585bdSRuslan Ermilov static void
675e64585bdSRuslan Ermilov random_init(void *dummy __unused)
676e64585bdSRuslan Ermilov {
677e64585bdSRuslan Ermilov 
678e64585bdSRuslan Ermilov 	/*
679e64585bdSRuslan Ermilov 	 * After CPU has been started we have some randomness on most
680e64585bdSRuslan Ermilov 	 * platforms via get_cyclecount().  For platforms that don't
681e64585bdSRuslan Ermilov 	 * we will reseed random(9) in proc0_post() as well.
682e64585bdSRuslan Ermilov 	 */
683e64585bdSRuslan Ermilov 	srandom(get_cyclecount());
684e64585bdSRuslan Ermilov }
685e64585bdSRuslan Ermilov SYSINIT(random, SI_SUB_RANDOM, SI_ORDER_FIRST, random_init, NULL);
686e64585bdSRuslan Ermilov 
687df8bae1dSRodney W. Grimes /*
6882b14f991SJulian Elischer  ***************************************************************************
6892b14f991SJulian Elischer  ****
6902b14f991SJulian Elischer  **** The following SYSINIT's and glue code should be moved to the
6912b14f991SJulian Elischer  **** respective files on a per subsystem basis.
6922b14f991SJulian Elischer  ****
6932b14f991SJulian Elischer  ***************************************************************************
694df8bae1dSRodney W. Grimes  */
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes /*
697df8bae1dSRodney W. Grimes  * List of paths to try when searching for "init".
698df8bae1dSRodney W. Grimes  */
6995f967b24SDag-Erling Smørgrav static char init_path[MAXPATHLEN] =
70067481196SJohn Birrell #ifdef	INIT_PATH
70167481196SJohn Birrell     __XSTRING(INIT_PATH);
70267481196SJohn Birrell #else
7033bedc940SSergey Kandaurov     "/sbin/init:/sbin/oinit:/sbin/init.bak:/rescue/init";
70467481196SJohn Birrell #endif
705af1408e3SLuigi Rizzo SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0,
706af1408e3SLuigi Rizzo 	"Path used to search the init process");
707df8bae1dSRodney W. Grimes 
708df8bae1dSRodney W. Grimes /*
709724447acSRalf S. Engelschall  * Shutdown timeout of init(8).
710724447acSRalf S. Engelschall  * Unused within kernel, but used to control init(8), hence do not remove.
711724447acSRalf S. Engelschall  */
712724447acSRalf S. Engelschall #ifndef INIT_SHUTDOWN_TIMEOUT
713724447acSRalf S. Engelschall #define INIT_SHUTDOWN_TIMEOUT 120
714724447acSRalf S. Engelschall #endif
715724447acSRalf S. Engelschall static int init_shutdown_timeout = INIT_SHUTDOWN_TIMEOUT;
716724447acSRalf S. Engelschall SYSCTL_INT(_kern, OID_AUTO, init_shutdown_timeout,
717a0c87b74SGavin Atkinson 	CTLFLAG_RW, &init_shutdown_timeout, 0, "Shutdown timeout of init(8). "
718a0c87b74SGavin Atkinson 	"Unused within kernel, but used to control init(8)");
719724447acSRalf S. Engelschall 
720724447acSRalf S. Engelschall /*
7215f967b24SDag-Erling Smørgrav  * Start the initial user process; try exec'ing each pathname in init_path.
722df8bae1dSRodney W. Grimes  * The program is invoked with one argument containing the boot flags.
723df8bae1dSRodney W. Grimes  */
724df8bae1dSRodney W. Grimes static void
72537b087a6SPeter Wemm start_init(void *dummy)
726df8bae1dSRodney W. Grimes {
7273a325decSBrooks Davis 	struct image_args args;
728827c3852SBrooks Davis 	int error;
729dedc82aeSBrooks Davis 	char *var, *path;
730dedc82aeSBrooks Davis 	char *free_init_path, *tmp_init_path;
731b40ce416SJulian Elischer 	struct thread *td;
7329c8b8baaSPeter Wemm 	struct proc *p;
7333a325decSBrooks Davis 	struct vmspace *oldvmspace;
734df8bae1dSRodney W. Grimes 
7356032e088SColin Percival 	TSENTER();	/* Here so we don't overlap with mi_startup. */
7366032e088SColin Percival 
737b40ce416SJulian Elischer 	td = curthread;
738b40ce416SJulian Elischer 	p = td->td_proc;
739df8bae1dSRodney W. Grimes 
740563af2ecSMaxime Henrion 	vfs_mountroot();
741fb92273bSPoul-Henning Kamp 
74243d4f8c4SDevin Teske 	/* Wipe GELI passphrase from the environment. */
74343d4f8c4SDevin Teske 	kern_unsetenv("kern.geom.eli.passphrase");
74443d4f8c4SDevin Teske 
7452be111bfSDavide Italiano 	if ((var = kern_getenv("init_path")) != NULL) {
746e80fb434SRobert Drehmel 		strlcpy(init_path, var, sizeof(init_path));
747d786139cSMaxime Henrion 		freeenv(var);
7485f967b24SDag-Erling Smørgrav 	}
749dedc82aeSBrooks Davis 	free_init_path = tmp_init_path = strdup(init_path, M_TEMP);
7505f967b24SDag-Erling Smørgrav 
751dedc82aeSBrooks Davis 	while ((path = strsep(&tmp_init_path, ":")) != NULL) {
7525f967b24SDag-Erling Smørgrav 		if (bootverbose)
753dedc82aeSBrooks Davis 			printf("start_init: trying %s\n", path);
7545f967b24SDag-Erling Smørgrav 
7553a325decSBrooks Davis 		memset(&args, 0, sizeof(args));
7563a325decSBrooks Davis 		error = exec_alloc_args(&args);
7573a325decSBrooks Davis 		if (error != 0)
7583a325decSBrooks Davis 			panic("%s: Can't allocate space for init arguments %d",
7593a325decSBrooks Davis 			    __func__, error);
7603a325decSBrooks Davis 
7613a325decSBrooks Davis 		error = exec_args_add_fname(&args, path, UIO_SYSSPACE);
7623a325decSBrooks Davis 		if (error != 0)
7633a325decSBrooks Davis 			panic("%s: Can't add fname %d", __func__, error);
7643a325decSBrooks Davis 		error = exec_args_add_arg(&args, path, UIO_SYSSPACE);
7653a325decSBrooks Davis 		if (error != 0)
7663a325decSBrooks Davis 			panic("%s: Can't add argv[0] %d", __func__, error);
767827c3852SBrooks Davis 		if (boothowto & RB_SINGLE)
768827c3852SBrooks Davis 			error = exec_args_add_arg(&args, "-s", UIO_SYSSPACE);
7693a325decSBrooks Davis 		if (error != 0)
7703a325decSBrooks Davis 			panic("%s: Can't add argv[0] %d", __func__, error);
771df8bae1dSRodney W. Grimes 
772df8bae1dSRodney W. Grimes 		/*
773df8bae1dSRodney W. Grimes 		 * Now try to exec the program.  If can't for any reason
774df8bae1dSRodney W. Grimes 		 * other than it doesn't exist, complain.
7752b14f991SJulian Elischer 		 *
77637b087a6SPeter Wemm 		 * Otherwise, return via fork_trampoline() all the way
7779c8b8baaSPeter Wemm 		 * to user mode as init!
778df8bae1dSRodney W. Grimes 		 */
7793a325decSBrooks Davis 		KASSERT((td->td_pflags & TDP_EXECVMSPC) == 0,
7803a325decSBrooks Davis 		    ("nested execve"));
7813a325decSBrooks Davis 		oldvmspace = td->td_proc->p_vmspace;
7823a325decSBrooks Davis 		error = kern_execve(td, &args, NULL);
7833a325decSBrooks Davis 		KASSERT(error != 0,
7843a325decSBrooks Davis 		    ("kern_execve returned success, not EJUSTRETURN"));
7853a325decSBrooks Davis 		if (error == EJUSTRETURN) {
7863a325decSBrooks Davis 			if ((td->td_pflags & TDP_EXECVMSPC) != 0) {
7873a325decSBrooks Davis 				KASSERT(p->p_vmspace != oldvmspace,
7883a325decSBrooks Davis 				    ("oldvmspace still used"));
7893a325decSBrooks Davis 				vmspace_free(oldvmspace);
7903a325decSBrooks Davis 				td->td_pflags &= ~TDP_EXECVMSPC;
7913a325decSBrooks Davis 			}
792dedc82aeSBrooks Davis 			free(free_init_path, M_TEMP);
7936032e088SColin Percival 			TSEXIT();
794df8bae1dSRodney W. Grimes 			return;
795db72809dSJohn Baldwin 		}
796df8bae1dSRodney W. Grimes 		if (error != ENOENT)
797dedc82aeSBrooks Davis 			printf("exec %s: error %d\n", path, error);
798df8bae1dSRodney W. Grimes 	}
799dedc82aeSBrooks Davis 	free(free_init_path, M_TEMP);
800580e7e5aSGreg Lehey 	printf("init: not found in path %s\n", init_path);
801df8bae1dSRodney W. Grimes 	panic("no init");
802df8bae1dSRodney W. Grimes }
80337b087a6SPeter Wemm 
80437b087a6SPeter Wemm /*
80528323addSBryan Drewery  * Like kproc_create(), but runs in its own address space.
80637b087a6SPeter Wemm  * We do this early to reserve pid 1.
80737b087a6SPeter Wemm  *
80837b087a6SPeter Wemm  * Note special case - do not make it runnable yet.  Other work
80937b087a6SPeter Wemm  * in progress will change this more.
81037b087a6SPeter Wemm  */
81137b087a6SPeter Wemm static void
81237b087a6SPeter Wemm create_init(const void *udata __unused)
81337b087a6SPeter Wemm {
81433fd9b9aSMateusz Guzik 	struct fork_req fr;
8155a06cb0cSRobert Watson 	struct ucred *newcred, *oldcred;
816cd672ca6SMateusz Guzik 	struct thread *td;
81737b087a6SPeter Wemm 	int error;
81837b087a6SPeter Wemm 
81933fd9b9aSMateusz Guzik 	bzero(&fr, sizeof(fr));
82033fd9b9aSMateusz Guzik 	fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
82133fd9b9aSMateusz Guzik 	fr.fr_procp = &initproc;
82233fd9b9aSMateusz Guzik 	error = fork1(&thread0, &fr);
82337b087a6SPeter Wemm 	if (error)
82437b087a6SPeter Wemm 		panic("cannot fork init: %d\n", error);
825bafc8f25SRobert Watson 	KASSERT(initproc->p_pid == 1, ("create_init: initproc->p_pid != 1"));
8265a06cb0cSRobert Watson 	/* divorce init's credentials from the kernel's */
8275a06cb0cSRobert Watson 	newcred = crget();
828237623b0SKonstantin Belousov 	sx_xlock(&proctree_lock);
829f202965eSJohn Baldwin 	PROC_LOCK(initproc);
830b61ce5b0SJeff Roberson 	initproc->p_flag |= P_SYSTEM | P_INMEM;
831237623b0SKonstantin Belousov 	initproc->p_treeflag |= P_TREE_REAPER;
8325a06cb0cSRobert Watson 	oldcred = initproc->p_ucred;
8335a06cb0cSRobert Watson 	crcopy(newcred, oldcred);
83440244964SRobert Watson #ifdef MAC
835212ab0cfSRobert Watson 	mac_cred_create_init(newcred);
83640244964SRobert Watson #endif
8373683665bSRobert Watson #ifdef AUDIT
838faef5371SRobert Watson 	audit_cred_proc1(newcred);
8393683665bSRobert Watson #endif
840daf63fd2SMateusz Guzik 	proc_set_cred(initproc, newcred);
841cd672ca6SMateusz Guzik 	td = FIRST_THREAD_IN_PROC(initproc);
842cd672ca6SMateusz Guzik 	crfree(td->td_ucred);
843cd672ca6SMateusz Guzik 	td->td_ucred = crhold(initproc->p_ucred);
844f202965eSJohn Baldwin 	PROC_UNLOCK(initproc);
845237623b0SKonstantin Belousov 	sx_xunlock(&proctree_lock);
8465a06cb0cSRobert Watson 	crfree(oldcred);
8475c2cf818SKonstantin Belousov 	cpu_fork_kthread_handler(FIRST_THREAD_IN_PROC(initproc),
8485c2cf818SKonstantin Belousov 	    start_init, NULL);
84937b087a6SPeter Wemm }
850237fdd78SRobert Watson SYSINIT(init, SI_SUB_CREATE_INIT, SI_ORDER_FIRST, create_init, NULL);
85137b087a6SPeter Wemm 
85237b087a6SPeter Wemm /*
85337b087a6SPeter Wemm  * Make it runnable now.
85437b087a6SPeter Wemm  */
85537b087a6SPeter Wemm static void
85637b087a6SPeter Wemm kick_init(const void *udata __unused)
85737b087a6SPeter Wemm {
858079b7badSJulian Elischer 	struct thread *td;
859f202965eSJohn Baldwin 
860079b7badSJulian Elischer 	td = FIRST_THREAD_IN_PROC(initproc);
861982d11f8SJeff Roberson 	thread_lock(td);
86271fad9fdSJulian Elischer 	TD_SET_CAN_RUN(td);
863f0393f06SJeff Roberson 	sched_add(td, SRQ_BORING);
864982d11f8SJeff Roberson 	thread_unlock(td);
86537b087a6SPeter Wemm }
86610cb2424SMark Murray SYSINIT(kickinit, SI_SUB_KTHREAD_INIT, SI_ORDER_MIDDLE, kick_init, NULL);
867