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