]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
d2b50a60ac4f9227376e8e01002ad04222613214
[apple/xnu.git] / osfmk / kern / startup.c
1 /*
2 * Copyright (c) 2000-2020 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 <mach_kdp.h>
71
72 #include <mach/boolean.h>
73 #include <mach/machine.h>
74 #include <mach/thread_act.h>
75 #include <mach/task_special_ports.h>
76 #include <mach/vm_param.h>
77 #include <ipc/ipc_init.h>
78 #include <kern/assert.h>
79 #include <kern/mach_param.h>
80 #include <kern/misc_protos.h>
81 #include <kern/clock.h>
82 #include <kern/coalition.h>
83 #include <kern/cpu_number.h>
84 #include <kern/cpu_quiesce.h>
85 #include <kern/ledger.h>
86 #include <kern/machine.h>
87 #include <kern/processor.h>
88 #include <kern/restartable.h>
89 #include <kern/sched_prim.h>
90 #include <kern/turnstile.h>
91 #if CONFIG_SCHED_SFI
92 #include <kern/sfi.h>
93 #endif
94 #include <kern/startup.h>
95 #include <kern/task.h>
96 #include <kern/thread.h>
97 #include <kern/timer.h>
98 #if CONFIG_TELEMETRY
99 #include <kern/telemetry.h>
100 #endif
101 #include <kern/zalloc.h>
102 #include <kern/locks.h>
103 #include <kern/debug.h>
104 #if KPERF
105 #include <kperf/kperf.h>
106 #endif /* KPERF */
107 #include <corpses/task_corpse.h>
108 #include <prng/random.h>
109 #include <console/serial_protos.h>
110 #include <vm/vm_kern.h>
111 #include <vm/vm_init.h>
112 #include <vm/vm_map.h>
113 #include <vm/vm_object.h>
114 #include <vm/vm_page.h>
115 #include <vm/vm_pageout.h>
116 #include <vm/vm_shared_region.h>
117 #include <machine/pmap.h>
118 #include <machine/commpage.h>
119 #include <machine/machine_routines.h>
120 #include <libkern/version.h>
121 #include <sys/codesign.h>
122 #include <sys/kdebug.h>
123 #include <sys/random.h>
124 #include <sys/ktrace.h>
125 #include <libkern/section_keywords.h>
126
127 #include <kern/ltable.h>
128 #include <kern/waitq.h>
129 #include <ipc/ipc_voucher.h>
130 #include <voucher/ipc_pthread_priority_internal.h>
131 #include <mach/host_info.h>
132 #include <pthread/workqueue_internal.h>
133
134 #if CONFIG_XNUPOST
135 #include <tests/ktest.h>
136 #include <tests/xnupost.h>
137 #endif
138
139 #if CONFIG_ATM
140 #include <atm/atm_internal.h>
141 #endif
142
143 #if CONFIG_CSR
144 #include <sys/csr.h>
145 #endif
146
147 #include <bank/bank_internal.h>
148
149 #if ALTERNATE_DEBUGGER
150 #include <arm64/alternate_debugger.h>
151 #endif
152
153 #if MACH_KDP
154 #include <kdp/kdp.h>
155 #endif
156
157 #if CONFIG_MACF
158 #include <security/mac_mach_internal.h>
159 #if CONFIG_VNGUARD
160 extern void vnguard_policy_init(void);
161 #endif
162 #endif
163
164 #if KPC
165 #include <kern/kpc.h>
166 #endif
167
168 #if HYPERVISOR
169 #include <kern/hv_support.h>
170 #endif
171
172 #include <san/kasan.h>
173
174 #include <i386/pmCPU.h>
175 static void kernel_bootstrap_thread(void);
176
177 static void load_context(
178 thread_t thread);
179
180 #if CONFIG_ECC_LOGGING
181 #include <kern/ecc.h>
182 #endif
183
184 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
185 #include <i386/vmx/vmx_cpu.h>
186 #endif
187
188 #if CONFIG_DTRACE
189 extern void dtrace_early_init(void);
190 extern void sdt_early_init(void);
191 #endif
192
193 // libkern/OSKextLib.cpp
194 extern void OSKextRemoveKextBootstrap(void);
195
196 void scale_setup(void);
197 extern void bsd_scale_setup(int);
198 extern unsigned int semaphore_max;
199 extern void stackshot_init(void);
200 extern void ktrace_init(void);
201
202 /*
203 * Running in virtual memory, on the interrupt stack.
204 */
205
206 extern struct startup_entry startup_entries[]
207 __SECTION_START_SYM(STARTUP_HOOK_SEGMENT, STARTUP_HOOK_SECTION);
208
209 extern struct startup_entry startup_entries_end[]
210 __SECTION_END_SYM(STARTUP_HOOK_SEGMENT, STARTUP_HOOK_SECTION);
211
212 static struct startup_entry *__startup_data startup_entry_cur = startup_entries;
213
214 SECURITY_READ_ONLY_LATE(startup_subsystem_id_t) startup_phase = STARTUP_SUB_NONE;
215
216 extern int serverperfmode;
217
218 #if DEBUG || DEVELOPMENT
219 TUNABLE(startup_debug_t, startup_debug, "startup_debug", 0);
220 #endif
221
222 /* size of kernel trace buffer, disabled by default */
223 TUNABLE(unsigned int, new_nkdbufs, "trace", 0);
224 TUNABLE(unsigned int, wake_nkdbufs, "trace_wake", 0);
225 TUNABLE(unsigned int, write_trace_on_panic, "trace_panic", 0);
226 TUNABLE(unsigned int, trace_wrap, "trace_wrap", 0);
227
228 /* mach leak logging */
229 TUNABLE(int, log_leaks, "-l", 0);
230
231 static inline void
232 kernel_bootstrap_log(const char *message)
233 {
234 if ((startup_debug & STARTUP_DEBUG_VERBOSE) &&
235 startup_phase >= STARTUP_SUB_KPRINTF) {
236 kprintf("kernel_bootstrap: %s\n", message);
237 }
238 kernel_debug_string_early(message);
239 }
240
241 static inline void
242 kernel_bootstrap_thread_log(const char *message)
243 {
244 if ((startup_debug & STARTUP_DEBUG_VERBOSE) &&
245 startup_phase >= STARTUP_SUB_KPRINTF) {
246 kprintf("kernel_bootstrap_thread: %s\n", message);
247 }
248 kernel_debug_string_early(message);
249 }
250
251 extern void
252 qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *));
253
254 __startup_func
255 static int
256 startup_entry_cmp(const void *e1, const void *e2)
257 {
258 const struct startup_entry *a = e1;
259 const struct startup_entry *b = e2;
260 if (a->subsystem == b->subsystem) {
261 if (a->rank == b->rank) {
262 return 0;
263 }
264 return a->rank > b->rank ? 1 : -1;
265 }
266 return a->subsystem > b->subsystem ? 1 : -1;
267 }
268
269 __startup_func
270 void
271 kernel_startup_bootstrap(void)
272 {
273 /*
274 * Sort the various STARTUP() entries by subsystem/rank.
275 */
276 size_t n = startup_entries_end - startup_entries;
277
278 if (n == 0) {
279 panic("Section %s,%s missing",
280 STARTUP_HOOK_SEGMENT, STARTUP_HOOK_SECTION);
281 }
282 if (((uintptr_t)startup_entries_end - (uintptr_t)startup_entries) %
283 sizeof(struct startup_entry)) {
284 panic("Section %s,%s has invalid size",
285 STARTUP_HOOK_SEGMENT, STARTUP_HOOK_SECTION);
286 }
287
288 qsort(startup_entries, n, sizeof(struct startup_entry), startup_entry_cmp);
289
290 /*
291 * Then initialize all tunables, and early locks
292 */
293 kernel_startup_initialize_upto(STARTUP_SUB_LOCKS_EARLY);
294 }
295
296 __startup_func
297 extern void
298 kernel_startup_tunable_init(const struct startup_tunable_spec *spec)
299 {
300 if (PE_parse_boot_argn(spec->name, spec->var_addr, spec->var_len)) {
301 if (spec->var_is_bool) {
302 /* make sure bool's are valued in {0, 1} */
303 *(bool *)spec->var_addr = *(uint8_t *)spec->var_addr;
304 }
305 }
306 }
307
308 static void
309 kernel_startup_log(startup_subsystem_id_t subsystem)
310 {
311 static const char *names[] = {
312 [STARTUP_SUB_TUNABLES] = "tunables",
313 [STARTUP_SUB_LOCKS_EARLY] = "locks_early",
314 [STARTUP_SUB_KPRINTF] = "kprintf",
315
316 [STARTUP_SUB_PMAP_STEAL] = "pmap_steal",
317 [STARTUP_SUB_VM_KERNEL] = "vm_kernel",
318 [STARTUP_SUB_KMEM] = "kmem",
319 [STARTUP_SUB_KMEM_ALLOC] = "kmem_alloc",
320 [STARTUP_SUB_ZALLOC] = "zalloc",
321 [STARTUP_SUB_PERCPU] = "percpu",
322 [STARTUP_SUB_LOCKS] = "locks",
323
324 [STARTUP_SUB_CODESIGNING] = "codesigning",
325 [STARTUP_SUB_OSLOG] = "oslog",
326 [STARTUP_SUB_MACH_IPC] = "mach_ipc",
327 [STARTUP_SUB_EARLY_BOOT] = "early_boot",
328
329 /* LOCKDOWN is special and its value won't fit here. */
330 };
331 static startup_subsystem_id_t logged = STARTUP_SUB_NONE;
332
333 if (subsystem <= logged) {
334 return;
335 }
336
337 if (subsystem < sizeof(names) / sizeof(names[0]) && names[subsystem]) {
338 kernel_bootstrap_log(names[subsystem]);
339 }
340 logged = subsystem;
341 }
342
343 __startup_func
344 void
345 kernel_startup_initialize_upto(startup_subsystem_id_t upto)
346 {
347 struct startup_entry *cur = startup_entry_cur;
348
349 assert(startup_phase < upto);
350
351 while (cur < startup_entries_end && cur->subsystem <= upto) {
352 if ((startup_debug & STARTUP_DEBUG_VERBOSE) &&
353 startup_phase >= STARTUP_SUB_KPRINTF) {
354 kprintf("%s[%d, rank %d]: %p(%p)\n", __func__,
355 cur->subsystem, cur->rank, cur->func, cur->arg);
356 }
357 startup_phase = cur->subsystem - 1;
358 kernel_startup_log(cur->subsystem);
359 cur->func(cur->arg);
360 startup_entry_cur = ++cur;
361 }
362 kernel_startup_log(upto);
363
364 if ((startup_debug & STARTUP_DEBUG_VERBOSE) &&
365 upto >= STARTUP_SUB_KPRINTF) {
366 kprintf("%s: reached phase %d\n", __func__, upto);
367 }
368 startup_phase = upto;
369 }
370
371 void
372 kernel_bootstrap(void)
373 {
374 kern_return_t result;
375 thread_t thread;
376 char namep[16];
377
378 printf("%s\n", version); /* log kernel version */
379
380 scale_setup();
381
382 kernel_bootstrap_log("vm_mem_bootstrap");
383 vm_mem_bootstrap();
384
385 machine_info.memory_size = (uint32_t)mem_size;
386 #if XNU_TARGET_OS_OSX
387 machine_info.max_mem = max_mem_actual;
388 #else
389 machine_info.max_mem = max_mem;
390 #endif /* XNU_TARGET_OS_OSX */
391 machine_info.major_version = version_major;
392 machine_info.minor_version = version_minor;
393
394 kernel_startup_initialize_upto(STARTUP_SUB_OSLOG);
395
396 #if KASAN
397 kernel_bootstrap_log("kasan_late_init");
398 kasan_late_init();
399 #endif
400
401 #if CONFIG_TELEMETRY
402 kernel_bootstrap_log("telemetry_init");
403 telemetry_init();
404 #endif
405
406 if (PE_i_can_has_debugger(NULL)) {
407 if (PE_parse_boot_argn("-show_pointers", &namep, sizeof(namep))) {
408 doprnt_hide_pointers = FALSE;
409 }
410 if (PE_parse_boot_argn("-no_slto_panic", &namep, sizeof(namep))) {
411 extern boolean_t spinlock_timeout_panic;
412 spinlock_timeout_panic = FALSE;
413 }
414 }
415
416 kernel_bootstrap_log("console_init");
417 console_init();
418
419 kernel_bootstrap_log("stackshot_init");
420 stackshot_init();
421
422 kernel_bootstrap_log("sched_init");
423 sched_init();
424
425 kernel_bootstrap_log("waitq_bootstrap");
426 waitq_bootstrap();
427
428 #if CONFIG_MACF
429 kernel_bootstrap_log("mac_policy_init");
430 mac_policy_init();
431 #endif
432
433 kernel_startup_initialize_upto(STARTUP_SUB_MACH_IPC);
434
435 /*
436 * As soon as the virtual memory system is up, we record
437 * that this CPU is using the kernel pmap.
438 */
439 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
440 PMAP_ACTIVATE_KERNEL(master_cpu);
441
442 kernel_bootstrap_log("mapping_free_prime");
443 mapping_free_prime(); /* Load up with temporary mapping blocks */
444
445 kernel_bootstrap_log("machine_init");
446 machine_init();
447
448 kernel_bootstrap_log("thread_machine_init_template");
449 thread_machine_init_template();
450
451 kernel_bootstrap_log("clock_init");
452 clock_init();
453
454 /*
455 * Initialize the IPC, task, and thread subsystems.
456 */
457 #if CONFIG_THREAD_GROUPS
458 kernel_bootstrap_log("thread_group_init");
459 thread_group_init();
460 #endif
461
462 #if CONFIG_COALITIONS
463 kernel_bootstrap_log("coalitions_init");
464 coalitions_init();
465 #endif
466
467 kernel_bootstrap_log("task_init");
468 task_init();
469
470 kernel_bootstrap_log("thread_init");
471 thread_init();
472
473 kernel_bootstrap_log("restartable_init");
474 restartable_init();
475
476 kernel_bootstrap_log("workq_init");
477 workq_init();
478
479 kernel_bootstrap_log("turnstiles_init");
480 turnstiles_init();
481
482 #if CONFIG_ATM
483 /* Initialize the Activity Trace Resource Manager. */
484 kernel_bootstrap_log("atm_init");
485 atm_init();
486 #endif
487 kernel_bootstrap_log("mach_init_activity_id");
488 mach_init_activity_id();
489
490 /* Initialize the BANK Manager. */
491 kernel_bootstrap_log("bank_init");
492 bank_init();
493
494 kernel_bootstrap_log("ipc_pthread_priority_init");
495 ipc_pthread_priority_init();
496
497 /* initialize the corpse config based on boot-args */
498 corpses_init();
499
500 /* initialize host_statistics */
501 host_statistics_init();
502
503 /* initialize exceptions */
504 kernel_bootstrap_log("exception_init");
505 exception_init();
506
507 #if CONFIG_SCHED_SFI
508 kernel_bootstrap_log("sfi_init");
509 sfi_init();
510 #endif
511
512 /*
513 * Create a kernel thread to execute the kernel bootstrap.
514 */
515
516 kernel_bootstrap_log("kernel_thread_create");
517 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
518
519 if (result != KERN_SUCCESS) {
520 panic("kernel_bootstrap: result = %08X\n", result);
521 }
522
523 /* The static init_thread is re-used as the bootstrap thread */
524 assert(thread == current_thread());
525
526 /* TODO: do a proper thread_start() (without the thread_setrun()) */
527 thread->state = TH_RUN;
528 thread->last_made_runnable_time = mach_absolute_time();
529 thread_set_thread_name(thread, "kernel_bootstrap_thread");
530
531 thread_deallocate(thread);
532
533 kernel_bootstrap_log("load_context - done");
534 load_context(thread);
535 /*NOTREACHED*/
536 }
537
538 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_addrperm;
539 SECURITY_READ_ONLY_LATE(vm_offset_t) buf_kernel_addrperm;
540 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_addrperm_ext;
541 SECURITY_READ_ONLY_LATE(uint64_t) vm_kernel_addrhash_salt;
542 SECURITY_READ_ONLY_LATE(uint64_t) vm_kernel_addrhash_salt_ext;
543
544 /*
545 * Now running in a thread. Kick off other services,
546 * invoke user bootstrap, enter pageout loop.
547 */
548 static void
549 kernel_bootstrap_thread(void)
550 {
551 processor_t processor = current_processor();
552
553 kernel_bootstrap_thread_log("idle_thread_create");
554 /*
555 * Create the idle processor thread.
556 */
557 idle_thread_create(processor);
558
559 /*
560 * N.B. Do not stick anything else
561 * before this point.
562 *
563 * Start up the scheduler services.
564 */
565 kernel_bootstrap_thread_log("sched_startup");
566 sched_startup();
567
568 /*
569 * Thread lifecycle maintenance (teardown, stack allocation)
570 */
571 kernel_bootstrap_thread_log("thread_daemon_init");
572 thread_daemon_init();
573
574 /* Create kernel map entry reserve */
575 vm_kernel_reserved_entry_init();
576
577 /*
578 * Thread callout service.
579 */
580 kernel_bootstrap_thread_log("thread_call_initialize");
581 thread_call_initialize();
582
583 /*
584 * Work interval subsystem initialization.
585 * Needs to be done once thread calls have been initialized.
586 */
587 kernel_bootstrap_thread_log("work_interval_initialize");
588 work_interval_subsystem_init();
589
590 /*
591 * Remain on current processor as
592 * additional processors come online.
593 */
594 kernel_bootstrap_thread_log("thread_bind");
595 thread_bind(processor);
596
597 /*
598 * Initialize ipc thread call support.
599 */
600 kernel_bootstrap_thread_log("ipc_thread_call_init");
601 ipc_thread_call_init();
602
603 /*
604 * Kick off memory mapping adjustments.
605 */
606 kernel_bootstrap_thread_log("mapping_adjust");
607 mapping_adjust();
608
609 /*
610 * Create the clock service.
611 */
612 kernel_bootstrap_thread_log("clock_service_create");
613 clock_service_create();
614
615 /*
616 * Create the device service.
617 */
618 device_service_create();
619
620 phys_carveout_init();
621
622 #if MACH_KDP
623 kernel_bootstrap_log("kdp_init");
624 kdp_init();
625 #endif
626
627 #if ALTERNATE_DEBUGGER
628 alternate_debugger_init();
629 #endif
630
631 #if KPC
632 kpc_init();
633 #endif
634
635 #if HYPERVISOR
636 kernel_bootstrap_thread_log("hv_support_init");
637 hv_support_init();
638 #endif
639
640 #if CONFIG_TELEMETRY
641 kernel_bootstrap_log("bootprofile_init");
642 bootprofile_init();
643 #endif
644
645 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
646 vmx_init();
647 #endif
648
649 kernel_bootstrap_thread_log("ktrace_init");
650 ktrace_init();
651
652 char trace_typefilter[256] = {};
653 PE_parse_boot_arg_str("trace_typefilter", trace_typefilter,
654 sizeof(trace_typefilter));
655 #if KPERF
656 kperf_init();
657 #endif /* KPERF */
658 kdebug_init(new_nkdbufs, trace_typefilter,
659 (trace_wrap ? KDOPT_WRAPPING : 0) | KDOPT_ATBOOT);
660
661 #ifdef MACH_BSD
662 kernel_bootstrap_log("bsd_early_init");
663 bsd_early_init();
664 #endif
665
666 #ifdef IOKIT
667 kernel_bootstrap_log("PE_init_iokit");
668 PE_init_iokit();
669 #endif
670
671 assert(ml_get_interrupts_enabled() == FALSE);
672
673 /*
674 * Past this point, kernel subsystems that expect to operate with
675 * interrupts or preemption enabled may begin enforcement.
676 */
677 kernel_startup_initialize_upto(STARTUP_SUB_EARLY_BOOT);
678
679 #if INTERRUPT_MASKED_DEBUG
680 // Reset interrupts masked timeout before we enable interrupts
681 ml_spin_debug_clear_self();
682 #endif
683 (void) spllo(); /* Allow interruptions */
684
685 /*
686 * This will start displaying progress to the user, start as early as possible
687 */
688 initialize_screen(NULL, kPEAcquireScreen);
689
690 /*
691 * Initialize the shared region module.
692 */
693 vm_commpage_init();
694 vm_commpage_text_init();
695
696 #if CONFIG_MACF
697 kernel_bootstrap_log("mac_policy_initmach");
698 mac_policy_initmach();
699 #if CONFIG_VNGUARD
700 kernel_bootstrap_log("vnguard_policy_init");
701 vnguard_policy_init();
702 #endif
703 #endif
704
705 #if CONFIG_DTRACE
706 kernel_bootstrap_log("dtrace_early_init");
707 dtrace_early_init();
708 sdt_early_init();
709 #endif
710
711
712 kernel_startup_initialize_upto(STARTUP_SUB_LOCKDOWN);
713
714 /*
715 * Get rid of segments used to bootstrap kext loading. This removes
716 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
717 * Must be done prior to lockdown so that we can free (and possibly relocate)
718 * the static KVA mappings used for the jettisoned bootstrap segments.
719 */
720 kernel_bootstrap_log("OSKextRemoveKextBootstrap");
721 OSKextRemoveKextBootstrap();
722
723 /*
724 * Initialize the globals used for permuting kernel
725 * addresses that may be exported to userland as tokens
726 * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL().
727 * Force the random number to be odd to avoid mapping a non-zero
728 * word-aligned address to zero via addition.
729 * Note: at this stage we can use the cryptographically secure PRNG
730 * rather than early_random().
731 */
732 read_random(&vm_kernel_addrperm, sizeof(vm_kernel_addrperm));
733 vm_kernel_addrperm |= 1;
734 read_random(&buf_kernel_addrperm, sizeof(buf_kernel_addrperm));
735 buf_kernel_addrperm |= 1;
736 read_random(&vm_kernel_addrperm_ext, sizeof(vm_kernel_addrperm_ext));
737 vm_kernel_addrperm_ext |= 1;
738 read_random(&vm_kernel_addrhash_salt, sizeof(vm_kernel_addrhash_salt));
739 read_random(&vm_kernel_addrhash_salt_ext, sizeof(vm_kernel_addrhash_salt_ext));
740
741 /* No changes to kernel text and rodata beyond this point. */
742 kernel_bootstrap_log("machine_lockdown");
743 machine_lockdown();
744
745 #ifdef IOKIT
746 kernel_bootstrap_log("PE_lockdown_iokit");
747 PE_lockdown_iokit();
748 #endif
749 /*
750 * max_cpus must be nailed down by the time PE_lockdown_iokit() finishes,
751 * at the latest
752 */
753 vm_set_restrictions(machine_info.max_cpus);
754
755 #ifdef CONFIG_XNUPOST
756 kern_return_t result = kernel_list_tests();
757 result = kernel_do_post();
758 if (result != KERN_SUCCESS) {
759 panic("kernel_do_post: Tests failed with result = 0x%08x\n", result);
760 }
761 kernel_bootstrap_log("kernel_do_post - done");
762 #endif /* CONFIG_XNUPOST */
763
764
765 #if KPERF
766 kperf_init_early();
767 #endif
768
769 /*
770 * Start the user bootstrap.
771 */
772 #ifdef MACH_BSD
773 bsd_init();
774 #endif
775
776
777 /*
778 * Get rid of pages used for early boot tracing.
779 */
780 kdebug_free_early_buf();
781
782 serial_keyboard_init(); /* Start serial keyboard if wanted */
783
784 vm_page_init_local_q(machine_info.max_cpus);
785
786 thread_bind(PROCESSOR_NULL);
787
788 /*
789 * Now that all CPUs are available to run threads, this is essentially
790 * a background thread. Take this opportunity to initialize and free
791 * any remaining vm_pages that were delayed earlier by pmap_startup().
792 */
793 vm_free_delayed_pages();
794
795 /*
796 * Become the pageout daemon.
797 */
798 vm_pageout();
799 /*NOTREACHED*/
800 }
801
802 /*
803 * slave_main:
804 *
805 * Load the first thread to start a processor.
806 * This path will also be used by the master processor
807 * after being offlined.
808 */
809 void
810 slave_main(void *machine_param)
811 {
812 processor_t processor = current_processor();
813 thread_t thread;
814
815 /*
816 * Use the idle processor thread if there
817 * is no dedicated start up thread.
818 */
819 if (processor->processor_offlined == true) {
820 /* Return to the saved processor_offline context */
821 assert(processor->startup_thread == THREAD_NULL);
822
823 thread = processor->idle_thread;
824 thread->parameter = machine_param;
825 } else if (processor->startup_thread) {
826 thread = processor->startup_thread;
827 processor->startup_thread = THREAD_NULL;
828 } else {
829 thread = processor->idle_thread;
830 thread->continuation = processor_start_thread;
831 thread->parameter = machine_param;
832 }
833
834 load_context(thread);
835 /*NOTREACHED*/
836 }
837
838 /*
839 * processor_start_thread:
840 *
841 * First thread to execute on a started processor.
842 *
843 * Called at splsched.
844 */
845 void
846 processor_start_thread(void *machine_param,
847 __unused wait_result_t result)
848 {
849 processor_t processor = current_processor();
850 thread_t self = current_thread();
851
852 slave_machine_init(machine_param);
853
854 /*
855 * If running the idle processor thread,
856 * reenter the idle loop, else terminate.
857 */
858 if (self == processor->idle_thread) {
859 thread_block(idle_thread);
860 }
861
862 thread_terminate(self);
863 /*NOTREACHED*/
864 }
865
866 /*
867 * load_context:
868 *
869 * Start the first thread on a processor.
870 * This may be the first thread ever run on a processor, or
871 * it could be a processor that was previously offlined.
872 */
873 static void __attribute__((noreturn))
874 load_context(
875 thread_t thread)
876 {
877 processor_t processor = current_processor();
878
879
880 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
881
882 load_context_kprintf("machine_set_current_thread\n");
883 machine_set_current_thread(thread);
884
885 load_context_kprintf("processor_up\n");
886
887 PMAP_ACTIVATE_KERNEL(processor->cpu_id);
888
889 /*
890 * Acquire a stack if none attached. The panic
891 * should never occur since the thread is expected
892 * to have reserved stack.
893 */
894 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread,
895 thread->kernel_stack, thread->machine.kstackptr);
896 if (!thread->kernel_stack) {
897 load_context_kprintf("stack_alloc_try\n");
898 if (!stack_alloc_try(thread)) {
899 panic("load_context");
900 }
901 }
902
903 /*
904 * The idle processor threads are not counted as
905 * running for load calculations.
906 */
907 if (!(thread->state & TH_IDLE)) {
908 SCHED(run_count_incr)(thread);
909 }
910
911 processor->active_thread = thread;
912 processor_state_update_explicit(processor, thread->sched_pri,
913 SFI_CLASS_KERNEL, PSET_SMP, thread_get_perfcontrol_class(thread), THREAD_URGENCY_NONE,
914 ((thread->state & TH_IDLE) || (thread->bound_processor != PROCESSOR_NULL)) ? TH_BUCKET_SCHED_MAX : thread->th_sched_bucket);
915 processor->current_is_bound = thread->bound_processor != PROCESSOR_NULL;
916 processor->current_is_NO_SMT = false;
917 #if CONFIG_THREAD_GROUPS
918 processor->current_thread_group = thread_group_get(thread);
919 #endif
920 processor->starting_pri = thread->sched_pri;
921 processor->deadline = UINT64_MAX;
922 thread->last_processor = processor;
923 processor_up(processor);
924 processor->last_dispatch = mach_absolute_time();
925 timer_start(&thread->system_timer, processor->last_dispatch);
926 processor->thread_timer = processor->kernel_timer = &thread->system_timer;
927
928 timer_start(&processor->system_state, processor->last_dispatch);
929 processor->current_state = &processor->system_state;
930
931
932 cpu_quiescent_counter_join(processor->last_dispatch);
933
934 PMAP_ACTIVATE_USER(thread, processor->cpu_id);
935
936 load_context_kprintf("machine_load_context\n");
937
938 machine_load_context(thread);
939 /*NOTREACHED*/
940 }
941
942 void
943 scale_setup(void)
944 {
945 int scale = 0;
946 #if defined(__LP64__)
947 typeof(task_max) task_max_base = task_max;
948
949 /* Raise limits for servers with >= 16G */
950 if ((serverperfmode != 0) && ((uint64_t)max_mem_actual >= (uint64_t)(16 * 1024 * 1024 * 1024ULL))) {
951 scale = (int)((uint64_t)sane_size / (uint64_t)(8 * 1024 * 1024 * 1024ULL));
952 /* limit to 128 G */
953 if (scale > 16) {
954 scale = 16;
955 }
956 task_max_base = 2500;
957 /* Raise limits for machines with >= 3GB */
958 } else if ((uint64_t)max_mem_actual >= (uint64_t)(3 * 1024 * 1024 * 1024ULL)) {
959 if ((uint64_t)max_mem_actual < (uint64_t)(8 * 1024 * 1024 * 1024ULL)) {
960 scale = 2;
961 } else {
962 /* limit to 64GB */
963 scale = MIN(16, (int)((uint64_t)max_mem_actual / (uint64_t)(4 * 1024 * 1024 * 1024ULL)));
964 }
965 }
966
967 task_max = MAX(task_max, task_max_base * scale);
968
969 if (scale != 0) {
970 task_threadmax = task_max;
971 thread_max = task_max * 5;
972 }
973
974 #endif
975
976 bsd_scale_setup(scale);
977 }