]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2018 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 A |
103 | #include <kern/debug.h> |
104 | #include <corpses/task_corpse.h> | |
fe8ab488 | 105 | #include <prng/random.h> |
0c530ab8 | 106 | #include <console/serial_protos.h> |
1c79356b A |
107 | #include <vm/vm_kern.h> |
108 | #include <vm/vm_init.h> | |
109 | #include <vm/vm_map.h> | |
110 | #include <vm/vm_object.h> | |
111 | #include <vm/vm_page.h> | |
112 | #include <vm/vm_pageout.h> | |
2d21ac55 | 113 | #include <vm/vm_shared_region.h> |
1c79356b | 114 | #include <machine/pmap.h> |
43866e37 | 115 | #include <machine/commpage.h> |
91447636 | 116 | #include <libkern/version.h> |
39236c6e | 117 | #include <sys/codesign.h> |
6d2010ae | 118 | #include <sys/kdebug.h> |
fe8ab488 | 119 | #include <sys/random.h> |
39037602 | 120 | #include <sys/ktrace.h> |
d9a64523 | 121 | #include <libkern/section_keywords.h> |
fe8ab488 | 122 | |
39037602 | 123 | #include <kern/ltable.h> |
3e170ce0 | 124 | #include <kern/waitq.h> |
39037602 A |
125 | #include <ipc/ipc_voucher.h> |
126 | #include <voucher/ipc_pthread_priority_internal.h> | |
cc8bc92a | 127 | #include <mach/host_info.h> |
d9a64523 | 128 | #include <pthread/workqueue_internal.h> |
3e170ce0 | 129 | |
d9a64523 A |
130 | #if CONFIG_XNUPOST |
131 | #include <tests/ktest.h> | |
132 | #include <tests/xnupost.h> | |
133 | #endif | |
3e170ce0 | 134 | |
fe8ab488 A |
135 | #if CONFIG_ATM |
136 | #include <atm/atm_internal.h> | |
137 | #endif | |
138 | ||
139 | #if CONFIG_CSR | |
140 | #include <sys/csr.h> | |
141 | #endif | |
142 | ||
fe8ab488 | 143 | #include <bank/bank_internal.h> |
fe8ab488 A |
144 | |
145 | #if ALTERNATE_DEBUGGER | |
146 | #include <arm64/alternate_debugger.h> | |
147 | #endif | |
1c79356b | 148 | |
593a1d5f A |
149 | #if MACH_KDP |
150 | #include <kdp/kdp.h> | |
151 | #endif | |
152 | ||
2d21ac55 A |
153 | #if CONFIG_MACF |
154 | #include <security/mac_mach_internal.h> | |
5ba3f43e A |
155 | #if CONFIG_VNGUARD |
156 | extern void vnguard_policy_init(void); | |
157 | #endif | |
2d21ac55 A |
158 | #endif |
159 | ||
39236c6e A |
160 | #if KPC |
161 | #include <kern/kpc.h> | |
162 | #endif | |
163 | ||
fe8ab488 A |
164 | #if HYPERVISOR |
165 | #include <kern/hv_support.h> | |
166 | #endif | |
167 | ||
5ba3f43e A |
168 | #include <san/kasan.h> |
169 | ||
170 | #if defined(__arm__) || defined(__arm64__) | |
171 | #include <arm/misc_protos.h> // for arm_vm_prot_finalize | |
172 | #endif | |
39236c6e | 173 | |
bd504ef0 | 174 | #include <i386/pmCPU.h> |
0a7de745 | 175 | static void kernel_bootstrap_thread(void); |
1c79356b | 176 | |
0a7de745 A |
177 | static void load_context( |
178 | thread_t thread); | |
b0d623f7 | 179 | #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0 |
2d21ac55 A |
180 | extern void cpu_userwindow_init(int); |
181 | extern void cpu_physwindow_init(int); | |
0c530ab8 A |
182 | #endif |
183 | ||
fe8ab488 A |
184 | #if CONFIG_ECC_LOGGING |
185 | #include <kern/ecc.h> | |
0a7de745 | 186 | #endif |
fe8ab488 A |
187 | |
188 | #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX | |
189 | #include <i386/vmx/vmx_cpu.h> | |
190 | #endif | |
191 | ||
d9a64523 A |
192 | #if CONFIG_DTRACE |
193 | extern void dtrace_early_init(void); | |
194 | extern void sdt_early_init(void); | |
195 | #endif | |
196 | ||
b0d623f7 | 197 | // libkern/OSKextLib.cpp |
d9a64523 | 198 | extern void OSKextRemoveKextBootstrap(void); |
b0d623f7 | 199 | |
6d2010ae A |
200 | void scale_setup(void); |
201 | extern void bsd_scale_setup(int); | |
b0d623f7 | 202 | extern unsigned int semaphore_max; |
39037602 A |
203 | extern void stackshot_init(void); |
204 | extern void ktrace_init(void); | |
205 | extern void oslog_init(void); | |
b0d623f7 | 206 | |
1c79356b A |
207 | /* |
208 | * Running in virtual memory, on the interrupt stack. | |
1c79356b | 209 | */ |
0c530ab8 | 210 | |
6d2010ae A |
211 | extern int serverperfmode; |
212 | ||
213 | /* size of kernel trace buffer, disabled by default */ | |
214 | unsigned int new_nkdbufs = 0; | |
bd504ef0 | 215 | unsigned int wake_nkdbufs = 0; |
fe8ab488 | 216 | unsigned int write_trace_on_panic = 0; |
a39ff7e2 | 217 | unsigned int trace_wrap = 0; |
39037602 | 218 | boolean_t trace_serial = FALSE; |
5ba3f43e | 219 | boolean_t early_boot_complete = FALSE; |
6d2010ae A |
220 | |
221 | /* mach leak logging */ | |
222 | int log_leaks = 0; | |
6d2010ae | 223 | |
fe8ab488 A |
224 | static inline void |
225 | kernel_bootstrap_log(const char *message) | |
226 | { | |
227 | // kprintf("kernel_bootstrap: %s\n", message); | |
39037602 | 228 | kernel_debug_string_early(message); |
fe8ab488 A |
229 | } |
230 | ||
231 | static inline void | |
232 | kernel_bootstrap_thread_log(const char *message) | |
233 | { | |
234 | // kprintf("kernel_bootstrap_thread: %s\n", message); | |
39037602 | 235 | kernel_debug_string_early(message); |
fe8ab488 | 236 | } |
6d2010ae A |
237 | |
238 | void | |
239 | kernel_early_bootstrap(void) | |
240 | { | |
15129b1c | 241 | /* serverperfmode is needed by timer setup */ |
0a7de745 A |
242 | if (PE_parse_boot_argn("serverperfmode", &serverperfmode, sizeof(serverperfmode))) { |
243 | serverperfmode = 1; | |
244 | } | |
6d2010ae | 245 | |
3e170ce0 | 246 | #if CONFIG_SCHED_SFI |
fe8ab488 A |
247 | /* |
248 | * Configure SFI classes | |
249 | */ | |
250 | sfi_early_init(); | |
3e170ce0 | 251 | #endif |
6d2010ae A |
252 | } |
253 | ||
b0d623f7 | 254 | |
1c79356b | 255 | void |
91447636 | 256 | kernel_bootstrap(void) |
1c79356b | 257 | { |
0a7de745 A |
258 | kern_return_t result; |
259 | thread_t thread; | |
260 | char namep[16]; | |
1c79356b | 261 | |
2d21ac55 A |
262 | printf("%s\n", version); /* log kernel version */ |
263 | ||
0a7de745 | 264 | if (PE_parse_boot_argn("-l", namep, sizeof(namep))) { /* leaks logging */ |
39037602 | 265 | log_leaks = 1; |
0a7de745 | 266 | } |
b0d623f7 | 267 | |
0a7de745 A |
268 | PE_parse_boot_argn("trace", &new_nkdbufs, sizeof(new_nkdbufs)); |
269 | PE_parse_boot_argn("trace_wake", &wake_nkdbufs, sizeof(wake_nkdbufs)); | |
fe8ab488 | 270 | PE_parse_boot_argn("trace_panic", &write_trace_on_panic, sizeof(write_trace_on_panic)); |
a39ff7e2 | 271 | PE_parse_boot_argn("trace_wrap", &trace_wrap, sizeof(trace_wrap)); |
bd504ef0 | 272 | |
6d2010ae | 273 | scale_setup(); |
2d21ac55 | 274 | |
fe8ab488 | 275 | kernel_bootstrap_log("vm_mem_bootstrap"); |
b0d623f7 A |
276 | vm_mem_bootstrap(); |
277 | ||
fe8ab488 | 278 | kernel_bootstrap_log("cs_init"); |
39236c6e A |
279 | cs_init(); |
280 | ||
fe8ab488 | 281 | kernel_bootstrap_log("vm_mem_init"); |
b0d623f7 A |
282 | vm_mem_init(); |
283 | ||
284 | machine_info.memory_size = (uint32_t)mem_size; | |
285 | machine_info.max_mem = max_mem; | |
286 | machine_info.major_version = version_major; | |
287 | machine_info.minor_version = version_minor; | |
288 | ||
39037602 | 289 | oslog_init(); |
3e170ce0 | 290 | |
5ba3f43e A |
291 | #if KASAN |
292 | kernel_bootstrap_log("kasan_late_init"); | |
293 | kasan_late_init(); | |
294 | #endif | |
295 | ||
39236c6e | 296 | #if CONFIG_TELEMETRY |
fe8ab488 | 297 | kernel_bootstrap_log("telemetry_init"); |
39236c6e A |
298 | telemetry_init(); |
299 | #endif | |
300 | ||
3e170ce0 A |
301 | #if CONFIG_CSR |
302 | kernel_bootstrap_log("csr_init"); | |
303 | csr_init(); | |
304 | #endif | |
305 | ||
d9a64523 A |
306 | if (PE_i_can_has_debugger(NULL)) { |
307 | if (PE_parse_boot_argn("-show_pointers", &namep, sizeof(namep))) { | |
308 | doprnt_hide_pointers = FALSE; | |
309 | } | |
310 | if (PE_parse_boot_argn("-no_slto_panic", &namep, sizeof(namep))) { | |
311 | extern boolean_t spinlock_timeout_panic; | |
312 | spinlock_timeout_panic = FALSE; | |
313 | } | |
4bd07ac2 A |
314 | } |
315 | ||
490019cf A |
316 | kernel_bootstrap_log("console_init"); |
317 | console_init(); | |
318 | ||
39037602 A |
319 | kernel_bootstrap_log("stackshot_init"); |
320 | stackshot_init(); | |
39236c6e | 321 | |
fe8ab488 | 322 | kernel_bootstrap_log("sched_init"); |
1c79356b | 323 | sched_init(); |
2d21ac55 | 324 | |
39037602 A |
325 | kernel_bootstrap_log("ltable_bootstrap"); |
326 | ltable_bootstrap(); | |
327 | ||
3e170ce0 A |
328 | kernel_bootstrap_log("waitq_bootstrap"); |
329 | waitq_bootstrap(); | |
2d21ac55 | 330 | |
fe8ab488 | 331 | kernel_bootstrap_log("ipc_bootstrap"); |
1c79356b | 332 | ipc_bootstrap(); |
2d21ac55 | 333 | |
2d21ac55 | 334 | #if CONFIG_MACF |
3e170ce0 | 335 | kernel_bootstrap_log("mac_policy_init"); |
2d21ac55 A |
336 | mac_policy_init(); |
337 | #endif | |
3e170ce0 | 338 | |
fe8ab488 | 339 | kernel_bootstrap_log("ipc_init"); |
1c79356b | 340 | ipc_init(); |
1c79356b A |
341 | |
342 | /* | |
343 | * As soon as the virtual memory system is up, we record | |
344 | * that this CPU is using the kernel pmap. | |
345 | */ | |
fe8ab488 | 346 | kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL"); |
1c79356b A |
347 | PMAP_ACTIVATE_KERNEL(master_cpu); |
348 | ||
fe8ab488 | 349 | kernel_bootstrap_log("mapping_free_prime"); |
0a7de745 | 350 | mapping_free_prime(); /* Load up with temporary mapping blocks */ |
1c79356b | 351 | |
fe8ab488 | 352 | kernel_bootstrap_log("machine_init"); |
1c79356b | 353 | machine_init(); |
2d21ac55 | 354 | |
fe8ab488 | 355 | kernel_bootstrap_log("clock_init"); |
1c79356b A |
356 | clock_init(); |
357 | ||
316670eb | 358 | ledger_init(); |
1c79356b A |
359 | |
360 | /* | |
361 | * Initialize the IPC, task, and thread subsystems. | |
362 | */ | |
5ba3f43e | 363 | |
fe8ab488 | 364 | #if CONFIG_COALITIONS |
3e170ce0 A |
365 | kernel_bootstrap_log("coalitions_init"); |
366 | coalitions_init(); | |
fe8ab488 A |
367 | #endif |
368 | ||
369 | kernel_bootstrap_log("task_init"); | |
1c79356b | 370 | task_init(); |
2d21ac55 | 371 | |
fe8ab488 | 372 | kernel_bootstrap_log("thread_init"); |
1c79356b | 373 | thread_init(); |
fe8ab488 | 374 | |
cb323159 A |
375 | kernel_bootstrap_log("restartable_init"); |
376 | restartable_init(); | |
377 | ||
d9a64523 A |
378 | kernel_bootstrap_log("workq_init"); |
379 | workq_init(); | |
380 | ||
381 | kernel_bootstrap_log("turnstiles_init"); | |
382 | turnstiles_init(); | |
383 | ||
fe8ab488 A |
384 | #if CONFIG_ATM |
385 | /* Initialize the Activity Trace Resource Manager. */ | |
386 | kernel_bootstrap_log("atm_init"); | |
387 | atm_init(); | |
388 | #endif | |
39037602 A |
389 | kernel_bootstrap_log("mach_init_activity_id"); |
390 | mach_init_activity_id(); | |
fe8ab488 | 391 | |
fe8ab488 A |
392 | /* Initialize the BANK Manager. */ |
393 | kernel_bootstrap_log("bank_init"); | |
394 | bank_init(); | |
39037602 A |
395 | |
396 | kernel_bootstrap_log("ipc_pthread_priority_init"); | |
397 | ipc_pthread_priority_init(); | |
398 | ||
3e170ce0 A |
399 | /* initialize the corpse config based on boot-args */ |
400 | corpses_init(); | |
401 | ||
cc8bc92a A |
402 | /* initialize host_statistics */ |
403 | host_statistics_init(); | |
404 | ||
cb323159 A |
405 | /* initialize exceptions */ |
406 | exception_init(); | |
407 | ||
4ba76501 A |
408 | #if CONFIG_SCHED_SFI |
409 | kernel_bootstrap_log("sfi_init"); | |
410 | sfi_init(); | |
411 | #endif | |
412 | ||
1c79356b | 413 | /* |
91447636 | 414 | * Create a kernel thread to execute the kernel bootstrap. |
1c79356b | 415 | */ |
0a7de745 | 416 | |
fe8ab488 | 417 | kernel_bootstrap_log("kernel_thread_create"); |
91447636 | 418 | result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread); |
0c530ab8 | 419 | |
0a7de745 A |
420 | if (result != KERN_SUCCESS) { |
421 | panic("kernel_bootstrap: result = %08X\n", result); | |
422 | } | |
423 | ||
424 | /* The static init_thread is re-used as the bootstrap thread */ | |
425 | assert(thread == current_thread()); | |
9bccf70c | 426 | |
0a7de745 | 427 | /* TODO: do a proper thread_start() (without the thread_setrun()) */ |
91447636 | 428 | thread->state = TH_RUN; |
3e170ce0 | 429 | thread->last_made_runnable_time = mach_absolute_time(); |
cb323159 | 430 | thread_set_thread_name(thread, "kernel_bootstrap_thread"); |
0a7de745 | 431 | |
91447636 | 432 | thread_deallocate(thread); |
55e303ae | 433 | |
fe8ab488 | 434 | kernel_bootstrap_log("load_context - done"); |
91447636 | 435 | load_context(thread); |
1c79356b | 436 | /*NOTREACHED*/ |
1c79356b A |
437 | } |
438 | ||
2d21ac55 A |
439 | int kth_started = 0; |
440 | ||
316670eb | 441 | vm_offset_t vm_kernel_addrperm; |
39236c6e | 442 | vm_offset_t buf_kernel_addrperm; |
3e170ce0 | 443 | vm_offset_t vm_kernel_addrperm_ext; |
5ba3f43e A |
444 | uint64_t vm_kernel_addrhash_salt; |
445 | uint64_t vm_kernel_addrhash_salt_ext; | |
316670eb | 446 | |
1c79356b | 447 | /* |
91447636 A |
448 | * Now running in a thread. Kick off other services, |
449 | * invoke user bootstrap, enter pageout loop. | |
1c79356b | 450 | */ |
91447636 A |
451 | static void |
452 | kernel_bootstrap_thread(void) | |
1c79356b | 453 | { |
0a7de745 | 454 | processor_t processor = current_processor(); |
1c79356b | 455 | |
2d21ac55 | 456 | #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */ |
fe8ab488 | 457 | kernel_bootstrap_thread_log("idle_thread_create"); |
1c79356b | 458 | /* |
91447636 | 459 | * Create the idle processor thread. |
1c79356b | 460 | */ |
91447636 | 461 | idle_thread_create(processor); |
0b4e3aa0 A |
462 | |
463 | /* | |
91447636 A |
464 | * N.B. Do not stick anything else |
465 | * before this point. | |
466 | * | |
467 | * Start up the scheduler services. | |
0b4e3aa0 | 468 | */ |
fe8ab488 | 469 | kernel_bootstrap_thread_log("sched_startup"); |
91447636 | 470 | sched_startup(); |
0b4e3aa0 | 471 | |
6d2010ae A |
472 | /* |
473 | * Thread lifecycle maintenance (teardown, stack allocation) | |
474 | */ | |
fe8ab488 | 475 | kernel_bootstrap_thread_log("thread_daemon_init"); |
6d2010ae | 476 | thread_daemon_init(); |
39236c6e A |
477 | |
478 | /* Create kernel map entry reserve */ | |
479 | vm_kernel_reserved_entry_init(); | |
480 | ||
6d2010ae A |
481 | /* |
482 | * Thread callout service. | |
483 | */ | |
fe8ab488 | 484 | kernel_bootstrap_thread_log("thread_call_initialize"); |
6d2010ae | 485 | thread_call_initialize(); |
fe8ab488 | 486 | |
1c79356b | 487 | /* |
91447636 A |
488 | * Remain on current processor as |
489 | * additional processors come online. | |
1c79356b | 490 | */ |
fe8ab488 | 491 | kernel_bootstrap_thread_log("thread_bind"); |
2d21ac55 | 492 | thread_bind(processor); |
1c79356b | 493 | |
fe8ab488 A |
494 | /* |
495 | * Initialize ipc thread call support. | |
496 | */ | |
497 | kernel_bootstrap_thread_log("ipc_thread_call_init"); | |
498 | ipc_thread_call_init(); | |
499 | ||
1c79356b | 500 | /* |
91447636 | 501 | * Kick off memory mapping adjustments. |
1c79356b | 502 | */ |
fe8ab488 | 503 | kernel_bootstrap_thread_log("mapping_adjust"); |
1c79356b | 504 | mapping_adjust(); |
1c79356b | 505 | |
1c79356b A |
506 | /* |
507 | * Create the clock service. | |
508 | */ | |
fe8ab488 | 509 | kernel_bootstrap_thread_log("clock_service_create"); |
1c79356b A |
510 | clock_service_create(); |
511 | ||
512 | /* | |
513 | * Create the device service. | |
514 | */ | |
515 | device_service_create(); | |
516 | ||
2d21ac55 | 517 | kth_started = 1; |
39037602 | 518 | |
b0d623f7 | 519 | #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0 |
2d21ac55 A |
520 | /* |
521 | * Create and initialize the physical copy window for processor 0 | |
cb323159 | 522 | * This is required before starting kicking off IOKit. |
2d21ac55 A |
523 | */ |
524 | cpu_physwindow_init(0); | |
525 | #endif | |
1c79356b | 526 | |
cb323159 | 527 | phys_carveout_init(); |
39236c6e | 528 | |
d9a64523 | 529 | #if MACH_KDP |
fe8ab488 | 530 | kernel_bootstrap_log("kdp_init"); |
7ddcb079 A |
531 | kdp_init(); |
532 | #endif | |
533 | ||
39236c6e A |
534 | #if ALTERNATE_DEBUGGER |
535 | alternate_debugger_init(); | |
536 | #endif | |
537 | ||
39236c6e A |
538 | #if KPC |
539 | kpc_init(); | |
540 | #endif | |
541 | ||
fe8ab488 A |
542 | #if CONFIG_ECC_LOGGING |
543 | ecc_log_init(); | |
0a7de745 | 544 | #endif |
fe8ab488 | 545 | |
fe8ab488 A |
546 | #if HYPERVISOR |
547 | hv_support_init(); | |
548 | #endif | |
549 | ||
39236c6e | 550 | #if CONFIG_TELEMETRY |
fe8ab488 | 551 | kernel_bootstrap_log("bootprofile_init"); |
39236c6e A |
552 | bootprofile_init(); |
553 | #endif | |
554 | ||
fe8ab488 A |
555 | #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX |
556 | vmx_init(); | |
557 | #endif | |
558 | ||
39037602 A |
559 | kernel_bootstrap_thread_log("ktrace_init"); |
560 | ktrace_init(); | |
39236c6e | 561 | |
94ff46dc A |
562 | char trace_typefilter[256] = {}; |
563 | PE_parse_boot_arg_str("trace_typefilter", trace_typefilter, | |
564 | sizeof(trace_typefilter)); | |
a39ff7e2 | 565 | kdebug_init(new_nkdbufs, trace_typefilter, trace_wrap); |
6d2010ae | 566 | |
0a7de745 | 567 | #ifdef MACH_BSD |
39037602 A |
568 | kernel_bootstrap_log("bsd_early_init"); |
569 | bsd_early_init(); | |
570 | #endif | |
571 | ||
5ba3f43e | 572 | #if defined(__arm64__) |
d9a64523 | 573 | ml_lockdown_init(); |
5ba3f43e A |
574 | #endif |
575 | ||
0a7de745 | 576 | #ifdef IOKIT |
39037602 | 577 | kernel_bootstrap_log("PE_init_iokit"); |
2d21ac55 | 578 | PE_init_iokit(); |
1c79356b | 579 | #endif |
fe8ab488 A |
580 | |
581 | assert(ml_get_interrupts_enabled() == FALSE); | |
39037602 | 582 | |
5ba3f43e A |
583 | /* |
584 | * Past this point, kernel subsystems that expect to operate with | |
585 | * interrupts or preemption enabled may begin enforcement. | |
586 | */ | |
587 | early_boot_complete = TRUE; | |
39037602 | 588 | |
5ba3f43e A |
589 | #if INTERRUPT_MASKED_DEBUG |
590 | // Reset interrupts masked timeout before we enable interrupts | |
591 | ml_spin_debug_clear_self(); | |
592 | #endif | |
0a7de745 | 593 | (void) spllo(); /* Allow interruptions */ |
43866e37 | 594 | |
b0d623f7 | 595 | #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0 |
0c530ab8 | 596 | /* |
2d21ac55 A |
597 | * Create and initialize the copy window for processor 0 |
598 | * This also allocates window space for all other processors. | |
599 | * However, this is dependent on the number of processors - so this call | |
600 | * must be after IOKit has been started because IOKit performs processor | |
601 | * discovery. | |
0c530ab8 | 602 | */ |
2d21ac55 A |
603 | cpu_userwindow_init(0); |
604 | #endif | |
0c530ab8 | 605 | |
0c530ab8 | 606 | /* |
2d21ac55 | 607 | * Initialize the shared region module. |
0c530ab8 | 608 | */ |
2d21ac55 A |
609 | vm_shared_region_init(); |
610 | vm_commpage_init(); | |
316670eb | 611 | vm_commpage_text_init(); |
2d21ac55 A |
612 | |
613 | #if CONFIG_MACF | |
fe8ab488 | 614 | kernel_bootstrap_log("mac_policy_initmach"); |
2d21ac55 | 615 | mac_policy_initmach(); |
5ba3f43e A |
616 | #if CONFIG_VNGUARD |
617 | vnguard_policy_init(); | |
618 | #endif | |
0c530ab8 | 619 | #endif |
1c79356b | 620 | |
d9a64523 A |
621 | #if CONFIG_DTRACE |
622 | dtrace_early_init(); | |
623 | sdt_early_init(); | |
624 | #endif | |
625 | ||
626 | ||
627 | /* | |
628 | * Get rid of segments used to bootstrap kext loading. This removes | |
629 | * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands. | |
630 | * Must be done prior to lockdown so that we can free (and possibly relocate) | |
631 | * the static KVA mappings used for the jettisoned bootstrap segments. | |
632 | */ | |
633 | OSKextRemoveKextBootstrap(); | |
5ba3f43e A |
634 | #if defined(__arm__) || defined(__arm64__) |
635 | #if CONFIG_KERNEL_INTEGRITY | |
d9a64523 | 636 | machine_lockdown_preflight(); |
5ba3f43e A |
637 | #endif |
638 | /* | |
639 | * Finalize protections on statically mapped pages now that comm page mapping is established. | |
640 | */ | |
0a7de745 | 641 | arm_vm_prot_finalize(PE_state.bootArgs); |
5ba3f43e | 642 | #endif |
4bd07ac2 | 643 | |
316670eb | 644 | /* |
3e170ce0 | 645 | * Initialize the globals used for permuting kernel |
316670eb | 646 | * addresses that may be exported to userland as tokens |
3e170ce0 A |
647 | * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL(). |
648 | * Force the random number to be odd to avoid mapping a non-zero | |
316670eb | 649 | * word-aligned address to zero via addition. |
fe8ab488 A |
650 | * Note: at this stage we can use the cryptographically secure PRNG |
651 | * rather than early_random(). | |
316670eb | 652 | */ |
fe8ab488 A |
653 | read_random(&vm_kernel_addrperm, sizeof(vm_kernel_addrperm)); |
654 | vm_kernel_addrperm |= 1; | |
655 | read_random(&buf_kernel_addrperm, sizeof(buf_kernel_addrperm)); | |
656 | buf_kernel_addrperm |= 1; | |
3e170ce0 A |
657 | read_random(&vm_kernel_addrperm_ext, sizeof(vm_kernel_addrperm_ext)); |
658 | vm_kernel_addrperm_ext |= 1; | |
5ba3f43e A |
659 | read_random(&vm_kernel_addrhash_salt, sizeof(vm_kernel_addrhash_salt)); |
660 | read_random(&vm_kernel_addrhash_salt_ext, sizeof(vm_kernel_addrhash_salt_ext)); | |
3e170ce0 A |
661 | |
662 | vm_set_restrictions(); | |
663 | ||
664 | ||
d9a64523 A |
665 | #ifdef CONFIG_XNUPOST |
666 | kern_return_t result = kernel_list_tests(); | |
667 | result = kernel_do_post(); | |
668 | if (result != KERN_SUCCESS) { | |
669 | panic("kernel_do_post: Tests failed with result = 0x%08x\n", result); | |
670 | } | |
671 | kernel_bootstrap_log("kernel_do_post - done"); | |
672 | #endif /* CONFIG_XNUPOST */ | |
316670eb | 673 | |
9d749ea3 | 674 | |
1c79356b A |
675 | /* |
676 | * Start the user bootstrap. | |
677 | */ | |
0a7de745 | 678 | #ifdef MACH_BSD |
2d21ac55 A |
679 | bsd_init(); |
680 | #endif | |
681 | ||
cb323159 A |
682 | #if defined (__x86_64__) |
683 | x86_64_protect_data_const(); | |
684 | #endif | |
685 | ||
1c79356b | 686 | |
5ba3f43e A |
687 | /* |
688 | * Get rid of pages used for early boot tracing. | |
689 | */ | |
690 | kdebug_free_early_buf(); | |
691 | ||
0a7de745 | 692 | serial_keyboard_init(); /* Start serial keyboard if wanted */ |
55e303ae | 693 | |
b0d623f7 | 694 | vm_page_init_local_q(); |
3e170ce0 | 695 | |
2d21ac55 | 696 | thread_bind(PROCESSOR_NULL); |
1c79356b | 697 | |
0a7de745 A |
698 | /* |
699 | * Now that all CPUs are available to run threads, this is essentially | |
700 | * a background thread. Take this opportunity to initialize and free | |
701 | * any remaining vm_pages that were delayed earlier by pmap_startup(). | |
702 | */ | |
703 | vm_free_delayed_pages(); | |
704 | ||
1c79356b A |
705 | /* |
706 | * Become the pageout daemon. | |
707 | */ | |
1c79356b A |
708 | vm_pageout(); |
709 | /*NOTREACHED*/ | |
710 | } | |
711 | ||
91447636 A |
712 | /* |
713 | * slave_main: | |
714 | * | |
715 | * Load the first thread to start a processor. | |
cb323159 A |
716 | * This path will also be used by the master processor |
717 | * after being offlined. | |
91447636 | 718 | */ |
1c79356b | 719 | void |
593a1d5f | 720 | slave_main(void *machine_param) |
1c79356b | 721 | { |
0a7de745 A |
722 | processor_t processor = current_processor(); |
723 | thread_t thread; | |
1c79356b | 724 | |
91447636 A |
725 | /* |
726 | * Use the idle processor thread if there | |
727 | * is no dedicated start up thread. | |
728 | */ | |
cb323159 A |
729 | if (processor->processor_offlined == true) { |
730 | /* Return to the saved processor_offline context */ | |
731 | assert(processor->startup_thread == THREAD_NULL); | |
732 | ||
91447636 | 733 | thread = processor->idle_thread; |
593a1d5f | 734 | thread->parameter = machine_param; |
cb323159 A |
735 | } else if (processor->startup_thread) { |
736 | thread = processor->startup_thread; | |
737 | processor->startup_thread = THREAD_NULL; | |
0a7de745 | 738 | } else { |
cb323159 A |
739 | thread = processor->idle_thread; |
740 | thread->continuation = processor_start_thread; | |
741 | thread->parameter = machine_param; | |
1c79356b | 742 | } |
9bccf70c | 743 | |
91447636 | 744 | load_context(thread); |
1c79356b | 745 | /*NOTREACHED*/ |
1c79356b A |
746 | } |
747 | ||
748 | /* | |
91447636 A |
749 | * processor_start_thread: |
750 | * | |
751 | * First thread to execute on a started processor. | |
752 | * | |
753 | * Called at splsched. | |
1c79356b A |
754 | */ |
755 | void | |
cb323159 A |
756 | processor_start_thread(void *machine_param, |
757 | __unused wait_result_t result) | |
1c79356b | 758 | { |
0a7de745 A |
759 | processor_t processor = current_processor(); |
760 | thread_t self = current_thread(); | |
91447636 | 761 | |
593a1d5f | 762 | slave_machine_init(machine_param); |
1c79356b | 763 | |
91447636 A |
764 | /* |
765 | * If running the idle processor thread, | |
766 | * reenter the idle loop, else terminate. | |
767 | */ | |
0a7de745 | 768 | if (self == processor->idle_thread) { |
cb323159 | 769 | thread_block(idle_thread); |
0a7de745 | 770 | } |
91447636 A |
771 | |
772 | thread_terminate(self); | |
773 | /*NOTREACHED*/ | |
1c79356b A |
774 | } |
775 | ||
776 | /* | |
91447636 A |
777 | * load_context: |
778 | * | |
779 | * Start the first thread on a processor. | |
cb323159 A |
780 | * This may be the first thread ever run on a processor, or |
781 | * it could be a processor that was previously offlined. | |
1c79356b | 782 | */ |
5ba3f43e | 783 | static void __attribute__((noreturn)) |
91447636 | 784 | load_context( |
0a7de745 | 785 | thread_t thread) |
1c79356b | 786 | { |
0a7de745 | 787 | processor_t processor = current_processor(); |
1c79356b | 788 | |
2d21ac55 A |
789 | |
790 | #define load_context_kprintf(x...) /* kprintf("load_context: " x) */ | |
791 | ||
fe8ab488 | 792 | load_context_kprintf("machine_set_current_thread\n"); |
91447636 | 793 | machine_set_current_thread(thread); |
2d21ac55 | 794 | |
fe8ab488 | 795 | load_context_kprintf("processor_up\n"); |
1c79356b | 796 | |
b0d623f7 | 797 | PMAP_ACTIVATE_KERNEL(processor->cpu_id); |
91447636 A |
798 | |
799 | /* | |
800 | * Acquire a stack if none attached. The panic | |
801 | * should never occur since the thread is expected | |
802 | * to have reserved stack. | |
803 | */ | |
39236c6e | 804 | load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread, |
0a7de745 | 805 | thread->kernel_stack, thread->machine.kstackptr); |
91447636 | 806 | if (!thread->kernel_stack) { |
fe8ab488 | 807 | load_context_kprintf("stack_alloc_try\n"); |
0a7de745 | 808 | if (!stack_alloc_try(thread)) { |
91447636 | 809 | panic("load_context"); |
0a7de745 | 810 | } |
91447636 A |
811 | } |
812 | ||
813 | /* | |
814 | * The idle processor threads are not counted as | |
815 | * running for load calculations. | |
816 | */ | |
0a7de745 | 817 | if (!(thread->state & TH_IDLE)) { |
cb323159 | 818 | SCHED(run_count_incr)(thread); |
0a7de745 | 819 | } |
1c79356b | 820 | |
55e303ae | 821 | processor->active_thread = thread; |
0a7de745 A |
822 | processor_state_update_explicit(processor, thread->sched_pri, |
823 | SFI_CLASS_KERNEL, PSET_SMP, thread_get_perfcontrol_class(thread), THREAD_URGENCY_NONE); | |
824 | processor->current_is_bound = thread->bound_processor != PROCESSOR_NULL; | |
825 | processor->current_is_NO_SMT = false; | |
39037602 | 826 | processor->starting_pri = thread->sched_pri; |
91447636 A |
827 | processor->deadline = UINT64_MAX; |
828 | thread->last_processor = processor; | |
829 | ||
cb323159 A |
830 | processor_up(processor); |
831 | ||
91447636 | 832 | processor->last_dispatch = mach_absolute_time(); |
2d21ac55 A |
833 | timer_start(&thread->system_timer, processor->last_dispatch); |
834 | PROCESSOR_DATA(processor, thread_timer) = PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer; | |
835 | ||
836 | timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch); | |
837 | PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state); | |
1c79356b | 838 | |
5ba3f43e | 839 | |
d9a64523 A |
840 | cpu_quiescent_counter_join(processor->last_dispatch); |
841 | ||
b0d623f7 | 842 | PMAP_ACTIVATE_USER(thread, processor->cpu_id); |
1c79356b | 843 | |
fe8ab488 | 844 | load_context_kprintf("machine_load_context\n"); |
0a7de745 A |
845 | |
846 | #if __arm__ || __arm64__ | |
847 | #if __SMP__ | |
848 | /* TODO: Should this be ordered? */ | |
849 | thread->machine.machine_thread_flags |= MACHINE_THREAD_FLAGS_ON_CPU; | |
850 | #endif /* __SMP__ */ | |
851 | #endif /* __arm__ || __arm64__ */ | |
852 | ||
55e303ae | 853 | machine_load_context(thread); |
1c79356b A |
854 | /*NOTREACHED*/ |
855 | } | |
b0d623f7 A |
856 | |
857 | void | |
6d2010ae | 858 | scale_setup() |
b0d623f7 A |
859 | { |
860 | int scale = 0; | |
861 | #if defined(__LP64__) | |
6d2010ae A |
862 | typeof(task_max) task_max_base = task_max; |
863 | ||
864 | /* Raise limits for servers with >= 16G */ | |
0a7de745 A |
865 | if ((serverperfmode != 0) && ((uint64_t)sane_size >= (uint64_t)(16 * 1024 * 1024 * 1024ULL))) { |
866 | scale = (int)((uint64_t)sane_size / (uint64_t)(8 * 1024 * 1024 * 1024ULL)); | |
b0d623f7 | 867 | /* limit to 128 G */ |
0a7de745 | 868 | if (scale > 16) { |
b0d623f7 | 869 | scale = 16; |
0a7de745 | 870 | } |
6d2010ae | 871 | task_max_base = 2500; |
0a7de745 A |
872 | /* Raise limits for machines with >= 3GB */ |
873 | } else if ((uint64_t)sane_size >= (uint64_t)(3 * 1024 * 1024 * 1024ULL)) { | |
874 | if ((uint64_t)sane_size < (uint64_t)(8 * 1024 * 1024 * 1024ULL)) { | |
5ba3f43e A |
875 | scale = 2; |
876 | } else { | |
877 | /* limit to 64GB */ | |
0a7de745 | 878 | scale = MIN(16, (int)((uint64_t)sane_size / (uint64_t)(4 * 1024 * 1024 * 1024ULL))); |
5ba3f43e A |
879 | } |
880 | } | |
6d2010ae A |
881 | |
882 | task_max = MAX(task_max, task_max_base * scale); | |
883 | ||
884 | if (scale != 0) { | |
b0d623f7 | 885 | task_threadmax = task_max; |
0a7de745 | 886 | thread_max = task_max * 5; |
6d2010ae A |
887 | } |
888 | ||
b0d623f7 | 889 | #endif |
6d2010ae A |
890 | |
891 | bsd_scale_setup(scale); | |
0a7de745 | 892 | |
b0d623f7 | 893 | ipc_space_max = SPACE_MAX; |
b0d623f7 A |
894 | ipc_port_max = PORT_MAX; |
895 | ipc_pset_max = SET_MAX; | |
896 | semaphore_max = SEMAPHORE_MAX; | |
897 | } |