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