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