xref: /xnu-8792.81.2/bsd/kern/bsd_init.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90) !
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(0, 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 	proc_sigacts_copy(kernproc, NULL);
654 	kernproc->p_subsystem_root_path = NULL;
655 
656 	/*
657 	 * Charge root for one process: launchd.
658 	 */
659 	bsd_init_kprintf("calling chgproccnt\n");
660 	(void)chgproccnt(0, 1);
661 
662 	/*
663 	 *	Allocate a kernel submap for pageable memory
664 	 *	for temporary copying (execve()).
665 	 */
666 	bsd_init_kprintf("calling kmem_suballoc\n");
667 	bsd_pageable_map = kmem_suballoc(kernel_map,
668 	    &bsd_pageable_range.min_address,
669 	    (vm_size_t)bsd_pageable_map_size,
670 	    VM_MAP_CREATE_PAGEABLE,
671 	    VM_FLAGS_FIXED_RANGE_SUBALLOC,
672 	    KMS_PERMANENT | KMS_NOFAIL,
673 	    VM_KERN_MEMORY_BSD).kmr_submap;
674 
675 	/*
676 	 * Initialize buffers and hash links for buffers
677 	 *
678 	 * SIDE EFFECT: Starts a thread for bcleanbuf_thread(), so must
679 	 *		happen after a credential has been associated with
680 	 *		the kernel task.
681 	 */
682 	bsd_init_kprintf("calling bsd_bufferinit\n");
683 	bsd_bufferinit();
684 
685 	/*
686 	 * Initialize the calendar.
687 	 */
688 	bsd_init_kprintf("calling IOKitInitializeTime\n");
689 	IOKitInitializeTime();
690 
691 	/* Initialize the file systems. */
692 	bsd_init_kprintf("calling vfsinit\n");
693 	vfsinit();
694 
695 #if CONFIG_PROC_UUID_POLICY
696 	/* Initial proc_uuid_policy subsystem */
697 	bsd_init_kprintf("calling proc_uuid_policy_init()\n");
698 	proc_uuid_policy_init();
699 #endif
700 
701 #if SOCKETS
702 	/* Initialize per-CPU cache allocator */
703 	mcache_init();
704 
705 	/* Initialize mbuf's. */
706 	bsd_init_kprintf("calling mbinit\n");
707 	mbinit();
708 	restricted_in_port_init();
709 #endif /* SOCKETS */
710 
711 	/*
712 	 * Initializes security event auditing.
713 	 * XXX: Should/could this occur later?
714 	 */
715 #if CONFIG_AUDIT
716 	bsd_init_kprintf("calling audit_init\n");
717 	audit_init();
718 #endif
719 
720 	/* Initialize kqueues */
721 	bsd_init_kprintf("calling knote_init\n");
722 	knote_init();
723 
724 	/* Initialize event handler */
725 	bsd_init_kprintf("calling eventhandler_init\n");
726 	eventhandler_init();
727 
728 	/* Initialize for async IO */
729 	bsd_init_kprintf("calling aio_init\n");
730 	aio_init();
731 
732 	pthread_init();
733 	/* POSIX Shm and Sem */
734 	bsd_init_kprintf("calling pshm_cache_init\n");
735 	pshm_cache_init();
736 	bsd_init_kprintf("calling psem_cache_init\n");
737 	psem_cache_init();
738 
739 	/*
740 	 * Initialize protocols.  Block reception of incoming packets
741 	 * until everything is ready.
742 	 */
743 #if NETWORKING
744 	bsd_init_kprintf("calling nwk_wq_init\n");
745 	nwk_wq_init();
746 	bsd_init_kprintf("calling dlil_init\n");
747 	dlil_init();
748 #endif /* NETWORKING */
749 #if SOCKETS
750 	bsd_init_kprintf("calling socketinit\n");
751 	socketinit();
752 	bsd_init_kprintf("calling domaininit\n");
753 	domaininit();
754 	iptap_init();
755 #if FLOW_DIVERT
756 	flow_divert_init();
757 #endif  /* FLOW_DIVERT */
758 #endif /* SOCKETS */
759 #if SKYWALK
760 	bsd_init_kprintf("calling skywalk_init\n");
761 	(void) skywalk_init();
762 #endif /* SKYWALK */
763 #if NETWORKING
764 #if NECP
765 	/* Initialize Network Extension Control Policies */
766 	necp_init();
767 #endif
768 	netagent_init();
769 #endif /* NETWORKING */
770 
771 #if CONFIG_FREEZE
772 #ifndef CONFIG_MEMORYSTATUS
773     #error "CONFIG_FREEZE defined without matching CONFIG_MEMORYSTATUS"
774 #endif
775 	/* Initialise background freezing */
776 	bsd_init_kprintf("calling memorystatus_freeze_init\n");
777 	memorystatus_freeze_init();
778 #endif
779 
780 #if CONFIG_MEMORYSTATUS
781 	/* Initialize kernel memory status notifications */
782 	bsd_init_kprintf("calling memorystatus_init\n");
783 	memorystatus_init();
784 #endif /* CONFIG_MEMORYSTATUS */
785 
786 	bsd_init_kprintf("calling sysctl_mib_init\n");
787 	sysctl_mib_init();
788 
789 	bsd_init_kprintf("calling bsd_autoconf\n");
790 	bsd_autoconf();
791 
792 #if CONFIG_DTRACE
793 	dtrace_postinit();
794 #endif
795 
796 	/*
797 	 * We attach the loopback interface *way* down here to ensure
798 	 * it happens after autoconf(), otherwise it becomes the
799 	 * "primary" interface.
800 	 */
801 #include <loop.h>
802 #if NLOOP > 0
803 	bsd_init_kprintf("calling loopattach\n");
804 	loopattach();                   /* XXX */
805 #endif
806 #if NGIF
807 	/* Initialize gif interface (after lo0) */
808 	gif_init();
809 #endif
810 
811 #if PFLOG
812 	/* Initialize packet filter log interface */
813 	pfloginit();
814 #endif /* PFLOG */
815 
816 #if NETHER > 0
817 	/* Register the built-in dlil ethernet interface family */
818 	bsd_init_kprintf("calling ether_family_init\n");
819 	ether_family_init();
820 #endif /* ETHER */
821 
822 #if NETWORKING
823 #if CONTENT_FILTER
824 	cfil_init();
825 #endif
826 
827 #if PACKET_MANGLER
828 	pkt_mnglr_init();
829 #endif
830 
831 	/*
832 	 * Register subsystems with kernel control handlers
833 	 */
834 	utun_register_control();
835 #if IPSEC
836 	ipsec_register_control();
837 #endif /* IPSEC */
838 	netsrc_init();
839 	nstat_init();
840 	tcp_cc_init();
841 #if MPTCP
842 	mptcp_control_register();
843 #endif /* MPTCP */
844 
845 #if REMOTE_VIF
846 	rvi_init();
847 #endif /* REMOTE_VIF */
848 
849 	/*
850 	 * The the networking stack is now initialized so it is a good time to call
851 	 * the clients that are waiting for the networking stack to be usable.
852 	 */
853 	bsd_init_kprintf("calling net_init_run\n");
854 	net_init_run();
855 #endif /* NETWORKING */
856 
857 	bsd_init_kprintf("calling inittodr\n");
858 	inittodr(0);
859 
860 	/* Mount the root file system. */
861 	while (TRUE) {
862 		int err;
863 
864 		bsd_init_kprintf("calling setconf\n");
865 		setconf();
866 #if CONFIG_NETBOOT
867 		netboot = (mountroot == netboot_mountroot);
868 #endif
869 
870 		bsd_init_kprintf("vfs_mountroot\n");
871 		if (0 == (err = vfs_mountroot())) {
872 			break;
873 		}
874 		rootdevice[0] = '\0';
875 #if CONFIG_NETBOOT
876 		if (netboot) {
877 			PE_display_icon( 0, "noroot");  /* XXX a netboot-specific icon would be nicer */
878 			vc_progress_set(FALSE, 0);
879 			for (uint32_t i = 1; 1; i *= 2) {
880 				printf("bsd_init: failed to mount network root, error %d, %s\n",
881 				    err, PE_boot_args());
882 				printf("We are hanging here...\n");
883 				IOSleep(i * 60 * 1000);
884 			}
885 			/*NOTREACHED*/
886 		}
887 #endif
888 		printf("cannot mount root, errno = %d\n", err);
889 	}
890 
891 	IOSecureBSDRoot(rootdevice);
892 
893 	mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
894 
895 	bsd_init_kprintf("calling VFS_ROOT\n");
896 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
897 	if (VFS_ROOT(mountlist.tqh_first, &init_rootvnode, vfs_context_kernel())) {
898 		panic("bsd_init: cannot find root vnode: %s", PE_boot_args());
899 	}
900 	(void)vnode_ref(init_rootvnode);
901 	(void)vnode_put(init_rootvnode);
902 
903 	lck_rw_lock_exclusive(&rootvnode_rw_lock);
904 	set_rootvnode(init_rootvnode);
905 	lck_rw_unlock_exclusive(&rootvnode_rw_lock);
906 	init_rootvnode = NULLVP;  /* use rootvnode after this point */
907 
908 
909 	if (!bsd_rooted_ramdisk()) {
910 		boolean_t require_rootauth = FALSE;
911 
912 #if XNU_TARGET_OS_OSX && defined(__arm64__)
913 #if CONFIG_IMAGEBOOT
914 		/* Apple Silicon MacOS */
915 		require_rootauth = !imageboot_desired();
916 #endif // CONFIG_IMAGEBOOT
917 #elif !XNU_TARGET_OS_OSX
918 		/* Non MacOS */
919 		require_rootauth = TRUE;
920 #endif // XNU_TARGET_OS_OSX && defined(__arm64__)
921 
922 		if (require_rootauth) {
923 			/* enforce sealedness */
924 			int autherr = VNOP_IOCTL(rootvnode, FSIOC_KERNEL_ROOTAUTH, NULL, 0, vfs_context_kernel());
925 			if (autherr) {
926 				panic("rootvp not authenticated after mounting");
927 			}
928 		}
929 	}
930 
931 
932 #if CONFIG_NETBOOT
933 	if (netboot) {
934 		int err;
935 
936 		netboot = TRUE;
937 		/* post mount setup */
938 		if ((err = netboot_setup()) != 0) {
939 			PE_display_icon( 0, "noroot");  /* XXX a netboot-specific icon would be nicer */
940 			vc_progress_set(FALSE, 0);
941 			for (uint32_t i = 1; 1; i *= 2) {
942 				printf("bsd_init: NetBoot could not find root, error %d: %s\n",
943 				    err, PE_boot_args());
944 				printf("We are hanging here...\n");
945 				IOSleep(i * 60 * 1000);
946 			}
947 			/*NOTREACHED*/
948 		}
949 	}
950 #endif
951 
952 
953 #if CONFIG_IMAGEBOOT
954 	/*
955 	 * See if a system disk image is present. If so, mount it and
956 	 * switch the root vnode to point to it
957 	 */
958 	imageboot_type_t imageboot_type = imageboot_needed();
959 	if (netboot == FALSE && imageboot_type) {
960 		/*
961 		 * An image was found.  No turning back: we're booted
962 		 * with a kernel from the disk image.
963 		 */
964 		bsd_init_kprintf("doing image boot: type = %d\n", imageboot_type);
965 		imageboot_setup(imageboot_type);
966 		IOSetImageBoot();
967 	}
968 
969 #endif /* CONFIG_IMAGEBOOT */
970 
971 	/* set initial time; all other resource data is  already zero'ed */
972 	microtime_with_abstime(&kernproc->p_start, &kernproc->p_stats->ps_start);
973 
974 #if DEVFS
975 	{
976 		char mounthere[] = "/dev"; /* !const because of internal casting */
977 
978 		bsd_init_kprintf("calling devfs_kernel_mount\n");
979 		devfs_kernel_mount(mounthere);
980 	}
981 #endif /* DEVFS */
982 
983 #if CONFIG_BASESYSTEMROOT
984 #if CONFIG_IMAGEBOOT
985 	if (bsdmgroot_bootable()) {
986 		int error;
987 		bool rooted_dmg = false;
988 		bool skip_signature_check = false;
989 
990 		printf("trying to find and mount BaseSystem dmg as root volume\n");
991 #if DEVELOPMENT || DEBUG
992 		printf("(set boot-arg -nobsdmgroot to avoid this)\n");
993 #endif // DEVELOPMENT || DEBUG
994 
995 		char *dmgpath = NULL;
996 		dmgpath = zalloc_flags(ZV_NAMEI, Z_ZERO | Z_WAITOK | Z_NOFAIL);
997 
998 		error = bsd_find_basesystem_dmg(dmgpath, &rooted_dmg, &skip_signature_check);
999 		if (error) {
1000 			bsd_init_kprintf("failed to to find BaseSystem dmg: error = %d\n", error);
1001 		} else {
1002 			PE_parse_boot_argn("bsdmgpath", dmgpath, sizeof(dmgpath));
1003 
1004 			bsd_init_kprintf("found BaseSystem dmg at: %s\n", dmgpath);
1005 
1006 			error = imageboot_pivot_image(dmgpath, IMAGEBOOT_DMG, "/System/Volumes/BaseSystem", "System/Volumes/macOS", rooted_dmg, skip_signature_check);
1007 			if (error) {
1008 				bsd_init_kprintf("couldn't mount BaseSystem dmg: error = %d", error);
1009 			} else {
1010 				IOSetImageBoot();
1011 			}
1012 		}
1013 		zfree(ZV_NAMEI, dmgpath);
1014 	}
1015 #else /* CONFIG_IMAGEBOOT */
1016 #error CONFIG_BASESYSTEMROOT requires CONFIG_IMAGEBOOT
1017 #endif /* CONFIG_IMAGEBOOT */
1018 #endif /* CONFIG_BASESYSTEMROOT */
1019 
1020 	/* Initialize signal state for process 0. */
1021 	bsd_init_kprintf("calling siginit\n");
1022 	siginit(kernproc);
1023 
1024 	bsd_init_kprintf("calling bsd_utaskbootstrap\n");
1025 	bsd_utaskbootstrap();
1026 
1027 	pal_kernel_announce();
1028 
1029 	bsd_init_kprintf("calling mountroot_post_hook\n");
1030 
1031 #if XNU_TARGET_OS_OSX
1032 	/* invoke post-root-mount hook */
1033 	if (mountroot_post_hook != NULL) {
1034 		mountroot_post_hook();
1035 	}
1036 #endif
1037 
1038 #if 0 /* not yet */
1039 	consider_zone_gc(FALSE);
1040 #endif
1041 
1042 	/*
1043 	 * At this point, we consider the kernel "booted" enough to apply
1044 	 * stricter timeouts.
1045 	 */
1046 	machine_timeout_bsd_init();
1047 
1048 	bsd_init_kprintf("done\n");
1049 }
1050 
1051 void
bsdinit_task(void)1052 bsdinit_task(void)
1053 {
1054 	proc_t p = current_proc();
1055 
1056 	process_name("init", p);
1057 
1058 	/* Set up exception-to-signal reflection */
1059 	ux_handler_setup();
1060 
1061 #if CONFIG_MACF
1062 	mac_cred_label_associate_user(proc_ucred(p));
1063 #endif
1064 
1065 	vm_init_before_launchd();
1066 
1067 #if CONFIG_XNUPOST
1068 	int result = bsd_list_tests();
1069 	result = bsd_do_post();
1070 	if (result != 0) {
1071 		panic("bsd_do_post: Tests failed with result = 0x%08x", result);
1072 	}
1073 #endif
1074 
1075 	bsd_init_kprintf("bsd_do_post - done");
1076 
1077 	load_init_program(p);
1078 	lock_trace = 1;
1079 }
1080 
1081 kern_return_t
bsd_autoconf(void)1082 bsd_autoconf(void)
1083 {
1084 	kprintf("bsd_autoconf: calling kminit\n");
1085 	kminit();
1086 
1087 	/*
1088 	 * Early startup for bsd pseudodevices.
1089 	 */
1090 	{
1091 		struct pseudo_init *pi;
1092 
1093 		for (pi = pseudo_inits; pi->ps_func; pi++) {
1094 			(*pi->ps_func)(pi->ps_count);
1095 		}
1096 	}
1097 
1098 	return IOKitBSDInit();
1099 }
1100 
1101 
1102 #include <sys/disklabel.h>  /* for MAXPARTITIONS */
1103 
1104 static void
setconf(void)1105 setconf(void)
1106 {
1107 	u_int32_t       flags;
1108 	kern_return_t   err;
1109 
1110 	err = IOFindBSDRoot(rootdevice, sizeof(rootdevice), &rootdev, &flags);
1111 	if (err) {
1112 		printf("setconf: IOFindBSDRoot returned an error (%d);"
1113 		    "setting rootdevice to 'sd0a'.\n", err);     /* XXX DEBUG TEMP */
1114 		rootdev = makedev( 6, 0 );
1115 		strlcpy(rootdevice, "sd0a", sizeof(rootdevice));
1116 		flags = 0;
1117 	}
1118 
1119 #if CONFIG_NETBOOT
1120 	if (flags & 1) {
1121 		/* network device */
1122 		mountroot = netboot_mountroot;
1123 	} else {
1124 #endif
1125 	/* otherwise have vfs determine root filesystem */
1126 	mountroot = NULL;
1127 #if CONFIG_NETBOOT
1128 }
1129 #endif
1130 }
1131 
1132 /*
1133  * Boot into the flavor of Recovery dictated by `mode`.
1134  */
1135 boolean_t
bsd_boot_to_recovery(bsd_bootfail_mode_t mode,uuid_t volume_uuid,boolean_t reboot)1136 bsd_boot_to_recovery(bsd_bootfail_mode_t mode, uuid_t volume_uuid, boolean_t reboot)
1137 {
1138 	return IOSetRecoveryBoot(mode, volume_uuid, reboot);
1139 }
1140 
1141 void
bsd_utaskbootstrap(void)1142 bsd_utaskbootstrap(void)
1143 {
1144 	thread_t thread;
1145 	struct uthread *ut;
1146 
1147 	/*
1148 	 * Clone the bootstrap process from the kernel process, without
1149 	 * inheriting either task characteristics or memory from the kernel;
1150 	 */
1151 	thread = cloneproc(TASK_NULL, NULL, kernproc, CLONEPROC_FLAGS_MEMSTAT_INTERNAL);
1152 
1153 	/* Hold the reference as it will be dropped during shutdown */
1154 	initproc = proc_find(1);
1155 #if __PROC_INTERNAL_DEBUG
1156 	if (initproc == PROC_NULL) {
1157 		panic("bsd_utaskbootstrap: initproc not set");
1158 	}
1159 #endif
1160 
1161 	zalloc_first_proc_made();
1162 
1163 	/*
1164 	 * Since we aren't going back out the normal way to our parent,
1165 	 * we have to drop the transition locks explicitly.
1166 	 */
1167 	proc_signalend(initproc, 0);
1168 	proc_transend(initproc, 0);
1169 
1170 	ut = (struct uthread *)get_bsdthread_info(thread);
1171 	ut->uu_sigmask = 0;
1172 	act_set_astbsd(thread);
1173 
1174 	ipc_task_enable(get_threadtask(thread));
1175 
1176 	task_clear_return_wait(get_threadtask(thread), TCRW_CLEAR_ALL_WAIT);
1177 }
1178 
1179 static void
parse_bsd_args(void)1180 parse_bsd_args(void)
1181 {
1182 	char namep[48];
1183 
1184 	if (PE_parse_boot_argn("-s", namep, sizeof(namep))) {
1185 		boothowto |= RB_SINGLE;
1186 	}
1187 
1188 	if (PE_parse_boot_argn("-x", namep, sizeof(namep))) { /* safe boot */
1189 		boothowto |= RB_SAFEBOOT;
1190 	}
1191 
1192 	if (PE_parse_boot_argn("nbuf", &max_nbuf_headers,
1193 	    sizeof(max_nbuf_headers))) {
1194 		customnbuf = 1;
1195 	}
1196 
1197 #if CONFIG_DARKBOOT
1198 	/*
1199 	 * The darkboot flag is specified by the bootloader and is stored in
1200 	 * boot_args->bootFlags. This flag is available starting revision 2.
1201 	 */
1202 	boot_args *args = (boot_args *) PE_state.bootArgs;
1203 	if ((args != NULL) && (args->Revision >= kBootArgsRevision2)) {
1204 		darkboot = (args->bootFlags & kBootFlagsDarkBoot) ? 1 : 0;
1205 	} else {
1206 		darkboot = 0;
1207 	}
1208 #endif
1209 
1210 #if DEVELOPMENT || DEBUG
1211 	if (PE_parse_boot_argn("dyldsuffix", dyld_suffix, sizeof(dyld_suffix))) {
1212 		if (strlen(dyld_suffix) > 0) {
1213 			use_dyld_suffix = 1;
1214 		}
1215 	}
1216 
1217 	if (PE_parse_boot_argn("alt-dyld", dyld_alt_path, sizeof(dyld_alt_path))) {
1218 		if (strlen(dyld_alt_path) > 0) {
1219 			use_alt_dyld = 1;
1220 		}
1221 	}
1222 
1223 	if (PE_parse_boot_arg_str("panic-on-proc-crash", panic_on_proc_crash, sizeof(panic_on_proc_crash))) {
1224 		if (strlen(panic_on_proc_crash) > 0) {
1225 			use_panic_on_proc_crash = 1;
1226 		}
1227 	}
1228 
1229 	if (PE_parse_boot_arg_str("panic-on-proc-exit", panic_on_proc_exit, sizeof(panic_on_proc_exit))) {
1230 		if (strlen(panic_on_proc_exit) > 0) {
1231 			use_panic_on_proc_exit = 1;
1232 		}
1233 	}
1234 
1235 	if (PE_parse_boot_arg_str("panic-on-proc-spawn-fail", panic_on_proc_spawn_fail, sizeof(panic_on_proc_spawn_fail))) {
1236 		if (strlen(panic_on_proc_spawn_fail) > 0) {
1237 			use_panic_on_proc_spawn_fail = 1;
1238 		}
1239 	}
1240 #endif /* DEVELOPMENT || DEBUG */
1241 }
1242 STARTUP(TUNABLES, STARTUP_RANK_MIDDLE, parse_bsd_args);
1243 
1244 #if CONFIG_BASESYSTEMROOT
1245 
1246 extern bool IOGetBootUUID(char *);
1247 extern bool IOGetApfsPrebootUUID(char *);
1248 
1249 
1250 // This function returns the UUID of the Preboot (and Recovery) folder associated with the
1251 // current boot volume, if applicable. The meaning of the UUID can be
1252 // filesystem-dependent and not all kinds of boots will have a UUID.
1253 // On success, the UUID is copied into the past-in parameter and TRUE is returned.
1254 // In case the current boot has no applicable Preboot UUID, FALSE is returned.
1255 static bool
get_preboot_uuid(uuid_string_t maybe_uuid_string)1256 get_preboot_uuid(uuid_string_t maybe_uuid_string)
1257 {
1258 	// try IOGetApfsPrebootUUID
1259 	if (IOGetApfsPrebootUUID(maybe_uuid_string)) {
1260 		uuid_t maybe_uuid;
1261 		int error = uuid_parse(maybe_uuid_string, maybe_uuid);
1262 		if (error == 0) {
1263 			return true;
1264 		}
1265 	}
1266 
1267 	// try IOGetBootUUID
1268 	if (IOGetBootUUID(maybe_uuid_string)) {
1269 		uuid_t maybe_uuid;
1270 		int error = uuid_parse(maybe_uuid_string, maybe_uuid);
1271 		if (error == 0) {
1272 			return true;
1273 		}
1274 	}
1275 
1276 	// didn't find it
1277 	return false;
1278 }
1279 
1280 #if defined(__arm64__)
1281 extern bool IOGetBootObjectsPath(char *);
1282 #endif
1283 
1284 // Find the BaseSystem.dmg to be used as the initial root volume during certain
1285 // kinds of boots.
1286 // This may mount volumes and lookup vnodes.
1287 // The DEVELOPMENT kernel will look for BaseSystem.rooted.dmg first.
1288 // If it returns 0 (no error), then it also writes the absolute path to the
1289 // BaseSystem.dmg into its argument (which must be a char[MAXPATHLEN]).
1290 static
1291 int
bsd_find_basesystem_dmg(char * bsdmgpath_out,bool * rooted_dmg,bool * skip_signature_check)1292 bsd_find_basesystem_dmg(char *bsdmgpath_out, bool *rooted_dmg, bool *skip_signature_check)
1293 {
1294 	int error;
1295 	size_t len;
1296 	char *dmgbasepath;
1297 	char *dmgpath;
1298 	bool allow_rooted_dmg = false;
1299 
1300 	dmgbasepath = zalloc_flags(ZV_NAMEI, Z_ZERO | Z_WAITOK);
1301 	dmgpath = zalloc_flags(ZV_NAMEI, Z_ZERO | Z_WAITOK);
1302 	vnode_t imagevp = NULLVP;
1303 
1304 #if DEVELOPMENT || DEBUG
1305 	allow_rooted_dmg = true;
1306 #endif
1307 
1308 	//must provide output bool
1309 	if (rooted_dmg && skip_signature_check) {
1310 		*rooted_dmg = false;
1311 		*skip_signature_check = false;
1312 	} else {
1313 		error = EINVAL;
1314 		goto done;
1315 	}
1316 
1317 	error = vfs_mount_recovery();
1318 	if (error) {
1319 		goto done;
1320 	}
1321 
1322 	len = strlcpy(dmgbasepath, "/System/Volumes/Recovery/", MAXPATHLEN);
1323 	if (len > MAXPATHLEN) {
1324 		error = ENAMETOOLONG;
1325 		goto done;
1326 	}
1327 
1328 	if (csr_check(CSR_ALLOW_ANY_RECOVERY_OS) == 0) {
1329 		*skip_signature_check = true;
1330 		allow_rooted_dmg = true;
1331 	}
1332 
1333 #if defined(__arm64__)
1334 	char boot_obj_path[MAXPATHLEN] = "";
1335 
1336 	if (IOGetBootObjectsPath(boot_obj_path)) {
1337 		if (boot_obj_path[0] == '/') {
1338 			dmgbasepath[len - 1] = '\0';
1339 		}
1340 
1341 		len = strlcat(dmgbasepath, boot_obj_path, MAXPATHLEN);
1342 		if (len > MAXPATHLEN) {
1343 			error = ENAMETOOLONG;
1344 			goto done;
1345 		}
1346 
1347 		len = strlcat(dmgbasepath, "/usr/standalone/firmware/", MAXPATHLEN);
1348 		if (len > MAXPATHLEN) {
1349 			error = ENAMETOOLONG;
1350 			goto done;
1351 		}
1352 
1353 		if (allow_rooted_dmg) {
1354 			len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1355 			if (len > MAXPATHLEN) {
1356 				error = ENAMETOOLONG;
1357 				goto done;
1358 			}
1359 
1360 			len = strlcat(dmgpath, "arm64eBaseSystem.rooted.dmg", MAXPATHLEN);
1361 			if (len > MAXPATHLEN) {
1362 				error = ENAMETOOLONG;
1363 				goto done;
1364 			}
1365 
1366 			error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1367 			if (error == 0) {
1368 				*rooted_dmg = true;
1369 				*skip_signature_check = true;
1370 				goto done;
1371 			}
1372 			memset(dmgpath, 0, MAXPATHLEN);
1373 		}
1374 
1375 		len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1376 		if (len > MAXPATHLEN) {
1377 			error = ENAMETOOLONG;
1378 			goto done;
1379 		}
1380 
1381 		len = strlcat(dmgpath, "arm64eBaseSystem.dmg", MAXPATHLEN);
1382 		if (len > MAXPATHLEN) {
1383 			error = ENAMETOOLONG;
1384 			goto done;
1385 		}
1386 
1387 		error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1388 		if (error == 0) {
1389 			goto done;
1390 		}
1391 		memset(dmgpath, 0, MAXPATHLEN);
1392 		dmgbasepath[strlen("/System/Volumes/Recovery/")] = '\0';
1393 	}
1394 #endif // __arm64__
1395 
1396 	uuid_string_t preboot_uuid;
1397 	if (!get_preboot_uuid(preboot_uuid)) {
1398 		// no preboot? bail out
1399 		return EINVAL;
1400 	}
1401 
1402 	len = strlcat(dmgbasepath, preboot_uuid, MAXPATHLEN);
1403 	if (len > MAXPATHLEN) {
1404 		error = ENAMETOOLONG;
1405 		goto done;
1406 	}
1407 
1408 	if (allow_rooted_dmg) {
1409 		// Try BaseSystem.rooted.dmg
1410 		len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1411 		if (len > MAXPATHLEN) {
1412 			error = ENAMETOOLONG;
1413 			goto done;
1414 		}
1415 
1416 		len = strlcat(dmgpath, "/BaseSystem.rooted.dmg", MAXPATHLEN);
1417 		if (len > MAXPATHLEN) {
1418 			error = ENAMETOOLONG;
1419 			goto done;
1420 		}
1421 
1422 		error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1423 		if (error == 0) {
1424 			// we found it! success!
1425 			*rooted_dmg = true;
1426 			*skip_signature_check = true;
1427 			goto done;
1428 		}
1429 	}
1430 
1431 	// Try BaseSystem.dmg
1432 	len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1433 	if (len > MAXPATHLEN) {
1434 		error = ENAMETOOLONG;
1435 		goto done;
1436 	}
1437 
1438 	len = strlcat(dmgpath, "/BaseSystem.dmg", MAXPATHLEN);
1439 	if (len > MAXPATHLEN) {
1440 		error = ENAMETOOLONG;
1441 		goto done;
1442 	}
1443 
1444 	error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1445 	if (error == 0) {
1446 		// success!
1447 		goto done;
1448 	}
1449 
1450 done:
1451 	if (error == 0) {
1452 		strlcpy(bsdmgpath_out, dmgpath, MAXPATHLEN);
1453 	} else {
1454 		bsd_init_kprintf("%s: error %d\n", __func__, error);
1455 	}
1456 	if (imagevp != NULLVP) {
1457 		vnode_put(imagevp);
1458 	}
1459 	zfree(ZV_NAMEI, dmgpath);
1460 	zfree(ZV_NAMEI, dmgbasepath);
1461 	return error;
1462 }
1463 
1464 static boolean_t
bsdmgroot_bootable(void)1465 bsdmgroot_bootable(void)
1466 {
1467 #if defined(__arm64__)
1468 #define BSDMGROOT_DEFAULT true
1469 #else
1470 #define BSDMGROOT_DEFAULT false
1471 #endif
1472 
1473 	boolean_t resolved = BSDMGROOT_DEFAULT;
1474 
1475 	boolean_t boot_arg_bsdmgroot = false;
1476 	boolean_t boot_arg_nobsdmgroot = false;
1477 	int error;
1478 	mount_t mp;
1479 	boolean_t root_part_of_volume_group = false;
1480 	struct vfs_attr vfsattr;
1481 
1482 	mp = rootvnode->v_mount;
1483 	VFSATTR_INIT(&vfsattr);
1484 	VFSATTR_WANTED(&vfsattr, f_capabilities);
1485 
1486 	boot_arg_bsdmgroot = PE_parse_boot_argn("-bsdmgroot", NULL, 0);
1487 	boot_arg_nobsdmgroot = PE_parse_boot_argn("-nobsdmgroot", NULL, 0);
1488 
1489 	error = vfs_getattr(mp, &vfsattr, vfs_context_kernel());
1490 	if (!error && VFSATTR_IS_SUPPORTED(&vfsattr, f_capabilities)) {
1491 		if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS) &&
1492 		    (vfsattr.f_capabilities.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS)) {
1493 			root_part_of_volume_group = true;
1494 		}
1495 	}
1496 
1497 	boolean_t singleuser = (boothowto & RB_SINGLE) != 0;
1498 
1499 	// Start with the #defined default above.
1500 	// If booting to single-user mode, default to false, because single-
1501 	// user mode inside the BaseSystem is probably not what's wanted.
1502 	// If the 'yes' boot-arg is set, we'll allow that even in single-user
1503 	// mode, we'll assume you know what you're doing.
1504 	// The 'no' boot-arg overpowers the 'yes' boot-arg.
1505 	// In any case, we will not attempt to root from BaseSystem if the
1506 	// original (booter-chosen) root volume isn't in a volume group.
1507 	// This is just out of an abundance of caution: if the boot environment
1508 	// seems to be "something other than a standard install",
1509 	// we'll be conservative in messing with the root volume.
1510 
1511 	if (singleuser) {
1512 		resolved = false;
1513 	}
1514 
1515 	if (boot_arg_bsdmgroot) {
1516 		resolved = true;
1517 	}
1518 
1519 	if (boot_arg_nobsdmgroot) {
1520 		resolved = false;
1521 	}
1522 
1523 	if (!root_part_of_volume_group) {
1524 		resolved = false;
1525 	}
1526 
1527 	return resolved;
1528 }
1529 #endif // CONFIG_BASESYSTEMROOT
1530 
1531 void
bsd_exec_setup(int scale)1532 bsd_exec_setup(int scale)
1533 {
1534 	switch (scale) {
1535 	case 0:
1536 	case 1:
1537 		bsd_simul_execs = BSD_SIMUL_EXECS;
1538 		break;
1539 	case 2:
1540 	case 3:
1541 		bsd_simul_execs = 65;
1542 		break;
1543 	case 4:
1544 	case 5:
1545 		bsd_simul_execs = 129;
1546 		break;
1547 	case 6:
1548 	case 7:
1549 		bsd_simul_execs = 257;
1550 		break;
1551 	default:
1552 		bsd_simul_execs = 513;
1553 		break;
1554 	}
1555 	bsd_pageable_map_size = (bsd_simul_execs * BSD_PAGEABLE_SIZE_PER_EXEC);
1556 }
1557 
1558 #if !CONFIG_NETBOOT
1559 int
1560 netboot_root(void);
1561 
1562 int
netboot_root(void)1563 netboot_root(void)
1564 {
1565 	return 0;
1566 }
1567 #endif
1568