]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/bsd_init.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / kern / bsd_init.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ff6e181a
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
1c79356b 12 *
ff6e181a
A
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ff6e181a
A
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
1c79356b
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
61 */
62
63/*
64 *
65 * Mach Operating System
66 * Copyright (c) 1987 Carnegie-Mellon University
67 * All rights reserved. The CMU software License Agreement specifies
68 * the terms and conditions for use and redistribution.
69 */
1c79356b
A
70
71#include <sys/param.h>
72#include <sys/filedesc.h>
73#include <sys/kernel.h>
91447636
A
74#include <sys/mount_internal.h>
75#include <sys/proc_internal.h>
76#include <sys/kauth.h>
1c79356b 77#include <sys/systm.h>
91447636 78#include <sys/vnode_internal.h>
1c79356b 79#include <sys/conf.h>
91447636 80#include <sys/buf_internal.h>
1c79356b
A
81#include <sys/clist.h>
82#include <sys/user.h>
55e303ae
A
83#include <sys/time.h>
84#include <sys/systm.h>
91447636 85#include <sys/mman.h>
55e303ae 86
e5568f75 87#include <bsm/audit_kernel.h>
1c79356b
A
88
89#include <sys/malloc.h>
90#include <sys/dkstat.h>
91
91447636 92#include <kern/startup.h>
1c79356b
A
93#include <kern/thread.h>
94#include <kern/task.h>
95#include <kern/ast.h>
96
97#include <mach/vm_param.h>
98
99#include <vm/vm_map.h>
100#include <vm/vm_kern.h>
101
55e303ae 102#include <sys/ux_exception.h> /* for ux_exception_port */
1c79356b
A
103
104#include <sys/reboot.h>
105#include <mach/exception_types.h>
55e303ae 106#include <dev/busvar.h> /* for pseudo_inits */
1c79356b
A
107#include <sys/kdebug.h>
108
765c9de3
A
109#include <mach/mach_types.h>
110#include <mach/vm_prot.h>
111#include <mach/semaphore.h>
112#include <mach/sync_policy.h>
113#include <kern/clock.h>
114#include <mach/kern_return.h>
115
9bccf70c
A
116#include <mach/shared_memory_server.h>
117#include <vm/vm_shared_memory_server.h>
118
91447636
A
119#include <net/init.h>
120
9bccf70c 121extern int app_profile; /* on/off switch for pre-heat cache */
1c79356b
A
122
123char copyright[] =
55e303ae
A
124"Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t"
125"The Regents of the University of California. "
126"All rights reserved.\n\n";
1c79356b
A
127
128extern void ux_handler();
129
130/* Components of the first process -- never freed. */
131struct proc proc0;
132struct session session0;
133struct pgrp pgrp0;
1c79356b
A
134struct filedesc filedesc0;
135struct plimit limit0;
136struct pstats pstats0;
137struct sigacts sigacts0;
138struct proc *kernproc, *initproc;
139
1c79356b
A
140long tk_cancc;
141long tk_nin;
142long tk_nout;
143long tk_rawcc;
144
91447636 145int lock_trace = 0;
1c79356b
A
146/* Global variables to make pstat happy. We do swapping differently */
147int nswdev, nswap;
148int nswapmap;
149void *swapmap;
150struct swdevt swdevt[1];
151
152dev_t rootdev; /* device of the root */
153dev_t dumpdev; /* device to take dumps on */
154long dumplo; /* offset into dumpdev */
155long hostid;
156char hostname[MAXHOSTNAMELEN];
55e303ae 157int hostnamelen;
1c79356b 158char domainname[MAXDOMNAMELEN];
55e303ae
A
159int domainnamelen;
160char classichandler[32] = {0};
91447636 161uint32_t classichandler_fsid = -1L;
55e303ae 162long classichandler_fileid = -1L;
1c79356b
A
163
164char rootdevice[16]; /* hfs device names have at least 9 chars */
1c79356b
A
165
166#ifdef KMEMSTATS
167struct kmemstats kmemstats[M_LAST];
168#endif
169
170int lbolt; /* awoken once a second */
171struct vnode *rootvp;
172int boothowto = RB_DEBUG;
173
55e303ae 174#define BSD_PAGABLE_MAP_SIZE (16 * 512 * 1024)
1c79356b
A
175vm_map_t bsd_pageable_map;
176vm_map_t mb_map;
765c9de3 177semaphore_t execve_semaphore;
1c79356b
A
178
179int cmask = CMASK;
180
181int parse_bsd_args(void);
182extern int bsd_hardclockinit;
9bccf70c 183extern task_t bsd_init_task;
1c79356b 184extern char init_task_failure_data[];
9bccf70c 185extern void time_zone_slock_init(void);
91447636
A
186static void process_name(char *, struct proc *);
187
188static void setconf(void);
1c79356b 189
55e303ae 190funnel_t *kernel_flock;
91447636
A
191
192extern void sysv_shm_lock_init(void);
193extern void sysv_sem_lock_init(void);
194extern void sysv_msg_lock_init(void);
195extern void pshm_lock_init();
196extern void psem_lock_init();
1c79356b
A
197
198/*
199 * Initialization code.
200 * Called from cold start routine as
201 * soon as a stack and segmentation
202 * have been established.
203 * Functions:
1c79356b
A
204 * turn on clock
205 * hand craft 0th process
206 * call all initialization routines
55e303ae 207 * hand craft 1st user process
1c79356b
A
208 */
209
210/*
211 * Sets the name for the given task.
212 */
91447636
A
213static void
214process_name(s, p)
1c79356b
A
215 char *s;
216 struct proc *p;
217{
218 int length = strlen(s);
219
220 bcopy(s, p->p_comm,
221 length >= sizeof(p->p_comm) ? sizeof(p->p_comm) :
222 length + 1);
223}
224
1c79356b
A
225/* To allow these values to be patched, they're globals here */
226#include <machine/vmparam.h>
227struct rlimit vm_initial_limit_stack = { DFLSSIZ, MAXSSIZ };
228struct rlimit vm_initial_limit_data = { DFLDSIZ, MAXDSIZ };
229struct rlimit vm_initial_limit_core = { DFLCSIZ, MAXCSIZ };
230
91447636
A
231extern thread_t cloneproc(struct proc *, int);
232extern int (*mountroot)(void);
9bccf70c
A
233extern int netboot_mountroot(); /* netboot.c */
234extern int netboot_setup(struct proc * p);
1c79356b 235
91447636
A
236lck_grp_t * proc_lck_grp;
237lck_grp_attr_t * proc_lck_grp_attr;
238lck_attr_t * proc_lck_attr;
239
9bccf70c
A
240/* hook called after root is mounted XXX temporary hack */
241void (*mountroot_post_hook)(void);
1c79356b 242
91447636
A
243/*
244 * This function is called very early on in the Mach startup, from the
245 * function start_kernel_threads() in osfmk/kern/startup.c. It's called
246 * in the context of the current (startup) task using a call to the
247 * function kernel_thread_create() to jump into start_kernel_threads().
248 * Internally, kernel_thread_create() calls thread_create_internal(),
249 * which calls uthread_alloc(). The function of uthread_alloc() is
250 * normally to allocate a uthread structure, and fill out the uu_sigmask,
251 * uu_act, and uu_ucred fields. It skips filling these out in the case
252 * of the "task" being "kernel_task", because the order of operation is
253 * inverted. To account for that, we need to manually fill in at least
254 * the uu_cred field so that the uthread structure can be used like any
255 * other.
256 */
1c79356b
A
257void
258bsd_init()
259{
260 register struct proc *p;
91447636
A
261 struct uthread *ut;
262 extern kauth_cred_t rootcred;
1c79356b
A
263 register int i;
264 int s;
265 thread_t th;
91447636 266 struct vfs_context context;
1c79356b
A
267 void lightning_bolt(void );
268 kern_return_t ret;
269 boolean_t funnel_state;
91447636
A
270 struct ucred temp_cred;
271 extern void file_lock_init(void);
1c79356b
A
272
273 kernel_flock = funnel_alloc(KERNEL_FUNNEL);
274 if (kernel_flock == (funnel_t *)0 ) {
9bccf70c 275 panic("bsd_init: Failed to allocate kernel funnel");
1c79356b
A
276 }
277
1c79356b
A
278 funnel_state = thread_funnel_set(kernel_flock, TRUE);
279
1c79356b 280 printf(copyright);
91447636 281
1c79356b
A
282 kmeminit();
283
284 parse_bsd_args();
285
1c79356b 286 /* Initialize the uthread zone */
91447636 287 //uthread_zone_init(); /* XXX redundant: previous uthread_alloc() */
1c79356b 288
91447636
A
289 /* Initialize kauth subsystem before instancing the first credential */
290 kauth_init();
291
292 /* Initialize process and pgrp structures. */
1c79356b
A
293 procinit();
294
295 kernproc = &proc0;
296
297 p = kernproc;
298
299 /* kernel_task->proc = kernproc; */
9bccf70c 300 set_bsdtask_info(kernel_task,(void *)p);
1c79356b
A
301 p->p_pid = 0;
302
303 /* give kernproc a name */
91447636
A
304 process_name("kernel_task", p);
305
306
307 /* allocate proc lock group attribute and group */
308 proc_lck_grp_attr= lck_grp_attr_alloc_init();
309 lck_grp_attr_setstat(proc_lck_grp_attr);
310
311 proc_lck_grp = lck_grp_alloc_init("proc", proc_lck_grp_attr);
312
313
314 /* Allocate proc lock attribute */
315 proc_lck_attr = lck_attr_alloc_init();
316 //lck_attr_setdebug(proc_lck_attr);
317
318 lck_mtx_init(&p->p_mlock, proc_lck_grp, proc_lck_attr);
319 lck_mtx_init(&p->p_fdmlock, proc_lck_grp, proc_lck_attr);
1c79356b
A
320
321 if (current_task() != kernel_task)
9bccf70c
A
322 printf("bsd_init: We have a problem, "
323 "current task is not kernel task\n");
1c79356b 324
91447636
A
325 ut = (uthread_t)get_bsdthread_info(current_thread());
326
1c79356b
A
327 /*
328 * Create process 0.
329 */
330 LIST_INSERT_HEAD(&allproc, p, p_list);
331 p->p_pgrp = &pgrp0;
332 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
333 LIST_INIT(&pgrp0.pg_members);
334 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
335
336 pgrp0.pg_session = &session0;
337 session0.s_count = 1;
338 session0.s_leader = p;
339
340 p->task = kernel_task;
341
342 p->p_stat = SRUN;
91447636 343 p->p_flag = P_SYSTEM;
1c79356b
A
344 p->p_nice = NZERO;
345 p->p_pptr = p;
346 lockinit(&p->signal_lock, PVM, "signal", 0, 0);
9bccf70c 347 TAILQ_INIT(&p->p_uthlist);
1c79356b
A
348 p->sigwait = FALSE;
349 p->sigwait_thread = THREAD_NULL;
350 p->exit_thread = THREAD_NULL;
351
91447636
A
352 /*
353 * Create credential. This also Initializes the audit information.
354 * XXX It is not clear what the initial values should be for audit ID,
355 * XXX session ID, etc..
356 */
357 bzero(&temp_cred, sizeof(temp_cred));
358 temp_cred.cr_ngroups = 1;
359
360 p->p_ucred = kauth_cred_create(&temp_cred);
361
362 /* give the (already exisiting) initial thread a reference on it */
363 kauth_cred_ref(p->p_ucred);
364 ut->uu_ucred = p->p_ucred;
55e303ae
A
365
366 TAILQ_INIT(&p->aio_activeq);
367 TAILQ_INIT(&p->aio_doneq);
368 p->aio_active_count = 0;
369 p->aio_done_count = 0;
370
91447636 371 file_lock_init();
1c79356b
A
372
373 /* Create the file descriptor table. */
374 filedesc0.fd_refcnt = 1+1; /* +1 so shutdown will not _FREE_ZONE */
375 p->p_fd = &filedesc0;
376 filedesc0.fd_cmask = cmask;
55e303ae
A
377 filedesc0.fd_knlistsize = -1;
378 filedesc0.fd_knlist = NULL;
379 filedesc0.fd_knhash = NULL;
380 filedesc0.fd_knhashmask = 0;
1c79356b
A
381
382 /* Create the limits structures. */
383 p->p_limit = &limit0;
384 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
385 limit0.pl_rlimit[i].rlim_cur =
386 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
387 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
388 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
55e303ae 389 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
1c79356b
A
390 limit0.pl_rlimit[RLIMIT_STACK] = vm_initial_limit_stack;
391 limit0.pl_rlimit[RLIMIT_DATA] = vm_initial_limit_data;
392 limit0.pl_rlimit[RLIMIT_CORE] = vm_initial_limit_core;
393 limit0.p_refcnt = 1;
394
395 p->p_stats = &pstats0;
396 p->p_sigacts = &sigacts0;
397
398 /*
91447636 399 * Charge root for two processes: init and mach_init.
1c79356b
A
400 */
401 (void)chgproccnt(0, 1);
402
1c79356b
A
403 /*
404 * Allocate a kernel submap for pageable memory
765c9de3 405 * for temporary copying (execve()).
1c79356b
A
406 */
407 {
408 vm_offset_t min;
409
410 ret = kmem_suballoc(kernel_map,
411 &min,
765c9de3 412 (vm_size_t)BSD_PAGABLE_MAP_SIZE,
1c79356b 413 TRUE,
91447636 414 VM_FLAGS_ANYWHERE,
1c79356b 415 &bsd_pageable_map);
9bccf70c
A
416 if (ret != KERN_SUCCESS)
417 panic("bsd_init: Failed to allocate bsd pageable map");
765c9de3
A
418 }
419
91447636
A
420 /*
421 * Initialize buffers and hash links for buffers
422 *
423 * SIDE EFFECT: Starts a thread for bcleanbuf_thread(), so must
424 * happen after a credential has been associated with
425 * the kernel task.
426 */
427 bsd_bufferinit();
428
765c9de3 429 /* Initialize the execve() semaphore */
9bccf70c
A
430 ret = semaphore_create(kernel_task, &execve_semaphore,
431 SYNC_POLICY_FIFO, (BSD_PAGABLE_MAP_SIZE / NCARGS));
432 if (ret != KERN_SUCCESS)
433 panic("bsd_init: Failed to create execve semaphore");
1c79356b
A
434
435 /*
0b4e3aa0
A
436 * Initialize the calendar.
437 */
1c79356b
A
438 IOKitResetTime();
439
440 ubc_init();
441
442 /* Initialize the file systems. */
443 vfsinit();
444
445 /* Initialize mbuf's. */
446 mbinit();
447
55e303ae
A
448 /*
449 * Initializes security event auditing.
450 * XXX: Should/could this occur later?
451 */
452 audit_init();
453
454 /* Initialize kqueues */
455 knote_init();
456
457 /* Initialize for async IO */
458 aio_init();
459
91447636
A
460 /* Initialize pipes */
461 pipeinit();
462
463 /* Initialize SysV shm subsystem locks; the subsystem proper is
464 * initialized through a sysctl.
465 */
466 sysv_shm_lock_init();
467 sysv_sem_lock_init();
468 sysv_msg_lock_init();
469 pshm_lock_init();
470 psem_lock_init();
471
9bccf70c
A
472 /* POSIX Shm and Sem */
473 pshm_cache_init();
474 psem_cache_init();
475 time_zone_slock_init();
1c79356b 476
1c79356b
A
477 /*
478 * Initialize protocols. Block reception of incoming packets
479 * until everything is ready.
480 */
1c79356b 481 sysctl_register_fixed();
43866e37 482 sysctl_mib_init();
1c79356b 483 dlil_init();
91447636 484 proto_kpi_init();
1c79356b
A
485 socketinit();
486 domaininit();
1c79356b 487
1c79356b
A
488 p->p_fd->fd_cdir = NULL;
489 p->p_fd->fd_rdir = NULL;
490
1c79356b
A
491#ifdef GPROF
492 /* Initialize kernel profiling. */
493 kmstartup();
494#endif
495
496 /* kick off timeout driven events by calling first time */
497 thread_wakeup(&lbolt);
55e303ae 498 timeout((void (*)(void *))lightning_bolt, 0, hz);
1c79356b
A
499
500 bsd_autoconf();
501
502 /*
503 * We attach the loopback interface *way* down here to ensure
504 * it happens after autoconf(), otherwise it becomes the
505 * "primary" interface.
506 */
507#include <loop.h>
508#if NLOOP > 0
509 loopattach(); /* XXX */
510#endif
9bccf70c
A
511
512 /* Register the built-in dlil ethernet interface family */
513 ether_family_init();
1c79356b 514
91447636
A
515 /* Call any kext code that wants to run just after network init */
516 net_init_run();
517
1c79356b 518 vnode_pager_bootstrap();
91447636
A
519#if 0
520 /* XXX Hack for early debug stop */
521 printf("\nabout to sleep for 10 seconds\n");
522 IOSleep( 10 * 1000 );
523 /* Debugger("hello"); */
524#endif
525
526 inittodr(0);
1c79356b
A
527
528 /* Mount the root file system. */
529 while( TRUE) {
530 int err;
531
532 setconf();
9bccf70c
A
533 bsd_hardclockinit = -1; /* start ticking */
534
1c79356b
A
535 if (0 == (err = vfs_mountroot()))
536 break;
91447636 537#if NFSCLIENT
9bccf70c
A
538 if (mountroot == netboot_mountroot) {
539 printf("cannot mount network root, errno = %d\n", err);
540 mountroot = NULL;
541 if (0 == (err = vfs_mountroot()))
542 break;
543 }
91447636 544#endif
1c79356b
A
545 printf("cannot mount root, errno = %d\n", err);
546 boothowto |= RB_ASKNAME;
547 }
548
91447636
A
549 context.vc_proc = p;
550 context.vc_ucred = p->p_ucred;
551 mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
1c79356b
A
552
553 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
91447636 554 if (VFS_ROOT(mountlist.tqh_first, &rootvnode, &context))
765c9de3 555 panic("bsd_init: cannot find root vnode");
91447636
A
556 rootvnode->v_flag |= VROOT;
557 (void)vnode_ref(rootvnode);
558 (void)vnode_put(rootvnode);
fa4905b1 559 filedesc0.fd_cdir = rootvnode;
9bccf70c 560
91447636 561#if NFSCLIENT
9bccf70c
A
562 if (mountroot == netboot_mountroot) {
563 int err;
564 /* post mount setup */
565 if (err = netboot_setup(p)) {
566 panic("bsd_init: NetBoot could not find root, %d", err);
567 }
568 }
91447636 569#endif
1c79356b
A
570
571
91447636 572 microtime(&p->p_stats->p_start);
1c79356b
A
573 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
574
9bccf70c 575#if DEVFS
1c79356b
A
576 {
577 extern void devfs_kernel_mount(char * str);
578
579 devfs_kernel_mount("/dev");
580 }
55e303ae 581#endif /* DEVFS */
1c79356b
A
582
583 /* Initialize signal state for process 0. */
584 siginit(p);
585
1c79356b
A
586 bsd_utaskbootstrap();
587
9bccf70c
A
588 /* invoke post-root-mount hook */
589 if (mountroot_post_hook != NULL)
590 mountroot_post_hook();
591
592 (void) thread_funnel_set(kernel_flock, funnel_state);
1c79356b
A
593}
594
9bccf70c 595/* Called with kernel funnel held */
1c79356b 596void
9bccf70c 597bsdinit_task(void)
1c79356b
A
598{
599 struct proc *p = current_proc();
600 struct uthread *ut;
601 kern_return_t kr;
91447636 602 thread_t th_act;
55e303ae 603 shared_region_mapping_t system_region;
1c79356b 604
91447636 605 process_name("init", p);
1c79356b
A
606
607 ux_handler_init();
1c79356b 608
91447636 609 th_act = current_thread();
1c79356b
A
610 (void) host_set_exception_ports(host_priv_self(),
611 EXC_MASK_ALL & ~(EXC_MASK_SYSCALL |
612 EXC_MASK_MACH_SYSCALL |
613 EXC_MASK_RPC_ALERT),
614 ux_exception_port,
615 EXCEPTION_DEFAULT, 0);
616
617 (void) task_set_exception_ports(get_threadtask(th_act),
618 EXC_MASK_ALL & ~(EXC_MASK_SYSCALL |
619 EXC_MASK_MACH_SYSCALL |
620 EXC_MASK_RPC_ALERT),
621 ux_exception_port,
622 EXCEPTION_DEFAULT, 0);
623
624
625
626
627 ut = (uthread_t)get_bsdthread_info(th_act);
628 ut->uu_ar0 = (void *)get_user_regs(th_act);
629
630 bsd_hardclockinit = 1; /* Start bsd hardclock */
631 bsd_init_task = get_threadtask(th_act);
632 init_task_failure_data[0] = 0;
91447636 633 system_region = lookup_default_shared_region(ENV_DEFAULT_ROOT, cpu_type());
55e303ae 634 if (system_region == NULL) {
91447636 635 shared_file_boot_time_init(ENV_DEFAULT_ROOT, cpu_type());
55e303ae
A
636 } else {
637 vm_set_shared_region(get_threadtask(th_act), system_region);
638 }
1c79356b 639 load_init_program(p);
9bccf70c
A
640 /* turn on app-profiling i.e. pre-heating */
641 app_profile = 1;
91447636 642 lock_trace = 1;
1c79356b
A
643}
644
645void
646lightning_bolt()
647{
648 boolean_t funnel_state;
649 extern void klogwakeup(void);
650
651 funnel_state = thread_funnel_set(kernel_flock, TRUE);
652
653 thread_wakeup(&lbolt);
654 timeout(lightning_bolt,0,hz);
655 klogwakeup();
656
657 (void) thread_funnel_set(kernel_flock, FALSE);
658}
659
9bccf70c
A
660bsd_autoconf()
661{
662 extern kern_return_t IOKitBSDInit( void );
1c79356b
A
663
664 kminit();
665
666 /*
667 * Early startup for bsd pseudodevices.
668 */
669 {
670 struct pseudo_init *pi;
671
672 for (pi = pseudo_inits; pi->ps_func; pi++)
673 (*pi->ps_func) (pi->ps_count);
674 }
675
9bccf70c 676 return( IOKitBSDInit());
1c79356b
A
677}
678
679
55e303ae 680#include <sys/disklabel.h> /* for MAXPARTITIONS */
1c79356b 681
91447636
A
682static void
683setconf(void)
1c79356b
A
684{
685 extern kern_return_t IOFindBSDRoot( char * rootName,
686 dev_t * root, u_int32_t * flags );
1c79356b
A
687 u_int32_t flags;
688 kern_return_t err;
689
690 /*
691 * calls into IOKit can generate networking registrations
692 * which needs to be under network funnel. Right thing to do
693 * here is to drop the funnel alltogether and regrab it afterwards
694 */
695 thread_funnel_set(kernel_flock, FALSE);
696 err = IOFindBSDRoot( rootdevice, &rootdev, &flags );
697 thread_funnel_set(kernel_flock, TRUE);
698 if( err) {
699 printf("setconf: IOFindBSDRoot returned an error (%d);"
700 "setting rootdevice to 'sd0a'.\n", err); /* XXX DEBUG TEMP */
701 rootdev = makedev( 6, 0 );
702 strcpy( rootdevice, "sd0a" );
703 flags = 0;
704 }
705
91447636 706#if NFSCLIENT
1c79356b 707 if( flags & 1 ) {
9bccf70c
A
708 /* network device */
709 mountroot = netboot_mountroot;
1c79356b 710 } else {
91447636 711#endif
1c79356b
A
712 /* otherwise have vfs determine root filesystem */
713 mountroot = NULL;
91447636 714#if NFSCLIENT
1c79356b 715 }
91447636 716#endif
1c79356b
A
717
718}
719
720bsd_utaskbootstrap()
721{
91447636 722 thread_t th_act;
9bccf70c 723 struct uthread *ut;
1c79356b 724
9bccf70c 725 th_act = cloneproc(kernproc, 0);
1c79356b 726 initproc = pfind(1);
9bccf70c 727 /* Set the launch time for init */
91447636 728 microtime(&initproc->p_stats->p_start);
9bccf70c
A
729
730 ut = (struct uthread *)get_bsdthread_info(th_act);
731 ut->uu_sigmask = 0;
9bccf70c 732 act_set_astbsd(th_act);
1c79356b
A
733 (void) thread_resume(th_act);
734}
735
736parse_bsd_args()
737{
738 extern char init_args[];
739 char namep[16];
740 extern int boothowto;
741 extern int srv;
742 extern int ncl;
743
744 int len;
745
746 if (PE_parse_boot_arg("-s", namep)) {
747 boothowto |= RB_SINGLE;
748 len = strlen(init_args);
749 if(len != 0)
750 strcat(init_args," -s");
751 else
752 strcat(init_args,"-s");
753 }
55e303ae 754
1c79356b
A
755 if (PE_parse_boot_arg("-b", namep)) {
756 boothowto |= RB_NOBOOTRC;
757 len = strlen(init_args);
758 if(len != 0)
759 strcat(init_args," -b");
760 else
761 strcat(init_args,"-b");
762 }
763
764 if (PE_parse_boot_arg("-F", namep)) {
765 len = strlen(init_args);
766 if(len != 0)
767 strcat(init_args," -F");
768 else
769 strcat(init_args,"-F");
770 }
771
772 if (PE_parse_boot_arg("-v", namep)) {
773 len = strlen(init_args);
774 if(len != 0)
775 strcat(init_args," -v");
776 else
777 strcat(init_args,"-v");
778 }
779
780 if (PE_parse_boot_arg("-x", namep)) { /* safe boot */
781 len = strlen(init_args);
782 if(len != 0)
783 strcat(init_args," -x");
784 else
785 strcat(init_args,"-x");
786 }
787
55e303ae
A
788 if (PE_parse_boot_arg("-d", namep)) {
789 len = strlen(init_args);
790 if(len != 0)
791 strcat(init_args," -d");
792 else
793 strcat(init_args,"-d");
794 }
795
1c79356b
A
796 PE_parse_boot_arg("srv", &srv);
797 PE_parse_boot_arg("ncl", &ncl);
798 PE_parse_boot_arg("nbuf", &nbuf);
799
800 return 0;
801}
802
91447636
A
803#if !NFSCLIENT
804int
805netboot_root(void)
1c79356b 806{
91447636 807 return(0);
1c79356b 808}
91447636 809#endif