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 extern shared_region_mapping_t system_shared_region
;
113 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California. All rights reserved.\n\n";
115 extern void ux_handler();
117 /* Components of the first process -- never freed. */
119 struct session session0
;
122 struct filedesc filedesc0
;
123 struct plimit limit0
;
124 struct pstats pstats0
;
125 struct sigacts sigacts0
;
126 struct proc
*kernproc
, *initproc
;
129 long cp_time
[CPUSTATES
];
130 long dk_seek
[DK_NDRIVE
];
131 long dk_time
[DK_NDRIVE
];
132 long dk_wds
[DK_NDRIVE
];
133 long dk_wpms
[DK_NDRIVE
];
134 long dk_xfer
[DK_NDRIVE
];
135 long dk_bps
[DK_NDRIVE
];
145 /* Global variables to make pstat happy. We do swapping differently */
149 struct swdevt swdevt
[1];
151 dev_t rootdev
; /* device of the root */
152 dev_t dumpdev
; /* device to take dumps on */
153 long dumplo
; /* offset into dumpdev */
155 char hostname
[MAXHOSTNAMELEN
];
157 char domainname
[MAXDOMNAMELEN
];
160 char rootdevice
[16]; /* hfs device names have at least 9 chars */
161 struct timeval boottime
; /* GRODY! This has to go... */
167 struct kmemstats kmemstats
[M_LAST
];
170 int lbolt
; /* awoken once a second */
171 struct vnode
*rootvp
;
172 int boothowto
= RB_DEBUG
;
174 vm_map_t bsd_pageable_map
;
179 int parse_bsd_args(void);
180 extern int bsd_hardclockinit
;
181 extern vm_address_t bsd_init_task
;
182 extern char init_task_failure_data
[];
184 funnel_t
* kernel_flock
;
185 funnel_t
* network_flock
;
186 int disable_funnel
= 0; /* disables split funnel */
187 int enable_funnel
= 0; /* disables split funnel */
190 * Initialization code.
191 * Called from cold start routine as
192 * soon as a stack and segmentation
193 * have been established.
195 * clear and free user core
197 * hand craft 0th process
198 * call all initialization routines
199 * fork - process 0 to schedule
200 * - process 1 execute bootstrap
201 * - process 2 to page out
205 * Sets the name for the given task.
211 int length
= strlen(s
);
214 length
>= sizeof(p
->p_comm
) ? sizeof(p
->p_comm
) :
219 /* To allow these values to be patched, they're globals here */
220 #include <machine/vmparam.h>
221 struct rlimit vm_initial_limit_stack
= { DFLSSIZ
, MAXSSIZ
};
222 struct rlimit vm_initial_limit_data
= { DFLDSIZ
, MAXDSIZ
};
223 struct rlimit vm_initial_limit_core
= { DFLCSIZ
, MAXCSIZ
};
225 extern thread_t first_thread
;
229 #define dprintf(x) printf x
232 #endif /* SPL_DEBUG */
234 extern thread_t
cloneproc(struct proc
*, int);
239 register struct proc
*p
;
240 extern struct ucred
*rootcred
;
244 extern void bsdinit_task();
245 void lightning_bolt(void );
247 boolean_t funnel_state
;
248 extern void uthread_zone_init();
250 extern int (*mountroot
) __P((void));
254 /* split funnel is enabled by default */
255 PE_parse_boot_arg("dfnl", &disable_funnel
);
257 /* split funnel is disabled befault */
259 PE_parse_boot_arg("efnl", &enable_funnel
);
261 /* enable only if efnl is set in bootarg */
266 kernel_flock
= funnel_alloc(KERNEL_FUNNEL
);
267 if (kernel_flock
== (funnel_t
*)0 ) {
268 panic("fail to allocate kernel mutex lock\n");
272 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
274 if (!disable_funnel
) {
275 network_flock
= funnel_alloc(NETWORK_FUNNEL
);
276 if (network_flock
== (funnel_t
*)0 ) {
277 panic("fail to allocate network mutex lock\n");
280 network_flock
= kernel_flock
;
292 /* Initialize the uthread zone */
296 * Initialize process and pgrp structures.
304 /* kernel_task->proc = kernproc; */
305 set_bsdtask_info(kernel_task
,(void *)kernproc
);
308 /* give kernproc a name */
309 proc_name("kernel_task", p
);
311 if (current_task() != kernel_task
)
312 printf("We are in for problem, current task in not kernel task\n");
317 LIST_INSERT_HEAD(&allproc
, p
, p_list
);
319 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0
, pg_hash
);
320 LIST_INIT(&pgrp0
.pg_members
);
321 LIST_INSERT_HEAD(&pgrp0
.pg_members
, p
, p_pglist
);
323 pgrp0
.pg_session
= &session0
;
324 session0
.s_count
= 1;
325 session0
.s_leader
= p
;
327 p
->task
= kernel_task
;
330 p
->p_flag
= P_INMEM
|P_SYSTEM
;
333 lockinit(&p
->signal_lock
, PVM
, "signal", 0, 0);
335 p
->sigwait_thread
= THREAD_NULL
;
336 p
->exit_thread
= THREAD_NULL
;
338 /* Create credentials. */
339 lockinit(&cred0
.pc_lock
, PLOCK
, "proc0 cred", 0, 0);
342 p
->p_ucred
= crget();
343 p
->p_ucred
->cr_ngroups
= 1; /* group 0 */
345 /* Create the file descriptor table. */
346 filedesc0
.fd_refcnt
= 1+1; /* +1 so shutdown will not _FREE_ZONE */
347 p
->p_fd
= &filedesc0
;
348 filedesc0
.fd_cmask
= cmask
;
350 /* Create the limits structures. */
351 p
->p_limit
= &limit0
;
352 for (i
= 0; i
< sizeof(p
->p_rlimit
)/sizeof(p
->p_rlimit
[0]); i
++)
353 limit0
.pl_rlimit
[i
].rlim_cur
=
354 limit0
.pl_rlimit
[i
].rlim_max
= RLIM_INFINITY
;
355 limit0
.pl_rlimit
[RLIMIT_NOFILE
].rlim_cur
= NOFILE
;
356 limit0
.pl_rlimit
[RLIMIT_NPROC
].rlim_cur
= MAXUPRC
;
357 limit0
.pl_rlimit
[RLIMIT_STACK
] = vm_initial_limit_stack
;
358 limit0
.pl_rlimit
[RLIMIT_DATA
] = vm_initial_limit_data
;
359 limit0
.pl_rlimit
[RLIMIT_CORE
] = vm_initial_limit_core
;
362 p
->p_stats
= &pstats0
;
363 p
->p_sigacts
= &sigacts0
;
366 * Charge root for one process.
368 (void)chgproccnt(0, 1);
372 * Allocate a kernel submap for pageable memory
373 * for temporary copying (table(), execve()).
378 ret
= kmem_suballoc(kernel_map
,
384 if (ret
!= KERN_SUCCESS
)
385 panic("Failed to allocare bsd pageable map\n");
389 * Initialize the calendar by
390 * reading the BBC, if not already set.
396 /* Initialize the file systems. */
399 /* Initialize mbuf's. */
402 /* Initialize syslog */
405 /* Initialize SysV shm */
408 /* POSIX Shm and Sem */
413 * Initialize protocols. Block reception of incoming packets
414 * until everything is ready.
417 sysctl_register_fixed();
424 * Create kernel idle cpu processes. This must be done
425 * before a context switch can occur (and hence I/O can
426 * happen in the binit() call).
428 p
->p_fd
->fd_cdir
= NULL
;
429 p
->p_fd
->fd_rdir
= NULL
;
433 /* Initialize kernel profiling. */
437 /* kick off timeout driven events by calling first time */
438 thread_wakeup(&lbolt
);
439 timeout(lightning_bolt
,0,hz
);
444 * We attach the loopback interface *way* down here to ensure
445 * it happens after autoconf(), otherwise it becomes the
446 * "primary" interface.
450 loopattach(); /* XXX */
453 vnode_pager_bootstrap();
455 /* Mount the root file system. */
461 * read the time after clock_initialize_calendar()
462 * and before nfs mount
466 if (0 == (err
= vfs_mountroot()))
468 printf("cannot mount root, errno = %d\n", err
);
469 boothowto
|= RB_ASKNAME
;
472 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
474 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
475 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
476 panic("cannot find root vnode");
477 filedesc0
.fd_cdir
= rootvnode
;
479 VOP_UNLOCK(rootvnode
, 0, p
);
483 * Now can look at time, having had a chance to verify the time
484 * from the file system. Reset p->p_rtime as it may have been
485 * munched in mi_switch() after the time got set.
487 p
->p_stats
->p_start
= boottime
= time
;
488 p
->p_rtime
.tv_sec
= p
->p_rtime
.tv_usec
= 0;
492 extern void devfs_kernel_mount(char * str
);
494 devfs_kernel_mount("/dev");
498 /* Initialize signal state for process 0. */
501 /* printf("Launching user process\n"); */
503 bsd_utaskbootstrap();
505 (void) thread_funnel_set(kernel_flock
, FALSE
);
511 struct proc
*p
= current_proc();
515 boolean_t funnel_state
;
517 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
521 ipc_port_t master_bootstrap_port
;
522 task_t bootstrap_task
;
523 thread_act_t bootstrap_thr_act
;
524 ipc_port_t root_device_port
;
526 master_bootstrap_port
= ipc_port_alloc_kernel();
527 if (master_bootstrap_port
== IP_NULL
)
528 panic("can't allocate master bootstrap port");
529 printf("setting bootstrap port \n");
530 task_set_special_port(bootstrap_task
,
532 ipc_port_make_send(master_bootstrap_port
));
534 printf("Setting exception port for the init task\n");
535 (void) task_set_exception_ports(get_threadtask(th
),
538 EXC_MASK_MACH_SYSCALL
| EXC_MASK_RPC_ALERT
),
540 EXCEPTION_DEFAULT
, 0);
543 proc_name("init", p
);
546 /* port_reference(ux_exception_port);*/
548 th_act
= current_act();
549 (void) host_set_exception_ports(host_priv_self(),
550 EXC_MASK_ALL
& ~(EXC_MASK_SYSCALL
|
551 EXC_MASK_MACH_SYSCALL
|
554 EXCEPTION_DEFAULT
, 0);
556 (void) task_set_exception_ports(get_threadtask(th_act
),
557 EXC_MASK_ALL
& ~(EXC_MASK_SYSCALL
|
558 EXC_MASK_MACH_SYSCALL
|
561 EXCEPTION_DEFAULT
, 0);
566 ut
= (uthread_t
)get_bsdthread_info(th_act
);
567 ut
->uu_ar0
= (void *)get_user_regs(th_act
);
569 bsd_hardclockinit
= 1; /* Start bsd hardclock */
570 bsd_init_task
= get_threadtask(th_act
);
571 init_task_failure_data
[0] = 0;
572 vm_set_shared_region(get_threadtask(th_act
), system_shared_region
);
573 load_init_program(p
);
575 (void) thread_funnel_set(kernel_flock
, FALSE
);
582 boolean_t funnel_state
;
583 extern void klogwakeup(void);
585 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
587 thread_wakeup(&lbolt
);
588 timeout(lightning_bolt
,0,hz
);
591 (void) thread_funnel_set(kernel_flock
, FALSE
);
595 extern kern_return_t
IOKitBSDInit( void );
600 * Early startup for bsd pseudodevices.
603 struct pseudo_init
*pi
;
605 for (pi
= pseudo_inits
; pi
->ps_func
; pi
++)
606 (*pi
->ps_func
) (pi
->ps_count
);
609 return( IOKitBSDInit());
613 #include <sys/disklabel.h> // for MAXPARTITIONS
617 extern kern_return_t
IOFindBSDRoot( char * rootName
,
618 dev_t
* root
, u_int32_t
* flags
);
620 extern int (*mountroot
) __P((void));
621 extern int nfs_mountroot(); /* nfs_vfsops.c */
627 * calls into IOKit can generate networking registrations
628 * which needs to be under network funnel. Right thing to do
629 * here is to drop the funnel alltogether and regrab it afterwards
631 thread_funnel_set(kernel_flock
, FALSE
);
632 err
= IOFindBSDRoot( rootdevice
, &rootdev
, &flags
);
633 thread_funnel_set(kernel_flock
, TRUE
);
635 printf("setconf: IOFindBSDRoot returned an error (%d);"
636 "setting rootdevice to 'sd0a'.\n", err
); /* XXX DEBUG TEMP */
637 rootdev
= makedev( 6, 0 );
638 strcpy( rootdevice
, "sd0a" );
642 /* if network device then force nfs root */
644 printf("mounting nfs root\n");
645 mountroot
= nfs_mountroot
;
647 /* otherwise have vfs determine root filesystem */
657 th_act
= (thread_act_t
)cloneproc(kernproc
, 0);
660 (void) thread_stop_wait(getshuttle_thread(th_act
));
661 thread_ast_set(th_act
,AST_BSD_INIT
);
662 thread_release(th_act
);
663 thread_unstop(getshuttle_thread(th_act
));
664 (void) thread_resume(th_act
);
669 extern char init_args
[];
671 extern int boothowto
;
677 if (PE_parse_boot_arg("-s", namep
)) {
678 boothowto
|= RB_SINGLE
;
679 len
= strlen(init_args
);
681 strcat(init_args
," -s");
683 strcat(init_args
,"-s");
685 if (PE_parse_boot_arg("-b", namep
)) {
686 boothowto
|= RB_NOBOOTRC
;
687 len
= strlen(init_args
);
689 strcat(init_args
," -b");
691 strcat(init_args
,"-b");
694 if (PE_parse_boot_arg("-F", namep
)) {
695 len
= strlen(init_args
);
697 strcat(init_args
," -F");
699 strcat(init_args
,"-F");
702 if (PE_parse_boot_arg("-v", namep
)) {
703 len
= strlen(init_args
);
705 strcat(init_args
," -v");
707 strcat(init_args
,"-v");
710 if (PE_parse_boot_arg("-x", namep
)) { /* safe boot */
711 len
= strlen(init_args
);
713 strcat(init_args
," -x");
715 strcat(init_args
,"-x");
718 PE_parse_boot_arg("srv", &srv
);
719 PE_parse_boot_arg("ncl", &ncl
);
720 PE_parse_boot_arg("nbuf", &nbuf
);
726 thread_funnel_switch(
731 boolean_t funnel_state_prev
;
736 funnel_t
* exist_funnel
;
737 extern int disable_funnel
;
743 if(oldfnl
== newfnl
) {
744 panic("thread_funnel_switch: can't switch to same funnel");
747 if ((oldfnl
!= NETWORK_FUNNEL
) && (oldfnl
!= KERNEL_FUNNEL
))
749 panic("thread_funnel_switch: invalid oldfunnel");
751 if ((newfnl
!= NETWORK_FUNNEL
) && (newfnl
!= KERNEL_FUNNEL
))
753 panic("thread_funnel_switch: invalid oldfunnel");
756 if((curflock
= thread_funnel_get()) == THR_FUNNEL_NULL
) {
757 panic("thread_funnel_switch: no funnel held");
760 cur_thread
= current_thread();
762 if ((oldfnl
== NETWORK_FUNNEL
) && (curflock
!= network_flock
))
763 panic("thread_funnel_switch: network funnel not held");
765 if ((oldfnl
== KERNEL_FUNNEL
) && (curflock
!= kernel_flock
))
766 panic("thread_funnel_switch: network funnel not held");
768 if(oldfnl
== NETWORK_FUNNEL
) {
769 oldflock
= network_flock
;
770 newflock
= kernel_flock
;
772 oldflock
= kernel_flock
;
773 newflock
= network_flock
;
775 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
, oldflock
, 1, 0, 0, 0);
776 thread_funnel_set(oldflock
, FALSE
);
777 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
, newflock
, 1, 0, 0, 0);
778 thread_funnel_set(newflock
, TRUE
);
779 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
, newflock
, 1, 0, 0, 0);