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