xref: /freebsd/sys/kern/init_main.c (revision 2c1011f7ef96332fae2af03f06aacf800af2dd2b)
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
422c1011f7SJohn Dyson  * $Id: init_main.c,v 1.63 1997/05/29 04:52:03 peter Exp $
43df8bae1dSRodney W. Grimes  */
44df8bae1dSRodney W. Grimes 
454bd49128SPeter Wemm #include "opt_rlimit.h"
4619060a3aSPoul-Henning Kamp #include "opt_devfs.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>
55df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
56df8bae1dSRodney W. Grimes #include <sys/signalvar.h>
57df8bae1dSRodney W. Grimes #include <sys/systm.h>
58df8bae1dSRodney W. Grimes #include <sys/vnode.h>
59f3f0ca60SSøren Schmidt #include <sys/sysent.h>
60df8bae1dSRodney W. Grimes #include <sys/reboot.h>
61ad7507e2SSteven Wallace #include <sys/sysproto.h>
62efeaf95aSDavid Greenman #include <sys/vmmeter.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <machine/cpu.h>
65477a642cSPeter Wemm #if defined(SMP)
66477a642cSPeter Wemm #include <machine/smp.h>
67477a642cSPeter Wemm #endif /* SMP */
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 
809ef6c28aSBruce Evans extern void __main __P((void));
819ef6c28aSBruce Evans extern void main __P((void *framep));
82477a642cSPeter Wemm extern void secondary_main __P((void));
83df8bae1dSRodney W. Grimes 
84df8bae1dSRodney W. Grimes /* Components of the first process -- never freed. */
85154c04e5SPoul-Henning Kamp static struct session session0;
86154c04e5SPoul-Henning Kamp static struct pgrp pgrp0;
87df8bae1dSRodney W. Grimes struct	proc proc0;
88154c04e5SPoul-Henning Kamp static struct pcred cred0;
89154c04e5SPoul-Henning Kamp static struct filedesc0 filedesc0;
90154c04e5SPoul-Henning Kamp static struct plimit limit0;
91154c04e5SPoul-Henning Kamp static struct vmspace vmspace0;
92477a642cSPeter Wemm #ifndef SMP	/* per-cpu on smp */
93df8bae1dSRodney W. Grimes struct	proc *curproc = &proc0;
94477a642cSPeter Wemm #endif
952b14f991SJulian Elischer struct	proc *initproc;
96df8bae1dSRodney W. Grimes 
97dedb7b62SPeter Wemm int cmask = CMASK;
98df8bae1dSRodney W. Grimes extern	struct user *proc0paddr;
99df8bae1dSRodney W. Grimes 
1002976b7f1SDavid Greenman struct	vnode *rootvp;
101df8bae1dSRodney W. Grimes int	boothowto;
102946bb7a2SPoul-Henning Kamp 
103df8bae1dSRodney W. Grimes struct	timeval boottime;
104946bb7a2SPoul-Henning Kamp SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime,
105946bb7a2SPoul-Henning Kamp 	CTLFLAG_RW, &boottime, timeval, "");
106946bb7a2SPoul-Henning Kamp 
107477a642cSPeter Wemm /*
108477a642cSPeter Wemm  * for SMP, the runtime variable has to be per-cpu, so we use the
109477a642cSPeter Wemm  * extern declaration in sys/kernel.h
110477a642cSPeter Wemm  */
111477a642cSPeter Wemm #ifndef SMP
112df8bae1dSRodney W. Grimes struct	timeval runtime;
113477a642cSPeter Wemm #endif
114df8bae1dSRodney W. Grimes 
1152b14f991SJulian Elischer /*
1162b14f991SJulian Elischer  * Promiscuous argument pass for start_init()
1172b14f991SJulian Elischer  *
1182b14f991SJulian Elischer  * This is a kludge because we use a return from main() rather than a call
11937b688c4SAlexander Langer  * to a new routine in locore.s to kick the kernel alive from locore.s.
1202b14f991SJulian Elischer  */
1212b14f991SJulian Elischer static void	*init_framep;
1222b14f991SJulian Elischer 
123df8bae1dSRodney W. Grimes 
12426f9a767SRodney W. Grimes #if __GNUC__ >= 2
12526f9a767SRodney W. Grimes void __main() {}
12626f9a767SRodney W. Grimes #endif
12726f9a767SRodney W. Grimes 
1282b14f991SJulian Elischer 
12926f9a767SRodney W. Grimes /*
1302b14f991SJulian Elischer  * This ensures that there is at least one entry so that the sysinit_set
1312b14f991SJulian Elischer  * symbol is not undefined.  A sybsystem ID of SI_SUB_DUMMY is never
1322b14f991SJulian Elischer  * executed.
13326f9a767SRodney W. Grimes  */
1342b14f991SJulian Elischer SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL)
1358a129caeSDavid Greenman 
13626f9a767SRodney W. Grimes 
137df8bae1dSRodney W. Grimes /*
138df8bae1dSRodney W. Grimes  * System startup; initialize the world, create process 0, mount root
139df8bae1dSRodney W. Grimes  * filesystem, and fork to create init and pagedaemon.  Most of the
140df8bae1dSRodney W. Grimes  * hard work is done in the lower-level initialization routines including
141df8bae1dSRodney W. Grimes  * startup(), which does memory initialization and autoconfiguration.
1422b14f991SJulian Elischer  *
1432b14f991SJulian Elischer  * This allows simple addition of new kernel subsystems that require
1442b14f991SJulian Elischer  * boot time initialization.  It also allows substitution of subsystem
1452b14f991SJulian Elischer  * (for instance, a scheduler, kernel profiler, or VM system) by object
1462b14f991SJulian Elischer  * module.  Finally, it allows for optional "kernel threads", like an LFS
1472b14f991SJulian Elischer  * cleaner.
148df8bae1dSRodney W. Grimes  */
14926f9a767SRodney W. Grimes void
150df8bae1dSRodney W. Grimes main(framep)
151df8bae1dSRodney W. Grimes 	void *framep;
152df8bae1dSRodney W. Grimes {
1532b14f991SJulian Elischer 
1542b14f991SJulian Elischer 	register struct sysinit **sipp;		/* system initialization*/
1552b14f991SJulian Elischer 	register struct sysinit **xipp;		/* interior loop of sort*/
1562b14f991SJulian Elischer 	register struct sysinit *save;		/* bubble*/
1572b14f991SJulian Elischer 	int			rval[2];	/* SI_TYPE_KTHREAD support*/
1582b14f991SJulian Elischer 
1592b14f991SJulian Elischer 	/*
160a2a1c95cSPeter Wemm 	 * Copy the locore.s frame pointer for proc0, this is forked into
161a2a1c95cSPeter Wemm 	 * all other processes.
1622b14f991SJulian Elischer 	 */
1632b14f991SJulian Elischer 	init_framep = framep;
1642b14f991SJulian Elischer 
1652b14f991SJulian Elischer 	/*
1662b14f991SJulian Elischer 	 * Perform a bubble sort of the system initialization objects by
1672b14f991SJulian Elischer 	 * their subsystem (primary key) and order (secondary key).
1682b14f991SJulian Elischer 	 *
1692b14f991SJulian Elischer 	 * Since some things care about execution order, this is the
1702b14f991SJulian Elischer 	 * operation which ensures continued function.
1712b14f991SJulian Elischer 	 */
1722b14f991SJulian Elischer 	for( sipp = (struct sysinit **)sysinit_set.ls_items; *sipp; sipp++) {
1732b14f991SJulian Elischer 		for( xipp = sipp + 1; *xipp; xipp++) {
1742b14f991SJulian Elischer 			if( (*sipp)->subsystem < (*xipp)->subsystem ||
1752b14f991SJulian Elischer 			    ( (*sipp)->subsystem == (*xipp)->subsystem &&
1762b14f991SJulian Elischer 			      (*sipp)->order < (*xipp)->order))
1772b14f991SJulian Elischer 				continue;	/* skip*/
1782b14f991SJulian Elischer 			save = *sipp;
1792b14f991SJulian Elischer 			*sipp = *xipp;
1802b14f991SJulian Elischer 			*xipp = save;
1812b14f991SJulian Elischer 		}
1822b14f991SJulian Elischer 	}
1832b14f991SJulian Elischer 
1842b14f991SJulian Elischer 	/*
1852b14f991SJulian Elischer 	 * Traverse the (now) ordered list of system initialization tasks.
1862b14f991SJulian Elischer 	 * Perform each task, and continue on to the next task.
1872b14f991SJulian Elischer 	 *
1882b14f991SJulian Elischer 	 * The last item on the list is expected to be the scheduler,
1892b14f991SJulian Elischer 	 * which will not return.
1902b14f991SJulian Elischer 	 */
1912b14f991SJulian Elischer 	for( sipp = (struct sysinit **)sysinit_set.ls_items; *sipp; sipp++) {
1922b14f991SJulian Elischer 		if( (*sipp)->subsystem == SI_SUB_DUMMY)
1932b14f991SJulian Elischer 			continue;	/* skip dummy task(s)*/
1942b14f991SJulian Elischer 
1952b14f991SJulian Elischer 		switch( (*sipp)->type) {
1962b14f991SJulian Elischer 		case SI_TYPE_DEFAULT:
1972b14f991SJulian Elischer 			/* no special processing*/
1982b14f991SJulian Elischer 			(*((*sipp)->func))( (*sipp)->udata);
1992b14f991SJulian Elischer 			break;
2002b14f991SJulian Elischer 
2012b14f991SJulian Elischer 		case SI_TYPE_KTHREAD:
2022b14f991SJulian Elischer 			/* kernel thread*/
2032b14f991SJulian Elischer 			if (fork(&proc0, NULL, rval))
2042b14f991SJulian Elischer 				panic("fork kernel process");
205a2a1c95cSPeter Wemm 			cpu_set_fork_handler(pfind(rval[0]), (*sipp)->func, (*sipp)->udata);
2062b14f991SJulian Elischer 			break;
2072b14f991SJulian Elischer 
2082b14f991SJulian Elischer 		default:
2092b14f991SJulian Elischer 			panic( "init_main: unrecognized init type");
2102b14f991SJulian Elischer 		}
2112b14f991SJulian Elischer 	}
2122b14f991SJulian Elischer 
213477a642cSPeter Wemm 	panic("Shouldn't get here!");
2142b14f991SJulian Elischer 	/* NOTREACHED*/
2152b14f991SJulian Elischer }
2162b14f991SJulian Elischer 
2172b14f991SJulian Elischer 
2182b14f991SJulian Elischer /*
2192b14f991SJulian Elischer  * Start a kernel process.  This is called after a fork() call in
2202b14f991SJulian Elischer  * main() in the file kern/init_main.c.
2212b14f991SJulian Elischer  *
2222b14f991SJulian Elischer  * This function is used to start "internal" daemons.
2232b14f991SJulian Elischer  */
2242b14f991SJulian Elischer /* ARGSUSED*/
2252b14f991SJulian Elischer void
2262b14f991SJulian Elischer kproc_start(udata)
227d841aaa7SBruce Evans 	void *udata;
2282b14f991SJulian Elischer {
229d841aaa7SBruce Evans 	struct kproc_desc	*kp = udata;
2302b14f991SJulian Elischer 	struct proc		*p = curproc;
2312b14f991SJulian Elischer 
232477a642cSPeter Wemm #ifdef DIAGNOSTIC
233477a642cSPeter Wemm 	printf("Start pid=%d <%s>\n",p->p_pid, kp->arg0);
234477a642cSPeter Wemm #endif
235477a642cSPeter Wemm 
236477a642cSPeter Wemm 
2372b14f991SJulian Elischer 	/* save a global descriptor, if desired*/
2382b14f991SJulian Elischer 	if( kp->global_procpp != NULL)
2392b14f991SJulian Elischer 		*kp->global_procpp	= p;
2402b14f991SJulian Elischer 
2412b14f991SJulian Elischer 	/* this is a non-swapped system process*/
2422b14f991SJulian Elischer 	p->p_flag |= P_INMEM | P_SYSTEM;
2432b14f991SJulian Elischer 
2442b14f991SJulian Elischer 	/* set up arg0 for 'ps', et al*/
2452b14f991SJulian Elischer 	strcpy( p->p_comm, kp->arg0);
2462b14f991SJulian Elischer 
2472b14f991SJulian Elischer 	/* call the processes' main()...*/
2482b14f991SJulian Elischer 	(*kp->func)();
2492b14f991SJulian Elischer 
2502b14f991SJulian Elischer 	/* NOTREACHED */
2512b14f991SJulian Elischer 	panic("kproc_start: %s", kp->arg0);
2522b14f991SJulian Elischer }
2532b14f991SJulian Elischer 
2542b14f991SJulian Elischer 
2552b14f991SJulian Elischer /*
2562b14f991SJulian Elischer  ***************************************************************************
2572b14f991SJulian Elischer  ****
2582b14f991SJulian Elischer  **** The following SYSINIT's belong elsewhere, but have not yet
2592b14f991SJulian Elischer  **** been moved.
2602b14f991SJulian Elischer  ****
2612b14f991SJulian Elischer  ***************************************************************************
2622b14f991SJulian Elischer  */
2632b14f991SJulian Elischer #ifdef OMIT
2642b14f991SJulian Elischer /*
2652b14f991SJulian Elischer  * Handled by vfs_mountroot (bad idea) at this time... should be
2662b14f991SJulian Elischer  * done the same as 4.4Lite2.
2672b14f991SJulian Elischer  */
2682b14f991SJulian Elischer SYSINIT(swapinit, SI_SUB_SWAP, SI_ORDER_FIRST, swapinit, NULL)
2692b14f991SJulian Elischer #endif	/* OMIT*/
2702b14f991SJulian Elischer 
2714590fd3aSDavid Greenman static void print_caddr_t __P((void *data));
2729ef6c28aSBruce Evans static void
2739ef6c28aSBruce Evans print_caddr_t(data)
2744590fd3aSDavid Greenman 	void *data;
2759ef6c28aSBruce Evans {
2769ef6c28aSBruce Evans 	printf("%s", (char *)data);
2779ef6c28aSBruce Evans }
278d841aaa7SBruce Evans SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright)
2792b14f991SJulian Elischer 
2802b14f991SJulian Elischer 
2812b14f991SJulian Elischer /*
2822b14f991SJulian Elischer  ***************************************************************************
2832b14f991SJulian Elischer  ****
2842b14f991SJulian Elischer  **** The two following SYSINT's are proc0 specific glue code.  I am not
2852b14f991SJulian Elischer  **** convinced that they can not be safely combined, but their order of
2862b14f991SJulian Elischer  **** operation has been maintained as the same as the original init_main.c
2872b14f991SJulian Elischer  **** for right now.
2882b14f991SJulian Elischer  ****
2892b14f991SJulian Elischer  **** These probably belong in init_proc.c or kern_proc.c, since they
2902b14f991SJulian Elischer  **** deal with proc0 (the fork template process).
2912b14f991SJulian Elischer  ****
2922b14f991SJulian Elischer  ***************************************************************************
2932b14f991SJulian Elischer  */
2942b14f991SJulian Elischer /* ARGSUSED*/
295154c04e5SPoul-Henning Kamp static void proc0_init __P((void *dummy));
296154c04e5SPoul-Henning Kamp static void
297d841aaa7SBruce Evans proc0_init(dummy)
298d841aaa7SBruce Evans 	void *dummy;
2992b14f991SJulian Elischer {
300df8bae1dSRodney W. Grimes 	register struct proc		*p;
301df8bae1dSRodney W. Grimes 	register struct filedesc0	*fdp;
30292579404SAlexander Langer 	register unsigned i;
303df8bae1dSRodney W. Grimes 
304df8bae1dSRodney W. Grimes 	/*
305df8bae1dSRodney W. Grimes 	 * Initialize the current process pointer (curproc) before
306df8bae1dSRodney W. Grimes 	 * any possible traps/probes to simplify trap processing.
307df8bae1dSRodney W. Grimes 	 */
308df8bae1dSRodney W. Grimes 	p = &proc0;
3092b14f991SJulian Elischer 	curproc = p;			/* XXX redundant*/
310df8bae1dSRodney W. Grimes 
311df8bae1dSRodney W. Grimes 	/*
312a3bfb996SJeffrey Hsu 	 * Initialize process and pgrp structures.
313a3bfb996SJeffrey Hsu 	 */
314a3bfb996SJeffrey Hsu 	procinit();
315a3bfb996SJeffrey Hsu 
316a3bfb996SJeffrey Hsu 	/*
317b1508c72SDavid Greenman 	 * Initialize sleep queue hash table
318b1508c72SDavid Greenman 	 */
319b1508c72SDavid Greenman 	sleepinit();
320b1508c72SDavid Greenman 
321b1508c72SDavid Greenman 	/*
322df8bae1dSRodney W. Grimes 	 * Create process 0 (the swapper).
323df8bae1dSRodney W. Grimes 	 */
324a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&allproc, p, p_list);
325df8bae1dSRodney W. Grimes 	p->p_pgrp = &pgrp0;
326a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
327a3bfb996SJeffrey Hsu 	LIST_INIT(&pgrp0.pg_members);
328a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
329a3bfb996SJeffrey Hsu 
330df8bae1dSRodney W. Grimes 	pgrp0.pg_session = &session0;
331df8bae1dSRodney W. Grimes 	session0.s_count = 1;
332df8bae1dSRodney W. Grimes 	session0.s_leader = p;
333df8bae1dSRodney W. Grimes 
334f3f0ca60SSøren Schmidt 	p->p_sysent = &aout_sysvec;
335f3f0ca60SSøren Schmidt 
336df8bae1dSRodney W. Grimes 	p->p_flag = P_INMEM | P_SYSTEM;
337df8bae1dSRodney W. Grimes 	p->p_stat = SRUN;
338df8bae1dSRodney W. Grimes 	p->p_nice = NZERO;
3397216391eSDavid Greenman 	p->p_rtprio.type = RTP_PRIO_NORMAL;
3407216391eSDavid Greenman 	p->p_rtprio.prio = 0;
341f992f480SDavid Greenman 
3422c1011f7SJohn Dyson /*
3432c1011f7SJohn Dyson  * Link for kernel based threads
3442c1011f7SJohn Dyson  */
3452c1011f7SJohn Dyson 	p->p_peers = 0;
3462c1011f7SJohn Dyson 	p->p_leader = p;
3472c1011f7SJohn Dyson 
348df8bae1dSRodney W. Grimes 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
349df8bae1dSRodney W. Grimes 
350df8bae1dSRodney W. Grimes 	/* Create credentials. */
351df8bae1dSRodney W. Grimes 	cred0.p_refcnt = 1;
352df8bae1dSRodney W. Grimes 	p->p_cred = &cred0;
353df8bae1dSRodney W. Grimes 	p->p_ucred = crget();
354df8bae1dSRodney W. Grimes 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
355df8bae1dSRodney W. Grimes 
356df8bae1dSRodney W. Grimes 	/* Create the file descriptor table. */
357df8bae1dSRodney W. Grimes 	fdp = &filedesc0;
358df8bae1dSRodney W. Grimes 	p->p_fd = &fdp->fd_fd;
359df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_refcnt = 1;
360df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_cmask = cmask;
361df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
362df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
363df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_nfiles = NDFILE;
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes 	/* Create the limits structures. */
366df8bae1dSRodney W. Grimes 	p->p_limit = &limit0;
367df8bae1dSRodney W. Grimes 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
368df8bae1dSRodney W. Grimes 		limit0.pl_rlimit[i].rlim_cur =
369df8bae1dSRodney W. Grimes 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
370b3c5c18dSBruce Evans 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
371b3c5c18dSBruce Evans 	    limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
372b3c5c18dSBruce Evans 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
373b3c5c18dSBruce Evans 	    limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
374df8bae1dSRodney W. Grimes 	i = ptoa(cnt.v_free_count);
375df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
376df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
377df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
378df8bae1dSRodney W. Grimes 	limit0.p_refcnt = 1;
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 	/* Allocate a prototype map so we have something to fork. */
381df8bae1dSRodney W. Grimes 	p->p_vmspace = &vmspace0;
382df8bae1dSRodney W. Grimes 	vmspace0.vm_refcnt = 1;
383df8bae1dSRodney W. Grimes 	pmap_pinit(&vmspace0.vm_pmap);
38426f9a767SRodney W. Grimes 	vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
38547dcd2e5SJohn Dyson 	    trunc_page(VM_MAXUSER_ADDRESS), TRUE);
386df8bae1dSRodney W. Grimes 	vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
387df8bae1dSRodney W. Grimes 	p->p_addr = proc0paddr;				/* XXX */
388df8bae1dSRodney W. Grimes 
3892b14f991SJulian Elischer #define INCOMPAT_LITES2
3902b14f991SJulian Elischer #ifdef INCOMPAT_LITES2
391df8bae1dSRodney W. Grimes 	/*
392a2a1c95cSPeter Wemm 	 * proc0 needs to have a coherent frame base in it's stack.
3932384fde5SDavid Greenman 	 */
3942b14f991SJulian Elischer 	cpu_set_init_frame(p, init_framep);			/* XXX! */
3952b14f991SJulian Elischer #endif	/* INCOMPAT_LITES2*/
3962384fde5SDavid Greenman 
3972384fde5SDavid Greenman 	/*
398df8bae1dSRodney W. Grimes 	 * We continue to place resource usage info and signal
399df8bae1dSRodney W. Grimes 	 * actions in the user struct so they're pageable.
400df8bae1dSRodney W. Grimes 	 */
401df8bae1dSRodney W. Grimes 	p->p_stats = &p->p_addr->u_stats;
402df8bae1dSRodney W. Grimes 	p->p_sigacts = &p->p_addr->u_sigacts;
403df8bae1dSRodney W. Grimes 
404df8bae1dSRodney W. Grimes 	/*
405a3bfb996SJeffrey Hsu 	 * Charge root for one process.
406df8bae1dSRodney W. Grimes 	 */
407df8bae1dSRodney W. Grimes 	(void)chgproccnt(0, 1);
40826f9a767SRodney W. Grimes }
4092b14f991SJulian Elischer SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL)
4102b14f991SJulian Elischer 
4112b14f991SJulian Elischer /* ARGSUSED*/
412154c04e5SPoul-Henning Kamp static void proc0_post __P((void *dummy));
413154c04e5SPoul-Henning Kamp static void
414d841aaa7SBruce Evans proc0_post(dummy)
415d841aaa7SBruce Evans 	void *dummy;
4162b14f991SJulian Elischer {
417a6fc8288SPeter Wemm 	struct timeval tv;
418a6fc8288SPeter Wemm 
4192b14f991SJulian Elischer 	/*
4202b14f991SJulian Elischer 	 * Now can look at time, having had a chance to verify the time
4212b14f991SJulian Elischer 	 * from the file system.  Reset p->p_rtime as it may have been
4222b14f991SJulian Elischer 	 * munched in mi_switch() after the time got set.
4232b14f991SJulian Elischer 	 */
4243c816944SBruce Evans 	gettime(&boottime);
4253c816944SBruce Evans 	proc0.p_stats->p_start = runtime = mono_time = boottime;
4262b14f991SJulian Elischer 	proc0.p_rtime.tv_sec = proc0.p_rtime.tv_usec = 0;
4272b14f991SJulian Elischer 
428a6fc8288SPeter Wemm 	/*
429a6fc8288SPeter Wemm 	 * Give the ``random'' number generator a thump.
430a6fc8288SPeter Wemm 	 */
431a6fc8288SPeter Wemm 	microtime(&tv);
432a6fc8288SPeter Wemm 	srandom(tv.tv_sec ^ tv.tv_usec);
433a6fc8288SPeter Wemm 
4342b14f991SJulian Elischer 	/* Initialize signal state for process 0. */
4352b14f991SJulian Elischer 	siginit(&proc0);
4362b14f991SJulian Elischer }
4372b14f991SJulian Elischer SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL)
4382b14f991SJulian Elischer 
4392b14f991SJulian Elischer 
4402b14f991SJulian Elischer 
441df8bae1dSRodney W. Grimes 
442df8bae1dSRodney W. Grimes /*
4432b14f991SJulian Elischer  ***************************************************************************
4442b14f991SJulian Elischer  ****
4452b14f991SJulian Elischer  **** The following SYSINIT's and glue code should be moved to the
4462b14f991SJulian Elischer  **** respective files on a per subsystem basis.
4472b14f991SJulian Elischer  ****
4482b14f991SJulian Elischer  ***************************************************************************
449df8bae1dSRodney W. Grimes  */
4502b14f991SJulian Elischer /* ARGSUSED*/
451154c04e5SPoul-Henning Kamp static void sched_setup __P((void *dummy));
452154c04e5SPoul-Henning Kamp static void
453d841aaa7SBruce Evans sched_setup(dummy)
454d841aaa7SBruce Evans 	void *dummy;
4552b14f991SJulian Elischer {
456df8bae1dSRodney W. Grimes 	/* Kick off timeout driven events by calling first time. */
457df8bae1dSRodney W. Grimes 	roundrobin(NULL);
458df8bae1dSRodney W. Grimes 	schedcpu(NULL);
4592b14f991SJulian Elischer }
4602b14f991SJulian Elischer SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL)
461df8bae1dSRodney W. Grimes 
4622b14f991SJulian Elischer /* ARGSUSED*/
463996c772fSJohn Dyson static void xxx_vfs_mountroot __P((void *fsnamep));
464432aad0eSTor Egge #ifdef BOOTP
465432aad0eSTor Egge extern void bootpc_init __P((void));
466432aad0eSTor Egge #endif
467154c04e5SPoul-Henning Kamp static void
468996c772fSJohn Dyson xxx_vfs_mountroot(fsnamep)
469996c772fSJohn Dyson 	void *fsnamep;
4702b14f991SJulian Elischer {
471432aad0eSTor Egge   /* XXX Add a separate SYSINIT entry */
472432aad0eSTor Egge #ifdef BOOTP
473432aad0eSTor Egge 	bootpc_init();
474432aad0eSTor Egge #endif
475df8bae1dSRodney W. Grimes 	/* Mount the root file system. */
476996c772fSJohn Dyson 	if (vfs_mountrootfs(*((char **) fsnamep)))
477df8bae1dSRodney W. Grimes 		panic("cannot mount root");
4782b14f991SJulian Elischer }
479996c772fSJohn Dyson SYSINIT(mountroot, SI_SUB_ROOT, SI_ORDER_FIRST, xxx_vfs_mountroot, &mountrootfsname)
4802b14f991SJulian Elischer 
4812b14f991SJulian Elischer /* ARGSUSED*/
482154c04e5SPoul-Henning Kamp static void xxx_vfs_root_fdtab __P((void *dummy));
483154c04e5SPoul-Henning Kamp static void
484d841aaa7SBruce Evans xxx_vfs_root_fdtab(dummy)
485d841aaa7SBruce Evans 	void *dummy;
4862b14f991SJulian Elischer {
4872b14f991SJulian Elischer 	register struct filedesc0	*fdp = &filedesc0;
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
490628641f8SDavid Greenman 	if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
491df8bae1dSRodney W. Grimes 		panic("cannot find root vnode");
492df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_cdir = rootvnode;
493df8bae1dSRodney W. Grimes 	VREF(fdp->fd_fd.fd_cdir);
494996c772fSJohn Dyson 	VOP_UNLOCK(rootvnode, 0, &proc0);
495df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_rdir = NULL;
4962b14f991SJulian Elischer }
4972b14f991SJulian Elischer SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL)
4982b14f991SJulian Elischer 
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes /*
5012b14f991SJulian Elischer  ***************************************************************************
5022b14f991SJulian Elischer  ****
5032b14f991SJulian Elischer  **** The following code probably belongs in another file, like
5042b14f991SJulian Elischer  **** kern/init_init.c.  It is here for two reasons only:
5052b14f991SJulian Elischer  ****
5062b14f991SJulian Elischer  ****	1)	This code returns to startup the system; this is
5072b14f991SJulian Elischer  ****		abnormal for a kernel thread.
5082b14f991SJulian Elischer  ****	2)	This code promiscuously uses init_frame
5092b14f991SJulian Elischer  ****
5102b14f991SJulian Elischer  ***************************************************************************
511df8bae1dSRodney W. Grimes  */
512df8bae1dSRodney W. Grimes 
513d841aaa7SBruce Evans static void kthread_init __P((void *dummy));
5142b14f991SJulian Elischer SYSINIT_KT(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kthread_init, NULL)
5152b14f991SJulian Elischer 
5162b14f991SJulian Elischer 
517a2a1c95cSPeter Wemm extern void prepare_usermode __P((void));
518a2a1c95cSPeter Wemm static void start_init __P((struct proc *p));
5192b14f991SJulian Elischer 
5202b14f991SJulian Elischer /* ARGSUSED*/
5212b14f991SJulian Elischer static void
522d841aaa7SBruce Evans kthread_init(dummy)
523d841aaa7SBruce Evans 	void *dummy;
5242b14f991SJulian Elischer {
525df8bae1dSRodney W. Grimes 
526df8bae1dSRodney W. Grimes 	/* Create process 1 (init(8)). */
527a2a1c95cSPeter Wemm 	start_init(curproc);
528a2a1c95cSPeter Wemm 
529a2a1c95cSPeter Wemm 	prepare_usermode();
5302b14f991SJulian Elischer 
5312b14f991SJulian Elischer 	/*
532a2a1c95cSPeter Wemm 	 * This returns to the fork trampoline, then to user mode.
5332b14f991SJulian Elischer 	 */
534df8bae1dSRodney W. Grimes 	return;
535df8bae1dSRodney W. Grimes }
536df8bae1dSRodney W. Grimes 
537df8bae1dSRodney W. Grimes 
538df8bae1dSRodney W. Grimes /*
539df8bae1dSRodney W. Grimes  * List of paths to try when searching for "init".
540df8bae1dSRodney W. Grimes  */
541df8bae1dSRodney W. Grimes static char *initpaths[] = {
542df8bae1dSRodney W. Grimes 	"/sbin/init",
543df8bae1dSRodney W. Grimes 	"/sbin/oinit",
544df8bae1dSRodney W. Grimes 	"/sbin/init.bak",
5452257b745SPoul-Henning Kamp 	"/stand/sysinstall",
546df8bae1dSRodney W. Grimes 	NULL,
547df8bae1dSRodney W. Grimes };
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes /*
550df8bae1dSRodney W. Grimes  * Start the initial user process; try exec'ing each pathname in "initpaths".
551df8bae1dSRodney W. Grimes  * The program is invoked with one argument containing the boot flags.
552df8bae1dSRodney W. Grimes  */
553df8bae1dSRodney W. Grimes static void
554a2a1c95cSPeter Wemm start_init(p)
555df8bae1dSRodney W. Grimes 	struct proc *p;
556df8bae1dSRodney W. Grimes {
557df8bae1dSRodney W. Grimes 	vm_offset_t addr;
558df8bae1dSRodney W. Grimes 	struct execve_args args;
559df8bae1dSRodney W. Grimes 	int options, i, retval[2], error;
560df8bae1dSRodney W. Grimes 	char **pathp, *path, *ucp, **uap, *arg0, *arg1;
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes 	initproc = p;
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 	/*
565df8bae1dSRodney W. Grimes 	 * Need just enough stack to hold the faked-up "execve()" arguments.
566df8bae1dSRodney W. Grimes 	 */
56726f9a767SRodney W. Grimes 	addr = trunc_page(VM_MAXUSER_ADDRESS - PAGE_SIZE);
568bd7e5f99SJohn Dyson 	if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
569df8bae1dSRodney W. Grimes 		panic("init: couldn't allocate argument space");
570df8bae1dSRodney W. Grimes 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
57126f9a767SRodney W. Grimes 	p->p_vmspace->vm_ssize = 1;
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
574df8bae1dSRodney W. Grimes 		/*
575df8bae1dSRodney W. Grimes 		 * Move out the boot flag argument.
576df8bae1dSRodney W. Grimes 		 */
577df8bae1dSRodney W. Grimes 		options = 0;
578df8bae1dSRodney W. Grimes 		ucp = (char *)USRSTACK;
579df8bae1dSRodney W. Grimes 		(void)subyte(--ucp, 0);		/* trailing zero */
580df8bae1dSRodney W. Grimes 		if (boothowto & RB_SINGLE) {
581df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, 's');
582df8bae1dSRodney W. Grimes 			options = 1;
583df8bae1dSRodney W. Grimes 		}
584df8bae1dSRodney W. Grimes #ifdef notyet
585df8bae1dSRodney W. Grimes                 if (boothowto & RB_FASTBOOT) {
586df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, 'f');
587df8bae1dSRodney W. Grimes 			options = 1;
588df8bae1dSRodney W. Grimes 		}
589df8bae1dSRodney W. Grimes #endif
59017755ac8SPoul-Henning Kamp 
59117755ac8SPoul-Henning Kamp #ifdef BOOTCDROM
59217755ac8SPoul-Henning Kamp 		(void)subyte(--ucp, 'C');
59317755ac8SPoul-Henning Kamp 		options = 1;
59417755ac8SPoul-Henning Kamp #endif
59519060a3aSPoul-Henning Kamp 
59619060a3aSPoul-Henning Kamp #if defined(DEVFS) && defined(DEVFS_ROOT)
59719060a3aSPoul-Henning Kamp 		(void)subyte(--ucp, 'd');
59819060a3aSPoul-Henning Kamp 		options = 1;
59919060a3aSPoul-Henning Kamp #endif
600df8bae1dSRodney W. Grimes 		if (options == 0)
601df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, '-');
602df8bae1dSRodney W. Grimes 		(void)subyte(--ucp, '-');		/* leading hyphen */
603df8bae1dSRodney W. Grimes 		arg1 = ucp;
604df8bae1dSRodney W. Grimes 
605df8bae1dSRodney W. Grimes 		/*
606df8bae1dSRodney W. Grimes 		 * Move out the file name (also arg 0).
607df8bae1dSRodney W. Grimes 		 */
608df8bae1dSRodney W. Grimes 		for (i = strlen(path) + 1; i >= 0; i--)
609df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, path[i]);
610df8bae1dSRodney W. Grimes 		arg0 = ucp;
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes 		/*
613df8bae1dSRodney W. Grimes 		 * Move out the arg pointers.
614df8bae1dSRodney W. Grimes 		 */
615df8bae1dSRodney W. Grimes 		uap = (char **)((int)ucp & ~(NBPW-1));
616df8bae1dSRodney W. Grimes 		(void)suword((caddr_t)--uap, 0);	/* terminator */
617df8bae1dSRodney W. Grimes 		(void)suword((caddr_t)--uap, (int)arg1);
618df8bae1dSRodney W. Grimes 		(void)suword((caddr_t)--uap, (int)arg0);
619df8bae1dSRodney W. Grimes 
620df8bae1dSRodney W. Grimes 		/*
621df8bae1dSRodney W. Grimes 		 * Point at the arguments.
622df8bae1dSRodney W. Grimes 		 */
623df8bae1dSRodney W. Grimes 		args.fname = arg0;
62426f9a767SRodney W. Grimes 		args.argv = uap;
62526f9a767SRodney W. Grimes 		args.envv = NULL;
626df8bae1dSRodney W. Grimes 
627df8bae1dSRodney W. Grimes 		/*
628df8bae1dSRodney W. Grimes 		 * Now try to exec the program.  If can't for any reason
629df8bae1dSRodney W. Grimes 		 * other than it doesn't exist, complain.
6302b14f991SJulian Elischer 		 *
6312b14f991SJulian Elischer 		 * Otherwise return to main() which returns to btext
6322b14f991SJulian Elischer 		 * which completes the system startup.
633df8bae1dSRodney W. Grimes 		 */
634b5e8ce9fSBruce Evans 		if ((error = execve(p, &args, &retval[0])) == 0)
635df8bae1dSRodney W. Grimes 			return;
636df8bae1dSRodney W. Grimes 		if (error != ENOENT)
637df8bae1dSRodney W. Grimes 			printf("exec %s: error %d\n", path, error);
638df8bae1dSRodney W. Grimes 	}
639df8bae1dSRodney W. Grimes 	printf("init: not found\n");
640df8bae1dSRodney W. Grimes 	panic("no init");
641df8bae1dSRodney W. Grimes }
642