2 * Copyright (c) 2000-2013 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>
109 #include <mach/mach_host.h>
111 #include <mach/vm_param.h>
113 #include <vm/vm_map.h>
114 #include <vm/vm_kern.h>
116 #include <sys/ux_exception.h> /* for ux_exception_port */
118 #include <sys/reboot.h>
119 #include <mach/exception_types.h>
120 #include <dev/busvar.h> /* for pseudo_inits */
121 #include <sys/kdebug.h>
123 #include <mach/mach_types.h>
124 #include <mach/vm_prot.h>
125 #include <mach/semaphore.h>
126 #include <mach/sync_policy.h>
127 #include <kern/clock.h>
128 #include <mach/kern_return.h>
129 #include <mach/thread_act.h> /* for thread_resume() */
130 #include <mach/task.h> /* for task_set_exception_ports() */
131 #include <sys/ux_exception.h> /* for ux_handler() */
132 #include <sys/ubc_internal.h> /* for ubc_init() */
133 #include <sys/mcache.h> /* for mcache_init() */
134 #include <sys/mbuf.h> /* for mbinit() */
135 #include <sys/event.h> /* for knote_init() */
136 #include <sys/kern_memorystatus.h> /* for memorystatus_init() */
137 #include <sys/aio_kern.h> /* for aio_init() */
138 #include <sys/semaphore.h> /* for psem_cache_init() */
139 #include <net/dlil.h> /* for dlil_init() */
140 #include <net/kpi_protocol.h> /* for proto_kpi_init() */
141 #include <net/iptap.h> /* for iptap_init() */
142 #include <sys/pipe.h> /* for pipeinit() */
143 #include <sys/socketvar.h> /* for socketinit() */
144 #include <sys/protosw.h> /* for domaininit() */
145 #include <kern/sched_prim.h> /* for thread_wakeup() */
146 #include <net/if_ether.h> /* for ether_family_init() */
147 #include <net/if_gif.h> /* for gif_init() */
148 #include <vm/vm_protos.h> /* for vnode_pager_bootstrap() */
149 #include <miscfs/devfs/devfsdefs.h> /* for devfs_kernel_mount() */
150 #include <mach/host_priv.h> /* for host_set_exception_ports() */
151 #include <kern/host.h> /* for host_priv_self() */
152 #include <vm/vm_kern.h> /* for kmem_suballoc() */
153 #include <sys/semaphore.h> /* for psem_lock_init() */
154 #include <sys/msgbuf.h> /* for log_setsize() */
155 #include <sys/tty.h> /* for tty_init() */
156 #include <sys/proc_uuid_policy.h> /* proc_uuid_policy_init() */
157 #include <netinet/flow_divert.h> /* flow_divert_init() */
158 #include <net/if_utun.h> /* for utun_register_control() */
159 #include <net/if_ipsec.h> /* for ipsec_register_control() */
160 #include <net/net_str_id.h> /* for net_str_id_init() */
161 #include <net/netsrc.h> /* for netsrc_init() */
162 #include <net/ntstat.h> /* for nstat_init() */
163 #include <kern/assert.h> /* for assert() */
164 #include <sys/kern_overrides.h> /* for init_system_override() */
166 #include <net/init.h>
169 #include <security/mac_framework.h>
170 #include <security/mac_internal.h> /* mac_init_bsd() */
171 #include <security/mac_mach_internal.h> /* mac_update_task_label() */
174 #include <machine/exec.h>
177 #include <sys/netboot.h>
181 #include <sys/imageboot.h>
185 #include <net/if_pflog.h>
188 #include <pexpert/pexpert.h>
189 #include <machine/pal_routines.h>
190 #include <console/video_console.h>
192 void * get_user_regs(thread_t
); /* XXX kludge for <machine/thread.h> */
193 void IOKitInitializeTime(void); /* XXX */
194 void IOSleep(unsigned int); /* XXX */
195 void loopattach(void); /* XXX */
197 const char copyright
[] =
198 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t"
199 "The Regents of the University of California. "
200 "All rights reserved.\n\n";
202 /* Components of the first process -- never freed. */
204 struct session session0
;
206 struct filedesc filedesc0
;
207 struct plimit limit0
;
208 struct pstats pstats0
;
209 struct sigacts sigacts0
;
219 /* Global variables to make pstat happy. We do swapping differently */
223 struct swdevt swdevt
[1];
225 dev_t rootdev
; /* device of the root */
226 dev_t dumpdev
; /* device to take dumps on */
227 long dumplo
; /* offset into dumpdev */
229 char hostname
[MAXHOSTNAMELEN
];
231 char domainname
[MAXDOMNAMELEN
];
234 char rootdevice
[16]; /* hfs device names have at least 9 chars */
237 struct kmemstats kmemstats
[M_LAST
];
240 struct vnode
*rootvp
;
241 int boothowto
= RB_DEBUG
;
243 extern kern_return_t
IOFindBSDRoot(char *, unsigned int, dev_t
*, u_int32_t
*);
244 extern void IOSecureBSDRoot(const char * rootName
);
245 extern kern_return_t
IOKitBSDInit(void );
246 extern void kminit(void);
247 extern void file_lock_init(void);
248 extern void kmeminit(void);
249 extern void bsd_bufferinit(void);
250 extern void throttle_init(void);
252 extern int serverperfmode
;
255 vm_map_t bsd_pageable_map
;
258 static int bsd_simul_execs
;
259 static int bsd_pageable_map_size
;
260 __private_extern__
int execargs_cache_size
= 0;
261 __private_extern__
int execargs_free_count
= 0;
262 __private_extern__ vm_offset_t
* execargs_cache
= NULL
;
264 void bsd_exec_setup(int);
266 __private_extern__
int bootarg_vnode_cache_defeat
= 0;
269 * Prevent kernel-based ASLR from being used, for testing.
271 #if DEVELOPMENT || DEBUG
272 __private_extern__
int bootarg_disable_aslr
= 0;
276 extern int customnbuf
;
279 kern_return_t
bsd_autoconf(void);
280 void bsd_utaskbootstrap(void);
282 static void parse_bsd_args(void);
283 extern task_t bsd_init_task
;
284 extern char init_task_failure_data
[];
285 extern void time_zone_slock_init(void);
286 extern void select_wait_queue_init(void);
287 static void process_name(const char *, proc_t
);
289 static void setconf(void);
291 funnel_t
*kernel_flock
;
294 extern void sysv_shm_lock_init(void);
297 extern void sysv_sem_lock_init(void);
300 extern void sysv_msg_lock_init(void);
303 #if !defined(SECURE_KERNEL)
304 /* kmem access not enabled by default; can be changed with boot-args */
305 /* We don't need to keep this symbol around in RELEASE kernel */
310 #if defined (__i386__) || defined (__x86_64__)
311 /* MACF policy_check configuration flags; see policy_check.c for details */
312 int policy_check_flags
= 0;
314 extern int check_policy_init(int);
316 #endif /* CONFIG_MACF */
318 /* If we are using CONFIG_DTRACE */
320 extern void dtrace_postinit(void);
324 * Initialization code.
325 * Called from cold start routine as
326 * soon as a stack and segmentation
327 * have been established.
330 * hand craft 0th process
331 * call all initialization routines
332 * hand craft 1st user process
336 * Sets the name for the given task.
339 process_name(const char *s
, proc_t p
)
341 size_t length
= strlen(s
);
344 length
>= sizeof(p
->p_comm
) ? sizeof(p
->p_comm
) :
348 /* To allow these values to be patched, they're globals here */
349 #include <machine/vmparam.h>
350 struct rlimit vm_initial_limit_stack
= { DFLSSIZ
, MAXSSIZ
- PAGE_SIZE
};
351 struct rlimit vm_initial_limit_data
= { DFLDSIZ
, MAXDSIZ
};
352 struct rlimit vm_initial_limit_core
= { DFLCSIZ
, MAXCSIZ
};
354 extern thread_t
cloneproc(task_t
, proc_t
, int, int);
355 extern int (*mountroot
)(void);
357 lck_grp_t
* proc_lck_grp
;
358 lck_grp_t
* proc_slock_grp
;
359 lck_grp_t
* proc_fdmlock_grp
;
360 lck_grp_t
* proc_mlock_grp
;
361 lck_grp_attr_t
* proc_lck_grp_attr
;
362 lck_attr_t
* proc_lck_attr
;
363 lck_mtx_t
* proc_list_mlock
;
364 lck_mtx_t
* proc_klist_mlock
;
366 extern lck_mtx_t
* execargs_cache_lock
;
368 /* hook called after root is mounted XXX temporary hack */
369 void (*mountroot_post_hook
)(void);
370 void (*unmountroot_pre_hook
)(void);
373 * This function is called very early on in the Mach startup, from the
374 * function start_kernel_threads() in osfmk/kern/startup.c. It's called
375 * in the context of the current (startup) task using a call to the
376 * function kernel_thread_create() to jump into start_kernel_threads().
377 * Internally, kernel_thread_create() calls thread_create_internal(),
378 * which calls uthread_alloc(). The function of uthread_alloc() is
379 * normally to allocate a uthread structure, and fill out the uu_sigmask,
380 * uu_context fields. It skips filling these out in the case of the "task"
381 * being "kernel_task", because the order of operation is inverted. To
382 * account for that, we need to manually fill in at least the contents
383 * of the uu_context.vc_ucred field so that the uthread structure can be
384 * used like any other.
387 extern void IOServicePublishResource(const char *, boolean_t
);
394 struct vfs_context context
;
396 struct ucred temp_cred
;
397 struct posix_cred temp_pcred
;
398 #if NFSCLIENT || CONFIG_IMAGEBOOT
399 boolean_t netboot
= FALSE
;
402 #define bsd_init_kprintf(x...) /* kprintf("bsd_init: " x) */
406 kernel_flock
= funnel_alloc(KERNEL_FUNNEL
);
407 if (kernel_flock
== (funnel_t
*)0 ) {
408 panic("bsd_init: Failed to allocate kernel funnel");
413 bsd_init_kprintf("calling kmeminit\n");
416 bsd_init_kprintf("calling parse_bsd_args\n");
419 /* Initialize kauth subsystem before instancing the first credential */
420 bsd_init_kprintf("calling kauth_init\n");
423 /* Initialize process and pgrp structures. */
424 bsd_init_kprintf("calling procinit\n");
427 /* Initialize the ttys (MUST be before kminit()/bsd_autoconf()!)*/
430 kernproc
= &proc0
; /* implicitly bzero'ed */
432 /* kernel_task->proc = kernproc; */
433 set_bsdtask_info(kernel_task
,(void *)kernproc
);
435 /* give kernproc a name */
436 bsd_init_kprintf("calling process_name\n");
437 process_name("kernel_task", kernproc
);
439 /* allocate proc lock group attribute and group */
440 bsd_init_kprintf("calling lck_grp_attr_alloc_init\n");
441 proc_lck_grp_attr
= lck_grp_attr_alloc_init();
443 proc_lck_grp
= lck_grp_alloc_init("proc", proc_lck_grp_attr
);
444 #if CONFIG_FINE_LOCK_GROUPS
445 proc_slock_grp
= lck_grp_alloc_init("proc-slock", proc_lck_grp_attr
);
446 proc_fdmlock_grp
= lck_grp_alloc_init("proc-fdmlock", proc_lck_grp_attr
);
447 proc_mlock_grp
= lck_grp_alloc_init("proc-mlock", proc_lck_grp_attr
);
449 /* Allocate proc lock attribute */
450 proc_lck_attr
= lck_attr_alloc_init();
452 #if __PROC_INTERNAL_DEBUG
453 lck_attr_setdebug(proc_lck_attr
);
457 #if CONFIG_FINE_LOCK_GROUPS
458 proc_list_mlock
= lck_mtx_alloc_init(proc_mlock_grp
, proc_lck_attr
);
459 proc_klist_mlock
= lck_mtx_alloc_init(proc_mlock_grp
, proc_lck_attr
);
460 lck_mtx_init(&kernproc
->p_mlock
, proc_mlock_grp
, proc_lck_attr
);
461 lck_mtx_init(&kernproc
->p_fdmlock
, proc_fdmlock_grp
, proc_lck_attr
);
462 lck_spin_init(&kernproc
->p_slock
, proc_slock_grp
, proc_lck_attr
);
464 proc_list_mlock
= lck_mtx_alloc_init(proc_lck_grp
, proc_lck_attr
);
465 proc_klist_mlock
= lck_mtx_alloc_init(proc_lck_grp
, proc_lck_attr
);
466 lck_mtx_init(&kernproc
->p_mlock
, proc_lck_grp
, proc_lck_attr
);
467 lck_mtx_init(&kernproc
->p_fdmlock
, proc_lck_grp
, proc_lck_attr
);
468 lck_spin_init(&kernproc
->p_slock
, proc_lck_grp
, proc_lck_attr
);
471 assert(bsd_simul_execs
!= 0);
472 execargs_cache_lock
= lck_mtx_alloc_init(proc_lck_grp
, proc_lck_attr
);
473 execargs_cache_size
= bsd_simul_execs
;
474 execargs_free_count
= bsd_simul_execs
;
475 execargs_cache
= (vm_offset_t
*)kalloc(bsd_simul_execs
* sizeof(vm_offset_t
));
476 bzero(execargs_cache
, bsd_simul_execs
* sizeof(vm_offset_t
));
478 if (current_task() != kernel_task
)
479 printf("bsd_init: We have a problem, "
480 "current task is not kernel task\n");
482 bsd_init_kprintf("calling get_bsdthread_info\n");
483 ut
= (uthread_t
)get_bsdthread_info(current_thread());
487 * Initialize the MAC Framework
489 mac_policy_initbsd();
490 kernproc
->p_mac_enforce
= 0;
492 #if defined (__i386__) || defined (__x86_64__)
494 * We currently only support this on i386/x86_64, as that is the
495 * only lock code we have instrumented so far.
497 check_policy_init(policy_check_flags
);
501 /* Initialize System Override call */
502 init_system_override();
508 LIST_INSERT_HEAD(&allproc
, kernproc
, p_list
);
509 kernproc
->p_pgrp
= &pgrp0
;
510 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0
, pg_hash
);
511 LIST_INIT(&pgrp0
.pg_members
);
512 #ifdef CONFIG_FINE_LOCK_GROUPS
513 lck_mtx_init(&pgrp0
.pg_mlock
, proc_mlock_grp
, proc_lck_attr
);
515 lck_mtx_init(&pgrp0
.pg_mlock
, proc_lck_grp
, proc_lck_attr
);
517 /* There is no other bsd thread this point and is safe without pgrp lock */
518 LIST_INSERT_HEAD(&pgrp0
.pg_members
, kernproc
, p_pglist
);
519 kernproc
->p_listflag
|= P_LIST_INPGRP
;
520 kernproc
->p_pgrpid
= 0;
521 kernproc
->p_uniqueid
= 0;
523 pgrp0
.pg_session
= &session0
;
524 pgrp0
.pg_membercnt
= 1;
526 session0
.s_count
= 1;
527 session0
.s_leader
= kernproc
;
528 session0
.s_listflags
= 0;
529 #ifdef CONFIG_FINE_LOCK_GROUPS
530 lck_mtx_init(&session0
.s_mlock
, proc_mlock_grp
, proc_lck_attr
);
532 lck_mtx_init(&session0
.s_mlock
, proc_lck_grp
, proc_lck_attr
);
534 LIST_INSERT_HEAD(SESSHASH(0), &session0
, s_hash
);
538 kernproc
->p_lctx
= NULL
;
541 kernproc
->task
= kernel_task
;
543 kernproc
->p_stat
= SRUN
;
544 kernproc
->p_flag
= P_SYSTEM
;
545 kernproc
->p_lflag
= 0;
546 kernproc
->p_ladvflag
= 0;
548 #if DEVELOPMENT || DEBUG
549 if (bootarg_disable_aslr
)
550 kernproc
->p_flag
|= P_DISABLE_ASLR
;
553 kernproc
->p_nice
= NZERO
;
554 kernproc
->p_pptr
= kernproc
;
556 TAILQ_INIT(&kernproc
->p_uthlist
);
557 TAILQ_INSERT_TAIL(&kernproc
->p_uthlist
, ut
, uu_list
);
559 kernproc
->sigwait
= FALSE
;
560 kernproc
->sigwait_thread
= THREAD_NULL
;
561 kernproc
->exit_thread
= THREAD_NULL
;
562 kernproc
->p_csflags
= CS_VALID
;
565 * Create credential. This also Initializes the audit information.
567 bsd_init_kprintf("calling bzero\n");
568 bzero(&temp_cred
, sizeof(temp_cred
));
569 bzero(&temp_pcred
, sizeof(temp_pcred
));
570 temp_pcred
.cr_ngroups
= 1;
571 /* kern_proc, shouldn't call up to DS for group membership */
572 temp_pcred
.cr_flags
= CRF_NOMEMBERD
;
573 temp_cred
.cr_audit
.as_aia_p
= audit_default_aia_p
;
575 bsd_init_kprintf("calling kauth_cred_create\n");
577 * We have to label the temp cred before we create from it to
578 * properly set cr_ngroups, or the create will fail.
580 posix_cred_label(&temp_cred
, &temp_pcred
);
581 kernproc
->p_ucred
= kauth_cred_create(&temp_cred
);
583 /* update cred on proc */
584 PROC_UPDATE_CREDS_ONPROC(kernproc
);
586 /* give the (already exisiting) initial thread a reference on it */
587 bsd_init_kprintf("calling kauth_cred_ref\n");
588 kauth_cred_ref(kernproc
->p_ucred
);
589 ut
->uu_context
.vc_ucred
= kernproc
->p_ucred
;
590 ut
->uu_context
.vc_thread
= current_thread();
592 TAILQ_INIT(&kernproc
->p_aio_activeq
);
593 TAILQ_INIT(&kernproc
->p_aio_doneq
);
594 kernproc
->p_aio_total_count
= 0;
595 kernproc
->p_aio_active_count
= 0;
597 bsd_init_kprintf("calling file_lock_init\n");
601 mac_cred_label_associate_kernel(kernproc
->p_ucred
);
602 mac_task_label_update_cred (kernproc
->p_ucred
, (struct task
*) kernproc
->task
);
605 /* Create the file descriptor table. */
606 kernproc
->p_fd
= &filedesc0
;
607 filedesc0
.fd_cmask
= cmask
;
608 filedesc0
.fd_knlistsize
= -1;
609 filedesc0
.fd_knlist
= NULL
;
610 filedesc0
.fd_knhash
= NULL
;
611 filedesc0
.fd_knhashmask
= 0;
613 /* Create the limits structures. */
614 kernproc
->p_limit
= &limit0
;
615 for (i
= 0; i
< sizeof(kernproc
->p_rlimit
)/sizeof(kernproc
->p_rlimit
[0]); i
++)
616 limit0
.pl_rlimit
[i
].rlim_cur
=
617 limit0
.pl_rlimit
[i
].rlim_max
= RLIM_INFINITY
;
618 limit0
.pl_rlimit
[RLIMIT_NOFILE
].rlim_cur
= NOFILE
;
619 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_cur
= maxprocperuid
;
620 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_max
= maxproc
;
621 limit0
.pl_rlimit
[RLIMIT_STACK
] = vm_initial_limit_stack
;
622 limit0
.pl_rlimit
[RLIMIT_DATA
] = vm_initial_limit_data
;
623 limit0
.pl_rlimit
[RLIMIT_CORE
] = vm_initial_limit_core
;
624 limit0
.pl_refcnt
= 1;
626 kernproc
->p_stats
= &pstats0
;
627 kernproc
->p_sigacts
= &sigacts0
;
630 * Charge root for two processes: init and mach_init.
632 bsd_init_kprintf("calling chgproccnt\n");
633 (void)chgproccnt(0, 1);
636 * Allocate a kernel submap for pageable memory
637 * for temporary copying (execve()).
642 bsd_init_kprintf("calling kmem_suballoc\n");
643 assert(bsd_pageable_map_size
!= 0);
644 ret
= kmem_suballoc(kernel_map
,
646 (vm_size_t
)bsd_pageable_map_size
,
650 if (ret
!= KERN_SUCCESS
)
651 panic("bsd_init: Failed to allocate bsd pageable map");
655 * Initialize buffers and hash links for buffers
657 * SIDE EFFECT: Starts a thread for bcleanbuf_thread(), so must
658 * happen after a credential has been associated with
661 bsd_init_kprintf("calling bsd_bufferinit\n");
664 /* Initialize the execve() semaphore */
665 bsd_init_kprintf("calling semaphore_create\n");
667 if (ret
!= KERN_SUCCESS
)
668 panic("bsd_init: Failed to create execve semaphore");
671 * Initialize the calendar.
673 bsd_init_kprintf("calling IOKitInitializeTime\n");
674 IOKitInitializeTime();
676 bsd_init_kprintf("calling ubc_init\n");
680 * Initialize device-switches.
682 bsd_init_kprintf("calling devsw_init() \n");
685 /* Initialize the file systems. */
686 bsd_init_kprintf("calling vfsinit\n");
689 #if CONFIG_PROC_UUID_POLICY
690 /* Initial proc_uuid_policy subsystem */
691 bsd_init_kprintf("calling proc_uuid_policy_init()\n");
692 proc_uuid_policy_init();
696 /* Initialize per-CPU cache allocator */
699 /* Initialize mbuf's. */
700 bsd_init_kprintf("calling mbinit\n");
702 net_str_id_init(); /* for mbuf tags */
706 * Initializes security event auditing.
707 * XXX: Should/could this occur later?
710 bsd_init_kprintf("calling audit_init\n");
714 /* Initialize kqueues */
715 bsd_init_kprintf("calling knote_init\n");
718 /* Initialize for async IO */
719 bsd_init_kprintf("calling aio_init\n");
722 /* Initialize pipes */
723 bsd_init_kprintf("calling pipeinit\n");
726 /* Initialize SysV shm subsystem locks; the subsystem proper is
727 * initialized through a sysctl.
730 bsd_init_kprintf("calling sysv_shm_lock_init\n");
731 sysv_shm_lock_init();
734 bsd_init_kprintf("calling sysv_sem_lock_init\n");
735 sysv_sem_lock_init();
738 bsd_init_kprintf("sysv_msg_lock_init\n");
739 sysv_msg_lock_init();
741 bsd_init_kprintf("calling pshm_lock_init\n");
743 bsd_init_kprintf("calling psem_lock_init\n");
747 /* POSIX Shm and Sem */
748 bsd_init_kprintf("calling pshm_cache_init\n");
750 bsd_init_kprintf("calling psem_cache_init\n");
752 bsd_init_kprintf("calling time_zone_slock_init\n");
753 time_zone_slock_init();
754 bsd_init_kprintf("calling select_wait_queue_init\n");
755 select_wait_queue_init();
758 * Initialize protocols. Block reception of incoming packets
759 * until everything is ready.
761 bsd_init_kprintf("calling sysctl_register_fixed\n");
762 sysctl_register_fixed();
763 bsd_init_kprintf("calling sysctl_mib_init\n");
766 bsd_init_kprintf("calling dlil_init\n");
768 bsd_init_kprintf("calling proto_kpi_init\n");
770 #endif /* NETWORKING */
772 bsd_init_kprintf("calling socketinit\n");
774 bsd_init_kprintf("calling domaininit\n");
779 #endif /* FLOW_DIVERT */
782 kernproc
->p_fd
->fd_cdir
= NULL
;
783 kernproc
->p_fd
->fd_rdir
= NULL
;
786 #ifndef CONFIG_MEMORYSTATUS
787 #error "CONFIG_FREEZE defined without matching CONFIG_MEMORYSTATUS"
789 /* Initialise background freezing */
790 bsd_init_kprintf("calling memorystatus_freeze_init\n");
791 memorystatus_freeze_init();
794 #if CONFIG_MEMORYSTATUS
795 /* Initialize kernel memory status notifications */
796 bsd_init_kprintf("calling memorystatus_init\n");
798 #endif /* CONFIG_MEMORYSTATUS */
801 /* Initialize kernel profiling. */
805 bsd_init_kprintf("calling bsd_autoconf\n");
813 * We attach the loopback interface *way* down here to ensure
814 * it happens after autoconf(), otherwise it becomes the
815 * "primary" interface.
819 bsd_init_kprintf("calling loopattach\n");
820 loopattach(); /* XXX */
823 /* Initialize gif interface (after lo0) */
828 /* Initialize packet filter log interface */
833 /* Register the built-in dlil ethernet interface family */
834 bsd_init_kprintf("calling ether_family_init\n");
839 /* Call any kext code that wants to run just after network init */
840 bsd_init_kprintf("calling net_init_run\n");
843 /* register user tunnel kernel control handler */
844 utun_register_control();
846 ipsec_register_control();
850 #endif /* NETWORKING */
852 bsd_init_kprintf("calling vnode_pager_bootstrap\n");
853 vnode_pager_bootstrap();
855 /* XXX Hack for early debug stop */
856 printf("\nabout to sleep for 10 seconds\n");
857 IOSleep( 10 * 1000 );
858 /* Debugger("hello"); */
861 bsd_init_kprintf("calling inittodr\n");
864 /* Mount the root file system. */
868 bsd_init_kprintf("calling setconf\n");
871 netboot
= (mountroot
== netboot_mountroot
);
874 bsd_init_kprintf("vfs_mountroot\n");
875 if (0 == (err
= vfs_mountroot()))
877 rootdevice
[0] = '\0';
880 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
881 vc_progress_set(FALSE
, 0);
883 printf("bsd_init: failed to mount network root, error %d, %s\n",
884 err
, PE_boot_args());
885 printf("We are hanging here...\n");
891 printf("cannot mount root, errno = %d\n", err
);
892 boothowto
|= RB_ASKNAME
;
895 IOSecureBSDRoot(rootdevice
);
897 context
.vc_thread
= current_thread();
898 context
.vc_ucred
= kernproc
->p_ucred
;
899 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
901 bsd_init_kprintf("calling VFS_ROOT\n");
902 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
903 if (VFS_ROOT(mountlist
.tqh_first
, &rootvnode
, &context
))
904 panic("bsd_init: cannot find root vnode: %s", PE_boot_args());
905 rootvnode
->v_flag
|= VROOT
;
906 (void)vnode_ref(rootvnode
);
907 (void)vnode_put(rootvnode
);
908 filedesc0
.fd_cdir
= rootvnode
;
915 /* post mount setup */
916 if ((err
= netboot_setup()) != 0) {
917 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
918 vc_progress_set(FALSE
, 0);
920 printf("bsd_init: NetBoot could not find root, error %d: %s\n",
921 err
, PE_boot_args());
922 printf("We are hanging here...\n");
933 * See if a system disk image is present. If so, mount it and
934 * switch the root vnode to point to it
936 if (netboot
== FALSE
&& imageboot_needed()) {
938 * An image was found. No turning back: we're booted
939 * with a kernel from the disk image.
943 #endif /* CONFIG_IMAGEBOOT */
945 /* set initial time; all other resource data is already zero'ed */
946 microtime_with_abstime(&kernproc
->p_start
, &kernproc
->p_stats
->ps_start
);
950 char mounthere
[] = "/dev"; /* !const because of internal casting */
952 bsd_init_kprintf("calling devfs_kernel_mount\n");
953 devfs_kernel_mount(mounthere
);
957 /* Initialize signal state for process 0. */
958 bsd_init_kprintf("calling siginit\n");
961 bsd_init_kprintf("calling bsd_utaskbootstrap\n");
962 bsd_utaskbootstrap();
964 #if defined(__LP64__)
965 kernproc
->p_flag
|= P_LP64
;
968 pal_kernel_announce();
970 bsd_init_kprintf("calling mountroot_post_hook\n");
972 /* invoke post-root-mount hook */
973 if (mountroot_post_hook
!= NULL
)
974 mountroot_post_hook();
977 consider_zone_gc(FALSE
);
980 bsd_init_kprintf("done\n");
984 /* Called with kernel funnel held */
988 proc_t p
= current_proc();
992 process_name("init", p
);
996 thread
= current_thread();
997 (void) host_set_exception_ports(host_priv_self(),
998 EXC_MASK_ALL
& ~(EXC_MASK_RPC_ALERT
),//pilotfish (shark) needs this port
999 (mach_port_t
) ux_exception_port
,
1000 EXCEPTION_DEFAULT
| MACH_EXCEPTION_CODES
,
1003 ut
= (uthread_t
)get_bsdthread_info(thread
);
1005 bsd_init_task
= get_threadtask(thread
);
1006 init_task_failure_data
[0] = 0;
1009 mac_cred_label_associate_user(p
->p_ucred
);
1010 mac_task_label_update_cred (p
->p_ucred
, (struct task
*) p
->task
);
1012 load_init_program(p
);
1019 kprintf("bsd_autoconf: calling kminit\n");
1023 * Early startup for bsd pseudodevices.
1026 struct pseudo_init
*pi
;
1028 for (pi
= pseudo_inits
; pi
->ps_func
; pi
++)
1029 (*pi
->ps_func
) (pi
->ps_count
);
1032 return( IOKitBSDInit());
1036 #include <sys/disklabel.h> /* for MAXPARTITIONS */
1045 * calls into IOKit can generate networking registrations
1046 * which needs to be under network funnel. Right thing to do
1047 * here is to drop the funnel alltogether and regrab it afterwards
1049 err
= IOFindBSDRoot(rootdevice
, sizeof(rootdevice
), &rootdev
, &flags
);
1051 printf("setconf: IOFindBSDRoot returned an error (%d);"
1052 "setting rootdevice to 'sd0a'.\n", err
); /* XXX DEBUG TEMP */
1053 rootdev
= makedev( 6, 0 );
1054 strlcpy(rootdevice
, "sd0a", sizeof(rootdevice
));
1060 /* network device */
1061 mountroot
= netboot_mountroot
;
1064 /* otherwise have vfs determine root filesystem */
1073 bsd_utaskbootstrap(void)
1079 * Clone the bootstrap process from the kernel process, without
1080 * inheriting either task characteristics or memory from the kernel;
1082 thread
= cloneproc(TASK_NULL
, kernproc
, FALSE
, TRUE
);
1084 /* Hold the reference as it will be dropped during shutdown */
1085 initproc
= proc_find(1);
1086 #if __PROC_INTERNAL_DEBUG
1087 if (initproc
== PROC_NULL
)
1088 panic("bsd_utaskbootstrap: initproc not set\n");
1091 * Since we aren't going back out the normal way to our parent,
1092 * we have to drop the transition locks explicitly.
1094 proc_signalend(initproc
, 0);
1095 proc_transend(initproc
, 0);
1097 ut
= (struct uthread
*)get_bsdthread_info(thread
);
1099 act_set_astbsd(thread
);
1100 (void) thread_resume(thread
);
1104 parse_bsd_args(void)
1109 if ( PE_parse_boot_argn("-s", namep
, sizeof (namep
)))
1110 boothowto
|= RB_SINGLE
;
1112 if (PE_parse_boot_argn("-b", namep
, sizeof (namep
)))
1113 boothowto
|= RB_NOBOOTRC
;
1115 if (PE_parse_boot_argn("-x", namep
, sizeof (namep
))) /* safe boot */
1116 boothowto
|= RB_SAFEBOOT
;
1118 /* disable vnode_cache_is_authorized() by setting vnode_cache_defeat */
1119 if (PE_parse_boot_argn("-vnode_cache_defeat", namep
, sizeof (namep
)))
1120 bootarg_vnode_cache_defeat
= 1;
1122 #if DEVELOPMENT || DEBUG
1123 if (PE_parse_boot_argn("-disable_aslr", namep
, sizeof (namep
)))
1124 bootarg_disable_aslr
= 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
));
1137 #if defined (__i386__) || defined (__x86_64__)
1138 PE_parse_boot_argn("policy_check", &policy_check_flags
, sizeof (policy_check_flags
));
1140 #endif /* CONFIG_MACF */
1142 if (PE_parse_boot_argn("msgbuf", &msgbuf
, sizeof (msgbuf
))) {
1143 log_setsize(msgbuf
);
1146 if (PE_parse_boot_argn("-novfscache", namep
, sizeof(namep
))) {
1153 bsd_exec_setup(int scale
)
1159 bsd_simul_execs
= BSD_SIMUL_EXECS
;
1163 bsd_simul_execs
= 65;
1167 bsd_simul_execs
= 129;
1171 bsd_simul_execs
= 257;
1174 bsd_simul_execs
= 513;
1178 bsd_pageable_map_size
= (bsd_simul_execs
* BSD_PAGEABLE_SIZE_PER_EXEC
);