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