Home | History | Annotate | Download | only in os
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 /*	Copyright (c) 1988 AT&T	*/
     27 /*	  All Rights Reserved  	*/
     28 
     29 
     30 #include <sys/types.h>
     31 #include <sys/param.h>
     32 #include <sys/sysmacros.h>
     33 #include <sys/pcb.h>
     34 #include <sys/systm.h>
     35 #include <sys/signal.h>
     36 #include <sys/cred.h>
     37 #include <sys/user.h>
     38 #include <sys/vfs.h>
     39 #include <sys/vnode.h>
     40 #include <sys/proc.h>
     41 #include <sys/time.h>
     42 #include <sys/file.h>
     43 #include <sys/priocntl.h>
     44 #include <sys/procset.h>
     45 #include <sys/disp.h>
     46 #include <sys/callo.h>
     47 #include <sys/callb.h>
     48 #include <sys/debug.h>
     49 #include <sys/conf.h>
     50 #include <sys/bootconf.h>
     51 #include <sys/utsname.h>
     52 #include <sys/cmn_err.h>
     53 #include <sys/vmparam.h>
     54 #include <sys/modctl.h>
     55 #include <sys/vm.h>
     56 #include <sys/callb.h>
     57 #include <sys/ddi_timer.h>
     58 #include <sys/kmem.h>
     59 #include <sys/vmem.h>
     60 #include <sys/cpuvar.h>
     61 #include <sys/cladm.h>
     62 #include <sys/corectl.h>
     63 #include <sys/exec.h>
     64 #include <sys/syscall.h>
     65 #include <sys/reboot.h>
     66 #include <sys/task.h>
     67 #include <sys/exacct.h>
     68 #include <sys/autoconf.h>
     69 #include <sys/errorq.h>
     70 #include <sys/class.h>
     71 #include <sys/stack.h>
     72 #include <sys/brand.h>
     73 
     74 #include <vm/as.h>
     75 #include <vm/seg_kmem.h>
     76 #include <sys/dc_ki.h>
     77 
     78 #include <c2/audit.h>
     79 
     80 /* well known processes */
     81 proc_t *proc_sched;		/* memory scheduler */
     82 proc_t *proc_init;		/* init */
     83 proc_t *proc_pageout;		/* pageout daemon */
     84 proc_t *proc_fsflush;		/* fsflush daemon */
     85 
     86 pgcnt_t	maxmem;		/* Maximum available memory in pages.	*/
     87 pgcnt_t	freemem;	/* Current available memory in pages.	*/
     88 int	audit_active;
     89 int	interrupts_unleashed;	/* set when we do the first spl0() */
     90 
     91 kmem_cache_t *process_cache;	/* kmem cache for proc structures */
     92 
     93 /*
     94  * Process 0's lwp directory and lwpid hash table.
     95  */
     96 lwpdir_t p0_lwpdir[2];
     97 lwpdir_t *p0_tidhash[2];
     98 lwpent_t p0_lep;
     99 
    100 /*
    101  * Machine-independent initialization code
    102  * Called from cold start routine as
    103  * soon as a stack and segmentation
    104  * have been established.
    105  * Functions:
    106  *	clear and free user core
    107  *	turn on clock
    108  *	hand craft 0th process
    109  *	call all initialization routines
    110  *	fork	- process 0 to schedule
    111  *		- process 1 execute bootstrap
    112  *		- process 2 to page out
    113  *	create system threads
    114  */
    115 
    116 int cluster_bootflags = 0;
    117 
    118 void
    119 cluster_wrapper(void)
    120 {
    121 	cluster();
    122 	panic("cluster()  returned");
    123 }
    124 
    125 char initname[INITNAME_SZ] = "/sbin/init";	/* also referenced by zone0 */
    126 char initargs[BOOTARGS_MAX] = "";		/* also referenced by zone0 */
    127 extern int64_t lwp_sigmask(int, uint_t, uint_t);
    128 
    129 /*
    130  * Construct a stack for init containing the arguments to it, then
    131  * pass control to exec_common.
    132  */
    133 int
    134 exec_init(const char *initpath, const char *args)
    135 {
    136 	caddr32_t ucp;
    137 	caddr32_t *uap;
    138 	caddr32_t *argv;
    139 	caddr32_t exec_fnamep;
    140 	char *scratchargs;
    141 	int i, sarg;
    142 	size_t argvlen, alen;
    143 	boolean_t in_arg;
    144 	int argc = 0;
    145 	int error = 0, count = 0;
    146 	proc_t *p = ttoproc(curthread);
    147 	klwp_t *lwp = ttolwp(curthread);
    148 	int brand_action;
    149 
    150 	if (args == NULL)
    151 		args = "";
    152 
    153 	alen = strlen(initpath) + 1 + strlen(args) + 1;
    154 	scratchargs = kmem_alloc(alen, KM_SLEEP);
    155 	(void) snprintf(scratchargs, alen, "%s %s", initpath, args);
    156 
    157 	/*
    158 	 * We do a quick two state parse of the string to sort out how big
    159 	 * argc should be.
    160 	 */
    161 	in_arg = B_FALSE;
    162 	for (i = 0; i < strlen(scratchargs); i++) {
    163 		if (scratchargs[i] == ' ' || scratchargs[i] == '\0') {
    164 			if (in_arg) {
    165 				in_arg = B_FALSE;
    166 				argc++;
    167 			}
    168 		} else {
    169 			in_arg = B_TRUE;
    170 		}
    171 	}
    172 	argvlen = sizeof (caddr32_t) * (argc + 1);
    173 	argv = kmem_zalloc(argvlen, KM_SLEEP);
    174 
    175 	/*
    176 	 * We pull off a bit of a hack here.  We work our way through the
    177 	 * args string, putting nulls at the ends of space delimited tokens
    178 	 * (boot args don't support quoting at this time).  Then we just
    179 	 * copy the whole mess to userland in one go.  In other words, we
    180 	 * transform this: "init -s -r\0" into this on the stack:
    181 	 *
    182 	 *	-0x00 \0
    183 	 *	-0x01 r
    184 	 *	-0x02 -  <--------.
    185 	 *	-0x03 \0	  |
    186 	 *	-0x04 s		  |
    187 	 *	-0x05 -  <------. |
    188 	 *	-0x06 \0	| |
    189 	 *	-0x07 t		| |
    190 	 *	-0x08 i 	| |
    191 	 *	-0x09 n		| |
    192 	 *	-0x0a i  <---.  | |
    193 	 *	-0x10 NULL   |  | |	(argv[3])
    194 	 *	-0x14   -----|--|-'	(argv[2])
    195 	 *	-0x18  ------|--'	(argv[1])
    196 	 *	-0x1c -------'		(argv[0])
    197 	 *
    198 	 * Since we know the value of ucp at the beginning of this process,
    199 	 * we can trivially compute the argv[] array which we also need to
    200 	 * place in userland: argv[i] = ucp - sarg(i), where ucp is the
    201 	 * stack ptr, and sarg is the string index of the start of the
    202 	 * argument.
    203 	 */
    204 	ucp = (caddr32_t)(uintptr_t)p->p_usrstack;
    205 
    206 	argc = 0;
    207 	in_arg = B_FALSE;
    208 	sarg = 0;
    209 
    210 	for (i = 0; i < alen; i++) {
    211 		if (scratchargs[i] == ' ' || scratchargs[i] == '\0') {
    212 			if (in_arg == B_TRUE) {
    213 				in_arg = B_FALSE;
    214 				scratchargs[i] = '\0';
    215 				argv[argc++] = ucp - (alen - sarg);
    216 			}
    217 		} else if (in_arg == B_FALSE) {
    218 			in_arg = B_TRUE;
    219 			sarg = i;
    220 		}
    221 	}
    222 	ucp -= alen;
    223 	error |= copyout(scratchargs, (caddr_t)(uintptr_t)ucp, alen);
    224 
    225 	uap = (caddr32_t *)P2ALIGN((uintptr_t)ucp, sizeof (caddr32_t));
    226 	uap--;	/* advance to be below the word we're in */
    227 	uap -= (argc + 1);	/* advance argc words down, plus one for NULL */
    228 	error |= copyout(argv, uap, argvlen);
    229 
    230 	if (error != 0) {
    231 		zcmn_err(p->p_zone->zone_id, CE_WARN,
    232 		    "Could not construct stack for init.\n");
    233 		kmem_free(argv, argvlen);
    234 		kmem_free(scratchargs, alen);
    235 		return (EFAULT);
    236 	}
    237 
    238 	exec_fnamep = argv[0];
    239 	kmem_free(argv, argvlen);
    240 	kmem_free(scratchargs, alen);
    241 
    242 	/*
    243 	 * Point at the arguments.
    244 	 */
    245 	lwp->lwp_ap = lwp->lwp_arg;
    246 	lwp->lwp_arg[0] = (uintptr_t)exec_fnamep;
    247 	lwp->lwp_arg[1] = (uintptr_t)uap;
    248 	lwp->lwp_arg[2] = NULL;
    249 	curthread->t_post_sys = 1;
    250 	curthread->t_sysnum = SYS_execve;
    251 
    252 	/*
    253 	 * If we are executing init from zsched, we may have inherited its
    254 	 * parent process's signal mask.  Clear it now so that we behave in
    255 	 * the same way as when started from the global zone.
    256 	 */
    257 	(void) lwp_sigmask(SIG_UNBLOCK, 0xffffffff, 0xffffffff);
    258 
    259 	brand_action = ZONE_IS_BRANDED(p->p_zone) ? EBA_BRAND : EBA_NONE;
    260 again:
    261 	error = exec_common((const char *)(uintptr_t)exec_fnamep,
    262 	    (const char **)(uintptr_t)uap, NULL, brand_action);
    263 
    264 	/*
    265 	 * Normally we would just set lwp_argsaved and t_post_sys and
    266 	 * let post_syscall reset lwp_ap for us.  Unfortunately,
    267 	 * exec_init isn't always called from a system call.  Instead
    268 	 * of making a mess of trap_cleanup, we just reset the args
    269 	 * pointer here.
    270 	 */
    271 	reset_syscall_args();
    272 
    273 	switch (error) {
    274 	case 0:
    275 		return (0);
    276 
    277 	case ENOENT:
    278 		zcmn_err(p->p_zone->zone_id, CE_WARN,
    279 		    "exec(%s) failed (file not found).\n", initpath);
    280 		return (ENOENT);
    281 
    282 	case EAGAIN:
    283 	case EINTR:
    284 		++count;
    285 		if (count < 5) {
    286 			zcmn_err(p->p_zone->zone_id, CE_WARN,
    287 			    "exec(%s) failed with errno %d.  Retrying...\n",
    288 			    initpath, error);
    289 			goto again;
    290 		}
    291 	}
    292 
    293 	zcmn_err(p->p_zone->zone_id, CE_WARN,
    294 	    "exec(%s) failed with errno %d.", initpath, error);
    295 	return (error);
    296 }
    297 
    298 /*
    299  * This routine does all of the common setup for invoking init; global
    300  * and non-global zones employ this routine for the functionality which is
    301  * in common.
    302  *
    303  * This program (init, presumably) must be a 32-bit process.
    304  */
    305 int
    306 start_init_common()
    307 {
    308 	proc_t *p = curproc;
    309 	ASSERT_STACK_ALIGNED();
    310 	p->p_zone->zone_proc_initpid = p->p_pid;
    311 
    312 	p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
    313 	p->p_usrstack = (caddr_t)USRSTACK32;
    314 	p->p_model = DATAMODEL_ILP32;
    315 	p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
    316 	p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
    317 	p->p_stk_ctl = INT32_MAX;
    318 
    319 	p->p_as = as_alloc();
    320 	p->p_as->a_proc = p;
    321 	p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
    322 	(void) hat_setup(p->p_as->a_hat, HAT_INIT);
    323 
    324 	init_core();
    325 
    326 	init_mstate(curthread, LMS_SYSTEM);
    327 	return (exec_init(p->p_zone->zone_initname, p->p_zone->zone_bootargs));
    328 }
    329 
    330 /*
    331  * Start the initial user process for the global zone; once running, if
    332  * init should subsequently fail, it will be automatically be caught in the
    333  * exit(2) path, and restarted by restart_init().
    334  */
    335 static void
    336 start_init(void)
    337 {
    338 	proc_init = curproc;
    339 
    340 	ASSERT(curproc->p_zone->zone_initname != NULL);
    341 
    342 	if (start_init_common() != 0)
    343 		halt("unix: Could not start init");
    344 	lwp_rtt();
    345 }
    346 
    347 #if defined(__i386) || defined(__amd64)
    348 extern void return_instr(void);
    349 void (*rootnex_iommu_add_intr)(void) = (void (*)(void))return_instr;
    350 #endif
    351 
    352 void
    353 main(void)
    354 {
    355 	proc_t		*p = ttoproc(curthread);	/* &p0 */
    356 	int		(**initptr)();
    357 	extern void	sched();
    358 	extern void	fsflush();
    359 	extern int	(*init_tbl[])();
    360 	extern int	(*mp_init_tbl[])();
    361 	extern id_t	syscid, defaultcid;
    362 	extern int	swaploaded;
    363 	extern int	netboot;
    364 	extern void	vm_init(void);
    365 	extern void	cbe_init_pre(void);
    366 	extern void	cbe_init(void);
    367 	extern void	clock_tick_init_pre(void);
    368 	extern void	clock_tick_init_post(void);
    369 	extern void	clock_init(void);
    370 	extern void	physio_bufs_init(void);
    371 	extern void	pm_cfb_setup_intr(void);
    372 	extern int	pm_adjust_timestamps(dev_info_t *, void *);
    373 	extern void	start_other_cpus(int);
    374 	extern void	sysevent_evc_thrinit();
    375 	extern void	lgrp_main_init(void);
    376 	extern void	lgrp_main_mp_init(void);
    377 #if defined(__x86)
    378 	extern void	cpupm_post_startup(void);
    379 #endif
    380 	/*
    381 	 * In the horrible world of x86 in-lines, you can't get symbolic
    382 	 * structure offsets a la genassym.  This assertion is here so
    383 	 * that the next poor slob who innocently changes the offset of
    384 	 * cpu_thread doesn't waste as much time as I just did finding
    385 	 * out that it's hard-coded in i86/ml/i86.il.  Similarly for
    386 	 * curcpup.  You're welcome.
    387 	 */
    388 	ASSERT(CPU == CPU->cpu_self);
    389 	ASSERT(curthread == CPU->cpu_thread);
    390 	ASSERT_STACK_ALIGNED();
    391 
    392 	/*
    393 	 * Setup the first lgroup, and home t0
    394 	 */
    395 	lgrp_setup();
    396 
    397 	/*
    398 	 * Once 'startup()' completes, the thread_reaper() daemon would be
    399 	 * created(in thread_init()). After that, it is safe to create threads
    400 	 * that could exit. These exited threads will get reaped.
    401 	 */
    402 	startup();
    403 	segkmem_gc();
    404 	callb_init();
    405 	callout_init();	/* callout table MUST be init'd before clock starts */
    406 	cbe_init_pre();	/* x86 must initialize gethrtimef before timer_init */
    407 	timer_init();	/* timer must be initialized before cyclic starts */
    408 	cbe_init();
    409 	clock_tick_init_pre();
    410 	clock_init();
    411 
    412 	/*
    413 	 * On some platforms, clkinitf() changes the timing source that
    414 	 * gethrtime_unscaled() uses to generate timestamps.  cbe_init() calls
    415 	 * clkinitf(), so re-initialize the microstate counters after the
    416 	 * timesource has been chosen.
    417 	 */
    418 	init_mstate(&t0, LMS_SYSTEM);
    419 	init_cpu_mstate(CPU, CMS_SYSTEM);
    420 
    421 	/*
    422 	 * May need to probe to determine latencies from CPU 0 after
    423 	 * gethrtime() comes alive in cbe_init() and before enabling interrupts
    424 	 */
    425 	lgrp_plat_probe();
    426 
    427 	/*
    428 	 * Call all system initialization functions.
    429 	 */
    430 	for (initptr = &init_tbl[0]; *initptr; initptr++)
    431 		(**initptr)();
    432 
    433 	/*
    434 	 * initialize vm related stuff.
    435 	 */
    436 	vm_init();
    437 
    438 	/*
    439 	 * initialize buffer pool for raw I/O requests
    440 	 */
    441 	physio_bufs_init();
    442 
    443 	ttolwp(curthread)->lwp_error = 0; /* XXX kludge for SCSI driver */
    444 
    445 	/*
    446 	 * Drop the interrupt level and allow interrupts.  At this point
    447 	 * the DDI guarantees that interrupts are enabled.
    448 	 */
    449 	(void) spl0();
    450 	interrupts_unleashed = 1;
    451 
    452 #if defined(__i386) || defined(__amd64)
    453 	/*
    454 	 * add intel iommu fault event handler
    455 	 */
    456 	rootnex_iommu_add_intr();
    457 #endif
    458 
    459 	vfs_mountroot();	/* Mount the root file system */
    460 	errorq_init();		/* after vfs_mountroot() so DDI root is ready */
    461 	cpu_kstat_init(CPU);	/* after vfs_mountroot() so TOD is valid */
    462 	ddi_walk_devs(ddi_root_node(), pm_adjust_timestamps, NULL);
    463 				/* after vfs_mountroot() so hrestime is valid */
    464 
    465 	post_startup();
    466 	swaploaded = 1;
    467 
    468 	/*
    469 	 * Initialize Solaris Audit Subsystem
    470 	 */
    471 	audit_init();
    472 
    473 	/*
    474 	 * Plumb the protocol modules and drivers only if we are not
    475 	 * networked booted, in this case we already did it in rootconf().
    476 	 */
    477 	if (netboot == 0)
    478 		(void) strplumb();
    479 
    480 	gethrestime(&PTOU(curproc)->u_start);
    481 	curthread->t_start = PTOU(curproc)->u_start.tv_sec;
    482 	p->p_mstart = gethrtime();
    483 
    484 	/*
    485 	 * Perform setup functions that can only be done after root
    486 	 * and swap have been set up.
    487 	 */
    488 	consconfig();
    489 	release_bootstrap();
    490 
    491 	/*
    492 	 * attach drivers with ddi-forceattach prop
    493 	 * This must be done after consconfig() to prevent usb key/mouse
    494 	 * from attaching before the upper console stream is plumbed.
    495 	 * It must be done early enough to load hotplug drivers (e.g.
    496 	 * pcmcia nexus) so that devices enumerated via hotplug is
    497 	 * available before I/O subsystem is fully initialized.
    498 	 */
    499 	i_ddi_forceattach_drivers();
    500 
    501 	/*
    502 	 * Set the scan rate and other parameters of the paging subsystem.
    503 	 */
    504 	setupclock(0);
    505 
    506 	/*
    507 	 * Create kmem cache for proc structures
    508 	 */
    509 	process_cache = kmem_cache_create("process_cache", sizeof (proc_t),
    510 	    0, NULL, NULL, NULL, NULL, NULL, 0);
    511 
    512 	/*
    513 	 * Initialize process 0's lwp directory and lwpid hash table.
    514 	 */
    515 	p->p_lwpdir = p->p_lwpfree = p0_lwpdir;
    516 	p->p_lwpdir->ld_next = p->p_lwpdir + 1;
    517 	p->p_lwpdir_sz = 2;
    518 	p->p_tidhash = p0_tidhash;
    519 	p->p_tidhash_sz = 2;
    520 	p0_lep.le_thread = curthread;
    521 	p0_lep.le_lwpid = curthread->t_tid;
    522 	p0_lep.le_start = curthread->t_start;
    523 	lwp_hash_in(p, &p0_lep);
    524 
    525 	/*
    526 	 * Initialize extended accounting.
    527 	 */
    528 	exacct_init();
    529 
    530 	/*
    531 	 * Initialize threads of sysevent event channels
    532 	 */
    533 	sysevent_evc_thrinit();
    534 
    535 	/*
    536 	 * main lgroup initialization
    537 	 * This must be done after post_startup(), but before
    538 	 * start_other_cpus()
    539 	 */
    540 	lgrp_main_init();
    541 
    542 	/*
    543 	 * Perform MP initialization, if any.
    544 	 */
    545 	start_other_cpus(0);
    546 
    547 	/*
    548 	 * Finish lgrp initialization after all CPUS are brought online.
    549 	 */
    550 	lgrp_main_mp_init();
    551 
    552 	/*
    553 	 * After mp_init(), number of cpus are known (this is
    554 	 * true for the time being, when there are actually
    555 	 * hot pluggable cpus then this scheme  would not do).
    556 	 * Any per cpu initialization is done here.
    557 	 */
    558 	kmem_mp_init();
    559 	vmem_update(NULL);
    560 
    561 	clock_tick_init_post();
    562 
    563 	for (initptr = &mp_init_tbl[0]; *initptr; initptr++)
    564 		(**initptr)();
    565 
    566 	/*
    567 	 * These must be called after start_other_cpus
    568 	 */
    569 	pm_cfb_setup_intr();
    570 #if defined(__x86)
    571 	cpupm_post_startup();
    572 #endif
    573 
    574 	/*
    575 	 * Make init process; enter scheduling loop with system process.
    576 	 */
    577 
    578 	/* create init process */
    579 	if (newproc(start_init, NULL, defaultcid, 59, NULL))
    580 		panic("main: unable to fork init.");
    581 
    582 	/* create pageout daemon */
    583 	if (newproc(pageout, NULL, syscid, maxclsyspri - 1, NULL))
    584 		panic("main: unable to fork pageout()");
    585 
    586 	/* create fsflush daemon */
    587 	if (newproc(fsflush, NULL, syscid, minclsyspri, NULL))
    588 		panic("main: unable to fork fsflush()");
    589 
    590 	/* create cluster process if we're a member of one */
    591 	if (cluster_bootflags & CLUSTER_BOOTED) {
    592 		if (newproc(cluster_wrapper, NULL, syscid, minclsyspri, NULL))
    593 			panic("main: unable to fork cluster()");
    594 	}
    595 
    596 	/*
    597 	 * Create system threads (threads are associated with p0)
    598 	 */
    599 
    600 	/* create module uninstall daemon */
    601 	/* BugID 1132273. If swapping over NFS need a bigger stack */
    602 	(void) thread_create(NULL, 0, (void (*)())mod_uninstall_daemon,
    603 	    NULL, 0, &p0, TS_RUN, minclsyspri);
    604 
    605 	(void) thread_create(NULL, 0, seg_pasync_thread,
    606 	    NULL, 0, &p0, TS_RUN, minclsyspri);
    607 
    608 	pid_setmin();
    609 
    610 	bcopy("sched", PTOU(curproc)->u_psargs, 6);
    611 	bcopy("sched", PTOU(curproc)->u_comm, 5);
    612 	sched();
    613 	/* NOTREACHED */
    614 }
    615