FreeBSD/Linux Kernel Cross Reference
sys/kern/init_main.c
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.51.2.8 1999/09/05 08:14:48 peter Exp $
43 */
44
45 #include "opt_rlimit.h"
46 #include "opt_devfs.h"
47
48 #include <sys/param.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/proc.h>
53 #include <sys/resourcevar.h>
54 #include <sys/signalvar.h>
55 #include <sys/systm.h>
56 #include <sys/vnode.h>
57 #include <sys/sysent.h>
58 #include <sys/reboot.h>
59 #include <sys/sysproto.h>
60 #include <sys/vmmeter.h>
61
62 #include <machine/cpu.h>
63
64 #include <vm/vm.h>
65 #include <vm/vm_param.h>
66 #include <vm/vm_prot.h>
67 #include <vm/lock.h>
68 #include <vm/pmap.h>
69 #include <vm/vm_map.h>
70 #include <sys/user.h>
71
72 extern struct linker_set sysinit_set; /* XXX */
73
74 extern void __main __P((void));
75 extern void main __P((void *framep));
76
77 /* Components of the first process -- never freed. */
78 static struct session session0;
79 static struct pgrp pgrp0;
80 struct proc proc0;
81 static struct pcred cred0;
82 static struct filedesc0 filedesc0;
83 static struct plimit limit0;
84 static struct vmspace vmspace0;
85 struct proc *curproc = &proc0;
86 struct proc *initproc;
87
88 int cmask = CMASK;
89 extern struct user *proc0paddr;
90
91 struct vnode *rootvp;
92 int boothowto;
93
94 struct timeval boottime;
95 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime,
96 CTLFLAG_RW, &boottime, timeval, "");
97
98 static int shutdowntimeout = 120;
99 SYSCTL_INT(_kern, OID_AUTO, shutdown_timeout,
100 CTLFLAG_RW, &shutdowntimeout, 0, "");
101
102 struct timeval runtime;
103
104 /*
105 * Promiscuous argument pass for start_init()
106 *
107 * This is a kludge because we use a return from main() rather than a call
108 * to a new routine in locore.s to kick the kernel alive from locore.s.
109 */
110 static void *init_framep;
111
112
113 #if __GNUC__ >= 2
114 void __main() {}
115 #endif
116
117
118 /*
119 * This ensures that there is at least one entry so that the sysinit_set
120 * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never
121 * executed.
122 */
123 SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL)
124
125
126 /*
127 * System startup; initialize the world, create process 0, mount root
128 * filesystem, and fork to create init and pagedaemon. Most of the
129 * hard work is done in the lower-level initialization routines including
130 * startup(), which does memory initialization and autoconfiguration.
131 *
132 * This allows simple addition of new kernel subsystems that require
133 * boot time initialization. It also allows substitution of subsystem
134 * (for instance, a scheduler, kernel profiler, or VM system) by object
135 * module. Finally, it allows for optional "kernel threads", like an LFS
136 * cleaner.
137 */
138 void
139 main(framep)
140 void *framep;
141 {
142
143 register struct sysinit **sipp; /* system initialization*/
144 register struct sysinit **xipp; /* interior loop of sort*/
145 register struct sysinit *save; /* bubble*/
146 int rval[2]; /* SI_TYPE_KTHREAD support*/
147
148 /*
149 * Save the locore.s frame pointer for start_init().
150 */
151 init_framep = framep;
152
153 /*
154 * Perform a bubble sort of the system initialization objects by
155 * their subsystem (primary key) and order (secondary key).
156 *
157 * Since some things care about execution order, this is the
158 * operation which ensures continued function.
159 */
160 for( sipp = (struct sysinit **)sysinit_set.ls_items; *sipp; sipp++) {
161 for( xipp = sipp + 1; *xipp; xipp++) {
162 if( (*sipp)->subsystem < (*xipp)->subsystem ||
163 ( (*sipp)->subsystem == (*xipp)->subsystem &&
164 (*sipp)->order < (*xipp)->order))
165 continue; /* skip*/
166 save = *sipp;
167 *sipp = *xipp;
168 *xipp = save;
169 }
170 }
171
172 /*
173 * Traverse the (now) ordered list of system initialization tasks.
174 * Perform each task, and continue on to the next task.
175 *
176 * The last item on the list is expected to be the scheduler,
177 * which will not return.
178 */
179 for( sipp = (struct sysinit **)sysinit_set.ls_items; *sipp; sipp++) {
180 if( (*sipp)->subsystem == SI_SUB_DUMMY)
181 continue; /* skip dummy task(s)*/
182
183 switch( (*sipp)->type) {
184 case SI_TYPE_DEFAULT:
185 /* no special processing*/
186 (*((*sipp)->func))( (*sipp)->udata);
187 break;
188
189 case SI_TYPE_KTHREAD:
190 /* kernel thread*/
191 if (fork(&proc0, NULL, rval))
192 panic("fork kernel process");
193 if (rval[1]) {
194 (*((*sipp)->func))( (*sipp)->udata);
195 /*
196 * The call to start "init" returns
197 * here after the scheduler has been
198 * started, and returns to the caller
199 * in i386/i386/locore.s. This is a
200 * necessary part of initialization
201 * and is rather non-obvious.
202 *
203 * No other "kernel threads" should
204 * return here. Call panic() instead.
205 */
206 return;
207 }
208 break;
209
210 default:
211 panic( "init_main: unrecognized init type");
212 }
213 }
214
215 /* NOTREACHED*/
216 }
217
218
219 /*
220 * Start a kernel process. This is called after a fork() call in
221 * main() in the file kern/init_main.c.
222 *
223 * This function is used to start "internal" daemons.
224 */
225 /* ARGSUSED*/
226 void
227 kproc_start(udata)
228 void *udata;
229 {
230 struct kproc_desc *kp = udata;
231 struct proc *p = curproc;
232
233 /* save a global descriptor, if desired*/
234 if( kp->global_procpp != NULL)
235 *kp->global_procpp = p;
236
237 /* this is a non-swapped system process*/
238 p->p_flag |= P_INMEM | P_SYSTEM;
239
240 /* set up arg0 for 'ps', et al*/
241 strcpy( p->p_comm, kp->arg0);
242
243 /* call the processes' main()...*/
244 (*kp->func)();
245
246 /* NOTREACHED */
247 panic("kproc_start: %s", kp->arg0);
248 }
249
250
251 /*
252 ***************************************************************************
253 ****
254 **** The following SYSINIT's belong elsewhere, but have not yet
255 **** been moved.
256 ****
257 ***************************************************************************
258 */
259 #ifdef OMIT
260 /*
261 * Handled by vfs_mountroot (bad idea) at this time... should be
262 * done the same as 4.4Lite2.
263 */
264 SYSINIT(swapinit, SI_SUB_SWAP, SI_ORDER_FIRST, swapinit, NULL)
265 #endif /* OMIT*/
266
267 /*
268 * Should get its own file...
269 */
270 #ifdef HPFPLIB
271 char copyright[] =
272 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California.\nCopyright (c) 1992 Hewlett-Packard Company\nCopyright (c) 1992 Motorola Inc.\nAll rights reserved.\n\n";
273 #else
274 char copyright[] =
275 "Copyright (c) 1992-1998 FreeBSD Inc.\n"
276 #ifdef PC98
277 "Copyright (c) 1994-1998 FreeBSD(98) porting team.\n"
278 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California.\n"
279 "Copyright (c) 1992 A.Kojima F.Ukai M.Ishii (KMC).\n"
280 "\tAll rights reserved.\n\n";
281 #else
282 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California. All rights reserved.\n\n";
283 #endif
284 #endif
285 static void print_caddr_t __P((void *data));
286 static void
287 print_caddr_t(data)
288 void *data;
289 {
290 printf("%s", (char *)data);
291 }
292 SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright)
293
294
295 /*
296 ***************************************************************************
297 ****
298 **** The two following SYSINT's are proc0 specific glue code. I am not
299 **** convinced that they can not be safely combined, but their order of
300 **** operation has been maintained as the same as the original init_main.c
301 **** for right now.
302 ****
303 **** These probably belong in init_proc.c or kern_proc.c, since they
304 **** deal with proc0 (the fork template process).
305 ****
306 ***************************************************************************
307 */
308 /* ARGSUSED*/
309 static void proc0_init __P((void *dummy));
310 static void
311 proc0_init(dummy)
312 void *dummy;
313 {
314 register struct proc *p;
315 register struct filedesc0 *fdp;
316 register unsigned i;
317
318 /*
319 * Initialize the current process pointer (curproc) before
320 * any possible traps/probes to simplify trap processing.
321 */
322 p = &proc0;
323 curproc = p; /* XXX redundant*/
324
325 /*
326 * Initialize process and pgrp structures.
327 */
328 procinit();
329
330 /*
331 * Initialize sleep queue hash table
332 */
333 sleepinit();
334
335 /*
336 * Create process 0 (the swapper).
337 */
338 LIST_INSERT_HEAD(&allproc, p, p_list);
339 p->p_pgrp = &pgrp0;
340 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
341 LIST_INIT(&pgrp0.pg_members);
342 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
343
344 pgrp0.pg_session = &session0;
345 session0.s_count = 1;
346 session0.s_leader = p;
347
348 p->p_sysent = &aout_sysvec;
349
350 p->p_flag = P_INMEM | P_SYSTEM;
351 p->p_stat = SRUN;
352 p->p_nice = NZERO;
353 p->p_rtprio.type = RTP_PRIO_NORMAL;
354 p->p_rtprio.prio = 0;
355
356 bcopy("swapper", p->p_comm, sizeof ("swapper"));
357
358 /* Create credentials. */
359 cred0.p_refcnt = 1;
360 p->p_cred = &cred0;
361 p->p_ucred = crget();
362 p->p_ucred->cr_ngroups = 1; /* group 0 */
363
364 /* Create the file descriptor table. */
365 fdp = &filedesc0;
366 p->p_fd = &fdp->fd_fd;
367 fdp->fd_fd.fd_refcnt = 1;
368 fdp->fd_fd.fd_cmask = cmask;
369 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
370 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
371 fdp->fd_fd.fd_nfiles = NDFILE;
372
373 /* Create the limits structures. */
374 p->p_limit = &limit0;
375 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
376 limit0.pl_rlimit[i].rlim_cur =
377 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
378 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
379 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
380 i = ptoa(cnt.v_free_count);
381 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
382 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
383 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
384 limit0.p_refcnt = 1;
385
386 /* Allocate a prototype map so we have something to fork. */
387 p->p_vmspace = &vmspace0;
388 vmspace0.vm_refcnt = 1;
389 pmap_pinit(&vmspace0.vm_pmap);
390 vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
391 trunc_page(VM_MAXUSER_ADDRESS), TRUE);
392 vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
393 p->p_addr = proc0paddr; /* XXX */
394
395 #define INCOMPAT_LITES2
396 #ifdef INCOMPAT_LITES2
397 /*
398 * proc0 needs to have a coherent frame base, too.
399 * This probably makes the identical call for the init proc
400 * that happens later unnecessary since it should inherit
401 * it during the fork.
402 */
403 cpu_set_init_frame(p, init_framep); /* XXX! */
404 #endif /* INCOMPAT_LITES2*/
405
406 /*
407 * We continue to place resource usage info and signal
408 * actions in the user struct so they're pageable.
409 */
410 p->p_stats = &p->p_addr->u_stats;
411 p->p_sigacts = &p->p_addr->u_sigacts;
412
413 /*
414 * Charge root for one process.
415 */
416 (void)chgproccnt(0, 1);
417 }
418 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL)
419
420 /* ARGSUSED*/
421 static void proc0_post __P((void *dummy));
422 static void
423 proc0_post(dummy)
424 void *dummy;
425 {
426 struct timeval tv;
427
428 /*
429 * Now can look at time, having had a chance to verify the time
430 * from the file system. Reset p->p_rtime as it may have been
431 * munched in mi_switch() after the time got set.
432 */
433 proc0.p_stats->p_start = runtime = mono_time = boottime = time;
434 proc0.p_rtime.tv_sec = proc0.p_rtime.tv_usec = 0;
435
436 /*
437 * Give the ``random'' number generator a thump.
438 */
439 microtime(&tv);
440 srandom(tv.tv_sec ^ tv.tv_usec);
441
442 /* Initialize signal state for process 0. */
443 siginit(&proc0);
444 }
445 SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL)
446
447
448
449
450 /*
451 ***************************************************************************
452 ****
453 **** The following SYSINIT's and glue code should be moved to the
454 **** respective files on a per subsystem basis.
455 ****
456 ***************************************************************************
457 */
458 /* ARGSUSED*/
459 static void sched_setup __P((void *dummy));
460 static void
461 sched_setup(dummy)
462 void *dummy;
463 {
464 /* Kick off timeout driven events by calling first time. */
465 roundrobin(NULL);
466 schedcpu(NULL);
467 }
468 SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL)
469
470 /* ARGSUSED */
471 static void root_conf __P((void *dummy));
472 static void
473 root_conf(dummy)
474 void *dummy;
475 {
476 cpu_rootconf();
477 }
478 SYSINIT(root_conf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, root_conf, NULL)
479
480 /* ARGSUSED*/
481 static void xxx_vfs_mountroot __P((void *dummy));
482 #ifdef BOOTP
483 extern void bootpc_init __P((void));
484 #endif
485 static void
486 xxx_vfs_mountroot(dummy)
487 void *dummy;
488 {
489 /* XXX Add a separate SYSINIT entry */
490 #ifdef BOOTP
491 bootpc_init();
492 #endif
493 /* Mount the root file system. */
494 if ((*mountroot)(mountrootvfsops))
495 panic("cannot mount root");
496 }
497 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, xxx_vfs_mountroot, NULL)
498
499 /* ARGSUSED*/
500 static void xxx_vfs_root_fdtab __P((void *dummy));
501 static void
502 xxx_vfs_root_fdtab(dummy)
503 void *dummy;
504 {
505 register struct filedesc0 *fdp = &filedesc0;
506
507 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
508 if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
509 panic("cannot find root vnode");
510 fdp->fd_fd.fd_cdir = rootvnode;
511 VREF(fdp->fd_fd.fd_cdir);
512 VOP_UNLOCK(rootvnode);
513 fdp->fd_fd.fd_rdir = NULL;
514 }
515 SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL)
516
517
518 /*
519 ***************************************************************************
520 ****
521 **** The following code probably belongs in another file, like
522 **** kern/init_init.c. It is here for two reasons only:
523 ****
524 **** 1) This code returns to startup the system; this is
525 **** abnormal for a kernel thread.
526 **** 2) This code promiscuously uses init_frame
527 ****
528 ***************************************************************************
529 */
530
531 static void kthread_init __P((void *dummy));
532 SYSINIT_KT(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kthread_init, NULL)
533
534
535 static void start_init __P((struct proc *p, void *framep));
536
537 /* ARGSUSED*/
538 static void
539 kthread_init(dummy)
540 void *dummy;
541 {
542
543 /* Create process 1 (init(8)). */
544 start_init(curproc, init_framep);
545
546 /*
547 * This is the only kernel thread allowed to return yo the
548 * caller!!!
549 */
550 return;
551 }
552
553
554 /*
555 * List of paths to try when searching for "init".
556 */
557 static char *initpaths[] = {
558 "/sbin/init",
559 "/sbin/oinit",
560 "/sbin/init.bak",
561 "/stand/sysinstall",
562 NULL,
563 };
564
565 /*
566 * Start the initial user process; try exec'ing each pathname in "initpaths".
567 * The program is invoked with one argument containing the boot flags.
568 */
569 static void
570 start_init(p, framep)
571 struct proc *p;
572 void *framep;
573 {
574 vm_offset_t addr;
575 struct execve_args args;
576 int options, i, retval[2], error;
577 char **pathp, *path, *ucp, **uap, *arg0, *arg1;
578
579 initproc = p;
580
581 /*
582 * We need to set the system call frame as if we were entered through
583 * a syscall() so that when we call execve() below, it will be able
584 * to set the entry point (see setregs) when it tries to exec. The
585 * startup code in "locore.s" has allocated space for the frame and
586 * passed a pointer to that space as main's argument.
587 */
588 cpu_set_init_frame(p, framep);
589
590 /*
591 * Need just enough stack to hold the faked-up "execve()" arguments.
592 */
593 addr = trunc_page(VM_MAXUSER_ADDRESS - PAGE_SIZE);
594 if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
595 panic("init: couldn't allocate argument space");
596 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
597 p->p_vmspace->vm_ssize = 1;
598
599 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
600 /*
601 * Move out the boot flag argument.
602 */
603 options = 0;
604 ucp = (char *)USRSTACK;
605 (void)subyte(--ucp, 0); /* trailing zero */
606 if (boothowto & RB_SINGLE) {
607 (void)subyte(--ucp, 's');
608 options = 1;
609 }
610 #ifdef notyet
611 if (boothowto & RB_FASTBOOT) {
612 (void)subyte(--ucp, 'f');
613 options = 1;
614 }
615 #endif
616
617 #ifdef BOOTCDROM
618 (void)subyte(--ucp, 'C');
619 options = 1;
620 #endif
621
622 #if defined(DEVFS) && defined(DEVFS_ROOT)
623 (void)subyte(--ucp, 'd');
624 options = 1;
625 #endif
626 if (options == 0)
627 (void)subyte(--ucp, '-');
628 (void)subyte(--ucp, '-'); /* leading hyphen */
629 arg1 = ucp;
630
631 /*
632 * Move out the file name (also arg 0).
633 */
634 for (i = strlen(path) + 1; i >= 0; i--)
635 (void)subyte(--ucp, path[i]);
636 arg0 = ucp;
637
638 /*
639 * Move out the arg pointers.
640 */
641 uap = (char **)((int)ucp & ~(NBPW-1));
642 (void)suword((caddr_t)--uap, 0); /* terminator */
643 (void)suword((caddr_t)--uap, (int)arg1);
644 (void)suword((caddr_t)--uap, (int)arg0);
645
646 /*
647 * Point at the arguments.
648 */
649 args.fname = arg0;
650 args.argv = uap;
651 args.envv = NULL;
652
653 /*
654 * Now try to exec the program. If can't for any reason
655 * other than it doesn't exist, complain.
656 *
657 * Otherwise return to main() which returns to btext
658 * which completes the system startup.
659 */
660 if ((error = execve(p, &args, &retval[0])) == 0)
661 return;
662 if (error != ENOENT)
663 printf("exec %s: error %d\n", path, error);
664 }
665 printf("init: not found\n");
666 panic("no init");
667 }
Cache object: 704bf6e14d2848abcc30f6d9ec2f25ac
|