From f95d8bf159d10b5a9521dcaa0bc37aa0e9dfc02b Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 5 May 2024 21:10:18 +0200 Subject: MHR-23: Add run_format.sh, kernel patches. Signed-off-by: Amlal El Mahrouss --- Private/HALKit/AMD64/HalDescriptorLoader.cpp | 152 +++++++++++++++------------ 1 file changed, 82 insertions(+), 70 deletions(-) (limited to 'Private/HALKit/AMD64/HalDescriptorLoader.cpp') diff --git a/Private/HALKit/AMD64/HalDescriptorLoader.cpp b/Private/HALKit/AMD64/HalDescriptorLoader.cpp index 53bcd621..53217021 100644 --- a/Private/HALKit/AMD64/HalDescriptorLoader.cpp +++ b/Private/HALKit/AMD64/HalDescriptorLoader.cpp @@ -6,73 +6,85 @@ #include -namespace NewOS::HAL { -namespace Detail { -STATIC RegisterGDT kRegGdt; -STATIC HAL::Register64 kRegIdt; - -STATIC ::NewOS::Detail::AMD64::InterruptDescriptorAMD64 - kInterruptVectorTable[kKernelIdtSize]; - -STATIC Void RemapPIC(Void) noexcept { - // Remap PIC. - HAL::Out8(0x20, 0x10 | 0x01); - HAL::Out8(0xA0, 0x10 | 0x01); - - HAL::Out8(0x21, 32); - HAL::Out8(0xA1, 40); - - HAL::Out8(0x21, 4); - HAL::Out8(0xA1, 2); - - HAL::Out8(0x21, 0x01); - HAL::Out8(0xA1, 0x01); - - HAL::Out8(0x21, 0x00); - HAL::Out8(0xA1, 0x00); -} -} // namespace Detail - -/// @brief Loads the provided Global Descriptor Table. -/// @param gdt -/// @return -Void GDTLoader::Load(RegisterGDT &gdt) { - MUST_PASS(gdt.Base != 0); - - Detail::kRegGdt.Base = gdt.Base; - Detail::kRegGdt.Limit = gdt.Limit; - - hal_load_gdt(Detail::kRegGdt); -} - -Void IDTLoader::Load(Register64 &idt) { - volatile ::NewOS::UIntPtr **baseIdt = (volatile ::NewOS::UIntPtr **)idt.Base; - - MUST_PASS(baseIdt); - - Detail::RemapPIC(); - - for (UInt16 i = 0; i < kKernelIdtSize; ++i) { - MUST_PASS(baseIdt[i]); - - Detail::kInterruptVectorTable[i].Selector = kGdtCodeSelector; - Detail::kInterruptVectorTable[i].Ist = 0x0; - Detail::kInterruptVectorTable[i].TypeAttributes = kInterruptGate; - Detail::kInterruptVectorTable[i].OffsetLow = ((UIntPtr)baseIdt[i] & 0xFFFF); - Detail::kInterruptVectorTable[i].OffsetMid = (((UIntPtr)baseIdt[i] >> 16) & 0xFFFF); - Detail::kInterruptVectorTable[i].OffsetHigh = - (((UIntPtr)baseIdt[i] >> 32) & 0xFFFFFFFF); - Detail::kInterruptVectorTable[i].Zero = 0x0; - } - - Detail::kRegIdt.Base = reinterpret_cast(Detail::kInterruptVectorTable); - Detail::kRegIdt.Limit = sizeof(::NewOS::Detail::AMD64::InterruptDescriptorAMD64) * - (kKernelIdtSize - 1); - - hal_load_idt(Detail::kRegIdt); -} - -void GDTLoader::Load(Ref &gdt) { GDTLoader::Load(gdt.Leak()); } - -void IDTLoader::Load(Ref &idt) { IDTLoader::Load(idt.Leak()); } -} // namespace NewOS::HAL +namespace NewOS::HAL +{ + namespace Detail + { + STATIC RegisterGDT kRegGdt; + STATIC HAL::Register64 kRegIdt; + + STATIC ::NewOS::Detail::AMD64::InterruptDescriptorAMD64 + kInterruptVectorTable[kKernelIdtSize]; + + STATIC Void RemapPIC(Void) noexcept + { + // Remap PIC. + HAL::Out8(0x20, 0x10 | 0x01); + HAL::Out8(0xA0, 0x10 | 0x01); + + HAL::Out8(0x21, 32); + HAL::Out8(0xA1, 40); + + HAL::Out8(0x21, 4); + HAL::Out8(0xA1, 2); + + HAL::Out8(0x21, 0x01); + HAL::Out8(0xA1, 0x01); + + HAL::Out8(0x21, 0x00); + HAL::Out8(0xA1, 0x00); + } + } // namespace Detail + + /// @brief Loads the provided Global Descriptor Table. + /// @param gdt + /// @return + Void GDTLoader::Load(RegisterGDT& gdt) + { + MUST_PASS(gdt.Base != 0); + + Detail::kRegGdt.Base = gdt.Base; + Detail::kRegGdt.Limit = gdt.Limit; + + hal_load_gdt(Detail::kRegGdt); + } + + Void IDTLoader::Load(Register64& idt) + { + volatile ::NewOS::UIntPtr** baseIdt = (volatile ::NewOS::UIntPtr**)idt.Base; + + MUST_PASS(baseIdt); + + Detail::RemapPIC(); + + for (UInt16 i = 0; i < kKernelIdtSize; ++i) + { + MUST_PASS(baseIdt[i]); + + Detail::kInterruptVectorTable[i].Selector = kGdtCodeSelector; + Detail::kInterruptVectorTable[i].Ist = 0x0; + Detail::kInterruptVectorTable[i].TypeAttributes = kInterruptGate; + Detail::kInterruptVectorTable[i].OffsetLow = ((UIntPtr)baseIdt[i] & 0xFFFF); + Detail::kInterruptVectorTable[i].OffsetMid = (((UIntPtr)baseIdt[i] >> 16) & 0xFFFF); + Detail::kInterruptVectorTable[i].OffsetHigh = + (((UIntPtr)baseIdt[i] >> 32) & 0xFFFFFFFF); + Detail::kInterruptVectorTable[i].Zero = 0x0; + } + + Detail::kRegIdt.Base = reinterpret_cast(Detail::kInterruptVectorTable); + Detail::kRegIdt.Limit = sizeof(::NewOS::Detail::AMD64::InterruptDescriptorAMD64) * + (kKernelIdtSize - 1); + + hal_load_idt(Detail::kRegIdt); + } + + void GDTLoader::Load(Ref& gdt) + { + GDTLoader::Load(gdt.Leak()); + } + + void IDTLoader::Load(Ref& idt) + { + IDTLoader::Load(idt.Leak()); + } +} // namespace NewOS::HAL -- cgit v1.2.3