2 * Copyright (c) 2000-2010 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>
73 #include <mach/boolean.h>
74 #include <mach/machine.h>
75 #include <mach/thread_act.h>
76 #include <mach/task_special_ports.h>
77 #include <mach/vm_param.h>
78 #include <ipc/ipc_init.h>
79 #include <kern/assert.h>
80 #include <kern/mach_param.h>
81 #include <kern/misc_protos.h>
82 #include <kern/clock.h>
83 #include <kern/coalition.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>
92 #include <kern/startup.h>
93 #include <kern/task.h>
94 #include <kern/thread.h>
95 #include <kern/timer.h>
97 #include <kern/telemetry.h>
100 #include <kern/zalloc.h>
101 #include <kern/locks.h>
102 #include <kern/debug.h>
103 #include <corpses/task_corpse.h>
104 #include <prng/random.h>
105 #include <console/serial_protos.h>
106 #include <vm/vm_kern.h>
107 #include <vm/vm_init.h>
108 #include <vm/vm_map.h>
109 #include <vm/vm_object.h>
110 #include <vm/vm_page.h>
111 #include <vm/vm_pageout.h>
112 #include <vm/vm_shared_region.h>
113 #include <machine/pmap.h>
114 #include <machine/commpage.h>
115 #include <libkern/version.h>
116 #include <sys/codesign.h>
117 #include <sys/kdebug.h>
118 #include <sys/random.h>
120 #include <kern/waitq.h>
124 #include <atm/atm_internal.h>
132 #include <bank/bank_internal.h>
135 #if ALTERNATE_DEBUGGER
136 #include <arm64/alternate_debugger.h>
144 #include <security/mac_mach_internal.h>
148 #include <kern/kpc.h>
152 #include <kperf/kperf.h>
156 #include <kern/hv_support.h>
160 #include <i386/pmCPU.h>
161 static void kernel_bootstrap_thread(void);
163 static void load_context(
165 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
166 extern void cpu_userwindow_init(int);
167 extern void cpu_physwindow_init(int);
170 #if CONFIG_ECC_LOGGING
171 #include <kern/ecc.h>
174 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
175 #include <i386/vmx/vmx_cpu.h>
178 // libkern/OSKextLib.cpp
179 extern void OSKextRemoveKextBootstrap(void);
181 void scale_setup(void);
182 extern void bsd_scale_setup(int);
183 extern unsigned int semaphore_max
;
184 extern void stackshot_lock_init(void);
185 extern void console_init(void);
188 * Running in virtual memory, on the interrupt stack.
191 extern int serverperfmode
;
193 /* size of kernel trace buffer, disabled by default */
194 unsigned int new_nkdbufs
= 0;
195 unsigned int wake_nkdbufs
= 0;
196 unsigned int write_trace_on_panic
= 0;
197 unsigned int trace_typefilter
= 0;
198 boolean_t trace_serial
= FALSE
;
200 /* mach leak logging */
202 int turn_on_log_leaks
= 0;
205 kernel_bootstrap_log(const char *message
)
207 // kprintf("kernel_bootstrap: %s\n", message);
208 kernel_debug_string_simple(message
);
212 kernel_bootstrap_thread_log(const char *message
)
214 // kprintf("kernel_bootstrap_thread: %s\n", message);
215 kernel_debug_string_simple(message
);
219 kernel_early_bootstrap(void)
221 /* serverperfmode is needed by timer setup */
222 if (PE_parse_boot_argn("serverperfmode", &serverperfmode
, sizeof (serverperfmode
))) {
229 * Initialize the timer callout world
235 * Configure SFI classes
241 extern boolean_t
IORamDiskBSDRoot(void);
242 extern kern_return_t
cpm_preallocate_early(void);
245 kernel_bootstrap(void)
247 kern_return_t result
;
251 printf("%s\n", version
); /* log kernel version */
253 if (PE_parse_boot_argn("-l", namep
, sizeof (namep
))) /* leaks logging */
254 turn_on_log_leaks
= 1;
256 PE_parse_boot_argn("trace", &new_nkdbufs
, sizeof (new_nkdbufs
));
257 PE_parse_boot_argn("trace_wake", &wake_nkdbufs
, sizeof (wake_nkdbufs
));
258 PE_parse_boot_argn("trace_panic", &write_trace_on_panic
, sizeof(write_trace_on_panic
));
259 PE_parse_boot_argn("trace_typefilter", &trace_typefilter
, sizeof(trace_typefilter
));
263 kernel_bootstrap_log("vm_mem_bootstrap");
266 kernel_bootstrap_log("cs_init");
269 kernel_bootstrap_log("vm_mem_init");
272 machine_info
.memory_size
= (uint32_t)mem_size
;
273 machine_info
.max_mem
= max_mem
;
274 machine_info
.major_version
= version_major
;
275 machine_info
.minor_version
= version_minor
;
279 kernel_bootstrap_log("telemetry_init");
284 kernel_bootstrap_log("csr_init");
288 if (PE_i_can_has_debugger(NULL
) &&
289 PE_parse_boot_argn("-show_pointers", &namep
, sizeof (namep
))) {
290 doprnt_hide_pointers
= FALSE
;
293 kernel_bootstrap_log("console_init");
296 kernel_bootstrap_log("stackshot_lock_init");
297 stackshot_lock_init();
299 kernel_bootstrap_log("sched_init");
302 kernel_bootstrap_log("waitq_bootstrap");
305 kernel_bootstrap_log("ipc_bootstrap");
309 kernel_bootstrap_log("mac_policy_init");
313 kernel_bootstrap_log("ipc_init");
317 * As soon as the virtual memory system is up, we record
318 * that this CPU is using the kernel pmap.
320 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
321 PMAP_ACTIVATE_KERNEL(master_cpu
);
323 kernel_bootstrap_log("mapping_free_prime");
324 mapping_free_prime(); /* Load up with temporary mapping blocks */
326 kernel_bootstrap_log("machine_init");
329 kernel_bootstrap_log("clock_init");
335 * Initialize the IPC, task, and thread subsystems.
337 #if CONFIG_COALITIONS
338 kernel_bootstrap_log("coalitions_init");
342 kernel_bootstrap_log("task_init");
345 kernel_bootstrap_log("thread_init");
349 /* Initialize the Activity Trace Resource Manager. */
350 kernel_bootstrap_log("atm_init");
355 /* Initialize the BANK Manager. */
356 kernel_bootstrap_log("bank_init");
360 /* initialize the corpse config based on boot-args */
364 * Create a kernel thread to execute the kernel bootstrap.
366 kernel_bootstrap_log("kernel_thread_create");
367 result
= kernel_thread_create((thread_continue_t
)kernel_bootstrap_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
369 if (result
!= KERN_SUCCESS
) panic("kernel_bootstrap: result = %08X\n", result
);
371 thread
->state
= TH_RUN
;
372 thread
->last_made_runnable_time
= mach_absolute_time();
373 thread_deallocate(thread
);
375 kernel_bootstrap_log("load_context - done");
376 load_context(thread
);
382 vm_offset_t vm_kernel_addrperm
;
383 vm_offset_t buf_kernel_addrperm
;
384 vm_offset_t vm_kernel_addrperm_ext
;
387 * Now running in a thread. Kick off other services,
388 * invoke user bootstrap, enter pageout loop.
391 kernel_bootstrap_thread(void)
393 processor_t processor
= current_processor();
395 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
396 kernel_bootstrap_thread_log("idle_thread_create");
398 * Create the idle processor thread.
400 idle_thread_create(processor
);
403 * N.B. Do not stick anything else
406 * Start up the scheduler services.
408 kernel_bootstrap_thread_log("sched_startup");
412 * Thread lifecycle maintenance (teardown, stack allocation)
414 kernel_bootstrap_thread_log("thread_daemon_init");
415 thread_daemon_init();
417 /* Create kernel map entry reserve */
418 vm_kernel_reserved_entry_init();
421 * Thread callout service.
423 kernel_bootstrap_thread_log("thread_call_initialize");
424 thread_call_initialize();
427 * Remain on current processor as
428 * additional processors come online.
430 kernel_bootstrap_thread_log("thread_bind");
431 thread_bind(processor
);
434 * Initialize ipc thread call support.
436 kernel_bootstrap_thread_log("ipc_thread_call_init");
437 ipc_thread_call_init();
440 * Kick off memory mapping adjustments.
442 kernel_bootstrap_thread_log("mapping_adjust");
446 * Create the clock service.
448 kernel_bootstrap_thread_log("clock_service_create");
449 clock_service_create();
452 * Create the device service.
454 device_service_create();
458 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
460 * Create and initialize the physical copy window for processor 0
461 * This is required before starting kicking off IOKit.
463 cpu_physwindow_init(0);
469 kernel_bootstrap_log("kdp_init");
473 #if ALTERNATE_DEBUGGER
474 alternate_debugger_init();
481 #if CONFIG_ECC_LOGGING
494 kernel_bootstrap_log("bootprofile_init");
498 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
502 #if (defined(__i386__) || defined(__x86_64__))
505 if (trace_typefilter
== 0)
506 trace_typefilter
= 1;
508 if (turn_on_log_leaks
&& !new_nkdbufs
)
509 new_nkdbufs
= 200000;
510 if (trace_typefilter
)
511 start_kern_tracing_with_typefilter(new_nkdbufs
,
515 start_kern_tracing(new_nkdbufs
, FALSE
);
516 if (turn_on_log_leaks
)
521 kernel_bootstrap_log("prng_init");
522 prng_cpu_init(master_cpu
);
528 assert(ml_get_interrupts_enabled() == FALSE
);
529 (void) spllo(); /* Allow interruptions */
531 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
533 * Create and initialize the copy window for processor 0
534 * This also allocates window space for all other processors.
535 * However, this is dependent on the number of processors - so this call
536 * must be after IOKit has been started because IOKit performs processor
539 cpu_userwindow_init(0);
542 #if (!defined(__i386__) && !defined(__x86_64__))
543 if (turn_on_log_leaks
&& !new_nkdbufs
)
544 new_nkdbufs
= 200000;
545 if (trace_typefilter
)
546 start_kern_tracing_with_typefilter(new_nkdbufs
, FALSE
, trace_typefilter
);
548 start_kern_tracing(new_nkdbufs
, FALSE
);
549 if (turn_on_log_leaks
)
554 * Initialize the shared region module.
556 vm_shared_region_init();
558 vm_commpage_text_init();
561 kernel_bootstrap_log("mac_policy_initmach");
562 mac_policy_initmach();
567 kernel_bootstrap_log("sfi_init");
572 * Initialize the globals used for permuting kernel
573 * addresses that may be exported to userland as tokens
574 * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL().
575 * Force the random number to be odd to avoid mapping a non-zero
576 * word-aligned address to zero via addition.
577 * Note: at this stage we can use the cryptographically secure PRNG
578 * rather than early_random().
580 read_random(&vm_kernel_addrperm
, sizeof(vm_kernel_addrperm
));
581 vm_kernel_addrperm
|= 1;
582 read_random(&buf_kernel_addrperm
, sizeof(buf_kernel_addrperm
));
583 buf_kernel_addrperm
|= 1;
584 read_random(&vm_kernel_addrperm_ext
, sizeof(vm_kernel_addrperm_ext
));
585 vm_kernel_addrperm_ext
|= 1;
587 vm_set_restrictions();
592 * Start the user bootstrap.
599 * Get rid of segments used to bootstrap kext loading. This removes
600 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
602 OSKextRemoveKextBootstrap();
604 serial_keyboard_init(); /* Start serial keyboard if wanted */
606 vm_page_init_local_q();
608 thread_bind(PROCESSOR_NULL
);
611 * Become the pageout daemon.
620 * Load the first thread to start a processor.
623 slave_main(void *machine_param
)
625 processor_t processor
= current_processor();
629 * Use the idle processor thread if there
630 * is no dedicated start up thread.
632 if (processor
->next_thread
== THREAD_NULL
) {
633 thread
= processor
->idle_thread
;
634 thread
->continuation
= (thread_continue_t
)processor_start_thread
;
635 thread
->parameter
= machine_param
;
638 thread
= processor
->next_thread
;
639 processor
->next_thread
= THREAD_NULL
;
642 load_context(thread
);
647 * processor_start_thread:
649 * First thread to execute on a started processor.
651 * Called at splsched.
654 processor_start_thread(void *machine_param
)
656 processor_t processor
= current_processor();
657 thread_t self
= current_thread();
659 slave_machine_init(machine_param
);
662 * If running the idle processor thread,
663 * reenter the idle loop, else terminate.
665 if (self
== processor
->idle_thread
)
666 thread_block((thread_continue_t
)idle_thread
);
668 thread_terminate(self
);
675 * Start the first thread on a processor.
681 processor_t processor
= current_processor();
684 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
686 load_context_kprintf("machine_set_current_thread\n");
687 machine_set_current_thread(thread
);
689 load_context_kprintf("processor_up\n");
690 processor_up(processor
);
692 PMAP_ACTIVATE_KERNEL(processor
->cpu_id
);
695 * Acquire a stack if none attached. The panic
696 * should never occur since the thread is expected
697 * to have reserved stack.
699 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread
,
700 thread
->kernel_stack
, thread
->machine
.kstackptr
);
701 if (!thread
->kernel_stack
) {
702 load_context_kprintf("stack_alloc_try\n");
703 if (!stack_alloc_try(thread
))
704 panic("load_context");
708 * The idle processor threads are not counted as
709 * running for load calculations.
711 if (!(thread
->state
& TH_IDLE
))
712 sched_run_incr(thread
);
714 processor
->active_thread
= thread
;
715 processor
->current_pri
= thread
->sched_pri
;
716 processor
->current_thmode
= thread
->sched_mode
;
717 processor
->deadline
= UINT64_MAX
;
718 thread
->last_processor
= processor
;
720 processor
->last_dispatch
= mach_absolute_time();
721 timer_start(&thread
->system_timer
, processor
->last_dispatch
);
722 PROCESSOR_DATA(processor
, thread_timer
) = PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
724 timer_start(&PROCESSOR_DATA(processor
, system_state
), processor
->last_dispatch
);
725 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, system_state
);
727 PMAP_ACTIVATE_USER(thread
, processor
->cpu_id
);
729 load_context_kprintf("machine_load_context\n");
730 machine_load_context(thread
);
738 #if defined(__LP64__)
739 typeof(task_max
) task_max_base
= task_max
;
741 /* Raise limits for servers with >= 16G */
742 if ((serverperfmode
!= 0) && ((uint64_t)sane_size
>= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
743 scale
= (int)((uint64_t)sane_size
/ (uint64_t)(8 * 1024 * 1024 *1024ULL));
747 task_max_base
= 2500;
748 } else if ((uint64_t)sane_size
>= (uint64_t)(3 * 1024 * 1024 *1024ULL))
751 task_max
= MAX(task_max
, task_max_base
* scale
);
754 task_threadmax
= task_max
;
755 thread_max
= task_max
* 5;
760 bsd_scale_setup(scale
);
762 ipc_space_max
= SPACE_MAX
;
763 ipc_port_max
= PORT_MAX
;
764 ipc_pset_max
= SET_MAX
;
765 semaphore_max
= SEMAPHORE_MAX
;