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]
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>
37 #include <sys/systm.h>
38 #include <sys/signal.h>
42 #include <sys/vnode.h>
46 #include <sys/priocntl.h>
47 #include <sys/procset.h>
49 #include <sys/callo.h>
50 #include <sys/callb.h>
51 #include <sys/debug.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>
59 #include <sys/callb.h>
60 #include <sys/ddi_periodic.h>
61 #include <sys/sunddi.h>
64 #include <sys/cpuvar.h>
65 #include <sys/corectl.h>
67 #include <sys/syscall.h>
68 #include <sys/reboot.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>
78 #include <vm/seg_kmem.h>
81 #include <sys/bootprops.h>
83 /* well known processes */
84 proc_t
*proc_sched
; /* memory scheduler */
85 proc_t
*proc_init
; /* init */
86 proc_t
*proc_pageout
; /* pageout daemon */
87 proc_t
*proc_fsflush
; /* fsflush daemon */
89 pgcnt_t maxmem
; /* Maximum available memory in pages. */
90 pgcnt_t freemem
; /* Current available memory in pages. */
91 int interrupts_unleashed
; /* set when we do the first spl0() */
93 kmem_cache_t
*process_cache
; /* kmem cache for proc structures */
96 * Indicates whether the auditing module (c2audit) is loaded. Possible
98 * 0 - c2audit module is excluded in /etc/system and cannot be loaded
99 * 1 - c2audit module is not loaded but can be anytime
100 * 2 - c2audit module is loaded
102 int audit_active
= C2AUDIT_DISABLED
;
105 * Process 0's lwp directory and lwpid hash table.
107 lwpdir_t p0_lwpdir
[2];
108 tidhash_t p0_tidhash
[2];
112 * Machine-independent initialization code
113 * Called from cold start routine as
114 * soon as a stack and segmentation
115 * have been established.
117 * clear and free user core
119 * hand craft 0th process
120 * call all initialization routines
121 * fork - process 0 to schedule
122 * - process 1 execute bootstrap
123 * - process 2 to page out
124 * create system threads
127 char initname
[INITNAME_SZ
] = "/sbin/init"; /* also referenced by zone0 */
128 char initargs
[BOOTARGS_MAX
] = ""; /* also referenced by zone0 */
131 * Construct a stack for init containing the arguments to it, then
132 * pass control to exec_common.
135 exec_init(const char *initpath
, const char *args
)
140 caddr32_t exec_fnamep
;
143 size_t argvlen
, alen
;
146 int error
= 0, count
= 0;
147 proc_t
*p
= ttoproc(curthread
);
148 klwp_t
*lwp
= ttolwp(curthread
);
154 alen
= strlen(initpath
) + 1 + strlen(args
) + 1;
155 scratchargs
= kmem_alloc(alen
, KM_SLEEP
);
156 (void) snprintf(scratchargs
, alen
, "%s %s", initpath
, args
);
159 * We do a quick two state parse of the string to sort out how big
163 for (i
= 0; i
< strlen(scratchargs
); i
++) {
164 if (scratchargs
[i
] == ' ' || scratchargs
[i
] == '\0') {
173 argvlen
= sizeof (caddr32_t
) * (argc
+ 1);
174 argv
= kmem_zalloc(argvlen
, KM_SLEEP
);
177 * We pull off a bit of a hack here. We work our way through the
178 * args string, putting nulls at the ends of space delimited tokens
179 * (boot args don't support quoting at this time). Then we just
180 * copy the whole mess to userland in one go. In other words, we
181 * transform this: "init -s -r\0" into this on the stack:
194 * -0x10 NULL | | | (argv[3])
195 * -0x14 -----|--|-' (argv[2])
196 * -0x18 ------|--' (argv[1])
197 * -0x1c -------' (argv[0])
199 * Since we know the value of ucp at the beginning of this process,
200 * we can trivially compute the argv[] array which we also need to
201 * place in userland: argv[i] = ucp - sarg(i), where ucp is the
202 * stack ptr, and sarg is the string index of the start of the
205 ucp
= (caddr32_t
)(uintptr_t)p
->p_usrstack
;
211 for (i
= 0; i
< alen
; i
++) {
212 if (scratchargs
[i
] == ' ' || scratchargs
[i
] == '\0') {
213 if (in_arg
== B_TRUE
) {
215 scratchargs
[i
] = '\0';
216 argv
[argc
++] = ucp
- (alen
- sarg
);
218 } else if (in_arg
== B_FALSE
) {
224 error
|= copyout(scratchargs
, (caddr_t
)(uintptr_t)ucp
, alen
);
226 uap
= (caddr32_t
*)P2ALIGN((uintptr_t)ucp
, sizeof (caddr32_t
));
227 uap
--; /* advance to be below the word we're in */
228 uap
-= (argc
+ 1); /* advance argc words down, plus one for NULL */
229 error
|= copyout(argv
, uap
, argvlen
);
232 zcmn_err(p
->p_zone
->zone_id
, CE_WARN
,
233 "Could not construct stack for init.\n");
234 kmem_free(argv
, argvlen
);
235 kmem_free(scratchargs
, alen
);
239 exec_fnamep
= argv
[0];
240 kmem_free(argv
, argvlen
);
241 kmem_free(scratchargs
, alen
);
244 * Point at the arguments.
246 lwp
->lwp_ap
= lwp
->lwp_arg
;
247 lwp
->lwp_arg
[0] = (uintptr_t)exec_fnamep
;
248 lwp
->lwp_arg
[1] = (uintptr_t)uap
;
249 lwp
->lwp_arg
[2] = (uintptr_t)NULL
;
250 curthread
->t_post_sys
= 1;
251 curthread
->t_sysnum
= SYS_execve
;
254 * If we are executing init from zsched, we may have inherited its
255 * parent process's signal mask. Clear it now so that we behave in
256 * the same way as when started from the global zone.
258 sigemptyset(&curthread
->t_hold
);
260 brand_action
= ZONE_IS_BRANDED(p
->p_zone
) ? EBA_BRAND
: EBA_NONE
;
262 error
= exec_common((const char *)(uintptr_t)exec_fnamep
,
263 (const char **)(uintptr_t)uap
, NULL
, brand_action
);
266 * Normally we would just set lwp_argsaved and t_post_sys and
267 * let post_syscall reset lwp_ap for us. Unfortunately,
268 * exec_init isn't always called from a system call. Instead
269 * of making a mess of trap_cleanup, we just reset the args
272 reset_syscall_args();
279 zcmn_err(p
->p_zone
->zone_id
, CE_WARN
,
280 "exec(%s) failed (file not found).\n", initpath
);
287 zcmn_err(p
->p_zone
->zone_id
, CE_WARN
,
288 "exec(%s) failed with errno %d. Retrying...\n",
294 zcmn_err(p
->p_zone
->zone_id
, CE_WARN
,
295 "exec(%s) failed with errno %d.", initpath
, error
);
300 * This routine does all of the common setup for invoking init; global
301 * and non-global zones employ this routine for the functionality which is
304 * This program (init, presumably) must be a 32-bit process.
310 ASSERT_STACK_ALIGNED();
311 p
->p_zone
->zone_proc_initpid
= p
->p_pid
;
313 p
->p_cstime
= p
->p_stime
= p
->p_cutime
= p
->p_utime
= 0;
314 p
->p_usrstack
= (caddr_t
)USRSTACK32
;
315 p
->p_model
= DATAMODEL_ILP32
;
316 p
->p_stkprot
= PROT_ZFOD
& ~PROT_EXEC
;
317 p
->p_datprot
= PROT_ZFOD
& ~PROT_EXEC
;
318 p
->p_stk_ctl
= INT32_MAX
;
320 p
->p_as
= as_alloc();
322 p
->p_as
->a_userlimit
= (caddr_t
)USERLIMIT32
;
323 (void) hat_setup(p
->p_as
->a_hat
, HAT_INIT
);
327 init_mstate(curthread
, LMS_SYSTEM
);
328 return (exec_init(p
->p_zone
->zone_initname
, p
->p_zone
->zone_bootargs
));
332 * Start the initial user process for the global zone; once running, if
333 * init should subsequently fail, it will be automatically be caught in the
334 * exit(2) path, and restarted by restart_init().
341 ASSERT(curproc
->p_zone
->zone_initname
!= NULL
);
343 if (start_init_common() != 0)
344 halt("unix: Could not start init");
351 proc_t
*p
= ttoproc(curthread
); /* &p0 */
354 extern void fsflush();
355 extern int (*init_tbl
[])();
356 extern int (*mp_init_tbl
[])();
357 extern id_t syscid
, defaultcid
;
358 extern int swaploaded
;
360 extern ib_boot_prop_t
*iscsiboot_prop
;
361 extern void vm_init(void);
362 extern void cbe_init_pre(void);
363 extern void cbe_init(void);
364 extern void clock_tick_init_pre(void);
365 extern void clock_tick_init_post(void);
366 extern void clock_init(void);
367 extern void physio_bufs_init(void);
368 extern void pm_cfb_setup_intr(void);
369 extern int pm_adjust_timestamps(dev_info_t
*, void *);
370 extern void start_other_cpus(int);
371 extern void sysevent_evc_thrinit();
372 extern kmutex_t ualock
;
374 extern void fastboot_post_startup(void);
375 extern void progressbar_start(void);
378 * In the horrible world of x86 in-lines, you can't get symbolic
379 * structure offsets a la genassym. This assertion is here so
380 * that the next poor slob who innocently changes the offset of
381 * cpu_thread doesn't waste as much time as I just did finding
382 * out that it's hard-coded in i86/ml/i86.il. Similarly for
383 * curcpup. You're welcome.
385 ASSERT(CPU
== CPU
->cpu_self
);
386 ASSERT(curthread
== CPU
->cpu_thread
);
387 ASSERT_STACK_ALIGNED();
390 * We take the ualock until we have completed the startup
391 * to prevent kadmin() from disrupting this work. In particular,
392 * we don't want kadmin() to bring the system down while we are
393 * trying to start it up.
395 mutex_enter(&ualock
);
398 * Setup root lgroup and leaf lgroup for CPU 0
400 lgrp_init(LGRP_INIT_STAGE2
);
402 vmobject_init(&kvps
[KV_KVP
].v_object
, &kvps
[KV_KVP
]);
403 vmobject_init(&kvps
[KV_ZVP
].v_object
, &kvps
[KV_ZVP
]);
406 * Once 'startup()' completes, the thread_reaper() daemon would be
407 * created(in thread_init()). After that, it is safe to create threads
408 * that could exit. These exited threads will get reaped.
413 cbe_init_pre(); /* x86 must initialize gethrtimef before timer_init */
416 callout_init(); /* callout table MUST be init'd after cyclics */
417 clock_tick_init_pre();
422 * The progressbar thread uses cv_reltimedwait() and hence needs to be
423 * started after the callout mechanism has been initialized.
428 * On some platforms, clkinitf() changes the timing source that
429 * gethrtime_unscaled() uses to generate timestamps. cbe_init() calls
430 * clkinitf(), so re-initialize the microstate counters after the
431 * timesource has been chosen.
433 init_mstate(&t0
, LMS_SYSTEM
);
434 init_cpu_mstate(CPU
, CMS_SYSTEM
);
437 * May need to probe to determine latencies from CPU 0 after
438 * gethrtime() comes alive in cbe_init() and before enabling interrupts
439 * and copy and release any temporary memory allocated with BOP_ALLOC()
440 * before release_bootstrap() frees boot memory
442 lgrp_init(LGRP_INIT_STAGE3
);
445 * Call all system initialization functions.
447 for (initptr
= &init_tbl
[0]; *initptr
; initptr
++)
450 * Load iSCSI boot properties
454 * initialize vm related stuff.
459 * initialize buffer pool for raw I/O requests
463 ttolwp(curthread
)->lwp_error
= 0; /* XXX kludge for SCSI driver */
466 * Drop the interrupt level and allow interrupts. At this point
467 * the DDI guarantees that interrupts are enabled.
470 interrupts_unleashed
= 1;
473 * Create kmem cache for proc structures
475 process_cache
= kmem_cache_create("process_cache", sizeof (proc_t
),
476 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
478 vfs_mountroot(); /* Mount the root file system */
479 errorq_init(); /* after vfs_mountroot() so DDI root is ready */
480 cpu_kstat_init(CPU
); /* after vfs_mountroot() so TOD is valid */
481 ddi_walk_devs(ddi_root_node(), pm_adjust_timestamps
, NULL
);
482 /* after vfs_mountroot() so hrestime is valid */
488 * Initialize Solaris Audit Subsystem
493 * Plumb the protocol modules and drivers only if we are not
494 * networked booted, in this case we already did it in rootconf().
496 if (netboot
== 0 && iscsiboot_prop
== NULL
)
499 gethrestime(&PTOU(curproc
)->u_start
);
500 curthread
->t_start
= PTOU(curproc
)->u_start
.tv_sec
;
501 p
->p_mstart
= gethrtime();
504 * Perform setup functions that can only be done after root
505 * and swap have been set up.
511 * attach drivers with ddi-forceattach prop
512 * It must be done early enough to load hotplug drivers (e.g.
513 * pcmcia nexus) so that devices enumerated via hotplug is
514 * available before I/O subsystem is fully initialized.
516 i_ddi_forceattach_drivers();
519 * Set the scan rate and other parameters of the paging subsystem.
524 * Initialize process 0's lwp directory and lwpid hash table.
526 p
->p_lwpdir
= p
->p_lwpfree
= p0_lwpdir
;
527 p
->p_lwpdir
->ld_next
= p
->p_lwpdir
+ 1;
529 p
->p_tidhash
= p0_tidhash
;
531 p0_lep
.le_thread
= curthread
;
532 p0_lep
.le_lwpid
= curthread
->t_tid
;
533 p0_lep
.le_start
= curthread
->t_start
;
534 lwp_hash_in(p
, &p0_lep
, p0_tidhash
, 2, 0);
537 * Initialize extended accounting.
542 * Initialize threads of sysevent event channels
544 sysevent_evc_thrinit();
547 * This must be done after post_startup() but before
550 lgrp_init(LGRP_INIT_STAGE4
);
553 * Perform MP initialization, if any.
559 * Finish lgrp initialization after all CPUS are brought online.
561 lgrp_init(LGRP_INIT_STAGE5
);
564 * After mp_init(), number of cpus are known (this is
565 * true for the time being, when there are actually
566 * hot pluggable cpus then this scheme would not do).
567 * Any per cpu initialization is done here.
572 clock_tick_init_post();
574 for (initptr
= &mp_init_tbl
[0]; *initptr
; initptr
++)
578 * These must be called after start_other_cpus
582 fastboot_post_startup();
586 * Make init process; enter scheduling loop with system process.
588 * Note that we manually assign the pids for these processes, for
589 * historical reasons. If more pre-assigned pids are needed,
590 * FAMOUS_PIDS will have to be updated.
593 /* create init process */
594 if (newproc(start_init
, NULL
, defaultcid
, 59, NULL
,
596 panic("main: unable to fork init.");
598 /* create pageout daemon */
599 if (newproc(pageout
, NULL
, syscid
, maxclsyspri
- 1, NULL
,
601 panic("main: unable to fork pageout()");
603 /* create fsflush daemon */
604 if (newproc(fsflush
, NULL
, syscid
, minclsyspri
, NULL
,
606 panic("main: unable to fork fsflush()");
609 * Create system threads (threads are associated with p0)
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
);
617 (void) thread_create(NULL
, 0, seg_pasync_thread
,
618 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
622 /* system is now ready */
625 bcopy("sched", PTOU(curproc
)->u_psargs
, 6);
626 bcopy("sched", PTOU(curproc
)->u_comm
, 5);