| 1 | /* |
| 2 | * Copyright (c) 2000-2015 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 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. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 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. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | * |
| 28 | * |
| 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. |
| 36 | * |
| 37 | * Redistribution and use in source and binary forms, with or without |
| 38 | * modification, are permitted provided that the following conditions |
| 39 | * are met: |
| 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. |
| 52 | * |
| 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 |
| 63 | * SUCH DAMAGE. |
| 64 | * |
| 65 | * @(#)init_main.c 8.16 (Berkeley) 5/14/95 |
| 66 | */ |
| 67 | |
| 68 | /* |
| 69 | * |
| 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. |
| 74 | */ |
| 75 | /* |
| 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, |
| 79 | * Version 2.0. |
| 80 | */ |
| 81 | |
| 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> |
| 90 | #include <sys/conf.h> |
| 91 | #include <sys/buf_internal.h> |
| 92 | #include <sys/clist.h> |
| 93 | #include <sys/user.h> |
| 94 | #include <sys/time.h> |
| 95 | #include <sys/systm.h> |
| 96 | #include <sys/mman.h> |
| 97 | #include <sys/kasl.h> |
| 98 | |
| 99 | #include <security/audit/audit.h> |
| 100 | |
| 101 | #include <sys/malloc.h> |
| 102 | #include <sys/dkstat.h> |
| 103 | #include <sys/codesign.h> |
| 104 | |
| 105 | #include <kern/startup.h> |
| 106 | #include <kern/thread.h> |
| 107 | #include <kern/task.h> |
| 108 | #include <kern/ast.h> |
| 109 | #include <kern/kalloc.h> |
| 110 | #include <kern/ux_handler.h> /* for ux_handler_setup() */ |
| 111 | |
| 112 | #include <mach/vm_param.h> |
| 113 | |
| 114 | #include <vm/vm_map.h> |
| 115 | #include <vm/vm_kern.h> |
| 116 | |
| 117 | #include <sys/reboot.h> |
| 118 | #include <dev/busvar.h> /* for pseudo_inits */ |
| 119 | #include <sys/kdebug.h> |
| 120 | #include <sys/monotonic.h> |
| 121 | #include <sys/reason.h> |
| 122 | |
| 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 <sys/ubc_internal.h> /* for ubc_init() */ |
| 131 | #include <sys/mcache.h> /* for mcache_init() */ |
| 132 | #include <sys/mbuf.h> /* for mbinit() */ |
| 133 | #include <sys/event.h> /* for knote_init() */ |
| 134 | #include <sys/eventhandler.h> /* for eventhandler_init() */ |
| 135 | #include <sys/kern_memorystatus.h> /* for memorystatus_init() */ |
| 136 | #include <sys/aio_kern.h> /* for aio_init() */ |
| 137 | #include <sys/semaphore.h> /* for psem_cache_init() */ |
| 138 | #include <net/dlil.h> /* for dlil_init() */ |
| 139 | #include <net/kpi_protocol.h> /* for proto_kpi_init() */ |
| 140 | #include <net/iptap.h> /* for iptap_init() */ |
| 141 | #include <sys/pipe.h> /* for pipeinit() */ |
| 142 | #include <sys/socketvar.h> /* for socketinit() */ |
| 143 | #include <sys/protosw.h> /* for domaininit() */ |
| 144 | #include <kern/sched_prim.h> /* for thread_wakeup() */ |
| 145 | #include <net/if_ether.h> /* for ether_family_init() */ |
| 146 | #include <net/if_gif.h> /* for gif_init() */ |
| 147 | #include <vm/vm_protos.h> /* for vnode_pager_bootstrap() */ |
| 148 | #include <miscfs/devfs/devfsdefs.h> /* for devfs_kernel_mount() */ |
| 149 | #include <vm/vm_kern.h> /* for kmem_suballoc() */ |
| 150 | #include <sys/semaphore.h> /* for psem_lock_init() */ |
| 151 | #include <sys/msgbuf.h> /* for log_setsize() */ |
| 152 | #include <sys/tty.h> /* for tty_init() */ |
| 153 | #include <sys/proc_uuid_policy.h> /* proc_uuid_policy_init() */ |
| 154 | #include <netinet/flow_divert.h> /* flow_divert_init() */ |
| 155 | #include <net/content_filter.h> /* for cfil_init() */ |
| 156 | #include <net/necp.h> /* for necp_init() */ |
| 157 | #include <net/network_agent.h> /* for netagent_init() */ |
| 158 | #include <net/packet_mangler.h> /* for pkt_mnglr_init() */ |
| 159 | #include <net/if_utun.h> /* for utun_register_control() */ |
| 160 | #include <net/if_ipsec.h> /* for ipsec_register_control() */ |
| 161 | #include <net/net_str_id.h> /* for net_str_id_init() */ |
| 162 | #include <net/netsrc.h> /* for netsrc_init() */ |
| 163 | #include <net/ntstat.h> /* for nstat_init() */ |
| 164 | #include <netinet/tcp_cc.h> /* for tcp_cc_init() */ |
| 165 | #include <netinet/mptcp_var.h> /* for mptcp_control_register() */ |
| 166 | #include <net/nwk_wq.h> /* for nwk_wq_init */ |
| 167 | #include <kern/assert.h> /* for assert() */ |
| 168 | #include <sys/kern_overrides.h> /* for init_system_override() */ |
| 169 | |
| 170 | #include <net/init.h> |
| 171 | |
| 172 | #if CONFIG_MACF |
| 173 | #include <security/mac_framework.h> |
| 174 | #include <security/mac_internal.h> /* mac_init_bsd() */ |
| 175 | #include <security/mac_mach_internal.h> /* mac_update_task_label() */ |
| 176 | #endif |
| 177 | |
| 178 | #include <machine/exec.h> |
| 179 | |
| 180 | #if NFSCLIENT |
| 181 | #include <sys/netboot.h> |
| 182 | #endif |
| 183 | |
| 184 | #if CONFIG_IMAGEBOOT |
| 185 | #include <sys/imageboot.h> |
| 186 | #endif |
| 187 | |
| 188 | #if PFLOG |
| 189 | #include <net/if_pflog.h> |
| 190 | #endif |
| 191 | |
| 192 | |
| 193 | #include <pexpert/pexpert.h> |
| 194 | #include <machine/pal_routines.h> |
| 195 | #include <console/video_console.h> |
| 196 | |
| 197 | #if CONFIG_XNUPOST |
| 198 | #include <tests/xnupost.h> |
| 199 | #endif |
| 200 | |
| 201 | void * get_user_regs(thread_t); /* XXX kludge for <machine/thread.h> */ |
| 202 | void IOKitInitializeTime(void); /* XXX */ |
| 203 | void IOSleep(unsigned int); /* XXX */ |
| 204 | void loopattach(void); /* XXX */ |
| 205 | |
| 206 | const char copyright[] = |
| 207 | "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t" |
| 208 | "The Regents of the University of California. " |
| 209 | "All rights reserved.\n\n" ; |
| 210 | |
| 211 | /* Components of the first process -- never freed. */ |
| 212 | struct proc proc0; |
| 213 | struct session session0; |
| 214 | struct pgrp pgrp0; |
| 215 | struct filedesc filedesc0; |
| 216 | struct plimit limit0; |
| 217 | struct pstats pstats0; |
| 218 | struct sigacts sigacts0; |
| 219 | proc_t kernproc; |
| 220 | proc_t initproc; |
| 221 | |
| 222 | long tk_cancc; |
| 223 | long tk_nin; |
| 224 | long tk_nout; |
| 225 | long tk_rawcc; |
| 226 | |
| 227 | int lock_trace = 0; |
| 228 | /* Global variables to make pstat happy. We do swapping differently */ |
| 229 | int nswdev, nswap; |
| 230 | int nswapmap; |
| 231 | void *swapmap; |
| 232 | struct swdevt swdevt[1]; |
| 233 | |
| 234 | dev_t rootdev; /* device of the root */ |
| 235 | dev_t dumpdev; /* device to take dumps on */ |
| 236 | long dumplo; /* offset into dumpdev */ |
| 237 | long hostid; |
| 238 | char hostname[MAXHOSTNAMELEN]; |
| 239 | int hostnamelen; |
| 240 | char domainname[MAXDOMNAMELEN]; |
| 241 | int domainnamelen; |
| 242 | |
| 243 | char rootdevice[DEVMAXNAMESIZE]; |
| 244 | |
| 245 | #if KMEMSTATS |
| 246 | struct kmemstats kmemstats[M_LAST]; |
| 247 | #endif |
| 248 | |
| 249 | struct vnode *rootvp; |
| 250 | int boothowto = RB_DEBUG; |
| 251 | int minimalboot = 0; |
| 252 | #if CONFIG_EMBEDDED |
| 253 | int darkboot = 0; |
| 254 | #endif |
| 255 | |
| 256 | #if PROC_REF_DEBUG |
| 257 | __private_extern__ int proc_ref_tracking_disabled = 0; /* disable panics on leaked proc refs across syscall boundary */ |
| 258 | #endif |
| 259 | |
| 260 | #if OS_REASON_DEBUG |
| 261 | __private_extern__ int os_reason_debug_disabled = 0; /* disable asserts for when we fail to allocate OS reasons */ |
| 262 | #endif |
| 263 | |
| 264 | extern kern_return_t IOFindBSDRoot(char *, unsigned int, dev_t *, u_int32_t *); |
| 265 | extern void IOSecureBSDRoot(const char * rootName); |
| 266 | extern kern_return_t IOKitBSDInit(void ); |
| 267 | extern void kminit(void); |
| 268 | extern void file_lock_init(void); |
| 269 | extern void kmeminit(void); |
| 270 | extern void bsd_bufferinit(void); |
| 271 | extern void oslog_setsize(int size); |
| 272 | extern void throttle_init(void); |
| 273 | extern void acct_init(void); |
| 274 | |
| 275 | extern int serverperfmode; |
| 276 | extern int ncl; |
| 277 | |
| 278 | vm_map_t bsd_pageable_map; |
| 279 | vm_map_t mb_map; |
| 280 | |
| 281 | static int bsd_simul_execs; |
| 282 | static int bsd_pageable_map_size; |
| 283 | __private_extern__ int execargs_cache_size = 0; |
| 284 | __private_extern__ int execargs_free_count = 0; |
| 285 | __private_extern__ vm_offset_t * execargs_cache = NULL; |
| 286 | |
| 287 | void bsd_exec_setup(int); |
| 288 | |
| 289 | #if __arm64__ |
| 290 | __private_extern__ int bootarg_no64exec = 0; |
| 291 | #endif |
| 292 | #if __x86_64__ |
| 293 | __private_extern__ int bootarg_no32exec = 0; |
| 294 | #endif |
| 295 | __private_extern__ int bootarg_vnode_cache_defeat = 0; |
| 296 | |
| 297 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) |
| 298 | __private_extern__ int bootarg_no_vnode_jetsam = 0; |
| 299 | #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ |
| 300 | |
| 301 | /* |
| 302 | * Prevent kernel-based ASLR from being used, for testing. |
| 303 | */ |
| 304 | #if DEVELOPMENT || DEBUG |
| 305 | __private_extern__ int bootarg_disable_aslr = 0; |
| 306 | #endif |
| 307 | |
| 308 | /* |
| 309 | * Allow an alternate dyld to be used for testing. |
| 310 | */ |
| 311 | |
| 312 | #if DEVELOPMENT || DEBUG |
| 313 | char dyld_alt_path[MAXPATHLEN]; |
| 314 | int use_alt_dyld = 0; |
| 315 | #endif |
| 316 | |
| 317 | int cmask = CMASK; |
| 318 | extern int customnbuf; |
| 319 | |
| 320 | kern_return_t bsd_autoconf(void); |
| 321 | void bsd_utaskbootstrap(void); |
| 322 | |
| 323 | static void parse_bsd_args(void); |
| 324 | #if CONFIG_DEV_KMEM |
| 325 | extern void dev_kmem_init(void); |
| 326 | #endif |
| 327 | extern void time_zone_slock_init(void); |
| 328 | extern void select_waitq_init(void); |
| 329 | static void process_name(const char *, proc_t); |
| 330 | |
| 331 | static void setconf(void); |
| 332 | |
| 333 | #if SYSV_SHM |
| 334 | extern void sysv_shm_lock_init(void); |
| 335 | #endif |
| 336 | #if SYSV_SEM |
| 337 | extern void sysv_sem_lock_init(void); |
| 338 | #endif |
| 339 | #if SYSV_MSG |
| 340 | extern void sysv_msg_lock_init(void); |
| 341 | #endif |
| 342 | |
| 343 | extern void ulock_initialize(void); |
| 344 | |
| 345 | #if CONFIG_MACF |
| 346 | #if defined (__i386__) || defined (__x86_64__) |
| 347 | /* MACF policy_check configuration flags; see policy_check.c for details */ |
| 348 | int policy_check_flags = 0; |
| 349 | |
| 350 | extern int check_policy_init(int); |
| 351 | #endif |
| 352 | #endif /* CONFIG_MACF */ |
| 353 | |
| 354 | /* If we are using CONFIG_DTRACE */ |
| 355 | #if CONFIG_DTRACE |
| 356 | extern void dtrace_postinit(void); |
| 357 | #endif |
| 358 | |
| 359 | /* |
| 360 | * Initialization code. |
| 361 | * Called from cold start routine as |
| 362 | * soon as a stack and segmentation |
| 363 | * have been established. |
| 364 | * Functions: |
| 365 | * turn on clock |
| 366 | * hand craft 0th process |
| 367 | * call all initialization routines |
| 368 | * hand craft 1st user process |
| 369 | */ |
| 370 | |
| 371 | /* |
| 372 | * Sets the name for the given task. |
| 373 | */ |
| 374 | static void |
| 375 | process_name(const char *s, proc_t p) |
| 376 | { |
| 377 | strlcpy(p->p_comm, s, sizeof(p->p_comm)); |
| 378 | strlcpy(p->p_name, s, sizeof(p->p_name)); |
| 379 | } |
| 380 | |
| 381 | /* To allow these values to be patched, they're globals here */ |
| 382 | #include <machine/vmparam.h> |
| 383 | struct rlimit vm_initial_limit_stack = { DFLSSIZ, MAXSSIZ - PAGE_MAX_SIZE }; |
| 384 | struct rlimit vm_initial_limit_data = { DFLDSIZ, MAXDSIZ }; |
| 385 | struct rlimit vm_initial_limit_core = { DFLCSIZ, MAXCSIZ }; |
| 386 | |
| 387 | extern thread_t cloneproc(task_t, coalition_t, proc_t, int, int); |
| 388 | extern int (*mountroot)(void); |
| 389 | |
| 390 | lck_grp_t * proc_lck_grp; |
| 391 | lck_grp_t * proc_slock_grp; |
| 392 | lck_grp_t * proc_fdmlock_grp; |
| 393 | lck_grp_t * proc_kqhashlock_grp; |
| 394 | lck_grp_t * proc_knhashlock_grp; |
| 395 | lck_grp_t * proc_ucred_mlock_grp; |
| 396 | lck_grp_t * proc_mlock_grp; |
| 397 | lck_grp_attr_t * proc_lck_grp_attr; |
| 398 | lck_attr_t * proc_lck_attr; |
| 399 | lck_mtx_t * proc_list_mlock; |
| 400 | lck_mtx_t * proc_klist_mlock; |
| 401 | |
| 402 | #if CONFIG_XNUPOST |
| 403 | lck_grp_t * sysctl_debug_test_stackshot_owner_grp; |
| 404 | lck_mtx_t * sysctl_debug_test_stackshot_owner_init_mtx; |
| 405 | #endif /* !CONFIG_XNUPOST */ |
| 406 | |
| 407 | extern lck_mtx_t * execargs_cache_lock; |
| 408 | |
| 409 | /* hook called after root is mounted XXX temporary hack */ |
| 410 | void (*mountroot_post_hook)(void); |
| 411 | void (*unmountroot_pre_hook)(void); |
| 412 | |
| 413 | /* |
| 414 | * This function is called before IOKit initialization, so that globals |
| 415 | * like the sysctl tree are initialized before kernel extensions |
| 416 | * are started (since they may want to register sysctls |
| 417 | */ |
| 418 | void |
| 419 | bsd_early_init(void) |
| 420 | { |
| 421 | sysctl_early_init(); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * This function is called very early on in the Mach startup, from the |
| 426 | * function start_kernel_threads() in osfmk/kern/startup.c. It's called |
| 427 | * in the context of the current (startup) task using a call to the |
| 428 | * function kernel_thread_create() to jump into start_kernel_threads(). |
| 429 | * Internally, kernel_thread_create() calls thread_create_internal(), |
| 430 | * which calls uthread_alloc(). The function of uthread_alloc() is |
| 431 | * normally to allocate a uthread structure, and fill out the uu_sigmask, |
| 432 | * uu_context fields. It skips filling these out in the case of the "task" |
| 433 | * being "kernel_task", because the order of operation is inverted. To |
| 434 | * account for that, we need to manually fill in at least the contents |
| 435 | * of the uu_context.vc_ucred field so that the uthread structure can be |
| 436 | * used like any other. |
| 437 | */ |
| 438 | |
| 439 | void |
| 440 | bsd_init(void) |
| 441 | { |
| 442 | struct uthread *ut; |
| 443 | unsigned int i; |
| 444 | struct vfs_context context; |
| 445 | kern_return_t ret; |
| 446 | struct ucred temp_cred; |
| 447 | struct posix_cred temp_pcred; |
| 448 | #if NFSCLIENT || CONFIG_IMAGEBOOT |
| 449 | boolean_t netboot = FALSE; |
| 450 | #endif |
| 451 | |
| 452 | #define bsd_init_kprintf(x...) /* kprintf("bsd_init: " x) */ |
| 453 | |
| 454 | throttle_init(); |
| 455 | |
| 456 | printf(copyright); |
| 457 | |
| 458 | bsd_init_kprintf("calling kmeminit\n" ); |
| 459 | kmeminit(); |
| 460 | |
| 461 | bsd_init_kprintf("calling parse_bsd_args\n" ); |
| 462 | parse_bsd_args(); |
| 463 | |
| 464 | #if CONFIG_DEV_KMEM |
| 465 | bsd_init_kprintf("calling dev_kmem_init\n" ); |
| 466 | dev_kmem_init(); |
| 467 | #endif |
| 468 | |
| 469 | /* Initialize kauth subsystem before instancing the first credential */ |
| 470 | bsd_init_kprintf("calling kauth_init\n" ); |
| 471 | kauth_init(); |
| 472 | |
| 473 | /* Initialize process and pgrp structures. */ |
| 474 | bsd_init_kprintf("calling procinit\n" ); |
| 475 | procinit(); |
| 476 | |
| 477 | /* Initialize the ttys (MUST be before kminit()/bsd_autoconf()!)*/ |
| 478 | tty_init(); |
| 479 | |
| 480 | kernproc = &proc0; /* implicitly bzero'ed */ |
| 481 | |
| 482 | /* kernel_task->proc = kernproc; */ |
| 483 | set_bsdtask_info(kernel_task,(void *)kernproc); |
| 484 | |
| 485 | /* give kernproc a name */ |
| 486 | bsd_init_kprintf("calling process_name\n" ); |
| 487 | process_name("kernel_task" , kernproc); |
| 488 | |
| 489 | /* allocate proc lock group attribute and group */ |
| 490 | bsd_init_kprintf("calling lck_grp_attr_alloc_init\n" ); |
| 491 | proc_lck_grp_attr= lck_grp_attr_alloc_init(); |
| 492 | |
| 493 | proc_lck_grp = lck_grp_alloc_init("proc" , proc_lck_grp_attr); |
| 494 | |
| 495 | #if CONFIG_FINE_LOCK_GROUPS |
| 496 | proc_slock_grp = lck_grp_alloc_init("proc-slock" , proc_lck_grp_attr); |
| 497 | proc_ucred_mlock_grp = lck_grp_alloc_init("proc-ucred-mlock" , proc_lck_grp_attr); |
| 498 | proc_mlock_grp = lck_grp_alloc_init("proc-mlock" , proc_lck_grp_attr); |
| 499 | proc_fdmlock_grp = lck_grp_alloc_init("proc-fdmlock" , proc_lck_grp_attr); |
| 500 | #endif |
| 501 | proc_kqhashlock_grp = lck_grp_alloc_init("proc-kqhashlock" , proc_lck_grp_attr); |
| 502 | proc_knhashlock_grp = lck_grp_alloc_init("proc-knhashlock" , proc_lck_grp_attr); |
| 503 | #if CONFIG_XNUPOST |
| 504 | sysctl_debug_test_stackshot_owner_grp = lck_grp_alloc_init("test-stackshot-owner-grp" , LCK_GRP_ATTR_NULL); |
| 505 | sysctl_debug_test_stackshot_owner_init_mtx = lck_mtx_alloc_init( |
| 506 | sysctl_debug_test_stackshot_owner_grp, |
| 507 | LCK_ATTR_NULL); |
| 508 | #endif /* !CONFIG_XNUPOST */ |
| 509 | /* Allocate proc lock attribute */ |
| 510 | proc_lck_attr = lck_attr_alloc_init(); |
| 511 | #if 0 |
| 512 | #if __PROC_INTERNAL_DEBUG |
| 513 | lck_attr_setdebug(proc_lck_attr); |
| 514 | #endif |
| 515 | #endif |
| 516 | |
| 517 | #if CONFIG_FINE_LOCK_GROUPS |
| 518 | proc_list_mlock = lck_mtx_alloc_init(proc_mlock_grp, proc_lck_attr); |
| 519 | proc_klist_mlock = lck_mtx_alloc_init(proc_mlock_grp, proc_lck_attr); |
| 520 | lck_mtx_init(&kernproc->p_mlock, proc_mlock_grp, proc_lck_attr); |
| 521 | lck_mtx_init(&kernproc->p_fdmlock, proc_fdmlock_grp, proc_lck_attr); |
| 522 | lck_mtx_init(&kernproc->p_ucred_mlock, proc_ucred_mlock_grp, proc_lck_attr); |
| 523 | lck_spin_init(&kernproc->p_slock, proc_slock_grp, proc_lck_attr); |
| 524 | #else |
| 525 | proc_list_mlock = lck_mtx_alloc_init(proc_lck_grp, proc_lck_attr); |
| 526 | proc_klist_mlock = lck_mtx_alloc_init(proc_lck_grp, proc_lck_attr); |
| 527 | lck_mtx_init(&kernproc->p_mlock, proc_lck_grp, proc_lck_attr); |
| 528 | lck_mtx_init(&kernproc->p_fdmlock, proc_lck_grp, proc_lck_attr); |
| 529 | lck_mtx_init(&kernproc->p_ucred_mlock, proc_lck_grp, proc_lck_attr); |
| 530 | lck_spin_init(&kernproc->p_slock, proc_lck_grp, proc_lck_attr); |
| 531 | #endif |
| 532 | |
| 533 | assert(bsd_simul_execs != 0); |
| 534 | execargs_cache_lock = lck_mtx_alloc_init(proc_lck_grp, proc_lck_attr); |
| 535 | execargs_cache_size = bsd_simul_execs; |
| 536 | execargs_free_count = bsd_simul_execs; |
| 537 | execargs_cache = (vm_offset_t *)kalloc(bsd_simul_execs * sizeof(vm_offset_t)); |
| 538 | bzero(execargs_cache, bsd_simul_execs * sizeof(vm_offset_t)); |
| 539 | |
| 540 | if (current_task() != kernel_task) |
| 541 | printf("bsd_init: We have a problem, " |
| 542 | "current task is not kernel task\n" ); |
| 543 | |
| 544 | bsd_init_kprintf("calling get_bsdthread_info\n" ); |
| 545 | ut = (uthread_t)get_bsdthread_info(current_thread()); |
| 546 | |
| 547 | #if CONFIG_MACF |
| 548 | /* |
| 549 | * Initialize the MAC Framework |
| 550 | */ |
| 551 | mac_policy_initbsd(); |
| 552 | |
| 553 | #if defined (__i386__) || defined (__x86_64__) |
| 554 | /* |
| 555 | * We currently only support this on i386/x86_64, as that is the |
| 556 | * only lock code we have instrumented so far. |
| 557 | */ |
| 558 | check_policy_init(policy_check_flags); |
| 559 | #endif |
| 560 | #endif /* MAC */ |
| 561 | |
| 562 | ulock_initialize(); |
| 563 | |
| 564 | /* |
| 565 | * Create process 0. |
| 566 | */ |
| 567 | proc_list_lock(); |
| 568 | LIST_INSERT_HEAD(&allproc, kernproc, p_list); |
| 569 | kernproc->p_pgrp = &pgrp0; |
| 570 | LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); |
| 571 | LIST_INIT(&pgrp0.pg_members); |
| 572 | #ifdef CONFIG_FINE_LOCK_GROUPS |
| 573 | lck_mtx_init(&pgrp0.pg_mlock, proc_mlock_grp, proc_lck_attr); |
| 574 | #else |
| 575 | lck_mtx_init(&pgrp0.pg_mlock, proc_lck_grp, proc_lck_attr); |
| 576 | #endif |
| 577 | /* There is no other bsd thread this point and is safe without pgrp lock */ |
| 578 | LIST_INSERT_HEAD(&pgrp0.pg_members, kernproc, p_pglist); |
| 579 | kernproc->p_listflag |= P_LIST_INPGRP; |
| 580 | kernproc->p_pgrpid = 0; |
| 581 | kernproc->p_uniqueid = 0; |
| 582 | |
| 583 | pgrp0.pg_session = &session0; |
| 584 | pgrp0.pg_membercnt = 1; |
| 585 | |
| 586 | session0.s_count = 1; |
| 587 | session0.s_leader = kernproc; |
| 588 | session0.s_listflags = 0; |
| 589 | #ifdef CONFIG_FINE_LOCK_GROUPS |
| 590 | lck_mtx_init(&session0.s_mlock, proc_mlock_grp, proc_lck_attr); |
| 591 | #else |
| 592 | lck_mtx_init(&session0.s_mlock, proc_lck_grp, proc_lck_attr); |
| 593 | #endif |
| 594 | LIST_INSERT_HEAD(SESSHASH(0), &session0, s_hash); |
| 595 | proc_list_unlock(); |
| 596 | |
| 597 | #if CONFIG_PERSONAS |
| 598 | kernproc->p_persona = NULL; |
| 599 | #endif |
| 600 | |
| 601 | kernproc->task = kernel_task; |
| 602 | |
| 603 | kernproc->p_stat = SRUN; |
| 604 | kernproc->p_flag = P_SYSTEM; |
| 605 | kernproc->p_lflag = 0; |
| 606 | kernproc->p_ladvflag = 0; |
| 607 | |
| 608 | #if defined(__LP64__) |
| 609 | kernproc->p_flag |= P_LP64; |
| 610 | #endif |
| 611 | |
| 612 | #if DEVELOPMENT || DEBUG |
| 613 | if (bootarg_disable_aslr) |
| 614 | kernproc->p_flag |= P_DISABLE_ASLR; |
| 615 | #endif |
| 616 | |
| 617 | kernproc->p_nice = NZERO; |
| 618 | kernproc->p_pptr = kernproc; |
| 619 | |
| 620 | TAILQ_INIT(&kernproc->p_uthlist); |
| 621 | TAILQ_INSERT_TAIL(&kernproc->p_uthlist, ut, uu_list); |
| 622 | |
| 623 | kernproc->sigwait = FALSE; |
| 624 | kernproc->sigwait_thread = THREAD_NULL; |
| 625 | kernproc->exit_thread = THREAD_NULL; |
| 626 | kernproc->p_csflags = CS_VALID; |
| 627 | |
| 628 | /* |
| 629 | * Create credential. This also Initializes the audit information. |
| 630 | */ |
| 631 | bsd_init_kprintf("calling bzero\n" ); |
| 632 | bzero(&temp_cred, sizeof(temp_cred)); |
| 633 | bzero(&temp_pcred, sizeof(temp_pcred)); |
| 634 | temp_pcred.cr_ngroups = 1; |
| 635 | /* kern_proc, shouldn't call up to DS for group membership */ |
| 636 | temp_pcred.cr_flags = CRF_NOMEMBERD; |
| 637 | temp_cred.cr_audit.as_aia_p = audit_default_aia_p; |
| 638 | |
| 639 | bsd_init_kprintf("calling kauth_cred_create\n" ); |
| 640 | /* |
| 641 | * We have to label the temp cred before we create from it to |
| 642 | * properly set cr_ngroups, or the create will fail. |
| 643 | */ |
| 644 | posix_cred_label(&temp_cred, &temp_pcred); |
| 645 | kernproc->p_ucred = kauth_cred_create(&temp_cred); |
| 646 | |
| 647 | /* update cred on proc */ |
| 648 | PROC_UPDATE_CREDS_ONPROC(kernproc); |
| 649 | |
| 650 | /* give the (already exisiting) initial thread a reference on it */ |
| 651 | bsd_init_kprintf("calling kauth_cred_ref\n" ); |
| 652 | kauth_cred_ref(kernproc->p_ucred); |
| 653 | ut->uu_context.vc_ucred = kernproc->p_ucred; |
| 654 | ut->uu_context.vc_thread = current_thread(); |
| 655 | |
| 656 | TAILQ_INIT(&kernproc->p_aio_activeq); |
| 657 | TAILQ_INIT(&kernproc->p_aio_doneq); |
| 658 | kernproc->p_aio_total_count = 0; |
| 659 | kernproc->p_aio_active_count = 0; |
| 660 | |
| 661 | bsd_init_kprintf("calling file_lock_init\n" ); |
| 662 | file_lock_init(); |
| 663 | |
| 664 | #if CONFIG_MACF |
| 665 | mac_cred_label_associate_kernel(kernproc->p_ucred); |
| 666 | #endif |
| 667 | |
| 668 | /* Create the file descriptor table. */ |
| 669 | kernproc->p_fd = &filedesc0; |
| 670 | filedesc0.fd_cmask = cmask; |
| 671 | filedesc0.fd_knlistsize = -1; |
| 672 | filedesc0.fd_knlist = NULL; |
| 673 | filedesc0.fd_knhash = NULL; |
| 674 | filedesc0.fd_knhashmask = 0; |
| 675 | lck_mtx_init(&filedesc0.fd_kqhashlock, proc_kqhashlock_grp, proc_lck_attr); |
| 676 | lck_mtx_init(&filedesc0.fd_knhashlock, proc_knhashlock_grp, proc_lck_attr); |
| 677 | |
| 678 | /* Create the limits structures. */ |
| 679 | kernproc->p_limit = &limit0; |
| 680 | for (i = 0; i < sizeof(kernproc->p_rlimit)/sizeof(kernproc->p_rlimit[0]); i++) |
| 681 | limit0.pl_rlimit[i].rlim_cur = |
| 682 | limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; |
| 683 | limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE; |
| 684 | limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = maxprocperuid; |
| 685 | limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; |
| 686 | limit0.pl_rlimit[RLIMIT_STACK] = vm_initial_limit_stack; |
| 687 | limit0.pl_rlimit[RLIMIT_DATA] = vm_initial_limit_data; |
| 688 | limit0.pl_rlimit[RLIMIT_CORE] = vm_initial_limit_core; |
| 689 | limit0.pl_refcnt = 1; |
| 690 | |
| 691 | kernproc->p_stats = &pstats0; |
| 692 | kernproc->p_sigacts = &sigacts0; |
| 693 | |
| 694 | /* |
| 695 | * Charge root for one process: launchd. |
| 696 | */ |
| 697 | bsd_init_kprintf("calling chgproccnt\n" ); |
| 698 | (void)chgproccnt(0, 1); |
| 699 | |
| 700 | /* |
| 701 | * Allocate a kernel submap for pageable memory |
| 702 | * for temporary copying (execve()). |
| 703 | */ |
| 704 | { |
| 705 | vm_offset_t minimum; |
| 706 | |
| 707 | bsd_init_kprintf("calling kmem_suballoc\n" ); |
| 708 | assert(bsd_pageable_map_size != 0); |
| 709 | ret = kmem_suballoc(kernel_map, |
| 710 | &minimum, |
| 711 | (vm_size_t)bsd_pageable_map_size, |
| 712 | TRUE, |
| 713 | VM_FLAGS_ANYWHERE, |
| 714 | VM_MAP_KERNEL_FLAGS_NONE, |
| 715 | VM_KERN_MEMORY_BSD, |
| 716 | &bsd_pageable_map); |
| 717 | if (ret != KERN_SUCCESS) |
| 718 | panic("bsd_init: Failed to allocate bsd pageable map" ); |
| 719 | } |
| 720 | |
| 721 | bsd_init_kprintf("calling fpxlog_init\n" ); |
| 722 | fpxlog_init(); |
| 723 | |
| 724 | /* |
| 725 | * Initialize buffers and hash links for buffers |
| 726 | * |
| 727 | * SIDE EFFECT: Starts a thread for bcleanbuf_thread(), so must |
| 728 | * happen after a credential has been associated with |
| 729 | * the kernel task. |
| 730 | */ |
| 731 | bsd_init_kprintf("calling bsd_bufferinit\n" ); |
| 732 | bsd_bufferinit(); |
| 733 | |
| 734 | /* |
| 735 | * Initialize the calendar. |
| 736 | */ |
| 737 | bsd_init_kprintf("calling IOKitInitializeTime\n" ); |
| 738 | IOKitInitializeTime(); |
| 739 | |
| 740 | bsd_init_kprintf("calling ubc_init\n" ); |
| 741 | ubc_init(); |
| 742 | |
| 743 | /* Initialize the file systems. */ |
| 744 | bsd_init_kprintf("calling vfsinit\n" ); |
| 745 | vfsinit(); |
| 746 | |
| 747 | #if CONFIG_PROC_UUID_POLICY |
| 748 | /* Initial proc_uuid_policy subsystem */ |
| 749 | bsd_init_kprintf("calling proc_uuid_policy_init()\n" ); |
| 750 | proc_uuid_policy_init(); |
| 751 | #endif |
| 752 | |
| 753 | #if SOCKETS |
| 754 | /* Initialize per-CPU cache allocator */ |
| 755 | mcache_init(); |
| 756 | |
| 757 | /* Initialize mbuf's. */ |
| 758 | bsd_init_kprintf("calling mbinit\n" ); |
| 759 | mbinit(); |
| 760 | net_str_id_init(); /* for mbuf tags */ |
| 761 | #endif /* SOCKETS */ |
| 762 | |
| 763 | /* |
| 764 | * Initializes security event auditing. |
| 765 | * XXX: Should/could this occur later? |
| 766 | */ |
| 767 | #if CONFIG_AUDIT |
| 768 | bsd_init_kprintf("calling audit_init\n" ); |
| 769 | audit_init(); |
| 770 | #endif |
| 771 | |
| 772 | /* Initialize kqueues */ |
| 773 | bsd_init_kprintf("calling knote_init\n" ); |
| 774 | knote_init(); |
| 775 | |
| 776 | /* Initialize event handler */ |
| 777 | bsd_init_kprintf("calling eventhandler_init\n" ); |
| 778 | eventhandler_init(); |
| 779 | |
| 780 | /* Initialize for async IO */ |
| 781 | bsd_init_kprintf("calling aio_init\n" ); |
| 782 | aio_init(); |
| 783 | |
| 784 | /* Initialize pipes */ |
| 785 | bsd_init_kprintf("calling pipeinit\n" ); |
| 786 | pipeinit(); |
| 787 | |
| 788 | /* Initialize SysV shm subsystem locks; the subsystem proper is |
| 789 | * initialized through a sysctl. |
| 790 | */ |
| 791 | #if SYSV_SHM |
| 792 | bsd_init_kprintf("calling sysv_shm_lock_init\n" ); |
| 793 | sysv_shm_lock_init(); |
| 794 | #endif |
| 795 | #if SYSV_SEM |
| 796 | bsd_init_kprintf("calling sysv_sem_lock_init\n" ); |
| 797 | sysv_sem_lock_init(); |
| 798 | #endif |
| 799 | #if SYSV_MSG |
| 800 | bsd_init_kprintf("sysv_msg_lock_init\n" ); |
| 801 | sysv_msg_lock_init(); |
| 802 | #endif |
| 803 | bsd_init_kprintf("calling pshm_lock_init\n" ); |
| 804 | pshm_lock_init(); |
| 805 | bsd_init_kprintf("calling psem_lock_init\n" ); |
| 806 | psem_lock_init(); |
| 807 | |
| 808 | pthread_init(); |
| 809 | /* POSIX Shm and Sem */ |
| 810 | bsd_init_kprintf("calling pshm_cache_init\n" ); |
| 811 | pshm_cache_init(); |
| 812 | bsd_init_kprintf("calling psem_cache_init\n" ); |
| 813 | psem_cache_init(); |
| 814 | bsd_init_kprintf("calling time_zone_slock_init\n" ); |
| 815 | time_zone_slock_init(); |
| 816 | bsd_init_kprintf("calling select_waitq_init\n" ); |
| 817 | select_waitq_init(); |
| 818 | |
| 819 | /* |
| 820 | * Initialize protocols. Block reception of incoming packets |
| 821 | * until everything is ready. |
| 822 | */ |
| 823 | #if NETWORKING |
| 824 | bsd_init_kprintf("calling nwk_wq_init\n" ); |
| 825 | nwk_wq_init(); |
| 826 | bsd_init_kprintf("calling dlil_init\n" ); |
| 827 | dlil_init(); |
| 828 | bsd_init_kprintf("calling proto_kpi_init\n" ); |
| 829 | proto_kpi_init(); |
| 830 | #endif /* NETWORKING */ |
| 831 | #if SOCKETS |
| 832 | bsd_init_kprintf("calling socketinit\n" ); |
| 833 | socketinit(); |
| 834 | bsd_init_kprintf("calling domaininit\n" ); |
| 835 | domaininit(); |
| 836 | iptap_init(); |
| 837 | #if FLOW_DIVERT |
| 838 | flow_divert_init(); |
| 839 | #endif /* FLOW_DIVERT */ |
| 840 | #endif /* SOCKETS */ |
| 841 | kernproc->p_fd->fd_cdir = NULL; |
| 842 | kernproc->p_fd->fd_rdir = NULL; |
| 843 | |
| 844 | #if CONFIG_FREEZE |
| 845 | #ifndef CONFIG_MEMORYSTATUS |
| 846 | #error "CONFIG_FREEZE defined without matching CONFIG_MEMORYSTATUS" |
| 847 | #endif |
| 848 | /* Initialise background freezing */ |
| 849 | bsd_init_kprintf("calling memorystatus_freeze_init\n" ); |
| 850 | memorystatus_freeze_init(); |
| 851 | #endif |
| 852 | |
| 853 | #if CONFIG_MEMORYSTATUS |
| 854 | /* Initialize kernel memory status notifications */ |
| 855 | bsd_init_kprintf("calling memorystatus_init\n" ); |
| 856 | memorystatus_init(); |
| 857 | #endif /* CONFIG_MEMORYSTATUS */ |
| 858 | |
| 859 | bsd_init_kprintf("calling acct_init\n" ); |
| 860 | acct_init(); |
| 861 | |
| 862 | #ifdef GPROF |
| 863 | /* Initialize kernel profiling. */ |
| 864 | kmstartup(); |
| 865 | #endif |
| 866 | |
| 867 | bsd_init_kprintf("calling sysctl_mib_init\n" ); |
| 868 | sysctl_mib_init() |
| 869 | |
| 870 | bsd_init_kprintf("calling bsd_autoconf\n" ); |
| 871 | bsd_autoconf(); |
| 872 | |
| 873 | bsd_init_kprintf("calling os_reason_init\n" ); |
| 874 | os_reason_init(); |
| 875 | |
| 876 | #if CONFIG_DTRACE |
| 877 | dtrace_postinit(); |
| 878 | #endif |
| 879 | |
| 880 | /* |
| 881 | * We attach the loopback interface *way* down here to ensure |
| 882 | * it happens after autoconf(), otherwise it becomes the |
| 883 | * "primary" interface. |
| 884 | */ |
| 885 | #include <loop.h> |
| 886 | #if NLOOP > 0 |
| 887 | bsd_init_kprintf("calling loopattach\n" ); |
| 888 | loopattach(); /* XXX */ |
| 889 | #endif |
| 890 | #if NGIF |
| 891 | /* Initialize gif interface (after lo0) */ |
| 892 | gif_init(); |
| 893 | #endif |
| 894 | |
| 895 | #if PFLOG |
| 896 | /* Initialize packet filter log interface */ |
| 897 | pfloginit(); |
| 898 | #endif /* PFLOG */ |
| 899 | |
| 900 | #if NETHER > 0 |
| 901 | /* Register the built-in dlil ethernet interface family */ |
| 902 | bsd_init_kprintf("calling ether_family_init\n" ); |
| 903 | ether_family_init(); |
| 904 | #endif /* ETHER */ |
| 905 | |
| 906 | #if NETWORKING |
| 907 | /* Call any kext code that wants to run just after network init */ |
| 908 | bsd_init_kprintf("calling net_init_run\n" ); |
| 909 | net_init_run(); |
| 910 | |
| 911 | #if CONTENT_FILTER |
| 912 | cfil_init(); |
| 913 | #endif |
| 914 | |
| 915 | #if PACKET_MANGLER |
| 916 | pkt_mnglr_init(); |
| 917 | #endif |
| 918 | |
| 919 | #if NECP |
| 920 | /* Initialize Network Extension Control Policies */ |
| 921 | necp_init(); |
| 922 | #endif |
| 923 | |
| 924 | netagent_init(); |
| 925 | |
| 926 | /* register user tunnel kernel control handler */ |
| 927 | utun_register_control(); |
| 928 | #if IPSEC |
| 929 | ipsec_register_control(); |
| 930 | #endif /* IPSEC */ |
| 931 | netsrc_init(); |
| 932 | nstat_init(); |
| 933 | tcp_cc_init(); |
| 934 | #if MPTCP |
| 935 | mptcp_control_register(); |
| 936 | #endif /* MPTCP */ |
| 937 | #endif /* NETWORKING */ |
| 938 | |
| 939 | bsd_init_kprintf("calling vnode_pager_bootstrap\n" ); |
| 940 | vnode_pager_bootstrap(); |
| 941 | |
| 942 | bsd_init_kprintf("calling inittodr\n" ); |
| 943 | inittodr(0); |
| 944 | |
| 945 | /* Mount the root file system. */ |
| 946 | while( TRUE) { |
| 947 | int err; |
| 948 | |
| 949 | bsd_init_kprintf("calling setconf\n" ); |
| 950 | setconf(); |
| 951 | #if NFSCLIENT |
| 952 | netboot = (mountroot == netboot_mountroot); |
| 953 | #endif |
| 954 | |
| 955 | bsd_init_kprintf("vfs_mountroot\n" ); |
| 956 | if (0 == (err = vfs_mountroot())) |
| 957 | break; |
| 958 | rootdevice[0] = '\0'; |
| 959 | #if NFSCLIENT |
| 960 | if (netboot) { |
| 961 | PE_display_icon( 0, "noroot" ); /* XXX a netboot-specific icon would be nicer */ |
| 962 | vc_progress_set(FALSE, 0); |
| 963 | for (i=1; 1; i*=2) { |
| 964 | printf("bsd_init: failed to mount network root, error %d, %s\n" , |
| 965 | err, PE_boot_args()); |
| 966 | printf("We are hanging here...\n" ); |
| 967 | IOSleep(i*60*1000); |
| 968 | } |
| 969 | /*NOTREACHED*/ |
| 970 | } |
| 971 | #endif |
| 972 | printf("cannot mount root, errno = %d\n" , err); |
| 973 | boothowto |= RB_ASKNAME; |
| 974 | } |
| 975 | |
| 976 | IOSecureBSDRoot(rootdevice); |
| 977 | |
| 978 | context.vc_thread = current_thread(); |
| 979 | context.vc_ucred = kernproc->p_ucred; |
| 980 | mountlist.tqh_first->mnt_flag |= MNT_ROOTFS; |
| 981 | |
| 982 | bsd_init_kprintf("calling VFS_ROOT\n" ); |
| 983 | /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */ |
| 984 | if (VFS_ROOT(mountlist.tqh_first, &rootvnode, &context)) |
| 985 | panic("bsd_init: cannot find root vnode: %s" , PE_boot_args()); |
| 986 | rootvnode->v_flag |= VROOT; |
| 987 | (void)vnode_ref(rootvnode); |
| 988 | (void)vnode_put(rootvnode); |
| 989 | filedesc0.fd_cdir = rootvnode; |
| 990 | |
| 991 | #if NFSCLIENT |
| 992 | if (netboot) { |
| 993 | int err; |
| 994 | |
| 995 | netboot = TRUE; |
| 996 | /* post mount setup */ |
| 997 | if ((err = netboot_setup()) != 0) { |
| 998 | PE_display_icon( 0, "noroot" ); /* XXX a netboot-specific icon would be nicer */ |
| 999 | vc_progress_set(FALSE, 0); |
| 1000 | for (i=1; 1; i*=2) { |
| 1001 | printf("bsd_init: NetBoot could not find root, error %d: %s\n" , |
| 1002 | err, PE_boot_args()); |
| 1003 | printf("We are hanging here...\n" ); |
| 1004 | IOSleep(i*60*1000); |
| 1005 | } |
| 1006 | /*NOTREACHED*/ |
| 1007 | } |
| 1008 | } |
| 1009 | #endif |
| 1010 | |
| 1011 | |
| 1012 | #if CONFIG_IMAGEBOOT |
| 1013 | /* |
| 1014 | * See if a system disk image is present. If so, mount it and |
| 1015 | * switch the root vnode to point to it |
| 1016 | */ |
| 1017 | if (netboot == FALSE && imageboot_needed()) { |
| 1018 | /* |
| 1019 | * An image was found. No turning back: we're booted |
| 1020 | * with a kernel from the disk image. |
| 1021 | */ |
| 1022 | imageboot_setup(); |
| 1023 | } |
| 1024 | #endif /* CONFIG_IMAGEBOOT */ |
| 1025 | |
| 1026 | /* set initial time; all other resource data is already zero'ed */ |
| 1027 | microtime_with_abstime(&kernproc->p_start, &kernproc->p_stats->ps_start); |
| 1028 | |
| 1029 | #if DEVFS |
| 1030 | { |
| 1031 | char mounthere[] = "/dev" ; /* !const because of internal casting */ |
| 1032 | |
| 1033 | bsd_init_kprintf("calling devfs_kernel_mount\n" ); |
| 1034 | devfs_kernel_mount(mounthere); |
| 1035 | } |
| 1036 | #endif /* DEVFS */ |
| 1037 | |
| 1038 | /* Initialize signal state for process 0. */ |
| 1039 | bsd_init_kprintf("calling siginit\n" ); |
| 1040 | siginit(kernproc); |
| 1041 | |
| 1042 | bsd_init_kprintf("calling bsd_utaskbootstrap\n" ); |
| 1043 | bsd_utaskbootstrap(); |
| 1044 | |
| 1045 | pal_kernel_announce(); |
| 1046 | |
| 1047 | bsd_init_kprintf("calling mountroot_post_hook\n" ); |
| 1048 | |
| 1049 | /* invoke post-root-mount hook */ |
| 1050 | if (mountroot_post_hook != NULL) |
| 1051 | mountroot_post_hook(); |
| 1052 | |
| 1053 | #if 0 /* not yet */ |
| 1054 | consider_zone_gc(FALSE); |
| 1055 | #endif |
| 1056 | |
| 1057 | /* Initialize System Override call */ |
| 1058 | init_system_override(); |
| 1059 | |
| 1060 | bsd_init_kprintf("done\n" ); |
| 1061 | } |
| 1062 | |
| 1063 | void |
| 1064 | bsdinit_task(void) |
| 1065 | { |
| 1066 | proc_t p = current_proc(); |
| 1067 | |
| 1068 | process_name("init" , p); |
| 1069 | |
| 1070 | /* Set up exception-to-signal reflection */ |
| 1071 | ux_handler_setup(); |
| 1072 | |
| 1073 | #if CONFIG_MACF |
| 1074 | mac_cred_label_associate_user(p->p_ucred); |
| 1075 | #endif |
| 1076 | |
| 1077 | vm_init_before_launchd(); |
| 1078 | |
| 1079 | #if CONFIG_XNUPOST |
| 1080 | int result = bsd_list_tests(); |
| 1081 | result = bsd_do_post(); |
| 1082 | if (result != 0) { |
| 1083 | panic("bsd_do_post: Tests failed with result = 0x%08x\n" , result); |
| 1084 | } |
| 1085 | #endif |
| 1086 | |
| 1087 | bsd_init_kprintf("bsd_do_post - done" ); |
| 1088 | |
| 1089 | load_init_program(p); |
| 1090 | lock_trace = 1; |
| 1091 | } |
| 1092 | |
| 1093 | kern_return_t |
| 1094 | bsd_autoconf(void) |
| 1095 | { |
| 1096 | kprintf("bsd_autoconf: calling kminit\n" ); |
| 1097 | kminit(); |
| 1098 | |
| 1099 | /* |
| 1100 | * Early startup for bsd pseudodevices. |
| 1101 | */ |
| 1102 | { |
| 1103 | struct pseudo_init *pi; |
| 1104 | |
| 1105 | for (pi = pseudo_inits; pi->ps_func; pi++) |
| 1106 | (*pi->ps_func) (pi->ps_count); |
| 1107 | } |
| 1108 | |
| 1109 | return( IOKitBSDInit()); |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | #include <sys/disklabel.h> /* for MAXPARTITIONS */ |
| 1114 | |
| 1115 | static void |
| 1116 | setconf(void) |
| 1117 | { |
| 1118 | u_int32_t flags; |
| 1119 | kern_return_t err; |
| 1120 | |
| 1121 | err = IOFindBSDRoot(rootdevice, sizeof(rootdevice), &rootdev, &flags); |
| 1122 | if( err) { |
| 1123 | printf("setconf: IOFindBSDRoot returned an error (%d);" |
| 1124 | "setting rootdevice to 'sd0a'.\n" , err); /* XXX DEBUG TEMP */ |
| 1125 | rootdev = makedev( 6, 0 ); |
| 1126 | strlcpy(rootdevice, "sd0a" , sizeof(rootdevice)); |
| 1127 | flags = 0; |
| 1128 | } |
| 1129 | |
| 1130 | #if NFSCLIENT |
| 1131 | if( flags & 1 ) { |
| 1132 | /* network device */ |
| 1133 | mountroot = netboot_mountroot; |
| 1134 | } else { |
| 1135 | #endif |
| 1136 | /* otherwise have vfs determine root filesystem */ |
| 1137 | mountroot = NULL; |
| 1138 | #if NFSCLIENT |
| 1139 | } |
| 1140 | #endif |
| 1141 | |
| 1142 | } |
| 1143 | |
| 1144 | void |
| 1145 | bsd_utaskbootstrap(void) |
| 1146 | { |
| 1147 | thread_t thread; |
| 1148 | struct uthread *ut; |
| 1149 | |
| 1150 | /* |
| 1151 | * Clone the bootstrap process from the kernel process, without |
| 1152 | * inheriting either task characteristics or memory from the kernel; |
| 1153 | */ |
| 1154 | thread = cloneproc(TASK_NULL, COALITION_NULL, kernproc, FALSE, TRUE); |
| 1155 | |
| 1156 | /* Hold the reference as it will be dropped during shutdown */ |
| 1157 | initproc = proc_find(1); |
| 1158 | #if __PROC_INTERNAL_DEBUG |
| 1159 | if (initproc == PROC_NULL) |
| 1160 | panic("bsd_utaskbootstrap: initproc not set\n" ); |
| 1161 | #endif |
| 1162 | /* |
| 1163 | * Since we aren't going back out the normal way to our parent, |
| 1164 | * we have to drop the transition locks explicitly. |
| 1165 | */ |
| 1166 | proc_signalend(initproc, 0); |
| 1167 | proc_transend(initproc, 0); |
| 1168 | |
| 1169 | ut = (struct uthread *)get_bsdthread_info(thread); |
| 1170 | ut->uu_sigmask = 0; |
| 1171 | act_set_astbsd(thread); |
| 1172 | task_clear_return_wait(get_threadtask(thread)); |
| 1173 | } |
| 1174 | |
| 1175 | static void |
| 1176 | parse_bsd_args(void) |
| 1177 | { |
| 1178 | char namep[16]; |
| 1179 | int msgbuf; |
| 1180 | |
| 1181 | if ( PE_parse_boot_argn("-s" , namep, sizeof (namep))) |
| 1182 | boothowto |= RB_SINGLE; |
| 1183 | |
| 1184 | if (PE_parse_boot_argn("-b" , namep, sizeof (namep))) |
| 1185 | boothowto |= RB_NOBOOTRC; |
| 1186 | |
| 1187 | if (PE_parse_boot_argn("-x" , namep, sizeof (namep))) /* safe boot */ |
| 1188 | boothowto |= RB_SAFEBOOT; |
| 1189 | |
| 1190 | if (PE_parse_boot_argn("-minimalboot" , namep, sizeof(namep))) { |
| 1191 | /* |
| 1192 | * -minimalboot indicates that we want userspace to be bootstrapped to a |
| 1193 | * minimal environment. What constitutes minimal is up to the bootstrap |
| 1194 | * process. |
| 1195 | */ |
| 1196 | minimalboot = 1; |
| 1197 | } |
| 1198 | |
| 1199 | #if __arm64__ |
| 1200 | /* disable 64 bit grading */ |
| 1201 | if (PE_parse_boot_argn("-no64exec" , namep, sizeof (namep))) |
| 1202 | bootarg_no64exec = 1; |
| 1203 | #endif |
| 1204 | #if __x86_64__ |
| 1205 | /* disable 32 bit grading */ |
| 1206 | if (PE_parse_boot_argn("-no32exec" , namep, sizeof (namep))) |
| 1207 | bootarg_no32exec = 1; |
| 1208 | #endif |
| 1209 | |
| 1210 | /* disable vnode_cache_is_authorized() by setting vnode_cache_defeat */ |
| 1211 | if (PE_parse_boot_argn("-vnode_cache_defeat" , namep, sizeof (namep))) |
| 1212 | bootarg_vnode_cache_defeat = 1; |
| 1213 | |
| 1214 | #if DEVELOPMENT || DEBUG |
| 1215 | if (PE_parse_boot_argn("-disable_aslr" , namep, sizeof (namep))) |
| 1216 | bootarg_disable_aslr = 1; |
| 1217 | #endif |
| 1218 | |
| 1219 | PE_parse_boot_argn("ncl" , &ncl, sizeof (ncl)); |
| 1220 | if (PE_parse_boot_argn("nbuf" , &max_nbuf_headers, |
| 1221 | sizeof (max_nbuf_headers))) { |
| 1222 | customnbuf = 1; |
| 1223 | } |
| 1224 | |
| 1225 | #if CONFIG_MACF |
| 1226 | #if defined (__i386__) || defined (__x86_64__) |
| 1227 | PE_parse_boot_argn("policy_check" , &policy_check_flags, sizeof (policy_check_flags)); |
| 1228 | #endif |
| 1229 | #endif /* CONFIG_MACF */ |
| 1230 | |
| 1231 | if (PE_parse_boot_argn("msgbuf" , &msgbuf, sizeof (msgbuf))) { |
| 1232 | log_setsize(msgbuf); |
| 1233 | oslog_setsize(msgbuf); |
| 1234 | } |
| 1235 | |
| 1236 | if (PE_parse_boot_argn("-novfscache" , namep, sizeof(namep))) { |
| 1237 | nc_disabled = 1; |
| 1238 | } |
| 1239 | |
| 1240 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) |
| 1241 | if (PE_parse_boot_argn("-no_vnode_jetsam" , namep, sizeof(namep))) |
| 1242 | bootarg_no_vnode_jetsam = 1; |
| 1243 | #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ |
| 1244 | |
| 1245 | |
| 1246 | #if CONFIG_EMBEDDED |
| 1247 | /* |
| 1248 | * The darkboot flag is specified by the bootloader and is stored in |
| 1249 | * boot_args->bootFlags. This flag is available starting revision 2. |
| 1250 | */ |
| 1251 | boot_args *args = (boot_args *) PE_state.bootArgs; |
| 1252 | if ((args != NULL) && (args->Revision >= kBootArgsRevision2)) { |
| 1253 | darkboot = (args->bootFlags & kBootFlagsDarkBoot) ? 1 : 0; |
| 1254 | } else { |
| 1255 | darkboot = 0; |
| 1256 | } |
| 1257 | #endif |
| 1258 | |
| 1259 | #if PROC_REF_DEBUG |
| 1260 | if (PE_parse_boot_argn("-disable_procref_tracking" , namep, sizeof(namep))) { |
| 1261 | proc_ref_tracking_disabled = 1; |
| 1262 | } |
| 1263 | #endif |
| 1264 | |
| 1265 | #if OS_REASON_DEBUG |
| 1266 | if (PE_parse_boot_argn("-disable_osreason_debug" , namep, sizeof(namep))) { |
| 1267 | os_reason_debug_disabled = 1; |
| 1268 | } |
| 1269 | #endif |
| 1270 | |
| 1271 | PE_parse_boot_argn("sigrestrict" , &sigrestrict_arg, sizeof(sigrestrict_arg)); |
| 1272 | |
| 1273 | #if DEVELOPMENT|| DEBUG |
| 1274 | if (PE_parse_boot_argn("-no_sigsys" , namep, sizeof(namep))) { |
| 1275 | send_sigsys = false; |
| 1276 | } |
| 1277 | #endif |
| 1278 | |
| 1279 | #if (DEVELOPMENT|| DEBUG) |
| 1280 | if (PE_parse_boot_argn("alt-dyld" , dyld_alt_path, sizeof(dyld_alt_path))) { |
| 1281 | if (strlen(dyld_alt_path) > 0) { |
| 1282 | use_alt_dyld = 1; |
| 1283 | } |
| 1284 | } |
| 1285 | #endif |
| 1286 | } |
| 1287 | |
| 1288 | void |
| 1289 | bsd_exec_setup(int scale) |
| 1290 | { |
| 1291 | |
| 1292 | switch (scale) { |
| 1293 | case 0: |
| 1294 | case 1: |
| 1295 | bsd_simul_execs = BSD_SIMUL_EXECS; |
| 1296 | break; |
| 1297 | case 2: |
| 1298 | case 3: |
| 1299 | bsd_simul_execs = 65; |
| 1300 | break; |
| 1301 | case 4: |
| 1302 | case 5: |
| 1303 | bsd_simul_execs = 129; |
| 1304 | break; |
| 1305 | case 6: |
| 1306 | case 7: |
| 1307 | bsd_simul_execs = 257; |
| 1308 | break; |
| 1309 | default: |
| 1310 | bsd_simul_execs = 513; |
| 1311 | break; |
| 1312 | |
| 1313 | } |
| 1314 | bsd_pageable_map_size = (bsd_simul_execs * BSD_PAGEABLE_SIZE_PER_EXEC); |
| 1315 | } |
| 1316 | |
| 1317 | #if !NFSCLIENT |
| 1318 | int |
| 1319 | netboot_root(void); |
| 1320 | |
| 1321 | int |
| 1322 | netboot_root(void) |
| 1323 | { |
| 1324 | return(0); |
| 1325 | } |
| 1326 | #endif |
| 1327 | |