]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
47290e3d8ac421c08349b48cc86e8b7036f573f4
2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
66 * Mach kernel startup.
70 #include <xpr_debug.h>
74 #include <mach/boolean.h>
75 #include <mach/machine.h>
76 #include <mach/thread_act.h>
77 #include <mach/task_special_ports.h>
78 #include <mach/vm_param.h>
79 #include <ipc/ipc_init.h>
80 #include <kern/assert.h>
81 #include <kern/mach_param.h>
82 #include <kern/misc_protos.h>
83 #include <kern/clock.h>
84 #include <kern/cpu_number.h>
85 #include <kern/ledger.h>
86 #include <kern/machine.h>
87 #include <kern/processor.h>
88 #include <kern/sched_prim.h>
89 #include <kern/startup.h>
90 #include <kern/task.h>
91 #include <kern/thread.h>
92 #include <kern/timer.h>
93 #include <kern/wait_queue.h>
95 #include <kern/zalloc.h>
96 #include <kern/locks.h>
97 #include <console/serial_protos.h>
98 #include <vm/vm_kern.h>
99 #include <vm/vm_init.h>
100 #include <vm/vm_map.h>
101 #include <vm/vm_object.h>
102 #include <vm/vm_page.h>
103 #include <vm/vm_pageout.h>
104 #include <vm/vm_shared_region.h>
105 #include <machine/pmap.h>
106 #include <machine/commpage.h>
107 #include <libkern/version.h>
108 #include <sys/kdebug.h>
115 #include <security/mac_mach_internal.h>
122 static void kernel_bootstrap_thread(void);
124 static void load_context(
126 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
127 extern void cpu_userwindow_init(int);
128 extern void cpu_physwindow_init(int);
131 // libkern/OSKextLib.cpp
132 extern void OSKextRemoveKextBootstrap(void);
134 void scale_setup(void);
135 extern void bsd_scale_setup(int);
136 extern unsigned int semaphore_max
;
139 * Running in virtual memory, on the interrupt stack.
142 extern int serverperfmode
;
144 /* size of kernel trace buffer, disabled by default */
145 unsigned int new_nkdbufs
= 0;
147 /* mach leak logging */
149 int turn_on_log_leaks
= 0;
153 kernel_early_bootstrap(void)
159 * Initialize the timer callout world
161 timer_call_initialize();
166 kernel_bootstrap(void)
168 kern_return_t result
;
172 printf("%s\n", version
); /* log kernel version */
174 #define kernel_bootstrap_kprintf(x...) /* kprintf("kernel_bootstrap: " x) */
176 if (PE_parse_boot_argn("-l", namep
, sizeof (namep
))) /* leaks logging */
177 turn_on_log_leaks
= 1;
179 PE_parse_boot_argn("trace", &new_nkdbufs
, sizeof (new_nkdbufs
));
181 /* i386_vm_init already checks for this ; do it aagin anyway */
182 if (PE_parse_boot_argn("serverperfmode", &serverperfmode
, sizeof (serverperfmode
))) {
187 kernel_bootstrap_kprintf("calling vm_mem_bootstrap\n");
190 kernel_bootstrap_kprintf("calling vm_mem_init\n");
193 machine_info
.memory_size
= (uint32_t)mem_size
;
194 machine_info
.max_mem
= max_mem
;
195 machine_info
.major_version
= version_major
;
196 machine_info
.minor_version
= version_minor
;
198 kernel_bootstrap_kprintf("calling sched_init\n");
201 kernel_bootstrap_kprintf("calling wait_queue_bootstrap\n");
202 wait_queue_bootstrap();
204 kernel_bootstrap_kprintf("calling ipc_bootstrap\n");
210 kernel_bootstrap_kprintf("calling ipc_init\n");
214 * As soon as the virtual memory system is up, we record
215 * that this CPU is using the kernel pmap.
217 kernel_bootstrap_kprintf("calling PMAP_ACTIVATE_KERNEL\n");
218 PMAP_ACTIVATE_KERNEL(master_cpu
);
220 kernel_bootstrap_kprintf("calling mapping_free_prime\n");
221 mapping_free_prime(); /* Load up with temporary mapping blocks */
223 kernel_bootstrap_kprintf("calling machine_init\n");
226 kernel_bootstrap_kprintf("calling clock_init\n");
231 * Initialize the IPC, task, and thread subsystems.
233 kernel_bootstrap_kprintf("calling ledger_init\n");
236 kernel_bootstrap_kprintf("calling task_init\n");
239 kernel_bootstrap_kprintf("calling thread_init\n");
243 * Create a kernel thread to execute the kernel bootstrap.
245 kernel_bootstrap_kprintf("calling kernel_thread_create\n");
246 result
= kernel_thread_create((thread_continue_t
)kernel_bootstrap_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
248 if (result
!= KERN_SUCCESS
) panic("kernel_bootstrap: result = %08X\n", result
);
250 thread
->state
= TH_RUN
;
251 thread_deallocate(thread
);
253 /* transfer statistics from init thread to kernel */
254 thread_t init_thread
= current_thread();
255 kernel_task
->tkm_private
.alloc
= init_thread
->tkm_private
.alloc
;
256 kernel_task
->tkm_private
.free
= init_thread
->tkm_private
.free
;
257 kernel_task
->tkm_shared
.alloc
= init_thread
->tkm_shared
.alloc
;
258 kernel_task
->tkm_shared
.free
= init_thread
->tkm_shared
.free
;
260 kernel_bootstrap_kprintf("calling load_context - done\n");
261 load_context(thread
);
268 * Now running in a thread. Kick off other services,
269 * invoke user bootstrap, enter pageout loop.
272 kernel_bootstrap_thread(void)
274 processor_t processor
= current_processor();
276 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
277 kernel_bootstrap_thread_kprintf("calling idle_thread_create\n");
279 * Create the idle processor thread.
281 idle_thread_create(processor
);
284 * N.B. Do not stick anything else
287 * Start up the scheduler services.
289 kernel_bootstrap_thread_kprintf("calling sched_startup\n");
293 * Thread lifecycle maintenance (teardown, stack allocation)
295 kernel_bootstrap_thread_kprintf("calling thread_daemon_init\n");
296 thread_daemon_init();
299 * Thread callout service.
301 kernel_bootstrap_thread_kprintf("calling thread_call_initialize\n");
302 thread_call_initialize();
305 * Remain on current processor as
306 * additional processors come online.
308 kernel_bootstrap_thread_kprintf("calling thread_bind\n");
309 thread_bind(processor
);
312 * Kick off memory mapping adjustments.
314 kernel_bootstrap_thread_kprintf("calling mapping_adjust\n");
318 * Create the clock service.
320 kernel_bootstrap_thread_kprintf("calling clock_service_create\n");
321 clock_service_create();
324 * Create the device service.
326 device_service_create();
331 kernel_bootstrap_kprintf("calling kdp_init\n");
335 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
337 * Create and initialize the physical copy window for processor 0
338 * This is required before starting kicking off IOKit.
340 cpu_physwindow_init(0);
347 #if (defined(__i386__) || defined(__x86_64__))
348 if (turn_on_log_leaks
&& !new_nkdbufs
)
349 new_nkdbufs
= 200000;
350 start_kern_tracing(new_nkdbufs
);
351 if (turn_on_log_leaks
)
359 (void) spllo(); /* Allow interruptions */
361 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
363 * Create and initialize the copy window for processor 0
364 * This also allocates window space for all other processors.
365 * However, this is dependent on the number of processors - so this call
366 * must be after IOKit has been started because IOKit performs processor
369 cpu_userwindow_init(0);
372 #if (!defined(__i386__) && !defined(__x86_64__))
373 if (turn_on_log_leaks
&& !new_nkdbufs
)
374 new_nkdbufs
= 200000;
375 start_kern_tracing(new_nkdbufs
);
376 if (turn_on_log_leaks
)
381 * Initialize the shared region module.
383 vm_shared_region_init();
387 mac_policy_initmach();
391 * Start the user bootstrap.
398 * Get rid of segments used to bootstrap kext loading. This removes
399 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
401 OSKextRemoveKextBootstrap();
403 serial_keyboard_init(); /* Start serial keyboard if wanted */
405 vm_page_init_local_q();
407 thread_bind(PROCESSOR_NULL
);
410 * Become the pageout daemon.
419 * Load the first thread to start a processor.
422 slave_main(void *machine_param
)
424 processor_t processor
= current_processor();
428 * Use the idle processor thread if there
429 * is no dedicated start up thread.
431 if (processor
->next_thread
== THREAD_NULL
) {
432 thread
= processor
->idle_thread
;
433 thread
->continuation
= (thread_continue_t
)processor_start_thread
;
434 thread
->parameter
= machine_param
;
437 thread
= processor
->next_thread
;
438 processor
->next_thread
= THREAD_NULL
;
441 load_context(thread
);
446 * processor_start_thread:
448 * First thread to execute on a started processor.
450 * Called at splsched.
453 processor_start_thread(void *machine_param
)
455 processor_t processor
= current_processor();
456 thread_t self
= current_thread();
458 slave_machine_init(machine_param
);
461 * If running the idle processor thread,
462 * reenter the idle loop, else terminate.
464 if (self
== processor
->idle_thread
)
465 thread_block((thread_continue_t
)idle_thread
);
467 thread_terminate(self
);
474 * Start the first thread on a processor.
480 processor_t processor
= current_processor();
483 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
485 load_context_kprintf("calling machine_set_current_thread\n");
486 machine_set_current_thread(thread
);
488 load_context_kprintf("calling processor_up\n");
489 processor_up(processor
);
491 PMAP_ACTIVATE_KERNEL(processor
->cpu_id
);
494 * Acquire a stack if none attached. The panic
495 * should never occur since the thread is expected
496 * to have reserved stack.
498 load_context_kprintf("stack %x, stackptr %x\n",
499 thread
->kernel_stack
, thread
->machine
.kstackptr
);
500 if (!thread
->kernel_stack
) {
501 load_context_kprintf("calling stack_alloc_try\n");
502 if (!stack_alloc_try(thread
))
503 panic("load_context");
507 * The idle processor threads are not counted as
508 * running for load calculations.
510 if (!(thread
->state
& TH_IDLE
))
513 processor
->active_thread
= thread
;
514 processor
->current_pri
= thread
->sched_pri
;
515 processor
->current_thmode
= thread
->sched_mode
;
516 processor
->deadline
= UINT64_MAX
;
517 thread
->last_processor
= processor
;
519 processor
->last_dispatch
= mach_absolute_time();
520 timer_start(&thread
->system_timer
, processor
->last_dispatch
);
521 PROCESSOR_DATA(processor
, thread_timer
) = PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
523 timer_start(&PROCESSOR_DATA(processor
, system_state
), processor
->last_dispatch
);
524 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, system_state
);
526 PMAP_ACTIVATE_USER(thread
, processor
->cpu_id
);
528 load_context_kprintf("calling machine_load_context\n");
529 machine_load_context(thread
);
537 #if defined(__LP64__)
538 typeof(task_max
) task_max_base
= task_max
;
540 /* Raise limits for servers with >= 16G */
541 if ((serverperfmode
!= 0) && ((uint64_t)sane_size
>= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
542 scale
= (int)((uint64_t)sane_size
/ (uint64_t)(8 * 1024 * 1024 *1024ULL));
546 task_max_base
= 2500;
547 } else if ((uint64_t)sane_size
>= (uint64_t)(3 * 1024 * 1024 *1024ULL))
550 task_max
= MAX(task_max
, task_max_base
* scale
);
553 task_threadmax
= task_max
;
554 thread_max
= task_max
* 5;
559 bsd_scale_setup(scale
);
561 ipc_space_max
= SPACE_MAX
;
562 ipc_tree_entry_max
= ITE_MAX
;
563 ipc_port_max
= PORT_MAX
;
564 ipc_pset_max
= SET_MAX
;
565 semaphore_max
= SEMAPHORE_MAX
;