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