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