1139804Simp/*- 210358Sjulian * Copyright (c) 1995 Terrence R. Lambert 310358Sjulian * All rights reserved. 410358Sjulian * 51541Srgrimes * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 61541Srgrimes * The Regents of the University of California. All rights reserved. 71541Srgrimes * (c) UNIX System Laboratories, Inc. 81541Srgrimes * All or some portions of this file are derived from material licensed 91541Srgrimes * to the University of California by American Telephone and Telegraph 101541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 111541Srgrimes * the permission of UNIX System Laboratories, Inc. 121541Srgrimes * 131541Srgrimes * Redistribution and use in source and binary forms, with or without 141541Srgrimes * modification, are permitted provided that the following conditions 151541Srgrimes * are met: 161541Srgrimes * 1. Redistributions of source code must retain the above copyright 171541Srgrimes * notice, this list of conditions and the following disclaimer. 181541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 191541Srgrimes * notice, this list of conditions and the following disclaimer in the 201541Srgrimes * documentation and/or other materials provided with the distribution. 211541Srgrimes * 3. All advertising materials mentioning features or use of this software 221541Srgrimes * must display the following acknowledgement: 231541Srgrimes * This product includes software developed by the University of 241541Srgrimes * California, Berkeley and its contributors. 251541Srgrimes * 4. Neither the name of the University nor the names of its contributors 261541Srgrimes * may be used to endorse or promote products derived from this software 271541Srgrimes * without specific prior written permission. 281541Srgrimes * 291541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 301541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 311541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 321541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 331541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 341541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 351541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 361541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 371541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 381541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 391541Srgrimes * SUCH DAMAGE. 401541Srgrimes * 411541Srgrimes * @(#)init_main.c 8.9 (Berkeley) 1/21/94 421541Srgrimes */ 431541Srgrimes 44116182Sobrien#include <sys/cdefs.h> 45116182Sobrien__FBSDID("$FreeBSD$"); 46116182Sobrien 47158558Sbenno#include "opt_ddb.h" 4846506Sjb#include "opt_init_path.h" 4914328Speter 501541Srgrimes#include <sys/param.h> 5189316Salfred#include <sys/kernel.h> 52102808Sjake#include <sys/exec.h> 5322039Sbde#include <sys/file.h> 541541Srgrimes#include <sys/filedesc.h> 55192895Sjamie#include <sys/jail.h> 5665557Sjasone#include <sys/ktr.h> 5774927Sjhb#include <sys/lock.h> 58219304Strasz#include <sys/loginclass.h> 5921776Sbde#include <sys/mount.h> 6067365Sjhb#include <sys/mutex.h> 61102779Siedowse#include <sys/syscallsubr.h> 6212623Sphk#include <sys/sysctl.h> 631541Srgrimes#include <sys/proc.h> 64220137Strasz#include <sys/racct.h> 651541Srgrimes#include <sys/resourcevar.h> 6665687Sdfr#include <sys/systm.h> 671541Srgrimes#include <sys/signalvar.h> 681541Srgrimes#include <sys/vnode.h> 692257Ssos#include <sys/sysent.h> 701541Srgrimes#include <sys/reboot.h> 71107126Sjeff#include <sys/sched.h> 7274927Sjhb#include <sys/sx.h> 7311332Sswallace#include <sys/sysproto.h> 7412662Sdg#include <sys/vmmeter.h> 7531675Sdyson#include <sys/unistd.h> 7640154Speter#include <sys/malloc.h> 7765374Sphk#include <sys/conf.h> 78176730Sjeff#include <sys/cpuset.h> 791541Srgrimes 801541Srgrimes#include <machine/cpu.h> 811541Srgrimes 82155196Srwatson#include <security/audit/audit.h> 83163606Srwatson#include <security/mac/mac_framework.h> 84155196Srwatson 851541Srgrimes#include <vm/vm.h> 8612662Sdg#include <vm/vm_param.h> 8712662Sdg#include <vm/pmap.h> 8812662Sdg#include <vm/vm_map.h> 8923244Swosch#include <sys/copyright.h> 901541Srgrimes 91158463Sbenno#include <ddb/ddb.h> 92158463Sbenno#include <ddb/db_sym.h> 93158463Sbenno 9464529Spetervoid mi_startup(void); /* Should be elsewhere */ 9510426Sbde 961541Srgrimes/* Components of the first process -- never freed. */ 9712725Sphkstatic struct session session0; 9812725Sphkstatic struct pgrp pgrp0; 991541Srgrimesstruct proc proc0; 100167944Sjhbstruct thread thread0 __aligned(16); 101113450Sjakestruct vmspace vmspace0; 10210358Sjulianstruct proc *initproc; 1031541Srgrimes 104249071Sbrooks#ifndef BOOTHOWTO 105249071Sbrooks#define BOOTHOWTO 0 106249071Sbrooks#endif 107249071Sbrooksint boothowto = BOOTHOWTO; /* initialized so that it can be patched */ 108211102SgavinSYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0, 109211102Sgavin "Boot control flags, passed from loader"); 110249071Sbrooks 111249071Sbrooks#ifndef BOOTVERBOSE 112249071Sbrooks#define BOOTVERBOSE 0 113249071Sbrooks#endif 114249071Sbrooksint bootverbose = BOOTVERBOSE; 115211102SgavinSYSCTL_INT(_debug, OID_AUTO, bootverbose, CTLFLAG_RW, &bootverbose, 0, 116211102Sgavin "Control the output of verbose kernel messages"); 11712623Sphk 11810358Sjulian/* 11910358Sjulian * This ensures that there is at least one entry so that the sysinit_set 12010358Sjulian * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never 12110358Sjulian * executed. 1221549Srgrimes */ 123177253SrwatsonSYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL); 1242320Sdg 12540154Speter/* 12640154Speter * The sysinit table itself. Items are checked off as the are run. 12740154Speter * If we want to register new sysinit types, add them to newsysinit. 12840154Speter */ 12978161SpeterSET_DECLARE(sysinit_set, struct sysinit); 13078161Speterstruct sysinit **sysinit, **sysinit_end; 13178161Speterstruct sysinit **newsysinit, **newsysinit_end; 1322320Sdg 1331549Srgrimes/* 13440154Speter * Merge a new sysinit set into the current set, reallocating it if 13540154Speter * necessary. This can only be called after malloc is running. 13640154Speter */ 13740154Spetervoid 13878161Spetersysinit_add(struct sysinit **set, struct sysinit **set_end) 13940154Speter{ 14040154Speter struct sysinit **newset; 14140154Speter struct sysinit **sipp; 14240154Speter struct sysinit **xipp; 14378161Speter int count; 14440154Speter 14578161Speter count = set_end - set; 14640394Speter if (newsysinit) 14778161Speter count += newsysinit_end - newsysinit; 14840394Speter else 14978161Speter count += sysinit_end - sysinit; 15040154Speter newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT); 15140154Speter if (newset == NULL) 15240154Speter panic("cannot malloc for sysinit"); 15340154Speter xipp = newset; 15440394Speter if (newsysinit) 15578161Speter for (sipp = newsysinit; sipp < newsysinit_end; sipp++) 15640394Speter *xipp++ = *sipp; 15740394Speter else 15878161Speter for (sipp = sysinit; sipp < sysinit_end; sipp++) 15940394Speter *xipp++ = *sipp; 16078161Speter for (sipp = set; sipp < set_end; sipp++) 16140154Speter *xipp++ = *sipp; 16240394Speter if (newsysinit) 16340394Speter free(newsysinit, M_TEMP); 16440154Speter newsysinit = newset; 16578161Speter newsysinit_end = newset + count; 16640154Speter} 16740154Speter 168236404Sjhb#if defined (DDB) && defined(VERBOSE_SYSINIT) 169236404Sjhbstatic const char * 170236404Sjhbsymbol_name(vm_offset_t va, db_strategy_t strategy) 171236404Sjhb{ 172236404Sjhb const char *name; 173236404Sjhb c_db_sym_t sym; 174236404Sjhb db_expr_t offset; 175236404Sjhb 176236404Sjhb if (va == 0) 177236404Sjhb return (NULL); 178236404Sjhb sym = db_search_symbol(va, strategy, &offset); 179236404Sjhb if (offset != 0) 180236404Sjhb return (NULL); 181236404Sjhb db_symbol_values(sym, &name, NULL); 182236404Sjhb return (name); 183236404Sjhb} 184236404Sjhb#endif 185236404Sjhb 18640154Speter/* 1871541Srgrimes * System startup; initialize the world, create process 0, mount root 1881541Srgrimes * filesystem, and fork to create init and pagedaemon. Most of the 1891541Srgrimes * hard work is done in the lower-level initialization routines including 1901541Srgrimes * startup(), which does memory initialization and autoconfiguration. 19110358Sjulian * 19210358Sjulian * This allows simple addition of new kernel subsystems that require 19310358Sjulian * boot time initialization. It also allows substitution of subsystem 19410358Sjulian * (for instance, a scheduler, kernel profiler, or VM system) by object 19532889Sphk * module. Finally, it allows for optional "kernel threads". 1961541Srgrimes */ 1971549Srgrimesvoid 19864529Spetermi_startup(void) 1991541Srgrimes{ 20010358Sjulian 20110358Sjulian register struct sysinit **sipp; /* system initialization*/ 20210358Sjulian register struct sysinit **xipp; /* interior loop of sort*/ 20310358Sjulian register struct sysinit *save; /* bubble*/ 20410358Sjulian 205158463Sbenno#if defined(VERBOSE_SYSINIT) 206158463Sbenno int last; 207158463Sbenno int verbose; 208158463Sbenno#endif 209158463Sbenno 210214449Sjhb if (boothowto & RB_VERBOSE) 211214449Sjhb bootverbose++; 212214449Sjhb 21378161Speter if (sysinit == NULL) { 21478161Speter sysinit = SET_BEGIN(sysinit_set); 21578161Speter sysinit_end = SET_LIMIT(sysinit_set); 21678161Speter } 21778161Speter 21840154Speterrestart: 21910358Sjulian /* 22010358Sjulian * Perform a bubble sort of the system initialization objects by 22110358Sjulian * their subsystem (primary key) and order (secondary key). 22210358Sjulian */ 22378161Speter for (sipp = sysinit; sipp < sysinit_end; sipp++) { 22478161Speter for (xipp = sipp + 1; xipp < sysinit_end; xipp++) { 22540154Speter if ((*sipp)->subsystem < (*xipp)->subsystem || 22640154Speter ((*sipp)->subsystem == (*xipp)->subsystem && 22764142Speter (*sipp)->order <= (*xipp)->order)) 22810358Sjulian continue; /* skip*/ 22910358Sjulian save = *sipp; 23010358Sjulian *sipp = *xipp; 23110358Sjulian *xipp = save; 23210358Sjulian } 23310358Sjulian } 23410358Sjulian 235158463Sbenno#if defined(VERBOSE_SYSINIT) 236158463Sbenno last = SI_SUB_COPYRIGHT; 237158463Sbenno verbose = 0; 238158463Sbenno#if !defined(DDB) 239158463Sbenno printf("VERBOSE_SYSINIT: DDB not enabled, symbol lookups disabled.\n"); 240158463Sbenno#endif 241158463Sbenno#endif 242158463Sbenno 24310358Sjulian /* 24410358Sjulian * Traverse the (now) ordered list of system initialization tasks. 24510358Sjulian * Perform each task, and continue on to the next task. 24610358Sjulian */ 24778161Speter for (sipp = sysinit; sipp < sysinit_end; sipp++) { 24831675Sdyson 24940154Speter if ((*sipp)->subsystem == SI_SUB_DUMMY) 25010358Sjulian continue; /* skip dummy task(s)*/ 25110358Sjulian 25240154Speter if ((*sipp)->subsystem == SI_SUB_DONE) 25340154Speter continue; 25440154Speter 255158463Sbenno#if defined(VERBOSE_SYSINIT) 256158463Sbenno if ((*sipp)->subsystem > last) { 257158463Sbenno verbose = 1; 258158463Sbenno last = (*sipp)->subsystem; 259158463Sbenno printf("subsystem %x\n", last); 260158463Sbenno } 261158463Sbenno if (verbose) { 262158463Sbenno#if defined(DDB) 263236404Sjhb const char *func, *data; 264158463Sbenno 265236404Sjhb func = symbol_name((vm_offset_t)(*sipp)->func, 266236404Sjhb DB_STGY_PROC); 267236404Sjhb data = symbol_name((vm_offset_t)(*sipp)->udata, 268236404Sjhb DB_STGY_ANY); 269236404Sjhb if (func != NULL && data != NULL) 270236404Sjhb printf(" %s(&%s)... ", func, data); 271236404Sjhb else if (func != NULL) 272236404Sjhb printf(" %s(%p)... ", func, (*sipp)->udata); 273158463Sbenno else 274158463Sbenno#endif 275158463Sbenno printf(" %p(%p)... ", (*sipp)->func, 276158463Sbenno (*sipp)->udata); 277158463Sbenno } 278158463Sbenno#endif 279158463Sbenno 28048391Speter /* Call function */ 28148391Speter (*((*sipp)->func))((*sipp)->udata); 28210358Sjulian 283158463Sbenno#if defined(VERBOSE_SYSINIT) 284158463Sbenno if (verbose) 285158463Sbenno printf("done.\n"); 286158463Sbenno#endif 287158463Sbenno 28840154Speter /* Check off the one we're just done */ 28940154Speter (*sipp)->subsystem = SI_SUB_DONE; 29040154Speter 29140154Speter /* Check if we've installed more sysinit items via KLD */ 29240154Speter if (newsysinit != NULL) { 29378161Speter if (sysinit != SET_BEGIN(sysinit_set)) 29440154Speter free(sysinit, M_TEMP); 29540154Speter sysinit = newsysinit; 29678161Speter sysinit_end = newsysinit_end; 29740154Speter newsysinit = NULL; 29878161Speter newsysinit_end = NULL; 29940154Speter goto restart; 30040154Speter } 30110358Sjulian } 30210358Sjulian 303253604Savg mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED); 304253604Savg mtx_unlock(&Giant); 305253604Savg 306253604Savg /* 307253604Savg * Now hand over this thread to swapper. 308253604Savg */ 309253604Savg swapper(); 31010358Sjulian /* NOTREACHED*/ 31110358Sjulian} 31210358Sjulian 31310358Sjulian 31410358Sjulian/* 31510358Sjulian *************************************************************************** 31610358Sjulian **** 31710358Sjulian **** The following SYSINIT's belong elsewhere, but have not yet 31810358Sjulian **** been moved. 31910358Sjulian **** 32010358Sjulian *************************************************************************** 32110358Sjulian */ 32210426Sbdestatic void 323197641Savgprint_caddr_t(void *data) 32410426Sbde{ 32510426Sbde printf("%s", (char *)data); 32610426Sbde} 327197658Savg 328197658Savgstatic void 329197658Savgprint_version(void *data __unused) 330197658Savg{ 331197658Savg int len; 332197658Savg 333197658Savg /* Strip a trailing newline from version. */ 334197658Savg len = strlen(version); 335197658Savg while (len > 0 && version[len - 1] == '\n') 336197658Savg len--; 337197658Savg printf("%.*s %s\n", len, version, machine); 338246246Savg printf("%s\n", compiler_version); 339197658Savg} 340197658Savg 341177253SrwatsonSYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, 342177253Srwatson copyright); 343177253SrwatsonSYSINIT(trademark, SI_SUB_COPYRIGHT, SI_ORDER_SECOND, print_caddr_t, 344177253Srwatson trademark); 345197658SavgSYSINIT(version, SI_SUB_COPYRIGHT, SI_ORDER_THIRD, print_version, NULL); 34610358Sjulian 347126410Sphk#ifdef WITNESS 348126410Sphkstatic char wit_warn[] = 349126410Sphk "WARNING: WITNESS option enabled, expect reduced performance.\n"; 350162639SrwatsonSYSINIT(witwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 1, 351177253Srwatson print_caddr_t, wit_warn); 352253604SavgSYSINIT(witwarn2, SI_SUB_LAST, SI_ORDER_THIRD + 1, 353177253Srwatson print_caddr_t, wit_warn); 354126410Sphk#endif 355126410Sphk 356126410Sphk#ifdef DIAGNOSTIC 357126410Sphkstatic char diag_warn[] = 358126410Sphk "WARNING: DIAGNOSTIC option enabled, expect reduced performance.\n"; 359162640SrwatsonSYSINIT(diagwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 2, 360177253Srwatson print_caddr_t, diag_warn); 361253604SavgSYSINIT(diagwarn2, SI_SUB_LAST, SI_ORDER_THIRD + 2, 362177253Srwatson print_caddr_t, diag_warn); 363126410Sphk#endif 364126410Sphk 365208453Skibstatic int 366208453Skibnull_fetch_syscall_args(struct thread *td __unused, 367208453Skib struct syscall_args *sa __unused) 368208453Skib{ 369208453Skib 370208453Skib panic("null_fetch_syscall_args"); 371208453Skib} 372208453Skib 373208453Skibstatic void 374208453Skibnull_set_syscall_retval(struct thread *td __unused, int error __unused) 375208453Skib{ 376208453Skib 377208453Skib panic("null_set_syscall_retval"); 378208453Skib} 379208453Skib 380102808Sjakestruct sysentvec null_sysvec = { 381183322Skib .sv_size = 0, 382183322Skib .sv_table = NULL, 383183322Skib .sv_mask = 0, 384183322Skib .sv_sigsize = 0, 385183322Skib .sv_sigtbl = NULL, 386183322Skib .sv_errsize = 0, 387183322Skib .sv_errtbl = NULL, 388183322Skib .sv_transtrap = NULL, 389183322Skib .sv_fixup = NULL, 390183322Skib .sv_sendsig = NULL, 391183322Skib .sv_sigcode = NULL, 392183322Skib .sv_szsigcode = NULL, 393183322Skib .sv_prepsyscall = NULL, 394183322Skib .sv_name = "null", 395183322Skib .sv_coredump = NULL, 396183322Skib .sv_imgact_try = NULL, 397183322Skib .sv_minsigstksz = 0, 398183322Skib .sv_pagesize = PAGE_SIZE, 399183322Skib .sv_minuser = VM_MIN_ADDRESS, 400183322Skib .sv_maxuser = VM_MAXUSER_ADDRESS, 401183322Skib .sv_usrstack = USRSTACK, 402183322Skib .sv_psstrings = PS_STRINGS, 403183322Skib .sv_stackprot = VM_PROT_ALL, 404183322Skib .sv_copyout_strings = NULL, 405183322Skib .sv_setregs = NULL, 406183322Skib .sv_fixlimit = NULL, 407208453Skib .sv_maxssiz = NULL, 408208453Skib .sv_flags = 0, 409208453Skib .sv_set_syscall_retval = null_set_syscall_retval, 410208453Skib .sv_fetch_syscall_args = null_fetch_syscall_args, 411208453Skib .sv_syscallnames = NULL, 412219405Sdchagin .sv_schedtail = NULL, 413102808Sjake}; 414100384Speter 41510358Sjulian/* 41610358Sjulian *************************************************************************** 41710358Sjulian **** 418107426Skeramida **** The two following SYSINIT's are proc0 specific glue code. I am not 41910358Sjulian **** convinced that they can not be safely combined, but their order of 42010358Sjulian **** operation has been maintained as the same as the original init_main.c 42110358Sjulian **** for right now. 42210358Sjulian **** 42310358Sjulian **** These probably belong in init_proc.c or kern_proc.c, since they 42410358Sjulian **** deal with proc0 (the fork template process). 42510358Sjulian **** 42610358Sjulian *************************************************************************** 42710358Sjulian */ 42810358Sjulian/* ARGSUSED*/ 42912725Sphkstatic void 43064529Speterproc0_init(void *dummy __unused) 43110358Sjulian{ 432137331Sphk struct proc *p; 43383366Sjulian struct thread *td; 434206483Salc vm_paddr_t pageablemem; 435206483Salc int i; 4361541Srgrimes 43779224Sdillon GIANT_REQUIRED; 4381541Srgrimes p = &proc0; 43990361Sjulian td = &thread0; 440173732Srrs 4411541Srgrimes /* 442174253Skib * Initialize magic number and osrel. 44365557Sjasone */ 44465557Sjasone p->p_magic = P_MAGIC; 445174253Skib p->p_osrel = osreldate; 44665557Sjasone 44765557Sjasone /* 448163709Sjb * Initialize thread and process structures. 449163709Sjb */ 450163709Sjb procinit(); /* set up proc zone */ 451163709Sjb threadinit(); /* set up UMA zones */ 452163709Sjb 453163709Sjb /* 454163709Sjb * Initialise scheduler resources. 455163709Sjb * Add scheduler specific parts to proc, thread as needed. 456163709Sjb */ 457134791Sjulian schedinit(); /* scheduler gets its house in order */ 45817366Sdg 45917366Sdg /* 4601541Srgrimes * Create process 0 (the swapper). 4611541Srgrimes */ 46214533Shsu LIST_INSERT_HEAD(&allproc, p, p_list); 46375423Sjhb LIST_INSERT_HEAD(PIDHASH(0), p, p_hash); 46493818Sjhb mtx_init(&pgrp0.pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 4651541Srgrimes p->p_pgrp = &pgrp0; 46614533Shsu LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 46714533Shsu LIST_INIT(&pgrp0.pg_members); 46814533Shsu LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 46914533Shsu 4701541Srgrimes pgrp0.pg_session = &session0; 47193818Sjhb mtx_init(&session0.s_mtx, "session", NULL, MTX_DEF); 472181905Sed refcount_init(&session0.s_count, 1); 4731541Srgrimes session0.s_leader = p; 4741541Srgrimes 475100384Speter p->p_sysent = &null_sysvec; 476172207Sjeff p->p_flag = P_SYSTEM | P_INMEM; 477255708Sjhb p->p_flag2 = 0; 47899072Sjulian p->p_state = PRS_NORMAL; 479193951Skib knlist_init_mtx(&p->p_klist, &p->p_mtx); 480152376Srwatson STAILQ_INIT(&p->p_ktr); 481130551Sjulian p->p_nice = NZERO; 482239328Skib /* pid_max cannot be greater than PID_MAX */ 483174848Sjulian td->td_tid = PID_MAX + 1; 484213950Sdavidxu LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash); 48599072Sjulian td->td_state = TDS_RUNNING; 486163709Sjb td->td_pri_class = PRI_TIMESHARE; 487163709Sjb td->td_user_pri = PUSER; 488164215Sdavidxu td->td_base_user_pri = PUSER; 489216313Sdavidxu td->td_lend_user_pri = PRI_MAX; 49099072Sjulian td->td_priority = PVM; 491217079Sjhb td->td_base_pri = PVM; 492113339Sjulian td->td_oncpu = 0; 493230455Spjd td->td_flags = TDF_INMEM; 494230455Spjd td->td_pflags = TDP_KTHREAD; 495176730Sjeff td->td_cpuset = cpuset_thread0(); 496192895Sjamie prison0.pr_cpuset = cpuset_ref(td->td_cpuset); 49726671Sdyson p->p_peers = 0; 49826671Sdyson p->p_leader = p; 49926671Sdyson 50099072Sjulian 501173004Sjulian strncpy(p->p_comm, "kernel", sizeof (p->p_comm)); 502173004Sjulian strncpy(td->td_name, "swapper", sizeof (td->td_name)); 5031541Srgrimes 504243869Skib callout_init_mtx(&p->p_itcallout, &p->p_mtx, 0); 505170174Sjeff callout_init_mtx(&p->p_limco, &p->p_mtx, 0); 506119137Ssam callout_init(&td->td_slpcallout, CALLOUT_MPSAFE); 50769286Sjake 5081541Srgrimes /* Create credentials. */ 5091541Srgrimes p->p_ucred = crget(); 5101541Srgrimes p->p_ucred->cr_ngroups = 1; /* group 0 */ 51165495Struckman p->p_ucred->cr_uidinfo = uifind(0); 51277183Srwatson p->p_ucred->cr_ruidinfo = uifind(0); 513192895Sjamie p->p_ucred->cr_prison = &prison0; 514219304Strasz p->p_ucred->cr_loginclass = loginclass_find("default"); 515155196Srwatson#ifdef AUDIT 516170407Srwatson audit_cred_kproc0(p->p_ucred); 517155196Srwatson#endif 518101001Srwatson#ifdef MAC 519184407Srwatson mac_cred_create_swapper(p->p_ucred); 520101001Srwatson#endif 52185525Sjhb td->td_ucred = crhold(p->p_ucred); 5221541Srgrimes 523114983Sjhb /* Create sigacts. */ 524114983Sjhb p->p_sigacts = sigacts_alloc(); 52541931Sjulian 52664529Speter /* Initialize signal state for process 0. */ 52764529Speter siginit(&proc0); 52864529Speter 5291541Srgrimes /* Create the file descriptor table. */ 530137331Sphk p->p_fd = fdinit(NULL); 531115702Stegge p->p_fdtol = NULL; 5321541Srgrimes 5331541Srgrimes /* Create the limits structures. */ 534125454Sjhb p->p_limit = lim_alloc(); 535125454Sjhb for (i = 0; i < RLIM_NLIMITS; i++) 536125454Sjhb p->p_limit->pl_rlimit[i].rlim_cur = 537125454Sjhb p->p_limit->pl_rlimit[i].rlim_max = RLIM_INFINITY; 538125454Sjhb p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur = 539125454Sjhb p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 540125454Sjhb p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_cur = 541125454Sjhb p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 542206483Salc p->p_limit->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz; 543206483Salc p->p_limit->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz; 544206483Salc p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz; 545206483Salc p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz; 546206483Salc /* Cast to avoid overflow on i386/PAE. */ 547206483Salc pageablemem = ptoa((vm_paddr_t)cnt.v_free_count); 548206483Salc p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_cur = 549206483Salc p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_max = pageablemem; 550206483Salc p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = pageablemem / 3; 551206483Salc p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = pageablemem; 552104719Sjhb p->p_cpulimit = RLIM_INFINITY; 5531541Srgrimes 554220137Strasz /* Initialize resource accounting structures. */ 555220137Strasz racct_create(&p->p_racct); 556220137Strasz 557137909Sdas p->p_stats = pstats_alloc(); 558137909Sdas 5591541Srgrimes /* Allocate a prototype map so we have something to fork. */ 56044146Sluoqi pmap_pinit0(vmspace_pmap(&vmspace0)); 5611541Srgrimes p->p_vmspace = &vmspace0; 5621541Srgrimes vmspace0.vm_refcnt = 1; 563197711Sbz 564197711Sbz /* 565197711Sbz * proc0 is not expected to enter usermode, so there is no special 566197711Sbz * handling for sv_minuser here, like is done for exec_new_vmspace(). 567197711Sbz */ 568206142Salc vm_map_init(&vmspace0.vm_map, vmspace_pmap(&vmspace0), 569206142Salc p->p_sysent->sv_minuser, p->p_sysent->sv_maxuser); 5701541Srgrimes 571210365Strasz /* 572210365Strasz * Call the init and ctor for the new thread and proc. We wait 573210365Strasz * to do this until all other structures are fairly sane. 574173732Srrs */ 575173732Srrs EVENTHANDLER_INVOKE(process_init, p); 576173732Srrs EVENTHANDLER_INVOKE(thread_init, td); 577173732Srrs EVENTHANDLER_INVOKE(process_ctor, p); 578173732Srrs EVENTHANDLER_INVOKE(thread_ctor, td); 579173732Srrs 5801541Srgrimes /* 58114533Shsu * Charge root for one process. 5821541Srgrimes */ 58377183Srwatson (void)chgproccnt(p->p_ucred->cr_ruidinfo, 1, 0); 584220222Strasz PROC_LOCK(p); 585220222Strasz racct_add_force(p, RACCT_NPROC, 1); 586220222Strasz PROC_UNLOCK(p); 58710358Sjulian} 588177253SrwatsonSYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL); 5891541Srgrimes 59010358Sjulian/* ARGSUSED*/ 59112725Sphkstatic void 59264529Speterproc0_post(void *dummy __unused) 59310358Sjulian{ 59435029Sphk struct timespec ts; 59564529Speter struct proc *p; 596170466Sattilio struct rusage ru; 597175219Srwatson struct thread *td; 59818475Speter 59910358Sjulian /* 60044256Sbde * Now we can look at the time, having had a chance to verify the 60196755Strhodes * time from the filesystem. Pretend that proc0 started now. 60210358Sjulian */ 60374927Sjhb sx_slock(&allproc_lock); 604166073Sdelphij FOREACH_PROC_IN_SYSTEM(p) { 605114434Sdes microuptime(&p->p_stats->p_start); 606170476Sattilio PROC_SLOCK(p); 607170466Sattilio rufetch(p, &ru); /* Clears thread stats */ 608170476Sattilio PROC_SUNLOCK(p); 609155444Sphk p->p_rux.rux_runtime = 0; 610170466Sattilio p->p_rux.rux_uticks = 0; 611170466Sattilio p->p_rux.rux_sticks = 0; 612170466Sattilio p->p_rux.rux_iticks = 0; 613175219Srwatson FOREACH_THREAD_IN_PROC(p, td) { 614175219Srwatson td->td_runtime = 0; 615175219Srwatson } 61664529Speter } 61774927Sjhb sx_sunlock(&allproc_lock); 618155444Sphk PCPU_SET(switchtime, cpu_ticks()); 61965557Sjasone PCPU_SET(switchticks, ticks); 6201541Srgrimes 62118475Speter /* 62218475Speter * Give the ``random'' number generator a thump. 62318475Speter */ 62435029Sphk nanotime(&ts); 62535029Sphk srandom(ts.tv_sec ^ ts.tv_nsec); 62610358Sjulian} 627177253SrwatsonSYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL); 6281541Srgrimes 629198295Srustatic void 630198295Srurandom_init(void *dummy __unused) 631198295Sru{ 632198295Sru 633198295Sru /* 634198295Sru * After CPU has been started we have some randomness on most 635198295Sru * platforms via get_cyclecount(). For platforms that don't 636198295Sru * we will reseed random(9) in proc0_post() as well. 637198295Sru */ 638198295Sru srandom(get_cyclecount()); 639198295Sru} 640198295SruSYSINIT(random, SI_SUB_RANDOM, SI_ORDER_FIRST, random_init, NULL); 641198295Sru 64210358Sjulian/* 64310358Sjulian *************************************************************************** 64410358Sjulian **** 64510358Sjulian **** The following SYSINIT's and glue code should be moved to the 64610358Sjulian **** respective files on a per subsystem basis. 64710358Sjulian **** 64810358Sjulian *************************************************************************** 64910358Sjulian */ 6501541Srgrimes 65110358Sjulian 65210358Sjulian/* 65310358Sjulian *************************************************************************** 65410358Sjulian **** 65510358Sjulian **** The following code probably belongs in another file, like 65664529Speter **** kern/init_init.c. 65710358Sjulian **** 65810358Sjulian *************************************************************************** 65910358Sjulian */ 6601541Srgrimes 66148391Speter/* 6621541Srgrimes * List of paths to try when searching for "init". 6631541Srgrimes */ 66445881Sdesstatic char init_path[MAXPATHLEN] = 66546506Sjb#ifdef INIT_PATH 66646506Sjb __XSTRING(INIT_PATH); 66746506Sjb#else 668226833Spluknet "/sbin/init:/sbin/oinit:/sbin/init.bak:/rescue/init"; 66946506Sjb#endif 67088019SluigiSYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, 67188019Sluigi "Path used to search the init process"); 6721541Srgrimes 6731541Srgrimes/* 674150154Srse * Shutdown timeout of init(8). 675150154Srse * Unused within kernel, but used to control init(8), hence do not remove. 676150154Srse */ 677150154Srse#ifndef INIT_SHUTDOWN_TIMEOUT 678150154Srse#define INIT_SHUTDOWN_TIMEOUT 120 679150154Srse#endif 680150154Srsestatic int init_shutdown_timeout = INIT_SHUTDOWN_TIMEOUT; 681150154SrseSYSCTL_INT(_kern, OID_AUTO, init_shutdown_timeout, 682211102Sgavin CTLFLAG_RW, &init_shutdown_timeout, 0, "Shutdown timeout of init(8). " 683211102Sgavin "Unused within kernel, but used to control init(8)"); 684150154Srse 685150154Srse/* 68645881Sdes * Start the initial user process; try exec'ing each pathname in init_path. 6871541Srgrimes * The program is invoked with one argument containing the boot flags. 6881541Srgrimes */ 6891541Srgrimesstatic void 69064529Speterstart_init(void *dummy) 6911541Srgrimes{ 6921541Srgrimes vm_offset_t addr; 6931541Srgrimes struct execve_args args; 69445881Sdes int options, error; 69545881Sdes char *var, *path, *next, *s; 69645881Sdes char *ucp, **uap, *arg0, *arg1; 69783366Sjulian struct thread *td; 69848391Speter struct proc *p; 6991541Srgrimes 70072200Sbmilekic mtx_lock(&Giant); 70165557Sjasone 70279224Sdillon GIANT_REQUIRED; 70379224Sdillon 70483366Sjulian td = curthread; 70583366Sjulian p = td->td_proc; 7061541Srgrimes 70799336Smux vfs_mountroot(); 70891859Sphk 709109526Sphk /* 7101541Srgrimes * Need just enough stack to hold the faked-up "execve()" arguments. 7111541Srgrimes */ 712103767Sjake addr = p->p_sysent->sv_usrstack - PAGE_SIZE; 713255426Sjhb if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, 0, 714255426Sjhb VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0) 7151541Srgrimes panic("init: couldn't allocate argument space"); 7161541Srgrimes p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 7171549Srgrimes p->p_vmspace->vm_ssize = 1; 7181541Srgrimes 71945881Sdes if ((var = getenv("init_path")) != NULL) { 720105354Srobert strlcpy(init_path, var, sizeof(init_path)); 72194936Smux freeenv(var); 72245881Sdes } 72345881Sdes 72446506Sjb for (path = init_path; *path != '\0'; path = next) { 72547003Sjb while (*path == ':') 72645881Sdes path++; 72746506Sjb if (*path == '\0') 72845881Sdes break; 72947003Sjb for (next = path; *next != '\0' && *next != ':'; next++) 73045881Sdes /* nothing */ ; 73145881Sdes if (bootverbose) 73246019Sdt printf("start_init: trying %.*s\n", (int)(next - path), 73346019Sdt path); 73445881Sdes 7351541Srgrimes /* 7361541Srgrimes * Move out the boot flag argument. 7371541Srgrimes */ 7381541Srgrimes options = 0; 739103767Sjake ucp = (char *)p->p_sysent->sv_usrstack; 7401541Srgrimes (void)subyte(--ucp, 0); /* trailing zero */ 7411541Srgrimes if (boothowto & RB_SINGLE) { 7421541Srgrimes (void)subyte(--ucp, 's'); 7431541Srgrimes options = 1; 7441541Srgrimes } 7451541Srgrimes#ifdef notyet 7461541Srgrimes if (boothowto & RB_FASTBOOT) { 7471541Srgrimes (void)subyte(--ucp, 'f'); 7481541Srgrimes options = 1; 7491541Srgrimes } 7501541Srgrimes#endif 7517731Sphk 7527731Sphk#ifdef BOOTCDROM 7537731Sphk (void)subyte(--ucp, 'C'); 7547731Sphk options = 1; 7557731Sphk#endif 75665374Sphk 7571541Srgrimes if (options == 0) 7581541Srgrimes (void)subyte(--ucp, '-'); 7591541Srgrimes (void)subyte(--ucp, '-'); /* leading hyphen */ 7601541Srgrimes arg1 = ucp; 7611541Srgrimes 7621541Srgrimes /* 7631541Srgrimes * Move out the file name (also arg 0). 7641541Srgrimes */ 76545881Sdes (void)subyte(--ucp, 0); 76645881Sdes for (s = next - 1; s >= path; s--) 76745881Sdes (void)subyte(--ucp, *s); 7681541Srgrimes arg0 = ucp; 7691541Srgrimes 7701541Srgrimes /* 7711541Srgrimes * Move out the arg pointers. 7721541Srgrimes */ 77339999Sdfr uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1)); 77437657Sbde (void)suword((caddr_t)--uap, (long)0); /* terminator */ 77537657Sbde (void)suword((caddr_t)--uap, (long)(intptr_t)arg1); 77637657Sbde (void)suword((caddr_t)--uap, (long)(intptr_t)arg0); 7771541Srgrimes 7781541Srgrimes /* 7791541Srgrimes * Point at the arguments. 7801541Srgrimes */ 7811541Srgrimes args.fname = arg0; 7821549Srgrimes args.argv = uap; 7831549Srgrimes args.envv = NULL; 7841541Srgrimes 7851541Srgrimes /* 7861541Srgrimes * Now try to exec the program. If can't for any reason 7871541Srgrimes * other than it doesn't exist, complain. 78810358Sjulian * 78964529Speter * Otherwise, return via fork_trampoline() all the way 79048391Speter * to user mode as init! 7911541Srgrimes */ 792225617Skmacy if ((error = sys_execve(td, &args)) == 0) { 79372200Sbmilekic mtx_unlock(&Giant); 7941541Srgrimes return; 79565899Sjhb } 7961541Srgrimes if (error != ENOENT) 79746019Sdt printf("exec %.*s: error %d\n", (int)(next - path), 79846019Sdt path, error); 7991541Srgrimes } 80054869Sgrog printf("init: not found in path %s\n", init_path); 8011541Srgrimes panic("no init"); 8021541Srgrimes} 80364529Speter 80464529Speter/* 805172836Sjulian * Like kproc_create(), but runs in it's own address space. 80664529Speter * We do this early to reserve pid 1. 80764529Speter * 80864529Speter * Note special case - do not make it runnable yet. Other work 80964529Speter * in progress will change this more. 81064529Speter */ 81164529Speterstatic void 81264529Spetercreate_init(const void *udata __unused) 81364529Speter{ 81495050Srwatson struct ucred *newcred, *oldcred; 81564529Speter int error; 81664529Speter 817224987Sjonathan error = fork1(&thread0, RFFDG | RFPROC | RFSTOPPED, 0, &initproc, 818224987Sjonathan NULL, 0); 81964529Speter if (error) 82064529Speter panic("cannot fork init: %d\n", error); 821124597Srwatson KASSERT(initproc->p_pid == 1, ("create_init: initproc->p_pid != 1")); 82295050Srwatson /* divorce init's credentials from the kernel's */ 82395050Srwatson newcred = crget(); 82471555Sjhb PROC_LOCK(initproc); 825172207Sjeff initproc->p_flag |= P_SYSTEM | P_INMEM; 82695050Srwatson oldcred = initproc->p_ucred; 82795050Srwatson crcopy(newcred, oldcred); 828101001Srwatson#ifdef MAC 829184407Srwatson mac_cred_create_init(newcred); 830101001Srwatson#endif 831155196Srwatson#ifdef AUDIT 832170407Srwatson audit_cred_proc1(newcred); 833155196Srwatson#endif 83495050Srwatson initproc->p_ucred = newcred; 83571555Sjhb PROC_UNLOCK(initproc); 83695050Srwatson crfree(oldcred); 837101477Srwatson cred_update_thread(FIRST_THREAD_IN_PROC(initproc)); 83890361Sjulian cpu_set_fork_handler(FIRST_THREAD_IN_PROC(initproc), start_init, NULL); 83964529Speter} 840177253SrwatsonSYSINIT(init, SI_SUB_CREATE_INIT, SI_ORDER_FIRST, create_init, NULL); 84164529Speter 84264529Speter/* 84364529Speter * Make it runnable now. 84464529Speter */ 84564529Speterstatic void 84664529Speterkick_init(const void *udata __unused) 84764529Speter{ 84890361Sjulian struct thread *td; 84971555Sjhb 85090361Sjulian td = FIRST_THREAD_IN_PROC(initproc); 851170307Sjeff thread_lock(td); 852103216Sjulian TD_SET_CAN_RUN(td); 853166188Sjeff sched_add(td, SRQ_BORING); 854170307Sjeff thread_unlock(td); 85564529Speter} 856177253SrwatsonSYSINIT(kickinit, SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kick_init, NULL); 857