xref: /original-bsd/sys/kern/init_main.c (revision 0ac4996f)
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.16 (Berkeley) 05/14/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 struct pdevinit pdevinit[];
87 	extern void roundrobin __P((void *));
88 	extern void schedcpu __P((void *));
89 
90 	/*
91 	 * Initialize the current process pointer (curproc) before
92 	 * any possible traps/probes to simplify trap processing.
93 	 */
94 	p = &proc0;
95 	curproc = p;
96 	/*
97 	 * Attempt to find console and initialize
98 	 * in case of early panic or other messages.
99 	 */
100 	consinit();
101 	printf(copyright);
102 
103 	vm_mem_init();
104 	kmeminit();
105 	cpu_startup();
106 
107 	/*
108 	 * Initialize process and pgrp structures.
109 	 */
110 	procinit();
111 
112 	/*
113 	 * Create process 0 (the swapper).
114 	 */
115 	LIST_INSERT_HEAD(&allproc, p, p_list);
116 	p->p_pgrp = &pgrp0;
117 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
118 	LIST_INIT(&pgrp0.pg_members);
119 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
120 
121 	pgrp0.pg_session = &session0;
122 	session0.s_count = 1;
123 	session0.s_leader = p;
124 
125 	p->p_flag = P_INMEM | P_SYSTEM;
126 	p->p_stat = SRUN;
127 	p->p_nice = NZERO;
128 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
129 
130 	/* Create credentials. */
131 	cred0.p_refcnt = 1;
132 	p->p_cred = &cred0;
133 	p->p_ucred = crget();
134 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
135 
136 	/* Create the file descriptor table. */
137 	fdp = &filedesc0;
138 	p->p_fd = &fdp->fd_fd;
139 	fdp->fd_fd.fd_refcnt = 1;
140 	fdp->fd_fd.fd_cmask = cmask;
141 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
142 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
143 	fdp->fd_fd.fd_nfiles = NDFILE;
144 
145 	/* Create the limits structures. */
146 	p->p_limit = &limit0;
147 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
148 		limit0.pl_rlimit[i].rlim_cur =
149 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
150 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
151 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
152 	i = ptoa(cnt.v_free_count);
153 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
154 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
155 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
156 	limit0.p_refcnt = 1;
157 
158 	/* Allocate a prototype map so we have something to fork. */
159 	p->p_vmspace = &vmspace0;
160 	vmspace0.vm_refcnt = 1;
161 	pmap_pinit(&vmspace0.vm_pmap);
162 	vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
163 	    trunc_page(VM_MAX_ADDRESS), TRUE);
164 	vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
165 	p->p_addr = proc0paddr;				/* XXX */
166 
167 	/*
168 	 * We continue to place resource usage info and signal
169 	 * actions in the user struct so they're pageable.
170 	 */
171 	p->p_stats = &p->p_addr->u_stats;
172 	p->p_sigacts = &p->p_addr->u_sigacts;
173 
174 	/*
175 	 * Charge root for one process.
176 	 */
177 	(void)chgproccnt(0, 1);
178 
179 	rqinit();
180 
181 	/* Configure virtual memory system, set vm rlimits. */
182 	vm_init_limits(p);
183 
184 	/* Initialize the file systems. */
185 	vfsinit();
186 
187 	/* Start real time and statistics clocks. */
188 	initclocks();
189 
190 	/* Initialize mbuf's. */
191 	mbinit();
192 
193 	/* Initialize clists. */
194 	clist_init();
195 
196 #ifdef SYSVSHM
197 	/* Initialize System V style shared memory. */
198 	shminit();
199 #endif
200 
201 	/* Attach pseudo-devices. */
202 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
203 		(*pdev->pdev_attach)(pdev->pdev_count);
204 
205 	/*
206 	 * Initialize protocols.  Block reception of incoming packets
207 	 * until everything is ready.
208 	 */
209 	s = splimp();
210 	ifinit();
211 	domaininit();
212 	splx(s);
213 
214 #ifdef GPROF
215 	/* Initialize kernel profiling. */
216 	kmstartup();
217 #endif
218 
219 	/* Kick off timeout driven events by calling first time. */
220 	roundrobin(NULL);
221 	schedcpu(NULL);
222 
223 	/* Mount the root file system. */
224 	if (vfs_mountroot())
225 		panic("cannot mount root");
226 	mountlist.cqh_first->mnt_flag |= MNT_ROOTFS;
227 
228 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
229 	if (VFS_ROOT(mountlist.cqh_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, 0, p);
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