2 * Copyright (c) 2000-2020 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>
94 #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/zalloc.h>
109 #include <kern/ux_handler.h> /* for ux_handler_setup() */
111 #include <mach/vm_param.h>
113 #include <vm/vm_map.h>
114 #include <vm/vm_kern.h>
116 #include <sys/reboot.h>
117 #include <dev/busvar.h> /* for pseudo_inits */
118 #include <sys/kdebug.h>
119 #include <sys/monotonic.h>
121 #include <mach/mach_types.h>
122 #include <mach/vm_prot.h>
123 #include <mach/semaphore.h>
124 #include <mach/sync_policy.h>
125 #include <kern/clock.h>
127 #include <mach/kern_return.h>
128 #include <mach/thread_act.h> /* for thread_resume() */
129 #include <sys/mcache.h> /* for mcache_init() */
130 #include <sys/mbuf.h> /* for mbinit() */
131 #include <sys/event.h> /* for knote_init() */
132 #include <sys/eventhandler.h> /* for eventhandler_init() */
133 #include <sys/kern_memorystatus.h> /* for memorystatus_init() */
134 #include <sys/kern_memorystatus_freeze.h> /* for memorystatus_freeze_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 <net/iptap.h> /* for iptap_init() */
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 <net/if_gif.h> /* for gif_init() */
145 #include <miscfs/devfs/devfsdefs.h> /* for devfs_kernel_mount() */
146 #include <vm/vm_kern.h> /* for kmem_suballoc() */
147 #include <sys/msgbuf.h> /* for log_setsize() */
148 #include <sys/proc_uuid_policy.h> /* proc_uuid_policy_init() */
149 #include <netinet/flow_divert.h> /* flow_divert_init() */
150 #include <net/content_filter.h> /* for cfil_init() */
151 #include <net/necp.h> /* for necp_init() */
152 #include <net/network_agent.h> /* for netagent_init() */
153 #include <net/packet_mangler.h> /* for pkt_mnglr_init() */
154 #include <net/if_utun.h> /* for utun_register_control() */
155 #include <net/if_ipsec.h> /* for ipsec_register_control() */
156 #include <net/net_str_id.h> /* for net_str_id_init() */
157 #include <net/netsrc.h> /* for netsrc_init() */
158 #include <net/ntstat.h> /* for nstat_init() */
159 #include <netinet/tcp_cc.h> /* for tcp_cc_init() */
160 #include <netinet/mptcp_var.h> /* for mptcp_control_register() */
161 #include <net/nwk_wq.h> /* for nwk_wq_init */
162 #include <net/restricted_in_port.h> /* for restricted_in_port_init() */
163 #include <kern/assert.h> /* for assert() */
164 #include <sys/kern_overrides.h> /* for init_system_override() */
165 #include <sys/lockf.h> /* for lf_init() */
166 #include <sys/fsctl.h>
168 #include <net/init.h>
171 #include <security/mac_framework.h>
172 #include <security/mac_internal.h> /* mac_init_bsd() */
173 #include <security/mac_mach_internal.h> /* mac_update_task_label() */
176 #include <machine/exec.h>
179 #include <sys/netboot.h>
183 #include <sys/imageboot.h>
187 #include <net/if_pflog.h>
191 #include <pexpert/pexpert.h>
192 #include <machine/pal_routines.h>
193 #include <console/video_console.h>
196 #include <tests/xnupost.h>
199 void * get_user_regs(thread_t
); /* XXX kludge for <machine/thread.h> */
200 void IOKitInitializeTime(void); /* XXX */
201 void IOSleep(unsigned int); /* XXX */
202 void loopattach(void); /* XXX */
204 const char *const copyright
=
205 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t"
206 "The Regents of the University of California. "
207 "All rights reserved.\n\n";
209 /* Components of the first process -- never freed. */
210 struct proc proc0
= { .p_comm
= "kernel_task", .p_name
= "kernel_task" };
211 struct session session0
;
213 struct filedesc filedesc0
;
214 struct plimit limit0
;
215 struct pstats pstats0
;
216 struct sigacts sigacts0
;
217 SECURITY_READ_ONLY_LATE(proc_t
) kernproc
= &proc0
;
218 proc_t
XNU_PTRAUTH_SIGNED_PTR("initproc") initproc
;
226 /* Global variables to make pstat happy. We do swapping differently */
230 struct swdevt swdevt
[1];
232 static LCK_GRP_DECLARE(hostname_lck_grp
, "hostname");
233 LCK_MTX_DECLARE(hostname_lock
, &hostname_lck_grp
);
234 LCK_MTX_DECLARE(domainname_lock
, &hostname_lck_grp
);
236 dev_t rootdev
; /* device of the root */
237 dev_t dumpdev
; /* device to take dumps on */
238 long dumplo
; /* offset into dumpdev */
240 char hostname
[MAXHOSTNAMELEN
];
241 char domainname
[MAXDOMNAMELEN
];
242 char rootdevice
[DEVMAXNAMESIZE
];
244 struct vnode
*rootvp
;
245 bool rootvp_is_ssd
= false;
253 int legacy_footprint_entitlement_mode
= LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE
;
254 #endif /* __arm64__ */
257 __private_extern__
int proc_ref_tracking_disabled
= 0; /* disable panics on leaked proc refs across syscall boundary */
260 extern kern_return_t
IOFindBSDRoot(char *, unsigned int, dev_t
*, u_int32_t
*);
261 extern void IOSecureBSDRoot(const char * rootName
);
262 extern kern_return_t
IOKitBSDInit(void );
263 extern boolean_t
IOSetRecoveryBoot(bsd_bootfail_mode_t
, uuid_t
, boolean_t
);
264 extern void kminit(void);
265 extern void bsd_bufferinit(void);
266 extern void oslog_setsize(int size
);
267 extern void throttle_init(void);
269 #if CONFIG_LOCKERBOOT
270 #define LOCKER_PROTOBOOT_MOUNT "/protoboot"
272 const char kernel_protoboot_mount
[] = LOCKER_PROTOBOOT_MOUNT
;
273 extern int mount_locker_protoboot(const char *fsname
, const char *mntpoint
,
274 const char *pbdevpath
);
278 #if DEVELOPMENT || DEBUG
279 extern int syscallfilter_disable
;
280 #endif // DEVELOPMENT || DEBUG
282 vm_map_t bsd_pageable_map
;
285 static int bsd_simul_execs
;
286 static int bsd_pageable_map_size
;
287 __private_extern__
int execargs_cache_size
= 0;
288 __private_extern__
int execargs_free_count
= 0;
289 __private_extern__ vm_offset_t
* execargs_cache
= NULL
;
291 void bsd_exec_setup(int);
293 __private_extern__
int bootarg_execfailurereports
= 0;
296 __private_extern__
int bootarg_no32exec
= 1;
298 __private_extern__
int bootarg_vnode_cache_defeat
= 0;
300 #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
301 __private_extern__
int bootarg_no_vnode_jetsam
= 0;
302 #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
304 __private_extern__
int bootarg_no_vnode_drain
= 0;
307 * Prevent kernel-based ASLR from being used, for testing.
309 #if DEVELOPMENT || DEBUG
310 __private_extern__
int bootarg_disable_aslr
= 0;
315 * Allow an alternate dyld to be used for testing.
318 #if DEVELOPMENT || DEBUG
319 char dyld_alt_path
[MAXPATHLEN
];
320 int use_alt_dyld
= 0;
321 extern uint64_t dyld_flags
;
325 extern int customnbuf
;
327 kern_return_t
bsd_autoconf(void);
328 void bsd_utaskbootstrap(void);
330 static void parse_bsd_args(void);
332 extern void dev_kmem_init(void);
334 extern void select_waitq_init(void);
335 static void process_name(const char *, proc_t
);
337 static void setconf(void);
339 #if CONFIG_BASESYSTEMROOT
340 static int bsd_find_basesystem_dmg(char *bsdmgpath_out
, bool *rooted_dmg
, bool *skip_signature_check
);
341 static boolean_t
bsdmgroot_bootable(void);
342 #endif // CONFIG_BASESYSTEMROOT
344 static bool bsd_rooted_ramdisk(void);
347 extern void sysv_shm_lock_init(void);
350 extern void sysv_sem_lock_init(void);
353 extern void sysv_msg_lock_init(void);
356 extern void ulock_initialize(void);
359 #if defined (__i386__) || defined (__x86_64__)
360 /* MACF policy_check configuration flags; see policy_check.c for details */
361 int policy_check_flags
= 0;
363 extern int check_policy_init(int);
365 #endif /* CONFIG_MACF */
367 /* If we are using CONFIG_DTRACE */
369 extern void dtrace_postinit(void);
373 * Initialization code.
374 * Called from cold start routine as
375 * soon as a stack and segmentation
376 * have been established.
379 * hand craft 0th process
380 * call all initialization routines
381 * hand craft 1st user process
385 * Sets the name for the given task.
388 process_name(const char *s
, proc_t p
)
390 strlcpy(p
->p_comm
, s
, sizeof(p
->p_comm
));
391 strlcpy(p
->p_name
, s
, sizeof(p
->p_name
));
394 /* To allow these values to be patched, they're globals here */
395 #include <machine/vmparam.h>
396 struct rlimit vm_initial_limit_stack
= { .rlim_cur
= DFLSSIZ
, .rlim_max
= MAXSSIZ
- PAGE_MAX_SIZE
};
397 struct rlimit vm_initial_limit_data
= { .rlim_cur
= DFLDSIZ
, .rlim_max
= MAXDSIZ
};
398 struct rlimit vm_initial_limit_core
= { .rlim_cur
= DFLCSIZ
, .rlim_max
= MAXCSIZ
};
400 extern struct os_refgrp rlimit_refgrp
;
402 extern thread_t
cloneproc(task_t
, coalition_t
, proc_t
, int, int);
403 extern int (*mountroot
)(void);
405 LCK_ATTR_DECLARE(proc_lck_attr
, 0, 0);
406 LCK_GRP_DECLARE(proc_lck_grp
, "proc");
407 LCK_GRP_DECLARE(proc_slock_grp
, "proc-slock");
408 LCK_GRP_DECLARE(proc_fdmlock_grp
, "proc-fdmlock");
409 LCK_GRP_DECLARE(proc_mlock_grp
, "proc-mlock");
410 LCK_GRP_DECLARE(proc_ucred_mlock_grp
, "proc-ucred-mlock");
411 LCK_GRP_DECLARE(proc_dirslock_grp
, "proc-dirslock");
412 LCK_GRP_DECLARE(proc_kqhashlock_grp
, "proc-kqhashlock");
413 LCK_GRP_DECLARE(proc_knhashlock_grp
, "proc-knhashlock");
416 LCK_MTX_DECLARE_ATTR(proc_list_mlock
, &proc_mlock_grp
, &proc_lck_attr
);
418 #if XNU_TARGET_OS_OSX
419 /* hook called after root is mounted XXX temporary hack */
420 void (*mountroot_post_hook
)(void);
421 void (*unmountroot_pre_hook
)(void);
423 void set_rootvnode(vnode_t
);
425 extern lck_rw_t rootvnode_rw_lock
;
427 /* called with an iocount and usecount on new_rootvnode */
429 set_rootvnode(vnode_t new_rootvnode
)
431 mount_t new_mount
= (new_rootvnode
!= NULL
) ? new_rootvnode
->v_mount
: NULL
;
432 vnode_t new_devvp
= (new_mount
!= NULL
) ? new_mount
->mnt_devvp
: NULL
;
433 vnode_t old_rootvnode
= rootvnode
;
435 new_rootvnode
->v_flag
|= VROOT
;
437 rootvnode
= new_rootvnode
;
438 filedesc0
.fd_cdir
= new_rootvnode
;
439 if (new_devvp
!= NULL
) {
440 rootdev
= vnode_specrdev(new_devvp
);
441 } else if (new_mount
!= NULL
) {
442 rootdev
= vfs_statfs(new_mount
)->f_fsid
.val
[0]; /* like ATTR_CMN_DEVID */
448 vnode_rele(old_rootvnode
);
455 bsd_rooted_ramdisk(void)
457 bool is_ramdisk
= false;
458 char *dev_path
= zalloc(ZV_NAMEI
);
459 if (dev_path
== NULL
) {
460 panic("failed to allocate devpath string! \n");
463 if (PE_parse_boot_argn("rd", dev_path
, MAXPATHLEN
)) {
464 if (strncmp(dev_path
, RAMDEV
, strlen(RAMDEV
)) == 0) {
469 zfree(ZV_NAMEI
, dev_path
);
474 * This function is called very early on in the Mach startup, from the
475 * function start_kernel_threads() in osfmk/kern/startup.c. It's called
476 * in the context of the current (startup) task using a call to the
477 * function kernel_thread_create() to jump into start_kernel_threads().
478 * Internally, kernel_thread_create() calls thread_create_internal(),
479 * which calls uthread_alloc(). The function of uthread_alloc() is
480 * normally to allocate a uthread structure, and fill out the uu_sigmask,
481 * uu_context fields. It skips filling these out in the case of the "task"
482 * being "kernel_task", because the order of operation is inverted. To
483 * account for that, we need to manually fill in at least the contents
484 * of the uu_context.vc_ucred field so that the uthread structure can be
485 * used like any other.
493 struct vfs_context context
;
495 struct ucred temp_cred
;
496 struct posix_cred temp_pcred
;
497 vnode_t init_rootvnode
= NULLVP
;
498 #if CONFIG_NETBOOT || CONFIG_IMAGEBOOT
499 boolean_t netboot
= FALSE
;
501 #if CONFIG_LOCKERBOOT
502 vnode_t pbvn
= NULLVP
;
503 mount_t pbmnt
= NULL
;
506 char pbfsname
[MFSNAMELEN
];
507 const char *slash_dev
= NULL
;
510 #define DEBUG_BSDINIT 0
513 #define bsd_init_kprintf(x, ...) kprintf("bsd_init: " x, ## __VA_ARGS__)
515 #define bsd_init_kprintf(x, ...)
522 bsd_init_kprintf("calling parse_bsd_args\n");
526 bsd_init_kprintf("calling dev_kmem_init\n");
530 /* Initialize kauth subsystem before instancing the first credential */
531 bsd_init_kprintf("calling kauth_init\n");
534 /* Initialize process and pgrp structures. */
535 bsd_init_kprintf("calling procinit\n");
538 /* kernel_task->proc = kernproc; */
539 set_bsdtask_info(kernel_task
, (void *)kernproc
);
541 /* give kernproc a name */
542 bsd_init_kprintf("calling process_name\n");
543 process_name("kernel_task", kernproc
);
545 /* Allocate proc lock attribute */
547 lck_mtx_init(&kernproc
->p_mlock
, &proc_mlock_grp
, &proc_lck_attr
);
548 lck_mtx_init(&kernproc
->p_fdmlock
, &proc_fdmlock_grp
, &proc_lck_attr
);
549 lck_mtx_init(&kernproc
->p_ucred_mlock
, &proc_ucred_mlock_grp
, &proc_lck_attr
);
550 lck_spin_init(&kernproc
->p_slock
, &proc_slock_grp
, &proc_lck_attr
);
551 lck_rw_init(&kernproc
->p_dirs_lock
, &proc_dirslock_grp
, &proc_lck_attr
);
553 assert(bsd_simul_execs
!= 0);
554 execargs_cache_size
= bsd_simul_execs
;
555 execargs_free_count
= bsd_simul_execs
;
556 execargs_cache
= zalloc_permanent(bsd_simul_execs
* sizeof(vm_offset_t
),
557 ZALIGN(vm_offset_t
));
559 if (current_task() != kernel_task
) {
560 printf("bsd_init: We have a problem, "
561 "current task is not kernel task\n");
564 bsd_init_kprintf("calling get_bsdthread_info\n");
565 ut
= (uthread_t
)get_bsdthread_info(current_thread());
569 * Initialize the MAC Framework
571 mac_policy_initbsd();
573 #if defined (__i386__) || defined (__x86_64__)
575 * We currently only support this on i386/x86_64, as that is the
576 * only lock code we have instrumented so far.
578 check_policy_init(policy_check_flags
);
588 LIST_INSERT_HEAD(&allproc
, kernproc
, p_list
);
589 kernproc
->p_pgrp
= &pgrp0
;
590 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0
, pg_hash
);
591 LIST_INIT(&pgrp0
.pg_members
);
592 lck_mtx_init(&pgrp0
.pg_mlock
, &proc_mlock_grp
, &proc_lck_attr
);
593 /* There is no other bsd thread this point and is safe without pgrp lock */
594 LIST_INSERT_HEAD(&pgrp0
.pg_members
, kernproc
, p_pglist
);
595 kernproc
->p_listflag
|= P_LIST_INPGRP
;
596 kernproc
->p_pgrpid
= 0;
597 kernproc
->p_uniqueid
= 0;
599 pgrp0
.pg_session
= &session0
;
600 pgrp0
.pg_membercnt
= 1;
602 session0
.s_count
= 1;
603 session0
.s_leader
= kernproc
;
604 session0
.s_listflags
= 0;
605 lck_mtx_init(&session0
.s_mlock
, &proc_mlock_grp
, &proc_lck_attr
);
606 LIST_INSERT_HEAD(SESSHASH(0), &session0
, s_hash
);
610 kernproc
->p_persona
= NULL
;
613 kernproc
->task
= kernel_task
;
615 kernproc
->p_stat
= SRUN
;
616 kernproc
->p_flag
= P_SYSTEM
;
617 kernproc
->p_lflag
= 0;
618 kernproc
->p_ladvflag
= 0;
620 #if defined(__LP64__)
621 kernproc
->p_flag
|= P_LP64
;
624 #if DEVELOPMENT || DEBUG
625 if (bootarg_disable_aslr
) {
626 kernproc
->p_flag
|= P_DISABLE_ASLR
;
630 kernproc
->p_nice
= NZERO
;
631 kernproc
->p_pptr
= kernproc
;
633 TAILQ_INIT(&kernproc
->p_uthlist
);
634 TAILQ_INSERT_TAIL(&kernproc
->p_uthlist
, ut
, uu_list
);
636 kernproc
->sigwait
= FALSE
;
637 kernproc
->sigwait_thread
= THREAD_NULL
;
638 kernproc
->exit_thread
= THREAD_NULL
;
639 kernproc
->p_csflags
= CS_VALID
;
642 * Create credential. This also Initializes the audit information.
644 bsd_init_kprintf("calling bzero\n");
645 bzero(&temp_cred
, sizeof(temp_cred
));
646 bzero(&temp_pcred
, sizeof(temp_pcred
));
647 temp_pcred
.cr_ngroups
= 1;
648 /* kern_proc, shouldn't call up to DS for group membership */
649 temp_pcred
.cr_flags
= CRF_NOMEMBERD
;
650 temp_cred
.cr_audit
.as_aia_p
= audit_default_aia_p
;
652 bsd_init_kprintf("calling kauth_cred_create\n");
654 * We have to label the temp cred before we create from it to
655 * properly set cr_ngroups, or the create will fail.
657 posix_cred_label(&temp_cred
, &temp_pcred
);
658 kernproc
->p_ucred
= kauth_cred_create(&temp_cred
);
660 /* update cred on proc */
661 PROC_UPDATE_CREDS_ONPROC(kernproc
);
663 /* give the (already exisiting) initial thread a reference on it */
664 bsd_init_kprintf("calling kauth_cred_ref\n");
665 kauth_cred_ref(kernproc
->p_ucred
);
666 ut
->uu_context
.vc_ucred
= kernproc
->p_ucred
;
667 ut
->uu_context
.vc_thread
= current_thread();
669 vfs_set_context_kernel(&ut
->uu_context
);
671 TAILQ_INIT(&kernproc
->p_aio_activeq
);
672 TAILQ_INIT(&kernproc
->p_aio_doneq
);
673 kernproc
->p_aio_total_count
= 0;
676 mac_cred_label_associate_kernel(kernproc
->p_ucred
);
679 /* Create the file descriptor table. */
680 kernproc
->p_fd
= &filedesc0
;
681 filedesc0
.fd_cmask
= (mode_t
)cmask
;
682 filedesc0
.fd_knlistsize
= 0;
683 filedesc0
.fd_knlist
= NULL
;
684 filedesc0
.fd_knhash
= NULL
;
685 filedesc0
.fd_knhashmask
= 0;
686 lck_mtx_init(&filedesc0
.fd_kqhashlock
, &proc_kqhashlock_grp
, &proc_lck_attr
);
687 lck_mtx_init(&filedesc0
.fd_knhashlock
, &proc_knhashlock_grp
, &proc_lck_attr
);
689 /* Create the limits structures. */
690 kernproc
->p_limit
= &limit0
;
691 for (i
= 0; i
< sizeof(kernproc
->p_limit
->pl_rlimit
) / sizeof(kernproc
->p_limit
->pl_rlimit
[0]); i
++) {
692 limit0
.pl_rlimit
[i
].rlim_cur
=
693 limit0
.pl_rlimit
[i
].rlim_max
= RLIM_INFINITY
;
695 limit0
.pl_rlimit
[RLIMIT_NOFILE
].rlim_cur
= NOFILE
;
696 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_cur
= maxprocperuid
;
697 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_max
= maxproc
;
698 limit0
.pl_rlimit
[RLIMIT_STACK
] = vm_initial_limit_stack
;
699 limit0
.pl_rlimit
[RLIMIT_DATA
] = vm_initial_limit_data
;
700 limit0
.pl_rlimit
[RLIMIT_CORE
] = vm_initial_limit_core
;
701 os_ref_init_count(&limit0
.pl_refcnt
, &rlimit_refgrp
, 1);
703 kernproc
->p_stats
= &pstats0
;
704 kernproc
->p_sigacts
= &sigacts0
;
705 kernproc
->p_subsystem_root_path
= NULL
;
708 * Charge root for one process: launchd.
710 bsd_init_kprintf("calling chgproccnt\n");
711 (void)chgproccnt(0, 1);
714 * Allocate a kernel submap for pageable memory
715 * for temporary copying (execve()).
720 bsd_init_kprintf("calling kmem_suballoc\n");
721 assert(bsd_pageable_map_size
!= 0);
722 ret
= kmem_suballoc(kernel_map
,
724 (vm_size_t
)bsd_pageable_map_size
,
727 VM_MAP_KERNEL_FLAGS_NONE
,
730 if (ret
!= KERN_SUCCESS
) {
731 panic("bsd_init: Failed to allocate bsd pageable map");
736 * Initialize buffers and hash links for buffers
738 * SIDE EFFECT: Starts a thread for bcleanbuf_thread(), so must
739 * happen after a credential has been associated with
742 bsd_init_kprintf("calling bsd_bufferinit\n");
746 * Initialize the calendar.
748 bsd_init_kprintf("calling IOKitInitializeTime\n");
749 IOKitInitializeTime();
751 /* Initialize the file systems. */
752 bsd_init_kprintf("calling vfsinit\n");
755 #if CONFIG_PROC_UUID_POLICY
756 /* Initial proc_uuid_policy subsystem */
757 bsd_init_kprintf("calling proc_uuid_policy_init()\n");
758 proc_uuid_policy_init();
762 /* Initialize per-CPU cache allocator */
765 /* Initialize mbuf's. */
766 bsd_init_kprintf("calling mbinit\n");
768 net_str_id_init(); /* for mbuf tags */
769 restricted_in_port_init();
773 * Initializes security event auditing.
774 * XXX: Should/could this occur later?
777 bsd_init_kprintf("calling audit_init\n");
781 /* Initialize kqueues */
782 bsd_init_kprintf("calling knote_init\n");
785 /* Initialize event handler */
786 bsd_init_kprintf("calling eventhandler_init\n");
789 /* Initialize for async IO */
790 bsd_init_kprintf("calling aio_init\n");
794 /* POSIX Shm and Sem */
795 bsd_init_kprintf("calling pshm_cache_init\n");
797 bsd_init_kprintf("calling psem_cache_init\n");
799 bsd_init_kprintf("calling select_waitq_init\n");
803 * Initialize protocols. Block reception of incoming packets
804 * until everything is ready.
807 bsd_init_kprintf("calling nwk_wq_init\n");
809 bsd_init_kprintf("calling dlil_init\n");
811 bsd_init_kprintf("calling proto_kpi_init\n");
813 #endif /* NETWORKING */
815 bsd_init_kprintf("calling socketinit\n");
817 bsd_init_kprintf("calling domaininit\n");
822 #endif /* FLOW_DIVERT */
826 /* Initialize Network Extension Control Policies */
830 #endif /* NETWORKING */
831 kernproc
->p_fd
->fd_cdir
= NULL
;
832 kernproc
->p_fd
->fd_rdir
= NULL
;
834 #if defined (__x86_64__) && (DEBUG || DEVELOPMENT)
836 #endif /* DEBUG || DEVELOPMENT */
839 #ifndef CONFIG_MEMORYSTATUS
840 #error "CONFIG_FREEZE defined without matching CONFIG_MEMORYSTATUS"
842 /* Initialise background freezing */
843 bsd_init_kprintf("calling memorystatus_freeze_init\n");
844 memorystatus_freeze_init();
847 #if CONFIG_MEMORYSTATUS
848 /* Initialize kernel memory status notifications */
849 bsd_init_kprintf("calling memorystatus_init\n");
851 #endif /* CONFIG_MEMORYSTATUS */
853 bsd_init_kprintf("calling sysctl_mib_init\n");
856 bsd_init_kprintf("calling bsd_autoconf\n");
864 * We attach the loopback interface *way* down here to ensure
865 * it happens after autoconf(), otherwise it becomes the
866 * "primary" interface.
870 bsd_init_kprintf("calling loopattach\n");
871 loopattach(); /* XXX */
874 /* Initialize gif interface (after lo0) */
879 /* Initialize packet filter log interface */
884 /* Register the built-in dlil ethernet interface family */
885 bsd_init_kprintf("calling ether_family_init\n");
899 * Register subsystems with kernel control handlers
901 utun_register_control();
903 ipsec_register_control();
909 mptcp_control_register();
913 * The the networking stack is now initialized so it is a good time to call
914 * the clients that are waiting for the networking stack to be usable.
916 bsd_init_kprintf("calling net_init_run\n");
918 #endif /* NETWORKING */
920 bsd_init_kprintf("calling inittodr\n");
923 /* Mount the root file system. */
927 bsd_init_kprintf("calling setconf\n");
930 netboot
= (mountroot
== netboot_mountroot
);
933 bsd_init_kprintf("vfs_mountroot\n");
934 if (0 == (err
= vfs_mountroot())) {
937 rootdevice
[0] = '\0';
940 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
941 vc_progress_set(FALSE
, 0);
942 for (i
= 1; 1; i
*= 2) {
943 printf("bsd_init: failed to mount network root, error %d, %s\n",
944 err
, PE_boot_args());
945 printf("We are hanging here...\n");
946 IOSleep(i
* 60 * 1000);
951 printf("cannot mount root, errno = %d\n", err
);
952 boothowto
|= RB_ASKNAME
;
955 IOSecureBSDRoot(rootdevice
);
957 context
.vc_thread
= current_thread();
958 context
.vc_ucred
= kernproc
->p_ucred
;
959 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
961 bsd_init_kprintf("calling VFS_ROOT\n");
962 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
963 if (VFS_ROOT(mountlist
.tqh_first
, &init_rootvnode
, &context
)) {
964 panic("bsd_init: cannot find root vnode: %s", PE_boot_args());
966 (void)vnode_ref(init_rootvnode
);
967 (void)vnode_put(init_rootvnode
);
969 lck_rw_lock_exclusive(&rootvnode_rw_lock
);
970 set_rootvnode(init_rootvnode
);
971 lck_rw_unlock_exclusive(&rootvnode_rw_lock
);
972 init_rootvnode
= NULLVP
; /* use rootvnode after this point */
975 if (!bsd_rooted_ramdisk()) {
977 #if XNU_TARGET_OS_OSX && defined(__arm64__)
978 /* Apple Silicon MacOS */
979 if (!imageboot_desired()) {
980 /* enforce sealedness */
981 int autherr
= VNOP_IOCTL(rootvnode
, FSIOC_KERNEL_ROOTAUTH
, NULL
, 0, vfs_context_kernel());
983 panic("rootvp not authenticated after mounting \n");
986 #endif // TARGET_OS_OSX && arm64
987 #endif // config_imageboot
988 /* Otherwise, noop */
997 /* post mount setup */
998 if ((err
= netboot_setup()) != 0) {
999 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
1000 vc_progress_set(FALSE
, 0);
1001 for (i
= 1; 1; i
*= 2) {
1002 printf("bsd_init: NetBoot could not find root, error %d: %s\n",
1003 err
, PE_boot_args());
1004 printf("We are hanging here...\n");
1005 IOSleep(i
* 60 * 1000);
1013 #if CONFIG_IMAGEBOOT
1014 #if CONFIG_LOCKERBOOT
1016 * Stash the protoboot vnode, mount, filesystem name, and device name for
1017 * later use. Note that the mount-from name may not have the "/dev/"
1018 * component, so we must sniff out this condition and add it as needed.
1021 pbmnt
= pbvn
->v_mount
;
1022 pbdevp
= vfs_statfs(pbmnt
)->f_mntfromname
;
1023 slash_dev
= strnstr(pbdevp
, "/dev/", strlen(pbdevp
));
1026 * If the old root is a snapshot mount, it will have the form:
1028 * com.apple.os.update-<boot manifest hash>@<dev node path>
1030 * So we just search the mntfromname for any occurrence of "/dev/" and
1031 * grab that as the device path. The image boot code needs a dev node to
1032 * do the re-mount, so we cannot directly mount the snapshot as the
1033 * protoboot volume currently.
1035 strlcpy(pbdevpath
, slash_dev
, sizeof(pbdevpath
));
1037 snprintf(pbdevpath
, sizeof(pbdevpath
), "/dev/%s", pbdevp
);
1040 bsd_init_kprintf("protoboot mount-from: %s\n", pbdevp
);
1041 bsd_init_kprintf("protoboot dev path: %s\n", pbdevpath
);
1043 strlcpy(pbfsname
, pbmnt
->mnt_vtable
->vfc_name
, sizeof(pbfsname
));
1047 * See if a system disk image is present. If so, mount it and
1048 * switch the root vnode to point to it
1050 imageboot_type_t imageboot_type
= imageboot_needed();
1051 if (netboot
== FALSE
&& imageboot_type
) {
1053 * An image was found. No turning back: we're booted
1054 * with a kernel from the disk image.
1056 bsd_init_kprintf("doing image boot: type = %d\n", imageboot_type
);
1057 imageboot_setup(imageboot_type
);
1060 #if CONFIG_LOCKERBOOT
1061 if (imageboot_type
== IMAGEBOOT_LOCKER
) {
1062 bsd_init_kprintf("booting from locker\n");
1063 if (vnode_tag(rootvnode
) != VT_LOCKERFS
) {
1064 panic("root filesystem not a locker: fsname = %s",
1065 rootvnode
->v_mount
->mnt_vtable
->vfc_name
);
1068 #endif /* CONFIG_LOCKERBOOT */
1069 #endif /* CONFIG_IMAGEBOOT */
1071 /* set initial time; all other resource data is already zero'ed */
1072 microtime_with_abstime(&kernproc
->p_start
, &kernproc
->p_stats
->ps_start
);
1076 char mounthere
[] = "/dev"; /* !const because of internal casting */
1078 bsd_init_kprintf("calling devfs_kernel_mount\n");
1079 devfs_kernel_mount(mounthere
);
1083 #if CONFIG_BASESYSTEMROOT
1084 #if CONFIG_IMAGEBOOT
1085 if (bsdmgroot_bootable()) {
1087 bool rooted_dmg
= false;
1088 bool skip_signature_check
= false;
1090 printf("trying to find and mount BaseSystem dmg as root volume\n");
1091 #if DEVELOPMENT || DEBUG
1092 printf("(set boot-arg -nobsdmgroot to avoid this)\n");
1093 #endif // DEVELOPMENT || DEBUG
1095 char *dmgpath
= NULL
;
1096 dmgpath
= zalloc_flags(ZV_NAMEI
, Z_ZERO
| Z_WAITOK
);
1097 if (dmgpath
== NULL
) {
1098 panic("%s: M_NAMEI zone exhausted", __FUNCTION__
);
1101 error
= bsd_find_basesystem_dmg(dmgpath
, &rooted_dmg
, &skip_signature_check
);
1103 bsd_init_kprintf("failed to to find BaseSystem dmg: error = %d\n", error
);
1105 PE_parse_boot_argn("bsdmgpath", dmgpath
, sizeof(dmgpath
));
1107 bsd_init_kprintf("found BaseSystem dmg at: %s\n", dmgpath
);
1109 error
= imageboot_pivot_image(dmgpath
, IMAGEBOOT_DMG
, "/System/Volumes/BaseSystem", "System/Volumes/macOS", rooted_dmg
, skip_signature_check
);
1111 bsd_init_kprintf("couldn't mount BaseSystem dmg: error = %d", error
);
1114 zfree(ZV_NAMEI
, dmgpath
);
1116 #else /* CONFIG_IMAGEBOOT */
1117 #error CONFIG_BASESYSTEMROOT requires CONFIG_IMAGEBOOT
1118 #endif /* CONFIG_IMAGEBOOT */
1119 #endif /* CONFIG_BASESYSTEMROOT */
1121 #if CONFIG_LOCKERBOOT
1123 * We need to wait until devfs is up before remounting the protoboot volume
1124 * within the locker so that it can have a real devfs vnode backing it.
1126 if (imageboot_type
== IMAGEBOOT_LOCKER
) {
1127 bsd_init_kprintf("re-mounting protoboot volume\n");
1128 int error
= mount_locker_protoboot(pbfsname
, LOCKER_PROTOBOOT_MOUNT
,
1131 panic("failed to mount protoboot volume: dev path = %s, error = %d",
1135 #endif /* CONFIG_LOCKERBOOT */
1137 /* Initialize signal state for process 0. */
1138 bsd_init_kprintf("calling siginit\n");
1141 bsd_init_kprintf("calling bsd_utaskbootstrap\n");
1142 bsd_utaskbootstrap();
1144 pal_kernel_announce();
1146 bsd_init_kprintf("calling mountroot_post_hook\n");
1148 #if XNU_TARGET_OS_OSX
1149 /* invoke post-root-mount hook */
1150 if (mountroot_post_hook
!= NULL
) {
1151 mountroot_post_hook();
1156 consider_zone_gc(FALSE
);
1159 bsd_init_kprintf("done\n");
1165 proc_t p
= current_proc();
1167 process_name("init", p
);
1169 /* Set up exception-to-signal reflection */
1173 mac_cred_label_associate_user(p
->p_ucred
);
1176 vm_init_before_launchd();
1179 int result
= bsd_list_tests();
1180 result
= bsd_do_post();
1182 panic("bsd_do_post: Tests failed with result = 0x%08x\n", result
);
1186 bsd_init_kprintf("bsd_do_post - done");
1188 load_init_program(p
);
1195 kprintf("bsd_autoconf: calling kminit\n");
1199 * Early startup for bsd pseudodevices.
1202 struct pseudo_init
*pi
;
1204 for (pi
= pseudo_inits
; pi
->ps_func
; pi
++) {
1205 (*pi
->ps_func
)(pi
->ps_count
);
1209 return IOKitBSDInit();
1213 #include <sys/disklabel.h> /* for MAXPARTITIONS */
1221 err
= IOFindBSDRoot(rootdevice
, sizeof(rootdevice
), &rootdev
, &flags
);
1223 printf("setconf: IOFindBSDRoot returned an error (%d);"
1224 "setting rootdevice to 'sd0a'.\n", err
); /* XXX DEBUG TEMP */
1225 rootdev
= makedev( 6, 0 );
1226 strlcpy(rootdevice
, "sd0a", sizeof(rootdevice
));
1232 /* network device */
1233 mountroot
= netboot_mountroot
;
1236 /* otherwise have vfs determine root filesystem */
1244 * Boot into the flavor of Recovery dictated by `mode`.
1247 bsd_boot_to_recovery(bsd_bootfail_mode_t mode
, uuid_t volume_uuid
, boolean_t reboot
)
1249 return IOSetRecoveryBoot(mode
, volume_uuid
, reboot
);
1253 bsd_utaskbootstrap(void)
1259 * Clone the bootstrap process from the kernel process, without
1260 * inheriting either task characteristics or memory from the kernel;
1262 thread
= cloneproc(TASK_NULL
, COALITION_NULL
, kernproc
, FALSE
, TRUE
);
1264 /* Hold the reference as it will be dropped during shutdown */
1265 initproc
= proc_find(1);
1266 #if __PROC_INTERNAL_DEBUG
1267 if (initproc
== PROC_NULL
) {
1268 panic("bsd_utaskbootstrap: initproc not set\n");
1272 zalloc_first_proc_made();
1275 * Since we aren't going back out the normal way to our parent,
1276 * we have to drop the transition locks explicitly.
1278 proc_signalend(initproc
, 0);
1279 proc_transend(initproc
, 0);
1281 ut
= (struct uthread
*)get_bsdthread_info(thread
);
1283 act_set_astbsd(thread
);
1284 task_clear_return_wait(get_threadtask(thread
), TCRW_CLEAR_ALL_WAIT
);
1288 parse_bsd_args(void)
1293 if (PE_parse_boot_argn("-s", namep
, sizeof(namep
))) {
1294 boothowto
|= RB_SINGLE
;
1297 if (PE_parse_boot_argn("-x", namep
, sizeof(namep
))) { /* safe boot */
1298 boothowto
|= RB_SAFEBOOT
;
1301 if (PE_parse_boot_argn("-minimalboot", namep
, sizeof(namep
))) {
1303 * -minimalboot indicates that we want userspace to be bootstrapped to a
1304 * minimal environment. What constitutes minimal is up to the bootstrap
1313 /* disable 32 bit grading */
1314 if (PE_parse_boot_argn("no32exec", &no32exec
, sizeof(no32exec
))) {
1315 bootarg_no32exec
= !!no32exec
;
1319 int execfailure_crashreports
;
1320 /* enable crash reports on various exec failures */
1321 if (PE_parse_boot_argn("execfailurecrashes", &execfailure_crashreports
, sizeof(execfailure_crashreports
))) {
1322 bootarg_execfailurereports
= !!execfailure_crashreports
;
1325 /* disable vnode_cache_is_authorized() by setting vnode_cache_defeat */
1326 if (PE_parse_boot_argn("-vnode_cache_defeat", namep
, sizeof(namep
))) {
1327 bootarg_vnode_cache_defeat
= 1;
1330 #if DEVELOPMENT || DEBUG
1331 if (PE_parse_boot_argn("-disable_aslr", namep
, sizeof(namep
))) {
1332 bootarg_disable_aslr
= 1;
1338 PE_parse_boot_argn("ncl", &ncl
, sizeof(ncl
));
1339 if (PE_parse_boot_argn("nbuf", &max_nbuf_headers
,
1340 sizeof(max_nbuf_headers
))) {
1345 #if defined (__i386__) || defined (__x86_64__)
1346 PE_parse_boot_argn("policy_check", &policy_check_flags
, sizeof(policy_check_flags
));
1348 #endif /* CONFIG_MACF */
1350 if (PE_parse_boot_argn("msgbuf", &msgbuf
, sizeof(msgbuf
))) {
1351 log_setsize(msgbuf
);
1352 oslog_setsize(msgbuf
);
1355 if (PE_parse_boot_argn("-novfscache", namep
, sizeof(namep
))) {
1359 #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
1360 if (PE_parse_boot_argn("-no_vnode_jetsam", namep
, sizeof(namep
))) {
1361 bootarg_no_vnode_jetsam
= 1;
1363 #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
1365 if (PE_parse_boot_argn("-no_vnode_drain", namep
, sizeof(namep
))) {
1366 bootarg_no_vnode_drain
= 1;
1371 * The darkboot flag is specified by the bootloader and is stored in
1372 * boot_args->bootFlags. This flag is available starting revision 2.
1374 boot_args
*args
= (boot_args
*) PE_state
.bootArgs
;
1375 if ((args
!= NULL
) && (args
->Revision
>= kBootArgsRevision2
)) {
1376 darkboot
= (args
->bootFlags
& kBootFlagsDarkBoot
) ? 1 : 0;
1383 if (PE_parse_boot_argn("-disable_procref_tracking", namep
, sizeof(namep
))) {
1384 proc_ref_tracking_disabled
= 1;
1388 PE_parse_boot_argn("sigrestrict", &sigrestrict_arg
, sizeof(sigrestrict_arg
));
1390 #if DEVELOPMENT || DEBUG
1391 if (PE_parse_boot_argn("-no_sigsys", namep
, sizeof(namep
))) {
1392 send_sigsys
= false;
1395 if (PE_parse_boot_argn("alt-dyld", dyld_alt_path
, sizeof(dyld_alt_path
))) {
1396 if (strlen(dyld_alt_path
) > 0) {
1400 PE_parse_boot_argn("dyld_flags", &dyld_flags
, sizeof(dyld_flags
));
1402 if (PE_parse_boot_argn("-disable_syscallfilter", &namep
, sizeof(namep
))) {
1403 syscallfilter_disable
= 1;
1407 if (PE_parse_boot_argn("legacy_footprint_entitlement_mode", &legacy_footprint_entitlement_mode
, sizeof(legacy_footprint_entitlement_mode
))) {
1409 * legacy_footprint_entitlement_mode specifies the behavior we want associated
1410 * with the entitlement. The supported modes are:
1412 * LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE:
1413 * Indicates that we want every process to have the memory accounting
1414 * that is available in iOS 12.0 and beyond.
1416 * LEGACY_FOOTPRINT_ENTITLEMENT_IOS11_ACCT:
1417 * Indicates that for every process that has the 'legacy footprint entitlement',
1418 * we want to give it the old iOS 11.0 accounting behavior which accounted some
1419 * of the process's memory to the kernel.
1421 * LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE:
1422 * Indicates that for every process that has the 'legacy footprint entitlement',
1423 * we want it to have a higher memory limit which will help them acclimate to the
1424 * iOS 12.0 (& beyond) accounting behavior that does the right accounting.
1425 * The bonus added to the system-wide task limit to calculate this higher memory limit
1426 * is available in legacy_footprint_bonus_mb.
1429 if (legacy_footprint_entitlement_mode
< LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE
||
1430 legacy_footprint_entitlement_mode
> LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE
) {
1431 legacy_footprint_entitlement_mode
= LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE
;
1434 #endif /* __arm64__ */
1435 #endif /* DEVELOPMENT || DEBUG */
1438 #if CONFIG_BASESYSTEMROOT
1440 extern const char* IOGetBootUUID(void);
1441 extern const char* IOGetApfsPrebootUUID(void);
1443 // Get the UUID of the Preboot (and Recovery) folder associated with the
1444 // current boot volume, if applicable. The meaning of the UUID can be
1445 // filesystem-dependent and not all kinds of boots will have a UUID.
1446 // If available, the string will be returned. It does not need to be
1447 // deallocate. (Future: if we need to return the string as a copy that the
1448 // caller must free, we'll introduce a new functcion for that.)
1449 // NULL will be returned if the current boot has no applicable Preboot UUID.
1452 get_preboot_uuid(void)
1454 const char *maybe_uuid_string
;
1456 // try IOGetApfsPrebootUUID
1457 maybe_uuid_string
= IOGetApfsPrebootUUID();
1458 if (maybe_uuid_string
) {
1460 int error
= uuid_parse(maybe_uuid_string
, maybe_uuid
);
1462 return maybe_uuid_string
;
1466 // try IOGetBootUUID
1467 maybe_uuid_string
= IOGetBootUUID();
1468 if (maybe_uuid_string
) {
1470 int error
= uuid_parse(maybe_uuid_string
, maybe_uuid
);
1472 return maybe_uuid_string
;
1480 #if defined(__arm64__)
1481 extern const char *IOGetBootObjectsPath(void);
1484 // Find the BaseSystem.dmg to be used as the initial root volume during certain
1486 // This may mount volumes and lookup vnodes.
1487 // The DEVELOPMENT kernel will look for BaseSystem.rooted.dmg first.
1488 // If it returns 0 (no error), then it also writes the absolute path to the
1489 // BaseSystem.dmg into its argument (which must be a char[MAXPATHLEN]).
1492 bsd_find_basesystem_dmg(char *bsdmgpath_out
, bool *rooted_dmg
, bool *skip_signature_check
)
1498 bool allow_rooted_dmg
= false;
1500 dmgbasepath
= zalloc_flags(ZV_NAMEI
, Z_ZERO
| Z_WAITOK
);
1501 dmgpath
= zalloc_flags(ZV_NAMEI
, Z_ZERO
| Z_WAITOK
);
1502 vnode_t imagevp
= NULLVP
;
1504 #if DEVELOPMENT || DEBUG
1505 allow_rooted_dmg
= true;
1508 //must provide output bool
1509 if (rooted_dmg
&& skip_signature_check
) {
1510 *rooted_dmg
= false;
1511 *skip_signature_check
= false;
1517 error
= vfs_mount_recovery();
1522 len
= strlcpy(dmgbasepath
, "/System/Volumes/Recovery/", MAXPATHLEN
);
1523 if (len
> MAXPATHLEN
) {
1524 error
= ENAMETOOLONG
;
1528 if (csr_check(CSR_ALLOW_ANY_RECOVERY_OS
) == 0) {
1529 *skip_signature_check
= true;
1530 allow_rooted_dmg
= true;
1533 #if defined(__arm64__)
1534 const char *boot_obj_path
= IOGetBootObjectsPath();
1535 if (boot_obj_path
) {
1536 if (boot_obj_path
[0] == '/') {
1537 dmgbasepath
[len
- 1] = '\0';
1540 len
= strlcat(dmgbasepath
, boot_obj_path
, MAXPATHLEN
);
1541 if (len
> MAXPATHLEN
) {
1542 error
= ENAMETOOLONG
;
1546 len
= strlcat(dmgbasepath
, "/usr/standalone/firmware/", MAXPATHLEN
);
1547 if (len
> MAXPATHLEN
) {
1548 error
= ENAMETOOLONG
;
1552 if (allow_rooted_dmg
) {
1553 len
= strlcpy(dmgpath
, dmgbasepath
, MAXPATHLEN
);
1554 if (len
> MAXPATHLEN
) {
1555 error
= ENAMETOOLONG
;
1559 len
= strlcat(dmgpath
, "arm64eBaseSystem.rooted.dmg", MAXPATHLEN
);
1560 if (len
> MAXPATHLEN
) {
1561 error
= ENAMETOOLONG
;
1565 error
= vnode_lookup(dmgpath
, 0, &imagevp
, vfs_context_kernel());
1568 *skip_signature_check
= true;
1571 memset(dmgpath
, 0, MAXPATHLEN
);
1574 len
= strlcpy(dmgpath
, dmgbasepath
, MAXPATHLEN
);
1575 if (len
> MAXPATHLEN
) {
1576 error
= ENAMETOOLONG
;
1580 len
= strlcat(dmgpath
, "arm64eBaseSystem.dmg", MAXPATHLEN
);
1581 if (len
> MAXPATHLEN
) {
1582 error
= ENAMETOOLONG
;
1586 error
= vnode_lookup(dmgpath
, 0, &imagevp
, vfs_context_kernel());
1590 memset(dmgpath
, 0, MAXPATHLEN
);
1591 dmgbasepath
[strlen("/System/Volumes/Recovery/")] = '\0';
1595 const char *preboot_uuid
= get_preboot_uuid();
1596 if (preboot_uuid
== NULL
) {
1597 // no preboot? bail out
1601 len
= strlcat(dmgbasepath
, preboot_uuid
, MAXPATHLEN
);
1602 if (len
> MAXPATHLEN
) {
1603 error
= ENAMETOOLONG
;
1607 if (allow_rooted_dmg
) {
1608 // Try BaseSystem.rooted.dmg
1609 len
= strlcpy(dmgpath
, dmgbasepath
, MAXPATHLEN
);
1610 if (len
> MAXPATHLEN
) {
1611 error
= ENAMETOOLONG
;
1615 len
= strlcat(dmgpath
, "/BaseSystem.rooted.dmg", MAXPATHLEN
);
1616 if (len
> MAXPATHLEN
) {
1617 error
= ENAMETOOLONG
;
1621 error
= vnode_lookup(dmgpath
, 0, &imagevp
, vfs_context_kernel());
1623 // we found it! success!
1625 *skip_signature_check
= true;
1630 // Try BaseSystem.dmg
1631 len
= strlcpy(dmgpath
, dmgbasepath
, MAXPATHLEN
);
1632 if (len
> MAXPATHLEN
) {
1633 error
= ENAMETOOLONG
;
1637 len
= strlcat(dmgpath
, "/BaseSystem.dmg", MAXPATHLEN
);
1638 if (len
> MAXPATHLEN
) {
1639 error
= ENAMETOOLONG
;
1643 error
= vnode_lookup(dmgpath
, 0, &imagevp
, vfs_context_kernel());
1651 strlcpy(bsdmgpath_out
, dmgpath
, MAXPATHLEN
);
1653 bsd_init_kprintf("%s: error %d\n", __func__
, error
);
1655 if (imagevp
!= NULLVP
) {
1658 zfree(ZV_NAMEI
, dmgpath
);
1659 zfree(ZV_NAMEI
, dmgbasepath
);
1664 bsdmgroot_bootable(void)
1666 #if defined(__arm64__)
1667 #define BSDMGROOT_DEFAULT true
1669 #define BSDMGROOT_DEFAULT false
1672 boolean_t resolved
= BSDMGROOT_DEFAULT
;
1674 boolean_t boot_arg_bsdmgroot
= false;
1675 boolean_t boot_arg_nobsdmgroot
= false;
1678 boolean_t root_part_of_volume_group
= false;
1679 struct vfs_attr vfsattr
;
1681 mp
= rootvnode
->v_mount
;
1682 VFSATTR_INIT(&vfsattr
);
1683 VFSATTR_WANTED(&vfsattr
, f_capabilities
);
1685 boot_arg_bsdmgroot
= PE_parse_boot_argn("-bsdmgroot", NULL
, 0);
1686 boot_arg_nobsdmgroot
= PE_parse_boot_argn("-nobsdmgroot", NULL
, 0);
1688 error
= vfs_getattr(mp
, &vfsattr
, vfs_context_kernel());
1689 if (!error
&& VFSATTR_IS_SUPPORTED(&vfsattr
, f_capabilities
)) {
1690 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_VOL_GROUPS
) &&
1691 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_VOL_GROUPS
)) {
1692 root_part_of_volume_group
= true;
1696 boolean_t singleuser
= (boothowto
& RB_SINGLE
) != 0;
1698 // Start with the #defined default above.
1699 // If booting to single-user mode, default to false, because single-
1700 // user mode inside the BaseSystem is probably not what's wanted.
1701 // If the 'yes' boot-arg is set, we'll allow that even in single-user
1702 // mode, we'll assume you know what you're doing.
1703 // The 'no' boot-arg overpowers the 'yes' boot-arg.
1704 // In any case, we will not attempt to root from BaseSystem if the
1705 // original (booter-chosen) root volume isn't in a volume group.
1706 // This is just out of an abundance of caution: if the boot environment
1707 // seems to be "something other than a standard install",
1708 // we'll be conservative in messing with the root volume.
1714 if (boot_arg_bsdmgroot
) {
1718 if (boot_arg_nobsdmgroot
) {
1722 if (!root_part_of_volume_group
) {
1728 #endif // CONFIG_BASESYSTEMROOT
1731 bsd_exec_setup(int scale
)
1736 bsd_simul_execs
= BSD_SIMUL_EXECS
;
1740 bsd_simul_execs
= 65;
1744 bsd_simul_execs
= 129;
1748 bsd_simul_execs
= 257;
1751 bsd_simul_execs
= 513;
1754 bsd_pageable_map_size
= (bsd_simul_execs
* BSD_PAGEABLE_SIZE_PER_EXEC
);