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