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