]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/bsd_init.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / bsd / kern / bsd_init.c
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 2000-2019 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
A
92#include <sys/clist.h>
93#include <sys/user.h>
55e303ae
A
94#include <sys/time.h>
95#include <sys/systm.h>
91447636 96#include <sys/mman.h>
cc8bc92a 97#include <sys/kasl.h>
55e303ae 98
b0d623f7 99#include <security/audit/audit.h>
1c79356b
A
100
101#include <sys/malloc.h>
102#include <sys/dkstat.h>
2d21ac55 103#include <sys/codesign.h>
1c79356b 104
91447636 105#include <kern/startup.h>
1c79356b
A
106#include <kern/thread.h>
107#include <kern/task.h>
108#include <kern/ast.h>
b0d623f7 109#include <kern/kalloc.h>
d9a64523 110#include <kern/ux_handler.h> /* for ux_handler_setup() */
1c79356b
A
111
112#include <mach/vm_param.h>
113
114#include <vm/vm_map.h>
115#include <vm/vm_kern.h>
116
1c79356b 117#include <sys/reboot.h>
0a7de745 118#include <dev/busvar.h> /* for pseudo_inits */
1c79356b 119#include <sys/kdebug.h>
5ba3f43e 120#include <sys/monotonic.h>
39037602 121#include <sys/reason.h>
1c79356b 122
765c9de3
A
123#include <mach/mach_types.h>
124#include <mach/vm_prot.h>
125#include <mach/semaphore.h>
126#include <mach/sync_policy.h>
127#include <kern/clock.h>
128#include <mach/kern_return.h>
0a7de745
A
129#include <mach/thread_act.h> /* for thread_resume() */
130#include <sys/ubc_internal.h> /* for ubc_init() */
131#include <sys/mcache.h> /* for mcache_init() */
132#include <sys/mbuf.h> /* for mbinit() */
133#include <sys/event.h> /* for knote_init() */
134#include <sys/eventhandler.h> /* for eventhandler_init() */
135#include <sys/kern_memorystatus.h> /* for memorystatus_init() */
cb323159 136#include <sys/kern_memorystatus_freeze.h> /* for memorystatus_freeze_init() */
0a7de745
A
137#include <sys/aio_kern.h> /* for aio_init() */
138#include <sys/semaphore.h> /* for psem_cache_init() */
139#include <net/dlil.h> /* for dlil_init() */
140#include <net/kpi_protocol.h> /* for proto_kpi_init() */
141#include <net/iptap.h> /* for iptap_init() */
142#include <sys/pipe.h> /* for pipeinit() */
143#include <sys/socketvar.h> /* for socketinit() */
144#include <sys/protosw.h> /* for domaininit() */
145#include <kern/sched_prim.h> /* for thread_wakeup() */
146#include <net/if_ether.h> /* for ether_family_init() */
147#include <net/if_gif.h> /* for gif_init() */
148#include <vm/vm_protos.h> /* for vnode_pager_bootstrap() */
149#include <miscfs/devfs/devfsdefs.h> /* for devfs_kernel_mount() */
150#include <vm/vm_kern.h> /* for kmem_suballoc() */
151#include <sys/semaphore.h> /* for psem_lock_init() */
152#include <sys/msgbuf.h> /* for log_setsize() */
153#include <sys/tty.h> /* for tty_init() */
154#include <sys/proc_uuid_policy.h> /* proc_uuid_policy_init() */
155#include <netinet/flow_divert.h> /* flow_divert_init() */
156#include <net/content_filter.h> /* for cfil_init() */
157#include <net/necp.h> /* for necp_init() */
158#include <net/network_agent.h> /* for netagent_init() */
159#include <net/packet_mangler.h> /* for pkt_mnglr_init() */
160#include <net/if_utun.h> /* for utun_register_control() */
161#include <net/if_ipsec.h> /* for ipsec_register_control() */
162#include <net/net_str_id.h> /* for net_str_id_init() */
163#include <net/netsrc.h> /* for netsrc_init() */
164#include <net/ntstat.h> /* for nstat_init() */
165#include <netinet/tcp_cc.h> /* for tcp_cc_init() */
166#include <netinet/mptcp_var.h> /* for mptcp_control_register() */
167#include <net/nwk_wq.h> /* for nwk_wq_init */
cb323159 168#include <net/restricted_in_port.h> /* for restricted_in_port_init() */
0a7de745
A
169#include <kern/assert.h> /* for assert() */
170#include <sys/kern_overrides.h> /* for init_system_override() */
94ff46dc 171#include <sys/lockf.h> /* for lf_init() */
9bccf70c 172
91447636
A
173#include <net/init.h>
174
2d21ac55
A
175#if CONFIG_MACF
176#include <security/mac_framework.h>
0a7de745
A
177#include <security/mac_internal.h> /* mac_init_bsd() */
178#include <security/mac_mach_internal.h> /* mac_update_task_label() */
2d21ac55
A
179#endif
180
0c530ab8
A
181#include <machine/exec.h>
182
cb323159 183#if CONFIG_NETBOOT
6d2010ae
A
184#include <sys/netboot.h>
185#endif
186
2d21ac55
A
187#if CONFIG_IMAGEBOOT
188#include <sys/imageboot.h>
189#endif
190
b0d623f7
A
191#if PFLOG
192#include <net/if_pflog.h>
193#endif
194
39037602 195
2d21ac55 196#include <pexpert/pexpert.h>
6d2010ae 197#include <machine/pal_routines.h>
13f56ec4 198#include <console/video_console.h>
2d21ac55 199
d9a64523
A
200#if CONFIG_XNUPOST
201#include <tests/xnupost.h>
202#endif
3e170ce0 203
0a7de745
A
204void * get_user_regs(thread_t); /* XXX kludge for <machine/thread.h> */
205void IOKitInitializeTime(void); /* XXX */
206void IOSleep(unsigned int); /* XXX */
207void loopattach(void); /* XXX */
1c79356b 208
b0d623f7 209const char copyright[] =
0a7de745
A
210 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t"
211 "The Regents of the University of California. "
212 "All rights reserved.\n\n";
1c79356b 213
1c79356b 214/* Components of the first process -- never freed. */
0a7de745
A
215struct proc proc0;
216struct session session0;
217struct pgrp pgrp0;
218struct filedesc filedesc0;
219struct plimit limit0;
220struct pstats pstats0;
221struct sigacts sigacts0;
2d21ac55
A
222proc_t kernproc;
223proc_t initproc;
1c79356b 224
1c79356b
A
225long tk_cancc;
226long tk_nin;
227long tk_nout;
228long tk_rawcc;
229
91447636 230int lock_trace = 0;
1c79356b
A
231/* Global variables to make pstat happy. We do swapping differently */
232int nswdev, nswap;
233int nswapmap;
234void *swapmap;
235struct swdevt swdevt[1];
236
0a7de745
A
237dev_t rootdev; /* device of the root */
238dev_t dumpdev; /* device to take dumps on */
239long dumplo; /* offset into dumpdev */
240long hostid;
241char hostname[MAXHOSTNAMELEN];
cb323159
A
242lck_mtx_t hostname_lock;
243lck_grp_t *hostname_lck_grp;
0a7de745 244char domainname[MAXDOMNAMELEN];
cb323159 245lck_mtx_t domainname_lock;
1c79356b 246
5ba3f43e 247char rootdevice[DEVMAXNAMESIZE];
1c79356b 248
2d21ac55 249#if KMEMSTATS
0a7de745 250struct kmemstats kmemstats[M_LAST];
1c79356b
A
251#endif
252
0a7de745 253struct vnode *rootvp;
cb323159 254int boothowto;
3e170ce0 255int minimalboot = 0;
5ba3f43e
A
256#if CONFIG_EMBEDDED
257int darkboot = 0;
258#endif
3e170ce0 259
cb323159
A
260#if __arm64__
261int legacy_footprint_entitlement_mode = LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE;
262#endif /* __arm64__ */
263
3e170ce0
A
264#if PROC_REF_DEBUG
265__private_extern__ int proc_ref_tracking_disabled = 0; /* disable panics on leaked proc refs across syscall boundary */
266#endif
1c79356b 267
39037602
A
268#if OS_REASON_DEBUG
269__private_extern__ int os_reason_debug_disabled = 0; /* disable asserts for when we fail to allocate OS reasons */
270#endif
271
cf7d32b8 272extern kern_return_t IOFindBSDRoot(char *, unsigned int, dev_t *, u_int32_t *);
2d21ac55
A
273extern void IOSecureBSDRoot(const char * rootName);
274extern kern_return_t IOKitBSDInit(void );
275extern void kminit(void);
2d21ac55
A
276extern void file_lock_init(void);
277extern void kmeminit(void);
278extern void bsd_bufferinit(void);
39037602 279extern void oslog_setsize(int size);
316670eb 280extern void throttle_init(void);
fe8ab488 281extern void acct_init(void);
2d21ac55 282
cb323159
A
283#if CONFIG_LOCKERBOOT
284#define LOCKER_PROTOBOOT_MOUNT "/protoboot"
285
286const char kernel_protoboot_mount[] = LOCKER_PROTOBOOT_MOUNT;
287extern int mount_locker_protoboot(const char *fsname, const char *mntpoint,
288 const char *pbdevpath);
289#endif
290
6d2010ae 291extern int serverperfmode;
2d21ac55 292extern int ncl;
cb323159
A
293#if DEVELOPMENT || DEBUG
294extern int syscallfilter_disable;
295#endif // DEVELOPMENT || DEBUG
2d21ac55 296
0a7de745
A
297vm_map_t bsd_pageable_map;
298vm_map_t mb_map;
b0d623f7 299
6d2010ae
A
300static int bsd_simul_execs;
301static int bsd_pageable_map_size;
302__private_extern__ int execargs_cache_size = 0;
303__private_extern__ int execargs_free_count = 0;
b0d623f7
A
304__private_extern__ vm_offset_t * execargs_cache = NULL;
305
39236c6e 306void bsd_exec_setup(int);
b0d623f7 307
cb323159
A
308__private_extern__ int bootarg_execfailurereports = 0;
309
a39ff7e2 310#if __x86_64__
cb323159 311__private_extern__ int bootarg_no32exec = 1;
a39ff7e2 312#endif
6d2010ae
A
313__private_extern__ int bootarg_vnode_cache_defeat = 0;
314
3e170ce0
A
315#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
316__private_extern__ int bootarg_no_vnode_jetsam = 0;
317#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
318
94ff46dc
A
319__private_extern__ int bootarg_no_vnode_drain = 0;
320
6d2010ae
A
321/*
322 * Prevent kernel-based ASLR from being used, for testing.
323 */
324#if DEVELOPMENT || DEBUG
325__private_extern__ int bootarg_disable_aslr = 0;
326#endif
1c79356b 327
39037602
A
328/*
329 * Allow an alternate dyld to be used for testing.
330 */
331
332#if DEVELOPMENT || DEBUG
333char dyld_alt_path[MAXPATHLEN];
334int use_alt_dyld = 0;
cb323159 335extern uint64_t dyld_flags;
39037602
A
336#endif
337
0a7de745 338int cmask = CMASK;
0c530ab8 339extern int customnbuf;
1c79356b 340
39236c6e
A
341kern_return_t bsd_autoconf(void);
342void bsd_utaskbootstrap(void);
2d21ac55
A
343
344static void parse_bsd_args(void);
fe8ab488
A
345#if CONFIG_DEV_KMEM
346extern void dev_kmem_init(void);
347#endif
9bccf70c 348extern void time_zone_slock_init(void);
3e170ce0 349extern void select_waitq_init(void);
2d21ac55 350static void process_name(const char *, proc_t);
91447636
A
351
352static void setconf(void);
1c79356b 353
2d21ac55 354#if SYSV_SHM
91447636 355extern void sysv_shm_lock_init(void);
2d21ac55
A
356#endif
357#if SYSV_SEM
91447636 358extern void sysv_sem_lock_init(void);
2d21ac55
A
359#endif
360#if SYSV_MSG
91447636 361extern void sysv_msg_lock_init(void);
2d21ac55 362#endif
0c530ab8 363
39037602
A
364extern void ulock_initialize(void);
365
6d2010ae
A
366#if CONFIG_MACF
367#if defined (__i386__) || defined (__x86_64__)
368/* MACF policy_check configuration flags; see policy_check.c for details */
369int policy_check_flags = 0;
2d21ac55 370
6d2010ae
A
371extern int check_policy_init(int);
372#endif
0a7de745 373#endif /* CONFIG_MACF */
2d21ac55 374
b0d623f7
A
375/* If we are using CONFIG_DTRACE */
376#if CONFIG_DTRACE
0a7de745 377extern void dtrace_postinit(void);
b0d623f7
A
378#endif
379
1c79356b
A
380/*
381 * Initialization code.
382 * Called from cold start routine as
383 * soon as a stack and segmentation
384 * have been established.
385 * Functions:
1c79356b
A
386 * turn on clock
387 * hand craft 0th process
388 * call all initialization routines
55e303ae 389 * hand craft 1st user process
1c79356b
A
390 */
391
392/*
393 * Sets the name for the given task.
394 */
91447636 395static void
2d21ac55 396process_name(const char *s, proc_t p)
1c79356b 397{
0a7de745
A
398 strlcpy(p->p_comm, s, sizeof(p->p_comm));
399 strlcpy(p->p_name, s, sizeof(p->p_name));
1c79356b
A
400}
401
1c79356b
A
402/* To allow these values to be patched, they're globals here */
403#include <machine/vmparam.h>
cb323159
A
404struct rlimit vm_initial_limit_stack = { .rlim_cur = DFLSSIZ, .rlim_max = MAXSSIZ - PAGE_MAX_SIZE };
405struct rlimit vm_initial_limit_data = { .rlim_cur = DFLDSIZ, .rlim_max = MAXDSIZ };
406struct rlimit vm_initial_limit_core = { .rlim_cur = DFLCSIZ, .rlim_max = MAXCSIZ };
1c79356b 407
0a7de745
A
408extern thread_t cloneproc(task_t, coalition_t, proc_t, int, int);
409extern int (*mountroot)(void);
1c79356b 410
91447636 411lck_grp_t * proc_lck_grp;
b0d623f7
A
412lck_grp_t * proc_slock_grp;
413lck_grp_t * proc_fdmlock_grp;
5ba3f43e
A
414lck_grp_t * proc_kqhashlock_grp;
415lck_grp_t * proc_knhashlock_grp;
4bd07ac2 416lck_grp_t * proc_ucred_mlock_grp;
b0d623f7 417lck_grp_t * proc_mlock_grp;
91447636
A
418lck_grp_attr_t * proc_lck_grp_attr;
419lck_attr_t * proc_lck_attr;
2d21ac55
A
420lck_mtx_t * proc_list_mlock;
421lck_mtx_t * proc_klist_mlock;
91447636 422
d9a64523
A
423#if CONFIG_XNUPOST
424lck_grp_t * sysctl_debug_test_stackshot_owner_grp;
425lck_mtx_t * sysctl_debug_test_stackshot_owner_init_mtx;
426#endif /* !CONFIG_XNUPOST */
813fb2f6 427
b0d623f7
A
428extern lck_mtx_t * execargs_cache_lock;
429
9bccf70c
A
430/* hook called after root is mounted XXX temporary hack */
431void (*mountroot_post_hook)(void);
b0d623f7 432void (*unmountroot_pre_hook)(void);
1c79356b 433
39037602
A
434/*
435 * This function is called before IOKit initialization, so that globals
436 * like the sysctl tree are initialized before kernel extensions
437 * are started (since they may want to register sysctls
438 */
439void
440bsd_early_init(void)
441{
442 sysctl_early_init();
443}
444
91447636
A
445/*
446 * This function is called very early on in the Mach startup, from the
447 * function start_kernel_threads() in osfmk/kern/startup.c. It's called
448 * in the context of the current (startup) task using a call to the
449 * function kernel_thread_create() to jump into start_kernel_threads().
450 * Internally, kernel_thread_create() calls thread_create_internal(),
451 * which calls uthread_alloc(). The function of uthread_alloc() is
452 * normally to allocate a uthread structure, and fill out the uu_sigmask,
2d21ac55
A
453 * uu_context fields. It skips filling these out in the case of the "task"
454 * being "kernel_task", because the order of operation is inverted. To
455 * account for that, we need to manually fill in at least the contents
456 * of the uu_context.vc_ucred field so that the uthread structure can be
457 * used like any other.
91447636 458 */
316670eb 459
1c79356b 460void
2d21ac55 461bsd_init(void)
1c79356b 462{
91447636 463 struct uthread *ut;
2d21ac55 464 unsigned int i;
91447636 465 struct vfs_context context;
0a7de745 466 kern_return_t ret;
91447636 467 struct ucred temp_cred;
6d2010ae 468 struct posix_cred temp_pcred;
cb323159 469#if CONFIG_NETBOOT || CONFIG_IMAGEBOOT
6d2010ae
A
470 boolean_t netboot = FALSE;
471#endif
cb323159
A
472#if CONFIG_LOCKERBOOT
473 vnode_t pbvn = NULLVP;
474 mount_t pbmnt = NULL;
475 char *pbdevp = NULL;
476 char pbdevpath[64];
477 char pbfsname[MFSNAMELEN];
478 char *slash_dev = NULL;
479#endif
2d21ac55 480
cb323159
A
481#define DEBUG_BSDINIT 0
482
483#if DEBUG_BSDINIT
484#define bsd_init_kprintf(x, ...) kprintf("bsd_init: " x, ## __VA_ARGS__)
485#else
486#define bsd_init_kprintf(x, ...)
487#endif
1c79356b 488
316670eb
A
489 throttle_init();
490
1c79356b 491 printf(copyright);
0a7de745 492
2d21ac55 493 bsd_init_kprintf("calling kmeminit\n");
1c79356b 494 kmeminit();
0a7de745 495
2d21ac55 496 bsd_init_kprintf("calling parse_bsd_args\n");
1c79356b
A
497 parse_bsd_args();
498
fe8ab488
A
499#if CONFIG_DEV_KMEM
500 bsd_init_kprintf("calling dev_kmem_init\n");
501 dev_kmem_init();
502#endif
503
91447636 504 /* Initialize kauth subsystem before instancing the first credential */
2d21ac55 505 bsd_init_kprintf("calling kauth_init\n");
91447636
A
506 kauth_init();
507
508 /* Initialize process and pgrp structures. */
2d21ac55 509 bsd_init_kprintf("calling procinit\n");
1c79356b
A
510 procinit();
511
b0d623f7
A
512 /* Initialize the ttys (MUST be before kminit()/bsd_autoconf()!)*/
513 tty_init();
1c79356b 514
0a7de745 515 kernproc = &proc0; /* implicitly bzero'ed */
1c79356b
A
516
517 /* kernel_task->proc = kernproc; */
0a7de745 518 set_bsdtask_info(kernel_task, (void *)kernproc);
1c79356b
A
519
520 /* give kernproc a name */
2d21ac55 521 bsd_init_kprintf("calling process_name\n");
b0d623f7 522 process_name("kernel_task", kernproc);
91447636 523
91447636 524 /* allocate proc lock group attribute and group */
2d21ac55 525 bsd_init_kprintf("calling lck_grp_attr_alloc_init\n");
0a7de745 526 proc_lck_grp_attr = lck_grp_attr_alloc_init();
91447636 527
0a7de745 528 proc_lck_grp = lck_grp_alloc_init("proc", proc_lck_grp_attr);
5ba3f43e 529
0a7de745
A
530 proc_slock_grp = lck_grp_alloc_init("proc-slock", proc_lck_grp_attr);
531 proc_ucred_mlock_grp = lck_grp_alloc_init("proc-ucred-mlock", proc_lck_grp_attr);
532 proc_mlock_grp = lck_grp_alloc_init("proc-mlock", proc_lck_grp_attr);
533 proc_fdmlock_grp = lck_grp_alloc_init("proc-fdmlock", proc_lck_grp_attr);
534 proc_kqhashlock_grp = lck_grp_alloc_init("proc-kqhashlock", proc_lck_grp_attr);
535 proc_knhashlock_grp = lck_grp_alloc_init("proc-knhashlock", proc_lck_grp_attr);
d9a64523
A
536#if CONFIG_XNUPOST
537 sysctl_debug_test_stackshot_owner_grp = lck_grp_alloc_init("test-stackshot-owner-grp", LCK_GRP_ATTR_NULL);
538 sysctl_debug_test_stackshot_owner_init_mtx = lck_mtx_alloc_init(
0a7de745
A
539 sysctl_debug_test_stackshot_owner_grp,
540 LCK_ATTR_NULL);
d9a64523 541#endif /* !CONFIG_XNUPOST */
91447636
A
542 /* Allocate proc lock attribute */
543 proc_lck_attr = lck_attr_alloc_init();
91447636 544
b0d623f7
A
545 proc_list_mlock = lck_mtx_alloc_init(proc_mlock_grp, proc_lck_attr);
546 proc_klist_mlock = lck_mtx_alloc_init(proc_mlock_grp, proc_lck_attr);
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);
4bd07ac2 549 lck_mtx_init(&kernproc->p_ucred_mlock, proc_ucred_mlock_grp, proc_lck_attr);
b0d623f7 550 lck_spin_init(&kernproc->p_slock, proc_slock_grp, proc_lck_attr);
1c79356b 551
6d2010ae 552 assert(bsd_simul_execs != 0);
b0d623f7
A
553 execargs_cache_lock = lck_mtx_alloc_init(proc_lck_grp, proc_lck_attr);
554 execargs_cache_size = bsd_simul_execs;
555 execargs_free_count = bsd_simul_execs;
556 execargs_cache = (vm_offset_t *)kalloc(bsd_simul_execs * sizeof(vm_offset_t));
557 bzero(execargs_cache, bsd_simul_execs * sizeof(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
cb323159
A
584 hostname_lck_grp = lck_grp_alloc_init("hostname", LCK_GRP_ATTR_NULL);
585 lck_mtx_init(&hostname_lock, hostname_lck_grp, LCK_ATTR_NULL);
586 lck_mtx_init(&domainname_lock, hostname_lck_grp, LCK_ATTR_NULL);
587
1c79356b
A
588 /*
589 * Create process 0.
590 */
2d21ac55 591 proc_list_lock();
b0d623f7
A
592 LIST_INSERT_HEAD(&allproc, kernproc, p_list);
593 kernproc->p_pgrp = &pgrp0;
1c79356b
A
594 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
595 LIST_INIT(&pgrp0.pg_members);
b0d623f7 596 lck_mtx_init(&pgrp0.pg_mlock, proc_mlock_grp, proc_lck_attr);
2d21ac55 597 /* There is no other bsd thread this point and is safe without pgrp lock */
b0d623f7
A
598 LIST_INSERT_HEAD(&pgrp0.pg_members, kernproc, p_pglist);
599 kernproc->p_listflag |= P_LIST_INPGRP;
600 kernproc->p_pgrpid = 0;
6d2010ae 601 kernproc->p_uniqueid = 0;
1c79356b
A
602
603 pgrp0.pg_session = &session0;
2d21ac55
A
604 pgrp0.pg_membercnt = 1;
605
1c79356b 606 session0.s_count = 1;
b0d623f7 607 session0.s_leader = kernproc;
2d21ac55 608 session0.s_listflags = 0;
b0d623f7 609 lck_mtx_init(&session0.s_mlock, proc_mlock_grp, proc_lck_attr);
2d21ac55
A
610 LIST_INSERT_HEAD(SESSHASH(0), &session0, s_hash);
611 proc_list_unlock();
612
490019cf
A
613#if CONFIG_PERSONAS
614 kernproc->p_persona = NULL;
615#endif
616
b0d623f7 617 kernproc->task = kernel_task;
0a7de745 618
b0d623f7
A
619 kernproc->p_stat = SRUN;
620 kernproc->p_flag = P_SYSTEM;
6d2010ae
A
621 kernproc->p_lflag = 0;
622 kernproc->p_ladvflag = 0;
39037602
A
623
624#if defined(__LP64__)
625 kernproc->p_flag |= P_LP64;
626#endif
627
6d2010ae 628#if DEVELOPMENT || DEBUG
0a7de745 629 if (bootarg_disable_aslr) {
6d2010ae 630 kernproc->p_flag |= P_DISABLE_ASLR;
0a7de745 631 }
6d2010ae
A
632#endif
633
b0d623f7
A
634 kernproc->p_nice = NZERO;
635 kernproc->p_pptr = kernproc;
2d21ac55 636
b0d623f7
A
637 TAILQ_INIT(&kernproc->p_uthlist);
638 TAILQ_INSERT_TAIL(&kernproc->p_uthlist, ut, uu_list);
0a7de745 639
b0d623f7
A
640 kernproc->sigwait = FALSE;
641 kernproc->sigwait_thread = THREAD_NULL;
642 kernproc->exit_thread = THREAD_NULL;
643 kernproc->p_csflags = CS_VALID;
1c79356b 644
91447636
A
645 /*
646 * Create credential. This also Initializes the audit information.
91447636 647 */
2d21ac55 648 bsd_init_kprintf("calling bzero\n");
91447636 649 bzero(&temp_cred, sizeof(temp_cred));
6d2010ae
A
650 bzero(&temp_pcred, sizeof(temp_pcred));
651 temp_pcred.cr_ngroups = 1;
39236c6e
A
652 /* kern_proc, shouldn't call up to DS for group membership */
653 temp_pcred.cr_flags = CRF_NOMEMBERD;
6d2010ae 654 temp_cred.cr_audit.as_aia_p = audit_default_aia_p;
0a7de745 655
2d21ac55 656 bsd_init_kprintf("calling kauth_cred_create\n");
6d2010ae
A
657 /*
658 * We have to label the temp cred before we create from it to
659 * properly set cr_ngroups, or the create will fail.
660 */
661 posix_cred_label(&temp_cred, &temp_pcred);
0a7de745 662 kernproc->p_ucred = kauth_cred_create(&temp_cred);
91447636 663
6d2010ae
A
664 /* update cred on proc */
665 PROC_UPDATE_CREDS_ONPROC(kernproc);
666
91447636 667 /* give the (already exisiting) initial thread a reference on it */
2d21ac55 668 bsd_init_kprintf("calling kauth_cred_ref\n");
b0d623f7
A
669 kauth_cred_ref(kernproc->p_ucred);
670 ut->uu_context.vc_ucred = kernproc->p_ucred;
2d21ac55
A
671 ut->uu_context.vc_thread = current_thread();
672
b0d623f7
A
673 TAILQ_INIT(&kernproc->p_aio_activeq);
674 TAILQ_INIT(&kernproc->p_aio_doneq);
675 kernproc->p_aio_total_count = 0;
676 kernproc->p_aio_active_count = 0;
55e303ae 677
2d21ac55 678 bsd_init_kprintf("calling file_lock_init\n");
91447636 679 file_lock_init();
1c79356b 680
2d21ac55 681#if CONFIG_MACF
b0d623f7 682 mac_cred_label_associate_kernel(kernproc->p_ucred);
2d21ac55
A
683#endif
684
1c79356b 685 /* Create the file descriptor table. */
b0d623f7 686 kernproc->p_fd = &filedesc0;
1c79356b 687 filedesc0.fd_cmask = cmask;
cb323159 688 filedesc0.fd_knlistsize = 0;
55e303ae
A
689 filedesc0.fd_knlist = NULL;
690 filedesc0.fd_knhash = NULL;
691 filedesc0.fd_knhashmask = 0;
5ba3f43e
A
692 lck_mtx_init(&filedesc0.fd_kqhashlock, proc_kqhashlock_grp, proc_lck_attr);
693 lck_mtx_init(&filedesc0.fd_knhashlock, proc_knhashlock_grp, proc_lck_attr);
1c79356b
A
694
695 /* Create the limits structures. */
b0d623f7 696 kernproc->p_limit = &limit0;
0a7de745
A
697 for (i = 0; i < sizeof(kernproc->p_rlimit) / sizeof(kernproc->p_rlimit[0]); i++) {
698 limit0.pl_rlimit[i].rlim_cur =
699 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
700 }
1c79356b 701 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
0c530ab8 702 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = maxprocperuid;
55e303ae 703 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
1c79356b
A
704 limit0.pl_rlimit[RLIMIT_STACK] = vm_initial_limit_stack;
705 limit0.pl_rlimit[RLIMIT_DATA] = vm_initial_limit_data;
706 limit0.pl_rlimit[RLIMIT_CORE] = vm_initial_limit_core;
2d21ac55 707 limit0.pl_refcnt = 1;
1c79356b 708
b0d623f7
A
709 kernproc->p_stats = &pstats0;
710 kernproc->p_sigacts = &sigacts0;
1c79356b
A
711
712 /*
fe8ab488 713 * Charge root for one process: launchd.
1c79356b 714 */
2d21ac55 715 bsd_init_kprintf("calling chgproccnt\n");
1c79356b
A
716 (void)chgproccnt(0, 1);
717
1c79356b
A
718 /*
719 * Allocate a kernel submap for pageable memory
765c9de3 720 * for temporary copying (execve()).
1c79356b
A
721 */
722 {
0a7de745 723 vm_offset_t minimum;
1c79356b 724
2d21ac55 725 bsd_init_kprintf("calling kmem_suballoc\n");
6d2010ae 726 assert(bsd_pageable_map_size != 0);
1c79356b 727 ret = kmem_suballoc(kernel_map,
0a7de745
A
728 &minimum,
729 (vm_size_t)bsd_pageable_map_size,
730 TRUE,
731 VM_FLAGS_ANYWHERE,
732 VM_MAP_KERNEL_FLAGS_NONE,
733 VM_KERN_MEMORY_BSD,
734 &bsd_pageable_map);
735 if (ret != KERN_SUCCESS) {
9bccf70c 736 panic("bsd_init: Failed to allocate bsd pageable map");
0a7de745 737 }
765c9de3
A
738 }
739
cc8bc92a
A
740 bsd_init_kprintf("calling fpxlog_init\n");
741 fpxlog_init();
742
91447636
A
743 /*
744 * Initialize buffers and hash links for buffers
745 *
746 * SIDE EFFECT: Starts a thread for bcleanbuf_thread(), so must
747 * happen after a credential has been associated with
748 * the kernel task.
749 */
2d21ac55 750 bsd_init_kprintf("calling bsd_bufferinit\n");
91447636
A
751 bsd_bufferinit();
752
1c79356b 753 /*
0b4e3aa0
A
754 * Initialize the calendar.
755 */
4a3eedf9 756 bsd_init_kprintf("calling IOKitInitializeTime\n");
2d21ac55
A
757 IOKitInitializeTime();
758
2d21ac55 759 bsd_init_kprintf("calling ubc_init\n");
1c79356b
A
760 ubc_init();
761
762 /* Initialize the file systems. */
2d21ac55 763 bsd_init_kprintf("calling vfsinit\n");
1c79356b
A
764 vfsinit();
765
94ff46dc
A
766 /* Initialize file locks. */
767 bsd_init_kprintf("calling lf_init\n");
768 lf_init();
769
39236c6e
A
770#if CONFIG_PROC_UUID_POLICY
771 /* Initial proc_uuid_policy subsystem */
772 bsd_init_kprintf("calling proc_uuid_policy_init()\n");
773 proc_uuid_policy_init();
774#endif
775
2d21ac55
A
776#if SOCKETS
777 /* Initialize per-CPU cache allocator */
778 mcache_init();
779
1c79356b 780 /* Initialize mbuf's. */
2d21ac55 781 bsd_init_kprintf("calling mbinit\n");
1c79356b 782 mbinit();
b0d623f7 783 net_str_id_init(); /* for mbuf tags */
cb323159 784 restricted_in_port_init();
2d21ac55 785#endif /* SOCKETS */
1c79356b 786
55e303ae
A
787 /*
788 * Initializes security event auditing.
789 * XXX: Should/could this occur later?
790 */
b0d623f7 791#if CONFIG_AUDIT
2d21ac55 792 bsd_init_kprintf("calling audit_init\n");
0a7de745 793 audit_init();
2d21ac55 794#endif
55e303ae
A
795
796 /* Initialize kqueues */
2d21ac55 797 bsd_init_kprintf("calling knote_init\n");
55e303ae
A
798 knote_init();
799
5ba3f43e
A
800 /* Initialize event handler */
801 bsd_init_kprintf("calling eventhandler_init\n");
802 eventhandler_init();
803
55e303ae 804 /* Initialize for async IO */
2d21ac55 805 bsd_init_kprintf("calling aio_init\n");
55e303ae
A
806 aio_init();
807
91447636 808 /* Initialize pipes */
2d21ac55 809 bsd_init_kprintf("calling pipeinit\n");
91447636
A
810 pipeinit();
811
812 /* Initialize SysV shm subsystem locks; the subsystem proper is
813 * initialized through a sysctl.
814 */
2d21ac55
A
815#if SYSV_SHM
816 bsd_init_kprintf("calling sysv_shm_lock_init\n");
91447636 817 sysv_shm_lock_init();
2d21ac55
A
818#endif
819#if SYSV_SEM
820 bsd_init_kprintf("calling sysv_sem_lock_init\n");
91447636 821 sysv_sem_lock_init();
2d21ac55
A
822#endif
823#if SYSV_MSG
824 bsd_init_kprintf("sysv_msg_lock_init\n");
91447636 825 sysv_msg_lock_init();
2d21ac55
A
826#endif
827 bsd_init_kprintf("calling pshm_lock_init\n");
91447636 828 pshm_lock_init();
2d21ac55 829 bsd_init_kprintf("calling psem_lock_init\n");
91447636
A
830 psem_lock_init();
831
2d21ac55 832 pthread_init();
9bccf70c 833 /* POSIX Shm and Sem */
2d21ac55 834 bsd_init_kprintf("calling pshm_cache_init\n");
9bccf70c 835 pshm_cache_init();
2d21ac55 836 bsd_init_kprintf("calling psem_cache_init\n");
9bccf70c 837 psem_cache_init();
2d21ac55 838 bsd_init_kprintf("calling time_zone_slock_init\n");
9bccf70c 839 time_zone_slock_init();
3e170ce0
A
840 bsd_init_kprintf("calling select_waitq_init\n");
841 select_waitq_init();
1c79356b 842
1c79356b
A
843 /*
844 * Initialize protocols. Block reception of incoming packets
845 * until everything is ready.
846 */
2d21ac55 847#if NETWORKING
5ba3f43e
A
848 bsd_init_kprintf("calling nwk_wq_init\n");
849 nwk_wq_init();
2d21ac55 850 bsd_init_kprintf("calling dlil_init\n");
1c79356b 851 dlil_init();
2d21ac55 852 bsd_init_kprintf("calling proto_kpi_init\n");
91447636 853 proto_kpi_init();
2d21ac55
A
854#endif /* NETWORKING */
855#if SOCKETS
856 bsd_init_kprintf("calling socketinit\n");
1c79356b 857 socketinit();
2d21ac55 858 bsd_init_kprintf("calling domaininit\n");
1c79356b 859 domaininit();
316670eb 860 iptap_init();
39236c6e
A
861#if FLOW_DIVERT
862 flow_divert_init();
0a7de745 863#endif /* FLOW_DIVERT */
2d21ac55 864#endif /* SOCKETS */
b0d623f7
A
865 kernproc->p_fd->fd_cdir = NULL;
866 kernproc->p_fd->fd_rdir = NULL;
1c79356b 867
6d2010ae 868#if CONFIG_FREEZE
316670eb
A
869#ifndef CONFIG_MEMORYSTATUS
870 #error "CONFIG_FREEZE defined without matching CONFIG_MEMORYSTATUS"
871#endif
872 /* Initialise background freezing */
873 bsd_init_kprintf("calling memorystatus_freeze_init\n");
874 memorystatus_freeze_init();
6d2010ae
A
875#endif
876
316670eb 877#if CONFIG_MEMORYSTATUS
d1ecb069 878 /* Initialize kernel memory status notifications */
316670eb
A
879 bsd_init_kprintf("calling memorystatus_init\n");
880 memorystatus_init();
881#endif /* CONFIG_MEMORYSTATUS */
d1ecb069 882
fe8ab488
A
883 bsd_init_kprintf("calling acct_init\n");
884 acct_init();
885
39037602 886 bsd_init_kprintf("calling sysctl_mib_init\n");
cb323159 887 sysctl_mib_init();
39037602 888
2d21ac55 889 bsd_init_kprintf("calling bsd_autoconf\n");
1c79356b
A
890 bsd_autoconf();
891
39037602
A
892 bsd_init_kprintf("calling os_reason_init\n");
893 os_reason_init();
894
2d21ac55 895#if CONFIG_DTRACE
2d21ac55
A
896 dtrace_postinit();
897#endif
898
1c79356b
A
899 /*
900 * We attach the loopback interface *way* down here to ensure
901 * it happens after autoconf(), otherwise it becomes the
902 * "primary" interface.
903 */
904#include <loop.h>
905#if NLOOP > 0
2d21ac55 906 bsd_init_kprintf("calling loopattach\n");
0a7de745 907 loopattach(); /* XXX */
1c79356b 908#endif
39236c6e
A
909#if NGIF
910 /* Initialize gif interface (after lo0) */
911 gif_init();
912#endif
b0d623f7
A
913
914#if PFLOG
915 /* Initialize packet filter log interface */
916 pfloginit();
917#endif /* PFLOG */
918
2d21ac55
A
919#if NETHER > 0
920 /* Register the built-in dlil ethernet interface family */
921 bsd_init_kprintf("calling ether_family_init\n");
9bccf70c 922 ether_family_init();
2d21ac55 923#endif /* ETHER */
1c79356b 924
2d21ac55 925#if NETWORKING
91447636 926 /* Call any kext code that wants to run just after network init */
2d21ac55 927 bsd_init_kprintf("calling net_init_run\n");
91447636 928 net_init_run();
0a7de745 929
fe8ab488
A
930#if CONTENT_FILTER
931 cfil_init();
932#endif
933
934#if PACKET_MANGLER
935 pkt_mnglr_init();
0a7de745 936#endif
fe8ab488
A
937
938#if NECP
939 /* Initialize Network Extension Control Policies */
940 necp_init();
941#endif
3e170ce0
A
942
943 netagent_init();
944
b0d623f7
A
945 /* register user tunnel kernel control handler */
946 utun_register_control();
39236c6e
A
947#if IPSEC
948 ipsec_register_control();
949#endif /* IPSEC */
316670eb
A
950 netsrc_init();
951 nstat_init();
fe8ab488 952 tcp_cc_init();
3e170ce0
A
953#if MPTCP
954 mptcp_control_register();
955#endif /* MPTCP */
2d21ac55 956#endif /* NETWORKING */
91447636 957
2d21ac55 958 bsd_init_kprintf("calling vnode_pager_bootstrap\n");
1c79356b 959 vnode_pager_bootstrap();
91447636 960
2d21ac55 961 bsd_init_kprintf("calling inittodr\n");
91447636 962 inittodr(0);
1c79356b
A
963
964 /* Mount the root file system. */
0a7de745 965 while (TRUE) {
1c79356b
A
966 int err;
967
2d21ac55 968 bsd_init_kprintf("calling setconf\n");
1c79356b 969 setconf();
cb323159 970#if CONFIG_NETBOOT
6d2010ae
A
971 netboot = (mountroot == netboot_mountroot);
972#endif
9bccf70c 973
2d21ac55 974 bsd_init_kprintf("vfs_mountroot\n");
0a7de745 975 if (0 == (err = vfs_mountroot())) {
1c79356b 976 break;
0a7de745 977 }
2d21ac55 978 rootdevice[0] = '\0';
cb323159 979#if CONFIG_NETBOOT
6d2010ae 980 if (netboot) {
b0d623f7
A
981 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
982 vc_progress_set(FALSE, 0);
0a7de745 983 for (i = 1; 1; i *= 2) {
b0d623f7 984 printf("bsd_init: failed to mount network root, error %d, %s\n",
0a7de745 985 err, PE_boot_args());
b0d623f7 986 printf("We are hanging here...\n");
0a7de745 987 IOSleep(i * 60 * 1000);
b0d623f7
A
988 }
989 /*NOTREACHED*/
9bccf70c 990 }
91447636 991#endif
1c79356b
A
992 printf("cannot mount root, errno = %d\n", err);
993 boothowto |= RB_ASKNAME;
994 }
995
2d21ac55
A
996 IOSecureBSDRoot(rootdevice);
997
998 context.vc_thread = current_thread();
b0d623f7 999 context.vc_ucred = kernproc->p_ucred;
91447636 1000 mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
1c79356b 1001
2d21ac55 1002 bsd_init_kprintf("calling VFS_ROOT\n");
1c79356b 1003 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
0a7de745 1004 if (VFS_ROOT(mountlist.tqh_first, &rootvnode, &context)) {
2d21ac55 1005 panic("bsd_init: cannot find root vnode: %s", PE_boot_args());
0a7de745 1006 }
91447636
A
1007 rootvnode->v_flag |= VROOT;
1008 (void)vnode_ref(rootvnode);
1009 (void)vnode_put(rootvnode);
fa4905b1 1010 filedesc0.fd_cdir = rootvnode;
9bccf70c 1011
cb323159 1012#if CONFIG_NETBOOT
6d2010ae 1013 if (netboot) {
9bccf70c 1014 int err;
6d2010ae
A
1015
1016 netboot = TRUE;
9bccf70c 1017 /* post mount setup */
2d21ac55 1018 if ((err = netboot_setup()) != 0) {
b0d623f7
A
1019 PE_display_icon( 0, "noroot"); /* XXX a netboot-specific icon would be nicer */
1020 vc_progress_set(FALSE, 0);
0a7de745 1021 for (i = 1; 1; i *= 2) {
b0d623f7 1022 printf("bsd_init: NetBoot could not find root, error %d: %s\n",
0a7de745 1023 err, PE_boot_args());
b0d623f7 1024 printf("We are hanging here...\n");
0a7de745 1025 IOSleep(i * 60 * 1000);
b0d623f7
A
1026 }
1027 /*NOTREACHED*/
9bccf70c
A
1028 }
1029 }
91447636 1030#endif
0a7de745 1031
1c79356b 1032
2d21ac55 1033#if CONFIG_IMAGEBOOT
cb323159
A
1034#if CONFIG_LOCKERBOOT
1035 /*
1036 * Stash the protoboot vnode, mount, filesystem name, and device name for
1037 * later use. Note that the mount-from name may not have the "/dev/"
1038 * component, so we must sniff out this condition and add it as needed.
1039 */
1040 pbvn = rootvnode;
1041 pbmnt = pbvn->v_mount;
1042 pbdevp = vfs_statfs(pbmnt)->f_mntfromname;
1043 slash_dev = strnstr(pbdevp, "/dev/", strlen(pbdevp));
1044 if (slash_dev) {
1045 /*
1046 * If the old root is a snapshot mount, it will have the form:
1047 *
1048 * com.apple.os.update-<boot manifest hash>@<dev node path>
1049 *
1050 * So we just search the mntfromname for any occurrence of "/dev/" and
1051 * grab that as the device path. The image boot code needs a dev node to
1052 * do the re-mount, so we cannot directly mount the snapshot as the
1053 * protoboot volume currently.
1054 */
1055 strlcpy(pbdevpath, slash_dev, sizeof(pbdevpath));
1056 } else {
1057 snprintf(pbdevpath, sizeof(pbdevpath), "/dev/%s", pbdevp);
1058 }
1059
1060 bsd_init_kprintf("protoboot mount-from: %s\n", pbdevp);
1061 bsd_init_kprintf("protoboot dev path: %s\n", pbdevpath);
1062
1063 strlcpy(pbfsname, pbmnt->mnt_vtable->vfc_name, sizeof(pbfsname));
1064#endif
2d21ac55
A
1065 /*
1066 * See if a system disk image is present. If so, mount it and
1067 * switch the root vnode to point to it
0a7de745 1068 */
cb323159
A
1069 imageboot_type_t imageboot_type = imageboot_needed();
1070 if (netboot == FALSE && imageboot_type) {
0a7de745 1071 /*
6d2010ae
A
1072 * An image was found. No turning back: we're booted
1073 * with a kernel from the disk image.
1074 */
cb323159
A
1075 bsd_init_kprintf("doing image boot: type = %d\n", imageboot_type);
1076 imageboot_setup(imageboot_type);
2d21ac55 1077 }
cb323159
A
1078
1079#if CONFIG_LOCKERBOOT
1080 if (imageboot_type == IMAGEBOOT_LOCKER) {
1081 bsd_init_kprintf("booting from locker\n");
1082 if (vnode_tag(rootvnode) != VT_LOCKERFS) {
1083 panic("root filesystem not a locker: fsname = %s",
1084 rootvnode->v_mount->mnt_vtable->vfc_name);
1085 }
1086 }
1087#endif /* CONFIG_LOCKERBOOT */
2d21ac55 1088#endif /* CONFIG_IMAGEBOOT */
0a7de745 1089
b0d623f7 1090 /* set initial time; all other resource data is already zero'ed */
39236c6e 1091 microtime_with_abstime(&kernproc->p_start, &kernproc->p_stats->ps_start);
1c79356b 1092
9bccf70c 1093#if DEVFS
1c79356b 1094 {
0a7de745 1095 char mounthere[] = "/dev"; /* !const because of internal casting */
2d21ac55 1096
0a7de745
A
1097 bsd_init_kprintf("calling devfs_kernel_mount\n");
1098 devfs_kernel_mount(mounthere);
1c79356b 1099 }
55e303ae 1100#endif /* DEVFS */
3e170ce0 1101
cb323159
A
1102 if (vfs_mount_rosv_data()) {
1103 panic("failed to mount data volume!");
1104 }
1105
1106 if (vfs_mount_vm()) {
1107 printf("failed to mount vm volume!");
1108 }
1109
1110#if CONFIG_LOCKERBOOT
1111 /*
1112 * We need to wait until devfs is up before remounting the protoboot volume
1113 * within the locker so that it can have a real devfs vnode backing it.
1114 */
1115 if (imageboot_type == IMAGEBOOT_LOCKER) {
1116 bsd_init_kprintf("re-mounting protoboot volume\n");
1117 int error = mount_locker_protoboot(pbfsname, LOCKER_PROTOBOOT_MOUNT,
1118 pbdevpath);
1119 if (error) {
1120 panic("failed to mount protoboot volume: dev path = %s, error = %d",
1121 pbdevpath, error);
1122 }
1123 }
1124#endif /* CONFIG_LOCKERBOOT */
1125
1c79356b 1126 /* Initialize signal state for process 0. */
2d21ac55 1127 bsd_init_kprintf("calling siginit\n");
b0d623f7 1128 siginit(kernproc);
1c79356b 1129
2d21ac55 1130 bsd_init_kprintf("calling bsd_utaskbootstrap\n");
1c79356b
A
1131 bsd_utaskbootstrap();
1132
6d2010ae
A
1133 pal_kernel_announce();
1134
2d21ac55
A
1135 bsd_init_kprintf("calling mountroot_post_hook\n");
1136
9bccf70c 1137 /* invoke post-root-mount hook */
0a7de745 1138 if (mountroot_post_hook != NULL) {
9bccf70c 1139 mountroot_post_hook();
0a7de745 1140 }
2d21ac55
A
1141
1142#if 0 /* not yet */
5ba3f43e 1143 consider_zone_gc(FALSE);
2d21ac55 1144#endif
b0d623f7 1145
d9a64523
A
1146 /* Initialize System Override call */
1147 init_system_override();
0a7de745 1148
2d21ac55 1149 bsd_init_kprintf("done\n");
1c79356b
A
1150}
1151
1152void
9bccf70c 1153bsdinit_task(void)
1c79356b 1154{
2d21ac55 1155 proc_t p = current_proc();
1c79356b 1156
91447636 1157 process_name("init", p);
1c79356b 1158
d9a64523
A
1159 /* Set up exception-to-signal reflection */
1160 ux_handler_setup();
1c79356b 1161
2d21ac55
A
1162#if CONFIG_MACF
1163 mac_cred_label_associate_user(p->p_ucred);
2d21ac55 1164#endif
813fb2f6 1165
0a7de745 1166 vm_init_before_launchd();
5ba3f43e 1167
d9a64523
A
1168#if CONFIG_XNUPOST
1169 int result = bsd_list_tests();
1170 result = bsd_do_post();
1171 if (result != 0) {
1172 panic("bsd_do_post: Tests failed with result = 0x%08x\n", result);
1173 }
1174#endif
5ba3f43e
A
1175
1176 bsd_init_kprintf("bsd_do_post - done");
813fb2f6 1177
1c79356b 1178 load_init_program(p);
91447636 1179 lock_trace = 1;
1c79356b
A
1180}
1181
2d21ac55
A
1182kern_return_t
1183bsd_autoconf(void)
9bccf70c 1184{
2d21ac55 1185 kprintf("bsd_autoconf: calling kminit\n");
1c79356b
A
1186 kminit();
1187
0a7de745 1188 /*
1c79356b
A
1189 * Early startup for bsd pseudodevices.
1190 */
1191 {
0a7de745
A
1192 struct pseudo_init *pi;
1193
1194 for (pi = pseudo_inits; pi->ps_func; pi++) {
1195 (*pi->ps_func)(pi->ps_count);
1196 }
1c79356b
A
1197 }
1198
0a7de745 1199 return IOKitBSDInit();
1c79356b
A
1200}
1201
1202
55e303ae 1203#include <sys/disklabel.h> /* for MAXPARTITIONS */
1c79356b 1204
91447636
A
1205static void
1206setconf(void)
0a7de745
A
1207{
1208 u_int32_t flags;
1209 kern_return_t err;
1c79356b 1210
cf7d32b8 1211 err = IOFindBSDRoot(rootdevice, sizeof(rootdevice), &rootdev, &flags);
0a7de745 1212 if (err) {
1c79356b 1213 printf("setconf: IOFindBSDRoot returned an error (%d);"
0a7de745 1214 "setting rootdevice to 'sd0a'.\n", err); /* XXX DEBUG TEMP */
1c79356b 1215 rootdev = makedev( 6, 0 );
2d21ac55 1216 strlcpy(rootdevice, "sd0a", sizeof(rootdevice));
1c79356b
A
1217 flags = 0;
1218 }
1219
cb323159 1220#if CONFIG_NETBOOT
0a7de745 1221 if (flags & 1) {
9bccf70c
A
1222 /* network device */
1223 mountroot = netboot_mountroot;
1c79356b 1224 } else {
91447636 1225#endif
0a7de745
A
1226 /* otherwise have vfs determine root filesystem */
1227 mountroot = NULL;
cb323159 1228#if CONFIG_NETBOOT
0a7de745 1229}
91447636 1230#endif
1c79356b
A
1231}
1232
2d21ac55
A
1233void
1234bsd_utaskbootstrap(void)
1c79356b 1235{
2d21ac55 1236 thread_t thread;
9bccf70c 1237 struct uthread *ut;
1c79356b 1238
b0d623f7
A
1239 /*
1240 * Clone the bootstrap process from the kernel process, without
1241 * inheriting either task characteristics or memory from the kernel;
1242 */
fe8ab488 1243 thread = cloneproc(TASK_NULL, COALITION_NULL, kernproc, FALSE, TRUE);
b0d623f7 1244
2d21ac55 1245 /* Hold the reference as it will be dropped during shutdown */
0a7de745 1246 initproc = proc_find(1);
2d21ac55 1247#if __PROC_INTERNAL_DEBUG
0a7de745 1248 if (initproc == PROC_NULL) {
2d21ac55 1249 panic("bsd_utaskbootstrap: initproc not set\n");
0a7de745 1250 }
2d21ac55 1251#endif
b0d623f7
A
1252 /*
1253 * Since we aren't going back out the normal way to our parent,
1254 * we have to drop the transition locks explicitly.
1255 */
1256 proc_signalend(initproc, 0);
1257 proc_transend(initproc, 0);
9bccf70c 1258
2d21ac55 1259 ut = (struct uthread *)get_bsdthread_info(thread);
9bccf70c 1260 ut->uu_sigmask = 0;
2d21ac55 1261 act_set_astbsd(thread);
cb323159 1262 task_clear_return_wait(get_threadtask(thread), TCRW_CLEAR_ALL_WAIT);
1c79356b
A
1263}
1264
2d21ac55
A
1265static void
1266parse_bsd_args(void)
1c79356b 1267{
cb323159 1268 char namep[48];
2d21ac55 1269 int msgbuf;
1c79356b 1270
0a7de745 1271 if (PE_parse_boot_argn("-s", namep, sizeof(namep))) {
1c79356b 1272 boothowto |= RB_SINGLE;
0a7de745 1273 }
55e303ae 1274
0a7de745 1275 if (PE_parse_boot_argn("-x", namep, sizeof(namep))) { /* safe boot */
2d21ac55 1276 boothowto |= RB_SAFEBOOT;
0a7de745 1277 }
1c79356b 1278
3e170ce0
A
1279 if (PE_parse_boot_argn("-minimalboot", namep, sizeof(namep))) {
1280 /*
1281 * -minimalboot indicates that we want userspace to be bootstrapped to a
1282 * minimal environment. What constitutes minimal is up to the bootstrap
1283 * process.
1284 */
1285 minimalboot = 1;
1286 }
1287
a39ff7e2 1288#if __x86_64__
cb323159
A
1289 int no32exec;
1290
a39ff7e2 1291 /* disable 32 bit grading */
cb323159
A
1292 if (PE_parse_boot_argn("no32exec", &no32exec, sizeof(no32exec))) {
1293 bootarg_no32exec = !!no32exec;
0a7de745 1294 }
a39ff7e2 1295#endif
fe8ab488 1296
cb323159
A
1297 int execfailure_crashreports;
1298 /* enable crash reports on various exec failures */
1299 if (PE_parse_boot_argn("execfailurecrashes", &execfailure_crashreports, sizeof(execfailure_crashreports))) {
1300 bootarg_execfailurereports = !!execfailure_crashreports;
1301 }
1302
6d2010ae 1303 /* disable vnode_cache_is_authorized() by setting vnode_cache_defeat */
0a7de745 1304 if (PE_parse_boot_argn("-vnode_cache_defeat", namep, sizeof(namep))) {
6d2010ae 1305 bootarg_vnode_cache_defeat = 1;
0a7de745 1306 }
6d2010ae
A
1307
1308#if DEVELOPMENT || DEBUG
0a7de745 1309 if (PE_parse_boot_argn("-disable_aslr", namep, sizeof(namep))) {
6d2010ae 1310 bootarg_disable_aslr = 1;
0a7de745 1311 }
6d2010ae
A
1312#endif
1313
0a7de745 1314 PE_parse_boot_argn("ncl", &ncl, sizeof(ncl));
b0d623f7 1315 if (PE_parse_boot_argn("nbuf", &max_nbuf_headers,
0a7de745 1316 sizeof(max_nbuf_headers))) {
0c530ab8 1317 customnbuf = 1;
2d21ac55 1318 }
6d2010ae
A
1319
1320#if CONFIG_MACF
1321#if defined (__i386__) || defined (__x86_64__)
0a7de745 1322 PE_parse_boot_argn("policy_check", &policy_check_flags, sizeof(policy_check_flags));
6d2010ae 1323#endif
0a7de745 1324#endif /* CONFIG_MACF */
1c79356b 1325
0a7de745 1326 if (PE_parse_boot_argn("msgbuf", &msgbuf, sizeof(msgbuf))) {
2d21ac55 1327 log_setsize(msgbuf);
39037602 1328 oslog_setsize(msgbuf);
2d21ac55 1329 }
6d2010ae
A
1330
1331 if (PE_parse_boot_argn("-novfscache", namep, sizeof(namep))) {
1332 nc_disabled = 1;
1333 }
3e170ce0
A
1334
1335#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
0a7de745
A
1336 if (PE_parse_boot_argn("-no_vnode_jetsam", namep, sizeof(namep))) {
1337 bootarg_no_vnode_jetsam = 1;
1338 }
3e170ce0
A
1339#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
1340
94ff46dc
A
1341 if (PE_parse_boot_argn("-no_vnode_drain", namep, sizeof(namep))) {
1342 bootarg_no_vnode_drain = 1;
1343 }
3e170ce0 1344
5ba3f43e
A
1345#if CONFIG_EMBEDDED
1346 /*
1347 * The darkboot flag is specified by the bootloader and is stored in
1348 * boot_args->bootFlags. This flag is available starting revision 2.
1349 */
1350 boot_args *args = (boot_args *) PE_state.bootArgs;
1351 if ((args != NULL) && (args->Revision >= kBootArgsRevision2)) {
1352 darkboot = (args->bootFlags & kBootFlagsDarkBoot) ? 1 : 0;
1353 } else {
1354 darkboot = 0;
1355 }
1356#endif
3e170ce0
A
1357
1358#if PROC_REF_DEBUG
1359 if (PE_parse_boot_argn("-disable_procref_tracking", namep, sizeof(namep))) {
1360 proc_ref_tracking_disabled = 1;
1361 }
1362#endif
1363
39037602
A
1364#if OS_REASON_DEBUG
1365 if (PE_parse_boot_argn("-disable_osreason_debug", namep, sizeof(namep))) {
1366 os_reason_debug_disabled = 1;
1367 }
1368#endif
1369
3e170ce0 1370 PE_parse_boot_argn("sigrestrict", &sigrestrict_arg, sizeof(sigrestrict_arg));
39037602 1371
0a7de745 1372#if DEVELOPMENT || DEBUG
39037602
A
1373 if (PE_parse_boot_argn("-no_sigsys", namep, sizeof(namep))) {
1374 send_sigsys = false;
1375 }
39037602 1376
39037602 1377 if (PE_parse_boot_argn("alt-dyld", dyld_alt_path, sizeof(dyld_alt_path))) {
0a7de745
A
1378 if (strlen(dyld_alt_path) > 0) {
1379 use_alt_dyld = 1;
1380 }
39037602 1381 }
cb323159
A
1382 PE_parse_boot_argn("dyld_flags", &dyld_flags, sizeof(dyld_flags));
1383
1384 if (PE_parse_boot_argn("-disable_syscallfilter", &namep, sizeof(namep))) {
1385 syscallfilter_disable = 1;
1386 }
1387
1388#if __arm64__
1389 if (PE_parse_boot_argn("legacy_footprint_entitlement_mode", &legacy_footprint_entitlement_mode, sizeof(legacy_footprint_entitlement_mode))) {
1390 /*
1391 * legacy_footprint_entitlement_mode specifies the behavior we want associated
1392 * with the entitlement. The supported modes are:
1393 *
1394 * LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE:
1395 * Indicates that we want every process to have the memory accounting
1396 * that is available in iOS 12.0 and beyond.
1397 *
1398 * LEGACY_FOOTPRINT_ENTITLEMENT_IOS11_ACCT:
1399 * Indicates that for every process that has the 'legacy footprint entitlement',
1400 * we want to give it the old iOS 11.0 accounting behavior which accounted some
1401 * of the process's memory to the kernel.
1402 *
1403 * LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE:
1404 * Indicates that for every process that has the 'legacy footprint entitlement',
1405 * we want it to have a higher memory limit which will help them acclimate to the
1406 * iOS 12.0 (& beyond) accounting behavior that does the right accounting.
1407 * The bonus added to the system-wide task limit to calculate this higher memory limit
1408 * is available in legacy_footprint_bonus_mb.
1409 */
1410
1411 if (legacy_footprint_entitlement_mode < LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE ||
1412 legacy_footprint_entitlement_mode > LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE) {
1413 legacy_footprint_entitlement_mode = LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE;
1414 }
1415 }
1416#endif /* __arm64__ */
1417#endif /* DEVELOPMENT || DEBUG */
1c79356b
A
1418}
1419
b0d623f7
A
1420void
1421bsd_exec_setup(int scale)
1422{
b0d623f7 1423 switch (scale) {
0a7de745
A
1424 case 0:
1425 case 1:
1426 bsd_simul_execs = BSD_SIMUL_EXECS;
1427 break;
1428 case 2:
1429 case 3:
1430 bsd_simul_execs = 65;
1431 break;
1432 case 4:
1433 case 5:
1434 bsd_simul_execs = 129;
1435 break;
1436 case 6:
1437 case 7:
1438 bsd_simul_execs = 257;
1439 break;
1440 default:
1441 bsd_simul_execs = 513;
1442 break;
b0d623f7 1443 }
6d2010ae 1444 bsd_pageable_map_size = (bsd_simul_execs * BSD_PAGEABLE_SIZE_PER_EXEC);
b0d623f7
A
1445}
1446
cb323159 1447#if !CONFIG_NETBOOT
0a7de745 1448int
6d2010ae
A
1449netboot_root(void);
1450
0a7de745 1451int
91447636 1452netboot_root(void)
1c79356b 1453{
0a7de745 1454 return 0;
1c79356b 1455}
91447636 1456#endif