xref: /freebsd/sys/kern/init_main.c (revision 9c8b8baa38c9a8135d7602f127cb0c735010837d)
1 /*
2  * Copyright (c) 1995 Terrence R. Lambert
3  * All rights reserved.
4  *
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  * $Id: init_main.c,v 1.123 1999/06/30 15:33:32 peter Exp $
43  */
44 
45 #include "opt_devfs.h"
46 #include "opt_init_path.h"
47 
48 #include <sys/param.h>
49 #include <sys/file.h>
50 #include <sys/filedesc.h>
51 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/sysctl.h>
54 #include <sys/proc.h>
55 #include <sys/kthread.h>
56 #include <sys/resourcevar.h>
57 #include <sys/signalvar.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
60 #include <sys/sysent.h>
61 #include <sys/reboot.h>
62 #include <sys/sysproto.h>
63 #include <sys/vmmeter.h>
64 #include <sys/unistd.h>
65 #include <sys/malloc.h>
66 
67 #include <machine/cpu.h>
68 
69 #include <vm/vm.h>
70 #include <vm/vm_param.h>
71 #include <vm/vm_prot.h>
72 #include <sys/lock.h>
73 #include <vm/pmap.h>
74 #include <vm/vm_map.h>
75 #include <sys/user.h>
76 #include <sys/copyright.h>
77 
78 extern struct linker_set	sysinit_set;	/* XXX */
79 
80 extern void mi_startup __P((void *framep));
81 
82 /* Components of the first process -- never freed. */
83 static struct session session0;
84 static struct pgrp pgrp0;
85 struct	proc proc0;
86 static struct pcred cred0;
87 static struct procsig procsig0;
88 static struct filedesc0 filedesc0;
89 static struct plimit limit0;
90 static struct vmspace vmspace0;
91 struct	proc *initproc;
92 
93 int cmask = CMASK;
94 extern	struct user *proc0paddr;
95 
96 struct	vnode *rootvp;
97 int	boothowto = 0;		/* initialized so that it can be patched */
98 
99 struct	timeval boottime;
100 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
101     &boottime, timeval, "System boottime");
102 
103 /*
104  * Promiscuous argument pass for start_init()
105  *
106  * This is a kludge because we use a return from mi_startup() rather than a call
107  * to a new routine in locore.s to kick the kernel alive from locore.s.
108  */
109 static void	*init_framep;
110 
111 /*
112  * This ensures that there is at least one entry so that the sysinit_set
113  * symbol is not undefined.  A sybsystem ID of SI_SUB_DUMMY is never
114  * executed.
115  */
116 SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL)
117 
118 /*
119  * The sysinit table itself.  Items are checked off as the are run.
120  * If we want to register new sysinit types, add them to newsysinit.
121  */
122 struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items;
123 struct sysinit **newsysinit;
124 
125 /*
126  * Merge a new sysinit set into the current set, reallocating it if
127  * necessary.  This can only be called after malloc is running.
128  */
129 void
130 sysinit_add(set)
131 	struct sysinit **set;
132 {
133 	struct sysinit **newset;
134 	struct sysinit **sipp;
135 	struct sysinit **xipp;
136 	int count = 0;
137 
138 	if (newsysinit)
139 		for (sipp = newsysinit; *sipp; sipp++)
140 			count++;
141 	else
142 		for (sipp = sysinit; *sipp; sipp++)
143 			count++;
144 	for (sipp = set; *sipp; sipp++)
145 		count++;
146 	count++;		/* Trailing NULL */
147 	newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT);
148 	if (newset == NULL)
149 		panic("cannot malloc for sysinit");
150 	xipp = newset;
151 	if (newsysinit)
152 		for (sipp = newsysinit; *sipp; sipp++)
153 			*xipp++ = *sipp;
154 	else
155 		for (sipp = sysinit; *sipp; sipp++)
156 			*xipp++ = *sipp;
157 	for (sipp = set; *sipp; sipp++)
158 		*xipp++ = *sipp;
159 	*xipp = NULL;
160 	if (newsysinit)
161 		free(newsysinit, M_TEMP);
162 	newsysinit = newset;
163 }
164 
165 /*
166  * System startup; initialize the world, create process 0, mount root
167  * filesystem, and fork to create init and pagedaemon.  Most of the
168  * hard work is done in the lower-level initialization routines including
169  * startup(), which does memory initialization and autoconfiguration.
170  *
171  * This allows simple addition of new kernel subsystems that require
172  * boot time initialization.  It also allows substitution of subsystem
173  * (for instance, a scheduler, kernel profiler, or VM system) by object
174  * module.  Finally, it allows for optional "kernel threads".
175  */
176 void
177 mi_startup(framep)
178 	void *framep;
179 {
180 
181 	register struct sysinit **sipp;		/* system initialization*/
182 	register struct sysinit **xipp;		/* interior loop of sort*/
183 	register struct sysinit *save;		/* bubble*/
184 
185 	/*
186 	 * Copy the locore.s frame pointer for proc0, this is forked into
187 	 * all other processes.
188 	 */
189 	init_framep = framep;
190 
191 restart:
192 	/*
193 	 * Perform a bubble sort of the system initialization objects by
194 	 * their subsystem (primary key) and order (secondary key).
195 	 */
196 	for (sipp = sysinit; *sipp; sipp++) {
197 		for (xipp = sipp + 1; *xipp; xipp++) {
198 			if ((*sipp)->subsystem < (*xipp)->subsystem ||
199 			     ((*sipp)->subsystem == (*xipp)->subsystem &&
200 			      (*sipp)->order < (*xipp)->order))
201 				continue;	/* skip*/
202 			save = *sipp;
203 			*sipp = *xipp;
204 			*xipp = save;
205 		}
206 	}
207 
208 	/*
209 	 * Traverse the (now) ordered list of system initialization tasks.
210 	 * Perform each task, and continue on to the next task.
211 	 *
212 	 * The last item on the list is expected to be the scheduler,
213 	 * which will not return.
214 	 */
215 	for (sipp = sysinit; *sipp; sipp++) {
216 
217 		if ((*sipp)->subsystem == SI_SUB_DUMMY)
218 			continue;	/* skip dummy task(s)*/
219 
220 		if ((*sipp)->subsystem == SI_SUB_DONE)
221 			continue;
222 
223 		/* Call function */
224 		(*((*sipp)->func))((*sipp)->udata);
225 
226 		/* Check off the one we're just done */
227 		(*sipp)->subsystem = SI_SUB_DONE;
228 
229 		/* Check if we've installed more sysinit items via KLD */
230 		if (newsysinit != NULL) {
231 			if (sysinit != (struct sysinit **)sysinit_set.ls_items)
232 				free(sysinit, M_TEMP);
233 			sysinit = newsysinit;
234 			newsysinit = NULL;
235 			goto restart;
236 		}
237 	}
238 
239 	panic("Shouldn't get here!");
240 	/* NOTREACHED*/
241 }
242 
243 
244 /*
245  ***************************************************************************
246  ****
247  **** The following SYSINIT's belong elsewhere, but have not yet
248  **** been moved.
249  ****
250  ***************************************************************************
251  */
252 #ifdef OMIT
253 /*
254  * Handled by vfs_mountroot (bad idea) at this time... should be
255  * done the same as 4.4Lite2.
256  */
257 SYSINIT(swapinit, SI_SUB_SWAP, SI_ORDER_FIRST, swapinit, NULL)
258 #endif	/* OMIT*/
259 
260 static void print_caddr_t __P((void *data));
261 static void
262 print_caddr_t(data)
263 	void *data;
264 {
265 	printf("%s", (char *)data);
266 }
267 SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright)
268 
269 
270 /*
271  ***************************************************************************
272  ****
273  **** The two following SYSINT's are proc0 specific glue code.  I am not
274  **** convinced that they can not be safely combined, but their order of
275  **** operation has been maintained as the same as the original init_main.c
276  **** for right now.
277  ****
278  **** These probably belong in init_proc.c or kern_proc.c, since they
279  **** deal with proc0 (the fork template process).
280  ****
281  ***************************************************************************
282  */
283 /* ARGSUSED*/
284 static void proc0_init __P((void *dummy));
285 static void
286 proc0_init(dummy)
287 	void *dummy;
288 {
289 	register struct proc		*p;
290 	register struct filedesc0	*fdp;
291 	register unsigned i;
292 
293 	p = &proc0;
294 
295 	/*
296 	 * Initialize process and pgrp structures.
297 	 */
298 	procinit();
299 
300 	/*
301 	 * Initialize sleep queue hash table
302 	 */
303 	sleepinit();
304 
305 	/*
306 	 * additional VM structures
307 	 */
308 	vm_init2();
309 
310 	/*
311 	 * Create process 0 (the swapper).
312 	 */
313 	LIST_INSERT_HEAD(&allproc, p, p_list);
314 	p->p_pgrp = &pgrp0;
315 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
316 	LIST_INIT(&pgrp0.pg_members);
317 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
318 
319 	pgrp0.pg_session = &session0;
320 	session0.s_count = 1;
321 	session0.s_leader = p;
322 
323 	p->p_sysent = &aout_sysvec;
324 
325 	p->p_flag = P_INMEM | P_SYSTEM;
326 	p->p_stat = SRUN;
327 	p->p_nice = NZERO;
328 	p->p_rtprio.type = RTP_PRIO_NORMAL;
329 	p->p_rtprio.prio = 0;
330 
331 /*
332  * Link for kernel based threads
333  */
334 	p->p_peers = 0;
335 	p->p_leader = p;
336 
337 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
338 
339 	/* Create credentials. */
340 	cred0.p_refcnt = 1;
341 	p->p_cred = &cred0;
342 	p->p_ucred = crget();
343 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
344 
345 	/* Don't jail it */
346 	p->p_prison = 0;
347 
348 	/* Create procsig. */
349 	p->p_procsig = &procsig0;
350 	p->p_procsig->ps_refcnt = 1;
351 
352 	/* Create the file descriptor table. */
353 	fdp = &filedesc0;
354 	p->p_fd = &fdp->fd_fd;
355 	fdp->fd_fd.fd_refcnt = 1;
356 	fdp->fd_fd.fd_cmask = cmask;
357 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
358 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
359 	fdp->fd_fd.fd_nfiles = NDFILE;
360 
361 	/* Create the limits structures. */
362 	p->p_limit = &limit0;
363 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
364 		limit0.pl_rlimit[i].rlim_cur =
365 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
366 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
367 	    limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
368 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
369 	    limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
370 	i = ptoa(cnt.v_free_count);
371 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
372 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
373 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
374 	limit0.p_cpulimit = RLIM_INFINITY;
375 	limit0.p_refcnt = 1;
376 
377 
378 	/* Allocate a prototype map so we have something to fork. */
379 	pmap_pinit0(vmspace_pmap(&vmspace0));
380 	p->p_vmspace = &vmspace0;
381 	vmspace0.vm_refcnt = 1;
382 	vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
383 	    trunc_page(VM_MAXUSER_ADDRESS));
384 	vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0);
385 	p->p_addr = proc0paddr;				/* XXX */
386 
387 #ifdef cpu_set_init_frame
388 	/*
389 	 * proc0 needs to have a coherent frame base in its stack.
390 	 */
391 	cpu_set_init_frame(p, init_framep);			/* XXX! */
392 #endif
393 
394 	/*
395 	 * We continue to place resource usage info and signal
396 	 * actions in the user struct so they're pageable.
397 	 */
398 	p->p_stats = &p->p_addr->u_stats;
399 	p->p_sigacts = &p->p_addr->u_sigacts;
400 
401 	/*
402 	 * Charge root for one process.
403 	 */
404 	(void)chgproccnt(0, 1);
405 
406 	/*
407 	 * Initialize the current process pointer (curproc) before
408 	 * any possible traps/probes to simplify trap processing.
409 	 */
410 	SET_CURPROC(p);
411 
412 }
413 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL)
414 
415 /* ARGSUSED*/
416 static void proc0_post __P((void *dummy));
417 static void
418 proc0_post(dummy)
419 	void *dummy;
420 {
421 	struct timespec ts;
422 
423 	/*
424 	 * Now we can look at the time, having had a chance to verify the
425 	 * time from the file system.  Pretend that proc0 started now.
426 	 */
427 	microtime(&proc0.p_stats->p_start);
428 	proc0.p_runtime = 0;
429 	microuptime(&switchtime);
430 	switchticks = ticks;
431 
432 	/*
433 	 * Give the ``random'' number generator a thump.
434 	 * XXX: Does read_random() contain enough bits to be used here ?
435 	 */
436 	nanotime(&ts);
437 	srandom(ts.tv_sec ^ ts.tv_nsec);
438 
439 	/* Initialize signal state for process 0. */
440 	siginit(&proc0);
441 }
442 SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL)
443 
444 
445 
446 
447 /*
448  ***************************************************************************
449  ****
450  **** The following SYSINIT's and glue code should be moved to the
451  **** respective files on a per subsystem basis.
452  ****
453  ***************************************************************************
454  */
455 
456 /* ARGSUSED */
457 static void root_conf __P((void *dummy));
458 static void
459 root_conf(dummy)
460 	void *dummy;
461 {
462 	cpu_rootconf();
463 }
464 SYSINIT(root_conf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, root_conf, NULL)
465 
466 /* ARGSUSED*/
467 static void xxx_vfs_root_fdtab __P((void *dummy));
468 static void
469 xxx_vfs_root_fdtab(dummy)
470 	void *dummy;
471 {
472 	register struct filedesc0	*fdp = &filedesc0;
473 
474 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
475 	if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
476 		panic("cannot find root vnode");
477 	fdp->fd_fd.fd_cdir = rootvnode;
478 	VREF(fdp->fd_fd.fd_cdir);
479 	VOP_UNLOCK(rootvnode, 0, &proc0);
480 	fdp->fd_fd.fd_rdir = rootvnode;
481 }
482 SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL)
483 
484 
485 /*
486  ***************************************************************************
487  ****
488  **** The following code probably belongs in another file, like
489  **** kern/init_init.c.  It is here for two reasons only:
490  ****
491  ****	1)	This code returns to startup the system; this is
492  ****		abnormal for a kernel thread.
493  ****	2)	This code promiscuously uses init_frame
494  ****
495  ***************************************************************************
496  */
497 
498 extern void prepare_usermode __P((void));
499 static void create_init __P((const void *dummy));
500 static void start_init __P((void *dummy));
501 SYSINIT(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, create_init, NULL)
502 
503 /*
504  * Like kthread_create(), but runs in it's own address space.
505  */
506 static void
507 create_init(udata)
508 	const void *udata;
509 {
510 	int error;
511 
512 	error = fork1(&proc0, RFFDG | RFPROC, &initproc);
513 	if (error)
514 		panic("cannot fork init: %d\n", error);
515 	initproc->p_flag |= P_INMEM | P_SYSTEM;
516 	cpu_set_fork_handler(initproc, start_init, NULL);
517 }
518 
519 /*
520  * List of paths to try when searching for "init".
521  */
522 static char init_path[MAXPATHLEN] =
523 #ifdef	INIT_PATH
524     __XSTRING(INIT_PATH);
525 #else
526     "/sbin/init:/sbin/oinit:/sbin/init.bak:/stand/sysinstall";
527 #endif
528 SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, "");
529 
530 /*
531  * Start the initial user process; try exec'ing each pathname in init_path.
532  * The program is invoked with one argument containing the boot flags.
533  */
534 static void
535 start_init(dummy)
536 	void *dummy;
537 {
538 	vm_offset_t addr;
539 	struct execve_args args;
540 	int options, error;
541 	char *var, *path, *next, *s;
542 	char *ucp, **uap, *arg0, *arg1;
543 	struct proc *p;
544 
545 	p = curproc;
546 
547 	/*
548 	 * Need just enough stack to hold the faked-up "execve()" arguments.
549 	 */
550 	addr = trunc_page(USRSTACK - PAGE_SIZE);
551 	if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE,
552 			FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
553 		panic("init: couldn't allocate argument space");
554 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
555 	p->p_vmspace->vm_ssize = 1;
556 
557 	if ((var = getenv("init_path")) != NULL) {
558 		strncpy(init_path, var, sizeof init_path);
559 		init_path[sizeof init_path - 1] = 0;
560 	}
561 
562 	for (path = init_path; *path != '\0'; path = next) {
563 		while (*path == ':')
564 			path++;
565 		if (*path == '\0')
566 			break;
567 		for (next = path; *next != '\0' && *next != ':'; next++)
568 			/* nothing */ ;
569 		if (bootverbose)
570 			printf("start_init: trying %.*s\n", (int)(next - path),
571 			    path);
572 
573 		/*
574 		 * Move out the boot flag argument.
575 		 */
576 		options = 0;
577 		ucp = (char *)USRSTACK;
578 		(void)subyte(--ucp, 0);		/* trailing zero */
579 		if (boothowto & RB_SINGLE) {
580 			(void)subyte(--ucp, 's');
581 			options = 1;
582 		}
583 #ifdef notyet
584                 if (boothowto & RB_FASTBOOT) {
585 			(void)subyte(--ucp, 'f');
586 			options = 1;
587 		}
588 #endif
589 
590 #ifdef BOOTCDROM
591 		(void)subyte(--ucp, 'C');
592 		options = 1;
593 #endif
594 		if (options == 0)
595 			(void)subyte(--ucp, '-');
596 		(void)subyte(--ucp, '-');		/* leading hyphen */
597 		arg1 = ucp;
598 
599 		/*
600 		 * Move out the file name (also arg 0).
601 		 */
602 		(void)subyte(--ucp, 0);
603 		for (s = next - 1; s >= path; s--)
604 			(void)subyte(--ucp, *s);
605 		arg0 = ucp;
606 
607 		/*
608 		 * Move out the arg pointers.
609 		 */
610 		uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1));
611 		(void)suword((caddr_t)--uap, (long)0);	/* terminator */
612 		(void)suword((caddr_t)--uap, (long)(intptr_t)arg1);
613 		(void)suword((caddr_t)--uap, (long)(intptr_t)arg0);
614 
615 		/*
616 		 * Point at the arguments.
617 		 */
618 		args.fname = arg0;
619 		args.argv = uap;
620 		args.envv = NULL;
621 
622 		/*
623 		 * Now try to exec the program.  If can't for any reason
624 		 * other than it doesn't exist, complain.
625 		 *
626 		 * Otherwise, return via the fork trampoline all the way
627 		 * to user mode as init!
628 		 */
629 		if ((error = execve(p, &args)) == 0) {
630 			prepare_usermode();
631 			return;
632 		}
633 		if (error != ENOENT)
634 			printf("exec %.*s: error %d\n", (int)(next - path),
635 			    path, error);
636 	}
637 	printf("init: not found\n");
638 	panic("no init");
639 }
640