#include <debug.h>
#include <xpr_debug.h>
#include <mach_kdp.h>
#include <norma_vm.h>
#include <mach/boolean.h>
#include <mach/machine.h>
#include <mach/thread_act.h>
#include <mach/task_special_ports.h>
#include <mach/vm_param.h>
#include <ipc/ipc_init.h>
#include <kern/assert.h>
#include <kern/mach_param.h>
#include <kern/misc_protos.h>
#include <kern/clock.h>
#include <kern/cpu_number.h>
#include <kern/ledger.h>
#include <kern/machine.h>
#include <kern/processor.h>
#include <kern/sched_prim.h>
#include <kern/startup.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/timer.h>
#include <kern/wait_queue.h>
#include <kern/xpr.h>
#include <kern/zalloc.h>
#include <kern/locks.h>
#include <console/serial_protos.h>
#include <vm/vm_kern.h>
#include <vm/vm_init.h>
#include <vm/vm_map.h>
#include <vm/vm_object.h>
#include <vm/vm_page.h>
#include <vm/vm_pageout.h>
#include <vm/vm_shared_region.h>
#include <machine/pmap.h>
#include <machine/commpage.h>
#include <libkern/version.h>
#if MACH_KDP
#include <kdp/kdp.h>
#endif
#if CONFIG_MACF
#include <security/mac_mach_internal.h>
#endif
#if CONFIG_COUNTERS
#include <pmc/pmc.h>
#endif
#ifdef __ppc__
#include <ppc/Firmware.h>
#include <ppc/mappings.h>
#endif
static void kernel_bootstrap_thread(void);
static void load_context(
thread_t thread);
#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
extern void cpu_userwindow_init(int);
extern void cpu_physwindow_init(int);
#endif
extern void OSKextRemoveKextBootstrap(void);
void srv_setup(void);
extern void bsd_srv_setup(int);
extern unsigned int semaphore_max;
extern int srv;
void
kernel_bootstrap(void)
{
kern_return_t result;
thread_t thread;
printf("%s\n", version);
#define kernel_bootstrap_kprintf(x...)
if (PE_parse_boot_argn("srv", &srv, sizeof (srv))) {
srv = 1;
}
srv_setup();
kernel_bootstrap_kprintf("calling lck_mod_init\n");
lck_mod_init();
kernel_bootstrap_kprintf("calling vm_mem_bootstrap\n");
vm_mem_bootstrap();
kernel_bootstrap_kprintf("calling vm_mem_init\n");
vm_mem_init();
machine_info.memory_size = (uint32_t)mem_size;
machine_info.max_mem = max_mem;
machine_info.major_version = version_major;
machine_info.minor_version = version_minor;
kernel_bootstrap_kprintf("calling sched_init\n");
sched_init();
kernel_bootstrap_kprintf("calling wait_queue_bootstrap\n");
wait_queue_bootstrap();
kernel_bootstrap_kprintf("calling ipc_bootstrap\n");
ipc_bootstrap();
#if CONFIG_MACF
mac_policy_init();
#endif
kernel_bootstrap_kprintf("calling ipc_init\n");
ipc_init();
kernel_bootstrap_kprintf("calling PMAP_ACTIVATE_KERNEL\n");
PMAP_ACTIVATE_KERNEL(master_cpu);
kernel_bootstrap_kprintf("calling mapping_free_prime\n");
mapping_free_prime();
kernel_bootstrap_kprintf("calling machine_init\n");
machine_init();
kernel_bootstrap_kprintf("calling clock_init\n");
clock_init();
kernel_bootstrap_kprintf("calling ledger_init\n");
ledger_init();
kernel_bootstrap_kprintf("calling task_init\n");
task_init();
kernel_bootstrap_kprintf("calling thread_init\n");
thread_init();
kernel_bootstrap_kprintf("calling kernel_thread_create\n");
result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
thread->state = TH_RUN;
thread_deallocate(thread);
kernel_bootstrap_kprintf("calling load_context - done\n");
load_context(thread);
}
int kth_started = 0;
static void
kernel_bootstrap_thread(void)
{
processor_t processor = current_processor();
#define kernel_bootstrap_thread_kprintf(x...)
kernel_bootstrap_thread_kprintf("calling idle_thread_create\n");
idle_thread_create(processor);
kernel_bootstrap_thread_kprintf("calling sched_startup\n");
sched_startup();
kernel_bootstrap_thread_kprintf("calling thread_bind\n");
thread_bind(processor);
kernel_bootstrap_thread_kprintf("calling mapping_adjust\n");
mapping_adjust();
kernel_bootstrap_thread_kprintf("calling clock_service_create\n");
clock_service_create();
device_service_create();
kth_started = 1;
#if MACH_KDP
kernel_bootstrap_kprintf("calling kdp_init\n");
kdp_init();
#endif
#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
cpu_physwindow_init(0);
#endif
#if CONFIG_COUNTERS
pmc_bootstrap();
#endif
#ifdef IOKIT
PE_init_iokit();
#endif
(void) spllo();
#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
cpu_userwindow_init(0);
#endif
vm_shared_region_init();
vm_commpage_init();
#if CONFIG_MACF
mac_policy_initmach();
#endif
#ifdef MACH_BSD
bsd_init();
#endif
OSKextRemoveKextBootstrap();
serial_keyboard_init();
vm_page_init_local_q();
thread_bind(PROCESSOR_NULL);
vm_pageout();
}
void
slave_main(void *machine_param)
{
processor_t processor = current_processor();
thread_t thread;
if (processor->next_thread == THREAD_NULL) {
thread = processor->idle_thread;
thread->continuation = (thread_continue_t)processor_start_thread;
thread->parameter = machine_param;
}
else {
thread = processor->next_thread;
processor->next_thread = THREAD_NULL;
}
load_context(thread);
}
void
processor_start_thread(void *machine_param)
{
processor_t processor = current_processor();
thread_t self = current_thread();
slave_machine_init(machine_param);
if (self == processor->idle_thread)
thread_block((thread_continue_t)idle_thread);
thread_terminate(self);
}
static void
load_context(
thread_t thread)
{
processor_t processor = current_processor();
#define load_context_kprintf(x...)
load_context_kprintf("calling machine_set_current_thread\n");
machine_set_current_thread(thread);
load_context_kprintf("calling processor_up\n");
processor_up(processor);
PMAP_ACTIVATE_KERNEL(processor->cpu_id);
load_context_kprintf("stack %x, stackptr %x\n",
thread->kernel_stack, thread->machine.kstackptr);
if (!thread->kernel_stack) {
load_context_kprintf("calling stack_alloc_try\n");
if (!stack_alloc_try(thread))
panic("load_context");
}
if (!(thread->state & TH_IDLE))
sched_run_incr();
processor->active_thread = thread;
processor->current_pri = thread->sched_pri;
processor->deadline = UINT64_MAX;
thread->last_processor = processor;
processor->last_dispatch = mach_absolute_time();
timer_start(&thread->system_timer, processor->last_dispatch);
PROCESSOR_DATA(processor, thread_timer) = PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch);
PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
PMAP_ACTIVATE_USER(thread, processor->cpu_id);
load_context_kprintf("calling machine_load_context\n");
machine_load_context(thread);
}
void
srv_setup()
{
int scale = 0;
#if defined(__LP64__)
if ((srv != 0) && ((uint64_t)sane_size >= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
scale = (int)((uint64_t)sane_size / (uint64_t)(8 * 1024 * 1024 *1024ULL));
if (scale > 16)
scale = 16;
task_max = 2500 * scale;
task_threadmax = task_max;
thread_max = task_max * 5;
} else
scale = 0;
#endif
bsd_srv_setup(scale);
ipc_space_max = SPACE_MAX;
ipc_tree_entry_max = ITE_MAX;
ipc_port_max = PORT_MAX;
ipc_pset_max = SET_MAX;
semaphore_max = SEMAPHORE_MAX;
}