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