2 * Copyright (c) 2000 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.
71 * 16-Apr-98 A. Ramesh at Apple
72 * Created for Apple Core from DR2 init_main.c.
77 #include <sys/param.h>
78 #include <sys/filedesc.h>
79 #include <sys/kernel.h>
80 #include <sys/mount.h>
82 #include <sys/systm.h>
83 #include <sys/vnode.h>
86 #include <sys/clist.h>
88 #include <ufs/ufs/quota.h>
90 #include <sys/malloc.h>
91 #include <sys/dkstat.h>
93 #include <machine/spl.h>
94 #include <kern/thread.h>
95 #include <kern/task.h>
98 #include <mach/vm_param.h>
100 #include <vm/vm_map.h>
101 #include <vm/vm_kern.h>
103 #include <sys/ux_exception.h>
105 #include <sys/reboot.h>
106 #include <mach/exception_types.h>
107 #include <dev/busvar.h>
108 #include <sys/kdebug.h>
110 #include <mach/mach_types.h>
111 #include <mach/vm_prot.h>
112 #include <mach/semaphore.h>
113 #include <mach/sync_policy.h>
114 #include <kern/clock.h>
115 #include <mach/kern_return.h>
117 extern shared_region_mapping_t system_shared_region
;
120 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California. All rights reserved.\n\n";
122 extern void ux_handler();
124 /* Components of the first process -- never freed. */
126 struct session session0
;
129 struct filedesc filedesc0
;
130 struct plimit limit0
;
131 struct pstats pstats0
;
132 struct sigacts sigacts0
;
133 struct proc
*kernproc
, *initproc
;
136 long cp_time
[CPUSTATES
];
137 long dk_seek
[DK_NDRIVE
];
138 long dk_time
[DK_NDRIVE
];
139 long dk_wds
[DK_NDRIVE
];
140 long dk_wpms
[DK_NDRIVE
];
141 long dk_xfer
[DK_NDRIVE
];
142 long dk_bps
[DK_NDRIVE
];
152 /* Global variables to make pstat happy. We do swapping differently */
156 struct swdevt swdevt
[1];
158 dev_t rootdev
; /* device of the root */
159 dev_t dumpdev
; /* device to take dumps on */
160 long dumplo
; /* offset into dumpdev */
162 char hostname
[MAXHOSTNAMELEN
];
164 char domainname
[MAXDOMNAMELEN
];
167 char rootdevice
[16]; /* hfs device names have at least 9 chars */
168 struct timeval boottime
; /* GRODY! This has to go... */
174 struct kmemstats kmemstats
[M_LAST
];
177 int lbolt
; /* awoken once a second */
178 struct vnode
*rootvp
;
179 int boothowto
= RB_DEBUG
;
181 #define BSD_PAGABLE_MAP_SIZE (4 * 512 * 1024)
182 vm_map_t bsd_pageable_map
;
184 semaphore_t execve_semaphore
;
188 int parse_bsd_args(void);
189 extern int bsd_hardclockinit
;
190 extern vm_address_t bsd_init_task
;
191 extern char init_task_failure_data
[];
193 funnel_t
* kernel_flock
;
194 funnel_t
* network_flock
;
195 int disable_funnel
= 0; /* disables split funnel */
196 int enable_funnel
= 0; /* disables split funnel */
199 * Initialization code.
200 * Called from cold start routine as
201 * soon as a stack and segmentation
202 * have been established.
204 * clear and free user core
206 * hand craft 0th process
207 * call all initialization routines
208 * fork - process 0 to schedule
209 * - process 1 execute bootstrap
210 * - process 2 to page out
214 * Sets the name for the given task.
221 int length
= strlen(s
);
224 length
>= sizeof(p
->p_comm
) ? sizeof(p
->p_comm
) :
229 /* To allow these values to be patched, they're globals here */
230 #include <machine/vmparam.h>
231 struct rlimit vm_initial_limit_stack
= { DFLSSIZ
, MAXSSIZ
};
232 struct rlimit vm_initial_limit_data
= { DFLDSIZ
, MAXDSIZ
};
233 struct rlimit vm_initial_limit_core
= { DFLCSIZ
, MAXCSIZ
};
235 extern thread_t first_thread
;
239 #define dprintf(x) printf x
242 #endif /* SPL_DEBUG */
244 extern thread_t
cloneproc(struct proc
*, int);
249 register struct proc
*p
;
250 extern struct ucred
*rootcred
;
254 extern void bsdinit_task();
255 void lightning_bolt(void );
257 boolean_t funnel_state
;
258 extern void uthread_zone_init();
260 extern int (*mountroot
) __P((void));
264 /* split funnel is enabled by default */
265 PE_parse_boot_arg("dfnl", &disable_funnel
);
267 /* split funnel is disabled befault */
269 PE_parse_boot_arg("efnl", &enable_funnel
);
271 /* enable only if efnl is set in bootarg */
276 kernel_flock
= funnel_alloc(KERNEL_FUNNEL
);
277 if (kernel_flock
== (funnel_t
*)0 ) {
278 panic("bsd_init: Fail to allocate kernel mutex lock");
282 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
284 if (!disable_funnel
) {
285 network_flock
= funnel_alloc(NETWORK_FUNNEL
);
286 if (network_flock
== (funnel_t
*)0 ) {
287 panic("bds_init: Fail to allocate network mutex lock");
290 network_flock
= kernel_flock
;
302 /* Initialize the uthread zone */
306 * Initialize process and pgrp structures.
314 /* kernel_task->proc = kernproc; */
315 set_bsdtask_info(kernel_task
,(void *)kernproc
);
318 /* give kernproc a name */
319 proc_name("kernel_task", p
);
321 if (current_task() != kernel_task
)
322 printf("We are in for problem, current task in not kernel task\n");
327 LIST_INSERT_HEAD(&allproc
, p
, p_list
);
329 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0
, pg_hash
);
330 LIST_INIT(&pgrp0
.pg_members
);
331 LIST_INSERT_HEAD(&pgrp0
.pg_members
, p
, p_pglist
);
333 pgrp0
.pg_session
= &session0
;
334 session0
.s_count
= 1;
335 session0
.s_leader
= p
;
337 p
->task
= kernel_task
;
340 p
->p_flag
= P_INMEM
|P_SYSTEM
;
343 lockinit(&p
->signal_lock
, PVM
, "signal", 0, 0);
345 p
->sigwait_thread
= THREAD_NULL
;
346 p
->exit_thread
= THREAD_NULL
;
348 /* Create credentials. */
349 lockinit(&cred0
.pc_lock
, PLOCK
, "proc0 cred", 0, 0);
352 p
->p_ucred
= crget();
353 p
->p_ucred
->cr_ngroups
= 1; /* group 0 */
355 /* Create the file descriptor table. */
356 filedesc0
.fd_refcnt
= 1+1; /* +1 so shutdown will not _FREE_ZONE */
357 p
->p_fd
= &filedesc0
;
358 filedesc0
.fd_cmask
= cmask
;
360 /* Create the limits structures. */
361 p
->p_limit
= &limit0
;
362 for (i
= 0; i
< sizeof(p
->p_rlimit
)/sizeof(p
->p_rlimit
[0]); i
++)
363 limit0
.pl_rlimit
[i
].rlim_cur
=
364 limit0
.pl_rlimit
[i
].rlim_max
= RLIM_INFINITY
;
365 limit0
.pl_rlimit
[RLIMIT_NOFILE
].rlim_cur
= NOFILE
;
366 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_cur
= MAXUPRC
;
367 limit0
.pl_rlimit
[RLIMIT_STACK
] = vm_initial_limit_stack
;
368 limit0
.pl_rlimit
[RLIMIT_DATA
] = vm_initial_limit_data
;
369 limit0
.pl_rlimit
[RLIMIT_CORE
] = vm_initial_limit_core
;
372 p
->p_stats
= &pstats0
;
373 p
->p_sigacts
= &sigacts0
;
376 * Charge root for one process.
378 (void)chgproccnt(0, 1);
382 * Allocate a kernel submap for pageable memory
383 * for temporary copying (execve()).
388 ret
= kmem_suballoc(kernel_map
,
390 (vm_size_t
)BSD_PAGABLE_MAP_SIZE
,
394 if (ret
!= KERN_SUCCESS
)
395 panic("bsd_init: Failed to allocare bsd pageable map");
398 /* Initialize the execve() semaphore */
403 value
= BSD_PAGABLE_MAP_SIZE
/ NCARGS
;
405 kret
= semaphore_create(kernel_task
, &execve_semaphore
,
406 SYNC_POLICY_FIFO
, value
);
407 if (kret
!= KERN_SUCCESS
)
408 panic("bsd_init: Failed to create execve semaphore");
412 * Initialize the calendar.
418 /* Initialize the file systems. */
421 /* Initialize mbuf's. */
424 /* Initialize syslog */
427 /* Initialize SysV shm */
430 /* POSIX Shm and Sem */
435 * Initialize protocols. Block reception of incoming packets
436 * until everything is ready.
439 sysctl_register_fixed();
446 * Create kernel idle cpu processes. This must be done
447 * before a context switch can occur (and hence I/O can
448 * happen in the binit() call).
450 p
->p_fd
->fd_cdir
= NULL
;
451 p
->p_fd
->fd_rdir
= NULL
;
455 /* Initialize kernel profiling. */
459 /* kick off timeout driven events by calling first time */
460 thread_wakeup(&lbolt
);
461 timeout(lightning_bolt
,0,hz
);
466 * We attach the loopback interface *way* down here to ensure
467 * it happens after autoconf(), otherwise it becomes the
468 * "primary" interface.
472 loopattach(); /* XXX */
475 vnode_pager_bootstrap();
477 /* Mount the root file system. */
483 * read the time after clock_initialize_calendar()
484 * and before nfs mount
488 if (0 == (err
= vfs_mountroot()))
490 printf("cannot mount root, errno = %d\n", err
);
491 boothowto
|= RB_ASKNAME
;
494 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
496 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
497 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
498 panic("bsd_init: cannot find root vnode");
499 filedesc0
.fd_cdir
= rootvnode
;
501 VOP_UNLOCK(rootvnode
, 0, p
);
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 /* printf("Launching user process\n"); */
525 bsd_utaskbootstrap();
527 (void) thread_funnel_set(kernel_flock
, FALSE
);
533 struct proc
*p
= current_proc();
537 boolean_t funnel_state
;
539 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
543 ipc_port_t master_bootstrap_port
;
544 task_t bootstrap_task
;
545 thread_act_t bootstrap_thr_act
;
546 ipc_port_t root_device_port
;
548 master_bootstrap_port
= ipc_port_alloc_kernel();
549 if (master_bootstrap_port
== IP_NULL
)
550 panic("can't allocate master bootstrap port");
551 printf("setting bootstrap port \n");
552 task_set_special_port(bootstrap_task
,
554 ipc_port_make_send(master_bootstrap_port
));
556 printf("Setting exception port for the init task\n");
557 (void) task_set_exception_ports(get_threadtask(th
),
560 EXC_MASK_MACH_SYSCALL
| EXC_MASK_RPC_ALERT
),
562 EXCEPTION_DEFAULT
, 0);
565 proc_name("init", p
);
568 /* port_reference(ux_exception_port);*/
570 th_act
= current_act();
571 (void) host_set_exception_ports(host_priv_self(),
572 EXC_MASK_ALL
& ~(EXC_MASK_SYSCALL
|
573 EXC_MASK_MACH_SYSCALL
|
576 EXCEPTION_DEFAULT
, 0);
578 (void) task_set_exception_ports(get_threadtask(th_act
),
579 EXC_MASK_ALL
& ~(EXC_MASK_SYSCALL
|
580 EXC_MASK_MACH_SYSCALL
|
583 EXCEPTION_DEFAULT
, 0);
588 ut
= (uthread_t
)get_bsdthread_info(th_act
);
589 ut
->uu_ar0
= (void *)get_user_regs(th_act
);
591 bsd_hardclockinit
= 1; /* Start bsd hardclock */
592 bsd_init_task
= get_threadtask(th_act
);
593 init_task_failure_data
[0] = 0;
594 vm_set_shared_region(get_threadtask(th_act
), system_shared_region
);
595 load_init_program(p
);
597 (void) thread_funnel_set(kernel_flock
, FALSE
);
604 boolean_t funnel_state
;
605 extern void klogwakeup(void);
607 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
609 thread_wakeup(&lbolt
);
610 timeout(lightning_bolt
,0,hz
);
613 (void) thread_funnel_set(kernel_flock
, FALSE
);
617 extern kern_return_t
IOKitBSDInit( void );
622 * Early startup for bsd pseudodevices.
625 struct pseudo_init
*pi
;
627 for (pi
= pseudo_inits
; pi
->ps_func
; pi
++)
628 (*pi
->ps_func
) (pi
->ps_count
);
631 return( IOKitBSDInit());
635 #include <sys/disklabel.h> // for MAXPARTITIONS
639 extern kern_return_t
IOFindBSDRoot( char * rootName
,
640 dev_t
* root
, u_int32_t
* flags
);
642 extern int (*mountroot
) __P((void));
643 extern int nfs_mountroot(); /* nfs_vfsops.c */
649 * calls into IOKit can generate networking registrations
650 * which needs to be under network funnel. Right thing to do
651 * here is to drop the funnel alltogether and regrab it afterwards
653 thread_funnel_set(kernel_flock
, FALSE
);
654 err
= IOFindBSDRoot( rootdevice
, &rootdev
, &flags
);
655 thread_funnel_set(kernel_flock
, TRUE
);
657 printf("setconf: IOFindBSDRoot returned an error (%d);"
658 "setting rootdevice to 'sd0a'.\n", err
); /* XXX DEBUG TEMP */
659 rootdev
= makedev( 6, 0 );
660 strcpy( rootdevice
, "sd0a" );
664 /* if network device then force nfs root */
666 printf("mounting nfs root\n");
667 mountroot
= nfs_mountroot
;
669 /* otherwise have vfs determine root filesystem */
679 th_act
= (thread_act_t
)cloneproc(kernproc
, 0);
682 (void) thread_stop_wait(getshuttle_thread(th_act
));
683 thread_ast_set(th_act
,AST_BSD_INIT
);
684 thread_release(th_act
);
685 thread_unstop(getshuttle_thread(th_act
));
686 (void) thread_resume(th_act
);
691 extern char init_args
[];
693 extern int boothowto
;
699 if (PE_parse_boot_arg("-s", namep
)) {
700 boothowto
|= RB_SINGLE
;
701 len
= strlen(init_args
);
703 strcat(init_args
," -s");
705 strcat(init_args
,"-s");
707 if (PE_parse_boot_arg("-b", namep
)) {
708 boothowto
|= RB_NOBOOTRC
;
709 len
= strlen(init_args
);
711 strcat(init_args
," -b");
713 strcat(init_args
,"-b");
716 if (PE_parse_boot_arg("-F", namep
)) {
717 len
= strlen(init_args
);
719 strcat(init_args
," -F");
721 strcat(init_args
,"-F");
724 if (PE_parse_boot_arg("-v", namep
)) {
725 len
= strlen(init_args
);
727 strcat(init_args
," -v");
729 strcat(init_args
,"-v");
732 if (PE_parse_boot_arg("-x", namep
)) { /* safe boot */
733 len
= strlen(init_args
);
735 strcat(init_args
," -x");
737 strcat(init_args
,"-x");
740 PE_parse_boot_arg("srv", &srv
);
741 PE_parse_boot_arg("ncl", &ncl
);
742 PE_parse_boot_arg("nbuf", &nbuf
);
748 thread_funnel_switch(
753 boolean_t funnel_state_prev
;
758 funnel_t
* exist_funnel
;
759 extern int disable_funnel
;
765 if(oldfnl
== newfnl
) {
766 panic("thread_funnel_switch: can't switch to same funnel");
769 if ((oldfnl
!= NETWORK_FUNNEL
) && (oldfnl
!= KERNEL_FUNNEL
))
771 panic("thread_funnel_switch: invalid oldfunnel");
773 if ((newfnl
!= NETWORK_FUNNEL
) && (newfnl
!= KERNEL_FUNNEL
))
775 panic("thread_funnel_switch: invalid oldfunnel");
778 if((curflock
= thread_funnel_get()) == THR_FUNNEL_NULL
) {
779 panic("thread_funnel_switch: no funnel held");
782 cur_thread
= current_thread();
784 if ((oldfnl
== NETWORK_FUNNEL
) && (curflock
!= network_flock
))
785 panic("thread_funnel_switch: network funnel not held");
787 if ((oldfnl
== KERNEL_FUNNEL
) && (curflock
!= kernel_flock
))
788 panic("thread_funnel_switch: network funnel not held");
790 if(oldfnl
== NETWORK_FUNNEL
) {
791 oldflock
= network_flock
;
792 newflock
= kernel_flock
;
794 oldflock
= kernel_flock
;
795 newflock
= network_flock
;
797 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
, oldflock
, 1, 0, 0, 0);
798 thread_funnel_set(oldflock
, FALSE
);
799 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
, newflock
, 1, 0, 0, 0);
800 thread_funnel_set(newflock
, TRUE
);
801 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
, newflock
, 1, 0, 0, 0);