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