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