source: rtems-libbsd/freebsd/kern/init_main.c @ 20b5ca1

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 20b5ca1 was 20b5ca1, checked in by Jennifer Averett <jennifer.averett@…>, on 05/30/12 at 16:49:04

Added support for initialization of uihashinit().

  • Property mode set to 100644
File size: 22.3 KB
Line 
1#include <freebsd/machine/rtems-bsd-config.h>
2
3/*-
4 * Copyright (c) 1995 Terrence R. Lambert
5 * All rights reserved.
6 *
7 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
8 *      The Regents of the University of California.  All rights reserved.
9 * (c) UNIX System Laboratories, Inc.
10 * All or some portions of this file are derived from material licensed
11 * to the University of California by American Telephone and Telegraph
12 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
13 * the permission of UNIX System Laboratories, Inc.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 *    must display the following acknowledgement:
25 *      This product includes software developed by the University of
26 *      California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 *    may be used to endorse or promote products derived from this software
29 *    without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 *
43 *      @(#)init_main.c 8.9 (Berkeley) 1/21/94
44 */
45
46#include <freebsd/sys/cdefs.h>
47__FBSDID("$FreeBSD$");
48
49#include <freebsd/local/opt_ddb.h>
50#include <freebsd/local/opt_init_path.h>
51
52#include <freebsd/sys/param.h>
53#include <freebsd/sys/kernel.h>
54#include <freebsd/sys/exec.h>
55#include <freebsd/sys/file.h>
56#include <freebsd/sys/filedesc.h>
57#include <freebsd/sys/jail.h>
58#include <freebsd/sys/ktr.h>
59#include <freebsd/sys/lock.h>
60#include <freebsd/sys/mount.h>
61#include <freebsd/sys/mutex.h>
62#include <freebsd/sys/syscallsubr.h>
63#include <freebsd/sys/sysctl.h>
64#include <freebsd/sys/proc.h>
65#include <freebsd/sys/resourcevar.h>
66#include <freebsd/sys/systm.h>
67#include <freebsd/sys/signalvar.h>
68#include <freebsd/sys/vnode.h>
69#include <freebsd/sys/sysent.h>
70#include <freebsd/sys/reboot.h>
71#include <freebsd/sys/sched.h>
72#include <freebsd/sys/sx.h>
73#include <freebsd/sys/sysproto.h>
74#include <freebsd/sys/vmmeter.h>
75#include <freebsd/sys/unistd.h>
76#include <freebsd/sys/malloc.h>
77#include <freebsd/sys/conf.h>
78#include <freebsd/sys/cpuset.h>
79
80#include <freebsd/machine/cpu.h>
81
82#include <freebsd/security/audit/audit.h>
83#include <freebsd/security/mac/mac_framework.h>
84
85#include <freebsd/vm/vm.h>
86#include <freebsd/vm/vm_param.h>
87#include <freebsd/vm/pmap.h>
88#include <freebsd/vm/vm_map.h>
89#include <freebsd/sys/copyright.h>
90
91#include <freebsd/ddb/ddb.h>
92#include <freebsd/ddb/db_sym.h>
93
94void mi_startup(void);                          /* Should be elsewhere */
95
96#ifndef __rtems__
97/* Components of the first process -- never freed. */
98static struct session session0;
99static struct pgrp pgrp0;
100struct  proc proc0;
101struct  thread thread0 __aligned(16);
102struct  vmspace vmspace0;
103struct  proc *initproc;
104
105int     boothowto = 0;          /* initialized so that it can be patched */
106SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0, "");
107int     bootverbose;
108SYSCTL_INT(_debug, OID_AUTO, bootverbose, CTLFLAG_RW, &bootverbose, 0, "");
109
110/*
111 * This ensures that there is at least one entry so that the sysinit_set
112 * symbol is not undefined.  A sybsystem ID of SI_SUB_DUMMY is never
113 * executed.
114 */
115SYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL);
116
117/*
118 * The sysinit table itself.  Items are checked off as the are run.
119 * If we want to register new sysinit types, add them to newsysinit.
120 */
121#endif /* __rtems__ */
122SET_DECLARE(sysinit_set, struct sysinit);
123#ifndef __rtems__
124struct sysinit **sysinit, **sysinit_end;
125struct sysinit **newsysinit, **newsysinit_end;
126
127/*
128 * Merge a new sysinit set into the current set, reallocating it if
129 * necessary.  This can only be called after malloc is running.
130 */
131void
132sysinit_add(struct sysinit **set, struct sysinit **set_end)
133{
134        struct sysinit **newset;
135        struct sysinit **sipp;
136        struct sysinit **xipp;
137        int count;
138
139        count = set_end - set;
140        if (newsysinit)
141                count += newsysinit_end - newsysinit;
142        else
143                count += sysinit_end - sysinit;
144        newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT);
145        if (newset == NULL)
146                panic("cannot malloc for sysinit");
147        xipp = newset;
148        if (newsysinit)
149                for (sipp = newsysinit; sipp < newsysinit_end; sipp++)
150                        *xipp++ = *sipp;
151        else
152                for (sipp = sysinit; sipp < sysinit_end; sipp++)
153                        *xipp++ = *sipp;
154        for (sipp = set; sipp < set_end; sipp++)
155                *xipp++ = *sipp;
156        if (newsysinit)
157                free(newsysinit, M_TEMP);
158        newsysinit = newset;
159        newsysinit_end = newset + count;
160}
161#endif /* __rtems__ */
162
163/*
164 * System startup; initialize the world, create process 0, mount root
165 * filesystem, and fork to create init and pagedaemon.  Most of the
166 * hard work is done in the lower-level initialization routines including
167 * startup(), which does memory initialization and autoconfiguration.
168 *
169 * This allows simple addition of new kernel subsystems that require
170 * boot time initialization.  It also allows substitution of subsystem
171 * (for instance, a scheduler, kernel profiler, or VM system) by object
172 * module.  Finally, it allows for optional "kernel threads".
173 */
174void
175mi_startup(void)
176{
177
178        register struct sysinit **sipp;         /* system initialization*/
179        register struct sysinit **xipp;         /* interior loop of sort*/
180        register struct sysinit *save;          /* bubble*/
181#ifdef __rtems__
182        struct sysinit **sysinit = NULL;
183        struct sysinit **sysinit_end = NULL;
184#endif /* __rtems__ */
185
186#define VERBOSE_SYSINIT
187#if defined(VERBOSE_SYSINIT)
188        int last;
189        int verbose;
190#endif
191
192#ifndef __rtems__
193        if (boothowto & RB_VERBOSE)
194                bootverbose++;
195#endif /* __rtems__ */
196
197        if (sysinit == NULL) {
198                sysinit = SET_BEGIN(sysinit_set);
199                sysinit_end = SET_LIMIT(sysinit_set);
200        }
201
202restart:
203        /*
204         * Perform a bubble sort of the system initialization objects by
205         * their subsystem (primary key) and order (secondary key).
206         */
207        for (sipp = sysinit; sipp < sysinit_end; sipp++) {
208                for (xipp = sipp + 1; xipp < sysinit_end; xipp++) {
209                        if ((*sipp)->subsystem < (*xipp)->subsystem ||
210                             ((*sipp)->subsystem == (*xipp)->subsystem &&
211                              (*sipp)->order <= (*xipp)->order))
212                                continue;       /* skip*/
213                        save = *sipp;
214                        *sipp = *xipp;
215                        *xipp = save;
216                }
217        }
218
219#if defined(VERBOSE_SYSINIT)
220        last = SI_SUB_COPYRIGHT;
221        verbose = 0;
222#if !defined(DDB)
223        printf("VERBOSE_SYSINIT: DDB not enabled, symbol lookups disabled.\n");
224#endif
225#endif
226
227        /*
228         * Traverse the (now) ordered list of system initialization tasks.
229         * Perform each task, and continue on to the next task.
230         *
231         * The last item on the list is expected to be the scheduler,
232         * which will not return.
233         */
234        for (sipp = sysinit; sipp < sysinit_end; sipp++) {
235
236                if ((*sipp)->subsystem == SI_SUB_DUMMY)
237                        continue;       /* skip dummy task(s)*/
238
239                if ((*sipp)->subsystem == SI_SUB_DONE)
240                        continue;
241
242#if defined(VERBOSE_SYSINIT)
243                if ((*sipp)->subsystem > last) {
244                        verbose = 1;
245                        last = (*sipp)->subsystem;
246                        printf("subsystem %x\n", last);
247                }
248                if (verbose) {
249#if defined(DDB)
250                        const char *name;
251                        c_db_sym_t sym;
252                        db_expr_t  offset;
253
254                        sym = db_search_symbol((vm_offset_t)(*sipp)->func,
255                            DB_STGY_PROC, &offset);
256                        db_symbol_values(sym, &name, NULL);
257                        if (name != NULL)
258                                printf("   %s(%p)... ", name, (*sipp)->udata);
259                        else
260#endif
261                                printf("   %p(%p)... ", (*sipp)->func,
262                                    (*sipp)->udata);
263                }
264#endif
265
266                /* Call function */
267                (*((*sipp)->func))((*sipp)->udata);
268
269#if defined(VERBOSE_SYSINIT)
270                if (verbose)
271                        printf("done.\n");
272#endif
273
274                /* Check off the one we're just done */
275                (*sipp)->subsystem = SI_SUB_DONE;
276
277#ifndef __rtems__
278                /* Check if we've installed more sysinit items via KLD */
279                if (newsysinit != NULL) {
280                        if (sysinit != SET_BEGIN(sysinit_set))
281                                free(sysinit, M_TEMP);
282                        sysinit = newsysinit;
283                        sysinit_end = newsysinit_end;
284                        newsysinit = NULL;
285                        newsysinit_end = NULL;
286                        goto restart;
287                }
288#endif /* __rtems__ */
289        }
290
291#ifndef __rtems__
292        panic("Shouldn't get here!");
293        /* NOTREACHED*/
294#endif /* __rtems__ */
295}
296
297
298#ifndef __rtems__
299/*
300 ***************************************************************************
301 ****
302 **** The following SYSINIT's belong elsewhere, but have not yet
303 **** been moved.
304 ****
305 ***************************************************************************
306 */
307static void
308print_caddr_t(void *data)
309{
310        printf("%s", (char *)data);
311}
312
313static void
314print_version(void *data __unused)
315{
316        int len;
317
318        /* Strip a trailing newline from version. */
319        len = strlen(version);
320        while (len > 0 && version[len - 1] == '\n')
321                len--;
322        printf("%.*s %s\n", len, version, machine);
323}
324
325SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t,
326    copyright);
327SYSINIT(trademark, SI_SUB_COPYRIGHT, SI_ORDER_SECOND, print_caddr_t,
328    trademark);
329SYSINIT(version, SI_SUB_COPYRIGHT, SI_ORDER_THIRD, print_version, NULL);
330
331#ifdef WITNESS
332static char wit_warn[] =
333     "WARNING: WITNESS option enabled, expect reduced performance.\n";
334SYSINIT(witwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 1,
335   print_caddr_t, wit_warn);
336SYSINIT(witwarn2, SI_SUB_RUN_SCHEDULER, SI_ORDER_THIRD + 1,
337   print_caddr_t, wit_warn);
338#endif
339
340#ifdef DIAGNOSTIC
341static char diag_warn[] =
342     "WARNING: DIAGNOSTIC option enabled, expect reduced performance.\n";
343SYSINIT(diagwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 2,
344    print_caddr_t, diag_warn);
345SYSINIT(diagwarn2, SI_SUB_RUN_SCHEDULER, SI_ORDER_THIRD + 2,
346    print_caddr_t, diag_warn);
347#endif
348
349static int
350null_fetch_syscall_args(struct thread *td __unused,
351    struct syscall_args *sa __unused)
352{
353
354        panic("null_fetch_syscall_args");
355}
356
357static void
358null_set_syscall_retval(struct thread *td __unused, int error __unused)
359{
360
361        panic("null_set_syscall_retval");
362}
363
364struct sysentvec null_sysvec = {
365        .sv_size        = 0,
366        .sv_table       = NULL,
367        .sv_mask        = 0,
368        .sv_sigsize     = 0,
369        .sv_sigtbl      = NULL,
370        .sv_errsize     = 0,
371        .sv_errtbl      = NULL,
372        .sv_transtrap   = NULL,
373        .sv_fixup       = NULL,
374        .sv_sendsig     = NULL,
375        .sv_sigcode     = NULL,
376        .sv_szsigcode   = NULL,
377        .sv_prepsyscall = NULL,
378        .sv_name        = "null",
379        .sv_coredump    = NULL,
380        .sv_imgact_try  = NULL,
381        .sv_minsigstksz = 0,
382        .sv_pagesize    = PAGE_SIZE,
383        .sv_minuser     = VM_MIN_ADDRESS,
384        .sv_maxuser     = VM_MAXUSER_ADDRESS,
385        .sv_usrstack    = USRSTACK,
386        .sv_psstrings   = PS_STRINGS,
387        .sv_stackprot   = VM_PROT_ALL,
388        .sv_copyout_strings     = NULL,
389        .sv_setregs     = NULL,
390        .sv_fixlimit    = NULL,
391        .sv_maxssiz     = NULL,
392        .sv_flags       = 0,
393        .sv_set_syscall_retval = null_set_syscall_retval,
394        .sv_fetch_syscall_args = null_fetch_syscall_args,
395        .sv_syscallnames = NULL,
396};
397#endif /* __rtems__ */
398
399/*
400 ***************************************************************************
401 ****
402 **** The two following SYSINIT's are proc0 specific glue code.  I am not
403 **** convinced that they can not be safely combined, but their order of
404 **** operation has been maintained as the same as the original init_main.c
405 **** for right now.
406 ****
407 **** These probably belong in init_proc.c or kern_proc.c, since they
408 **** deal with proc0 (the fork template process).
409 ****
410 ***************************************************************************
411 */
412/* ARGSUSED*/
413static void
414proc0_init(void *dummy __unused)
415{
416#ifndef __rtems__
417        struct proc *p;
418        unsigned i;
419        struct thread *td;
420
421        GIANT_REQUIRED;
422        p = &proc0;
423        td = &thread0;
424       
425        /*
426         * Initialize magic number and osrel.
427         */
428        p->p_magic = P_MAGIC;
429        p->p_osrel = osreldate;
430#endif /* __rtems__ */
431
432        /*
433         * Initialize thread and process structures.
434         */
435        procinit();     /* set up proc zone */
436#ifndef __rtems__
437        threadinit();   /* set up UMA zones */
438
439        /*
440         * Initialise scheduler resources.
441         * Add scheduler specific parts to proc, thread as needed.
442         */
443        schedinit();    /* scheduler gets its house in order */
444        /*
445         * Initialize sleep queue hash table
446         */
447        sleepinit();
448
449        /*
450         * additional VM structures
451         */
452        vm_init2();
453
454        /*
455         * Create process 0 (the swapper).
456         */
457        LIST_INSERT_HEAD(&allproc, p, p_list);
458        LIST_INSERT_HEAD(PIDHASH(0), p, p_hash);
459        mtx_init(&pgrp0.pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
460        p->p_pgrp = &pgrp0;
461        LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
462        LIST_INIT(&pgrp0.pg_members);
463        LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
464
465        pgrp0.pg_session = &session0;
466        mtx_init(&session0.s_mtx, "session", NULL, MTX_DEF);
467        refcount_init(&session0.s_count, 1);
468        session0.s_leader = p;
469
470        p->p_sysent = &null_sysvec;
471        p->p_flag = P_SYSTEM | P_INMEM;
472        p->p_state = PRS_NORMAL;
473        knlist_init_mtx(&p->p_klist, &p->p_mtx);
474        STAILQ_INIT(&p->p_ktr);
475        p->p_nice = NZERO;
476        td->td_tid = PID_MAX + 1;
477        td->td_state = TDS_RUNNING;
478        td->td_pri_class = PRI_TIMESHARE;
479        td->td_user_pri = PUSER;
480        td->td_base_user_pri = PUSER;
481        td->td_priority = PVM;
482        td->td_base_pri = PUSER;
483        td->td_oncpu = 0;
484        td->td_flags = TDF_INMEM|TDP_KTHREAD;
485        td->td_cpuset = cpuset_thread0();
486        prison0.pr_cpuset = cpuset_ref(td->td_cpuset);
487        p->p_peers = 0;
488        p->p_leader = p;
489
490
491        strncpy(p->p_comm, "kernel", sizeof (p->p_comm));
492        strncpy(td->td_name, "swapper", sizeof (td->td_name));
493
494        callout_init(&p->p_itcallout, CALLOUT_MPSAFE);
495        callout_init_mtx(&p->p_limco, &p->p_mtx, 0);
496        callout_init(&td->td_slpcallout, CALLOUT_MPSAFE);
497
498        /* Create credentials. */
499        p->p_ucred = crget();
500        p->p_ucred->cr_ngroups = 1;     /* group 0 */
501        p->p_ucred->cr_uidinfo = uifind(0);
502        p->p_ucred->cr_ruidinfo = uifind(0);
503        p->p_ucred->cr_prison = &prison0;
504#ifdef AUDIT
505        audit_cred_kproc0(p->p_ucred);
506#endif
507#ifdef MAC
508        mac_cred_create_swapper(p->p_ucred);
509#endif
510        td->td_ucred = crhold(p->p_ucred);
511
512        /* Create sigacts. */
513        p->p_sigacts = sigacts_alloc();
514
515        /* Initialize signal state for process 0. */
516        siginit(&proc0);
517
518        /* Create the file descriptor table. */
519        p->p_fd = fdinit(NULL);
520        p->p_fdtol = NULL;
521
522        /* Create the limits structures. */
523        p->p_limit = lim_alloc();
524        for (i = 0; i < RLIM_NLIMITS; i++)
525                p->p_limit->pl_rlimit[i].rlim_cur =
526                    p->p_limit->pl_rlimit[i].rlim_max = RLIM_INFINITY;
527        p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur =
528            p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
529        p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_cur =
530            p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
531        i = ptoa(cnt.v_free_count);
532        p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_max = i;
533        p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
534        p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
535        p->p_cpulimit = RLIM_INFINITY;
536
537        p->p_stats = pstats_alloc();
538
539        /* Allocate a prototype map so we have something to fork. */
540        pmap_pinit0(vmspace_pmap(&vmspace0));
541        p->p_vmspace = &vmspace0;
542        vmspace0.vm_refcnt = 1;
543
544        /*
545         * proc0 is not expected to enter usermode, so there is no special
546         * handling for sv_minuser here, like is done for exec_new_vmspace().
547         */
548        vm_map_init(&vmspace0.vm_map, p->p_sysent->sv_minuser,
549            p->p_sysent->sv_maxuser);
550        vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0);
551
552        /*-
553         * call the init and ctor for the new thread and proc
554         * we wait to do this until all other structures
555         * are fairly sane.
556         */
557        EVENTHANDLER_INVOKE(process_init, p);
558        EVENTHANDLER_INVOKE(thread_init, td);
559        EVENTHANDLER_INVOKE(process_ctor, p);
560        EVENTHANDLER_INVOKE(thread_ctor, td);
561
562        /*
563         * Charge root for one process.
564         */
565        (void)chgproccnt(p->p_ucred->cr_ruidinfo, 1, 0);
566#endif /* __rtems__ */
567}
568SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL);
569
570#ifndef __rtems__
571/* ARGSUSED*/
572static void
573proc0_post(void *dummy __unused)
574{
575        struct timespec ts;
576        struct proc *p;
577        struct rusage ru;
578        struct thread *td;
579
580        /*
581         * Now we can look at the time, having had a chance to verify the
582         * time from the filesystem.  Pretend that proc0 started now.
583         */
584        sx_slock(&allproc_lock);
585        FOREACH_PROC_IN_SYSTEM(p) {
586                microuptime(&p->p_stats->p_start);
587                PROC_SLOCK(p);
588                rufetch(p, &ru);        /* Clears thread stats */
589                PROC_SUNLOCK(p);
590                p->p_rux.rux_runtime = 0;
591                p->p_rux.rux_uticks = 0;
592                p->p_rux.rux_sticks = 0;
593                p->p_rux.rux_iticks = 0;
594                FOREACH_THREAD_IN_PROC(p, td) {
595                        td->td_runtime = 0;
596                }
597        }
598        sx_sunlock(&allproc_lock);
599        PCPU_SET(switchtime, cpu_ticks());
600        PCPU_SET(switchticks, ticks);
601
602        /*
603         * Give the ``random'' number generator a thump.
604         */
605        nanotime(&ts);
606        srandom(ts.tv_sec ^ ts.tv_nsec);
607}
608SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL);
609
610static void
611random_init(void *dummy __unused)
612{
613
614        /*
615         * After CPU has been started we have some randomness on most
616         * platforms via get_cyclecount().  For platforms that don't
617         * we will reseed random(9) in proc0_post() as well.
618         */
619        srandom(get_cyclecount());
620}
621SYSINIT(random, SI_SUB_RANDOM, SI_ORDER_FIRST, random_init, NULL);
622
623/*
624 ***************************************************************************
625 ****
626 **** The following SYSINIT's and glue code should be moved to the
627 **** respective files on a per subsystem basis.
628 ****
629 ***************************************************************************
630 */
631
632
633/*
634 ***************************************************************************
635 ****
636 **** The following code probably belongs in another file, like
637 **** kern/init_init.c.
638 ****
639 ***************************************************************************
640 */
641
642/*
643 * List of paths to try when searching for "init".
644 */
645static char init_path[MAXPATHLEN] =
646#ifdef  INIT_PATH
647    __XSTRING(INIT_PATH);
648#else
649    "/sbin/init:/sbin/oinit:/sbin/init.bak:/rescue/init:/stand/sysinstall";
650#endif
651SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0,
652        "Path used to search the init process");
653
654/*
655 * Shutdown timeout of init(8).
656 * Unused within kernel, but used to control init(8), hence do not remove.
657 */
658#ifndef INIT_SHUTDOWN_TIMEOUT
659#define INIT_SHUTDOWN_TIMEOUT 120
660#endif
661static int init_shutdown_timeout = INIT_SHUTDOWN_TIMEOUT;
662SYSCTL_INT(_kern, OID_AUTO, init_shutdown_timeout,
663        CTLFLAG_RW, &init_shutdown_timeout, 0, "");
664
665/*
666 * Start the initial user process; try exec'ing each pathname in init_path.
667 * The program is invoked with one argument containing the boot flags.
668 */
669static void
670start_init(void *dummy)
671{
672        vm_offset_t addr;
673        struct execve_args args;
674        int options, error;
675        char *var, *path, *next, *s;
676        char *ucp, **uap, *arg0, *arg1;
677        struct thread *td;
678        struct proc *p;
679
680        mtx_lock(&Giant);
681
682        GIANT_REQUIRED;
683
684        td = curthread;
685        p = td->td_proc;
686
687        vfs_mountroot();
688
689        /*
690         * Need just enough stack to hold the faked-up "execve()" arguments.
691         */
692        addr = p->p_sysent->sv_usrstack - PAGE_SIZE;
693        if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE,
694                        FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
695                panic("init: couldn't allocate argument space");
696        p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
697        p->p_vmspace->vm_ssize = 1;
698
699        if ((var = getenv("init_path")) != NULL) {
700                strlcpy(init_path, var, sizeof(init_path));
701                freeenv(var);
702        }
703       
704        for (path = init_path; *path != '\0'; path = next) {
705                while (*path == ':')
706                        path++;
707                if (*path == '\0')
708                        break;
709                for (next = path; *next != '\0' && *next != ':'; next++)
710                        /* nothing */ ;
711                if (bootverbose)
712                        printf("start_init: trying %.*s\n", (int)(next - path),
713                            path);
714                       
715                /*
716                 * Move out the boot flag argument.
717                 */
718                options = 0;
719                ucp = (char *)p->p_sysent->sv_usrstack;
720                (void)subyte(--ucp, 0);         /* trailing zero */
721                if (boothowto & RB_SINGLE) {
722                        (void)subyte(--ucp, 's');
723                        options = 1;
724                }
725#ifdef notyet
726                if (boothowto & RB_FASTBOOT) {
727                        (void)subyte(--ucp, 'f');
728                        options = 1;
729                }
730#endif
731
732#ifdef BOOTCDROM
733                (void)subyte(--ucp, 'C');
734                options = 1;
735#endif
736
737                if (options == 0)
738                        (void)subyte(--ucp, '-');
739                (void)subyte(--ucp, '-');               /* leading hyphen */
740                arg1 = ucp;
741
742                /*
743                 * Move out the file name (also arg 0).
744                 */
745                (void)subyte(--ucp, 0);
746                for (s = next - 1; s >= path; s--)
747                        (void)subyte(--ucp, *s);
748                arg0 = ucp;
749
750                /*
751                 * Move out the arg pointers.
752                 */
753                uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1));
754                (void)suword((caddr_t)--uap, (long)0);  /* terminator */
755                (void)suword((caddr_t)--uap, (long)(intptr_t)arg1);
756                (void)suword((caddr_t)--uap, (long)(intptr_t)arg0);
757
758                /*
759                 * Point at the arguments.
760                 */
761                args.fname = arg0;
762                args.argv = uap;
763                args.envv = NULL;
764
765                /*
766                 * Now try to exec the program.  If can't for any reason
767                 * other than it doesn't exist, complain.
768                 *
769                 * Otherwise, return via fork_trampoline() all the way
770                 * to user mode as init!
771                 */
772                if ((error = execve(td, &args)) == 0) {
773                        mtx_unlock(&Giant);
774                        return;
775                }
776                if (error != ENOENT)
777                        printf("exec %.*s: error %d\n", (int)(next - path),
778                            path, error);
779        }
780        printf("init: not found in path %s\n", init_path);
781        panic("no init");
782}
783
784/*
785 * Like kproc_create(), but runs in it's own address space.
786 * We do this early to reserve pid 1.
787 *
788 * Note special case - do not make it runnable yet.  Other work
789 * in progress will change this more.
790 */
791static void
792create_init(const void *udata __unused)
793{
794        struct ucred *newcred, *oldcred;
795        int error;
796
797        error = fork1(&thread0, RFFDG | RFPROC | RFSTOPPED, 0, &initproc);
798        if (error)
799                panic("cannot fork init: %d\n", error);
800        KASSERT(initproc->p_pid == 1, ("create_init: initproc->p_pid != 1"));
801        /* divorce init's credentials from the kernel's */
802        newcred = crget();
803        PROC_LOCK(initproc);
804        initproc->p_flag |= P_SYSTEM | P_INMEM;
805        oldcred = initproc->p_ucred;
806        crcopy(newcred, oldcred);
807#ifdef MAC
808        mac_cred_create_init(newcred);
809#endif
810#ifdef AUDIT
811        audit_cred_proc1(newcred);
812#endif
813        initproc->p_ucred = newcred;
814        PROC_UNLOCK(initproc);
815        crfree(oldcred);
816        cred_update_thread(FIRST_THREAD_IN_PROC(initproc));
817        cpu_set_fork_handler(FIRST_THREAD_IN_PROC(initproc), start_init, NULL);
818}
819SYSINIT(init, SI_SUB_CREATE_INIT, SI_ORDER_FIRST, create_init, NULL);
820
821/*
822 * Make it runnable now.
823 */
824static void
825kick_init(const void *udata __unused)
826{
827        struct thread *td;
828
829        td = FIRST_THREAD_IN_PROC(initproc);
830        thread_lock(td);
831        TD_SET_CAN_RUN(td);
832        sched_add(td, SRQ_BORING);
833        thread_unlock(td);
834}
835SYSINIT(kickinit, SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kick_init, NULL);
836#endif /* __rtems__ */
Note: See TracBrowser for help on using the repository browser.