xref: /titanic_51/usr/src/uts/common/os/main.c (revision 6eb30ec3b9418789da0a22b0987f64f325c69491)
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  */
21*6eb30ec3SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23753a6d45SSherry Moore  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/signal.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/user.h>
387c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
397c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/time.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
447c478bd9Sstevel@tonic-gate #include <sys/procset.h>
457c478bd9Sstevel@tonic-gate #include <sys/disp.h>
467c478bd9Sstevel@tonic-gate #include <sys/callo.h>
477c478bd9Sstevel@tonic-gate #include <sys/callb.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate #include <sys/conf.h>
507c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
517c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
527c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
537c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
547c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
557c478bd9Sstevel@tonic-gate #include <sys/vm.h>
567c478bd9Sstevel@tonic-gate #include <sys/callb.h>
57dd4eeefdSeota #include <sys/ddi_timer.h>
587c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
597c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
607c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
617c478bd9Sstevel@tonic-gate #include <sys/cladm.h>
627c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
637c478bd9Sstevel@tonic-gate #include <sys/exec.h>
647c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
657c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
667c478bd9Sstevel@tonic-gate #include <sys/task.h>
677c478bd9Sstevel@tonic-gate #include <sys/exacct.h>
687c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
697c478bd9Sstevel@tonic-gate #include <sys/errorq.h>
707c478bd9Sstevel@tonic-gate #include <sys/class.h>
717c478bd9Sstevel@tonic-gate #include <sys/stack.h>
729acbbeafSnn35248 #include <sys/brand.h>
730616c1c3SMichael Corcoran #include <sys/mmapobj.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #include <vm/as.h>
767c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
777c478bd9Sstevel@tonic-gate #include <sys/dc_ki.h>
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #include <c2/audit.h>
806cefaae1SJack Meng #include <sys/bootprops.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* well known processes */
837c478bd9Sstevel@tonic-gate proc_t *proc_sched;		/* memory scheduler */
847c478bd9Sstevel@tonic-gate proc_t *proc_init;		/* init */
857c478bd9Sstevel@tonic-gate proc_t *proc_pageout;		/* pageout daemon */
867c478bd9Sstevel@tonic-gate proc_t *proc_fsflush;		/* fsflush daemon */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate pgcnt_t	maxmem;		/* Maximum available memory in pages.	*/
897c478bd9Sstevel@tonic-gate pgcnt_t	freemem;	/* Current available memory in pages.	*/
907c478bd9Sstevel@tonic-gate int	audit_active;
917c478bd9Sstevel@tonic-gate int	interrupts_unleashed;	/* set when we do the first spl0() */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate kmem_cache_t *process_cache;	/* kmem cache for proc structures */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Process 0's lwp directory and lwpid hash table.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate lwpdir_t p0_lwpdir[2];
99*6eb30ec3SRoger A. Faulkner tidhash_t p0_tidhash[2];
1007c478bd9Sstevel@tonic-gate lwpent_t p0_lep;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * Machine-independent initialization code
1047c478bd9Sstevel@tonic-gate  * Called from cold start routine as
1057c478bd9Sstevel@tonic-gate  * soon as a stack and segmentation
1067c478bd9Sstevel@tonic-gate  * have been established.
1077c478bd9Sstevel@tonic-gate  * Functions:
1087c478bd9Sstevel@tonic-gate  *	clear and free user core
1097c478bd9Sstevel@tonic-gate  *	turn on clock
1107c478bd9Sstevel@tonic-gate  *	hand craft 0th process
1117c478bd9Sstevel@tonic-gate  *	call all initialization routines
1127c478bd9Sstevel@tonic-gate  *	fork	- process 0 to schedule
1137c478bd9Sstevel@tonic-gate  *		- process 1 execute bootstrap
1147c478bd9Sstevel@tonic-gate  *		- process 2 to page out
1157c478bd9Sstevel@tonic-gate  *	create system threads
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int cluster_bootflags = 0;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate void
1217c478bd9Sstevel@tonic-gate cluster_wrapper(void)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	cluster();
1247c478bd9Sstevel@tonic-gate 	panic("cluster()  returned");
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1273f2f09c1Sdp char initname[INITNAME_SZ] = "/sbin/init";	/* also referenced by zone0 */
1283f2f09c1Sdp char initargs[BOOTARGS_MAX] = "";		/* also referenced by zone0 */
1299acbbeafSnn35248 extern int64_t lwp_sigmask(int, uint_t, uint_t);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1323f2f09c1Sdp  * Construct a stack for init containing the arguments to it, then
1333f2f09c1Sdp  * pass control to exec_common.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate int
1363f2f09c1Sdp exec_init(const char *initpath, const char *args)
1377c478bd9Sstevel@tonic-gate {
1383f2f09c1Sdp 	caddr32_t ucp;
1397c478bd9Sstevel@tonic-gate 	caddr32_t *uap;
1403f2f09c1Sdp 	caddr32_t *argv;
1413f2f09c1Sdp 	caddr32_t exec_fnamep;
1423f2f09c1Sdp 	char *scratchargs;
1433f2f09c1Sdp 	int i, sarg;
1443f2f09c1Sdp 	size_t argvlen, alen;
1453f2f09c1Sdp 	boolean_t in_arg;
1467c478bd9Sstevel@tonic-gate 	int argc = 0;
1473f2f09c1Sdp 	int error = 0, count = 0;
1487c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1497c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1509acbbeafSnn35248 	int brand_action;
1517c478bd9Sstevel@tonic-gate 
1523f2f09c1Sdp 	if (args == NULL)
1533f2f09c1Sdp 		args = "";
1547c478bd9Sstevel@tonic-gate 
1553f2f09c1Sdp 	alen = strlen(initpath) + 1 + strlen(args) + 1;
1563f2f09c1Sdp 	scratchargs = kmem_alloc(alen, KM_SLEEP);
1573f2f09c1Sdp 	(void) snprintf(scratchargs, alen, "%s %s", initpath, args);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1603f2f09c1Sdp 	 * We do a quick two state parse of the string to sort out how big
1613f2f09c1Sdp 	 * argc should be.
1627c478bd9Sstevel@tonic-gate 	 */
1633f2f09c1Sdp 	in_arg = B_FALSE;
1643f2f09c1Sdp 	for (i = 0; i < strlen(scratchargs); i++) {
1653f2f09c1Sdp 		if (scratchargs[i] == ' ' || scratchargs[i] == '\0') {
1663f2f09c1Sdp 			if (in_arg) {
1673f2f09c1Sdp 				in_arg = B_FALSE;
1683f2f09c1Sdp 				argc++;
1693f2f09c1Sdp 			}
1703f2f09c1Sdp 		} else {
1713f2f09c1Sdp 			in_arg = B_TRUE;
1723f2f09c1Sdp 		}
1733f2f09c1Sdp 	}
1743f2f09c1Sdp 	argvlen = sizeof (caddr32_t) * (argc + 1);
1753f2f09c1Sdp 	argv = kmem_zalloc(argvlen, KM_SLEEP);
1763f2f09c1Sdp 
1773f2f09c1Sdp 	/*
1783f2f09c1Sdp 	 * We pull off a bit of a hack here.  We work our way through the
1793f2f09c1Sdp 	 * args string, putting nulls at the ends of space delimited tokens
1803f2f09c1Sdp 	 * (boot args don't support quoting at this time).  Then we just
1813f2f09c1Sdp 	 * copy the whole mess to userland in one go.  In other words, we
1823f2f09c1Sdp 	 * transform this: "init -s -r\0" into this on the stack:
1833f2f09c1Sdp 	 *
1843f2f09c1Sdp 	 *	-0x00 \0
1853f2f09c1Sdp 	 *	-0x01 r
1863f2f09c1Sdp 	 *	-0x02 -  <--------.
1873f2f09c1Sdp 	 *	-0x03 \0	  |
1883f2f09c1Sdp 	 *	-0x04 s		  |
1893f2f09c1Sdp 	 *	-0x05 -  <------. |
1903f2f09c1Sdp 	 *	-0x06 \0	| |
1913f2f09c1Sdp 	 *	-0x07 t		| |
1923f2f09c1Sdp 	 *	-0x08 i 	| |
1933f2f09c1Sdp 	 *	-0x09 n		| |
1943f2f09c1Sdp 	 *	-0x0a i  <---.  | |
1953f2f09c1Sdp 	 *	-0x10 NULL   |  | |	(argv[3])
1963f2f09c1Sdp 	 *	-0x14   -----|--|-'	(argv[2])
1973f2f09c1Sdp 	 *	-0x18  ------|--'	(argv[1])
1983f2f09c1Sdp 	 *	-0x1c -------'		(argv[0])
1993f2f09c1Sdp 	 *
2003f2f09c1Sdp 	 * Since we know the value of ucp at the beginning of this process,
2013f2f09c1Sdp 	 * we can trivially compute the argv[] array which we also need to
2023f2f09c1Sdp 	 * place in userland: argv[i] = ucp - sarg(i), where ucp is the
2033f2f09c1Sdp 	 * stack ptr, and sarg is the string index of the start of the
2043f2f09c1Sdp 	 * argument.
2053f2f09c1Sdp 	 */
2063f2f09c1Sdp 	ucp = (caddr32_t)(uintptr_t)p->p_usrstack;
2073f2f09c1Sdp 
2083f2f09c1Sdp 	argc = 0;
2093f2f09c1Sdp 	in_arg = B_FALSE;
2103f2f09c1Sdp 	sarg = 0;
2113f2f09c1Sdp 
2123f2f09c1Sdp 	for (i = 0; i < alen; i++) {
2133f2f09c1Sdp 		if (scratchargs[i] == ' ' || scratchargs[i] == '\0') {
2143f2f09c1Sdp 			if (in_arg == B_TRUE) {
2153f2f09c1Sdp 				in_arg = B_FALSE;
2163f2f09c1Sdp 				scratchargs[i] = '\0';
2173f2f09c1Sdp 				argv[argc++] = ucp - (alen - sarg);
2183f2f09c1Sdp 			}
2193f2f09c1Sdp 		} else if (in_arg == B_FALSE) {
2203f2f09c1Sdp 			in_arg = B_TRUE;
2213f2f09c1Sdp 			sarg = i;
2223f2f09c1Sdp 		}
2233f2f09c1Sdp 	}
2243f2f09c1Sdp 	ucp -= alen;
2253f2f09c1Sdp 	error |= copyout(scratchargs, (caddr_t)(uintptr_t)ucp, alen);
2263f2f09c1Sdp 
2277c478bd9Sstevel@tonic-gate 	uap = (caddr32_t *)P2ALIGN((uintptr_t)ucp, sizeof (caddr32_t));
2283f2f09c1Sdp 	uap--;	/* advance to be below the word we're in */
2293f2f09c1Sdp 	uap -= (argc + 1);	/* advance argc words down, plus one for NULL */
2303f2f09c1Sdp 	error |= copyout(argv, uap, argvlen);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (error != 0) {
2337c478bd9Sstevel@tonic-gate 		zcmn_err(p->p_zone->zone_id, CE_WARN,
2347c478bd9Sstevel@tonic-gate 		    "Could not construct stack for init.\n");
2353f2f09c1Sdp 		kmem_free(argv, argvlen);
2363f2f09c1Sdp 		kmem_free(scratchargs, alen);
2377c478bd9Sstevel@tonic-gate 		return (EFAULT);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2403f2f09c1Sdp 	exec_fnamep = argv[0];
2413f2f09c1Sdp 	kmem_free(argv, argvlen);
2423f2f09c1Sdp 	kmem_free(scratchargs, alen);
2433f2f09c1Sdp 
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * Point at the arguments.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	lwp->lwp_ap = lwp->lwp_arg;
2483f2f09c1Sdp 	lwp->lwp_arg[0] = (uintptr_t)exec_fnamep;
2497c478bd9Sstevel@tonic-gate 	lwp->lwp_arg[1] = (uintptr_t)uap;
2507c478bd9Sstevel@tonic-gate 	lwp->lwp_arg[2] = NULL;
2517c478bd9Sstevel@tonic-gate 	curthread->t_post_sys = 1;
2527c478bd9Sstevel@tonic-gate 	curthread->t_sysnum = SYS_execve;
2537c478bd9Sstevel@tonic-gate 
2549acbbeafSnn35248 	/*
2559acbbeafSnn35248 	 * If we are executing init from zsched, we may have inherited its
2569acbbeafSnn35248 	 * parent process's signal mask.  Clear it now so that we behave in
2579acbbeafSnn35248 	 * the same way as when started from the global zone.
2589acbbeafSnn35248 	 */
2599acbbeafSnn35248 	(void) lwp_sigmask(SIG_UNBLOCK, 0xffffffff, 0xffffffff);
2609acbbeafSnn35248 
2619acbbeafSnn35248 	brand_action = ZONE_IS_BRANDED(p->p_zone) ? EBA_BRAND : EBA_NONE;
2627c478bd9Sstevel@tonic-gate again:
2633f2f09c1Sdp 	error = exec_common((const char *)(uintptr_t)exec_fnamep,
2649acbbeafSnn35248 	    (const char **)(uintptr_t)uap, NULL, brand_action);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Normally we would just set lwp_argsaved and t_post_sys and
2687c478bd9Sstevel@tonic-gate 	 * let post_syscall reset lwp_ap for us.  Unfortunately,
2697c478bd9Sstevel@tonic-gate 	 * exec_init isn't always called from a system call.  Instead
2707c478bd9Sstevel@tonic-gate 	 * of making a mess of trap_cleanup, we just reset the args
2717c478bd9Sstevel@tonic-gate 	 * pointer here.
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	reset_syscall_args();
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	switch (error) {
2767c478bd9Sstevel@tonic-gate 	case 0:
2777c478bd9Sstevel@tonic-gate 		return (0);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	case ENOENT:
2807c478bd9Sstevel@tonic-gate 		zcmn_err(p->p_zone->zone_id, CE_WARN,
2817c478bd9Sstevel@tonic-gate 		    "exec(%s) failed (file not found).\n", initpath);
2827c478bd9Sstevel@tonic-gate 		return (ENOENT);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	case EAGAIN:
2857c478bd9Sstevel@tonic-gate 	case EINTR:
2867c478bd9Sstevel@tonic-gate 		++count;
2877c478bd9Sstevel@tonic-gate 		if (count < 5) {
2887c478bd9Sstevel@tonic-gate 			zcmn_err(p->p_zone->zone_id, CE_WARN,
2897c478bd9Sstevel@tonic-gate 			    "exec(%s) failed with errno %d.  Retrying...\n",
2907c478bd9Sstevel@tonic-gate 			    initpath, error);
2917c478bd9Sstevel@tonic-gate 			goto again;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
2967c478bd9Sstevel@tonic-gate 	    "exec(%s) failed with errno %d.", initpath, error);
2977c478bd9Sstevel@tonic-gate 	return (error);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3003f2f09c1Sdp /*
3013f2f09c1Sdp  * This routine does all of the common setup for invoking init; global
3023f2f09c1Sdp  * and non-global zones employ this routine for the functionality which is
3033f2f09c1Sdp  * in common.
3043f2f09c1Sdp  *
3053f2f09c1Sdp  * This program (init, presumably) must be a 32-bit process.
3063f2f09c1Sdp  */
3073f2f09c1Sdp int
3083f2f09c1Sdp start_init_common()
3093f2f09c1Sdp {
3103f2f09c1Sdp 	proc_t *p = curproc;
3113f2f09c1Sdp 	ASSERT_STACK_ALIGNED();
3123f2f09c1Sdp 	p->p_zone->zone_proc_initpid = p->p_pid;
3133f2f09c1Sdp 
3143f2f09c1Sdp 	p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
3153f2f09c1Sdp 	p->p_usrstack = (caddr_t)USRSTACK32;
3163f2f09c1Sdp 	p->p_model = DATAMODEL_ILP32;
3173f2f09c1Sdp 	p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
3183f2f09c1Sdp 	p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
3193f2f09c1Sdp 	p->p_stk_ctl = INT32_MAX;
3203f2f09c1Sdp 
3213f2f09c1Sdp 	p->p_as = as_alloc();
322c6939658Ssl108498 	p->p_as->a_proc = p;
3233f2f09c1Sdp 	p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
3243f2f09c1Sdp 	(void) hat_setup(p->p_as->a_hat, HAT_INIT);
3253f2f09c1Sdp 
3263f2f09c1Sdp 	init_core();
3273f2f09c1Sdp 
3283f2f09c1Sdp 	init_mstate(curthread, LMS_SYSTEM);
3293f2f09c1Sdp 	return (exec_init(p->p_zone->zone_initname, p->p_zone->zone_bootargs));
3303f2f09c1Sdp }
3313f2f09c1Sdp 
3323f2f09c1Sdp /*
3333f2f09c1Sdp  * Start the initial user process for the global zone; once running, if
3343f2f09c1Sdp  * init should subsequently fail, it will be automatically be caught in the
3353f2f09c1Sdp  * exit(2) path, and restarted by restart_init().
3363f2f09c1Sdp  */
3373f2f09c1Sdp static void
3383f2f09c1Sdp start_init(void)
3393f2f09c1Sdp {
3403f2f09c1Sdp 	proc_init = curproc;
3413f2f09c1Sdp 
3423f2f09c1Sdp 	ASSERT(curproc->p_zone->zone_initname != NULL);
3433f2f09c1Sdp 
3443f2f09c1Sdp 	if (start_init_common() != 0)
3453f2f09c1Sdp 		halt("unix: Could not start init");
3463f2f09c1Sdp 	lwp_rtt();
3473f2f09c1Sdp }
3483f2f09c1Sdp 
349782793a8SVikram Hegde #if defined(__i386) || defined(__amd64)
35086c1f4dcSVikram Hegde extern void return_instr(void);
35186c1f4dcSVikram Hegde void (*rootnex_iommu_add_intr)(void) = (void (*)(void))return_instr;
352782793a8SVikram Hegde #endif
35386c1f4dcSVikram Hegde 
3547c478bd9Sstevel@tonic-gate void
3557c478bd9Sstevel@tonic-gate main(void)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	proc_t		*p = ttoproc(curthread);	/* &p0 */
3587c478bd9Sstevel@tonic-gate 	int		(**initptr)();
3597c478bd9Sstevel@tonic-gate 	extern void	sched();
3607c478bd9Sstevel@tonic-gate 	extern void	fsflush();
3617c478bd9Sstevel@tonic-gate 	extern int	(*init_tbl[])();
3627c478bd9Sstevel@tonic-gate 	extern int	(*mp_init_tbl[])();
3637c478bd9Sstevel@tonic-gate 	extern id_t	syscid, defaultcid;
3647c478bd9Sstevel@tonic-gate 	extern int	swaploaded;
3657c478bd9Sstevel@tonic-gate 	extern int	netboot;
3666cefaae1SJack Meng 	extern ib_boot_prop_t *iscsiboot_prop;
3677c478bd9Sstevel@tonic-gate 	extern void	vm_init(void);
368b9499e44Smrj 	extern void	cbe_init_pre(void);
3697c478bd9Sstevel@tonic-gate 	extern void	cbe_init(void);
3702850d85bSmv143129 	extern void	clock_tick_init_pre(void);
3712850d85bSmv143129 	extern void	clock_tick_init_post(void);
3727c478bd9Sstevel@tonic-gate 	extern void	clock_init(void);
3737c478bd9Sstevel@tonic-gate 	extern void	physio_bufs_init(void);
3747c478bd9Sstevel@tonic-gate 	extern void	pm_cfb_setup_intr(void);
3757c478bd9Sstevel@tonic-gate 	extern int	pm_adjust_timestamps(dev_info_t *, void *);
3767c478bd9Sstevel@tonic-gate 	extern void	start_other_cpus(int);
3777c478bd9Sstevel@tonic-gate 	extern void	sysevent_evc_thrinit();
3787c478bd9Sstevel@tonic-gate 	extern void	lgrp_main_init(void);
3797c478bd9Sstevel@tonic-gate 	extern void	lgrp_main_mp_init(void);
3805cff7825Smh27603 #if defined(__x86)
3815cff7825Smh27603 	extern void	cpupm_post_startup(void);
382753a6d45SSherry Moore 	extern void	fastboot_post_startup(void);
3835cff7825Smh27603 #endif
3847c478bd9Sstevel@tonic-gate 	/*
3857c478bd9Sstevel@tonic-gate 	 * In the horrible world of x86 in-lines, you can't get symbolic
3867c478bd9Sstevel@tonic-gate 	 * structure offsets a la genassym.  This assertion is here so
3877c478bd9Sstevel@tonic-gate 	 * that the next poor slob who innocently changes the offset of
3887c478bd9Sstevel@tonic-gate 	 * cpu_thread doesn't waste as much time as I just did finding
3897c478bd9Sstevel@tonic-gate 	 * out that it's hard-coded in i86/ml/i86.il.  Similarly for
3907c478bd9Sstevel@tonic-gate 	 * curcpup.  You're welcome.
3917c478bd9Sstevel@tonic-gate 	 */
3927c478bd9Sstevel@tonic-gate 	ASSERT(CPU == CPU->cpu_self);
3937c478bd9Sstevel@tonic-gate 	ASSERT(curthread == CPU->cpu_thread);
3947c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * Setup the first lgroup, and home t0
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	lgrp_setup();
4007c478bd9Sstevel@tonic-gate 
4015ce42f36Spraks 	/*
4025ce42f36Spraks 	 * Once 'startup()' completes, the thread_reaper() daemon would be
4035ce42f36Spraks 	 * created(in thread_init()). After that, it is safe to create threads
4045ce42f36Spraks 	 * that could exit. These exited threads will get reaped.
4055ce42f36Spraks 	 */
4067c478bd9Sstevel@tonic-gate 	startup();
4077c478bd9Sstevel@tonic-gate 	segkmem_gc();
4087c478bd9Sstevel@tonic-gate 	callb_init();
409b9499e44Smrj 	cbe_init_pre();	/* x86 must initialize gethrtimef before timer_init */
410dd4eeefdSeota 	timer_init();	/* timer must be initialized before cyclic starts */
4117c478bd9Sstevel@tonic-gate 	cbe_init();
41287a18d3fSMadhavan Venkataraman 	callout_init();	/* callout table MUST be init'd after cyclics */
4132850d85bSmv143129 	clock_tick_init_pre();
4147c478bd9Sstevel@tonic-gate 	clock_init();
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
417ae115bc7Smrj 	 * On some platforms, clkinitf() changes the timing source that
418ae115bc7Smrj 	 * gethrtime_unscaled() uses to generate timestamps.  cbe_init() calls
419ae115bc7Smrj 	 * clkinitf(), so re-initialize the microstate counters after the
420ae115bc7Smrj 	 * timesource has been chosen.
421ae115bc7Smrj 	 */
422ae115bc7Smrj 	init_mstate(&t0, LMS_SYSTEM);
423ae115bc7Smrj 	init_cpu_mstate(CPU, CMS_SYSTEM);
424ae115bc7Smrj 
425ae115bc7Smrj 	/*
4267c478bd9Sstevel@tonic-gate 	 * May need to probe to determine latencies from CPU 0 after
4277c478bd9Sstevel@tonic-gate 	 * gethrtime() comes alive in cbe_init() and before enabling interrupts
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	lgrp_plat_probe();
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/*
4327c478bd9Sstevel@tonic-gate 	 * Call all system initialization functions.
4337c478bd9Sstevel@tonic-gate 	 */
4347c478bd9Sstevel@tonic-gate 	for (initptr = &init_tbl[0]; *initptr; initptr++)
4357c478bd9Sstevel@tonic-gate 		(**initptr)();
4366cefaae1SJack Meng 	/*
4376cefaae1SJack Meng 	 * Load iSCSI boot properties
4386cefaae1SJack Meng 	 */
4396cefaae1SJack Meng 	ld_ib_prop();
4407c478bd9Sstevel@tonic-gate 	/*
4417c478bd9Sstevel@tonic-gate 	 * initialize vm related stuff.
4427c478bd9Sstevel@tonic-gate 	 */
4437c478bd9Sstevel@tonic-gate 	vm_init();
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/*
4467c478bd9Sstevel@tonic-gate 	 * initialize buffer pool for raw I/O requests
4477c478bd9Sstevel@tonic-gate 	 */
4487c478bd9Sstevel@tonic-gate 	physio_bufs_init();
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_error = 0; /* XXX kludge for SCSI driver */
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	/*
4537c478bd9Sstevel@tonic-gate 	 * Drop the interrupt level and allow interrupts.  At this point
4547c478bd9Sstevel@tonic-gate 	 * the DDI guarantees that interrupts are enabled.
4557c478bd9Sstevel@tonic-gate 	 */
4567c478bd9Sstevel@tonic-gate 	(void) spl0();
4577c478bd9Sstevel@tonic-gate 	interrupts_unleashed = 1;
4587c478bd9Sstevel@tonic-gate 
459782793a8SVikram Hegde #if defined(__i386) || defined(__amd64)
46086c1f4dcSVikram Hegde 	/*
46186c1f4dcSVikram Hegde 	 * add intel iommu fault event handler
46286c1f4dcSVikram Hegde 	 */
46386c1f4dcSVikram Hegde 	rootnex_iommu_add_intr();
464782793a8SVikram Hegde #endif
46586c1f4dcSVikram Hegde 
4667c478bd9Sstevel@tonic-gate 	vfs_mountroot();	/* Mount the root file system */
4677c478bd9Sstevel@tonic-gate 	errorq_init();		/* after vfs_mountroot() so DDI root is ready */
4687c478bd9Sstevel@tonic-gate 	cpu_kstat_init(CPU);	/* after vfs_mountroot() so TOD is valid */
4697c478bd9Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), pm_adjust_timestamps, NULL);
4707c478bd9Sstevel@tonic-gate 				/* after vfs_mountroot() so hrestime is valid */
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	post_startup();
4737c478bd9Sstevel@tonic-gate 	swaploaded = 1;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
476d3e55dcdSgww 	 * Initialize Solaris Audit Subsystem
4777c478bd9Sstevel@tonic-gate 	 */
478d3e55dcdSgww 	audit_init();
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Plumb the protocol modules and drivers only if we are not
4827c478bd9Sstevel@tonic-gate 	 * networked booted, in this case we already did it in rootconf().
4837c478bd9Sstevel@tonic-gate 	 */
4846cefaae1SJack Meng 	if (netboot == 0 && iscsiboot_prop == NULL)
4857c478bd9Sstevel@tonic-gate 		(void) strplumb();
4867c478bd9Sstevel@tonic-gate 
487ae115bc7Smrj 	gethrestime(&PTOU(curproc)->u_start);
488ae115bc7Smrj 	curthread->t_start = PTOU(curproc)->u_start.tv_sec;
4897c478bd9Sstevel@tonic-gate 	p->p_mstart = gethrtime();
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * Perform setup functions that can only be done after root
4937c478bd9Sstevel@tonic-gate 	 * and swap have been set up.
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 	consconfig();
4967c478bd9Sstevel@tonic-gate 	release_bootstrap();
4971b83305cSjm22469 
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * attach drivers with ddi-forceattach prop
5007c478bd9Sstevel@tonic-gate 	 * This must be done after consconfig() to prevent usb key/mouse
5017c478bd9Sstevel@tonic-gate 	 * from attaching before the upper console stream is plumbed.
5027c478bd9Sstevel@tonic-gate 	 * It must be done early enough to load hotplug drivers (e.g.
5037c478bd9Sstevel@tonic-gate 	 * pcmcia nexus) so that devices enumerated via hotplug is
5047c478bd9Sstevel@tonic-gate 	 * available before I/O subsystem is fully initialized.
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	i_ddi_forceattach_drivers();
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * Set the scan rate and other parameters of the paging subsystem.
5107c478bd9Sstevel@tonic-gate 	 */
5117c478bd9Sstevel@tonic-gate 	setupclock(0);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * Create kmem cache for proc structures
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	process_cache = kmem_cache_create("process_cache", sizeof (proc_t),
5177c478bd9Sstevel@tonic-gate 	    0, NULL, NULL, NULL, NULL, NULL, 0);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * Initialize process 0's lwp directory and lwpid hash table.
5217c478bd9Sstevel@tonic-gate 	 */
5227c478bd9Sstevel@tonic-gate 	p->p_lwpdir = p->p_lwpfree = p0_lwpdir;
5237c478bd9Sstevel@tonic-gate 	p->p_lwpdir->ld_next = p->p_lwpdir + 1;
5247c478bd9Sstevel@tonic-gate 	p->p_lwpdir_sz = 2;
5257c478bd9Sstevel@tonic-gate 	p->p_tidhash = p0_tidhash;
5267c478bd9Sstevel@tonic-gate 	p->p_tidhash_sz = 2;
5277c478bd9Sstevel@tonic-gate 	p0_lep.le_thread = curthread;
5287c478bd9Sstevel@tonic-gate 	p0_lep.le_lwpid = curthread->t_tid;
5297c478bd9Sstevel@tonic-gate 	p0_lep.le_start = curthread->t_start;
530*6eb30ec3SRoger A. Faulkner 	lwp_hash_in(p, &p0_lep, p0_tidhash, 2, 0);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Initialize extended accounting.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	exacct_init();
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	/*
5387c478bd9Sstevel@tonic-gate 	 * Initialize threads of sysevent event channels
5397c478bd9Sstevel@tonic-gate 	 */
5407c478bd9Sstevel@tonic-gate 	sysevent_evc_thrinit();
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * main lgroup initialization
5447c478bd9Sstevel@tonic-gate 	 * This must be done after post_startup(), but before
5457c478bd9Sstevel@tonic-gate 	 * start_other_cpus()
5467c478bd9Sstevel@tonic-gate 	 */
5477c478bd9Sstevel@tonic-gate 	lgrp_main_init();
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/*
5507c478bd9Sstevel@tonic-gate 	 * Perform MP initialization, if any.
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 	start_other_cpus(0);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * Finish lgrp initialization after all CPUS are brought online.
5567c478bd9Sstevel@tonic-gate 	 */
5577c478bd9Sstevel@tonic-gate 	lgrp_main_mp_init();
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	/*
5607c478bd9Sstevel@tonic-gate 	 * After mp_init(), number of cpus are known (this is
5617c478bd9Sstevel@tonic-gate 	 * true for the time being, when there are actually
5627c478bd9Sstevel@tonic-gate 	 * hot pluggable cpus then this scheme  would not do).
5637c478bd9Sstevel@tonic-gate 	 * Any per cpu initialization is done here.
5647c478bd9Sstevel@tonic-gate 	 */
5657c478bd9Sstevel@tonic-gate 	kmem_mp_init();
5667c478bd9Sstevel@tonic-gate 	vmem_update(NULL);
5677c478bd9Sstevel@tonic-gate 
5682850d85bSmv143129 	clock_tick_init_post();
5692850d85bSmv143129 
5707c478bd9Sstevel@tonic-gate 	for (initptr = &mp_init_tbl[0]; *initptr; initptr++)
5717c478bd9Sstevel@tonic-gate 		(**initptr)();
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/*
5745cff7825Smh27603 	 * These must be called after start_other_cpus
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	pm_cfb_setup_intr();
5775cff7825Smh27603 #if defined(__x86)
5785cff7825Smh27603 	cpupm_post_startup();
579753a6d45SSherry Moore 	fastboot_post_startup();
5805cff7825Smh27603 #endif
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	/*
5837c478bd9Sstevel@tonic-gate 	 * Make init process; enter scheduling loop with system process.
5847c478bd9Sstevel@tonic-gate 	 */
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* create init process */
5873f2f09c1Sdp 	if (newproc(start_init, NULL, defaultcid, 59, NULL))
5887c478bd9Sstevel@tonic-gate 		panic("main: unable to fork init.");
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/* create pageout daemon */
5917c478bd9Sstevel@tonic-gate 	if (newproc(pageout, NULL, syscid, maxclsyspri - 1, NULL))
5927c478bd9Sstevel@tonic-gate 		panic("main: unable to fork pageout()");
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/* create fsflush daemon */
5957c478bd9Sstevel@tonic-gate 	if (newproc(fsflush, NULL, syscid, minclsyspri, NULL))
5967c478bd9Sstevel@tonic-gate 		panic("main: unable to fork fsflush()");
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	/* create cluster process if we're a member of one */
5997c478bd9Sstevel@tonic-gate 	if (cluster_bootflags & CLUSTER_BOOTED) {
6007c478bd9Sstevel@tonic-gate 		if (newproc(cluster_wrapper, NULL, syscid, minclsyspri, NULL))
6017c478bd9Sstevel@tonic-gate 			panic("main: unable to fork cluster()");
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/*
6057c478bd9Sstevel@tonic-gate 	 * Create system threads (threads are associated with p0)
6067c478bd9Sstevel@tonic-gate 	 */
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* create module uninstall daemon */
6097c478bd9Sstevel@tonic-gate 	/* BugID 1132273. If swapping over NFS need a bigger stack */
6107c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())mod_uninstall_daemon,
6117c478bd9Sstevel@tonic-gate 	    NULL, 0, &p0, TS_RUN, minclsyspri);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, seg_pasync_thread,
6147c478bd9Sstevel@tonic-gate 	    NULL, 0, &p0, TS_RUN, minclsyspri);
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	pid_setmin();
6177c478bd9Sstevel@tonic-gate 
618ae115bc7Smrj 	bcopy("sched", PTOU(curproc)->u_psargs, 6);
619ae115bc7Smrj 	bcopy("sched", PTOU(curproc)->u_comm, 5);
6207c478bd9Sstevel@tonic-gate 	sched();
6217c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
6227c478bd9Sstevel@tonic-gate }
623