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

55-freebsd-126-freebsd-12
Last change on this file since b42dea9 was b42dea9, checked in by Sebastian Huber <sebastian.huber@…>, on 09/25/18 at 07:27:50

CONFIG_INTRHOOK(9): Port to RTEMS

Some device drivers (e.g. MMC) need a complex intialization with working
callouts. Remove the dummy CONFIG_INTRHOOK() implementation and replace
it with the real one from FreeBSD. Make sure TIMEOUT(9) services work
at this point.

Update #3525.

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