xref: /original-bsd/sys/kern/init_main.c (revision 07303858)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)init_main.c	7.58 (Berkeley) 04/17/93
8  */
9 
10 #include <sys/param.h>
11 #include <sys/filedesc.h>
12 #include <sys/kernel.h>
13 #include <sys/mount.h>
14 #include <sys/map.h>
15 #include <sys/proc.h>
16 #include <sys/resourcevar.h>
17 #include <sys/signalvar.h>
18 #include <sys/systm.h>
19 #include <sys/vnode.h>
20 #include <sys/conf.h>
21 #include <sys/buf.h>
22 #include <sys/clist.h>
23 #include <sys/device.h>
24 #include <sys/protosw.h>
25 #include <sys/reboot.h>
26 #include <sys/user.h>
27 
28 #include <ufs/ufs/quota.h>
29 
30 #include <machine/cpu.h>
31 
32 #include <vm/vm.h>
33 
34 #ifdef HPFPLIB
35 char	copyright[] =
36 "Copyright (c) 1982,1986,1989,1991 The Regents of the University of California.\nCopyright (c) 1992 Hewlett-Packard Company\nCopyright (c) 1992 Motorola Inc.\nAll rights reserved.\n\n";
37 #else
38 char	copyright[] =
39 "Copyright (c) 1982,1986,1989,1991 The Regents of the University of California.\nAll rights reserved.\n\n";
40 #endif
41 
42 /*
43  * Components of process 0;
44  * never freed.
45  */
46 struct	session session0;
47 struct	pgrp pgrp0;
48 struct	proc proc0;
49 struct	pcred cred0;
50 struct	filedesc0 filedesc0;
51 struct	plimit limit0;
52 struct	vmspace vmspace0;
53 struct	proc *curproc = &proc0;
54 struct	proc *initproc, *pageproc;
55 
56 int	cmask = CMASK;
57 extern	struct user *proc0paddr;
58 extern	int (*mountroot)();
59 
60 struct	vnode *rootvp, *swapdev_vp;
61 int	boothowto;
62 struct	timeval boottime;
63 struct	timeval runtime;
64 
65 /*
66  * System startup; initialize the world, create process 0,
67  * mount root filesystem, and fork to create init and pagedaemon.
68  * Most of the hard work is done in the lower-level initialization
69  * routines including startup(), which does memory initialization
70  * and autoconfiguration.
71  */
72 main()
73 {
74 	register int i;
75 	register struct proc *p;
76 	register struct filedesc0 *fdp;
77 	register struct pdevinit *pdev;
78 	int s, rval[2];
79 	extern struct pdevinit pdevinit[];
80 	extern void roundrobin __P((void *));
81 	extern void schedcpu __P((void *));
82 
83 	/*
84 	 * Initialize curproc before any possible traps/probes
85 	 * to simplify trap processing.
86 	 */
87 	p = &proc0;
88 	curproc = p;
89 	/*
90 	 * Attempt to find console and initialize
91 	 * in case of early panic or other messages.
92 	 */
93 	consinit();
94 	printf(copyright);
95 
96 	vm_mem_init();
97 	kmeminit();
98 	cpu_startup();
99 
100 	/*
101 	 * set up system process 0 (swapper)
102 	 */
103 	p = &proc0;
104 	curproc = p;
105 
106 	allproc = (volatile struct proc *)p;
107 	p->p_prev = (struct proc **)&allproc;
108 	p->p_pgrp = &pgrp0;
109 	pgrphash[0] = &pgrp0;
110 	pgrp0.pg_mem = p;
111 	pgrp0.pg_session = &session0;
112 	session0.s_count = 1;
113 	session0.s_leader = p;
114 
115 	p->p_flag = SLOAD|SSYS;
116 	p->p_stat = SRUN;
117 	p->p_nice = NZERO;
118 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
119 
120 	/*
121 	 * Setup credentials
122 	 */
123 	cred0.p_refcnt = 1;
124 	p->p_cred = &cred0;
125 	p->p_ucred = crget();
126 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
127 
128 	/*
129 	 * Create the file descriptor table for process 0.
130 	 */
131 	fdp = &filedesc0;
132 	p->p_fd = &fdp->fd_fd;
133 	fdp->fd_fd.fd_refcnt = 1;
134 	fdp->fd_fd.fd_cmask = cmask;
135 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
136 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
137 	fdp->fd_fd.fd_nfiles = NDFILE;
138 
139 	/*
140 	 * Set initial limits
141 	 */
142 	p->p_limit = &limit0;
143 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
144 		limit0.pl_rlimit[i].rlim_cur =
145 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
146 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
147 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
148 	limit0.p_refcnt = 1;
149 
150 	/*
151 	 * Allocate a prototype map so we have something to fork
152 	 */
153 	p->p_vmspace = &vmspace0;
154 	vmspace0.vm_refcnt = 1;
155 	pmap_pinit(&vmspace0.vm_pmap);
156 	vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
157 	    trunc_page(VM_MAX_ADDRESS), TRUE);
158 	vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
159 	p->p_addr = proc0paddr;				/* XXX */
160 
161 	/*
162 	 * We continue to place resource usage info
163 	 * and signal actions in the user struct so they're pageable.
164 	 */
165 	p->p_stats = &p->p_addr->u_stats;
166 	p->p_sigacts = &p->p_addr->u_sigacts;
167 
168 	/*
169 	 * Initialize per uid information structure and charge
170 	 * root for one process.
171 	 */
172 	usrinfoinit();
173 	(void)chgproccnt(0, 1);
174 
175 	rqinit();
176 
177 	/* Configure virtual memory system, set vm rlimits. */
178 	vm_init_limits(p);
179 
180 	/* Initialize the file systems. */
181 	vfsinit();
182 
183 	/* Start real time and statistics clocks. */
184 	initclocks();
185 
186 	/* Initialize tables. */
187 	mbinit();
188 	cinit();
189 #ifdef SYSVSHM
190 	shminit();
191 #endif
192 
193 	/* Attach pseudo-devices. */
194 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
195 		(*pdev->pdev_attach)(pdev->pdev_count);
196 
197 	/*
198 	 * Initialize protocols.  Block reception of incoming packets
199 	 * until everything is ready.
200 	 */
201 	s = splimp();
202 	ifinit();
203 	domaininit();
204 	splx(s);
205 
206 #ifdef GPROF
207 	kmstartup();
208 #endif
209 
210 	/* kick off timeout driven events by calling first time */
211 	roundrobin(NULL);
212 	schedcpu(NULL);
213 
214 	/*
215 	 * Set up the root file system and vnode.
216 	 */
217 	if ((*mountroot)())
218 		panic("cannot mount root");
219 	/*
220 	 * Get vnode for '/'.
221 	 * Setup rootdir and fdp->fd_fd.fd_cdir to point to it.
222 	 */
223 	if (VFS_ROOT(rootfs, &rootdir))
224 		panic("cannot find root vnode");
225 	fdp->fd_fd.fd_cdir = rootdir;
226 	VREF(fdp->fd_fd.fd_cdir);
227 	VOP_UNLOCK(rootdir);
228 	fdp->fd_fd.fd_rdir = NULL;
229 	swapinit();
230 
231 	/*
232 	 * Now can look at time, having had a chance to verify the time
233 	 * from the file system.  Reset p->p_rtime as it may have been
234 	 * munched in swtch() after the time got set.
235 	 */
236 	p->p_stats->p_start = runtime = mono_time = boottime = time;
237 	p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
238 
239 	/*
240 	 * make init process
241 	 */
242 	siginit(p);
243 	if (fork(p, NULL, rval))
244 		panic("fork init");
245 	if (rval[1]) {
246 		extern int icode[];		/* user init code */
247 		extern int szicode;		/* size of icode */
248 		static char initflags[] = "-sf";
249 		vm_offset_t addr;
250 		char *ip;
251 
252 		/*
253 		 * Now in process 1.  Set init flags into icode, get a minimal
254 		 * address space, copy out "icode", and return to it to do an
255 		 * exec of init.
256 		 */
257 		ip = initflags + 1;
258 		if (boothowto&RB_SINGLE)
259 			*ip++ = 's';
260 #ifdef notyet
261 		if (boothowto&RB_FASTBOOT)
262 			*ip++ = 'f';
263 #endif
264 		if (ip == initflags + 1)
265 			*ip++ = '-';
266 		*ip++ = '\0';
267 
268 		addr = VM_MIN_ADDRESS;
269 		initproc = p = curproc;
270 		if (vm_allocate(&p->p_vmspace->vm_map, &addr,
271 		    round_page(szicode + sizeof(initflags)), FALSE) != 0 ||
272 		    addr != VM_MIN_ADDRESS)
273 			panic("init: couldn't allocate at zero");
274 
275 		/* need just enough stack to exec from */
276 		addr = trunc_page(USRSTACK - PAGE_SIZE);
277 		if (vm_allocate(&p->p_vmspace->vm_map, &addr,
278 		    PAGE_SIZE, FALSE) != KERN_SUCCESS)
279 			panic("vm_allocate init stack");
280 		p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
281 		(void)copyout((caddr_t)icode, (caddr_t)VM_MIN_ADDRESS,
282 		    (u_int)szicode);
283 		(void)copyout(initflags, (caddr_t)(VM_MIN_ADDRESS + szicode),
284 		    sizeof(initflags));
285 		return;			/* returns to icode */
286 	}
287 
288 	/*
289 	 * Start up pageout daemon (process 2).
290 	 */
291 	if (fork(p, NULL, rval))
292 		panic("fork pager");
293 	if (rval[1]) {
294 		/*
295 		 * Now in process 2.
296 		 */
297 		p = curproc;
298 		pageproc = p;
299 		p->p_flag |= SLOAD|SSYS;		/* XXX */
300 		bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
301 		vm_pageout();
302 		/*NOTREACHED*/
303 	}
304 
305 	/*
306 	 * enter scheduling loop
307 	 */
308 	sched();
309 }
310