]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/kern/bsd_init.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / bsd_init.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2020 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
111#include <mach/vm_param.h>
112
113#include <vm/vm_map.h>
114#include <vm/vm_kern.h>
115
116#include <sys/reboot.h>
117#include <dev/busvar.h> /* for pseudo_inits */
118#include <sys/kdebug.h>
119#include <sys/monotonic.h>
120
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>
126#include <sys/csr.h>
127#include <mach/kern_return.h>
128#include <mach/thread_act.h> /* for thread_resume() */
129#include <sys/mcache.h> /* for mcache_init() */
130#include <sys/mbuf.h> /* for mbinit() */
131#include <sys/event.h> /* for knote_init() */
132#include <sys/eventhandler.h> /* for eventhandler_init() */
133#include <sys/kern_memorystatus.h> /* for memorystatus_init() */
134#include <sys/kern_memorystatus_freeze.h> /* for memorystatus_freeze_init() */
135#include <sys/aio_kern.h> /* for aio_init() */
136#include <sys/semaphore.h> /* for psem_cache_init() */
137#include <net/dlil.h> /* for dlil_init() */
138#include <net/kpi_protocol.h> /* for proto_kpi_init() */
139#include <net/iptap.h> /* for iptap_init() */
140#include <sys/socketvar.h> /* for socketinit() */
141#include <sys/protosw.h> /* for domaininit() */
142#include <kern/sched_prim.h> /* for thread_wakeup() */
143#include <net/if_ether.h> /* for ether_family_init() */
144#include <net/if_gif.h> /* for gif_init() */
145#include <miscfs/devfs/devfsdefs.h> /* for devfs_kernel_mount() */
146#include <vm/vm_kern.h> /* for kmem_suballoc() */
147#include <sys/msgbuf.h> /* for log_setsize() */
148#include <sys/proc_uuid_policy.h> /* proc_uuid_policy_init() */
149#include <netinet/flow_divert.h> /* flow_divert_init() */
150#include <net/content_filter.h> /* for cfil_init() */
151#include <net/necp.h> /* for necp_init() */
152#include <net/network_agent.h> /* for netagent_init() */
153#include <net/packet_mangler.h> /* for pkt_mnglr_init() */
154#include <net/if_utun.h> /* for utun_register_control() */
155#include <net/if_ipsec.h> /* for ipsec_register_control() */
156#include <net/net_str_id.h> /* for net_str_id_init() */
157#include <net/netsrc.h> /* for netsrc_init() */
158#include <net/ntstat.h> /* for nstat_init() */
159#include <netinet/tcp_cc.h> /* for tcp_cc_init() */
160#include <netinet/mptcp_var.h> /* for mptcp_control_register() */
161#include <net/nwk_wq.h> /* for nwk_wq_init */
162#include <net/restricted_in_port.h> /* for restricted_in_port_init() */
163#include <kern/assert.h> /* for assert() */
164#include <sys/kern_overrides.h> /* for init_system_override() */
165#include <sys/lockf.h> /* for lf_init() */
166#include <sys/fsctl.h>
167
168#include <net/init.h>
169
170#if CONFIG_MACF
171#include <security/mac_framework.h>
172#include <security/mac_internal.h> /* mac_init_bsd() */
173#include <security/mac_mach_internal.h> /* mac_update_task_label() */
174#endif
175
176#include <machine/exec.h>
177
178#if CONFIG_NETBOOT
179#include <sys/netboot.h>
180#endif
181
182#if CONFIG_IMAGEBOOT
183#include <sys/imageboot.h>
184#endif
185
186#if PFLOG
187#include <net/if_pflog.h>
188#endif
189
190
191#include <pexpert/pexpert.h>
192#include <machine/pal_routines.h>
193#include <console/video_console.h>
194
195#if CONFIG_XNUPOST
196#include <tests/xnupost.h>
197#endif
198
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 */
203
204const char *const copyright =
205 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t"
206 "The Regents of the University of California. "
207 "All rights reserved.\n\n";
208
209/* Components of the first process -- never freed. */
210struct proc proc0 = { .p_comm = "kernel_task", .p_name = "kernel_task" };
211struct session session0;
212struct pgrp pgrp0;
213struct filedesc filedesc0;
214struct plimit limit0;
215struct pstats pstats0;
216struct sigacts sigacts0;
217SECURITY_READ_ONLY_LATE(proc_t) kernproc = &proc0;
218proc_t XNU_PTRAUTH_SIGNED_PTR("initproc") initproc;
219
220long tk_cancc;
221long tk_nin;
222long tk_nout;
223long tk_rawcc;
224
225int lock_trace = 0;
226/* Global variables to make pstat happy. We do swapping differently */
227int nswdev, nswap;
228int nswapmap;
229void *swapmap;
230struct swdevt swdevt[1];
231
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
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];
241char domainname[MAXDOMNAMELEN];
242char rootdevice[DEVMAXNAMESIZE];
243
244struct vnode *rootvp;
245bool rootvp_is_ssd = false;
246int boothowto;
247int minimalboot = 0;
248#if CONFIG_DARKBOOT
249int darkboot = 0;
250#endif
251
252#if __arm64__
253int legacy_footprint_entitlement_mode = LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE;
254#endif /* __arm64__ */
255
256#if PROC_REF_DEBUG
257__private_extern__ int proc_ref_tracking_disabled = 0; /* disable panics on leaked proc refs across syscall boundary */
258#endif
259
260extern kern_return_t IOFindBSDRoot(char *, unsigned int, dev_t *, u_int32_t *);
261extern void IOSecureBSDRoot(const char * rootName);
262extern kern_return_t IOKitBSDInit(void );
263extern boolean_t IOSetRecoveryBoot(bsd_bootfail_mode_t, uuid_t, boolean_t);
264extern void kminit(void);
265extern void bsd_bufferinit(void);
266extern void oslog_setsize(int size);
267extern void throttle_init(void);
268
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
277extern int ncl;
278#if DEVELOPMENT || DEBUG
279extern int syscallfilter_disable;
280#endif // DEVELOPMENT || DEBUG
281
282vm_map_t bsd_pageable_map;
283vm_map_t mb_map;
284
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;
289__private_extern__ vm_offset_t * execargs_cache = NULL;
290
291void bsd_exec_setup(int);
292
293__private_extern__ int bootarg_execfailurereports = 0;
294
295#if __x86_64__
296__private_extern__ int bootarg_no32exec = 1;
297#endif
298__private_extern__ int bootarg_vnode_cache_defeat = 0;
299
300#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
301__private_extern__ int bootarg_no_vnode_jetsam = 0;
302#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
303
304__private_extern__ int bootarg_no_vnode_drain = 0;
305
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
312
313
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;
321extern uint64_t dyld_flags;
322#endif
323
324int cmask = CMASK;
325extern int customnbuf;
326
327kern_return_t bsd_autoconf(void);
328void bsd_utaskbootstrap(void);
329
330static void parse_bsd_args(void);
331#if CONFIG_DEV_KMEM
332extern void dev_kmem_init(void);
333#endif
334extern void select_waitq_init(void);
335static void process_name(const char *, proc_t);
336
337static void setconf(void);
338
339#if CONFIG_BASESYSTEMROOT
340static int bsd_find_basesystem_dmg(char *bsdmgpath_out, bool *rooted_dmg, bool *skip_signature_check);
341static boolean_t bsdmgroot_bootable(void);
342#endif // CONFIG_BASESYSTEMROOT
343
344static bool bsd_rooted_ramdisk(void);
345
346#if SYSV_SHM
347extern void sysv_shm_lock_init(void);
348#endif
349#if SYSV_SEM
350extern void sysv_sem_lock_init(void);
351#endif
352#if SYSV_MSG
353extern void sysv_msg_lock_init(void);
354#endif
355
356extern void ulock_initialize(void);
357
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;
362
363extern int check_policy_init(int);
364#endif
365#endif /* CONFIG_MACF */
366
367/* If we are using CONFIG_DTRACE */
368#if CONFIG_DTRACE
369extern void dtrace_postinit(void);
370#endif
371
372/*
373 * Initialization code.
374 * Called from cold start routine as
375 * soon as a stack and segmentation
376 * have been established.
377 * Functions:
378 * turn on clock
379 * hand craft 0th process
380 * call all initialization routines
381 * hand craft 1st user process
382 */
383
384/*
385 * Sets the name for the given task.
386 */
387static void
388process_name(const char *s, proc_t p)
389{
390 strlcpy(p->p_comm, s, sizeof(p->p_comm));
391 strlcpy(p->p_name, s, sizeof(p->p_name));
392}
393
394/* To allow these values to be patched, they're globals here */
395#include <machine/vmparam.h>
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 };
399
400extern struct os_refgrp rlimit_refgrp;
401
402extern thread_t cloneproc(task_t, coalition_t, proc_t, int, int);
403extern int (*mountroot)(void);
404
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");
414
415
416LCK_MTX_DECLARE_ATTR(proc_list_mlock, &proc_mlock_grp, &proc_lck_attr);
417
418#if XNU_TARGET_OS_OSX
419/* hook called after root is mounted XXX temporary hack */
420void (*mountroot_post_hook)(void);
421void (*unmountroot_pre_hook)(void);
422#endif
423void set_rootvnode(vnode_t);
424
425extern lck_rw_t rootvnode_rw_lock;
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}
472
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,
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.
486 */
487
488void
489bsd_init(void)
490{
491 struct uthread *ut;
492 unsigned int i;
493 struct vfs_context context;
494 kern_return_t ret;
495 struct ucred temp_cred;
496 struct posix_cred temp_pcred;
497 vnode_t init_rootvnode = NULLVP;
498#if CONFIG_NETBOOT || CONFIG_IMAGEBOOT
499 boolean_t netboot = FALSE;
500#endif
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];
507 const char *slash_dev = NULL;
508#endif
509
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
517
518 throttle_init();
519
520 printf(copyright);
521
522 bsd_init_kprintf("calling parse_bsd_args\n");
523 parse_bsd_args();
524
525#if CONFIG_DEV_KMEM
526 bsd_init_kprintf("calling dev_kmem_init\n");
527 dev_kmem_init();
528#endif
529
530 /* Initialize kauth subsystem before instancing the first credential */
531 bsd_init_kprintf("calling kauth_init\n");
532 kauth_init();
533
534 /* Initialize process and pgrp structures. */
535 bsd_init_kprintf("calling procinit\n");
536 procinit();
537
538 /* kernel_task->proc = kernproc; */
539 set_bsdtask_info(kernel_task, (void *)kernproc);
540
541 /* give kernproc a name */
542 bsd_init_kprintf("calling process_name\n");
543 process_name("kernel_task", kernproc);
544
545 /* Allocate proc lock attribute */
546
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);
552
553 assert(bsd_simul_execs != 0);
554 execargs_cache_size = bsd_simul_execs;
555 execargs_free_count = bsd_simul_execs;
556 execargs_cache = zalloc_permanent(bsd_simul_execs * sizeof(vm_offset_t),
557 ZALIGN(vm_offset_t));
558
559 if (current_task() != kernel_task) {
560 printf("bsd_init: We have a problem, "
561 "current task is not kernel task\n");
562 }
563
564 bsd_init_kprintf("calling get_bsdthread_info\n");
565 ut = (uthread_t)get_bsdthread_info(current_thread());
566
567#if CONFIG_MACF
568 /*
569 * Initialize the MAC Framework
570 */
571 mac_policy_initbsd();
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
580#endif /* MAC */
581
582 ulock_initialize();
583
584 /*
585 * Create process 0.
586 */
587 proc_list_lock();
588 LIST_INSERT_HEAD(&allproc, kernproc, p_list);
589 kernproc->p_pgrp = &pgrp0;
590 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
591 LIST_INIT(&pgrp0.pg_members);
592 lck_mtx_init(&pgrp0.pg_mlock, &proc_mlock_grp, &proc_lck_attr);
593 /* There is no other bsd thread this point and is safe without pgrp lock */
594 LIST_INSERT_HEAD(&pgrp0.pg_members, kernproc, p_pglist);
595 kernproc->p_listflag |= P_LIST_INPGRP;
596 kernproc->p_pgrpid = 0;
597 kernproc->p_uniqueid = 0;
598
599 pgrp0.pg_session = &session0;
600 pgrp0.pg_membercnt = 1;
601
602 session0.s_count = 1;
603 session0.s_leader = kernproc;
604 session0.s_listflags = 0;
605 lck_mtx_init(&session0.s_mlock, &proc_mlock_grp, &proc_lck_attr);
606 LIST_INSERT_HEAD(SESSHASH(0), &session0, s_hash);
607 proc_list_unlock();
608
609#if CONFIG_PERSONAS
610 kernproc->p_persona = NULL;
611#endif
612
613 kernproc->task = kernel_task;
614
615 kernproc->p_stat = SRUN;
616 kernproc->p_flag = P_SYSTEM;
617 kernproc->p_lflag = 0;
618 kernproc->p_ladvflag = 0;
619
620#if defined(__LP64__)
621 kernproc->p_flag |= P_LP64;
622#endif
623
624#if DEVELOPMENT || DEBUG
625 if (bootarg_disable_aslr) {
626 kernproc->p_flag |= P_DISABLE_ASLR;
627 }
628#endif
629
630 kernproc->p_nice = NZERO;
631 kernproc->p_pptr = kernproc;
632
633 TAILQ_INIT(&kernproc->p_uthlist);
634 TAILQ_INSERT_TAIL(&kernproc->p_uthlist, ut, uu_list);
635
636 kernproc->sigwait = FALSE;
637 kernproc->sigwait_thread = THREAD_NULL;
638 kernproc->exit_thread = THREAD_NULL;
639 kernproc->p_csflags = CS_VALID;
640
641 /*
642 * Create credential. This also Initializes the audit information.
643 */
644 bsd_init_kprintf("calling bzero\n");
645 bzero(&temp_cred, sizeof(temp_cred));
646 bzero(&temp_pcred, sizeof(temp_pcred));
647 temp_pcred.cr_ngroups = 1;
648 /* kern_proc, shouldn't call up to DS for group membership */
649 temp_pcred.cr_flags = CRF_NOMEMBERD;
650 temp_cred.cr_audit.as_aia_p = audit_default_aia_p;
651
652 bsd_init_kprintf("calling kauth_cred_create\n");
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);
658 kernproc->p_ucred = kauth_cred_create(&temp_cred);
659
660 /* update cred on proc */
661 PROC_UPDATE_CREDS_ONPROC(kernproc);
662
663 /* give the (already exisiting) initial thread a reference on it */
664 bsd_init_kprintf("calling kauth_cred_ref\n");
665 kauth_cred_ref(kernproc->p_ucred);
666 ut->uu_context.vc_ucred = kernproc->p_ucred;
667 ut->uu_context.vc_thread = current_thread();
668
669 vfs_set_context_kernel(&ut->uu_context);
670
671 TAILQ_INIT(&kernproc->p_aio_activeq);
672 TAILQ_INIT(&kernproc->p_aio_doneq);
673 kernproc->p_aio_total_count = 0;
674
675#if CONFIG_MACF
676 mac_cred_label_associate_kernel(kernproc->p_ucred);
677#endif
678
679 /* Create the file descriptor table. */
680 kernproc->p_fd = &filedesc0;
681 filedesc0.fd_cmask = (mode_t)cmask;
682 filedesc0.fd_knlistsize = 0;
683 filedesc0.fd_knlist = NULL;
684 filedesc0.fd_knhash = NULL;
685 filedesc0.fd_knhashmask = 0;
686 lck_mtx_init(&filedesc0.fd_kqhashlock, &proc_kqhashlock_grp, &proc_lck_attr);
687 lck_mtx_init(&filedesc0.fd_knhashlock, &proc_knhashlock_grp, &proc_lck_attr);
688
689 /* Create the limits structures. */
690 kernproc->p_limit = &limit0;
691 for (i = 0; i < sizeof(kernproc->p_limit->pl_rlimit) / sizeof(kernproc->p_limit->pl_rlimit[0]); i++) {
692 limit0.pl_rlimit[i].rlim_cur =
693 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
694 }
695 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
696 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = maxprocperuid;
697 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
698 limit0.pl_rlimit[RLIMIT_STACK] = vm_initial_limit_stack;
699 limit0.pl_rlimit[RLIMIT_DATA] = vm_initial_limit_data;
700 limit0.pl_rlimit[RLIMIT_CORE] = vm_initial_limit_core;
701 os_ref_init_count(&limit0.pl_refcnt, &rlimit_refgrp, 1);
702
703 kernproc->p_stats = &pstats0;
704 kernproc->p_sigacts = &sigacts0;
705 kernproc->p_subsystem_root_path = NULL;
706
707 /*
708 * Charge root for one process: launchd.
709 */
710 bsd_init_kprintf("calling chgproccnt\n");
711 (void)chgproccnt(0, 1);
712
713 /*
714 * Allocate a kernel submap for pageable memory
715 * for temporary copying (execve()).
716 */
717 {
718 vm_offset_t minimum;
719
720 bsd_init_kprintf("calling kmem_suballoc\n");
721 assert(bsd_pageable_map_size != 0);
722 ret = kmem_suballoc(kernel_map,
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) {
731 panic("bsd_init: Failed to allocate bsd pageable map");
732 }
733 }
734
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 */
742 bsd_init_kprintf("calling bsd_bufferinit\n");
743 bsd_bufferinit();
744
745 /*
746 * Initialize the calendar.
747 */
748 bsd_init_kprintf("calling IOKitInitializeTime\n");
749 IOKitInitializeTime();
750
751 /* Initialize the file systems. */
752 bsd_init_kprintf("calling vfsinit\n");
753 vfsinit();
754
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
761#if SOCKETS
762 /* Initialize per-CPU cache allocator */
763 mcache_init();
764
765 /* Initialize mbuf's. */
766 bsd_init_kprintf("calling mbinit\n");
767 mbinit();
768 net_str_id_init(); /* for mbuf tags */
769 restricted_in_port_init();
770#endif /* SOCKETS */
771
772 /*
773 * Initializes security event auditing.
774 * XXX: Should/could this occur later?
775 */
776#if CONFIG_AUDIT
777 bsd_init_kprintf("calling audit_init\n");
778 audit_init();
779#endif
780
781 /* Initialize kqueues */
782 bsd_init_kprintf("calling knote_init\n");
783 knote_init();
784
785 /* Initialize event handler */
786 bsd_init_kprintf("calling eventhandler_init\n");
787 eventhandler_init();
788
789 /* Initialize for async IO */
790 bsd_init_kprintf("calling aio_init\n");
791 aio_init();
792
793 pthread_init();
794 /* POSIX Shm and Sem */
795 bsd_init_kprintf("calling pshm_cache_init\n");
796 pshm_cache_init();
797 bsd_init_kprintf("calling psem_cache_init\n");
798 psem_cache_init();
799 bsd_init_kprintf("calling select_waitq_init\n");
800 select_waitq_init();
801
802 /*
803 * Initialize protocols. Block reception of incoming packets
804 * until everything is ready.
805 */
806#if NETWORKING
807 bsd_init_kprintf("calling nwk_wq_init\n");
808 nwk_wq_init();
809 bsd_init_kprintf("calling dlil_init\n");
810 dlil_init();
811 bsd_init_kprintf("calling proto_kpi_init\n");
812 proto_kpi_init();
813#endif /* NETWORKING */
814#if SOCKETS
815 bsd_init_kprintf("calling socketinit\n");
816 socketinit();
817 bsd_init_kprintf("calling domaininit\n");
818 domaininit();
819 iptap_init();
820#if FLOW_DIVERT
821 flow_divert_init();
822#endif /* FLOW_DIVERT */
823#endif /* SOCKETS */
824#if NETWORKING
825#if NECP
826 /* Initialize Network Extension Control Policies */
827 necp_init();
828#endif
829 netagent_init();
830#endif /* NETWORKING */
831 kernproc->p_fd->fd_cdir = NULL;
832 kernproc->p_fd->fd_rdir = NULL;
833
834#if defined (__x86_64__) && (DEBUG || DEVELOPMENT)
835 hvg_bsd_init();
836#endif /* DEBUG || DEVELOPMENT */
837
838#if CONFIG_FREEZE
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();
845#endif
846
847#if CONFIG_MEMORYSTATUS
848 /* Initialize kernel memory status notifications */
849 bsd_init_kprintf("calling memorystatus_init\n");
850 memorystatus_init();
851#endif /* CONFIG_MEMORYSTATUS */
852
853 bsd_init_kprintf("calling sysctl_mib_init\n");
854 sysctl_mib_init();
855
856 bsd_init_kprintf("calling bsd_autoconf\n");
857 bsd_autoconf();
858
859#if CONFIG_DTRACE
860 dtrace_postinit();
861#endif
862
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
870 bsd_init_kprintf("calling loopattach\n");
871 loopattach(); /* XXX */
872#endif
873#if NGIF
874 /* Initialize gif interface (after lo0) */
875 gif_init();
876#endif
877
878#if PFLOG
879 /* Initialize packet filter log interface */
880 pfloginit();
881#endif /* PFLOG */
882
883#if NETHER > 0
884 /* Register the built-in dlil ethernet interface family */
885 bsd_init_kprintf("calling ether_family_init\n");
886 ether_family_init();
887#endif /* ETHER */
888
889#if NETWORKING
890#if CONTENT_FILTER
891 cfil_init();
892#endif
893
894#if PACKET_MANGLER
895 pkt_mnglr_init();
896#endif
897
898 /*
899 * Register subsystems with kernel control handlers
900 */
901 utun_register_control();
902#if IPSEC
903 ipsec_register_control();
904#endif /* IPSEC */
905 netsrc_init();
906 nstat_init();
907 tcp_cc_init();
908#if MPTCP
909 mptcp_control_register();
910#endif /* MPTCP */
911
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 */
919
920 bsd_init_kprintf("calling inittodr\n");
921 inittodr(0);
922
923 /* Mount the root file system. */
924 while (TRUE) {
925 int err;
926
927 bsd_init_kprintf("calling setconf\n");
928 setconf();
929#if CONFIG_NETBOOT
930 netboot = (mountroot == netboot_mountroot);
931#endif
932
933 bsd_init_kprintf("vfs_mountroot\n");
934 if (0 == (err = vfs_mountroot())) {
935 break;
936 }
937 rootdevice[0] = '\0';
938#if CONFIG_NETBOOT
939 if (netboot) {
940 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
941 vc_progress_set(FALSE, 0);
942 for (i = 1; 1; i *= 2) {
943 printf("bsd_init: failed to mount network root, error %d, %s\n",
944 err, PE_boot_args());
945 printf("We are hanging here...\n");
946 IOSleep(i * 60 * 1000);
947 }
948 /*NOTREACHED*/
949 }
950#endif
951 printf("cannot mount root, errno = %d\n", err);
952 boothowto |= RB_ASKNAME;
953 }
954
955 IOSecureBSDRoot(rootdevice);
956
957 context.vc_thread = current_thread();
958 context.vc_ucred = kernproc->p_ucred;
959 mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
960
961 bsd_init_kprintf("calling VFS_ROOT\n");
962 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
963 if (VFS_ROOT(mountlist.tqh_first, &init_rootvnode, &context)) {
964 panic("bsd_init: cannot find root vnode: %s", PE_boot_args());
965 }
966 (void)vnode_ref(init_rootvnode);
967 (void)vnode_put(init_rootvnode);
968
969 lck_rw_lock_exclusive(&rootvnode_rw_lock);
970 set_rootvnode(init_rootvnode);
971 lck_rw_unlock_exclusive(&rootvnode_rw_lock);
972 init_rootvnode = NULLVP; /* use rootvnode after this point */
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
991
992#if CONFIG_NETBOOT
993 if (netboot) {
994 int err;
995
996 netboot = TRUE;
997 /* post mount setup */
998 if ((err = netboot_setup()) != 0) {
999 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
1000 vc_progress_set(FALSE, 0);
1001 for (i = 1; 1; i *= 2) {
1002 printf("bsd_init: NetBoot could not find root, error %d: %s\n",
1003 err, PE_boot_args());
1004 printf("We are hanging here...\n");
1005 IOSleep(i * 60 * 1000);
1006 }
1007 /*NOTREACHED*/
1008 }
1009 }
1010#endif
1011
1012
1013#if CONFIG_IMAGEBOOT
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
1045
1046 /*
1047 * See if a system disk image is present. If so, mount it and
1048 * switch the root vnode to point to it
1049 */
1050 imageboot_type_t imageboot_type = imageboot_needed();
1051 if (netboot == FALSE && imageboot_type) {
1052 /*
1053 * An image was found. No turning back: we're booted
1054 * with a kernel from the disk image.
1055 */
1056 bsd_init_kprintf("doing image boot: type = %d\n", imageboot_type);
1057 imageboot_setup(imageboot_type);
1058 }
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 */
1069#endif /* CONFIG_IMAGEBOOT */
1070
1071 /* set initial time; all other resource data is already zero'ed */
1072 microtime_with_abstime(&kernproc->p_start, &kernproc->p_stats->ps_start);
1073
1074#if DEVFS
1075 {
1076 char mounthere[] = "/dev"; /* !const because of internal casting */
1077
1078 bsd_init_kprintf("calling devfs_kernel_mount\n");
1079 devfs_kernel_mount(mounthere);
1080 }
1081#endif /* DEVFS */
1082
1083#if CONFIG_BASESYSTEMROOT
1084#if CONFIG_IMAGEBOOT
1085 if (bsdmgroot_bootable()) {
1086 int error;
1087 bool rooted_dmg = false;
1088 bool skip_signature_check = false;
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
1094
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
1101 error = bsd_find_basesystem_dmg(dmgpath, &rooted_dmg, &skip_signature_check);
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
1109 error = imageboot_pivot_image(dmgpath, IMAGEBOOT_DMG, "/System/Volumes/BaseSystem", "System/Volumes/macOS", rooted_dmg, skip_signature_check);
1110 if (error) {
1111 bsd_init_kprintf("couldn't mount BaseSystem dmg: error = %d", error);
1112 }
1113 }
1114 zfree(ZV_NAMEI, dmgpath);
1115 }
1116#else /* CONFIG_IMAGEBOOT */
1117#error CONFIG_BASESYSTEMROOT requires CONFIG_IMAGEBOOT
1118#endif /* CONFIG_IMAGEBOOT */
1119#endif /* CONFIG_BASESYSTEMROOT */
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
1137 /* Initialize signal state for process 0. */
1138 bsd_init_kprintf("calling siginit\n");
1139 siginit(kernproc);
1140
1141 bsd_init_kprintf("calling bsd_utaskbootstrap\n");
1142 bsd_utaskbootstrap();
1143
1144 pal_kernel_announce();
1145
1146 bsd_init_kprintf("calling mountroot_post_hook\n");
1147
1148#if XNU_TARGET_OS_OSX
1149 /* invoke post-root-mount hook */
1150 if (mountroot_post_hook != NULL) {
1151 mountroot_post_hook();
1152 }
1153#endif
1154
1155#if 0 /* not yet */
1156 consider_zone_gc(FALSE);
1157#endif
1158
1159 bsd_init_kprintf("done\n");
1160}
1161
1162void
1163bsdinit_task(void)
1164{
1165 proc_t p = current_proc();
1166
1167 process_name("init", p);
1168
1169 /* Set up exception-to-signal reflection */
1170 ux_handler_setup();
1171
1172#if CONFIG_MACF
1173 mac_cred_label_associate_user(p->p_ucred);
1174#endif
1175
1176 vm_init_before_launchd();
1177
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
1185
1186 bsd_init_kprintf("bsd_do_post - done");
1187
1188 load_init_program(p);
1189 lock_trace = 1;
1190}
1191
1192kern_return_t
1193bsd_autoconf(void)
1194{
1195 kprintf("bsd_autoconf: calling kminit\n");
1196 kminit();
1197
1198 /*
1199 * Early startup for bsd pseudodevices.
1200 */
1201 {
1202 struct pseudo_init *pi;
1203
1204 for (pi = pseudo_inits; pi->ps_func; pi++) {
1205 (*pi->ps_func)(pi->ps_count);
1206 }
1207 }
1208
1209 return IOKitBSDInit();
1210}
1211
1212
1213#include <sys/disklabel.h> /* for MAXPARTITIONS */
1214
1215static void
1216setconf(void)
1217{
1218 u_int32_t flags;
1219 kern_return_t err;
1220
1221 err = IOFindBSDRoot(rootdevice, sizeof(rootdevice), &rootdev, &flags);
1222 if (err) {
1223 printf("setconf: IOFindBSDRoot returned an error (%d);"
1224 "setting rootdevice to 'sd0a'.\n", err); /* XXX DEBUG TEMP */
1225 rootdev = makedev( 6, 0 );
1226 strlcpy(rootdevice, "sd0a", sizeof(rootdevice));
1227 flags = 0;
1228 }
1229
1230#if CONFIG_NETBOOT
1231 if (flags & 1) {
1232 /* network device */
1233 mountroot = netboot_mountroot;
1234 } else {
1235#endif
1236 /* otherwise have vfs determine root filesystem */
1237 mountroot = NULL;
1238#if CONFIG_NETBOOT
1239}
1240#endif
1241}
1242
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
1252void
1253bsd_utaskbootstrap(void)
1254{
1255 thread_t thread;
1256 struct uthread *ut;
1257
1258 /*
1259 * Clone the bootstrap process from the kernel process, without
1260 * inheriting either task characteristics or memory from the kernel;
1261 */
1262 thread = cloneproc(TASK_NULL, COALITION_NULL, kernproc, FALSE, TRUE);
1263
1264 /* Hold the reference as it will be dropped during shutdown */
1265 initproc = proc_find(1);
1266#if __PROC_INTERNAL_DEBUG
1267 if (initproc == PROC_NULL) {
1268 panic("bsd_utaskbootstrap: initproc not set\n");
1269 }
1270#endif
1271
1272 zalloc_first_proc_made();
1273
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);
1280
1281 ut = (struct uthread *)get_bsdthread_info(thread);
1282 ut->uu_sigmask = 0;
1283 act_set_astbsd(thread);
1284 task_clear_return_wait(get_threadtask(thread), TCRW_CLEAR_ALL_WAIT);
1285}
1286
1287static void
1288parse_bsd_args(void)
1289{
1290 char namep[48];
1291 int msgbuf;
1292
1293 if (PE_parse_boot_argn("-s", namep, sizeof(namep))) {
1294 boothowto |= RB_SINGLE;
1295 }
1296
1297 if (PE_parse_boot_argn("-x", namep, sizeof(namep))) { /* safe boot */
1298 boothowto |= RB_SAFEBOOT;
1299 }
1300
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
1310#if __x86_64__
1311 int no32exec;
1312
1313 /* disable 32 bit grading */
1314 if (PE_parse_boot_argn("no32exec", &no32exec, sizeof(no32exec))) {
1315 bootarg_no32exec = !!no32exec;
1316 }
1317#endif
1318
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
1325 /* disable vnode_cache_is_authorized() by setting vnode_cache_defeat */
1326 if (PE_parse_boot_argn("-vnode_cache_defeat", namep, sizeof(namep))) {
1327 bootarg_vnode_cache_defeat = 1;
1328 }
1329
1330#if DEVELOPMENT || DEBUG
1331 if (PE_parse_boot_argn("-disable_aslr", namep, sizeof(namep))) {
1332 bootarg_disable_aslr = 1;
1333 }
1334#endif
1335
1336
1337
1338 PE_parse_boot_argn("ncl", &ncl, sizeof(ncl));
1339 if (PE_parse_boot_argn("nbuf", &max_nbuf_headers,
1340 sizeof(max_nbuf_headers))) {
1341 customnbuf = 1;
1342 }
1343
1344#if CONFIG_MACF
1345#if defined (__i386__) || defined (__x86_64__)
1346 PE_parse_boot_argn("policy_check", &policy_check_flags, sizeof(policy_check_flags));
1347#endif
1348#endif /* CONFIG_MACF */
1349
1350 if (PE_parse_boot_argn("msgbuf", &msgbuf, sizeof(msgbuf))) {
1351 log_setsize(msgbuf);
1352 oslog_setsize(msgbuf);
1353 }
1354
1355 if (PE_parse_boot_argn("-novfscache", namep, sizeof(namep))) {
1356 nc_disabled = 1;
1357 }
1358
1359#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
1360 if (PE_parse_boot_argn("-no_vnode_jetsam", namep, sizeof(namep))) {
1361 bootarg_no_vnode_jetsam = 1;
1362 }
1363#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
1364
1365 if (PE_parse_boot_argn("-no_vnode_drain", namep, sizeof(namep))) {
1366 bootarg_no_vnode_drain = 1;
1367 }
1368
1369#if CONFIG_DARKBOOT
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
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));
1389
1390#if DEVELOPMENT || DEBUG
1391 if (PE_parse_boot_argn("-no_sigsys", namep, sizeof(namep))) {
1392 send_sigsys = false;
1393 }
1394
1395 if (PE_parse_boot_argn("alt-dyld", dyld_alt_path, sizeof(dyld_alt_path))) {
1396 if (strlen(dyld_alt_path) > 0) {
1397 use_alt_dyld = 1;
1398 }
1399 }
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 */
1436}
1437
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
1492bsd_find_basesystem_dmg(char *bsdmgpath_out, bool *rooted_dmg, bool *skip_signature_check)
1493{
1494 int error;
1495 size_t len;
1496 char *dmgbasepath;
1497 char *dmgpath;
1498 bool allow_rooted_dmg = false;
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
1504#if DEVELOPMENT || DEBUG
1505 allow_rooted_dmg = true;
1506#endif
1507
1508 //must provide output bool
1509 if (rooted_dmg && skip_signature_check) {
1510 *rooted_dmg = false;
1511 *skip_signature_check = false;
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
1528 if (csr_check(CSR_ALLOW_ANY_RECOVERY_OS) == 0) {
1529 *skip_signature_check = true;
1530 allow_rooted_dmg = true;
1531 }
1532
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
1552 if (allow_rooted_dmg) {
1553 len = strlcpy(dmgpath, dmgbasepath, MAXPATHLEN);
1554 if (len > MAXPATHLEN) {
1555 error = ENAMETOOLONG;
1556 goto done;
1557 }
1558
1559 len = strlcat(dmgpath, "arm64eBaseSystem.rooted.dmg", MAXPATHLEN);
1560 if (len > MAXPATHLEN) {
1561 error = ENAMETOOLONG;
1562 goto done;
1563 }
1564
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);
1572 }
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
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 }
1614
1615 len = strlcat(dmgpath, "/BaseSystem.rooted.dmg", MAXPATHLEN);
1616 if (len > MAXPATHLEN) {
1617 error = ENAMETOOLONG;
1618 goto done;
1619 }
1620
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 }
1628 }
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
1730void
1731bsd_exec_setup(int scale)
1732{
1733 switch (scale) {
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;
1753 }
1754 bsd_pageable_map_size = (bsd_simul_execs * BSD_PAGEABLE_SIZE_PER_EXEC);
1755}
1756
1757#if !CONFIG_NETBOOT
1758int
1759netboot_root(void);
1760
1761int
1762netboot_root(void)
1763{
1764 return 0;
1765}
1766#endif