uts: make emu10k non-verbose
[unleashed.git] / kernel / os / main.c
blob35942c35f1c574d68b9c7bd4edf66c7b97e633d5
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
26 /* Copyright (c) 1988 AT&T */
27 /* All Rights Reserved */
30 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
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/disp.h>
49 #include <sys/callo.h>
50 #include <sys/callb.h>
51 #include <sys/debug.h>
52 #include <sys/conf.h>
53 #include <sys/bootconf.h>
54 #include <sys/utsname.h>
55 #include <sys/cmn_err.h>
56 #include <sys/vmparam.h>
57 #include <sys/modctl.h>
58 #include <sys/vm.h>
59 #include <sys/callb.h>
60 #include <sys/ddi_periodic.h>
61 #include <sys/sunddi.h>
62 #include <sys/kmem.h>
63 #include <sys/vmem.h>
64 #include <sys/cpuvar.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 #include <sys/brand.h>
76 #include <sys/mmapobj.h>
78 #include <vm/as.h>
79 #include <vm/seg_kmem.h>
81 #include <c2/audit.h>
82 #include <sys/bootprops.h>
84 /* well known processes */
85 proc_t *proc_sched; /* memory scheduler */
86 proc_t *proc_init; /* init */
87 proc_t *proc_pageout; /* pageout daemon */
88 proc_t *proc_fsflush; /* fsflush daemon */
90 pgcnt_t maxmem; /* Maximum available memory in pages. */
91 pgcnt_t freemem; /* Current available memory in pages. */
92 int interrupts_unleashed; /* set when we do the first spl0() */
94 kmem_cache_t *process_cache; /* kmem cache for proc structures */
97 * Indicates whether the auditing module (c2audit) is loaded. Possible
98 * values are:
99 * 0 - c2audit module is excluded in /etc/system and cannot be loaded
100 * 1 - c2audit module is not loaded but can be anytime
101 * 2 - c2audit module is loaded
103 int audit_active = C2AUDIT_DISABLED;
106 * Process 0's lwp directory and lwpid hash table.
108 lwpdir_t p0_lwpdir[2];
109 tidhash_t p0_tidhash[2];
110 lwpent_t p0_lep;
113 * Machine-independent initialization code
114 * Called from cold start routine as
115 * soon as a stack and segmentation
116 * have been established.
117 * Functions:
118 * clear and free user core
119 * turn on clock
120 * hand craft 0th process
121 * call all initialization routines
122 * fork - process 0 to schedule
123 * - process 1 execute bootstrap
124 * - process 2 to page out
125 * create system threads
128 char initname[INITNAME_SZ] = "/sbin/init"; /* also referenced by zone0 */
129 char initargs[BOOTARGS_MAX] = ""; /* also referenced by zone0 */
132 * Construct a stack for init containing the arguments to it, then
133 * pass control to exec_common.
136 exec_init(const char *initpath, const char *args)
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;
152 if (args == NULL)
153 args = "";
155 alen = strlen(initpath) + 1 + strlen(args) + 1;
156 scratchargs = kmem_alloc(alen, KM_SLEEP);
157 (void) snprintf(scratchargs, alen, "%s %s", initpath, args);
160 * We do a quick two state parse of the string to sort out how big
161 * argc should be.
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++;
170 } else {
171 in_arg = B_TRUE;
174 argvlen = sizeof (caddr32_t) * (argc + 1);
175 argv = kmem_zalloc(argvlen, KM_SLEEP);
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:
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])
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.
206 ucp = (caddr32_t)(uintptr_t)p->p_usrstack;
208 argc = 0;
209 in_arg = B_FALSE;
210 sarg = 0;
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);
219 } else if (in_arg == B_FALSE) {
220 in_arg = B_TRUE;
221 sarg = i;
224 ucp -= alen;
225 error |= copyout(scratchargs, (caddr_t)(uintptr_t)ucp, alen);
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);
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);
240 exec_fnamep = argv[0];
241 kmem_free(argv, argvlen);
242 kmem_free(scratchargs, alen);
245 * Point at the arguments.
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] = (uintptr_t)NULL;
251 curthread->t_post_sys = 1;
252 curthread->t_sysnum = SYS_execve;
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.
259 sigemptyset(&curthread->t_hold);
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);
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.
273 reset_syscall_args();
275 switch (error) {
276 case 0:
277 return (0);
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);
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;
295 zcmn_err(p->p_zone->zone_id, CE_WARN,
296 "exec(%s) failed with errno %d.", initpath, error);
297 return (error);
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.
305 * This program (init, presumably) must be a 32-bit process.
308 start_init_common()
310 proc_t *p = curproc;
311 ASSERT_STACK_ALIGNED();
312 p->p_zone->zone_proc_initpid = p->p_pid;
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;
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);
326 init_core();
328 init_mstate(curthread, LMS_SYSTEM);
329 return (exec_init(p->p_zone->zone_initname, p->p_zone->zone_bootargs));
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().
337 static void
338 start_init(void)
340 proc_init = curproc;
342 ASSERT(curproc->p_zone->zone_initname != NULL);
344 if (start_init_common() != 0)
345 halt("unix: Could not start init");
346 lwp_rtt();
349 void
350 main(void)
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 extern kmutex_t ualock;
374 #if defined(__x86)
375 extern void fastboot_post_startup(void);
376 extern void progressbar_start(void);
377 #endif
379 * In the horrible world of x86 in-lines, you can't get symbolic
380 * structure offsets a la genassym. This assertion is here so
381 * that the next poor slob who innocently changes the offset of
382 * cpu_thread doesn't waste as much time as I just did finding
383 * out that it's hard-coded in i86/ml/i86.il. Similarly for
384 * curcpup. You're welcome.
386 ASSERT(CPU == CPU->cpu_self);
387 ASSERT(curthread == CPU->cpu_thread);
388 ASSERT_STACK_ALIGNED();
391 * We take the ualock until we have completed the startup
392 * to prevent kadmin() from disrupting this work. In particular,
393 * we don't want kadmin() to bring the system down while we are
394 * trying to start it up.
396 mutex_enter(&ualock);
399 * Setup root lgroup and leaf lgroup for CPU 0
401 lgrp_init(LGRP_INIT_STAGE2);
403 pagecache_init(&kvps[KV_KVP]);
404 pagecache_init(&kvps[KV_ZVP]);
405 #if defined(__sparc)
406 pagecache_init(&kvps[KV_MPVP]);
407 pagecache_init(&kvps[KV_PROMVP]);
408 #endif
411 * Once 'startup()' completes, the thread_reaper() daemon would be
412 * created(in thread_init()). After that, it is safe to create threads
413 * that could exit. These exited threads will get reaped.
415 startup();
416 segkmem_gc();
417 callb_init();
418 cbe_init_pre(); /* x86 must initialize gethrtimef before timer_init */
419 ddi_periodic_init();
420 cbe_init();
421 callout_init(); /* callout table MUST be init'd after cyclics */
422 clock_tick_init_pre();
423 clock_init();
425 #if defined(__x86)
427 * The progressbar thread uses cv_reltimedwait() and hence needs to be
428 * started after the callout mechanism has been initialized.
430 progressbar_start();
431 #endif
433 * On some platforms, clkinitf() changes the timing source that
434 * gethrtime_unscaled() uses to generate timestamps. cbe_init() calls
435 * clkinitf(), so re-initialize the microstate counters after the
436 * timesource has been chosen.
438 init_mstate(&t0, LMS_SYSTEM);
439 init_cpu_mstate(CPU, CMS_SYSTEM);
442 * May need to probe to determine latencies from CPU 0 after
443 * gethrtime() comes alive in cbe_init() and before enabling interrupts
444 * and copy and release any temporary memory allocated with BOP_ALLOC()
445 * before release_bootstrap() frees boot memory
447 lgrp_init(LGRP_INIT_STAGE3);
450 * Call all system initialization functions.
452 for (initptr = &init_tbl[0]; *initptr; initptr++)
453 (**initptr)();
455 * Load iSCSI boot properties
457 ld_ib_prop();
459 * initialize vm related stuff.
461 vm_init();
464 * initialize buffer pool for raw I/O requests
466 physio_bufs_init();
468 ttolwp(curthread)->lwp_error = 0; /* XXX kludge for SCSI driver */
471 * Drop the interrupt level and allow interrupts. At this point
472 * the DDI guarantees that interrupts are enabled.
474 (void) spl0();
475 interrupts_unleashed = 1;
478 * Create kmem cache for proc structures
480 process_cache = kmem_cache_create("process_cache", sizeof (proc_t),
481 0, NULL, NULL, NULL, NULL, NULL, 0);
483 vfs_mountroot(); /* Mount the root file system */
484 errorq_init(); /* after vfs_mountroot() so DDI root is ready */
485 cpu_kstat_init(CPU); /* after vfs_mountroot() so TOD is valid */
486 ddi_walk_devs(ddi_root_node(), pm_adjust_timestamps, NULL);
487 /* after vfs_mountroot() so hrestime is valid */
489 post_startup();
490 swaploaded = 1;
493 * Initialize Solaris Audit Subsystem
495 audit_init();
498 * Plumb the protocol modules and drivers only if we are not
499 * networked booted, in this case we already did it in rootconf().
501 if (netboot == 0 && iscsiboot_prop == NULL)
502 (void) strplumb();
504 gethrestime(&PTOU(curproc)->u_start);
505 curthread->t_start = PTOU(curproc)->u_start.tv_sec;
506 p->p_mstart = gethrtime();
509 * Perform setup functions that can only be done after root
510 * and swap have been set up.
512 consconfig();
513 #ifndef __sparc
514 release_bootstrap();
515 #endif
518 * attach drivers with ddi-forceattach prop
519 * It must be done early enough to load hotplug drivers (e.g.
520 * pcmcia nexus) so that devices enumerated via hotplug is
521 * available before I/O subsystem is fully initialized.
523 i_ddi_forceattach_drivers();
526 * Set the scan rate and other parameters of the paging subsystem.
528 setupclock(0);
531 * Initialize process 0's lwp directory and lwpid hash table.
533 p->p_lwpdir = p->p_lwpfree = p0_lwpdir;
534 p->p_lwpdir->ld_next = p->p_lwpdir + 1;
535 p->p_lwpdir_sz = 2;
536 p->p_tidhash = p0_tidhash;
537 p->p_tidhash_sz = 2;
538 p0_lep.le_thread = curthread;
539 p0_lep.le_lwpid = curthread->t_tid;
540 p0_lep.le_start = curthread->t_start;
541 lwp_hash_in(p, &p0_lep, p0_tidhash, 2, 0);
544 * Initialize extended accounting.
546 exacct_init();
549 * Initialize threads of sysevent event channels
551 sysevent_evc_thrinit();
554 * This must be done after post_startup() but before
555 * start_other_cpus()
557 lgrp_init(LGRP_INIT_STAGE4);
560 * Perform MP initialization, if any.
562 start_other_cpus(0);
564 #ifdef __sparc
566 * Release bootstrap here since PROM interfaces are
567 * used to start other CPUs above.
569 release_bootstrap();
570 #endif
573 * Finish lgrp initialization after all CPUS are brought online.
575 lgrp_init(LGRP_INIT_STAGE5);
578 * After mp_init(), number of cpus are known (this is
579 * true for the time being, when there are actually
580 * hot pluggable cpus then this scheme would not do).
581 * Any per cpu initialization is done here.
583 kmem_mp_init();
584 vmem_update(NULL);
586 clock_tick_init_post();
588 for (initptr = &mp_init_tbl[0]; *initptr; initptr++)
589 (**initptr)();
592 * These must be called after start_other_cpus
594 pm_cfb_setup_intr();
595 #if defined(__x86)
596 fastboot_post_startup();
597 #endif
600 * Make init process; enter scheduling loop with system process.
602 * Note that we manually assign the pids for these processes, for
603 * historical reasons. If more pre-assigned pids are needed,
604 * FAMOUS_PIDS will have to be updated.
607 /* create init process */
608 if (newproc(start_init, NULL, defaultcid, 59, NULL,
609 FAMOUS_PID_INIT))
610 panic("main: unable to fork init.");
612 /* create pageout daemon */
613 if (newproc(pageout, NULL, syscid, maxclsyspri - 1, NULL,
614 FAMOUS_PID_PAGEOUT))
615 panic("main: unable to fork pageout()");
617 /* create fsflush daemon */
618 if (newproc(fsflush, NULL, syscid, minclsyspri, NULL,
619 FAMOUS_PID_FSFLUSH))
620 panic("main: unable to fork fsflush()");
623 * Create system threads (threads are associated with p0)
626 /* create module uninstall daemon */
627 /* BugID 1132273. If swapping over NFS need a bigger stack */
628 (void) thread_create(NULL, 0, (void (*)())mod_uninstall_daemon,
629 NULL, 0, &p0, TS_RUN, minclsyspri);
631 (void) thread_create(NULL, 0, seg_pasync_thread,
632 NULL, 0, &p0, TS_RUN, minclsyspri);
634 pid_setmin();
636 /* system is now ready */
637 mutex_exit(&ualock);
639 bcopy("sched", PTOU(curproc)->u_psargs, 6);
640 bcopy("sched", PTOU(curproc)->u_comm, 5);
641 sched();
642 /* NOTREACHED */