xref: /openbsd-src/sys/kern/init_main.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: init_main.c,v 1.97 2002/11/22 16:47:28 art Exp $	*/
2 /*	$NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1995 Christopher G. Demetriou.  All rights reserved.
6  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)init_main.c	8.9 (Berkeley) 1/21/94
43  */
44 
45 #include <sys/param.h>
46 #include <sys/filedesc.h>
47 #include <sys/file.h>
48 #include <sys/errno.h>
49 #include <sys/exec.h>
50 #include <sys/kernel.h>
51 #include <sys/kthread.h>
52 #include <sys/mount.h>
53 #include <sys/proc.h>
54 #include <sys/resourcevar.h>
55 #include <sys/signalvar.h>
56 #include <sys/systm.h>
57 #include <sys/namei.h>
58 #include <sys/vnode.h>
59 #include <sys/tty.h>
60 #include <sys/conf.h>
61 #include <sys/buf.h>
62 #include <sys/device.h>
63 #include <sys/socketvar.h>
64 #include <sys/protosw.h>
65 #include <sys/reboot.h>
66 #include <sys/user.h>
67 #ifdef SYSVSHM
68 #include <sys/shm.h>
69 #endif
70 #ifdef SYSVSEM
71 #include <sys/sem.h>
72 #endif
73 #ifdef SYSVMSG
74 #include <sys/msg.h>
75 #endif
76 #include <sys/domain.h>
77 #include <sys/mbuf.h>
78 #include <sys/pipe.h>
79 
80 #include <sys/syscall.h>
81 #include <sys/syscallargs.h>
82 
83 #include <ufs/ufs/quota.h>
84 
85 #include <machine/cpu.h>
86 
87 #include <uvm/uvm.h>
88 
89 #include <net/if.h>
90 #include <net/raw_cb.h>
91 
92 #if defined(CRYPTO)
93 #include <crypto/cryptodev.h>
94 #include <crypto/cryptosoft.h>
95 #endif
96 
97 #if defined(NFSSERVER) || defined(NFSCLIENT)
98 extern void nfs_init(void);
99 #endif
100 
101 const char	copyright[] =
102 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n"
103 "\tThe Regents of the University of California.  All rights reserved.\n"
104 "Copyright (c) 1995-2002 OpenBSD. All rights reserved.  http://www.OpenBSD.org\n";
105 
106 /* Components of the first process -- never freed. */
107 struct	session session0;
108 struct	pgrp pgrp0;
109 struct	proc proc0;
110 struct	pcred cred0;
111 struct	plimit limit0;
112 struct	vmspace vmspace0;
113 struct	sigacts sigacts0;
114 #ifndef curproc
115 struct	proc *curproc;
116 #endif
117 struct	proc *initproc;
118 
119 int	cmask = CMASK;
120 extern	struct user *proc0paddr;
121 
122 void	(*md_diskconf)(void) = NULL;
123 struct	vnode *rootvp, *swapdev_vp;
124 int	boothowto;
125 struct	timeval boottime;
126 struct	timeval runtime;
127 
128 /* XXX return int so gcc -Werror won't complain */
129 int	main(void *);
130 void	check_console(struct proc *);
131 void	start_init(void *);
132 void	start_cleaner(void *);
133 void	start_update(void *);
134 void	start_reaper(void *);
135 void    start_crypto(void *);
136 
137 extern char sigcode[], esigcode[];
138 #ifdef SYSCALL_DEBUG
139 extern char *syscallnames[];
140 #endif
141 
142 struct emul emul_native = {
143 	"native",
144 	NULL,
145 	sendsig,
146 	SYS_syscall,
147 	SYS_MAXSYSCALL,
148 	sysent,
149 #ifdef SYSCALL_DEBUG
150 	syscallnames,
151 #else
152 	NULL,
153 #endif
154 	0,
155 	copyargs,
156 	setregs,
157 	NULL,
158 	sigcode,
159 	esigcode,
160 };
161 
162 /*
163  * System startup; initialize the world, create process 0, mount root
164  * filesystem, and fork to create init and pagedaemon.  Most of the
165  * hard work is done in the lower-level initialization routines including
166  * startup(), which does memory initialization and autoconfiguration.
167  */
168 /* XXX return int, so gcc -Werror won't complain */
169 int
170 main(framep)
171 	void *framep;				/* XXX should go away */
172 {
173 	register struct proc *p;
174 	register struct pdevinit *pdev;
175 	struct timeval rtv;
176 	register int i;
177 	quad_t lim;
178 	int s;
179 	register_t rval[2];
180 	extern struct pdevinit pdevinit[];
181 	extern void scheduler_start(void);
182 	extern void disk_init(void);
183 	extern void endtsleep(void *);
184 	extern void realitexpire(void *);
185 
186 	/*
187 	 * Initialize the current process pointer (curproc) before
188 	 * any possible traps/probes to simplify trap processing.
189 	 */
190 	p = &proc0;
191 	curproc = p;
192 
193 	/*
194 	 * Attempt to find console and initialize
195 	 * in case of early panic or other messages.
196 	 */
197 	config_init();		/* init autoconfiguration data structures */
198 	consinit();
199 	printf(copyright);
200 	printf("\n");
201 
202 	uvm_init();
203 	disk_init();		/* must come before autoconfiguration */
204 	tty_init();		/* initialise tty's */
205 	cpu_startup();
206 
207 	/*
208 	 * Initialize mbuf's.  Do this now because we might attempt to
209 	 * allocate mbufs or mbuf clusters during autoconfiguration.
210 	 */
211 	mbinit();
212 
213 	/* Initalize sockets. */
214 	soinit();
215 
216 	/*
217 	 * Initialize timeouts.
218 	 */
219 	timeout_startup();
220 
221 	cpu_configure();
222 
223 	/* Initialize sysctls (must be done before any processes run) */
224 	sysctl_init();
225 
226 	/*
227 	 * Initialize process and pgrp structures.
228 	 */
229 	procinit();
230 
231 	/*
232 	 * Initialize filedescriptors.
233 	 */
234 	filedesc_init();
235 
236 	/*
237 	 * Initialize pipes.
238 	 */
239 	pipe_init();
240 
241 	/*
242 	 * Create process 0 (the swapper).
243 	 */
244 	LIST_INSERT_HEAD(&allproc, p, p_list);
245 	p->p_pgrp = &pgrp0;
246 	LIST_INSERT_HEAD(PIDHASH(0), p, p_hash);
247 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
248 	LIST_INIT(&pgrp0.pg_members);
249 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
250 
251 	pgrp0.pg_session = &session0;
252 	session0.s_count = 1;
253 	session0.s_leader = p;
254 
255 	p->p_flag = P_INMEM | P_SYSTEM | P_NOCLDWAIT;
256 	p->p_stat = SRUN;
257 	p->p_nice = NZERO;
258 	p->p_emul = &emul_native;
259 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
260 
261 	/* Init timeouts. */
262 	timeout_set(&p->p_sleep_to, endtsleep, p);
263 	timeout_set(&p->p_realit_to, realitexpire, p);
264 
265 	/* Create credentials. */
266 	cred0.p_refcnt = 1;
267 	p->p_cred = &cred0;
268 	p->p_ucred = crget();
269 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
270 
271 	/* Create the file descriptor table. */
272 	p->p_fd = fdinit(NULL);
273 
274 	/* Create the limits structures. */
275 	p->p_limit = &limit0;
276 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
277 		limit0.pl_rlimit[i].rlim_cur =
278 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
279 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
280 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = MIN(NOFILE_MAX,
281 	    (maxfiles - NOFILE > NOFILE) ?  maxfiles - NOFILE : NOFILE);
282 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
283 	lim = ptoa(uvmexp.free);
284 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
285 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
286 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
287 	limit0.p_refcnt = 1;
288 
289 	/* Allocate a prototype map so we have something to fork. */
290 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
291 	    trunc_page(VM_MAX_ADDRESS), TRUE);
292 	p->p_vmspace = &vmspace0;
293 
294 	p->p_addr = proc0paddr;				/* XXX */
295 
296 	/*
297 	 * We continue to place resource usage info in the
298 	 * user struct so they're pageable.
299 	 */
300 	p->p_stats = &p->p_addr->u_stats;
301 
302 	/*
303 	 * Charge root for one process.
304 	 */
305 	(void)chgproccnt(0, 1);
306 
307 	rqinit();
308 
309 	/* Configure virtual memory system, set vm rlimits. */
310 	uvm_init_limits(p);
311 
312 	/* Initialize the file systems. */
313 #if defined(NFSSERVER) || defined(NFSCLIENT)
314 	nfs_init();			/* initialize server/shared data */
315 #endif
316 	vfsinit();
317 
318 	/* Start real time and statistics clocks. */
319 	initclocks();
320 
321 #ifdef SYSVSHM
322 	/* Initialize System V style shared memory. */
323 	shminit();
324 #endif
325 
326 #ifdef SYSVSEM
327 	/* Initialize System V style semaphores. */
328 	seminit();
329 #endif
330 
331 #ifdef SYSVMSG
332 	/* Initialize System V style message queues. */
333 	msginit();
334 #endif
335 
336 	/* Attach pseudo-devices. */
337 	randomattach();
338 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
339 		if (pdev->pdev_count > 0)
340 			(*pdev->pdev_attach)(pdev->pdev_count);
341 
342 #ifdef CRYPTO
343 	swcr_init();
344 #endif /* CRYPTO */
345 
346 	/*
347 	 * Initialize protocols.  Block reception of incoming packets
348 	 * until everything is ready.
349 	 */
350 	s = splimp();
351 	ifinit();
352 	domaininit();
353 	if_attachdomain();
354 	splx(s);
355 
356 #ifdef GPROF
357 	/* Initialize kernel profiling. */
358 	kmstartup();
359 #endif
360 
361 	/* Start the scheduler */
362 	scheduler_start();
363 
364 	/* Initialize signal state for process 0. */
365 	signal_init();
366 	p->p_sigacts = &sigacts0;
367 	siginit(p);
368 
369 	dostartuphooks();
370 
371 	/* Configure root/swap devices */
372 	if (md_diskconf)
373 		(*md_diskconf)();
374 
375 	/* Mount the root file system. */
376 	if (vfs_mountroot())
377 		panic("cannot mount root");
378 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
379 
380 	/* Get the vnode for '/'.  Set p->p_fd->fd_cdir to reference it. */
381 	if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
382 		panic("cannot find root vnode");
383 	p->p_fd->fd_cdir = rootvnode;
384 	VREF(p->p_fd->fd_cdir);
385 	VOP_UNLOCK(rootvnode, 0, p);
386 	p->p_fd->fd_rdir = NULL;
387 
388 	uvm_swap_init();
389 
390 	/*
391 	 * Now can look at time, having had a chance to verify the time
392 	 * from the file system.  Reset p->p_rtime as it may have been
393 	 * munched in mi_switch() after the time got set.
394 	 */
395 	p->p_stats->p_start = runtime = mono_time = boottime = time;
396 	p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
397 
398 	/* Create process 1 (init(8)). */
399 	if (fork1(p, SIGCHLD, FORK_FORK, NULL, 0, start_init, NULL, rval))
400 		panic("fork init");
401 
402 	/* Create process 2, the pageout daemon kernel thread. */
403 	if (kthread_create(uvm_pageout, NULL, NULL, "pagedaemon"))
404 		panic("fork pagedaemon");
405 
406 	/* Create process 3, the reaper daemon kernel thread. */
407 	if (kthread_create(start_reaper, NULL, NULL, "reaper"))
408 		panic("fork reaper");
409 
410 	/* Create process 4, the cleaner daemon kernel thread. */
411 	if (kthread_create(start_cleaner, NULL, NULL, "cleaner"))
412 		panic("fork cleaner");
413 
414 	/* Create process 5, the update daemon kernel thread. */
415 	if (kthread_create(start_update, NULL, NULL, "update"))
416 		panic("fork update");
417 
418 	/* Create process 6, the aiodone daemon kernel thread. */
419 	if (kthread_create(uvm_aiodone_daemon, NULL, NULL, "aiodoned"))
420 		panic("fork aiodoned");
421 
422 #ifdef CRYPTO
423 	/* Create process 7, the crypto kernel thread. */
424 	if (kthread_create(start_crypto, NULL, NULL, "crypto"))
425 		panic("crypto thread");
426 #endif /* CRYPTO */
427 
428 	/* Create any other deferred kernel threads. */
429 	kthread_run_deferred_queue();
430 
431 	microtime(&rtv);
432 	srandom((u_long)(rtv.tv_sec ^ rtv.tv_usec));
433 
434 	randompid = 1;
435 	/* The scheduler is an infinite loop. */
436 	uvm_scheduler();
437 	/* NOTREACHED */
438 }
439 
440 /*
441  * List of paths to try when searching for "init".
442  */
443 static char *initpaths[] = {
444 	"/sbin/init",
445 	"/sbin/oinit",
446 	"/sbin/init.bak",
447 	NULL,
448 };
449 
450 void
451 check_console(p)
452 	struct proc *p;
453 {
454 	struct nameidata nd;
455 	int error;
456 
457 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
458 	error = namei(&nd);
459 	if (error) {
460 		if (error == ENOENT)
461 			printf("warning: /dev/console does not exist\n");
462 		else
463 			printf("warning: /dev/console error %d\n", error);
464 	} else
465 		vrele(nd.ni_vp);
466 }
467 
468 /*
469  * Start the initial user process; try exec'ing each pathname in "initpaths".
470  * The program is invoked with one argument containing the boot flags.
471  */
472 void
473 start_init(arg)
474 	void *arg;
475 {
476 	struct proc *p = arg;
477 	vaddr_t addr;
478 	struct sys_execve_args /* {
479 		syscallarg(char *) path;
480 		syscallarg(char **) argp;
481 		syscallarg(char **) envp;
482 	} */ args;
483 	int options, i, error;
484 	register_t retval[2];
485 	char flags[4], *flagsp;
486 	char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL;
487 
488 	initproc = p;
489 
490 	/*
491 	 * Now in process 1.
492 	 */
493 	check_console(p);
494 
495 	/*
496 	 * Need just enough stack to hold the faked-up "execve()" arguments.
497 	 */
498 #ifdef MACHINE_STACK_GROWS_UP
499 	addr = USRSTACK;
500 #else
501 	addr = USRSTACK - PAGE_SIZE;
502 #endif
503 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
504 	    NULL, UVM_UNKNOWN_OFFSET, 0,
505 	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
506 	    UVM_ADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)))
507 		panic("init: couldn't allocate argument space");
508 #ifdef MACHINE_STACK_GROWS_UP
509 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr + PAGE_SIZE;
510 #else
511 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
512 #endif
513 
514 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
515 #ifdef MACHINE_STACK_GROWS_UP
516 		ucp = (char *)addr;
517 #else
518 		ucp = (char *)(addr + PAGE_SIZE);
519 #endif
520 		/*
521 		 * Construct the boot flag argument.
522 		 */
523 		flagsp = flags;
524 		*flagsp++ = '-';
525 		options = 0;
526 
527 		if (boothowto & RB_SINGLE) {
528 			*flagsp++ = 's';
529 			options = 1;
530 		}
531 #ifdef notyet
532 		if (boothowto & RB_FASTBOOT) {
533 			*flagsp++ = 'f';
534 			options = 1;
535 		}
536 #endif
537 
538 		/*
539 		 * Move out the flags (arg 1), if necessary.
540 		 */
541 		if (options != 0) {
542 			*flagsp++ = '\0';
543 			i = flagsp - flags;
544 #ifdef DEBUG
545 			printf("init: copying out flags `%s' %d\n", flags, i);
546 #endif
547 #ifdef MACHINE_STACK_GROWS_UP
548 			arg1 = ucp;
549 			(void)copyout((caddr_t)flags, (caddr_t)ucp, i);
550 			ucp += i;
551 #else
552 			(void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
553 			arg1 = ucp;
554 #endif
555 		}
556 
557 		/*
558 		 * Move out the file name (also arg 0).
559 		 */
560 		i = strlen(path) + 1;
561 #ifdef DEBUG
562 		printf("init: copying out path `%s' %d\n", path, i);
563 #endif
564 #ifdef MACHINE_STACK_GROWS_UP
565 		arg0 = ucp;
566 		(void)copyout((caddr_t)path, (caddr_t)ucp, i);
567 		ucp += i;
568 		ucp = (caddr_t)ALIGN((u_long)ucp);
569 		uap = (char **)ucp + 3;
570 #else
571 		(void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
572 		arg0 = ucp;
573 		uap = (char **)((u_long)ucp & ~ALIGNBYTES);
574 #endif
575 
576 		/*
577 		 * Move out the arg pointers.
578 		 */
579 		(void)suword((caddr_t)--uap, 0);	/* terminator */
580 		if (options != 0)
581 			(void)suword((caddr_t)--uap, (long)arg1);
582 		(void)suword((caddr_t)--uap, (long)arg0);
583 
584 		/*
585 		 * Point at the arguments.
586 		 */
587 		SCARG(&args, path) = arg0;
588 		SCARG(&args, argp) = uap;
589 		SCARG(&args, envp) = NULL;
590 
591 		/*
592 		 * Now try to exec the program.  If can't for any reason
593 		 * other than it doesn't exist, complain.
594 		 */
595 		if ((error = sys_execve(p, &args, retval)) == 0)
596 			return;
597 		if (error != ENOENT)
598 			printf("exec %s: error %d\n", path, error);
599 	}
600 	printf("init: not found\n");
601 	panic("no init");
602 }
603 
604 void
605 start_update(arg)
606 	void *arg;
607 {
608 	sched_sync(curproc);
609 	/* NOTREACHED */
610 }
611 
612 void
613 start_cleaner(arg)
614 	void *arg;
615 {
616 	buf_daemon(curproc);
617 	/* NOTREACHED */
618 }
619 
620 void
621 start_reaper(arg)
622 	void *arg;
623 {
624 	reaper();
625 	/* NOTREACHED */
626 }
627 
628 #ifdef CRYPTO
629 void
630 start_crypto(arg)
631 	void *arg;
632 {
633 	crypto_thread();
634 	/* NOTREACHED */
635 }
636 #endif /* CRYPTO */
637