init_main.c revision 1.112 1 /* $NetBSD: init_main.c,v 1.112 1998/01/07 00:41:43 thorpej 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) 1996, 1997, 1998
100 The NetBSD Foundation, Inc. All rights reserved.
101 Copyright (c) 1982, 1986, 1989, 1991, 1993
102 The Regents of the University of California. All rights reserved.
103
104 ";
105
106 /* Components of the first process -- never freed. */
107 struct session session0;
108 struct pgrp pgrp0;
109 struct proc proc0;
110 struct pcred cred0;
111 struct filedesc0 filedesc0;
112 struct plimit limit0;
113 struct vmspace vmspace0;
114 struct proc *curproc = &proc0;
115 struct proc *initproc;
116
117 int cmask = CMASK;
118 extern struct user *proc0paddr;
119
120 struct vnode *rootvp, *swapdev_vp;
121 int boothowto;
122 struct timeval boottime;
123 struct timeval runtime;
124
125 static void check_console __P((struct proc *p));
126 static void start_init __P((struct proc *));
127 static void start_pagedaemon __P((struct proc *));
128 void main __P((void *));
129
130 extern char sigcode[], esigcode[];
131 #ifdef SYSCALL_DEBUG
132 extern char *syscallnames[];
133 #endif
134
135 struct emul emul_netbsd = {
136 "netbsd",
137 NULL,
138 sendsig,
139 SYS_syscall,
140 SYS_MAXSYSCALL,
141 sysent,
142 #ifdef SYSCALL_DEBUG
143 syscallnames,
144 #else
145 NULL,
146 #endif
147 0,
148 copyargs,
149 setregs,
150 sigcode,
151 esigcode,
152 };
153
154 /*
155 * System startup; initialize the world, create process 0, mount root
156 * filesystem, and fork to create init and pagedaemon. Most of the
157 * hard work is done in the lower-level initialization routines including
158 * startup(), which does memory initialization and autoconfiguration.
159 */
160 void
161 main(framep)
162 void *framep; /* XXX should go away */
163 {
164 struct proc *p, *p2;
165 struct pdevinit *pdev;
166 int i, s, error;
167 extern struct pdevinit pdevinit[];
168 extern void roundrobin __P((void *));
169 extern void schedcpu __P((void *));
170 extern void disk_init __P((void));
171 #if defined(NFSSERVER) || defined(NFS)
172 extern void nfs_init __P((void));
173 #endif
174
175 /*
176 * Initialize the current process pointer (curproc) before
177 * any possible traps/probes to simplify trap processing.
178 */
179 p = &proc0;
180 curproc = p;
181 /*
182 * Attempt to find console and initialize
183 * in case of early panic or other messages.
184 */
185 consinit();
186 printf(copyright);
187
188 vm_mem_init();
189 kmeminit();
190 disk_init(); /* must come before autoconfiguration */
191 tty_init(); /* initialise tty list */
192 #if NRND > 0
193 rnd_init();
194 #endif
195 config_init(); /* init autoconfiguration data structures */
196 cpu_startup();
197
198 /*
199 * Initialize process and pgrp structures.
200 */
201 procinit();
202
203 /*
204 * Create process 0 (the swapper).
205 */
206 LIST_INSERT_HEAD(&allproc, p, p_list);
207 p->p_pgrp = &pgrp0;
208 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
209 LIST_INIT(&pgrp0.pg_members);
210 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
211
212 pgrp0.pg_session = &session0;
213 session0.s_count = 1;
214 session0.s_leader = p;
215
216 p->p_flag = P_INMEM | P_SYSTEM;
217 p->p_stat = SRUN;
218 p->p_nice = NZERO;
219 p->p_emul = &emul_netbsd;
220 bcopy("swapper", p->p_comm, sizeof ("swapper"));
221
222 /* Create credentials. */
223 cred0.p_refcnt = 1;
224 p->p_cred = &cred0;
225 p->p_ucred = crget();
226 p->p_ucred->cr_ngroups = 1; /* group 0 */
227
228 /* Create the file descriptor table. */
229 p->p_fd = &filedesc0.fd_fd;
230 fdinit1(&filedesc0);
231
232 /* Create the limits structures. */
233 p->p_limit = &limit0;
234 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
235 limit0.pl_rlimit[i].rlim_cur =
236 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
237 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
238 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
239 i = ptoa(cnt.v_free_count);
240 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
241 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
242 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
243 limit0.p_refcnt = 1;
244
245 /* Allocate a prototype map so we have something to fork. */
246 p->p_vmspace = vmspace_alloc(round_page(VM_MIN_ADDRESS),
247 trunc_page(VM_MAX_ADDRESS), TRUE);
248
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 and dump devices. */
323 cpu_rootconf();
324 cpu_dumpconf();
325
326 /* Mount the root file system. */
327 do {
328 domountroothook();
329 if ((error = vfs_mountroot())) {
330 printf("cannot mount root, error = %d\n", error);
331 boothowto |= RB_ASKNAME;
332 setroot(root_device,
333 (rootdev != NODEV) ? DISKPART(rootdev) : 0, NULL);
334 }
335 } while (error != 0);
336 mountroothook_destroy();
337
338 mountlist.cqh_first->mnt_flag |= MNT_ROOTFS;
339 mountlist.cqh_first->mnt_op->vfs_refcount++;
340
341 /*
342 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
343 * reference it.
344 */
345 if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
346 panic("cannot find root vnode");
347 filedesc0.fd_fd.fd_cdir = rootvnode;
348 VREF(filedesc0.fd_fd.fd_cdir);
349 VOP_UNLOCK(rootvnode);
350 filedesc0.fd_fd.fd_rdir = NULL;
351 swapinit();
352
353 /*
354 * Now can look at time, having had a chance to verify the time
355 * from the file system. Reset p->p_rtime as it may have been
356 * munched in mi_switch() after the time got set.
357 */
358 p->p_stats->p_start = runtime = mono_time = boottime = time;
359 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
360
361 /* Initialize signal state for process 0. */
362 siginit(p);
363
364 /* Create process 1 (init(8)). */
365 if (fork1(p, 0, NULL, &p2))
366 panic("fork init");
367 cpu_set_kpc(p2, start_init);
368
369 /* Create process 2 (the pageout daemon). */
370 if (fork1(p, 0, NULL, &p2))
371 panic("fork pager");
372 cpu_set_kpc(p2, start_pagedaemon);
373
374 /* The scheduler is an infinite loop. */
375 scheduler();
376 /* NOTREACHED */
377 }
378
379 static void
380 check_console(p)
381 struct proc *p;
382 {
383 struct nameidata nd;
384 int error;
385
386 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
387 error = namei(&nd);
388 if (error == 0)
389 vrele(nd.ni_vp);
390 else if (error == ENOENT)
391 printf("warning: no /dev/console\n");
392 else
393 printf("warning: lookup /dev/console: error %d\n", error);
394 }
395
396 /*
397 * List of paths to try when searching for "init".
398 */
399 static char *initpaths[] = {
400 "/sbin/init",
401 "/sbin/oinit",
402 "/sbin/init.bak",
403 NULL,
404 };
405
406 /*
407 * Start the initial user process; try exec'ing each pathname in "initpaths".
408 * The program is invoked with one argument containing the boot flags.
409 */
410 static void
411 start_init(p)
412 struct proc *p;
413 {
414 vm_offset_t addr;
415 struct sys_execve_args /* {
416 syscallarg(const char *) path;
417 syscallarg(char * const *) argp;
418 syscallarg(char * const *) envp;
419 } */ args;
420 int options, i, error;
421 register_t retval[2];
422 char flags[4], *flagsp;
423 char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL;
424
425 /*
426 * Now in process 1.
427 */
428 initproc = p;
429
430 /*
431 * This is not the right way to do this. We really should
432 * hand-craft a descriptor onto /dev/console to hand to init,
433 * but that's a _lot_ more work, and the benefit from this easy
434 * hack makes up for the "good is the enemy of the best" effect.
435 */
436 check_console(p);
437
438 /*
439 * Need just enough stack to hold the faked-up "execve()" arguments.
440 */
441 addr = USRSTACK - PAGE_SIZE;
442 if (vm_allocate(&p->p_vmspace->vm_map, &addr, (vm_size_t)PAGE_SIZE,
443 FALSE) != 0)
444 panic("init: couldn't allocate argument space");
445 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
446
447 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
448 ucp = (char *)(addr + PAGE_SIZE);
449
450 /*
451 * Construct the boot flag argument.
452 */
453 flagsp = flags;
454 *flagsp++ = '-';
455 options = 0;
456
457 if (boothowto & RB_SINGLE) {
458 *flagsp++ = 's';
459 options = 1;
460 }
461 #ifdef notyet
462 if (boothowto & RB_FASTBOOT) {
463 *flagsp++ = 'f';
464 options = 1;
465 }
466 #endif
467
468 /*
469 * Move out the flags (arg 1), if necessary.
470 */
471 if (options != 0) {
472 *flagsp++ = '\0';
473 i = flagsp - flags;
474 #ifdef DEBUG
475 printf("init: copying out flags `%s' %d\n", flags, i);
476 #endif
477 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
478 arg1 = ucp;
479 }
480
481 /*
482 * Move out the file name (also arg 0).
483 */
484 i = strlen(path) + 1;
485 #ifdef DEBUG
486 printf("init: copying out path `%s' %d\n", path, i);
487 #endif
488 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
489 arg0 = ucp;
490
491 /*
492 * Move out the arg pointers.
493 */
494 uap = (char **)((long)ucp & ~ALIGNBYTES);
495 (void)suword((caddr_t)--uap, 0); /* terminator */
496 if (options != 0)
497 (void)suword((caddr_t)--uap, (long)arg1);
498 (void)suword((caddr_t)--uap, (long)arg0);
499
500 /*
501 * Point at the arguments.
502 */
503 SCARG(&args, path) = arg0;
504 SCARG(&args, argp) = uap;
505 SCARG(&args, envp) = NULL;
506
507 /*
508 * Now try to exec the program. If can't for any reason
509 * other than it doesn't exist, complain.
510 */
511 error = sys_execve(p, &args, retval);
512 if (error == 0 || error == EJUSTRETURN)
513 return;
514 if (error != ENOENT)
515 printf("exec %s: error %d\n", path, error);
516 }
517 printf("init: not found\n");
518 panic("no init");
519 }
520
521 static void
522 start_pagedaemon(p)
523 struct proc *p;
524 {
525
526 /*
527 * Now in process 2.
528 */
529 p->p_flag |= P_INMEM | P_SYSTEM; /* XXX */
530 bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
531 vm_pageout();
532 /* NOTREACHED */
533 }
534