diff options
Diffstat (limited to 'Private')
90 files changed, 2365 insertions, 2912 deletions
diff --git a/Private/HALKit/AMD64/ACPI/ACPI.hpp b/Private/HALKit/AMD64/ACPI/ACPI.hpp index 590358ff..aa408c6f 100644 --- a/Private/HALKit/AMD64/ACPI/ACPI.hpp +++ b/Private/HALKit/AMD64/ACPI/ACPI.hpp @@ -13,60 +13,53 @@ #include <NewKit/Defines.hpp> namespace hCore { - class SDT { - public: - Char Signature[4]; - UInt32 Length; - UInt8 Revision; - Char Checksum; - Char OemId[6]; - Char OemTableId[8]; - UInt32 OemRev; - UInt32 CreatorID; - UInt32 CreatorRevision; - }; +class SDT { + public: + Char Signature[4]; + UInt32 Length; + UInt8 Revision; + Char Checksum; + Char OemId[6]; + Char OemTableId[8]; + UInt32 OemRev; + UInt32 CreatorID; + UInt32 CreatorRevision; +}; - class RSDP : public SDT - { - public: - UInt32 RsdtAddress; - UIntPtr XsdtAddress; - UInt8 ExtendedChecksum; - UInt8 Reserved0[3]; +class RSDP : public SDT { + public: + UInt32 RsdtAddress; + UIntPtr XsdtAddress; + UInt8 ExtendedChecksum; + UInt8 Reserved0[3]; +}; - }; +class ConfigHeader { + public: + UInt64 BaseAddress; + UInt16 PciSegGroup; + UInt8 StartBus; + UInt8 EndBus; + UInt32 Reserved; +}; - class ConfigHeader - { - public: - UInt64 BaseAddress; - UInt16 PciSegGroup; - UInt8 StartBus; - UInt8 EndBus; - UInt32 Reserved; +enum class AddressSpace : UInt8 { + SystemMemory = 0, + SystemIO = 1, + Pci = 2, + Controller = 3, + SmBus = 4, + Invalid = 0xFF, +}; - }; +class Address { + public: + AddressSpace AddressSpaceId; + UInt8 RegisterBitWidth; + UInt8 RegisterBitOffset; + UInt8 Reserved; + UIntPtr Address; +}; +} // namespace hCore - enum class AddressSpace : UInt8 - { - SystemMemory = 0, - SystemIO = 1, - Pci = 2, - Controller = 3, - SmBus = 4, - Invalid = 0xFF, - }; - - class Address - { - public: - AddressSpace AddressSpaceId; - UInt8 RegisterBitWidth; - UInt8 RegisterBitOffset; - UInt8 Reserved; - UIntPtr Address; - - }; -} // namespace hCore - -#endif // !_INC_ACPI_MANAGER_H +#endif // !_INC_ACPI_MANAGER_H diff --git a/Private/HALKit/AMD64/ACPI/ACPIManager.hpp b/Private/HALKit/AMD64/ACPI/ACPIManager.hpp index 63afe972..385ea854 100644 --- a/Private/HALKit/AMD64/ACPI/ACPIManager.hpp +++ b/Private/HALKit/AMD64/ACPI/ACPIManager.hpp @@ -15,38 +15,36 @@ #include <NewKit/Defines.hpp> #include <NewKit/Ref.hpp> -namespace hCore -{ - class ACPIManager - { - public: - ACPIManager(voidPtr rsdptr); +namespace hCore { +class ACPIManager { + public: + ACPIManager(voidPtr rsdptr); - public: - ~ACPIManager() = default; + public: + ~ACPIManager() = default; - ACPIManager &operator=(const ACPIManager &) = default; + ACPIManager &operator=(const ACPIManager &) = default; - ACPIManager(const ACPIManager &) = default; + ACPIManager(const ACPIManager &) = default; - public: - void Shutdown(); // shutdown - void Reset(); // soft-reboot + public: + void Shutdown(); // shutdown + void Reset(); // soft-reboot - ErrorOr <voidPtr> Find(const char *signature); + ErrorOr<voidPtr> Find(const char *signature); - bool Checksum(const char *checksum, SSizeT len); // watch for collides! + bool Checksum(const char *checksum, SSizeT len); // watch for collides! - public: - ErrorOr <voidPtr> operator[](const char *signature) { - return this->Find(signature); - } + public: + ErrorOr<voidPtr> operator[](const char *signature) { + return this->Find(signature); + } - private: - VoidPtr m_Rsdp; // pointer to root descriptor. - SSizeT m_Entries; // number of entries, -1 tells that no invalid entries were found. + private: + VoidPtr m_Rsdp; // pointer to root descriptor. + SSizeT m_Entries; // number of entries, -1 tells that no invalid entries were + // found. +}; +} // namespace hCore - }; -} // namespace hCore - -#endif // !_INC_ACPI_H +#endif // !_INC_ACPI_H diff --git a/Private/HALKit/AMD64/ACPIManagerAMD64.cpp b/Private/HALKit/AMD64/ACPIManagerAMD64.cpp index 0f5e6f68..0ca172a9 100644 --- a/Private/HALKit/AMD64/ACPIManagerAMD64.cpp +++ b/Private/HALKit/AMD64/ACPIManagerAMD64.cpp @@ -8,85 +8,62 @@ */ #include <HALKit/AMD64/ACPI/ACPIManager.hpp> -#include <NewKit/String.hpp> - #include <HALKit/AMD64/Processor.hpp> +#include <NewKit/String.hpp> -namespace hCore -{ -ACPIManager::ACPIManager(voidPtr rsdPtr) : m_Rsdp(rsdPtr), m_Entries(0) -{ - RSDP *_rsdPtr = reinterpret_cast<RSDP *>(this->m_Rsdp); +namespace hCore { +ACPIManager::ACPIManager(voidPtr rsdPtr) : m_Rsdp(rsdPtr), m_Entries(0) { + RSDP *_rsdPtr = reinterpret_cast<RSDP *>(this->m_Rsdp); - MUST_PASS(_rsdPtr); - MUST_PASS(_rsdPtr->Revision >= 2); + MUST_PASS(_rsdPtr); + MUST_PASS(_rsdPtr->Revision >= 2); } -void ACPIManager::Shutdown() -{ -} -void ACPIManager::Reset() -{ -} +void ACPIManager::Shutdown() {} +void ACPIManager::Reset() {} -ErrorOr<voidPtr> ACPIManager::Find(const char *signature) -{ - MUST_PASS(m_Rsdp); +ErrorOr<voidPtr> ACPIManager::Find(const char *signature) { + MUST_PASS(m_Rsdp); - if (!signature) - return ErrorOr<voidPtr>{-2}; + if (!signature) return ErrorOr<voidPtr>{-2}; - if (*signature == 0) - return ErrorOr<voidPtr>{-3}; + if (*signature == 0) return ErrorOr<voidPtr>{-3}; - RSDP *rsdPtr = reinterpret_cast<RSDP *>(this->m_Rsdp); + RSDP *rsdPtr = reinterpret_cast<RSDP *>(this->m_Rsdp); - auto xsdt = rsdPtr->XsdtAddress; - SizeT num = (rsdPtr->Length + sizeof(SDT)) / 8; + auto xsdt = rsdPtr->XsdtAddress; + SizeT num = (rsdPtr->Length + sizeof(SDT)) / 8; - for (Size index = 0; index < num; ++index) - { - SDT *sdt = reinterpret_cast<SDT *>(xsdt + sizeof(SDT) + index * 8); + for (Size index = 0; index < num; ++index) { + SDT *sdt = reinterpret_cast<SDT *>(xsdt + sizeof(SDT) + index * 8); - if (!Checksum(sdt->Signature, 4)) - panic(RUNTIME_CHECK_ACPI); + if (!Checksum(sdt->Signature, 4)) panic(RUNTIME_CHECK_ACPI); - if (StringBuilder::Equals(const_cast<const char *>(sdt->Signature), signature)) - return ErrorOr<voidPtr>(reinterpret_cast<voidPtr>(sdt)); - } + if (StringBuilder::Equals(const_cast<const char *>(sdt->Signature), + signature)) + return ErrorOr<voidPtr>(reinterpret_cast<voidPtr>(sdt)); + } - return ErrorOr<voidPtr>{-1}; + return ErrorOr<voidPtr>{-1}; } -bool ACPIManager::Checksum(const char *checksum, SSizeT len) -{ - if (len == 0) - return -1; +bool ACPIManager::Checksum(const char *checksum, SSizeT len) { + if (len == 0) return -1; - char chr = 0; + char chr = 0; - for (int index = 0; index < len; ++index) - { - chr += checksum[index]; - } + for (int index = 0; index < len; ++index) { + chr += checksum[index]; + } - return chr == 0; + return chr == 0; } -void rt_shutdown_acpi_qemu_20(void) -{ - HAL::out16(0xb004, 0x2000); -} +void rt_shutdown_acpi_qemu_20(void) { HAL::out16(0xb004, 0x2000); } -void rt_shutdown_acpi_qemu_30_plus(void) -{ - HAL::out16(0x604, 0x2000); -} +void rt_shutdown_acpi_qemu_30_plus(void) { HAL::out16(0x604, 0x2000); } -void rt_shutdown_acpi_virtualbox(void) -{ - HAL::out16(0x4004, 0x3400); -} +void rt_shutdown_acpi_virtualbox(void) { HAL::out16(0x4004, 0x3400); } /// you'll have to parse the MADT otherwise! -} // namespace hCore +} // namespace hCore diff --git a/Private/HALKit/AMD64/CoreInterruptHandlerAMD64.cpp b/Private/HALKit/AMD64/CoreInterruptHandlerAMD64.cpp index fd037c5f..23262109 100644 --- a/Private/HALKit/AMD64/CoreInterruptHandlerAMD64.cpp +++ b/Private/HALKit/AMD64/CoreInterruptHandlerAMD64.cpp @@ -8,75 +8,74 @@ */ #include <ArchKit/Arch.hpp> -#include <NewKit/String.hpp> #include <KernelKit/ProcessManager.hpp> +#include <NewKit/String.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); +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->SID); + hCore::kcout << "System Call with ID: " + << hCore::StringBuilder::FromInt("syscall{%}", sf->SID); } -extern "C" void idt_handle_gpf(hCore::UIntPtr rsp) -{ - MUST_PASS(hCore::ProcessManager::Shared().Leak().GetCurrent()); +extern "C" void idt_handle_gpf(hCore::UIntPtr rsp) { + MUST_PASS(hCore::ProcessManager::Shared().Leak().GetCurrent()); - hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); + hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); - hCore::kcout << "General Protection Fault, Caused by " - << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); + hCore::kcout + << "General Protection Fault, Caused by " + << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); - hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } -extern "C" void idt_handle_scheduler(hCore::UIntPtr rsp) -{ - hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); +extern "C" void idt_handle_scheduler(hCore::UIntPtr rsp) { + hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); - hCore::kcout << "Will be scheduled back later " - << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); + hCore::kcout + << "Will be scheduled back later " + << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); - /// schedule another process. - if (!hCore::ProcessHelper::StartScheduling()) - { - hCore::kcout << "Let's continue schedule this process...\r\n"; - } + /// schedule another process. + if (!hCore::ProcessHelper::StartScheduling()) { + hCore::kcout << "Let's continue schedule this process...\r\n"; + } } -extern "C" void idt_handle_pf(hCore::UIntPtr rsp) -{ - hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); +extern "C" void idt_handle_pf(hCore::UIntPtr rsp) { + hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); - MUST_PASS(hCore::ProcessManager::Shared().Leak().GetCurrent()); + MUST_PASS(hCore::ProcessManager::Shared().Leak().GetCurrent()); - hCore::kcout << "Segmentation Fault, Caused by " - << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); + hCore::kcout + << "Segmentation Fault, Caused by " + << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); - hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } -extern "C" void idt_handle_math(hCore::UIntPtr rsp) -{ - hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); +extern "C" void idt_handle_math(hCore::UIntPtr rsp) { + hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); - MUST_PASS(hCore::ProcessManager::Shared().Leak().GetCurrent()); + MUST_PASS(hCore::ProcessManager::Shared().Leak().GetCurrent()); - hCore::kcout << "Math error, Caused by " - << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); + hCore::kcout + << "Math error, Caused by " + << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); - hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } -extern "C" void idt_handle_generic(hCore::UIntPtr rsp) -{ - hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); +extern "C" void idt_handle_generic(hCore::UIntPtr rsp) { + hCore::kcout << hCore::StringBuilder::FromInt("sp{%}", rsp); - MUST_PASS(hCore::ProcessManager::Shared().Leak().GetCurrent()); + MUST_PASS(hCore::ProcessManager::Shared().Leak().GetCurrent()); - hCore::kcout << "Processor error, Caused by " - << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); + hCore::kcout + << "Processor error, Caused by " + << hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); - hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + hCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } diff --git a/Private/HALKit/AMD64/CoreMultiProcessingAMD64.cpp b/Private/HALKit/AMD64/CoreMultiProcessingAMD64.cpp index 14a7a06b..330a90e6 100644 --- a/Private/HALKit/AMD64/CoreMultiProcessingAMD64.cpp +++ b/Private/HALKit/AMD64/CoreMultiProcessingAMD64.cpp @@ -7,128 +7,121 @@ * ======================================================== */ -#include <HALKit/AMD64/Processor.hpp> #include <HALKit/AMD64/ACPI/ACPIManager.hpp> +#include <HALKit/AMD64/Processor.hpp> /////////////////////////////////////////////////////////////////////////////////////// //! NOTE: fGSI stands 'Field Global System Interrupt' -namespace hCore::HAL -{ - constexpr Int32 kThreadAPIC = 0; - constexpr Int32 kThreadLAPIC = 1; - constexpr Int32 kThreadIOAPIC = 2; - constexpr Int32 kThreadAPIC64 = 3; - constexpr Int32 kThreadBoot = 4; - - /* - * - * this is used to store info about the current running thread - * we use this struct to determine if we can use it, or mark it as used or on sleep. - * - */ - - struct ProcessorInfoAMD64 final - { - Int32 ThreadType; - UIntPtr JumpAddress; - - struct - { - UInt32 Code; - UInt32 Data; - UInt32 BSS; - } Selector; - }; - - static voidPtr kApicMadt = nullptr; - static const char* kApicSignature = "APIC"; - - struct Madt final - { - char fMag[4]; - Int32 fLength; - char fRev; - - struct MadtAddress final - { - UInt32 fPhysicalAddress; - UInt32 fFlags; // 1 = Dual Legacy PICs installed - - Char fType; - Char fRecLen; // record length - }; - }; - - struct MadtProcessorLocalApic final - { - Char fProcessorId; - Char fApicId; - UInt32 fFlags; - }; - - struct MadtIOApic final - { - Char fApicId; - Char fReserved; - UInt32 fAddress; - UInt32 fSystemInterruptBase; - }; - - struct MadtInterruptSource final - { - Char fBusSource; - Char fIrqSource; - UInt32 fGSI; - UInt16 fFlags; - }; - - struct MadtInterruptNmi final - { - Char fNmiSource; - Char fReserved; - UInt16 fFlags; - UInt32 fGSI; - }; - - struct MadtLocalApicAddressOverride final { UInt16 fResvered; UIntPtr fAddress; }; +namespace hCore::HAL { +constexpr Int32 kThreadAPIC = 0; +constexpr Int32 kThreadLAPIC = 1; +constexpr Int32 kThreadIOAPIC = 2; +constexpr Int32 kThreadAPIC64 = 3; +constexpr Int32 kThreadBoot = 4; + +/* + * + * this is used to store info about the current running thread + * we use this struct to determine if we can use it, or mark it as used or on + * sleep. + * + */ + +struct ProcessorInfoAMD64 final { + Int32 ThreadType; + UIntPtr JumpAddress; + + struct { + UInt32 Code; + UInt32 Data; + UInt32 BSS; + } Selector; +}; + +static voidPtr kApicMadt = nullptr; +static const char* kApicSignature = "APIC"; + +struct Madt final { + char fMag[4]; + Int32 fLength; + char fRev; + + struct MadtAddress final { + UInt32 fPhysicalAddress; + UInt32 fFlags; // 1 = Dual Legacy PICs installed + + Char fType; + Char fRecLen; // record length + }; +}; + +struct MadtProcessorLocalApic final { + Char fProcessorId; + Char fApicId; + UInt32 fFlags; +}; + +struct MadtIOApic final { + Char fApicId; + Char fReserved; + UInt32 fAddress; + UInt32 fSystemInterruptBase; +}; + +struct MadtInterruptSource final { + Char fBusSource; + Char fIrqSource; + UInt32 fGSI; + UInt16 fFlags; +}; + +struct MadtInterruptNmi final { + Char fNmiSource; + Char fReserved; + UInt16 fFlags; + UInt32 fGSI; +}; + +struct MadtLocalApicAddressOverride final { + UInt16 fResvered; + UIntPtr fAddress; +}; /////////////////////////////////////////////////////////////////////////////////////// - static Madt kApicMadtList[256]; +static Madt kApicMadtList[256]; - Madt* system_find_core(Madt* madt) - { - madt = madt + sizeof(Madt); +Madt* system_find_core(Madt* madt) { + madt = madt + sizeof(Madt); - if (string_compare(madt->fMag, kApicSignature, string_length(kApicSignature)) == 0) - return madt; + if (string_compare(madt->fMag, kApicSignature, + string_length(kApicSignature)) == 0) + return madt; - return nullptr; - } + return nullptr; +} /////////////////////////////////////////////////////////////////////////////////////// - void system_get_cores(voidPtr rsdPtr) - { - auto acpi = ACPIManager(rsdPtr); - kApicMadt = acpi.Find(kApicSignature).Leak().Leak(); - - MUST_PASS(kApicMadt); // MADT must exist. - - SizeT counter = 0UL; - Madt* offset = system_find_core((Madt*)kApicMadt); - //! now find core addresses. - while (offset != nullptr) - { - // calls rt_copy_memory in NewC++ - kApicMadtList[counter] = *offset; - offset = system_find_core(offset); - - ++counter; - } - } +void system_get_cores(voidPtr rsdPtr) { + auto acpi = ACPIManager(rsdPtr); + kApicMadt = acpi.Find(kApicSignature).Leak().Leak(); + + MUST_PASS(kApicMadt); // MADT must exist. + + SizeT counter = 0UL; + Madt* offset = system_find_core((Madt*)kApicMadt); + //! now find core addresses. + while (offset != nullptr) { + // calls rt_copy_memory in NewC++ + kApicMadtList[counter] = *offset; + offset = system_find_core(offset); + + ++counter; + } } +} // namespace hCore::HAL /////////////////////////////////////////////////////////////////////////////////////// diff --git a/Private/HALKit/AMD64/CoreSyscallHandlerAMD64.cpp b/Private/HALKit/AMD64/CoreSyscallHandlerAMD64.cpp index 3e8544e7..ccd81b9a 100644 --- a/Private/HALKit/AMD64/CoreSyscallHandlerAMD64.cpp +++ b/Private/HALKit/AMD64/CoreSyscallHandlerAMD64.cpp @@ -7,19 +7,17 @@ * ======================================================== */ -#include <KernelKit/PermissionSelector.hxx> -#include <HALKit/AMD64/Processor.hpp> - #include <ArchKit/Arch.hpp> +#include <HALKit/AMD64/Processor.hpp> +#include <KernelKit/PermissionSelector.hxx> -hCore::Array<void (*)(hCore::Int32 id, hCore::HAL::StackFrame *), kMaxSyscalls> kSyscalls; +hCore::Array<void (*)(hCore::Int32 id, hCore::HAL::StackFrame *), kMaxSyscalls> + kSyscalls; // IDT System Call Handler. // NOTE: don't trust the user. -extern "C" void rt_syscall_handle(hCore::HAL::StackFrame *stack) -{ - for (hCore::SizeT index = 0UL; index < kMaxSyscalls; ++index) - { - (kSyscalls[index].Leak().Leak())(stack->SID, stack); - } +extern "C" void rt_syscall_handle(hCore::HAL::StackFrame *stack) { + for (hCore::SizeT index = 0UL; index < kMaxSyscalls; ++index) { + (kSyscalls[index].Leak().Leak())(stack->SID, stack); + } } diff --git a/Private/HALKit/AMD64/DebugManager.asm b/Private/HALKit/AMD64/DebugManager.asm index 0c717039..96c7e2f3 100644 --- a/Private/HALKit/AMD64/DebugManager.asm +++ b/Private/HALKit/AMD64/DebugManager.asm @@ -30,4 +30,4 @@ rt_debug_fence: pop rsi ret -;; //////////////////////////////////////////////////// ;;
\ No newline at end of file +;; //////////////////////////////////////////////////// ;; diff --git a/Private/HALKit/AMD64/DebugOutput.cxx b/Private/HALKit/AMD64/DebugOutput.cxx index 8a8705cf..43ab2538 100644 --- a/Private/HALKit/AMD64/DebugOutput.cxx +++ b/Private/HALKit/AMD64/DebugOutput.cxx @@ -7,62 +7,54 @@ * ======================================================== */ -#include <KernelKit/DebugOutput.hpp> - #include <ArchKit/Arch.hpp> +#include <KernelKit/DebugOutput.hpp> #include <NewKit/Utils.hpp> -namespace hCore -{ - namespace Detail - { - constexpr short PORT = 0x3F8; - - bool serial_init() - { - HAL::out8(PORT + 1, 0x00); // Disable all interrupts - HAL::out8(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) - HAL::out8(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud - HAL::out8(PORT + 1, 0x00); // (hi byte) - HAL::out8(PORT + 3, 0x03); // 8 bits, no parity, one stop bit - HAL::out8(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold - HAL::out8(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set - HAL::out8(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip - HAL::out8(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if - // serial returns same byte) - - // Check if serial is faulty (i.e: not same byte as sent) - if (HAL::in8(PORT) != 0xAE) - { +namespace hCore { +namespace Detail { +constexpr short PORT = 0x3F8; + +bool serial_init() { + HAL::out8(PORT + 1, 0x00); // Disable all interrupts + HAL::out8(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) + HAL::out8(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud + HAL::out8(PORT + 1, 0x00); // (hi byte) + HAL::out8(PORT + 3, 0x03); // 8 bits, no parity, one stop bit + HAL::out8(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold + HAL::out8(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set + HAL::out8(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip + HAL::out8(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if + // serial returns same byte) + + // Check if serial is faulty (i.e: not same byte as sent) + if (HAL::in8(PORT) != 0xAE) { #ifdef __DEBUG__ - panic(RUNTIME_CHECK_HANDSHAKE); + panic(RUNTIME_CHECK_HANDSHAKE); #else - return false; + return false; #endif - } + } - // If serial is not faulty set it in normal operation mode - // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) - HAL::out8(Detail::PORT + 4, 0x0F); + // If serial is not faulty set it in normal operation mode + // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) + HAL::out8(Detail::PORT + 4, 0x0F); - return true; - } - } + return true; +} +} // namespace Detail - void system_io_print(const char *bytes) - { - if (!bytes) - return; +void system_io_print(const char *bytes) { + if (!bytes) return; - SizeT index = 0; - SizeT len = string_length(bytes, 256); + SizeT index = 0; + SizeT len = string_length(bytes, 256); - while (index < len) - { - HAL::out8(Detail::PORT, bytes[index]); - ++index; - } - } + while (index < len) { + HAL::out8(Detail::PORT, bytes[index]); + ++index; + } +} - TerminalDevice kcout(hCore::system_io_print, nullptr); -} // namespace hCore +TerminalDevice kcout(hCore::system_io_print, nullptr); +} // namespace hCore diff --git a/Private/HALKit/AMD64/DebugPort.cxx b/Private/HALKit/AMD64/DebugPort.cxx index 28e3f5e3..764aab0a 100644 --- a/Private/HALKit/AMD64/DebugPort.cxx +++ b/Private/HALKit/AMD64/DebugPort.cxx @@ -13,7 +13,7 @@ #include <ArchKit/Arch.hpp> #define kDebugMaxPorts 16 - + #define kDebugUnboundPort 0xFFFF #define kDebugMag0 'X' @@ -22,45 +22,38 @@ #define kDebugMag3 'G' #define kDebugSourceFile 0 -#define kDebugLine 33 -#define kDebugTeam 43 -#define kDebugEOP 49 +#define kDebugLine 33 +#define kDebugTeam 43 +#define kDebugEOP 49 // after that we have start of additional data. -namespace hCore -{ - typedef Char rt_debug_type[255]; - - class DebuggerPorts final - { - public: - Int16 fPort[kDebugMaxPorts]; - Int16 fBoundCnt; - - }; - - void rt_debug_listen(DebuggerPorts* theHook) noexcept - { - if (theHook == nullptr) - return; - - for (UInt32 i = 0U; i < kDebugMaxPorts; ++i) - { - HAL::out16(theHook->fPort[i], kDebugMag0); - HAL::rt_wait_for_io(); - - HAL::out16(theHook->fPort[i], kDebugMag1); - HAL::rt_wait_for_io(); - - HAL::out16(theHook->fPort[i], kDebugMag2); - HAL::rt_wait_for_io(); - - HAL::out16(theHook->fPort[i], kDebugMag3); - HAL::rt_wait_for_io(); - - if (HAL::in16(theHook->fPort[i] != kDebugUnboundPort)) - theHook->fBoundCnt++; - } - } +namespace hCore { +typedef Char rt_debug_type[255]; + +class DebuggerPorts final { + public: + Int16 fPort[kDebugMaxPorts]; + Int16 fBoundCnt; +}; + +void rt_debug_listen(DebuggerPorts* theHook) noexcept { + if (theHook == nullptr) return; + + for (UInt32 i = 0U; i < kDebugMaxPorts; ++i) { + HAL::out16(theHook->fPort[i], kDebugMag0); + HAL::rt_wait_for_io(); + + HAL::out16(theHook->fPort[i], kDebugMag1); + HAL::rt_wait_for_io(); + + HAL::out16(theHook->fPort[i], kDebugMag2); + HAL::rt_wait_for_io(); + + HAL::out16(theHook->fPort[i], kDebugMag3); + HAL::rt_wait_for_io(); + + if (HAL::in16(theHook->fPort[i] != kDebugUnboundPort)) theHook->fBoundCnt++; + } } +} // namespace hCore diff --git a/Private/HALKit/AMD64/HalPageAlloc.cpp b/Private/HALKit/AMD64/HalPageAlloc.cpp index 07cfbecf..25757942 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.cpp +++ b/Private/HALKit/AMD64/HalPageAlloc.cpp @@ -16,46 +16,41 @@ static hCore::UIntPtr kPagePtr = kPagePtrAddress; static hCore::SizeT kPageCnt = 0UL; -namespace hCore -{ - namespace HAL - { - static auto hal_try_alloc_new_page(SizeT sz, Boolean rw, Boolean user) -> PageTable64* - { - char *ptr = &(reinterpret_cast<char*>(kPagePtr))[kPageCnt + 1]; - - PageTable64 *pte = reinterpret_cast<PageTable64*>(ptr); - pte->Rw = rw; - pte->User = user; - pte->Present = true; - - return pte; - } - - auto hal_alloc_page(SizeT sz, Boolean rw, Boolean user) -> PageTable64* - { - for (SizeT i = 0; i < kPageCnt; ++i) - { - PageTable64 *pte = (reinterpret_cast<PageTable64*>(&kPagePtr) + i); - - if (!pte->Present) { - pte->User = user; - pte->Rw = rw; - pte->Present = true; - - return pte; - } - } - - return hal_try_alloc_new_page(sz, rw, user); - } - - auto hal_create_page(Boolean rw, Boolean user) -> UIntPtr - { - PageTable64 *new_pte = hal_alloc_page(sizeof(PageTable64), rw, user); - MUST_PASS(new_pte); - - return reinterpret_cast<UIntPtr>(new_pte); - } - } // namespace HAL -} // namespace hCore +namespace hCore { +namespace HAL { +static auto hal_try_alloc_new_page(SizeT sz, Boolean rw, Boolean user) + -> PageTable64 * { + char *ptr = &(reinterpret_cast<char *>(kPagePtr))[kPageCnt + 1]; + + PageTable64 *pte = reinterpret_cast<PageTable64 *>(ptr); + pte->Rw = rw; + pte->User = user; + pte->Present = true; + + return pte; +} + +auto hal_alloc_page(SizeT sz, Boolean rw, Boolean user) -> PageTable64 * { + for (SizeT i = 0; i < kPageCnt; ++i) { + PageTable64 *pte = (reinterpret_cast<PageTable64 *>(&kPagePtr) + i); + + if (!pte->Present) { + pte->User = user; + pte->Rw = rw; + pte->Present = true; + + return pte; + } + } + + return hal_try_alloc_new_page(sz, rw, user); +} + +auto hal_create_page(Boolean rw, Boolean user) -> UIntPtr { + PageTable64 *new_pte = hal_alloc_page(sizeof(PageTable64), rw, user); + MUST_PASS(new_pte); + + return reinterpret_cast<UIntPtr>(new_pte); +} +} // namespace HAL +} // namespace hCore diff --git a/Private/HALKit/AMD64/HalPageAlloc.hpp b/Private/HALKit/AMD64/HalPageAlloc.hpp index 06a883bf..dc954153 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.hpp +++ b/Private/HALKit/AMD64/HalPageAlloc.hpp @@ -13,11 +13,11 @@ #ifndef PTE_MAX #define PTE_MAX (512) -#endif //! PTE_MAX +#endif //! PTE_MAX #ifndef PTE_ALIGN #define PTE_ALIGN (4096) -#endif //! PTE_ALIGN +#endif //! PTE_ALIGN #define kPagePtrAddress 0x0900000 @@ -29,24 +29,21 @@ extern "C" hCore::UIntPtr read_cr0(); extern "C" hCore::UIntPtr read_cr2(); extern "C" hCore::UIntPtr read_cr3(); -namespace hCore::HAL -{ - struct PageTable64 - { - bool Present: 1; - bool Rw: 1; - bool User: 1; - bool Wt: 1; - bool Cache: 1; - bool Accessed: 1; - hCore::Int32 Reserved: 6; - hCore::UIntPtr PhysicalAddress: 36; - hCore::Int32 Reserved1: 15; - bool ExecDisable: 1; - - }; - - PageTable64 *hal_alloc_page(SizeT sz, Boolean rw, Boolean user); - - UIntPtr hal_create_page(Boolean rw, Boolean user); -} // namespace hCore::HAL +namespace hCore::HAL { +struct PageTable64 { + bool Present : 1; + bool Rw : 1; + bool User : 1; + bool Wt : 1; + bool Cache : 1; + bool Accessed : 1; + hCore::Int32 Reserved : 6; + hCore::UIntPtr PhysicalAddress : 36; + hCore::Int32 Reserved1 : 15; + bool ExecDisable : 1; +}; + +PageTable64 *hal_alloc_page(SizeT sz, Boolean rw, Boolean user); + +UIntPtr hal_create_page(Boolean rw, Boolean user); +} // namespace hCore::HAL diff --git a/Private/HALKit/AMD64/HalRoutines.s b/Private/HALKit/AMD64/HalRoutines.s index ed15418a..78b9e503 100644 --- a/Private/HALKit/AMD64/HalRoutines.s +++ b/Private/HALKit/AMD64/HalRoutines.s @@ -24,4 +24,3 @@ rt_wait_for_io: rt_get_current_context: mov %rbp, %rax ret - diff --git a/Private/HALKit/AMD64/HardwareAPIC.cpp b/Private/HALKit/AMD64/HardwareAPIC.cpp index f78cfe3b..9a058eae 100644 --- a/Private/HALKit/AMD64/HardwareAPIC.cpp +++ b/Private/HALKit/AMD64/HardwareAPIC.cpp @@ -11,36 +11,32 @@ // bugs = 0 -namespace hCore -{ - // @brief wakes up thread. - // wakes up thread from hang. - void rt_wakeup_thread(HAL::StackFrame* stack) - { - __asm__ volatile ("cli"); - - stack->Rbp = stack->R15; - stack->Rsi = stack->Rbp; - - __asm__ volatile ("sti"); - } - - static void __rt_hang_proc(void) - { - while (1) - ; - } - - // @brief makes thread sleep. - // hooks and hangs thread to prevent code from executing. - 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; - - __asm__ volatile ("sti"); - } -}
\ No newline at end of file +namespace hCore { +// @brief wakes up thread. +// wakes up thread from hang. +void rt_wakeup_thread(HAL::StackFrame* stack) { + __asm__ volatile("cli"); + + stack->Rbp = stack->R15; + stack->Rsi = stack->Rbp; + + __asm__ volatile("sti"); +} + +static void __rt_hang_proc(void) { + while (1) + ; +} + +// @brief makes thread sleep. +// hooks and hangs thread to prevent code from executing. +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; + + __asm__ volatile("sti"); +} +} // namespace hCore diff --git a/Private/HALKit/AMD64/HardwareInit.cpp b/Private/HALKit/AMD64/HardwareInit.cpp index 367f083b..7d769bfa 100644 --- a/Private/HALKit/AMD64/HardwareInit.cpp +++ b/Private/HALKit/AMD64/HardwareInit.cpp @@ -11,12 +11,10 @@ // bugs = 0 -namespace hCore -{ - bool init_hal() - { - // TODO: Hardware Specific stuff. +namespace hCore { +bool init_hal() { + // TODO: Hardware Specific stuff. - return true; - } -}
\ No newline at end of file + return true; +} +} // namespace hCore diff --git a/Private/HALKit/AMD64/Hypervisor.hpp b/Private/HALKit/AMD64/Hypervisor.hpp index 5c321f23..c830ef00 100644 --- a/Private/HALKit/AMD64/Hypervisor.hpp +++ b/Private/HALKit/AMD64/Hypervisor.hpp @@ -12,27 +12,17 @@ #include <NewKit/Defines.hpp> namespace hCore { - MAKE_STRING_ENUM(HYPERVISOR) - ENUM_STRING(Qemu, - "TCGTCGTCGTCG"); - ENUM_STRING(KVM, - " KVMKVMKVM "); - ENUM_STRING(VMWare, - "VMwareVMware"); - ENUM_STRING(VirtualBox, - "VBoxVBoxVBox"); - ENUM_STRING(Xen, - "XenVMMXenVMM"); - ENUM_STRING(Microsoft, - "Microsoft Hv"); - ENUM_STRING(Parallels, - " prl hyperv "); - ENUM_STRING(ParallelsAlt, - " lrpepyh vr "); - ENUM_STRING(Bhyve, - "bhyve bhyve "); - ENUM_STRING(Qnx, - " QNXQVMBSQG "); +MAKE_STRING_ENUM(HYPERVISOR) +ENUM_STRING(Qemu, "TCGTCGTCGTCG"); +ENUM_STRING(KVM, " KVMKVMKVM "); +ENUM_STRING(VMWare, "VMwareVMware"); +ENUM_STRING(VirtualBox, "VBoxVBoxVBox"); +ENUM_STRING(Xen, "XenVMMXenVMM"); +ENUM_STRING(Microsoft, "Microsoft Hv"); +ENUM_STRING(Parallels, " prl hyperv "); +ENUM_STRING(ParallelsAlt, " lrpepyh vr "); +ENUM_STRING(Bhyve, "bhyve bhyve "); +ENUM_STRING(Qnx, " QNXQVMBSQG "); - END_STRING_ENUM() -} // namespace hCore +END_STRING_ENUM() +} // namespace hCore diff --git a/Private/HALKit/AMD64/TIBInstall.asm b/Private/HALKit/AMD64/InstallTIB.asm index 69aa7503..6957e1a6 100644 --- a/Private/HALKit/AMD64/TIBInstall.asm +++ b/Private/HALKit/AMD64/InstallTIB.asm @@ -15,4 +15,4 @@ rt_install_tib: mov rsi, gs ret -;; //////////////////////////////////////////////////// ;;
\ No newline at end of file +;; //////////////////////////////////////////////////// ;; diff --git a/Private/HALKit/AMD64/PCI/Database.cpp b/Private/HALKit/AMD64/PCI/Database.cpp index b3a1961c..daf4bc61 100644 --- a/Private/HALKit/AMD64/PCI/Database.cpp +++ b/Private/HALKit/AMD64/PCI/Database.cpp @@ -9,6 +9,4 @@ #include <KernelKit/PCI/Database.hpp> -namespace hCore -{ -} +namespace hCore {} diff --git a/Private/HALKit/AMD64/PCI/Device.cpp b/Private/HALKit/AMD64/PCI/Device.cpp index f8b5f629..597e936b 100644 --- a/Private/HALKit/AMD64/PCI/Device.cpp +++ b/Private/HALKit/AMD64/PCI/Device.cpp @@ -10,118 +10,103 @@ #include <ArchKit/Arch.hpp> #include <KernelKit/PCI/Device.hpp> +hCore::UInt LumiaPCIReadRaw(hCore::UInt bar, hCore::UShort bus, + hCore::UShort dev, hCore::UShort fun) { + hCore::UInt target = 0x80000000 | ((hCore::UInt)bus << 16) | + ((hCore::UInt)dev << 11) | ((hCore::UInt)fun << 8) | + (bar & 0xFC); -hCore::UInt LumiaPCIReadRaw(hCore::UInt bar, hCore::UShort bus, hCore::UShort dev, hCore::UShort fun) -{ - hCore::UInt target = 0x80000000 | ((hCore::UInt) bus << 16) | ((hCore::UInt) dev << 11) | ((hCore::UInt) fun << 8) | - (bar & 0xFC); + hCore::HAL::out32((hCore::UShort)hCore::PCI::PciConfigKind::ConfigAddress, + target); - hCore::HAL::out32((hCore::UShort) hCore::PCI::PciConfigKind::ConfigAddress, target); + return hCore::HAL::in32((hCore::UShort)hCore::PCI::PciConfigKind::ConfigData); +} + +void LumiaPCISetCfgTarget(hCore::UInt bar, hCore::UShort bus, hCore::UShort dev, + hCore::UShort fun) { + hCore::UInt target = 0x80000000 | ((hCore::UInt)bus << 16) | + ((hCore::UInt)dev << 11) | ((hCore::UInt)fun << 8) | + (bar & ~3); + + hCore::HAL::out32((hCore::UShort)hCore::PCI::PciConfigKind::ConfigAddress, + target); +} + +namespace hCore::PCI { +Device::Device(UShort bus, UShort device, UShort func, UShort bar) + : m_Bus(bus), m_Device(device), m_Function(func), m_Bar(bar) {} + +Device::~Device() {} + +UInt Device::Read(UInt bar, Size sz) { + LumiaPCISetCfgTarget(bar, m_Bus, m_Device, m_Function); + + if (sz == 4) + return HAL::in32((UShort)PciConfigKind::ConfigData + (m_Bar & 3)); + if (sz == 2) + return HAL::in16((UShort)PciConfigKind::ConfigData + (m_Bar & 3)); + if (sz == 1) return HAL::in8((UShort)PciConfigKind::ConfigData + (m_Bar & 3)); + + return 0xFFFF; +} + +void Device::Write(UInt bar, UIntPtr data, Size sz) { + LumiaPCISetCfgTarget(bar, m_Bus, m_Device, m_Function); + + if (sz == 4) + HAL::out32((UShort)PciConfigKind::ConfigData + (m_Bar & 3), (UInt)data); + if (sz == 2) + HAL::out16((UShort)PciConfigKind::ConfigData + (m_Bar & 3), (UShort)data); + if (sz == 1) + HAL::out8((UShort)PciConfigKind::ConfigData + (m_Bar & 3), (UChar)data); +} + +UShort Device::DeviceId() { + return (UShort)(LumiaPCIReadRaw(0x0 >> 16, m_Bus, m_Device, m_Function)); +} - return hCore::HAL::in32((hCore::UShort) hCore::PCI::PciConfigKind::ConfigData); +UShort Device::VendorId() { + return (UShort)(LumiaPCIReadRaw(0x0, m_Bus, m_Device, m_Function) >> 16); } -void LumiaPCISetCfgTarget(hCore::UInt bar, hCore::UShort bus, hCore::UShort dev, hCore::UShort fun) -{ - hCore::UInt target = - 0x80000000 | ((hCore::UInt) bus << 16) | ((hCore::UInt) dev << 11) | ((hCore::UInt) fun << 8) | (bar & ~3); +UShort Device::InterfaceId() { + return (UShort)(LumiaPCIReadRaw(0x0, m_Bus, m_Device, m_Function) >> 16); +} + +UChar Device::Class() { + return (UChar)(LumiaPCIReadRaw(0x08, m_Bus, m_Device, m_Function) >> 24); +} + +UChar Device::Subclass() { + return (UChar)(LumiaPCIReadRaw(0x08, m_Bus, m_Device, m_Function) >> 16); +} + +UChar Device::ProgIf() { + return (UChar)(LumiaPCIReadRaw(0x08, m_Bus, m_Device, m_Function) >> 8); +} + +UChar Device::HeaderType() { + return (UChar)(LumiaPCIReadRaw(0xC, m_Bus, m_Device, m_Function) >> 16); +} + +void Device::EnableMmio() { + bool enable = Read(0x04, sizeof(UChar)) | (1 << 1); + Write(0x04, enable, sizeof(UShort)); +} + +void Device::BecomeBusMaster() { + bool enable = Read(0x04, sizeof(UShort)) | (1 << 2); + Write(0x04, enable, sizeof(UShort)); +} + +UShort Device::Vendor() { + UShort vendor = VendorId(); + + if (vendor != (UShort)PciConfigKind::Invalid) + m_Device = (UShort)Read(0x0, sizeof(UShort)); - hCore::HAL::out32((hCore::UShort) hCore::PCI::PciConfigKind::ConfigAddress, target); + return m_Device; } -namespace hCore::PCI -{ - Device::Device(UShort bus, UShort device, UShort func, UShort bar) - : m_Bus(bus), m_Device(device), m_Function(func), m_Bar(bar) - {} - - Device::~Device() {} - - UInt Device::Read(UInt bar, Size sz) - { - LumiaPCISetCfgTarget(bar, m_Bus, m_Device, m_Function); - - if (sz == 4) - return HAL::in32((UShort) PciConfigKind::ConfigData + (m_Bar & 3)); - if (sz == 2) - return HAL::in16((UShort) PciConfigKind::ConfigData + (m_Bar & 3)); - if (sz == 1) - return HAL::in8((UShort) PciConfigKind::ConfigData + (m_Bar & 3)); - - return 0xFFFF; - } - - void Device::Write(UInt bar, UIntPtr data, Size sz) - { - LumiaPCISetCfgTarget(bar, m_Bus, m_Device, m_Function); - - if (sz == 4) - HAL::out32((UShort) PciConfigKind::ConfigData + (m_Bar & 3), (UInt) data); - if (sz == 2) - HAL::out16((UShort) PciConfigKind::ConfigData + (m_Bar & 3), (UShort) data); - if (sz == 1) - HAL::out8((UShort) PciConfigKind::ConfigData + (m_Bar & 3), (UChar) data); - } - - UShort Device::DeviceId() - { - return (UShort)(LumiaPCIReadRaw(0x0 >> 16, m_Bus, m_Device, m_Function)); - } - - UShort Device::VendorId() - { - return (UShort) (LumiaPCIReadRaw(0x0, m_Bus, m_Device, m_Function) >> 16); - } - - UShort Device::InterfaceId() - { - return (UShort)(LumiaPCIReadRaw(0x0, m_Bus, m_Device, m_Function) >> 16); - } - - UChar Device::Class() - { - return (UChar)(LumiaPCIReadRaw(0x08, m_Bus, m_Device, m_Function) >> 24); - } - - UChar Device::Subclass() - { - return (UChar)(LumiaPCIReadRaw(0x08, m_Bus, m_Device, m_Function) >> 16); - } - - UChar Device::ProgIf() - { - return (UChar)(LumiaPCIReadRaw(0x08, m_Bus, m_Device, m_Function) >> 8); - } - - UChar Device::HeaderType() - { - return (UChar)(LumiaPCIReadRaw(0xC, m_Bus, m_Device, m_Function) >> 16); - } - - void Device::EnableMmio() - { - bool enable = Read(0x04, sizeof(UChar)) | (1 << 1); - Write(0x04, enable, sizeof(UShort)); - } - - void Device::BecomeBusMaster() - { - bool enable = Read(0x04, sizeof(UShort)) | (1 << 2); - Write(0x04, enable, sizeof(UShort)); - } - - UShort Device::Vendor() - { - UShort vendor = VendorId(); - - if (vendor != (UShort) PciConfigKind::Invalid) - m_Device = (UShort) Read(0x0, sizeof(UShort)); - - return m_Device; - } - - Device::operator bool() - { - return VendorId() != (UShort) PciConfigKind::Invalid; - } -} // namespace hCore::PCI +Device::operator bool() { return VendorId() != (UShort)PciConfigKind::Invalid; } +} // namespace hCore::PCI diff --git a/Private/HALKit/AMD64/PCI/Dma.cpp b/Private/HALKit/AMD64/PCI/Dma.cpp index 0b75776b..e52ef743 100644 --- a/Private/HALKit/AMD64/PCI/Dma.cpp +++ b/Private/HALKit/AMD64/PCI/Dma.cpp @@ -10,66 +10,57 @@ #include <KernelKit/PCI/Dma.hpp> namespace hCore { - DMAWrapper::operator bool() { - return m_Address; - } +DMAWrapper::operator bool() { return m_Address; } - bool DMAWrapper::operator!() { - return !m_Address; - } +bool DMAWrapper::operator!() { return !m_Address; } - Boolean DMAWrapper::Check(UIntPtr offset) const { - if (!m_Address) - return false; - if (offset == 0) - return true; +Boolean DMAWrapper::Check(UIntPtr offset) const { + if (!m_Address) return false; + if (offset == 0) return true; - kcout << "[DMAWrapper::IsIn] Checking offset..\n"; - return reinterpret_cast<UIntPtr>(m_Address) >= offset; - } + kcout << "[DMAWrapper::IsIn] Checking offset..\n"; + return reinterpret_cast<UIntPtr>(m_Address) >= offset; +} - bool DMAWrapper::Write(const UIntPtr &bit, const UIntPtr &offset) { - if (!m_Address) - return false; +bool DMAWrapper::Write(const UIntPtr &bit, const UIntPtr &offset) { + if (!m_Address) return false; - kcout << "[DMAWrapper::Write] Writing at address..\n"; + kcout << "[DMAWrapper::Write] Writing at address..\n"; - auto addr = (volatile UIntPtr *) (reinterpret_cast<UIntPtr>(m_Address) + offset); - *addr = bit; + auto addr = + (volatile UIntPtr *)(reinterpret_cast<UIntPtr>(m_Address) + offset); + *addr = bit; - return true; - } + return true; +} - UIntPtr DMAWrapper::Read(const UIntPtr &offset) { - kcout << "[DMAWrapper::Read] checking m_Address..\n"; - if (!m_Address) - return 0; +UIntPtr DMAWrapper::Read(const UIntPtr &offset) { + kcout << "[DMAWrapper::Read] checking m_Address..\n"; + if (!m_Address) return 0; - kcout << "[DMAWrapper::Read] Reading m_Address..\n"; - return *(volatile UIntPtr *) (reinterpret_cast<UIntPtr>(m_Address) + offset);; - } + kcout << "[DMAWrapper::Read] Reading m_Address..\n"; + return *(volatile UIntPtr *)(reinterpret_cast<UIntPtr>(m_Address) + offset); + ; +} - UIntPtr DMAWrapper::operator[](const UIntPtr &offset) { - return this->Read(offset); - } +UIntPtr DMAWrapper::operator[](const UIntPtr &offset) { + return this->Read(offset); +} - OwnPtr <IOBuf<Char *>> DMAFactory::Construct(OwnPtr <DMAWrapper> &dma) { - if (!dma) - return {}; +OwnPtr<IOBuf<Char *>> DMAFactory::Construct(OwnPtr<DMAWrapper> &dma) { + if (!dma) return {}; - OwnPtr < IOBuf < Char * >> dmaOwnPtr = make_ptr < IOBuf < Char * >, char * > - (reinterpret_cast<char*>(dma->m_Address)); + OwnPtr<IOBuf<Char *>> dmaOwnPtr = + make_ptr<IOBuf<Char *>, char *>(reinterpret_cast<char *>(dma->m_Address)); - if (!dmaOwnPtr) - return {}; + if (!dmaOwnPtr) return {}; - kcout << "Returning the new OwnPtr<IOBuf<Char*>>!\r\n"; - return dmaOwnPtr; - } + kcout << "Returning the new OwnPtr<IOBuf<Char*>>!\r\n"; + return dmaOwnPtr; +} - DMAWrapper& DMAWrapper::operator=(voidPtr Ptr) - { - m_Address = Ptr; - return *this; - } -} // namespace hCore +DMAWrapper &DMAWrapper::operator=(voidPtr Ptr) { + m_Address = Ptr; + return *this; +} +} // namespace hCore diff --git a/Private/HALKit/AMD64/PCI/Express.cpp b/Private/HALKit/AMD64/PCI/Express.cpp index dca3f37b..eaf886b7 100644 --- a/Private/HALKit/AMD64/PCI/Express.cpp +++ b/Private/HALKit/AMD64/PCI/Express.cpp @@ -9,5 +9,4 @@ #include <KernelKit/PCI/Express.hpp> -namespace hCore { -}
\ No newline at end of file +namespace hCore {} diff --git a/Private/HALKit/AMD64/PCI/Iterator.cpp b/Private/HALKit/AMD64/PCI/Iterator.cpp index 6f80d496..3d357901 100644 --- a/Private/HALKit/AMD64/PCI/Iterator.cpp +++ b/Private/HALKit/AMD64/PCI/Iterator.cpp @@ -9,31 +9,29 @@ #include <KernelKit/PCI/Iterator.hpp> -#define PCI_ITERATOR_FIND_AND_UNWRAP(DEV, SZ) \ - if (DEV.Leak()) \ - return DEV.Leak(); +#define PCI_ITERATOR_FIND_AND_UNWRAP(DEV, SZ) \ + if (DEV.Leak()) return DEV.Leak(); namespace hCore::PCI { - Iterator::Iterator(const Types::PciDeviceKind &type) { - // probe devices. - for (int bus = 0; bus < ME_BUS_COUNT; ++bus) { - for (int device = 0; device < ME_DEVICE_COUNT; ++device) { - for (int function = 0; function < ME_FUNCTION_COUNT; ++function) { - Device dev(bus, device, function, 0); +Iterator::Iterator(const Types::PciDeviceKind &type) { + // probe devices. + for (int bus = 0; bus < ME_BUS_COUNT; ++bus) { + for (int device = 0; device < ME_DEVICE_COUNT; ++device) { + for (int function = 0; function < ME_FUNCTION_COUNT; ++function) { + Device dev(bus, device, function, 0); - if (dev.Class() == (UChar) type) { - m_Devices[bus].Leak().Leak() = dev; - } - } - } + if (dev.Class() == (UChar)type) { + m_Devices[bus].Leak().Leak() = dev; } + } } + } +} - Iterator::~Iterator() { - } +Iterator::~Iterator() {} - Ref<PCI::Device> Iterator::operator[](const Size &sz) { - PCI_ITERATOR_FIND_AND_UNWRAP(m_Devices[sz], sz); - return {}; - } -} // namespace hCore::PCI +Ref<PCI::Device> Iterator::operator[](const Size &sz) { + PCI_ITERATOR_FIND_AND_UNWRAP(m_Devices[sz], sz); + return {}; +} +} // namespace hCore::PCI diff --git a/Private/HALKit/AMD64/PCI/PCI.cpp b/Private/HALKit/AMD64/PCI/PCI.cpp index 66770939..833bce22 100644 --- a/Private/HALKit/AMD64/PCI/PCI.cpp +++ b/Private/HALKit/AMD64/PCI/PCI.cpp @@ -7,4 +7,4 @@ * ======================================================== */ -#include <KernelKit/PCI/PCI.hpp>
\ No newline at end of file +#include <KernelKit/PCI/PCI.hpp> diff --git a/Private/HALKit/AMD64/PlatformAMD64.cpp b/Private/HALKit/AMD64/PlatformAMD64.cpp index 0bade8dc..3005cc07 100644 --- a/Private/HALKit/AMD64/PlatformAMD64.cpp +++ b/Private/HALKit/AMD64/PlatformAMD64.cpp @@ -9,50 +9,39 @@ #include <ArchKit/Arch.hpp> -namespace hCore::HAL -{ -namespace Detail -{ -struct RegisterAMD64 final -{ - UIntPtr base; - UShort limit; +namespace hCore::HAL { +namespace Detail { +struct RegisterAMD64 final { + UIntPtr base; + UShort limit; }; -} // namespace Detail +} // namespace Detail -void GDTLoader::Load(Register64 &gdt) -{ - Detail::RegisterAMD64 *reg = new Detail::RegisterAMD64(); - MUST_PASS(reg); +void GDTLoader::Load(Register64 &gdt) { + Detail::RegisterAMD64 *reg = new Detail::RegisterAMD64(); + MUST_PASS(reg); - reg->base = gdt.Base; - reg->limit = gdt.Limit; + reg->base = gdt.Base; + reg->limit = gdt.Limit; - rt_cli(); - load_gdt(reg); - rt_sti(); + rt_cli(); + load_gdt(reg); + rt_sti(); } -void IDTLoader::Load(Register64 &idt) -{ - Detail::RegisterAMD64 *reg = new Detail::RegisterAMD64(); - MUST_PASS(reg); +void IDTLoader::Load(Register64 &idt) { + Detail::RegisterAMD64 *reg = new Detail::RegisterAMD64(); + MUST_PASS(reg); - reg->base = idt.Base; - reg->limit = idt.Limit; + reg->base = idt.Base; + reg->limit = idt.Limit; - rt_cli(); - load_idt(reg); - rt_sti(); + rt_cli(); + load_idt(reg); + rt_sti(); } -void GDTLoader::Load(Ref<Register64> &gdt) -{ - GDTLoader::Load(gdt.Leak()); -} +void GDTLoader::Load(Ref<Register64> &gdt) { GDTLoader::Load(gdt.Leak()); } -void IDTLoader::Load(Ref<Register64> &idt) -{ - IDTLoader::Load(idt.Leak()); -} -} // namespace hCore::HAL +void IDTLoader::Load(Ref<Register64> &idt) { IDTLoader::Load(idt.Leak()); } +} // namespace hCore::HAL diff --git a/Private/HALKit/AMD64/ProcessPrimitives.cxx b/Private/HALKit/AMD64/ProcessPrimitives.cxx index f5b19861..557eb7f0 100644 --- a/Private/HALKit/AMD64/ProcessPrimitives.cxx +++ b/Private/HALKit/AMD64/ProcessPrimitives.cxx @@ -7,20 +7,19 @@ * ======================================================== */ -#include "NewKit/Defines.hpp" #include <KernelKit/ProcessManager.hpp> +#include "NewKit/Defines.hpp" + using namespace hCore; -Void Process::AssignStart(UIntPtr &imageStart) noexcept -{ - if (imageStart == 0) - this->Crash(); +Void Process::AssignStart(UIntPtr &imageStart) noexcept { + if (imageStart == 0) this->Crash(); #ifdef __x86_64__ - this->StackFrame->Rbp = imageStart; + this->StackFrame->Rbp = imageStart; #elif defined(__powerpc) - // link return register towards the __start symbol. - this->StackFrame->R3 = imageStart; + // link return register towards the __start symbol. + this->StackFrame->R3 = imageStart; #endif } diff --git a/Private/HALKit/AMD64/Processor.cpp b/Private/HALKit/AMD64/Processor.cpp index 4e1216e6..893f4a58 100644 --- a/Private/HALKit/AMD64/Processor.cpp +++ b/Private/HALKit/AMD64/Processor.cpp @@ -14,64 +14,45 @@ * @brief This file is about processor specific functions (in/out...) */ -namespace hCore::HAL -{ -void out8(UInt16 port, UInt8 value) -{ - asm volatile("outb %%al, %1" : : "a"(value), "Nd"(port) : "memory"); +namespace hCore::HAL { +void out8(UInt16 port, UInt8 value) { + asm volatile("outb %%al, %1" : : "a"(value), "Nd"(port) : "memory"); } -void out16(UInt16 port, UInt16 value) -{ - asm volatile("outw %%ax, %1" : : "a"(value), "Nd"(port) : "memory"); +void out16(UInt16 port, UInt16 value) { + asm volatile("outw %%ax, %1" : : "a"(value), "Nd"(port) : "memory"); } -void out32(UInt16 port, UInt32 value) -{ - asm volatile("outl %%eax, %1" : : "a"(value), "Nd"(port) : "memory"); +void out32(UInt16 port, UInt32 value) { + asm volatile("outl %%eax, %1" : : "a"(value), "Nd"(port) : "memory"); } -UInt8 in8(UInt16 port) -{ - UInt8 value = 0UL; - asm volatile("inb %1, %%al" : "=a"(value) : "Nd"(port) : "memory"); +UInt8 in8(UInt16 port) { + UInt8 value = 0UL; + asm volatile("inb %1, %%al" : "=a"(value) : "Nd"(port) : "memory"); - return value; + return value; } -UInt16 in16(UInt16 port) -{ - UInt16 value = 0UL; - asm volatile("inw %1, %%ax" : "=a"(value) : "Nd"(port) : "memory"); +UInt16 in16(UInt16 port) { + UInt16 value = 0UL; + asm volatile("inw %1, %%ax" : "=a"(value) : "Nd"(port) : "memory"); - return value; + return value; } -UInt32 in32(UInt16 port) -{ - UInt32 value = 0UL; - asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); +UInt32 in32(UInt16 port) { + UInt32 value = 0UL; + asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); - return value; + return value; } -void rt_halt() -{ - asm volatile("hlt"); -} +void rt_halt() { asm volatile("hlt"); } -void rt_cli() -{ - asm volatile("cli"); -} +void rt_cli() { asm volatile("cli"); } -void rt_sti() -{ - asm volatile("sti"); -} +void rt_sti() { asm volatile("sti"); } -void rt_cld() -{ - asm volatile("cld"); -} -} // namespace hCore::HAL +void rt_cld() { asm volatile("cld"); } +} // namespace hCore::HAL diff --git a/Private/HALKit/AMD64/Processor.hpp b/Private/HALKit/AMD64/Processor.hpp index ca5db0ed..82eed130 100644 --- a/Private/HALKit/AMD64/Processor.hpp +++ b/Private/HALKit/AMD64/Processor.hpp @@ -9,122 +9,110 @@ #pragma once -#include <NewKit/Defines.hpp> #include <NewKit/Array.hpp> +#include <NewKit/Defines.hpp> #include <NewKit/Utils.hpp> #define IsActiveLow(flag) (flag & 2) #define IsLevelTriggered(flag) (flag & 8) -namespace hCore::HAL -{ - extern "C" UChar in8(UInt16 port); - extern "C" UShort in16(UInt16 port); - extern "C" UInt in32(UInt16 port); - - extern "C" void out16(UShort port, UShort byte); - extern "C" void out8(UShort port, UChar byte); - extern "C" void out32(UShort port, UInt byte); - - extern "C" void rt_wait_for_io(); - extern "C" void rt_halt(); - extern "C" void rt_cli(); - extern "C" void rt_sti(); - extern "C" void rt_cld(); - - class Register64 - { - public: - UIntPtr Base; - UShort Limit; - - operator bool() { return Base > Limit; } - - }; - - using RawRegister = UInt64; - - using InterruptId = UShort; /* For each element in the IVT */ - using interruptTrap = UIntPtr(UIntPtr sp); - - typedef UIntPtr Reg; - - struct __attribute__((packed)) StackFrame - { - 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; // Reserved: Multi Processor manager (Hal) - Reg SID; // Reserved: system call id (Hal) - }; - - typedef StackFrame* StackFramePtr; - - class InterruptDescriptor final - { - public: - UShort Offset; - UShort Selector; - UChar Ist; - UChar Atrributes; - - UShort SecondOffset; - UInt ThirdOffset; - UInt Zero; - - operator bool() { return Offset != 0xFFFF; } - - }; - - using InterruptDescriptorArray = Array<InterruptDescriptor, 256>; - - class SegmentDescriptor final - { - public: - UIntPtr Base; - UIntPtr BaseMiddle; - UIntPtr BaseHigh; - - UShort Limit; - UChar Gran; - UChar AB; - - operator bool() { return Base > Limit; } - - }; - - using SegmentArray = Array<SegmentDescriptor, 6>; - - class GDTLoader final - { - public: - static void Load(Register64 &gdt); - static void Load(Ref<Register64> &gdt); - - }; - - class IDTLoader final - { - public: - static void Load(Register64 &idt); - static void Load(Ref<Register64> &idt); - - }; - - void system_get_cores(voidPtr rsdPtr); -} // namespace hCore::HAL +namespace hCore::HAL { +extern "C" UChar in8(UInt16 port); +extern "C" UShort in16(UInt16 port); +extern "C" UInt in32(UInt16 port); + +extern "C" void out16(UShort port, UShort byte); +extern "C" void out8(UShort port, UChar byte); +extern "C" void out32(UShort port, UInt byte); + +extern "C" void rt_wait_for_io(); +extern "C" void rt_halt(); +extern "C" void rt_cli(); +extern "C" void rt_sti(); +extern "C" void rt_cld(); + +class Register64 { + public: + UIntPtr Base; + UShort Limit; + + operator bool() { return Base > Limit; } +}; + +using RawRegister = UInt64; + +using InterruptId = UShort; /* For each element in the IVT */ +using interruptTrap = UIntPtr(UIntPtr sp); + +typedef UIntPtr Reg; + +struct __attribute__((packed)) StackFrame { + 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; // Reserved: Multi Processor manager (Hal) + Reg SID; // Reserved: system call id (Hal) +}; + +typedef StackFrame *StackFramePtr; + +class InterruptDescriptor final { + public: + UShort Offset; + UShort Selector; + UChar Ist; + UChar Atrributes; + + UShort SecondOffset; + UInt ThirdOffset; + UInt Zero; + + operator bool() { return Offset != 0xFFFF; } +}; + +using InterruptDescriptorArray = Array<InterruptDescriptor, 256>; + +class SegmentDescriptor final { + public: + UIntPtr Base; + UIntPtr BaseMiddle; + UIntPtr BaseHigh; + + UShort Limit; + UChar Gran; + UChar AB; + + operator bool() { return Base > Limit; } +}; + +using SegmentArray = Array<SegmentDescriptor, 6>; + +class GDTLoader final { + public: + static void Load(Register64 &gdt); + static void Load(Ref<Register64> &gdt); +}; + +class IDTLoader final { + public: + static void Load(Register64 &idt); + static void Load(Ref<Register64> &idt); +}; + +void system_get_cores(voidPtr rsdPtr); +} // namespace hCore::HAL extern "C" void idt_handle_system_call(hCore::UIntPtr rsp); extern "C" void idt_handle_generic(hCore::UIntPtr rsp); diff --git a/Private/HALKit/AMD64/SMPCoreManager.asm b/Private/HALKit/AMD64/SMPCoreManager.asm index ed56d7db..40664a7e 100644 --- a/Private/HALKit/AMD64/SMPCoreManager.asm +++ b/Private/HALKit/AMD64/SMPCoreManager.asm @@ -22,4 +22,4 @@ rt_do_context_switch_unprotected: mov [rdi+0], rax mov [rdi+8], rbx mov [rdi+16], rcx - ret
\ No newline at end of file + ret diff --git a/Private/HALKit/AMD64/crti.s b/Private/HALKit/AMD64/crti.s index 5856b9e9..a85782ea 100644 --- a/Private/HALKit/AMD64/crti.s +++ b/Private/HALKit/AMD64/crti.s @@ -14,5 +14,3 @@ _fini: push %rbp movq %rsp, %rbp /* gcc will nicely put the contents of crtbegin.o's .fini section here. */ - - diff --git a/Private/HALKit/AMD64/crtn.s b/Private/HALKit/AMD64/crtn.s index acc8faa5..b446d9f3 100644 --- a/Private/HALKit/AMD64/crtn.s +++ b/Private/HALKit/AMD64/crtn.s @@ -8,4 +8,3 @@ /* gcc will nicely put the contents of crtend.o's .fini section here. */ popq %rbp ret - diff --git a/Private/HALKit/PowerPC/CoreSyscallHandlerPowerPC.cpp b/Private/HALKit/PowerPC/CoreSyscallHandlerPowerPC.cpp index 6a723cba..ee300501 100644 --- a/Private/HALKit/PowerPC/CoreSyscallHandlerPowerPC.cpp +++ b/Private/HALKit/PowerPC/CoreSyscallHandlerPowerPC.cpp @@ -10,12 +10,11 @@ #include <ArchKit/Arch.hpp> #include <NewKit/Array.hpp> -hCore::Array<void (*)(hCore::Int32 id, hCore::HAL::StackFrame *), kMaxSyscalls> kSyscalls; +hCore::Array<void (*)(hCore::Int32 id, hCore::HAL::StackFrame *), kMaxSyscalls> + kSyscalls; -extern "C" void rt_syscall_handle(hCore::HAL::StackFrame *stack) -{ - for (hCore::SizeT index = 0UL; index < kMaxSyscalls; ++index) - { - (kSyscalls[index].Leak().Leak())(stack->ID, stack); - } +extern "C" void rt_syscall_handle(hCore::HAL::StackFrame *stack) { + for (hCore::SizeT index = 0UL; index < kMaxSyscalls; ++index) { + (kSyscalls[index].Leak().Leak())(stack->ID, stack); + } } diff --git a/Private/HALKit/PowerPC/PCI/Device.cxx b/Private/HALKit/PowerPC/PCI/Device.cxx index e69de29b..8b137891 100644 --- a/Private/HALKit/PowerPC/PCI/Device.cxx +++ b/Private/HALKit/PowerPC/PCI/Device.cxx @@ -0,0 +1 @@ + diff --git a/Private/HALKit/PowerPC/Processor.cpp b/Private/HALKit/PowerPC/Processor.cpp index dc614fa9..e3b207c6 100644 --- a/Private/HALKit/PowerPC/Processor.cpp +++ b/Private/HALKit/PowerPC/Processor.cpp @@ -1,68 +1,48 @@ /* -* ======================================================== -* -* hCore -* Copyright 2024 Mahrouss Logic, all rights reserved. -* -* ======================================================== -*/ + * ======================================================== + * + * hCore + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ #include <HALKit/PowerPC/Processor.hpp> #include <KernelKit/DebugOutput.hpp> extern "C" void flush_tlb() {} extern "C" void rt_wait_for_io() {} -extern "C" hCore::HAL::StackFrame* rt_get_current_context() { } +extern "C" hCore::HAL::StackFrame* rt_get_current_context() {} -namespace hCore -{ -namespace HAL -{ - UIntPtr hal_create_page(bool rw, bool user) - { - return 0; - } +namespace hCore { +namespace HAL { +UIntPtr hal_create_page(bool rw, bool user) { return 0; } - UIntPtr hal_alloc_page(UIntPtr offset, bool rw, bool user) - { - return 0; - } -} +UIntPtr hal_alloc_page(UIntPtr offset, bool rw, bool user) { return 0; } +} // namespace HAL // @brief wakes up thread. // wakes up thread from hang. -void rt_wakeup_thread(HAL::StackFrame* stack) -{ - -} +void rt_wakeup_thread(HAL::StackFrame* stack) {} // @brief makes thread sleep. // hooks and hangs thread to prevent code from executing. -void rt_hang_thread(HAL::StackFrame* stack) -{ - -} +void rt_hang_thread(HAL::StackFrame* stack) {} // @brief main HAL entrypoint -void init_hal() -{ - -} +void init_hal() {} -void system_io_print(const char *bytes) -{ - if (!bytes) - return; +void system_io_print(const char* bytes) { + if (!bytes) return; - SizeT index = 0; - SizeT len = string_length(bytes, 256); + SizeT index = 0; + SizeT len = string_length(bytes, 256); - while (index < len) - { - // TODO - ++index; - } + while (index < len) { + // TODO + ++index; + } } TerminalDevice kcout(hCore::system_io_print, nullptr); -}
\ No newline at end of file +} // namespace hCore diff --git a/Private/HALKit/PowerPC/Processor.hpp b/Private/HALKit/PowerPC/Processor.hpp index 58de1aa6..5fc0dcb2 100644 --- a/Private/HALKit/PowerPC/Processor.hpp +++ b/Private/HALKit/PowerPC/Processor.hpp @@ -14,37 +14,31 @@ #define __aligned __attribute__((aligned(4))) -namespace hCore::HAL -{ - typedef UIntPtr Reg; - - struct __aligned StackFrame - { - Reg R0; - Reg R1; - Reg R2; - Reg R3; - Reg R4; - Reg R5; - Reg R6; - Reg R7; - Reg ID; // R8 - }; - - typedef StackFrame* StackFramePtr; - - inline void rt_halt() - { - while (1) - {} - } - - inline void rt_cli() - { - - } +namespace hCore::HAL { +typedef UIntPtr Reg; + +struct __aligned StackFrame { + Reg R0; + Reg R1; + Reg R2; + Reg R3; + Reg R4; + Reg R5; + Reg R6; + Reg R7; + Reg ID; // R8 +}; + +typedef StackFrame* StackFramePtr; + +inline void rt_halt() { + while (1) { + } } +inline void rt_cli() {} +} // namespace hCore::HAL + extern "C" void int_handle_math(hCore::UIntPtr sp); extern "C" void int_handle_pf(hCore::UIntPtr sp); -extern "C" void* __ppc_alloca(size_t sz);
\ No newline at end of file +extern "C" void* __ppc_alloca(size_t sz); diff --git a/Private/HALKit/PowerPC/CorePowerPCStart.s b/Private/HALKit/PowerPC/StartSequence.s index f0c5ae2b..0b9807d0 100644 --- a/Private/HALKit/PowerPC/CorePowerPCStart.s +++ b/Private/HALKit/PowerPC/StartSequence.s @@ -3,4 +3,4 @@ .text __AssemblerStart: - b __AssemblerStart
\ No newline at end of file + b __AssemblerStart diff --git a/Private/KernelKit/ProcessManager.hpp b/Private/KernelKit/ProcessManager.hpp index 8f3e04a5..89f94941 100644 --- a/Private/KernelKit/ProcessManager.hpp +++ b/Private/KernelKit/ProcessManager.hpp @@ -13,7 +13,7 @@ #include <ArchKit/Arch.hpp> #include <KernelKit/FileManager.hpp> #include <KernelKit/PermissionSelector.hxx> -#include <NewKit/Heap.hpp> +#include <NewKit/UserHeap.hpp> #include <NewKit/LockDelegate.hpp> #include <NewKit/MutableArray.hpp> diff --git a/Private/NewBoot/Source/HEL/AMD64/Boot.cxx b/Private/NewBoot/Source/HEL/AMD64/Boot.cxx index 96c30c12..70f280e6 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Boot.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Boot.cxx @@ -11,46 +11,43 @@ constexpr hCore::UInt32 kVGABaseAddress = 0xb8000; -hCore::SizeT BStrLen(const char *ptr) -{ - long long int cnt = 0; +hCore::SizeT BStrLen(const char *ptr) { + long long int cnt = 0; - while (*ptr != 0) - { - ++ptr; - ++cnt; - } + while (*ptr != 0) { + ++ptr; + ++cnt; + } - return cnt; + return cnt; } /** @brief puts wrapper over VGA. */ -void BKTextWriter::WriteString(const char *str, unsigned char forecolour, unsigned char backcolour, int x, int y) -{ - if (*str == 0 || !str) - return; - - for (SizeT idx = 0; idx < BStrLen(str); ++idx) - { - this->WriteCharacter(str[idx], forecolour, backcolour, x, y); - ++x; - } +void BKTextWriter::WriteString(const char *str, unsigned char forecolour, + unsigned char backcolour, int x, int y) { + if (*str == 0 || !str) return; + + for (SizeT idx = 0; idx < BStrLen(str); ++idx) { + this->WriteCharacter(str[idx], forecolour, backcolour, x, y); + ++x; + } } /** @brief putc wrapper over VGA. */ -void BKTextWriter::WriteCharacter(char c, unsigned char forecolour, unsigned char backcolour, int x, int y) -{ - UInt16 attrib = (backcolour << 4) | (forecolour & 0x0F); - - // Video Graphics Array - // Reads at kVGABaseAddress - // Decodes UInt16, gets attributes (back colour, fore colour) - // Gets character, send it to video display with according colour in the registry. - - fWhere = (volatile UInt16 *)kVGABaseAddress + (y * 80 + x); - *fWhere = c | (attrib << 8); +void BKTextWriter::WriteCharacter(char c, unsigned char forecolour, + unsigned char backcolour, int x, int y) { + UInt16 attrib = (backcolour << 4) | (forecolour & 0x0F); + + // Video Graphics Array + // Reads at kVGABaseAddress + // Decodes UInt16, gets attributes (back colour, fore colour) + // Gets character, send it to video display with according colour in the + // registry. + + fWhere = (volatile UInt16 *)kVGABaseAddress + (y * 80 + x); + *fWhere = c | (attrib << 8); } diff --git a/Private/NewBoot/Source/HEL/AMD64/EfiMain.cxx b/Private/NewBoot/Source/HEL/AMD64/EfiMain.cxx index 0102fa8c..0d44086b 100644 --- a/Private/NewBoot/Source/HEL/AMD64/EfiMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/EfiMain.cxx @@ -11,16 +11,19 @@ #include <BootKit/Processor.hxx> #include <BootKit/Protocol.hxx> -EFI_EXTERN_C int EfiMain(EfiHandlePtr ImageHandle, EfiSystemTable *SystemTable) -{ - SystemTable->ConOut->OutputString(SystemTable->ConOut, L"HCoreLdr: Starting \\EPM\\HCore...\r\n"); +EFI_EXTERN_C int EfiMain(EfiHandlePtr ImageHandle, + EfiSystemTable *SystemTable) { + SystemTable->ConOut->OutputString(SystemTable->ConOut, + L"HCoreLdr: Starting \\EPM\\HCore...\r\n"); - if (SystemTable->BootServices->ExitBootServices(ImageHandle, kBaseHandoverStruct) != kEfiOk) - { - SystemTable->ConOut->OutputString(SystemTable->ConOut, L"HCoreLdr: Could not Exit UEFI!\r\nHanging...\r\n"); + if (SystemTable->BootServices->ExitBootServices( + ImageHandle, kBaseHandoverStruct) != kEfiOk) { + SystemTable->ConOut->OutputString( + SystemTable->ConOut, + L"HCoreLdr: Could not Exit UEFI!\r\nHanging...\r\n"); - return kEfiFail; - } + return kEfiFail; + } - return kEfiOk; + return kEfiOk; } diff --git a/Private/NewBoot/Source/HEL/AMD64/Processor.cxx b/Private/NewBoot/Source/HEL/AMD64/Processor.cxx index f1f9b617..a0c93ab5 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Processor.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Processor.cxx @@ -16,22 +16,10 @@ #include <BootKit/Processor.hxx> -extern "C" void rt_halt(void) -{ - asm volatile("hlt"); -} +extern "C" void rt_halt(void) { asm volatile("hlt"); } -extern "C" void rt_cli(void) -{ - asm volatile("cli"); -} +extern "C" void rt_cli(void) { asm volatile("cli"); } -extern "C" void rt_sti(void) -{ - asm volatile("sti"); -} +extern "C" void rt_sti(void) { asm volatile("sti"); } -extern "C" void rt_cld(void) -{ - asm volatile("cld"); -} +extern "C" void rt_cld(void) { asm volatile("cld"); } diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 6ccec377..1fa6cc46 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -6,7 +6,7 @@ CC_GNU=x86_64-w64-mingw32-g++ LD_GNU=x86_64-w64-mingw32-ld -FLAG_GNU=-fshort-wchar -fPIC -D__DBG__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I../../efiSDK/inc -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__DBG__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I../../efiSDK/inc -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ .PHONY: arch-amd64 arch-amd64: diff --git a/Private/NewKit/KHeap.hpp b/Private/NewKit/KernelHeap.hpp index 7c9802d7..13037aa1 100644 --- a/Private/NewKit/KHeap.hpp +++ b/Private/NewKit/KernelHeap.hpp @@ -11,13 +11,13 @@ // last-rev 5/03/23 // file: KHeap.hpp -// description: page allocation for kernel. +// description: heap allocation for the kernel. #include <NewKit/Defines.hpp> #include <NewKit/Pmm.hpp> namespace hCore { -Int32 kernel_delete_ptr(voidPtr allocatedPtr); -voidPtr kernel_new_ptr(const SizeT &sz, const bool rw, const bool user); + Int32 kernel_delete_ptr(voidPtr allocatedPtr); + voidPtr kernel_new_ptr(const SizeT &sz, const bool rw, const bool user); } // namespace hCore diff --git a/Private/NewKit/Macros.hpp b/Private/NewKit/Macros.hpp index 8972e7b5..a6cb58dc 100644 --- a/Private/NewKit/Macros.hpp +++ b/Private/NewKit/Macros.hpp @@ -37,9 +37,9 @@ #define ATTRIBUTE(X) __attribute__((X)) #endif // #ifndef ATTRIBUTE -#ifndef __hCore__ -#define __hCore__ (202401) -#endif // !__hCore__ +#ifndef __HCORE__ +#define __HCORE__ (202401) +#endif // !__HCORE__ #ifndef EXTERN_C #define EXTERN_C() \ diff --git a/Private/NewKit/New.hpp b/Private/NewKit/New.hpp index 07939ab6..ba862788 100644 --- a/Private/NewKit/New.hpp +++ b/Private/NewKit/New.hpp @@ -9,7 +9,7 @@ */ #pragma once -#include <NewKit/KHeap.hpp> +#include <NewKit/KernelHeap.hpp> typedef __SIZE_TYPE__ size_t; // gcc will complain about that diff --git a/Private/NewKit/NewKit.hpp b/Private/NewKit/NewKit.hpp index e6797f33..02931085 100644 --- a/Private/NewKit/NewKit.hpp +++ b/Private/NewKit/NewKit.hpp @@ -13,7 +13,7 @@ #include <NewKit/OwnPtr.hpp> #include <NewKit/New.hpp> #include <NewKit/Json.hpp> -#include <NewKit/Heap.hpp> +#include <NewKit/UserHeap.hpp> #include <NewKit/ErrorOr.hpp> #include <NewKit/ArrayList.hpp> #include <NewKit/Array.hpp> diff --git a/Private/NewKit/Heap.hpp b/Private/NewKit/UserHeap.hpp index 71a93f6b..71a93f6b 100644 --- a/Private/NewKit/Heap.hpp +++ b/Private/NewKit/UserHeap.hpp diff --git a/Private/Source/CodeManager.cxx b/Private/Source/CodeManager.cxx index f1c6840e..ced403cc 100644 --- a/Private/Source/CodeManager.cxx +++ b/Private/Source/CodeManager.cxx @@ -14,7 +14,7 @@ #include <KernelKit/FileManager.hpp> #include <KernelKit/ProcessManager.hpp> #include <NewKit/ErrorID.hpp> -#include <NewKit/KHeap.hpp> +#include <NewKit/KernelHeap.hpp> #include <NewKit/OwnPtr.hpp> #include <NewKit/String.hpp> diff --git a/Private/Source/Crc32.cxx b/Private/Source/Crc32.cxx index 72b4da55..37663b8d 100644 --- a/Private/Source/Crc32.cxx +++ b/Private/Source/Crc32.cxx @@ -12,56 +12,67 @@ // @file CRC32.cpp // @brief Checksum implementation. -namespace hCore -{ - UInt kCrcTbl[kCrcCnt] = { - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, - 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, - 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, - 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, - 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, - 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, - 0xb6662d3d, 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, - 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, - 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, - 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, - 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, - 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, - 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, - 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, - 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, - 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, - 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, - 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, - 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, - 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, - 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, - 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d - }; +namespace hCore { +UInt kCrcTbl[kCrcCnt] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; - static Int crc_byte(Int crc, UChar byte) - { - crc = (crc >> 8) ^ kCrcTbl[(byte) ^ (crc & 0x000000FF)]; // shift 8 bytes to the right - - // XOR polynomial XOR the crc - // without the 2 highest bytes - return crc; - } +static Int crc_byte(Int crc, UChar byte) { + crc = (crc >> 8) ^ + kCrcTbl[(byte) ^ (crc & 0x000000FF)]; // shift 8 bytes to the right - Int crc32(const Char *byte, Int len) - { - Int checksum = 0; + // XOR polynomial XOR the crc + // without the 2 highest bytes + return crc; +} - for (UChar index = 1; index < len; ++index) - checksum = crc_byte(checksum, byte[index]); +Int crc32(const Char *byte, Int len) { + Int checksum = 0; - return checksum; - } -} // namespace hCore + for (UChar index = 1; index < len; ++index) + checksum = crc_byte(checksum, byte[index]); + + return checksum; +} +} // namespace hCore diff --git a/Private/Source/CxxAbi.cxx b/Private/Source/CxxAbi.cxx index 4984c81a..49b5834a 100644 --- a/Private/Source/CxxAbi.cxx +++ b/Private/Source/CxxAbi.cxx @@ -18,98 +18,75 @@ void *__dso_handle; atexit_func_entry_t __atexit_funcs[DSO_MAX_OBJECTS]; uarch_t __atexit_func_count; -extern "C" void __cxa_pure_virtual() -{ - hCore::kcout << "[__cxa_pure_virtual] Placeholder\n"; +extern "C" void __cxa_pure_virtual() { + hCore::kcout << "[__cxa_pure_virtual] Placeholder\n"; } -extern "C" void __stack_chk_fail() -{ - hCore::kcout << "[__stack_chk_fail] Buffer overflow detected\n"; - hCore::panic(RUNTIME_CHECK_POINTER); +extern "C" void __stack_chk_fail() { + hCore::kcout << "[__stack_chk_fail] Buffer overflow detected\n"; + hCore::panic(RUNTIME_CHECK_POINTER); } -extern "C" int __cxa_atexit(void (*f)(void *), void *arg, void *dso) -{ - if (__atexit_func_count >= DSO_MAX_OBJECTS) - return -1; +extern "C" int __cxa_atexit(void (*f)(void *), void *arg, void *dso) { + if (__atexit_func_count >= DSO_MAX_OBJECTS) return -1; - __atexit_funcs[__atexit_func_count].destructor_func = f; - __atexit_funcs[__atexit_func_count].obj_ptr = arg; - __atexit_funcs[__atexit_func_count].dso_handle = dso; + __atexit_funcs[__atexit_func_count].destructor_func = f; + __atexit_funcs[__atexit_func_count].obj_ptr = arg; + __atexit_funcs[__atexit_func_count].dso_handle = dso; - __atexit_func_count++; + __atexit_func_count++; - return 0; + return 0; } -extern "C" void __cxa_finalize(void *f) -{ - uarch_t i = __atexit_func_count; - if (!f) - { - while (i--) - { - if (__atexit_funcs[i].destructor_func) - { - (*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr); - }; - } - - return; +extern "C" void __cxa_finalize(void *f) { + uarch_t i = __atexit_func_count; + if (!f) { + while (i--) { + if (__atexit_funcs[i].destructor_func) { + (*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr); + }; } - while (i--) - { - if (__atexit_funcs[i].destructor_func) - { - (*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr); - __atexit_funcs[i].destructor_func = 0; - }; - } + return; + } + + while (i--) { + if (__atexit_funcs[i].destructor_func) { + (*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr); + __atexit_funcs[i].destructor_func = 0; + }; + } } -namespace cxxabiv1 -{ - extern "C" int __cxa_guard_acquire(__guard *g) - { - (void) g; - return 0; - } +namespace cxxabiv1 { +extern "C" int __cxa_guard_acquire(__guard *g) { + (void)g; + return 0; +} - extern "C" int __cxa_guard_release(__guard *g) - { - *(char *) g = 1; - return 0; - } +extern "C" int __cxa_guard_release(__guard *g) { + *(char *)g = 1; + return 0; +} - extern "C" void __cxa_guard_abort(__guard *g) - { - (void) g; - } -} // namespace cxxabiv1 +extern "C" void __cxa_guard_abort(__guard *g) { (void)g; } +} // namespace cxxabiv1 #else -namespace cxxkit -{ - extern "C" void __unwind(void(**finis)(void), int cnt) - { - for (int i = 0; i < cnt; ++i) - (finis[i])(); - } +namespace cxxkit { +extern "C" void __unwind(void (**finis)(void), int cnt) { + for (int i = 0; i < cnt; ++i) (finis[i])(); +} - extern "C" void __init_local(void(**init)(void), int cnt) - { - for (int i = 0; i < cnt; ++i) - (init[i])(); - } +extern "C" void __init_local(void (**init)(void), int cnt) { + for (int i = 0; i < cnt; ++i) (init[i])(); +} - extern "C" void __fini_local(void(**finis)(void), int cnt) - { - for (int i = 0; i < cnt; ++i) - (finis[i])(); - } +extern "C" void __fini_local(void (**finis)(void), int cnt) { + for (int i = 0; i < cnt; ++i) (finis[i])(); } +} // namespace cxxkit #endif diff --git a/Private/Source/CxxKitRT.cxx b/Private/Source/CxxKitRT.cxx index 21ebb342..e608ab3c 100644 --- a/Private/Source/CxxKitRT.cxx +++ b/Private/Source/CxxKitRT.cxx @@ -14,35 +14,30 @@ using namespace hCore; // unimplemented _HintTell -void _HintTell(_HintMessage* ppMsg, _HintId* pId) -{ - switch (*pId) - { - case kErrorExecutable: - { - const char* msg = "CodeManager doesn't recognize this executable."; - rt_copy_memory((const voidPtr)msg, *ppMsg, string_length(msg)); - break; +void _HintTell(_HintMessage* ppMsg, _HintId* pId) { + switch (*pId) { + case kErrorExecutable: { + const char* msg = "CodeManager doesn't recognize this executable."; + rt_copy_memory((const voidPtr)msg, *ppMsg, string_length(msg)); + break; } - case kErrorExecutableLib: - { - const char* msg = "CodeManager doesn't recognize this library."; - rt_copy_memory((const voidPtr)msg, *ppMsg, string_length(msg)); - break; + case kErrorExecutableLib: { + const char* msg = "CodeManager doesn't recognize this library."; + rt_copy_memory((const voidPtr)msg, *ppMsg, string_length(msg)); + break; } - case kErrorFileNotFound: - { - const char* msg = "FileManager doesn't find this file."; - rt_copy_memory((const voidPtr)msg, *ppMsg, string_length(msg)); - break; + case kErrorFileNotFound: { + const char* msg = "FileManager doesn't find this file."; + rt_copy_memory((const voidPtr)msg, *ppMsg, string_length(msg)); + break; } - case kErrorNoNetwork: - { - const char* msg = "NetworkManager doesn't detect any WiFi/Ethernet connection."; - rt_copy_memory((const voidPtr)msg, *ppMsg, string_length(msg)); - break; + case kErrorNoNetwork: { + const char* msg = + "NetworkManager doesn't detect any WiFi/Ethernet connection."; + rt_copy_memory((const voidPtr)msg, *ppMsg, string_length(msg)); + break; } default: - break; - } -}
\ No newline at end of file + break; + } +} diff --git a/Private/Source/Defines.cxx b/Private/Source/Defines.cxx index 5b78388f..3e0bd946 100644 --- a/Private/Source/Defines.cxx +++ b/Private/Source/Defines.cxx @@ -8,4 +8,3 @@ */ #include <NewKit/Defines.hpp> - diff --git a/Private/Source/DriveManager.cxx b/Private/Source/DriveManager.cxx index be35e275..ee279ed1 100644 --- a/Private/Source/DriveManager.cxx +++ b/Private/Source/DriveManager.cxx @@ -10,53 +10,43 @@ #include <KernelKit/DebugOutput.hpp> #include <KernelKit/DriveManager.hpp> -namespace hCore -{ -DriveSelector::DriveSelector() : fDrive(nullptr) -{ -} +namespace hCore { +DriveSelector::DriveSelector() : fDrive(nullptr) {} -DriveSelector::~DriveSelector() -{ - if (fDrive) - { - this->Unmount(); - } +DriveSelector::~DriveSelector() { + if (fDrive) { + this->Unmount(); + } } -DriveTraits &DriveSelector::GetMounted() -{ - MUST_PASS(fDrive != nullptr); - return *fDrive; +DriveTraits &DriveSelector::GetMounted() { + MUST_PASS(fDrive != nullptr); + return *fDrive; } -bool DriveSelector::Mount(DriveTraits *drive) -{ - if (drive && drive->fReady() && fDrive == nullptr) - { - fDrive = drive; - fDrive->fMount(); +bool DriveSelector::Mount(DriveTraits *drive) { + if (drive && drive->fReady() && fDrive == nullptr) { + fDrive = drive; + fDrive->fMount(); - kcout << "[Mount] drive: " << fDrive->fName << "\n"; + kcout << "[Mount] drive: " << fDrive->fName << "\n"; - return true; - } + return true; + } - return false; + return false; } -DriveTraits *DriveSelector::Unmount() -{ - if (!fDrive) - return nullptr; +DriveTraits *DriveSelector::Unmount() { + if (!fDrive) return nullptr; - auto drivePointer = fDrive; + auto drivePointer = fDrive; - fDrive->fUnmount(); - fDrive = nullptr; + fDrive->fUnmount(); + fDrive = nullptr; - kcout << "[Unmount] drive: " << drivePointer->fName << "\n"; + kcout << "[Unmount] drive: " << drivePointer->fName << "\n"; - return drivePointer; + return drivePointer; } -} // namespace hCore +} // namespace hCore diff --git a/Private/Source/ErrorOr.cxx b/Private/Source/ErrorOr.cxx index cf6f2317..4137c243 100644 --- a/Private/Source/ErrorOr.cxx +++ b/Private/Source/ErrorOr.cxx @@ -9,4 +9,4 @@ #include <NewKit/ErrorOr.hpp> -/* BUILDME needs that file */
\ No newline at end of file +/* BUILDME needs that file */ diff --git a/Private/Source/FileManager.cxx b/Private/Source/FileManager.cxx index 2a6648ce..9bf26c29 100644 --- a/Private/Source/FileManager.cxx +++ b/Private/Source/FileManager.cxx @@ -13,35 +13,30 @@ //! @brief File manager for hCore. -namespace hCore -{ - static IFilesystemManager* kMounted = nullptr; - - /// @brief FilesystemManager getter. - /// @return The mounted filesystem. - IFilesystemManager* IFilesystemManager::GetMounted() { return kMounted; } - - IFilesystemManager* IFilesystemManager::Unmount() - { - if (kMounted) - { - auto mount = kMounted; - kMounted = nullptr; - - return mount; - } - - return nullptr; - } - - bool IFilesystemManager::Mount(IFilesystemManager* pMount) - { - if (pMount) - { - kMounted = pMount; - return true; - } - - return false; - } +namespace hCore { +static IFilesystemManager* kMounted = nullptr; + +/// @brief FilesystemManager getter. +/// @return The mounted filesystem. +IFilesystemManager* IFilesystemManager::GetMounted() { return kMounted; } + +IFilesystemManager* IFilesystemManager::Unmount() { + if (kMounted) { + auto mount = kMounted; + kMounted = nullptr; + + return mount; + } + + return nullptr; +} + +bool IFilesystemManager::Mount(IFilesystemManager* pMount) { + if (pMount) { + kMounted = pMount; + return true; + } + + return false; } +} // namespace hCore diff --git a/Private/Source/Framebuffer.cxx b/Private/Source/Framebuffer.cxx index 208e90e9..1a73e449 100644 --- a/Private/Source/Framebuffer.cxx +++ b/Private/Source/Framebuffer.cxx @@ -9,31 +9,28 @@ #include <KernelKit/Framebuffer.hpp> -namespace hCore -{ - Framebuffer::Framebuffer(hCore::Ref<FramebufferContext*> &addr) - : m_FrameBufferAddr(addr), m_Colour(FramebufferColorKind::RGB32) - {} +namespace hCore { +Framebuffer::Framebuffer(hCore::Ref<FramebufferContext*>& addr) + : m_FrameBufferAddr(addr), m_Colour(FramebufferColorKind::RGB32) {} - Framebuffer::~Framebuffer() = default; +Framebuffer::~Framebuffer() = default; - volatile UIntPtr* Framebuffer::operator[](const UIntPtr& width_and_height) - { - if (m_FrameBufferAddr) - return reinterpret_cast<volatile hCore::UIntPtr*>(m_FrameBufferAddr->m_Base + width_and_height); +volatile UIntPtr* Framebuffer::operator[](const UIntPtr& width_and_height) { + if (m_FrameBufferAddr) + return reinterpret_cast<volatile hCore::UIntPtr*>( + m_FrameBufferAddr->m_Base + width_and_height); - return nullptr; - } + return nullptr; +} - Ref<FramebufferContext*>& Framebuffer::Leak() { return m_FrameBufferAddr; } +Ref<FramebufferContext*>& Framebuffer::Leak() { return m_FrameBufferAddr; } - Framebuffer::operator bool() { return m_FrameBufferAddr; } +Framebuffer::operator bool() { return m_FrameBufferAddr; } - const FramebufferColorKind& Framebuffer::Color(const FramebufferColorKind& colour) - { - if (colour != FramebufferColorKind::INVALID) - m_Colour = colour; +const FramebufferColorKind& Framebuffer::Color( + const FramebufferColorKind& colour) { + if (colour != FramebufferColorKind::INVALID) m_Colour = colour; - return m_Colour; - } -} // namespace hCore + return m_Colour; +} +} // namespace hCore diff --git a/Private/Source/GUIDWizard.cxx b/Private/Source/GUIDWizard.cxx index 25875671..d3b11b0a 100644 --- a/Private/Source/GUIDWizard.cxx +++ b/Private/Source/GUIDWizard.cxx @@ -16,49 +16,42 @@ // @brief Size of UUID. #define kUUIDSize 32 -namespace hCore::XRN::Version1 -{ - auto make_sequence(const ArrayList<UShort>& uuidSeq) -> Ref<GUIDSequence*> - { - GUIDSequence *seq = new GUIDSequence(); - MUST_PASS(seq); +namespace hCore::XRN::Version1 { +auto make_sequence(const ArrayList<UShort>& uuidSeq) -> Ref<GUIDSequence*> { + GUIDSequence* seq = new GUIDSequence(); + MUST_PASS(seq); - Ref<GUIDSequence*> sequenceReference{seq, true}; + Ref<GUIDSequence*> sequenceReference{seq, true}; - sequenceReference->m_Ms1 |= uuidSeq[0]; - sequenceReference->m_Ms2 |= uuidSeq[1]; - sequenceReference->m_Ms3 |= uuidSeq[2]; - sequenceReference->m_Ms3 |= uuidSeq[3]; + sequenceReference->m_Ms1 |= uuidSeq[0]; + sequenceReference->m_Ms2 |= uuidSeq[1]; + sequenceReference->m_Ms3 |= uuidSeq[2]; + sequenceReference->m_Ms3 |= uuidSeq[3]; - return sequenceReference; - } + return sequenceReference; +} - // @brief Tries to make a guid out of a string. - // This function is not complete for now - auto try_guid_to_string(Ref<GUIDSequence*>& seq) -> ErrorOr <Ref<StringView>> - { - Char buf[kUUIDSize]; +// @brief Tries to make a guid out of a string. +// This function is not complete for now +auto try_guid_to_string(Ref<GUIDSequence*>& seq) -> ErrorOr<Ref<StringView>> { + Char buf[kUUIDSize]; - for (SizeT index = 0; index < 16; ++index) - { - buf[index] = seq->u8[index] + kAsciiBegin; - } + for (SizeT index = 0; index < 16; ++index) { + buf[index] = seq->u8[index] + kAsciiBegin; + } - for (SizeT index = 16; index < 24; ++index) - { - buf[index] = seq->u16[index] + kAsciiBegin; - } + for (SizeT index = 16; index < 24; ++index) { + buf[index] = seq->u16[index] + kAsciiBegin; + } - for (SizeT index = 24; index < 28; ++index) - { - buf[index] = seq->u32[index] + kAsciiBegin; - } + for (SizeT index = 24; index < 28; ++index) { + buf[index] = seq->u32[index] + kAsciiBegin; + } - auto view = StringBuilder::Construct(buf); + auto view = StringBuilder::Construct(buf); - if (view) - return ErrorOr<Ref<StringView>>{ view.Leak() }; + if (view) return ErrorOr<Ref<StringView>>{view.Leak()}; - return ErrorOr<Ref<StringView>>{-1}; - } -} // namespace hCore::XRN::Version1 + return ErrorOr<Ref<StringView>>{-1}; +} +} // namespace hCore::XRN::Version1 diff --git a/Private/Source/GUIDWrapper.cxx b/Private/Source/GUIDWrapper.cxx index 3ae42eeb..fa9a52a6 100644 --- a/Private/Source/GUIDWrapper.cxx +++ b/Private/Source/GUIDWrapper.cxx @@ -9,7 +9,4 @@ #include <CFKit/GUIDWrapper.hpp> -namespace hCore::XRN -{ - -} +namespace hCore::XRN {} diff --git a/Private/Source/Heap.cxx b/Private/Source/Heap.cxx deleted file mode 100644 index ea667a5c..00000000 --- a/Private/Source/Heap.cxx +++ /dev/null @@ -1,208 +0,0 @@ -/* - * ======================================================== - * - * hCore - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include <NewKit/Heap.hpp> -#include <NewKit/PageManager.hpp> - -/// @file Heap.cxx -/// @brief hCore Process Heap Manager -/// @note if you want to look at kernel allocs, please look for KHeap.cxx -/// bugs: 0 - -namespace hCore -{ - class HeapManager final - { - public: - static SizeT& GetCount() { return s_NumPools; } - static Ref<Pmm>& GetPmm() { return s_Pmm; } - static Boolean& IsEnabled() { return s_PoolsAreEnabled; } - static Array<Ref<PTEWrapper*>, kPoolMaxSz>& The() { return s_Pool; } - - private: - static Size s_NumPools; - static Ref<Pmm> s_Pmm; - - private: - static Boolean s_PoolsAreEnabled; - static Array<Ref<PTEWrapper*>, kPoolMaxSz> s_Pool; - - }; - - //! declare fields - - SizeT HeapManager::s_NumPools = 0UL; - Ref<Pmm> HeapManager::s_Pmm; - Boolean HeapManager::s_PoolsAreEnabled = true; - Array<Ref<PTEWrapper*>, kPoolMaxSz> HeapManager::s_Pool; - - static voidPtr rt_find_unused_heap(Int flags); - static void rt_free_heap_internal(voidPtr vaddr); - static voidPtr rt_make_heap(voidPtr vaddr, Int flags); - static bool rt_check_and_free_heap(const SizeT &index, voidPtr ptr); - - static voidPtr rt_find_unused_heap(Int flags) - { - for (SizeT index = 0; index < kPoolMaxSz; ++index) - { - if (HeapManager::The()[index] && - !HeapManager::The()[index].Leak().Leak().Leak()->Present()) - { - HeapManager::GetPmm().Leak().TogglePresent(HeapManager::The()[index].Leak().Leak(), true); - kcout << "[rt_find_unused_heap] Done, trying now to make a pool\r\n"; - - return rt_make_heap((voidPtr)HeapManager::The()[index].Leak().Leak().Leak()->VirtualAddress(), flags); - } - } - - return nullptr; - } - - static voidPtr rt_make_heap(voidPtr virtualAddress, Int flags) - { - if (virtualAddress) - { - HeapHeader* pool_hdr = reinterpret_cast<HeapHeader*>(virtualAddress); - - if (!pool_hdr->Free) - { - kcout << "[rt_make_heap] pool_hdr->Free, Pool already exists\n"; - return nullptr; - } - - pool_hdr->Flags = flags; - pool_hdr->Magic = kPoolMag; - pool_hdr->Free = false; - - kcout << "[rt_make_heap] New allocation has been done.\n"; - return reinterpret_cast<voidPtr>((reinterpret_cast<UIntPtr>(virtualAddress) + sizeof(HeapHeader))); - } - - kcout << "[rt_make_heap] Address is invalid"; - return nullptr; - } - - static void rt_free_heap_internal(voidPtr virtualAddress) - { - HeapHeader* pool_hdr = reinterpret_cast<HeapHeader*>(reinterpret_cast<UIntPtr>(virtualAddress) - sizeof(HeapHeader)); - - if (pool_hdr->Magic == kPoolMag) - { - pool_hdr->Free = false; - pool_hdr->Flags = 0; - - kcout << "[rt_free_heap_internal] Successfully marked header as free!\r\n"; - } - } - - static bool rt_check_and_free_heap(const SizeT& index, voidPtr ptr) - { - if (HeapManager::The()[index]) - { - // ErrorOr<>::operator bool - if (!HeapManager::The()[index].Leak().Leak().IsStrong()) - { - // we want them to be weak - // because we allocated it. - if (HeapManager::The()[index].Leak().Leak().Leak()->VirtualAddress() == (UIntPtr) ptr) - { - HeapManager::GetPmm().Leak().FreePage(HeapManager::The()[index].Leak().Leak()); - --HeapManager::GetCount(); - - rt_free_heap_internal(ptr); - ptr = nullptr; - - return true; - } - } - } - - return false; - } - - /// @brief Creates a new pool pointer. - /// @param flags the flags attached to it. - /// @return a pool pointer with selected permissions. - voidPtr pool_new_ptr(Int32 flags) - { - if (!HeapManager::IsEnabled()) - return nullptr; - - if (HeapManager::GetCount() > kPoolMaxSz) - return nullptr; - - if (voidPtr ret = rt_find_unused_heap(flags)) - return ret; - - // this wasn't set to true - auto ref_page = HeapManager::GetPmm().Leak().RequestPage(((flags & kPoolUser)), - (flags & kPoolRw)); - if (ref_page) - { - ///! reserve page. - HeapManager::The()[HeapManager::GetCount()].Leak() = ref_page; - auto& ref = HeapManager::GetCount(); - ++ref; // increment the number of addresses we have now. - - kcout << "[pool_new_ptr] New Address found!\r\n"; - - // finally make the pool address. - return rt_make_heap(reinterpret_cast<voidPtr>(ref_page.Leak()->VirtualAddress()), flags); - } - - return nullptr; - } - - /// @brief free a pool pointer. - /// @param ptr The pool pointer to free. - /// @return status code - Int32 pool_free_ptr(voidPtr ptr) - { - if (!HeapManager::IsEnabled()) - return -1; - - if (ptr) - { - SizeT base = HeapManager::GetCount(); - - if (rt_check_and_free_heap(base, ptr)) - return 0; - - for (SizeT index = 0; index < kPoolMaxSz; ++index) - { - if (rt_check_and_free_heap(index, ptr)) - return 0; - - --base; - } - } - - return -1; - } - - /// @brief Checks if pointer is valid. - /// @param thePool the pool pointer. - /// @param thePtr the pointer. - /// @param theLimit the last address of the pool. - /// @return if it is valid. - Boolean pool_ptr_exists(UIntPtr thePool, UIntPtr thePtr, SizeT theLimit) - { - if (HeapManager::GetCount() < 1) - return false; - - if (thePool == 0 || - thePtr == 0 || - theLimit == 0) - { - return false; - } - - return ((thePool) < (thePtr) < (theLimit)); - } -} // namespace hCore diff --git a/Private/Source/IndexableProperty.cxx b/Private/Source/IndexableProperty.cxx index 5b49d6c1..2dac5a09 100644 --- a/Private/Source/IndexableProperty.cxx +++ b/Private/Source/IndexableProperty.cxx @@ -9,20 +9,18 @@ //! @brief hCore NewFS Indexer. -#include <FSKit/IndexableProperty.hxx> #include <CompilerKit/Compiler.hpp> +#include <FSKit/IndexableProperty.hxx> #include <NewKit/MutableArray.hpp> #include <NewKit/Utils.hpp> #define kMaxLenIndexer 256 -namespace hCore -{ - namespace Indexer - { - IndexProperty& IndexableProperty::LeakProperty() noexcept { return fIndex; } +namespace hCore { +namespace Indexer { +IndexProperty& IndexableProperty::LeakProperty() noexcept { return fIndex; } - void IndexableProperty::AddFlag(Int16 flag) { fFlags |= flag; } - void IndexableProperty::RemoveFlag(Int16 flag) { fFlags &= flag; } - } -} +void IndexableProperty::AddFlag(Int16 flag) { fFlags |= flag; } +void IndexableProperty::RemoveFlag(Int16 flag) { fFlags &= flag; } +} // namespace Indexer +} // namespace hCore diff --git a/Private/Source/KHeap.cxx b/Private/Source/KHeap.cxx deleted file mode 100644 index c36c6ad7..00000000 --- a/Private/Source/KHeap.cxx +++ /dev/null @@ -1,126 +0,0 @@ -/* - * ======================================================== - * - * hCore - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include <NewKit/KHeap.hpp> - -//! @file KHeap.cpp -//! @brief this allocator is used by kernel to allocate pages. - -#define kMaxWrappers (4096 * 8) - -namespace hCore -{ -static Ref<PTEWrapper *> kWrapperList[kMaxWrappers]; -static SizeT kWrapperCount = 0UL; -static Ref<PTEWrapper *> kLastWrapper; -static Pmm kPmm; - -namespace Detail -{ -static voidPtr find_ptr(const SizeT &sz, const bool rw, const bool user) -{ - for (SizeT indexWrapper = 0; indexWrapper < kMaxWrappers; ++indexWrapper) - { - if (!kWrapperList[indexWrapper]->Present()) - { - kWrapperList[indexWrapper]->Reclaim(); /* very straight-forward as you can see. */ - return reinterpret_cast<voidPtr>(kWrapperList[indexWrapper]->VirtualAddress()); - } - } - - return nullptr; -} -} // namespace Detail - -/// @brief manual allocation -/// @param sz size of pointer -/// @param rw read write (true to enable it) -/// @param user is it accesible by user processes? -/// @return the pointer -VoidPtr kernel_new_ptr(const SizeT &sz, const bool rw, const bool user) -{ - if (kWrapperCount < sz) - return nullptr; - - if (auto ptr = Detail::find_ptr(sz, rw, user); ptr) - return ptr; - - Ref<PTEWrapper *> wrapper = kPmm.RequestPage(user, rw); - - if (wrapper) - { - kLastWrapper = wrapper; - - kWrapperList[kWrapperCount] = wrapper; - ++kWrapperCount; - - return reinterpret_cast<voidPtr>(wrapper->VirtualAddress()); - } - - return nullptr; -} - -/// @brief Declare pointer as free. -/// @param ptr the pointer. -/// @return -Int32 kernel_delete_ptr(voidPtr ptr) -{ - if (ptr) - { - const UIntPtr virtualAddress = reinterpret_cast<UIntPtr>(ptr); - - if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) - { - return kPmm.FreePage(kLastWrapper); - } - - Ref<PTEWrapper *> wrapper; - - for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) - { - if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) - { - wrapper = kWrapperList[indexWrapper]; - return kPmm.FreePage(wrapper); - } - } - } - - return -1; -} - -/// @brief find pointer in kernel heap -/// @param ptr the pointer -/// @return if it exists. -Boolean kernel_valid_ptr(voidPtr ptr) -{ - if (ptr) - { - const UIntPtr virtualAddress = reinterpret_cast<UIntPtr>(ptr); - - if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) - { - return true; - } - - Ref<PTEWrapper *> wrapper; - - for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) - { - if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) - { - wrapper = kWrapperList[indexWrapper]; - return true; - } - } - } - - return false; -} -} // namespace hCore diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx new file mode 100644 index 00000000..02688a2f --- /dev/null +++ b/Private/Source/KernelHeap.cxx @@ -0,0 +1,109 @@ +/* + * ======================================================== + * + * hCore + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include <NewKit/KernelHeap.hpp> + +//! @file KernelHeap.cpp +//! @brief Kernel allocator. + +#define kMaxWrappers (4096 * 8) + +namespace hCore { +static Ref<PTEWrapper *> kWrapperList[kMaxWrappers]; +static SizeT kWrapperCount = 0UL; +static Ref<PTEWrapper *> kLastWrapper; +static Pmm kPmm; + +namespace Detail { +static voidPtr find_ptr(const SizeT &sz, const bool rw, const bool user) { + for (SizeT indexWrapper = 0; indexWrapper < kMaxWrappers; ++indexWrapper) { + if (!kWrapperList[indexWrapper]->Present()) { + kWrapperList[indexWrapper] + ->Reclaim(); /* very straight-forward as you can see. */ + return reinterpret_cast<voidPtr>( + kWrapperList[indexWrapper]->VirtualAddress()); + } + } + + return nullptr; +} +} // namespace Detail + +/// @brief manual allocation +/// @param sz size of pointer +/// @param rw read write (true to enable it) +/// @param user is it accesible by user processes? +/// @return the pointer +VoidPtr kernel_new_ptr(const SizeT &sz, const bool rw, const bool user) { + if (kWrapperCount < sz) return nullptr; + + if (auto ptr = Detail::find_ptr(sz, rw, user); ptr) return ptr; + + Ref<PTEWrapper *> wrapper = kPmm.RequestPage(user, rw); + + if (wrapper) { + kLastWrapper = wrapper; + + kWrapperList[kWrapperCount] = wrapper; + ++kWrapperCount; + + return reinterpret_cast<voidPtr>(wrapper->VirtualAddress()); + } + + return nullptr; +} + +/// @brief Declare pointer as free. +/// @param ptr the pointer. +/// @return +Int32 kernel_delete_ptr(voidPtr ptr) { + if (ptr) { + const UIntPtr virtualAddress = reinterpret_cast<UIntPtr>(ptr); + + if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) { + return kPmm.FreePage(kLastWrapper); + } + + Ref<PTEWrapper *> wrapper; + + for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) { + if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) { + wrapper = kWrapperList[indexWrapper]; + return kPmm.FreePage(wrapper); + } + } + } + + return -1; +} + +/// @brief find pointer in kernel heap +/// @param ptr the pointer +/// @return if it exists. +Boolean kernel_valid_ptr(voidPtr ptr) { + if (ptr) { + const UIntPtr virtualAddress = reinterpret_cast<UIntPtr>(ptr); + + if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) { + return true; + } + + Ref<PTEWrapper *> wrapper; + + for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) { + if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) { + wrapper = kWrapperList[indexWrapper]; + return true; + } + } + } + + return false; +} +} // namespace hCore diff --git a/Private/Source/LockDelegate.cxx b/Private/Source/LockDelegate.cxx index 7bc742ec..d6608e60 100644 --- a/Private/Source/LockDelegate.cxx +++ b/Private/Source/LockDelegate.cxx @@ -7,4 +7,4 @@ * ======================================================== */ -#include <NewKit/LockDelegate.hpp>
\ No newline at end of file +#include <NewKit/LockDelegate.hpp> diff --git a/Private/Source/Network/IP.cpp b/Private/Source/Network/IP.cpp index ba6fd71f..4e8f602a 100644 --- a/Private/Source/Network/IP.cpp +++ b/Private/Source/Network/IP.cpp @@ -10,118 +10,86 @@ #include <NetworkKit/IP.hpp> #include <NewKit/Utils.hpp> -namespace hCore -{ - char* RawIPAddress::Address() - { - return m_Addr; - } +namespace hCore { +char* RawIPAddress::Address() { return m_Addr; } - RawIPAddress::RawIPAddress(char bytes[4]) - { - rt_copy_memory(bytes, m_Addr, 4); - } +RawIPAddress::RawIPAddress(char bytes[4]) { rt_copy_memory(bytes, m_Addr, 4); } - bool RawIPAddress::operator==(const RawIPAddress& ipv4) - { - for (Size index = 0; index < 4; ++index) - { - if (ipv4.m_Addr[index] != m_Addr[index]) - return false; - } +bool RawIPAddress::operator==(const RawIPAddress& ipv4) { + for (Size index = 0; index < 4; ++index) { + if (ipv4.m_Addr[index] != m_Addr[index]) return false; + } - return true; - } + return true; +} - bool RawIPAddress::operator!=(const RawIPAddress& ipv4) - { - for (Size index = 0; index < 4; ++index) - { - if (ipv4.m_Addr[index] == m_Addr[index]) - return false; - } +bool RawIPAddress::operator!=(const RawIPAddress& ipv4) { + for (Size index = 0; index < 4; ++index) { + if (ipv4.m_Addr[index] == m_Addr[index]) return false; + } - return true; - } + return true; +} - char& RawIPAddress::operator[](const Size& index) - { - kcout << "[RawIPAddress::operator[]] Fetching Index...\r\n"; +char& RawIPAddress::operator[](const Size& index) { + kcout << "[RawIPAddress::operator[]] Fetching Index...\r\n"; - if (index > 4) - panic(RUNTIME_CHECK_EXPRESSION); + if (index > 4) panic(RUNTIME_CHECK_EXPRESSION); - return m_Addr[index]; - } + return m_Addr[index]; +} - RawIPAddress6::RawIPAddress6(char bytes[8]) - { - rt_copy_memory(bytes, m_Addr, 8); - } +RawIPAddress6::RawIPAddress6(char bytes[8]) { + rt_copy_memory(bytes, m_Addr, 8); +} - char &RawIPAddress6::operator[](const Size &index) - { - kcout << "[RawIPAddress6::operator[]] Fetching Index...\r\n"; +char& RawIPAddress6::operator[](const Size& index) { + kcout << "[RawIPAddress6::operator[]] Fetching Index...\r\n"; - if (index > 8) - panic(RUNTIME_CHECK_EXPRESSION); + if (index > 8) panic(RUNTIME_CHECK_EXPRESSION); - return m_Addr[index]; - } + return m_Addr[index]; +} - bool RawIPAddress6::operator==(const RawIPAddress6& ipv6) - { - for (SizeT index = 0; index < 8; ++index) - { - if (ipv6.m_Addr[index] != m_Addr[index]) - return false; - } +bool RawIPAddress6::operator==(const RawIPAddress6& ipv6) { + for (SizeT index = 0; index < 8; ++index) { + if (ipv6.m_Addr[index] != m_Addr[index]) return false; + } - return true; - } + return true; +} - bool RawIPAddress6::operator!=(const RawIPAddress6& ipv6) - { - for (SizeT index = 0; index < 8; ++index) - { - if (ipv6.m_Addr[index] == m_Addr[index]) - return false; - } +bool RawIPAddress6::operator!=(const RawIPAddress6& ipv6) { + for (SizeT index = 0; index < 8; ++index) { + if (ipv6.m_Addr[index] == m_Addr[index]) return false; + } - return true; - } + return true; +} - ErrorOr<StringView> IPFactory::ToStringView(Ref<RawIPAddress6> ipv6) - { - auto str = StringBuilder::Construct(ipv6.Leak().Address()); - return str; - } +ErrorOr<StringView> IPFactory::ToStringView(Ref<RawIPAddress6> ipv6) { + auto str = StringBuilder::Construct(ipv6.Leak().Address()); + return str; +} - ErrorOr<StringView> IPFactory::ToStringView(Ref<RawIPAddress> ipv4) - { - auto str = StringBuilder::Construct(ipv4.Leak().Address()); - return str; - } +ErrorOr<StringView> IPFactory::ToStringView(Ref<RawIPAddress> ipv4) { + auto str = StringBuilder::Construct(ipv4.Leak().Address()); + return str; +} - bool IPFactory::IpCheckVersion4(const char *ip) - { - int cnter = 0; - - for (Size base = 0; base < string_length(ip); ++base) - { - if (ip[base] == '.') - { - cnter = 0; - } - else - { - if (cnter == 3) - return false; - - ++cnter; - } - } - - return true; +bool IPFactory::IpCheckVersion4(const char* ip) { + int cnter = 0; + + for (Size base = 0; base < string_length(ip); ++base) { + if (ip[base] == '.') { + cnter = 0; + } else { + if (cnter == 3) return false; + + ++cnter; } -} // namespace NewKit + } + + return true; +} +} // namespace hCore diff --git a/Private/Source/Network/NetworkDevice.cpp b/Private/Source/Network/NetworkDevice.cpp index 766e1f2e..bd75d7ed 100644 --- a/Private/Source/Network/NetworkDevice.cpp +++ b/Private/Source/Network/NetworkDevice.cpp @@ -12,23 +12,21 @@ // network devices implementation. // PPPNetworkService, TCPNetworkDevice, UDPNetworkService -namespace hCore -{ - NetworkDevice::NetworkDevice(void (*out)(NetworkDeviceCommand), void (*in)(NetworkDeviceCommand), void(*on_cleanup)(void)) - : DeviceInterface<NetworkDeviceCommand>(out, in), fCleanup(on_cleanup) - { +namespace hCore { +NetworkDevice::NetworkDevice(void (*out)(NetworkDeviceCommand), + void (*in)(NetworkDeviceCommand), + void (*on_cleanup)(void)) + : DeviceInterface<NetworkDeviceCommand>(out, in), fCleanup(on_cleanup) { #ifdef __DEBUG__ - kcout << "NetworkDevice init.\r\n"; + kcout << "NetworkDevice init.\r\n"; #endif - } +} - NetworkDevice::~NetworkDevice() - { +NetworkDevice::~NetworkDevice() { #ifdef __DEBUG__ - kcout << "NetworkDevice cleanup.\r\n"; + kcout << "NetworkDevice cleanup.\r\n"; #endif - if (fCleanup) - fCleanup(); - } -} // namespace NewKit + if (fCleanup) fCleanup(); +} +} // namespace hCore diff --git a/Private/Source/New+Delete.cxx b/Private/Source/New+Delete.cxx index ff2bf8bb..0e247e0c 100644 --- a/Private/Source/New+Delete.cxx +++ b/Private/Source/New+Delete.cxx @@ -7,7 +7,7 @@ * ======================================================== */ -#include <NewKit/KHeap.hpp> +#include <NewKit/KernelHeap.hpp> #include <NewKit/New.hpp> void* operator new[](size_t sz) diff --git a/Private/Source/NewFS-Journal.cxx b/Private/Source/NewFS-Journal.cxx index 9c96928d..14ba417f 100644 --- a/Private/Source/NewFS-Journal.cxx +++ b/Private/Source/NewFS-Journal.cxx @@ -14,54 +14,46 @@ #define kOpCache (4) -namespace hCore -{ - typedef Boolean(*NewFSRunner)(VoidPtr delegate); - - class NewFSJournalRunner final - { - public: - NewFSRunner fLoadRoutine{ nullptr }; - NewFSRunner fCacheRoutine{ nullptr }; - NewFSRunner fUnloadRoutine{ nullptr }; - - explicit NewFSJournalRunner(NewFSRunner load_runner) - : fLoadRoutine(load_runner) - { - MUST_PASS(fLoadRoutine); - - fLoadRoutine(this); - } - - ~NewFSJournalRunner() noexcept - { - MUST_PASS(fUnloadRoutine); - - fUnloadRoutine(this); - } - - HCORE_COPY_DEFAULT(NewFSJournalRunner); - - public: - Boolean Run(const Int32& operation, VoidPtr class_ptr) - { - switch (operation) - { - case kOpCache: - { - if (!class_ptr) - { - kcout << "Miss for class_ptr at NewFSJournalManager::Run(class_ptr) " << __FILE__ << "\n"; - return false; - } +namespace hCore { +typedef Boolean (*NewFSRunner)(VoidPtr delegate); - MUST_PASS(fCacheRoutine); - return fCacheRoutine(class_ptr); - } - }; - - return false; - } - +class NewFSJournalRunner final { + public: + NewFSRunner fLoadRoutine{nullptr}; + NewFSRunner fCacheRoutine{nullptr}; + NewFSRunner fUnloadRoutine{nullptr}; + + explicit NewFSJournalRunner(NewFSRunner load_runner) + : fLoadRoutine(load_runner) { + MUST_PASS(fLoadRoutine); + + fLoadRoutine(this); + } + + ~NewFSJournalRunner() noexcept { + MUST_PASS(fUnloadRoutine); + + fUnloadRoutine(this); + } + + HCORE_COPY_DEFAULT(NewFSJournalRunner); + + public: + Boolean Run(const Int32& operation, VoidPtr class_ptr) { + switch (operation) { + case kOpCache: { + if (!class_ptr) { + kcout << "Miss for class_ptr at NewFSJournalManager::Run(class_ptr) " + << __FILE__ << "\n"; + return false; + } + + MUST_PASS(fCacheRoutine); + return fCacheRoutine(class_ptr); + } }; -} // namespace hCore + + return false; + } +}; +} // namespace hCore diff --git a/Private/Source/NewFS.cxx b/Private/Source/NewFS.cxx index cd787e9b..2283e779 100644 --- a/Private/Source/NewFS.cxx +++ b/Private/Source/NewFS.cxx @@ -10,42 +10,34 @@ #include <FSKit/NewFS.hxx> #include <KernelKit/FileManager.hpp> -namespace hCore -{ - NewFilesystemManager::NewFilesystemManager() = default; - - NewFilesystemManager::~NewFilesystemManager() = default; - - /** - * Unallocates a file from disk. - * @param node_name it's path. - * @return operation status boolean. - */ - bool NewFilesystemManager::Remove(const char* node_name) - { - if (node_name == nullptr || - *node_name == 0) - return false; - - if (auto catalog = fIO->GetCatalog(node_name); - catalog) - return fIO->RemoveCatalog(*catalog); - - return false; - } - - NodePtr NewFilesystemManager::Create(const char* path) - { - return node_cast(fIO->CreateCatalog(path, 0, kCatalogKindFile)); - } - - NodePtr NewFilesystemManager::CreateDirectory(const char *path) - { - return node_cast(fIO->CreateCatalog(path, 0, kCatalogKindDir)); - } - - NodePtr NewFilesystemManager::CreateAlias(const char *path) - { - return node_cast(fIO->CreateCatalog(path, 0, kCatalogKindAlias)); - } -} // namespace hCore +namespace hCore { +NewFilesystemManager::NewFilesystemManager() = default; + +NewFilesystemManager::~NewFilesystemManager() = default; + +/** + * Unallocates a file from disk. + * @param node_name it's path. + * @return operation status boolean. + */ +bool NewFilesystemManager::Remove(const char* node_name) { + if (node_name == nullptr || *node_name == 0) return false; + + if (auto catalog = fIO->GetCatalog(node_name); catalog) + return fIO->RemoveCatalog(*catalog); + + return false; +} + +NodePtr NewFilesystemManager::Create(const char* path) { + return node_cast(fIO->CreateCatalog(path, 0, kCatalogKindFile)); +} + +NodePtr NewFilesystemManager::CreateDirectory(const char* path) { + return node_cast(fIO->CreateCatalog(path, 0, kCatalogKindDir)); +} + +NodePtr NewFilesystemManager::CreateAlias(const char* path) { + return node_cast(fIO->CreateCatalog(path, 0, kCatalogKindAlias)); +} +} // namespace hCore diff --git a/Private/Source/OSErr.cxx b/Private/Source/OSErr.cxx index 810b77b7..6c37c40d 100644 --- a/Private/Source/OSErr.cxx +++ b/Private/Source/OSErr.cxx @@ -8,4 +8,3 @@ */ #include <KernelKit/OSErr.hpp> - diff --git a/Private/Source/OwnPtr.cxx b/Private/Source/OwnPtr.cxx index 79a9a368..f7f9d31f 100644 --- a/Private/Source/OwnPtr.cxx +++ b/Private/Source/OwnPtr.cxx @@ -7,4 +7,4 @@ * ======================================================== */ -#include <NewKit/OwnPtr.hpp>
\ No newline at end of file +#include <NewKit/OwnPtr.hpp> diff --git a/Private/Source/PRDT.cxx b/Private/Source/PRDT.cxx index 7e74c200..8ea2ee70 100644 --- a/Private/Source/PRDT.cxx +++ b/Private/Source/PRDT.cxx @@ -8,42 +8,28 @@ */ #include <KernelKit/DebugOutput.hpp> -#include <StorageKit/PRDT.hpp> #include <NewKit/String.hpp> +#include <StorageKit/PRDT.hpp> -namespace hCore -{ - PRDT::PRDT(const UIntPtr &physAddr) - : m_PrdtAddr(physAddr) - { - MUST_PASS(physAddr); - kcout << "PRDT::PRDT() {}\r\n"; - } +namespace hCore { +PRDT::PRDT(const UIntPtr& physAddr) : m_PrdtAddr(physAddr) { + MUST_PASS(physAddr); + kcout << "PRDT::PRDT() {}\r\n"; +} - PRDT::~PRDT() - { - kcout << "PRDT::~PRDT() {}\r\n"; - m_PrdtAddr = 0; - } +PRDT::~PRDT() { + kcout << "PRDT::~PRDT() {}\r\n"; + m_PrdtAddr = 0; +} - const UInt& PRDT::Low() - { - return m_Low; - } +const UInt& PRDT::Low() { return m_Low; } - const UShort& PRDT::High() - { - return m_High; - } +const UShort& PRDT::High() { return m_High; } - const UIntPtr& PRDT::PhysicalAddress() - { - return m_PrdtAddr; - } +const UIntPtr& PRDT::PhysicalAddress() { return m_PrdtAddr; } - PRDT& PRDT::operator=(const UIntPtr& prdtAddress) - { - m_PrdtAddr = prdtAddress; - return *this; - } -} // namespace hCore +PRDT& PRDT::operator=(const UIntPtr& prdtAddress) { + m_PrdtAddr = prdtAddress; + return *this; +} +} // namespace hCore diff --git a/Private/Source/PageAllocator.cxx b/Private/Source/PageAllocator.cxx index e29710dd..59ad6b28 100644 --- a/Private/Source/PageAllocator.cxx +++ b/Private/Source/PageAllocator.cxx @@ -12,45 +12,39 @@ #include <NewKit/PageAllocator.hpp> // empty for now. -namespace hCore::Detail -{ - UIntPtr create_page_wrapper(Boolean rw, Boolean user) - { - auto addr = HAL::hal_create_page(rw, user); - - if (addr == kBadAddress) - { - kcout << "[create_page_wrapper] kBadAddress returned\n"; - panic(RUNTIME_CHECK_POINTER); - } - - return addr; - } - - void exec_disable(UIntPtr VirtualAddr) - { - PTE *VirtualAddrTable = reinterpret_cast<PTE*>(VirtualAddr); - MUST_PASS(!VirtualAddrTable->Accessed); - VirtualAddrTable->ExecDisable = true; - - flush_tlb(VirtualAddr); - } - - bool page_disable(UIntPtr VirtualAddr) - { - if (VirtualAddr) { - auto VirtualAddrTable = (PTE * )(VirtualAddr); - MUST_PASS(!VirtualAddrTable->Accessed); - - if (VirtualAddrTable->Accessed) - return false; - VirtualAddrTable->Present = false; - - flush_tlb(VirtualAddr); - - return true; - } - - return false; - } -} // namespace hCore::Detail +namespace hCore::Detail { +UIntPtr create_page_wrapper(Boolean rw, Boolean user) { + auto addr = HAL::hal_create_page(rw, user); + + if (addr == kBadAddress) { + kcout << "[create_page_wrapper] kBadAddress returned\n"; + panic(RUNTIME_CHECK_POINTER); + } + + return addr; +} + +void exec_disable(UIntPtr VirtualAddr) { + PTE *VirtualAddrTable = reinterpret_cast<PTE *>(VirtualAddr); + MUST_PASS(!VirtualAddrTable->Accessed); + VirtualAddrTable->ExecDisable = true; + + flush_tlb(VirtualAddr); +} + +bool page_disable(UIntPtr VirtualAddr) { + if (VirtualAddr) { + auto VirtualAddrTable = (PTE *)(VirtualAddr); + MUST_PASS(!VirtualAddrTable->Accessed); + + if (VirtualAddrTable->Accessed) return false; + VirtualAddrTable->Present = false; + + flush_tlb(VirtualAddr); + + return true; + } + + return false; +} +} // namespace hCore::Detail diff --git a/Private/Source/PageManager.cxx b/Private/Source/PageManager.cxx index fc6b8e80..32c1f22a 100644 --- a/Private/Source/PageManager.cxx +++ b/Private/Source/PageManager.cxx @@ -10,137 +10,114 @@ #include <KernelKit/DebugOutput.hpp> #include <NewKit/PageManager.hpp> -namespace hCore -{ - PTEWrapper::PTEWrapper(Boolean Rw, Boolean User, Boolean ExecDisable, UIntPtr VirtAddr) - : m_Rw(Rw), - m_User(User), - m_ExecDisable(ExecDisable), - m_VirtAddr(VirtAddr), - m_Cache(false), - m_Shareable(false), - m_Wt(false), - m_Present(true), - m_Accessed(false) - { - // special case for the null region. - if (VirtAddr == 0) - { - m_Wt = false; - m_Rw = false; - m_Cache = false; - m_Shareable = false; - } - } - - PTEWrapper::~PTEWrapper() - { - PTE* raw = reinterpret_cast<PTE*>(m_VirtAddr); - - MUST_PASS(raw); - MUST_PASS(!raw->Accessed); - - if (raw->Present) - raw->Present = false; - } - - void PTEWrapper::FlushTLB(Ref<PageManager> &pm) - { - pm.Leak().FlushTLB(this->m_VirtAddr); - } - - void PageManager::FlushTLB(UIntPtr VirtAddr) - { - if (VirtAddr == kBadAddress) - return; - - flush_tlb(VirtAddr); - } - - bool PTEWrapper::Reclaim() - { - if (!this->m_Present) - { - this->m_Present = true; - return true; - } - - return false; - } - - PTEWrapper *PageManager::Request(Boolean Rw, Boolean User, Boolean ExecDisable) - { - PTEWrapper *PageTableEntry = - reinterpret_cast<PTEWrapper*>(hCore::HAL::hal_alloc_page(sizeof(PTEWrapper), Rw, User)); - - if (PageTableEntry == nullptr) { - kcout << "PTEWrapper : Page table is nullptr!, kernel_new_ptr failed!"; - return nullptr; - } - - *PageTableEntry = PTEWrapper{Rw, User, ExecDisable, Detail::create_page_wrapper(Rw, User)}; - return PageTableEntry; - } - - bool PageManager::Free(Ref<PTEWrapper*> &wrapper) - { - if (wrapper) { - if (!Detail::page_disable(wrapper->VirtualAddress())) - return false; - - this->FlushTLB(wrapper->VirtualAddress()); - return true; - } - - return false; - } - - const UIntPtr &PTEWrapper::VirtualAddress() - { - return m_VirtAddr; - } - - bool PTEWrapper::Shareable() - { - auto raw = reinterpret_cast<PTE*>(m_VirtAddr); - - if (raw->Present) - { - m_Shareable = raw->Shared; - return m_Shareable; - } - else - { - kcout << "[PTEWrapper::Shareable] page is not present!"; - return false; - } - } - - bool PTEWrapper::Present() - { - auto raw = reinterpret_cast<PTE*>(m_VirtAddr); - - if (raw->Present) - { - m_Present = raw->Present; - return m_Present; - } - else - { - kcout << "[PTEWrapper::Present] page is not present!"; - return false; - } - } - - bool PTEWrapper::Access() - { - auto raw = reinterpret_cast<PTE*>(m_VirtAddr); - - if (raw->Present) - { - m_Accessed = raw->Accessed; - } - - return m_Accessed; - } -} // namespace hCore +namespace hCore { +PTEWrapper::PTEWrapper(Boolean Rw, Boolean User, Boolean ExecDisable, + UIntPtr VirtAddr) + : m_Rw(Rw), + m_User(User), + m_ExecDisable(ExecDisable), + m_VirtAddr(VirtAddr), + m_Cache(false), + m_Shareable(false), + m_Wt(false), + m_Present(true), + m_Accessed(false) { + // special case for the null region. + if (VirtAddr == 0) { + m_Wt = false; + m_Rw = false; + m_Cache = false; + m_Shareable = false; + } +} + +PTEWrapper::~PTEWrapper() { + PTE *raw = reinterpret_cast<PTE *>(m_VirtAddr); + + MUST_PASS(raw); + MUST_PASS(!raw->Accessed); + + if (raw->Present) raw->Present = false; +} + +void PTEWrapper::FlushTLB(Ref<PageManager> &pm) { + pm.Leak().FlushTLB(this->m_VirtAddr); +} + +void PageManager::FlushTLB(UIntPtr VirtAddr) { + if (VirtAddr == kBadAddress) return; + + flush_tlb(VirtAddr); +} + +bool PTEWrapper::Reclaim() { + if (!this->m_Present) { + this->m_Present = true; + return true; + } + + return false; +} + +PTEWrapper *PageManager::Request(Boolean Rw, Boolean User, + Boolean ExecDisable) { + PTEWrapper *PageTableEntry = reinterpret_cast<PTEWrapper *>( + hCore::HAL::hal_alloc_page(sizeof(PTEWrapper), Rw, User)); + + if (PageTableEntry == nullptr) { + kcout << "PTEWrapper : Page table is nullptr!, kernel_new_ptr failed!"; + return nullptr; + } + + *PageTableEntry = + PTEWrapper{Rw, User, ExecDisable, Detail::create_page_wrapper(Rw, User)}; + return PageTableEntry; +} + +bool PageManager::Free(Ref<PTEWrapper *> &wrapper) { + if (wrapper) { + if (!Detail::page_disable(wrapper->VirtualAddress())) return false; + + this->FlushTLB(wrapper->VirtualAddress()); + return true; + } + + return false; +} + +const UIntPtr &PTEWrapper::VirtualAddress() { return m_VirtAddr; } + +bool PTEWrapper::Shareable() { + auto raw = reinterpret_cast<PTE *>(m_VirtAddr); + + if (raw->Present) { + m_Shareable = raw->Shared; + return m_Shareable; + } else { + kcout << "[PTEWrapper::Shareable] page is not present!"; + return false; + } +} + +bool PTEWrapper::Present() { + auto raw = reinterpret_cast<PTE *>(m_VirtAddr); + + if (raw->Present) { + m_Present = raw->Present; + return m_Present; + } else { + kcout << "[PTEWrapper::Present] page is not present!"; + return false; + } +} + +bool PTEWrapper::Access() { + auto raw = reinterpret_cast<PTE *>(m_VirtAddr); + + if (raw->Present) { + m_Accessed = raw->Accessed; + } + + return m_Accessed; +} +} // namespace hCore diff --git a/Private/Source/Panic.cxx b/Private/Source/Panic.cxx index 46e184d3..9e3dddfb 100644 --- a/Private/Source/Panic.cxx +++ b/Private/Source/Panic.cxx @@ -7,50 +7,43 @@ * ======================================================== */ -#include <NewKit/Panic.hpp> #include <ArchKit/Arch.hpp> -#include <NewKit/String.hpp> - #include <KernelKit/DebugOutput.hpp> +#include <NewKit/Panic.hpp> +#include <NewKit/String.hpp> -extern "C" [[noreturn]] void wait_for_debugger() -{ - while (true) - { - hCore::HAL::rt_cli(); - hCore::HAL::rt_halt(); - } +extern "C" [[noreturn]] void wait_for_debugger() { + while (true) { + hCore::HAL::rt_cli(); + hCore::HAL::rt_halt(); + } } /* Each error code is attributed with an ID, which will prompt a string onto the * screen. Wait for debugger... */ -namespace hCore -{ - void panic(const hCore::Int& id) - { +namespace hCore { +void panic(const hCore::Int &id) { #ifdef __DEBUG__ - kcout << "hCore: Kernel Panic! \r\n"; - kcout << StringBuilder::FromInt("kError : %\n", id); + kcout << "hCore: Kernel Panic! \r\n"; + kcout << StringBuilder::FromInt("kError : %\n", id); - DumpManager::Dump(); -#endif // __DEBUG__ + DumpManager::Dump(); +#endif // __DEBUG__ - wait_for_debugger(); - } + wait_for_debugger(); +} - void runtime_check(bool expr, const char *file, const char *line) - { - if (!expr) - { +void runtime_check(bool expr, const char *file, const char *line) { + if (!expr) { #ifdef __DEBUG__ - kcout << "[KERNEL] Check Failed!\n"; - kcout << "[KERNEL] File: " << file << "\n"; - kcout << "[KERNEL] Where: " << line << "\n"; + kcout << "[KERNEL] Check Failed!\n"; + kcout << "[KERNEL] File: " << file << "\n"; + kcout << "[KERNEL] Where: " << line << "\n"; -#endif // __DEBUG__ +#endif // __DEBUG__ - hCore::panic(RUNTIME_CHECK_FAILED); // Runtime Check failed - } - } -} // namespace hCore + hCore::panic(RUNTIME_CHECK_FAILED); // Runtime Check failed + } +} +} // namespace hCore diff --git a/Private/Source/PermissionSelector.cxx b/Private/Source/PermissionSelector.cxx index 04245502..69bb949b 100644 --- a/Private/Source/PermissionSelector.cxx +++ b/Private/Source/PermissionSelector.cxx @@ -13,31 +13,24 @@ #include <KernelKit/PermissionSelector.hxx> #include <NewKit/Panic.hpp> -namespace hCore -{ -PermissionSelector::PermissionSelector(const Int32 &sel) : fRing((RingKind)sel) -{ - MUST_PASS(sel > 0); +namespace hCore { +PermissionSelector::PermissionSelector(const Int32 &sel) + : fRing((RingKind)sel) { + MUST_PASS(sel > 0); } -PermissionSelector::PermissionSelector(const RingKind &ringKind) : fRing(ringKind) -{ -} +PermissionSelector::PermissionSelector(const RingKind &ringKind) + : fRing(ringKind) {} PermissionSelector::~PermissionSelector() = default; -bool PermissionSelector::operator==(const PermissionSelector &lhs) -{ - return lhs.fRing == this->fRing; +bool PermissionSelector::operator==(const PermissionSelector &lhs) { + return lhs.fRing == this->fRing; } -bool PermissionSelector::operator!=(const PermissionSelector &lhs) -{ - return lhs.fRing != this->fRing; +bool PermissionSelector::operator!=(const PermissionSelector &lhs) { + return lhs.fRing != this->fRing; } -const RingKind &PermissionSelector::Ring() noexcept -{ - return this->fRing; -} -} // namespace hCore +const RingKind &PermissionSelector::Ring() noexcept { return this->fRing; } +} // namespace hCore diff --git a/Private/Source/Pmm.cxx b/Private/Source/Pmm.cxx index 5d4ce965..b87e450b 100644 --- a/Private/Source/Pmm.cxx +++ b/Private/Source/Pmm.cxx @@ -10,84 +10,69 @@ #include <KernelKit/DebugOutput.hpp> #include <NewKit/Pmm.hpp> -namespace hCore -{ - Pmm::Pmm() = default; +namespace hCore { +Pmm::Pmm() = default; - Pmm::Pmm(Ref<PageManager*> &pm) : m_PageManager(pm) - { - MUST_PASS(pm.Leak()); - kcout << "[PMM] New PhysicalMemoryManager\r\n"; - } +Pmm::Pmm(Ref<PageManager *> &pm) : m_PageManager(pm) { + MUST_PASS(pm.Leak()); + kcout << "[PMM] New PhysicalMemoryManager\r\n"; +} - Pmm::~Pmm() = default; +Pmm::~Pmm() = default; - /* If this returns Null pointer, enter emergency mode */ - Ref<PTEWrapper*> Pmm::RequestPage(Boolean user, Boolean readWrite) - { - if (m_PageManager) - { - PTEWrapper *pt = m_PageManager.Leak()->Request(user, readWrite, true); +/* If this returns Null pointer, enter emergency mode */ +Ref<PTEWrapper *> Pmm::RequestPage(Boolean user, Boolean readWrite) { + if (m_PageManager) { + PTEWrapper *pt = m_PageManager.Leak()->Request(user, readWrite, true); - if (pt) - return Ref<PTEWrapper*>(pt); + if (pt) return Ref<PTEWrapper *>(pt); - return {}; - } + return {}; + } - kcout << "[Pmm::RequestPage] Ref<PTEWrapper*> could not be created! " - "m_PageManager is nullptr!\r\n"; + kcout << "[Pmm::RequestPage] Ref<PTEWrapper*> could not be created! " + "m_PageManager is nullptr!\r\n"; - return {}; - } + return {}; +} - Boolean Pmm::FreePage(Ref<PTEWrapper*> PageRef) - { - if (!PageRef) - return false; +Boolean Pmm::FreePage(Ref<PTEWrapper *> PageRef) { + if (!PageRef) return false; - PageRef->m_Present = false; + PageRef->m_Present = false; - return true; - } + return true; +} - Boolean Pmm::TogglePresent(Ref<PTEWrapper*> PageRef, Boolean Enable) - { - if (!PageRef) - return false; +Boolean Pmm::TogglePresent(Ref<PTEWrapper *> PageRef, Boolean Enable) { + if (!PageRef) return false; - PageRef->m_Present = Enable; + PageRef->m_Present = Enable; - return true; - } + return true; +} - Boolean Pmm::ToggleUser(Ref<PTEWrapper*> PageRef, Boolean Enable) - { - if (!PageRef) - return false; +Boolean Pmm::ToggleUser(Ref<PTEWrapper *> PageRef, Boolean Enable) { + if (!PageRef) return false; - PageRef->m_Rw = Enable; + PageRef->m_Rw = Enable; - return true; - } + return true; +} - Boolean Pmm::ToggleRw(Ref<PTEWrapper*> PageRef, Boolean Enable) - { - if (!PageRef) - return false; +Boolean Pmm::ToggleRw(Ref<PTEWrapper *> PageRef, Boolean Enable) { + if (!PageRef) return false; - PageRef->m_Rw = Enable; + PageRef->m_Rw = Enable; - return true; - } + return true; +} - Boolean Pmm::ToggleShare(Ref<PTEWrapper*> PageRef, Boolean Enable) - { - if (!PageRef) - return false; +Boolean Pmm::ToggleShare(Ref<PTEWrapper *> PageRef, Boolean Enable) { + if (!PageRef) return false; - PageRef->m_Shareable = Enable; + PageRef->m_Shareable = Enable; - return true; - } -} // namespace hCore + return true; +} +} // namespace hCore diff --git a/Private/Source/ProcessManager.cxx b/Private/Source/ProcessManager.cxx index 664a1c7c..987b8093 100644 --- a/Private/Source/ProcessManager.cxx +++ b/Private/Source/ProcessManager.cxx @@ -10,7 +10,7 @@ #include "NewKit/Panic.hpp" #include <KernelKit/ProcessManager.hpp> #include <KernelKit/SMPManager.hpp> -#include <NewKit/KHeap.hpp> +#include <NewKit/KernelHeap.hpp> #include <NewKit/String.hpp> #define kPoolAlign (4) diff --git a/Private/Source/Property.cxx b/Private/Source/Property.cxx index ebb87fb9..f3633598 100644 --- a/Private/Source/Property.cxx +++ b/Private/Source/Property.cxx @@ -9,15 +9,10 @@ #include <CFKit/Property.hpp> -namespace hCore -{ - bool Property::StringEquals(StringView &name) - { - return m_sName && this->m_sName == name; - } +namespace hCore { +bool Property::StringEquals(StringView& name) { + return m_sName && this->m_sName == name; +} - const PropertyId& Property::GetPropertyById() - { - return m_Action; - } -} // namespace hCore +const PropertyId& Property::GetPropertyById() { return m_Action; } +} // namespace hCore diff --git a/Private/Source/RuntimeMain.cxx b/Private/Source/RuntimeMain.cxx index 210a5c43..a2b777d8 100644 --- a/Private/Source/RuntimeMain.cxx +++ b/Private/Source/RuntimeMain.cxx @@ -7,7 +7,6 @@ * ======================================================== */ -#include "NewKit/Defines.hpp" #include <ArchKit/Arch.hpp> #include <KernelKit/CodeManager.hpp> #include <KernelKit/FileManager.hpp> @@ -17,20 +16,18 @@ extern "C" void (*__SYSTEM_FINI)(); extern "C" void (**__SYSTEM_INIT)(); -extern "C" void RuntimeMain() -{ - for (hCore::SizeT index_init = 0UL; __SYSTEM_INIT[index_init] != __SYSTEM_FINI; ++index_init) - { - __SYSTEM_INIT[index_init](); - } +extern "C" void RuntimeMain() { + for (hCore::SizeT index_init = 0UL; + __SYSTEM_INIT[index_init] != __SYSTEM_FINI; ++index_init) { + __SYSTEM_INIT[index_init](); + } - MUST_PASS(hCore::init_hal()); + MUST_PASS(hCore::init_hal()); - hCore::IFilesystemManager::Mount(new hCore::NewFilesystemManager()); - hCore::PEFLoader img("/System/Seeker.cm"); + hCore::IFilesystemManager::Mount(new hCore::NewFilesystemManager()); + hCore::PEFLoader img("/System/Seeker.cm"); - if (!hCore::Utils::execute_from_image(img)) - { - hCore::panic(RUNTIME_CHECK_BOOTSTRAP); - } + if (!hCore::Utils::execute_from_image(img)) { + hCore::panic(RUNTIME_CHECK_BOOTSTRAP); + } } diff --git a/Private/Source/SMPManager.cxx b/Private/Source/SMPManager.cxx index 4897165b..05e47ea8 100644 --- a/Private/Source/SMPManager.cxx +++ b/Private/Source/SMPManager.cxx @@ -7,144 +7,137 @@ * ======================================================== */ -#include <KernelKit/SMPManager.hpp> -#include <KernelKit/ProcessManager.hpp> #include <ArchKit/Arch.hpp> +#include <KernelKit/ProcessManager.hpp> +#include <KernelKit/SMPManager.hpp> /// BUGS: 0 //! This file handles multi processing in hCore. //! Multi processing is needed for File I/O, networking and scheduling. -namespace hCore -{ - // A ProcessorCore class takes care of it's owned hardware thread. - // It has a stack for it's core. +namespace hCore { +// A ProcessorCore class takes care of it's owned hardware thread. +// It has a stack for it's core. + +// @brief constructor +ProcessorCore::ProcessorCore() = default; + +// @brief destructor +ProcessorCore::~ProcessorCore() = default; + +//! @brief returns the id - // @brief constructor - ProcessorCore::ProcessorCore() = default; +const ThreadID& ProcessorCore::ID() noexcept { return m_ID; } - // @brief destructor - ProcessorCore::~ProcessorCore() = default; - - //! @brief returns the id +//! @brief returns the kind - const ThreadID& ProcessorCore::ID() noexcept { return m_ID; } - - //! @brief returns the kind +const ThreadKind& ProcessorCore::Kind() noexcept { return m_Kind; } - const ThreadKind& ProcessorCore::Kind() noexcept { return m_Kind; } +//! @brief is the core busy? - //! @brief is the core busy? +bool ProcessorCore::IsBusy() noexcept { return m_Busy; } - bool ProcessorCore::IsBusy() noexcept { return m_Busy; } +/// @brief Get processor stack frame. - /// @brief Get processor stack frame. +HAL::StackFrame* ProcessorCore::StackFrame() noexcept { + MUST_PASS(m_Stack); + return m_Stack; +} - HAL::StackFrame* ProcessorCore::StackFrame() noexcept - { - MUST_PASS(m_Stack); - return m_Stack; - } +void ProcessorCore::Busy(const bool busy) noexcept { m_Busy = busy; } - void ProcessorCore::Busy(const bool busy) noexcept { m_Busy = busy; } +ProcessorCore::operator bool() { return m_Stack; } - ProcessorCore::operator bool() { return m_Stack; } +/// @brief Wakeup the processor. - /// @brief Wakeup the processor. +void ProcessorCore::Wake(const bool wakeup) noexcept { + m_Wakeup = wakeup; - void ProcessorCore::Wake(const bool wakeup) noexcept - { - m_Wakeup = wakeup; + if (!m_Wakeup) + rt_hang_thread(m_Stack); + else + rt_wakeup_thread(m_Stack); +} - if (!m_Wakeup) - rt_hang_thread(m_Stack); - else - rt_wakeup_thread(m_Stack); - } +bool ProcessorCore::Switch(HAL::StackFrame* stack) { + if (stack == nullptr) return false; - bool ProcessorCore::Switch(HAL::StackFrame* stack) - { - if (stack == nullptr) - return false; + return rt_do_context_switch(m_Stack, stack) == 0; +} - return rt_do_context_switch(m_Stack, stack) == 0; - } +///! @brief Tells if processor is waked up. +bool ProcessorCore::IsWakeup() noexcept { return m_Wakeup; } - ///! @brief Tells if processor is waked up. - bool ProcessorCore::IsWakeup() noexcept { return m_Wakeup; } +//! @brief Constructor and destructor - //! @brief Constructor and destructor +///! @brief Default constructor. +SMPManager::SMPManager() = default; - ///! @brief Default constructor. - SMPManager::SMPManager() = default; +///! @brief Default destructor. +SMPManager::~SMPManager() = default; - ///! @brief Default destructor. - SMPManager::~SMPManager() = default; +/// @brief Shared singleton function +Ref<SMPManager> SMPManager::Shared() { + static SMPManager manager; + return {manager}; +} - /// @brief Shared singleton function - Ref<SMPManager> SMPManager::Shared() - { - static SMPManager manager; - return { manager }; - } +/// @brief Get Stack Frame of Core +HAL::StackFramePtr SMPManager::GetStackFrame() noexcept { + if (m_ThreadList[m_CurrentThread].Leak() && + ProcessHelper::GetCurrentPID() == + m_ThreadList[m_CurrentThread].Leak().Leak().m_PID) + return m_ThreadList[m_CurrentThread].Leak().Leak().m_Stack; - /// @brief Get Stack Frame of Core - HAL::StackFramePtr SMPManager::GetStackFrame() noexcept - { - if (m_ThreadList[m_CurrentThread].Leak() && - ProcessHelper::GetCurrentPID() == m_ThreadList[m_CurrentThread].Leak().Leak().m_PID) - return m_ThreadList[m_CurrentThread].Leak().Leak().m_Stack; + return nullptr; +} - return nullptr; - } +/// @brief Finds and switch to a free core. +bool SMPManager::Switch(HAL::StackFrame* stack) { + if (stack == nullptr) return false; - /// @brief Finds and switch to a free core. - bool SMPManager::Switch(HAL::StackFrame* stack) - { - if (stack == nullptr) - return false; + for (SizeT idx = 0; idx < kMaxHarts; ++idx) { + // stack != nullptr -> if core is used, then continue. + if (!m_ThreadList[idx].Leak() || + !m_ThreadList[idx].Leak().Leak().IsWakeup() || + m_ThreadList[idx].Leak().Leak().IsBusy()) + continue; - for (SizeT idx = 0; idx < kMaxHarts; ++idx) - { - // stack != nullptr -> if core is used, then continue. - if (!m_ThreadList[idx].Leak() || - !m_ThreadList[idx].Leak().Leak().IsWakeup() || - m_ThreadList[idx].Leak().Leak().IsBusy()) - continue; + m_ThreadList[idx].Leak().Leak().m_ID = idx; + m_ThreadList[idx].Leak().Leak().m_Stack = stack; + m_ThreadList[idx].Leak().Leak().m_PID = ProcessHelper::GetCurrentPID(); - m_ThreadList[idx].Leak().Leak().m_ID = idx; - m_ThreadList[idx].Leak().Leak().m_Stack = stack; - m_ThreadList[idx].Leak().Leak().m_PID = ProcessHelper::GetCurrentPID(); - - m_ThreadList[idx].Leak().Leak().Busy(true); + m_ThreadList[idx].Leak().Leak().Busy(true); - Boolean ret = (rt_do_context_switch(rt_get_current_context(), stack) == 0); - - m_ThreadList[idx].Leak().Leak().Busy(false); + Boolean ret = (rt_do_context_switch(rt_get_current_context(), stack) == 0); - return ret; - } + m_ThreadList[idx].Leak().Leak().Busy(false); - return false; - } + return ret; + } - /** - * Index hart - * @param idx - * @return - */ - Ref<ProcessorCore> SMPManager::operator[](const SizeT& idx) { return m_ThreadList[idx].Leak(); } - - /** - * Check if thread pool isn't empty. - * @return - */ - SMPManager::operator bool() noexcept { return !m_ThreadList.Empty(); } - - /** - * Reverse operator bool - * @return - */ - bool SMPManager::operator!() noexcept { return m_ThreadList.Empty(); } -} // namespace hCore + return false; +} + +/** + * Index hart + * @param idx + * @return + */ +Ref<ProcessorCore> SMPManager::operator[](const SizeT& idx) { + return m_ThreadList[idx].Leak(); +} + +/** + * Check if thread pool isn't empty. + * @return + */ +SMPManager::operator bool() noexcept { return !m_ThreadList.Empty(); } + +/** + * Reverse operator bool + * @return + */ +bool SMPManager::operator!() noexcept { return m_ThreadList.Empty(); } +} // namespace hCore diff --git a/Private/Source/Semaphore.cxx b/Private/Source/Semaphore.cxx index 0c530274..5702b9fb 100644 --- a/Private/Source/Semaphore.cxx +++ b/Private/Source/Semaphore.cxx @@ -7,47 +7,38 @@ * ======================================================== */ +#include <KernelKit/ProcessManager.hpp> #include <KernelKit/Semaphore.hpp> - #include <KernelKit/Timer.hpp> -#include <KernelKit/ProcessManager.hpp> -namespace hCore -{ - bool Semaphore::Unlock() noexcept - { - if (fLockingProcess) - fLockingProcess = nullptr; +namespace hCore { +bool Semaphore::Unlock() noexcept { + if (fLockingProcess) fLockingProcess = nullptr; - return fLockingProcess == nullptr; - } + return fLockingProcess == nullptr; +} - bool Semaphore::Lock(Process* process) - { - if (!process || - fLockingProcess) - return false; +bool Semaphore::Lock(Process* process) { + if (!process || fLockingProcess) return false; - fLockingProcess = process; + fLockingProcess = process; - return true; - } + return true; +} - bool Semaphore::IsLocked() const { return fLockingProcess; } +bool Semaphore::IsLocked() const { return fLockingProcess; } - bool Semaphore::LockOrWait(Process* process, const Int64& seconds) - { - if (process == nullptr) - return false; +bool Semaphore::LockOrWait(Process* process, const Int64& seconds) { + if (process == nullptr) return false; - HardwareTimer timer(Seconds(seconds)); - timer.Wait(); + HardwareTimer timer(Seconds(seconds)); + timer.Wait(); - return this->Lock(process); - } + return this->Lock(process); +} - void Semaphore::Sync() noexcept - { - while (fLockingProcess) {} - } +void Semaphore::Sync() noexcept { + while (fLockingProcess) { + } } +} // namespace hCore diff --git a/Private/Source/SharedObject.cxx b/Private/Source/SharedObject.cxx index a6ccc224..0e824b27 100644 --- a/Private/Source/SharedObject.cxx +++ b/Private/Source/SharedObject.cxx @@ -18,38 +18,37 @@ using namespace hCore; /***********************************************************************************/ -extern "C" SharedObject *__LibMain(VoidPtr image) -{ - /***********************************************************************************/ - /* Allocate new library to be added to the lookup table. */ - /***********************************************************************************/ +extern "C" SharedObject *__LibMain(VoidPtr image) { + /***********************************************************************************/ + /* Allocate new library to be added to the lookup table. + */ + /***********************************************************************************/ - SharedObject *library = hcore_tls_new_class<SharedObject>(); + SharedObject *library = hcore_tls_new_class<SharedObject>(); - if (!library) - { - kcout << "__LibMain: Out of Memory!\n"; - ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + if (!library) { + kcout << "__LibMain: Out of Memory!\n"; + ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); - return nullptr; - } + return nullptr; + } - library->Mount(hcore_tls_new_class<SharedObject::SharedObjectTraits>()); + library->Mount(hcore_tls_new_class<SharedObject::SharedObjectTraits>()); - if (!library->Get()) - { - kcout << "__LibMain: Out of Memory!\n"; - ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + if (!library->Get()) { + kcout << "__LibMain: Out of Memory!\n"; + ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); - return nullptr; - } + return nullptr; + } - library->Get()->fImageObject = ProcessManager::Shared().Leak().GetCurrent().Leak().Image; - library->Get()->fImageEntrypointOffset = library->Load<VoidPtr>(kPefStart); + library->Get()->fImageObject = + ProcessManager::Shared().Leak().GetCurrent().Leak().Image; + library->Get()->fImageEntrypointOffset = library->Load<VoidPtr>(kPefStart); - kcout << "__LibMain: Done allocating Shared Library...\n"; + kcout << "__LibMain: Done allocating Shared Library...\n"; - return library; + return library; } /***********************************************************************************/ diff --git a/Private/Source/Storage/ATA.cxx b/Private/Source/Storage/ATA.cxx index 08fa43b3..f960907b 100644 --- a/Private/Source/Storage/ATA.cxx +++ b/Private/Source/Storage/ATA.cxx @@ -7,93 +7,85 @@ * ======================================================== */ -#include <StorageKit/ATA.hpp> #include <ArchKit/Arch.hpp> +#include <StorageKit/ATA.hpp> //! @brief Driver for ATA, listens to a specific address for data to come. //! mapped by NewFirmware. #define kATAError 2 -namespace hCore -{ - Ref<PRDT*> kPrdt = nullptr; +namespace hCore { +Ref<PRDT*> kPrdt = nullptr; - bool set_prdt_struct(Ref<PRDT*>& refCtrl) - { - if (!kPrdt) - { - kPrdt = refCtrl; - kcout << "[set_prdt_struct] PRDT is set."; +bool set_prdt_struct(Ref<PRDT*>& refCtrl) { + if (!kPrdt) { + kPrdt = refCtrl; + kcout << "[set_prdt_struct] PRDT is set."; - return true; - } + return true; + } - kcout << "[set_prdt_struct] [WARNING] Tried to change PRDT.\n"; - return false; - } + kcout << "[set_prdt_struct] [WARNING] Tried to change PRDT.\n"; + return false; +} - enum - { - k28BitRead = 0xC8, - k48BitRead = 0x25, - k28BitWrite = 0xCA, - k48BitWrite = 0x35, - }; +enum { + k28BitRead = 0xC8, + k48BitRead = 0x25, + k28BitWrite = 0xCA, + k48BitWrite = 0x35, +}; - const char* ata_read_28(ULong lba) - { - static char buffer[512]; +const char* ata_read_28(ULong lba) { + static char buffer[512]; - UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress(); + UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress(); - packet[0] = k28BitRead; - packet[1] = (UIntPtr)&buffer; - packet[2] = lba; + packet[0] = k28BitRead; + packet[1] = (UIntPtr)&buffer; + packet[2] = lba; - rt_wait_for_io(); + rt_wait_for_io(); - return buffer; - } + return buffer; +} - const char* ata_read_48(ULong lba) - { - static char buffer[512]; +const char* ata_read_48(ULong lba) { + static char buffer[512]; - UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress(); + UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress(); - packet[0] = k48BitRead; - packet[1] = (UIntPtr)&buffer; - packet[4] = lba; + packet[0] = k48BitRead; + packet[1] = (UIntPtr)&buffer; + packet[4] = lba; - rt_wait_for_io(); + rt_wait_for_io(); - return buffer; - } + return buffer; +} - Int32 ata_write_48(ULong lba, const char *text) - { - UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress(); +Int32 ata_write_48(ULong lba, const char* text) { + UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress(); - packet[0] = k48BitWrite; - packet[1] = (UIntPtr)&text; - packet[2] = lba; + packet[0] = k48BitWrite; + packet[1] = (UIntPtr)&text; + packet[2] = lba; - rt_wait_for_io(); + rt_wait_for_io(); - return packet[1] == 2 ? kATAError : 0; - } + return packet[1] == 2 ? kATAError : 0; +} - Int32 ata_write_28(ULong lba, const char *text) - { - UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress(); +Int32 ata_write_28(ULong lba, const char* text) { + UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress(); - packet[0] = k28BitWrite; - packet[1] = (UIntPtr)&text; - packet[2] = lba; + packet[0] = k28BitWrite; + packet[1] = (UIntPtr)&text; + packet[2] = lba; - rt_wait_for_io(); + rt_wait_for_io(); - return packet[1] == 2 ? kATAError : 0; - } -} // namespace hCore + return packet[1] == 2 ? kATAError : 0; +} +} // namespace hCore diff --git a/Private/Source/Storage/NVME.cxx b/Private/Source/Storage/NVME.cxx index aa1d8ecc..9501b8fa 100644 --- a/Private/Source/Storage/NVME.cxx +++ b/Private/Source/Storage/NVME.cxx @@ -9,10 +9,6 @@ #include <StorageKit/NVME.hpp> -namespace hCore -{ - const char *NVMEDevice::Name() const - { - return ("NVMEDevice"); - } -} // namespace hCore +namespace hCore { +const char *NVMEDevice::Name() const { return ("NVMEDevice"); } +} // namespace hCore diff --git a/Private/Source/Storage/Storage.cxx b/Private/Source/Storage/Storage.cxx index 6384479a..2340763a 100644 --- a/Private/Source/Storage/Storage.cxx +++ b/Private/Source/Storage/Storage.cxx @@ -9,4 +9,5 @@ #include <StorageKit/Storage.hpp> -const OSScsiPacket kCDRomPacketTemplate = { 0x43, 0, 1, 0, 0, 0, 0, 12, 0x40, 0, 0 }; +const OSScsiPacket kCDRomPacketTemplate = {0x43, 0, 1, 0, 0, 0, + 0, 12, 0x40, 0, 0}; diff --git a/Private/Source/String.cxx b/Private/Source/String.cxx index 3ad2a0cd..b4f95f6c 100644 --- a/Private/Source/String.cxx +++ b/Private/Source/String.cxx @@ -10,230 +10,185 @@ #include <NewKit/String.hpp> #include <NewKit/Utils.hpp> -namespace hCore -{ - Char* StringView::Data() { return m_Data; } +namespace hCore { +Char *StringView::Data() { return m_Data; } - const Char* StringView::CData() { return m_Data; } +const Char *StringView::CData() { return m_Data; } - Size StringView::Length() const { return string_length(m_Data); } +Size StringView::Length() const { return string_length(m_Data); } - bool StringView::operator==(const StringView &rhs) const - { - if (rhs.Length() != this->Length()) - return false; +bool StringView::operator==(const StringView &rhs) const { + if (rhs.Length() != this->Length()) return false; - for (Size index = 0; index < this->Length(); ++index) - { - if (rhs.m_Data[index] != m_Data[index]) - return false; - } + for (Size index = 0; index < this->Length(); ++index) { + if (rhs.m_Data[index] != m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator==(const Char *rhs) const - { - if (string_length(rhs) != this->Length()) - return false; +bool StringView::operator==(const Char *rhs) const { + if (string_length(rhs) != this->Length()) return false; - for (Size index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] != m_Data[index]) - return false; - } + for (Size index = 0; index < string_length(rhs); ++index) { + if (rhs[index] != m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator!=(const StringView &rhs) const - { - if (rhs.Length() != this->Length()) - return false; +bool StringView::operator!=(const StringView &rhs) const { + if (rhs.Length() != this->Length()) return false; - for (Size index = 0; index < rhs.Length(); ++index) - { - if (rhs.m_Data[index] == m_Data[index]) - return false; - } + for (Size index = 0; index < rhs.Length(); ++index) { + if (rhs.m_Data[index] == m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator!=(const Char *rhs) const - { - if (string_length(rhs) != this->Length()) - return false; +bool StringView::operator!=(const Char *rhs) const { + if (string_length(rhs) != this->Length()) return false; - for (Size index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] == m_Data[index]) - return false; - } - - return true; - } + for (Size index = 0; index < string_length(rhs); ++index) { + if (rhs[index] == m_Data[index]) return false; + } - ErrorOr<StringView> StringBuilder::Construct(const Char *data) - { - if (!data || - *data == 0) - return {}; + return true; +} - StringView view(string_length(data)); - - rt_copy_memory(reinterpret_cast<voidPtr>(const_cast<Char*>(data)), reinterpret_cast<voidPtr>(view.Data()), - view.Length()); +ErrorOr<StringView> StringBuilder::Construct(const Char *data) { + if (!data || *data == 0) return {}; - return ErrorOr<StringView>(view); - } + StringView view(string_length(data)); - const char* StringBuilder::FromInt(const char *fmt, int i) - { - if (!fmt) - return ("-1"); + rt_copy_memory(reinterpret_cast<voidPtr>(const_cast<Char *>(data)), + reinterpret_cast<voidPtr>(view.Data()), view.Length()); - char* ret = (char*)__alloca(sizeof(char) * 8 + string_length(fmt)); + return ErrorOr<StringView>(view); +} - if (!ret) - return ("-1"); +const char *StringBuilder::FromInt(const char *fmt, int i) { + if (!fmt) return ("-1"); - Char result[8]; + char *ret = (char *)__alloca(sizeof(char) * 8 + string_length(fmt)); - if (!to_str(result, sizeof(int), i)) - { - return ("-1"); - } + if (!ret) return ("-1"); - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(result); + Char result[8]; - for (Size idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') { - SizeT result_cnt = idx; + if (!to_str(result, sizeof(int), i)) { + return ("-1"); + } - for (auto y_idx = idx; y_idx < res_len; ++y_idx) { - ret[result_cnt] = result[y_idx]; - ++result_cnt; - } + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(result); - break; - } + for (Size idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeT result_cnt = idx; - ret[idx] = fmt[idx]; - } + for (auto y_idx = idx; y_idx < res_len; ++y_idx) { + ret[result_cnt] = result[y_idx]; + ++result_cnt; + } - return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ + break; } - const char *StringBuilder::FromBool(const char *fmt, bool i) - { - if (!fmt) - return ("?"); + ret[idx] = fmt[idx]; + } - const char* boolean_expr = i ? "true" : "false"; - char* ret = (char*)__alloca((sizeof(char) * i) ? 4 : 5 + string_length(fmt)); + return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ +} - if (!ret) - return ("?"); +const char *StringBuilder::FromBool(const char *fmt, bool i) { + if (!fmt) return ("?"); - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(boolean_expr); + const char *boolean_expr = i ? "true" : "false"; + char *ret = (char *)__alloca((sizeof(char) * i) ? 4 : 5 + string_length(fmt)); - for (Size idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') { - SizeT result_cnt = idx; + if (!ret) return ("?"); - for (auto y_idx = idx; y_idx < res_len; ++y_idx) - { - ret[result_cnt] = boolean_expr[y_idx]; - ++result_cnt; - } + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(boolean_expr); - break; - } + for (Size idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeT result_cnt = idx; - ret[idx] = fmt[idx]; - } + for (auto y_idx = idx; y_idx < res_len; ++y_idx) { + ret[result_cnt] = boolean_expr[y_idx]; + ++result_cnt; + } - return ret; + break; } - bool StringBuilder::Equals(const char *lhs, const char *rhs) - { - if (string_length(rhs) != string_length(lhs)) - return false; + ret[idx] = fmt[idx]; + } - for (Size index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] != lhs[index]) - return false; - } + return ret; +} - return true; - } +bool StringBuilder::Equals(const char *lhs, const char *rhs) { + if (string_length(rhs) != string_length(lhs)) return false; - const char *StringBuilder::Format(const char *fmt, const char *fmt2) - { - if (!fmt || !fmt2) - return ("?"); - - char* ret = (char*)alloca(sizeof(char) * string_length(fmt2) + string_length(fmt2)); - - if (!ret) - return ("?"); - - for (Size idx = 0; idx < string_length(fmt); ++idx) - { - if (fmt[idx] == '%') { - Size result_cnt = idx; - for (Size y_idx = 0; y_idx < string_length(fmt2); ++y_idx) - { - ret[result_cnt] = fmt2[y_idx]; - ++result_cnt; - } - - break; - } - - ret[idx] = fmt[idx]; - } - - return ret; - } + for (Size index = 0; index < string_length(rhs); ++index) { + if (rhs[index] != lhs[index]) return false; + } - static void string_append(char *lhs, char *rhs, int cur) - { - if (lhs && rhs && - cur < string_length(lhs)) - { - SizeT sz_rhs = string_length(rhs); + return true; +} - rt_copy_memory(rhs, lhs + cur, sz_rhs); - } - } +const char *StringBuilder::Format(const char *fmt, const char *fmt2) { + if (!fmt || !fmt2) return ("?"); + + char *ret = + (char *)alloca(sizeof(char) * string_length(fmt2) + string_length(fmt2)); - StringView& StringView::operator+=(const Char *rhs) - { - if (string_length(rhs) > string_length(this->m_Data)) - return *this; + if (!ret) return ("?"); - string_append(this->m_Data, const_cast<char*>(rhs), this->m_Cur); - this->m_Cur += string_length(rhs); - - return *this; + for (Size idx = 0; idx < string_length(fmt); ++idx) { + if (fmt[idx] == '%') { + Size result_cnt = idx; + for (Size y_idx = 0; y_idx < string_length(fmt2); ++y_idx) { + ret[result_cnt] = fmt2[y_idx]; + ++result_cnt; + } + + break; } - StringView& StringView::operator+=(const StringView &rhs) - { - if (string_length(rhs.m_Data) > string_length(this->m_Data)) - return *this; + ret[idx] = fmt[idx]; + } - string_append(this->m_Data, const_cast<char*>(rhs.m_Data), this->m_Cur); - this->m_Cur += string_length(const_cast<char*>(rhs.m_Data)); + return ret; +} - return *this; - } -} // namespace hCore +static void string_append(char *lhs, char *rhs, int cur) { + if (lhs && rhs && cur < string_length(lhs)) { + SizeT sz_rhs = string_length(rhs); + + rt_copy_memory(rhs, lhs + cur, sz_rhs); + } +} + +StringView &StringView::operator+=(const Char *rhs) { + if (string_length(rhs) > string_length(this->m_Data)) return *this; + + string_append(this->m_Data, const_cast<char *>(rhs), this->m_Cur); + this->m_Cur += string_length(rhs); + + return *this; +} + +StringView &StringView::operator+=(const StringView &rhs) { + if (string_length(rhs.m_Data) > string_length(this->m_Data)) return *this; + + string_append(this->m_Data, const_cast<char *>(rhs.m_Data), this->m_Cur); + this->m_Cur += string_length(const_cast<char *>(rhs.m_Data)); + + return *this; +} +} // namespace hCore diff --git a/Private/Source/ThreadLocalStorage.cxx b/Private/Source/ThreadLocalStorage.cxx index bcafdd10..66ec69d6 100644 --- a/Private/Source/ThreadLocalStorage.cxx +++ b/Private/Source/ThreadLocalStorage.cxx @@ -17,16 +17,15 @@ using namespace hCore; * @param ptr * @return if the cookie is enabled. */ -Boolean hcore_tls_check(VoidPtr ptr) -{ - if (!ptr) - return false; +Boolean hcore_tls_check(VoidPtr ptr) { + if (!ptr) return false; - const char* _ptr = (const char*)ptr; + const char* _ptr = (const char*)ptr; - kcout << "TLS: Check for cookie...\n"; + kcout << "TLS: Checking for cookie...\n"; - return _ptr[0] == kCookieMag0 && _ptr[1] == kCookieMag1 && _ptr[2] == kCookieMag2; + return _ptr[0] == kCookieMag0 && _ptr[1] == kCookieMag1 && + _ptr[2] == kCookieMag2; } /** @@ -34,11 +33,11 @@ Boolean hcore_tls_check(VoidPtr ptr) * @param ptr * @return */ -Void hcore_tls_check_syscall_impl(ThreadInformationBlock ptr) noexcept -{ - if (!hcore_tls_check(ptr.Cookie)) - { - kcout << "TLS: Verification failure, crashing...\n"; - ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); - } +Void hcore_tls_check_syscall_impl(ThreadInformationBlock ptr) noexcept { + if (!hcore_tls_check(ptr.Cookie)) { + kcout << "TLS: Verification failure, crashing...\n"; + ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + } + + kcout << "TLS: Verification succeeded! Keeping on...\n"; } diff --git a/Private/Source/Timer.cxx b/Private/Source/Timer.cxx index 73343fad..137a5545 100644 --- a/Private/Source/Timer.cxx +++ b/Private/Source/Timer.cxx @@ -18,12 +18,11 @@ Int32 HardwareTimerInterface::Wait() noexcept { return H_UNIMPLEMENTED; } HardwareTimer::HardwareTimer(Int64 seconds) : fWaitFor(seconds) {} HardwareTimer::~HardwareTimer() { fWaitFor = 0; } -Int32 HardwareTimer::Wait() noexcept -{ - if (fWaitFor < 1) - return -1; +Int32 HardwareTimer::Wait() noexcept { + if (fWaitFor < 1) return -1; - while (*fDigitalTimer < (*fDigitalTimer + fWaitFor)) {} + while (*fDigitalTimer < (*fDigitalTimer + fWaitFor)) { + } - return 0; -}
\ No newline at end of file + return 0; +} diff --git a/Private/Source/URL.cxx b/Private/Source/URL.cxx index 0aa75ab9..02a3ec77 100644 --- a/Private/Source/URL.cxx +++ b/Private/Source/URL.cxx @@ -8,92 +8,80 @@ */ #include <CFKit/URL.hpp> -#include <NewKit/Utils.hpp> - #include <KernelKit/DebugOutput.hpp> +#include <NewKit/Utils.hpp> // Bugs = 0 -namespace hCore -{ - Url::Url(StringView &strUrl) : m_urlView(strUrl, false) {} - - Url::~Url() = default; - - constexpr const char *kProtos[] = { - "https", // http with the secure. - "http", // http without the secure - "file", // filesystem protocol - "ftp", // file transfer protocol - "params", // system settings - "rsh", // remote shell (gui) - }; - - constexpr const int kUrlOutSz = 3; //! :// - constexpr const int kProtosCount = 8; - constexpr const int kRangeSz = 4096; - - static ErrorOr<StringView> url_extract_location(const char *url) - { - if (!url || *url == 0 || string_length(url, kRangeSz) > kRangeSz) - return ErrorOr<StringView>{-1}; - - StringView view(string_length(url)); - - SizeT i = 0; - bool scheme_found = false; - - for (; i < string_length(url); ++i) - { - if (!scheme_found) - { - for (int y = 0; kProtosCount; ++y) - { - if (string_in_string(view.CData(), kProtos[y])) - { - i += string_length(kProtos[y]) + kUrlOutSz; - scheme_found = true; - - break; - } - } - } - - view.Data()[i] = url[i]; - } +namespace hCore { +Url::Url(StringView &strUrl) : m_urlView(strUrl, false) {} - return ErrorOr<StringView>(view); - } +Url::~Url() = default; - static ErrorOr<StringView> url_extract_protocol(const char *url) - { - if (!url || *url == 0 || string_length(url, kRangeSz) > kRangeSz) - return ErrorOr<StringView>{-1}; +constexpr const char *kProtos[] = { + "https", // http with the secure. + "http", // http without the secure + "file", // filesystem protocol + "ftp", // file transfer protocol + "params", // system settings + "rsh", // remote shell (gui) +}; - ErrorOr<StringView> view{ -1 }; - - return view; - } +constexpr const int kUrlOutSz = 3; //! :// +constexpr const int kProtosCount = 8; +constexpr const int kRangeSz = 4096; + +static ErrorOr<StringView> url_extract_location(const char *url) { + if (!url || *url == 0 || string_length(url, kRangeSz) > kRangeSz) + return ErrorOr<StringView>{-1}; - Ref<ErrorOr<StringView>> Url::Location() noexcept - { - const char *src = m_urlView.Leak().CData(); - auto loc = url_extract_location(src); + StringView view(string_length(url)); - if (!loc) - return {}; + SizeT i = 0; + bool scheme_found = false; - return Ref<ErrorOr<StringView>>(loc); + for (; i < string_length(url); ++i) { + if (!scheme_found) { + for (int y = 0; kProtosCount; ++y) { + if (string_in_string(view.CData(), kProtos[y])) { + i += string_length(kProtos[y]) + kUrlOutSz; + scheme_found = true; + + break; + } + } } - Ref<ErrorOr<StringView>> Url::Protocol() noexcept - { - const char *src = m_urlView.Leak().CData(); - auto loc = url_extract_protocol(src); + view.Data()[i] = url[i]; + } - if (!loc) - return {}; + return ErrorOr<StringView>(view); +} - return Ref<ErrorOr<StringView>>(loc); - } -} // namespace hCore +static ErrorOr<StringView> url_extract_protocol(const char *url) { + if (!url || *url == 0 || string_length(url, kRangeSz) > kRangeSz) + return ErrorOr<StringView>{-1}; + + ErrorOr<StringView> view{-1}; + + return view; +} + +Ref<ErrorOr<StringView>> Url::Location() noexcept { + const char *src = m_urlView.Leak().CData(); + auto loc = url_extract_location(src); + + if (!loc) return {}; + + return Ref<ErrorOr<StringView>>(loc); +} + +Ref<ErrorOr<StringView>> Url::Protocol() noexcept { + const char *src = m_urlView.Leak().CData(); + auto loc = url_extract_protocol(src); + + if (!loc) return {}; + + return Ref<ErrorOr<StringView>>(loc); +} +} // namespace hCore diff --git a/Private/Source/UserHeap.cxx b/Private/Source/UserHeap.cxx new file mode 100644 index 00000000..e748e315 --- /dev/null +++ b/Private/Source/UserHeap.cxx @@ -0,0 +1,188 @@ +/* + * ======================================================== + * + * hCore + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include <NewKit/PageManager.hpp> +#include <NewKit/UserHeap.hpp> + +/// @file Heap.cxx +/// @brief Heap Manager, user mode allocator. +/// @note if you want to look at kernel allocs, please look for KHeap.cxx +/// bugs: 0 + +namespace hCore { +class HeapManager final { + public: + static SizeT& GetCount() { return s_NumPools; } + static Ref<Pmm>& GetPmm() { return s_Pmm; } + static Boolean& IsEnabled() { return s_PoolsAreEnabled; } + static Array<Ref<PTEWrapper*>, kPoolMaxSz>& The() { return s_Pool; } + + private: + static Size s_NumPools; + static Ref<Pmm> s_Pmm; + + private: + static Boolean s_PoolsAreEnabled; + static Array<Ref<PTEWrapper*>, kPoolMaxSz> s_Pool; +}; + +//! declare fields + +SizeT HeapManager::s_NumPools = 0UL; +Ref<Pmm> HeapManager::s_Pmm; +Boolean HeapManager::s_PoolsAreEnabled = true; +Array<Ref<PTEWrapper*>, kPoolMaxSz> HeapManager::s_Pool; + +static voidPtr ke_find_unused_heap(Int flags); +static void ke_free_heap_internal(voidPtr vaddr); +static voidPtr ke_make_heap(voidPtr vaddr, Int flags); +static bool ke_check_and_free_heap(const SizeT& index, voidPtr ptr); + +static voidPtr ke_find_unused_heap(Int flags) { + for (SizeT index = 0; index < kPoolMaxSz; ++index) { + if (HeapManager::The()[index] && + !HeapManager::The()[index].Leak().Leak().Leak()->Present()) { + HeapManager::GetPmm().Leak().TogglePresent( + HeapManager::The()[index].Leak().Leak(), true); + kcout << "[ke_find_unused_heap] Done, trying now to make a pool\r\n"; + + return ke_make_heap((voidPtr)HeapManager::The()[index] + .Leak() + .Leak() + .Leak() + ->VirtualAddress(), + flags); + } + } + + return nullptr; +} + +static voidPtr ke_make_heap(voidPtr virtualAddress, Int flags) { + if (virtualAddress) { + HeapHeader* pool_hdr = reinterpret_cast<HeapHeader*>(virtualAddress); + + if (!pool_hdr->Free) { + kcout << "[ke_make_heap] pool_hdr->Free, Pool already exists\n"; + return nullptr; + } + + pool_hdr->Flags = flags; + pool_hdr->Magic = kPoolMag; + pool_hdr->Free = false; + + kcout << "[ke_make_heap] New allocation has been done.\n"; + return reinterpret_cast<voidPtr>( + (reinterpret_cast<UIntPtr>(virtualAddress) + sizeof(HeapHeader))); + } + + kcout << "[ke_make_heap] Address is invalid"; + return nullptr; +} + +static void ke_free_heap_internal(voidPtr virtualAddress) { + HeapHeader* pool_hdr = reinterpret_cast<HeapHeader*>( + reinterpret_cast<UIntPtr>(virtualAddress) - sizeof(HeapHeader)); + + if (pool_hdr->Magic == kPoolMag) { + pool_hdr->Free = false; + pool_hdr->Flags = 0; + + kcout << "[ke_free_heap_internal] Successfully marked header as free!\r\n"; + } +} + +static bool ke_check_and_free_heap(const SizeT& index, voidPtr ptr) { + if (HeapManager::The()[index]) { + // ErrorOr<>::operator bool + if (!HeapManager::The()[index].Leak().Leak().IsStrong()) { + // we want them to be weak + // because we allocated it. + if (HeapManager::The()[index].Leak().Leak().Leak()->VirtualAddress() == + (UIntPtr)ptr) { + HeapManager::GetPmm().Leak().FreePage( + HeapManager::The()[index].Leak().Leak()); + --HeapManager::GetCount(); + + ke_free_heap_internal(ptr); + ptr = nullptr; + + return true; + } + } + } + + return false; +} + +/// @brief Creates a new pool pointer. +/// @param flags the flags attached to it. +/// @return a pool pointer with selected permissions. +voidPtr pool_new_ptr(Int32 flags) { + if (!HeapManager::IsEnabled()) return nullptr; + + if (HeapManager::GetCount() > kPoolMaxSz) return nullptr; + + if (voidPtr ret = ke_find_unused_heap(flags)) return ret; + + // this wasn't set to true + auto ref_page = HeapManager::GetPmm().Leak().RequestPage( + ((flags & kPoolUser)), (flags & kPoolRw)); + if (ref_page) { + ///! reserve page. + HeapManager::The()[HeapManager::GetCount()].Leak() = ref_page; + auto& ref = HeapManager::GetCount(); + ++ref; // increment the number of addresses we have now. + + kcout << "[pool_new_ptr] New Address found!\r\n"; + + // finally make the pool address. + return ke_make_heap( + reinterpret_cast<voidPtr>(ref_page.Leak()->VirtualAddress()), flags); + } + + return nullptr; +} + +/// @brief free a pool pointer. +/// @param ptr The pool pointer to free. +/// @return status code +Int32 pool_free_ptr(voidPtr ptr) { + if (!HeapManager::IsEnabled()) return -1; + + if (ptr) { + SizeT base = HeapManager::GetCount(); + + if (ke_check_and_free_heap(base, ptr)) return 0; + + for (SizeT index = 0; index < kPoolMaxSz; ++index) { + if (ke_check_and_free_heap(index, ptr)) return 0; + + --base; + } + } + + return -1; +} + +/// @brief Checks if pointer is valid. +/// @param thePool the pool pointer. +/// @param thePtr the pointer. +/// @param theLimit the last address of the pool. +/// @return if it is valid. +Boolean pool_ptr_exists(UIntPtr thePool, UIntPtr thePtr, SizeT theLimit) { + if (HeapManager::GetCount() < 1) return false; + + if (thePool == 0 || thePtr == 0 || theLimit == 0) { + return false; + } + + return ((thePool) < (thePtr) < (theLimit)); +} +} // namespace hCore diff --git a/Private/Source/Utils.cxx b/Private/Source/Utils.cxx index 9f767d72..6569258b 100644 --- a/Private/Source/Utils.cxx +++ b/Private/Source/Utils.cxx @@ -9,211 +9,166 @@ #include <NewKit/Utils.hpp> -namespace hCore -{ - Int string_compare(const Char *src, const Char *cmp, Size size) - { - Int32 counter = 0; - - for (Size index = 0; index < size; ++index) - { - if (src[index] != cmp[index]) - ++counter; - } - - return counter; - } +namespace hCore { +Int string_compare(const Char *src, const Char *cmp, Size size) { + Int32 counter = 0; - void rt_zero_memory(voidPtr pointer, Size len) - { - rt_set_memory((voidPtr) pointer, 0, len); - } + for (Size index = 0; index < size; ++index) { + if (src[index] != cmp[index]) ++counter; + } - Size string_length(const Char *str, SizeT _len) - { - if (*str == '\0') - return 0; + return counter; +} - Size len{0}; - while (str[len] != '\0') - { - if (len > _len) - { - break; - } +void rt_zero_memory(voidPtr pointer, Size len) { + rt_set_memory((voidPtr)pointer, 0, len); +} - ++len; - } +Size string_length(const Char *str, SizeT _len) { + if (*str == '\0') return 0; - return len; + Size len{0}; + while (str[len] != '\0') { + if (len > _len) { + break; } - Size string_length(const Char *str) - { - if (*str == '\0') - return 0; + ++len; + } - Size len{0}; - while (str[len] != '\0') - ++len; + return len; +} - return len; - } +Size string_length(const Char *str) { + if (*str == '\0') return 0; - voidPtr rt_set_memory(voidPtr src, char value, Size len) - { - if (!src || len < 1) - return nullptr; - char *start = reinterpret_cast<Char*>(src); + Size len{0}; + while (str[len] != '\0') ++len; - while (len) - { - *start = value; - ++start; - --len; - } + return len; +} - return (voidPtr) start; - } +voidPtr rt_set_memory(voidPtr src, char value, Size len) { + if (!src || len < 1) return nullptr; + char *start = reinterpret_cast<Char *>(src); - Int rt_move_memory(const voidPtr src, voidPtr dst, Size len) - { - if (len < 1) - return -2; - if (!src || !dst) - return -1; + while (len) { + *start = value; + ++start; + --len; + } - char *srcChr = reinterpret_cast<Char*>(src); - char *dstChar = reinterpret_cast<Char*>(dst); - Size index = 0; + return (voidPtr)start; +} - while (index < len) - { - dstChar[index] = srcChr[index]; - srcChr[index] = 0; +Int rt_move_memory(const voidPtr src, voidPtr dst, Size len) { + if (len < 1) return -2; + if (!src || !dst) return -1; - index++; - } + char *srcChr = reinterpret_cast<Char *>(src); + char *dstChar = reinterpret_cast<Char *>(dst); + Size index = 0; - return 0; - } + while (index < len) { + dstChar[index] = srcChr[index]; + srcChr[index] = 0; - Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len) - { - if (len < 1) - return -2; - if (!src || !dst) - return -1; + index++; + } - char *srcChr = reinterpret_cast<char*>(src); - char *dstChar = reinterpret_cast<char*>(dst); - Size index = 0; + return 0; +} - while (index < len) - { - dstChar[index] = srcChr[index]; - index++; - } +Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len) { + if (len < 1) return -2; + if (!src || !dst) return -1; - return 0; - } + char *srcChr = reinterpret_cast<char *>(src); + char *dstChar = reinterpret_cast<char *>(dst); + Size index = 0; - const Char *alloc_string(const Char *text) - { - if (!text) - return nullptr; + while (index < len) { + dstChar[index] = srcChr[index]; + index++; + } - const Char *string = new Char[string_length(text)]; - if (!string) - return nullptr; + return 0; +} - voidPtr vText = reinterpret_cast<voidPtr>(const_cast<char*>(text)); - voidPtr vStr = reinterpret_cast<voidPtr>(const_cast<char*>(string)); - rt_copy_memory(vText, vStr, string_length(text)); - return string; - } +const Char *alloc_string(const Char *text) { + if (!text) return nullptr; - Int to_uppercase(Int character) - { - if (character >= 'a' && character <= 'z') - return character - 0x20; + const Char *string = new Char[string_length(text)]; + if (!string) return nullptr; - return character; - } + voidPtr vText = reinterpret_cast<voidPtr>(const_cast<char *>(text)); + voidPtr vStr = reinterpret_cast<voidPtr>(const_cast<char *>(string)); + rt_copy_memory(vText, vStr, string_length(text)); + return string; +} - Int to_lower(Int character) - { - if (character >= 'A' && character <= 'Z') - return character + 0x20; +Int to_uppercase(Int character) { + if (character >= 'a' && character <= 'z') return character - 0x20; - return character; - } + return character; +} - bool to_str(Char *str, Int limit, Int base) - { - if (limit == 0) - return false; +Int to_lower(Int character) { + if (character >= 'A' && character <= 'Z') return character + 0x20; - Int copy_limit = limit; - Int cnt = 0; - Int ret = base; + return character; +} - while (limit != 1) { - ret = ret % 10; - str[cnt] = ret; +bool to_str(Char *str, Int limit, Int base) { + if (limit == 0) return false; - ++cnt; - --limit; - --ret; - } + Int copy_limit = limit; + Int cnt = 0; + Int ret = base; - str[copy_limit] = '\0'; - return true; - } + while (limit != 1) { + ret = ret % 10; + str[cnt] = ret; - Boolean is_space(Char chr) - { - return chr == ' '; - } + ++cnt; + --limit; + --ret; + } - Boolean is_newln(Char chr) - { - return chr == '\n'; - } + str[copy_limit] = '\0'; + return true; +} - voidPtr string_in_string(const char *in, const char *needle) - { - for (size_t i = 0; i < string_length(in); ++i) - { - if (string_compare(in + i, needle, string_length(needle)) == 0) - return reinterpret_cast<voidPtr>(const_cast<char*>(in + i)); - } +Boolean is_space(Char chr) { return chr == ' '; } - return nullptr; - } +Boolean is_newln(Char chr) { return chr == '\n'; } - // @brief Checks for a string start at the character. - - char* string_from_char(char* str, const char chr) - { - while (*str != chr) - { - ++str; - - if (*str == 0) - return nullptr; - } - - return str; - } -} // namespace hCore +voidPtr string_in_string(const char *in, const char *needle) { + for (size_t i = 0; i < string_length(in); ++i) { + if (string_compare(in + i, needle, string_length(needle)) == 0) + return reinterpret_cast<voidPtr>(const_cast<char *>(in + i)); + } -extern "C" void memset(void* dst, char src, size_t len) -{ - hCore::rt_set_memory(dst, src, len); + return nullptr; } -extern "C" void memcpy(void* dst, void* src, size_t len) -{ - hCore::rt_copy_memory(src, dst, len); -}
\ No newline at end of file +// @brief Checks for a string start at the character. + +char *string_from_char(char *str, const char chr) { + while (*str != chr) { + ++str; + + if (*str == 0) return nullptr; + } + + return str; +} +} // namespace hCore + +extern "C" void memset(void *dst, char src, size_t len) { + hCore::rt_set_memory(dst, src, len); +} + +extern "C" void memcpy(void *dst, void *src, size_t len) { + hCore::rt_copy_memory(src, dst, len); +} diff --git a/Private/Source/Variant.cxx b/Private/Source/Variant.cxx index 3881a07d..cb1a9a01 100644 --- a/Private/Source/Variant.cxx +++ b/Private/Source/Variant.cxx @@ -9,24 +9,20 @@ #include <NewKit/Variant.hpp> -namespace hCore -{ - const Char* Variant::ToString() - { - if (m_Ptr == nullptr) - { - return ("Memory:{Nullptr}"); - } +namespace hCore { +const Char* Variant::ToString() { + if (m_Ptr == nullptr) { + return ("Memory:{Nullptr}"); + } - switch (m_Kind) - { - case VariantKind::kString: - return ("Class:{String}"); - case VariantKind::kPointer: - return ("Memory:{Pointer}"); - default: - return ("Class:{Undefined}"); - } - } + switch (m_Kind) { + case VariantKind::kString: + return ("Class:{String}"); + case VariantKind::kPointer: + return ("Memory:{Pointer}"); + default: + return ("Class:{Undefined}"); + } +} -} // namespace hCore +} // namespace hCore diff --git a/Private/makefile b/Private/makefile index 7e6a6639..b4245f03 100644 --- a/Private/makefile +++ b/Private/makefile @@ -5,7 +5,7 @@ CC = x86_64-elf-gcc LD = x86_64-elf-ld -CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__hCore__ -I../ -I./ -I$(HOME)/ +CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I../ -I./ -I$(HOME)/ ASM = nasm ASMFLAGS = -f elf64 @@ -18,12 +18,12 @@ SCRIPT = --script=Linker/Platforms/PC.lds # we want a flat binary FMT = elf64 -.PHONY: kernel-no -kernel-no: - @echo "Use make all instead." +.PHONY: invalid-recipe +invalid-recipe: + @echo "invalid-recipe: Use make all instead." -.PHONY: kernel-build -kernel-build: +.PHONY: h-core +h-core: $(CC) $(CCFLAGS) Source/*.cxx HALKit/AMD64/PCI/*.cpp Source/Network/*.cpp\ Source/Storage/*.cxx HALKit/AMD64/*.cxx HALKit/AMD64/*.cpp HALKit/AMD64/*.s $(ASM) -f elf64 HALKit/AMD64/DebugManager.asm @@ -34,16 +34,16 @@ kernel-build: OBJCOPY = x86_64-elf-objcopy -.PHONY: kernel-link -kernel-link: +.PHONY: link +link: $(LD) $(SCRIPT) Obj/*.o -o $(KERNEL) $(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 $(KERNEL) $(KERNEL) cp $(KERNEL) Root/System .PHONY: all -all: kernel-build kernel-link - @echo "[hKernel] Job is done." +all: h-core link + @echo "[hKernel] HCore is linked." -.PHONY: kernel-clean -kernel-clean: +.PHONY: clean +clean: rm -f Obj/*.o $(KERNEL) |
