#include <platforms.h>
#include <mach_kdb.h>
#include <mach/i386/vm_param.h>
#include <string.h>
#include <mach/vm_param.h>
#include <mach/vm_prot.h>
#include <mach/machine.h>
#include <mach/time_value.h>
#include <kern/spl.h>
#include <kern/assert.h>
#include <kern/debug.h>
#include <kern/misc_protos.h>
#include <kern/startup.h>
#include <kern/clock.h>
#include <kern/pms.h>
#include <kern/xpr.h>
#include <kern/cpu_data.h>
#include <kern/processor.h>
#include <console/serial_protos.h>
#include <vm/vm_page.h>
#include <vm/pmap.h>
#include <vm/vm_kern.h>
#include <machine/pal_routines.h>
#include <i386/fpu.h>
#include <i386/pmap.h>
#include <i386/misc_protos.h>
#include <i386/cpu_threads.h>
#include <i386/cpuid.h>
#include <i386/lapic.h>
#include <i386/mp.h>
#include <i386/mp_desc.h>
#if CONFIG_MTRR
#include <i386/mtrr.h>
#endif
#include <i386/machine_routines.h>
#if CONFIG_MCA
#include <i386/machine_check.h>
#endif
#include <i386/ucode.h>
#include <i386/postcode.h>
#include <i386/Diagnostics.h>
#include <i386/pmCPU.h>
#include <i386/tsc.h>
#include <i386/locks.h>
#ifdef __i386__
#include <i386/cpu_capabilities.h>
#if MACH_KDB
#include <machine/db_machdep.h>
#endif
#endif
#if DEBUG
#include <machine/pal_routines.h>
#endif
#if DEBUG
#define DBG(x...) kprintf(x)
#else
#define DBG(x...)
#endif
#if MACH_KDB
#include <ddb/db_aout.h>
#endif
int debug_task;
static boot_args *kernelBootArgs;
extern int disableConsoleOutput;
extern const char version[];
extern const char version_variant[];
extern int nx_enabled;
#ifdef __x86_64__
extern void *low_eintstack;
#endif
void *KPTphys;
pd_entry_t *IdlePTD;
#ifdef __i386__
pd_entry_t *IdlePDPT64;
#endif
char *physfree;
static void *
ALLOCPAGES(int npages)
{
uintptr_t tmp = (uintptr_t)physfree;
bzero(physfree, npages * PAGE_SIZE);
physfree += npages * PAGE_SIZE;
#ifdef __x86_64__
tmp += VM_MIN_KERNEL_ADDRESS & ~LOW_4GB_MASK;
#endif
return (void *)tmp;
}
static void
fillkpt(pt_entry_t *base, int prot, uintptr_t src, int index, int count)
{
int i;
for (i=0; i<count; i++) {
base[index] = src | prot | INTEL_PTE_VALID;
src += PAGE_SIZE;
index++;
}
}
extern pmap_paddr_t first_avail;
#ifdef __x86_64__
int break_kprintf = 0;
uint64_t
x86_64_pre_sleep(void)
{
IdlePML4[0] = IdlePML4[KERNEL_PML4_INDEX];
uint64_t oldcr3 = get_cr3_raw();
set_cr3_raw((uint32_t) (uintptr_t)ID_MAP_VTOP(IdlePML4));
return oldcr3;
}
void
x86_64_post_sleep(uint64_t new_cr3)
{
IdlePML4[0] = 0;
set_cr3_raw((uint32_t) new_cr3);
}
#endif
#ifdef __i386__
#define ID_MAP_VTOP(x) x
#endif
#ifdef __x86_64__
#define NPHYSMAP MAX(K64_MAXMEM/GB + 4, 4)
extern int maxphymapsupported[NPHYSMAP <= PTE_PER_PAGE ? 1 : -1];
static void
physmap_init(void)
{
pt_entry_t *physmapL3 = ALLOCPAGES(1);
struct {
pt_entry_t entries[PTE_PER_PAGE];
} * physmapL2 = ALLOCPAGES(NPHYSMAP);
uintptr_t i;
for(i=0;i<NPHYSMAP;i++) {
physmapL3[i] = ((uintptr_t)ID_MAP_VTOP(&physmapL2[i]))
| INTEL_PTE_VALID
| INTEL_PTE_WRITE;
uintptr_t j;
for(j=0;j<PTE_PER_PAGE;j++) {
physmapL2[i].entries[j] = (((i*PTE_PER_PAGE+j)<<PDSHIFT)
| INTEL_PTE_PS
| INTEL_PTE_VALID
| INTEL_PTE_WRITE);
}
}
IdlePML4[KERNEL_PHYSMAP_INDEX] = ((uintptr_t)ID_MAP_VTOP(physmapL3))
| INTEL_PTE_VALID
| INTEL_PTE_WRITE;
if (cpuid_extfeatures() & CPUID_EXTFEATURE_XD) {
IdlePML4[KERNEL_PHYSMAP_INDEX] |= INTEL_PTE_NX;
}
DBG("physical map idlepml4[%d]: 0x%llx\n",
KERNEL_PHYSMAP_INDEX, IdlePML4[KERNEL_PHYSMAP_INDEX]);
}
#endif
static void
Idle_PTs_init(void)
{
KPTphys = ALLOCPAGES(NKPT);
IdlePTD = ALLOCPAGES(NPGPTD);
#ifdef __x86_64__
physmap_init();
#else
IdlePDPT64 = ALLOCPAGES(1);
fillkpt(IdlePTD, INTEL_PTE_WRITE, (uintptr_t)IdlePTD, PTDPTDI, NPGPTD);
fillkpt(IdlePTD, INTEL_PTE_WRITE|INTEL_PTE_USER, (uintptr_t)ALLOCPAGES(1), _COMM_PAGE32_BASE_ADDRESS >> PDESHIFT,1);
#endif
fillkpt(KPTphys,
INTEL_PTE_WRITE, 0, 0, (int)(((uintptr_t)physfree) >> PAGE_SHIFT));
fillkpt(IdlePTD,
INTEL_PTE_WRITE, (uintptr_t)ID_MAP_VTOP(KPTphys), 0, NKPT);
#ifdef __i386__
fillkpt(IdlePDPT, 0, (uintptr_t)IdlePTD, 0, NPGPTD);
#else
fillkpt(IdlePDPT, INTEL_PTE_WRITE, (uintptr_t)ID_MAP_VTOP(IdlePTD), 0, NPGPTD);
#endif
set_cr3_raw(get_cr3_raw());
}
void
vstart(vm_offset_t boot_args_start)
{
boolean_t is_boot_cpu = !(boot_args_start == 0);
int cpu;
uint32_t lphysfree;
postcode(VSTART_ENTRY);
if (is_boot_cpu) {
kernelBootArgs = (boot_args *)boot_args_start;
lphysfree = kernelBootArgs->kaddr + kernelBootArgs->ksize;
physfree = (void *)(uintptr_t)((lphysfree + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1));
#if DEBUG
pal_serial_init();
#endif
DBG("revision 0x%x\n", kernelBootArgs->Revision);
DBG("version 0x%x\n", kernelBootArgs->Version);
DBG("command line %s\n", kernelBootArgs->CommandLine);
DBG("memory map 0x%x\n", kernelBootArgs->MemoryMap);
DBG("memory map sz 0x%x\n", kernelBootArgs->MemoryMapSize);
DBG("kaddr 0x%x\n", kernelBootArgs->kaddr);
DBG("ksize 0x%x\n", kernelBootArgs->ksize);
DBG("physfree %p\n", physfree);
DBG("bootargs: %p, &ksize: %p &kaddr: %p\n",
kernelBootArgs,
&kernelBootArgs->ksize,
&kernelBootArgs->kaddr);
#ifdef __x86_64__
if (cpuid_extfeatures() & CPUID_EXTFEATURE_XD) {
wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_NXE);
DBG("vstart() NX/XD enabled\n");
}
#endif
postcode(PSTART_PAGE_TABLES);
Idle_PTs_init();
first_avail = (vm_offset_t)ID_MAP_VTOP(physfree);
cpu = 0;
cpu_data_alloc(TRUE);
} else {
cpu = lapic_to_cpu[(LAPIC_READ(ID)>>LAPIC_ID_SHIFT) & LAPIC_ID_MASK];
#ifdef __x86_64__
if (cpuid_extfeatures() & CPUID_EXTFEATURE_XD) {
wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_NXE);
DBG("vstart() NX/XD enabled, non-boot\n");
}
#endif
}
#ifdef __x86_64__
if(is_boot_cpu)
cpu_desc_init64(cpu_datap(cpu));
cpu_desc_load64(cpu_datap(cpu));
#else
if(is_boot_cpu)
cpu_desc_init(cpu_datap(cpu));
cpu_desc_load(cpu_datap(cpu));
#endif
if (is_boot_cpu)
cpu_mode_init(current_cpu_datap());
#ifdef __x86_64__
IdlePML4[0] = 0;
#endif
postcode(VSTART_EXIT);
#ifdef __i386__
if (cpuid_extfeatures() & CPUID_EXTFEATURE_XD) {
wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_NXE);
DBG("vstart() NX/XD enabled, i386\n");
}
if (is_boot_cpu)
i386_init(boot_args_start);
else
i386_init_slave();
#else
if (is_boot_cpu)
{
asm volatile(
"mov %1, %%rdi;"
"mov %0, %%rsp;"
"call _i386_init;" : : "r"
(cpu_datap(cpu)->cpu_int_stack_top), "r" (boot_args_start));
}
else
{
asm volatile(
"mov %0, %%rsp;"
"call _i386_init_slave;" : : "r"
(cpu_datap(cpu)->cpu_int_stack_top));
}
#endif
}
void
i386_init(vm_offset_t boot_args_start)
{
unsigned int maxmem;
uint64_t maxmemtouse;
unsigned int cpus = 0;
boolean_t fidn;
boolean_t IA32e = TRUE;
postcode(I386_INIT_ENTRY);
pal_i386_init();
#if CONFIG_MCA
mca_cpu_init();
#endif
kernelBootArgs = (boot_args *)
ml_static_ptovirt(boot_args_start);
DBG("i386_init(0x%lx) kernelBootArgs=%p\n",
(unsigned long)boot_args_start, kernelBootArgs);
PE_init_platform(FALSE, kernelBootArgs);
postcode(PE_INIT_PLATFORM_D);
kernel_early_bootstrap();
master_cpu = 0;
cpu_init();
postcode(CPU_INIT_D);
printf_init();
panic_init();
PE_init_kprintf(FALSE);
if (!PE_parse_boot_argn("diag", &dgWork.dgFlags, sizeof (dgWork.dgFlags)))
dgWork.dgFlags = 0;
serialmode = 0;
if(PE_parse_boot_argn("serial", &serialmode, sizeof (serialmode))) {
kprintf("Serial mode specified: %08X\n", serialmode);
}
if(serialmode & 1) {
(void)switch_to_serial_console();
disableConsoleOutput = FALSE;
}
PE_init_printf(FALSE);
kprintf("version_variant = %s\n", version_variant);
kprintf("version = %s\n", version);
if (!PE_parse_boot_argn("maxmem", &maxmem, sizeof (maxmem)))
maxmemtouse = 0;
else
maxmemtouse = ((uint64_t)maxmem) * MB;
if (PE_parse_boot_argn("cpus", &cpus, sizeof (cpus))) {
if ((0 < cpus) && (cpus < max_ncpus))
max_ncpus = cpus;
}
if (!PE_parse_boot_argn("himemory_mode", &vm_himemory_mode, sizeof (vm_himemory_mode)))
vm_himemory_mode = 0;
if (!PE_parse_boot_argn("immediate_NMI", &fidn, sizeof (fidn)))
force_immediate_debugger_NMI = FALSE;
else
force_immediate_debugger_NMI = fidn;
#if DEBUG
nanoseconds_to_absolutetime(URGENCY_NOTIFICATION_ASSERT_NS, &urgency_notification_assert_abstime_threshold);
#endif
PE_parse_boot_argn("urgency_notification_abstime",
&urgency_notification_assert_abstime_threshold,
sizeof(urgency_notification_assert_abstime_threshold));
#if CONFIG_YONAH
if (cpuid_extfeatures() & CPUID_EXTFEATURE_EM64T) {
boolean_t legacy_mode;
kprintf("EM64T supported");
if (PE_parse_boot_argn("-legacy", &legacy_mode, sizeof (legacy_mode))) {
kprintf(" but legacy mode forced\n");
IA32e = FALSE;
} else {
kprintf(" and will be enabled\n");
}
} else
IA32e = FALSE;
#endif
if (!(cpuid_extfeatures() & CPUID_EXTFEATURE_XD))
nx_enabled = 0;
i386_vm_init(maxmemtouse, IA32e, kernelBootArgs);
PE_init_platform(TRUE, kernelBootArgs);
PE_create_console();
tsc_init();
power_management_init();
processor_bootstrap();
thread_bootstrap();
machine_startup();
}
static void
do_init_slave(boolean_t fast_restart)
{
void *init_param = FULL_SLAVE_INIT;
postcode(I386_INIT_SLAVE);
if (!fast_restart) {
set_cr0(get_cr0() & ~(CR0_NW|CR0_CD));
DBG("i386_init_slave() CPU%d: phys (%d) active.\n",
get_cpu_number(), get_cpu_phys_number());
assert(!ml_get_interrupts_enabled());
cpu_mode_init(current_cpu_datap());
#if CONFIG_MCA
mca_cpu_init();
#endif
lapic_configure();
LAPIC_DUMP();
LAPIC_CPU_MAP_DUMP();
init_fpu();
#if CONFIG_MTRR
mtrr_update_cpu();
#endif
} else
init_param = FAST_SLAVE_INIT;
ucode_update_wake();
#if CONFIG_VMX
vmx_resume();
#endif
#if CONFIG_MTRR
if (!fast_restart)
pat_init();
#endif
cpu_thread_init();
#ifdef __x86_64__
if (!fast_restart)
IdlePML4[0] = 0;
#endif
cpu_init();
slave_main(init_param);
panic("do_init_slave() returned from slave_main()");
}
void
i386_init_slave(void)
{
do_init_slave(FALSE);
}
void
i386_init_slave_fast(void)
{
do_init_slave(TRUE);
}