xref: /titanic_52/usr/src/uts/common/os/main.c (revision 1cb6af97c6f66f456d4f726ef056e1ebc0f73305)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.31 */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/sysmacros.h>
36 #include <sys/pcb.h>
37 #include <sys/systm.h>
38 #include <sys/signal.h>
39 #include <sys/cred.h>
40 #include <sys/user.h>
41 #include <sys/vfs.h>
42 #include <sys/vnode.h>
43 #include <sys/proc.h>
44 #include <sys/time.h>
45 #include <sys/file.h>
46 #include <sys/priocntl.h>
47 #include <sys/procset.h>
48 #include <sys/var.h>
49 #include <sys/disp.h>
50 #include <sys/callo.h>
51 #include <sys/callb.h>
52 #include <sys/debug.h>
53 #include <sys/conf.h>
54 #include <sys/bootconf.h>
55 #include <sys/utsname.h>
56 #include <sys/cmn_err.h>
57 #include <sys/vmparam.h>
58 #include <sys/modctl.h>
59 #include <sys/vm.h>
60 #include <sys/callb.h>
61 #include <sys/kmem.h>
62 #include <sys/vmem.h>
63 #include <sys/cpuvar.h>
64 #include <sys/cladm.h>
65 #include <sys/corectl.h>
66 #include <sys/exec.h>
67 #include <sys/syscall.h>
68 #include <sys/reboot.h>
69 #include <sys/task.h>
70 #include <sys/exacct.h>
71 #include <sys/autoconf.h>
72 #include <sys/errorq.h>
73 #include <sys/class.h>
74 #include <sys/stack.h>
75 
76 #include <vm/as.h>
77 #include <vm/seg_kmem.h>
78 #include <sys/dc_ki.h>
79 
80 #include <c2/audit.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 lwpdir_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";
128 char initargs[INITARGS_SZ] = "";
129 
130 /*
131  * Start the initial user process.
132  * The program [initname] may be invoked with one argument
133  * containing the boot flags.
134  *
135  * It must be a 32-bit program.
136  */
137 void
138 icode(void)
139 {
140 	proc_t *p = ttoproc(curthread);
141 
142 	ASSERT_STACK_ALIGNED();
143 
144 	/*
145 	 * Allocate user address space and stack segment
146 	 */
147 	proc_init = p;
148 	zone0.zone_proc_initpid = proc_init->p_pid;
149 
150 	p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
151 	p->p_usrstack = (caddr_t)USRSTACK32;
152 	p->p_model = DATAMODEL_ILP32;
153 	p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
154 	p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
155 	p->p_stk_ctl = INT32_MAX;
156 
157 	p->p_as = as_alloc();
158 	p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
159 	(void) hat_setup(p->p_as->a_hat, HAT_INIT);
160 	init_core();
161 
162 	init_mstate(curthread, LMS_SYSTEM);
163 
164 	if (exec_init(initname, 1, initargs[0] == '\0' ? NULL : initargs) != 0)
165 		halt("Could not start init");
166 
167 	lwp_rtt();
168 }
169 
170 int
171 exec_init(const char *initpath, int useboothowto, const char *args)
172 {
173 	char *ucp;
174 	caddr32_t *uap;
175 	char *argv[4];				/* backwards */
176 	int argc = 0;
177 	int error = 0, len, count = 0, i;
178 	proc_t *p = ttoproc(curthread);
179 	klwp_t *lwp = ttolwp(curthread);
180 
181 	/*
182 	 * Construct the exec arguments in userland.  That is, make an array
183 	 * of pointers to the argument strings, just like for execv().  This
184 	 * is done backwards.
185 	 */
186 	ucp = p->p_usrstack;
187 
188 	argv[0] = NULL;				/* argv terminator */
189 
190 	if (args != NULL) {
191 		len = strlen(args) + 1;
192 		ucp -= len;
193 		error |= copyoutstr(args, ucp, len, NULL);
194 		argv[++argc] = ucp;
195 	}
196 
197 	if (useboothowto &&
198 	    boothowto & (RB_SINGLE|RB_RECONFIG|RB_VERBOSE)) {
199 		error |= subyte(--ucp, '\0');		/* trailing null byte */
200 
201 		if (boothowto & RB_SINGLE)
202 			error |= subyte(--ucp, 's');
203 		if (boothowto & RB_RECONFIG)
204 			error |= subyte(--ucp, 'r');
205 		if (boothowto & RB_VERBOSE)
206 			error |= subyte(--ucp, 'v');
207 		error |= subyte(--ucp, '-');	/* leading hyphen */
208 
209 		argv[++argc] = ucp;
210 	}
211 
212 	len = strlen(initpath) + 1;
213 	ucp -= len;
214 	error |= copyoutstr(initpath, ucp, len, NULL);
215 	argv[++argc] = ucp;
216 
217 	/*
218 	 * Move out the arg pointers.
219 	 */
220 	uap = (caddr32_t *)P2ALIGN((uintptr_t)ucp, sizeof (caddr32_t));
221 	for (i = 0; i < argc + 1; ++i)
222 		error |= suword32(--uap, (uint32_t)(uintptr_t)argv[i]);
223 
224 	if (error != 0) {
225 		zcmn_err(p->p_zone->zone_id, CE_WARN,
226 		    "Could not construct stack for init.\n");
227 		return (EFAULT);
228 	}
229 
230 	/*
231 	 * Point at the arguments.
232 	 */
233 	lwp->lwp_ap = lwp->lwp_arg;
234 	lwp->lwp_arg[0] = (uintptr_t)argv[argc];
235 	lwp->lwp_arg[1] = (uintptr_t)uap;
236 	lwp->lwp_arg[2] = NULL;
237 	curthread->t_post_sys = 1;
238 	curthread->t_sysnum = SYS_execve;
239 
240 again:
241 	error = exec_common((const char *)argv[argc], (const char **)uap, NULL);
242 
243 	/*
244 	 * Normally we would just set lwp_argsaved and t_post_sys and
245 	 * let post_syscall reset lwp_ap for us.  Unfortunately,
246 	 * exec_init isn't always called from a system call.  Instead
247 	 * of making a mess of trap_cleanup, we just reset the args
248 	 * pointer here.
249 	 */
250 	reset_syscall_args();
251 
252 	switch (error) {
253 	case 0:
254 		return (0);
255 
256 	case ENOENT:
257 		zcmn_err(p->p_zone->zone_id, CE_WARN,
258 		    "exec(%s) failed (file not found).\n", initpath);
259 		return (ENOENT);
260 
261 	case EAGAIN:
262 	case EINTR:
263 		++count;
264 		if (count < 5) {
265 			zcmn_err(p->p_zone->zone_id, CE_WARN,
266 			    "exec(%s) failed with errno %d.  Retrying...\n",
267 			    initpath, error);
268 			goto again;
269 		}
270 	}
271 
272 	zcmn_err(p->p_zone->zone_id, CE_WARN,
273 	    "exec(%s) failed with errno %d.", initpath, error);
274 	return (error);
275 }
276 
277 void
278 main(void)
279 {
280 	proc_t		*p = ttoproc(curthread);	/* &p0 */
281 	int		(**initptr)();
282 	extern void	sched();
283 	extern void	fsflush();
284 	extern void	thread_reaper();
285 	extern int	(*init_tbl[])();
286 	extern int	(*mp_init_tbl[])();
287 	extern id_t	syscid, defaultcid;
288 	extern int	swaploaded;
289 	extern int	netboot;
290 	extern void	vm_init(void);
291 	extern void	cbe_init(void);
292 	extern void	clock_init(void);
293 	extern void	physio_bufs_init(void);
294 	extern void	pm_cfb_setup_intr(void);
295 	extern int	pm_adjust_timestamps(dev_info_t *, void *);
296 	extern void	start_other_cpus(int);
297 	extern void	sysevent_evc_thrinit();
298 	extern void	lgrp_main_init(void);
299 	extern void	lgrp_main_mp_init(void);
300 
301 	/*
302 	 * In the horrible world of x86 in-lines, you can't get symbolic
303 	 * structure offsets a la genassym.  This assertion is here so
304 	 * that the next poor slob who innocently changes the offset of
305 	 * cpu_thread doesn't waste as much time as I just did finding
306 	 * out that it's hard-coded in i86/ml/i86.il.  Similarly for
307 	 * curcpup.  You're welcome.
308 	 */
309 	ASSERT(CPU == CPU->cpu_self);
310 	ASSERT(curthread == CPU->cpu_thread);
311 	ASSERT_STACK_ALIGNED();
312 
313 	/*
314 	 * Setup the first lgroup, and home t0
315 	 */
316 	lgrp_setup();
317 
318 	startup();
319 	segkmem_gc();
320 	callb_init();
321 	callout_init();	/* callout table MUST be init'd before clock starts */
322 	cbe_init();
323 	clock_init();
324 
325 	/*
326 	 * May need to probe to determine latencies from CPU 0 after
327 	 * gethrtime() comes alive in cbe_init() and before enabling interrupts
328 	 */
329 	lgrp_plat_probe();
330 
331 	/*
332 	 * Call all system initialization functions.
333 	 */
334 	for (initptr = &init_tbl[0]; *initptr; initptr++)
335 		(**initptr)();
336 
337 	/*
338 	 * initialize vm related stuff.
339 	 */
340 	vm_init();
341 
342 	/*
343 	 * initialize buffer pool for raw I/O requests
344 	 */
345 	physio_bufs_init();
346 
347 	ttolwp(curthread)->lwp_error = 0; /* XXX kludge for SCSI driver */
348 
349 	/*
350 	 * Drop the interrupt level and allow interrupts.  At this point
351 	 * the DDI guarantees that interrupts are enabled.
352 	 */
353 	(void) spl0();
354 	interrupts_unleashed = 1;
355 
356 	vfs_mountroot();	/* Mount the root file system */
357 	errorq_init();		/* after vfs_mountroot() so DDI root is ready */
358 	cpu_kstat_init(CPU);	/* after vfs_mountroot() so TOD is valid */
359 	ddi_walk_devs(ddi_root_node(), pm_adjust_timestamps, NULL);
360 				/* after vfs_mountroot() so hrestime is valid */
361 
362 	post_startup();
363 	swaploaded = 1;
364 
365 	/*
366 	 * Initial C2 audit system
367 	 */
368 #ifdef C2_AUDIT
369 	audit_init();	/* C2 hook */
370 #endif
371 
372 	/*
373 	 * Plumb the protocol modules and drivers only if we are not
374 	 * networked booted, in this case we already did it in rootconf().
375 	 */
376 	if (netboot == 0)
377 		(void) strplumb();
378 
379 	gethrestime(&u.u_start);
380 	curthread->t_start = u.u_start.tv_sec;
381 	p->p_mstart = gethrtime();
382 
383 	/*
384 	 * Perform setup functions that can only be done after root
385 	 * and swap have been set up.
386 	 */
387 	consconfig();
388 #if defined(__i386) || defined(__amd64)
389 	release_bootstrap();
390 #endif
391 	/*
392 	 * attach drivers with ddi-forceattach prop
393 	 * This must be done after consconfig() to prevent usb key/mouse
394 	 * from attaching before the upper console stream is plumbed.
395 	 * It must be done early enough to load hotplug drivers (e.g.
396 	 * pcmcia nexus) so that devices enumerated via hotplug is
397 	 * available before I/O subsystem is fully initialized.
398 	 */
399 	i_ddi_forceattach_drivers();
400 
401 	/*
402 	 * Set the scan rate and other parameters of the paging subsystem.
403 	 */
404 	setupclock(0);
405 
406 	/*
407 	 * Create kmem cache for proc structures
408 	 */
409 	process_cache = kmem_cache_create("process_cache", sizeof (proc_t),
410 	    0, NULL, NULL, NULL, NULL, NULL, 0);
411 
412 	/*
413 	 * Initialize process 0's lwp directory and lwpid hash table.
414 	 */
415 	p->p_lwpdir = p->p_lwpfree = p0_lwpdir;
416 	p->p_lwpdir->ld_next = p->p_lwpdir + 1;
417 	p->p_lwpdir_sz = 2;
418 	p->p_tidhash = p0_tidhash;
419 	p->p_tidhash_sz = 2;
420 	p0_lep.le_thread = curthread;
421 	p0_lep.le_lwpid = curthread->t_tid;
422 	p0_lep.le_start = curthread->t_start;
423 	lwp_hash_in(p, &p0_lep);
424 
425 	/*
426 	 * Initialize extended accounting.
427 	 */
428 	exacct_init();
429 
430 	/*
431 	 * Initialize threads of sysevent event channels
432 	 */
433 	sysevent_evc_thrinit();
434 
435 	/*
436 	 * main lgroup initialization
437 	 * This must be done after post_startup(), but before
438 	 * start_other_cpus()
439 	 */
440 	lgrp_main_init();
441 
442 	/*
443 	 * Perform MP initialization, if any.
444 	 */
445 	start_other_cpus(0);
446 
447 	/*
448 	 * Finish lgrp initialization after all CPUS are brought online.
449 	 */
450 	lgrp_main_mp_init();
451 
452 	/*
453 	 * After mp_init(), number of cpus are known (this is
454 	 * true for the time being, when there are actually
455 	 * hot pluggable cpus then this scheme  would not do).
456 	 * Any per cpu initialization is done here.
457 	 */
458 	kmem_mp_init();
459 	vmem_update(NULL);
460 
461 	for (initptr = &mp_init_tbl[0]; *initptr; initptr++)
462 		(**initptr)();
463 
464 	/*
465 	 * This must be called after start_other_cpus
466 	 */
467 	pm_cfb_setup_intr();
468 
469 	/*
470 	 * Make init process; enter scheduling loop with system process.
471 	 */
472 
473 	/* create init process */
474 	if (newproc(icode, NULL, defaultcid, 59, NULL))
475 		panic("main: unable to fork init.");
476 
477 	/* create pageout daemon */
478 	if (newproc(pageout, NULL, syscid, maxclsyspri - 1, NULL))
479 		panic("main: unable to fork pageout()");
480 
481 	/* create fsflush daemon */
482 	if (newproc(fsflush, NULL, syscid, minclsyspri, NULL))
483 		panic("main: unable to fork fsflush()");
484 
485 	/* create cluster process if we're a member of one */
486 	if (cluster_bootflags & CLUSTER_BOOTED) {
487 		if (newproc(cluster_wrapper, NULL, syscid, minclsyspri, NULL))
488 			panic("main: unable to fork cluster()");
489 	}
490 
491 	/*
492 	 * Create system threads (threads are associated with p0)
493 	 */
494 
495 	/* create thread_reaper daemon */
496 	(void) thread_create(NULL, 0, (void (*)())thread_reaper,
497 	    NULL, 0, &p0, TS_RUN, minclsyspri);
498 
499 	/* create module uninstall daemon */
500 	/* BugID 1132273. If swapping over NFS need a bigger stack */
501 	(void) thread_create(NULL, 0, (void (*)())mod_uninstall_daemon,
502 	    NULL, 0, &p0, TS_RUN, minclsyspri);
503 
504 	(void) thread_create(NULL, 0, seg_pasync_thread,
505 	    NULL, 0, &p0, TS_RUN, minclsyspri);
506 
507 	pid_setmin();
508 
509 	bcopy("sched", u.u_psargs, 6);
510 	bcopy("sched", u.u_comm, 5);
511 	sched();
512 	/* NOTREACHED */
513 }
514