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