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