17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
545916cd2Sjpk * Common Development and Distribution License (the "License").
645916cd2Sjpk * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
216eb30ec3SRoger A. Faulkner
227c478bd9Sstevel@tonic-gate /*
23c3f63b71SAlexandre Chartre * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
2735b5a2efSHans Rosenfeld /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
29a288e5a9SJoshua M. Clulow /*
30c3377ee9SJohn Levon * Copyright 2019 Joyent, Inc.
31a288e5a9SJoshua M. Clulow */
32a288e5a9SJoshua M. Clulow
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/signal.h>
397c478bd9Sstevel@tonic-gate #include <sys/cred.h>
407c478bd9Sstevel@tonic-gate #include <sys/user.h>
417c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
427c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
437c478bd9Sstevel@tonic-gate #include <sys/proc.h>
447c478bd9Sstevel@tonic-gate #include <sys/time.h>
457c478bd9Sstevel@tonic-gate #include <sys/file.h>
467c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
477c478bd9Sstevel@tonic-gate #include <sys/procset.h>
487c478bd9Sstevel@tonic-gate #include <sys/disp.h>
497c478bd9Sstevel@tonic-gate #include <sys/callo.h>
507c478bd9Sstevel@tonic-gate #include <sys/callb.h>
517c478bd9Sstevel@tonic-gate #include <sys/debug.h>
527c478bd9Sstevel@tonic-gate #include <sys/conf.h>
537c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
547c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
557c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
567c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
577c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
587c478bd9Sstevel@tonic-gate #include <sys/vm.h>
597c478bd9Sstevel@tonic-gate #include <sys/callb.h>
60a288e5a9SJoshua M. Clulow #include <sys/ddi_periodic.h>
617c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
627c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
637c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
647c478bd9Sstevel@tonic-gate #include <sys/cladm.h>
657c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
667c478bd9Sstevel@tonic-gate #include <sys/exec.h>
677c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
687c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
697c478bd9Sstevel@tonic-gate #include <sys/task.h>
707c478bd9Sstevel@tonic-gate #include <sys/exacct.h>
717c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
727c478bd9Sstevel@tonic-gate #include <sys/errorq.h>
737c478bd9Sstevel@tonic-gate #include <sys/class.h>
747c478bd9Sstevel@tonic-gate #include <sys/stack.h>
759acbbeafSnn #include <sys/brand.h>
760616c1c3SMichael Corcoran #include <sys/mmapobj.h>
77c3377ee9SJohn Levon #include <sys/smt.h>
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate #include <vm/as.h>
807c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
817c478bd9Sstevel@tonic-gate #include <sys/dc_ki.h>
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate #include <c2/audit.h>
846cefaae1SJack Meng #include <sys/bootprops.h>
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /* well known processes */
877c478bd9Sstevel@tonic-gate proc_t *proc_sched; /* memory scheduler */
887c478bd9Sstevel@tonic-gate proc_t *proc_init; /* init */
897c478bd9Sstevel@tonic-gate proc_t *proc_pageout; /* pageout daemon */
907c478bd9Sstevel@tonic-gate proc_t *proc_fsflush; /* fsflush daemon */
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate pgcnt_t maxmem; /* Maximum available memory in pages. */
937c478bd9Sstevel@tonic-gate pgcnt_t freemem; /* Current available memory in pages. */
947c478bd9Sstevel@tonic-gate int interrupts_unleashed; /* set when we do the first spl0() */
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate kmem_cache_t *process_cache; /* kmem cache for proc structures */
977c478bd9Sstevel@tonic-gate
98005d3febSMarek Pospisil /*
99005d3febSMarek Pospisil * Indicates whether the auditing module (c2audit) is loaded. Possible
100005d3febSMarek Pospisil * values are:
101005d3febSMarek Pospisil * 0 - c2audit module is excluded in /etc/system and cannot be loaded
102005d3febSMarek Pospisil * 1 - c2audit module is not loaded but can be anytime
103005d3febSMarek Pospisil * 2 - c2audit module is loaded
104005d3febSMarek Pospisil */
105005d3febSMarek Pospisil int audit_active = C2AUDIT_DISABLED;
106005d3febSMarek Pospisil
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * Process 0's lwp directory and lwpid hash table.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate lwpdir_t p0_lwpdir[2];
1116eb30ec3SRoger A. Faulkner tidhash_t p0_tidhash[2];
1127c478bd9Sstevel@tonic-gate lwpent_t p0_lep;
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate * Machine-independent initialization code
1167c478bd9Sstevel@tonic-gate * Called from cold start routine as
1177c478bd9Sstevel@tonic-gate * soon as a stack and segmentation
1187c478bd9Sstevel@tonic-gate * have been established.
1197c478bd9Sstevel@tonic-gate * Functions:
1207c478bd9Sstevel@tonic-gate * clear and free user core
1217c478bd9Sstevel@tonic-gate * turn on clock
1227c478bd9Sstevel@tonic-gate * hand craft 0th process
1237c478bd9Sstevel@tonic-gate * call all initialization routines
1247c478bd9Sstevel@tonic-gate * fork - process 0 to schedule
1257c478bd9Sstevel@tonic-gate * - process 1 execute bootstrap
1267c478bd9Sstevel@tonic-gate * - process 2 to page out
1277c478bd9Sstevel@tonic-gate * create system threads
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate int cluster_bootflags = 0;
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate void
cluster_wrapper(void)1337c478bd9Sstevel@tonic-gate cluster_wrapper(void)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate cluster();
1367c478bd9Sstevel@tonic-gate panic("cluster() returned");
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1393f2f09c1Sdp char initname[INITNAME_SZ] = "/sbin/init"; /* also referenced by zone0 */
1403f2f09c1Sdp char initargs[BOOTARGS_MAX] = ""; /* also referenced by zone0 */
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate /*
1433f2f09c1Sdp * Construct a stack for init containing the arguments to it, then
1443f2f09c1Sdp * pass control to exec_common.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate int
exec_init(const char * initpath,const char * args)1473f2f09c1Sdp exec_init(const char *initpath, const char *args)
1487c478bd9Sstevel@tonic-gate {
14935b5a2efSHans Rosenfeld uintptr_t ucp;
15035b5a2efSHans Rosenfeld uintptr_t uap;
15135b5a2efSHans Rosenfeld uintptr_t *argv;
15235b5a2efSHans Rosenfeld uintptr_t exec_fnamep;
1533f2f09c1Sdp char *scratchargs;
1543f2f09c1Sdp int i, sarg;
1553f2f09c1Sdp size_t argvlen, alen;
15635b5a2efSHans Rosenfeld size_t wlen = sizeof (uintptr_t);
1573f2f09c1Sdp boolean_t in_arg;
1587c478bd9Sstevel@tonic-gate int argc = 0;
1593f2f09c1Sdp int error = 0, count = 0;
1607c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
1617c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
1629acbbeafSnn int brand_action;
1637c478bd9Sstevel@tonic-gate
1643f2f09c1Sdp if (args == NULL)
1653f2f09c1Sdp args = "";
1667c478bd9Sstevel@tonic-gate
1673f2f09c1Sdp alen = strlen(initpath) + 1 + strlen(args) + 1;
1683f2f09c1Sdp scratchargs = kmem_alloc(alen, KM_SLEEP);
1693f2f09c1Sdp (void) snprintf(scratchargs, alen, "%s %s", initpath, args);
1707c478bd9Sstevel@tonic-gate
1713f2f09c1Sdp /*
1723f2f09c1Sdp * We do a quick two state parse of the string to sort out how big
1733f2f09c1Sdp * argc should be.
1743f2f09c1Sdp */
1753f2f09c1Sdp in_arg = B_FALSE;
1763f2f09c1Sdp for (i = 0; i < strlen(scratchargs); i++) {
1773f2f09c1Sdp if (scratchargs[i] == ' ' || scratchargs[i] == '\0') {
1783f2f09c1Sdp if (in_arg) {
1793f2f09c1Sdp in_arg = B_FALSE;
1803f2f09c1Sdp argc++;
1813f2f09c1Sdp }
1823f2f09c1Sdp } else {
1833f2f09c1Sdp in_arg = B_TRUE;
1843f2f09c1Sdp }
1857c478bd9Sstevel@tonic-gate }
18635b5a2efSHans Rosenfeld argvlen = sizeof (uintptr_t) * (argc + 1);
1873f2f09c1Sdp argv = kmem_zalloc(argvlen, KM_SLEEP);
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /*
1903f2f09c1Sdp * We pull off a bit of a hack here. We work our way through the
1913f2f09c1Sdp * args string, putting nulls at the ends of space delimited tokens
1923f2f09c1Sdp * (boot args don't support quoting at this time). Then we just
1933f2f09c1Sdp * copy the whole mess to userland in one go. In other words, we
1943f2f09c1Sdp * transform this: "init -s -r\0" into this on the stack:
1953f2f09c1Sdp *
1963f2f09c1Sdp * -0x00 \0
1973f2f09c1Sdp * -0x01 r
1983f2f09c1Sdp * -0x02 - <--------.
1993f2f09c1Sdp * -0x03 \0 |
2003f2f09c1Sdp * -0x04 s |
2013f2f09c1Sdp * -0x05 - <------. |
2023f2f09c1Sdp * -0x06 \0 | |
2033f2f09c1Sdp * -0x07 t | |
20435b5a2efSHans Rosenfeld * -0x08 i | |
2053f2f09c1Sdp * -0x09 n | |
2063f2f09c1Sdp * -0x0a i <---. | |
2073f2f09c1Sdp * -0x10 NULL | | | (argv[3])
2083f2f09c1Sdp * -0x14 -----|--|-' (argv[2])
2093f2f09c1Sdp * -0x18 ------|--' (argv[1])
2103f2f09c1Sdp * -0x1c -------' (argv[0])
2113f2f09c1Sdp *
2123f2f09c1Sdp * Since we know the value of ucp at the beginning of this process,
2133f2f09c1Sdp * we can trivially compute the argv[] array which we also need to
2143f2f09c1Sdp * place in userland: argv[i] = ucp - sarg(i), where ucp is the
2153f2f09c1Sdp * stack ptr, and sarg is the string index of the start of the
2163f2f09c1Sdp * argument.
2177c478bd9Sstevel@tonic-gate */
21835b5a2efSHans Rosenfeld ucp = (uintptr_t)p->p_usrstack;
2193f2f09c1Sdp
2203f2f09c1Sdp argc = 0;
2213f2f09c1Sdp in_arg = B_FALSE;
2223f2f09c1Sdp sarg = 0;
2233f2f09c1Sdp
2243f2f09c1Sdp for (i = 0; i < alen; i++) {
2253f2f09c1Sdp if (scratchargs[i] == ' ' || scratchargs[i] == '\0') {
2263f2f09c1Sdp if (in_arg == B_TRUE) {
2273f2f09c1Sdp in_arg = B_FALSE;
2283f2f09c1Sdp scratchargs[i] = '\0';
2293f2f09c1Sdp argv[argc++] = ucp - (alen - sarg);
2303f2f09c1Sdp }
2313f2f09c1Sdp } else if (in_arg == B_FALSE) {
2323f2f09c1Sdp in_arg = B_TRUE;
2333f2f09c1Sdp sarg = i;
2343f2f09c1Sdp }
2353f2f09c1Sdp }
23635b5a2efSHans Rosenfeld
23735b5a2efSHans Rosenfeld exec_fnamep = argv[0];
23835b5a2efSHans Rosenfeld
2393f2f09c1Sdp ucp -= alen;
24035b5a2efSHans Rosenfeld error |= copyout(scratchargs, (caddr_t)ucp, alen);
2413f2f09c1Sdp
24235b5a2efSHans Rosenfeld if (p->p_model == DATAMODEL_ILP32) {
24335b5a2efSHans Rosenfeld uintptr32_t *argv32;
24435b5a2efSHans Rosenfeld
24535b5a2efSHans Rosenfeld argv32 = kmem_zalloc(argvlen / 2, KM_SLEEP);
24635b5a2efSHans Rosenfeld
24735b5a2efSHans Rosenfeld for (i = 0; i < argc; i++)
24835b5a2efSHans Rosenfeld argv32[i] = (uintptr32_t)argv[i];
24935b5a2efSHans Rosenfeld
25035b5a2efSHans Rosenfeld kmem_free(argv, argvlen);
25135b5a2efSHans Rosenfeld argv = (uintptr_t *)argv32;
25235b5a2efSHans Rosenfeld argvlen /= 2;
25335b5a2efSHans Rosenfeld
25435b5a2efSHans Rosenfeld wlen = sizeof (uintptr32_t);
25535b5a2efSHans Rosenfeld }
25635b5a2efSHans Rosenfeld
25735b5a2efSHans Rosenfeld uap = P2ALIGN(ucp, wlen);
25835b5a2efSHans Rosenfeld /* advance to be below the word we're in */
25935b5a2efSHans Rosenfeld uap -= wlen;
26035b5a2efSHans Rosenfeld /* advance argc words down, plus one for NULL */
26135b5a2efSHans Rosenfeld uap -= (argc + 1) * wlen;
26235b5a2efSHans Rosenfeld error |= copyout(argv, (caddr_t)uap, argvlen);
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate if (error != 0) {
2657c478bd9Sstevel@tonic-gate zcmn_err(p->p_zone->zone_id, CE_WARN,
2667c478bd9Sstevel@tonic-gate "Could not construct stack for init.\n");
2673f2f09c1Sdp kmem_free(argv, argvlen);
2683f2f09c1Sdp kmem_free(scratchargs, alen);
2697c478bd9Sstevel@tonic-gate return (EFAULT);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate
2723f2f09c1Sdp kmem_free(argv, argvlen);
2733f2f09c1Sdp kmem_free(scratchargs, alen);
2743f2f09c1Sdp
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate * Point at the arguments.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate lwp->lwp_ap = lwp->lwp_arg;
27935b5a2efSHans Rosenfeld lwp->lwp_arg[0] = exec_fnamep;
28035b5a2efSHans Rosenfeld lwp->lwp_arg[1] = uap;
2817e12ceb3SToomas Soome lwp->lwp_arg[2] = 0;
2827c478bd9Sstevel@tonic-gate curthread->t_post_sys = 1;
2837c478bd9Sstevel@tonic-gate curthread->t_sysnum = SYS_execve;
2847c478bd9Sstevel@tonic-gate
2859acbbeafSnn /*
2869acbbeafSnn * If we are executing init from zsched, we may have inherited its
2879acbbeafSnn * parent process's signal mask. Clear it now so that we behave in
2889acbbeafSnn * the same way as when started from the global zone.
2899acbbeafSnn */
290bdf0047cSRoger A. Faulkner sigemptyset(&curthread->t_hold);
2919acbbeafSnn
2929acbbeafSnn brand_action = ZONE_IS_BRANDED(p->p_zone) ? EBA_BRAND : EBA_NONE;
2937c478bd9Sstevel@tonic-gate again:
29435b5a2efSHans Rosenfeld error = exec_common((const char *)exec_fnamep,
295*2b395c3cSAndy Fiddaman (const char **)uap, NULL, NULL, brand_action);
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate * Normally we would just set lwp_argsaved and t_post_sys and
2997c478bd9Sstevel@tonic-gate * let post_syscall reset lwp_ap for us. Unfortunately,
3007c478bd9Sstevel@tonic-gate * exec_init isn't always called from a system call. Instead
3017c478bd9Sstevel@tonic-gate * of making a mess of trap_cleanup, we just reset the args
3027c478bd9Sstevel@tonic-gate * pointer here.
3037c478bd9Sstevel@tonic-gate */
3047c478bd9Sstevel@tonic-gate reset_syscall_args();
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate switch (error) {
3077c478bd9Sstevel@tonic-gate case 0:
3087c478bd9Sstevel@tonic-gate return (0);
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate case ENOENT:
3117c478bd9Sstevel@tonic-gate zcmn_err(p->p_zone->zone_id, CE_WARN,
3127c478bd9Sstevel@tonic-gate "exec(%s) failed (file not found).\n", initpath);
3137c478bd9Sstevel@tonic-gate return (ENOENT);
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate case EAGAIN:
3167c478bd9Sstevel@tonic-gate case EINTR:
3177c478bd9Sstevel@tonic-gate ++count;
3187c478bd9Sstevel@tonic-gate if (count < 5) {
3197c478bd9Sstevel@tonic-gate zcmn_err(p->p_zone->zone_id, CE_WARN,
3207c478bd9Sstevel@tonic-gate "exec(%s) failed with errno %d. Retrying...\n",
3217c478bd9Sstevel@tonic-gate initpath, error);
3227c478bd9Sstevel@tonic-gate goto again;
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate zcmn_err(p->p_zone->zone_id, CE_WARN,
3277c478bd9Sstevel@tonic-gate "exec(%s) failed with errno %d.", initpath, error);
3287c478bd9Sstevel@tonic-gate return (error);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate
3313f2f09c1Sdp /*
3323f2f09c1Sdp * This routine does all of the common setup for invoking init; global
3333f2f09c1Sdp * and non-global zones employ this routine for the functionality which is
3343f2f09c1Sdp * in common.
3353f2f09c1Sdp *
3363f2f09c1Sdp * This program (init, presumably) must be a 32-bit process.
3373f2f09c1Sdp */
3383f2f09c1Sdp int
start_init_common()3393f2f09c1Sdp start_init_common()
3403f2f09c1Sdp {
3413f2f09c1Sdp proc_t *p = curproc;
3423f2f09c1Sdp ASSERT_STACK_ALIGNED();
3433f2f09c1Sdp p->p_zone->zone_proc_initpid = p->p_pid;
3443f2f09c1Sdp
3453f2f09c1Sdp p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
3463f2f09c1Sdp p->p_usrstack = (caddr_t)USRSTACK32;
3473f2f09c1Sdp p->p_model = DATAMODEL_ILP32;
3483f2f09c1Sdp p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
3493f2f09c1Sdp p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
3503f2f09c1Sdp p->p_stk_ctl = INT32_MAX;
3513f2f09c1Sdp
3523f2f09c1Sdp p->p_as = as_alloc();
353c6939658Ssl p->p_as->a_proc = p;
3543f2f09c1Sdp p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
3553f2f09c1Sdp (void) hat_setup(p->p_as->a_hat, HAT_INIT);
3563f2f09c1Sdp
3573f2f09c1Sdp init_core();
3583f2f09c1Sdp
3593f2f09c1Sdp init_mstate(curthread, LMS_SYSTEM);
3603f2f09c1Sdp return (exec_init(p->p_zone->zone_initname, p->p_zone->zone_bootargs));
3613f2f09c1Sdp }
3623f2f09c1Sdp
3633f2f09c1Sdp /*
3643f2f09c1Sdp * Start the initial user process for the global zone; once running, if
3653f2f09c1Sdp * init should subsequently fail, it will be automatically be caught in the
3663f2f09c1Sdp * exit(2) path, and restarted by restart_init().
3673f2f09c1Sdp */
3683f2f09c1Sdp static void
start_init(void)3693f2f09c1Sdp start_init(void)
3703f2f09c1Sdp {
3713f2f09c1Sdp proc_init = curproc;
3723f2f09c1Sdp
3733f2f09c1Sdp ASSERT(curproc->p_zone->zone_initname != NULL);
3743f2f09c1Sdp
3753f2f09c1Sdp if (start_init_common() != 0)
3763f2f09c1Sdp halt("unix: Could not start init");
3773f2f09c1Sdp lwp_rtt();
3783f2f09c1Sdp }
3793f2f09c1Sdp
3807c478bd9Sstevel@tonic-gate void
main(void)3817c478bd9Sstevel@tonic-gate main(void)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); /* &p0 */
3847c478bd9Sstevel@tonic-gate int (**initptr)();
3857c478bd9Sstevel@tonic-gate extern void sched();
3867c478bd9Sstevel@tonic-gate extern void fsflush();
3877c478bd9Sstevel@tonic-gate extern int (*init_tbl[])();
3887c478bd9Sstevel@tonic-gate extern int (*mp_init_tbl[])();
3897c478bd9Sstevel@tonic-gate extern id_t syscid, defaultcid;
3907c478bd9Sstevel@tonic-gate extern int swaploaded;
3917c478bd9Sstevel@tonic-gate extern int netboot;
3926cefaae1SJack Meng extern ib_boot_prop_t *iscsiboot_prop;
3937c478bd9Sstevel@tonic-gate extern void vm_init(void);
394b9499e44Smrj extern void cbe_init_pre(void);
3957c478bd9Sstevel@tonic-gate extern void cbe_init(void);
3962850d85bSmv extern void clock_tick_init_pre(void);
3972850d85bSmv extern void clock_tick_init_post(void);
3987c478bd9Sstevel@tonic-gate extern void clock_init(void);
3997c478bd9Sstevel@tonic-gate extern void physio_bufs_init(void);
4007c478bd9Sstevel@tonic-gate extern void pm_cfb_setup_intr(void);
4017c478bd9Sstevel@tonic-gate extern int pm_adjust_timestamps(dev_info_t *, void *);
4027c478bd9Sstevel@tonic-gate extern void start_other_cpus(int);
4037c478bd9Sstevel@tonic-gate extern void sysevent_evc_thrinit();
404c3f63b71SAlexandre Chartre extern kmutex_t ualock;
4055cff7825Smh #if defined(__x86)
406753a6d45SSherry Moore extern void fastboot_post_startup(void);
407486aec20SEnrico Perla - Sun Microsystems extern void progressbar_start(void);
4085cff7825Smh #endif
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate * In the horrible world of x86 in-lines, you can't get symbolic
4117c478bd9Sstevel@tonic-gate * structure offsets a la genassym. This assertion is here so
4127c478bd9Sstevel@tonic-gate * that the next poor slob who innocently changes the offset of
4137c478bd9Sstevel@tonic-gate * cpu_thread doesn't waste as much time as I just did finding
4147c478bd9Sstevel@tonic-gate * out that it's hard-coded in i86/ml/i86.il. Similarly for
4157c478bd9Sstevel@tonic-gate * curcpup. You're welcome.
4167c478bd9Sstevel@tonic-gate */
4177c478bd9Sstevel@tonic-gate ASSERT(CPU == CPU->cpu_self);
4187c478bd9Sstevel@tonic-gate ASSERT(curthread == CPU->cpu_thread);
4197c478bd9Sstevel@tonic-gate ASSERT_STACK_ALIGNED();
4207c478bd9Sstevel@tonic-gate
421c3f63b71SAlexandre Chartre /*
422c3f63b71SAlexandre Chartre * We take the ualock until we have completed the startup
423c3f63b71SAlexandre Chartre * to prevent kadmin() from disrupting this work. In particular,
424c3f63b71SAlexandre Chartre * we don't want kadmin() to bring the system down while we are
425c3f63b71SAlexandre Chartre * trying to start it up.
426c3f63b71SAlexandre Chartre */
427c3f63b71SAlexandre Chartre mutex_enter(&ualock);
428c3f63b71SAlexandre Chartre
4297c478bd9Sstevel@tonic-gate /*
430d5d7cf4eSJonathan Chew * Setup root lgroup and leaf lgroup for CPU 0
4317c478bd9Sstevel@tonic-gate */
432d5d7cf4eSJonathan Chew lgrp_init(LGRP_INIT_STAGE2);
4337c478bd9Sstevel@tonic-gate
4345ce42f36Spraks /*
4355ce42f36Spraks * Once 'startup()' completes, the thread_reaper() daemon would be
4365ce42f36Spraks * created(in thread_init()). After that, it is safe to create threads
4375ce42f36Spraks * that could exit. These exited threads will get reaped.
4385ce42f36Spraks */
4397c478bd9Sstevel@tonic-gate startup();
4407c478bd9Sstevel@tonic-gate segkmem_gc();
4417c478bd9Sstevel@tonic-gate callb_init();
442b9499e44Smrj cbe_init_pre(); /* x86 must initialize gethrtimef before timer_init */
443a288e5a9SJoshua M. Clulow ddi_periodic_init();
4447c478bd9Sstevel@tonic-gate cbe_init();
44587a18d3fSMadhavan Venkataraman callout_init(); /* callout table MUST be init'd after cyclics */
4462850d85bSmv clock_tick_init_pre();
4477c478bd9Sstevel@tonic-gate clock_init();
4487c478bd9Sstevel@tonic-gate
449486aec20SEnrico Perla - Sun Microsystems #if defined(__x86)
450486aec20SEnrico Perla - Sun Microsystems /*
451486aec20SEnrico Perla - Sun Microsystems * The progressbar thread uses cv_reltimedwait() and hence needs to be
452486aec20SEnrico Perla - Sun Microsystems * started after the callout mechanism has been initialized.
453486aec20SEnrico Perla - Sun Microsystems */
454486aec20SEnrico Perla - Sun Microsystems progressbar_start();
455486aec20SEnrico Perla - Sun Microsystems #endif
456ae115bc7Smrj /*
457ae115bc7Smrj * On some platforms, clkinitf() changes the timing source that
458ae115bc7Smrj * gethrtime_unscaled() uses to generate timestamps. cbe_init() calls
459ae115bc7Smrj * clkinitf(), so re-initialize the microstate counters after the
460ae115bc7Smrj * timesource has been chosen.
461ae115bc7Smrj */
462ae115bc7Smrj init_mstate(&t0, LMS_SYSTEM);
463ae115bc7Smrj init_cpu_mstate(CPU, CMS_SYSTEM);
464ae115bc7Smrj
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate * May need to probe to determine latencies from CPU 0 after
4677c478bd9Sstevel@tonic-gate * gethrtime() comes alive in cbe_init() and before enabling interrupts
468d5d7cf4eSJonathan Chew * and copy and release any temporary memory allocated with BOP_ALLOC()
469d5d7cf4eSJonathan Chew * before release_bootstrap() frees boot memory
4707c478bd9Sstevel@tonic-gate */
471d5d7cf4eSJonathan Chew lgrp_init(LGRP_INIT_STAGE3);
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate * Call all system initialization functions.
4757c478bd9Sstevel@tonic-gate */
4767c478bd9Sstevel@tonic-gate for (initptr = &init_tbl[0]; *initptr; initptr++)
4777c478bd9Sstevel@tonic-gate (**initptr)();
4786cefaae1SJack Meng /*
4796cefaae1SJack Meng * Load iSCSI boot properties
4806cefaae1SJack Meng */
4816cefaae1SJack Meng ld_ib_prop();
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate * initialize vm related stuff.
4847c478bd9Sstevel@tonic-gate */
4857c478bd9Sstevel@tonic-gate vm_init();
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate * initialize buffer pool for raw I/O requests
4897c478bd9Sstevel@tonic-gate */
4907c478bd9Sstevel@tonic-gate physio_bufs_init();
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate ttolwp(curthread)->lwp_error = 0; /* XXX kludge for SCSI driver */
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate /*
4957c478bd9Sstevel@tonic-gate * Drop the interrupt level and allow interrupts. At this point
4967c478bd9Sstevel@tonic-gate * the DDI guarantees that interrupts are enabled.
4977c478bd9Sstevel@tonic-gate */
4987c478bd9Sstevel@tonic-gate (void) spl0();
4997c478bd9Sstevel@tonic-gate interrupts_unleashed = 1;
5007c478bd9Sstevel@tonic-gate
50135a5a358SJonathan Adams /*
50235a5a358SJonathan Adams * Create kmem cache for proc structures
50335a5a358SJonathan Adams */
50435a5a358SJonathan Adams process_cache = kmem_cache_create("process_cache", sizeof (proc_t),
50535a5a358SJonathan Adams 0, NULL, NULL, NULL, NULL, NULL, 0);
50635a5a358SJonathan Adams
5077c478bd9Sstevel@tonic-gate vfs_mountroot(); /* Mount the root file system */
5087c478bd9Sstevel@tonic-gate errorq_init(); /* after vfs_mountroot() so DDI root is ready */
5097c478bd9Sstevel@tonic-gate cpu_kstat_init(CPU); /* after vfs_mountroot() so TOD is valid */
5107c478bd9Sstevel@tonic-gate ddi_walk_devs(ddi_root_node(), pm_adjust_timestamps, NULL);
5117c478bd9Sstevel@tonic-gate /* after vfs_mountroot() so hrestime is valid */
5127c478bd9Sstevel@tonic-gate
5137c478bd9Sstevel@tonic-gate post_startup();
5147c478bd9Sstevel@tonic-gate swaploaded = 1;
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate /*
517d3e55dcdSgww * Initialize Solaris Audit Subsystem
5187c478bd9Sstevel@tonic-gate */
519d3e55dcdSgww audit_init();
5207c478bd9Sstevel@tonic-gate
52194876170SMarcel Telka /*
52294876170SMarcel Telka * Start the periodic hash rescale for all vmem arenas before we load
52394876170SMarcel Telka * protocol modules and drivers via strplumb() below. Some drivers
52494876170SMarcel Telka * might rely on heavy vmem operations that could hurt performance
52594876170SMarcel Telka * without the rescale.
52694876170SMarcel Telka */
52794876170SMarcel Telka vmem_update(NULL);
52894876170SMarcel Telka
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate * Plumb the protocol modules and drivers only if we are not
5317c478bd9Sstevel@tonic-gate * networked booted, in this case we already did it in rootconf().
5327c478bd9Sstevel@tonic-gate */
5336cefaae1SJack Meng if (netboot == 0 && iscsiboot_prop == NULL)
5347c478bd9Sstevel@tonic-gate (void) strplumb();
5357c478bd9Sstevel@tonic-gate
536ae115bc7Smrj gethrestime(&PTOU(curproc)->u_start);
537ae115bc7Smrj curthread->t_start = PTOU(curproc)->u_start.tv_sec;
5387c478bd9Sstevel@tonic-gate p->p_mstart = gethrtime();
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate * Perform setup functions that can only be done after root
5427c478bd9Sstevel@tonic-gate * and swap have been set up.
5437c478bd9Sstevel@tonic-gate */
5447c478bd9Sstevel@tonic-gate consconfig();
545fe85ab99SBijan Mottahedeh #ifndef __sparc
546fe85ab99SBijan Mottahedeh release_bootstrap();
547fe85ab99SBijan Mottahedeh #endif
5481b83305cSjm
5497c478bd9Sstevel@tonic-gate /*
5507c478bd9Sstevel@tonic-gate * attach drivers with ddi-forceattach prop
5517c478bd9Sstevel@tonic-gate * It must be done early enough to load hotplug drivers (e.g.
5527c478bd9Sstevel@tonic-gate * pcmcia nexus) so that devices enumerated via hotplug is
5537c478bd9Sstevel@tonic-gate * available before I/O subsystem is fully initialized.
5547c478bd9Sstevel@tonic-gate */
5557c478bd9Sstevel@tonic-gate i_ddi_forceattach_drivers();
5567c478bd9Sstevel@tonic-gate
5577c478bd9Sstevel@tonic-gate /*
5587c478bd9Sstevel@tonic-gate * Set the scan rate and other parameters of the paging subsystem.
5597c478bd9Sstevel@tonic-gate */
5602d9166aeSJoshua M. Clulow setupclock();
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate * Initialize process 0's lwp directory and lwpid hash table.
5647c478bd9Sstevel@tonic-gate */
5657c478bd9Sstevel@tonic-gate p->p_lwpdir = p->p_lwpfree = p0_lwpdir;
5667c478bd9Sstevel@tonic-gate p->p_lwpdir->ld_next = p->p_lwpdir + 1;
5677c478bd9Sstevel@tonic-gate p->p_lwpdir_sz = 2;
5687c478bd9Sstevel@tonic-gate p->p_tidhash = p0_tidhash;
5697c478bd9Sstevel@tonic-gate p->p_tidhash_sz = 2;
5707c478bd9Sstevel@tonic-gate p0_lep.le_thread = curthread;
5717c478bd9Sstevel@tonic-gate p0_lep.le_lwpid = curthread->t_tid;
5727c478bd9Sstevel@tonic-gate p0_lep.le_start = curthread->t_start;
5736eb30ec3SRoger A. Faulkner lwp_hash_in(p, &p0_lep, p0_tidhash, 2, 0);
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate * Initialize extended accounting.
5777c478bd9Sstevel@tonic-gate */
5787c478bd9Sstevel@tonic-gate exacct_init();
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate * Initialize threads of sysevent event channels
5827c478bd9Sstevel@tonic-gate */
5837c478bd9Sstevel@tonic-gate sysevent_evc_thrinit();
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate /*
586d5d7cf4eSJonathan Chew * This must be done after post_startup() but before
5877c478bd9Sstevel@tonic-gate * start_other_cpus()
5887c478bd9Sstevel@tonic-gate */
589d5d7cf4eSJonathan Chew lgrp_init(LGRP_INIT_STAGE4);
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate * Perform MP initialization, if any.
5937c478bd9Sstevel@tonic-gate */
5947c478bd9Sstevel@tonic-gate start_other_cpus(0);
5957c478bd9Sstevel@tonic-gate
596fe85ab99SBijan Mottahedeh #ifdef __sparc
5979853d9e8SJason Beloro /*
5989853d9e8SJason Beloro * Release bootstrap here since PROM interfaces are
5999853d9e8SJason Beloro * used to start other CPUs above.
6009853d9e8SJason Beloro */
6019853d9e8SJason Beloro release_bootstrap();
602fe85ab99SBijan Mottahedeh #endif
6039853d9e8SJason Beloro
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate * Finish lgrp initialization after all CPUS are brought online.
6067c478bd9Sstevel@tonic-gate */
607d5d7cf4eSJonathan Chew lgrp_init(LGRP_INIT_STAGE5);
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate * After mp_init(), number of cpus are known (this is
6117c478bd9Sstevel@tonic-gate * true for the time being, when there are actually
6127c478bd9Sstevel@tonic-gate * hot pluggable cpus then this scheme would not do).
6137c478bd9Sstevel@tonic-gate * Any per cpu initialization is done here.
6147c478bd9Sstevel@tonic-gate */
6157c478bd9Sstevel@tonic-gate kmem_mp_init();
6167c478bd9Sstevel@tonic-gate
6172850d85bSmv clock_tick_init_post();
6182850d85bSmv
6197c478bd9Sstevel@tonic-gate for (initptr = &mp_init_tbl[0]; *initptr; initptr++)
6207c478bd9Sstevel@tonic-gate (**initptr)();
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate /*
6235cff7825Smh * These must be called after start_other_cpus
6247c478bd9Sstevel@tonic-gate */
6257c478bd9Sstevel@tonic-gate pm_cfb_setup_intr();
6265cff7825Smh #if defined(__x86)
627753a6d45SSherry Moore fastboot_post_startup();
628c3377ee9SJohn Levon
629c3377ee9SJohn Levon smt_late_init();
6305cff7825Smh #endif
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate /*
6337c478bd9Sstevel@tonic-gate * Make init process; enter scheduling loop with system process.
63435a5a358SJonathan Adams *
63535a5a358SJonathan Adams * Note that we manually assign the pids for these processes, for
63635a5a358SJonathan Adams * historical reasons. If more pre-assigned pids are needed,
63735a5a358SJonathan Adams * FAMOUS_PIDS will have to be updated.
6387c478bd9Sstevel@tonic-gate */
6397c478bd9Sstevel@tonic-gate
6407c478bd9Sstevel@tonic-gate /* create init process */
64135a5a358SJonathan Adams if (newproc(start_init, NULL, defaultcid, 59, NULL,
64235a5a358SJonathan Adams FAMOUS_PID_INIT))
6437c478bd9Sstevel@tonic-gate panic("main: unable to fork init.");
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate /* create pageout daemon */
64635a5a358SJonathan Adams if (newproc(pageout, NULL, syscid, maxclsyspri - 1, NULL,
64735a5a358SJonathan Adams FAMOUS_PID_PAGEOUT))
6487c478bd9Sstevel@tonic-gate panic("main: unable to fork pageout()");
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate /* create fsflush daemon */
65135a5a358SJonathan Adams if (newproc(fsflush, NULL, syscid, minclsyspri, NULL,
65235a5a358SJonathan Adams FAMOUS_PID_FSFLUSH))
6537c478bd9Sstevel@tonic-gate panic("main: unable to fork fsflush()");
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate /* create cluster process if we're a member of one */
6567c478bd9Sstevel@tonic-gate if (cluster_bootflags & CLUSTER_BOOTED) {
65735a5a358SJonathan Adams if (newproc(cluster_wrapper, NULL, syscid, minclsyspri,
65835a5a358SJonathan Adams NULL, 0)) {
6597c478bd9Sstevel@tonic-gate panic("main: unable to fork cluster()");
66035a5a358SJonathan Adams }
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate * Create system threads (threads are associated with p0)
6657c478bd9Sstevel@tonic-gate */
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate /* create module uninstall daemon */
6687c478bd9Sstevel@tonic-gate /* BugID 1132273. If swapping over NFS need a bigger stack */
6697c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, (void (*)())mod_uninstall_daemon,
6707c478bd9Sstevel@tonic-gate NULL, 0, &p0, TS_RUN, minclsyspri);
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, seg_pasync_thread,
6737c478bd9Sstevel@tonic-gate NULL, 0, &p0, TS_RUN, minclsyspri);
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate pid_setmin();
6767c478bd9Sstevel@tonic-gate
677c3f63b71SAlexandre Chartre /* system is now ready */
678c3f63b71SAlexandre Chartre mutex_exit(&ualock);
679c3f63b71SAlexandre Chartre
680ae115bc7Smrj bcopy("sched", PTOU(curproc)->u_psargs, 6);
681ae115bc7Smrj bcopy("sched", PTOU(curproc)->u_comm, 5);
6827c478bd9Sstevel@tonic-gate sched();
6837c478bd9Sstevel@tonic-gate /* NOTREACHED */
6847c478bd9Sstevel@tonic-gate }
685