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