xref: /freebsd/sys/kern/init_main.c (revision 37b087a645354d60200c774d51b305b268e41c83)
1df8bae1dSRodney W. Grimes /*
22b14f991SJulian Elischer  * Copyright (c) 1995 Terrence R. Lambert
32b14f991SJulian Elischer  * All rights reserved.
42b14f991SJulian Elischer  *
5df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
6df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
7df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
8df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
9df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
10df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
22df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
23df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
24df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
25df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
26df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
27df8bae1dSRodney W. Grimes  *    without specific prior written permission.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
40df8bae1dSRodney W. Grimes  *
41df8bae1dSRodney W. Grimes  *	@(#)init_main.c	8.9 (Berkeley) 1/21/94
42c3aac50fSPeter Wemm  * $FreeBSD$
43df8bae1dSRodney W. Grimes  */
44df8bae1dSRodney W. Grimes 
4567481196SJohn Birrell #include "opt_init_path.h"
464bd49128SPeter Wemm 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
48b3c5c18dSBruce Evans #include <sys/file.h>
49df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
50df8bae1dSRodney W. Grimes #include <sys/kernel.h>
51ac0ad63fSBruce Evans #include <sys/mount.h>
52946bb7a2SPoul-Henning Kamp #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
54df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
55df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
56df8bae1dSRodney W. Grimes #include <sys/systm.h>
57df8bae1dSRodney W. Grimes #include <sys/vnode.h>
58f3f0ca60SSøren Schmidt #include <sys/sysent.h>
59df8bae1dSRodney W. Grimes #include <sys/reboot.h>
60ad7507e2SSteven Wallace #include <sys/sysproto.h>
61efeaf95aSDavid Greenman #include <sys/vmmeter.h>
6274b2192aSJohn Dyson #include <sys/unistd.h>
6394e9d7c1SPeter Wemm #include <sys/malloc.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <machine/cpu.h>
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes #include <vm/vm.h>
68efeaf95aSDavid Greenman #include <vm/vm_param.h>
69996c772fSJohn Dyson #include <sys/lock.h>
70efeaf95aSDavid Greenman #include <vm/pmap.h>
71efeaf95aSDavid Greenman #include <vm/vm_map.h>
72efeaf95aSDavid Greenman #include <sys/user.h>
736faaa756SWolfram Schneider #include <sys/copyright.h>
74df8bae1dSRodney W. Grimes 
759ef6c28aSBruce Evans extern struct linker_set	sysinit_set;	/* XXX */
769ef6c28aSBruce Evans 
7737b087a6SPeter Wemm void mi_startup(void);				/* Should be elsewhere */
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes /* Components of the first process -- never freed. */
80154c04e5SPoul-Henning Kamp static struct session session0;
81154c04e5SPoul-Henning Kamp static struct pgrp pgrp0;
82df8bae1dSRodney W. Grimes struct	proc proc0;
83154c04e5SPoul-Henning Kamp static struct pcred cred0;
846626c604SJulian Elischer static struct procsig procsig0;
85154c04e5SPoul-Henning Kamp static struct filedesc0 filedesc0;
86154c04e5SPoul-Henning Kamp static struct plimit limit0;
87154c04e5SPoul-Henning Kamp static struct vmspace vmspace0;
882b14f991SJulian Elischer struct	proc *initproc;
89df8bae1dSRodney W. Grimes 
90dedb7b62SPeter Wemm int cmask = CMASK;
91df8bae1dSRodney W. Grimes extern	struct user *proc0paddr;
92df8bae1dSRodney W. Grimes 
932976b7f1SDavid Greenman struct	vnode *rootvp;
94c463cf1cSBruce Evans int	boothowto = 0;		/* initialized so that it can be patched */
958d0bf3d6SJordan K. Hubbard SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0, "");
96946bb7a2SPoul-Henning Kamp 
972b14f991SJulian Elischer /*
982b14f991SJulian Elischer  * This ensures that there is at least one entry so that the sysinit_set
992b14f991SJulian Elischer  * symbol is not undefined.  A sybsystem ID of SI_SUB_DUMMY is never
1002b14f991SJulian Elischer  * executed.
10126f9a767SRodney W. Grimes  */
1022b14f991SJulian Elischer SYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL)
1038a129caeSDavid Greenman 
10494e9d7c1SPeter Wemm /*
10594e9d7c1SPeter Wemm  * The sysinit table itself.  Items are checked off as the are run.
10694e9d7c1SPeter Wemm  * If we want to register new sysinit types, add them to newsysinit.
10794e9d7c1SPeter Wemm  */
10894e9d7c1SPeter Wemm struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items;
10994e9d7c1SPeter Wemm struct sysinit **newsysinit;
11094e9d7c1SPeter Wemm 
11194e9d7c1SPeter Wemm /*
11294e9d7c1SPeter Wemm  * Merge a new sysinit set into the current set, reallocating it if
11394e9d7c1SPeter Wemm  * necessary.  This can only be called after malloc is running.
11494e9d7c1SPeter Wemm  */
11594e9d7c1SPeter Wemm void
11637b087a6SPeter Wemm sysinit_add(struct sysinit **set)
11794e9d7c1SPeter Wemm {
11894e9d7c1SPeter Wemm 	struct sysinit **newset;
11994e9d7c1SPeter Wemm 	struct sysinit **sipp;
12094e9d7c1SPeter Wemm 	struct sysinit **xipp;
12194e9d7c1SPeter Wemm 	int count = 0;
12294e9d7c1SPeter Wemm 
123ddd62546SPeter Wemm 	if (newsysinit)
124ddd62546SPeter Wemm 		for (sipp = newsysinit; *sipp; sipp++)
125ddd62546SPeter Wemm 			count++;
126ddd62546SPeter Wemm 	else
12794e9d7c1SPeter Wemm 		for (sipp = sysinit; *sipp; sipp++)
12894e9d7c1SPeter Wemm 			count++;
12994e9d7c1SPeter Wemm 	for (sipp = set; *sipp; sipp++)
13094e9d7c1SPeter Wemm 		count++;
131ddd62546SPeter Wemm 	count++;		/* Trailing NULL */
13294e9d7c1SPeter Wemm 	newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT);
13394e9d7c1SPeter Wemm 	if (newset == NULL)
13494e9d7c1SPeter Wemm 		panic("cannot malloc for sysinit");
13594e9d7c1SPeter Wemm 	xipp = newset;
136ddd62546SPeter Wemm 	if (newsysinit)
137ddd62546SPeter Wemm 		for (sipp = newsysinit; *sipp; sipp++)
138ddd62546SPeter Wemm 			*xipp++ = *sipp;
139ddd62546SPeter Wemm 	else
14094e9d7c1SPeter Wemm 		for (sipp = sysinit; *sipp; sipp++)
14194e9d7c1SPeter Wemm 			*xipp++ = *sipp;
14294e9d7c1SPeter Wemm 	for (sipp = set; *sipp; sipp++)
14394e9d7c1SPeter Wemm 		*xipp++ = *sipp;
144ddd62546SPeter Wemm 	*xipp = NULL;
145ddd62546SPeter Wemm 	if (newsysinit)
146ddd62546SPeter Wemm 		free(newsysinit, M_TEMP);
14794e9d7c1SPeter Wemm 	newsysinit = newset;
14894e9d7c1SPeter Wemm }
14926f9a767SRodney W. Grimes 
150df8bae1dSRodney W. Grimes /*
151df8bae1dSRodney W. Grimes  * System startup; initialize the world, create process 0, mount root
152df8bae1dSRodney W. Grimes  * filesystem, and fork to create init and pagedaemon.  Most of the
153df8bae1dSRodney W. Grimes  * hard work is done in the lower-level initialization routines including
154df8bae1dSRodney W. Grimes  * startup(), which does memory initialization and autoconfiguration.
1552b14f991SJulian Elischer  *
1562b14f991SJulian Elischer  * This allows simple addition of new kernel subsystems that require
1572b14f991SJulian Elischer  * boot time initialization.  It also allows substitution of subsystem
1582b14f991SJulian Elischer  * (for instance, a scheduler, kernel profiler, or VM system) by object
159c5b193bfSPoul-Henning Kamp  * module.  Finally, it allows for optional "kernel threads".
160df8bae1dSRodney W. Grimes  */
16126f9a767SRodney W. Grimes void
16237b087a6SPeter Wemm mi_startup(void)
163df8bae1dSRodney W. Grimes {
1642b14f991SJulian Elischer 
1652b14f991SJulian Elischer 	register struct sysinit **sipp;		/* system initialization*/
1662b14f991SJulian Elischer 	register struct sysinit **xipp;		/* interior loop of sort*/
1672b14f991SJulian Elischer 	register struct sysinit *save;		/* bubble*/
1682b14f991SJulian Elischer 
16994e9d7c1SPeter Wemm restart:
1702b14f991SJulian Elischer 	/*
1712b14f991SJulian Elischer 	 * Perform a bubble sort of the system initialization objects by
1722b14f991SJulian Elischer 	 * their subsystem (primary key) and order (secondary key).
1732b14f991SJulian Elischer 	 */
17494e9d7c1SPeter Wemm 	for (sipp = sysinit; *sipp; sipp++) {
1752b14f991SJulian Elischer 		for (xipp = sipp + 1; *xipp; xipp++) {
1762b14f991SJulian Elischer 			if ((*sipp)->subsystem < (*xipp)->subsystem ||
1772b14f991SJulian Elischer 			     ((*sipp)->subsystem == (*xipp)->subsystem &&
178af4b2d2dSPeter Wemm 			      (*sipp)->order <= (*xipp)->order))
1792b14f991SJulian Elischer 				continue;	/* skip*/
1802b14f991SJulian Elischer 			save = *sipp;
1812b14f991SJulian Elischer 			*sipp = *xipp;
1822b14f991SJulian Elischer 			*xipp = save;
1832b14f991SJulian Elischer 		}
1842b14f991SJulian Elischer 	}
1852b14f991SJulian Elischer 
1862b14f991SJulian Elischer 	/*
1872b14f991SJulian Elischer 	 * Traverse the (now) ordered list of system initialization tasks.
1882b14f991SJulian Elischer 	 * Perform each task, and continue on to the next task.
1892b14f991SJulian Elischer 	 *
1902b14f991SJulian Elischer 	 * The last item on the list is expected to be the scheduler,
1912b14f991SJulian Elischer 	 * which will not return.
1922b14f991SJulian Elischer 	 */
19394e9d7c1SPeter Wemm 	for (sipp = sysinit; *sipp; sipp++) {
19474b2192aSJohn Dyson 
1952b14f991SJulian Elischer 		if ((*sipp)->subsystem == SI_SUB_DUMMY)
1962b14f991SJulian Elischer 			continue;	/* skip dummy task(s)*/
1972b14f991SJulian Elischer 
19894e9d7c1SPeter Wemm 		if ((*sipp)->subsystem == SI_SUB_DONE)
19994e9d7c1SPeter Wemm 			continue;
20094e9d7c1SPeter Wemm 
2019c8b8baaSPeter Wemm 		/* Call function */
2022b14f991SJulian Elischer 		(*((*sipp)->func))((*sipp)->udata);
20394e9d7c1SPeter Wemm 
20494e9d7c1SPeter Wemm 		/* Check off the one we're just done */
20594e9d7c1SPeter Wemm 		(*sipp)->subsystem = SI_SUB_DONE;
20694e9d7c1SPeter Wemm 
20794e9d7c1SPeter Wemm 		/* Check if we've installed more sysinit items via KLD */
20894e9d7c1SPeter Wemm 		if (newsysinit != NULL) {
20994e9d7c1SPeter Wemm 			if (sysinit != (struct sysinit **)sysinit_set.ls_items)
21094e9d7c1SPeter Wemm 				free(sysinit, M_TEMP);
21194e9d7c1SPeter Wemm 			sysinit = newsysinit;
21294e9d7c1SPeter Wemm 			newsysinit = NULL;
21394e9d7c1SPeter Wemm 			goto restart;
21494e9d7c1SPeter Wemm 		}
2152b14f991SJulian Elischer 	}
2162b14f991SJulian Elischer 
217477a642cSPeter Wemm 	panic("Shouldn't get here!");
2182b14f991SJulian Elischer 	/* NOTREACHED*/
2192b14f991SJulian Elischer }
2202b14f991SJulian Elischer 
2212b14f991SJulian Elischer 
2222b14f991SJulian Elischer /*
2232b14f991SJulian Elischer  ***************************************************************************
2242b14f991SJulian Elischer  ****
2252b14f991SJulian Elischer  **** The following SYSINIT's belong elsewhere, but have not yet
2262b14f991SJulian Elischer  **** been moved.
2272b14f991SJulian Elischer  ****
2282b14f991SJulian Elischer  ***************************************************************************
2292b14f991SJulian Elischer  */
2309ef6c28aSBruce Evans static void
23137b087a6SPeter Wemm print_caddr_t(void *data __unused)
2329ef6c28aSBruce Evans {
2339ef6c28aSBruce Evans 	printf("%s", (char *)data);
2349ef6c28aSBruce Evans }
235d841aaa7SBruce Evans SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright)
2362b14f991SJulian Elischer 
2372b14f991SJulian Elischer 
2382b14f991SJulian Elischer /*
2392b14f991SJulian Elischer  ***************************************************************************
2402b14f991SJulian Elischer  ****
2412b14f991SJulian Elischer  **** The two following SYSINT's are proc0 specific glue code.  I am not
2422b14f991SJulian Elischer  **** convinced that they can not be safely combined, but their order of
2432b14f991SJulian Elischer  **** operation has been maintained as the same as the original init_main.c
2442b14f991SJulian Elischer  **** for right now.
2452b14f991SJulian Elischer  ****
2462b14f991SJulian Elischer  **** These probably belong in init_proc.c or kern_proc.c, since they
2472b14f991SJulian Elischer  **** deal with proc0 (the fork template process).
2482b14f991SJulian Elischer  ****
2492b14f991SJulian Elischer  ***************************************************************************
2502b14f991SJulian Elischer  */
2512b14f991SJulian Elischer /* ARGSUSED*/
252154c04e5SPoul-Henning Kamp static void
25337b087a6SPeter Wemm proc0_init(void *dummy __unused)
2542b14f991SJulian Elischer {
255df8bae1dSRodney W. Grimes 	register struct proc		*p;
256df8bae1dSRodney W. Grimes 	register struct filedesc0	*fdp;
25792579404SAlexander Langer 	register unsigned i;
258df8bae1dSRodney W. Grimes 
259df8bae1dSRodney W. Grimes 	p = &proc0;
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 	/*
262a3bfb996SJeffrey Hsu 	 * Initialize process and pgrp structures.
263a3bfb996SJeffrey Hsu 	 */
264a3bfb996SJeffrey Hsu 	procinit();
265a3bfb996SJeffrey Hsu 
266a3bfb996SJeffrey Hsu 	/*
267b1508c72SDavid Greenman 	 * Initialize sleep queue hash table
268b1508c72SDavid Greenman 	 */
269b1508c72SDavid Greenman 	sleepinit();
270b1508c72SDavid Greenman 
271b1508c72SDavid Greenman 	/*
2723075778bSJohn Dyson 	 * additional VM structures
2733075778bSJohn Dyson 	 */
2743075778bSJohn Dyson 	vm_init2();
2753075778bSJohn Dyson 
2763075778bSJohn Dyson 	/*
277df8bae1dSRodney W. Grimes 	 * Create process 0 (the swapper).
278df8bae1dSRodney W. Grimes 	 */
279a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&allproc, p, p_list);
280df8bae1dSRodney W. Grimes 	p->p_pgrp = &pgrp0;
281a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
282a3bfb996SJeffrey Hsu 	LIST_INIT(&pgrp0.pg_members);
283a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
284a3bfb996SJeffrey Hsu 
285df8bae1dSRodney W. Grimes 	pgrp0.pg_session = &session0;
286df8bae1dSRodney W. Grimes 	session0.s_count = 1;
287df8bae1dSRodney W. Grimes 	session0.s_leader = p;
288df8bae1dSRodney W. Grimes 
289f3f0ca60SSøren Schmidt 	p->p_sysent = &aout_sysvec;
290f3f0ca60SSøren Schmidt 
291df8bae1dSRodney W. Grimes 	p->p_flag = P_INMEM | P_SYSTEM;
292df8bae1dSRodney W. Grimes 	p->p_stat = SRUN;
293df8bae1dSRodney W. Grimes 	p->p_nice = NZERO;
2947216391eSDavid Greenman 	p->p_rtprio.type = RTP_PRIO_NORMAL;
2957216391eSDavid Greenman 	p->p_rtprio.prio = 0;
296f992f480SDavid Greenman 
2972c1011f7SJohn Dyson 	p->p_peers = 0;
2982c1011f7SJohn Dyson 	p->p_leader = p;
2992c1011f7SJohn Dyson 
300df8bae1dSRodney W. Grimes 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes 	/* Create credentials. */
303df8bae1dSRodney W. Grimes 	cred0.p_refcnt = 1;
304df8bae1dSRodney W. Grimes 	p->p_cred = &cred0;
305df8bae1dSRodney W. Grimes 	p->p_ucred = crget();
306df8bae1dSRodney W. Grimes 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
307df8bae1dSRodney W. Grimes 
30875c13541SPoul-Henning Kamp 	/* Don't jail it */
30975c13541SPoul-Henning Kamp 	p->p_prison = 0;
31075c13541SPoul-Henning Kamp 
3116626c604SJulian Elischer 	/* Create procsig. */
3126626c604SJulian Elischer 	p->p_procsig = &procsig0;
31375ffaf59SLuoqi Chen 	p->p_procsig->ps_refcnt = 1;
3146626c604SJulian Elischer 
31537b087a6SPeter Wemm 	/* Initialize signal state for process 0. */
31637b087a6SPeter Wemm 	siginit(&proc0);
31737b087a6SPeter Wemm 
318df8bae1dSRodney W. Grimes 	/* Create the file descriptor table. */
319df8bae1dSRodney W. Grimes 	fdp = &filedesc0;
320df8bae1dSRodney W. Grimes 	p->p_fd = &fdp->fd_fd;
321df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_refcnt = 1;
322df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_cmask = cmask;
323df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
324df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
325df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_nfiles = NDFILE;
326df8bae1dSRodney W. Grimes 
327df8bae1dSRodney W. Grimes 	/* Create the limits structures. */
328df8bae1dSRodney W. Grimes 	p->p_limit = &limit0;
329df8bae1dSRodney W. Grimes 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
330df8bae1dSRodney W. Grimes 		limit0.pl_rlimit[i].rlim_cur =
331df8bae1dSRodney W. Grimes 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
332b3c5c18dSBruce Evans 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
333b3c5c18dSBruce Evans 	    limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
334b3c5c18dSBruce Evans 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
335b3c5c18dSBruce Evans 	    limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
336df8bae1dSRodney W. Grimes 	i = ptoa(cnt.v_free_count);
337df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
338df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
339df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
340e796e00dSPoul-Henning Kamp 	limit0.p_cpulimit = RLIM_INFINITY;
341df8bae1dSRodney W. Grimes 	limit0.p_refcnt = 1;
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes 	/* Allocate a prototype map so we have something to fork. */
344b1028ad1SLuoqi Chen 	pmap_pinit0(vmspace_pmap(&vmspace0));
345df8bae1dSRodney W. Grimes 	p->p_vmspace = &vmspace0;
346df8bae1dSRodney W. Grimes 	vmspace0.vm_refcnt = 1;
34726f9a767SRodney W. Grimes 	vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
3482d8acc0fSJohn Dyson 	    trunc_page(VM_MAXUSER_ADDRESS));
349b1028ad1SLuoqi Chen 	vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0);
350df8bae1dSRodney W. Grimes 	p->p_addr = proc0paddr;				/* XXX */
351df8bae1dSRodney W. Grimes 
35239fb8e6bSJulian Elischer 	/*
353df8bae1dSRodney W. Grimes 	 * We continue to place resource usage info and signal
354df8bae1dSRodney W. Grimes 	 * actions in the user struct so they're pageable.
355df8bae1dSRodney W. Grimes 	 */
356df8bae1dSRodney W. Grimes 	p->p_stats = &p->p_addr->u_stats;
357df8bae1dSRodney W. Grimes 	p->p_sigacts = &p->p_addr->u_sigacts;
358df8bae1dSRodney W. Grimes 
359df8bae1dSRodney W. Grimes 	/*
360a3bfb996SJeffrey Hsu 	 * Charge root for one process.
361df8bae1dSRodney W. Grimes 	 */
362c6362551SAlfred Perlstein 	(void)chgproccnt(0, 1, 0);
3632a024a2bSSean Eric Fagan 
3642a024a2bSSean Eric Fagan 	/*
365188554bbSDmitrij Tejblum 	 * Initialize the current process pointer (curproc) before
366188554bbSDmitrij Tejblum 	 * any possible traps/probes to simplify trap processing.
3672a024a2bSSean Eric Fagan 	 */
368188554bbSDmitrij Tejblum 	SET_CURPROC(p);
3692a024a2bSSean Eric Fagan 
37026f9a767SRodney W. Grimes }
3712b14f991SJulian Elischer SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL)
3722b14f991SJulian Elischer 
3732b14f991SJulian Elischer /* ARGSUSED*/
374154c04e5SPoul-Henning Kamp static void
37537b087a6SPeter Wemm proc0_post(void *dummy __unused)
3762b14f991SJulian Elischer {
37700af9731SPoul-Henning Kamp 	struct timespec ts;
37837b087a6SPeter Wemm 	struct proc *p;
379a6fc8288SPeter Wemm 
3802b14f991SJulian Elischer 	/*
3811b0b259eSBruce Evans 	 * Now we can look at the time, having had a chance to verify the
3821b0b259eSBruce Evans 	 * time from the file system.  Pretend that proc0 started now.
3832b14f991SJulian Elischer 	 */
38437b087a6SPeter Wemm 	LIST_FOREACH(p, &allproc, p_list) {
38537b087a6SPeter Wemm 		microtime(&p->p_stats->p_start);
38637b087a6SPeter Wemm 		p->p_runtime = 0;
38737b087a6SPeter Wemm 	}
388e7ba67f2SBruce Evans 	microuptime(&switchtime);
3891b0b259eSBruce Evans 	switchticks = ticks;
3902b14f991SJulian Elischer 
391a6fc8288SPeter Wemm 	/*
392a6fc8288SPeter Wemm 	 * Give the ``random'' number generator a thump.
393a6fc8288SPeter Wemm 	 */
39400af9731SPoul-Henning Kamp 	nanotime(&ts);
39500af9731SPoul-Henning Kamp 	srandom(ts.tv_sec ^ ts.tv_nsec);
3962b14f991SJulian Elischer }
3972b14f991SJulian Elischer SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL)
3982b14f991SJulian Elischer 
399df8bae1dSRodney W. Grimes /*
4002b14f991SJulian Elischer  ***************************************************************************
4012b14f991SJulian Elischer  ****
4022b14f991SJulian Elischer  **** The following SYSINIT's and glue code should be moved to the
4032b14f991SJulian Elischer  **** respective files on a per subsystem basis.
4042b14f991SJulian Elischer  ****
4052b14f991SJulian Elischer  ***************************************************************************
406df8bae1dSRodney W. Grimes  */
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 
409df8bae1dSRodney W. Grimes /*
4102b14f991SJulian Elischer  ***************************************************************************
4112b14f991SJulian Elischer  ****
4122b14f991SJulian Elischer  **** The following code probably belongs in another file, like
41337b087a6SPeter Wemm  **** kern/init_init.c.
4142b14f991SJulian Elischer  ****
4152b14f991SJulian Elischer  ***************************************************************************
416df8bae1dSRodney W. Grimes  */
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes /*
420df8bae1dSRodney W. Grimes  * List of paths to try when searching for "init".
421df8bae1dSRodney W. Grimes  */
4225f967b24SDag-Erling Smørgrav static char init_path[MAXPATHLEN] =
42367481196SJohn Birrell #ifdef	INIT_PATH
42467481196SJohn Birrell     __XSTRING(INIT_PATH);
42567481196SJohn Birrell #else
426e4082284SJohn Birrell     "/sbin/init:/sbin/oinit:/sbin/init.bak:/stand/sysinstall";
42767481196SJohn Birrell #endif
4285f967b24SDag-Erling Smørgrav SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, "");
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes /*
4315f967b24SDag-Erling Smørgrav  * Start the initial user process; try exec'ing each pathname in init_path.
432df8bae1dSRodney W. Grimes  * The program is invoked with one argument containing the boot flags.
433df8bae1dSRodney W. Grimes  */
434df8bae1dSRodney W. Grimes static void
43537b087a6SPeter Wemm start_init(void *dummy)
436df8bae1dSRodney W. Grimes {
437df8bae1dSRodney W. Grimes 	vm_offset_t addr;
438df8bae1dSRodney W. Grimes 	struct execve_args args;
4395f967b24SDag-Erling Smørgrav 	int options, error;
4405f967b24SDag-Erling Smørgrav 	char *var, *path, *next, *s;
4415f967b24SDag-Erling Smørgrav 	char *ucp, **uap, *arg0, *arg1;
4429c8b8baaSPeter Wemm 	struct proc *p;
443df8bae1dSRodney W. Grimes 
4449c8b8baaSPeter Wemm 	p = curproc;
445df8bae1dSRodney W. Grimes 
44637b087a6SPeter Wemm 	/* Get the vnode for '/'.  Set p->p_fd->fd_cdir to reference it. */
44737b087a6SPeter Wemm 	if (VFS_ROOT(TAILQ_FIRST(&mountlist), &rootvnode))
44837b087a6SPeter Wemm 		panic("cannot find root vnode");
44937b087a6SPeter Wemm 	p->p_fd->fd_cdir = rootvnode;
45037b087a6SPeter Wemm 	VREF(p->p_fd->fd_cdir);
45137b087a6SPeter Wemm 	p->p_fd->fd_rdir = rootvnode;
45237b087a6SPeter Wemm 	VOP_UNLOCK(rootvnode, 0, p);
45337b087a6SPeter Wemm 
454df8bae1dSRodney W. Grimes 	/*
455df8bae1dSRodney W. Grimes 	 * Need just enough stack to hold the faked-up "execve()" arguments.
456df8bae1dSRodney W. Grimes 	 */
4579c0fed3dSDoug Rabson 	addr = trunc_page(USRSTACK - PAGE_SIZE);
4585f967b24SDag-Erling Smørgrav 	if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE,
4595f967b24SDag-Erling Smørgrav 			FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
460df8bae1dSRodney W. Grimes 		panic("init: couldn't allocate argument space");
461df8bae1dSRodney W. Grimes 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
46226f9a767SRodney W. Grimes 	p->p_vmspace->vm_ssize = 1;
463df8bae1dSRodney W. Grimes 
4645f967b24SDag-Erling Smørgrav 	if ((var = getenv("init_path")) != NULL) {
465b83308b0SDag-Erling Smørgrav 		strncpy(init_path, var, sizeof init_path);
4665f967b24SDag-Erling Smørgrav 		init_path[sizeof init_path - 1] = 0;
4675f967b24SDag-Erling Smørgrav 	}
4685f967b24SDag-Erling Smørgrav 
46967481196SJohn Birrell 	for (path = init_path; *path != '\0'; path = next) {
470e4082284SJohn Birrell 		while (*path == ':')
4715f967b24SDag-Erling Smørgrav 			path++;
47267481196SJohn Birrell 		if (*path == '\0')
4735f967b24SDag-Erling Smørgrav 			break;
474e4082284SJohn Birrell 		for (next = path; *next != '\0' && *next != ':'; next++)
4755f967b24SDag-Erling Smørgrav 			/* nothing */ ;
4765f967b24SDag-Erling Smørgrav 		if (bootverbose)
477ba41a07dSDmitrij Tejblum 			printf("start_init: trying %.*s\n", (int)(next - path),
478ba41a07dSDmitrij Tejblum 			    path);
4795f967b24SDag-Erling Smørgrav 
480df8bae1dSRodney W. Grimes 		/*
481df8bae1dSRodney W. Grimes 		 * Move out the boot flag argument.
482df8bae1dSRodney W. Grimes 		 */
483df8bae1dSRodney W. Grimes 		options = 0;
484df8bae1dSRodney W. Grimes 		ucp = (char *)USRSTACK;
485df8bae1dSRodney W. Grimes 		(void)subyte(--ucp, 0);		/* trailing zero */
486df8bae1dSRodney W. Grimes 		if (boothowto & RB_SINGLE) {
487df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, 's');
488df8bae1dSRodney W. Grimes 			options = 1;
489df8bae1dSRodney W. Grimes 		}
490df8bae1dSRodney W. Grimes #ifdef notyet
491df8bae1dSRodney W. Grimes                 if (boothowto & RB_FASTBOOT) {
492df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, 'f');
493df8bae1dSRodney W. Grimes 			options = 1;
494df8bae1dSRodney W. Grimes 		}
495df8bae1dSRodney W. Grimes #endif
49617755ac8SPoul-Henning Kamp 
49717755ac8SPoul-Henning Kamp #ifdef BOOTCDROM
49817755ac8SPoul-Henning Kamp 		(void)subyte(--ucp, 'C');
49917755ac8SPoul-Henning Kamp 		options = 1;
50017755ac8SPoul-Henning Kamp #endif
501df8bae1dSRodney W. Grimes 		if (options == 0)
502df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, '-');
503df8bae1dSRodney W. Grimes 		(void)subyte(--ucp, '-');		/* leading hyphen */
504df8bae1dSRodney W. Grimes 		arg1 = ucp;
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 		/*
507df8bae1dSRodney W. Grimes 		 * Move out the file name (also arg 0).
508df8bae1dSRodney W. Grimes 		 */
5095f967b24SDag-Erling Smørgrav 		(void)subyte(--ucp, 0);
5105f967b24SDag-Erling Smørgrav 		for (s = next - 1; s >= path; s--)
5115f967b24SDag-Erling Smørgrav 			(void)subyte(--ucp, *s);
512df8bae1dSRodney W. Grimes 		arg0 = ucp;
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes 		/*
515df8bae1dSRodney W. Grimes 		 * Move out the arg pointers.
516df8bae1dSRodney W. Grimes 		 */
517a20d7755SDoug Rabson 		uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1));
518c2da0fd9SBruce Evans 		(void)suword((caddr_t)--uap, (long)0);	/* terminator */
519c2da0fd9SBruce Evans 		(void)suword((caddr_t)--uap, (long)(intptr_t)arg1);
520c2da0fd9SBruce Evans 		(void)suword((caddr_t)--uap, (long)(intptr_t)arg0);
521df8bae1dSRodney W. Grimes 
522df8bae1dSRodney W. Grimes 		/*
523df8bae1dSRodney W. Grimes 		 * Point at the arguments.
524df8bae1dSRodney W. Grimes 		 */
525df8bae1dSRodney W. Grimes 		args.fname = arg0;
52626f9a767SRodney W. Grimes 		args.argv = uap;
52726f9a767SRodney W. Grimes 		args.envv = NULL;
528df8bae1dSRodney W. Grimes 
529df8bae1dSRodney W. Grimes 		/*
530df8bae1dSRodney W. Grimes 		 * Now try to exec the program.  If can't for any reason
531df8bae1dSRodney W. Grimes 		 * other than it doesn't exist, complain.
5322b14f991SJulian Elischer 		 *
53337b087a6SPeter Wemm 		 * Otherwise, return via fork_trampoline() all the way
5349c8b8baaSPeter Wemm 		 * to user mode as init!
535df8bae1dSRodney W. Grimes 		 */
53637b087a6SPeter Wemm 		if ((error = execve(p, &args)) == 0)
537df8bae1dSRodney W. Grimes 			return;
538df8bae1dSRodney W. Grimes 		if (error != ENOENT)
539ba41a07dSDmitrij Tejblum 			printf("exec %.*s: error %d\n", (int)(next - path),
540ba41a07dSDmitrij Tejblum 			    path, error);
541df8bae1dSRodney W. Grimes 	}
542580e7e5aSGreg Lehey 	printf("init: not found in path %s\n", init_path);
543df8bae1dSRodney W. Grimes 	panic("no init");
544df8bae1dSRodney W. Grimes }
54537b087a6SPeter Wemm 
54637b087a6SPeter Wemm /*
54737b087a6SPeter Wemm  * Like kthread_create(), but runs in it's own address space.
54837b087a6SPeter Wemm  * We do this early to reserve pid 1.
54937b087a6SPeter Wemm  *
55037b087a6SPeter Wemm  * Note special case - do not make it runnable yet.  Other work
55137b087a6SPeter Wemm  * in progress will change this more.
55237b087a6SPeter Wemm  */
55337b087a6SPeter Wemm static void
55437b087a6SPeter Wemm create_init(const void *udata __unused)
55537b087a6SPeter Wemm {
55637b087a6SPeter Wemm 	int error;
55737b087a6SPeter Wemm 	int s;
55837b087a6SPeter Wemm 
55937b087a6SPeter Wemm 	s = splhigh();
56037b087a6SPeter Wemm 	error = fork1(&proc0, RFFDG | RFPROC, &initproc);
56137b087a6SPeter Wemm 	if (error)
56237b087a6SPeter Wemm 		panic("cannot fork init: %d\n", error);
56337b087a6SPeter Wemm 	initproc->p_flag |= P_INMEM | P_SYSTEM;
56437b087a6SPeter Wemm 	cpu_set_fork_handler(initproc, start_init, NULL);
56537b087a6SPeter Wemm 	remrunqueue(initproc);
56637b087a6SPeter Wemm 	splx(s);
56737b087a6SPeter Wemm }
56837b087a6SPeter Wemm SYSINIT(init,SI_SUB_CREATE_INIT, SI_ORDER_FIRST, create_init, NULL)
56937b087a6SPeter Wemm 
57037b087a6SPeter Wemm /*
57137b087a6SPeter Wemm  * Make it runnable now.
57237b087a6SPeter Wemm  */
57337b087a6SPeter Wemm static void
57437b087a6SPeter Wemm kick_init(const void *udata __unused)
57537b087a6SPeter Wemm {
57637b087a6SPeter Wemm 	setrunqueue(initproc);
57737b087a6SPeter Wemm }
57837b087a6SPeter Wemm SYSINIT(kickinit,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kick_init, NULL)
579