Changed SMP code

This commit is contained in:
Alex 2022-10-20 05:04:15 +03:00
parent 7f47b2a3a4
commit fa92676d9f
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
7 changed files with 39 additions and 61 deletions

View File

@ -220,7 +220,7 @@ namespace APIC
Timer::Timer(APIC *apic) : Interrupts::Handler(CPU::x64::IRQ0) 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 = apic;
this->lapic->Write(APIC_TDCR, 0x3); 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_TIMER, (long)CPU::x64::IRQ0 | (long)APIC_PERIODIC);
this->lapic->Write(APIC_TDCR, 0x3); this->lapic->Write(APIC_TDCR, 0x3);
this->lapic->Write(APIC_TICR, TicksIn10ms / 10); 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); KPrint("APIC Timer: %d ticks in 10ms", TicksIn10ms / 10);
} }

View File

@ -31,46 +31,21 @@ volatile bool CPUEnabled = false;
#pragma GCC diagnostic ignored "-Wmissing-field-initializers" #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
static __attribute__((aligned(PAGE_SIZE))) CPUData CPUs[MAX_CPU] = {0}; static __attribute__((aligned(PAGE_SIZE))) CPUData CPUs[MAX_CPU] = {0};
CPUData *GetCPU(uint64_t id) { return &CPUs[id]; } CPUData *GetCPU(long id) { return &CPUs[id]; }
CPUData *GetCurrentCPU() { return (CPUData *)CPU::x64::rdmsr(CPU::x64::MSR_GS_BASE); }
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];
}
extern "C" void StartCPU() extern "C" void StartCPU()
{ {
CPU::Interrupts(CPU::Disable); CPU::Interrupts(CPU::Disable);
CPU::InitializeFeatures(); CPU::InitializeFeatures();
uintptr_t CoreID = CORE; uint64_t CoreID = (int)*reinterpret_cast<int *>(CORE);
CPU::x64::wrmsr(CPU::x64::MSR_FS_BASE, (int)*reinterpret_cast<int *>(CoreID));
uint64_t CPU_ID = CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE);
// Initialize GDT and IDT // Initialize GDT and IDT
Interrupts::Initialize(CPU_ID); Interrupts::Initialize(CoreID);
Interrupts::Enable(CPU_ID); Interrupts::Enable(CoreID);
Interrupts::InitializeTimer(CPU_ID); Interrupts::InitializeTimer(CoreID);
CPU::Interrupts(CPU::Enable); CPU::Interrupts(CPU::Enable);
KPrint("CPU %d is online", CPU_ID); KPrint("CPU %d is online", CoreID);
CPUEnabled = true; CPUEnabled = true;
CPU::Stop(); // Stop and surpress interrupts. CPU::Stop(); // Stop and surpress interrupts.
} }
@ -109,7 +84,7 @@ namespace SMP
memcpy((void *)TRAMPOLINE_START, &_trampoline_start, TrampolineLength); memcpy((void *)TRAMPOLINE_START, &_trampoline_start, TrampolineLength);
POKE(volatile uint64_t, PAGE_TABLE) = CPU::x64::readcr3().raw; 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; POKE(volatile uint64_t, CORE) = i;
asm volatile("sgdt [0x580]\n" asm volatile("sgdt [0x580]\n"
@ -124,6 +99,7 @@ namespace SMP
; ;
trace("CPU %d loaded.", ((ACPI::MADT *)madt)->lapic[i]->APICId); trace("CPU %d loaded.", ((ACPI::MADT *)madt)->lapic[i]->APICId);
KernelAllocator.FreePages((void *)*reinterpret_cast<long *>(STACK), TO_PAGES(STACK_SIZE));
CPUEnabled = false; CPUEnabled = false;
} }
else else

View File

@ -177,8 +177,6 @@ namespace CPU
{ {
#if defined(__amd64__) #if defined(__amd64__)
static int BSP = 0; static int BSP = 0;
if (!BSP)
CPU::x64::wrmsr(CPU::x64::MSR_FS_BASE, 0);
CPU::x64::CR0 cr0 = CPU::x64::readcr0(); CPU::x64::CR0 cr0 = CPU::x64::readcr0();
CPU::x64::CR4 cr4 = CPU::x64::readcr4(); CPU::x64::CR4 cr4 = CPU::x64::readcr4();
uint32_t rax, rbx, rcx, rdx; uint32_t rax, rbx, rcx, rdx;

View File

@ -3,6 +3,7 @@
#include <display.hpp> #include <display.hpp>
#include <printf.h> #include <printf.h>
#include <debug.h> #include <debug.h>
#include <smp.hpp>
#include <cpu.hpp> #include <cpu.hpp>
#if defined(__amd64__) #if defined(__amd64__)
@ -215,8 +216,7 @@ namespace CrashHandler
} }
} }
EHPrint("\e7981FCTechnical Informations on CPU %lld:\n", EHPrint("\e7981FCTechnical Informations on CPU %lld:\n", GetCurrentCPU()->ID);
CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE));
EHPrint("FS=%#llx GS=%#llx SS=%#llx CS=%#llx DS=%#llx\n", 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), CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE), CPU::x64::rdmsr(CPU::x64::MSR_GS_BASE),
Frame->ss, Frame->cs, Frame->ds); Frame->ss, Frame->cs, Frame->ds);

View File

@ -54,9 +54,6 @@ EXTERNC void Entry(BootInfo *Info)
BootClock.Hour, BootClock.Minute, BootClock.Second, BootClock.Hour, BootClock.Minute, BootClock.Second,
BootClock.Day, BootClock.Month, BootClock.Year); BootClock.Day, BootClock.Month, BootClock.Year);
KPrint("CPU: \e8822AA%s \e8888FF%s (\e058C19%s\e8888FF)", CPU::Vendor(), CPU::Name(), CPU::Hypervisor()); 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"); KPrint("Initializing GDT and IDT");
Interrupts::Initialize(0); Interrupts::Initialize(0);
KPrint("Initializing CPU features"); KPrint("Initializing CPU features");
@ -86,5 +83,6 @@ EXTERNC void Entry(BootInfo *Info)
SMP::Initialize(PowerManager->GetMADT()); SMP::Initialize(PowerManager->GetMADT());
KPrint("\e058C19######## \eE85230END \e058C19########"); KPrint("\e058C19######## \eE85230END \e058C19########");
CPU::Interrupts(CPU::Enable); CPU::Interrupts(CPU::Enable);
// asm("int $0x1");
CPU::Stop(); CPU::Stop();
} }

View File

@ -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 TO_GPB(d) (d / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024)
#define PAGE_SIZE 0x1000 #define PAGE_SIZE 0x1000
#define STACK_SIZE 0x10000
// to pages // to pages
#define TO_PAGES(d) (d / PAGE_SIZE + 1) #define TO_PAGES(d) (d / PAGE_SIZE + 1)

View File

@ -5,35 +5,40 @@
/** @brief Maximum supported number of CPU cores by the kernel */ /** @brief Maximum supported number of CPU cores by the kernel */
#define MAX_CPU 256 #define MAX_CPU 256
#define CPU_DATA_CHECKSUM 0xC0FFEE #define CPU_DATA_CHECKSUM 0xC0FFEE
struct CPUArchData
{
#if defined(__amd64__)
int stub;
/* TODO */
#elif defined(__i386__)
#elif defined(__aarch64__)
#endif
};
struct CPUData struct CPUData
{ {
/** /** @brief Used by syscall handler */
* @brief CPU ID. uint8_t *SystemCallStack; /* gs+0x0 */
*/ /** @brief Used by syscall handler */
uint64_t ID; uint64_t TempStack; /* gs+0x8 */
/** /** @brief Used by CPU */
* @brief Local CPU error code. uint64_t Stack;
*/ /** @brief CPU ID. */
long ID;
/** @brief Local CPU error code. */
long ErrorCode; long ErrorCode;
/** /** @brief Is CPU online? */
* @brief Is CPU online?
*/
bool IsActive; bool IsActive;
/** /** @brief Architecture-specific CPU data. */
* @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). */
void *Data;
/**
* @brief Checksum. Used to verify the integrity of the data. Must be equal to CPU_DATA_CHECKSUM (0xC0FFEE).
*/
int Checksum; int Checksum;
} __attribute__((packed)); } __attribute__((packed));
CPUData *GetCurrentCPU(); CPUData *GetCurrentCPU();
CPUData *GetCPU(uint64_t ID); CPUData *GetCPU(long ID);
namespace SMP namespace SMP
{ {