xref: /xnu-8796.101.5/bsd/kern/bsd_init.c (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
1 /*
2  * Copyright (c) 2000-2021 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/user.h>
93 #include <sys/time.h>
94 #include <sys/systm.h>
95 #include <sys/mman.h>
96 #include <sys/kasl.h>
97 
98 #include <security/audit/audit.h>
99 
100 #include <sys/malloc.h>
101 #include <sys/dkstat.h>
102 #include <sys/codesign.h>
103 
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() */
110 #include <kern/sched_hygiene.h>
111 
112 #if (DEVELOPMENT || DEBUG)
113 #include <kern/debug.h>
114 #endif
115 
116 #include <mach/vm_param.h>
117 
118 #include <vm/vm_map.h>
119 #include <vm/vm_kern.h>
120 
121 #include <sys/reboot.h>
122 #include <dev/busvar.h>                 /* for pseudo_inits */
123 #include <sys/kdebug.h>
124 #include <sys/monotonic.h>
125 
126 #include <mach/mach_types.h>
127 #include <mach/vm_prot.h>
128 #include <mach/semaphore.h>
129 #include <mach/sync_policy.h>
130 #include <kern/clock.h>
131 #include <sys/csr.h>
132 #include <mach/kern_return.h>
133 #include <mach/thread_act.h>            /* for thread_resume() */
134 #include <sys/mcache.h>                 /* for mcache_init() */
135 #include <sys/mbuf.h>                   /* for mbinit() */
136 #include <sys/event.h>                  /* for knote_init() */
137 #include <sys/eventhandler.h>           /* for eventhandler_init() */
138 #include <sys/kern_memorystatus.h>      /* for memorystatus_init() */
139 #include <sys/kern_memorystatus_freeze.h> /* for memorystatus_freeze_init() */
140 #include <sys/aio_kern.h>               /* for aio_init() */
141 #include <sys/semaphore.h>              /* for psem_cache_init() */
142 #include <net/dlil.h>                   /* for dlil_init() */
143 #include <net/iptap.h>                  /* for iptap_init() */
144 #include <sys/socketvar.h>              /* for socketinit() */
145 #include <sys/protosw.h>                /* for domaininit() */
146 #include <kern/sched_prim.h>            /* for thread_wakeup() */
147 #include <net/if_ether.h>               /* for ether_family_init() */
148 #include <net/if_gif.h>                 /* for gif_init() */
149 #include <miscfs/devfs/devfsdefs.h>     /* for devfs_kernel_mount() */
150 #include <vm/vm_kern.h>                 /* for kmem_suballoc() */
151 #include <sys/proc_uuid_policy.h>       /* proc_uuid_policy_init() */
152 #include <netinet/flow_divert.h>        /* flow_divert_init() */
153 #include <net/content_filter.h>         /* for cfil_init() */
154 #include <net/necp.h>                   /* for necp_init() */
155 #include <net/network_agent.h>          /* for netagent_init() */
156 #include <net/packet_mangler.h>         /* for pkt_mnglr_init() */
157 #include <net/if_utun.h>                /* for utun_register_control() */
158 #include <net/if_ipsec.h>               /* for ipsec_register_control() */
159 #include <net/netsrc.h>                 /* for netsrc_init() */
160 #include <net/ntstat.h>                 /* for nstat_init() */
161 #include <netinet/tcp_cc.h>                     /* for tcp_cc_init() */
162 #include <netinet/mptcp_var.h>          /* for mptcp_control_register() */
163 #include <net/nwk_wq.h>                 /* for nwk_wq_init */
164 #include <net/restricted_in_port.h>     /* for restricted_in_port_init() */
165 #include <net/remote_vif.h>             /* for rvi_init() */
166 #include <kern/assert.h>                /* for assert() */
167 #include <sys/kern_overrides.h>         /* for init_system_override() */
168 #include <sys/lockf.h>                  /* for lf_init() */
169 #include <sys/fsctl.h>
170 
171 #include <net/init.h>
172 
173 #if CONFIG_MACF
174 #include <security/mac_framework.h>
175 #include <security/mac_internal.h>      /* mac_init_bsd() */
176 #include <security/mac_mach_internal.h> /* mac_update_task_label() */
177 #endif
178 
179 #include <machine/exec.h>
180 
181 #if CONFIG_NETBOOT
182 #include <sys/netboot.h>
183 #endif
184 
185 #if CONFIG_IMAGEBOOT
186 #include <sys/imageboot.h>
187 #endif
188 
189 #if PFLOG
190 #include <net/if_pflog.h>
191 #endif
192 
193 #if SKYWALK
194 #include <skywalk/os_skywalk_private.h>
195 #endif /* SKYWALK */
196 
197 #include <pexpert/pexpert.h>
198 #include <machine/pal_routines.h>
199 #include <console/video_console.h>
200 
201 #if CONFIG_XNUPOST
202 #include <tests/xnupost.h>
203 #endif
204 
205 void * get_user_regs(thread_t);         /* XXX kludge for <machine/thread.h> */
206 void IOKitInitializeTime(void);         /* XXX */
207 void IOSleep(unsigned int);             /* XXX */
208 void IOSetImageBoot(void);              /* XXX */
209 void loopattach(void);                  /* XXX */
210 
211 void ipc_task_enable(task_t task);
212 
213 const char *const copyright =
214     "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t"
215     "The Regents of the University of California. "
216     "All rights reserved.\n\n";
217 
218 /* Components of the first process -- never freed. */
219 SECURITY_READ_ONLY_LATE(struct vfs_context) vfs_context0;
220 
221 static struct plimit limit0;
222 static struct pstats pstats0;
223 SECURITY_READ_ONLY_LATE(proc_t) kernproc;
224 proc_t XNU_PTRAUTH_SIGNED_PTR("initproc") initproc;
225 
226 long tk_cancc;
227 long tk_nin;
228 long tk_nout;
229 long tk_rawcc;
230 
231 int lock_trace = 0;
232 /* Global variables to make pstat happy. We do swapping differently */
233 int nswdev, nswap;
234 int nswapmap;
235 void *swapmap;
236 struct swdevt swdevt[1];
237 
238 static LCK_GRP_DECLARE(hostname_lck_grp, "hostname");
239 LCK_MTX_DECLARE(hostname_lock, &hostname_lck_grp);
240 LCK_MTX_DECLARE(domainname_lock, &hostname_lck_grp);
241 
242 dev_t   rootdev;                /* device of the root */
243 dev_t   dumpdev;                /* device to take dumps on */
244 long    dumplo;                 /* offset into dumpdev */
245 long    hostid;
246 char    hostname[MAXHOSTNAMELEN];
247 char    domainname[MAXDOMNAMELEN];
248 char    rootdevice[DEVMAXNAMESIZE];
249 
250 struct  vnode *rootvp;
251 bool rootvp_is_ssd = false;
252 SECURITY_READ_ONLY_LATE(int) boothowto;
253 /*
254  * -minimalboot indicates that we want userspace to be bootstrapped to a
255  * minimal environment.  What constitutes minimal is up to the bootstrap
256  * process.
257  */
258 TUNABLE(int, minimalboot, "-minimalboot", 0);
259 #if CONFIG_DARKBOOT
260 int darkboot = 0;
261 #endif
262 
263 extern kern_return_t IOFindBSDRoot(char *, unsigned int, dev_t *, u_int32_t *);
264 extern void IOSecureBSDRoot(const char * rootName);
265 extern kern_return_t IOKitBSDInit(void );
266 extern boolean_t IOSetRecoveryBoot(bsd_bootfail_mode_t, uuid_t, boolean_t);
267 extern void kminit(void);
268 extern void bsd_bufferinit(void);
269 extern void throttle_init(void);
270 
271 vm_map_t        bsd_pageable_map;
272 vm_map_t        mb_map;
273 
274 static  int bsd_simul_execs;
275 static int bsd_pageable_map_size;
276 __private_extern__ int execargs_cache_size = 0;
277 __private_extern__ int execargs_free_count = 0;
278 __private_extern__ vm_offset_t * execargs_cache = NULL;
279 
280 void bsd_exec_setup(int);
281 
282 __private_extern__ int bootarg_execfailurereports = 0;
283 
284 #if __x86_64__
285 __private_extern__ TUNABLE(int, bootarg_no32exec, "no32exec", 1);
286 #endif
287 
288 #if DEVELOPMENT || DEBUG
289 /* Prevent kernel-based ASLR from being used. */
290 __private_extern__ TUNABLE(bool, bootarg_disable_aslr, "-disable_aslr", 0);
291 #endif
292 
293 /*
294  * Allow an alternate dyld to be used for testing.
295  */
296 
297 #if DEVELOPMENT || DEBUG
298 char dyld_alt_path[MAXPATHLEN];
299 int use_alt_dyld = 0;
300 
301 char panic_on_proc_crash[NAME_MAX];
302 int use_panic_on_proc_crash = 0;
303 
304 char panic_on_proc_exit[NAME_MAX];
305 int use_panic_on_proc_exit = 0;
306 
307 char panic_on_proc_spawn_fail[NAME_MAX];
308 int use_panic_on_proc_spawn_fail = 0;
309 
310 char dyld_suffix[NAME_MAX];
311 int use_dyld_suffix = 0;
312 #endif
313 
314 int     cmask = CMASK;
315 extern int customnbuf;
316 
317 kern_return_t bsd_autoconf(void);
318 void bsd_utaskbootstrap(void);
319 
320 #if CONFIG_DEV_KMEM
321 extern void dev_kmem_init(void);
322 #endif
323 static void process_name(const char *, proc_t);
324 
325 static void setconf(void);
326 
327 #if CONFIG_BASESYSTEMROOT
328 static int bsd_find_basesystem_dmg(char *bsdmgpath_out, bool *rooted_dmg, bool *skip_signature_check);
329 static boolean_t bsdmgroot_bootable(void);
330 #endif // CONFIG_BASESYSTEMROOT
331 
332 static bool bsd_rooted_ramdisk(void);
333 
334 #if SYSV_SHM
335 extern void sysv_shm_lock_init(void);
336 #endif
337 #if SYSV_SEM
338 extern void sysv_sem_lock_init(void);
339 #endif
340 #if SYSV_MSG
341 extern void sysv_msg_lock_init(void);
342 #endif
343 
344 #if CONFIG_MACF
345 #if defined (__i386__) || defined (__x86_64__)
346 /* MACF policy_check configuration flags; see policy_check.c for details */
347 extern int check_policy_init(int);
348 #endif
349 #endif  /* CONFIG_MACF */
350 
351 /* If we are using CONFIG_DTRACE */
352 #if CONFIG_DTRACE
353 extern void dtrace_postinit(void);
354 #endif
355 
356 /*
357  * Initialization code.
358  * Called from cold start routine as
359  * soon as a stack and segmentation
360  * have been established.
361  * Functions:
362  *	turn on clock
363  *	hand craft 0th process
364  *	call all initialization routines
365  *  hand craft 1st user process
366  */
367 
368 /*
369  *	Sets the name for the given task.
370  */
371 static void
process_name(const char * s,proc_t p)372 process_name(const char *s, proc_t p)
373 {
374 	strlcpy(p->p_comm, s, sizeof(p->p_comm));
375 	strlcpy(p->p_name, s, sizeof(p->p_name));
376 }
377 
378 /* To allow these values to be patched, they're globals here */
379 #include <machine/vmparam.h>
380 struct rlimit vm_initial_limit_stack = { .rlim_cur = DFLSSIZ, .rlim_max = MAXSSIZ - PAGE_MAX_SIZE };
381 struct rlimit vm_initial_limit_data = { .rlim_cur = DFLDSIZ, .rlim_max = MAXDSIZ };
382 struct rlimit vm_initial_limit_core = { .rlim_cur = DFLCSIZ, .rlim_max = MAXCSIZ };
383 
384 extern struct os_refgrp rlimit_refgrp;
385 
386 extern int      (*mountroot)(void);
387 
388 LCK_ATTR_DECLARE(proc_lck_attr, 0, 0);
389 LCK_GRP_DECLARE(proc_lck_grp, "proc");
390 LCK_GRP_DECLARE(proc_slock_grp, "proc-slock");
391 LCK_GRP_DECLARE(proc_fdmlock_grp, "proc-fdmlock");
392 LCK_GRP_DECLARE(proc_mlock_grp, "proc-mlock");
393 LCK_GRP_DECLARE(proc_ucred_mlock_grp, "proc-ucred-mlock");
394 LCK_GRP_DECLARE(proc_dirslock_grp, "proc-dirslock");
395 LCK_GRP_DECLARE(proc_kqhashlock_grp, "proc-kqhashlock");
396 LCK_GRP_DECLARE(proc_knhashlock_grp, "proc-knhashlock");
397 
398 
399 LCK_MTX_DECLARE_ATTR(proc_list_mlock, &proc_mlock_grp, &proc_lck_attr);
400 
401 #if XNU_TARGET_OS_OSX
402 /* hook called after root is mounted XXX temporary hack */
403 void (*mountroot_post_hook)(void);
404 void (*unmountroot_pre_hook)(void);
405 #endif
406 void set_rootvnode(vnode_t);
407 
408 extern lck_rw_t rootvnode_rw_lock;
409 
410 SECURITY_READ_ONLY_LATE(struct mach_vm_range) bsd_pageable_range = {};
411 KMEM_RANGE_REGISTER_DYNAMIC(bsd_pageable, &bsd_pageable_range, ^() {
412 	assert(bsd_pageable_map_size != 0);
413 	return (vm_map_size_t) bsd_pageable_map_size;
414 });
415 
416 /* called with an iocount and usecount on new_rootvnode */
417 void
set_rootvnode(vnode_t new_rootvnode)418 set_rootvnode(vnode_t new_rootvnode)
419 {
420 	mount_t new_mount = (new_rootvnode != NULL) ? new_rootvnode->v_mount : NULL;
421 	vnode_t new_devvp = (new_mount != NULL) ? new_mount->mnt_devvp : NULL;
422 	vnode_t old_rootvnode = rootvnode;
423 
424 	new_rootvnode->v_flag |= VROOT;
425 	rootvp = new_devvp;
426 	rootvnode = new_rootvnode;
427 	kernproc->p_fd.fd_cdir = new_rootvnode;
428 	if (new_devvp != NULL) {
429 		rootdev = vnode_specrdev(new_devvp);
430 	} else if (new_mount != NULL) {
431 		rootdev = vfs_statfs(new_mount)->f_fsid.val[0];  /* like ATTR_CMN_DEVID */
432 	} else {
433 		rootdev = NODEV;
434 	}
435 
436 	if (old_rootvnode) {
437 		vnode_rele(old_rootvnode);
438 	}
439 }
440 
441 #define RAMDEV "md0"
442 
443 bool
bsd_rooted_ramdisk(void)444 bsd_rooted_ramdisk(void)
445 {
446 	bool is_ramdisk = false;
447 	char *dev_path = zalloc(ZV_NAMEI);
448 	if (dev_path == NULL) {
449 		panic("failed to allocate devpath string!");
450 	}
451 
452 	if (PE_parse_boot_argn("rd", dev_path, MAXPATHLEN)) {
453 		if (strncmp(dev_path, RAMDEV, strlen(RAMDEV)) == 0) {
454 			is_ramdisk = true;
455 		}
456 	}
457 
458 	zfree(ZV_NAMEI, dev_path);
459 	return is_ramdisk;
460 }
461 
462 /*
463  * This function is called very early on in the Mach startup, from the
464  * function start_kernel_threads() in osfmk/kern/startup.c.  It's called
465  * in the context of the current (startup) task using a call to the
466  * function kernel_thread_create() to jump into start_kernel_threads().
467  * Internally, kernel_thread_create() calls thread_create_internal(),
468  * which calls uthread_init().  The function of uthread_init() is
469  * normally to init a uthread structure, and fill out the uu_sigmask,
470  * tro_ucred/tro_proc fields.  It skips filling these out in the case of the "task"
471  * being "kernel_task", because the order of operation is inverted.  To
472  * account for that, we need to manually fill in at least the contents
473  * of the tro_ucred field so that the uthread structure can be
474  * used like any other.
475  */
476 void
bsd_init(void)477 bsd_init(void)
478 {
479 	struct uthread *ut;
480 	vnode_t init_rootvnode = NULLVP;
481 	struct proc_ro_data kernproc_ro_data = {
482 		.p_csflags = CS_VALID,
483 	};
484 	struct task_ro_data kerntask_ro_data = { };
485 #if CONFIG_NETBOOT || CONFIG_IMAGEBOOT
486 	boolean_t       netboot = FALSE;
487 #endif
488 
489 #if (DEVELOPMENT || DEBUG)
490 	platform_stall_panic_or_spin(PLATFORM_STALL_XNU_LOCATION_BSD_INIT);
491 #endif
492 
493 #define DEBUG_BSDINIT 0
494 
495 #if DEBUG_BSDINIT
496 #define bsd_init_kprintf(x, ...) kprintf("bsd_init: " x, ## __VA_ARGS__)
497 #else
498 #define bsd_init_kprintf(x, ...)
499 #endif
500 
501 	throttle_init();
502 
503 	printf(copyright);
504 
505 #if CONFIG_DEV_KMEM
506 	bsd_init_kprintf("calling dev_kmem_init\n");
507 	dev_kmem_init();
508 #endif
509 
510 	/* Initialize kauth subsystem before instancing the first credential */
511 	bsd_init_kprintf("calling kauth_init\n");
512 	kauth_init();
513 
514 	/* kernel_task->proc = kernproc; */
515 	set_bsdtask_info(kernel_task, (void *)kernproc);
516 
517 	/* Set the parent of kernproc to itself */
518 	kernproc->p_pptr = kernproc;
519 
520 	/* Set the state to SRUN */
521 	kernproc->p_stat = SRUN;
522 
523 	/* Set the proc flags */
524 #if defined(__LP64__)
525 	kernproc->p_flag = P_SYSTEM | P_LP64;
526 #else
527 	kernproc->p_flag = P_SYSTEM;
528 #endif
529 
530 	kernproc->p_nice = NZERO;
531 	TAILQ_INIT(&kernproc->p_uthlist);
532 
533 	/* set the cred */
534 	kauth_cred_set(&kernproc_ro_data.p_ucred, vfs_context0.vc_ucred);
535 	kernproc->p_proc_ro = proc_ro_alloc(kernproc, &kernproc_ro_data,
536 	    kernel_task, &kerntask_ro_data);
537 
538 	/* give kernproc a name */
539 	bsd_init_kprintf("calling process_name\n");
540 	process_name("kernel_task", kernproc);
541 
542 	/* Allocate proc lock attribute */
543 
544 	lck_mtx_init(&kernproc->p_mlock, &proc_mlock_grp, &proc_lck_attr);
545 	lck_mtx_init(&kernproc->p_ucred_mlock, &proc_ucred_mlock_grp, &proc_lck_attr);
546 	lck_spin_init(&kernproc->p_slock, &proc_slock_grp, &proc_lck_attr);
547 
548 	/* Init the file descriptor table. */
549 	fdt_init(kernproc);
550 	kernproc->p_fd.fd_cmask = (mode_t)cmask;
551 
552 	assert(bsd_simul_execs != 0);
553 	execargs_cache_size = bsd_simul_execs;
554 	execargs_free_count = bsd_simul_execs;
555 	execargs_cache = zalloc_permanent(bsd_simul_execs * sizeof(vm_offset_t),
556 	    ZALIGN(vm_offset_t));
557 
558 	if (current_task() != kernel_task) {
559 		printf("bsd_init: We have a problem, "
560 		    "current task is not kernel task\n");
561 	}
562 
563 	bsd_init_kprintf("calling get_bsdthread_info\n");
564 	ut = current_uthread();
565 
566 #if CONFIG_MACF
567 	/*
568 	 * Initialize the MAC Framework
569 	 */
570 	mac_policy_initbsd();
571 
572 #if defined (__i386__) || defined (__x86_64__)
573 	/*
574 	 * We currently only support this on i386/x86_64, as that is the
575 	 * only lock code we have instrumented so far.
576 	 */
577 	int policy_check_flags;
578 	PE_parse_boot_argn("policy_check", &policy_check_flags, sizeof(policy_check_flags));
579 	check_policy_init(policy_check_flags);
580 #endif
581 #endif /* MAC */
582 
583 	/*
584 	 * Make a session and group
585 	 *
586 	 * No need to hold the pgrp lock,
587 	 * there are no other BSD threads yet.
588 	 */
589 	struct session *session0 = session_alloc(kernproc);
590 	struct pgrp *pgrp0 = pgrp_alloc(0, PGRP_REF_NONE);
591 	session0->s_ttypgrpid = 0;
592 	pgrp0->pg_session = session0;
593 
594 	/*
595 	 * Create process 0.
596 	 */
597 	proc_list_lock();
598 	os_ref_init_mask(&kernproc->p_refcount, P_REF_BITS, &p_refgrp, P_REF_NONE);
599 	os_ref_init_raw(&kernproc->p_waitref, &p_refgrp);
600 	proc_ref_hold_proc_task_struct(kernproc);
601 
602 	/*
603 	 * Make a group and session, then simulate pinsertchild(),
604 	 * adjusted for the kernel.
605 	 */
606 	pghash_insert_locked(pgrp0);
607 
608 	LIST_INSERT_HEAD(&pgrp0->pg_members, kernproc, p_pglist);
609 	smr_init_store(&kernproc->p_pgrp, pgrp0);
610 	LIST_INSERT_HEAD(&allproc, kernproc, p_list);
611 
612 	LIST_INSERT_HEAD(SESSHASH(0), session0, s_hash);
613 	proc_list_unlock();
614 
615 	proc_set_task(kernproc, kernel_task);
616 
617 #if DEVELOPMENT || DEBUG
618 	if (bootarg_disable_aslr) {
619 		kernproc->p_flag |= P_DISABLE_ASLR;
620 	}
621 #endif
622 
623 	TAILQ_INSERT_TAIL(&kernproc->p_uthlist, ut, uu_list);
624 
625 	bsd_init_kprintf("calling kauth_cred_create\n");
626 	/*
627 	 * Officially associate the kernel with vfs_context0.vc_ucred.
628 	 */
629 #if CONFIG_MACF
630 	mac_cred_label_associate_kernel(vfs_context0.vc_ucred);
631 #endif
632 	proc_update_creds_onproc(kernproc);
633 
634 	TAILQ_INIT(&kernproc->p_aio_activeq);
635 	TAILQ_INIT(&kernproc->p_aio_doneq);
636 	kernproc->p_aio_total_count = 0;
637 
638 	/* Create the limits structures. */
639 	for (uint32_t i = 0; i < ARRAY_COUNT(limit0.pl_rlimit); i++) {
640 		limit0.pl_rlimit[i].rlim_cur =
641 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
642 	}
643 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
644 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = maxprocperuid;
645 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
646 	limit0.pl_rlimit[RLIMIT_STACK] = vm_initial_limit_stack;
647 	limit0.pl_rlimit[RLIMIT_DATA] = vm_initial_limit_data;
648 	limit0.pl_rlimit[RLIMIT_CORE] = vm_initial_limit_core;
649 	os_ref_init_count(&limit0.pl_refcnt, &rlimit_refgrp, 1);
650 
651 	smr_init_store(&kernproc->p_limit, &limit0);
652 	kernproc->p_stats = &pstats0;
653 	kernproc->p_subsystem_root_path = NULL;
654 
655 	/*
656 	 * Charge root for one process: launchd.
657 	 */
658 	bsd_init_kprintf("calling chgproccnt\n");
659 	(void)chgproccnt(0, 1);
660 
661 	/*
662 	 *	Allocate a kernel submap for pageable memory
663 	 *	for temporary copying (execve()).
664 	 */
665 	bsd_init_kprintf("calling kmem_suballoc\n");
666 	bsd_pageable_map = kmem_suballoc(kernel_map,
667 	    &bsd_pageable_range.min_address,
668 	    (vm_size_t)bsd_pageable_map_size,
669 	    VM_MAP_CREATE_PAGEABLE,
670 	    VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
671 	    KMS_PERMANENT | KMS_NOFAIL,
672 	    VM_KERN_MEMORY_BSD).kmr_submap;
673 
674 	/*
675 	 * Initialize buffers and hash links for buffers
676 	 *
677 	 * SIDE EFFECT: Starts a thread for bcleanbuf_thread(), so must
678 	 *		happen after a credential has been associated with
679 	 *		the kernel task.
680 	 */
681 	bsd_init_kprintf("calling bsd_bufferinit\n");
682 	bsd_bufferinit();
683 
684 	/*
685 	 * Initialize the calendar.
686 	 */
687 	bsd_init_kprintf("calling IOKitInitializeTime\n");
688 	IOKitInitializeTime();
689 
690 	/* Initialize the file systems. */
691 	bsd_init_kprintf("calling vfsinit\n");
692 	vfsinit();
693 
694 #if CONFIG_PROC_UUID_POLICY
695 	/* Initial proc_uuid_policy subsystem */
696 	bsd_init_kprintf("calling proc_uuid_policy_init()\n");
697 	proc_uuid_policy_init();
698 #endif
699 
700 #if SOCKETS
701 	/* Initialize per-CPU cache allocator */
702 	mcache_init();
703 
704 	/* Initialize mbuf's. */
705 	bsd_init_kprintf("calling mbinit\n");
706 	mbinit();
707 	restricted_in_port_init();
708 #endif /* SOCKETS */
709 
710 	/*
711 	 * Initializes security event auditing.
712 	 * XXX: Should/could this occur later?
713 	 */
714 #if CONFIG_AUDIT
715 	bsd_init_kprintf("calling audit_init\n");
716 	audit_init();
717 #endif
718 
719 	/* Initialize kqueues */
720 	bsd_init_kprintf("calling knote_init\n");
721 	knote_init();
722 
723 	/* Initialize event handler */
724 	bsd_init_kprintf("calling eventhandler_init\n");
725 	eventhandler_init();
726 
727 	/* Initialize for async IO */
728 	bsd_init_kprintf("calling aio_init\n");
729 	aio_init();
730 
731 	pthread_init();
732 	/* POSIX Shm and Sem */
733 	bsd_init_kprintf("calling pshm_cache_init\n");
734 	pshm_cache_init();
735 	bsd_init_kprintf("calling psem_cache_init\n");
736 	psem_cache_init();
737 
738 	/*
739 	 * Initialize protocols.  Block reception of incoming packets
740 	 * until everything is ready.
741 	 */
742 #if NETWORKING
743 	bsd_init_kprintf("calling nwk_wq_init\n");
744 	nwk_wq_init();
745 	bsd_init_kprintf("calling dlil_init\n");
746 	dlil_init();
747 #endif /* NETWORKING */
748 #if SOCKETS
749 	bsd_init_kprintf("calling socketinit\n");
750 	socketinit();
751 	bsd_init_kprintf("calling domaininit\n");
752 	domaininit();
753 	iptap_init();
754 #if FLOW_DIVERT
755 	flow_divert_init();
756 #endif  /* FLOW_DIVERT */
757 #endif /* SOCKETS */
758 #if SKYWALK
759 	bsd_init_kprintf("calling skywalk_init\n");
760 	(void) skywalk_init();
761 #endif /* SKYWALK */
762 #if NETWORKING
763 #if NECP
764 	/* Initialize Network Extension Control Policies */
765 	necp_init();
766 #endif
767 	netagent_init();
768 #endif /* NETWORKING */
769 
770 #if CONFIG_FREEZE
771 #ifndef CONFIG_MEMORYSTATUS
772     #error "CONFIG_FREEZE defined without matching CONFIG_MEMORYSTATUS"
773 #endif
774 	/* Initialise background freezing */
775 	bsd_init_kprintf("calling memorystatus_freeze_init\n");
776 	memorystatus_freeze_init();
777 #endif
778 
779 #if CONFIG_MEMORYSTATUS
780 	/* Initialize kernel memory status notifications */
781 	bsd_init_kprintf("calling memorystatus_init\n");
782 	memorystatus_init();
783 #endif /* CONFIG_MEMORYSTATUS */
784 
785 	bsd_init_kprintf("calling sysctl_mib_init\n");
786 	sysctl_mib_init();
787 
788 	bsd_init_kprintf("calling bsd_autoconf\n");
789 	bsd_autoconf();
790 
791 #if CONFIG_DTRACE
792 	dtrace_postinit();
793 #endif
794 
795 	/*
796 	 * We attach the loopback interface *way* down here to ensure
797 	 * it happens after autoconf(), otherwise it becomes the
798 	 * "primary" interface.
799 	 */
800 #include <loop.h>
801 #if NLOOP > 0
802 	bsd_init_kprintf("calling loopattach\n");
803 	loopattach();                   /* XXX */
804 #endif
805 #if NGIF
806 	/* Initialize gif interface (after lo0) */
807 	gif_init();
808 #endif
809 
810 #if PFLOG
811 	/* Initialize packet filter log interface */
812 	pfloginit();
813 #endif /* PFLOG */
814 
815 #if NETHER > 0
816 	/* Register the built-in dlil ethernet interface family */
817 	bsd_init_kprintf("calling ether_family_init\n");
818 	ether_family_init();
819 #endif /* ETHER */
820 
821 #if NETWORKING
822 #if CONTENT_FILTER
823 	cfil_init();
824 #endif
825 
826 #if PACKET_MANGLER
827 	pkt_mnglr_init();
828 #endif
829 
830 	/*
831 	 * Register subsystems with kernel control handlers
832 	 */
833 	utun_register_control();
834 #if IPSEC
835 	ipsec_register_control();
836 #endif /* IPSEC */
837 	netsrc_init();
838 	nstat_init();
839 	tcp_cc_init();
840 #if MPTCP
841 	mptcp_control_register();
842 #endif /* MPTCP */
843 
844 #if REMOTE_VIF
845 	rvi_init();
846 #endif /* REMOTE_VIF */
847 
848 	/*
849 	 * The the networking stack is now initialized so it is a good time to call
850 	 * the clients that are waiting for the networking stack to be usable.
851 	 */
852 	bsd_init_kprintf("calling net_init_run\n");
853 	net_init_run();
854 #endif /* NETWORKING */
855 
856 	bsd_init_kprintf("calling inittodr\n");
857 	inittodr(0);
858 
859 	/* Mount the root file system. */
860 	while (TRUE) {
861 		int err;
862 
863 		bsd_init_kprintf("calling setconf\n");
864 		setconf();
865 #if CONFIG_NETBOOT
866 		netboot = (mountroot == netboot_mountroot);
867 #endif
868 
869 		bsd_init_kprintf("vfs_mountroot\n");
870 		if (0 == (err = vfs_mountroot())) {
871 			break;
872 		}
873 		rootdevice[0] = '\0';
874 #if CONFIG_NETBOOT
875 		if (netboot) {
876 			PE_display_icon( 0, "noroot");  /* XXX a netboot-specific icon would be nicer */
877 			vc_progress_set(FALSE, 0);
878 			for (uint32_t i = 1; 1; i *= 2) {
879 				printf("bsd_init: failed to mount network root, error %d, %s\n",
880 				    err, PE_boot_args());
881 				printf("We are hanging here...\n");
882 				IOSleep(i * 60 * 1000);
883 			}
884 			/*NOTREACHED*/
885 		}
886 #endif
887 		printf("cannot mount root, errno = %d\n", err);
888 	}
889 
890 	IOSecureBSDRoot(rootdevice);
891 
892 	mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
893 
894 	bsd_init_kprintf("calling VFS_ROOT\n");
895 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
896 	if (VFS_ROOT(mountlist.tqh_first, &init_rootvnode, vfs_context_kernel())) {
897 		panic("bsd_init: cannot find root vnode: %s", PE_boot_args());
898 	}
899 	(void)vnode_ref(init_rootvnode);
900 	(void)vnode_put(init_rootvnode);
901 
902 	lck_rw_lock_exclusive(&rootvnode_rw_lock);
903 	set_rootvnode(init_rootvnode);
904 	lck_rw_unlock_exclusive(&rootvnode_rw_lock);
905 	init_rootvnode = NULLVP;  /* use rootvnode after this point */
906 
907 
908 	if (!bsd_rooted_ramdisk()) {
909 		boolean_t require_rootauth = FALSE;
910 
911 #if XNU_TARGET_OS_OSX && defined(__arm64__)
912 #if CONFIG_IMAGEBOOT
913 		/* Apple Silicon MacOS */
914 		require_rootauth = !imageboot_desired();
915 #endif // CONFIG_IMAGEBOOT
916 #elif !XNU_TARGET_OS_OSX
917 		/* Non MacOS */
918 		require_rootauth = TRUE;
919 #endif // XNU_TARGET_OS_OSX && defined(__arm64__)
920 
921 		if (require_rootauth) {
922 			/* enforce sealedness */
923 			int autherr = VNOP_IOCTL(rootvnode, FSIOC_KERNEL_ROOTAUTH, NULL, 0, vfs_context_kernel());
924 			if (autherr) {
925 				panic("rootvp not authenticated after mounting");
926 			}
927 		}
928 	}
929 
930 
931 #if CONFIG_NETBOOT
932 	if (netboot) {
933 		int err;
934 
935 		netboot = TRUE;
936 		/* post mount setup */
937 		if ((err = netboot_setup()) != 0) {
938 			PE_display_icon( 0, "noroot");  /* XXX a netboot-specific icon would be nicer */
939 			vc_progress_set(FALSE, 0);
940 			for (uint32_t i = 1; 1; i *= 2) {
941 				printf("bsd_init: NetBoot could not find root, error %d: %s\n",
942 				    err, PE_boot_args());
943 				printf("We are hanging here...\n");
944 				IOSleep(i * 60 * 1000);
945 			}
946 			/*NOTREACHED*/
947 		}
948 	}
949 #endif
950 
951 
952 #if CONFIG_IMAGEBOOT
953 	/*
954 	 * See if a system disk image is present. If so, mount it and
955 	 * switch the root vnode to point to it
956 	 */
957 	imageboot_type_t imageboot_type = imageboot_needed();
958 	if (netboot == FALSE && imageboot_type) {
959 		/*
960 		 * An image was found.  No turning back: we're booted
961 		 * with a kernel from the disk image.
962 		 */
963 		bsd_init_kprintf("doing image boot: type = %d\n", imageboot_type);
964 		imageboot_setup(imageboot_type);
965 		IOSetImageBoot();
966 	}
967 
968 #endif /* CONFIG_IMAGEBOOT */
969 
970 	/* set initial time; all other resource data is  already zero'ed */
971 	microtime_with_abstime(&kernproc->p_start, &kernproc->p_stats->ps_start);
972 
973 #if DEVFS
974 	{
975 		char mounthere[] = "/dev"; /* !const because of internal casting */
976 
977 		bsd_init_kprintf("calling devfs_kernel_mount\n");
978 		devfs_kernel_mount(mounthere);
979 	}
980 #endif /* DEVFS */
981 
982 #if CONFIG_BASESYSTEMROOT
983 #if CONFIG_IMAGEBOOT
984 	if (bsdmgroot_bootable()) {
985 		int error;
986 		bool rooted_dmg = false;
987 		bool skip_signature_check = false;
988 
989 		printf("trying to find and mount BaseSystem dmg as root volume\n");
990 #if DEVELOPMENT || DEBUG
991 		printf("(set boot-arg -nobsdmgroot to avoid this)\n");
992 #endif // DEVELOPMENT || DEBUG
993 
994 		char *dmgpath = NULL;
995 		dmgpath = zalloc_flags(ZV_NAMEI, Z_ZERO | Z_WAITOK | Z_NOFAIL);
996 
997 		error = bsd_find_basesystem_dmg(dmgpath, &rooted_dmg, &skip_signature_check);
998 		if (error) {
999 			bsd_init_kprintf("failed to to find BaseSystem dmg: error = %d\n", error);
1000 		} else {
1001 			PE_parse_boot_argn("bsdmgpath", dmgpath, sizeof(dmgpath));
1002 
1003 			bsd_init_kprintf("found BaseSystem dmg at: %s\n", dmgpath);
1004 
1005 			error = imageboot_pivot_image(dmgpath, IMAGEBOOT_DMG, "/System/Volumes/BaseSystem", "System/Volumes/macOS", rooted_dmg, skip_signature_check);
1006 			if (error) {
1007 				bsd_init_kprintf("couldn't mount BaseSystem dmg: error = %d", error);
1008 			} else {
1009 				IOSetImageBoot();
1010 			}
1011 		}
1012 		zfree(ZV_NAMEI, dmgpath);
1013 	}
1014 #else /* CONFIG_IMAGEBOOT */
1015 #error CONFIG_BASESYSTEMROOT requires CONFIG_IMAGEBOOT
1016 #endif /* CONFIG_IMAGEBOOT */
1017 #endif /* CONFIG_BASESYSTEMROOT */
1018 
1019 	/* Initialize signal state for process 0. */
1020 	bsd_init_kprintf("calling siginit\n");
1021 	siginit(kernproc);
1022 
1023 	bsd_init_kprintf("calling bsd_utaskbootstrap\n");
1024 	bsd_utaskbootstrap();
1025 
1026 	pal_kernel_announce();
1027 
1028 	bsd_init_kprintf("calling mountroot_post_hook\n");
1029 
1030 #if XNU_TARGET_OS_OSX
1031 	/* invoke post-root-mount hook */
1032 	if (mountroot_post_hook != NULL) {
1033 		mountroot_post_hook();
1034 	}
1035 #endif
1036 
1037 #if 0 /* not yet */
1038 	consider_zone_gc(FALSE);
1039 #endif
1040 
1041 #if DEVELOPMENT || DEBUG
1042 	/*
1043 	 * At this point, we consider the kernel "booted" enough to apply
1044 	 * stricter timeouts. Only used for debug timeouts.
1045 	 */
1046 	machine_timeout_bsd_init();
1047 #endif /* DEVELOPMENT || DEBUG */
1048 
1049 	bsd_init_kprintf("done\n");
1050 }
1051 
1052 void
bsdinit_task(void)1053 bsdinit_task(void)
1054 {
1055 	proc_t p = current_proc();
1056 
1057 	process_name("init", p);
1058 
1059 	/* Set up exception-to-signal reflection */
1060 	ux_handler_setup();
1061 
1062 #if CONFIG_MACF
1063 	mac_cred_label_associate_user(proc_ucred(p));
1064 #endif
1065 
1066 	vm_init_before_launchd();
1067 
1068 #if CONFIG_XNUPOST
1069 	int result = bsd_list_tests();
1070 	result = bsd_do_post();
1071 	if (result != 0) {
1072 		panic("bsd_do_post: Tests failed with result = 0x%08x", result);
1073 	}
1074 #endif
1075 
1076 	bsd_init_kprintf("bsd_do_post - done");
1077 
1078 	load_init_program(p);
1079 	lock_trace = 1;
1080 }
1081 
1082 kern_return_t
bsd_autoconf(void)1083 bsd_autoconf(void)
1084 {
1085 	kprintf("bsd_autoconf: calling kminit\n");
1086 	kminit();
1087 
1088 	/*
1089 	 * Early startup for bsd pseudodevices.
1090 	 */
1091 	{
1092 		struct pseudo_init *pi;
1093 
1094 		for (pi = pseudo_inits; pi->ps_func; pi++) {
1095 			(*pi->ps_func)(pi->ps_count);
1096 		}
1097 	}
1098 
1099 	return IOKitBSDInit();
1100 }
1101 
1102 
1103 #include <sys/disklabel.h>  /* for MAXPARTITIONS */
1104 
1105 static void
setconf(void)1106 setconf(void)
1107 {
1108 	u_int32_t       flags;
1109 	kern_return_t   err;
1110 
1111 	err = IOFindBSDRoot(rootdevice, sizeof(rootdevice), &rootdev, &flags);
1112 	if (err) {
1113 		printf("setconf: IOFindBSDRoot returned an error (%d);"
1114 		    "setting rootdevice to 'sd0a'.\n", err);     /* XXX DEBUG TEMP */
1115 		rootdev = makedev( 6, 0 );
1116 		strlcpy(rootdevice, "sd0a", sizeof(rootdevice));
1117 		flags = 0;
1118 	}
1119 
1120 #if CONFIG_NETBOOT
1121 	if (flags & 1) {
1122 		/* network device */
1123 		mountroot = netboot_mountroot;
1124 	} else {
1125 #endif
1126 	/* otherwise have vfs determine root filesystem */
1127 	mountroot = NULL;
1128 #if CONFIG_NETBOOT
1129 }
1130 #endif
1131 }
1132 
1133 /*
1134  * Boot into the flavor of Recovery dictated by `mode`.
1135  */
1136 boolean_t
bsd_boot_to_recovery(bsd_bootfail_mode_t mode,uuid_t volume_uuid,boolean_t reboot)1137 bsd_boot_to_recovery(bsd_bootfail_mode_t mode, uuid_t volume_uuid, boolean_t reboot)
1138 {
1139 	return IOSetRecoveryBoot(mode, volume_uuid, reboot);
1140 }
1141 
1142 void
bsd_utaskbootstrap(void)1143 bsd_utaskbootstrap(void)
1144 {
1145 	thread_t thread;
1146 	struct uthread *ut;
1147 
1148 	/*
1149 	 * Clone the bootstrap process from the kernel process, without
1150 	 * inheriting either task characteristics or memory from the kernel;
1151 	 */
1152 	thread = cloneproc(TASK_NULL, NULL, kernproc, CLONEPROC_FLAGS_MEMSTAT_INTERNAL);
1153 
1154 	/* Hold the reference as it will be dropped during shutdown */
1155 	initproc = proc_find(1);
1156 #if __PROC_INTERNAL_DEBUG
1157 	if (initproc == PROC_NULL) {
1158 		panic("bsd_utaskbootstrap: initproc not set");
1159 	}
1160 #endif
1161 
1162 	zalloc_first_proc_made();
1163 
1164 	/*
1165 	 * Since we aren't going back out the normal way to our parent,
1166 	 * we have to drop the transition locks explicitly.
1167 	 */
1168 	proc_signalend(initproc, 0);
1169 	proc_transend(initproc, 0);
1170 
1171 	ut = (struct uthread *)get_bsdthread_info(thread);
1172 	ut->uu_sigmask = 0;
1173 	act_set_astbsd(thread);
1174 
1175 	ipc_task_enable(get_threadtask(thread));
1176 
1177 	task_clear_return_wait(get_threadtask(thread), TCRW_CLEAR_ALL_WAIT);
1178 }
1179 
1180 static void
parse_bsd_args(void)1181 parse_bsd_args(void)
1182 {
1183 	char namep[48];
1184 
1185 	if (PE_parse_boot_argn("-s", namep, sizeof(namep))) {
1186 		boothowto |= RB_SINGLE;
1187 	}
1188 
1189 	if (PE_parse_boot_argn("-x", namep, sizeof(namep))) { /* safe boot */
1190 		boothowto |= RB_SAFEBOOT;
1191 	}
1192 
1193 	if (PE_parse_boot_argn("nbuf", &max_nbuf_headers,
1194 	    sizeof(max_nbuf_headers))) {
1195 		customnbuf = 1;
1196 	}
1197 
1198 #if CONFIG_DARKBOOT
1199 	/*
1200 	 * The darkboot flag is specified by the bootloader and is stored in
1201 	 * boot_args->bootFlags. This flag is available starting revision 2.
1202 	 */
1203 	boot_args *args = (boot_args *) PE_state.bootArgs;
1204 	if ((args != NULL) && (args->Revision >= kBootArgsRevision2)) {
1205 		darkboot = (args->bootFlags & kBootFlagsDarkBoot) ? 1 : 0;
1206 	} else {
1207 		darkboot = 0;
1208 	}
1209 #endif
1210 
1211 #if DEVELOPMENT || DEBUG
1212 	if (PE_parse_boot_argn("dyldsuffix", dyld_suffix, sizeof(dyld_suffix))) {
1213 		if (strlen(dyld_suffix) > 0) {
1214 			use_dyld_suffix = 1;
1215 		}
1216 	}
1217 
1218 	if (PE_parse_boot_argn("alt-dyld", dyld_alt_path, sizeof(dyld_alt_path))) {
1219 		if (strlen(dyld_alt_path) > 0) {
1220 			use_alt_dyld = 1;
1221 		}
1222 	}
1223 
1224 	if (PE_parse_boot_arg_str("panic-on-proc-crash", panic_on_proc_crash, sizeof(panic_on_proc_crash))) {
1225 		if (strlen(panic_on_proc_crash) > 0) {
1226 			use_panic_on_proc_crash = 1;
1227 		}
1228 	}
1229 
1230 	if (PE_parse_boot_arg_str("panic-on-proc-exit", panic_on_proc_exit, sizeof(panic_on_proc_exit))) {
1231 		if (strlen(panic_on_proc_exit) > 0) {
1232 			use_panic_on_proc_exit = 1;
1233 		}
1234 	}
1235 
1236 	if (PE_parse_boot_arg_str("panic-on-proc-spawn-fail", panic_on_proc_spawn_fail, sizeof(panic_on_proc_spawn_fail))) {
1237 		if (strlen(panic_on_proc_spawn_fail) > 0) {
1238 			use_panic_on_proc_spawn_fail = 1;
1239 		}
1240 	}
1241 #endif /* DEVELOPMENT || DEBUG */
1242 }
1243 STARTUP(TUNABLES, STARTUP_RANK_MIDDLE, parse_bsd_args);
1244 
1245 #if CONFIG_BASESYSTEMROOT
1246 
1247 extern bool IOGetBootUUID(char *);
1248 extern bool IOGetApfsPrebootUUID(char *);
1249 
1250 
1251 // This function returns the UUID of the Preboot (and Recovery) folder associated with the
1252 // current boot volume, if applicable. The meaning of the UUID can be
1253 // filesystem-dependent and not all kinds of boots will have a UUID.
1254 // On success, the UUID is copied into the past-in parameter and TRUE is returned.
1255 // In case the current boot has no applicable Preboot UUID, FALSE is returned.
1256 static bool
get_preboot_uuid(uuid_string_t maybe_uuid_string)1257 get_preboot_uuid(uuid_string_t maybe_uuid_string)
1258 {
1259 	// try IOGetApfsPrebootUUID
1260 	if (IOGetApfsPrebootUUID(maybe_uuid_string)) {
1261 		uuid_t maybe_uuid;
1262 		int error = uuid_parse(maybe_uuid_string, maybe_uuid);
1263 		if (error == 0) {
1264 			return true;
1265 		}
1266 	}
1267 
1268 	// try IOGetBootUUID
1269 	if (IOGetBootUUID(maybe_uuid_string)) {
1270 		uuid_t maybe_uuid;
1271 		int error = uuid_parse(maybe_uuid_string, maybe_uuid);
1272 		if (error == 0) {
1273 			return true;
1274 		}
1275 	}
1276 
1277 	// didn't find it
1278 	return false;
1279 }
1280 
1281 #if defined(__arm64__)
1282 extern bool IOGetBootObjectsPath(char *);
1283 #endif
1284 
1285 // Find the BaseSystem.dmg to be used as the initial root volume during certain
1286 // kinds of boots.
1287 // This may mount volumes and lookup vnodes.
1288 // The DEVELOPMENT kernel will look for BaseSystem.rooted.dmg first.
1289 // If it returns 0 (no error), then it also writes the absolute path to the
1290 // BaseSystem.dmg into its argument (which must be a char[MAXPATHLEN]).
1291 static
1292 int
bsd_find_basesystem_dmg(char * bsdmgpath_out,bool * rooted_dmg,bool * skip_signature_check)1293 bsd_find_basesystem_dmg(char *bsdmgpath_out, bool *rooted_dmg, bool *skip_signature_check)
1294 {
1295 	int error;
1296 	size_t len;
1297 	char *dmgbasepath;
1298 	char *dmgpath;
1299 	bool allow_rooted_dmg = false;
1300 
1301 	dmgbasepath = zalloc_flags(ZV_NAMEI, Z_ZERO | Z_WAITOK);
1302 	dmgpath = zalloc_flags(ZV_NAMEI, Z_ZERO | Z_WAITOK);
1303 	vnode_t imagevp = NULLVP;
1304 
1305 #if DEVELOPMENT || DEBUG
1306 	allow_rooted_dmg = true;
1307 #endif
1308 
1309 	//must provide output bool
1310 	if (rooted_dmg && skip_signature_check) {
1311 		*rooted_dmg = false;
1312 		*skip_signature_check = false;
1313 	} else {
1314 		error = EINVAL;
1315 		goto done;
1316 	}
1317 
1318 	error = vfs_mount_recovery();
1319 	if (error) {
1320 		goto done;
1321 	}
1322 
1323 	len = strlcpy(dmgbasepath, "/System/Volumes/Recovery/", MAXPATHLEN);
1324 	if (len > MAXPATHLEN) {
1325 		error = ENAMETOOLONG;
1326 		goto done;
1327 	}
1328 
1329 	if (csr_check(CSR_ALLOW_ANY_RECOVERY_OS) == 0) {
1330 		*skip_signature_check = true;
1331 		allow_rooted_dmg = true;
1332 	}
1333 
1334 #if defined(__arm64__)
1335 	char boot_obj_path[MAXPATHLEN] = "";
1336 
1337 	if (IOGetBootObjectsPath(boot_obj_path)) {
1338 		if (boot_obj_path[0] == '/') {
1339 			dmgbasepath[len - 1] = '\0';
1340 		}
1341 
1342 		len = strlcat(dmgbasepath, boot_obj_path, MAXPATHLEN);
1343 		if (len > MAXPATHLEN) {
1344 			error = ENAMETOOLONG;
1345 			goto done;
1346 		}
1347 
1348 		len = strlcat(dmgbasepath, "/usr/standalone/firmware/", MAXPATHLEN);
1349 		if (len > MAXPATHLEN) {
1350 			error = ENAMETOOLONG;
1351 			goto done;
1352 		}
1353 
1354 		if (allow_rooted_dmg) {
1355 			len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1356 			if (len > MAXPATHLEN) {
1357 				error = ENAMETOOLONG;
1358 				goto done;
1359 			}
1360 
1361 			len = strlcat(dmgpath, "arm64eBaseSystem.rooted.dmg", MAXPATHLEN);
1362 			if (len > MAXPATHLEN) {
1363 				error = ENAMETOOLONG;
1364 				goto done;
1365 			}
1366 
1367 			error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1368 			if (error == 0) {
1369 				*rooted_dmg = true;
1370 				*skip_signature_check = true;
1371 				goto done;
1372 			}
1373 			memset(dmgpath, 0, MAXPATHLEN);
1374 		}
1375 
1376 		len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1377 		if (len > MAXPATHLEN) {
1378 			error = ENAMETOOLONG;
1379 			goto done;
1380 		}
1381 
1382 		len = strlcat(dmgpath, "arm64eBaseSystem.dmg", MAXPATHLEN);
1383 		if (len > MAXPATHLEN) {
1384 			error = ENAMETOOLONG;
1385 			goto done;
1386 		}
1387 
1388 		error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1389 		if (error == 0) {
1390 			goto done;
1391 		}
1392 		memset(dmgpath, 0, MAXPATHLEN);
1393 		dmgbasepath[strlen("/System/Volumes/Recovery/")] = '\0';
1394 	}
1395 #endif // __arm64__
1396 
1397 	uuid_string_t preboot_uuid;
1398 	if (!get_preboot_uuid(preboot_uuid)) {
1399 		// no preboot? bail out
1400 		return EINVAL;
1401 	}
1402 
1403 	len = strlcat(dmgbasepath, preboot_uuid, MAXPATHLEN);
1404 	if (len > MAXPATHLEN) {
1405 		error = ENAMETOOLONG;
1406 		goto done;
1407 	}
1408 
1409 	if (allow_rooted_dmg) {
1410 		// Try BaseSystem.rooted.dmg
1411 		len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1412 		if (len > MAXPATHLEN) {
1413 			error = ENAMETOOLONG;
1414 			goto done;
1415 		}
1416 
1417 		len = strlcat(dmgpath, "/BaseSystem.rooted.dmg", MAXPATHLEN);
1418 		if (len > MAXPATHLEN) {
1419 			error = ENAMETOOLONG;
1420 			goto done;
1421 		}
1422 
1423 		error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1424 		if (error == 0) {
1425 			// we found it! success!
1426 			*rooted_dmg = true;
1427 			*skip_signature_check = true;
1428 			goto done;
1429 		}
1430 	}
1431 
1432 	// Try BaseSystem.dmg
1433 	len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1434 	if (len > MAXPATHLEN) {
1435 		error = ENAMETOOLONG;
1436 		goto done;
1437 	}
1438 
1439 	len = strlcat(dmgpath, "/BaseSystem.dmg", MAXPATHLEN);
1440 	if (len > MAXPATHLEN) {
1441 		error = ENAMETOOLONG;
1442 		goto done;
1443 	}
1444 
1445 	error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1446 	if (error == 0) {
1447 		// success!
1448 		goto done;
1449 	}
1450 
1451 done:
1452 	if (error == 0) {
1453 		strlcpy(bsdmgpath_out, dmgpath, MAXPATHLEN);
1454 	} else {
1455 		bsd_init_kprintf("%s: error %d\n", __func__, error);
1456 	}
1457 	if (imagevp != NULLVP) {
1458 		vnode_put(imagevp);
1459 	}
1460 	zfree(ZV_NAMEI, dmgpath);
1461 	zfree(ZV_NAMEI, dmgbasepath);
1462 	return error;
1463 }
1464 
1465 static boolean_t
bsdmgroot_bootable(void)1466 bsdmgroot_bootable(void)
1467 {
1468 #if defined(__arm64__)
1469 #define BSDMGROOT_DEFAULT true
1470 #else
1471 #define BSDMGROOT_DEFAULT false
1472 #endif
1473 
1474 	boolean_t resolved = BSDMGROOT_DEFAULT;
1475 
1476 	boolean_t boot_arg_bsdmgroot = false;
1477 	boolean_t boot_arg_nobsdmgroot = false;
1478 	int error;
1479 	mount_t mp;
1480 	boolean_t root_part_of_volume_group = false;
1481 	struct vfs_attr vfsattr;
1482 
1483 	mp = rootvnode->v_mount;
1484 	VFSATTR_INIT(&vfsattr);
1485 	VFSATTR_WANTED(&vfsattr, f_capabilities);
1486 
1487 	boot_arg_bsdmgroot = PE_parse_boot_argn("-bsdmgroot", NULL, 0);
1488 	boot_arg_nobsdmgroot = PE_parse_boot_argn("-nobsdmgroot", NULL, 0);
1489 
1490 	error = vfs_getattr(mp, &vfsattr, vfs_context_kernel());
1491 	if (!error && VFSATTR_IS_SUPPORTED(&vfsattr, f_capabilities)) {
1492 		if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS) &&
1493 		    (vfsattr.f_capabilities.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS)) {
1494 			root_part_of_volume_group = true;
1495 		}
1496 	}
1497 
1498 	boolean_t singleuser = (boothowto & RB_SINGLE) != 0;
1499 
1500 	// Start with the #defined default above.
1501 	// If booting to single-user mode, default to false, because single-
1502 	// user mode inside the BaseSystem is probably not what's wanted.
1503 	// If the 'yes' boot-arg is set, we'll allow that even in single-user
1504 	// mode, we'll assume you know what you're doing.
1505 	// The 'no' boot-arg overpowers the 'yes' boot-arg.
1506 	// In any case, we will not attempt to root from BaseSystem if the
1507 	// original (booter-chosen) root volume isn't in a volume group.
1508 	// This is just out of an abundance of caution: if the boot environment
1509 	// seems to be "something other than a standard install",
1510 	// we'll be conservative in messing with the root volume.
1511 
1512 	if (singleuser) {
1513 		resolved = false;
1514 	}
1515 
1516 	if (boot_arg_bsdmgroot) {
1517 		resolved = true;
1518 	}
1519 
1520 	if (boot_arg_nobsdmgroot) {
1521 		resolved = false;
1522 	}
1523 
1524 	if (!root_part_of_volume_group) {
1525 		resolved = false;
1526 	}
1527 
1528 	return resolved;
1529 }
1530 #endif // CONFIG_BASESYSTEMROOT
1531 
1532 void
bsd_exec_setup(int scale)1533 bsd_exec_setup(int scale)
1534 {
1535 	switch (scale) {
1536 	case 0:
1537 	case 1:
1538 		bsd_simul_execs = BSD_SIMUL_EXECS;
1539 		break;
1540 	case 2:
1541 	case 3:
1542 		bsd_simul_execs = 65;
1543 		break;
1544 	case 4:
1545 	case 5:
1546 		bsd_simul_execs = 129;
1547 		break;
1548 	case 6:
1549 	case 7:
1550 		bsd_simul_execs = 257;
1551 		break;
1552 	default:
1553 		bsd_simul_execs = 513;
1554 		break;
1555 	}
1556 	bsd_pageable_map_size = (bsd_simul_execs * BSD_PAGEABLE_SIZE_PER_EXEC);
1557 }
1558 
1559 #if !CONFIG_NETBOOT
1560 int
1561 netboot_root(void);
1562 
1563 int
netboot_root(void)1564 netboot_root(void)
1565 {
1566 	return 0;
1567 }
1568 #endif
1569