Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / kern / init_main.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1995 Terrence R. Lambert
3 * All rights reserved.
4 *
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 * $FreeBSD: src/sys/kern/init_main.c,v 1.134.2.8 2003/06/06 20:21:32 tegge Exp $
43 */
44
45#include "opt_init_path.h"
46
47#include <sys/param.h>
48#include <sys/file.h>
49#include <sys/filedesc.h>
50#include <sys/kernel.h>
51#include <sys/mount.h>
52#include <sys/sysctl.h>
53#include <sys/proc.h>
54#include <sys/resourcevar.h>
55#include <sys/signalvar.h>
56#include <sys/systm.h>
57#include <sys/vnode.h>
58#include <sys/sysent.h>
59#include <sys/reboot.h>
60#include <sys/sysproto.h>
61#include <sys/vmmeter.h>
62#include <sys/unistd.h>
63#include <sys/malloc.h>
64
65#include <machine/cpu.h>
66
67#include <vm/vm.h>
68#include <vm/vm_param.h>
69#include <sys/lock.h>
70#include <vm/pmap.h>
71#include <vm/vm_map.h>
72#include <sys/user.h>
73#include <sys/copyright.h>
74
75extern struct linker_set sysinit_set; /* XXX */
76
77void mi_startup(void); /* Should be elsewhere */
78
79/* Components of the first process -- never freed. */
80static struct session session0;
81static struct pgrp pgrp0;
82struct proc proc0;
83static struct pcred cred0;
84static struct procsig procsig0;
85static struct filedesc0 filedesc0;
86static struct plimit limit0;
87static struct vmspace vmspace0;
88struct proc *initproc;
89
90int cmask = CMASK;
91extern struct user *proc0paddr;
92extern int fallback_elf_brand;
93
94struct vnode *rootvp;
95int boothowto = 0; /* initialized so that it can be patched */
96SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0, "");
97
98/*
99 * This ensures that there is at least one entry so that the sysinit_set
100 * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never
101 * executed.
102 */
103SYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL)
104
105/*
106 * The sysinit table itself. Items are checked off as the are run.
107 * If we want to register new sysinit types, add them to newsysinit.
108 */
109struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items;
110struct sysinit **newsysinit;
111
112/*
113 * Merge a new sysinit set into the current set, reallocating it if
114 * necessary. This can only be called after malloc is running.
115 */
116void
117sysinit_add(struct sysinit **set)
118{
119 struct sysinit **newset;
120 struct sysinit **sipp;
121 struct sysinit **xipp;
122 int count = 0;
123
124 if (newsysinit)
125 for (sipp = newsysinit; *sipp; sipp++)
126 count++;
127 else
128 for (sipp = sysinit; *sipp; sipp++)
129 count++;
130 for (sipp = set; *sipp; sipp++)
131 count++;
132 count++; /* Trailing NULL */
133 newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT);
134 if (newset == NULL)
135 panic("cannot malloc for sysinit");
136 xipp = newset;
137 if (newsysinit)
138 for (sipp = newsysinit; *sipp; sipp++)
139 *xipp++ = *sipp;
140 else
141 for (sipp = sysinit; *sipp; sipp++)
142 *xipp++ = *sipp;
143 for (sipp = set; *sipp; sipp++)
144 *xipp++ = *sipp;
145 *xipp = NULL;
146 if (newsysinit)
147 free(newsysinit, M_TEMP);
148 newsysinit = newset;
149}
150
151/*
152 * System startup; initialize the world, create process 0, mount root
153 * filesystem, and fork to create init and pagedaemon. Most of the
154 * hard work is done in the lower-level initialization routines including
155 * startup(), which does memory initialization and autoconfiguration.
156 *
157 * This allows simple addition of new kernel subsystems that require
158 * boot time initialization. It also allows substitution of subsystem
159 * (for instance, a scheduler, kernel profiler, or VM system) by object
160 * module. Finally, it allows for optional "kernel threads".
161 */
162void
163mi_startup(void)
164{
165
166 register struct sysinit **sipp; /* system initialization*/
167 register struct sysinit **xipp; /* interior loop of sort*/
168 register struct sysinit *save; /* bubble*/
169
170restart:
171 /*
172 * Perform a bubble sort of the system initialization objects by
173 * their subsystem (primary key) and order (secondary key).
174 */
175 for (sipp = sysinit; *sipp; sipp++) {
176 for (xipp = sipp + 1; *xipp; xipp++) {
177 if ((*sipp)->subsystem < (*xipp)->subsystem ||
178 ((*sipp)->subsystem == (*xipp)->subsystem &&
179 (*sipp)->order <= (*xipp)->order))
180 continue; /* skip*/
181 save = *sipp;
182 *sipp = *xipp;
183 *xipp = save;
184 }
185 }
186
187 /*
188 * Traverse the (now) ordered list of system initialization tasks.
189 * Perform each task, and continue on to the next task.
190 *
191 * The last item on the list is expected to be the scheduler,
192 * which will not return.
193 */
194 for (sipp = sysinit; *sipp; sipp++) {
195
196 if ((*sipp)->subsystem == SI_SUB_DUMMY)
197 continue; /* skip dummy task(s)*/
198
199 if ((*sipp)->subsystem == SI_SUB_DONE)
200 continue;
201
202 /* Call function */
203 (*((*sipp)->func))((*sipp)->udata);
204
205 /* Check off the one we're just done */
206 (*sipp)->subsystem = SI_SUB_DONE;
207
208 /* Check if we've installed more sysinit items via KLD */
209 if (newsysinit != NULL) {
210 if (sysinit != (struct sysinit **)sysinit_set.ls_items)
211 free(sysinit, M_TEMP);
212 sysinit = newsysinit;
213 newsysinit = NULL;
214 goto restart;
215 }
216 }
217
218 panic("Shouldn't get here!");
219 /* NOTREACHED*/
220}
221
222
223/*
224 ***************************************************************************
225 ****
226 **** The following SYSINIT's belong elsewhere, but have not yet
227 **** been moved.
228 ****
229 ***************************************************************************
230 */
231static void
232print_caddr_t(void *data __unused)
233{
234 printf("%s", (char *)data);
235}
236SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright)
237
238
239/*
240 ***************************************************************************
241 ****
242 **** The two following SYSINT's are proc0 specific glue code. I am not
243 **** convinced that they can not be safely combined, but their order of
244 **** operation has been maintained as the same as the original init_main.c
245 **** for right now.
246 ****
247 **** These probably belong in init_proc.c or kern_proc.c, since they
248 **** deal with proc0 (the fork template process).
249 ****
250 ***************************************************************************
251 */
252/* ARGSUSED*/
253static void
254proc0_init(void *dummy __unused)
255{
256 register struct proc *p;
257 register struct filedesc0 *fdp;
258 register unsigned i;
259
260 p = &proc0;
261
262 /*
263 * Initialize process and pgrp structures.
264 */
265 procinit();
266
267 /*
268 * Initialize sleep queue hash table
269 */
270 sleepinit();
271
272 /*
273 * additional VM structures
274 */
275 vm_init2();
276
277 /*
278 * Create process 0 (the swapper).
279 */
280 LIST_INSERT_HEAD(&allproc, p, p_list);
281 p->p_pgrp = &pgrp0;
282 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
283 LIST_INIT(&pgrp0.pg_members);
284 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
285
286 pgrp0.pg_session = &session0;
287 session0.s_count = 1;
288 session0.s_leader = p;
289
290 p->p_sysent = &aout_sysvec;
291
292 p->p_flag = P_INMEM | P_SYSTEM;
293 p->p_stat = SRUN;
294 p->p_nice = NZERO;
295 p->p_rtprio.type = RTP_PRIO_NORMAL;
296 p->p_rtprio.prio = 0;
297
298 p->p_peers = 0;
299 p->p_leader = p;
300
301 bcopy("swapper", p->p_comm, sizeof ("swapper"));
302
303 /* Create credentials. */
304 cred0.p_refcnt = 1;
305 cred0.p_uidinfo = uifind(0);
306 p->p_cred = &cred0;
307 p->p_ucred = crget();
308 p->p_ucred->cr_ngroups = 1; /* group 0 */
309 p->p_ucred->cr_uidinfo = uifind(0);
310
311 /* Don't jail it */
312 p->p_prison = 0;
313
314 /* Create procsig. */
315 p->p_procsig = &procsig0;
316 p->p_procsig->ps_refcnt = 1;
317
318 /* Initialize signal state for process 0. */
319 siginit(&proc0);
320
321 /* Create the file descriptor table. */
322 fdp = &filedesc0;
323 p->p_fd = &fdp->fd_fd;
324 p->p_fdtol = NULL;
325 fdp->fd_fd.fd_refcnt = 1;
326 fdp->fd_fd.fd_cmask = cmask;
327 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
328 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
329 fdp->fd_fd.fd_nfiles = NDFILE;
330
331 /* Create the limits structures. */
332 p->p_limit = &limit0;
333 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
334 limit0.pl_rlimit[i].rlim_cur =
335 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
336 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
337 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
338 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
339 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
340 i = ptoa(cnt.v_free_count);
341 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
342 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
343 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
344 limit0.p_cpulimit = RLIM_INFINITY;
345 limit0.p_refcnt = 1;
346
347 /* Allocate a prototype map so we have something to fork. */
348 pmap_pinit0(vmspace_pmap(&vmspace0));
349 p->p_vmspace = &vmspace0;
350 vmspace0.vm_refcnt = 1;
351 vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
352 trunc_page(VM_MAXUSER_ADDRESS));
353 vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0);
354 p->p_addr = proc0paddr; /* XXX */
355
356 /*
357 * We continue to place resource usage info and signal
358 * actions in the user struct so they're pageable.
359 */
360 p->p_stats = &p->p_addr->u_stats;
361 p->p_sigacts = &p->p_addr->u_sigacts;
362
363 /*
364 * Charge root for one process.
365 */
366 (void)chgproccnt(cred0.p_uidinfo, 1, 0);
367
368 /*
369 * Initialize the current process pointer (curproc) before
370 * any possible traps/probes to simplify trap processing.
371 */
372 SET_CURPROC(p);
373
374}
375SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL)
376
377/* ARGSUSED*/
378static void
379proc0_post(void *dummy __unused)
380{
381 struct timespec ts;
382 struct proc *p;
383
384 /*
385 * Now we can look at the time, having had a chance to verify the
386 * time from the file system. Pretend that proc0 started now.
387 */
388 LIST_FOREACH(p, &allproc, p_list) {
389 microtime(&p->p_stats->p_start);
390 p->p_runtime = 0;
391 }
392 microuptime(&switchtime);
393 switchticks = ticks;
394
395 /*
396 * Give the ``random'' number generator a thump.
397 * XXX: Does read_random() contain enough bits to be used here ?
398 */
399 nanotime(&ts);
400 srandom(ts.tv_sec ^ ts.tv_nsec);
401}
402SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL)
403
404/*
405 ***************************************************************************
406 ****
407 **** The following SYSINIT's and glue code should be moved to the
408 **** respective files on a per subsystem basis.
409 ****
410 ***************************************************************************
411 */
412
413
414/*
415 ***************************************************************************
416 ****
417 **** The following code probably belongs in another file, like
418 **** kern/init_init.c.
419 ****
420 ***************************************************************************
421 */
422
423/*
424 * List of paths to try when searching for "init".
425 */
426static char init_path[MAXPATHLEN] =
427#ifdef INIT_PATH
428 __XSTRING(INIT_PATH);
429#else
430 "/sbin/init:/sbin/oinit:/sbin/init.bak:/stand/sysinstall";
431#endif
432SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, "");
433
434/*
435 * Start the initial user process; try exec'ing each pathname in init_path.
436 * The program is invoked with one argument containing the boot flags.
437 */
438static void
439start_init(void *dummy)
440{
441 vm_offset_t addr;
442 struct execve_args args;
443 int options, error;
444 char *var, *path, *next, *s;
445 char *ucp, **uap, *arg0, *arg1;
446 struct proc *p;
447
448 p = curproc;
449
450 /* Get the vnode for '/'. Set p->p_fd->fd_cdir to reference it. */
451 if (VFS_ROOT(TAILQ_FIRST(&mountlist), &rootvnode))
452 panic("cannot find root vnode");
453 p->p_fd->fd_cdir = rootvnode;
454 VREF(p->p_fd->fd_cdir);
455 p->p_fd->fd_rdir = rootvnode;
456 VREF(p->p_fd->fd_rdir);
457 VOP_UNLOCK(rootvnode, 0, p);
458
459 /*
460 * Need just enough stack to hold the faked-up "execve()" arguments.
461 */
462 addr = trunc_page(USRSTACK - PAGE_SIZE);
463 if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE,
464 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
465 panic("init: couldn't allocate argument space");
466 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
467 p->p_vmspace->vm_ssize = 1;
468
469 if ((var = getenv("init_path")) != NULL) {
470 strncpy(init_path, var, sizeof init_path);
471 init_path[sizeof init_path - 1] = 0;
472 }
473 if ((var = getenv("kern.fallback_elf_brand")) != NULL)
474 fallback_elf_brand = strtol(var, NULL, 0);
475
476 for (path = init_path; *path != '\0'; path = next) {
477 while (*path == ':')
478 path++;
479 if (*path == '\0')
480 break;
481 for (next = path; *next != '\0' && *next != ':'; next++)
482 /* nothing */ ;
483 if (bootverbose)
484 printf("start_init: trying %.*s\n", (int)(next - path),
485 path);
486
487 /*
488 * Move out the boot flag argument.
489 */
490 options = 0;
491 ucp = (char *)USRSTACK;
492 (void)subyte(--ucp, 0); /* trailing zero */
493 if (boothowto & RB_SINGLE) {
494 (void)subyte(--ucp, 's');
495 options = 1;
496 }
497#ifdef notyet
498 if (boothowto & RB_FASTBOOT) {
499 (void)subyte(--ucp, 'f');
500 options = 1;
501 }
502#endif
503
504#ifdef BOOTCDROM
505 (void)subyte(--ucp, 'C');
506 options = 1;
507#endif
508 if (options == 0)
509 (void)subyte(--ucp, '-');
510 (void)subyte(--ucp, '-'); /* leading hyphen */
511 arg1 = ucp;
512
513 /*
514 * Move out the file name (also arg 0).
515 */
516 (void)subyte(--ucp, 0);
517 for (s = next - 1; s >= path; s--)
518 (void)subyte(--ucp, *s);
519 arg0 = ucp;
520
521 /*
522 * Move out the arg pointers.
523 */
524 uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1));
525 (void)suword((caddr_t)--uap, (long)0); /* terminator */
526 (void)suword((caddr_t)--uap, (long)(intptr_t)arg1);
527 (void)suword((caddr_t)--uap, (long)(intptr_t)arg0);
528
529 /*
530 * Point at the arguments.
531 */
532 args.fname = arg0;
533 args.argv = uap;
534 args.envv = NULL;
535
536 /*
537 * Now try to exec the program. If can't for any reason
538 * other than it doesn't exist, complain.
539 *
540 * Otherwise, return via fork_trampoline() all the way
541 * to user mode as init!
542 */
543 if ((error = execve(p, &args)) == 0)
544 return;
545 if (error != ENOENT)
546 printf("exec %.*s: error %d\n", (int)(next - path),
547 path, error);
548 }
549 printf("init: not found in path %s\n", init_path);
550 panic("no init");
551}
552
553/*
554 * Like kthread_create(), but runs in it's own address space.
555 * We do this early to reserve pid 1.
556 *
557 * Note special case - do not make it runnable yet. Other work
558 * in progress will change this more.
559 */
560static void
561create_init(const void *udata __unused)
562{
563 int error;
564 int s;
565
566 s = splhigh();
567 error = fork1(&proc0, RFFDG | RFPROC, &initproc);
568 if (error)
569 panic("cannot fork init: %d\n", error);
570 initproc->p_flag |= P_INMEM | P_SYSTEM;
571 cpu_set_fork_handler(initproc, start_init, NULL);
572 remrunqueue(initproc);
573 splx(s);
574}
575SYSINIT(init,SI_SUB_CREATE_INIT, SI_ORDER_FIRST, create_init, NULL)
576
577/*
578 * Make it runnable now.
579 */
580static void
581kick_init(const void *udata __unused)
582{
583 setrunqueue(initproc);
584}
585SYSINIT(kickinit,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kick_init, NULL)