xref: /onnv-gate/usr/src/uts/common/os/main.c (revision 11895:fe3d4b94cbe2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
219393SRoger.Faulkner@Sun.COM 
220Sstevel@tonic-gate /*
2311585SVincent.Wang@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <sys/pcb.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/signal.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/vfs.h>
390Sstevel@tonic-gate #include <sys/vnode.h>
400Sstevel@tonic-gate #include <sys/proc.h>
410Sstevel@tonic-gate #include <sys/time.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <sys/priocntl.h>
440Sstevel@tonic-gate #include <sys/procset.h>
450Sstevel@tonic-gate #include <sys/disp.h>
460Sstevel@tonic-gate #include <sys/callo.h>
470Sstevel@tonic-gate #include <sys/callb.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/conf.h>
500Sstevel@tonic-gate #include <sys/bootconf.h>
510Sstevel@tonic-gate #include <sys/utsname.h>
520Sstevel@tonic-gate #include <sys/cmn_err.h>
530Sstevel@tonic-gate #include <sys/vmparam.h>
540Sstevel@tonic-gate #include <sys/modctl.h>
550Sstevel@tonic-gate #include <sys/vm.h>
560Sstevel@tonic-gate #include <sys/callb.h>
575107Seota #include <sys/ddi_timer.h>
580Sstevel@tonic-gate #include <sys/kmem.h>
590Sstevel@tonic-gate #include <sys/vmem.h>
600Sstevel@tonic-gate #include <sys/cpuvar.h>
610Sstevel@tonic-gate #include <sys/cladm.h>
620Sstevel@tonic-gate #include <sys/corectl.h>
630Sstevel@tonic-gate #include <sys/exec.h>
640Sstevel@tonic-gate #include <sys/syscall.h>
650Sstevel@tonic-gate #include <sys/reboot.h>
660Sstevel@tonic-gate #include <sys/task.h>
670Sstevel@tonic-gate #include <sys/exacct.h>
680Sstevel@tonic-gate #include <sys/autoconf.h>
690Sstevel@tonic-gate #include <sys/errorq.h>
700Sstevel@tonic-gate #include <sys/class.h>
710Sstevel@tonic-gate #include <sys/stack.h>
722712Snn35248 #include <sys/brand.h>
738212SMichael.Corcoran@Sun.COM #include <sys/mmapobj.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <vm/as.h>
760Sstevel@tonic-gate #include <vm/seg_kmem.h>
770Sstevel@tonic-gate #include <sys/dc_ki.h>
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #include <c2/audit.h>
808194SJack.Meng@Sun.COM #include <sys/bootprops.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /* well known processes */
830Sstevel@tonic-gate proc_t *proc_sched;		/* memory scheduler */
840Sstevel@tonic-gate proc_t *proc_init;		/* init */
850Sstevel@tonic-gate proc_t *proc_pageout;		/* pageout daemon */
860Sstevel@tonic-gate proc_t *proc_fsflush;		/* fsflush daemon */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate pgcnt_t	maxmem;		/* Maximum available memory in pages.	*/
890Sstevel@tonic-gate pgcnt_t	freemem;	/* Current available memory in pages.	*/
900Sstevel@tonic-gate int	interrupts_unleashed;	/* set when we do the first spl0() */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate kmem_cache_t *process_cache;	/* kmem cache for proc structures */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
9511861SMarek.Pospisil@Sun.COM  * Indicates whether the auditing module (c2audit) is loaded. Possible
9611861SMarek.Pospisil@Sun.COM  * values are:
9711861SMarek.Pospisil@Sun.COM  * 0 - c2audit module is excluded in /etc/system and cannot be loaded
9811861SMarek.Pospisil@Sun.COM  * 1 - c2audit module is not loaded but can be anytime
9911861SMarek.Pospisil@Sun.COM  * 2 - c2audit module is loaded
10011861SMarek.Pospisil@Sun.COM  */
10111861SMarek.Pospisil@Sun.COM int audit_active = C2AUDIT_DISABLED;
10211861SMarek.Pospisil@Sun.COM 
10311861SMarek.Pospisil@Sun.COM /*
1040Sstevel@tonic-gate  * Process 0's lwp directory and lwpid hash table.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate lwpdir_t p0_lwpdir[2];
1079393SRoger.Faulkner@Sun.COM tidhash_t p0_tidhash[2];
1080Sstevel@tonic-gate lwpent_t p0_lep;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * Machine-independent initialization code
1120Sstevel@tonic-gate  * Called from cold start routine as
1130Sstevel@tonic-gate  * soon as a stack and segmentation
1140Sstevel@tonic-gate  * have been established.
1150Sstevel@tonic-gate  * Functions:
1160Sstevel@tonic-gate  *	clear and free user core
1170Sstevel@tonic-gate  *	turn on clock
1180Sstevel@tonic-gate  *	hand craft 0th process
1190Sstevel@tonic-gate  *	call all initialization routines
1200Sstevel@tonic-gate  *	fork	- process 0 to schedule
1210Sstevel@tonic-gate  *		- process 1 execute bootstrap
1220Sstevel@tonic-gate  *		- process 2 to page out
1230Sstevel@tonic-gate  *	create system threads
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate int cluster_bootflags = 0;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate void
1290Sstevel@tonic-gate cluster_wrapper(void)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	cluster();
1320Sstevel@tonic-gate 	panic("cluster()  returned");
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1352267Sdp char initname[INITNAME_SZ] = "/sbin/init";	/* also referenced by zone0 */
1362267Sdp char initargs[BOOTARGS_MAX] = "";		/* also referenced by zone0 */
1372712Snn35248 extern int64_t lwp_sigmask(int, uint_t, uint_t);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1402267Sdp  * Construct a stack for init containing the arguments to it, then
1412267Sdp  * pass control to exec_common.
1420Sstevel@tonic-gate  */
1432267Sdp int
1442267Sdp exec_init(const char *initpath, const char *args)
1450Sstevel@tonic-gate {
1462267Sdp 	caddr32_t ucp;
1470Sstevel@tonic-gate 	caddr32_t *uap;
1482267Sdp 	caddr32_t *argv;
1492267Sdp 	caddr32_t exec_fnamep;
1502267Sdp 	char *scratchargs;
1512267Sdp 	int i, sarg;
1522267Sdp 	size_t argvlen, alen;
1532267Sdp 	boolean_t in_arg;
1540Sstevel@tonic-gate 	int argc = 0;
1552267Sdp 	int error = 0, count = 0;
1560Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1570Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1582712Snn35248 	int brand_action;
1590Sstevel@tonic-gate 
1602267Sdp 	if (args == NULL)
1612267Sdp 		args = "";
1620Sstevel@tonic-gate 
1632267Sdp 	alen = strlen(initpath) + 1 + strlen(args) + 1;
1642267Sdp 	scratchargs = kmem_alloc(alen, KM_SLEEP);
1652267Sdp 	(void) snprintf(scratchargs, alen, "%s %s", initpath, args);
1660Sstevel@tonic-gate 
1672267Sdp 	/*
1682267Sdp 	 * We do a quick two state parse of the string to sort out how big
1692267Sdp 	 * argc should be.
1702267Sdp 	 */
1712267Sdp 	in_arg = B_FALSE;
1722267Sdp 	for (i = 0; i < strlen(scratchargs); i++) {
1732267Sdp 		if (scratchargs[i] == ' ' || scratchargs[i] == '\0') {
1742267Sdp 			if (in_arg) {
1752267Sdp 				in_arg = B_FALSE;
1762267Sdp 				argc++;
1772267Sdp 			}
1782267Sdp 		} else {
1792267Sdp 			in_arg = B_TRUE;
1802267Sdp 		}
1810Sstevel@tonic-gate 	}
1822267Sdp 	argvlen = sizeof (caddr32_t) * (argc + 1);
1832267Sdp 	argv = kmem_zalloc(argvlen, KM_SLEEP);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1862267Sdp 	 * We pull off a bit of a hack here.  We work our way through the
1872267Sdp 	 * args string, putting nulls at the ends of space delimited tokens
1882267Sdp 	 * (boot args don't support quoting at this time).  Then we just
1892267Sdp 	 * copy the whole mess to userland in one go.  In other words, we
1902267Sdp 	 * transform this: "init -s -r\0" into this on the stack:
1912267Sdp 	 *
1922267Sdp 	 *	-0x00 \0
1932267Sdp 	 *	-0x01 r
1942267Sdp 	 *	-0x02 -  <--------.
1952267Sdp 	 *	-0x03 \0	  |
1962267Sdp 	 *	-0x04 s		  |
1972267Sdp 	 *	-0x05 -  <------. |
1982267Sdp 	 *	-0x06 \0	| |
1992267Sdp 	 *	-0x07 t		| |
2002267Sdp 	 *	-0x08 i 	| |
2012267Sdp 	 *	-0x09 n		| |
2022267Sdp 	 *	-0x0a i  <---.  | |
2032267Sdp 	 *	-0x10 NULL   |  | |	(argv[3])
2042267Sdp 	 *	-0x14   -----|--|-'	(argv[2])
2052267Sdp 	 *	-0x18  ------|--'	(argv[1])
2062267Sdp 	 *	-0x1c -------'		(argv[0])
2072267Sdp 	 *
2082267Sdp 	 * Since we know the value of ucp at the beginning of this process,
2092267Sdp 	 * we can trivially compute the argv[] array which we also need to
2102267Sdp 	 * place in userland: argv[i] = ucp - sarg(i), where ucp is the
2112267Sdp 	 * stack ptr, and sarg is the string index of the start of the
2122267Sdp 	 * argument.
2130Sstevel@tonic-gate 	 */
2142267Sdp 	ucp = (caddr32_t)(uintptr_t)p->p_usrstack;
2152267Sdp 
2162267Sdp 	argc = 0;
2172267Sdp 	in_arg = B_FALSE;
2182267Sdp 	sarg = 0;
2192267Sdp 
2202267Sdp 	for (i = 0; i < alen; i++) {
2212267Sdp 		if (scratchargs[i] == ' ' || scratchargs[i] == '\0') {
2222267Sdp 			if (in_arg == B_TRUE) {
2232267Sdp 				in_arg = B_FALSE;
2242267Sdp 				scratchargs[i] = '\0';
2252267Sdp 				argv[argc++] = ucp - (alen - sarg);
2262267Sdp 			}
2272267Sdp 		} else if (in_arg == B_FALSE) {
2282267Sdp 			in_arg = B_TRUE;
2292267Sdp 			sarg = i;
2302267Sdp 		}
2312267Sdp 	}
2322267Sdp 	ucp -= alen;
2332267Sdp 	error |= copyout(scratchargs, (caddr_t)(uintptr_t)ucp, alen);
2342267Sdp 
2350Sstevel@tonic-gate 	uap = (caddr32_t *)P2ALIGN((uintptr_t)ucp, sizeof (caddr32_t));
2362267Sdp 	uap--;	/* advance to be below the word we're in */
2372267Sdp 	uap -= (argc + 1);	/* advance argc words down, plus one for NULL */
2382267Sdp 	error |= copyout(argv, uap, argvlen);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (error != 0) {
2410Sstevel@tonic-gate 		zcmn_err(p->p_zone->zone_id, CE_WARN,
2420Sstevel@tonic-gate 		    "Could not construct stack for init.\n");
2432267Sdp 		kmem_free(argv, argvlen);
2442267Sdp 		kmem_free(scratchargs, alen);
2450Sstevel@tonic-gate 		return (EFAULT);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2482267Sdp 	exec_fnamep = argv[0];
2492267Sdp 	kmem_free(argv, argvlen);
2502267Sdp 	kmem_free(scratchargs, alen);
2512267Sdp 
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * Point at the arguments.
2540Sstevel@tonic-gate 	 */
2550Sstevel@tonic-gate 	lwp->lwp_ap = lwp->lwp_arg;
2562267Sdp 	lwp->lwp_arg[0] = (uintptr_t)exec_fnamep;
2570Sstevel@tonic-gate 	lwp->lwp_arg[1] = (uintptr_t)uap;
2580Sstevel@tonic-gate 	lwp->lwp_arg[2] = NULL;
2590Sstevel@tonic-gate 	curthread->t_post_sys = 1;
2600Sstevel@tonic-gate 	curthread->t_sysnum = SYS_execve;
2610Sstevel@tonic-gate 
2622712Snn35248 	/*
2632712Snn35248 	 * If we are executing init from zsched, we may have inherited its
2642712Snn35248 	 * parent process's signal mask.  Clear it now so that we behave in
2652712Snn35248 	 * the same way as when started from the global zone.
2662712Snn35248 	 */
2672712Snn35248 	(void) lwp_sigmask(SIG_UNBLOCK, 0xffffffff, 0xffffffff);
2682712Snn35248 
2692712Snn35248 	brand_action = ZONE_IS_BRANDED(p->p_zone) ? EBA_BRAND : EBA_NONE;
2700Sstevel@tonic-gate again:
2712267Sdp 	error = exec_common((const char *)(uintptr_t)exec_fnamep,
2722712Snn35248 	    (const char **)(uintptr_t)uap, NULL, brand_action);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 * Normally we would just set lwp_argsaved and t_post_sys and
2760Sstevel@tonic-gate 	 * let post_syscall reset lwp_ap for us.  Unfortunately,
2770Sstevel@tonic-gate 	 * exec_init isn't always called from a system call.  Instead
2780Sstevel@tonic-gate 	 * of making a mess of trap_cleanup, we just reset the args
2790Sstevel@tonic-gate 	 * pointer here.
2800Sstevel@tonic-gate 	 */
2810Sstevel@tonic-gate 	reset_syscall_args();
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	switch (error) {
2840Sstevel@tonic-gate 	case 0:
2850Sstevel@tonic-gate 		return (0);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	case ENOENT:
2880Sstevel@tonic-gate 		zcmn_err(p->p_zone->zone_id, CE_WARN,
2890Sstevel@tonic-gate 		    "exec(%s) failed (file not found).\n", initpath);
2900Sstevel@tonic-gate 		return (ENOENT);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	case EAGAIN:
2930Sstevel@tonic-gate 	case EINTR:
2940Sstevel@tonic-gate 		++count;
2950Sstevel@tonic-gate 		if (count < 5) {
2960Sstevel@tonic-gate 			zcmn_err(p->p_zone->zone_id, CE_WARN,
2970Sstevel@tonic-gate 			    "exec(%s) failed with errno %d.  Retrying...\n",
2980Sstevel@tonic-gate 			    initpath, error);
2990Sstevel@tonic-gate 			goto again;
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
3040Sstevel@tonic-gate 	    "exec(%s) failed with errno %d.", initpath, error);
3050Sstevel@tonic-gate 	return (error);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
3082267Sdp /*
3092267Sdp  * This routine does all of the common setup for invoking init; global
3102267Sdp  * and non-global zones employ this routine for the functionality which is
3112267Sdp  * in common.
3122267Sdp  *
3132267Sdp  * This program (init, presumably) must be a 32-bit process.
3142267Sdp  */
3152267Sdp int
3162267Sdp start_init_common()
3172267Sdp {
3182267Sdp 	proc_t *p = curproc;
3192267Sdp 	ASSERT_STACK_ALIGNED();
3202267Sdp 	p->p_zone->zone_proc_initpid = p->p_pid;
3212267Sdp 
3222267Sdp 	p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
3232267Sdp 	p->p_usrstack = (caddr_t)USRSTACK32;
3242267Sdp 	p->p_model = DATAMODEL_ILP32;
3252267Sdp 	p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
3262267Sdp 	p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
3272267Sdp 	p->p_stk_ctl = INT32_MAX;
3282267Sdp 
3292267Sdp 	p->p_as = as_alloc();
3302768Ssl108498 	p->p_as->a_proc = p;
3312267Sdp 	p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
3322267Sdp 	(void) hat_setup(p->p_as->a_hat, HAT_INIT);
3332267Sdp 
3342267Sdp 	init_core();
3352267Sdp 
3362267Sdp 	init_mstate(curthread, LMS_SYSTEM);
3372267Sdp 	return (exec_init(p->p_zone->zone_initname, p->p_zone->zone_bootargs));
3382267Sdp }
3392267Sdp 
3402267Sdp /*
3412267Sdp  * Start the initial user process for the global zone; once running, if
3422267Sdp  * init should subsequently fail, it will be automatically be caught in the
3432267Sdp  * exit(2) path, and restarted by restart_init().
3442267Sdp  */
3452267Sdp static void
3462267Sdp start_init(void)
3472267Sdp {
3482267Sdp 	proc_init = curproc;
3492267Sdp 
3502267Sdp 	ASSERT(curproc->p_zone->zone_initname != NULL);
3512267Sdp 
3522267Sdp 	if (start_init_common() != 0)
3532267Sdp 		halt("unix: Could not start init");
3542267Sdp 	lwp_rtt();
3552267Sdp }
3562267Sdp 
3570Sstevel@tonic-gate void
3580Sstevel@tonic-gate main(void)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	proc_t		*p = ttoproc(curthread);	/* &p0 */
3610Sstevel@tonic-gate 	int		(**initptr)();
3620Sstevel@tonic-gate 	extern void	sched();
3630Sstevel@tonic-gate 	extern void	fsflush();
3640Sstevel@tonic-gate 	extern int	(*init_tbl[])();
3650Sstevel@tonic-gate 	extern int	(*mp_init_tbl[])();
3660Sstevel@tonic-gate 	extern id_t	syscid, defaultcid;
3670Sstevel@tonic-gate 	extern int	swaploaded;
3680Sstevel@tonic-gate 	extern int	netboot;
3698194SJack.Meng@Sun.COM 	extern ib_boot_prop_t *iscsiboot_prop;
3700Sstevel@tonic-gate 	extern void	vm_init(void);
3717039Smrj 	extern void	cbe_init_pre(void);
3720Sstevel@tonic-gate 	extern void	cbe_init(void);
3735788Smv143129 	extern void	clock_tick_init_pre(void);
3745788Smv143129 	extern void	clock_tick_init_post(void);
3750Sstevel@tonic-gate 	extern void	clock_init(void);
3760Sstevel@tonic-gate 	extern void	physio_bufs_init(void);
3770Sstevel@tonic-gate 	extern void	pm_cfb_setup_intr(void);
3780Sstevel@tonic-gate 	extern int	pm_adjust_timestamps(dev_info_t *, void *);
3790Sstevel@tonic-gate 	extern void	start_other_cpus(int);
3800Sstevel@tonic-gate 	extern void	sysevent_evc_thrinit();
3814667Smh27603 #if defined(__x86)
3829160SSherry.Moore@Sun.COM 	extern void	fastboot_post_startup(void);
383*11895SEnrico.Perla@Sun.COM 	extern void	progressbar_start(void);
3844667Smh27603 #endif
3850Sstevel@tonic-gate 	/*
3860Sstevel@tonic-gate 	 * In the horrible world of x86 in-lines, you can't get symbolic
3870Sstevel@tonic-gate 	 * structure offsets a la genassym.  This assertion is here so
3880Sstevel@tonic-gate 	 * that the next poor slob who innocently changes the offset of
3890Sstevel@tonic-gate 	 * cpu_thread doesn't waste as much time as I just did finding
3900Sstevel@tonic-gate 	 * out that it's hard-coded in i86/ml/i86.il.  Similarly for
3910Sstevel@tonic-gate 	 * curcpup.  You're welcome.
3920Sstevel@tonic-gate 	 */
3930Sstevel@tonic-gate 	ASSERT(CPU == CPU->cpu_self);
3940Sstevel@tonic-gate 	ASSERT(curthread == CPU->cpu_thread);
3950Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
39810710Sjonathan.chew@sun.com 	 * Setup root lgroup and leaf lgroup for CPU 0
3990Sstevel@tonic-gate 	 */
40010710Sjonathan.chew@sun.com 	lgrp_init(LGRP_INIT_STAGE2);
4010Sstevel@tonic-gate 
4024036Spraks 	/*
4034036Spraks 	 * Once 'startup()' completes, the thread_reaper() daemon would be
4044036Spraks 	 * created(in thread_init()). After that, it is safe to create threads
4054036Spraks 	 * that could exit. These exited threads will get reaped.
4064036Spraks 	 */
4070Sstevel@tonic-gate 	startup();
4080Sstevel@tonic-gate 	segkmem_gc();
4090Sstevel@tonic-gate 	callb_init();
4107039Smrj 	cbe_init_pre();	/* x86 must initialize gethrtimef before timer_init */
4115107Seota 	timer_init();	/* timer must be initialized before cyclic starts */
4120Sstevel@tonic-gate 	cbe_init();
4138048SMadhavan.Venkataraman@Sun.COM 	callout_init();	/* callout table MUST be init'd after cyclics */
4145788Smv143129 	clock_tick_init_pre();
4150Sstevel@tonic-gate 	clock_init();
4160Sstevel@tonic-gate 
417*11895SEnrico.Perla@Sun.COM #if defined(__x86)
418*11895SEnrico.Perla@Sun.COM 	/*
419*11895SEnrico.Perla@Sun.COM 	 * The progressbar thread uses cv_reltimedwait() and hence needs to be
420*11895SEnrico.Perla@Sun.COM 	 * started after the callout mechanism has been initialized.
421*11895SEnrico.Perla@Sun.COM 	 */
422*11895SEnrico.Perla@Sun.COM 	progressbar_start();
423*11895SEnrico.Perla@Sun.COM #endif
4240Sstevel@tonic-gate 	/*
4253446Smrj 	 * On some platforms, clkinitf() changes the timing source that
4263446Smrj 	 * gethrtime_unscaled() uses to generate timestamps.  cbe_init() calls
4273446Smrj 	 * clkinitf(), so re-initialize the microstate counters after the
4283446Smrj 	 * timesource has been chosen.
4293446Smrj 	 */
4303446Smrj 	init_mstate(&t0, LMS_SYSTEM);
4313446Smrj 	init_cpu_mstate(CPU, CMS_SYSTEM);
4323446Smrj 
4333446Smrj 	/*
4340Sstevel@tonic-gate 	 * May need to probe to determine latencies from CPU 0 after
4350Sstevel@tonic-gate 	 * gethrtime() comes alive in cbe_init() and before enabling interrupts
43610710Sjonathan.chew@sun.com 	 * and copy and release any temporary memory allocated with BOP_ALLOC()
43710710Sjonathan.chew@sun.com 	 * before release_bootstrap() frees boot memory
4380Sstevel@tonic-gate 	 */
43910710Sjonathan.chew@sun.com 	lgrp_init(LGRP_INIT_STAGE3);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	/*
4420Sstevel@tonic-gate 	 * Call all system initialization functions.
4430Sstevel@tonic-gate 	 */
4440Sstevel@tonic-gate 	for (initptr = &init_tbl[0]; *initptr; initptr++)
4450Sstevel@tonic-gate 		(**initptr)();
4468194SJack.Meng@Sun.COM 	/*
4478194SJack.Meng@Sun.COM 	 * Load iSCSI boot properties
4488194SJack.Meng@Sun.COM 	 */
4498194SJack.Meng@Sun.COM 	ld_ib_prop();
4500Sstevel@tonic-gate 	/*
4510Sstevel@tonic-gate 	 * initialize vm related stuff.
4520Sstevel@tonic-gate 	 */
4530Sstevel@tonic-gate 	vm_init();
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/*
4560Sstevel@tonic-gate 	 * initialize buffer pool for raw I/O requests
4570Sstevel@tonic-gate 	 */
4580Sstevel@tonic-gate 	physio_bufs_init();
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	ttolwp(curthread)->lwp_error = 0; /* XXX kludge for SCSI driver */
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	/*
4630Sstevel@tonic-gate 	 * Drop the interrupt level and allow interrupts.  At this point
4640Sstevel@tonic-gate 	 * the DDI guarantees that interrupts are enabled.
4650Sstevel@tonic-gate 	 */
4660Sstevel@tonic-gate 	(void) spl0();
4670Sstevel@tonic-gate 	interrupts_unleashed = 1;
4680Sstevel@tonic-gate 
46911173SJonathan.Adams@Sun.COM 	/*
47011173SJonathan.Adams@Sun.COM 	 * Create kmem cache for proc structures
47111173SJonathan.Adams@Sun.COM 	 */
47211173SJonathan.Adams@Sun.COM 	process_cache = kmem_cache_create("process_cache", sizeof (proc_t),
47311173SJonathan.Adams@Sun.COM 	    0, NULL, NULL, NULL, NULL, NULL, 0);
47411173SJonathan.Adams@Sun.COM 
4750Sstevel@tonic-gate 	vfs_mountroot();	/* Mount the root file system */
4760Sstevel@tonic-gate 	errorq_init();		/* after vfs_mountroot() so DDI root is ready */
4770Sstevel@tonic-gate 	cpu_kstat_init(CPU);	/* after vfs_mountroot() so TOD is valid */
4780Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), pm_adjust_timestamps, NULL);
4790Sstevel@tonic-gate 				/* after vfs_mountroot() so hrestime is valid */
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	post_startup();
4820Sstevel@tonic-gate 	swaploaded = 1;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/*
4855753Sgww 	 * Initialize Solaris Audit Subsystem
4860Sstevel@tonic-gate 	 */
4875753Sgww 	audit_init();
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	/*
4900Sstevel@tonic-gate 	 * Plumb the protocol modules and drivers only if we are not
4910Sstevel@tonic-gate 	 * networked booted, in this case we already did it in rootconf().
4920Sstevel@tonic-gate 	 */
4938194SJack.Meng@Sun.COM 	if (netboot == 0 && iscsiboot_prop == NULL)
4940Sstevel@tonic-gate 		(void) strplumb();
4950Sstevel@tonic-gate 
4963446Smrj 	gethrestime(&PTOU(curproc)->u_start);
4973446Smrj 	curthread->t_start = PTOU(curproc)->u_start.tv_sec;
4980Sstevel@tonic-gate 	p->p_mstart = gethrtime();
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * Perform setup functions that can only be done after root
5020Sstevel@tonic-gate 	 * and swap have been set up.
5030Sstevel@tonic-gate 	 */
5040Sstevel@tonic-gate 	consconfig();
50510182Sbijan.mottahedeh@sun.com #ifndef	__sparc
50610182Sbijan.mottahedeh@sun.com 	release_bootstrap();
50710182Sbijan.mottahedeh@sun.com #endif
5085974Sjm22469 
5090Sstevel@tonic-gate 	/*
5100Sstevel@tonic-gate 	 * attach drivers with ddi-forceattach prop
5110Sstevel@tonic-gate 	 * It must be done early enough to load hotplug drivers (e.g.
5120Sstevel@tonic-gate 	 * pcmcia nexus) so that devices enumerated via hotplug is
5130Sstevel@tonic-gate 	 * available before I/O subsystem is fully initialized.
5140Sstevel@tonic-gate 	 */
5150Sstevel@tonic-gate 	i_ddi_forceattach_drivers();
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/*
5180Sstevel@tonic-gate 	 * Set the scan rate and other parameters of the paging subsystem.
5190Sstevel@tonic-gate 	 */
5200Sstevel@tonic-gate 	setupclock(0);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/*
5230Sstevel@tonic-gate 	 * Initialize process 0's lwp directory and lwpid hash table.
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	p->p_lwpdir = p->p_lwpfree = p0_lwpdir;
5260Sstevel@tonic-gate 	p->p_lwpdir->ld_next = p->p_lwpdir + 1;
5270Sstevel@tonic-gate 	p->p_lwpdir_sz = 2;
5280Sstevel@tonic-gate 	p->p_tidhash = p0_tidhash;
5290Sstevel@tonic-gate 	p->p_tidhash_sz = 2;
5300Sstevel@tonic-gate 	p0_lep.le_thread = curthread;
5310Sstevel@tonic-gate 	p0_lep.le_lwpid = curthread->t_tid;
5320Sstevel@tonic-gate 	p0_lep.le_start = curthread->t_start;
5339393SRoger.Faulkner@Sun.COM 	lwp_hash_in(p, &p0_lep, p0_tidhash, 2, 0);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/*
5360Sstevel@tonic-gate 	 * Initialize extended accounting.
5370Sstevel@tonic-gate 	 */
5380Sstevel@tonic-gate 	exacct_init();
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/*
5410Sstevel@tonic-gate 	 * Initialize threads of sysevent event channels
5420Sstevel@tonic-gate 	 */
5430Sstevel@tonic-gate 	sysevent_evc_thrinit();
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	/*
54610710Sjonathan.chew@sun.com 	 * This must be done after post_startup() but before
5470Sstevel@tonic-gate 	 * start_other_cpus()
5480Sstevel@tonic-gate 	 */
54910710Sjonathan.chew@sun.com 	lgrp_init(LGRP_INIT_STAGE4);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	/*
5520Sstevel@tonic-gate 	 * Perform MP initialization, if any.
5530Sstevel@tonic-gate 	 */
5540Sstevel@tonic-gate 	start_other_cpus(0);
5550Sstevel@tonic-gate 
55610182Sbijan.mottahedeh@sun.com #ifdef	__sparc
5570Sstevel@tonic-gate 	/*
55810106SJason.Beloro@Sun.COM 	 * Release bootstrap here since PROM interfaces are
55910106SJason.Beloro@Sun.COM 	 * used to start other CPUs above.
56010106SJason.Beloro@Sun.COM 	 */
56110106SJason.Beloro@Sun.COM 	release_bootstrap();
56210182Sbijan.mottahedeh@sun.com #endif
56310106SJason.Beloro@Sun.COM 
56410106SJason.Beloro@Sun.COM 	/*
5650Sstevel@tonic-gate 	 * Finish lgrp initialization after all CPUS are brought online.
5660Sstevel@tonic-gate 	 */
56710710Sjonathan.chew@sun.com 	lgrp_init(LGRP_INIT_STAGE5);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * After mp_init(), number of cpus are known (this is
5710Sstevel@tonic-gate 	 * true for the time being, when there are actually
5720Sstevel@tonic-gate 	 * hot pluggable cpus then this scheme  would not do).
5730Sstevel@tonic-gate 	 * Any per cpu initialization is done here.
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 	kmem_mp_init();
5760Sstevel@tonic-gate 	vmem_update(NULL);
5770Sstevel@tonic-gate 
5785788Smv143129 	clock_tick_init_post();
5795788Smv143129 
5800Sstevel@tonic-gate 	for (initptr = &mp_init_tbl[0]; *initptr; initptr++)
5810Sstevel@tonic-gate 		(**initptr)();
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/*
5844667Smh27603 	 * These must be called after start_other_cpus
5850Sstevel@tonic-gate 	 */
5860Sstevel@tonic-gate 	pm_cfb_setup_intr();
5874667Smh27603 #if defined(__x86)
5889160SSherry.Moore@Sun.COM 	fastboot_post_startup();
5894667Smh27603 #endif
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/*
5920Sstevel@tonic-gate 	 * Make init process; enter scheduling loop with system process.
59311173SJonathan.Adams@Sun.COM 	 *
59411173SJonathan.Adams@Sun.COM 	 * Note that we manually assign the pids for these processes, for
59511173SJonathan.Adams@Sun.COM 	 * historical reasons.  If more pre-assigned pids are needed,
59611173SJonathan.Adams@Sun.COM 	 * FAMOUS_PIDS will have to be updated.
5970Sstevel@tonic-gate 	 */
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	/* create init process */
60011173SJonathan.Adams@Sun.COM 	if (newproc(start_init, NULL, defaultcid, 59, NULL,
60111173SJonathan.Adams@Sun.COM 	    FAMOUS_PID_INIT))
6020Sstevel@tonic-gate 		panic("main: unable to fork init.");
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	/* create pageout daemon */
60511173SJonathan.Adams@Sun.COM 	if (newproc(pageout, NULL, syscid, maxclsyspri - 1, NULL,
60611173SJonathan.Adams@Sun.COM 	    FAMOUS_PID_PAGEOUT))
6070Sstevel@tonic-gate 		panic("main: unable to fork pageout()");
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	/* create fsflush daemon */
61011173SJonathan.Adams@Sun.COM 	if (newproc(fsflush, NULL, syscid, minclsyspri, NULL,
61111173SJonathan.Adams@Sun.COM 	    FAMOUS_PID_FSFLUSH))
6120Sstevel@tonic-gate 		panic("main: unable to fork fsflush()");
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/* create cluster process if we're a member of one */
6150Sstevel@tonic-gate 	if (cluster_bootflags & CLUSTER_BOOTED) {
61611173SJonathan.Adams@Sun.COM 		if (newproc(cluster_wrapper, NULL, syscid, minclsyspri,
61711173SJonathan.Adams@Sun.COM 		    NULL, 0)) {
6180Sstevel@tonic-gate 			panic("main: unable to fork cluster()");
61911173SJonathan.Adams@Sun.COM 		}
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/*
6230Sstevel@tonic-gate 	 * Create system threads (threads are associated with p0)
6240Sstevel@tonic-gate 	 */
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	/* create module uninstall daemon */
6270Sstevel@tonic-gate 	/* BugID 1132273. If swapping over NFS need a bigger stack */
6280Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())mod_uninstall_daemon,
6290Sstevel@tonic-gate 	    NULL, 0, &p0, TS_RUN, minclsyspri);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	(void) thread_create(NULL, 0, seg_pasync_thread,
6320Sstevel@tonic-gate 	    NULL, 0, &p0, TS_RUN, minclsyspri);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	pid_setmin();
6350Sstevel@tonic-gate 
6363446Smrj 	bcopy("sched", PTOU(curproc)->u_psargs, 6);
6373446Smrj 	bcopy("sched", PTOU(curproc)->u_comm, 5);
6380Sstevel@tonic-gate 	sched();
6390Sstevel@tonic-gate 	/* NOTREACHED */
6400Sstevel@tonic-gate }
641