Sync/kernel/bootstrap.c

99 lines
2.6 KiB
C
Raw Normal View History

/************************
*** Team Kitty, 2019 ***
*** Sync ***
***********************/
/* This file contains all of the functions required to handle
* the system handover from Syncboot.
*/
#include <kernel.h>
static void UpdateSegments(void);
static void InstallInterrupt(size_t ISR, size_t Function);
static void InstallTrap(size_t ISR, size_t Function);
/* Main system handover from UEFI.
* Prepares the processor, the screen, and memory. */
void PrepareSystem(FILELOADER_PARAMS* FLOP) {
Memory_Info.MemoryMap = FLOP->MemoryMap;
Memory_Info.MemoryMapSize = FLOP->MemoryMap_Size;
Memory_Info.MemoryMapDescriptorSize = FLOP->MemoryMapDescriptorSize;
Memory_Info.MemoryMapDescriptorVersion = FLOP->MemoryMapDescriptorVersion;
if(SetIdentityMap(FLOP->RTServices) == NULL) {
Memory_Info.MemoryMap = FLOP->MemoryMap;
}
SetupPrinting(FLOP->GPU_Info->GPUs[0]);
/* All print functions are now available. */
PrepareAVX();
/* Bit 5 of CR0 is Numeric Error, which enables
* the internal x87 Floating Point Math error
* reporting. */
size_t CR0 = ReadControlRegister(0);
// Mask CR0 to only see bit 5
if( !(CR0 & (1 << 5))) {
// Preserve CR0, but set bit 5.
size_t TempReg = CR0 ^ (1 << 5);
// Write it back
WriteControlRegister(0, TempReg);
// Double check. Some processors can be tricky.
TempReg = ReadControlRegister(CR0);
if(TempReg == CR0)
printf("Error setting CR0.NE\r\n");
}
/* Bit 10 of CR4 is OSXMMEXCPT, which enables
* SSE instructions. */
size_t CR4 = ReadControlRegister(4);
// Mask CR4 to only see bit 10
if( !(CR4 & (1 << 10))) {
// Preserve CR4, but set bit 10.
size_t TempReg = CR4 ^ (1 << 10);
// Write it back
WriteControlRegister(4, TempReg);
// Double check. Some processors can be tricky.
TempReg = ReadControlRegister(4);
if(TempReg == CR4)
printf("Error setting CR4.OSXMMEXCPT\r\n");
}
// Set up memory management
InstallGDT();
InstallIDT();
InstallMemoryMap();
InstallPaging();
// Clean up UEFI's mess
ReclaimEfiBootServicesMemory();
ReclaimEfiLoaderCodeMemory();
// Let Intel ME take over power management
PreparePowerManagement();
}
/* A temporary system for keeping track of system performance. */
size_t ClockTick() {
// We will be reading a register, so we need to split it up.
size_t RegHigh = 0, RegLow = 0;
__asm__ __volatile__("rdtscp" : "=a" (RegLow), "=d" (RegHigh) : : "%rcx");
}