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