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