xref: /original-bsd/sys/kern/init_main.c (revision 27393bdf)
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.11 (Berkeley) 02/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 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 **pathp, *path, *ucp, **uap, *arg0, *arg1;
304 
305 	initproc = p;
306 
307 	/*
308 	 * We need to set the system call frame as if we were entered through
309 	 * a syscall() so that when we call execve() below, it will be able
310 	 * to set the entry point (see setregs) when it tries to exec.  The
311 	 * startup code in "locore.s" has allocated space for the frame and
312 	 * passed a pointer to that space as main's argument.
313 	 */
314 	cpu_set_init_frame(p, framep);
315 
316 	/*
317 	 * Need just enough stack to hold the faked-up "execve()" arguments.
318 	 */
319 	addr = trunc_page(VM_MAX_ADDRESS - PAGE_SIZE);
320 	if (vm_allocate(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, FALSE) != 0)
321 		panic("init: couldn't allocate argument space");
322 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
323 
324 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
325 		/*
326 		 * Move out the boot flag argument.
327 		 */
328 		options = 0;
329 		ucp = (char *)USRSTACK;
330 		(void)subyte(--ucp, 0);		/* trailing zero */
331 		if (boothowto & RB_SINGLE) {
332 			(void)subyte(--ucp, 's');
333 			options = 1;
334 		}
335 #ifdef notyet
336                 if (boothowto & RB_FASTBOOT) {
337 			(void)subyte(--ucp, 'f');
338 			options = 1;
339 		}
340 #endif
341 		if (options == 0)
342 			(void)subyte(--ucp, '-');
343 		(void)subyte(--ucp, '-');		/* leading hyphen */
344 		arg1 = ucp;
345 
346 		/*
347 		 * Move out the file name (also arg 0).
348 		 */
349 		for (i = strlen(path) + 1; i >= 0; i--)
350 			(void)subyte(--ucp, path[i]);
351 		arg0 = ucp;
352 
353 		/*
354 		 * Move out the arg pointers.
355 		 */
356 		uap = (char **)((long)ucp & ~ALIGNBYTES);
357 		(void)suword((caddr_t)--uap, 0);	/* terminator */
358 		(void)suword((caddr_t)--uap, (long)arg1);
359 		(void)suword((caddr_t)--uap, (long)arg0);
360 
361 		/*
362 		 * Point at the arguments.
363 		 */
364 		SCARG(&args, path) = arg0;
365 		SCARG(&args, argp) = uap;
366 		SCARG(&args, envp) = NULL;
367 
368 		/*
369 		 * Now try to exec the program.  If can't for any reason
370 		 * other than it doesn't exist, complain.
371 		 */
372 		if ((error = execve(p, &args, retval)) == 0)
373 			return;
374 		if (error != ENOENT)
375 			printf("exec %s: error %d\n", path, error);
376 	}
377 	printf("init: not found\n");
378 	panic("no init");
379 }
380