mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 14:28:12 +00:00
Kernel: Prevent sign bit extension when creating a PDPTE
When doing the cast to u64 on the page directory physical address, the sign bit was being extended. This only beomes an issue when crossing the 2 GiB boundary. At >= 2 GiB, the physical address has the sign bit set. For example, 0x80000000. This set all the reserved bits in the PDPTE, causing a GPF when loading the PDPT pointer into CR3. The reserved bits are presumably there to stop you writing out a physical address that the CPU physically cannot handle, as the size of the reserved bits is determined by the physical address width of the CPU. This fixes this by casting to FlatPtr instead. I believe the sign extension only happens when casting to a bigger type. I'm also using FlatPtr because it's a pointer we're writing into the PDPTE. sizeof(FlatPtr) will always be the same size as sizeof(void*). This also now asserts that the physical address in the PDPTE is within the max physical address the CPU supports. This is better than getting a GPF, because CPU::handle_crash tries to do the same operation that caused the GPF in the first place. That would cause an infinite loop of GPFs until the stack was exhausted, causing a triple fault. As far as I know and tested, I believe we can now use the full 32-bit physical range without crashing. Fixes #4584. See that issue for the full debugging story.
This commit is contained in:
parent
d014277973
commit
865f5ed4f6
3 changed files with 44 additions and 4 deletions
|
@ -916,6 +916,7 @@ u32 read_cr3()
|
|||
|
||||
void write_cr3(u32 cr3)
|
||||
{
|
||||
// NOTE: If you're here from a GPF crash, it's very likely that a PDPT entry is incorrect, not this!
|
||||
asm volatile("movl %%eax, %%cr3" ::"a"(cr3)
|
||||
: "memory");
|
||||
}
|
||||
|
@ -1033,6 +1034,15 @@ void Processor::cpu_detect()
|
|||
}
|
||||
}
|
||||
|
||||
if (max_extended_leaf >= 0x80000008) {
|
||||
// CPUID.80000008H:EAX[7:0] reports the physical-address width supported by the processor.
|
||||
CPUID cpuid(0x80000008);
|
||||
m_physical_address_bit_width = cpuid.eax() & 0xff;
|
||||
} else {
|
||||
// For processors that do not support CPUID function 80000008H, the width is generally 36 if CPUID.01H:EDX.PAE [bit 6] = 1 and 32 otherwise.
|
||||
m_physical_address_bit_width = has_feature(CPUFeature::PAE) ? 36 : 32;
|
||||
}
|
||||
|
||||
CPUID extended_features(0x7);
|
||||
if (extended_features.ebx() & (1 << 20))
|
||||
set_feature(CPUFeature::SMAP);
|
||||
|
@ -1218,6 +1228,7 @@ void Processor::initialize(u32 cpu)
|
|||
klog() << "CPU[" << id() << "]: Supported features: " << features_string();
|
||||
if (!has_feature(CPUFeature::RDRAND))
|
||||
klog() << "CPU[" << id() << "]: No RDRAND support detected, randomness will be poor";
|
||||
klog() << "CPU[" << id() << "]: Physical address bit width: " << m_physical_address_bit_width;
|
||||
|
||||
if (cpu == 0)
|
||||
idt_init();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue