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>
90 #include <kern/startup.h>
91 #include <kern/task.h>
92 #include <kern/thread.h>
93 #include <kern/timer.h>
95 #include <kern/telemetry.h>
97 #include <kern/wait_queue.h>
99 #include <kern/zalloc.h>
100 #include <kern/locks.h>
101 #include <prng/random.h>
102 #include <console/serial_protos.h>
103 #include <vm/vm_kern.h>
104 #include <vm/vm_init.h>
105 #include <vm/vm_map.h>
106 #include <vm/vm_object.h>
107 #include <vm/vm_page.h>
108 #include <vm/vm_pageout.h>
109 #include <vm/vm_shared_region.h>
110 #include <machine/pmap.h>
111 #include <machine/commpage.h>
112 #include <libkern/version.h>
113 #include <sys/codesign.h>
114 #include <sys/kdebug.h>
115 #include <sys/random.h>
118 #include <atm/atm_internal.h>
126 #include <bank/bank_internal.h>
129 #if ALTERNATE_DEBUGGER
130 #include <arm64/alternate_debugger.h>
138 #include <security/mac_mach_internal.h>
146 #include <kern/kpc.h>
150 #include <kperf/kperf.h>
154 #include <kern/hv_support.h>
158 #include <i386/pmCPU.h>
159 static void kernel_bootstrap_thread(void);
161 static void load_context(
163 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
164 extern void cpu_userwindow_init(int);
165 extern void cpu_physwindow_init(int);
168 #if CONFIG_ECC_LOGGING
169 #include <kern/ecc.h>
172 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
173 #include <i386/vmx/vmx_cpu.h>
176 // libkern/OSKextLib.cpp
177 extern void OSKextRemoveKextBootstrap(void);
179 void scale_setup(void);
180 extern void bsd_scale_setup(int);
181 extern unsigned int semaphore_max
;
182 extern void stackshot_lock_init(void);
185 * Running in virtual memory, on the interrupt stack.
188 extern int serverperfmode
;
190 /* size of kernel trace buffer, disabled by default */
191 unsigned int new_nkdbufs
= 0;
192 unsigned int wake_nkdbufs
= 0;
193 unsigned int write_trace_on_panic
= 0;
194 unsigned int trace_typefilter
= 0;
196 /* mach leak logging */
198 int turn_on_log_leaks
= 0;
201 kernel_bootstrap_log(const char *message
)
203 // kprintf("kernel_bootstrap: %s\n", message);
204 kernel_debug_string(message
);
208 kernel_bootstrap_thread_log(const char *message
)
210 // kprintf("kernel_bootstrap_thread: %s\n", message);
211 kernel_debug_string(message
);
215 kernel_early_bootstrap(void)
217 /* serverperfmode is needed by timer setup */
218 if (PE_parse_boot_argn("serverperfmode", &serverperfmode
, sizeof (serverperfmode
))) {
225 * Initialize the timer callout world
230 * Configure SFI classes
237 kernel_bootstrap(void)
239 kern_return_t result
;
243 printf("%s\n", version
); /* log kernel version */
245 if (PE_parse_boot_argn("-l", namep
, sizeof (namep
))) /* leaks logging */
246 turn_on_log_leaks
= 1;
248 PE_parse_boot_argn("trace", &new_nkdbufs
, sizeof (new_nkdbufs
));
249 PE_parse_boot_argn("trace_wake", &wake_nkdbufs
, sizeof (wake_nkdbufs
));
250 PE_parse_boot_argn("trace_panic", &write_trace_on_panic
, sizeof(write_trace_on_panic
));
251 PE_parse_boot_argn("trace_typefilter", &trace_typefilter
, sizeof(trace_typefilter
));
255 kernel_bootstrap_log("vm_mem_bootstrap");
258 kernel_bootstrap_log("cs_init");
261 kernel_bootstrap_log("vm_mem_init");
264 machine_info
.memory_size
= (uint32_t)mem_size
;
265 machine_info
.max_mem
= max_mem
;
266 machine_info
.major_version
= version_major
;
267 machine_info
.minor_version
= version_minor
;
270 kernel_bootstrap_log("telemetry_init");
274 kernel_bootstrap_log("stackshot_lock_init");
275 stackshot_lock_init();
277 kernel_bootstrap_log("sched_init");
280 kernel_bootstrap_log("wait_queue_bootstrap");
281 wait_queue_bootstrap();
283 kernel_bootstrap_log("ipc_bootstrap");
289 kernel_bootstrap_log("ipc_init");
293 * As soon as the virtual memory system is up, we record
294 * that this CPU is using the kernel pmap.
296 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
297 PMAP_ACTIVATE_KERNEL(master_cpu
);
299 kernel_bootstrap_log("mapping_free_prime");
300 mapping_free_prime(); /* Load up with temporary mapping blocks */
302 kernel_bootstrap_log("machine_init");
305 kernel_bootstrap_log("clock_init");
311 * Initialize the IPC, task, and thread subsystems.
313 #if CONFIG_COALITIONS
314 kernel_bootstrap_log("coalition_init");
318 kernel_bootstrap_log("task_init");
321 kernel_bootstrap_log("thread_init");
325 /* Initialize the Activity Trace Resource Manager. */
326 kernel_bootstrap_log("atm_init");
331 kernel_bootstrap_log("csr_init");
336 /* Initialize the BANK Manager. */
337 kernel_bootstrap_log("bank_init");
342 * Create a kernel thread to execute the kernel bootstrap.
344 kernel_bootstrap_log("kernel_thread_create");
345 result
= kernel_thread_create((thread_continue_t
)kernel_bootstrap_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
347 if (result
!= KERN_SUCCESS
) panic("kernel_bootstrap: result = %08X\n", result
);
349 thread
->state
= TH_RUN
;
350 thread_deallocate(thread
);
352 kernel_bootstrap_log("load_context - done");
353 load_context(thread
);
359 vm_offset_t vm_kernel_addrperm
;
360 vm_offset_t buf_kernel_addrperm
;
363 * Now running in a thread. Kick off other services,
364 * invoke user bootstrap, enter pageout loop.
367 kernel_bootstrap_thread(void)
369 processor_t processor
= current_processor();
371 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
372 kernel_bootstrap_thread_log("idle_thread_create");
374 * Create the idle processor thread.
376 idle_thread_create(processor
);
379 * N.B. Do not stick anything else
382 * Start up the scheduler services.
384 kernel_bootstrap_thread_log("sched_startup");
388 * Thread lifecycle maintenance (teardown, stack allocation)
390 kernel_bootstrap_thread_log("thread_daemon_init");
391 thread_daemon_init();
393 /* Create kernel map entry reserve */
394 vm_kernel_reserved_entry_init();
397 * Thread callout service.
399 kernel_bootstrap_thread_log("thread_call_initialize");
400 thread_call_initialize();
403 * Remain on current processor as
404 * additional processors come online.
406 kernel_bootstrap_thread_log("thread_bind");
407 thread_bind(processor
);
410 * Initialize ipc thread call support.
412 kernel_bootstrap_thread_log("ipc_thread_call_init");
413 ipc_thread_call_init();
416 * Kick off memory mapping adjustments.
418 kernel_bootstrap_thread_log("mapping_adjust");
422 * Create the clock service.
424 kernel_bootstrap_thread_log("clock_service_create");
425 clock_service_create();
428 * Create the device service.
430 device_service_create();
434 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
436 * Create and initialize the physical copy window for processor 0
437 * This is required before starting kicking off IOKit.
439 cpu_physwindow_init(0);
445 kernel_bootstrap_log("kdp_init");
449 #if ALTERNATE_DEBUGGER
450 alternate_debugger_init();
461 #if CONFIG_ECC_LOGGING
474 kernel_bootstrap_log("bootprofile_init");
478 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
482 #if (defined(__i386__) || defined(__x86_64__))
483 if (turn_on_log_leaks
&& !new_nkdbufs
)
484 new_nkdbufs
= 200000;
485 if (trace_typefilter
)
486 start_kern_tracing_with_typefilter(new_nkdbufs
,
490 start_kern_tracing(new_nkdbufs
, FALSE
);
491 if (turn_on_log_leaks
)
496 kernel_bootstrap_log("prng_init");
497 prng_cpu_init(master_cpu
);
503 assert(ml_get_interrupts_enabled() == FALSE
);
504 (void) spllo(); /* Allow interruptions */
506 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
508 * Create and initialize the copy window for processor 0
509 * This also allocates window space for all other processors.
510 * However, this is dependent on the number of processors - so this call
511 * must be after IOKit has been started because IOKit performs processor
514 cpu_userwindow_init(0);
517 #if (!defined(__i386__) && !defined(__x86_64__))
518 if (turn_on_log_leaks
&& !new_nkdbufs
)
519 new_nkdbufs
= 200000;
520 if (trace_typefilter
)
521 start_kern_tracing_with_typefilter(new_nkdbufs
, FALSE
, trace_typefilter
);
523 start_kern_tracing(new_nkdbufs
, FALSE
);
524 if (turn_on_log_leaks
)
529 * Initialize the shared region module.
531 vm_shared_region_init();
533 vm_commpage_text_init();
537 kernel_bootstrap_log("mac_policy_initmach");
538 mac_policy_initmach();
541 kernel_bootstrap_log("sfi_init");
545 * Initialize the global used for permuting kernel
546 * addresses that may be exported to userland as tokens
547 * using VM_KERNEL_ADDRPERM(). Force the random number
548 * to be odd to avoid mapping a non-zero
549 * word-aligned address to zero via addition.
550 * Note: at this stage we can use the cryptographically secure PRNG
551 * rather than early_random().
553 read_random(&vm_kernel_addrperm
, sizeof(vm_kernel_addrperm
));
554 vm_kernel_addrperm
|= 1;
555 read_random(&buf_kernel_addrperm
, sizeof(buf_kernel_addrperm
));
556 buf_kernel_addrperm
|= 1;
559 * Start the user bootstrap.
566 * Get rid of segments used to bootstrap kext loading. This removes
567 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
569 OSKextRemoveKextBootstrap();
571 serial_keyboard_init(); /* Start serial keyboard if wanted */
573 vm_page_init_local_q();
575 thread_bind(PROCESSOR_NULL
);
578 * Become the pageout daemon.
587 * Load the first thread to start a processor.
590 slave_main(void *machine_param
)
592 processor_t processor
= current_processor();
596 * Use the idle processor thread if there
597 * is no dedicated start up thread.
599 if (processor
->next_thread
== THREAD_NULL
) {
600 thread
= processor
->idle_thread
;
601 thread
->continuation
= (thread_continue_t
)processor_start_thread
;
602 thread
->parameter
= machine_param
;
605 thread
= processor
->next_thread
;
606 processor
->next_thread
= THREAD_NULL
;
609 load_context(thread
);
614 * processor_start_thread:
616 * First thread to execute on a started processor.
618 * Called at splsched.
621 processor_start_thread(void *machine_param
)
623 processor_t processor
= current_processor();
624 thread_t self
= current_thread();
626 slave_machine_init(machine_param
);
629 * If running the idle processor thread,
630 * reenter the idle loop, else terminate.
632 if (self
== processor
->idle_thread
)
633 thread_block((thread_continue_t
)idle_thread
);
635 thread_terminate(self
);
642 * Start the first thread on a processor.
648 processor_t processor
= current_processor();
651 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
653 load_context_kprintf("machine_set_current_thread\n");
654 machine_set_current_thread(thread
);
656 load_context_kprintf("processor_up\n");
657 processor_up(processor
);
659 PMAP_ACTIVATE_KERNEL(processor
->cpu_id
);
662 * Acquire a stack if none attached. The panic
663 * should never occur since the thread is expected
664 * to have reserved stack.
666 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread
,
667 thread
->kernel_stack
, thread
->machine
.kstackptr
);
668 if (!thread
->kernel_stack
) {
669 load_context_kprintf("stack_alloc_try\n");
670 if (!stack_alloc_try(thread
))
671 panic("load_context");
675 * The idle processor threads are not counted as
676 * running for load calculations.
678 if (!(thread
->state
& TH_IDLE
))
679 sched_run_incr(thread
);
681 processor
->active_thread
= thread
;
682 processor
->current_pri
= thread
->sched_pri
;
683 processor
->current_thmode
= thread
->sched_mode
;
684 processor
->deadline
= UINT64_MAX
;
685 thread
->last_processor
= processor
;
687 processor
->last_dispatch
= mach_absolute_time();
688 timer_start(&thread
->system_timer
, processor
->last_dispatch
);
689 PROCESSOR_DATA(processor
, thread_timer
) = PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
691 timer_start(&PROCESSOR_DATA(processor
, system_state
), processor
->last_dispatch
);
692 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, system_state
);
694 PMAP_ACTIVATE_USER(thread
, processor
->cpu_id
);
696 load_context_kprintf("machine_load_context\n");
697 machine_load_context(thread
);
705 #if defined(__LP64__)
706 typeof(task_max
) task_max_base
= task_max
;
708 /* Raise limits for servers with >= 16G */
709 if ((serverperfmode
!= 0) && ((uint64_t)sane_size
>= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
710 scale
= (int)((uint64_t)sane_size
/ (uint64_t)(8 * 1024 * 1024 *1024ULL));
714 task_max_base
= 2500;
715 } else if ((uint64_t)sane_size
>= (uint64_t)(3 * 1024 * 1024 *1024ULL))
718 task_max
= MAX(task_max
, task_max_base
* scale
);
721 task_threadmax
= task_max
;
722 thread_max
= task_max
* 5;
727 bsd_scale_setup(scale
);
729 ipc_space_max
= SPACE_MAX
;
730 ipc_port_max
= PORT_MAX
;
731 ipc_pset_max
= SET_MAX
;
732 semaphore_max
= SEMAPHORE_MAX
;