2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
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,
66 * Mach kernel startup.
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>
94 #include <kern/startup.h>
95 #include <kern/task.h>
96 #include <kern/thread.h>
97 #include <kern/timer.h>
99 #include <kern/telemetry.h>
101 #include <kern/zalloc.h>
102 #include <kern/locks.h>
103 #include <kern/debug.h>
105 #include <kperf/kperf.h>
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>
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>
135 #include <tests/ktest.h>
136 #include <tests/xnupost.h>
140 #include <atm/atm_internal.h>
147 #include <bank/bank_internal.h>
149 #if ALTERNATE_DEBUGGER
150 #include <arm64/alternate_debugger.h>
158 #include <security/mac_mach_internal.h>
160 extern void vnguard_policy_init(void);
165 #include <kern/kpc.h>
169 #include <kern/hv_support.h>
172 #include <san/kasan.h>
174 #include <i386/pmCPU.h>
175 static void kernel_bootstrap_thread(void);
177 static void load_context(
180 #if CONFIG_ECC_LOGGING
181 #include <kern/ecc.h>
184 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
185 #include <i386/vmx/vmx_cpu.h>
189 extern void dtrace_early_init(void);
190 extern void sdt_early_init(void);
193 // libkern/OSKextLib.cpp
194 extern void OSKextRemoveKextBootstrap(void);
196 void scale_setup(void);
197 extern void bsd_scale_setup(int);
198 extern unsigned int semaphore_max
;
199 extern void stackshot_init(void);
202 * Running in virtual memory, on the interrupt stack.
205 extern struct startup_entry startup_entries
[]
206 __SECTION_START_SYM(STARTUP_HOOK_SEGMENT
, STARTUP_HOOK_SECTION
);
208 extern struct startup_entry startup_entries_end
[]
209 __SECTION_END_SYM(STARTUP_HOOK_SEGMENT
, STARTUP_HOOK_SECTION
);
211 static struct startup_entry
*__startup_data startup_entry_cur
= startup_entries
;
213 SECURITY_READ_ONLY_LATE(startup_subsystem_id_t
) startup_phase
= STARTUP_SUB_NONE
;
215 extern int serverperfmode
;
217 #if DEBUG || DEVELOPMENT
218 TUNABLE(startup_debug_t
, startup_debug
, "startup_debug", 0);
221 /* size of kernel trace buffer, disabled by default */
222 TUNABLE(unsigned int, new_nkdbufs
, "trace", 0);
223 TUNABLE(unsigned int, wake_nkdbufs
, "trace_wake", 0);
224 TUNABLE(unsigned int, write_trace_on_panic
, "trace_panic", 0);
225 TUNABLE(unsigned int, trace_wrap
, "trace_wrap", 0);
227 /* mach leak logging */
228 TUNABLE(int, log_leaks
, "-l", 0);
231 kernel_bootstrap_log(const char *message
)
233 if ((startup_debug
& STARTUP_DEBUG_VERBOSE
) &&
234 startup_phase
>= STARTUP_SUB_KPRINTF
) {
235 kprintf("kernel_bootstrap: %s\n", message
);
237 kernel_debug_string_early(message
);
241 kernel_bootstrap_thread_log(const char *message
)
243 if ((startup_debug
& STARTUP_DEBUG_VERBOSE
) &&
244 startup_phase
>= STARTUP_SUB_KPRINTF
) {
245 kprintf("kernel_bootstrap_thread: %s\n", message
);
247 kernel_debug_string_early(message
);
251 qsort(void *a
, size_t n
, size_t es
, int (*cmp
)(const void *, const void *));
255 startup_entry_cmp(const void *e1
, const void *e2
)
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
) {
263 return a
->rank
> b
->rank
? 1 : -1;
265 return a
->subsystem
> b
->subsystem
? 1 : -1;
270 kernel_startup_bootstrap(void)
273 * Sort the various STARTUP() entries by subsystem/rank.
275 size_t n
= startup_entries_end
- startup_entries
;
278 panic("Section %s,%s missing",
279 STARTUP_HOOK_SEGMENT
, STARTUP_HOOK_SECTION
);
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
);
287 qsort(startup_entries
, n
, sizeof(struct startup_entry
), startup_entry_cmp
);
290 * Then initialize all tunables, and early locks
292 kernel_startup_initialize_upto(STARTUP_SUB_LOCKS_EARLY
);
297 kernel_startup_tunable_init(const struct startup_tunable_spec
*spec
)
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
;
308 kernel_startup_log(startup_subsystem_id_t subsystem
)
310 static const char *names
[] = {
311 [STARTUP_SUB_TUNABLES
] = "tunables",
312 [STARTUP_SUB_LOCKS_EARLY
] = "locks_early",
313 [STARTUP_SUB_KPRINTF
] = "kprintf",
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",
323 [STARTUP_SUB_CODESIGNING
] = "codesigning",
324 [STARTUP_SUB_OSLOG
] = "oslog",
325 [STARTUP_SUB_MACH_IPC
] = "mach_ipc",
326 [STARTUP_SUB_SYSCTL
] = "sysctl",
327 [STARTUP_SUB_EARLY_BOOT
] = "early_boot",
329 /* LOCKDOWN is special and its value won't fit here. */
331 static startup_subsystem_id_t logged
= STARTUP_SUB_NONE
;
333 if (subsystem
<= logged
) {
337 if (subsystem
< sizeof(names
) / sizeof(names
[0]) && names
[subsystem
]) {
338 kernel_bootstrap_log(names
[subsystem
]);
345 kernel_startup_initialize_upto(startup_subsystem_id_t upto
)
347 struct startup_entry
*cur
= startup_entry_cur
;
349 assert(startup_phase
< upto
);
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
);
357 startup_phase
= cur
->subsystem
- 1;
358 kernel_startup_log(cur
->subsystem
);
360 startup_entry_cur
= ++cur
;
362 kernel_startup_log(upto
);
364 if ((startup_debug
& STARTUP_DEBUG_VERBOSE
) &&
365 upto
>= STARTUP_SUB_KPRINTF
) {
366 kprintf("%s: reached phase %d\n", __func__
, upto
);
368 startup_phase
= upto
;
372 kernel_bootstrap(void)
374 kern_return_t result
;
378 printf("%s\n", version
); /* log kernel version */
382 kernel_bootstrap_log("vm_mem_bootstrap");
385 machine_info
.memory_size
= (uint32_t)mem_size
;
386 #if XNU_TARGET_OS_OSX
387 machine_info
.max_mem
= max_mem_actual
;
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
;
394 kernel_startup_initialize_upto(STARTUP_SUB_OSLOG
);
397 kernel_bootstrap_log("kasan_late_init");
402 kernel_bootstrap_log("telemetry_init");
406 if (PE_i_can_has_debugger(NULL
)) {
407 if (PE_parse_boot_argn("-show_pointers", &namep
, sizeof(namep
))) {
408 doprnt_hide_pointers
= FALSE
;
410 if (PE_parse_boot_argn("-no_slto_panic", &namep
, sizeof(namep
))) {
411 extern boolean_t spinlock_timeout_panic
;
412 spinlock_timeout_panic
= FALSE
;
416 kernel_bootstrap_log("console_init");
419 kernel_bootstrap_log("stackshot_init");
422 kernel_bootstrap_log("sched_init");
425 kernel_bootstrap_log("waitq_bootstrap");
429 kernel_bootstrap_log("mac_policy_init");
433 kernel_startup_initialize_upto(STARTUP_SUB_MACH_IPC
);
436 * As soon as the virtual memory system is up, we record
437 * that this CPU is using the kernel pmap.
439 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
440 PMAP_ACTIVATE_KERNEL(master_cpu
);
442 kernel_bootstrap_log("mapping_free_prime");
443 mapping_free_prime(); /* Load up with temporary mapping blocks */
445 kernel_bootstrap_log("machine_init");
448 kernel_bootstrap_log("thread_machine_init_template");
449 thread_machine_init_template();
451 kernel_bootstrap_log("clock_init");
455 * Initialize the IPC, task, and thread subsystems.
457 #if CONFIG_THREAD_GROUPS
458 kernel_bootstrap_log("thread_group_init");
462 #if CONFIG_COALITIONS
463 kernel_bootstrap_log("coalitions_init");
467 kernel_bootstrap_log("task_init");
470 kernel_bootstrap_log("thread_init");
473 kernel_bootstrap_log("restartable_init");
476 kernel_bootstrap_log("workq_init");
479 kernel_bootstrap_log("turnstiles_init");
483 /* Initialize the Activity Trace Resource Manager. */
484 kernel_bootstrap_log("atm_init");
487 kernel_bootstrap_log("mach_init_activity_id");
488 mach_init_activity_id();
490 /* Initialize the BANK Manager. */
491 kernel_bootstrap_log("bank_init");
494 kernel_bootstrap_log("ipc_pthread_priority_init");
495 ipc_pthread_priority_init();
497 /* initialize the corpse config based on boot-args */
500 /* initialize host_statistics */
501 host_statistics_init();
503 /* initialize exceptions */
504 kernel_bootstrap_log("exception_init");
508 kernel_bootstrap_log("sfi_init");
513 * Create a kernel thread to execute the kernel bootstrap.
516 kernel_bootstrap_log("kernel_thread_create");
517 result
= kernel_thread_create((thread_continue_t
)kernel_bootstrap_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
519 if (result
!= KERN_SUCCESS
) {
520 panic("kernel_bootstrap: result = %08X\n", result
);
523 /* The static init_thread is re-used as the bootstrap thread */
524 assert(thread
== current_thread());
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");
531 thread_deallocate(thread
);
533 kernel_bootstrap_log("load_context - done");
534 load_context(thread
);
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
;
545 * Now running in a thread. Kick off other services,
546 * invoke user bootstrap, enter pageout loop.
549 kernel_bootstrap_thread(void)
551 processor_t processor
= current_processor();
553 kernel_bootstrap_thread_log("idle_thread_create");
555 * Create the idle processor thread.
557 idle_thread_create(processor
);
560 * N.B. Do not stick anything else
563 * Start up the scheduler services.
565 kernel_bootstrap_thread_log("sched_startup");
569 * Thread lifecycle maintenance (teardown, stack allocation)
571 kernel_bootstrap_thread_log("thread_daemon_init");
572 thread_daemon_init();
574 /* Create kernel map entry reserve */
575 vm_kernel_reserved_entry_init();
578 * Thread callout service.
580 kernel_bootstrap_thread_log("thread_call_initialize");
581 thread_call_initialize();
584 * Work interval subsystem initialization.
585 * Needs to be done once thread calls have been initialized.
587 kernel_bootstrap_thread_log("work_interval_initialize");
588 work_interval_subsystem_init();
591 * Remain on current processor as
592 * additional processors come online.
594 kernel_bootstrap_thread_log("thread_bind");
595 thread_bind(processor
);
598 * Initialize ipc thread call support.
600 kernel_bootstrap_thread_log("ipc_thread_call_init");
601 ipc_thread_call_init();
604 * Kick off memory mapping adjustments.
606 kernel_bootstrap_thread_log("mapping_adjust");
610 * Create the clock service.
612 kernel_bootstrap_thread_log("clock_service_create");
613 clock_service_create();
616 * Create the device service.
618 device_service_create();
620 phys_carveout_init();
623 kernel_bootstrap_log("kdp_init");
627 #if ALTERNATE_DEBUGGER
628 alternate_debugger_init();
636 kernel_bootstrap_thread_log("hv_support_init");
641 kernel_bootstrap_log("bootprofile_init");
645 char trace_typefilter
[256] = {};
646 PE_parse_boot_arg_str("trace_typefilter", trace_typefilter
,
647 sizeof(trace_typefilter
));
651 kdebug_init(new_nkdbufs
, trace_typefilter
,
652 (trace_wrap
? KDOPT_WRAPPING
: 0) | KDOPT_ATBOOT
);
654 kernel_startup_initialize_upto(STARTUP_SUB_SYSCTL
);
657 kernel_bootstrap_log("PE_init_iokit");
661 assert(ml_get_interrupts_enabled() == FALSE
);
664 * Past this point, kernel subsystems that expect to operate with
665 * interrupts or preemption enabled may begin enforcement.
667 kernel_startup_initialize_upto(STARTUP_SUB_EARLY_BOOT
);
669 #if INTERRUPT_MASKED_DEBUG
670 // Reset interrupts masked timeout before we enable interrupts
671 ml_spin_debug_clear_self();
673 (void) spllo(); /* Allow interruptions */
676 * This will start displaying progress to the user, start as early as possible
678 initialize_screen(NULL
, kPEAcquireScreen
);
681 * Initialize the shared region module.
684 vm_commpage_text_init();
687 kernel_bootstrap_log("mac_policy_initmach");
688 mac_policy_initmach();
690 kernel_bootstrap_log("vnguard_policy_init");
691 vnguard_policy_init();
696 kernel_bootstrap_log("dtrace_early_init");
702 kernel_bootstrap_log("trust_cache_init");
706 kernel_startup_initialize_upto(STARTUP_SUB_LOCKDOWN
);
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.
714 kernel_bootstrap_log("OSKextRemoveKextBootstrap");
715 OSKextRemoveKextBootstrap();
718 * Initialize the globals used for permuting kernel
719 * addresses that may be exported to userland as tokens
720 * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL().
721 * Force the random number to be odd to avoid mapping a non-zero
722 * word-aligned address to zero via addition.
723 * Note: at this stage we can use the cryptographically secure PRNG
724 * rather than early_random().
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;
730 read_random(&vm_kernel_addrperm_ext
, sizeof(vm_kernel_addrperm_ext
));
731 vm_kernel_addrperm_ext
|= 1;
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
));
735 /* No changes to kernel text and rodata beyond this point. */
736 kernel_bootstrap_log("machine_lockdown");
740 kernel_bootstrap_log("PE_lockdown_iokit");
744 * max_cpus must be nailed down by the time PE_lockdown_iokit() finishes,
747 vm_set_restrictions(machine_info
.max_cpus
);
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
);
755 kernel_bootstrap_log("kernel_do_post - done");
756 #endif /* CONFIG_XNUPOST */
764 * Start the user bootstrap.
772 * Get rid of pages used for early boot tracing.
774 kdebug_free_early_buf();
776 serial_keyboard_init(); /* Start serial keyboard if wanted */
778 vm_page_init_local_q(machine_info
.max_cpus
);
780 thread_bind(PROCESSOR_NULL
);
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().
787 vm_free_delayed_pages();
790 * Become the pageout daemon.
799 * Load the first thread to start a processor.
800 * This path will also be used by the master processor
801 * after being offlined.
804 slave_main(void *machine_param
)
806 processor_t processor
= current_processor();
810 * Use the idle processor thread if there
811 * is no dedicated start up thread.
813 if (processor
->processor_offlined
== true) {
814 /* Return to the saved processor_offline context */
815 assert(processor
->startup_thread
== THREAD_NULL
);
817 thread
= processor
->idle_thread
;
818 thread
->parameter
= machine_param
;
819 } else if (processor
->startup_thread
) {
820 thread
= processor
->startup_thread
;
821 processor
->startup_thread
= THREAD_NULL
;
823 thread
= processor
->idle_thread
;
824 thread
->continuation
= processor_start_thread
;
825 thread
->parameter
= machine_param
;
828 load_context(thread
);
833 * processor_start_thread:
835 * First thread to execute on a started processor.
837 * Called at splsched.
840 processor_start_thread(void *machine_param
,
841 __unused wait_result_t result
)
843 processor_t processor
= current_processor();
844 thread_t self
= current_thread();
846 slave_machine_init(machine_param
);
849 * If running the idle processor thread,
850 * reenter the idle loop, else terminate.
852 if (self
== processor
->idle_thread
) {
853 thread_block(idle_thread
);
856 thread_terminate(self
);
863 * Start the first thread on a processor.
864 * This may be the first thread ever run on a processor, or
865 * it could be a processor that was previously offlined.
867 static void __attribute__((noreturn
))
871 processor_t processor
= current_processor();
874 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
876 load_context_kprintf("machine_set_current_thread\n");
877 machine_set_current_thread(thread
);
879 load_context_kprintf("processor_up\n");
881 PMAP_ACTIVATE_KERNEL(processor
->cpu_id
);
884 * Acquire a stack if none attached. The panic
885 * should never occur since the thread is expected
886 * to have reserved stack.
888 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread
,
889 thread
->kernel_stack
, thread
->machine
.kstackptr
);
890 if (!thread
->kernel_stack
) {
891 load_context_kprintf("stack_alloc_try\n");
892 if (!stack_alloc_try(thread
)) {
893 panic("load_context");
898 * The idle processor threads are not counted as
899 * running for load calculations.
901 if (!(thread
->state
& TH_IDLE
)) {
902 SCHED(run_count_incr
)(thread
);
905 processor
->active_thread
= thread
;
906 processor_state_update_explicit(processor
, thread
->sched_pri
,
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
);
909 processor
->current_is_bound
= thread
->bound_processor
!= PROCESSOR_NULL
;
910 processor
->current_is_NO_SMT
= false;
911 processor
->current_is_eagerpreempt
= false;
912 #if CONFIG_THREAD_GROUPS
913 processor
->current_thread_group
= thread_group_get(thread
);
915 processor
->starting_pri
= thread
->sched_pri
;
916 processor
->deadline
= UINT64_MAX
;
917 thread
->last_processor
= processor
;
918 processor_up(processor
);
919 processor
->last_dispatch
= mach_absolute_time();
920 timer_start(&thread
->system_timer
, processor
->last_dispatch
);
921 processor
->thread_timer
= processor
->kernel_timer
= &thread
->system_timer
;
923 timer_start(&processor
->system_state
, processor
->last_dispatch
);
924 processor
->current_state
= &processor
->system_state
;
927 if (processor
->processor_set
->pset_cluster_type
== PSET_AMP_P
) {
928 timer_start(&thread
->ptime
, processor
->last_dispatch
);
932 cpu_quiescent_counter_join(processor
->last_dispatch
);
934 PMAP_ACTIVATE_USER(thread
, processor
->cpu_id
);
936 load_context_kprintf("machine_load_context\n");
938 machine_load_context(thread
);
946 #if defined(__LP64__)
947 typeof(task_max
) task_max_base
= task_max
;
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));
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)) {
963 scale
= MIN(16, (int)((uint64_t)max_mem_actual
/ (uint64_t)(4 * 1024 * 1024 * 1024ULL)));
967 task_max
= MAX(task_max
, task_max_base
* scale
);
970 task_threadmax
= task_max
;
971 thread_max
= task_max
* 5;
976 bsd_scale_setup(scale
);