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