]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
xnu-3248.50.21.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
120 #include <kern/waitq.h>
121
122
123 #if CONFIG_ATM
124 #include <atm/atm_internal.h>
125 #endif
126
127 #if CONFIG_CSR
128 #include <sys/csr.h>
129 #endif
130
131 #if CONFIG_BANK
132 #include <bank/bank_internal.h>
133 #endif
134
135 #if ALTERNATE_DEBUGGER
136 #include <arm64/alternate_debugger.h>
137 #endif
138
139 #if MACH_KDP
140 #include <kdp/kdp.h>
141 #endif
142
143 #if CONFIG_MACF
144 #include <security/mac_mach_internal.h>
145 #endif
146
147 #if KPC
148 #include <kern/kpc.h>
149 #endif
150
151 #if KPERF
152 #include <kperf/kperf.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_lock_init(void);
185 extern void console_init(void);
186
187 /*
188 * Running in virtual memory, on the interrupt stack.
189 */
190
191 extern int serverperfmode;
192
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;
199
200 /* mach leak logging */
201 int log_leaks = 0;
202 int turn_on_log_leaks = 0;
203
204 static inline void
205 kernel_bootstrap_log(const char *message)
206 {
207 // kprintf("kernel_bootstrap: %s\n", message);
208 kernel_debug_string_simple(message);
209 }
210
211 static inline void
212 kernel_bootstrap_thread_log(const char *message)
213 {
214 // kprintf("kernel_bootstrap_thread: %s\n", message);
215 kernel_debug_string_simple(message);
216 }
217
218 void
219 kernel_early_bootstrap(void)
220 {
221 /* serverperfmode is needed by timer setup */
222 if (PE_parse_boot_argn("serverperfmode", &serverperfmode, sizeof (serverperfmode))) {
223 serverperfmode = 1;
224 }
225
226 lck_mod_init();
227
228 /*
229 * Initialize the timer callout world
230 */
231 timer_call_init();
232
233 #if CONFIG_SCHED_SFI
234 /*
235 * Configure SFI classes
236 */
237 sfi_early_init();
238 #endif
239 }
240
241 extern boolean_t IORamDiskBSDRoot(void);
242 extern kern_return_t cpm_preallocate_early(void);
243
244 void
245 kernel_bootstrap(void)
246 {
247 kern_return_t result;
248 thread_t thread;
249 char namep[16];
250
251 printf("%s\n", version); /* log kernel version */
252
253 if (PE_parse_boot_argn("-l", namep, sizeof (namep))) /* leaks logging */
254 turn_on_log_leaks = 1;
255
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));
260
261 scale_setup();
262
263 kernel_bootstrap_log("vm_mem_bootstrap");
264 vm_mem_bootstrap();
265
266 kernel_bootstrap_log("cs_init");
267 cs_init();
268
269 kernel_bootstrap_log("vm_mem_init");
270 vm_mem_init();
271
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;
276
277
278 #if CONFIG_TELEMETRY
279 kernel_bootstrap_log("telemetry_init");
280 telemetry_init();
281 #endif
282
283 #if CONFIG_CSR
284 kernel_bootstrap_log("csr_init");
285 csr_init();
286 #endif
287
288 if (PE_i_can_has_debugger(NULL) &&
289 PE_parse_boot_argn("-show_pointers", &namep, sizeof (namep))) {
290 doprnt_hide_pointers = FALSE;
291 }
292
293 kernel_bootstrap_log("console_init");
294 console_init();
295
296 kernel_bootstrap_log("stackshot_lock_init");
297 stackshot_lock_init();
298
299 kernel_bootstrap_log("sched_init");
300 sched_init();
301
302 kernel_bootstrap_log("waitq_bootstrap");
303 waitq_bootstrap();
304
305 kernel_bootstrap_log("ipc_bootstrap");
306 ipc_bootstrap();
307
308 #if CONFIG_MACF
309 kernel_bootstrap_log("mac_policy_init");
310 mac_policy_init();
311 #endif
312
313 kernel_bootstrap_log("ipc_init");
314 ipc_init();
315
316 /*
317 * As soon as the virtual memory system is up, we record
318 * that this CPU is using the kernel pmap.
319 */
320 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
321 PMAP_ACTIVATE_KERNEL(master_cpu);
322
323 kernel_bootstrap_log("mapping_free_prime");
324 mapping_free_prime(); /* Load up with temporary mapping blocks */
325
326 kernel_bootstrap_log("machine_init");
327 machine_init();
328
329 kernel_bootstrap_log("clock_init");
330 clock_init();
331
332 ledger_init();
333
334 /*
335 * Initialize the IPC, task, and thread subsystems.
336 */
337 #if CONFIG_COALITIONS
338 kernel_bootstrap_log("coalitions_init");
339 coalitions_init();
340 #endif
341
342 kernel_bootstrap_log("task_init");
343 task_init();
344
345 kernel_bootstrap_log("thread_init");
346 thread_init();
347
348 #if CONFIG_ATM
349 /* Initialize the Activity Trace Resource Manager. */
350 kernel_bootstrap_log("atm_init");
351 atm_init();
352 #endif
353
354 #if CONFIG_BANK
355 /* Initialize the BANK Manager. */
356 kernel_bootstrap_log("bank_init");
357 bank_init();
358 #endif
359
360 /* initialize the corpse config based on boot-args */
361 corpses_init();
362
363 /*
364 * Create a kernel thread to execute the kernel bootstrap.
365 */
366 kernel_bootstrap_log("kernel_thread_create");
367 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
368
369 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
370
371 thread->state = TH_RUN;
372 thread->last_made_runnable_time = mach_absolute_time();
373 thread_deallocate(thread);
374
375 kernel_bootstrap_log("load_context - done");
376 load_context(thread);
377 /*NOTREACHED*/
378 }
379
380 int kth_started = 0;
381
382 vm_offset_t vm_kernel_addrperm;
383 vm_offset_t buf_kernel_addrperm;
384 vm_offset_t vm_kernel_addrperm_ext;
385
386 /*
387 * Now running in a thread. Kick off other services,
388 * invoke user bootstrap, enter pageout loop.
389 */
390 static void
391 kernel_bootstrap_thread(void)
392 {
393 processor_t processor = current_processor();
394
395 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
396 kernel_bootstrap_thread_log("idle_thread_create");
397 /*
398 * Create the idle processor thread.
399 */
400 idle_thread_create(processor);
401
402 /*
403 * N.B. Do not stick anything else
404 * before this point.
405 *
406 * Start up the scheduler services.
407 */
408 kernel_bootstrap_thread_log("sched_startup");
409 sched_startup();
410
411 /*
412 * Thread lifecycle maintenance (teardown, stack allocation)
413 */
414 kernel_bootstrap_thread_log("thread_daemon_init");
415 thread_daemon_init();
416
417 /* Create kernel map entry reserve */
418 vm_kernel_reserved_entry_init();
419
420 /*
421 * Thread callout service.
422 */
423 kernel_bootstrap_thread_log("thread_call_initialize");
424 thread_call_initialize();
425
426 /*
427 * Remain on current processor as
428 * additional processors come online.
429 */
430 kernel_bootstrap_thread_log("thread_bind");
431 thread_bind(processor);
432
433 /*
434 * Initialize ipc thread call support.
435 */
436 kernel_bootstrap_thread_log("ipc_thread_call_init");
437 ipc_thread_call_init();
438
439 /*
440 * Kick off memory mapping adjustments.
441 */
442 kernel_bootstrap_thread_log("mapping_adjust");
443 mapping_adjust();
444
445 /*
446 * Create the clock service.
447 */
448 kernel_bootstrap_thread_log("clock_service_create");
449 clock_service_create();
450
451 /*
452 * Create the device service.
453 */
454 device_service_create();
455
456 kth_started = 1;
457
458 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
459 /*
460 * Create and initialize the physical copy window for processor 0
461 * This is required before starting kicking off IOKit.
462 */
463 cpu_physwindow_init(0);
464 #endif
465
466
467
468 #if MACH_KDP
469 kernel_bootstrap_log("kdp_init");
470 kdp_init();
471 #endif
472
473 #if ALTERNATE_DEBUGGER
474 alternate_debugger_init();
475 #endif
476
477 #if KPC
478 kpc_init();
479 #endif
480
481 #if CONFIG_ECC_LOGGING
482 ecc_log_init();
483 #endif
484
485 #if KPERF
486 kperf_bootstrap();
487 #endif
488
489 #if HYPERVISOR
490 hv_support_init();
491 #endif
492
493 #if CONFIG_TELEMETRY
494 kernel_bootstrap_log("bootprofile_init");
495 bootprofile_init();
496 #endif
497
498 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
499 vmx_init();
500 #endif
501
502 #if (defined(__i386__) || defined(__x86_64__))
503 if (kdebug_serial) {
504 new_nkdbufs = 1;
505 if (trace_typefilter == 0)
506 trace_typefilter = 1;
507 }
508 if (turn_on_log_leaks && !new_nkdbufs)
509 new_nkdbufs = 200000;
510 if (trace_typefilter)
511 start_kern_tracing_with_typefilter(new_nkdbufs,
512 FALSE,
513 trace_typefilter);
514 else
515 start_kern_tracing(new_nkdbufs, FALSE);
516 if (turn_on_log_leaks)
517 log_leaks = 1;
518
519 #endif
520
521 kernel_bootstrap_log("prng_init");
522 prng_cpu_init(master_cpu);
523
524 #ifdef IOKIT
525 PE_init_iokit();
526 #endif
527
528 assert(ml_get_interrupts_enabled() == FALSE);
529 (void) spllo(); /* Allow interruptions */
530
531 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
532 /*
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
537 * discovery.
538 */
539 cpu_userwindow_init(0);
540 #endif
541
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);
547 else
548 start_kern_tracing(new_nkdbufs, FALSE);
549 if (turn_on_log_leaks)
550 log_leaks = 1;
551 #endif
552
553 /*
554 * Initialize the shared region module.
555 */
556 vm_shared_region_init();
557 vm_commpage_init();
558 vm_commpage_text_init();
559
560 #if CONFIG_MACF
561 kernel_bootstrap_log("mac_policy_initmach");
562 mac_policy_initmach();
563 #endif
564
565
566 #if CONFIG_SCHED_SFI
567 kernel_bootstrap_log("sfi_init");
568 sfi_init();
569 #endif
570
571 /*
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().
579 */
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;
586
587 vm_set_restrictions();
588
589
590
591 /*
592 * Start the user bootstrap.
593 */
594 #ifdef MACH_BSD
595 bsd_init();
596 #endif
597
598 /*
599 * Get rid of segments used to bootstrap kext loading. This removes
600 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
601 */
602 OSKextRemoveKextBootstrap();
603
604 serial_keyboard_init(); /* Start serial keyboard if wanted */
605
606 vm_page_init_local_q();
607
608 thread_bind(PROCESSOR_NULL);
609
610 /*
611 * Become the pageout daemon.
612 */
613 vm_pageout();
614 /*NOTREACHED*/
615 }
616
617 /*
618 * slave_main:
619 *
620 * Load the first thread to start a processor.
621 */
622 void
623 slave_main(void *machine_param)
624 {
625 processor_t processor = current_processor();
626 thread_t thread;
627
628 /*
629 * Use the idle processor thread if there
630 * is no dedicated start up thread.
631 */
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;
636 }
637 else {
638 thread = processor->next_thread;
639 processor->next_thread = THREAD_NULL;
640 }
641
642 load_context(thread);
643 /*NOTREACHED*/
644 }
645
646 /*
647 * processor_start_thread:
648 *
649 * First thread to execute on a started processor.
650 *
651 * Called at splsched.
652 */
653 void
654 processor_start_thread(void *machine_param)
655 {
656 processor_t processor = current_processor();
657 thread_t self = current_thread();
658
659 slave_machine_init(machine_param);
660
661 /*
662 * If running the idle processor thread,
663 * reenter the idle loop, else terminate.
664 */
665 if (self == processor->idle_thread)
666 thread_block((thread_continue_t)idle_thread);
667
668 thread_terminate(self);
669 /*NOTREACHED*/
670 }
671
672 /*
673 * load_context:
674 *
675 * Start the first thread on a processor.
676 */
677 static void
678 load_context(
679 thread_t thread)
680 {
681 processor_t processor = current_processor();
682
683
684 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
685
686 load_context_kprintf("machine_set_current_thread\n");
687 machine_set_current_thread(thread);
688
689 load_context_kprintf("processor_up\n");
690 processor_up(processor);
691
692 PMAP_ACTIVATE_KERNEL(processor->cpu_id);
693
694 /*
695 * Acquire a stack if none attached. The panic
696 * should never occur since the thread is expected
697 * to have reserved stack.
698 */
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");
705 }
706
707 /*
708 * The idle processor threads are not counted as
709 * running for load calculations.
710 */
711 if (!(thread->state & TH_IDLE))
712 sched_run_incr(thread);
713
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;
719
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;
723
724 timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch);
725 PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
726
727 PMAP_ACTIVATE_USER(thread, processor->cpu_id);
728
729 load_context_kprintf("machine_load_context\n");
730 machine_load_context(thread);
731 /*NOTREACHED*/
732 }
733
734 void
735 scale_setup()
736 {
737 int scale = 0;
738 #if defined(__LP64__)
739 typeof(task_max) task_max_base = task_max;
740
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));
744 /* limit to 128 G */
745 if (scale > 16)
746 scale = 16;
747 task_max_base = 2500;
748 } else if ((uint64_t)sane_size >= (uint64_t)(3 * 1024 * 1024 *1024ULL))
749 scale = 2;
750
751 task_max = MAX(task_max, task_max_base * scale);
752
753 if (scale != 0) {
754 task_threadmax = task_max;
755 thread_max = task_max * 5;
756 }
757
758 #endif
759
760 bsd_scale_setup(scale);
761
762 ipc_space_max = SPACE_MAX;
763 ipc_port_max = PORT_MAX;
764 ipc_pset_max = SET_MAX;
765 semaphore_max = SEMAPHORE_MAX;
766 }
767