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