Optimize GetCurrentCPU

This commit is contained in:
Alex
2023-02-14 16:07:02 +02:00
parent b3cbcd474d
commit 2c714bbcbc
2 changed files with 6 additions and 5 deletions

View File

@ -28,17 +28,17 @@ volatile bool CPUEnabled = false;
static __attribute__((aligned(PAGE_SIZE))) CPUData CPUs[MAX_CPU] = {0};
CPUData *GetCPU(long id) { return &CPUs[id]; }
CPUData *GetCurrentCPU()
SafeFunction CPUData *GetCurrentCPU()
{
CPUData *data = (CPUData *)CPU::x64::rdmsr(CPU::x64::MSR_GS_BASE);
if (data == nullptr && Interrupts::apic[0])
if (unlikely(data == nullptr && Interrupts::apic[0]))
data = &CPUs[((APIC::APIC *)Interrupts::apic[0])->Read(APIC::APIC_ID) >> 24];
if (data == nullptr)
if (unlikely(data == nullptr))
return nullptr; // The caller should handle this.
if (!data->IsActive)
if (unlikely(data->IsActive != true))
{
error("CPU %d is not active!", data->ID);
if ((&CPUs[0])->IsActive)
@ -46,6 +46,7 @@ CPUData *GetCurrentCPU()
else
return nullptr; // We are in trouble.
}
assert(data->Checksum == CPU_DATA_CHECKSUM); // This should never happen.
return data;
}