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