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