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