xref: /freebsd/sys/kern/init_main.c (revision 9c8b8baa38c9a8135d7602f127cb0c735010837d)
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
429c8b8baaSPeter Wemm  * $Id: init_main.c,v 1.123 1999/06/30 15:33:32 peter Exp $
43df8bae1dSRodney W. Grimes  */
44df8bae1dSRodney W. Grimes 
4519060a3aSPoul-Henning Kamp #include "opt_devfs.h"
4667481196SJohn Birrell #include "opt_init_path.h"
474bd49128SPeter Wemm 
48df8bae1dSRodney W. Grimes #include <sys/param.h>
49b3c5c18dSBruce Evans #include <sys/file.h>
50df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
51df8bae1dSRodney W. Grimes #include <sys/kernel.h>
52ac0ad63fSBruce Evans #include <sys/mount.h>
53946bb7a2SPoul-Henning Kamp #include <sys/sysctl.h>
54df8bae1dSRodney W. Grimes #include <sys/proc.h>
559c8b8baaSPeter Wemm #include <sys/kthread.h>
56df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
57df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
58df8bae1dSRodney W. Grimes #include <sys/systm.h>
59df8bae1dSRodney W. Grimes #include <sys/vnode.h>
60f3f0ca60SSøren Schmidt #include <sys/sysent.h>
61df8bae1dSRodney W. Grimes #include <sys/reboot.h>
62ad7507e2SSteven Wallace #include <sys/sysproto.h>
63efeaf95aSDavid Greenman #include <sys/vmmeter.h>
6474b2192aSJohn Dyson #include <sys/unistd.h>
6594e9d7c1SPeter Wemm #include <sys/malloc.h>
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes #include <machine/cpu.h>
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes #include <vm/vm.h>
70efeaf95aSDavid Greenman #include <vm/vm_param.h>
71efeaf95aSDavid Greenman #include <vm/vm_prot.h>
72996c772fSJohn Dyson #include <sys/lock.h>
73efeaf95aSDavid Greenman #include <vm/pmap.h>
74efeaf95aSDavid Greenman #include <vm/vm_map.h>
75efeaf95aSDavid Greenman #include <sys/user.h>
766faaa756SWolfram Schneider #include <sys/copyright.h>
77df8bae1dSRodney W. Grimes 
789ef6c28aSBruce Evans extern struct linker_set	sysinit_set;	/* XXX */
799ef6c28aSBruce Evans 
8018c3dd17SPeter Wemm extern void mi_startup __P((void *framep));
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes /* Components of the first process -- never freed. */
83154c04e5SPoul-Henning Kamp static struct session session0;
84154c04e5SPoul-Henning Kamp static struct pgrp pgrp0;
85df8bae1dSRodney W. Grimes struct	proc proc0;
86154c04e5SPoul-Henning Kamp static struct pcred cred0;
876626c604SJulian Elischer static struct procsig procsig0;
88154c04e5SPoul-Henning Kamp static struct filedesc0 filedesc0;
89154c04e5SPoul-Henning Kamp static struct plimit limit0;
90154c04e5SPoul-Henning Kamp static struct vmspace vmspace0;
912b14f991SJulian Elischer struct	proc *initproc;
92df8bae1dSRodney W. Grimes 
93dedb7b62SPeter Wemm int cmask = CMASK;
94df8bae1dSRodney W. Grimes extern	struct user *proc0paddr;
95df8bae1dSRodney W. Grimes 
962976b7f1SDavid Greenman struct	vnode *rootvp;
97c463cf1cSBruce Evans int	boothowto = 0;		/* initialized so that it can be patched */
98946bb7a2SPoul-Henning Kamp 
99df8bae1dSRodney W. Grimes struct	timeval boottime;
1003d177f46SBill Fumerola SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
1013d177f46SBill Fumerola     &boottime, timeval, "System boottime");
102946bb7a2SPoul-Henning Kamp 
1032b14f991SJulian Elischer /*
1042b14f991SJulian Elischer  * Promiscuous argument pass for start_init()
1052b14f991SJulian Elischer  *
10618c3dd17SPeter Wemm  * This is a kludge because we use a return from mi_startup() rather than a call
10737b688c4SAlexander Langer  * to a new routine in locore.s to kick the kernel alive from locore.s.
1082b14f991SJulian Elischer  */
1092b14f991SJulian Elischer static void	*init_framep;
1102b14f991SJulian Elischer 
11126f9a767SRodney W. Grimes /*
1122b14f991SJulian Elischer  * This ensures that there is at least one entry so that the sysinit_set
1132b14f991SJulian Elischer  * symbol is not undefined.  A sybsystem ID of SI_SUB_DUMMY is never
1142b14f991SJulian Elischer  * executed.
11526f9a767SRodney W. Grimes  */
1162b14f991SJulian Elischer SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL)
1178a129caeSDavid Greenman 
11894e9d7c1SPeter Wemm /*
11994e9d7c1SPeter Wemm  * The sysinit table itself.  Items are checked off as the are run.
12094e9d7c1SPeter Wemm  * If we want to register new sysinit types, add them to newsysinit.
12194e9d7c1SPeter Wemm  */
12294e9d7c1SPeter Wemm struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items;
12394e9d7c1SPeter Wemm struct sysinit **newsysinit;
12494e9d7c1SPeter Wemm 
12594e9d7c1SPeter Wemm /*
12694e9d7c1SPeter Wemm  * Merge a new sysinit set into the current set, reallocating it if
12794e9d7c1SPeter Wemm  * necessary.  This can only be called after malloc is running.
12894e9d7c1SPeter Wemm  */
12994e9d7c1SPeter Wemm void
13094e9d7c1SPeter Wemm sysinit_add(set)
13194e9d7c1SPeter Wemm 	struct sysinit **set;
13294e9d7c1SPeter Wemm {
13394e9d7c1SPeter Wemm 	struct sysinit **newset;
13494e9d7c1SPeter Wemm 	struct sysinit **sipp;
13594e9d7c1SPeter Wemm 	struct sysinit **xipp;
13694e9d7c1SPeter Wemm 	int count = 0;
13794e9d7c1SPeter Wemm 
138ddd62546SPeter Wemm 	if (newsysinit)
139ddd62546SPeter Wemm 		for (sipp = newsysinit; *sipp; sipp++)
140ddd62546SPeter Wemm 			count++;
141ddd62546SPeter Wemm 	else
14294e9d7c1SPeter Wemm 		for (sipp = sysinit; *sipp; sipp++)
14394e9d7c1SPeter Wemm 			count++;
14494e9d7c1SPeter Wemm 	for (sipp = set; *sipp; sipp++)
14594e9d7c1SPeter Wemm 		count++;
146ddd62546SPeter Wemm 	count++;		/* Trailing NULL */
14794e9d7c1SPeter Wemm 	newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT);
14894e9d7c1SPeter Wemm 	if (newset == NULL)
14994e9d7c1SPeter Wemm 		panic("cannot malloc for sysinit");
15094e9d7c1SPeter Wemm 	xipp = newset;
151ddd62546SPeter Wemm 	if (newsysinit)
152ddd62546SPeter Wemm 		for (sipp = newsysinit; *sipp; sipp++)
153ddd62546SPeter Wemm 			*xipp++ = *sipp;
154ddd62546SPeter Wemm 	else
15594e9d7c1SPeter Wemm 		for (sipp = sysinit; *sipp; sipp++)
15694e9d7c1SPeter Wemm 			*xipp++ = *sipp;
15794e9d7c1SPeter Wemm 	for (sipp = set; *sipp; sipp++)
15894e9d7c1SPeter Wemm 		*xipp++ = *sipp;
159ddd62546SPeter Wemm 	*xipp = NULL;
160ddd62546SPeter Wemm 	if (newsysinit)
161ddd62546SPeter Wemm 		free(newsysinit, M_TEMP);
16294e9d7c1SPeter Wemm 	newsysinit = newset;
16394e9d7c1SPeter Wemm }
16426f9a767SRodney W. Grimes 
165df8bae1dSRodney W. Grimes /*
166df8bae1dSRodney W. Grimes  * System startup; initialize the world, create process 0, mount root
167df8bae1dSRodney W. Grimes  * filesystem, and fork to create init and pagedaemon.  Most of the
168df8bae1dSRodney W. Grimes  * hard work is done in the lower-level initialization routines including
169df8bae1dSRodney W. Grimes  * startup(), which does memory initialization and autoconfiguration.
1702b14f991SJulian Elischer  *
1712b14f991SJulian Elischer  * This allows simple addition of new kernel subsystems that require
1722b14f991SJulian Elischer  * boot time initialization.  It also allows substitution of subsystem
1732b14f991SJulian Elischer  * (for instance, a scheduler, kernel profiler, or VM system) by object
174c5b193bfSPoul-Henning Kamp  * module.  Finally, it allows for optional "kernel threads".
175df8bae1dSRodney W. Grimes  */
17626f9a767SRodney W. Grimes void
17718c3dd17SPeter Wemm mi_startup(framep)
178df8bae1dSRodney W. Grimes 	void *framep;
179df8bae1dSRodney W. Grimes {
1802b14f991SJulian Elischer 
1812b14f991SJulian Elischer 	register struct sysinit **sipp;		/* system initialization*/
1822b14f991SJulian Elischer 	register struct sysinit **xipp;		/* interior loop of sort*/
1832b14f991SJulian Elischer 	register struct sysinit *save;		/* bubble*/
1842b14f991SJulian Elischer 
1852b14f991SJulian Elischer 	/*
186a2a1c95cSPeter Wemm 	 * Copy the locore.s frame pointer for proc0, this is forked into
187a2a1c95cSPeter Wemm 	 * all other processes.
1882b14f991SJulian Elischer 	 */
1892b14f991SJulian Elischer 	init_framep = framep;
1902b14f991SJulian Elischer 
19194e9d7c1SPeter Wemm restart:
1922b14f991SJulian Elischer 	/*
1932b14f991SJulian Elischer 	 * Perform a bubble sort of the system initialization objects by
1942b14f991SJulian Elischer 	 * their subsystem (primary key) and order (secondary key).
1952b14f991SJulian Elischer 	 */
19694e9d7c1SPeter Wemm 	for (sipp = sysinit; *sipp; sipp++) {
1972b14f991SJulian Elischer 		for (xipp = sipp + 1; *xipp; xipp++) {
1982b14f991SJulian Elischer 			if ((*sipp)->subsystem < (*xipp)->subsystem ||
1992b14f991SJulian Elischer 			     ((*sipp)->subsystem == (*xipp)->subsystem &&
2002b14f991SJulian Elischer 			      (*sipp)->order < (*xipp)->order))
2012b14f991SJulian Elischer 				continue;	/* skip*/
2022b14f991SJulian Elischer 			save = *sipp;
2032b14f991SJulian Elischer 			*sipp = *xipp;
2042b14f991SJulian Elischer 			*xipp = save;
2052b14f991SJulian Elischer 		}
2062b14f991SJulian Elischer 	}
2072b14f991SJulian Elischer 
2082b14f991SJulian Elischer 	/*
2092b14f991SJulian Elischer 	 * Traverse the (now) ordered list of system initialization tasks.
2102b14f991SJulian Elischer 	 * Perform each task, and continue on to the next task.
2112b14f991SJulian Elischer 	 *
2122b14f991SJulian Elischer 	 * The last item on the list is expected to be the scheduler,
2132b14f991SJulian Elischer 	 * which will not return.
2142b14f991SJulian Elischer 	 */
21594e9d7c1SPeter Wemm 	for (sipp = sysinit; *sipp; sipp++) {
21674b2192aSJohn Dyson 
2172b14f991SJulian Elischer 		if ((*sipp)->subsystem == SI_SUB_DUMMY)
2182b14f991SJulian Elischer 			continue;	/* skip dummy task(s)*/
2192b14f991SJulian Elischer 
22094e9d7c1SPeter Wemm 		if ((*sipp)->subsystem == SI_SUB_DONE)
22194e9d7c1SPeter Wemm 			continue;
22294e9d7c1SPeter Wemm 
2239c8b8baaSPeter Wemm 		/* Call function */
2242b14f991SJulian Elischer 		(*((*sipp)->func))((*sipp)->udata);
22594e9d7c1SPeter Wemm 
22694e9d7c1SPeter Wemm 		/* Check off the one we're just done */
22794e9d7c1SPeter Wemm 		(*sipp)->subsystem = SI_SUB_DONE;
22894e9d7c1SPeter Wemm 
22994e9d7c1SPeter Wemm 		/* Check if we've installed more sysinit items via KLD */
23094e9d7c1SPeter Wemm 		if (newsysinit != NULL) {
23194e9d7c1SPeter Wemm 			if (sysinit != (struct sysinit **)sysinit_set.ls_items)
23294e9d7c1SPeter Wemm 				free(sysinit, M_TEMP);
23394e9d7c1SPeter Wemm 			sysinit = newsysinit;
23494e9d7c1SPeter Wemm 			newsysinit = NULL;
23594e9d7c1SPeter Wemm 			goto restart;
23694e9d7c1SPeter Wemm 		}
2372b14f991SJulian Elischer 	}
2382b14f991SJulian Elischer 
239477a642cSPeter Wemm 	panic("Shouldn't get here!");
2402b14f991SJulian Elischer 	/* NOTREACHED*/
2412b14f991SJulian Elischer }
2422b14f991SJulian Elischer 
2432b14f991SJulian Elischer 
2442b14f991SJulian Elischer /*
2452b14f991SJulian Elischer  ***************************************************************************
2462b14f991SJulian Elischer  ****
2472b14f991SJulian Elischer  **** The following SYSINIT's belong elsewhere, but have not yet
2482b14f991SJulian Elischer  **** been moved.
2492b14f991SJulian Elischer  ****
2502b14f991SJulian Elischer  ***************************************************************************
2512b14f991SJulian Elischer  */
2522b14f991SJulian Elischer #ifdef OMIT
2532b14f991SJulian Elischer /*
2542b14f991SJulian Elischer  * Handled by vfs_mountroot (bad idea) at this time... should be
2552b14f991SJulian Elischer  * done the same as 4.4Lite2.
2562b14f991SJulian Elischer  */
2572b14f991SJulian Elischer SYSINIT(swapinit, SI_SUB_SWAP, SI_ORDER_FIRST, swapinit, NULL)
2582b14f991SJulian Elischer #endif	/* OMIT*/
2592b14f991SJulian Elischer 
2604590fd3aSDavid Greenman static void print_caddr_t __P((void *data));
2619ef6c28aSBruce Evans static void
2629ef6c28aSBruce Evans print_caddr_t(data)
2634590fd3aSDavid Greenman 	void *data;
2649ef6c28aSBruce Evans {
2659ef6c28aSBruce Evans 	printf("%s", (char *)data);
2669ef6c28aSBruce Evans }
267d841aaa7SBruce Evans SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright)
2682b14f991SJulian Elischer 
2692b14f991SJulian Elischer 
2702b14f991SJulian Elischer /*
2712b14f991SJulian Elischer  ***************************************************************************
2722b14f991SJulian Elischer  ****
2732b14f991SJulian Elischer  **** The two following SYSINT's are proc0 specific glue code.  I am not
2742b14f991SJulian Elischer  **** convinced that they can not be safely combined, but their order of
2752b14f991SJulian Elischer  **** operation has been maintained as the same as the original init_main.c
2762b14f991SJulian Elischer  **** for right now.
2772b14f991SJulian Elischer  ****
2782b14f991SJulian Elischer  **** These probably belong in init_proc.c or kern_proc.c, since they
2792b14f991SJulian Elischer  **** deal with proc0 (the fork template process).
2802b14f991SJulian Elischer  ****
2812b14f991SJulian Elischer  ***************************************************************************
2822b14f991SJulian Elischer  */
2832b14f991SJulian Elischer /* ARGSUSED*/
284154c04e5SPoul-Henning Kamp static void proc0_init __P((void *dummy));
285154c04e5SPoul-Henning Kamp static void
286d841aaa7SBruce Evans proc0_init(dummy)
287d841aaa7SBruce Evans 	void *dummy;
2882b14f991SJulian Elischer {
289df8bae1dSRodney W. Grimes 	register struct proc		*p;
290df8bae1dSRodney W. Grimes 	register struct filedesc0	*fdp;
29192579404SAlexander Langer 	register unsigned i;
292df8bae1dSRodney W. Grimes 
293df8bae1dSRodney W. Grimes 	p = &proc0;
294df8bae1dSRodney W. Grimes 
295df8bae1dSRodney W. Grimes 	/*
296a3bfb996SJeffrey Hsu 	 * Initialize process and pgrp structures.
297a3bfb996SJeffrey Hsu 	 */
298a3bfb996SJeffrey Hsu 	procinit();
299a3bfb996SJeffrey Hsu 
300a3bfb996SJeffrey Hsu 	/*
301b1508c72SDavid Greenman 	 * Initialize sleep queue hash table
302b1508c72SDavid Greenman 	 */
303b1508c72SDavid Greenman 	sleepinit();
304b1508c72SDavid Greenman 
305b1508c72SDavid Greenman 	/*
3063075778bSJohn Dyson 	 * additional VM structures
3073075778bSJohn Dyson 	 */
3083075778bSJohn Dyson 	vm_init2();
3093075778bSJohn Dyson 
3103075778bSJohn Dyson 	/*
311df8bae1dSRodney W. Grimes 	 * Create process 0 (the swapper).
312df8bae1dSRodney W. Grimes 	 */
313a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&allproc, p, p_list);
314df8bae1dSRodney W. Grimes 	p->p_pgrp = &pgrp0;
315a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
316a3bfb996SJeffrey Hsu 	LIST_INIT(&pgrp0.pg_members);
317a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
318a3bfb996SJeffrey Hsu 
319df8bae1dSRodney W. Grimes 	pgrp0.pg_session = &session0;
320df8bae1dSRodney W. Grimes 	session0.s_count = 1;
321df8bae1dSRodney W. Grimes 	session0.s_leader = p;
322df8bae1dSRodney W. Grimes 
323f3f0ca60SSøren Schmidt 	p->p_sysent = &aout_sysvec;
324f3f0ca60SSøren Schmidt 
325df8bae1dSRodney W. Grimes 	p->p_flag = P_INMEM | P_SYSTEM;
326df8bae1dSRodney W. Grimes 	p->p_stat = SRUN;
327df8bae1dSRodney W. Grimes 	p->p_nice = NZERO;
3287216391eSDavid Greenman 	p->p_rtprio.type = RTP_PRIO_NORMAL;
3297216391eSDavid Greenman 	p->p_rtprio.prio = 0;
330f992f480SDavid Greenman 
3312c1011f7SJohn Dyson /*
3322c1011f7SJohn Dyson  * Link for kernel based threads
3332c1011f7SJohn Dyson  */
3342c1011f7SJohn Dyson 	p->p_peers = 0;
3352c1011f7SJohn Dyson 	p->p_leader = p;
3362c1011f7SJohn Dyson 
337df8bae1dSRodney W. Grimes 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
338df8bae1dSRodney W. Grimes 
339df8bae1dSRodney W. Grimes 	/* Create credentials. */
340df8bae1dSRodney W. Grimes 	cred0.p_refcnt = 1;
341df8bae1dSRodney W. Grimes 	p->p_cred = &cred0;
342df8bae1dSRodney W. Grimes 	p->p_ucred = crget();
343df8bae1dSRodney W. Grimes 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
344df8bae1dSRodney W. Grimes 
34575c13541SPoul-Henning Kamp 	/* Don't jail it */
34675c13541SPoul-Henning Kamp 	p->p_prison = 0;
34775c13541SPoul-Henning Kamp 
3486626c604SJulian Elischer 	/* Create procsig. */
3496626c604SJulian Elischer 	p->p_procsig = &procsig0;
35075ffaf59SLuoqi Chen 	p->p_procsig->ps_refcnt = 1;
3516626c604SJulian Elischer 
352df8bae1dSRodney W. Grimes 	/* Create the file descriptor table. */
353df8bae1dSRodney W. Grimes 	fdp = &filedesc0;
354df8bae1dSRodney W. Grimes 	p->p_fd = &fdp->fd_fd;
355df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_refcnt = 1;
356df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_cmask = cmask;
357df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
358df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
359df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_nfiles = NDFILE;
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes 	/* Create the limits structures. */
362df8bae1dSRodney W. Grimes 	p->p_limit = &limit0;
363df8bae1dSRodney W. Grimes 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
364df8bae1dSRodney W. Grimes 		limit0.pl_rlimit[i].rlim_cur =
365df8bae1dSRodney W. Grimes 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
366b3c5c18dSBruce Evans 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
367b3c5c18dSBruce Evans 	    limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
368b3c5c18dSBruce Evans 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
369b3c5c18dSBruce Evans 	    limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
370df8bae1dSRodney W. Grimes 	i = ptoa(cnt.v_free_count);
371df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
372df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
373df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
374e796e00dSPoul-Henning Kamp 	limit0.p_cpulimit = RLIM_INFINITY;
375df8bae1dSRodney W. Grimes 	limit0.p_refcnt = 1;
376df8bae1dSRodney W. Grimes 
377e796e00dSPoul-Henning Kamp 
378df8bae1dSRodney W. Grimes 	/* Allocate a prototype map so we have something to fork. */
379b1028ad1SLuoqi Chen 	pmap_pinit0(vmspace_pmap(&vmspace0));
380df8bae1dSRodney W. Grimes 	p->p_vmspace = &vmspace0;
381df8bae1dSRodney W. Grimes 	vmspace0.vm_refcnt = 1;
38226f9a767SRodney W. Grimes 	vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
3832d8acc0fSJohn Dyson 	    trunc_page(VM_MAXUSER_ADDRESS));
384b1028ad1SLuoqi Chen 	vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0);
385df8bae1dSRodney W. Grimes 	p->p_addr = proc0paddr;				/* XXX */
386df8bae1dSRodney W. Grimes 
3877067d561SPeter Wemm #ifdef cpu_set_init_frame
388df8bae1dSRodney W. Grimes 	/*
389dc733423SDag-Erling Smørgrav 	 * proc0 needs to have a coherent frame base in its stack.
3902384fde5SDavid Greenman 	 */
3912b14f991SJulian Elischer 	cpu_set_init_frame(p, init_framep);			/* XXX! */
392ecbb00a2SDoug Rabson #endif
3932384fde5SDavid Greenman 
39439fb8e6bSJulian Elischer 	/*
395df8bae1dSRodney W. Grimes 	 * We continue to place resource usage info and signal
396df8bae1dSRodney W. Grimes 	 * actions in the user struct so they're pageable.
397df8bae1dSRodney W. Grimes 	 */
398df8bae1dSRodney W. Grimes 	p->p_stats = &p->p_addr->u_stats;
399df8bae1dSRodney W. Grimes 	p->p_sigacts = &p->p_addr->u_sigacts;
400df8bae1dSRodney W. Grimes 
401df8bae1dSRodney W. Grimes 	/*
402a3bfb996SJeffrey Hsu 	 * Charge root for one process.
403df8bae1dSRodney W. Grimes 	 */
404df8bae1dSRodney W. Grimes 	(void)chgproccnt(0, 1);
4052a024a2bSSean Eric Fagan 
4062a024a2bSSean Eric Fagan 	/*
407188554bbSDmitrij Tejblum 	 * Initialize the current process pointer (curproc) before
408188554bbSDmitrij Tejblum 	 * any possible traps/probes to simplify trap processing.
4092a024a2bSSean Eric Fagan 	 */
410188554bbSDmitrij Tejblum 	SET_CURPROC(p);
4112a024a2bSSean Eric Fagan 
41226f9a767SRodney W. Grimes }
4132b14f991SJulian Elischer SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL)
4142b14f991SJulian Elischer 
4152b14f991SJulian Elischer /* ARGSUSED*/
416154c04e5SPoul-Henning Kamp static void proc0_post __P((void *dummy));
417154c04e5SPoul-Henning Kamp static void
418d841aaa7SBruce Evans proc0_post(dummy)
419d841aaa7SBruce Evans 	void *dummy;
4202b14f991SJulian Elischer {
42100af9731SPoul-Henning Kamp 	struct timespec ts;
422a6fc8288SPeter Wemm 
4232b14f991SJulian Elischer 	/*
4241b0b259eSBruce Evans 	 * Now we can look at the time, having had a chance to verify the
4251b0b259eSBruce Evans 	 * time from the file system.  Pretend that proc0 started now.
4262b14f991SJulian Elischer 	 */
4275f88ec36SPoul-Henning Kamp 	microtime(&proc0.p_stats->p_start);
428e796e00dSPoul-Henning Kamp 	proc0.p_runtime = 0;
429e7ba67f2SBruce Evans 	microuptime(&switchtime);
4301b0b259eSBruce Evans 	switchticks = ticks;
4312b14f991SJulian Elischer 
432a6fc8288SPeter Wemm 	/*
433a6fc8288SPeter Wemm 	 * Give the ``random'' number generator a thump.
4345f88ec36SPoul-Henning Kamp 	 * XXX: Does read_random() contain enough bits to be used here ?
435a6fc8288SPeter Wemm 	 */
43600af9731SPoul-Henning Kamp 	nanotime(&ts);
43700af9731SPoul-Henning Kamp 	srandom(ts.tv_sec ^ ts.tv_nsec);
438a6fc8288SPeter Wemm 
4392b14f991SJulian Elischer 	/* Initialize signal state for process 0. */
4402b14f991SJulian Elischer 	siginit(&proc0);
4412b14f991SJulian Elischer }
4422b14f991SJulian Elischer SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL)
4432b14f991SJulian Elischer 
4442b14f991SJulian Elischer 
4452b14f991SJulian Elischer 
446df8bae1dSRodney W. Grimes 
447df8bae1dSRodney W. Grimes /*
4482b14f991SJulian Elischer  ***************************************************************************
4492b14f991SJulian Elischer  ****
4502b14f991SJulian Elischer  **** The following SYSINIT's and glue code should be moved to the
4512b14f991SJulian Elischer  **** respective files on a per subsystem basis.
4522b14f991SJulian Elischer  ****
4532b14f991SJulian Elischer  ***************************************************************************
454df8bae1dSRodney W. Grimes  */
455df8bae1dSRodney W. Grimes 
4562b14f991SJulian Elischer /* ARGSUSED */
457ab36c067SJustin T. Gibbs static void root_conf __P((void *dummy));
458ab36c067SJustin T. Gibbs static void
459ab36c067SJustin T. Gibbs root_conf(dummy)
460ab36c067SJustin T. Gibbs 	void *dummy;
461ab36c067SJustin T. Gibbs {
462ab36c067SJustin T. Gibbs 	cpu_rootconf();
463ab36c067SJustin T. Gibbs }
464ab36c067SJustin T. Gibbs SYSINIT(root_conf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, root_conf, NULL)
465ab36c067SJustin T. Gibbs 
466ab36c067SJustin T. Gibbs /* ARGSUSED*/
467154c04e5SPoul-Henning Kamp static void xxx_vfs_root_fdtab __P((void *dummy));
468154c04e5SPoul-Henning Kamp static void
469d841aaa7SBruce Evans xxx_vfs_root_fdtab(dummy)
470d841aaa7SBruce Evans 	void *dummy;
4712b14f991SJulian Elischer {
4722b14f991SJulian Elischer 	register struct filedesc0	*fdp = &filedesc0;
473df8bae1dSRodney W. Grimes 
474df8bae1dSRodney W. Grimes 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
475628641f8SDavid Greenman 	if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
476df8bae1dSRodney W. Grimes 		panic("cannot find root vnode");
477df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_cdir = rootvnode;
478df8bae1dSRodney W. Grimes 	VREF(fdp->fd_fd.fd_cdir);
479996c772fSJohn Dyson 	VOP_UNLOCK(rootvnode, 0, &proc0);
4809f24f214SJohn Dyson 	fdp->fd_fd.fd_rdir = rootvnode;
4812b14f991SJulian Elischer }
4822b14f991SJulian Elischer SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL)
4832b14f991SJulian Elischer 
484df8bae1dSRodney W. Grimes 
485df8bae1dSRodney W. Grimes /*
4862b14f991SJulian Elischer  ***************************************************************************
4872b14f991SJulian Elischer  ****
4882b14f991SJulian Elischer  **** The following code probably belongs in another file, like
4892b14f991SJulian Elischer  **** kern/init_init.c.  It is here for two reasons only:
4902b14f991SJulian Elischer  ****
4912b14f991SJulian Elischer  ****	1)	This code returns to startup the system; this is
4922b14f991SJulian Elischer  ****		abnormal for a kernel thread.
4932b14f991SJulian Elischer  ****	2)	This code promiscuously uses init_frame
4942b14f991SJulian Elischer  ****
4952b14f991SJulian Elischer  ***************************************************************************
496df8bae1dSRodney W. Grimes  */
497df8bae1dSRodney W. Grimes 
498a2a1c95cSPeter Wemm extern void prepare_usermode __P((void));
4999c8b8baaSPeter Wemm static void create_init __P((const void *dummy));
5009c8b8baaSPeter Wemm static void start_init __P((void *dummy));
5019c8b8baaSPeter Wemm SYSINIT(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, create_init, NULL)
5022b14f991SJulian Elischer 
5032b14f991SJulian Elischer /*
5049c8b8baaSPeter Wemm  * Like kthread_create(), but runs in it's own address space.
5052b14f991SJulian Elischer  */
5069c8b8baaSPeter Wemm static void
5079c8b8baaSPeter Wemm create_init(udata)
5089c8b8baaSPeter Wemm 	const void *udata;
5099c8b8baaSPeter Wemm {
5109c8b8baaSPeter Wemm 	int error;
511df8bae1dSRodney W. Grimes 
5129c8b8baaSPeter Wemm 	error = fork1(&proc0, RFFDG | RFPROC, &initproc);
5139c8b8baaSPeter Wemm 	if (error)
5149c8b8baaSPeter Wemm 		panic("cannot fork init: %d\n", error);
5159c8b8baaSPeter Wemm 	initproc->p_flag |= P_INMEM | P_SYSTEM;
5169c8b8baaSPeter Wemm 	cpu_set_fork_handler(initproc, start_init, NULL);
5179c8b8baaSPeter Wemm }
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes /*
520df8bae1dSRodney W. Grimes  * List of paths to try when searching for "init".
521df8bae1dSRodney W. Grimes  */
5225f967b24SDag-Erling Smørgrav static char init_path[MAXPATHLEN] =
52367481196SJohn Birrell #ifdef	INIT_PATH
52467481196SJohn Birrell     __XSTRING(INIT_PATH);
52567481196SJohn Birrell #else
526e4082284SJohn Birrell     "/sbin/init:/sbin/oinit:/sbin/init.bak:/stand/sysinstall";
52767481196SJohn Birrell #endif
5285f967b24SDag-Erling Smørgrav SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, "");
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes /*
5315f967b24SDag-Erling Smørgrav  * Start the initial user process; try exec'ing each pathname in init_path.
532df8bae1dSRodney W. Grimes  * The program is invoked with one argument containing the boot flags.
533df8bae1dSRodney W. Grimes  */
534df8bae1dSRodney W. Grimes static void
5359c8b8baaSPeter Wemm start_init(dummy)
5369c8b8baaSPeter Wemm 	void *dummy;
537df8bae1dSRodney W. Grimes {
538df8bae1dSRodney W. Grimes 	vm_offset_t addr;
539df8bae1dSRodney W. Grimes 	struct execve_args args;
5405f967b24SDag-Erling Smørgrav 	int options, error;
5415f967b24SDag-Erling Smørgrav 	char *var, *path, *next, *s;
5425f967b24SDag-Erling Smørgrav 	char *ucp, **uap, *arg0, *arg1;
5439c8b8baaSPeter Wemm 	struct proc *p;
544df8bae1dSRodney W. Grimes 
5459c8b8baaSPeter Wemm 	p = curproc;
546df8bae1dSRodney W. Grimes 
547df8bae1dSRodney W. Grimes 	/*
548df8bae1dSRodney W. Grimes 	 * Need just enough stack to hold the faked-up "execve()" arguments.
549df8bae1dSRodney W. Grimes 	 */
5509c0fed3dSDoug Rabson 	addr = trunc_page(USRSTACK - PAGE_SIZE);
5515f967b24SDag-Erling Smørgrav 	if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE,
5525f967b24SDag-Erling Smørgrav 			FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
553df8bae1dSRodney W. Grimes 		panic("init: couldn't allocate argument space");
554df8bae1dSRodney W. Grimes 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
55526f9a767SRodney W. Grimes 	p->p_vmspace->vm_ssize = 1;
556df8bae1dSRodney W. Grimes 
5575f967b24SDag-Erling Smørgrav 	if ((var = getenv("init_path")) != NULL) {
558b83308b0SDag-Erling Smørgrav 		strncpy(init_path, var, sizeof init_path);
5595f967b24SDag-Erling Smørgrav 		init_path[sizeof init_path - 1] = 0;
5605f967b24SDag-Erling Smørgrav 	}
5615f967b24SDag-Erling Smørgrav 
56267481196SJohn Birrell 	for (path = init_path; *path != '\0'; path = next) {
563e4082284SJohn Birrell 		while (*path == ':')
5645f967b24SDag-Erling Smørgrav 			path++;
56567481196SJohn Birrell 		if (*path == '\0')
5665f967b24SDag-Erling Smørgrav 			break;
567e4082284SJohn Birrell 		for (next = path; *next != '\0' && *next != ':'; next++)
5685f967b24SDag-Erling Smørgrav 			/* nothing */ ;
5695f967b24SDag-Erling Smørgrav 		if (bootverbose)
570ba41a07dSDmitrij Tejblum 			printf("start_init: trying %.*s\n", (int)(next - path),
571ba41a07dSDmitrij Tejblum 			    path);
5725f967b24SDag-Erling Smørgrav 
573df8bae1dSRodney W. Grimes 		/*
574df8bae1dSRodney W. Grimes 		 * Move out the boot flag argument.
575df8bae1dSRodney W. Grimes 		 */
576df8bae1dSRodney W. Grimes 		options = 0;
577df8bae1dSRodney W. Grimes 		ucp = (char *)USRSTACK;
578df8bae1dSRodney W. Grimes 		(void)subyte(--ucp, 0);		/* trailing zero */
579df8bae1dSRodney W. Grimes 		if (boothowto & RB_SINGLE) {
580df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, 's');
581df8bae1dSRodney W. Grimes 			options = 1;
582df8bae1dSRodney W. Grimes 		}
583df8bae1dSRodney W. Grimes #ifdef notyet
584df8bae1dSRodney W. Grimes                 if (boothowto & RB_FASTBOOT) {
585df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, 'f');
586df8bae1dSRodney W. Grimes 			options = 1;
587df8bae1dSRodney W. Grimes 		}
588df8bae1dSRodney W. Grimes #endif
58917755ac8SPoul-Henning Kamp 
59017755ac8SPoul-Henning Kamp #ifdef BOOTCDROM
59117755ac8SPoul-Henning Kamp 		(void)subyte(--ucp, 'C');
59217755ac8SPoul-Henning Kamp 		options = 1;
59317755ac8SPoul-Henning Kamp #endif
594df8bae1dSRodney W. Grimes 		if (options == 0)
595df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, '-');
596df8bae1dSRodney W. Grimes 		(void)subyte(--ucp, '-');		/* leading hyphen */
597df8bae1dSRodney W. Grimes 		arg1 = ucp;
598df8bae1dSRodney W. Grimes 
599df8bae1dSRodney W. Grimes 		/*
600df8bae1dSRodney W. Grimes 		 * Move out the file name (also arg 0).
601df8bae1dSRodney W. Grimes 		 */
6025f967b24SDag-Erling Smørgrav 		(void)subyte(--ucp, 0);
6035f967b24SDag-Erling Smørgrav 		for (s = next - 1; s >= path; s--)
6045f967b24SDag-Erling Smørgrav 			(void)subyte(--ucp, *s);
605df8bae1dSRodney W. Grimes 		arg0 = ucp;
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 		/*
608df8bae1dSRodney W. Grimes 		 * Move out the arg pointers.
609df8bae1dSRodney W. Grimes 		 */
610a20d7755SDoug Rabson 		uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1));
611c2da0fd9SBruce Evans 		(void)suword((caddr_t)--uap, (long)0);	/* terminator */
612c2da0fd9SBruce Evans 		(void)suword((caddr_t)--uap, (long)(intptr_t)arg1);
613c2da0fd9SBruce Evans 		(void)suword((caddr_t)--uap, (long)(intptr_t)arg0);
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 		/*
616df8bae1dSRodney W. Grimes 		 * Point at the arguments.
617df8bae1dSRodney W. Grimes 		 */
618df8bae1dSRodney W. Grimes 		args.fname = arg0;
61926f9a767SRodney W. Grimes 		args.argv = uap;
62026f9a767SRodney W. Grimes 		args.envv = NULL;
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes 		/*
623df8bae1dSRodney W. Grimes 		 * Now try to exec the program.  If can't for any reason
624df8bae1dSRodney W. Grimes 		 * other than it doesn't exist, complain.
6252b14f991SJulian Elischer 		 *
6269c8b8baaSPeter Wemm 		 * Otherwise, return via the fork trampoline all the way
6279c8b8baaSPeter Wemm 		 * to user mode as init!
628df8bae1dSRodney W. Grimes 		 */
6299c8b8baaSPeter Wemm 		if ((error = execve(p, &args)) == 0) {
6309c8b8baaSPeter Wemm 			prepare_usermode();
631df8bae1dSRodney W. Grimes 			return;
6329c8b8baaSPeter Wemm 		}
633df8bae1dSRodney W. Grimes 		if (error != ENOENT)
634ba41a07dSDmitrij Tejblum 			printf("exec %.*s: error %d\n", (int)(next - path),
635ba41a07dSDmitrij Tejblum 			    path, error);
636df8bae1dSRodney W. Grimes 	}
637df8bae1dSRodney W. Grimes 	printf("init: not found\n");
638df8bae1dSRodney W. Grimes 	panic("no init");
639df8bae1dSRodney W. Grimes }
640