]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/bsd_init.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / bsd_init.c
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
0a7de745 27 *
b0d623f7 28 *
1c79356b
A
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
0a7de745 68/*
1c79356b
A
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 */
2d21ac55
A
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 */
1c79356b
A
81
82#include <sys/param.h>
83#include <sys/filedesc.h>
84#include <sys/kernel.h>
91447636
A
85#include <sys/mount_internal.h>
86#include <sys/proc_internal.h>
87#include <sys/kauth.h>
1c79356b 88#include <sys/systm.h>
91447636 89#include <sys/vnode_internal.h>
1c79356b 90#include <sys/conf.h>
91447636 91#include <sys/buf_internal.h>
1c79356b 92#include <sys/user.h>
55e303ae
A
93#include <sys/time.h>
94#include <sys/systm.h>
91447636 95#include <sys/mman.h>
cc8bc92a 96#include <sys/kasl.h>
55e303ae 97
b0d623f7 98#include <security/audit/audit.h>
1c79356b
A
99
100#include <sys/malloc.h>
101#include <sys/dkstat.h>
2d21ac55 102#include <sys/codesign.h>
1c79356b 103
91447636 104#include <kern/startup.h>
1c79356b
A
105#include <kern/thread.h>
106#include <kern/task.h>
107#include <kern/ast.h>
f427ee49 108#include <kern/zalloc.h>
d9a64523 109#include <kern/ux_handler.h> /* for ux_handler_setup() */
1c79356b
A
110
111#include <mach/vm_param.h>
112
113#include <vm/vm_map.h>
114#include <vm/vm_kern.h>
115
1c79356b 116#include <sys/reboot.h>
0a7de745 117#include <dev/busvar.h> /* for pseudo_inits */
1c79356b 118#include <sys/kdebug.h>
5ba3f43e 119#include <sys/monotonic.h>
1c79356b 120
765c9de3
A
121#include <mach/mach_types.h>
122#include <mach/vm_prot.h>
123#include <mach/semaphore.h>
124#include <mach/sync_policy.h>
125#include <kern/clock.h>
c3c9b80d 126#include <sys/csr.h>
765c9de3 127#include <mach/kern_return.h>
0a7de745 128#include <mach/thread_act.h> /* for thread_resume() */
0a7de745
A
129#include <sys/mcache.h> /* for mcache_init() */
130#include <sys/mbuf.h> /* for mbinit() */
131#include <sys/event.h> /* for knote_init() */
132#include <sys/eventhandler.h> /* for eventhandler_init() */
133#include <sys/kern_memorystatus.h> /* for memorystatus_init() */
cb323159 134#include <sys/kern_memorystatus_freeze.h> /* for memorystatus_freeze_init() */
0a7de745
A
135#include <sys/aio_kern.h> /* for aio_init() */
136#include <sys/semaphore.h> /* for psem_cache_init() */
137#include <net/dlil.h> /* for dlil_init() */
138#include <net/kpi_protocol.h> /* for proto_kpi_init() */
139#include <net/iptap.h> /* for iptap_init() */
0a7de745
A
140#include <sys/socketvar.h> /* for socketinit() */
141#include <sys/protosw.h> /* for domaininit() */
142#include <kern/sched_prim.h> /* for thread_wakeup() */
143#include <net/if_ether.h> /* for ether_family_init() */
144#include <net/if_gif.h> /* for gif_init() */
0a7de745
A
145#include <miscfs/devfs/devfsdefs.h> /* for devfs_kernel_mount() */
146#include <vm/vm_kern.h> /* for kmem_suballoc() */
0a7de745 147#include <sys/msgbuf.h> /* for log_setsize() */
0a7de745
A
148#include <sys/proc_uuid_policy.h> /* proc_uuid_policy_init() */
149#include <netinet/flow_divert.h> /* flow_divert_init() */
150#include <net/content_filter.h> /* for cfil_init() */
151#include <net/necp.h> /* for necp_init() */
152#include <net/network_agent.h> /* for netagent_init() */
153#include <net/packet_mangler.h> /* for pkt_mnglr_init() */
154#include <net/if_utun.h> /* for utun_register_control() */
155#include <net/if_ipsec.h> /* for ipsec_register_control() */
156#include <net/net_str_id.h> /* for net_str_id_init() */
157#include <net/netsrc.h> /* for netsrc_init() */
158#include <net/ntstat.h> /* for nstat_init() */
159#include <netinet/tcp_cc.h> /* for tcp_cc_init() */
160#include <netinet/mptcp_var.h> /* for mptcp_control_register() */
161#include <net/nwk_wq.h> /* for nwk_wq_init */
cb323159 162#include <net/restricted_in_port.h> /* for restricted_in_port_init() */
0a7de745
A
163#include <kern/assert.h> /* for assert() */
164#include <sys/kern_overrides.h> /* for init_system_override() */
94ff46dc 165#include <sys/lockf.h> /* for lf_init() */
f427ee49 166#include <sys/fsctl.h>
9bccf70c 167
91447636
A
168#include <net/init.h>
169
2d21ac55
A
170#if CONFIG_MACF
171#include <security/mac_framework.h>
0a7de745
A
172#include <security/mac_internal.h> /* mac_init_bsd() */
173#include <security/mac_mach_internal.h> /* mac_update_task_label() */
2d21ac55
A
174#endif
175
0c530ab8
A
176#include <machine/exec.h>
177
cb323159 178#if CONFIG_NETBOOT
6d2010ae
A
179#include <sys/netboot.h>
180#endif
181
2d21ac55
A
182#if CONFIG_IMAGEBOOT
183#include <sys/imageboot.h>
184#endif
185
b0d623f7
A
186#if PFLOG
187#include <net/if_pflog.h>
188#endif
189
39037602 190
2d21ac55 191#include <pexpert/pexpert.h>
6d2010ae 192#include <machine/pal_routines.h>
13f56ec4 193#include <console/video_console.h>
2d21ac55 194
d9a64523
A
195#if CONFIG_XNUPOST
196#include <tests/xnupost.h>
197#endif
3e170ce0 198
0a7de745
A
199void * get_user_regs(thread_t); /* XXX kludge for <machine/thread.h> */
200void IOKitInitializeTime(void); /* XXX */
201void IOSleep(unsigned int); /* XXX */
202void loopattach(void); /* XXX */
1c79356b 203
f427ee49 204const char *const copyright =
0a7de745
A
205 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t"
206 "The Regents of the University of California. "
207 "All rights reserved.\n\n";
1c79356b 208
1c79356b 209/* Components of the first process -- never freed. */
f427ee49 210struct proc proc0 = { .p_comm = "kernel_task", .p_name = "kernel_task" };
0a7de745
A
211struct session session0;
212struct pgrp pgrp0;
213struct filedesc filedesc0;
214struct plimit limit0;
215struct pstats pstats0;
216struct sigacts sigacts0;
f427ee49
A
217SECURITY_READ_ONLY_LATE(proc_t) kernproc = &proc0;
218proc_t XNU_PTRAUTH_SIGNED_PTR("initproc") initproc;
1c79356b 219
1c79356b
A
220long tk_cancc;
221long tk_nin;
222long tk_nout;
223long tk_rawcc;
224
91447636 225int lock_trace = 0;
1c79356b
A
226/* Global variables to make pstat happy. We do swapping differently */
227int nswdev, nswap;
228int nswapmap;
229void *swapmap;
230struct swdevt swdevt[1];
231
c3c9b80d
A
232static LCK_GRP_DECLARE(hostname_lck_grp, "hostname");
233LCK_MTX_DECLARE(hostname_lock, &hostname_lck_grp);
234LCK_MTX_DECLARE(domainname_lock, &hostname_lck_grp);
235
0a7de745
A
236dev_t rootdev; /* device of the root */
237dev_t dumpdev; /* device to take dumps on */
238long dumplo; /* offset into dumpdev */
239long hostid;
240char hostname[MAXHOSTNAMELEN];
0a7de745 241char domainname[MAXDOMNAMELEN];
c3c9b80d 242char rootdevice[DEVMAXNAMESIZE];
1c79356b 243
0a7de745 244struct vnode *rootvp;
f427ee49 245bool rootvp_is_ssd = false;
cb323159 246int boothowto;
3e170ce0 247int minimalboot = 0;
f427ee49 248#if CONFIG_DARKBOOT
5ba3f43e
A
249int darkboot = 0;
250#endif
3e170ce0 251
cb323159
A
252#if __arm64__
253int legacy_footprint_entitlement_mode = LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE;
254#endif /* __arm64__ */
255
3e170ce0
A
256#if PROC_REF_DEBUG
257__private_extern__ int proc_ref_tracking_disabled = 0; /* disable panics on leaked proc refs across syscall boundary */
258#endif
1c79356b 259
cf7d32b8 260extern kern_return_t IOFindBSDRoot(char *, unsigned int, dev_t *, u_int32_t *);
2d21ac55
A
261extern void IOSecureBSDRoot(const char * rootName);
262extern kern_return_t IOKitBSDInit(void );
f427ee49 263extern boolean_t IOSetRecoveryBoot(bsd_bootfail_mode_t, uuid_t, boolean_t);
2d21ac55 264extern void kminit(void);
2d21ac55 265extern void bsd_bufferinit(void);
39037602 266extern void oslog_setsize(int size);
316670eb 267extern void throttle_init(void);
2d21ac55 268
cb323159
A
269#if CONFIG_LOCKERBOOT
270#define LOCKER_PROTOBOOT_MOUNT "/protoboot"
271
272const char kernel_protoboot_mount[] = LOCKER_PROTOBOOT_MOUNT;
273extern int mount_locker_protoboot(const char *fsname, const char *mntpoint,
274 const char *pbdevpath);
275#endif
276
2d21ac55 277extern int ncl;
cb323159
A
278#if DEVELOPMENT || DEBUG
279extern int syscallfilter_disable;
280#endif // DEVELOPMENT || DEBUG
2d21ac55 281
0a7de745
A
282vm_map_t bsd_pageable_map;
283vm_map_t mb_map;
b0d623f7 284
6d2010ae
A
285static int bsd_simul_execs;
286static int bsd_pageable_map_size;
287__private_extern__ int execargs_cache_size = 0;
288__private_extern__ int execargs_free_count = 0;
b0d623f7
A
289__private_extern__ vm_offset_t * execargs_cache = NULL;
290
39236c6e 291void bsd_exec_setup(int);
b0d623f7 292
cb323159
A
293__private_extern__ int bootarg_execfailurereports = 0;
294
a39ff7e2 295#if __x86_64__
cb323159 296__private_extern__ int bootarg_no32exec = 1;
a39ff7e2 297#endif
6d2010ae
A
298__private_extern__ int bootarg_vnode_cache_defeat = 0;
299
3e170ce0
A
300#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
301__private_extern__ int bootarg_no_vnode_jetsam = 0;
302#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
303
94ff46dc
A
304__private_extern__ int bootarg_no_vnode_drain = 0;
305
6d2010ae
A
306/*
307 * Prevent kernel-based ASLR from being used, for testing.
308 */
309#if DEVELOPMENT || DEBUG
310__private_extern__ int bootarg_disable_aslr = 0;
311#endif
1c79356b 312
f427ee49 313
39037602
A
314/*
315 * Allow an alternate dyld to be used for testing.
316 */
317
318#if DEVELOPMENT || DEBUG
319char dyld_alt_path[MAXPATHLEN];
320int use_alt_dyld = 0;
cb323159 321extern uint64_t dyld_flags;
39037602
A
322#endif
323
0a7de745 324int cmask = CMASK;
0c530ab8 325extern int customnbuf;
1c79356b 326
39236c6e
A
327kern_return_t bsd_autoconf(void);
328void bsd_utaskbootstrap(void);
2d21ac55
A
329
330static void parse_bsd_args(void);
fe8ab488
A
331#if CONFIG_DEV_KMEM
332extern void dev_kmem_init(void);
333#endif
3e170ce0 334extern void select_waitq_init(void);
2d21ac55 335static void process_name(const char *, proc_t);
91447636
A
336
337static void setconf(void);
1c79356b 338
f427ee49 339#if CONFIG_BASESYSTEMROOT
c3c9b80d 340static int bsd_find_basesystem_dmg(char *bsdmgpath_out, bool *rooted_dmg, bool *skip_signature_check);
f427ee49
A
341static boolean_t bsdmgroot_bootable(void);
342#endif // CONFIG_BASESYSTEMROOT
343
344static bool bsd_rooted_ramdisk(void);
345
2d21ac55 346#if SYSV_SHM
91447636 347extern void sysv_shm_lock_init(void);
2d21ac55
A
348#endif
349#if SYSV_SEM
91447636 350extern void sysv_sem_lock_init(void);
2d21ac55
A
351#endif
352#if SYSV_MSG
91447636 353extern void sysv_msg_lock_init(void);
2d21ac55 354#endif
0c530ab8 355
39037602
A
356extern void ulock_initialize(void);
357
6d2010ae
A
358#if CONFIG_MACF
359#if defined (__i386__) || defined (__x86_64__)
360/* MACF policy_check configuration flags; see policy_check.c for details */
361int policy_check_flags = 0;
2d21ac55 362
6d2010ae
A
363extern int check_policy_init(int);
364#endif
0a7de745 365#endif /* CONFIG_MACF */
2d21ac55 366
b0d623f7
A
367/* If we are using CONFIG_DTRACE */
368#if CONFIG_DTRACE
0a7de745 369extern void dtrace_postinit(void);
b0d623f7
A
370#endif
371
1c79356b
A
372/*
373 * Initialization code.
374 * Called from cold start routine as
375 * soon as a stack and segmentation
376 * have been established.
377 * Functions:
1c79356b
A
378 * turn on clock
379 * hand craft 0th process
380 * call all initialization routines
55e303ae 381 * hand craft 1st user process
1c79356b
A
382 */
383
384/*
385 * Sets the name for the given task.
386 */
91447636 387static void
2d21ac55 388process_name(const char *s, proc_t p)
1c79356b 389{
0a7de745
A
390 strlcpy(p->p_comm, s, sizeof(p->p_comm));
391 strlcpy(p->p_name, s, sizeof(p->p_name));
1c79356b
A
392}
393
1c79356b
A
394/* To allow these values to be patched, they're globals here */
395#include <machine/vmparam.h>
cb323159
A
396struct rlimit vm_initial_limit_stack = { .rlim_cur = DFLSSIZ, .rlim_max = MAXSSIZ - PAGE_MAX_SIZE };
397struct rlimit vm_initial_limit_data = { .rlim_cur = DFLDSIZ, .rlim_max = MAXDSIZ };
398struct rlimit vm_initial_limit_core = { .rlim_cur = DFLCSIZ, .rlim_max = MAXCSIZ };
1c79356b 399
f427ee49
A
400extern struct os_refgrp rlimit_refgrp;
401
0a7de745
A
402extern thread_t cloneproc(task_t, coalition_t, proc_t, int, int);
403extern int (*mountroot)(void);
1c79356b 404
c3c9b80d
A
405LCK_ATTR_DECLARE(proc_lck_attr, 0, 0);
406LCK_GRP_DECLARE(proc_lck_grp, "proc");
407LCK_GRP_DECLARE(proc_slock_grp, "proc-slock");
408LCK_GRP_DECLARE(proc_fdmlock_grp, "proc-fdmlock");
409LCK_GRP_DECLARE(proc_mlock_grp, "proc-mlock");
410LCK_GRP_DECLARE(proc_ucred_mlock_grp, "proc-ucred-mlock");
411LCK_GRP_DECLARE(proc_dirslock_grp, "proc-dirslock");
412LCK_GRP_DECLARE(proc_kqhashlock_grp, "proc-kqhashlock");
413LCK_GRP_DECLARE(proc_knhashlock_grp, "proc-knhashlock");
91447636 414
813fb2f6 415
c3c9b80d 416LCK_MTX_DECLARE_ATTR(proc_list_mlock, &proc_mlock_grp, &proc_lck_attr);
b0d623f7 417
f427ee49 418#if XNU_TARGET_OS_OSX
9bccf70c
A
419/* hook called after root is mounted XXX temporary hack */
420void (*mountroot_post_hook)(void);
b0d623f7 421void (*unmountroot_pre_hook)(void);
f427ee49
A
422#endif
423void set_rootvnode(vnode_t);
424
c3c9b80d 425extern lck_rw_t rootvnode_rw_lock;
f427ee49
A
426
427/* called with an iocount and usecount on new_rootvnode */
428void
429set_rootvnode(vnode_t new_rootvnode)
430{
431 mount_t new_mount = (new_rootvnode != NULL) ? new_rootvnode->v_mount : NULL;
432 vnode_t new_devvp = (new_mount != NULL) ? new_mount->mnt_devvp : NULL;
433 vnode_t old_rootvnode = rootvnode;
434
435 new_rootvnode->v_flag |= VROOT;
436 rootvp = new_devvp;
437 rootvnode = new_rootvnode;
438 filedesc0.fd_cdir = new_rootvnode;
439 if (new_devvp != NULL) {
440 rootdev = vnode_specrdev(new_devvp);
441 } else if (new_mount != NULL) {
442 rootdev = vfs_statfs(new_mount)->f_fsid.val[0]; /* like ATTR_CMN_DEVID */
443 } else {
444 rootdev = NODEV;
445 }
446
447 if (old_rootvnode) {
448 vnode_rele(old_rootvnode);
449 }
450}
451
452#define RAMDEV "md0"
453
454bool
455bsd_rooted_ramdisk(void)
456{
457 bool is_ramdisk = false;
458 char *dev_path = zalloc(ZV_NAMEI);
459 if (dev_path == NULL) {
460 panic("failed to allocate devpath string! \n");
461 }
462
463 if (PE_parse_boot_argn("rd", dev_path, MAXPATHLEN)) {
464 if (strncmp(dev_path, RAMDEV, strlen(RAMDEV)) == 0) {
465 is_ramdisk = true;
466 }
467 }
468
469 zfree(ZV_NAMEI, dev_path);
470 return is_ramdisk;
471}
1c79356b 472
91447636
A
473/*
474 * This function is called very early on in the Mach startup, from the
475 * function start_kernel_threads() in osfmk/kern/startup.c. It's called
476 * in the context of the current (startup) task using a call to the
477 * function kernel_thread_create() to jump into start_kernel_threads().
478 * Internally, kernel_thread_create() calls thread_create_internal(),
479 * which calls uthread_alloc(). The function of uthread_alloc() is
480 * normally to allocate a uthread structure, and fill out the uu_sigmask,
2d21ac55
A
481 * uu_context fields. It skips filling these out in the case of the "task"
482 * being "kernel_task", because the order of operation is inverted. To
483 * account for that, we need to manually fill in at least the contents
484 * of the uu_context.vc_ucred field so that the uthread structure can be
485 * used like any other.
91447636 486 */
316670eb 487
1c79356b 488void
2d21ac55 489bsd_init(void)
1c79356b 490{
91447636 491 struct uthread *ut;
2d21ac55 492 unsigned int i;
91447636 493 struct vfs_context context;
0a7de745 494 kern_return_t ret;
91447636 495 struct ucred temp_cred;
6d2010ae 496 struct posix_cred temp_pcred;
f427ee49 497 vnode_t init_rootvnode = NULLVP;
cb323159 498#if CONFIG_NETBOOT || CONFIG_IMAGEBOOT
6d2010ae
A
499 boolean_t netboot = FALSE;
500#endif
cb323159
A
501#if CONFIG_LOCKERBOOT
502 vnode_t pbvn = NULLVP;
503 mount_t pbmnt = NULL;
504 char *pbdevp = NULL;
505 char pbdevpath[64];
506 char pbfsname[MFSNAMELEN];
f427ee49 507 const char *slash_dev = NULL;
cb323159 508#endif
2d21ac55 509
cb323159
A
510#define DEBUG_BSDINIT 0
511
512#if DEBUG_BSDINIT
513#define bsd_init_kprintf(x, ...) kprintf("bsd_init: " x, ## __VA_ARGS__)
514#else
515#define bsd_init_kprintf(x, ...)
516#endif
1c79356b 517
316670eb
A
518 throttle_init();
519
1c79356b 520 printf(copyright);
0a7de745 521
2d21ac55 522 bsd_init_kprintf("calling parse_bsd_args\n");
1c79356b
A
523 parse_bsd_args();
524
fe8ab488
A
525#if CONFIG_DEV_KMEM
526 bsd_init_kprintf("calling dev_kmem_init\n");
527 dev_kmem_init();
528#endif
529
91447636 530 /* Initialize kauth subsystem before instancing the first credential */
2d21ac55 531 bsd_init_kprintf("calling kauth_init\n");
91447636
A
532 kauth_init();
533
534 /* Initialize process and pgrp structures. */
2d21ac55 535 bsd_init_kprintf("calling procinit\n");
1c79356b
A
536 procinit();
537
1c79356b 538 /* kernel_task->proc = kernproc; */
0a7de745 539 set_bsdtask_info(kernel_task, (void *)kernproc);
1c79356b
A
540
541 /* give kernproc a name */
2d21ac55 542 bsd_init_kprintf("calling process_name\n");
b0d623f7 543 process_name("kernel_task", kernproc);
91447636 544
91447636 545 /* Allocate proc lock attribute */
91447636 546
c3c9b80d
A
547 lck_mtx_init(&kernproc->p_mlock, &proc_mlock_grp, &proc_lck_attr);
548 lck_mtx_init(&kernproc->p_fdmlock, &proc_fdmlock_grp, &proc_lck_attr);
549 lck_mtx_init(&kernproc->p_ucred_mlock, &proc_ucred_mlock_grp, &proc_lck_attr);
550 lck_spin_init(&kernproc->p_slock, &proc_slock_grp, &proc_lck_attr);
551 lck_rw_init(&kernproc->p_dirs_lock, &proc_dirslock_grp, &proc_lck_attr);
1c79356b 552
6d2010ae 553 assert(bsd_simul_execs != 0);
b0d623f7
A
554 execargs_cache_size = bsd_simul_execs;
555 execargs_free_count = bsd_simul_execs;
f427ee49
A
556 execargs_cache = zalloc_permanent(bsd_simul_execs * sizeof(vm_offset_t),
557 ZALIGN(vm_offset_t));
0a7de745
A
558
559 if (current_task() != kernel_task) {
9bccf70c 560 printf("bsd_init: We have a problem, "
0a7de745
A
561 "current task is not kernel task\n");
562 }
563
2d21ac55 564 bsd_init_kprintf("calling get_bsdthread_info\n");
91447636
A
565 ut = (uthread_t)get_bsdthread_info(current_thread());
566
2d21ac55
A
567#if CONFIG_MACF
568 /*
569 * Initialize the MAC Framework
570 */
571 mac_policy_initbsd();
6d2010ae
A
572
573#if defined (__i386__) || defined (__x86_64__)
574 /*
575 * We currently only support this on i386/x86_64, as that is the
576 * only lock code we have instrumented so far.
577 */
578 check_policy_init(policy_check_flags);
579#endif
2d21ac55
A
580#endif /* MAC */
581
39037602
A
582 ulock_initialize();
583
1c79356b
A
584 /*
585 * Create process 0.
586 */
2d21ac55 587 proc_list_lock();
b0d623f7
A
588 LIST_INSERT_HEAD(&allproc, kernproc, p_list);
589 kernproc->p_pgrp = &pgrp0;
1c79356b
A
590 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
591 LIST_INIT(&pgrp0.pg_members);
c3c9b80d 592 lck_mtx_init(&pgrp0.pg_mlock, &proc_mlock_grp, &proc_lck_attr);
2d21ac55 593 /* There is no other bsd thread this point and is safe without pgrp lock */
b0d623f7
A
594 LIST_INSERT_HEAD(&pgrp0.pg_members, kernproc, p_pglist);
595 kernproc->p_listflag |= P_LIST_INPGRP;
596 kernproc->p_pgrpid = 0;
6d2010ae 597 kernproc->p_uniqueid = 0;
1c79356b
A
598
599 pgrp0.pg_session = &session0;
2d21ac55
A
600 pgrp0.pg_membercnt = 1;
601
1c79356b 602 session0.s_count = 1;
b0d623f7 603 session0.s_leader = kernproc;
2d21ac55 604 session0.s_listflags = 0;
c3c9b80d 605 lck_mtx_init(&session0.s_mlock, &proc_mlock_grp, &proc_lck_attr);
2d21ac55
A
606 LIST_INSERT_HEAD(SESSHASH(0), &session0, s_hash);
607 proc_list_unlock();
608
490019cf
A
609#if CONFIG_PERSONAS
610 kernproc->p_persona = NULL;
611#endif
612
b0d623f7 613 kernproc->task = kernel_task;
0a7de745 614
b0d623f7
A
615 kernproc->p_stat = SRUN;
616 kernproc->p_flag = P_SYSTEM;
6d2010ae
A
617 kernproc->p_lflag = 0;
618 kernproc->p_ladvflag = 0;
39037602
A
619
620#if defined(__LP64__)
621 kernproc->p_flag |= P_LP64;
622#endif
623
6d2010ae 624#if DEVELOPMENT || DEBUG
0a7de745 625 if (bootarg_disable_aslr) {
6d2010ae 626 kernproc->p_flag |= P_DISABLE_ASLR;
0a7de745 627 }
6d2010ae
A
628#endif
629
b0d623f7
A
630 kernproc->p_nice = NZERO;
631 kernproc->p_pptr = kernproc;
2d21ac55 632
b0d623f7
A
633 TAILQ_INIT(&kernproc->p_uthlist);
634 TAILQ_INSERT_TAIL(&kernproc->p_uthlist, ut, uu_list);
0a7de745 635
b0d623f7
A
636 kernproc->sigwait = FALSE;
637 kernproc->sigwait_thread = THREAD_NULL;
638 kernproc->exit_thread = THREAD_NULL;
639 kernproc->p_csflags = CS_VALID;
1c79356b 640
91447636
A
641 /*
642 * Create credential. This also Initializes the audit information.
91447636 643 */
2d21ac55 644 bsd_init_kprintf("calling bzero\n");
91447636 645 bzero(&temp_cred, sizeof(temp_cred));
6d2010ae
A
646 bzero(&temp_pcred, sizeof(temp_pcred));
647 temp_pcred.cr_ngroups = 1;
39236c6e
A
648 /* kern_proc, shouldn't call up to DS for group membership */
649 temp_pcred.cr_flags = CRF_NOMEMBERD;
6d2010ae 650 temp_cred.cr_audit.as_aia_p = audit_default_aia_p;
0a7de745 651
2d21ac55 652 bsd_init_kprintf("calling kauth_cred_create\n");
6d2010ae
A
653 /*
654 * We have to label the temp cred before we create from it to
655 * properly set cr_ngroups, or the create will fail.
656 */
657 posix_cred_label(&temp_cred, &temp_pcred);
0a7de745 658 kernproc->p_ucred = kauth_cred_create(&temp_cred);
91447636 659
6d2010ae
A
660 /* update cred on proc */
661 PROC_UPDATE_CREDS_ONPROC(kernproc);
662
91447636 663 /* give the (already exisiting) initial thread a reference on it */
2d21ac55 664 bsd_init_kprintf("calling kauth_cred_ref\n");
b0d623f7
A
665 kauth_cred_ref(kernproc->p_ucred);
666 ut->uu_context.vc_ucred = kernproc->p_ucred;
2d21ac55
A
667 ut->uu_context.vc_thread = current_thread();
668
f427ee49
A
669 vfs_set_context_kernel(&ut->uu_context);
670
b0d623f7
A
671 TAILQ_INIT(&kernproc->p_aio_activeq);
672 TAILQ_INIT(&kernproc->p_aio_doneq);
673 kernproc->p_aio_total_count = 0;
55e303ae 674
2d21ac55 675#if CONFIG_MACF
b0d623f7 676 mac_cred_label_associate_kernel(kernproc->p_ucred);
2d21ac55
A
677#endif
678
1c79356b 679 /* Create the file descriptor table. */
b0d623f7 680 kernproc->p_fd = &filedesc0;
f427ee49 681 filedesc0.fd_cmask = (mode_t)cmask;
cb323159 682 filedesc0.fd_knlistsize = 0;
55e303ae
A
683 filedesc0.fd_knlist = NULL;
684 filedesc0.fd_knhash = NULL;
685 filedesc0.fd_knhashmask = 0;
c3c9b80d
A
686 lck_mtx_init(&filedesc0.fd_kqhashlock, &proc_kqhashlock_grp, &proc_lck_attr);
687 lck_mtx_init(&filedesc0.fd_knhashlock, &proc_knhashlock_grp, &proc_lck_attr);
1c79356b
A
688
689 /* Create the limits structures. */
b0d623f7 690 kernproc->p_limit = &limit0;
f427ee49 691 for (i = 0; i < sizeof(kernproc->p_limit->pl_rlimit) / sizeof(kernproc->p_limit->pl_rlimit[0]); i++) {
0a7de745
A
692 limit0.pl_rlimit[i].rlim_cur =
693 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
694 }
1c79356b 695 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
0c530ab8 696 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = maxprocperuid;
55e303ae 697 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
1c79356b
A
698 limit0.pl_rlimit[RLIMIT_STACK] = vm_initial_limit_stack;
699 limit0.pl_rlimit[RLIMIT_DATA] = vm_initial_limit_data;
700 limit0.pl_rlimit[RLIMIT_CORE] = vm_initial_limit_core;
f427ee49 701 os_ref_init_count(&limit0.pl_refcnt, &rlimit_refgrp, 1);
1c79356b 702
b0d623f7
A
703 kernproc->p_stats = &pstats0;
704 kernproc->p_sigacts = &sigacts0;
f427ee49 705 kernproc->p_subsystem_root_path = NULL;
1c79356b
A
706
707 /*
fe8ab488 708 * Charge root for one process: launchd.
1c79356b 709 */
2d21ac55 710 bsd_init_kprintf("calling chgproccnt\n");
1c79356b
A
711 (void)chgproccnt(0, 1);
712
1c79356b
A
713 /*
714 * Allocate a kernel submap for pageable memory
765c9de3 715 * for temporary copying (execve()).
1c79356b
A
716 */
717 {
0a7de745 718 vm_offset_t minimum;
1c79356b 719
2d21ac55 720 bsd_init_kprintf("calling kmem_suballoc\n");
6d2010ae 721 assert(bsd_pageable_map_size != 0);
1c79356b 722 ret = kmem_suballoc(kernel_map,
0a7de745
A
723 &minimum,
724 (vm_size_t)bsd_pageable_map_size,
725 TRUE,
726 VM_FLAGS_ANYWHERE,
727 VM_MAP_KERNEL_FLAGS_NONE,
728 VM_KERN_MEMORY_BSD,
729 &bsd_pageable_map);
730 if (ret != KERN_SUCCESS) {
9bccf70c 731 panic("bsd_init: Failed to allocate bsd pageable map");
0a7de745 732 }
765c9de3
A
733 }
734
91447636
A
735 /*
736 * Initialize buffers and hash links for buffers
737 *
738 * SIDE EFFECT: Starts a thread for bcleanbuf_thread(), so must
739 * happen after a credential has been associated with
740 * the kernel task.
741 */
2d21ac55 742 bsd_init_kprintf("calling bsd_bufferinit\n");
91447636
A
743 bsd_bufferinit();
744
1c79356b 745 /*
0b4e3aa0
A
746 * Initialize the calendar.
747 */
4a3eedf9 748 bsd_init_kprintf("calling IOKitInitializeTime\n");
2d21ac55
A
749 IOKitInitializeTime();
750
1c79356b 751 /* Initialize the file systems. */
2d21ac55 752 bsd_init_kprintf("calling vfsinit\n");
1c79356b
A
753 vfsinit();
754
39236c6e
A
755#if CONFIG_PROC_UUID_POLICY
756 /* Initial proc_uuid_policy subsystem */
757 bsd_init_kprintf("calling proc_uuid_policy_init()\n");
758 proc_uuid_policy_init();
759#endif
760
2d21ac55
A
761#if SOCKETS
762 /* Initialize per-CPU cache allocator */
763 mcache_init();
764
1c79356b 765 /* Initialize mbuf's. */
2d21ac55 766 bsd_init_kprintf("calling mbinit\n");
1c79356b 767 mbinit();
b0d623f7 768 net_str_id_init(); /* for mbuf tags */
cb323159 769 restricted_in_port_init();
2d21ac55 770#endif /* SOCKETS */
1c79356b 771
55e303ae
A
772 /*
773 * Initializes security event auditing.
774 * XXX: Should/could this occur later?
775 */
b0d623f7 776#if CONFIG_AUDIT
2d21ac55 777 bsd_init_kprintf("calling audit_init\n");
0a7de745 778 audit_init();
2d21ac55 779#endif
55e303ae
A
780
781 /* Initialize kqueues */
2d21ac55 782 bsd_init_kprintf("calling knote_init\n");
55e303ae
A
783 knote_init();
784
5ba3f43e
A
785 /* Initialize event handler */
786 bsd_init_kprintf("calling eventhandler_init\n");
787 eventhandler_init();
788
55e303ae 789 /* Initialize for async IO */
2d21ac55 790 bsd_init_kprintf("calling aio_init\n");
55e303ae
A
791 aio_init();
792
2d21ac55 793 pthread_init();
9bccf70c 794 /* POSIX Shm and Sem */
2d21ac55 795 bsd_init_kprintf("calling pshm_cache_init\n");
9bccf70c 796 pshm_cache_init();
2d21ac55 797 bsd_init_kprintf("calling psem_cache_init\n");
9bccf70c 798 psem_cache_init();
3e170ce0
A
799 bsd_init_kprintf("calling select_waitq_init\n");
800 select_waitq_init();
1c79356b 801
1c79356b
A
802 /*
803 * Initialize protocols. Block reception of incoming packets
804 * until everything is ready.
805 */
2d21ac55 806#if NETWORKING
5ba3f43e
A
807 bsd_init_kprintf("calling nwk_wq_init\n");
808 nwk_wq_init();
2d21ac55 809 bsd_init_kprintf("calling dlil_init\n");
1c79356b 810 dlil_init();
2d21ac55 811 bsd_init_kprintf("calling proto_kpi_init\n");
91447636 812 proto_kpi_init();
2d21ac55
A
813#endif /* NETWORKING */
814#if SOCKETS
815 bsd_init_kprintf("calling socketinit\n");
1c79356b 816 socketinit();
2d21ac55 817 bsd_init_kprintf("calling domaininit\n");
1c79356b 818 domaininit();
316670eb 819 iptap_init();
39236c6e
A
820#if FLOW_DIVERT
821 flow_divert_init();
0a7de745 822#endif /* FLOW_DIVERT */
2d21ac55 823#endif /* SOCKETS */
f427ee49
A
824#if NETWORKING
825#if NECP
826 /* Initialize Network Extension Control Policies */
827 necp_init();
828#endif
829 netagent_init();
830#endif /* NETWORKING */
b0d623f7
A
831 kernproc->p_fd->fd_cdir = NULL;
832 kernproc->p_fd->fd_rdir = NULL;
1c79356b 833
c3c9b80d
A
834#if defined (__x86_64__) && (DEBUG || DEVELOPMENT)
835 hvg_bsd_init();
836#endif /* DEBUG || DEVELOPMENT */
837
6d2010ae 838#if CONFIG_FREEZE
316670eb
A
839#ifndef CONFIG_MEMORYSTATUS
840 #error "CONFIG_FREEZE defined without matching CONFIG_MEMORYSTATUS"
841#endif
842 /* Initialise background freezing */
843 bsd_init_kprintf("calling memorystatus_freeze_init\n");
844 memorystatus_freeze_init();
6d2010ae
A
845#endif
846
316670eb 847#if CONFIG_MEMORYSTATUS
d1ecb069 848 /* Initialize kernel memory status notifications */
316670eb
A
849 bsd_init_kprintf("calling memorystatus_init\n");
850 memorystatus_init();
851#endif /* CONFIG_MEMORYSTATUS */
d1ecb069 852
39037602 853 bsd_init_kprintf("calling sysctl_mib_init\n");
cb323159 854 sysctl_mib_init();
39037602 855
2d21ac55 856 bsd_init_kprintf("calling bsd_autoconf\n");
1c79356b
A
857 bsd_autoconf();
858
2d21ac55 859#if CONFIG_DTRACE
2d21ac55
A
860 dtrace_postinit();
861#endif
862
1c79356b
A
863 /*
864 * We attach the loopback interface *way* down here to ensure
865 * it happens after autoconf(), otherwise it becomes the
866 * "primary" interface.
867 */
868#include <loop.h>
869#if NLOOP > 0
2d21ac55 870 bsd_init_kprintf("calling loopattach\n");
0a7de745 871 loopattach(); /* XXX */
1c79356b 872#endif
39236c6e
A
873#if NGIF
874 /* Initialize gif interface (after lo0) */
875 gif_init();
876#endif
b0d623f7
A
877
878#if PFLOG
879 /* Initialize packet filter log interface */
880 pfloginit();
881#endif /* PFLOG */
882
2d21ac55
A
883#if NETHER > 0
884 /* Register the built-in dlil ethernet interface family */
885 bsd_init_kprintf("calling ether_family_init\n");
9bccf70c 886 ether_family_init();
2d21ac55 887#endif /* ETHER */
1c79356b 888
2d21ac55 889#if NETWORKING
fe8ab488
A
890#if CONTENT_FILTER
891 cfil_init();
892#endif
893
894#if PACKET_MANGLER
895 pkt_mnglr_init();
0a7de745 896#endif
fe8ab488 897
f427ee49
A
898 /*
899 * Register subsystems with kernel control handlers
900 */
b0d623f7 901 utun_register_control();
39236c6e
A
902#if IPSEC
903 ipsec_register_control();
904#endif /* IPSEC */
316670eb
A
905 netsrc_init();
906 nstat_init();
fe8ab488 907 tcp_cc_init();
3e170ce0
A
908#if MPTCP
909 mptcp_control_register();
910#endif /* MPTCP */
91447636 911
f427ee49
A
912 /*
913 * The the networking stack is now initialized so it is a good time to call
914 * the clients that are waiting for the networking stack to be usable.
915 */
916 bsd_init_kprintf("calling net_init_run\n");
917 net_init_run();
918#endif /* NETWORKING */
91447636 919
2d21ac55 920 bsd_init_kprintf("calling inittodr\n");
91447636 921 inittodr(0);
1c79356b
A
922
923 /* Mount the root file system. */
0a7de745 924 while (TRUE) {
1c79356b
A
925 int err;
926
2d21ac55 927 bsd_init_kprintf("calling setconf\n");
1c79356b 928 setconf();
cb323159 929#if CONFIG_NETBOOT
6d2010ae
A
930 netboot = (mountroot == netboot_mountroot);
931#endif
9bccf70c 932
2d21ac55 933 bsd_init_kprintf("vfs_mountroot\n");
0a7de745 934 if (0 == (err = vfs_mountroot())) {
1c79356b 935 break;
0a7de745 936 }
2d21ac55 937 rootdevice[0] = '\0';
cb323159 938#if CONFIG_NETBOOT
6d2010ae 939 if (netboot) {
b0d623f7
A
940 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
941 vc_progress_set(FALSE, 0);
0a7de745 942 for (i = 1; 1; i *= 2) {
b0d623f7 943 printf("bsd_init: failed to mount network root, error %d, %s\n",
0a7de745 944 err, PE_boot_args());
b0d623f7 945 printf("We are hanging here...\n");
0a7de745 946 IOSleep(i * 60 * 1000);
b0d623f7
A
947 }
948 /*NOTREACHED*/
9bccf70c 949 }
91447636 950#endif
1c79356b
A
951 printf("cannot mount root, errno = %d\n", err);
952 boothowto |= RB_ASKNAME;
953 }
954
2d21ac55
A
955 IOSecureBSDRoot(rootdevice);
956
957 context.vc_thread = current_thread();
b0d623f7 958 context.vc_ucred = kernproc->p_ucred;
91447636 959 mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
1c79356b 960
2d21ac55 961 bsd_init_kprintf("calling VFS_ROOT\n");
1c79356b 962 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
f427ee49 963 if (VFS_ROOT(mountlist.tqh_first, &init_rootvnode, &context)) {
2d21ac55 964 panic("bsd_init: cannot find root vnode: %s", PE_boot_args());
0a7de745 965 }
f427ee49
A
966 (void)vnode_ref(init_rootvnode);
967 (void)vnode_put(init_rootvnode);
968
c3c9b80d 969 lck_rw_lock_exclusive(&rootvnode_rw_lock);
f427ee49 970 set_rootvnode(init_rootvnode);
c3c9b80d 971 lck_rw_unlock_exclusive(&rootvnode_rw_lock);
f427ee49
A
972 init_rootvnode = NULLVP; /* use rootvnode after this point */
973
974
975 if (!bsd_rooted_ramdisk()) {
976#if CONFIG_IMAGEBOOT
977#if XNU_TARGET_OS_OSX && defined(__arm64__)
978 /* Apple Silicon MacOS */
979 if (!imageboot_desired()) {
980 /* enforce sealedness */
981 int autherr = VNOP_IOCTL(rootvnode, FSIOC_KERNEL_ROOTAUTH, NULL, 0, vfs_context_kernel());
982 if (autherr) {
983 panic("rootvp not authenticated after mounting \n");
984 }
985 }
986#endif // TARGET_OS_OSX && arm64
987#endif // config_imageboot
988 /* Otherwise, noop */
989 }
990
9bccf70c 991
cb323159 992#if CONFIG_NETBOOT
6d2010ae 993 if (netboot) {
9bccf70c 994 int err;
6d2010ae
A
995
996 netboot = TRUE;
9bccf70c 997 /* post mount setup */
2d21ac55 998 if ((err = netboot_setup()) != 0) {
b0d623f7
A
999 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
1000 vc_progress_set(FALSE, 0);
0a7de745 1001 for (i = 1; 1; i *= 2) {
b0d623f7 1002 printf("bsd_init: NetBoot could not find root, error %d: %s\n",
0a7de745 1003 err, PE_boot_args());
b0d623f7 1004 printf("We are hanging here...\n");
0a7de745 1005 IOSleep(i * 60 * 1000);
b0d623f7
A
1006 }
1007 /*NOTREACHED*/
9bccf70c
A
1008 }
1009 }
91447636 1010#endif
0a7de745 1011
1c79356b 1012
2d21ac55 1013#if CONFIG_IMAGEBOOT
cb323159
A
1014#if CONFIG_LOCKERBOOT
1015 /*
1016 * Stash the protoboot vnode, mount, filesystem name, and device name for
1017 * later use. Note that the mount-from name may not have the "/dev/"
1018 * component, so we must sniff out this condition and add it as needed.
1019 */
1020 pbvn = rootvnode;
1021 pbmnt = pbvn->v_mount;
1022 pbdevp = vfs_statfs(pbmnt)->f_mntfromname;
1023 slash_dev = strnstr(pbdevp, "/dev/", strlen(pbdevp));
1024 if (slash_dev) {
1025 /*
1026 * If the old root is a snapshot mount, it will have the form:
1027 *
1028 * com.apple.os.update-<boot manifest hash>@<dev node path>
1029 *
1030 * So we just search the mntfromname for any occurrence of "/dev/" and
1031 * grab that as the device path. The image boot code needs a dev node to
1032 * do the re-mount, so we cannot directly mount the snapshot as the
1033 * protoboot volume currently.
1034 */
1035 strlcpy(pbdevpath, slash_dev, sizeof(pbdevpath));
1036 } else {
1037 snprintf(pbdevpath, sizeof(pbdevpath), "/dev/%s", pbdevp);
1038 }
1039
1040 bsd_init_kprintf("protoboot mount-from: %s\n", pbdevp);
1041 bsd_init_kprintf("protoboot dev path: %s\n", pbdevpath);
1042
1043 strlcpy(pbfsname, pbmnt->mnt_vtable->vfc_name, sizeof(pbfsname));
1044#endif
f427ee49 1045
2d21ac55
A
1046 /*
1047 * See if a system disk image is present. If so, mount it and
1048 * switch the root vnode to point to it
0a7de745 1049 */
cb323159
A
1050 imageboot_type_t imageboot_type = imageboot_needed();
1051 if (netboot == FALSE && imageboot_type) {
0a7de745 1052 /*
6d2010ae
A
1053 * An image was found. No turning back: we're booted
1054 * with a kernel from the disk image.
1055 */
cb323159
A
1056 bsd_init_kprintf("doing image boot: type = %d\n", imageboot_type);
1057 imageboot_setup(imageboot_type);
2d21ac55 1058 }
cb323159
A
1059
1060#if CONFIG_LOCKERBOOT
1061 if (imageboot_type == IMAGEBOOT_LOCKER) {
1062 bsd_init_kprintf("booting from locker\n");
1063 if (vnode_tag(rootvnode) != VT_LOCKERFS) {
1064 panic("root filesystem not a locker: fsname = %s",
1065 rootvnode->v_mount->mnt_vtable->vfc_name);
1066 }
1067 }
1068#endif /* CONFIG_LOCKERBOOT */
2d21ac55 1069#endif /* CONFIG_IMAGEBOOT */
0a7de745 1070
b0d623f7 1071 /* set initial time; all other resource data is already zero'ed */
39236c6e 1072 microtime_with_abstime(&kernproc->p_start, &kernproc->p_stats->ps_start);
1c79356b 1073
9bccf70c 1074#if DEVFS
1c79356b 1075 {
0a7de745 1076 char mounthere[] = "/dev"; /* !const because of internal casting */
2d21ac55 1077
0a7de745
A
1078 bsd_init_kprintf("calling devfs_kernel_mount\n");
1079 devfs_kernel_mount(mounthere);
1c79356b 1080 }
55e303ae 1081#endif /* DEVFS */
3e170ce0 1082
f427ee49
A
1083#if CONFIG_BASESYSTEMROOT
1084#if CONFIG_IMAGEBOOT
1085 if (bsdmgroot_bootable()) {
1086 int error;
1087 bool rooted_dmg = false;
c3c9b80d 1088 bool skip_signature_check = false;
f427ee49
A
1089
1090 printf("trying to find and mount BaseSystem dmg as root volume\n");
1091#if DEVELOPMENT || DEBUG
1092 printf("(set boot-arg -nobsdmgroot to avoid this)\n");
1093#endif // DEVELOPMENT || DEBUG
cb323159 1094
f427ee49
A
1095 char *dmgpath = NULL;
1096 dmgpath = zalloc_flags(ZV_NAMEI, Z_ZERO | Z_WAITOK);
1097 if (dmgpath == NULL) {
1098 panic("%s: M_NAMEI zone exhausted", __FUNCTION__);
1099 }
1100
c3c9b80d 1101 error = bsd_find_basesystem_dmg(dmgpath, &rooted_dmg, &skip_signature_check);
f427ee49
A
1102 if (error) {
1103 bsd_init_kprintf("failed to to find BaseSystem dmg: error = %d\n", error);
1104 } else {
1105 PE_parse_boot_argn("bsdmgpath", dmgpath, sizeof(dmgpath));
1106
1107 bsd_init_kprintf("found BaseSystem dmg at: %s\n", dmgpath);
1108
c3c9b80d 1109 error = imageboot_pivot_image(dmgpath, IMAGEBOOT_DMG, "/System/Volumes/BaseSystem", "System/Volumes/macOS", rooted_dmg, skip_signature_check);
f427ee49
A
1110 if (error) {
1111 bsd_init_kprintf("couldn't mount BaseSystem dmg: error = %d", error);
1112 }
1113 }
1114 zfree(ZV_NAMEI, dmgpath);
cb323159 1115 }
f427ee49
A
1116#else /* CONFIG_IMAGEBOOT */
1117#error CONFIG_BASESYSTEMROOT requires CONFIG_IMAGEBOOT
1118#endif /* CONFIG_IMAGEBOOT */
1119#endif /* CONFIG_BASESYSTEMROOT */
cb323159
A
1120
1121#if CONFIG_LOCKERBOOT
1122 /*
1123 * We need to wait until devfs is up before remounting the protoboot volume
1124 * within the locker so that it can have a real devfs vnode backing it.
1125 */
1126 if (imageboot_type == IMAGEBOOT_LOCKER) {
1127 bsd_init_kprintf("re-mounting protoboot volume\n");
1128 int error = mount_locker_protoboot(pbfsname, LOCKER_PROTOBOOT_MOUNT,
1129 pbdevpath);
1130 if (error) {
1131 panic("failed to mount protoboot volume: dev path = %s, error = %d",
1132 pbdevpath, error);
1133 }
1134 }
1135#endif /* CONFIG_LOCKERBOOT */
1136
1c79356b 1137 /* Initialize signal state for process 0. */
2d21ac55 1138 bsd_init_kprintf("calling siginit\n");
b0d623f7 1139 siginit(kernproc);
1c79356b 1140
2d21ac55 1141 bsd_init_kprintf("calling bsd_utaskbootstrap\n");
1c79356b
A
1142 bsd_utaskbootstrap();
1143
6d2010ae
A
1144 pal_kernel_announce();
1145
2d21ac55
A
1146 bsd_init_kprintf("calling mountroot_post_hook\n");
1147
f427ee49 1148#if XNU_TARGET_OS_OSX
9bccf70c 1149 /* invoke post-root-mount hook */
0a7de745 1150 if (mountroot_post_hook != NULL) {
9bccf70c 1151 mountroot_post_hook();
0a7de745 1152 }
f427ee49 1153#endif
2d21ac55
A
1154
1155#if 0 /* not yet */
5ba3f43e 1156 consider_zone_gc(FALSE);
2d21ac55 1157#endif
b0d623f7 1158
2d21ac55 1159 bsd_init_kprintf("done\n");
1c79356b
A
1160}
1161
1162void
9bccf70c 1163bsdinit_task(void)
1c79356b 1164{
2d21ac55 1165 proc_t p = current_proc();
1c79356b 1166
91447636 1167 process_name("init", p);
1c79356b 1168
d9a64523
A
1169 /* Set up exception-to-signal reflection */
1170 ux_handler_setup();
1c79356b 1171
2d21ac55
A
1172#if CONFIG_MACF
1173 mac_cred_label_associate_user(p->p_ucred);
2d21ac55 1174#endif
813fb2f6 1175
0a7de745 1176 vm_init_before_launchd();
5ba3f43e 1177
d9a64523
A
1178#if CONFIG_XNUPOST
1179 int result = bsd_list_tests();
1180 result = bsd_do_post();
1181 if (result != 0) {
1182 panic("bsd_do_post: Tests failed with result = 0x%08x\n", result);
1183 }
1184#endif
5ba3f43e
A
1185
1186 bsd_init_kprintf("bsd_do_post - done");
813fb2f6 1187
1c79356b 1188 load_init_program(p);
91447636 1189 lock_trace = 1;
1c79356b
A
1190}
1191
2d21ac55
A
1192kern_return_t
1193bsd_autoconf(void)
9bccf70c 1194{
2d21ac55 1195 kprintf("bsd_autoconf: calling kminit\n");
1c79356b
A
1196 kminit();
1197
0a7de745 1198 /*
1c79356b
A
1199 * Early startup for bsd pseudodevices.
1200 */
1201 {
0a7de745
A
1202 struct pseudo_init *pi;
1203
1204 for (pi = pseudo_inits; pi->ps_func; pi++) {
1205 (*pi->ps_func)(pi->ps_count);
1206 }
1c79356b
A
1207 }
1208
0a7de745 1209 return IOKitBSDInit();
1c79356b
A
1210}
1211
1212
55e303ae 1213#include <sys/disklabel.h> /* for MAXPARTITIONS */
1c79356b 1214
91447636
A
1215static void
1216setconf(void)
0a7de745
A
1217{
1218 u_int32_t flags;
1219 kern_return_t err;
1c79356b 1220
cf7d32b8 1221 err = IOFindBSDRoot(rootdevice, sizeof(rootdevice), &rootdev, &flags);
0a7de745 1222 if (err) {
1c79356b 1223 printf("setconf: IOFindBSDRoot returned an error (%d);"
0a7de745 1224 "setting rootdevice to 'sd0a'.\n", err); /* XXX DEBUG TEMP */
1c79356b 1225 rootdev = makedev( 6, 0 );
2d21ac55 1226 strlcpy(rootdevice, "sd0a", sizeof(rootdevice));
1c79356b
A
1227 flags = 0;
1228 }
1229
cb323159 1230#if CONFIG_NETBOOT
0a7de745 1231 if (flags & 1) {
9bccf70c
A
1232 /* network device */
1233 mountroot = netboot_mountroot;
1c79356b 1234 } else {
91447636 1235#endif
0a7de745
A
1236 /* otherwise have vfs determine root filesystem */
1237 mountroot = NULL;
cb323159 1238#if CONFIG_NETBOOT
0a7de745 1239}
91447636 1240#endif
1c79356b
A
1241}
1242
f427ee49
A
1243/*
1244 * Boot into the flavor of Recovery dictated by `mode`.
1245 */
1246boolean_t
1247bsd_boot_to_recovery(bsd_bootfail_mode_t mode, uuid_t volume_uuid, boolean_t reboot)
1248{
1249 return IOSetRecoveryBoot(mode, volume_uuid, reboot);
1250}
1251
2d21ac55
A
1252void
1253bsd_utaskbootstrap(void)
1c79356b 1254{
2d21ac55 1255 thread_t thread;
9bccf70c 1256 struct uthread *ut;
1c79356b 1257
b0d623f7
A
1258 /*
1259 * Clone the bootstrap process from the kernel process, without
1260 * inheriting either task characteristics or memory from the kernel;
1261 */
fe8ab488 1262 thread = cloneproc(TASK_NULL, COALITION_NULL, kernproc, FALSE, TRUE);
b0d623f7 1263
2d21ac55 1264 /* Hold the reference as it will be dropped during shutdown */
0a7de745 1265 initproc = proc_find(1);
2d21ac55 1266#if __PROC_INTERNAL_DEBUG
0a7de745 1267 if (initproc == PROC_NULL) {
2d21ac55 1268 panic("bsd_utaskbootstrap: initproc not set\n");
0a7de745 1269 }
2d21ac55 1270#endif
c3c9b80d
A
1271
1272 zalloc_first_proc_made();
1273
b0d623f7
A
1274 /*
1275 * Since we aren't going back out the normal way to our parent,
1276 * we have to drop the transition locks explicitly.
1277 */
1278 proc_signalend(initproc, 0);
1279 proc_transend(initproc, 0);
9bccf70c 1280
2d21ac55 1281 ut = (struct uthread *)get_bsdthread_info(thread);
9bccf70c 1282 ut->uu_sigmask = 0;
2d21ac55 1283 act_set_astbsd(thread);
cb323159 1284 task_clear_return_wait(get_threadtask(thread), TCRW_CLEAR_ALL_WAIT);
1c79356b
A
1285}
1286
2d21ac55
A
1287static void
1288parse_bsd_args(void)
1c79356b 1289{
cb323159 1290 char namep[48];
2d21ac55 1291 int msgbuf;
1c79356b 1292
0a7de745 1293 if (PE_parse_boot_argn("-s", namep, sizeof(namep))) {
1c79356b 1294 boothowto |= RB_SINGLE;
0a7de745 1295 }
55e303ae 1296
0a7de745 1297 if (PE_parse_boot_argn("-x", namep, sizeof(namep))) { /* safe boot */
2d21ac55 1298 boothowto |= RB_SAFEBOOT;
0a7de745 1299 }
1c79356b 1300
3e170ce0
A
1301 if (PE_parse_boot_argn("-minimalboot", namep, sizeof(namep))) {
1302 /*
1303 * -minimalboot indicates that we want userspace to be bootstrapped to a
1304 * minimal environment. What constitutes minimal is up to the bootstrap
1305 * process.
1306 */
1307 minimalboot = 1;
1308 }
1309
a39ff7e2 1310#if __x86_64__
cb323159
A
1311 int no32exec;
1312
a39ff7e2 1313 /* disable 32 bit grading */
cb323159
A
1314 if (PE_parse_boot_argn("no32exec", &no32exec, sizeof(no32exec))) {
1315 bootarg_no32exec = !!no32exec;
0a7de745 1316 }
a39ff7e2 1317#endif
fe8ab488 1318
cb323159
A
1319 int execfailure_crashreports;
1320 /* enable crash reports on various exec failures */
1321 if (PE_parse_boot_argn("execfailurecrashes", &execfailure_crashreports, sizeof(execfailure_crashreports))) {
1322 bootarg_execfailurereports = !!execfailure_crashreports;
1323 }
1324
6d2010ae 1325 /* disable vnode_cache_is_authorized() by setting vnode_cache_defeat */
0a7de745 1326 if (PE_parse_boot_argn("-vnode_cache_defeat", namep, sizeof(namep))) {
6d2010ae 1327 bootarg_vnode_cache_defeat = 1;
0a7de745 1328 }
6d2010ae
A
1329
1330#if DEVELOPMENT || DEBUG
0a7de745 1331 if (PE_parse_boot_argn("-disable_aslr", namep, sizeof(namep))) {
6d2010ae 1332 bootarg_disable_aslr = 1;
0a7de745 1333 }
6d2010ae
A
1334#endif
1335
f427ee49
A
1336
1337
0a7de745 1338 PE_parse_boot_argn("ncl", &ncl, sizeof(ncl));
b0d623f7 1339 if (PE_parse_boot_argn("nbuf", &max_nbuf_headers,
0a7de745 1340 sizeof(max_nbuf_headers))) {
0c530ab8 1341 customnbuf = 1;
2d21ac55 1342 }
6d2010ae
A
1343
1344#if CONFIG_MACF
1345#if defined (__i386__) || defined (__x86_64__)
0a7de745 1346 PE_parse_boot_argn("policy_check", &policy_check_flags, sizeof(policy_check_flags));
6d2010ae 1347#endif
0a7de745 1348#endif /* CONFIG_MACF */
1c79356b 1349
0a7de745 1350 if (PE_parse_boot_argn("msgbuf", &msgbuf, sizeof(msgbuf))) {
2d21ac55 1351 log_setsize(msgbuf);
39037602 1352 oslog_setsize(msgbuf);
2d21ac55 1353 }
6d2010ae
A
1354
1355 if (PE_parse_boot_argn("-novfscache", namep, sizeof(namep))) {
1356 nc_disabled = 1;
1357 }
3e170ce0
A
1358
1359#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
0a7de745
A
1360 if (PE_parse_boot_argn("-no_vnode_jetsam", namep, sizeof(namep))) {
1361 bootarg_no_vnode_jetsam = 1;
1362 }
3e170ce0
A
1363#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
1364
94ff46dc
A
1365 if (PE_parse_boot_argn("-no_vnode_drain", namep, sizeof(namep))) {
1366 bootarg_no_vnode_drain = 1;
1367 }
3e170ce0 1368
f427ee49 1369#if CONFIG_DARKBOOT
5ba3f43e
A
1370 /*
1371 * The darkboot flag is specified by the bootloader and is stored in
1372 * boot_args->bootFlags. This flag is available starting revision 2.
1373 */
1374 boot_args *args = (boot_args *) PE_state.bootArgs;
1375 if ((args != NULL) && (args->Revision >= kBootArgsRevision2)) {
1376 darkboot = (args->bootFlags & kBootFlagsDarkBoot) ? 1 : 0;
1377 } else {
1378 darkboot = 0;
1379 }
1380#endif
3e170ce0
A
1381
1382#if PROC_REF_DEBUG
1383 if (PE_parse_boot_argn("-disable_procref_tracking", namep, sizeof(namep))) {
1384 proc_ref_tracking_disabled = 1;
1385 }
1386#endif
1387
1388 PE_parse_boot_argn("sigrestrict", &sigrestrict_arg, sizeof(sigrestrict_arg));
39037602 1389
0a7de745 1390#if DEVELOPMENT || DEBUG
39037602
A
1391 if (PE_parse_boot_argn("-no_sigsys", namep, sizeof(namep))) {
1392 send_sigsys = false;
1393 }
39037602 1394
39037602 1395 if (PE_parse_boot_argn("alt-dyld", dyld_alt_path, sizeof(dyld_alt_path))) {
0a7de745
A
1396 if (strlen(dyld_alt_path) > 0) {
1397 use_alt_dyld = 1;
1398 }
39037602 1399 }
cb323159
A
1400 PE_parse_boot_argn("dyld_flags", &dyld_flags, sizeof(dyld_flags));
1401
1402 if (PE_parse_boot_argn("-disable_syscallfilter", &namep, sizeof(namep))) {
1403 syscallfilter_disable = 1;
1404 }
1405
1406#if __arm64__
1407 if (PE_parse_boot_argn("legacy_footprint_entitlement_mode", &legacy_footprint_entitlement_mode, sizeof(legacy_footprint_entitlement_mode))) {
1408 /*
1409 * legacy_footprint_entitlement_mode specifies the behavior we want associated
1410 * with the entitlement. The supported modes are:
1411 *
1412 * LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE:
1413 * Indicates that we want every process to have the memory accounting
1414 * that is available in iOS 12.0 and beyond.
1415 *
1416 * LEGACY_FOOTPRINT_ENTITLEMENT_IOS11_ACCT:
1417 * Indicates that for every process that has the 'legacy footprint entitlement',
1418 * we want to give it the old iOS 11.0 accounting behavior which accounted some
1419 * of the process's memory to the kernel.
1420 *
1421 * LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE:
1422 * Indicates that for every process that has the 'legacy footprint entitlement',
1423 * we want it to have a higher memory limit which will help them acclimate to the
1424 * iOS 12.0 (& beyond) accounting behavior that does the right accounting.
1425 * The bonus added to the system-wide task limit to calculate this higher memory limit
1426 * is available in legacy_footprint_bonus_mb.
1427 */
1428
1429 if (legacy_footprint_entitlement_mode < LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE ||
1430 legacy_footprint_entitlement_mode > LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE) {
1431 legacy_footprint_entitlement_mode = LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE;
1432 }
1433 }
1434#endif /* __arm64__ */
1435#endif /* DEVELOPMENT || DEBUG */
1c79356b
A
1436}
1437
f427ee49
A
1438#if CONFIG_BASESYSTEMROOT
1439
1440extern const char* IOGetBootUUID(void);
1441extern const char* IOGetApfsPrebootUUID(void);
1442
1443// Get the UUID of the Preboot (and Recovery) folder associated with the
1444// current boot volume, if applicable. The meaning of the UUID can be
1445// filesystem-dependent and not all kinds of boots will have a UUID.
1446// If available, the string will be returned. It does not need to be
1447// deallocate. (Future: if we need to return the string as a copy that the
1448// caller must free, we'll introduce a new functcion for that.)
1449// NULL will be returned if the current boot has no applicable Preboot UUID.
1450static
1451const char *
1452get_preboot_uuid(void)
1453{
1454 const char *maybe_uuid_string;
1455
1456 // try IOGetApfsPrebootUUID
1457 maybe_uuid_string = IOGetApfsPrebootUUID();
1458 if (maybe_uuid_string) {
1459 uuid_t maybe_uuid;
1460 int error = uuid_parse(maybe_uuid_string, maybe_uuid);
1461 if (error == 0) {
1462 return maybe_uuid_string;
1463 }
1464 }
1465
1466 // try IOGetBootUUID
1467 maybe_uuid_string = IOGetBootUUID();
1468 if (maybe_uuid_string) {
1469 uuid_t maybe_uuid;
1470 int error = uuid_parse(maybe_uuid_string, maybe_uuid);
1471 if (error == 0) {
1472 return maybe_uuid_string;
1473 }
1474 }
1475
1476 // didn't find it
1477 return NULL;
1478}
1479
1480#if defined(__arm64__)
1481extern const char *IOGetBootObjectsPath(void);
1482#endif
1483
1484// Find the BaseSystem.dmg to be used as the initial root volume during certain
1485// kinds of boots.
1486// This may mount volumes and lookup vnodes.
1487// The DEVELOPMENT kernel will look for BaseSystem.rooted.dmg first.
1488// If it returns 0 (no error), then it also writes the absolute path to the
1489// BaseSystem.dmg into its argument (which must be a char[MAXPATHLEN]).
1490static
1491int
c3c9b80d 1492bsd_find_basesystem_dmg(char *bsdmgpath_out, bool *rooted_dmg, bool *skip_signature_check)
f427ee49
A
1493{
1494 int error;
1495 size_t len;
1496 char *dmgbasepath;
1497 char *dmgpath;
c3c9b80d 1498 bool allow_rooted_dmg = false;
f427ee49
A
1499
1500 dmgbasepath = zalloc_flags(ZV_NAMEI, Z_ZERO | Z_WAITOK);
1501 dmgpath = zalloc_flags(ZV_NAMEI, Z_ZERO | Z_WAITOK);
1502 vnode_t imagevp = NULLVP;
1503
c3c9b80d
A
1504#if DEVELOPMENT || DEBUG
1505 allow_rooted_dmg = true;
1506#endif
1507
f427ee49 1508 //must provide output bool
c3c9b80d 1509 if (rooted_dmg && skip_signature_check) {
f427ee49 1510 *rooted_dmg = false;
c3c9b80d 1511 *skip_signature_check = false;
f427ee49
A
1512 } else {
1513 error = EINVAL;
1514 goto done;
1515 }
1516
1517 error = vfs_mount_recovery();
1518 if (error) {
1519 goto done;
1520 }
1521
1522 len = strlcpy(dmgbasepath, "/System/Volumes/Recovery/", MAXPATHLEN);
1523 if (len > MAXPATHLEN) {
1524 error = ENAMETOOLONG;
1525 goto done;
1526 }
1527
c3c9b80d
A
1528 if (csr_check(CSR_ALLOW_ANY_RECOVERY_OS) == 0) {
1529 *skip_signature_check = true;
1530 allow_rooted_dmg = true;
1531 }
1532
f427ee49
A
1533#if defined(__arm64__)
1534 const char *boot_obj_path = IOGetBootObjectsPath();
1535 if (boot_obj_path) {
1536 if (boot_obj_path[0] == '/') {
1537 dmgbasepath[len - 1] = '\0';
1538 }
1539
1540 len = strlcat(dmgbasepath, boot_obj_path, MAXPATHLEN);
1541 if (len > MAXPATHLEN) {
1542 error = ENAMETOOLONG;
1543 goto done;
1544 }
1545
1546 len = strlcat(dmgbasepath, "/usr/standalone/firmware/", MAXPATHLEN);
1547 if (len > MAXPATHLEN) {
1548 error = ENAMETOOLONG;
1549 goto done;
1550 }
1551
c3c9b80d
A
1552 if (allow_rooted_dmg) {
1553 len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1554 if (len > MAXPATHLEN) {
1555 error = ENAMETOOLONG;
1556 goto done;
1557 }
f427ee49 1558
c3c9b80d
A
1559 len = strlcat(dmgpath, "arm64eBaseSystem.rooted.dmg", MAXPATHLEN);
1560 if (len > MAXPATHLEN) {
1561 error = ENAMETOOLONG;
1562 goto done;
1563 }
f427ee49 1564
c3c9b80d
A
1565 error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1566 if (error == 0) {
1567 *rooted_dmg = true;
1568 *skip_signature_check = true;
1569 goto done;
1570 }
1571 memset(dmgpath, 0, MAXPATHLEN);
f427ee49 1572 }
f427ee49
A
1573
1574 len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1575 if (len > MAXPATHLEN) {
1576 error = ENAMETOOLONG;
1577 goto done;
1578 }
1579
1580 len = strlcat(dmgpath, "arm64eBaseSystem.dmg", MAXPATHLEN);
1581 if (len > MAXPATHLEN) {
1582 error = ENAMETOOLONG;
1583 goto done;
1584 }
1585
1586 error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1587 if (error == 0) {
1588 goto done;
1589 }
1590 memset(dmgpath, 0, MAXPATHLEN);
1591 dmgbasepath[strlen("/System/Volumes/Recovery/")] = '\0';
1592 }
1593#endif // __arm64__
1594
1595 const char *preboot_uuid = get_preboot_uuid();
1596 if (preboot_uuid == NULL) {
1597 // no preboot? bail out
1598 return EINVAL;
1599 }
1600
1601 len = strlcat(dmgbasepath, preboot_uuid, MAXPATHLEN);
1602 if (len > MAXPATHLEN) {
1603 error = ENAMETOOLONG;
1604 goto done;
1605 }
1606
c3c9b80d
A
1607 if (allow_rooted_dmg) {
1608 // Try BaseSystem.rooted.dmg
1609 len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1610 if (len > MAXPATHLEN) {
1611 error = ENAMETOOLONG;
1612 goto done;
1613 }
f427ee49 1614
c3c9b80d
A
1615 len = strlcat(dmgpath, "/BaseSystem.rooted.dmg", MAXPATHLEN);
1616 if (len > MAXPATHLEN) {
1617 error = ENAMETOOLONG;
1618 goto done;
1619 }
f427ee49 1620
c3c9b80d
A
1621 error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1622 if (error == 0) {
1623 // we found it! success!
1624 *rooted_dmg = true;
1625 *skip_signature_check = true;
1626 goto done;
1627 }
f427ee49 1628 }
f427ee49
A
1629
1630 // Try BaseSystem.dmg
1631 len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1632 if (len > MAXPATHLEN) {
1633 error = ENAMETOOLONG;
1634 goto done;
1635 }
1636
1637 len = strlcat(dmgpath, "/BaseSystem.dmg", MAXPATHLEN);
1638 if (len > MAXPATHLEN) {
1639 error = ENAMETOOLONG;
1640 goto done;
1641 }
1642
1643 error = vnode_lookup(dmgpath, 0, &imagevp, vfs_context_kernel());
1644 if (error == 0) {
1645 // success!
1646 goto done;
1647 }
1648
1649done:
1650 if (error == 0) {
1651 strlcpy(bsdmgpath_out, dmgpath, MAXPATHLEN);
1652 } else {
1653 bsd_init_kprintf("%s: error %d\n", __func__, error);
1654 }
1655 if (imagevp != NULLVP) {
1656 vnode_put(imagevp);
1657 }
1658 zfree(ZV_NAMEI, dmgpath);
1659 zfree(ZV_NAMEI, dmgbasepath);
1660 return error;
1661}
1662
1663static boolean_t
1664bsdmgroot_bootable(void)
1665{
1666#if defined(__arm64__)
1667#define BSDMGROOT_DEFAULT true
1668#else
1669#define BSDMGROOT_DEFAULT false
1670#endif
1671
1672 boolean_t resolved = BSDMGROOT_DEFAULT;
1673
1674 boolean_t boot_arg_bsdmgroot = false;
1675 boolean_t boot_arg_nobsdmgroot = false;
1676 int error;
1677 mount_t mp;
1678 boolean_t root_part_of_volume_group = false;
1679 struct vfs_attr vfsattr;
1680
1681 mp = rootvnode->v_mount;
1682 VFSATTR_INIT(&vfsattr);
1683 VFSATTR_WANTED(&vfsattr, f_capabilities);
1684
1685 boot_arg_bsdmgroot = PE_parse_boot_argn("-bsdmgroot", NULL, 0);
1686 boot_arg_nobsdmgroot = PE_parse_boot_argn("-nobsdmgroot", NULL, 0);
1687
1688 error = vfs_getattr(mp, &vfsattr, vfs_context_kernel());
1689 if (!error && VFSATTR_IS_SUPPORTED(&vfsattr, f_capabilities)) {
1690 if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS) &&
1691 (vfsattr.f_capabilities.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS)) {
1692 root_part_of_volume_group = true;
1693 }
1694 }
1695
1696 boolean_t singleuser = (boothowto & RB_SINGLE) != 0;
1697
1698 // Start with the #defined default above.
1699 // If booting to single-user mode, default to false, because single-
1700 // user mode inside the BaseSystem is probably not what's wanted.
1701 // If the 'yes' boot-arg is set, we'll allow that even in single-user
1702 // mode, we'll assume you know what you're doing.
1703 // The 'no' boot-arg overpowers the 'yes' boot-arg.
1704 // In any case, we will not attempt to root from BaseSystem if the
1705 // original (booter-chosen) root volume isn't in a volume group.
1706 // This is just out of an abundance of caution: if the boot environment
1707 // seems to be "something other than a standard install",
1708 // we'll be conservative in messing with the root volume.
1709
1710 if (singleuser) {
1711 resolved = false;
1712 }
1713
1714 if (boot_arg_bsdmgroot) {
1715 resolved = true;
1716 }
1717
1718 if (boot_arg_nobsdmgroot) {
1719 resolved = false;
1720 }
1721
1722 if (!root_part_of_volume_group) {
1723 resolved = false;
1724 }
1725
1726 return resolved;
1727}
1728#endif // CONFIG_BASESYSTEMROOT
1729
b0d623f7
A
1730void
1731bsd_exec_setup(int scale)
1732{
b0d623f7 1733 switch (scale) {
0a7de745
A
1734 case 0:
1735 case 1:
1736 bsd_simul_execs = BSD_SIMUL_EXECS;
1737 break;
1738 case 2:
1739 case 3:
1740 bsd_simul_execs = 65;
1741 break;
1742 case 4:
1743 case 5:
1744 bsd_simul_execs = 129;
1745 break;
1746 case 6:
1747 case 7:
1748 bsd_simul_execs = 257;
1749 break;
1750 default:
1751 bsd_simul_execs = 513;
1752 break;
b0d623f7 1753 }
6d2010ae 1754 bsd_pageable_map_size = (bsd_simul_execs * BSD_PAGEABLE_SIZE_PER_EXEC);
b0d623f7
A
1755}
1756
cb323159 1757#if !CONFIG_NETBOOT
0a7de745 1758int
6d2010ae
A
1759netboot_root(void);
1760
0a7de745 1761int
91447636 1762netboot_root(void)
1c79356b 1763{
0a7de745 1764 return 0;
1c79356b 1765}
91447636 1766#endif