source: rtems-libbsd/freebsd/sys/kern/init_main.c @ 53b03a1

4.115-freebsd-12freebsd-9.3
Last change on this file since 53b03a1 was 53b03a1, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 24, 2015 at 9:09:43 AM

SLEEP(9): Port to RTEMS

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