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