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);
200 extern void ktrace_init(void);
203 * Running in virtual memory, on the interrupt stack.
206 extern struct startup_entry startup_entries
[]
207 __SECTION_START_SYM(STARTUP_HOOK_SEGMENT
, STARTUP_HOOK_SECTION
);
209 extern struct startup_entry startup_entries_end
[]
210 __SECTION_END_SYM(STARTUP_HOOK_SEGMENT
, STARTUP_HOOK_SECTION
);
212 static struct startup_entry
*__startup_data startup_entry_cur
= startup_entries
;
214 SECURITY_READ_ONLY_LATE(startup_subsystem_id_t
) startup_phase
= STARTUP_SUB_NONE
;
216 extern int serverperfmode
;
218 #if DEBUG || DEVELOPMENT
219 TUNABLE(startup_debug_t
, startup_debug
, "startup_debug", 0);
222 /* size of kernel trace buffer, disabled by default */
223 TUNABLE(unsigned int, new_nkdbufs
, "trace", 0);
224 TUNABLE(unsigned int, wake_nkdbufs
, "trace_wake", 0);
225 TUNABLE(unsigned int, write_trace_on_panic
, "trace_panic", 0);
226 TUNABLE(unsigned int, trace_wrap
, "trace_wrap", 0);
228 /* mach leak logging */
229 TUNABLE(int, log_leaks
, "-l", 0);
232 kernel_bootstrap_log(const char *message
)
234 if ((startup_debug
& STARTUP_DEBUG_VERBOSE
) &&
235 startup_phase
>= STARTUP_SUB_KPRINTF
) {
236 kprintf("kernel_bootstrap: %s\n", message
);
238 kernel_debug_string_early(message
);
242 kernel_bootstrap_thread_log(const char *message
)
244 if ((startup_debug
& STARTUP_DEBUG_VERBOSE
) &&
245 startup_phase
>= STARTUP_SUB_KPRINTF
) {
246 kprintf("kernel_bootstrap_thread: %s\n", message
);
248 kernel_debug_string_early(message
);
252 qsort(void *a
, size_t n
, size_t es
, int (*cmp
)(const void *, const void *));
256 startup_entry_cmp(const void *e1
, const void *e2
)
258 const struct startup_entry
*a
= e1
;
259 const struct startup_entry
*b
= e2
;
260 if (a
->subsystem
== b
->subsystem
) {
261 if (a
->rank
== b
->rank
) {
264 return a
->rank
> b
->rank
? 1 : -1;
266 return a
->subsystem
> b
->subsystem
? 1 : -1;
271 kernel_startup_bootstrap(void)
274 * Sort the various STARTUP() entries by subsystem/rank.
276 size_t n
= startup_entries_end
- startup_entries
;
279 panic("Section %s,%s missing",
280 STARTUP_HOOK_SEGMENT
, STARTUP_HOOK_SECTION
);
282 if (((uintptr_t)startup_entries_end
- (uintptr_t)startup_entries
) %
283 sizeof(struct startup_entry
)) {
284 panic("Section %s,%s has invalid size",
285 STARTUP_HOOK_SEGMENT
, STARTUP_HOOK_SECTION
);
288 qsort(startup_entries
, n
, sizeof(struct startup_entry
), startup_entry_cmp
);
291 * Then initialize all tunables, and early locks
293 kernel_startup_initialize_upto(STARTUP_SUB_LOCKS_EARLY
);
298 kernel_startup_tunable_init(const struct startup_tunable_spec
*spec
)
300 if (PE_parse_boot_argn(spec
->name
, spec
->var_addr
, spec
->var_len
)) {
301 if (spec
->var_is_bool
) {
302 /* make sure bool's are valued in {0, 1} */
303 *(bool *)spec
->var_addr
= *(uint8_t *)spec
->var_addr
;
309 kernel_startup_log(startup_subsystem_id_t subsystem
)
311 static const char *names
[] = {
312 [STARTUP_SUB_TUNABLES
] = "tunables",
313 [STARTUP_SUB_LOCKS_EARLY
] = "locks_early",
314 [STARTUP_SUB_KPRINTF
] = "kprintf",
316 [STARTUP_SUB_PMAP_STEAL
] = "pmap_steal",
317 [STARTUP_SUB_VM_KERNEL
] = "vm_kernel",
318 [STARTUP_SUB_KMEM
] = "kmem",
319 [STARTUP_SUB_KMEM_ALLOC
] = "kmem_alloc",
320 [STARTUP_SUB_ZALLOC
] = "zalloc",
321 [STARTUP_SUB_PERCPU
] = "percpu",
322 [STARTUP_SUB_LOCKS
] = "locks",
324 [STARTUP_SUB_CODESIGNING
] = "codesigning",
325 [STARTUP_SUB_OSLOG
] = "oslog",
326 [STARTUP_SUB_MACH_IPC
] = "mach_ipc",
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 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
649 kernel_bootstrap_thread_log("ktrace_init");
652 char trace_typefilter
[256] = {};
653 PE_parse_boot_arg_str("trace_typefilter", trace_typefilter
,
654 sizeof(trace_typefilter
));
658 kdebug_init(new_nkdbufs
, trace_typefilter
,
659 (trace_wrap
? KDOPT_WRAPPING
: 0) | KDOPT_ATBOOT
);
662 kernel_bootstrap_log("bsd_early_init");
667 kernel_bootstrap_log("PE_init_iokit");
671 assert(ml_get_interrupts_enabled() == FALSE
);
674 * Past this point, kernel subsystems that expect to operate with
675 * interrupts or preemption enabled may begin enforcement.
677 kernel_startup_initialize_upto(STARTUP_SUB_EARLY_BOOT
);
679 #if INTERRUPT_MASKED_DEBUG
680 // Reset interrupts masked timeout before we enable interrupts
681 ml_spin_debug_clear_self();
683 (void) spllo(); /* Allow interruptions */
686 * This will start displaying progress to the user, start as early as possible
688 initialize_screen(NULL
, kPEAcquireScreen
);
691 * Initialize the shared region module.
694 vm_commpage_text_init();
697 kernel_bootstrap_log("mac_policy_initmach");
698 mac_policy_initmach();
700 kernel_bootstrap_log("vnguard_policy_init");
701 vnguard_policy_init();
706 kernel_bootstrap_log("dtrace_early_init");
712 kernel_startup_initialize_upto(STARTUP_SUB_LOCKDOWN
);
715 * Get rid of segments used to bootstrap kext loading. This removes
716 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
717 * Must be done prior to lockdown so that we can free (and possibly relocate)
718 * the static KVA mappings used for the jettisoned bootstrap segments.
720 kernel_bootstrap_log("OSKextRemoveKextBootstrap");
721 OSKextRemoveKextBootstrap();
724 * Initialize the globals used for permuting kernel
725 * addresses that may be exported to userland as tokens
726 * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL().
727 * Force the random number to be odd to avoid mapping a non-zero
728 * word-aligned address to zero via addition.
729 * Note: at this stage we can use the cryptographically secure PRNG
730 * rather than early_random().
732 read_random(&vm_kernel_addrperm
, sizeof(vm_kernel_addrperm
));
733 vm_kernel_addrperm
|= 1;
734 read_random(&buf_kernel_addrperm
, sizeof(buf_kernel_addrperm
));
735 buf_kernel_addrperm
|= 1;
736 read_random(&vm_kernel_addrperm_ext
, sizeof(vm_kernel_addrperm_ext
));
737 vm_kernel_addrperm_ext
|= 1;
738 read_random(&vm_kernel_addrhash_salt
, sizeof(vm_kernel_addrhash_salt
));
739 read_random(&vm_kernel_addrhash_salt_ext
, sizeof(vm_kernel_addrhash_salt_ext
));
741 /* No changes to kernel text and rodata beyond this point. */
742 kernel_bootstrap_log("machine_lockdown");
746 kernel_bootstrap_log("PE_lockdown_iokit");
750 * max_cpus must be nailed down by the time PE_lockdown_iokit() finishes,
753 vm_set_restrictions(machine_info
.max_cpus
);
755 #ifdef CONFIG_XNUPOST
756 kern_return_t result
= kernel_list_tests();
757 result
= kernel_do_post();
758 if (result
!= KERN_SUCCESS
) {
759 panic("kernel_do_post: Tests failed with result = 0x%08x\n", result
);
761 kernel_bootstrap_log("kernel_do_post - done");
762 #endif /* CONFIG_XNUPOST */
770 * Start the user bootstrap.
778 * Get rid of pages used for early boot tracing.
780 kdebug_free_early_buf();
782 serial_keyboard_init(); /* Start serial keyboard if wanted */
784 vm_page_init_local_q(machine_info
.max_cpus
);
786 thread_bind(PROCESSOR_NULL
);
789 * Now that all CPUs are available to run threads, this is essentially
790 * a background thread. Take this opportunity to initialize and free
791 * any remaining vm_pages that were delayed earlier by pmap_startup().
793 vm_free_delayed_pages();
796 * Become the pageout daemon.
805 * Load the first thread to start a processor.
806 * This path will also be used by the master processor
807 * after being offlined.
810 slave_main(void *machine_param
)
812 processor_t processor
= current_processor();
816 * Use the idle processor thread if there
817 * is no dedicated start up thread.
819 if (processor
->processor_offlined
== true) {
820 /* Return to the saved processor_offline context */
821 assert(processor
->startup_thread
== THREAD_NULL
);
823 thread
= processor
->idle_thread
;
824 thread
->parameter
= machine_param
;
825 } else if (processor
->startup_thread
) {
826 thread
= processor
->startup_thread
;
827 processor
->startup_thread
= THREAD_NULL
;
829 thread
= processor
->idle_thread
;
830 thread
->continuation
= processor_start_thread
;
831 thread
->parameter
= machine_param
;
834 load_context(thread
);
839 * processor_start_thread:
841 * First thread to execute on a started processor.
843 * Called at splsched.
846 processor_start_thread(void *machine_param
,
847 __unused wait_result_t result
)
849 processor_t processor
= current_processor();
850 thread_t self
= current_thread();
852 slave_machine_init(machine_param
);
855 * If running the idle processor thread,
856 * reenter the idle loop, else terminate.
858 if (self
== processor
->idle_thread
) {
859 thread_block(idle_thread
);
862 thread_terminate(self
);
869 * Start the first thread on a processor.
870 * This may be the first thread ever run on a processor, or
871 * it could be a processor that was previously offlined.
873 static void __attribute__((noreturn
))
877 processor_t processor
= current_processor();
880 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
882 load_context_kprintf("machine_set_current_thread\n");
883 machine_set_current_thread(thread
);
885 load_context_kprintf("processor_up\n");
887 PMAP_ACTIVATE_KERNEL(processor
->cpu_id
);
890 * Acquire a stack if none attached. The panic
891 * should never occur since the thread is expected
892 * to have reserved stack.
894 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread
,
895 thread
->kernel_stack
, thread
->machine
.kstackptr
);
896 if (!thread
->kernel_stack
) {
897 load_context_kprintf("stack_alloc_try\n");
898 if (!stack_alloc_try(thread
)) {
899 panic("load_context");
904 * The idle processor threads are not counted as
905 * running for load calculations.
907 if (!(thread
->state
& TH_IDLE
)) {
908 SCHED(run_count_incr
)(thread
);
911 processor
->active_thread
= thread
;
912 processor_state_update_explicit(processor
, thread
->sched_pri
,
913 SFI_CLASS_KERNEL
, PSET_SMP
, thread_get_perfcontrol_class(thread
), THREAD_URGENCY_NONE
,
914 ((thread
->state
& TH_IDLE
) || (thread
->bound_processor
!= PROCESSOR_NULL
)) ? TH_BUCKET_SCHED_MAX
: thread
->th_sched_bucket
);
915 processor
->current_is_bound
= thread
->bound_processor
!= PROCESSOR_NULL
;
916 processor
->current_is_NO_SMT
= false;
917 #if CONFIG_THREAD_GROUPS
918 processor
->current_thread_group
= thread_group_get(thread
);
920 processor
->starting_pri
= thread
->sched_pri
;
921 processor
->deadline
= UINT64_MAX
;
922 thread
->last_processor
= processor
;
923 processor_up(processor
);
924 processor
->last_dispatch
= mach_absolute_time();
925 timer_start(&thread
->system_timer
, processor
->last_dispatch
);
926 processor
->thread_timer
= processor
->kernel_timer
= &thread
->system_timer
;
928 timer_start(&processor
->system_state
, processor
->last_dispatch
);
929 processor
->current_state
= &processor
->system_state
;
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
);