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