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