2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
30 * The Regents of the University of California. All rights reserved.
31 * (c) UNIX System Laboratories, Inc.
32 * All or some portions of this file are derived from material licensed
33 * to the University of California by American Telephone and Telegraph
34 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
35 * the permission of UNIX System Laboratories, Inc.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
70 * Mach Operating System
71 * Copyright (c) 1987 Carnegie-Mellon University
72 * All rights reserved. The CMU software License Agreement specifies
73 * the terms and conditions for use and redistribution.
76 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
77 * support for mandatory and extensible security protections. This notice
78 * is included in support of clause 2.2 (b) of the Apple Public License,
82 #include <sys/param.h>
83 #include <sys/filedesc.h>
84 #include <sys/kernel.h>
85 #include <sys/mount_internal.h>
86 #include <sys/proc_internal.h>
87 #include <sys/kauth.h>
88 #include <sys/systm.h>
89 #include <sys/vnode_internal.h>
91 #include <sys/buf_internal.h>
92 #include <sys/clist.h>
95 #include <sys/systm.h>
98 #include <security/audit/audit.h>
100 #include <sys/malloc.h>
101 #include <sys/dkstat.h>
102 #include <sys/codesign.h>
104 #include <kern/startup.h>
105 #include <kern/thread.h>
106 #include <kern/task.h>
107 #include <kern/ast.h>
108 #include <kern/kalloc.h>
110 #include <mach/vm_param.h>
112 #include <vm/vm_map.h>
113 #include <vm/vm_kern.h>
115 #include <sys/ux_exception.h> /* for ux_exception_port */
117 #include <sys/reboot.h>
118 #include <mach/exception_types.h>
119 #include <dev/busvar.h> /* for pseudo_inits */
120 #include <sys/kdebug.h>
122 #include <mach/mach_types.h>
123 #include <mach/vm_prot.h>
124 #include <mach/semaphore.h>
125 #include <mach/sync_policy.h>
126 #include <kern/clock.h>
127 #include <mach/kern_return.h>
128 #include <mach/thread_act.h> /* for thread_resume() */
129 #include <mach/task.h> /* for task_set_exception_ports() */
130 #include <sys/ux_exception.h> /* for ux_handler() */
131 #include <sys/ubc_internal.h> /* for ubc_init() */
132 #include <sys/mcache.h> /* for mcache_init() */
133 #include <sys/mbuf.h> /* for mbinit() */
134 #include <sys/event.h> /* for knote_init() */
135 #include <sys/aio_kern.h> /* for aio_init() */
136 #include <sys/semaphore.h> /* for psem_cache_init() */
137 #include <net/dlil.h> /* for dlil_init() */
138 #include <net/kpi_protocol.h> /* for proto_kpi_init() */
139 #include <sys/pipe.h> /* for pipeinit() */
140 #include <sys/socketvar.h> /* for socketinit() */
141 #include <sys/protosw.h> /* for domaininit() */
142 #include <kern/sched_prim.h> /* for thread_wakeup() */
143 #include <net/if_ether.h> /* for ether_family_init() */
144 #include <vm/vm_protos.h> /* for vnode_pager_bootstrap() */
145 #include <miscfs/devfs/devfsdefs.h> /* for devfs_kernel_mount() */
146 #include <mach/host_priv.h> /* for host_set_exception_ports() */
147 #include <kern/host.h> /* for host_priv_self() */
148 #include <vm/vm_kern.h> /* for kmem_suballoc() */
149 #include <sys/semaphore.h> /* for psem_lock_init() */
150 #include <sys/msgbuf.h> /* for log_setsize() */
151 #include <sys/tty.h> /* for tty_init() */
152 #include <net/if_utun.h> /* for utun_register_control() */
153 #include <net/net_str_id.h> /* for net_str_id_init() */
155 #include <net/init.h>
158 #include <security/mac_framework.h>
159 #include <security/mac_internal.h> /* mac_init_bsd() */
160 #include <security/mac_mach_internal.h> /* mac_update_task_label() */
163 #include <machine/exec.h>
166 #include <sys/imageboot.h>
170 #include <net/if_pflog.h>
173 #include <pexpert/pexpert.h>
175 void * get_user_regs(thread_t
); /* XXX kludge for <machine/thread.h> */
176 void IOKitInitializeTime(void); /* XXX */
177 void IOSleep(unsigned int); /* XXX */
178 void loopattach(void); /* XXX */
179 void vc_progress_set(boolean_t
, uint32_t); /* XXX */
181 const char copyright
[] =
182 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t"
183 "The Regents of the University of California. "
184 "All rights reserved.\n\n";
186 /* Components of the first process -- never freed. */
188 struct session session0
;
190 struct filedesc filedesc0
;
191 struct plimit limit0
;
192 struct pstats pstats0
;
193 struct sigacts sigacts0
;
203 /* Global variables to make pstat happy. We do swapping differently */
207 struct swdevt swdevt
[1];
209 dev_t rootdev
; /* device of the root */
210 dev_t dumpdev
; /* device to take dumps on */
211 long dumplo
; /* offset into dumpdev */
213 char hostname
[MAXHOSTNAMELEN
];
215 char domainname
[MAXDOMNAMELEN
];
217 #if defined(__i386__) || defined(__x86_64__)
218 struct exec_archhandler exec_archhandler_ppc
= {
219 .path
= "/usr/libexec/oah/translate",
221 const char * const kRosettaStandIn_str
= "/usr/libexec/oah/RosettaNonGrata";
223 struct exec_archhandler exec_archhandler_ppc
;
224 #endif /* __i386__ */
226 char rootdevice
[16]; /* hfs device names have at least 9 chars */
229 struct kmemstats kmemstats
[M_LAST
];
232 int lbolt
; /* awoken once a second */
233 struct vnode
*rootvp
;
234 int boothowto
= RB_DEBUG
;
236 void lightning_bolt(void *);
237 extern kern_return_t
IOFindBSDRoot(char *, unsigned int, dev_t
*, u_int32_t
*);
238 extern void IOSecureBSDRoot(const char * rootName
);
239 extern kern_return_t
IOKitBSDInit(void );
240 extern void kminit(void);
241 extern void klogwakeup(void);
242 extern void file_lock_init(void);
243 extern void kmeminit(void);
244 extern void bsd_bufferinit(void);
249 vm_map_t bsd_pageable_map
;
252 static int bsd_simul_execs
= BSD_SIMUL_EXECS
;
253 static int bsd_pageable_map_size
= BSD_PAGABLE_MAP_SIZE
;
254 __private_extern__
int execargs_cache_size
= BSD_SIMUL_EXECS
;
255 __private_extern__
int execargs_free_count
= BSD_SIMUL_EXECS
;
256 __private_extern__ vm_offset_t
* execargs_cache
= NULL
;
258 void bsd_exec_setup(int);
261 * Set to disable grading 64 bit Mach-o binaries as executable, for testing;
264 __private_extern__
int bootarg_no64exec
= 0;
267 extern int customnbuf
;
269 void bsd_init(void) __attribute__((section("__TEXT, initcode")));
270 kern_return_t
bsd_autoconf(void) __attribute__((section("__TEXT, initcode")));
271 void bsd_utaskbootstrap(void) __attribute__((section("__TEXT, initcode")));
273 static void parse_bsd_args(void);
274 extern task_t bsd_init_task
;
275 extern char init_task_failure_data
[];
276 extern void time_zone_slock_init(void);
277 static void process_name(const char *, proc_t
);
279 static void setconf(void);
281 funnel_t
*kernel_flock
;
284 extern void sysv_shm_lock_init(void);
287 extern void sysv_sem_lock_init(void);
290 extern void sysv_msg_lock_init(void);
292 extern void pthread_init(void);
294 /* kmem access not enabled by default; can be changed with boot-args */
297 /* size of kernel trace buffer, disabled by default */
298 unsigned int new_nkdbufs
= 0;
300 /* mach leak logging */
302 int turn_on_log_leaks
= 0;
304 extern void stackshot_lock_init(void);
307 /* If we are using CONFIG_DTRACE */
309 extern void dtrace_postinit(void);
314 * Initialization code.
315 * Called from cold start routine as
316 * soon as a stack and segmentation
317 * have been established.
320 * hand craft 0th process
321 * call all initialization routines
322 * hand craft 1st user process
326 * Sets the name for the given task.
329 process_name(const char *s
, proc_t p
)
331 size_t length
= strlen(s
);
334 length
>= sizeof(p
->p_comm
) ? sizeof(p
->p_comm
) :
338 /* To allow these values to be patched, they're globals here */
339 #include <machine/vmparam.h>
340 struct rlimit vm_initial_limit_stack
= { DFLSSIZ
, MAXSSIZ
- PAGE_SIZE
};
341 struct rlimit vm_initial_limit_data
= { DFLDSIZ
, MAXDSIZ
};
342 struct rlimit vm_initial_limit_core
= { DFLCSIZ
, MAXCSIZ
};
344 extern thread_t
cloneproc(task_t
, proc_t
, int);
345 extern int (*mountroot
)(void);
346 extern int netboot_mountroot(void); /* netboot.c */
347 extern int netboot_setup(void);
349 lck_grp_t
* proc_lck_grp
;
350 lck_grp_t
* proc_slock_grp
;
351 lck_grp_t
* proc_fdmlock_grp
;
352 lck_grp_t
* proc_mlock_grp
;
353 lck_grp_attr_t
* proc_lck_grp_attr
;
354 lck_attr_t
* proc_lck_attr
;
355 lck_mtx_t
* proc_list_mlock
;
356 lck_mtx_t
* proc_klist_mlock
;
358 extern lck_mtx_t
* execargs_cache_lock
;
360 /* hook called after root is mounted XXX temporary hack */
361 void (*mountroot_post_hook
)(void);
362 void (*unmountroot_pre_hook
)(void);
365 * This function is called very early on in the Mach startup, from the
366 * function start_kernel_threads() in osfmk/kern/startup.c. It's called
367 * in the context of the current (startup) task using a call to the
368 * function kernel_thread_create() to jump into start_kernel_threads().
369 * Internally, kernel_thread_create() calls thread_create_internal(),
370 * which calls uthread_alloc(). The function of uthread_alloc() is
371 * normally to allocate a uthread structure, and fill out the uu_sigmask,
372 * uu_context fields. It skips filling these out in the case of the "task"
373 * being "kernel_task", because the order of operation is inverted. To
374 * account for that, we need to manually fill in at least the contents
375 * of the uu_context.vc_ucred field so that the uthread structure can be
376 * used like any other.
383 #if __i386__ || __x86_64__
386 struct vfs_context context
;
388 struct ucred temp_cred
;
390 #define bsd_init_kprintf(x...) /* kprintf("bsd_init: " x) */
392 kernel_flock
= funnel_alloc(KERNEL_FUNNEL
);
393 if (kernel_flock
== (funnel_t
*)0 ) {
394 panic("bsd_init: Failed to allocate kernel funnel");
399 bsd_init_kprintf("calling kmeminit\n");
402 bsd_init_kprintf("calling parse_bsd_args\n");
405 /* Initialize kauth subsystem before instancing the first credential */
406 bsd_init_kprintf("calling kauth_init\n");
409 /* Initialize process and pgrp structures. */
410 bsd_init_kprintf("calling procinit\n");
413 /* Initialize the ttys (MUST be before kminit()/bsd_autoconf()!)*/
416 kernproc
= &proc0
; /* implicitly bzero'ed */
418 /* kernel_task->proc = kernproc; */
419 set_bsdtask_info(kernel_task
,(void *)kernproc
);
421 /* give kernproc a name */
422 bsd_init_kprintf("calling process_name\n");
423 process_name("kernel_task", kernproc
);
425 /* allocate proc lock group attribute and group */
426 bsd_init_kprintf("calling lck_grp_attr_alloc_init\n");
427 proc_lck_grp_attr
= lck_grp_attr_alloc_init();
429 proc_lck_grp
= lck_grp_alloc_init("proc", proc_lck_grp_attr
);
430 #ifndef CONFIG_EMBEDDED
431 proc_slock_grp
= lck_grp_alloc_init("proc-slock", proc_lck_grp_attr
);
432 proc_fdmlock_grp
= lck_grp_alloc_init("proc-fdmlock", proc_lck_grp_attr
);
433 proc_mlock_grp
= lck_grp_alloc_init("proc-mlock", proc_lck_grp_attr
);
435 /* Allocate proc lock attribute */
436 proc_lck_attr
= lck_attr_alloc_init();
438 #if __PROC_INTERNAL_DEBUG
439 lck_attr_setdebug(proc_lck_attr
);
443 #ifdef CONFIG_EMBEDDED
444 proc_list_mlock
= lck_mtx_alloc_init(proc_lck_grp
, proc_lck_attr
);
445 proc_klist_mlock
= lck_mtx_alloc_init(proc_lck_grp
, proc_lck_attr
);
446 lck_mtx_init(&kernproc
->p_mlock
, proc_lck_grp
, proc_lck_attr
);
447 lck_mtx_init(&kernproc
->p_fdmlock
, proc_lck_grp
, proc_lck_attr
);
448 lck_spin_init(&kernproc
->p_slock
, proc_lck_grp
, proc_lck_attr
);
450 proc_list_mlock
= lck_mtx_alloc_init(proc_mlock_grp
, proc_lck_attr
);
451 proc_klist_mlock
= lck_mtx_alloc_init(proc_mlock_grp
, proc_lck_attr
);
452 lck_mtx_init(&kernproc
->p_mlock
, proc_mlock_grp
, proc_lck_attr
);
453 lck_mtx_init(&kernproc
->p_fdmlock
, proc_fdmlock_grp
, proc_lck_attr
);
454 lck_spin_init(&kernproc
->p_slock
, proc_slock_grp
, proc_lck_attr
);
457 execargs_cache_lock
= lck_mtx_alloc_init(proc_lck_grp
, proc_lck_attr
);
458 execargs_cache_size
= bsd_simul_execs
;
459 execargs_free_count
= bsd_simul_execs
;
460 execargs_cache
= (vm_offset_t
*)kalloc(bsd_simul_execs
* sizeof(vm_offset_t
));
461 bzero(execargs_cache
, bsd_simul_execs
* sizeof(vm_offset_t
));
463 if (current_task() != kernel_task
)
464 printf("bsd_init: We have a problem, "
465 "current task is not kernel task\n");
467 bsd_init_kprintf("calling get_bsdthread_info\n");
468 ut
= (uthread_t
)get_bsdthread_info(current_thread());
472 * Initialize the MAC Framework
474 mac_policy_initbsd();
475 kernproc
->p_mac_enforce
= 0;
482 LIST_INSERT_HEAD(&allproc
, kernproc
, p_list
);
483 kernproc
->p_pgrp
= &pgrp0
;
484 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0
, pg_hash
);
485 LIST_INIT(&pgrp0
.pg_members
);
486 #ifdef CONFIG_EMBEDDED
487 lck_mtx_init(&pgrp0
.pg_mlock
, proc_lck_grp
, proc_lck_attr
);
489 lck_mtx_init(&pgrp0
.pg_mlock
, proc_mlock_grp
, proc_lck_attr
);
491 /* There is no other bsd thread this point and is safe without pgrp lock */
492 LIST_INSERT_HEAD(&pgrp0
.pg_members
, kernproc
, p_pglist
);
493 kernproc
->p_listflag
|= P_LIST_INPGRP
;
494 kernproc
->p_pgrpid
= 0;
496 pgrp0
.pg_session
= &session0
;
497 pgrp0
.pg_membercnt
= 1;
499 session0
.s_count
= 1;
500 session0
.s_leader
= kernproc
;
501 session0
.s_listflags
= 0;
502 #ifdef CONFIG_EMBEDDED
503 lck_mtx_init(&session0
.s_mlock
, proc_lck_grp
, proc_lck_attr
);
505 lck_mtx_init(&session0
.s_mlock
, proc_mlock_grp
, proc_lck_attr
);
507 LIST_INSERT_HEAD(SESSHASH(0), &session0
, s_hash
);
511 kernproc
->p_lctx
= NULL
;
514 kernproc
->task
= kernel_task
;
516 kernproc
->p_stat
= SRUN
;
517 kernproc
->p_flag
= P_SYSTEM
;
518 kernproc
->p_nice
= NZERO
;
519 kernproc
->p_pptr
= kernproc
;
521 TAILQ_INIT(&kernproc
->p_uthlist
);
522 TAILQ_INSERT_TAIL(&kernproc
->p_uthlist
, ut
, uu_list
);
524 kernproc
->sigwait
= FALSE
;
525 kernproc
->sigwait_thread
= THREAD_NULL
;
526 kernproc
->exit_thread
= THREAD_NULL
;
527 kernproc
->p_csflags
= CS_VALID
;
530 * Create credential. This also Initializes the audit information.
532 bsd_init_kprintf("calling bzero\n");
533 bzero(&temp_cred
, sizeof(temp_cred
));
534 temp_cred
.cr_ngroups
= 1;
536 temp_cred
.cr_audit
.as_aia_p
= &audit_default_aia
;
537 /* XXX the following will go away with cr_au */
538 temp_cred
.cr_au
.ai_auid
= AU_DEFAUDITID
;
540 bsd_init_kprintf("calling kauth_cred_create\n");
541 kernproc
->p_ucred
= kauth_cred_create(&temp_cred
);
543 /* give the (already exisiting) initial thread a reference on it */
544 bsd_init_kprintf("calling kauth_cred_ref\n");
545 kauth_cred_ref(kernproc
->p_ucred
);
546 ut
->uu_context
.vc_ucred
= kernproc
->p_ucred
;
547 ut
->uu_context
.vc_thread
= current_thread();
549 TAILQ_INIT(&kernproc
->p_aio_activeq
);
550 TAILQ_INIT(&kernproc
->p_aio_doneq
);
551 kernproc
->p_aio_total_count
= 0;
552 kernproc
->p_aio_active_count
= 0;
554 bsd_init_kprintf("calling file_lock_init\n");
558 mac_cred_label_associate_kernel(kernproc
->p_ucred
);
559 mac_task_label_update_cred (kernproc
->p_ucred
, (struct task
*) kernproc
->task
);
562 /* Create the file descriptor table. */
563 filedesc0
.fd_refcnt
= 1+1; /* +1 so shutdown will not _FREE_ZONE */
564 kernproc
->p_fd
= &filedesc0
;
565 filedesc0
.fd_cmask
= cmask
;
566 filedesc0
.fd_knlistsize
= -1;
567 filedesc0
.fd_knlist
= NULL
;
568 filedesc0
.fd_knhash
= NULL
;
569 filedesc0
.fd_knhashmask
= 0;
571 /* Create the limits structures. */
572 kernproc
->p_limit
= &limit0
;
573 for (i
= 0; i
< sizeof(kernproc
->p_rlimit
)/sizeof(kernproc
->p_rlimit
[0]); i
++)
574 limit0
.pl_rlimit
[i
].rlim_cur
=
575 limit0
.pl_rlimit
[i
].rlim_max
= RLIM_INFINITY
;
576 limit0
.pl_rlimit
[RLIMIT_NOFILE
].rlim_cur
= NOFILE
;
577 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_cur
= maxprocperuid
;
578 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_max
= maxproc
;
579 limit0
.pl_rlimit
[RLIMIT_STACK
] = vm_initial_limit_stack
;
580 limit0
.pl_rlimit
[RLIMIT_DATA
] = vm_initial_limit_data
;
581 limit0
.pl_rlimit
[RLIMIT_CORE
] = vm_initial_limit_core
;
582 limit0
.pl_refcnt
= 1;
584 kernproc
->p_stats
= &pstats0
;
585 kernproc
->p_sigacts
= &sigacts0
;
588 * Charge root for two processes: init and mach_init.
590 bsd_init_kprintf("calling chgproccnt\n");
591 (void)chgproccnt(0, 1);
594 * Allocate a kernel submap for pageable memory
595 * for temporary copying (execve()).
600 bsd_init_kprintf("calling kmem_suballoc\n");
601 ret
= kmem_suballoc(kernel_map
,
603 (vm_size_t
)bsd_pageable_map_size
,
607 if (ret
!= KERN_SUCCESS
)
608 panic("bsd_init: Failed to allocate bsd pageable map");
612 * Initialize buffers and hash links for buffers
614 * SIDE EFFECT: Starts a thread for bcleanbuf_thread(), so must
615 * happen after a credential has been associated with
618 bsd_init_kprintf("calling bsd_bufferinit\n");
621 /* Initialize the execve() semaphore */
622 bsd_init_kprintf("calling semaphore_create\n");
624 if (ret
!= KERN_SUCCESS
)
625 panic("bsd_init: Failed to create execve semaphore");
628 * Initialize the calendar.
630 bsd_init_kprintf("calling IOKitInitializeTime\n");
631 IOKitInitializeTime();
633 if (turn_on_log_leaks
&& !new_nkdbufs
)
634 new_nkdbufs
= 200000;
635 start_kern_tracing(new_nkdbufs
);
636 if (turn_on_log_leaks
)
639 bsd_init_kprintf("calling ubc_init\n");
642 /* Initialize the file systems. */
643 bsd_init_kprintf("calling vfsinit\n");
647 /* Initialize per-CPU cache allocator */
650 /* Initialize mbuf's. */
651 bsd_init_kprintf("calling mbinit\n");
653 net_str_id_init(); /* for mbuf tags */
657 * Initializes security event auditing.
658 * XXX: Should/could this occur later?
661 bsd_init_kprintf("calling audit_init\n");
665 /* Initialize kqueues */
666 bsd_init_kprintf("calling knote_init\n");
669 /* Initialize for async IO */
670 bsd_init_kprintf("calling aio_init\n");
673 /* Initialize pipes */
674 bsd_init_kprintf("calling pipeinit\n");
677 /* Initialize SysV shm subsystem locks; the subsystem proper is
678 * initialized through a sysctl.
681 bsd_init_kprintf("calling sysv_shm_lock_init\n");
682 sysv_shm_lock_init();
685 bsd_init_kprintf("calling sysv_sem_lock_init\n");
686 sysv_sem_lock_init();
689 bsd_init_kprintf("sysv_msg_lock_init\n");
690 sysv_msg_lock_init();
692 bsd_init_kprintf("calling pshm_lock_init\n");
694 bsd_init_kprintf("calling psem_lock_init\n");
698 /* POSIX Shm and Sem */
699 bsd_init_kprintf("calling pshm_cache_init\n");
701 bsd_init_kprintf("calling psem_cache_init\n");
703 bsd_init_kprintf("calling time_zone_slock_init\n");
704 time_zone_slock_init();
706 /* Stack snapshot facility lock */
707 stackshot_lock_init();
709 * Initialize protocols. Block reception of incoming packets
710 * until everything is ready.
712 bsd_init_kprintf("calling sysctl_register_fixed\n");
713 sysctl_register_fixed();
714 bsd_init_kprintf("calling sysctl_mib_init\n");
717 bsd_init_kprintf("calling dlil_init\n");
719 bsd_init_kprintf("calling proto_kpi_init\n");
721 #endif /* NETWORKING */
723 bsd_init_kprintf("calling socketinit\n");
725 bsd_init_kprintf("calling domaininit\n");
729 kernproc
->p_fd
->fd_cdir
= NULL
;
730 kernproc
->p_fd
->fd_rdir
= NULL
;
733 /* Initialize kernel memory status notifications */
734 bsd_init_kprintf("calling kern_memorystatus_init\n");
735 kern_memorystatus_init();
739 /* Initialize kernel profiling. */
743 /* kick off timeout driven events by calling first time */
744 thread_wakeup(&lbolt
);
745 timeout(lightning_bolt
, 0, hz
);
747 bsd_init_kprintf("calling bsd_autoconf\n");
755 * We attach the loopback interface *way* down here to ensure
756 * it happens after autoconf(), otherwise it becomes the
757 * "primary" interface.
761 bsd_init_kprintf("calling loopattach\n");
762 loopattach(); /* XXX */
766 /* Initialize packet filter log interface */
771 /* Register the built-in dlil ethernet interface family */
772 bsd_init_kprintf("calling ether_family_init\n");
777 /* Call any kext code that wants to run just after network init */
778 bsd_init_kprintf("calling net_init_run\n");
781 /* register user tunnel kernel control handler */
782 utun_register_control();
783 #endif /* NETWORKING */
785 bsd_init_kprintf("calling vnode_pager_bootstrap\n");
786 vnode_pager_bootstrap();
788 /* XXX Hack for early debug stop */
789 printf("\nabout to sleep for 10 seconds\n");
790 IOSleep( 10 * 1000 );
791 /* Debugger("hello"); */
794 bsd_init_kprintf("calling inittodr\n");
799 /* print out early VM statistics */
801 vm_statistics_data_t stat
;
802 mach_msg_type_number_t count
;
804 count
= HOST_VM_INFO_COUNT
;
805 kr1
= host_statistics(host_self(),
809 kprintf("Mach Virtual Memory Statistics (page size of 4096) bytes\n"
810 "Pages free:\t\t\t%u.\n"
811 "Pages active:\t\t\t%u.\n"
812 "Pages inactive:\t\t\t%u.\n"
813 "Pages wired down:\t\t%u.\n"
814 "\"Translation faults\":\t\t%u.\n"
815 "Pages copy-on-write:\t\t%u.\n"
816 "Pages zero filled:\t\t%u.\n"
817 "Pages reactivated:\t\t%u.\n"
818 "Pageins:\t\t\t%u.\n"
819 "Pageouts:\t\t\t%u.\n"
820 "Object cache: %u hits of %u lookups (%d%% hit rate)\n",
828 stat
.zero_fill_count
,
834 (stat
.hits
== 0) ? 100 :
835 ((stat
.lookups
* 100) / stat
.hits
));
837 #endif /* CONFIG_EMBEDDED */
839 /* Mount the root file system. */
843 bsd_init_kprintf("calling setconf\n");
846 bsd_init_kprintf("vfs_mountroot\n");
847 if (0 == (err
= vfs_mountroot()))
849 rootdevice
[0] = '\0';
851 if (mountroot
== netboot_mountroot
) {
852 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
853 vc_progress_set(FALSE
, 0);
855 printf("bsd_init: failed to mount network root, error %d, %s\n",
856 err
, PE_boot_args());
857 printf("We are hanging here...\n");
863 printf("cannot mount root, errno = %d\n", err
);
864 boothowto
|= RB_ASKNAME
;
867 IOSecureBSDRoot(rootdevice
);
869 context
.vc_thread
= current_thread();
870 context
.vc_ucred
= kernproc
->p_ucred
;
871 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
873 bsd_init_kprintf("calling VFS_ROOT\n");
874 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
875 if (VFS_ROOT(mountlist
.tqh_first
, &rootvnode
, &context
))
876 panic("bsd_init: cannot find root vnode: %s", PE_boot_args());
877 rootvnode
->v_flag
|= VROOT
;
878 (void)vnode_ref(rootvnode
);
879 (void)vnode_put(rootvnode
);
880 filedesc0
.fd_cdir
= rootvnode
;
883 if (mountroot
== netboot_mountroot
) {
885 /* post mount setup */
886 if ((err
= netboot_setup()) != 0) {
887 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
888 vc_progress_set(FALSE
, 0);
890 printf("bsd_init: NetBoot could not find root, error %d: %s\n",
891 err
, PE_boot_args());
892 printf("We are hanging here...\n");
903 * See if a system disk image is present. If so, mount it and
904 * switch the root vnode to point to it
907 if(imageboot_needed()) {
910 /* An image was found */
911 if((err
= imageboot_setup())) {
913 * this is not fatal. Keep trying to root
914 * off the original media
916 printf("%s: imageboot could not find root, %d\n",
920 #endif /* CONFIG_IMAGEBOOT */
922 /* set initial time; all other resource data is already zero'ed */
923 microtime(&kernproc
->p_start
);
924 kernproc
->p_stats
->p_start
= kernproc
->p_start
; /* for compat */
928 char mounthere
[] = "/dev"; /* !const because of internal casting */
930 bsd_init_kprintf("calling devfs_kernel_mount\n");
931 devfs_kernel_mount(mounthere
);
935 /* Initialize signal state for process 0. */
936 bsd_init_kprintf("calling siginit\n");
939 bsd_init_kprintf("calling bsd_utaskbootstrap\n");
940 bsd_utaskbootstrap();
942 #if defined(__LP64__)
943 kernproc
->p_flag
|= P_LP64
;
944 printf("Kernel is LP64\n");
946 #if __i386__ || __x86_64__
947 /* this should be done after the root filesystem is mounted */
948 error
= set_archhandler(kernproc
, CPU_TYPE_POWERPC
);
949 // 10/30/08 - gab: <rdar://problem/6324501>
950 // if default 'translate' can't be found, see if the understudy is available
951 if (ENOENT
== error
) {
952 strlcpy(exec_archhandler_ppc
.path
, kRosettaStandIn_str
, MAXPATHLEN
);
953 error
= set_archhandler(kernproc
, CPU_TYPE_POWERPC
);
955 if (error
) /* XXX make more generic */
956 exec_archhandler_ppc
.path
[0] = 0;
959 bsd_init_kprintf("calling mountroot_post_hook\n");
961 /* invoke post-root-mount hook */
962 if (mountroot_post_hook
!= NULL
)
963 mountroot_post_hook();
966 consider_zone_gc(FALSE
);
969 bsd_init_kprintf("done\n");
972 /* Called with kernel funnel held */
976 proc_t p
= current_proc();
980 process_name("init", p
);
984 thread
= current_thread();
985 (void) host_set_exception_ports(host_priv_self(),
986 EXC_MASK_ALL
& ~(EXC_MASK_RPC_ALERT
),//pilotfish (shark) needs this port
987 (mach_port_t
) ux_exception_port
,
988 EXCEPTION_DEFAULT
| MACH_EXCEPTION_CODES
,
991 ut
= (uthread_t
)get_bsdthread_info(thread
);
993 bsd_init_task
= get_threadtask(thread
);
994 init_task_failure_data
[0] = 0;
997 mac_cred_label_associate_user(p
->p_ucred
);
998 mac_task_label_update_cred (p
->p_ucred
, (struct task
*) p
->task
);
1000 load_init_program(p
);
1005 lightning_bolt(__unused
void *dummy
)
1007 boolean_t funnel_state
;
1009 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1011 thread_wakeup(&lbolt
);
1012 timeout(lightning_bolt
,0,hz
);
1015 (void) thread_funnel_set(kernel_flock
, FALSE
);
1021 kprintf("bsd_autoconf: calling kminit\n");
1025 * Early startup for bsd pseudodevices.
1028 struct pseudo_init
*pi
;
1030 for (pi
= pseudo_inits
; pi
->ps_func
; pi
++)
1031 (*pi
->ps_func
) (pi
->ps_count
);
1034 return( IOKitBSDInit());
1038 #include <sys/disklabel.h> /* for MAXPARTITIONS */
1047 * calls into IOKit can generate networking registrations
1048 * which needs to be under network funnel. Right thing to do
1049 * here is to drop the funnel alltogether and regrab it afterwards
1051 err
= IOFindBSDRoot(rootdevice
, sizeof(rootdevice
), &rootdev
, &flags
);
1053 printf("setconf: IOFindBSDRoot returned an error (%d);"
1054 "setting rootdevice to 'sd0a'.\n", err
); /* XXX DEBUG TEMP */
1055 rootdev
= makedev( 6, 0 );
1056 strlcpy(rootdevice
, "sd0a", sizeof(rootdevice
));
1062 /* network device */
1063 mountroot
= netboot_mountroot
;
1066 /* otherwise have vfs determine root filesystem */
1075 bsd_utaskbootstrap(void)
1081 * Clone the bootstrap process from the kernel process, without
1082 * inheriting either task characteristics or memory from the kernel;
1084 thread
= cloneproc(TASK_NULL
, kernproc
, FALSE
);
1086 /* Hold the reference as it will be dropped during shutdown */
1087 initproc
= proc_find(1);
1088 #if __PROC_INTERNAL_DEBUG
1089 if (initproc
== PROC_NULL
)
1090 panic("bsd_utaskbootstrap: initproc not set\n");
1093 * Since we aren't going back out the normal way to our parent,
1094 * we have to drop the transition locks explicitly.
1096 proc_signalend(initproc
, 0);
1097 proc_transend(initproc
, 0);
1099 ut
= (struct uthread
*)get_bsdthread_info(thread
);
1101 act_set_astbsd(thread
);
1102 (void) thread_resume(thread
);
1106 parse_bsd_args(void)
1111 if (PE_parse_boot_argn("-s", namep
, sizeof (namep
)))
1112 boothowto
|= RB_SINGLE
;
1114 if (PE_parse_boot_argn("-b", namep
, sizeof (namep
)))
1115 boothowto
|= RB_NOBOOTRC
;
1117 if (PE_parse_boot_argn("-x", namep
, sizeof (namep
))) /* safe boot */
1118 boothowto
|= RB_SAFEBOOT
;
1120 if (PE_parse_boot_argn("-l", namep
, sizeof (namep
))) /* leaks logging */
1121 turn_on_log_leaks
= 1;
1123 /* disable 64 bit grading */
1124 if (PE_parse_boot_argn("-no64exec", namep
, sizeof (namep
)))
1125 bootarg_no64exec
= 1;
1127 PE_parse_boot_argn("ncl", &ncl
, sizeof (ncl
));
1128 if (PE_parse_boot_argn("nbuf", &max_nbuf_headers
,
1129 sizeof (max_nbuf_headers
))) {
1132 #if !defined(SECURE_KERNEL)
1133 PE_parse_boot_argn("kmem", &setup_kmem
, sizeof (setup_kmem
));
1135 PE_parse_boot_argn("trace", &new_nkdbufs
, sizeof (new_nkdbufs
));
1137 if (PE_parse_boot_argn("msgbuf", &msgbuf
, sizeof (msgbuf
))) {
1138 log_setsize(msgbuf
);
1143 bsd_exec_setup(int scale
)
1149 bsd_simul_execs
= BSD_SIMUL_EXECS
;
1153 bsd_simul_execs
= 65;
1157 bsd_simul_execs
= 129;
1161 bsd_simul_execs
= 257;
1164 bsd_simul_execs
= 513;
1168 bsd_pageable_map_size
= (bsd_simul_execs
* (NCARGS
+ PAGE_SIZE
));