diff options
Diffstat (limited to 'Private')
| -rw-r--r-- | Private/HALKit/AMD64/HalCoreInterruptHandler.cpp | 30 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp | 12 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalCoreSyscallHandlerAMD64.cpp | 8 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalDescriptorLoader.cpp | 29 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalHardwareAPIC.cpp | 7 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalInterruptRouting.asm | 121 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalKernelMain.cxx | 22 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalPageAlloc.cpp | 20 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/Processor.hpp | 34 | ||||
| -rw-r--r-- | Private/NewBoot/Source/makefile | 4 | ||||
| -rw-r--r-- | Private/Source/String.cxx | 1 | ||||
| -rw-r--r-- | Private/makefile | 2 |
12 files changed, 131 insertions, 159 deletions
diff --git a/Private/HALKit/AMD64/HalCoreInterruptHandler.cpp b/Private/HALKit/AMD64/HalCoreInterruptHandler.cpp index 2acfc454..9f2e37d8 100644 --- a/Private/HALKit/AMD64/HalCoreInterruptHandler.cpp +++ b/Private/HALKit/AMD64/HalCoreInterruptHandler.cpp @@ -50,22 +50,24 @@ static const char* kExceptionMessages[32] = { #define kKernelSyscallInterrupt (0x21) EXTERN_C { + HCore::Void rt_handle_interrupts( + HCore::HAL::StackFramePtr stack) { + switch (stack->IntNum) { + case kKernelSyscallInterrupt: { + HCore::kcout << "HCoreKrnl: System call raised, checking.." + << HCore::end_line(); + rt_syscall_handle(nullptr); + break; + } -HCore::UIntPtr rt_handle_interrupts(HCore::HAL::StackFramePtr sf) { - MUST_PASS(sf); + default: + break; + } - if (sf->IntNum < 32) { - } else if (sf->IntNum == 0x21) { - rt_syscall_handle(sf); - } + if ((stack->IntNum - 32) >= 12) { + HCore::HAL::Out8(0xA0, 0x20); + } - if ((sf->IntNum - 32) >= 12) { - HCore::HAL::Out8(0xA0, 0x20); + HCore::HAL::Out8(0x20, 0x20); } - - HCore::HAL::Out8(0x20, 0x20); - - return (HCore::UIntPtr)sf; -} - }
\ No newline at end of file diff --git a/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp b/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp index d1483ef3..486c1513 100644 --- a/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp +++ b/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp @@ -11,17 +11,7 @@ #include <KernelKit/ProcessManager.hpp> #include <NewKit/String.hpp> -#include "KernelKit/DebugOutput.hpp" - -extern "C" void idt_handle_system_call(HCore::UIntPtr rsp) { - HCore::HAL::StackFrame *sf = reinterpret_cast<HCore::HAL::StackFrame *>(rsp); - rt_syscall_handle(sf); - - HCore::kcout << "System Call with ID: " - << HCore::StringBuilder::FromInt("syscall{%}", sf->R15); -} - -extern "C" void idt_handle_gpf(HCore::UIntPtr rsp) { +EXTERN_C void idt_handle_gpf(HCore::UIntPtr rsp) { MUST_PASS(HCore::ProcessManager::Shared().Leak().GetCurrent()); HCore::kcout << HCore::StringBuilder::FromInt("rsp{%}", rsp); diff --git a/Private/HALKit/AMD64/HalCoreSyscallHandlerAMD64.cpp b/Private/HALKit/AMD64/HalCoreSyscallHandlerAMD64.cpp index 3a3b49b3..24eee910 100644 --- a/Private/HALKit/AMD64/HalCoreSyscallHandlerAMD64.cpp +++ b/Private/HALKit/AMD64/HalCoreSyscallHandlerAMD64.cpp @@ -16,8 +16,10 @@ typedef HCore::Void (*rt_syscall_proc)(HCore::Int32 id, HCore::HAL::StackFramePt HCore::Array<rt_syscall_proc, kKernelMaxSystemCalls> kSyscalls; /// @brief Interrupt system call handler. -extern "C" void rt_syscall_handle(HCore::HAL::StackFramePtr stack) { - if (stack->R15 < kKernelMaxSystemCalls && kSyscalls[stack->R15] != 0) { - (kSyscalls[stack->R15].Leak().Leak())(stack->R15, stack); +EXTERN_C void rt_syscall_handle(HCore::HAL::StackFramePtr stack) { + if (!stack) return; + + if (stack->Rcx < kKernelMaxSystemCalls && kSyscalls[stack->Rcx] != 0) { + (kSyscalls[stack->Rcx].Leak().Leak())(stack->Rcx, stack); } } diff --git a/Private/HALKit/AMD64/HalDescriptorLoader.cpp b/Private/HALKit/AMD64/HalDescriptorLoader.cpp index 53218c17..3c84f151 100644 --- a/Private/HALKit/AMD64/HalDescriptorLoader.cpp +++ b/Private/HALKit/AMD64/HalDescriptorLoader.cpp @@ -10,9 +10,9 @@ #include <ArchKit/ArchKit.hpp> namespace HCore::HAL { -STATIC Register64 kRegGdt; +STATIC RegisterGDT kRegGdt; -void GDTLoader::Load(Register64 &gdt) { +void GDTLoader::Load(RegisterGDT &gdt) { kRegGdt.Base = gdt.Base; kRegGdt.Limit = gdt.Limit; @@ -21,6 +21,9 @@ void GDTLoader::Load(Register64 &gdt) { STATIC HAL::Register64 kRegIdt; +STATIC ::HCore::Detail::AMD64::InterruptDescriptorAMD64 + kInterruptVectorTable[kKernelIdtSize]; + void IDTLoader::Load(Register64 &idt) { UInt8 a1, a2; @@ -48,32 +51,30 @@ void IDTLoader::Load(Register64 &idt) { HAL::rt_wait_400ns(); HAL::Out8(0xA1, a2); - volatile ::HCore::UIntPtr *baseIdt = (::HCore::UIntPtr *)idt.Base; + volatile ::HCore::UIntPtr **baseIdt = (volatile ::HCore::UIntPtr **)idt.Base; MUST_PASS(baseIdt[0]); - ::HCore::Detail::AMD64::InterruptDescriptorAMD64 *kInterruptVectorTable = - new ::HCore::Detail::AMD64::InterruptDescriptorAMD64[kKernelIdtSize]; - - for (auto i = 0; i < kKernelIdtSize; i++) { + for (UInt16 i = 0; i < 32; i++) { kInterruptVectorTable[i].Selector = kGdtCodeSelector; kInterruptVectorTable[i].Ist = 0x0; kInterruptVectorTable[i].TypeAttributes = kInterruptGate; - kInterruptVectorTable[i].OffsetLow = (baseIdt[i] & 0xFF); - kInterruptVectorTable[i].OffsetMid = ((baseIdt[i] & 0xFFFF) >> 16); - kInterruptVectorTable[i].OffsetHigh = ((baseIdt[i] & 0xFFFFFFFF) >> 32); + kInterruptVectorTable[i].OffsetLow = ((UIntPtr)baseIdt[i] & 0xFFFF); + kInterruptVectorTable[i].OffsetMid = (((UIntPtr)baseIdt[i] >> 16) & 0xFFFF); + kInterruptVectorTable[i].OffsetHigh = + (((UIntPtr)baseIdt[i] >> 32) & 0xFFFFFFFF); kInterruptVectorTable[i].Zero = 0x0; } - kRegIdt.Base = (UIntPtr)&kInterruptVectorTable[0]; + kRegIdt.Base = (UIntPtr)kInterruptVectorTable; kRegIdt.Limit = sizeof(::HCore::Detail::AMD64::InterruptDescriptorAMD64) * - kKernelIdtSize - - 1; + kKernelIdtSize - + 1; rt_load_idt(kRegIdt); } -void GDTLoader::Load(Ref<Register64> &gdt) { GDTLoader::Load(gdt.Leak()); } +void GDTLoader::Load(Ref<RegisterGDT> &gdt) { GDTLoader::Load(gdt.Leak()); } void IDTLoader::Load(Ref<Register64> &idt) { IDTLoader::Load(idt.Leak()); } } // namespace HCore::HAL diff --git a/Private/HALKit/AMD64/HalHardwareAPIC.cpp b/Private/HALKit/AMD64/HalHardwareAPIC.cpp index cae54658..d587975e 100644 --- a/Private/HALKit/AMD64/HalHardwareAPIC.cpp +++ b/Private/HALKit/AMD64/HalHardwareAPIC.cpp @@ -17,8 +17,7 @@ namespace HCore { void rt_wakeup_thread(HAL::StackFrame* stack) { HAL::rt_cli(); - stack->Rbp = stack->R15; - stack->Rsi = stack->Rbp; + // TODO HAL::rt_sti(); } @@ -33,9 +32,7 @@ static void __rt_hang_proc(void) { void rt_hang_thread(HAL::StackFrame* stack) { __asm__ volatile("cli"); - stack->R15 = stack->Rbp; - stack->Rbp = (HAL::Reg)&__rt_hang_proc; - stack->Rsp = stack->Rbp; + // TODO __asm__ volatile("sti"); } diff --git a/Private/HALKit/AMD64/HalInterruptRouting.asm b/Private/HALKit/AMD64/HalInterruptRouting.asm index 1ed802b4..8d10ed4f 100644 --- a/Private/HALKit/AMD64/HalInterruptRouting.asm +++ b/Private/HALKit/AMD64/HalInterruptRouting.asm @@ -11,22 +11,62 @@ [bits 64] -%macro IntDecl 1 - dq __HCR_INT_%1 -%endmacro - %macro IntExp 1 __HCR_INT_%1: - push 0 + cli + + push 1 push %1 - call ke_handle_irq + + push rax + push rcx + push rdx + push rbx + push rbp + push rsi + push rdi + + jmp rt_handle_interrupts + + pop rdi + pop rsi + pop rbp + pop rbx + pop rdx + pop rcx + pop rax + + add rsp, 16 + sti iretq %endmacro %macro IntNormal 1 __HCR_INT_%1: + cli + + push 0 push %1 - call ke_handle_irq + + push rax + push rcx + push rdx + push rbx + push rbp + push rsi + push rdi + + jmp rt_handle_interrupts + + pop rdi + pop rsi + pop rbp + pop rbx + pop rdx + pop rcx + pop rax + + sti iretq %endmacro @@ -40,47 +80,6 @@ global kInterruptVectorTable section .text -ke_handle_irq: - push rax - push rbx - push rcx - push rdx - push rsi - push rdi - push rbp - push r8 - push r9 - push r10 - push r11 - push r12 - push r13 - push r14 - push r15 - - mov rcx, rsp - call rt_handle_interrupts - mov rsp, rcx - - pop r15 - pop r14 - pop r13 - pop r12 - pop r11 - pop r10 - pop r9 - pop r8 - pop rbp - pop rdi - pop rsi - pop rdx - pop rcx - pop rbx - pop rax - - ret - -section .data - IntNormal 0 IntNormal 1 IntNormal 2 @@ -114,24 +113,11 @@ IntNormal 29 IntExp 30 IntNormal 31 -%assign i 32 -%rep 255 - IntNormal i -%assign i i+1 -%endrep - -section .text - ;; this one is doing a POST for us. ;; testing interrupts. _ke_power_on_self_test: - int 0x21 - int 0x21 - int 0x21 - int 0x21 - int 0x21 - int 0x21 - int 0x21 + mov rcx, 0x80 + mov rdx, 0x01 int 0x21 ret @@ -155,17 +141,14 @@ rt_reload_segments: global rt_load_idt -section .text - rt_load_idt: lidt [rcx] + sti ret -section .data - kInterruptVectorTable: %assign i 0 - %rep 255 - IntDecl i + %rep 32 + dq __HCR_INT_%+i %assign i i+1 %endrep diff --git a/Private/HALKit/AMD64/HalKernelMain.cxx b/Private/HALKit/AMD64/HalKernelMain.cxx index d799e5ae..eaddb338 100644 --- a/Private/HALKit/AMD64/HalKernelMain.cxx +++ b/Private/HALKit/AMD64/HalKernelMain.cxx @@ -23,7 +23,7 @@ ///! @brief Disk contains HCore files. #define kInstalledMedia 0xDD -EXTERN_C VoidStar kInterruptVectorTable[]; +EXTERN_C HCore::VoidPtr kInterruptVectorTable[]; namespace Detail { using namespace HCore; @@ -69,10 +69,10 @@ EXTERN_C void RuntimeMain( {0, 0, 0, 0x92, 0xaf, 0}, // kernel data {0, 0, 0, 0x00, 0x00, 0}, // null entry {0, 0, 0, 0x9a, 0xaf, 0}, // user code - {0, 0, 0, 0x92, 0xaf, 0}, // user data + {0, 0, 0, 0x92, 0xaf, 0}, // user data }; - HCore::HAL::Register64 gdtBase; + HCore::HAL::RegisterGDT gdtBase; gdtBase.Base = (HCore::UIntPtr)&GDT; gdtBase.Limit = sizeof(Detail::HC_GDT) - 1; @@ -91,6 +91,10 @@ EXTERN_C void RuntimeMain( HCore::HAL::IDTLoader idt; idt.Load(idtBase); + Detail::_ke_power_on_self_test(); + + HCore::kcout << "HCoreKrnl: System Call issued, everything is OK...\r\n"; + if (HandoverHeader->f_Bootloader == kInstalledMedia) { /// Mounts a NewFS block. HCore::IFilesystemManager::Mount(new HCore::NewFilesystemManager()); @@ -98,21 +102,17 @@ EXTERN_C void RuntimeMain( // Open file from first hard-drive. HCore::PEFLoader img("A:/System/HCoreServer.exe"); - /// Run the shell. - if (!HCore::Utils::execute_from_image(img)) { - HCore::ke_stop(RUNTIME_CHECK_BOOTSTRAP); - } + /// Run the server executive. + HCore::Utils::execute_from_image(img); } else { /** ** This does the POST. */ - Detail::_ke_power_on_self_test(); - /** This mounts the NewFS drive. */ - - HCore::ke_stop(RUNTIME_CHECK_BOOTSTRAP); } + + HCore::ke_stop(RUNTIME_CHECK_BOOTSTRAP); } diff --git a/Private/HALKit/AMD64/HalPageAlloc.cpp b/Private/HALKit/AMD64/HalPageAlloc.cpp index 84ddc665..30996d19 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.cpp +++ b/Private/HALKit/AMD64/HalPageAlloc.cpp @@ -25,12 +25,14 @@ static auto hal_try_alloc_new_page(SizeT sz, Boolean rw, Boolean user) MUST_PASS(sz != 0); PageTable64 *pte = - reinterpret_cast<PageTable64 *>((UIntPtr)kKernelVirtualStart); + &reinterpret_cast<PageDirectory64 *>((UIntPtr)kKernelVirtualStart)->Pte[0]; pte->Rw = rw; pte->User = user; pte->Present = true; + write_cr3((UIntPtr)kKernelVirtualStart); + kKernelVirtualStart = (VoidPtr)((UIntPtr)kKernelVirtualStart + kPageCnt + sz + kKernelPagingPadding); return pte; @@ -38,16 +40,20 @@ static auto hal_try_alloc_new_page(SizeT sz, Boolean rw, Boolean user) auto hal_alloc_page(SizeT sz, Boolean rw, Boolean user) -> PageTable64 * { for (SizeT i = 0; i < kPageCnt; ++i) { - PageTable64 *pte = reinterpret_cast<PageTable64 *>( + PageDirectory64 *pte = reinterpret_cast<PageDirectory64 *>( (UIntPtr)kKernelVirtualStart + kPageCnt); - if (!pte->Present) { - pte->User = user; - pte->Rw = rw; - pte->Present = true; + for (size_t indexPte = 0; indexPte < kPTEMax; ++indexPte) + { + if (!pte->Pte[indexPte].Present) { + pte->Pte[indexPte].User = user; + pte->Pte[indexPte].Rw = rw; + pte->Pte[indexPte].Present = true; - return pte; + return &(pte->Pte[indexPte]); + } } + } return hal_try_alloc_new_page(sz, rw, user); diff --git a/Private/HALKit/AMD64/Processor.hpp b/Private/HALKit/AMD64/Processor.hpp index 035b095f..435847a8 100644 --- a/Private/HALKit/AMD64/Processor.hpp +++ b/Private/HALKit/AMD64/Processor.hpp @@ -61,12 +61,17 @@ EXTERN_C void rt_cli(); EXTERN_C void rt_sti(); EXTERN_C void rt_cld(); -class PACKED Register64 { - public: +struct PACKED Register64 final { + UShort Limit; + UIntPtr Base; +}; + +struct PACKED RegisterGDT final { UShort Limit; UIntPtr Base; }; + using RawRegister = UInt64; using InterruptId = UShort; /* For each element in the IVT */ @@ -75,23 +80,8 @@ using interruptTrap = UIntPtr(UIntPtr sp); typedef UIntPtr Reg; struct PACKED StackFrame { - Reg IntNum; - Reg Rax; - Reg Rbx; - Reg Rcx; - Reg Rdx; - Reg Rsi; - Reg Rdi; - Reg Rbp; - Reg Rsp; - Reg R8; - Reg R9; - Reg R10; - Reg R11; - Reg R12; - Reg R13; - Reg R14; - Reg R15; + Reg IntNum, ErrCode; + Reg Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax; }; typedef StackFrame *StackFramePtr; @@ -138,8 +128,8 @@ using SegmentArray = Array<SegmentDescriptor, 6>; class GDTLoader final { public: - static void Load(Register64 &gdt); - static void Load(Ref<Register64> &gdt); + static void Load(RegisterGDT &gdt); + static void Load(Ref<RegisterGDT> &gdt); }; class IDTLoader final { @@ -158,7 +148,7 @@ EXTERN_C void idt_handle_math(HCore::UIntPtr rsp); EXTERN_C void idt_handle_pf(HCore::UIntPtr rsp); EXTERN_C void rt_load_idt(HCore::HAL::Register64 ptr); -EXTERN_C void rt_load_gdt(HCore::HAL::Register64 ptr); +EXTERN_C void rt_load_gdt(HCore::HAL::RegisterGDT ptr); /// @brief Maximum size of the IDT. #define kKernelIdtSize 256 diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 933dd1dd..6931362c 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -10,7 +10,7 @@ ASM=nasm OBJ=$(wildcard *.o) $(wildcard ../../Obj/*.obj) $(wildcard HEL/AMD64/*.obj) FLAG_ASM=-f win64 -FLAG_GNU=-fshort-wchar -fPIC -O0 -D__DEBUG__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ +FLAG_GNU=-fshort-wchar -fPIC -D__DEBUG__ -maccumulate-outgoing-args -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ .PHONY: invalid-recipe invalid-recipe: @@ -27,7 +27,7 @@ bootloader-amd64: .PHONY: run-efi-amd64 run-efi-amd64: - qemu-system-x86_64 -net none -smp 2 -m 4G -M q35 -bios OVMF.fd -drive file=fat:rw:CDROM,index=1,format=raw -d int + qemu-system-x86_64 -net none -smp 2 -m 4G -M q35 -bios OVMF.fd -drive file=fat:rw:CDROM,index=1,format=raw -serial stdio .PHONY: download-edk download-edk: diff --git a/Private/Source/String.cxx b/Private/Source/String.cxx index 0e1b7f41..b51a560b 100644 --- a/Private/Source/String.cxx +++ b/Private/Source/String.cxx @@ -9,6 +9,7 @@ #include <NewKit/String.hpp> #include <NewKit/Utils.hpp> +#include <KernelKit/DebugOutput.hpp> namespace HCore { Char *StringView::Data() { return m_Data; } diff --git a/Private/makefile b/Private/makefile index 37245e2c..2eda7358 100644 --- a/Private/makefile +++ b/Private/makefile @@ -5,7 +5,7 @@ CC = x86_64-w64-mingw32-g++ LD = x86_64-w64-mingw32-ld -CCFLAGS = -c -ffreestanding -O0 -fPIC -D__DEBUG__ -fno-rtti -fno-exceptions -std=c++20 -D__FSKIT_NEWFS__ -D__HAVE_HCORE_APIS__ -D__HCORE__ -I../ -I./ -I$(HOME)/ +CCFLAGS = -c -ffreestanding -fPIC -D__DEBUG__ -maccumulate-outgoing-args -fno-rtti -fno-exceptions -std=c++20 -D__FSKIT_NEWFS__ -D__HAVE_HCORE_APIS__ -D__HCORE__ -I../ -I./ -I$(HOME)/ ASM = nasm ASMFLAGS = -f win64 LDFLAGS = -e Main --subsystem=17 |
