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