init_main.c revision 1.61 1 /* NetBSD $Id: init_main.c,v 1.61 1994/07/03 11:45:41 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 <ufs/ufs/quota.h>
66
67 #include <machine/cpu.h>
68
69 #include <vm/vm.h>
70
71 char copyright[] =
72 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California. All rights reserved.\n\n";
73
74 /* Components of the first process -- never freed. */
75 struct session session0;
76 struct pgrp pgrp0;
77 struct proc proc0;
78 struct pcred cred0;
79 struct filedesc0 filedesc0;
80 struct plimit limit0;
81 struct vmspace vmspace0;
82 struct proc *curproc = &proc0;
83 struct proc *initproc, *pageproc;
84
85 int cmask = CMASK;
86 extern struct user *proc0paddr;
87
88 struct vnode *rootvp, *swapdev_vp;
89 int boothowto;
90 struct timeval boottime;
91 struct timeval runtime;
92
93 static void start_init __P((struct proc *p, void *framep));
94
95 /*
96 * System startup; initialize the world, create process 0, mount root
97 * filesystem, and fork to create init and pagedaemon. Most of the
98 * hard work is done in the lower-level initialization routines including
99 * startup(), which does memory initialization and autoconfiguration.
100 */
101 main(framep)
102 void *framep;
103 {
104 register struct proc *p;
105 register struct filedesc0 *fdp;
106 register struct pdevinit *pdev;
107 register int i;
108 int s, rval[2];
109 extern int (*mountroot) __P((void));
110 extern struct pdevinit pdevinit[];
111 extern void roundrobin __P((void *));
112 extern void schedcpu __P((void *));
113
114 /*
115 * Initialize the current process pointer (curproc) before
116 * any possible traps/probes to simplify trap processing.
117 */
118 p = &proc0;
119 curproc = p;
120 /*
121 * Attempt to find console and initialize
122 * in case of early panic or other messages.
123 */
124 consinit();
125 printf(copyright);
126
127 vm_mem_init();
128 kmeminit();
129 cpu_startup();
130
131 /*
132 * Create process 0 (the swapper).
133 */
134 allproc = (volatile struct proc *)p;
135 p->p_prev = (struct proc **)&allproc;
136 p->p_pgrp = &pgrp0;
137 pgrphash[0] = &pgrp0;
138 pgrp0.pg_mem = p;
139 pgrp0.pg_session = &session0;
140 session0.s_count = 1;
141 session0.s_leader = p;
142
143 p->p_flag = P_INMEM | P_SYSTEM;
144 p->p_stat = SRUN;
145 p->p_nice = NZERO;
146 bcopy("swapper", p->p_comm, sizeof ("swapper"));
147
148 /* Create credentials. */
149 cred0.p_refcnt = 1;
150 p->p_cred = &cred0;
151 p->p_ucred = crget();
152 p->p_ucred->cr_ngroups = 1; /* group 0 */
153
154 /* Create the file descriptor table. */
155 fdp = &filedesc0;
156 p->p_fd = &fdp->fd_fd;
157 fdp->fd_fd.fd_refcnt = 1;
158 fdp->fd_fd.fd_cmask = cmask;
159 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
160 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
161 fdp->fd_fd.fd_nfiles = NDFILE;
162
163 /* Create the limits structures. */
164 p->p_limit = &limit0;
165 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
166 limit0.pl_rlimit[i].rlim_cur =
167 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
168 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
169 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
170 i = ptoa(cnt.v_free_count);
171 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
172 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
173 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
174 limit0.p_refcnt = 1;
175
176 /* Allocate a prototype map so we have something to fork. */
177 p->p_vmspace = &vmspace0;
178 vmspace0.vm_refcnt = 1;
179 pmap_pinit(&vmspace0.vm_pmap);
180 vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
181 trunc_page(VM_MAX_ADDRESS), TRUE);
182 vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
183 p->p_addr = proc0paddr; /* XXX */
184
185 /*
186 * We continue to place resource usage info and signal
187 * actions in the user struct so they're pageable.
188 */
189 p->p_stats = &p->p_addr->u_stats;
190 p->p_sigacts = &p->p_addr->u_sigacts;
191
192 /*
193 * Initialize per uid information structure and charge
194 * root for one process.
195 */
196 usrinfoinit();
197 (void)chgproccnt(0, 1);
198
199 rqinit();
200
201 /* Configure virtual memory system, set vm rlimits. */
202 vm_init_limits(p);
203
204 /* Initialize the file systems. */
205 vfsinit();
206
207 /* Start real time and statistics clocks. */
208 initclocks();
209
210 /* Initialize mbuf's. */
211 mbinit();
212
213 #ifdef REAL_CLISTS
214 /* Initialize clists. */
215 clist_init();
216 #endif
217
218 #ifdef SYSVSHM
219 /* Initialize System V style shared memory. */
220 shminit();
221 #endif
222
223 #ifdef SYSVSEM
224 /* Initialize System V style semaphores. */
225 seminit();
226 #endif
227
228 #ifdef SYSVMSG
229 /* Initialize System V style message queues. */
230 msginit();
231 #endif
232
233 /* Attach pseudo-devices. */
234 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
235 (*pdev->pdev_attach)(pdev->pdev_count);
236
237 /*
238 * Initialize protocols. Block reception of incoming packets
239 * until everything is ready.
240 */
241 s = splimp();
242 ifinit();
243 domaininit();
244 splx(s);
245
246 #ifdef GPROF
247 /* Initialize kernel profiling. */
248 kmstartup();
249 #endif
250
251 /* Kick off timeout driven events by calling first time. */
252 roundrobin(NULL);
253 schedcpu(NULL);
254
255 /* Mount the root file system. */
256 if ((*mountroot)())
257 panic("cannot mount root");
258
259 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
260 if (VFS_ROOT(mountlist.tqh_first, &rootvnode))
261 panic("cannot find root vnode");
262 fdp->fd_fd.fd_cdir = rootvnode;
263 VREF(fdp->fd_fd.fd_cdir);
264 VOP_UNLOCK(rootvnode);
265 fdp->fd_fd.fd_rdir = NULL;
266 swapinit();
267
268 /*
269 * Now can look at time, having had a chance to verify the time
270 * from the file system. Reset p->p_rtime as it may have been
271 * munched in mi_switch() after the time got set.
272 */
273 p->p_stats->p_start = runtime = mono_time = boottime = time;
274 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
275
276 /* Initialize signal state for process 0. */
277 siginit(p);
278
279 /* Create process 1 (init(8)). */
280 if (fork(p, NULL, rval))
281 panic("fork init");
282 if (rval[1]) {
283 start_init(curproc, framep);
284 return;
285 }
286
287 /* Create process 2 (the pageout daemon). */
288 if (fork(p, NULL, rval))
289 panic("fork pager");
290 if (rval[1]) {
291 /*
292 * Now in process 2.
293 */
294 p = curproc;
295 pageproc = p;
296 p->p_flag |= P_INMEM | P_SYSTEM; /* XXX */
297 bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
298 vm_pageout();
299 /* NOTREACHED */
300 }
301
302 /* The scheduler is an infinite loop. */
303 scheduler();
304 /* NOTREACHED */
305 }
306
307 /*
308 * List of paths to try when searching for "init".
309 */
310 static char *initpaths[] = {
311 "/sbin/init",
312 "/sbin/oinit",
313 "/sbin/init.bak",
314 NULL,
315 };
316
317 /*
318 * Start the initial user process; try exec'ing each pathname in "initpaths".
319 * The program is invoked with one argument containing the boot flags.
320 */
321 static void
322 start_init(p, framep)
323 struct proc *p;
324 void *framep;
325 {
326 vm_offset_t addr;
327 struct execve_args args;
328 int options, i, retval[2], error;
329 char **pathp, *path, *ucp, **uap, *arg0, *arg1;
330
331 initproc = p;
332
333 /*
334 * We need to set the system call frame as if we were entered through
335 * a syscall() so that when we call execve() below, it will be able
336 * to set the entry point (see setregs) when it tries to exec. The
337 * startup code in "locore.s" has allocated space for the frame and
338 * passed a pointer to that space as main's argument.
339 */
340 cpu_set_init_frame(p, framep);
341
342 /*
343 * Need just enough stack to hold the faked-up "execve()" arguments.
344 */
345 addr = USRSTACK - PAGE_SIZE;
346 if (vm_allocate(&p->p_vmspace->vm_map, &addr, (vm_size_t)PAGE_SIZE,
347 FALSE) != 0)
348 panic("init: couldn't allocate argument space");
349 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
350
351 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
352 /*
353 * Move out the boot flag argument.
354 */
355 options = 0;
356 ucp = (char *)(addr + PAGE_SIZE);
357 (void)subyte(--ucp, 0); /* trailing zero */
358 if (boothowto & RB_SINGLE) {
359 (void)subyte(--ucp, 's');
360 options = 1;
361 }
362 #ifdef notyet
363 if (boothowto & RB_FASTBOOT) {
364 (void)subyte(--ucp, 'f');
365 options = 1;
366 }
367 #endif
368 if (options == 0)
369 (void)subyte(--ucp, '-');
370 (void)subyte(--ucp, '-'); /* leading hyphen */
371 arg1 = ucp;
372
373 /*
374 * Move out the file name (also arg 0).
375 */
376 for (i = strlen(path) + 1; i >= 0; i--)
377 (void)subyte(--ucp, path[i]);
378 arg0 = ucp;
379
380 /*
381 * Move out the arg pointers.
382 */
383 uap = (char **)((int)ucp & ~(NBPW-1));
384 (void)suword((caddr_t)--uap, 0); /* terminator */
385 (void)suword((caddr_t)--uap, (int)arg1);
386 (void)suword((caddr_t)--uap, (int)arg0);
387
388 /*
389 * Point at the arguments.
390 */
391 args.path = arg0;
392 args.argp = uap;
393 args.envp = NULL;
394
395 /*
396 * Now try to exec the program. If can't for any reason
397 * other than it doesn't exist, complain.
398 */
399 if ((error = execve(p, &args, retval)) == 0)
400 return;
401 if (error != ENOENT)
402 printf("exec %s: error %d\n", path, error);
403 }
404 printf("init: not found\n");
405 panic("no init");
406 }
407