xref: /freebsd/sys/kern/init_main.c (revision c5b193bfba1554a4cff9c68e314075232062cb0c)
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
42c5b193bfSPoul-Henning Kamp  * $Id: init_main.c,v 1.80 1998/01/22 17:29:44 dyson Exp $
43df8bae1dSRodney W. Grimes  */
44df8bae1dSRodney W. Grimes 
4519060a3aSPoul-Henning Kamp #include "opt_devfs.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>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <machine/cpu.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <vm/vm.h>
67efeaf95aSDavid Greenman #include <vm/vm_param.h>
68efeaf95aSDavid Greenman #include <vm/vm_prot.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 
779ef6c28aSBruce Evans extern void __main __P((void));
789ef6c28aSBruce Evans extern void main __P((void *framep));
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes /* Components of the first process -- never freed. */
81154c04e5SPoul-Henning Kamp static struct session session0;
82154c04e5SPoul-Henning Kamp static struct pgrp pgrp0;
83df8bae1dSRodney W. Grimes struct	proc proc0;
84154c04e5SPoul-Henning Kamp static struct pcred cred0;
85154c04e5SPoul-Henning Kamp static struct filedesc0 filedesc0;
86154c04e5SPoul-Henning Kamp static struct plimit limit0;
87154c04e5SPoul-Henning Kamp static struct vmspace vmspace0;
88477a642cSPeter Wemm #ifndef SMP	/* per-cpu on smp */
89df8bae1dSRodney W. Grimes struct	proc *curproc = &proc0;
90477a642cSPeter Wemm #endif
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;
100946bb7a2SPoul-Henning Kamp SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime,
101c463cf1cSBruce Evans 	CTLFLAG_RD, &boottime, timeval, "");
102946bb7a2SPoul-Henning Kamp 
103f4e39ee7SDavid Nugent static int shutdowntimeout = 120;
104f4e39ee7SDavid Nugent SYSCTL_INT(_kern, OID_AUTO, shutdown_timeout,
105f4e39ee7SDavid Nugent 	CTLFLAG_RW, &shutdowntimeout, 0, "");
106f4e39ee7SDavid Nugent 
107b3196e4bSPeter Wemm #ifndef SMP	/* per-cpu on smp */
108df8bae1dSRodney W. Grimes struct	timeval runtime;
109477a642cSPeter Wemm #endif
110df8bae1dSRodney W. Grimes 
1112b14f991SJulian Elischer /*
1122b14f991SJulian Elischer  * Promiscuous argument pass for start_init()
1132b14f991SJulian Elischer  *
1142b14f991SJulian Elischer  * This is a kludge because we use a return from main() rather than a call
11537b688c4SAlexander Langer  * to a new routine in locore.s to kick the kernel alive from locore.s.
1162b14f991SJulian Elischer  */
1172b14f991SJulian Elischer static void	*init_framep;
1182b14f991SJulian Elischer 
119df8bae1dSRodney W. Grimes 
12026f9a767SRodney W. Grimes #if __GNUC__ >= 2
12126f9a767SRodney W. Grimes void __main() {}
12226f9a767SRodney W. Grimes #endif
12326f9a767SRodney W. Grimes 
1242b14f991SJulian Elischer 
12526f9a767SRodney W. Grimes /*
1262b14f991SJulian Elischer  * This ensures that there is at least one entry so that the sysinit_set
1272b14f991SJulian Elischer  * symbol is not undefined.  A sybsystem ID of SI_SUB_DUMMY is never
1282b14f991SJulian Elischer  * executed.
12926f9a767SRodney W. Grimes  */
1302b14f991SJulian Elischer SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL)
1318a129caeSDavid Greenman 
13226f9a767SRodney W. Grimes 
133df8bae1dSRodney W. Grimes /*
134df8bae1dSRodney W. Grimes  * System startup; initialize the world, create process 0, mount root
135df8bae1dSRodney W. Grimes  * filesystem, and fork to create init and pagedaemon.  Most of the
136df8bae1dSRodney W. Grimes  * hard work is done in the lower-level initialization routines including
137df8bae1dSRodney W. Grimes  * startup(), which does memory initialization and autoconfiguration.
1382b14f991SJulian Elischer  *
1392b14f991SJulian Elischer  * This allows simple addition of new kernel subsystems that require
1402b14f991SJulian Elischer  * boot time initialization.  It also allows substitution of subsystem
1412b14f991SJulian Elischer  * (for instance, a scheduler, kernel profiler, or VM system) by object
142c5b193bfSPoul-Henning Kamp  * module.  Finally, it allows for optional "kernel threads".
143df8bae1dSRodney W. Grimes  */
14426f9a767SRodney W. Grimes void
145df8bae1dSRodney W. Grimes main(framep)
146df8bae1dSRodney W. Grimes 	void *framep;
147df8bae1dSRodney W. Grimes {
1482b14f991SJulian Elischer 
1492b14f991SJulian Elischer 	register struct sysinit **sipp;		/* system initialization*/
1502b14f991SJulian Elischer 	register struct sysinit **xipp;		/* interior loop of sort*/
1512b14f991SJulian Elischer 	register struct sysinit *save;		/* bubble*/
1522b14f991SJulian Elischer 
1532b14f991SJulian Elischer 	/*
154a2a1c95cSPeter Wemm 	 * Copy the locore.s frame pointer for proc0, this is forked into
155a2a1c95cSPeter Wemm 	 * all other processes.
1562b14f991SJulian Elischer 	 */
1572b14f991SJulian Elischer 	init_framep = framep;
1582b14f991SJulian Elischer 
1596b556c4bSSteve Passe #ifdef SMP
1606b556c4bSSteve Passe 	/*
1616b556c4bSSteve Passe 	 * XXX curproc is per-cpu in SMP, and exists in freshly mapped pages,
1626b556c4bSSteve Passe 	 * so its value must be initialized now before it is used by various
1636b556c4bSSteve Passe 	 * initialization routines.  proc0_init() isn't soon enough:
1646b556c4bSSteve Passe 	 * among other things, configure() is called first, and may call
1656b556c4bSSteve Passe 	 * setdumpdev(), which panics without a valid curproc.
1666b556c4bSSteve Passe 	 */
1676b556c4bSSteve Passe 	curproc = &proc0;
1686b556c4bSSteve Passe #endif /* SMP */
1696b556c4bSSteve Passe 
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 	 *
1742b14f991SJulian Elischer 	 * Since some things care about execution order, this is the
1752b14f991SJulian Elischer 	 * operation which ensures continued function.
1762b14f991SJulian Elischer 	 */
1772b14f991SJulian Elischer 	for( sipp = (struct sysinit **)sysinit_set.ls_items; *sipp; sipp++) {
1782b14f991SJulian Elischer 		for( xipp = sipp + 1; *xipp; xipp++) {
1792b14f991SJulian Elischer 			if( (*sipp)->subsystem < (*xipp)->subsystem ||
1802b14f991SJulian Elischer 			    ( (*sipp)->subsystem == (*xipp)->subsystem &&
1812b14f991SJulian Elischer 			      (*sipp)->order < (*xipp)->order))
1822b14f991SJulian Elischer 				continue;	/* skip*/
1832b14f991SJulian Elischer 			save = *sipp;
1842b14f991SJulian Elischer 			*sipp = *xipp;
1852b14f991SJulian Elischer 			*xipp = save;
1862b14f991SJulian Elischer 		}
1872b14f991SJulian Elischer 	}
1882b14f991SJulian Elischer 
1892b14f991SJulian Elischer 	/*
1902b14f991SJulian Elischer 	 * Traverse the (now) ordered list of system initialization tasks.
1912b14f991SJulian Elischer 	 * Perform each task, and continue on to the next task.
1922b14f991SJulian Elischer 	 *
1932b14f991SJulian Elischer 	 * The last item on the list is expected to be the scheduler,
1942b14f991SJulian Elischer 	 * which will not return.
1952b14f991SJulian Elischer 	 */
1962b14f991SJulian Elischer 	for( sipp = (struct sysinit **)sysinit_set.ls_items; *sipp; sipp++) {
19774b2192aSJohn Dyson 
1982b14f991SJulian Elischer 		if( (*sipp)->subsystem == SI_SUB_DUMMY)
1992b14f991SJulian Elischer 			continue;	/* skip dummy task(s)*/
2002b14f991SJulian Elischer 
2012b14f991SJulian Elischer 		switch( (*sipp)->type) {
2022b14f991SJulian Elischer 		case SI_TYPE_DEFAULT:
2032b14f991SJulian Elischer 			/* no special processing*/
2042b14f991SJulian Elischer 			(*((*sipp)->func))( (*sipp)->udata);
2052b14f991SJulian Elischer 			break;
2062b14f991SJulian Elischer 
2072b14f991SJulian Elischer 		case SI_TYPE_KTHREAD:
20874b2192aSJohn Dyson #if !defined(SMP)
2092b14f991SJulian Elischer 			/* kernel thread*/
21074b2192aSJohn Dyson 			if (fork1(&proc0, RFMEM|RFFDG|RFPROC))
21174b2192aSJohn Dyson 				panic("fork kernel thread");
21274b2192aSJohn Dyson 			cpu_set_fork_handler(pfind(proc0.p_retval[0]),
21374b2192aSJohn Dyson 			    (*sipp)->func, (*sipp)->udata);
21474b2192aSJohn Dyson 			break;
21574b2192aSJohn Dyson #endif
21674b2192aSJohn Dyson 
21774b2192aSJohn Dyson 		case SI_TYPE_KPROCESS:
21874b2192aSJohn Dyson 			if (fork1(&proc0, RFFDG|RFPROC))
2192b14f991SJulian Elischer 				panic("fork kernel process");
220cb226aaaSPoul-Henning Kamp 			cpu_set_fork_handler(pfind(proc0.p_retval[0]),
221cb226aaaSPoul-Henning Kamp 			    (*sipp)->func, (*sipp)->udata);
2222b14f991SJulian Elischer 			break;
2232b14f991SJulian Elischer 
2242b14f991SJulian Elischer 		default:
2252b14f991SJulian Elischer 			panic( "init_main: unrecognized init type");
2262b14f991SJulian Elischer 		}
2272b14f991SJulian Elischer 	}
2282b14f991SJulian Elischer 
229477a642cSPeter Wemm 	panic("Shouldn't get here!");
2302b14f991SJulian Elischer 	/* NOTREACHED*/
2312b14f991SJulian Elischer }
2322b14f991SJulian Elischer 
2332b14f991SJulian Elischer 
2342b14f991SJulian Elischer /*
2352b14f991SJulian Elischer  * Start a kernel process.  This is called after a fork() call in
2362b14f991SJulian Elischer  * main() in the file kern/init_main.c.
2372b14f991SJulian Elischer  *
2382b14f991SJulian Elischer  * This function is used to start "internal" daemons.
2392b14f991SJulian Elischer  */
2402b14f991SJulian Elischer /* ARGSUSED*/
2412b14f991SJulian Elischer void
2422b14f991SJulian Elischer kproc_start(udata)
243d841aaa7SBruce Evans 	void *udata;
2442b14f991SJulian Elischer {
245d841aaa7SBruce Evans 	struct kproc_desc	*kp = udata;
2462b14f991SJulian Elischer 	struct proc		*p = curproc;
2472b14f991SJulian Elischer 
248477a642cSPeter Wemm #ifdef DIAGNOSTIC
249477a642cSPeter Wemm 	printf("Start pid=%d <%s>\n",p->p_pid, kp->arg0);
250477a642cSPeter Wemm #endif
251477a642cSPeter Wemm 
2522b14f991SJulian Elischer 	/* save a global descriptor, if desired*/
2532b14f991SJulian Elischer 	if( kp->global_procpp != NULL)
2542b14f991SJulian Elischer 		*kp->global_procpp	= p;
2552b14f991SJulian Elischer 
2562b14f991SJulian Elischer 	/* this is a non-swapped system process*/
2572b14f991SJulian Elischer 	p->p_flag |= P_INMEM | P_SYSTEM;
2582b14f991SJulian Elischer 
2592b14f991SJulian Elischer 	/* set up arg0 for 'ps', et al*/
2602b14f991SJulian Elischer 	strcpy( p->p_comm, kp->arg0);
2612b14f991SJulian Elischer 
2622b14f991SJulian Elischer 	/* call the processes' main()...*/
2632b14f991SJulian Elischer 	(*kp->func)();
2642b14f991SJulian Elischer 
2652b14f991SJulian Elischer 	/* NOTREACHED */
2662b14f991SJulian Elischer 	panic("kproc_start: %s", kp->arg0);
2672b14f991SJulian Elischer }
2682b14f991SJulian Elischer 
2692b14f991SJulian Elischer 
2702b14f991SJulian Elischer /*
2712b14f991SJulian Elischer  ***************************************************************************
2722b14f991SJulian Elischer  ****
2732b14f991SJulian Elischer  **** The following SYSINIT's belong elsewhere, but have not yet
2742b14f991SJulian Elischer  **** been moved.
2752b14f991SJulian Elischer  ****
2762b14f991SJulian Elischer  ***************************************************************************
2772b14f991SJulian Elischer  */
2782b14f991SJulian Elischer #ifdef OMIT
2792b14f991SJulian Elischer /*
2802b14f991SJulian Elischer  * Handled by vfs_mountroot (bad idea) at this time... should be
2812b14f991SJulian Elischer  * done the same as 4.4Lite2.
2822b14f991SJulian Elischer  */
2832b14f991SJulian Elischer SYSINIT(swapinit, SI_SUB_SWAP, SI_ORDER_FIRST, swapinit, NULL)
2842b14f991SJulian Elischer #endif	/* OMIT*/
2852b14f991SJulian Elischer 
2864590fd3aSDavid Greenman static void print_caddr_t __P((void *data));
2879ef6c28aSBruce Evans static void
2889ef6c28aSBruce Evans print_caddr_t(data)
2894590fd3aSDavid Greenman 	void *data;
2909ef6c28aSBruce Evans {
2919ef6c28aSBruce Evans 	printf("%s", (char *)data);
2929ef6c28aSBruce Evans }
293d841aaa7SBruce Evans SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright)
2942b14f991SJulian Elischer 
2952b14f991SJulian Elischer 
2962b14f991SJulian Elischer /*
2972b14f991SJulian Elischer  ***************************************************************************
2982b14f991SJulian Elischer  ****
2992b14f991SJulian Elischer  **** The two following SYSINT's are proc0 specific glue code.  I am not
3002b14f991SJulian Elischer  **** convinced that they can not be safely combined, but their order of
3012b14f991SJulian Elischer  **** operation has been maintained as the same as the original init_main.c
3022b14f991SJulian Elischer  **** for right now.
3032b14f991SJulian Elischer  ****
3042b14f991SJulian Elischer  **** These probably belong in init_proc.c or kern_proc.c, since they
3052b14f991SJulian Elischer  **** deal with proc0 (the fork template process).
3062b14f991SJulian Elischer  ****
3072b14f991SJulian Elischer  ***************************************************************************
3082b14f991SJulian Elischer  */
3092b14f991SJulian Elischer /* ARGSUSED*/
310154c04e5SPoul-Henning Kamp static void proc0_init __P((void *dummy));
311154c04e5SPoul-Henning Kamp static void
312d841aaa7SBruce Evans proc0_init(dummy)
313d841aaa7SBruce Evans 	void *dummy;
3142b14f991SJulian Elischer {
315df8bae1dSRodney W. Grimes 	register struct proc		*p;
316df8bae1dSRodney W. Grimes 	register struct filedesc0	*fdp;
31792579404SAlexander Langer 	register unsigned i;
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	/*
320df8bae1dSRodney W. Grimes 	 * Initialize the current process pointer (curproc) before
321df8bae1dSRodney W. Grimes 	 * any possible traps/probes to simplify trap processing.
322df8bae1dSRodney W. Grimes 	 */
323df8bae1dSRodney W. Grimes 	p = &proc0;
3242b14f991SJulian Elischer 	curproc = p;			/* XXX redundant*/
325df8bae1dSRodney W. Grimes 
326df8bae1dSRodney W. Grimes 	/*
327a3bfb996SJeffrey Hsu 	 * Initialize process and pgrp structures.
328a3bfb996SJeffrey Hsu 	 */
329a3bfb996SJeffrey Hsu 	procinit();
330a3bfb996SJeffrey Hsu 
331a3bfb996SJeffrey Hsu 	/*
332b1508c72SDavid Greenman 	 * Initialize sleep queue hash table
333b1508c72SDavid Greenman 	 */
334b1508c72SDavid Greenman 	sleepinit();
335b1508c72SDavid Greenman 
336b1508c72SDavid Greenman 	/*
3373075778bSJohn Dyson 	 * additional VM structures
3383075778bSJohn Dyson 	 */
3393075778bSJohn Dyson 	vm_init2();
3403075778bSJohn Dyson 
3413075778bSJohn Dyson 	/*
342df8bae1dSRodney W. Grimes 	 * Create process 0 (the swapper).
343df8bae1dSRodney W. Grimes 	 */
344a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&allproc, p, p_list);
345df8bae1dSRodney W. Grimes 	p->p_pgrp = &pgrp0;
346a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
347a3bfb996SJeffrey Hsu 	LIST_INIT(&pgrp0.pg_members);
348a3bfb996SJeffrey Hsu 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
349a3bfb996SJeffrey Hsu 
350df8bae1dSRodney W. Grimes 	pgrp0.pg_session = &session0;
351df8bae1dSRodney W. Grimes 	session0.s_count = 1;
352df8bae1dSRodney W. Grimes 	session0.s_leader = p;
353df8bae1dSRodney W. Grimes 
354f3f0ca60SSøren Schmidt 	p->p_sysent = &aout_sysvec;
355f3f0ca60SSøren Schmidt 
356df8bae1dSRodney W. Grimes 	p->p_flag = P_INMEM | P_SYSTEM;
357df8bae1dSRodney W. Grimes 	p->p_stat = SRUN;
358df8bae1dSRodney W. Grimes 	p->p_nice = NZERO;
3597216391eSDavid Greenman 	p->p_rtprio.type = RTP_PRIO_NORMAL;
3607216391eSDavid Greenman 	p->p_rtprio.prio = 0;
361f992f480SDavid Greenman 
3622c1011f7SJohn Dyson /*
3632c1011f7SJohn Dyson  * Link for kernel based threads
3642c1011f7SJohn Dyson  */
3652c1011f7SJohn Dyson 	p->p_peers = 0;
3662c1011f7SJohn Dyson 	p->p_leader = p;
3672c1011f7SJohn Dyson 
368df8bae1dSRodney W. Grimes 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes 	/* Create credentials. */
371df8bae1dSRodney W. Grimes 	cred0.p_refcnt = 1;
372df8bae1dSRodney W. Grimes 	p->p_cred = &cred0;
373df8bae1dSRodney W. Grimes 	p->p_ucred = crget();
374df8bae1dSRodney W. Grimes 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
375df8bae1dSRodney W. Grimes 
376df8bae1dSRodney W. Grimes 	/* Create the file descriptor table. */
377df8bae1dSRodney W. Grimes 	fdp = &filedesc0;
378df8bae1dSRodney W. Grimes 	p->p_fd = &fdp->fd_fd;
379df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_refcnt = 1;
380df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_cmask = cmask;
381df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
382df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
383df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_nfiles = NDFILE;
384df8bae1dSRodney W. Grimes 
385df8bae1dSRodney W. Grimes 	/* Create the limits structures. */
386df8bae1dSRodney W. Grimes 	p->p_limit = &limit0;
387df8bae1dSRodney W. Grimes 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
388df8bae1dSRodney W. Grimes 		limit0.pl_rlimit[i].rlim_cur =
389df8bae1dSRodney W. Grimes 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
390b3c5c18dSBruce Evans 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
391b3c5c18dSBruce Evans 	    limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
392b3c5c18dSBruce Evans 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
393b3c5c18dSBruce Evans 	    limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
394df8bae1dSRodney W. Grimes 	i = ptoa(cnt.v_free_count);
395df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
396df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
397df8bae1dSRodney W. Grimes 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
398df8bae1dSRodney W. Grimes 	limit0.p_refcnt = 1;
399df8bae1dSRodney W. Grimes 
400df8bae1dSRodney W. Grimes 	/* Allocate a prototype map so we have something to fork. */
401df8bae1dSRodney W. Grimes 	p->p_vmspace = &vmspace0;
402df8bae1dSRodney W. Grimes 	vmspace0.vm_refcnt = 1;
40382566551SJohn Dyson 	pmap_pinit0(&vmspace0.vm_pmap);
40426f9a767SRodney W. Grimes 	vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
4052d8acc0fSJohn Dyson 	    trunc_page(VM_MAXUSER_ADDRESS));
406df8bae1dSRodney W. Grimes 	vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
407df8bae1dSRodney W. Grimes 	p->p_addr = proc0paddr;				/* XXX */
408df8bae1dSRodney W. Grimes 
4092b14f991SJulian Elischer #define INCOMPAT_LITES2
4102b14f991SJulian Elischer #ifdef INCOMPAT_LITES2
411df8bae1dSRodney W. Grimes 	/*
412a2a1c95cSPeter Wemm 	 * proc0 needs to have a coherent frame base in it's stack.
4132384fde5SDavid Greenman 	 */
4142b14f991SJulian Elischer 	cpu_set_init_frame(p, init_framep);			/* XXX! */
4152b14f991SJulian Elischer #endif	/* INCOMPAT_LITES2*/
4162384fde5SDavid Greenman 
4172384fde5SDavid Greenman 	/*
418df8bae1dSRodney W. Grimes 	 * We continue to place resource usage info and signal
419df8bae1dSRodney W. Grimes 	 * actions in the user struct so they're pageable.
420df8bae1dSRodney W. Grimes 	 */
421df8bae1dSRodney W. Grimes 	p->p_stats = &p->p_addr->u_stats;
422df8bae1dSRodney W. Grimes 	p->p_sigacts = &p->p_addr->u_sigacts;
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes 	/*
425a3bfb996SJeffrey Hsu 	 * Charge root for one process.
426df8bae1dSRodney W. Grimes 	 */
427df8bae1dSRodney W. Grimes 	(void)chgproccnt(0, 1);
4282a024a2bSSean Eric Fagan 
4292a024a2bSSean Eric Fagan 	/*
4302a024a2bSSean Eric Fagan 	 * Initialize the procfs flags (to 0, of course)
4312a024a2bSSean Eric Fagan 	 */
4322a024a2bSSean Eric Fagan 	p->p_stops = p->p_stype = p->p_step = 0;
4332a024a2bSSean Eric Fagan 
43426f9a767SRodney W. Grimes }
4352b14f991SJulian Elischer SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL)
4362b14f991SJulian Elischer 
4372b14f991SJulian Elischer /* ARGSUSED*/
438154c04e5SPoul-Henning Kamp static void proc0_post __P((void *dummy));
439154c04e5SPoul-Henning Kamp static void
440d841aaa7SBruce Evans proc0_post(dummy)
441d841aaa7SBruce Evans 	void *dummy;
4422b14f991SJulian Elischer {
443a6fc8288SPeter Wemm 	struct timeval tv;
444a6fc8288SPeter Wemm 
4452b14f991SJulian Elischer 	/*
4462b14f991SJulian Elischer 	 * Now can look at time, having had a chance to verify the time
4472b14f991SJulian Elischer 	 * from the file system.  Reset p->p_rtime as it may have been
4482b14f991SJulian Elischer 	 * munched in mi_switch() after the time got set.
4492b14f991SJulian Elischer 	 */
4503c816944SBruce Evans 	gettime(&boottime);
4513c816944SBruce Evans 	proc0.p_stats->p_start = runtime = mono_time = boottime;
4522b14f991SJulian Elischer 	proc0.p_rtime.tv_sec = proc0.p_rtime.tv_usec = 0;
4532b14f991SJulian Elischer 
454a6fc8288SPeter Wemm 	/*
455a6fc8288SPeter Wemm 	 * Give the ``random'' number generator a thump.
456a6fc8288SPeter Wemm 	 */
457a6fc8288SPeter Wemm 	microtime(&tv);
458a6fc8288SPeter Wemm 	srandom(tv.tv_sec ^ tv.tv_usec);
459a6fc8288SPeter Wemm 
4602b14f991SJulian Elischer 	/* Initialize signal state for process 0. */
4612b14f991SJulian Elischer 	siginit(&proc0);
4622b14f991SJulian Elischer }
4632b14f991SJulian Elischer SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL)
4642b14f991SJulian Elischer 
4652b14f991SJulian Elischer 
4662b14f991SJulian Elischer 
467df8bae1dSRodney W. Grimes 
468df8bae1dSRodney W. Grimes /*
4692b14f991SJulian Elischer  ***************************************************************************
4702b14f991SJulian Elischer  ****
4712b14f991SJulian Elischer  **** The following SYSINIT's and glue code should be moved to the
4722b14f991SJulian Elischer  **** respective files on a per subsystem basis.
4732b14f991SJulian Elischer  ****
4742b14f991SJulian Elischer  ***************************************************************************
475df8bae1dSRodney W. Grimes  */
476df8bae1dSRodney W. Grimes 
4772b14f991SJulian Elischer /* ARGSUSED */
478ab36c067SJustin T. Gibbs static void root_conf __P((void *dummy));
479ab36c067SJustin T. Gibbs static void
480ab36c067SJustin T. Gibbs root_conf(dummy)
481ab36c067SJustin T. Gibbs 	void *dummy;
482ab36c067SJustin T. Gibbs {
483ab36c067SJustin T. Gibbs 	cpu_rootconf();
484ab36c067SJustin T. Gibbs }
485ab36c067SJustin T. Gibbs SYSINIT(root_conf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, root_conf, NULL)
486ab36c067SJustin T. Gibbs 
487ab36c067SJustin T. Gibbs /* ARGSUSED*/
488154c04e5SPoul-Henning Kamp static void xxx_vfs_root_fdtab __P((void *dummy));
489154c04e5SPoul-Henning Kamp static void
490d841aaa7SBruce Evans xxx_vfs_root_fdtab(dummy)
491d841aaa7SBruce Evans 	void *dummy;
4922b14f991SJulian Elischer {
4932b14f991SJulian Elischer 	register struct filedesc0	*fdp = &filedesc0;
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
496628641f8SDavid Greenman 	if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
497df8bae1dSRodney W. Grimes 		panic("cannot find root vnode");
498df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_cdir = rootvnode;
499df8bae1dSRodney W. Grimes 	VREF(fdp->fd_fd.fd_cdir);
500996c772fSJohn Dyson 	VOP_UNLOCK(rootvnode, 0, &proc0);
501df8bae1dSRodney W. Grimes 	fdp->fd_fd.fd_rdir = NULL;
5022b14f991SJulian Elischer }
5032b14f991SJulian Elischer SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL)
5042b14f991SJulian Elischer 
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes /*
5072b14f991SJulian Elischer  ***************************************************************************
5082b14f991SJulian Elischer  ****
5092b14f991SJulian Elischer  **** The following code probably belongs in another file, like
5102b14f991SJulian Elischer  **** kern/init_init.c.  It is here for two reasons only:
5112b14f991SJulian Elischer  ****
5122b14f991SJulian Elischer  ****	1)	This code returns to startup the system; this is
5132b14f991SJulian Elischer  ****		abnormal for a kernel thread.
5142b14f991SJulian Elischer  ****	2)	This code promiscuously uses init_frame
5152b14f991SJulian Elischer  ****
5162b14f991SJulian Elischer  ***************************************************************************
517df8bae1dSRodney W. Grimes  */
518df8bae1dSRodney W. Grimes 
519d841aaa7SBruce Evans static void kthread_init __P((void *dummy));
52074b2192aSJohn Dyson SYSINIT_KP(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kthread_init, NULL)
5212b14f991SJulian Elischer 
5222b14f991SJulian Elischer 
523a2a1c95cSPeter Wemm extern void prepare_usermode __P((void));
524a2a1c95cSPeter Wemm static void start_init __P((struct proc *p));
5252b14f991SJulian Elischer 
5262b14f991SJulian Elischer /* ARGSUSED*/
5272b14f991SJulian Elischer static void
528d841aaa7SBruce Evans kthread_init(dummy)
529d841aaa7SBruce Evans 	void *dummy;
5302b14f991SJulian Elischer {
531df8bae1dSRodney W. Grimes 	/* Create process 1 (init(8)). */
532a2a1c95cSPeter Wemm 	start_init(curproc);
533a2a1c95cSPeter Wemm 
534a2a1c95cSPeter Wemm 	prepare_usermode();
5352b14f991SJulian Elischer 
5362b14f991SJulian Elischer 	/*
537a2a1c95cSPeter Wemm 	 * This returns to the fork trampoline, then to user mode.
5382b14f991SJulian Elischer 	 */
539df8bae1dSRodney W. Grimes 	return;
540df8bae1dSRodney W. Grimes }
541df8bae1dSRodney W. Grimes 
542df8bae1dSRodney W. Grimes 
543df8bae1dSRodney W. Grimes /*
544df8bae1dSRodney W. Grimes  * List of paths to try when searching for "init".
545df8bae1dSRodney W. Grimes  */
546df8bae1dSRodney W. Grimes static char *initpaths[] = {
547df8bae1dSRodney W. Grimes 	"/sbin/init",
548df8bae1dSRodney W. Grimes 	"/sbin/oinit",
549df8bae1dSRodney W. Grimes 	"/sbin/init.bak",
5502257b745SPoul-Henning Kamp 	"/stand/sysinstall",
551df8bae1dSRodney W. Grimes 	NULL,
552df8bae1dSRodney W. Grimes };
553df8bae1dSRodney W. Grimes 
554df8bae1dSRodney W. Grimes /*
555df8bae1dSRodney W. Grimes  * Start the initial user process; try exec'ing each pathname in "initpaths".
556df8bae1dSRodney W. Grimes  * The program is invoked with one argument containing the boot flags.
557df8bae1dSRodney W. Grimes  */
558df8bae1dSRodney W. Grimes static void
559a2a1c95cSPeter Wemm start_init(p)
560df8bae1dSRodney W. Grimes 	struct proc *p;
561df8bae1dSRodney W. Grimes {
562df8bae1dSRodney W. Grimes 	vm_offset_t addr;
563df8bae1dSRodney W. Grimes 	struct execve_args args;
5644a11ca4eSPoul-Henning Kamp 	int options, i, error;
565df8bae1dSRodney W. Grimes 	char **pathp, *path, *ucp, **uap, *arg0, *arg1;
566df8bae1dSRodney W. Grimes 
567df8bae1dSRodney W. Grimes 	initproc = p;
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes 	/*
570df8bae1dSRodney W. Grimes 	 * Need just enough stack to hold the faked-up "execve()" arguments.
571df8bae1dSRodney W. Grimes 	 */
57226f9a767SRodney W. Grimes 	addr = trunc_page(VM_MAXUSER_ADDRESS - PAGE_SIZE);
573bd7e5f99SJohn Dyson 	if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
574df8bae1dSRodney W. Grimes 		panic("init: couldn't allocate argument space");
575df8bae1dSRodney W. Grimes 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
57626f9a767SRodney W. Grimes 	p->p_vmspace->vm_ssize = 1;
577df8bae1dSRodney W. Grimes 
578df8bae1dSRodney W. Grimes 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
579df8bae1dSRodney W. Grimes 		/*
580df8bae1dSRodney W. Grimes 		 * Move out the boot flag argument.
581df8bae1dSRodney W. Grimes 		 */
582df8bae1dSRodney W. Grimes 		options = 0;
583df8bae1dSRodney W. Grimes 		ucp = (char *)USRSTACK;
584df8bae1dSRodney W. Grimes 		(void)subyte(--ucp, 0);		/* trailing zero */
585df8bae1dSRodney W. Grimes 		if (boothowto & RB_SINGLE) {
586df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, 's');
587df8bae1dSRodney W. Grimes 			options = 1;
588df8bae1dSRodney W. Grimes 		}
589df8bae1dSRodney W. Grimes #ifdef notyet
590df8bae1dSRodney W. Grimes                 if (boothowto & RB_FASTBOOT) {
591df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, 'f');
592df8bae1dSRodney W. Grimes 			options = 1;
593df8bae1dSRodney W. Grimes 		}
594df8bae1dSRodney W. Grimes #endif
59517755ac8SPoul-Henning Kamp 
59617755ac8SPoul-Henning Kamp #ifdef BOOTCDROM
59717755ac8SPoul-Henning Kamp 		(void)subyte(--ucp, 'C');
59817755ac8SPoul-Henning Kamp 		options = 1;
59917755ac8SPoul-Henning Kamp #endif
60019060a3aSPoul-Henning Kamp 
60119060a3aSPoul-Henning Kamp #if defined(DEVFS) && defined(DEVFS_ROOT)
60219060a3aSPoul-Henning Kamp 		(void)subyte(--ucp, 'd');
60319060a3aSPoul-Henning Kamp 		options = 1;
60419060a3aSPoul-Henning Kamp #endif
605df8bae1dSRodney W. Grimes 		if (options == 0)
606df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, '-');
607df8bae1dSRodney W. Grimes 		(void)subyte(--ucp, '-');		/* leading hyphen */
608df8bae1dSRodney W. Grimes 		arg1 = ucp;
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes 		/*
611df8bae1dSRodney W. Grimes 		 * Move out the file name (also arg 0).
612df8bae1dSRodney W. Grimes 		 */
613df8bae1dSRodney W. Grimes 		for (i = strlen(path) + 1; i >= 0; i--)
614df8bae1dSRodney W. Grimes 			(void)subyte(--ucp, path[i]);
615df8bae1dSRodney W. Grimes 		arg0 = ucp;
616df8bae1dSRodney W. Grimes 
617df8bae1dSRodney W. Grimes 		/*
618df8bae1dSRodney W. Grimes 		 * Move out the arg pointers.
619df8bae1dSRodney W. Grimes 		 */
620df8bae1dSRodney W. Grimes 		uap = (char **)((int)ucp & ~(NBPW-1));
621df8bae1dSRodney W. Grimes 		(void)suword((caddr_t)--uap, 0);	/* terminator */
622df8bae1dSRodney W. Grimes 		(void)suword((caddr_t)--uap, (int)arg1);
623df8bae1dSRodney W. Grimes 		(void)suword((caddr_t)--uap, (int)arg0);
624df8bae1dSRodney W. Grimes 
625df8bae1dSRodney W. Grimes 		/*
626df8bae1dSRodney W. Grimes 		 * Point at the arguments.
627df8bae1dSRodney W. Grimes 		 */
628df8bae1dSRodney W. Grimes 		args.fname = arg0;
62926f9a767SRodney W. Grimes 		args.argv = uap;
63026f9a767SRodney W. Grimes 		args.envv = NULL;
631df8bae1dSRodney W. Grimes 
632df8bae1dSRodney W. Grimes 		/*
633df8bae1dSRodney W. Grimes 		 * Now try to exec the program.  If can't for any reason
634df8bae1dSRodney W. Grimes 		 * other than it doesn't exist, complain.
6352b14f991SJulian Elischer 		 *
6362b14f991SJulian Elischer 		 * Otherwise return to main() which returns to btext
6372b14f991SJulian Elischer 		 * which completes the system startup.
638df8bae1dSRodney W. Grimes 		 */
639cb226aaaSPoul-Henning Kamp 		if ((error = execve(p, &args)) == 0)
640df8bae1dSRodney W. Grimes 			return;
641df8bae1dSRodney W. Grimes 		if (error != ENOENT)
642df8bae1dSRodney W. Grimes 			printf("exec %s: error %d\n", path, error);
643df8bae1dSRodney W. Grimes 	}
644df8bae1dSRodney W. Grimes 	printf("init: not found\n");
645df8bae1dSRodney W. Grimes 	panic("no init");
646df8bae1dSRodney W. Grimes }
647