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