xref: /netbsd-src/sys/kern/init_main.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*	$NetBSD: init_main.c,v 1.71 1994/12/19 14:36:46 cgd Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)init_main.c	8.9 (Berkeley) 1/21/94
41  */
42 
43 #include <sys/param.h>
44 #include <sys/filedesc.h>
45 #include <sys/errno.h>
46 #include <sys/exec.h>
47 #include <sys/kernel.h>
48 #include <sys/mount.h>
49 #include <sys/map.h>
50 #include <sys/proc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/signalvar.h>
53 #include <sys/systm.h>
54 #include <sys/vnode.h>
55 #include <sys/conf.h>
56 #include <sys/buf.h>
57 #ifdef REAL_CLISTS
58 #include <sys/clist.h>
59 #endif
60 #include <sys/device.h>
61 #include <sys/protosw.h>
62 #include <sys/reboot.h>
63 #include <sys/user.h>
64 
65 #include <sys/syscallargs.h>
66 
67 #include <ufs/ufs/quota.h>
68 
69 #include <machine/cpu.h>
70 
71 #include <vm/vm.h>
72 
73 char	copyright[] =
74 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California.  All rights reserved.\n\n";
75 
76 /* Components of the first process -- never freed. */
77 struct	session session0;
78 struct	pgrp pgrp0;
79 struct	proc proc0;
80 struct	pcred cred0;
81 struct	filedesc0 filedesc0;
82 struct	plimit limit0;
83 struct	vmspace vmspace0;
84 struct	proc *curproc = &proc0;
85 struct	proc *initproc, *pageproc;
86 
87 int	cmask = CMASK;
88 extern	struct user *proc0paddr;
89 
90 struct	vnode *rootvp, *swapdev_vp;
91 int	boothowto;
92 struct	timeval boottime;
93 struct	timeval runtime;
94 
95 static void start_init __P((struct proc *p, void *framep));
96 
97 /*
98  * System startup; initialize the world, create process 0, mount root
99  * filesystem, and fork to create init and pagedaemon.  Most of the
100  * hard work is done in the lower-level initialization routines including
101  * startup(), which does memory initialization and autoconfiguration.
102  */
103 main(framep)
104 	void *framep;
105 {
106 	register struct proc *p;
107 	register struct filedesc0 *fdp;
108 	register struct pdevinit *pdev;
109 	register int i;
110 	int s;
111 	register_t rval[2];
112 	extern int (*mountroot) __P((void));
113 	extern struct pdevinit pdevinit[];
114 	extern void roundrobin __P((void *));
115 	extern void schedcpu __P((void *));
116 
117 	/*
118 	 * Initialize the current process pointer (curproc) before
119 	 * any possible traps/probes to simplify trap processing.
120 	 */
121 	p = &proc0;
122 	curproc = p;
123 	/*
124 	 * Attempt to find console and initialize
125 	 * in case of early panic or other messages.
126 	 */
127 	consinit();
128 	printf(copyright);
129 
130 	vm_mem_init();
131 	kmeminit();
132 	cpu_startup();
133 
134 	/*
135 	 * Initialize process and pgrp structures.
136 	 */
137 	procinit();
138 
139 	/*
140 	 * Create process 0 (the swapper).
141 	 */
142 	LIST_INSERT_HEAD(&allproc, p, p_list);
143 	p->p_pgrp = &pgrp0;
144 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
145 	LIST_INIT(&pgrp0.pg_members);
146 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
147 
148 	pgrp0.pg_session = &session0;
149 	session0.s_count = 1;
150 	session0.s_leader = p;
151 
152 	p->p_flag = P_INMEM | P_SYSTEM;
153 	p->p_stat = SRUN;
154 	p->p_nice = NZERO;
155 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
156 
157 	/* Create credentials. */
158 	cred0.p_refcnt = 1;
159 	p->p_cred = &cred0;
160 	p->p_ucred = crget();
161 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
162 
163 	/* Create the file descriptor table. */
164 	fdp = &filedesc0;
165 	p->p_fd = &fdp->fd_fd;
166 	fdp->fd_fd.fd_refcnt = 1;
167 	fdp->fd_fd.fd_cmask = cmask;
168 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
169 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
170 	fdp->fd_fd.fd_nfiles = NDFILE;
171 
172 	/* Create the limits structures. */
173 	p->p_limit = &limit0;
174 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
175 		limit0.pl_rlimit[i].rlim_cur =
176 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
177 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
178 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
179 	i = ptoa(cnt.v_free_count);
180 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
181 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
182 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
183 	limit0.p_refcnt = 1;
184 
185 	/* Allocate a prototype map so we have something to fork. */
186 	p->p_vmspace = &vmspace0;
187 	vmspace0.vm_refcnt = 1;
188 	pmap_pinit(&vmspace0.vm_pmap);
189 	vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
190 	    trunc_page(VM_MAX_ADDRESS), TRUE);
191 	vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
192 	p->p_addr = proc0paddr;				/* XXX */
193 
194 	/*
195 	 * We continue to place resource usage info and signal
196 	 * actions in the user struct so they're pageable.
197 	 */
198 	p->p_stats = &p->p_addr->u_stats;
199 	p->p_sigacts = &p->p_addr->u_sigacts;
200 
201 	/*
202 	 * Charge root for one process.
203 	 */
204 	(void)chgproccnt(0, 1);
205 
206 	rqinit();
207 
208 	/* Configure virtual memory system, set vm rlimits. */
209 	vm_init_limits(p);
210 
211 	/* Initialize the file systems. */
212 	vfsinit();
213 
214 	/* Start real time and statistics clocks. */
215 	initclocks();
216 
217 	/* Initialize mbuf's. */
218 	mbinit();
219 
220 #ifdef REAL_CLISTS
221 	/* Initialize clists. */
222 	clist_init();
223 #endif
224 
225 #ifdef SYSVSHM
226 	/* Initialize System V style shared memory. */
227 	shminit();
228 #endif
229 
230 #ifdef SYSVSEM
231 	/* Initialize System V style semaphores. */
232 	seminit();
233 #endif
234 
235 #ifdef SYSVMSG
236 	/* Initialize System V style message queues. */
237 	msginit();
238 #endif
239 
240 	/* Attach pseudo-devices. */
241 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
242 		(*pdev->pdev_attach)(pdev->pdev_count);
243 
244 	/*
245 	 * Initialize protocols.  Block reception of incoming packets
246 	 * until everything is ready.
247 	 */
248 	s = splimp();
249 	ifinit();
250 	domaininit();
251 	splx(s);
252 
253 #ifdef GPROF
254 	/* Initialize kernel profiling. */
255 	kmstartup();
256 #endif
257 
258 	/* Kick off timeout driven events by calling first time. */
259 	roundrobin(NULL);
260 	schedcpu(NULL);
261 
262 	/* Mount the root file system. */
263 	if ((*mountroot)())
264 		panic("cannot mount root");
265 	mountlist.tqh_first->mnt_op->vfs_refcount++;
266 
267 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
268 	if (VFS_ROOT(mountlist.tqh_first, &rootvnode))
269 		panic("cannot find root vnode");
270 	fdp->fd_fd.fd_cdir = rootvnode;
271 	VREF(fdp->fd_fd.fd_cdir);
272 	VOP_UNLOCK(rootvnode);
273 	fdp->fd_fd.fd_rdir = NULL;
274 	swapinit();
275 
276 	/*
277 	 * Now can look at time, having had a chance to verify the time
278 	 * from the file system.  Reset p->p_rtime as it may have been
279 	 * munched in mi_switch() after the time got set.
280 	 */
281 	p->p_stats->p_start = runtime = mono_time = boottime = time;
282 	p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
283 
284 	/* Initialize signal state for process 0. */
285 	siginit(p);
286 
287 	/* Create process 1 (init(8)). */
288 	if (fork(p, NULL, rval))
289 		panic("fork init");
290 	if (rval[1]) {
291 		start_init(curproc, framep);
292 		return;
293 	}
294 
295 	/* Create process 2 (the pageout daemon). */
296 	if (fork(p, NULL, rval))
297 		panic("fork pager");
298 	if (rval[1]) {
299 		/*
300 		 * Now in process 2.
301 		 */
302 		p = curproc;
303 		pageproc = p;
304 		p->p_flag |= P_INMEM | P_SYSTEM;	/* XXX */
305 		bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
306 		vm_pageout();
307 		/* NOTREACHED */
308 	}
309 
310 	/* The scheduler is an infinite loop. */
311 	scheduler();
312 	/* NOTREACHED */
313 }
314 
315 /*
316  * List of paths to try when searching for "init".
317  */
318 static char *initpaths[] = {
319 	"/sbin/init",
320 	"/sbin/oinit",
321 	"/sbin/init.bak",
322 	NULL,
323 };
324 
325 #ifdef DEBUG
326 /* Print things out while trying to start init if startinit_verbose != 0. */
327 int startinit_verbose = 0;
328 #endif
329 
330 /*
331  * Start the initial user process; try exec'ing each pathname in "initpaths".
332  * The program is invoked with one argument containing the boot flags.
333  */
334 static void
335 start_init(p, framep)
336 	struct proc *p;
337 	void *framep;
338 {
339 	vm_offset_t addr;
340 	struct execve_args /* {
341 		syscallarg(char *) path;
342 		syscallarg(char **) argp;
343 		syscallarg(char **) envp;
344 	} */ args;
345 	int options, i, error;
346 	register_t retval[2];
347 	char flags[4], *flagsp;
348 	char **pathp, *path, *ucp, **uap, *arg0, *arg1;
349 
350 	initproc = p;
351 
352 	/*
353 	 * We need to set the system call frame as if we were entered through
354 	 * a syscall() so that when we call execve() below, it will be able
355 	 * to set the entry point (see setregs) when it tries to exec.  The
356 	 * startup code in "locore.s" has allocated space for the frame and
357 	 * passed a pointer to that space as main's argument.
358 	 */
359 	cpu_set_init_frame(p, framep);
360 
361 	/*
362 	 * Need just enough stack to hold the faked-up "execve()" arguments.
363 	 */
364 	addr = USRSTACK - PAGE_SIZE;
365 	if (vm_allocate(&p->p_vmspace->vm_map, &addr, (vm_size_t)PAGE_SIZE,
366 	    FALSE) != 0)
367 		panic("init: couldn't allocate argument space");
368 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
369 
370 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
371 		ucp = (char *)(addr + PAGE_SIZE);
372 
373 		/*
374 		 * Construct the boot flag argument.
375 		 */
376 		flagsp = flags;
377 		*flagsp++ = '-';
378 		options = 0;
379 
380 		if (boothowto & RB_SINGLE) {
381 			*flagsp++ = 's';
382 			options = 1;
383 		}
384 #ifdef notyet
385 		if (boothowto & RB_FASTBOOT) {
386 			*flagsp++ = 'f';
387 			options = 1;
388 		}
389 #endif
390 
391 		/*
392 		 * Move out the flags (arg 1), if necessary.
393 		 */
394 		if (options != 0) {
395 			*flagsp++ = '\0';
396 			i = flagsp - flags;
397 #ifdef DEBUG
398 			if (startinit_verbose)
399 				printf("init: copying out flags `%s' %d\n",
400 				    flags, i);
401 #endif
402 			(void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
403 			arg1 = ucp;
404 		}
405 
406 		/*
407 		 * Move out the file name (also arg 0).
408 		 */
409 		i = strlen(path) + 1;
410 #ifdef DEBUG
411 		if (startinit_verbose)
412 			printf("init: copying out path `%s' %d\n", path, i);
413 #endif
414 		(void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
415 		arg0 = ucp;
416 
417 		/*
418 		 * Move out the arg pointers.
419 		 */
420 		uap = (char **)((int)ucp & ~ALIGNBYTES);
421 		(void)suword((caddr_t)--uap, 0);	/* terminator */
422 		if (options != 0)
423 			(void)suword((caddr_t)--uap, (long)arg1);
424 		(void)suword((caddr_t)--uap, (long)arg0);
425 
426 		/*
427 		 * Point at the arguments.
428 		 */
429 		SCARG(&args, path) = arg0;
430 		SCARG(&args, argp) = uap;
431 		SCARG(&args, envp) = NULL;
432 
433 		/*
434 		 * Now try to exec the program.  If can't for any reason
435 		 * other than it doesn't exist, complain.
436 		 */
437 		if ((error = execve(p, &args, retval)) == 0)
438 			return;
439 #ifdef DEBUG
440 		if (error != ENOENT || startinit_verbose)
441 #else
442 		if (error != ENOENT)
443 #endif
444 			printf("exec %s: error %d\n", path, error);
445 	}
446 	printf("init: not found\n");
447 	panic("no init");
448 }
449