]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
xnu-3789.1.32.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
1 /*
2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
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,
60 * Version 2.0.
61 */
62 /*
63 */
64
65 /*
66 * Mach kernel startup.
67 */
68
69 #include <debug.h>
70 #include <xpr_debug.h>
71 #include <mach_kdp.h>
72
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>
89 #if CONFIG_SCHED_SFI
90 #include <kern/sfi.h>
91 #endif
92 #include <kern/startup.h>
93 #include <kern/task.h>
94 #include <kern/thread.h>
95 #include <kern/timer.h>
96 #if CONFIG_TELEMETRY
97 #include <kern/telemetry.h>
98 #endif
99 #include <kern/xpr.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>
119 #include <sys/ktrace.h>
120
121 #include <kern/ltable.h>
122 #include <kern/waitq.h>
123 #include <ipc/ipc_voucher.h>
124 #include <voucher/ipc_pthread_priority_internal.h>
125
126
127 #if CONFIG_ATM
128 #include <atm/atm_internal.h>
129 #endif
130
131 #if CONFIG_CSR
132 #include <sys/csr.h>
133 #endif
134
135 #if CONFIG_BANK
136 #include <bank/bank_internal.h>
137 #endif
138
139 #if ALTERNATE_DEBUGGER
140 #include <arm64/alternate_debugger.h>
141 #endif
142
143 #if MACH_KDP
144 #include <kdp/kdp.h>
145 #endif
146
147 #if CONFIG_MACF
148 #include <security/mac_mach_internal.h>
149 #endif
150
151 #if KPC
152 #include <kern/kpc.h>
153 #endif
154
155 #if HYPERVISOR
156 #include <kern/hv_support.h>
157 #endif
158
159
160 #include <i386/pmCPU.h>
161 static void kernel_bootstrap_thread(void);
162
163 static void load_context(
164 thread_t thread);
165 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
166 extern void cpu_userwindow_init(int);
167 extern void cpu_physwindow_init(int);
168 #endif
169
170 #if CONFIG_ECC_LOGGING
171 #include <kern/ecc.h>
172 #endif
173
174 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
175 #include <i386/vmx/vmx_cpu.h>
176 #endif
177
178 // libkern/OSKextLib.cpp
179 extern void OSKextRemoveKextBootstrap(void);
180
181 void scale_setup(void);
182 extern void bsd_scale_setup(int);
183 extern unsigned int semaphore_max;
184 extern void stackshot_init(void);
185 extern void ktrace_init(void);
186 extern void oslog_init(void);
187
188 /*
189 * Running in virtual memory, on the interrupt stack.
190 */
191
192 extern int serverperfmode;
193
194 /* size of kernel trace buffer, disabled by default */
195 unsigned int new_nkdbufs = 0;
196 unsigned int wake_nkdbufs = 0;
197 unsigned int write_trace_on_panic = 0;
198 static char trace_typefilter[64] = { 0 };
199 boolean_t trace_serial = FALSE;
200 boolean_t oslog_early_boot_complete = FALSE;
201
202 /* mach leak logging */
203 int log_leaks = 0;
204
205 static inline void
206 kernel_bootstrap_log(const char *message)
207 {
208 // kprintf("kernel_bootstrap: %s\n", message);
209 kernel_debug_string_early(message);
210 }
211
212 static inline void
213 kernel_bootstrap_thread_log(const char *message)
214 {
215 // kprintf("kernel_bootstrap_thread: %s\n", message);
216 kernel_debug_string_early(message);
217 }
218
219 void
220 kernel_early_bootstrap(void)
221 {
222 /* serverperfmode is needed by timer setup */
223 if (PE_parse_boot_argn("serverperfmode", &serverperfmode, sizeof (serverperfmode))) {
224 serverperfmode = 1;
225 }
226
227 lck_mod_init();
228
229 /*
230 * Initialize the timer callout world
231 */
232 timer_call_init();
233
234 #if CONFIG_SCHED_SFI
235 /*
236 * Configure SFI classes
237 */
238 sfi_early_init();
239 #endif
240 }
241
242 extern boolean_t IORamDiskBSDRoot(void);
243 extern kern_return_t cpm_preallocate_early(void);
244
245 void
246 kernel_bootstrap(void)
247 {
248 kern_return_t result;
249 thread_t thread;
250 char namep[16];
251
252 printf("%s\n", version); /* log kernel version */
253
254 if (PE_parse_boot_argn("-l", namep, sizeof (namep))) /* leaks logging */
255 log_leaks = 1;
256
257 PE_parse_boot_argn("trace", &new_nkdbufs, sizeof (new_nkdbufs));
258 PE_parse_boot_argn("trace_wake", &wake_nkdbufs, sizeof (wake_nkdbufs));
259 PE_parse_boot_argn("trace_panic", &write_trace_on_panic, sizeof(write_trace_on_panic));
260 PE_parse_boot_arg_str("trace_typefilter", trace_typefilter, sizeof(trace_typefilter));
261
262 scale_setup();
263
264 kernel_bootstrap_log("vm_mem_bootstrap");
265 vm_mem_bootstrap();
266
267 kernel_bootstrap_log("cs_init");
268 cs_init();
269
270 kernel_bootstrap_log("vm_mem_init");
271 vm_mem_init();
272
273 machine_info.memory_size = (uint32_t)mem_size;
274 machine_info.max_mem = max_mem;
275 machine_info.major_version = version_major;
276 machine_info.minor_version = version_minor;
277
278 oslog_init();
279
280 #if CONFIG_TELEMETRY
281 kernel_bootstrap_log("telemetry_init");
282 telemetry_init();
283 #endif
284
285 #if CONFIG_CSR
286 kernel_bootstrap_log("csr_init");
287 csr_init();
288 #endif
289
290 if (PE_i_can_has_debugger(NULL) &&
291 PE_parse_boot_argn("-show_pointers", &namep, sizeof (namep))) {
292 doprnt_hide_pointers = FALSE;
293 }
294
295 kernel_bootstrap_log("console_init");
296 console_init();
297
298 kernel_bootstrap_log("stackshot_init");
299 stackshot_init();
300
301 kernel_bootstrap_log("sched_init");
302 sched_init();
303
304 kernel_bootstrap_log("ltable_bootstrap");
305 ltable_bootstrap();
306
307 kernel_bootstrap_log("waitq_bootstrap");
308 waitq_bootstrap();
309
310 kernel_bootstrap_log("ipc_bootstrap");
311 ipc_bootstrap();
312
313 #if CONFIG_MACF
314 kernel_bootstrap_log("mac_policy_init");
315 mac_policy_init();
316 #endif
317
318 kernel_bootstrap_log("ipc_init");
319 ipc_init();
320
321 /*
322 * As soon as the virtual memory system is up, we record
323 * that this CPU is using the kernel pmap.
324 */
325 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
326 PMAP_ACTIVATE_KERNEL(master_cpu);
327
328 kernel_bootstrap_log("mapping_free_prime");
329 mapping_free_prime(); /* Load up with temporary mapping blocks */
330
331 kernel_bootstrap_log("machine_init");
332 machine_init();
333
334 kernel_bootstrap_log("clock_init");
335 clock_init();
336
337 ledger_init();
338
339 /*
340 * Initialize the IPC, task, and thread subsystems.
341 */
342 #if CONFIG_COALITIONS
343 kernel_bootstrap_log("coalitions_init");
344 coalitions_init();
345 #endif
346
347 kernel_bootstrap_log("task_init");
348 task_init();
349
350 kernel_bootstrap_log("thread_init");
351 thread_init();
352
353 #if CONFIG_ATM
354 /* Initialize the Activity Trace Resource Manager. */
355 kernel_bootstrap_log("atm_init");
356 atm_init();
357 #endif
358 kernel_bootstrap_log("mach_init_activity_id");
359 mach_init_activity_id();
360
361 #if CONFIG_BANK
362 /* Initialize the BANK Manager. */
363 kernel_bootstrap_log("bank_init");
364 bank_init();
365 #endif
366
367 kernel_bootstrap_log("ipc_pthread_priority_init");
368 ipc_pthread_priority_init();
369
370 /* initialize the corpse config based on boot-args */
371 corpses_init();
372
373 /*
374 * Create a kernel thread to execute the kernel bootstrap.
375 */
376 kernel_bootstrap_log("kernel_thread_create");
377 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
378
379 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
380
381 thread->state = TH_RUN;
382 thread->last_made_runnable_time = mach_absolute_time();
383 thread_deallocate(thread);
384
385 kernel_bootstrap_log("load_context - done");
386 load_context(thread);
387 /*NOTREACHED*/
388 }
389
390 int kth_started = 0;
391
392 vm_offset_t vm_kernel_addrperm;
393 vm_offset_t buf_kernel_addrperm;
394 vm_offset_t vm_kernel_addrperm_ext;
395
396 /*
397 * Now running in a thread. Kick off other services,
398 * invoke user bootstrap, enter pageout loop.
399 */
400 static void
401 kernel_bootstrap_thread(void)
402 {
403 processor_t processor = current_processor();
404
405 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
406 kernel_bootstrap_thread_log("idle_thread_create");
407 /*
408 * Create the idle processor thread.
409 */
410 idle_thread_create(processor);
411
412 /*
413 * N.B. Do not stick anything else
414 * before this point.
415 *
416 * Start up the scheduler services.
417 */
418 kernel_bootstrap_thread_log("sched_startup");
419 sched_startup();
420
421 /*
422 * Thread lifecycle maintenance (teardown, stack allocation)
423 */
424 kernel_bootstrap_thread_log("thread_daemon_init");
425 thread_daemon_init();
426
427 /* Create kernel map entry reserve */
428 vm_kernel_reserved_entry_init();
429
430 /*
431 * Thread callout service.
432 */
433 kernel_bootstrap_thread_log("thread_call_initialize");
434 thread_call_initialize();
435
436 /*
437 * Remain on current processor as
438 * additional processors come online.
439 */
440 kernel_bootstrap_thread_log("thread_bind");
441 thread_bind(processor);
442
443
444 /*
445 * Initialize ipc thread call support.
446 */
447 kernel_bootstrap_thread_log("ipc_thread_call_init");
448 ipc_thread_call_init();
449
450 /*
451 * Kick off memory mapping adjustments.
452 */
453 kernel_bootstrap_thread_log("mapping_adjust");
454 mapping_adjust();
455
456 /*
457 * Create the clock service.
458 */
459 kernel_bootstrap_thread_log("clock_service_create");
460 clock_service_create();
461
462 /*
463 * Create the device service.
464 */
465 device_service_create();
466
467 kth_started = 1;
468
469 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
470 /*
471 * Create and initialize the physical copy window for processor 0
472 * This is required before starting kicking off IOKit.
473 */
474 cpu_physwindow_init(0);
475 #endif
476
477
478
479 #if MACH_KDP
480 kernel_bootstrap_log("kdp_init");
481 kdp_init();
482 #endif
483
484 #if ALTERNATE_DEBUGGER
485 alternate_debugger_init();
486 #endif
487
488 #if KPC
489 kpc_init();
490 #endif
491
492 #if CONFIG_ECC_LOGGING
493 ecc_log_init();
494 #endif
495
496 #if HYPERVISOR
497 hv_support_init();
498 #endif
499
500 #if CONFIG_TELEMETRY
501 kernel_bootstrap_log("bootprofile_init");
502 bootprofile_init();
503 #endif
504
505 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
506 vmx_init();
507 #endif
508
509 kernel_bootstrap_thread_log("ktrace_init");
510 ktrace_init();
511
512 if (new_nkdbufs > 0 || kdebug_serial || log_leaks) {
513 kdebug_boot_trace(new_nkdbufs, trace_typefilter);
514 }
515
516 kernel_bootstrap_log("prng_init");
517 prng_cpu_init(master_cpu);
518
519 #ifdef MACH_BSD
520 kernel_bootstrap_log("bsd_early_init");
521 bsd_early_init();
522 #endif
523
524 #ifdef IOKIT
525 kernel_bootstrap_log("PE_init_iokit");
526 PE_init_iokit();
527 #endif
528
529 assert(ml_get_interrupts_enabled() == FALSE);
530
531 // Set this flag to indicate that it is now okay to start testing
532 // for interrupts / preemeption disabled while logging
533 oslog_early_boot_complete = TRUE;
534
535 (void) spllo(); /* Allow interruptions */
536
537 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
538 /*
539 * Create and initialize the copy window for processor 0
540 * This also allocates window space for all other processors.
541 * However, this is dependent on the number of processors - so this call
542 * must be after IOKit has been started because IOKit performs processor
543 * discovery.
544 */
545 cpu_userwindow_init(0);
546 #endif
547
548 /*
549 * Initialize the shared region module.
550 */
551 vm_shared_region_init();
552 vm_commpage_init();
553 vm_commpage_text_init();
554
555 #if CONFIG_MACF
556 kernel_bootstrap_log("mac_policy_initmach");
557 mac_policy_initmach();
558 #endif
559
560
561 #if CONFIG_SCHED_SFI
562 kernel_bootstrap_log("sfi_init");
563 sfi_init();
564 #endif
565
566 /*
567 * Initialize the globals used for permuting kernel
568 * addresses that may be exported to userland as tokens
569 * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL().
570 * Force the random number to be odd to avoid mapping a non-zero
571 * word-aligned address to zero via addition.
572 * Note: at this stage we can use the cryptographically secure PRNG
573 * rather than early_random().
574 */
575 read_random(&vm_kernel_addrperm, sizeof(vm_kernel_addrperm));
576 vm_kernel_addrperm |= 1;
577 read_random(&buf_kernel_addrperm, sizeof(buf_kernel_addrperm));
578 buf_kernel_addrperm |= 1;
579 read_random(&vm_kernel_addrperm_ext, sizeof(vm_kernel_addrperm_ext));
580 vm_kernel_addrperm_ext |= 1;
581
582 vm_set_restrictions();
583
584
585
586 /*
587 * Start the user bootstrap.
588 */
589 #ifdef MACH_BSD
590 bsd_init();
591 #endif
592
593 /*
594 * Get rid of segments used to bootstrap kext loading. This removes
595 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
596 */
597 OSKextRemoveKextBootstrap();
598
599 serial_keyboard_init(); /* Start serial keyboard if wanted */
600
601 vm_page_init_local_q();
602
603 thread_bind(PROCESSOR_NULL);
604
605 /*
606 * Become the pageout daemon.
607 */
608 vm_pageout();
609 /*NOTREACHED*/
610 }
611
612 /*
613 * slave_main:
614 *
615 * Load the first thread to start a processor.
616 */
617 void
618 slave_main(void *machine_param)
619 {
620 processor_t processor = current_processor();
621 thread_t thread;
622
623 /*
624 * Use the idle processor thread if there
625 * is no dedicated start up thread.
626 */
627 if (processor->next_thread == THREAD_NULL) {
628 thread = processor->idle_thread;
629 thread->continuation = (thread_continue_t)processor_start_thread;
630 thread->parameter = machine_param;
631 }
632 else {
633 thread = processor->next_thread;
634 processor->next_thread = THREAD_NULL;
635 }
636
637 load_context(thread);
638 /*NOTREACHED*/
639 }
640
641 /*
642 * processor_start_thread:
643 *
644 * First thread to execute on a started processor.
645 *
646 * Called at splsched.
647 */
648 void
649 processor_start_thread(void *machine_param)
650 {
651 processor_t processor = current_processor();
652 thread_t self = current_thread();
653
654 slave_machine_init(machine_param);
655
656 /*
657 * If running the idle processor thread,
658 * reenter the idle loop, else terminate.
659 */
660 if (self == processor->idle_thread)
661 thread_block((thread_continue_t)idle_thread);
662
663 thread_terminate(self);
664 /*NOTREACHED*/
665 }
666
667 /*
668 * load_context:
669 *
670 * Start the first thread on a processor.
671 */
672 static void
673 load_context(
674 thread_t thread)
675 {
676 processor_t processor = current_processor();
677
678
679 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
680
681 load_context_kprintf("machine_set_current_thread\n");
682 machine_set_current_thread(thread);
683
684 load_context_kprintf("processor_up\n");
685 processor_up(processor);
686
687 PMAP_ACTIVATE_KERNEL(processor->cpu_id);
688
689 /*
690 * Acquire a stack if none attached. The panic
691 * should never occur since the thread is expected
692 * to have reserved stack.
693 */
694 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread,
695 thread->kernel_stack, thread->machine.kstackptr);
696 if (!thread->kernel_stack) {
697 load_context_kprintf("stack_alloc_try\n");
698 if (!stack_alloc_try(thread))
699 panic("load_context");
700 }
701
702 /*
703 * The idle processor threads are not counted as
704 * running for load calculations.
705 */
706 if (!(thread->state & TH_IDLE))
707 sched_run_incr(thread);
708
709 processor->active_thread = thread;
710 processor->current_pri = thread->sched_pri;
711 processor->current_thmode = thread->sched_mode;
712 processor->current_sfi_class = SFI_CLASS_KERNEL;
713 processor->starting_pri = thread->sched_pri;
714
715 processor->deadline = UINT64_MAX;
716 thread->last_processor = processor;
717
718 processor->last_dispatch = mach_absolute_time();
719 timer_start(&thread->system_timer, processor->last_dispatch);
720 PROCESSOR_DATA(processor, thread_timer) = PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
721
722 timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch);
723 PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
724
725 PMAP_ACTIVATE_USER(thread, processor->cpu_id);
726
727 load_context_kprintf("machine_load_context\n");
728 machine_load_context(thread);
729 /*NOTREACHED*/
730 }
731
732 void
733 scale_setup()
734 {
735 int scale = 0;
736 #if defined(__LP64__)
737 typeof(task_max) task_max_base = task_max;
738
739 /* Raise limits for servers with >= 16G */
740 if ((serverperfmode != 0) && ((uint64_t)sane_size >= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
741 scale = (int)((uint64_t)sane_size / (uint64_t)(8 * 1024 * 1024 *1024ULL));
742 /* limit to 128 G */
743 if (scale > 16)
744 scale = 16;
745 task_max_base = 2500;
746 } else if ((uint64_t)sane_size >= (uint64_t)(3 * 1024 * 1024 *1024ULL))
747 scale = 2;
748
749 task_max = MAX(task_max, task_max_base * scale);
750
751 if (scale != 0) {
752 task_threadmax = task_max;
753 thread_max = task_max * 5;
754 }
755
756 #endif
757
758 bsd_scale_setup(scale);
759
760 ipc_space_max = SPACE_MAX;
761 ipc_port_max = PORT_MAX;
762 ipc_pset_max = SET_MAX;
763 semaphore_max = SEMAPHORE_MAX;
764 }
765