xref: /csrg-svn/sys/kern/init_main.c (revision 69348)
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.14 (Berkeley) 05/10/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 	mountlist.cqh_first->mnt_flag |= MNT_ROOTFS;
228 
229 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
230 	if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
231 		panic("cannot find root vnode");
232 	fdp->fd_fd.fd_cdir = rootvnode;
233 	VREF(fdp->fd_fd.fd_cdir);
234 	VOP_UNLOCK(rootvnode);
235 	fdp->fd_fd.fd_rdir = NULL;
236 	swapinit();
237 
238 	/*
239 	 * Now can look at time, having had a chance to verify the time
240 	 * from the file system.  Reset p->p_rtime as it may have been
241 	 * munched in mi_switch() after the time got set.
242 	 */
243 	p->p_stats->p_start = runtime = mono_time = boottime = time;
244 	p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
245 
246 	/* Initialize signal state for process 0. */
247 	siginit(p);
248 
249 	/* Create process 1 (init(8)). */
250 	if (fork(p, NULL, rval))
251 		panic("fork init");
252 	if (rval[1]) {
253 		start_init(curproc, framep);
254 		return;
255 	}
256 
257 	/* Create process 2 (the pageout daemon). */
258 	if (fork(p, NULL, rval))
259 		panic("fork pager");
260 	if (rval[1]) {
261 		/*
262 		 * Now in process 2.
263 		 */
264 		p = curproc;
265 		pageproc = p;
266 		p->p_flag |= P_INMEM | P_SYSTEM;	/* XXX */
267 		bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
268 		vm_pageout();
269 		/* NOTREACHED */
270 	}
271 
272 	/* The scheduler is an infinite loop. */
273 	scheduler();
274 	/* NOTREACHED */
275 }
276 
277 /*
278  * List of paths to try when searching for "init".
279  */
280 static char *initpaths[] = {
281 	"/sbin/init",
282 	"/sbin/oinit",
283 	"/sbin/init.bak",
284 	NULL,
285 };
286 
287 /*
288  * Start the initial user process; try exec'ing each pathname in "initpaths".
289  * The program is invoked with one argument containing the boot flags.
290  */
291 static void
292 start_init(p, framep)
293 	struct proc *p;
294 	void *framep;
295 {
296 	vm_offset_t addr;
297 	struct execve_args /* {
298 		syscallarg(char *) path;
299 		syscallarg(char **) argp;
300 		syscallarg(char **) envp;
301 	} */ args;
302 	int options, i, error;
303 	register_t retval[2];
304 	char flags[4] = "-", *flagsp;
305 	char **pathp, *path, *ucp, **uap, *arg0, *arg1;
306 
307 	initproc = p;
308 
309 	/*
310 	 * We need to set the system call frame as if we were entered through
311 	 * a syscall() so that when we call execve() below, it will be able
312 	 * to set the entry point (see setregs) when it tries to exec.  The
313 	 * startup code in "locore.s" has allocated space for the frame and
314 	 * passed a pointer to that space as main's argument.
315 	 */
316 	cpu_set_init_frame(p, framep);
317 
318 	/*
319 	 * Need just enough stack to hold the faked-up "execve()" arguments.
320 	 */
321 	addr = trunc_page(VM_MAX_ADDRESS - PAGE_SIZE);
322 	if (vm_allocate(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, FALSE) != 0)
323 		panic("init: couldn't allocate argument space");
324 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
325 
326 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
327 		/*
328 		 * Construct the boot flag argument.
329 		 */
330 		options = 0;
331 		flagsp = flags + 1;
332 		ucp = (char *)USRSTACK;
333 		if (boothowto & RB_SINGLE) {
334 			*flagsp++ = 's';
335 			options = 1;
336 		}
337 #ifdef notyet
338                 if (boothowto & RB_FASTBOOT) {
339 			*flagsp++ = 'f';
340 			options = 1;
341 		}
342 #endif
343 		/*
344 		 * Move out the flags (arg 1), if necessary.
345 		 */
346 		if (options != 0) {
347 			*flagsp++ = '\0';
348 			i = flagsp - flags;
349 			(void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
350 			arg1 = ucp;
351 		}
352 
353 		/*
354 		 * Move out the file name (also arg 0).
355 		 */
356 		i = strlen(path) + 1;
357 		(void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
358 		arg0 = ucp;
359 
360 		/*
361 		 * Move out the arg pointers.
362 		 */
363 		uap = (char **)((long)ucp & ~ALIGNBYTES);
364 		(void)suword((caddr_t)--uap, 0);	/* terminator */
365 		if (options != 0)
366 			(void)suword((caddr_t)--uap, (long)arg1);
367 		(void)suword((caddr_t)--uap, (long)arg0);
368 
369 		/*
370 		 * Point at the arguments.
371 		 */
372 		SCARG(&args, path) = arg0;
373 		SCARG(&args, argp) = uap;
374 		SCARG(&args, envp) = NULL;
375 
376 		/*
377 		 * Now try to exec the program.  If can't for any reason
378 		 * other than it doesn't exist, complain.
379 		 */
380 		if ((error = execve(p, &args, retval)) == 0)
381 			return;
382 		if (error != ENOENT)
383 			printf("exec %s: error %d\n", path, error);
384 	}
385 	printf("init: not found\n");
386 	panic("no init");
387 }
388