2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
24 * The Regents of the University of California. All rights reserved.
25 * (c) UNIX System Laboratories, Inc.
26 * All or some portions of this file are derived from material licensed
27 * to the University of California by American Telephone and Telegraph
28 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
29 * the permission of UNIX System Laboratories, Inc.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
64 * Mach Operating System
65 * Copyright (c) 1987 Carnegie-Mellon University
66 * All rights reserved. The CMU software License Agreement specifies
67 * the terms and conditions for use and redistribution.
70 #include <sys/param.h>
71 #include <sys/filedesc.h>
72 #include <sys/kernel.h>
73 #include <sys/mount.h>
75 #include <sys/systm.h>
76 #include <sys/vnode.h>
79 #include <sys/clist.h>
82 #include <sys/systm.h>
84 #include <bsm/audit_kernel.h>
86 #include <sys/malloc.h>
87 #include <sys/dkstat.h>
89 #include <machine/spl.h>
90 #include <kern/thread.h>
91 #include <kern/task.h>
94 #include <mach/vm_param.h>
96 #include <vm/vm_map.h>
97 #include <vm/vm_kern.h>
99 #include <sys/ux_exception.h> /* for ux_exception_port */
101 #include <sys/reboot.h>
102 #include <mach/exception_types.h>
103 #include <dev/busvar.h> /* for pseudo_inits */
104 #include <sys/kdebug.h>
106 #include <mach/mach_types.h>
107 #include <mach/vm_prot.h>
108 #include <mach/semaphore.h>
109 #include <mach/sync_policy.h>
110 #include <kern/clock.h>
111 #include <mach/kern_return.h>
113 #include <mach/shared_memory_server.h>
114 #include <vm/vm_shared_memory_server.h>
116 extern int app_profile
; /* on/off switch for pre-heat cache */
119 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\t"
120 "The Regents of the University of California. "
121 "All rights reserved.\n\n";
123 extern void ux_handler();
125 /* Components of the first process -- never freed. */
127 struct session session0
;
130 struct filedesc filedesc0
;
131 struct plimit limit0
;
132 struct pstats pstats0
;
133 struct sigacts sigacts0
;
134 struct proc
*kernproc
, *initproc
;
141 /* Global variables to make pstat happy. We do swapping differently */
145 struct swdevt swdevt
[1];
147 dev_t rootdev
; /* device of the root */
148 dev_t dumpdev
; /* device to take dumps on */
149 long dumplo
; /* offset into dumpdev */
151 char hostname
[MAXHOSTNAMELEN
];
153 char domainname
[MAXDOMNAMELEN
];
155 char classichandler
[32] = {0};
156 long classichandler_fsid
= -1L;
157 long classichandler_fileid
= -1L;
159 char rootdevice
[16]; /* hfs device names have at least 9 chars */
160 struct timeval boottime
; /* GRODY! This has to go... */
163 struct kmemstats kmemstats
[M_LAST
];
166 int lbolt
; /* awoken once a second */
167 struct vnode
*rootvp
;
168 int boothowto
= RB_DEBUG
;
170 #define BSD_PAGABLE_MAP_SIZE (16 * 512 * 1024)
171 vm_map_t bsd_pageable_map
;
173 semaphore_t execve_semaphore
;
177 int parse_bsd_args(void);
178 extern int bsd_hardclockinit
;
179 extern task_t bsd_init_task
;
180 extern char init_task_failure_data
[];
181 extern void time_zone_slock_init(void);
183 funnel_t
*kernel_flock
;
184 funnel_t
*network_flock
;
185 int disable_funnel
= 0; /* disables split funnel */
186 int enable_funnel
= 0; /* disables split funnel */
189 * Initialization code.
190 * Called from cold start routine as
191 * soon as a stack and segmentation
192 * have been established.
195 * hand craft 0th process
196 * call all initialization routines
197 * hand craft 1st user process
201 * Sets the name for the given task.
208 int length
= strlen(s
);
211 length
>= sizeof(p
->p_comm
) ? sizeof(p
->p_comm
) :
215 /* To allow these values to be patched, they're globals here */
216 #include <machine/vmparam.h>
217 struct rlimit vm_initial_limit_stack
= { DFLSSIZ
, MAXSSIZ
};
218 struct rlimit vm_initial_limit_data
= { DFLDSIZ
, MAXDSIZ
};
219 struct rlimit vm_initial_limit_core
= { DFLCSIZ
, MAXCSIZ
};
221 extern thread_t first_thread
;
222 extern thread_act_t
cloneproc(struct proc
*, int);
223 extern int (*mountroot
) __P((void));
224 extern int netboot_mountroot(); /* netboot.c */
225 extern int netboot_setup(struct proc
* p
);
227 /* hook called after root is mounted XXX temporary hack */
228 void (*mountroot_post_hook
)(void);
233 register struct proc
*p
;
234 extern struct ucred
*rootcred
;
238 void lightning_bolt(void );
240 boolean_t funnel_state
;
241 extern void uthread_zone_init();
244 /* split funnel is enabled by default */
245 PE_parse_boot_arg("dfnl", &disable_funnel
);
247 kernel_flock
= funnel_alloc(KERNEL_FUNNEL
);
248 if (kernel_flock
== (funnel_t
*)0 ) {
249 panic("bsd_init: Failed to allocate kernel funnel");
252 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
254 if (!disable_funnel
) {
255 network_flock
= funnel_alloc(NETWORK_FUNNEL
);
256 if (network_flock
== (funnel_t
*)0 ) {
257 panic("bsd_init: Failed to allocate network funnel");
260 network_flock
= kernel_flock
;
271 /* Initialize the uthread zone */
275 * Initialize process and pgrp structures.
283 /* kernel_task->proc = kernproc; */
284 set_bsdtask_info(kernel_task
,(void *)p
);
287 /* give kernproc a name */
288 proc_name("kernel_task", p
);
290 if (current_task() != kernel_task
)
291 printf("bsd_init: We have a problem, "
292 "current task is not kernel task\n");
297 LIST_INSERT_HEAD(&allproc
, p
, p_list
);
299 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0
, pg_hash
);
300 LIST_INIT(&pgrp0
.pg_members
);
301 LIST_INSERT_HEAD(&pgrp0
.pg_members
, p
, p_pglist
);
303 pgrp0
.pg_session
= &session0
;
304 session0
.s_count
= 1;
305 session0
.s_leader
= p
;
307 p
->task
= kernel_task
;
310 p
->p_flag
= P_INMEM
|P_SYSTEM
;
313 lockinit(&p
->signal_lock
, PVM
, "signal", 0, 0);
314 TAILQ_INIT(&p
->p_uthlist
);
316 p
->sigwait_thread
= THREAD_NULL
;
317 p
->exit_thread
= THREAD_NULL
;
319 /* Create credentials. */
320 lockinit(&cred0
.pc_lock
, PLOCK
, "proc0 cred", 0, 0);
323 p
->p_ucred
= crget();
324 p
->p_ucred
->cr_ngroups
= 1; /* group 0 */
326 TAILQ_INIT(&p
->aio_activeq
);
327 TAILQ_INIT(&p
->aio_doneq
);
328 p
->aio_active_count
= 0;
329 p
->aio_done_count
= 0;
331 /* Set the audit info for this process */
334 /* Create the file descriptor table. */
335 filedesc0
.fd_refcnt
= 1+1; /* +1 so shutdown will not _FREE_ZONE */
336 p
->p_fd
= &filedesc0
;
337 filedesc0
.fd_cmask
= cmask
;
338 filedesc0
.fd_knlistsize
= -1;
339 filedesc0
.fd_knlist
= NULL
;
340 filedesc0
.fd_knhash
= NULL
;
341 filedesc0
.fd_knhashmask
= 0;
343 /* Create the limits structures. */
344 p
->p_limit
= &limit0
;
345 for (i
= 0; i
< sizeof(p
->p_rlimit
)/sizeof(p
->p_rlimit
[0]); i
++)
346 limit0
.pl_rlimit
[i
].rlim_cur
=
347 limit0
.pl_rlimit
[i
].rlim_max
= RLIM_INFINITY
;
348 limit0
.pl_rlimit
[RLIMIT_NOFILE
].rlim_cur
= NOFILE
;
349 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_cur
= MAXUPRC
;
350 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_max
= maxproc
;
351 limit0
.pl_rlimit
[RLIMIT_STACK
] = vm_initial_limit_stack
;
352 limit0
.pl_rlimit
[RLIMIT_DATA
] = vm_initial_limit_data
;
353 limit0
.pl_rlimit
[RLIMIT_CORE
] = vm_initial_limit_core
;
356 p
->p_stats
= &pstats0
;
357 p
->p_sigacts
= &sigacts0
;
360 * Charge root for one process.
362 (void)chgproccnt(0, 1);
365 * Allocate a kernel submap for pageable memory
366 * for temporary copying (execve()).
371 ret
= kmem_suballoc(kernel_map
,
373 (vm_size_t
)BSD_PAGABLE_MAP_SIZE
,
377 if (ret
!= KERN_SUCCESS
)
378 panic("bsd_init: Failed to allocate bsd pageable map");
381 /* Initialize the execve() semaphore */
382 ret
= semaphore_create(kernel_task
, &execve_semaphore
,
383 SYNC_POLICY_FIFO
, (BSD_PAGABLE_MAP_SIZE
/ NCARGS
));
384 if (ret
!= KERN_SUCCESS
)
385 panic("bsd_init: Failed to create execve semaphore");
388 * Initialize the calendar.
394 /* Initialize the file systems. */
397 /* Initialize mbuf's. */
400 /* Initialize syslog */
404 * Initializes security event auditing.
405 * XXX: Should/could this occur later?
409 /* Initialize kqueues */
412 /* Initialize for async IO */
415 /* POSIX Shm and Sem */
418 time_zone_slock_init();
421 * Initialize protocols. Block reception of incoming packets
422 * until everything is ready.
425 sysctl_register_fixed();
432 p
->p_fd
->fd_cdir
= NULL
;
433 p
->p_fd
->fd_rdir
= NULL
;
436 /* Initialize kernel profiling. */
440 /* kick off timeout driven events by calling first time */
441 thread_wakeup(&lbolt
);
442 timeout((void (*)(void *))lightning_bolt
, 0, hz
);
447 * We attach the loopback interface *way* down here to ensure
448 * it happens after autoconf(), otherwise it becomes the
449 * "primary" interface.
453 loopattach(); /* XXX */
456 /* Register the built-in dlil ethernet interface family */
459 vnode_pager_bootstrap();
461 /* Mount the root file system. */
467 * read the time after clock_initialize_calendar()
468 * and before nfs mount
470 microtime((struct timeval
*)&time
);
472 bsd_hardclockinit
= -1; /* start ticking */
474 if (0 == (err
= vfs_mountroot()))
476 if (mountroot
== netboot_mountroot
) {
477 printf("cannot mount network root, errno = %d\n", err
);
479 if (0 == (err
= vfs_mountroot()))
482 printf("cannot mount root, errno = %d\n", err
);
483 boothowto
|= RB_ASKNAME
;
486 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
488 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
489 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
490 panic("bsd_init: cannot find root vnode");
492 filedesc0
.fd_cdir
= rootvnode
;
493 VOP_UNLOCK(rootvnode
, 0, p
);
495 if (mountroot
== netboot_mountroot
) {
497 /* post mount setup */
498 if (err
= netboot_setup(p
)) {
499 panic("bsd_init: NetBoot could not find root, %d", err
);
505 * Now can look at time, having had a chance to verify the time
506 * from the file system. Reset p->p_rtime as it may have been
507 * munched in mi_switch() after the time got set.
509 p
->p_stats
->p_start
= boottime
= time
;
510 p
->p_rtime
.tv_sec
= p
->p_rtime
.tv_usec
= 0;
514 extern void devfs_kernel_mount(char * str
);
516 devfs_kernel_mount("/dev");
520 /* Initialize signal state for process 0. */
523 bsd_utaskbootstrap();
525 /* invoke post-root-mount hook */
526 if (mountroot_post_hook
!= NULL
)
527 mountroot_post_hook();
529 (void) thread_funnel_set(kernel_flock
, funnel_state
);
532 /* Called with kernel funnel held */
536 struct proc
*p
= current_proc();
540 shared_region_mapping_t system_region
;
542 proc_name("init", p
);
546 th_act
= current_act();
547 (void) host_set_exception_ports(host_priv_self(),
548 EXC_MASK_ALL
& ~(EXC_MASK_SYSCALL
|
549 EXC_MASK_MACH_SYSCALL
|
552 EXCEPTION_DEFAULT
, 0);
554 (void) task_set_exception_ports(get_threadtask(th_act
),
555 EXC_MASK_ALL
& ~(EXC_MASK_SYSCALL
|
556 EXC_MASK_MACH_SYSCALL
|
559 EXCEPTION_DEFAULT
, 0);
564 ut
= (uthread_t
)get_bsdthread_info(th_act
);
565 ut
->uu_ar0
= (void *)get_user_regs(th_act
);
567 bsd_hardclockinit
= 1; /* Start bsd hardclock */
568 bsd_init_task
= get_threadtask(th_act
);
569 init_task_failure_data
[0] = 0;
570 system_region
= lookup_default_shared_region(ENV_DEFAULT_ROOT
,
571 machine_slot
[cpu_number()].cpu_type
);
572 if (system_region
== NULL
) {
573 shared_file_boot_time_init(ENV_DEFAULT_ROOT
,
574 machine_slot
[cpu_number()].cpu_type
);
576 vm_set_shared_region(get_threadtask(th_act
), system_region
);
578 load_init_program(p
);
579 /* turn on app-profiling i.e. pre-heating */
586 boolean_t funnel_state
;
587 extern void klogwakeup(void);
589 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
591 thread_wakeup(&lbolt
);
592 timeout(lightning_bolt
,0,hz
);
595 (void) thread_funnel_set(kernel_flock
, FALSE
);
600 extern kern_return_t
IOKitBSDInit( void );
605 * Early startup for bsd pseudodevices.
608 struct pseudo_init
*pi
;
610 for (pi
= pseudo_inits
; pi
->ps_func
; pi
++)
611 (*pi
->ps_func
) (pi
->ps_count
);
614 return( IOKitBSDInit());
618 #include <sys/disklabel.h> /* for MAXPARTITIONS */
622 extern kern_return_t
IOFindBSDRoot( char * rootName
,
623 dev_t
* root
, u_int32_t
* flags
);
628 * calls into IOKit can generate networking registrations
629 * which needs to be under network funnel. Right thing to do
630 * here is to drop the funnel alltogether and regrab it afterwards
632 thread_funnel_set(kernel_flock
, FALSE
);
633 err
= IOFindBSDRoot( rootdevice
, &rootdev
, &flags
);
634 thread_funnel_set(kernel_flock
, TRUE
);
636 printf("setconf: IOFindBSDRoot returned an error (%d);"
637 "setting rootdevice to 'sd0a'.\n", err
); /* XXX DEBUG TEMP */
638 rootdev
= makedev( 6, 0 );
639 strcpy( rootdevice
, "sd0a" );
645 mountroot
= netboot_mountroot
;
647 /* otherwise have vfs determine root filesystem */
658 th_act
= cloneproc(kernproc
, 0);
660 /* Set the launch time for init */
661 initproc
->p_stats
->p_start
= time
;
663 ut
= (struct uthread
*)get_bsdthread_info(th_act
);
665 act_set_astbsd(th_act
);
666 (void) thread_resume(th_act
);
671 extern char init_args
[];
673 extern int boothowto
;
679 if (PE_parse_boot_arg("-s", namep
)) {
680 boothowto
|= RB_SINGLE
;
681 len
= strlen(init_args
);
683 strcat(init_args
," -s");
685 strcat(init_args
,"-s");
688 if (PE_parse_boot_arg("-b", namep
)) {
689 boothowto
|= RB_NOBOOTRC
;
690 len
= strlen(init_args
);
692 strcat(init_args
," -b");
694 strcat(init_args
,"-b");
697 if (PE_parse_boot_arg("-F", namep
)) {
698 len
= strlen(init_args
);
700 strcat(init_args
," -F");
702 strcat(init_args
,"-F");
705 if (PE_parse_boot_arg("-v", namep
)) {
706 len
= strlen(init_args
);
708 strcat(init_args
," -v");
710 strcat(init_args
,"-v");
713 if (PE_parse_boot_arg("-x", namep
)) { /* safe boot */
714 len
= strlen(init_args
);
716 strcat(init_args
," -x");
718 strcat(init_args
,"-x");
721 if (PE_parse_boot_arg("-d", namep
)) {
722 len
= strlen(init_args
);
724 strcat(init_args
," -d");
726 strcat(init_args
,"-d");
729 PE_parse_boot_arg("srv", &srv
);
730 PE_parse_boot_arg("ncl", &ncl
);
731 PE_parse_boot_arg("nbuf", &nbuf
);
737 thread_funnel_switch(
741 boolean_t funnel_state_prev
;
746 funnel_t
* exist_funnel
;
747 extern int disable_funnel
;
753 if(oldfnl
== newfnl
) {
754 panic("thread_funnel_switch: can't switch to same funnel");
757 if ((oldfnl
!= NETWORK_FUNNEL
) && (oldfnl
!= KERNEL_FUNNEL
)) {
758 panic("thread_funnel_switch: invalid oldfunnel");
760 if ((newfnl
!= NETWORK_FUNNEL
) && (newfnl
!= KERNEL_FUNNEL
)) {
761 panic("thread_funnel_switch: invalid newfunnel");
764 if((curflock
= thread_funnel_get()) == THR_FUNNEL_NULL
) {
765 panic("thread_funnel_switch: no funnel held");
768 if ((oldfnl
== NETWORK_FUNNEL
) && (curflock
!= network_flock
))
769 panic("thread_funnel_switch: network funnel not held");
771 if ((oldfnl
== KERNEL_FUNNEL
) && (curflock
!= kernel_flock
))
772 panic("thread_funnel_switch: kernel funnel not held");
774 if(oldfnl
== NETWORK_FUNNEL
) {
775 oldflock
= network_flock
;
776 newflock
= kernel_flock
;
778 oldflock
= kernel_flock
;
779 newflock
= network_flock
;
781 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
, oldflock
, 1, 0, 0, 0);
782 thread_funnel_set(oldflock
, FALSE
);
783 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
, newflock
, 1, 0, 0, 0);
784 thread_funnel_set(newflock
, TRUE
);
785 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
, newflock
, 1, 0, 0, 0);