init_main.c revision 1.74 1 /* $NetBSD: init_main.c,v 1.74 1995/01/18 06:14:43 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)init_main.c 8.9 (Berkeley) 1/21/94
41 */
42
43 #include <sys/param.h>
44 #include <sys/filedesc.h>
45 #include <sys/errno.h>
46 #include <sys/exec.h>
47 #include <sys/kernel.h>
48 #include <sys/mount.h>
49 #include <sys/map.h>
50 #include <sys/proc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/signalvar.h>
53 #include <sys/systm.h>
54 #include <sys/vnode.h>
55 #include <sys/conf.h>
56 #include <sys/buf.h>
57 #ifdef REAL_CLISTS
58 #include <sys/clist.h>
59 #endif
60 #include <sys/device.h>
61 #include <sys/protosw.h>
62 #include <sys/reboot.h>
63 #include <sys/user.h>
64
65 #include <sys/syscallargs.h>
66
67 #include <ufs/ufs/quota.h>
68
69 #include <machine/cpu.h>
70
71 #include <vm/vm.h>
72
73 char copyright[] =
74 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California. All rights reserved.\n\n";
75
76 /* Components of the first process -- never freed. */
77 struct session session0;
78 struct pgrp pgrp0;
79 struct proc proc0;
80 struct pcred cred0;
81 struct filedesc0 filedesc0;
82 struct plimit limit0;
83 struct vmspace vmspace0;
84 struct proc *curproc = &proc0;
85 struct proc *initproc, *pageproc;
86
87 int cmask = CMASK;
88 extern struct user *proc0paddr;
89
90 struct vnode *rootvp, *swapdev_vp;
91 int boothowto;
92 struct timeval boottime;
93 struct timeval runtime;
94
95 static void start_init __P((struct proc *p, void *framep));
96
97 /*
98 * System startup; initialize the world, create process 0, mount root
99 * filesystem, and fork to create init and pagedaemon. Most of the
100 * hard work is done in the lower-level initialization routines including
101 * startup(), which does memory initialization and autoconfiguration.
102 */
103 int
104 main(framep)
105 void *framep;
106 {
107 register struct proc *p;
108 register struct filedesc0 *fdp;
109 register struct pdevinit *pdev;
110 register int i;
111 int s;
112 register_t rval[2];
113 extern int (*mountroot) __P((void));
114 extern struct pdevinit pdevinit[];
115 extern void roundrobin __P((void *));
116 extern void schedcpu __P((void *));
117
118 /*
119 * Initialize the current process pointer (curproc) before
120 * any possible traps/probes to simplify trap processing.
121 */
122 p = &proc0;
123 curproc = p;
124 /*
125 * Attempt to find console and initialize
126 * in case of early panic or other messages.
127 */
128 consinit();
129 printf(copyright);
130
131 vm_mem_init();
132 kmeminit();
133 cpu_startup();
134
135 /*
136 * Initialize process and pgrp structures.
137 */
138 procinit();
139
140 /*
141 * Create process 0 (the swapper).
142 */
143 LIST_INSERT_HEAD(&allproc, p, p_list);
144 p->p_pgrp = &pgrp0;
145 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
146 LIST_INIT(&pgrp0.pg_members);
147 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
148
149 pgrp0.pg_session = &session0;
150 session0.s_count = 1;
151 session0.s_leader = p;
152
153 p->p_flag = P_INMEM | P_SYSTEM;
154 p->p_stat = SRUN;
155 p->p_nice = NZERO;
156 bcopy("swapper", p->p_comm, sizeof ("swapper"));
157
158 /* Create credentials. */
159 cred0.p_refcnt = 1;
160 p->p_cred = &cred0;
161 p->p_ucred = crget();
162 p->p_ucred->cr_ngroups = 1; /* group 0 */
163
164 /* Create the file descriptor table. */
165 fdp = &filedesc0;
166 p->p_fd = &fdp->fd_fd;
167 fdp->fd_fd.fd_refcnt = 1;
168 fdp->fd_fd.fd_cmask = cmask;
169 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
170 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
171 fdp->fd_fd.fd_nfiles = NDFILE;
172
173 /* Create the limits structures. */
174 p->p_limit = &limit0;
175 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
176 limit0.pl_rlimit[i].rlim_cur =
177 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
178 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
179 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
180 i = ptoa(cnt.v_free_count);
181 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
182 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
183 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
184 limit0.p_refcnt = 1;
185
186 /* Allocate a prototype map so we have something to fork. */
187 p->p_vmspace = &vmspace0;
188 vmspace0.vm_refcnt = 1;
189 pmap_pinit(&vmspace0.vm_pmap);
190 vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
191 trunc_page(VM_MAX_ADDRESS), TRUE);
192 vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
193 p->p_addr = proc0paddr; /* XXX */
194
195 /*
196 * We continue to place resource usage info and signal
197 * actions in the user struct so they're pageable.
198 */
199 p->p_stats = &p->p_addr->u_stats;
200 p->p_sigacts = &p->p_addr->u_sigacts;
201
202 /*
203 * Charge root for one process.
204 */
205 (void)chgproccnt(0, 1);
206
207 rqinit();
208
209 /* Configure virtual memory system, set vm rlimits. */
210 vm_init_limits(p);
211
212 /* Initialize the file systems. */
213 vfsinit();
214
215 /* Start real time and statistics clocks. */
216 initclocks();
217
218 /* Initialize mbuf's. */
219 mbinit();
220
221 #ifdef REAL_CLISTS
222 /* Initialize clists. */
223 clist_init();
224 #endif
225
226 #ifdef SYSVSHM
227 /* Initialize System V style shared memory. */
228 shminit();
229 #endif
230
231 #ifdef SYSVSEM
232 /* Initialize System V style semaphores. */
233 seminit();
234 #endif
235
236 #ifdef SYSVMSG
237 /* Initialize System V style message queues. */
238 msginit();
239 #endif
240
241 /* Attach pseudo-devices. */
242 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
243 (*pdev->pdev_attach)(pdev->pdev_count);
244
245 /*
246 * Initialize protocols. Block reception of incoming packets
247 * until everything is ready.
248 */
249 s = splimp();
250 ifinit();
251 domaininit();
252 splx(s);
253
254 #ifdef GPROF
255 /* Initialize kernel profiling. */
256 kmstartup();
257 #endif
258
259 /* Kick off timeout driven events by calling first time. */
260 roundrobin(NULL);
261 schedcpu(NULL);
262
263 /* Mount the root file system. */
264 if ((*mountroot)())
265 panic("cannot mount root");
266 mountlist.cqh_first->mnt_flag |= MNT_ROOTFS;
267 mountlist.cqh_first->mnt_op->vfs_refcount++;
268
269 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
270 if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
271 panic("cannot find root vnode");
272 fdp->fd_fd.fd_cdir = rootvnode;
273 VREF(fdp->fd_fd.fd_cdir);
274 VOP_UNLOCK(rootvnode);
275 fdp->fd_fd.fd_rdir = NULL;
276 swapinit();
277
278 /*
279 * Now can look at time, having had a chance to verify the time
280 * from the file system. Reset p->p_rtime as it may have been
281 * munched in mi_switch() after the time got set.
282 */
283 p->p_stats->p_start = runtime = mono_time = boottime = time;
284 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
285
286 /* Initialize signal state for process 0. */
287 siginit(p);
288
289 /* Create process 1 (init(8)). */
290 if (fork(p, NULL, rval))
291 panic("fork init");
292 if (rval[1]) {
293 start_init(curproc, framep);
294 return;
295 }
296
297 /* Create process 2 (the pageout daemon). */
298 if (fork(p, NULL, rval))
299 panic("fork pager");
300 if (rval[1]) {
301 /*
302 * Now in process 2.
303 */
304 p = curproc;
305 pageproc = p;
306 p->p_flag |= P_INMEM | P_SYSTEM; /* XXX */
307 bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
308 vm_pageout();
309 /* NOTREACHED */
310 }
311
312 /* The scheduler is an infinite loop. */
313 scheduler();
314 /* NOTREACHED */
315 }
316
317 /*
318 * List of paths to try when searching for "init".
319 */
320 static char *initpaths[] = {
321 "/sbin/init",
322 "/sbin/oinit",
323 "/sbin/init.bak",
324 NULL,
325 };
326
327 #ifdef DEBUG
328 /* Print things out while trying to start init if startinit_verbose != 0. */
329 int startinit_verbose = 0;
330 #endif
331
332 /*
333 * Start the initial user process; try exec'ing each pathname in "initpaths".
334 * The program is invoked with one argument containing the boot flags.
335 */
336 static void
337 start_init(p, framep)
338 struct proc *p;
339 void *framep;
340 {
341 vm_offset_t addr;
342 struct execve_args /* {
343 syscallarg(char *) path;
344 syscallarg(char **) argp;
345 syscallarg(char **) envp;
346 } */ args;
347 int options, i, error;
348 register_t retval[2];
349 char flags[4], *flagsp;
350 char **pathp, *path, *ucp, **uap, *arg0, *arg1;
351
352 initproc = p;
353
354 /*
355 * We need to set the system call frame as if we were entered through
356 * a syscall() so that when we call execve() below, it will be able
357 * to set the entry point (see setregs) when it tries to exec. The
358 * startup code in "locore.s" has allocated space for the frame and
359 * passed a pointer to that space as main's argument.
360 */
361 cpu_set_init_frame(p, framep);
362
363 /*
364 * Need just enough stack to hold the faked-up "execve()" arguments.
365 */
366 addr = USRSTACK - PAGE_SIZE;
367 if (vm_allocate(&p->p_vmspace->vm_map, &addr, (vm_size_t)PAGE_SIZE,
368 FALSE) != 0)
369 panic("init: couldn't allocate argument space");
370 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
371
372 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
373 ucp = (char *)(addr + PAGE_SIZE);
374
375 /*
376 * Construct the boot flag argument.
377 */
378 flagsp = flags;
379 *flagsp++ = '-';
380 options = 0;
381
382 if (boothowto & RB_SINGLE) {
383 *flagsp++ = 's';
384 options = 1;
385 }
386 #ifdef notyet
387 if (boothowto & RB_FASTBOOT) {
388 *flagsp++ = 'f';
389 options = 1;
390 }
391 #endif
392
393 /*
394 * Move out the flags (arg 1), if necessary.
395 */
396 if (options != 0) {
397 *flagsp++ = '\0';
398 i = flagsp - flags;
399 #ifdef DEBUG
400 if (startinit_verbose)
401 printf("init: copying out flags `%s' %d\n",
402 flags, i);
403 #endif
404 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
405 arg1 = ucp;
406 }
407
408 /*
409 * Move out the file name (also arg 0).
410 */
411 i = strlen(path) + 1;
412 #ifdef DEBUG
413 if (startinit_verbose)
414 printf("init: copying out path `%s' %d\n", path, i);
415 #endif
416 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
417 arg0 = ucp;
418
419 /*
420 * Move out the arg pointers.
421 */
422 uap = (char **)((long)ucp & ~ALIGNBYTES);
423 (void)suword((caddr_t)--uap, 0); /* terminator */
424 if (options != 0)
425 (void)suword((caddr_t)--uap, (long)arg1);
426 (void)suword((caddr_t)--uap, (long)arg0);
427
428 /*
429 * Point at the arguments.
430 */
431 SCARG(&args, path) = arg0;
432 SCARG(&args, argp) = uap;
433 SCARG(&args, envp) = NULL;
434
435 /*
436 * Now try to exec the program. If can't for any reason
437 * other than it doesn't exist, complain.
438 */
439 if ((error = execve(p, &args, retval)) == 0)
440 return;
441 #ifdef DEBUG
442 if (error != ENOENT || startinit_verbose)
443 #else
444 if (error != ENOENT)
445 #endif
446 printf("exec %s: error %d\n", path, error);
447 }
448 printf("init: not found\n");
449 panic("no init");
450 }
451