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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since be8032d was 73a8881, checked in by Jennifer Averett <jennifer.averett@…>, on 05/01/12 at 18:52:03

Added rtems_bsd_thread0_ucred as a single global ucred.

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