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