diff --git a/Architecture/amd64/cpu/AdvancedProgrammableInterruptController.cpp b/Architecture/amd64/cpu/AdvancedProgrammableInterruptController.cpp index f685dc9..b793289 100644 --- a/Architecture/amd64/cpu/AdvancedProgrammableInterruptController.cpp +++ b/Architecture/amd64/cpu/AdvancedProgrammableInterruptController.cpp @@ -220,7 +220,7 @@ namespace APIC Timer::Timer(APIC *apic) : Interrupts::Handler(CPU::x64::IRQ0) { - trace("Initializing APIC timer on CPU %d", CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE)); + trace("Initializing APIC timer on CPU %d", GetCurrentCPU()->ID); this->lapic = apic; this->lapic->Write(APIC_TDCR, 0x3); @@ -259,7 +259,7 @@ namespace APIC this->lapic->Write(APIC_TIMER, (long)CPU::x64::IRQ0 | (long)APIC_PERIODIC); this->lapic->Write(APIC_TDCR, 0x3); this->lapic->Write(APIC_TICR, TicksIn10ms / 10); - trace("APIC Timer (CPU %d): %d ticks in 10ms", CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE), TicksIn10ms / 10); + trace("APIC Timer (CPU %d): %d ticks in 10ms", GetCurrentCPU()->ID, TicksIn10ms / 10); KPrint("APIC Timer: %d ticks in 10ms", TicksIn10ms / 10); } diff --git a/Architecture/amd64/cpu/SymmetricMultiprocessing.cpp b/Architecture/amd64/cpu/SymmetricMultiprocessing.cpp index 51c279c..13aa1ab 100644 --- a/Architecture/amd64/cpu/SymmetricMultiprocessing.cpp +++ b/Architecture/amd64/cpu/SymmetricMultiprocessing.cpp @@ -31,46 +31,21 @@ volatile bool CPUEnabled = false; #pragma GCC diagnostic ignored "-Wmissing-field-initializers" static __attribute__((aligned(PAGE_SIZE))) CPUData CPUs[MAX_CPU] = {0}; -CPUData *GetCPU(uint64_t id) { return &CPUs[id]; } - -CPUData *GetCurrentCPU() -{ - uint64_t ret = 0; -#if defined(__amd64__) - ret = CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE); -#elif defined(__i386__) -#elif defined(__aarch64__) -#endif - - if (!CPUs[ret].IsActive) - { - error("CPU %d is not active!", ret); - return &CPUs[0]; - } - - if (CPUs[ret].Checksum != CPU_DATA_CHECKSUM) - { - error("CPU %d data is corrupted!", ret); - return &CPUs[0]; - } - return &CPUs[ret]; -} +CPUData *GetCPU(long id) { return &CPUs[id]; } +CPUData *GetCurrentCPU() { return (CPUData *)CPU::x64::rdmsr(CPU::x64::MSR_GS_BASE); } extern "C" void StartCPU() { CPU::Interrupts(CPU::Disable); CPU::InitializeFeatures(); - uintptr_t CoreID = CORE; - CPU::x64::wrmsr(CPU::x64::MSR_FS_BASE, (int)*reinterpret_cast(CoreID)); - uint64_t CPU_ID = CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE); - + uint64_t CoreID = (int)*reinterpret_cast(CORE); // Initialize GDT and IDT - Interrupts::Initialize(CPU_ID); - Interrupts::Enable(CPU_ID); - Interrupts::InitializeTimer(CPU_ID); + Interrupts::Initialize(CoreID); + Interrupts::Enable(CoreID); + Interrupts::InitializeTimer(CoreID); CPU::Interrupts(CPU::Enable); - KPrint("CPU %d is online", CPU_ID); + KPrint("CPU %d is online", CoreID); CPUEnabled = true; CPU::Stop(); // Stop and surpress interrupts. } @@ -109,7 +84,7 @@ namespace SMP memcpy((void *)TRAMPOLINE_START, &_trampoline_start, TrampolineLength); POKE(volatile uint64_t, PAGE_TABLE) = CPU::x64::readcr3().raw; - POKE(volatile uint64_t, STACK) = (uint64_t)KernelAllocator.RequestPage(); + POKE(volatile uint64_t, STACK) = (uint64_t)KernelAllocator.RequestPages(TO_PAGES(STACK_SIZE)) + STACK_SIZE; POKE(volatile uint64_t, CORE) = i; asm volatile("sgdt [0x580]\n" @@ -124,6 +99,7 @@ namespace SMP ; trace("CPU %d loaded.", ((ACPI::MADT *)madt)->lapic[i]->APICId); + KernelAllocator.FreePages((void *)*reinterpret_cast(STACK), TO_PAGES(STACK_SIZE)); CPUEnabled = false; } else diff --git a/Core/CPU.cpp b/Core/CPU.cpp index 0761b61..0f92294 100644 --- a/Core/CPU.cpp +++ b/Core/CPU.cpp @@ -177,8 +177,6 @@ namespace CPU { #if defined(__amd64__) static int BSP = 0; - if (!BSP) - CPU::x64::wrmsr(CPU::x64::MSR_FS_BASE, 0); CPU::x64::CR0 cr0 = CPU::x64::readcr0(); CPU::x64::CR4 cr4 = CPU::x64::readcr4(); uint32_t rax, rbx, rcx, rdx; diff --git a/Core/CrashHandler.cpp b/Core/CrashHandler.cpp index cf722de..bf5cc32 100644 --- a/Core/CrashHandler.cpp +++ b/Core/CrashHandler.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #if defined(__amd64__) @@ -215,8 +216,7 @@ namespace CrashHandler } } - EHPrint("\e7981FCTechnical Informations on CPU %lld:\n", - CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE)); + EHPrint("\e7981FCTechnical Informations on CPU %lld:\n", GetCurrentCPU()->ID); EHPrint("FS=%#llx GS=%#llx SS=%#llx CS=%#llx DS=%#llx\n", CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE), CPU::x64::rdmsr(CPU::x64::MSR_GS_BASE), Frame->ss, Frame->cs, Frame->ds); diff --git a/Kernel.cpp b/Kernel.cpp index 6c0d085..75a163f 100644 --- a/Kernel.cpp +++ b/Kernel.cpp @@ -54,9 +54,6 @@ EXTERNC void Entry(BootInfo *Info) BootClock.Hour, BootClock.Minute, BootClock.Second, BootClock.Day, BootClock.Month, BootClock.Year); KPrint("CPU: \e8822AA%s \e8888FF%s (\e058C19%s\e8888FF)", CPU::Vendor(), CPU::Name(), CPU::Hypervisor()); - GetCPU(0)->ID = 0; - GetCPU(0)->IsActive = true; - GetCPU(0)->Checksum = CPU_DATA_CHECKSUM; KPrint("Initializing GDT and IDT"); Interrupts::Initialize(0); KPrint("Initializing CPU features"); @@ -86,5 +83,6 @@ EXTERNC void Entry(BootInfo *Info) SMP::Initialize(PowerManager->GetMADT()); KPrint("\e058C19######## \eE85230END \e058C19########"); CPU::Interrupts(CPU::Enable); + // asm("int $0x1"); CPU::Stop(); } diff --git a/include/memory.hpp b/include/memory.hpp index 2fe6d43..006b12e 100644 --- a/include/memory.hpp +++ b/include/memory.hpp @@ -35,6 +35,7 @@ extern uint64_t _kernel_text_end, _kernel_data_end, _kernel_rodata_end; #define TO_GPB(d) (d / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024) #define PAGE_SIZE 0x1000 +#define STACK_SIZE 0x10000 // to pages #define TO_PAGES(d) (d / PAGE_SIZE + 1) diff --git a/include/smp.hpp b/include/smp.hpp index 79d0840..f3597fd 100644 --- a/include/smp.hpp +++ b/include/smp.hpp @@ -5,35 +5,40 @@ /** @brief Maximum supported number of CPU cores by the kernel */ #define MAX_CPU 256 - #define CPU_DATA_CHECKSUM 0xC0FFEE +struct CPUArchData +{ +#if defined(__amd64__) + int stub; + /* TODO */ +#elif defined(__i386__) +#elif defined(__aarch64__) +#endif +}; + struct CPUData { - /** - * @brief CPU ID. - */ - uint64_t ID; - /** - * @brief Local CPU error code. - */ + /** @brief Used by syscall handler */ + uint8_t *SystemCallStack; /* gs+0x0 */ + /** @brief Used by syscall handler */ + uint64_t TempStack; /* gs+0x8 */ + /** @brief Used by CPU */ + uint64_t Stack; + /** @brief CPU ID. */ + long ID; + /** @brief Local CPU error code. */ long ErrorCode; - /** - * @brief Is CPU online? - */ + /** @brief Is CPU online? */ bool IsActive; - /** - * @brief Architecture-specific CPU data. - */ - void *Data; - /** - * @brief Checksum. Used to verify the integrity of the data. Must be equal to CPU_DATA_CHECKSUM (0xC0FFEE). - */ + /** @brief Architecture-specific CPU data. */ + CPUArchData *Data; + /** @brief Checksum. Used to verify the integrity of the data. Must be equal to CPU_DATA_CHECKSUM (0xC0FFEE). */ int Checksum; } __attribute__((packed)); CPUData *GetCurrentCPU(); -CPUData *GetCPU(uint64_t ID); +CPUData *GetCPU(long ID); namespace SMP {