From fb790b07aeba8e22e4190cf3e1834d11ecde6c96 Mon Sep 17 00:00:00 2001 From: Amlal Date: Fri, 25 Apr 2025 13:08:33 +0200 Subject: dev: better .clang-format, ran format command. Signed-off-by: Amlal --- dev/kernel/HALKit/AMD64/Processor.h | 518 +++++++++++++++++------------------- 1 file changed, 245 insertions(+), 273 deletions(-) (limited to 'dev/kernel/HALKit/AMD64/Processor.h') diff --git a/dev/kernel/HALKit/AMD64/Processor.h b/dev/kernel/HALKit/AMD64/Processor.h index 13819f3e..1319277f 100644 --- a/dev/kernel/HALKit/AMD64/Processor.h +++ b/dev/kernel/HALKit/AMD64/Processor.h @@ -1,32 +1,32 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - File: Prcoessor.h - Purpose: AMD64 processor abstraction. + File: Prcoessor.h + Purpose: AMD64 processor abstraction. - Revision History: + Revision History: - 30/01/24: Added file (amlel) + 30/01/24: Added file (amlel) ------------------------------------------- */ #pragma once +#include +#include #include #include #include -#include -#include -#define kPITControlPort (0x43) +#define kPITControlPort (0x43) #define kPITChannel0Port (0x40) -#define kPITFrequency (1193180) +#define kPITFrequency (1193180) -#define kPICCommand (0x20) -#define kPICData (0x21) +#define kPICCommand (0x20) +#define kPICData (0x21) #define kPIC2Command (0xA0) -#define kPIC2Data (0xA1) +#define kPIC2Data (0xA1) #include @@ -38,269 +38,241 @@ /// @brief interrupt for system call. #define kKernelInterruptId (0x32) -#define IsActiveLow(FLG) (FLG & 2) +#define IsActiveLow(FLG) (FLG & 2) #define IsLevelTriggered(FLG) (FLG & 8) #define kInterruptGate (0x8E) -#define kTrapGate (0xEF) -#define kTaskGate (0b10001100) -#define kIDTSelector (0x08) - -namespace Kernel -{ - namespace Detail::AMD64 - { - struct PACKED InterruptDescriptorAMD64 final - { - UInt16 OffsetLow; // offset bits 0..15 - UInt16 Selector; // a code segment selector in GDT or LDT - UInt8 Ist; - UInt8 TypeAttributes; - UInt16 OffsetMid; - UInt32 OffsetHigh; - UInt32 Zero; // reserved - }; - } // namespace Detail::AMD64 -} // namespace Kernel - -namespace Kernel::HAL -{ - /// @brief Memory Manager mapping flags. - enum - { - kMMFlagsInvalid = 1 << 0, - kMMFlagsPresent = 1 << 1, - kMMFlagsWr = 1 << 2, - kMMFlagsUser = 1 << 3, - kMMFlagsNX = 1 << 4, - kMMFlagsPCD = 1 << 5, - kMMFlagsPwt = 1 << 6, - kMMFlagsCount = 4, - }; - - struct PACKED Register64 final - { - UShort Limit; - UIntPtr Base; - }; - - using RawRegister = UInt64; - using Reg = RawRegister; - using InterruptId = UInt16; /* For each element in the IVT */ - - /// @brief Stack frame (as retrieved from assembly.) - struct PACKED StackFrame final - { - RawRegister R8{0}; - RawRegister R9{0}; - RawRegister R10{0}; - RawRegister FS{0}; - RawRegister R12{0}; - RawRegister R13{0}; - RawRegister R14{0}; - RawRegister R15{0}; - RawRegister GS{0}; - RawRegister SP{0}; - RawRegister BP{0}; - }; - - 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; - - class SegmentDescriptor final - { - public: - UInt16 Base; - UInt8 BaseMiddle; - UInt8 BaseHigh; - - UShort Limit; - UChar Gran; - UChar AccessByte; - }; - - /*** - * @brief Segment Boolean operations - */ - class SegmentDescriptorComparator final - { - public: - Bool IsValid(SegmentDescriptor& seg) - { - return seg.Base > seg.Limit; - } - - Bool Equals(SegmentDescriptor& seg, SegmentDescriptor& segRight) - { - return seg.Base == segRight.Base && seg.Limit == segRight.Limit; - } - }; - - using SegmentArray = Array; - - class GDTLoader final - { - public: - static Void Load(Register64& gdt); - static Void Load(Ref& gdt); - }; - - class IDTLoader final - { - public: - static Void Load(Register64& idt); - static Void Load(Ref& idt); - }; - - /***********************************************************************************/ - /// @brief Is the current config SMP aware? - /// @return True if YES, False if not. - /***********************************************************************************/ - - Bool mp_is_smp(Void) noexcept; - - /***********************************************************************************/ - /// @brief Fetch and enable SMP scheduler. - /// @param vendor_ptr SMP containing structure. - /***********************************************************************************/ - Void mp_init_cores(VoidPtr vendor_ptr) noexcept; - - /***********************************************************************************/ - /// @brief Do a cpuid to check if MSR exists on CPU. - /// @retval true it does exists. - /// @retval false it doesn't. - /***********************************************************************************/ - inline Bool hal_has_msr() noexcept - { - static UInt32 eax, unused, edx; // eax, edx - - __get_cpuid(1, &eax, &unused, &unused, &edx); - - // edx returns the flag for MSR (which is 1 shifted to 5.) - return edx & (1 << 5); - } - - UIntPtr hal_get_phys_address(VoidPtr virtual_address); - - /***********************************************************************************/ - /// @brief Get Model specific register inside core. - /// @param msr MSR - /// @param lo low byte - /// @param hi high byte - /***********************************************************************************/ - inline UInt32 hal_get_msr(UInt32 msr, UInt32* lo, UInt32* hi) noexcept - { - if (!lo || !hi) - return 0; - - asm volatile("rdmsr" - : "=a"(*lo), "=d"(*hi) - : "c"(msr)); - - return *lo + *hi; - } - - /// @brief Set Model-specific register. - /// @param msr MSR - /// @param lo low byte - /// @param hi high byte - Void hal_set_msr(UInt32 msr, UInt32 lo, UInt32 hi) noexcept; - - /// @brief Processor specific namespace. - namespace Detail - { - /* @brief TSS struct. */ - struct NE_TSS final - { - UInt32 fReserved1; - UInt64 fRsp0; - UInt64 fRsp1; - UInt64 fRsp2; - UInt64 fReserved2; - UInt64 fIst1; - UInt64 fIst2; - UInt64 fIst3; - UInt64 fIst4; - UInt64 fIst5; - UInt64 fIst6; - UInt64 fIst7; - UInt64 fReserved3; - UInt16 fReserved4; - UInt16 fIopb; - }; - - /** - @brief Global descriptor table entry, either null, code or data. - */ - - struct PACKED NE_GDT_ENTRY final - { - UInt16 fLimitLow; - UInt16 fBaseLow; - UInt8 fBaseMid; - UInt8 fAccessByte; - UInt8 fFlags; - UInt8 fBaseHigh; - }; - } // namespace Detail - - class APICController final - { - public: - explicit APICController(VoidPtr base); - ~APICController() = default; - - NE_COPY_DEFAULT(APICController) - - public: - UInt32 Read(UInt32 reg) noexcept; - Void Write(UInt32 reg, UInt32 value) noexcept; - - private: - VoidPtr fApic{nullptr}; - }; - - /// @brief Set a PTE from pd_base. - /// @param virt_addr a valid virtual address. - /// @param phys_addr point to physical address. - /// @param flags the flags to put on the page. - /// @return Status code of page manip. - EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags); - - EXTERN_C UInt8 rt_in8(UInt16 port); - EXTERN_C UInt16 rt_in16(UInt16 port); - EXTERN_C UInt32 rt_in32(UInt16 port); - - EXTERN_C Void rt_out16(UShort port, UShort byte); - EXTERN_C Void rt_out8(UShort port, UChar byte); - EXTERN_C Void rt_out32(UShort port, UInt byte); - - EXTERN_C Void rt_wait_400ns(); - EXTERN_C Void rt_halt(); - EXTERN_C Void rt_cli(); - EXTERN_C Void rt_sti(); - EXTERN_C Void rt_cld(); - EXTERN_C Void rt_std(); -} // namespace Kernel::HAL +#define kTrapGate (0xEF) +#define kTaskGate (0b10001100) +#define kIDTSelector (0x08) + +namespace Kernel { +namespace Detail::AMD64 { + struct PACKED InterruptDescriptorAMD64 final { + UInt16 OffsetLow; // offset bits 0..15 + UInt16 Selector; // a code segment selector in GDT or LDT + UInt8 Ist; + UInt8 TypeAttributes; + UInt16 OffsetMid; + UInt32 OffsetHigh; + UInt32 Zero; // reserved + }; +} // namespace Detail::AMD64 +} // namespace Kernel + +namespace Kernel::HAL { +/// @brief Memory Manager mapping flags. +enum { + kMMFlagsInvalid = 1 << 0, + kMMFlagsPresent = 1 << 1, + kMMFlagsWr = 1 << 2, + kMMFlagsUser = 1 << 3, + kMMFlagsNX = 1 << 4, + kMMFlagsPCD = 1 << 5, + kMMFlagsPwt = 1 << 6, + kMMFlagsCount = 4, +}; + +struct PACKED Register64 final { + UShort Limit; + UIntPtr Base; +}; + +using RawRegister = UInt64; +using Reg = RawRegister; +using InterruptId = UInt16; /* For each element in the IVT */ + +/// @brief Stack frame (as retrieved from assembly.) +struct PACKED StackFrame final { + RawRegister R8{0}; + RawRegister R9{0}; + RawRegister R10{0}; + RawRegister FS{0}; + RawRegister R12{0}; + RawRegister R13{0}; + RawRegister R14{0}; + RawRegister R15{0}; + RawRegister GS{0}; + RawRegister SP{0}; + RawRegister BP{0}; +}; + +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; + +class SegmentDescriptor final { + public: + UInt16 Base; + UInt8 BaseMiddle; + UInt8 BaseHigh; + + UShort Limit; + UChar Gran; + UChar AccessByte; +}; + +/*** + * @brief Segment Boolean operations + */ +class SegmentDescriptorComparator final { + public: + Bool IsValid(SegmentDescriptor& seg) { return seg.Base > seg.Limit; } + + Bool Equals(SegmentDescriptor& seg, SegmentDescriptor& segRight) { + return seg.Base == segRight.Base && seg.Limit == segRight.Limit; + } +}; + +using SegmentArray = Array; + +class GDTLoader final { + public: + static Void Load(Register64& gdt); + static Void Load(Ref& gdt); +}; + +class IDTLoader final { + public: + static Void Load(Register64& idt); + static Void Load(Ref& idt); +}; + +/***********************************************************************************/ +/// @brief Is the current config SMP aware? +/// @return True if YES, False if not. +/***********************************************************************************/ + +Bool mp_is_smp(Void) noexcept; + +/***********************************************************************************/ +/// @brief Fetch and enable SMP scheduler. +/// @param vendor_ptr SMP containing structure. +/***********************************************************************************/ +Void mp_init_cores(VoidPtr vendor_ptr) noexcept; + +/***********************************************************************************/ +/// @brief Do a cpuid to check if MSR exists on CPU. +/// @retval true it does exists. +/// @retval false it doesn't. +/***********************************************************************************/ +inline Bool hal_has_msr() noexcept { + static UInt32 eax, unused, edx; // eax, edx + + __get_cpuid(1, &eax, &unused, &unused, &edx); + + // edx returns the flag for MSR (which is 1 shifted to 5.) + return edx & (1 << 5); +} + +UIntPtr hal_get_phys_address(VoidPtr virtual_address); + +/***********************************************************************************/ +/// @brief Get Model specific register inside core. +/// @param msr MSR +/// @param lo low byte +/// @param hi high byte +/***********************************************************************************/ +inline UInt32 hal_get_msr(UInt32 msr, UInt32* lo, UInt32* hi) noexcept { + if (!lo || !hi) return 0; + + asm volatile("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr)); + + return *lo + *hi; +} + +/// @brief Set Model-specific register. +/// @param msr MSR +/// @param lo low byte +/// @param hi high byte +Void hal_set_msr(UInt32 msr, UInt32 lo, UInt32 hi) noexcept; + +/// @brief Processor specific namespace. +namespace Detail { + /* @brief TSS struct. */ + struct NE_TSS final { + UInt32 fReserved1; + UInt64 fRsp0; + UInt64 fRsp1; + UInt64 fRsp2; + UInt64 fReserved2; + UInt64 fIst1; + UInt64 fIst2; + UInt64 fIst3; + UInt64 fIst4; + UInt64 fIst5; + UInt64 fIst6; + UInt64 fIst7; + UInt64 fReserved3; + UInt16 fReserved4; + UInt16 fIopb; + }; + + /** + @brief Global descriptor table entry, either null, code or data. + */ + + struct PACKED NE_GDT_ENTRY final { + UInt16 fLimitLow; + UInt16 fBaseLow; + UInt8 fBaseMid; + UInt8 fAccessByte; + UInt8 fFlags; + UInt8 fBaseHigh; + }; +} // namespace Detail + +class APICController final { + public: + explicit APICController(VoidPtr base); + ~APICController() = default; + + NE_COPY_DEFAULT(APICController) + + public: + UInt32 Read(UInt32 reg) noexcept; + Void Write(UInt32 reg, UInt32 value) noexcept; + + private: + VoidPtr fApic{nullptr}; +}; + +/// @brief Set a PTE from pd_base. +/// @param virt_addr a valid virtual address. +/// @param phys_addr point to physical address. +/// @param flags the flags to put on the page. +/// @return Status code of page manip. +EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags); + +EXTERN_C UInt8 rt_in8(UInt16 port); +EXTERN_C UInt16 rt_in16(UInt16 port); +EXTERN_C UInt32 rt_in32(UInt16 port); + +EXTERN_C Void rt_out16(UShort port, UShort byte); +EXTERN_C Void rt_out8(UShort port, UChar byte); +EXTERN_C Void rt_out32(UShort port, UInt byte); + +EXTERN_C Void rt_wait_400ns(); +EXTERN_C Void rt_halt(); +EXTERN_C Void rt_cli(); +EXTERN_C Void rt_sti(); +EXTERN_C Void rt_cld(); +EXTERN_C Void rt_std(); +} // namespace Kernel::HAL EXTERN_C Kernel::Void idt_handle_generic(Kernel::UIntPtr rsp); EXTERN_C Kernel::Void idt_handle_gpf(Kernel::UIntPtr rsp); @@ -311,4 +283,4 @@ EXTERN_C ATTRIBUTE(naked) Kernel::Void hal_load_idt(Kernel::HAL::Register64 ptr) EXTERN_C ATTRIBUTE(naked) Kernel::Void hal_load_gdt(Kernel::HAL::Register64 ptr); inline Kernel::VoidPtr kKernelBitMpStart = nullptr; -inline Kernel::UIntPtr kKernelBitMpSize = 0UL; +inline Kernel::UIntPtr kKernelBitMpSize = 0UL; -- cgit v1.2.3 From 2ead335ccc7afd8e1b2a6533e966c10f49fbdfe9 Mon Sep 17 00:00:00 2001 From: Amlal Date: Mon, 28 Apr 2025 09:06:35 +0200 Subject: dev, kernel: HeFS, Tooling: reworked the mkfs.hefs tool for the new filesystem. other/related: - Add new KPC codes. - Final refactors for HeFS's Format method. - Dma pool improvements. - Better standard disk I/O names. - Add mm_memory_fence function inside HalPagingMgrAMD64.cc Signed-off-by: Amlal --- dev/boot/BootKit/HW/SATA.h | 2 +- dev/kernel/FSKit/HeFS.h | 36 ++++++------- dev/kernel/HALKit/AMD64/HalKernelMain.cc | 2 +- dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc | 16 ++++-- dev/kernel/HALKit/AMD64/Processor.h | 2 + dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 14 +++--- dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc | 6 +-- dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc | 4 +- dev/kernel/KernelKit/FileMgr.h | 2 +- dev/kernel/KernelKit/KPC.h | 6 ++- dev/kernel/NewKit/KString.h | 2 +- dev/kernel/StorageKit/DmaPool.h | 21 ++++++-- dev/kernel/src/DriveMgr.cc | 2 +- dev/kernel/src/FS/HeFS+FileMgr.cc | 4 +- dev/kernel/src/FS/HeFS.cc | 67 ++++++++++++++----------- dev/kernel/src/FS/NeFS.cc | 8 +-- dev/kernel/src/Utils.cc | 1 - dev/modules/AHCI/AHCI.h | 4 +- dev/modules/ATA/ATA.h | 4 +- dev/user/ProcessCodes.h | 1 + docs/tex/hefs.tex | 2 +- tooling/hefs.h | 67 +++++++++++-------------- tooling/mkfs.hefs.cc | 24 ++++----- 23 files changed, 161 insertions(+), 136 deletions(-) (limited to 'dev/kernel/HALKit/AMD64/Processor.h') diff --git a/dev/boot/BootKit/HW/SATA.h b/dev/boot/BootKit/HW/SATA.h index 5c70c18c..8cf48ec7 100644 --- a/dev/boot/BootKit/HW/SATA.h +++ b/dev/boot/BootKit/HW/SATA.h @@ -28,7 +28,7 @@ class BootDeviceSATA final { operator bool() { return this->Leak().mDetected; } - SizeT GetDiskSize() { return drv_get_size(); } + SizeT GetDiskSize() { return drv_std_get_size(); } constexpr static auto kSectorSize = kAHCISectorSize; diff --git a/dev/kernel/FSKit/HeFS.h b/dev/kernel/FSKit/HeFS.h index 90a2e274..515034fc 100644 --- a/dev/kernel/FSKit/HeFS.h +++ b/dev/kernel/FSKit/HeFS.h @@ -28,12 +28,6 @@ #define kHeFSDefaultVoluneName u8"HeFS Volume" -#define kHeFSDIMBootDir u8"boot-x/dir" -#define kHeFSMIMEBootFile u8"boot-x/file" - -#define kHeFSDIMSystemDir u8"system-x/dir" -#define kHeFSMIMESystemFile u8"system-x/file" - #define kHeFSSearchAllStr u8"*" struct HEFS_BOOT_NODE; @@ -101,19 +95,19 @@ typedef UInt64 ATime; /// @details Acts like a superblock, it contains the information about the filesystem. /// @note The boot node is the first block of the filesystem. struct PACKED HEFS_BOOT_NODE final { - Kernel::Char fMagic[kHeFSMagicLen]; /// @brief Magic number of the filesystem. + Kernel::Char fMagic[kHeFSMagicLen]; /// @brief Magic number of the filesystem. Kernel::Utf8Char fVolName[kHeFSPartNameLen]; /// @brief Volume name. - Kernel::UInt32 fVersion; /// @brief Version of the filesystem. - Kernel::UInt64 fBadSectors; /// @brief Number of bad sectors in the filesystem. - Kernel::UInt64 fSectorCount; /// @brief Number of sectors in the filesystem. - Kernel::UInt64 fSectorSize; /// @brief Size of the sector. - Kernel::UInt32 fChecksum; /// @brief Checksum of the boot node. - Kernel::UInt8 fDiskKind; /// @brief Kind of the drive. (Hard Drive, Solid State Drive, Optical - /// Drive, etc). - Kernel::UInt8 fEncoding; /// @brief Encoding of the filesystem. (UTF-8, UTF-16, etc). - Kernel::UInt64 fStartIND; /// @brief Start of the INode tree. - Kernel::UInt64 fEndIND; /// @brief End of the INode tree. - Kernel::UInt64 fINDCount; /// @brief Number of leafs in the INode tree. + Kernel::UInt32 fVersion; /// @brief Version of the filesystem. + Kernel::UInt64 fBadSectors; /// @brief Number of bad sectors in the filesystem. + Kernel::UInt64 fSectorCount; /// @brief Number of sectors in the filesystem. + Kernel::UInt64 fSectorSize; /// @brief Size of the sector. + Kernel::UInt32 fChecksum; /// @brief Checksum of the boot node. + Kernel::UInt8 fDiskKind; /// @brief Kind of the drive. (Hard Drive, Solid State Drive, Optical + /// Drive, etc). + Kernel::UInt8 fEncoding; /// @brief Encoding of the filesystem. (UTF-8, UTF-16, etc). + Kernel::UInt64 fStartIND; /// @brief Start of the INode tree. + Kernel::UInt64 fEndIND; /// @brief End of the INode tree. + Kernel::UInt64 fINDCount; /// @brief Number of leafs in the INode tree. Kernel::UInt64 fDiskSize; /// @brief Size of the disk. (Could be a virtual size, that is not the /// real size of the disk.) Kernel::UInt16 fDiskStatus; /// @brief Status of the disk. (locked, unlocked, error, invalid). @@ -124,7 +118,7 @@ struct PACKED HEFS_BOOT_NODE final { Kernel::UInt64 fReserved2; /// @brief Reserved for future use. Kernel::UInt64 fReserved3; /// @brief Reserved for future use. Kernel::UInt64 fReserved4; /// @brief Reserved for future use. - Kernel::Char fPad[272]; + Kernel::Char fPad[272]; }; inline constexpr Kernel::ATime kHeFSTimeInvalid = 0x0000000000000000; @@ -136,11 +130,11 @@ inline constexpr Kernel::ATime kHeFSTimeMax = 0xFFFFFFFFFFFFFFFF - 1; /// @note The index node is used to store the file information of a file. struct PACKED ALIGN(8) HEFS_INDEX_NODE final { Kernel::Utf8Char fName[kHeFSFileNameLen]; /// @brief File name. - Kernel::UInt32 fFlags; /// @brief File flags. + Kernel::UInt32 fFlags; /// @brief File flags. Kernel::UInt16 fKind; /// @brief File kind. (Regular, Directory, Block, Character, FIFO, Socket, /// Symbolic Link, Unknown). Kernel::UInt32 fSize; /// @brief File size. - Kernel::UInt32 fChecksum; /// @brief Checksum. + Kernel::UInt32 fChecksum; /// @brief Checksum. Kernel::Boolean fSymLink; /// @brief Is this a symbolic link? (if yes, the fName is the path to /// the file and blocklinkstart and end contains it's inodes.) diff --git a/dev/kernel/HALKit/AMD64/HalKernelMain.cc b/dev/kernel/HALKit/AMD64/HalKernelMain.cc index 66793aa6..a6bf09c7 100644 --- a/dev/kernel/HALKit/AMD64/HalKernelMain.cc +++ b/dev/kernel/HALKit/AMD64/HalKernelMain.cc @@ -142,4 +142,4 @@ EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept { ++team_index; } } -#endif // ifndef __NE_MODULAR_KERNEL_COMPONENTS__ \ No newline at end of file +#endif // ifndef __NE_MODULAR_KERNEL_COMPONENTS__ \ No newline at end of file diff --git a/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc b/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc index d7a8baf6..b6176e39 100644 --- a/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc +++ b/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc @@ -93,6 +93,18 @@ UIntPtr hal_get_phys_address(VoidPtr virt) { return (pte->PhysicalAddress << 12) | (kVMAddr & 0xFFF); } +/***********************************************************************************/ +/// @brief clflush+mfence helper function. +/***********************************************************************************/ +EXTERN_C Int32 mm_memory_fence(VoidPtr virtual_address) { + if (!virtual_address || !hal_get_phys_address(virtual_address)) return kErrorInvalidData; + + asm volatile("clflush (%0)" : : "r"(virtual_address) : "memory"); + asm volatile("mfence" ::: "memory"); + + return kErrorSuccess; +} + /***********************************************************************************/ /// @brief Maps or allocates a page from virtual_address. /// @param virtual_address a valid virtual address. @@ -136,9 +148,7 @@ EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UI hal_invl_tlb(virtual_address); - asm volatile("clflush (%0)" : : "r"(virtual_address) : "memory"); - - asm volatile("mfence" ::: "memory"); + mm_memory_fence(virtual_address); mmi_page_status(pte); diff --git a/dev/kernel/HALKit/AMD64/Processor.h b/dev/kernel/HALKit/AMD64/Processor.h index 1319277f..b57e9abf 100644 --- a/dev/kernel/HALKit/AMD64/Processor.h +++ b/dev/kernel/HALKit/AMD64/Processor.h @@ -272,6 +272,8 @@ EXTERN_C Void rt_cli(); EXTERN_C Void rt_sti(); EXTERN_C Void rt_cld(); EXTERN_C Void rt_std(); + +EXTERN_C Int32 mm_memory_fence(VoidPtr virtual_address); } // namespace Kernel::HAL EXTERN_C Kernel::Void idt_handle_generic(Kernel::UIntPtr rsp); diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index 06c7781c..050826e3 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -257,7 +257,7 @@ SizeT drv_get_sector_count_ahci() { /// @brief Get the drive size. /// @return Disk size in bytes. SizeT drv_get_size_ahci() { - return drv_get_sector_count() * kAHCISectorSize; + return drv_std_get_sector_count() * kAHCISectorSize; } /// @brief Enable Host and probe using the IDENTIFY command. @@ -426,16 +426,16 @@ Bool drv_std_detected_ahci() { /// //////////////////////////////////////////////////// Void drv_std_write(UInt64 lba, Char* buffer, SizeT sector_sz, SizeT size_buffer) { - drv_std_input_output_ahci(lba / sector_sz, reinterpret_cast(buffer), sector_sz, - size_buffer); + drv_std_input_output_ahci(lba / sector_sz, reinterpret_cast(buffer), + sector_sz, size_buffer); } //////////////////////////////////////////////////// /// //////////////////////////////////////////////////// Void drv_std_read(UInt64 lba, Char* buffer, SizeT sector_sz, SizeT size_buffer) { - drv_std_input_output_ahci(lba / sector_sz, reinterpret_cast(buffer), sector_sz, - size_buffer); + drv_std_input_output_ahci(lba / sector_sz, reinterpret_cast(buffer), + sector_sz, size_buffer); } //////////////////////////////////////////////////// @@ -459,7 +459,7 @@ Bool drv_std_detected(Void) { @return Sector size in bytes. */ //////////////////////////////////////////////////// -SizeT drv_get_sector_count() { +SizeT drv_std_get_sector_count() { return drv_get_sector_count_ahci(); } @@ -467,7 +467,7 @@ SizeT drv_get_sector_count() { /// @brief Get the drive size. /// @return Disk size in bytes. //////////////////////////////////////////////////// -SizeT drv_get_size() { +SizeT drv_std_get_size() { return drv_get_size_ahci(); } diff --git a/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc index ca3e9ee4..4688203f 100644 --- a/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc @@ -183,15 +183,15 @@ Boolean drv_std_detected(Void) { @return Number of sectors, or zero. */ /***********************************************************************************/ -Kernel::SizeT drv_get_sector_count() { +Kernel::SizeT drv_std_get_sector_count() { return (kATAIdentifyData[61] << 16) | kATAIdentifyData[60]; } /***********************************************************************************/ /// @brief Get the size of the current drive. /***********************************************************************************/ -Kernel::SizeT drv_get_size() { - return (drv_get_sector_count()) * kATASectorSize; +Kernel::SizeT drv_std_get_size() { + return (drv_std_get_sector_count()) * kATASectorSize; } #endif /* ifdef __ATA_DMA__ */ diff --git a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc index a024d2fe..e57494a7 100644 --- a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc @@ -245,11 +245,11 @@ Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorS drv_pio_std_write(Lba, IO, Master, Buf, SectorSz, Size); } -SizeT drv_get_size() { +SizeT drv_std_get_size() { return drv_pio_get_size(); } -SizeT drv_get_sector_count() { +SizeT drv_std_get_sector_count() { return drv_pio_get_sector_count(); } diff --git a/dev/kernel/KernelKit/FileMgr.h b/dev/kernel/KernelKit/FileMgr.h index 3dff61b0..2c4b2055 100644 --- a/dev/kernel/KernelKit/FileMgr.h +++ b/dev/kernel/KernelKit/FileMgr.h @@ -59,7 +59,7 @@ namespace Kernel { enum { - kFileIOInvalid = 0, + kFileIOInvalid = 0, kFileWriteAll = 100, kFileReadAll = 101, kFileReadChunk = 102, diff --git a/dev/kernel/KernelKit/KPC.h b/dev/kernel/KernelKit/KPC.h index e195d5ad..9de1f70f 100644 --- a/dev/kernel/KernelKit/KPC.h +++ b/dev/kernel/KernelKit/KPC.h @@ -61,7 +61,11 @@ inline constexpr KPCError kErrorInvalidCreds = 61; inline constexpr KPCError kErrorCDTrayBroken = 62; inline constexpr KPCError kErrorUnrecoverableDisk = 63; inline constexpr KPCError kErrorFileLocked = 64; -inline constexpr KPCError kErrorUnimplemented = -1; +inline constexpr KPCError kErrorDiskIsTooTiny = 65; +/// Kernel errors. +inline constexpr KPCError kErrorDmaExhausted = 101; +/// Generic errors. +inline constexpr KPCError kErrorUnimplemented = -1; /// @brief Does a system wide bug check. /// @param void no params are needed. diff --git a/dev/kernel/NewKit/KString.h b/dev/kernel/NewKit/KString.h index a9a58b7e..08f783c0 100644 --- a/dev/kernel/NewKit/KString.h +++ b/dev/kernel/NewKit/KString.h @@ -71,7 +71,7 @@ class KString final { }; class KStringBuilder final { -public: + public: static ErrorOr Construct(const Char* data); static const Char* FromBool(const Char* fmt, bool n); static const Char* Format(const Char* fmt, const Char* from); diff --git a/dev/kernel/StorageKit/DmaPool.h b/dev/kernel/StorageKit/DmaPool.h index 9e59910a..09851a0c 100644 --- a/dev/kernel/StorageKit/DmaPool.h +++ b/dev/kernel/StorageKit/DmaPool.h @@ -23,6 +23,7 @@ #pragma once #include +#include "NewKit/Defines.h" #ifdef __NE_AMD64__ #define NE_DMA_POOL_START (0x1000000) @@ -30,11 +31,14 @@ namespace Kernel { /// @brief DMA pool base pointer, here we're sure that AHCI or whatever tricky standard sees it. -inline UInt8* kDmaPoolPtr = (UInt8*) NE_DMA_POOL_START; +inline UInt8* kDmaPoolPtr = (UInt8*) NE_DMA_POOL_START; +inline const UInt8* kDmaPoolEnd = (UInt8*) (NE_DMA_POOL_START + NE_DMA_POOL_SIZE); +/***********************************************************************************/ /// @brief allocate from the rtl_dma_alloc system. /// @param size the size of the chunk to allocate. /// @param align alignement of pointer. +/***********************************************************************************/ inline VoidPtr rtl_dma_alloc(SizeT size, SizeT align) { if (!size) { return nullptr; @@ -42,11 +46,14 @@ inline VoidPtr rtl_dma_alloc(SizeT size, SizeT align) { UIntPtr addr = (UIntPtr) kDmaPoolPtr; - /// here we just align the address according to a `align` variable, i'd rather be a power of two really. + /// here we just align the address according to a `align` variable, i'd rather be a power of two + /// really. addr = (addr + (align - 1)) & ~(align - 1); - if ((addr + size) >= (NE_DMA_POOL_START + NE_DMA_POOL_SIZE)) { - kout << "DMA Pool exhausted!\r"; + if ((addr + size) >= reinterpret_cast(kDmaPoolEnd)) { + kout << "DMA Pool is exhausted!\r"; + + err_global_get() = kErrorDmaExhausted; return nullptr; } @@ -55,12 +62,18 @@ inline VoidPtr rtl_dma_alloc(SizeT size, SizeT align) { return (VoidPtr) addr; } +/***********************************************************************************/ +/// @brief Free DMA pointer. +/***********************************************************************************/ inline Void rtl_dma_free(SizeT size) { if (!size) return; kDmaPoolPtr = (UInt8*) (kDmaPoolPtr - size); } +/***********************************************************************************/ +/// @brief Flush DMA pointer. +/***********************************************************************************/ inline Void rtl_dma_flush(VoidPtr ptr, SizeT size_buffer) { if (ptr > (Void*) (NE_DMA_POOL_START + NE_DMA_POOL_SIZE)) { return; diff --git a/dev/kernel/src/DriveMgr.cc b/dev/kernel/src/DriveMgr.cc index 46a5c588..b0f78ae4 100644 --- a/dev/kernel/src/DriveMgr.cc +++ b/dev/kernel/src/DriveMgr.cc @@ -193,7 +193,7 @@ namespace Detail { trait.fKind = kMassStorageDrive | kUnformattedDrive | kReadOnlyDrive; trait.fSectorSz = 512; - trait.fLbaEnd = drv_get_sector_count() - 1; + trait.fLbaEnd = drv_std_get_sector_count() - 1; trait.fLbaStart = 0x400; } } diff --git a/dev/kernel/src/FS/HeFS+FileMgr.cc b/dev/kernel/src/FS/HeFS+FileMgr.cc index d7530d8f..e0b92a8d 100644 --- a/dev/kernel/src/FS/HeFS+FileMgr.cc +++ b/dev/kernel/src/FS/HeFS+FileMgr.cc @@ -10,5 +10,5 @@ #include #include -#endif // ifdef __FSKIT_INCLUDES_HEFS__ -#endif // ifndef __NE_MINIMAL_OS__ +#endif // ifdef __FSKIT_INCLUDES_HEFS__ +#endif // ifndef __NE_MINIMAL_OS__ diff --git a/dev/kernel/src/FS/HeFS.cc b/dev/kernel/src/FS/HeFS.cc index 37c2f8fb..f4776686 100644 --- a/dev/kernel/src/FS/HeFS.cc +++ b/dev/kernel/src/FS/HeFS.cc @@ -756,7 +756,7 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input NE_UNUSED(flags); NE_UNUSED(part_name); - // verify disk. + // Verify Disk. drive->fVerify(drive->fPacket); // if disk isn't good, then error out. @@ -765,6 +765,12 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input return false; } + if (drv_std_get_size() < kHeFSMinimumDiskSize) { + err_global_get() = kErrorDiskIsTooTiny; + kout << "Error: Failed to allocate memory for boot node.\r"; + return NO; + } + HEFS_BOOT_NODE* root = new HEFS_BOOT_NODE(); if (!root) { @@ -806,11 +812,11 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input rt_string_len("fs/hefs-packet")); urt_copy_memory((VoidPtr) part_name, root->fVolName, urt_string_len(part_name)); - rt_copy_memory((VoidPtr) kHeFSMagic, root->fMagic, sizeof(kHeFSMagic)); + rt_copy_memory((VoidPtr) kHeFSMagic, root->fMagic, kHeFSMagicLen - 1); root->fBadSectors = 0; - root->fSectorCount = drv_get_sector_count(); + root->fSectorCount = drv_std_get_sector_count(); root->fSectorSize = drive->fSectorSz; @@ -821,7 +827,7 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input root->fINDCount = 0; - root->fDiskSize = drv_get_size(); + root->fDiskSize = drv_std_get_size(); root->fDiskStatus = kHeFSStatusUnlocked; root->fDiskFlags = flags; @@ -844,6 +850,8 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input root->fVID = kHeFSInvalidVID; + root->fChecksum = ke_calculate_crc32((Char*) root, sizeof(HEFS_BOOT_NODE)); + drive->fPacket.fPacketLba = start; drive->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); drive->fPacket.fPacketContent = root; @@ -861,52 +869,55 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input start = root->fStartIND; - constexpr SizeT kHeFSPreallocateCount = 16UL; + constexpr SizeT kHeFSPreallocateCount = 0x10UL; - HEFS_INDEX_NODE_DIRECTORY* index_node = new HEFS_INDEX_NODE_DIRECTORY(); + HEFS_INDEX_NODE_DIRECTORY* dir = new HEFS_INDEX_NODE_DIRECTORY(); // Pre-allocate index node directory tree for (SizeT i = 0; i < kHeFSPreallocateCount; ++i) { - rt_set_memory(index_node, 0, sizeof(HEFS_INDEX_NODE_DIRECTORY)); - urt_copy_memory((VoidPtr) u8"?", index_node->fName, urt_string_len(u8"?")); + rt_set_memory(dir, 0, sizeof(HEFS_INDEX_NODE_DIRECTORY)); + urt_copy_memory((VoidPtr) u8".deleted", dir->fName, urt_string_len(u8".deleted")); + + dir->fFlags = flags; + dir->fKind = kHeFSFileKindDirectory; - index_node->fFlags = flags; - index_node->fKind = kHeFSFileKindDirectory; + dir->fDeleted = kHeFSTimeMax; /// TODO: Add current time. - index_node->fDeleted = kHeFSTimeMax; + dir->fEntryCount = 0; - index_node->fEntryCount = 1; + dir->fIndexNodeChecksum = 0; - index_node->fChecksum = 0; - index_node->fIndexNodeChecksum = 0; + dir->fUID = 0; + dir->fGID = 0; + dir->fMode = 0; - index_node->fUID = 0; - index_node->fGID = 0; - index_node->fMode = 0; + dir->fColor = kHeFSBlack; + dir->fChild = 0; + dir->fParent = 0; + dir->fNext = 0; + dir->fPrev = 0; - index_node->fColor = kHeFSBlack; - index_node->fChild = 0; - index_node->fParent = 0; - index_node->fNext = 0; - index_node->fPrev = 0; + dir->fChecksum = ke_calculate_crc32((Char*) dir, sizeof(HEFS_INDEX_NODE_DIRECTORY)); drive->fPacket.fPacketLba = start; drive->fPacket.fPacketSize = sizeof(HEFS_INDEX_NODE_DIRECTORY); - drive->fPacket.fPacketContent = index_node; + drive->fPacket.fPacketContent = dir; start += sizeof(HEFS_INDEX_NODE_DIRECTORY); drive->fOutput(drive->fPacket); } - delete index_node; - index_node = nullptr; + delete dir; + dir = nullptr; delete root; root = nullptr; Detail::hefsi_balance_filesystem(root, drive); + err_global_get() = kErrorSuccess; + if (drive->fPacket.fPacketGood) return YES; err_global_get() = kErrorDiskIsCorrupted; @@ -953,7 +964,7 @@ _Output Bool HeFileSystemParser::CreateDirectory(_Input DriveTrait* drive, _Inpu Detail::hefsi_balance_filesystem(root, drive); - auto dirent = new HEFS_INDEX_NODE_DIRECTORY(); + HEFS_INDEX_NODE_DIRECTORY* dirent = new HEFS_INDEX_NODE_DIRECTORY(); rt_set_memory(dirent, 0, sizeof(HEFS_INDEX_NODE_DIRECTORY)); @@ -969,7 +980,7 @@ _Output Bool HeFileSystemParser::CreateDirectory(_Input DriveTrait* drive, _Inpu dirent->fFlags = flags; dirent->fChecksum = 0; - dirent->fEntryCount = 1; + dirent->fEntryCount = 0; if (Detail::hefs_allocate_index_directory_node(root, drive, dirent)) { delete dirent; @@ -1093,7 +1104,7 @@ Boolean fs_init_hefs(Void) noexcept { HeFileSystemParser parser; - parser.Format(&drv, kHeFSEncodingUTF16, kHeFSDefaultVoluneName); + parser.Format(&drv, kHeFSEncodingUTF8, kHeFSDefaultVoluneName); return YES; } diff --git a/dev/kernel/src/FS/NeFS.cc b/dev/kernel/src/FS/NeFS.cc index e68a4035..d572bffc 100644 --- a/dev/kernel/src/FS/NeFS.cc +++ b/dev/kernel/src/FS/NeFS.cc @@ -32,12 +32,12 @@ using namespace Kernel; /***********************************************************************************/ /// @brief get sector count. /***********************************************************************************/ -Kernel::SizeT drv_get_sector_count(); +Kernel::SizeT drv_std_get_sector_count(); /***********************************************************************************/ /// @brief get device size. /***********************************************************************************/ -Kernel::SizeT drv_get_size(); +Kernel::SizeT drv_std_get_size(); #endif @@ -455,8 +455,8 @@ bool NeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input const I rt_copy_memory((VoidPtr) kNeFSUntitledHD, (VoidPtr) part_block->PartitionName, rt_string_len(kNeFSUntitledHD)); - SizeT sectorCount = drv_get_sector_count(); - SizeT diskSize = drv_get_size(); + SizeT sectorCount = drv_std_get_sector_count(); + SizeT diskSize = drv_std_get_size(); part_block->Version = kNeFSVersionInteger; diff --git a/dev/kernel/src/Utils.cc b/dev/kernel/src/Utils.cc index ea0ff7a7..4f47849b 100644 --- a/dev/kernel/src/Utils.cc +++ b/dev/kernel/src/Utils.cc @@ -193,4 +193,3 @@ EXTERN_C void* memcpy(void* dst, const void* src, long long unsigned int len) { EXTERN_C Kernel::Int32 strcmp(const char* dst, const char* src) { return Kernel::rt_string_cmp(dst, src, Kernel::rt_string_len(dst)); } - diff --git a/dev/modules/AHCI/AHCI.h b/dev/modules/AHCI/AHCI.h index 98fb3d60..7303e8c5 100644 --- a/dev/modules/AHCI/AHCI.h +++ b/dev/modules/AHCI/AHCI.h @@ -349,10 +349,10 @@ Kernel::Void drv_std_write(Kernel::UInt64 lba, Kernel::Char* buf, Kernel::SizeT Kernel::SizeT buf_sz); /// @brief Gets the sector count from AHCI disk. -Kernel::SizeT drv_get_sector_count(); +Kernel::SizeT drv_std_get_sector_count(); /// @brief Gets the AHCI disk size. -Kernel::SizeT drv_get_size(); +Kernel::SizeT drv_std_get_size(); /// @brief Checks if the drive has completed the command. Kernel::Bool drv_is_ready(void); diff --git a/dev/modules/ATA/ATA.h b/dev/modules/ATA/ATA.h index bd21d106..e7cf200f 100644 --- a/dev/modules/ATA/ATA.h +++ b/dev/modules/ATA/ATA.h @@ -149,9 +149,9 @@ Kernel::Void drv_std_write(Kernel::UInt64 lba, Kernel::UInt16 io, Kernel::UInt8 Kernel::Char* buf, Kernel::SizeT sec_sz, Kernel::SizeT buf_sz); /// @brief get sector count. -Kernel::SizeT drv_get_sector_count(); +Kernel::SizeT drv_std_get_sector_count(); /// @brief get device size. -Kernel::SizeT drv_get_size(); +Kernel::SizeT drv_std_get_size(); #endif // ifdef __NEOSKRNL__ \ No newline at end of file diff --git a/dev/user/ProcessCodes.h b/dev/user/ProcessCodes.h index 74b50c68..90457944 100644 --- a/dev/user/ProcessCodes.h +++ b/dev/user/ProcessCodes.h @@ -51,6 +51,7 @@ inline constexpr ErrRef kErrorInvalidCreds = 61; inline constexpr ErrRef kErrorCDTrayBroken = 62; inline constexpr ErrRef kErrorUnrecoverableDisk = 63; inline constexpr ErrRef kErrorFileLocked = 64; +inline constexpr ErrRef kErrorDiskIsTooTiny = 65; inline constexpr ErrRef kErrorUnimplemented = -1; /// @brief The last error reported by the system to the process. diff --git a/docs/tex/hefs.tex b/docs/tex/hefs.tex index 75f98bcc..f3c2d6bf 100644 --- a/docs/tex/hefs.tex +++ b/docs/tex/hefs.tex @@ -26,7 +26,7 @@ \title{HeFS (High-Throughput Extended File System) Specification} \author{Amlal El Mahrouss} -\date{2024--2025} +\date{2024-2025} \begin{document} diff --git a/tooling/hefs.h b/tooling/hefs.h index d0ad823b..2a04a1c3 100644 --- a/tooling/hefs.h +++ b/tooling/hefs.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -16,17 +16,11 @@ #define kHeFSFileNameLen (256U) #define kHeFSPartNameLen (128U) -#define kHeFSMinimumDiskSize (gib_cast(4)) - -#define kHeFSDefaultVoluneName u"HeFS Volume" - -#define kHeFSDIMBootDir u"boot-x/dir" -#define kHeFSMIMEBootFile u"boot-x/file" - -#define kHeFSDIMSystemDir u"system-x/dir" -#define kHeFSMIMESystemFile u"system-x/file" +#define kHeFSDefaultVoluneName u8"HeFS Volume" namespace mkfs::hefs { + +// Drive kinds enum { kHeFSHardDrive = 0xC0, // Hard Drive kHeFSSolidStateDrive = 0xC1, // Solid State Drive @@ -38,6 +32,7 @@ enum { kHeFSDriveCount = 7, }; +// Disk status enum { kHeFSStatusUnlocked = 0x18, kHeFSStatusLocked, @@ -46,6 +41,7 @@ enum { kHeFSStatusCount, }; +// Encodings enum { kHeFSEncodingUTF8 = 0x00, kHeFSEncodingUTF16, @@ -60,18 +56,13 @@ enum { kHeFSEncodingCount, }; -// Constants +// Block constants constexpr std::size_t kHeFSBlockCount = 16; -// Types +// Time type using ATime = std::uint64_t; -enum { - kHeFSRed = 100, - kHeFSBlack, - kHeFSColorCount, -}; - +// File kinds inline constexpr uint16_t kHeFSFileKindRegular = 0x00; inline constexpr uint16_t kHeFSFileKindDirectory = 0x01; inline constexpr uint16_t kHeFSFileKindBlock = 0x02; @@ -82,14 +73,22 @@ inline constexpr uint16_t kHeFSFileKindSymbolicLink = 0x06; inline constexpr uint16_t kHeFSFileKindUnknown = 0x07; inline constexpr uint16_t kHeFSFileKindCount = 0x08; -// Basic Time Constants +// Red-black tree colors +enum { + kHeFSInvalidColor = 0, + kHeFSRed = 100, + kHeFSBlack, + kHeFSColorCount, +}; + +// Time constants inline constexpr ATime kHeFSTimeInvalid = 0x0000000000000000; inline constexpr ATime kHeFSTimeMax = 0xFFFFFFFFFFFFFFFF - 1; -// Boot Node (Superblock Equivalent) +// Boot Node struct alignas(8) BootNode { char magic[kHeFSMagicLen]{}; - char16_t volumeName[kHeFSPartNameLen]{}; + char8_t volumeName[kHeFSPartNameLen]{}; std::uint32_t version{}; std::uint64_t badSectors{}; std::uint64_t sectorCount{}; @@ -108,19 +107,16 @@ struct alignas(8) BootNode { std::uint64_t reserved2{}; std::uint64_t reserved3{}; std::uint64_t reserved4{}; + char pad[272]{}; }; -// File Node (Index Node) +// Index Node struct alignas(8) IndexNode { - char16_t name[kHeFSFileNameLen]{}; + char8_t name[kHeFSFileNameLen]{}; std::uint32_t flags{}; std::uint16_t kind{}; std::uint32_t size{}; std::uint32_t checksum{}; - std::uint32_t recoverChecksum{}; - std::uint32_t blockChecksum{}; - std::uint32_t linkChecksum{}; - char16_t mime[kHeFSFileNameLen]{}; bool symbolicLink{false}; ATime created{}; ATime accessed{}; @@ -129,23 +125,18 @@ struct alignas(8) IndexNode { std::uint32_t uid{}; std::uint32_t gid{}; std::uint32_t mode{}; - std::uint64_t blockLinkStart[kHeFSBlockCount]{}; - std::uint64_t blockLinkEnd[kHeFSBlockCount]{}; - std::uint64_t blockStart[kHeFSBlockCount]{}; - std::uint64_t blockEnd[kHeFSBlockCount]{}; - std::uint64_t blockRecoveryStart[kHeFSBlockCount]{}; - std::uint64_t blockRecoveryEnd[kHeFSBlockCount]{}; + std::uint64_t block[kHeFSBlockCount]{}; + char pad[62]{}; }; -// Directory Node (Red-Black Tree Node) +// Index Node Directory (Red-Black Tree Node) struct alignas(8) IndexNodeDirectory { - char16_t name[kHeFSFileNameLen]{}; + char8_t name[kHeFSFileNameLen]{}; std::uint32_t flags{}; std::uint16_t kind{}; std::uint32_t entryCount{}; std::uint32_t checksum{}; std::uint32_t indexNodeChecksum{}; - char16_t dim[kHeFSFileNameLen]{}; ATime created{}; ATime accessed{}; ATime modified{}; @@ -153,13 +144,13 @@ struct alignas(8) IndexNodeDirectory { std::uint32_t uid{}; std::uint32_t gid{}; std::uint32_t mode{}; - std::uint64_t indexNodeStart[kHeFSBlockCount]{}; - std::uint64_t indexNodeEnd[kHeFSBlockCount]{}; + std::uint64_t indexNode[kHeFSBlockCount]{}; std::uint8_t color{}; std::uint64_t next{}; std::uint64_t prev{}; std::uint64_t child{}; std::uint64_t parent{}; + char pad[32]{}; }; } // namespace mkfs::hefs diff --git a/tooling/mkfs.hefs.cc b/tooling/mkfs.hefs.cc index 542ae175..3147a6e6 100644 --- a/tooling/mkfs.hefs.cc +++ b/tooling/mkfs.hefs.cc @@ -28,10 +28,10 @@ static std::basic_string get_option(const std::basic_string& } } // namespace mkfs::detail -static size_t kDiskSize = 1024 * 1024 * 1024 * 4UL; -static uint16_t kVersion = kHeFSVersion; -static std::u16string kLabel = kHeFSDefaultVoluneName; -static size_t kSectorSize = 512; +static size_t kDiskSize = 1024 * 1024 * 1024 * 4UL; +static uint16_t kVersion = kHeFSVersion; +static std::u8string kLabel = kHeFSDefaultVoluneName; +static size_t kSectorSize = 512; int main(int argc, char** argv) { if (argc < 2) { @@ -42,8 +42,8 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - std::string args; - std::u16string args_wide; + std::string args; + std::u8string args_wide; for (int i = 1; i < argc; ++i) { args += argv[i]; @@ -55,13 +55,13 @@ int main(int argc, char** argv) { args_wide.push_back(ch); } - args_wide += u" "; + args_wide += u8" "; } auto output_device = mkfs::detail::get_option(args, "-o"); kSectorSize = std::strtol(mkfs::detail::get_option(args, "-s").data(), nullptr, 10); - kLabel = mkfs::detail::get_option(args_wide, u"-L"); + kLabel = mkfs::detail::get_option(args_wide, u8"-L"); if (kLabel.empty()) kLabel = kHeFSDefaultVoluneName; @@ -91,14 +91,14 @@ int main(int argc, char** argv) { bootNode.version = kVersion; bootNode.diskKind = mkfs::hefs::kHeFSHardDrive; - bootNode.encoding = mkfs::hefs::kHeFSEncodingUTF16; + bootNode.encoding = mkfs::hefs::kHeFSEncodingUTF8; bootNode.diskSize = kDiskSize; bootNode.sectorSize = kSectorSize; bootNode.startIND = start_ind; bootNode.endIND = end_ind; bootNode.diskStatus = mkfs::hefs::kHeFSStatusUnlocked; - std::memcpy(bootNode.magic, kHeFSMagic, kHeFSMagicLen); + std::memcpy(bootNode.magic, kHeFSMagic, kHeFSMagicLen - 1); std::memcpy(bootNode.volumeName, kLabel.data(), kLabel.size() * sizeof(char16_t)); filesystem.seekp(std::strtol(mkfs::detail::get_option(args, "-p").data(), nullptr, 10)); @@ -121,9 +121,9 @@ int main(int argc, char** argv) { for (size_t i = 0; i < cnt; ++i) { mkfs::hefs::IndexNodeDirectory indexNode{}; - std::memcpy(indexNode.name, u"/", std::u16string(u"/").size() * sizeof(char16_t)); + std::memcpy(indexNode.name, u8"/", std::u8string(u8"/").size() * sizeof(char16_t)); - indexNode.flags = mkfs::hefs::kHeFSEncodingUTF16; + indexNode.flags = mkfs::hefs::kHeFSEncodingUTF8; indexNode.kind = mkfs::hefs::kHeFSFileKindDirectory; indexNode.deleted = mkfs::hefs::kHeFSTimeMax; -- cgit v1.2.3 From fcea594f2df75dcd0f3d4bfeac988b748be89c13 Mon Sep 17 00:00:00 2001 From: Amlal Date: Wed, 30 Apr 2025 12:34:54 +0200 Subject: kernel: keep track of cr3 in AMD HAL/HEL. why? To keep track of it, avoid security issues in the future. Signed-off-by: Amlal --- dev/boot/modules/SysChk/SysChk.cc | 2 +- dev/boot/src/HEL/AMD64/BootAPI.S | 2 +- dev/boot/src/HEL/AMD64/BootEFI.cc | 12 ++++++------ dev/ddk/DDKKit/net.h | 16 ++++++++++++++++ dev/kernel/FirmwareKit/Handover.h | 4 ++-- dev/kernel/HALKit/AMD64/HalKernelMain.cc | 7 ++++++- dev/kernel/HALKit/AMD64/Paging.h | 2 +- dev/kernel/HALKit/AMD64/Processor.h | 2 ++ dev/kernel/src/FS/HeFS.cc | 1 - dev/kernel/src/UserProcessScheduler.cc | 6 ++---- 10 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 dev/ddk/DDKKit/net.h (limited to 'dev/kernel/HALKit/AMD64/Processor.h') diff --git a/dev/boot/modules/SysChk/SysChk.cc b/dev/boot/modules/SysChk/SysChk.cc index 1c5255fb..c93ef83b 100644 --- a/dev/boot/modules/SysChk/SysChk.cc +++ b/dev/boot/modules/SysChk/SysChk.cc @@ -24,7 +24,7 @@ // Makes the compiler shut up. #ifndef kMachineModel #define kMachineModel "OS" -#endif // !kMachineModel +#endif // !kMachineModel EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) { fw_init_efi((EfiSystemTable*) handover->f_FirmwareCustomTables[1]); diff --git a/dev/boot/src/HEL/AMD64/BootAPI.S b/dev/boot/src/HEL/AMD64/BootAPI.S index 9cc15918..43775fc2 100644 --- a/dev/boot/src/HEL/AMD64/BootAPI.S +++ b/dev/boot/src/HEL/AMD64/BootAPI.S @@ -52,7 +52,7 @@ reset_wait: .global boot_read_cr3 boot_read_cr3: - mov rax, rax + mov rax, cr3 ret boot_write_cr3: diff --git a/dev/boot/src/HEL/AMD64/BootEFI.cc b/dev/boot/src/HEL/AMD64/BootEFI.cc index 739876da..ffc3c692 100644 --- a/dev/boot/src/HEL/AMD64/BootEFI.cc +++ b/dev/boot/src/HEL/AMD64/BootEFI.cc @@ -32,6 +32,8 @@ STATIC EfiGUID kGopGuid; EXTERN_C Void rt_reset_hardware(); +EXTERN_C Kernel::VoidPtr boot_read_cr3(); // @brief Page directory inside cr3 register. + /** @brief Finds and stores the GOP object. */ @@ -173,7 +175,7 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa if (reader_syschk.Blob()) { syschk_thread = new Boot::BootThread(reader_syschk.Blob()); - syschk_thread->SetName("BootZ/SysChk"); + syschk_thread->SetName("SysChk"); syschk_thread->Start(handover_hdr, NO); } @@ -236,13 +238,11 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa // ------------------------------------------ // if (reader_kernel.Blob()) { - // ------------------------------------------ // - // null these fields, to avoid being reused later. - // ------------------------------------------ // + handover_hdr->f_PageStart = boot_read_cr3(); auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); - kernel_thread.SetName("BootZ/NeKernel"); + kernel_thread.SetName("NeKernel"); handover_hdr->f_KernelImage = reader_kernel.Blob(); handover_hdr->f_KernelSz = reader_kernel.Size(); @@ -256,7 +256,7 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa if (!reader_netboot.Blob()) return kEfiFail; auto netboot_thread = Boot::BootThread(reader_netboot.Blob()); - netboot_thread.SetName("BootZ/BootNet"); + netboot_thread.SetName("BootNet"); return netboot_thread.Start(handover_hdr, NO); } diff --git a/dev/ddk/DDKKit/net.h b/dev/ddk/DDKKit/net.h new file mode 100644 index 00000000..ea85dee7 --- /dev/null +++ b/dev/ddk/DDKKit/net.h @@ -0,0 +1,16 @@ +/* ------------------------------------------- + + Copyright Amlal El Mahrouss. + + FILE: net.h + PURPOSE: Network model base header. + +------------------------------------------- */ + +#pragma once + +#include + +struct DDK_NET_MANIFEST; + +/// @brief IFS hooks to plug into the FileMgr. diff --git a/dev/kernel/FirmwareKit/Handover.h b/dev/kernel/FirmwareKit/Handover.h index fbf4bb28..d3ccc724 100644 --- a/dev/kernel/FirmwareKit/Handover.h +++ b/dev/kernel/FirmwareKit/Handover.h @@ -20,8 +20,8 @@ #include #include -#define kHandoverMagic 0xBADCC -#define kHandoverVersion 0x0117 +#define kHandoverMagic (0xBADCC) +#define kHandoverVersion (0x0117) /* Initial bitmap pointer location and size. */ #define kHandoverBitMapSz (gib_cast(4)) diff --git a/dev/kernel/HALKit/AMD64/HalKernelMain.cc b/dev/kernel/HALKit/AMD64/HalKernelMain.cc index fba02bc1..29244add 100644 --- a/dev/kernel/HALKit/AMD64/HalKernelMain.cc +++ b/dev/kernel/HALKit/AMD64/HalKernelMain.cc @@ -27,7 +27,8 @@ STATIC Kernel::Void hal_pre_init_scheduler() noexcept { } } -/// @brief Kernel init procedure. +/// @brief Kernel init function. +/// @param handover_hdr Handover boot header. EXTERN_C Int32 hal_init_platform(Kernel::HEL::BootInfoHeader* handover_hdr) { if (handover_hdr->f_Magic != kHandoverMagic && handover_hdr->f_Version != kHandoverVersion) { return kEfiFail; @@ -42,6 +43,10 @@ EXTERN_C Int32 hal_init_platform(Kernel::HEL::BootInfoHeader* handover_hdr) { Boot::ExitBootServices(handover_hdr->f_HardwareTables.f_ImageKey, handover_hdr->f_HardwareTables.f_ImageHandle); + kKernelCR3 = kHandoverHeader->f_PageStart; + + hal_write_cr3(kKernelCR3); + /************************************** */ /* INITIALIZE BIT MAP. */ /************************************** */ diff --git a/dev/kernel/HALKit/AMD64/Paging.h b/dev/kernel/HALKit/AMD64/Paging.h index 074c1113..3c7107bc 100644 --- a/dev/kernel/HALKit/AMD64/Paging.h +++ b/dev/kernel/HALKit/AMD64/Paging.h @@ -37,7 +37,7 @@ EXTERN_C void hal_write_cr0(Kernel::VoidPtr bit); EXTERN_C Kernel::VoidPtr hal_read_cr0(); // @brief CPU control register. EXTERN_C Kernel::VoidPtr hal_read_cr2(); // @brief Fault address. -EXTERN_C Kernel::VoidPtr hal_read_cr3(); // @brief Page table. +EXTERN_C Kernel::VoidPtr hal_read_cr3(); // @brief Page directory inside cr3 register. namespace Kernel::HAL { namespace Detail { diff --git a/dev/kernel/HALKit/AMD64/Processor.h b/dev/kernel/HALKit/AMD64/Processor.h index b57e9abf..d7d594d9 100644 --- a/dev/kernel/HALKit/AMD64/Processor.h +++ b/dev/kernel/HALKit/AMD64/Processor.h @@ -286,3 +286,5 @@ EXTERN_C ATTRIBUTE(naked) Kernel::Void hal_load_gdt(Kernel::HAL::Register64 ptr) inline Kernel::VoidPtr kKernelBitMpStart = nullptr; inline Kernel::UIntPtr kKernelBitMpSize = 0UL; + +inline Kernel::VoidPtr kKernelCR3 = nullptr; \ No newline at end of file diff --git a/dev/kernel/src/FS/HeFS.cc b/dev/kernel/src/FS/HeFS.cc index d1e41d1f..605df480 100644 --- a/dev/kernel/src/FS/HeFS.cc +++ b/dev/kernel/src/FS/HeFS.cc @@ -4,7 +4,6 @@ ------------------------------------------- */ -#include "NewKit/Macros.h" #ifdef __FSKIT_INCLUDES_HEFS__ #include diff --git a/dev/kernel/src/UserProcessScheduler.cc b/dev/kernel/src/UserProcessScheduler.cc index 4b206317..23ab22fa 100644 --- a/dev/kernel/src/UserProcessScheduler.cc +++ b/dev/kernel/src/UserProcessScheduler.cc @@ -20,8 +20,6 @@ #include #include #include -#include "KernelKit/CoreProcessScheduler.h" -#include "NewKit/Defines.h" ///! BUGS: 0 @@ -130,7 +128,7 @@ ErrorOr USER_PROCESS::New(SizeT sz, SizeT pad_amount) { if (this->UsedMemory > kSchedMaxMemoryLimit) return ErrorOr(-kErrorHeapOutOfMemory); #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - auto vm_register = hal_read_cr3(); + auto vm_register = kKernelCR3; hal_write_cr3(this->VMRegister); auto ptr = mm_new_heap(sz, Yes, Yes, pad_amount); @@ -267,7 +265,7 @@ Void USER_PROCESS::Exit(const Int32& exit_code) { auto memory_heap_list = this->HeapTree; #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - auto pd = hal_read_cr3(); + auto pd = kKernelCR3; hal_write_cr3(this->VMRegister); #endif -- cgit v1.2.3 From 2ef7b73cff0d99d04e5091e98b3988532e2b1063 Mon Sep 17 00:00:00 2001 From: Amlal Date: Thu, 1 May 2025 08:50:50 +0200 Subject: kernel: mmap the blob to VMAddress in PEFCodeMgr, alongside other fixes regarding memory leaks. Signed-off-by: Amlal --- dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc | 21 ++++--- dev/kernel/HALKit/AMD64/Processor.h | 2 +- dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 4 +- dev/kernel/HALKit/ARM64/Processor.h | 2 +- dev/kernel/KernelKit/IPEFDylibObject.h | 2 +- dev/kernel/KernelKit/LoaderInterface.h | 2 +- dev/kernel/KernelKit/PECodeMgr.h | 2 +- dev/kernel/KernelKit/PEF.h | 2 +- dev/kernel/KernelKit/PEFCodeMgr.h | 2 +- dev/kernel/src/PEFCodeMgr.cc | 84 ++++++++++++++++--------- tooling/mkfs.hefs.cc | 2 +- 11 files changed, 76 insertions(+), 49 deletions(-) (limited to 'dev/kernel/HALKit/AMD64/Processor.h') diff --git a/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc b/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc index 7a3e776f..4681b5e5 100644 --- a/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc +++ b/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc @@ -40,13 +40,14 @@ STATIC Void mmi_page_status(Detail::PTE* pte) { NE_UNUSED(pte); #ifdef __NE_VERBOSE_BITMAP__ - (Void)(kout << (pte->Present ? "Present" : "Not Present") << kendl); - (Void)(kout << (pte->Wr ? "W/R" : "Not W/R") << kendl); - (Void)(kout << (pte->Nx ? "NX" : "Not NX") << kendl); - (Void)(kout << (pte->User ? "User" : "Not User") << kendl); - (Void)(kout << (pte->Pcd ? "Not Cached" : "Cached") << kendl); - (Void)(kout << (pte->Accessed ? "Accessed" : "Not Accessed") << kendl); - (Void)(kout << (pte->ProtectionKey ? "Protected" : "Not Protected/PKU Disabled") << kendl); + (Void)(kout << "Flag: " << (pte->Present ? "Present" : "Not Present") << kendl); + (Void)(kout << "Flag: " << (pte->Wr ? "W/R" : "Not W/R") << kendl); + (Void)(kout << "Flag: " << (pte->Nx ? "NX" : "Not NX") << kendl); + (Void)(kout << "Flag: " << pte->User ? "User" : "Not User") << kendl); + (Void)(kout << "Flag: " << (pte->Pcd ? "Not Cached" : "Cached") << kendl); + (Void)(kout << "Flag: " << (pte->Accessed ? "Accessed" : "Not Accessed") << kendl); + (Void)(kout << "Flag: " << (pte->ProtectionKey ? "Protected" : "Not Protected/PKU Disabled") + << kendl); (Void)(kout << "Physical Address: " << hex_number(pte->PhysicalAddress) << kendl); #endif } @@ -56,7 +57,7 @@ STATIC Void mmi_page_status(Detail::PTE* pte) { /// @param virt a valid virtual address. /// @return Physical address. /***********************************************************************************/ -UIntPtr hal_get_phys_address(VoidPtr virt) { +UIntPtr mm_get_phys_address(VoidPtr virt) { const UInt64 kVMAddr = (UInt64) virt; const UInt64 kMask9Bits = 0x1FFULL; const UInt64 kPageOffsetMask = 0xFFFULL; @@ -101,7 +102,7 @@ UIntPtr hal_get_phys_address(VoidPtr virt) { /// @brief clflush+mfence helper function. /***********************************************************************************/ EXTERN_C Int32 mm_memory_fence(VoidPtr virtual_address) { - if (!virtual_address || !hal_get_phys_address(virtual_address)) return kErrorInvalidData; + if (!virtual_address || !mm_get_phys_address(virtual_address)) return kErrorInvalidData; asm volatile("clflush (%0)" : : "r"(virtual_address) : "memory"); asm volatile("mfence" ::: "memory"); @@ -117,6 +118,8 @@ EXTERN_C Int32 mm_memory_fence(VoidPtr virtual_address) { /// @return Status code of page manipulation process. /***********************************************************************************/ EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags) { + if (physical_address == 0) return kErrorInvalidData; + const UInt64 kVMAddr = (UInt64) virtual_address; constexpr UInt64 kMask9 = 0x1FF; constexpr UInt64 kPageMask = 0xFFF; diff --git a/dev/kernel/HALKit/AMD64/Processor.h b/dev/kernel/HALKit/AMD64/Processor.h index d7d594d9..e1ce8718 100644 --- a/dev/kernel/HALKit/AMD64/Processor.h +++ b/dev/kernel/HALKit/AMD64/Processor.h @@ -179,7 +179,7 @@ inline Bool hal_has_msr() noexcept { return edx & (1 << 5); } -UIntPtr hal_get_phys_address(VoidPtr virtual_address); +UIntPtr mm_get_phys_address(VoidPtr virtual_address); /***********************************************************************************/ /// @brief Get Model specific register inside core. diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index d09fd71a..eba2f892 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -324,7 +324,7 @@ STATIC Bool drv_init_command_structures_ahci() { return NO; } - UIntPtr clb_phys = HAL::hal_get_phys_address(clb_mem); + UIntPtr clb_phys = HAL::mm_get_phys_address(clb_mem); kSATAHba->Ports[kSATAIndex].Clb = (UInt32) (clb_phys & 0xFFFFFFFF); kSATAHba->Ports[kSATAIndex].Clbu = (UInt32) (clb_phys >> 32); @@ -344,7 +344,7 @@ STATIC Bool drv_init_command_structures_ahci() { return NO; } - UIntPtr ct_phys = HAL::hal_get_phys_address(ct_mem); + UIntPtr ct_phys = HAL::mm_get_phys_address(ct_mem); header[i].Ctba = (UInt32) (ct_phys & 0xFFFFFFFF); header[i].Ctbau = (UInt32) (ct_phys >> 32); diff --git a/dev/kernel/HALKit/ARM64/Processor.h b/dev/kernel/HALKit/ARM64/Processor.h index 38902627..1d9d2af2 100644 --- a/dev/kernel/HALKit/ARM64/Processor.h +++ b/dev/kernel/HALKit/ARM64/Processor.h @@ -36,7 +36,7 @@ enum { /// @return Status code of page manip. EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags); -EXTERN_C UIntPtr hal_get_phys_address(VoidPtr virtual_address); +EXTERN_C UIntPtr mm_get_phys_address(VoidPtr virtual_address); typedef UIntPtr Reg; typedef Register64 Register; diff --git a/dev/kernel/KernelKit/IPEFDylibObject.h b/dev/kernel/KernelKit/IPEFDylibObject.h index 4031bd85..42ed1830 100644 --- a/dev/kernel/KernelKit/IPEFDylibObject.h +++ b/dev/kernel/KernelKit/IPEFDylibObject.h @@ -62,7 +62,7 @@ class IPEFDylibObject final NE_DYLIB_OBJECT { if (symbol_name == nullptr || *symbol_name == 0) return nullptr; if (len > kPathLen || len < 1) return nullptr; - auto ret = reinterpret_cast(fLoader->FindSymbol(symbol_name, kind)); + auto ret = reinterpret_cast(fLoader->FindSymbol(symbol_name, kind).Leak().Leak()); if (!ret) { if (kind == kPefCode) return (VoidPtr) &__zka_pure_call; diff --git a/dev/kernel/KernelKit/LoaderInterface.h b/dev/kernel/KernelKit/LoaderInterface.h index 42046a53..f6a1b7e9 100644 --- a/dev/kernel/KernelKit/LoaderInterface.h +++ b/dev/kernel/KernelKit/LoaderInterface.h @@ -27,6 +27,6 @@ class LoaderInterface { virtual _Output const Char* MIME() = 0; virtual _Output const Char* Path() = 0; virtual _Output ErrorOr FindStart() = 0; - virtual _Output VoidPtr FindSymbol(_Input const Char* name, _Input Int32 kind) = 0; + virtual _Output ErrorOr FindSymbol(_Input const Char* name, _Input Int32 kind) = 0; }; } // namespace Kernel diff --git a/dev/kernel/KernelKit/PECodeMgr.h b/dev/kernel/KernelKit/PECodeMgr.h index 860f3426..05a2674c 100644 --- a/dev/kernel/KernelKit/PECodeMgr.h +++ b/dev/kernel/KernelKit/PECodeMgr.h @@ -55,7 +55,7 @@ class PE32Loader : public LoaderInterface { public: ErrorOr FindStart() override; - VoidPtr FindSymbol(const Char* name, Int32 kind) override; + ErrorOr FindSymbol(const Char* name, Int32 kind) override; ErrorOr GetBlob() override; public: diff --git a/dev/kernel/KernelKit/PEF.h b/dev/kernel/KernelKit/PEF.h index 03398b48..9381e491 100644 --- a/dev/kernel/KernelKit/PEF.h +++ b/dev/kernel/KernelKit/PEF.h @@ -98,7 +98,7 @@ typedef struct PEFCommandHeader final { UInt32 Flags; /* container flags */ UInt16 Kind; /* container kind */ UIntPtr Offset; /* content offset */ - UIntPtr VMAddress; /* VM offset */ + UIntPtr VMAddress; /* VM offset */ SizeT Size; /* content Size */ } PACKED PEFCommandHeader; diff --git a/dev/kernel/KernelKit/PEFCodeMgr.h b/dev/kernel/KernelKit/PEFCodeMgr.h index b3ca43d0..a637892f 100644 --- a/dev/kernel/KernelKit/PEFCodeMgr.h +++ b/dev/kernel/KernelKit/PEFCodeMgr.h @@ -42,7 +42,7 @@ class PEFLoader : public LoaderInterface { public: ErrorOr FindStart() override; - VoidPtr FindSymbol(const Char* name, Int32 kind) override; + ErrorOr FindSymbol(const Char* name, Int32 kind) override; ErrorOr GetBlob() override; public: diff --git a/dev/kernel/src/PEFCodeMgr.cc b/dev/kernel/src/PEFCodeMgr.cc index 7a75f386..632d5baa 100644 --- a/dev/kernel/src/PEFCodeMgr.cc +++ b/dev/kernel/src/PEFCodeMgr.cc @@ -80,7 +80,7 @@ PEFLoader::PEFLoader(const Char* path) : fCachedBlob(nullptr), fFatBinary(false) if (fCachedBlob) mm_delete_heap(fCachedBlob); - kout << "PEFLoader: warn: Executable format error!\r"; + kout << "PEFLoader: Warning: Executable format error!\r"; fCachedBlob = nullptr; } @@ -99,43 +99,44 @@ PEFLoader::~PEFLoader() { /// @param name name of symbol. /// @param kind kind of symbol we want. /***********************************************************************************/ -VoidPtr PEFLoader::FindSymbol(const Char* name, Int32 kind) { - if (!fCachedBlob || fBad || !name) return nullptr; +ErrorOr PEFLoader::FindSymbol(const Char* name, Int32 kind) { + if (!fCachedBlob || fBad || !name) return ErrorOr{kErrorInvalidData}; PEFContainer* container = reinterpret_cast(fCachedBlob); - auto blob = fFile->Read(name, mib_cast(16)); + auto blob = fFile->Read(name, sizeof(PEFCommandHeader)); PEFCommandHeader* container_header = reinterpret_cast(blob); - constexpr auto cMangleCharacter = '$'; - const Char* cContainerKinds[] = {".code64", ".data64", ".zero64", nullptr}; + constexpr auto kMangleCharacter = '$'; + const Char* kContainerKinds[] = {".code64", ".data64", ".zero64", nullptr}; ErrorOr error_or_symbol; switch (kind) { case kPefCode: { - error_or_symbol = KStringBuilder::Construct(cContainerKinds[0]); // code symbol. + error_or_symbol = KStringBuilder::Construct(kContainerKinds[0]); // code symbol. break; } case kPefData: { - error_or_symbol = KStringBuilder::Construct(cContainerKinds[1]); // data symbol. + error_or_symbol = KStringBuilder::Construct(kContainerKinds[1]); // data symbol. break; } case kPefZero: { - error_or_symbol = KStringBuilder::Construct(cContainerKinds[2]); // block starting symbol. + error_or_symbol = KStringBuilder::Construct(kContainerKinds[2]); // block starting symbol. break; } default: - return nullptr; // prevent that from the kernel's mode perspective, let that happen if it - // were a user process. + return ErrorOr{kErrorInvalidData}; + ; // prevent that from the kernel's mode perspective, let that happen if it + // were a user process. } Char* unconst_symbol = const_cast(name); for (SizeT i = 0UL; i < rt_string_len(unconst_symbol, kPefNameLen); ++i) { if (unconst_symbol[i] == ' ') { - unconst_symbol[i] = cMangleCharacter; + unconst_symbol[i] = kMangleCharacter; } } @@ -147,7 +148,7 @@ VoidPtr PEFLoader::FindSymbol(const Char* name, Int32 kind) { if (container_header->Cpu != Detail::ldr_get_platform()) { if (!this->fFatBinary) { mm_delete_heap(blob); - return nullptr; + return ErrorOr{kErrorInvalidData}; } } @@ -157,27 +158,36 @@ VoidPtr PEFLoader::FindSymbol(const Char* name, Int32 kind) { container_header->Size); mm_delete_heap(blob); - kout << "PEFLoader: INFO: Load stub: " << container_header->Name << "!\r"; + kout << "PEFLoader: Information: Loaded stub: " << container_header->Name << "!\r"; - return container_blob_value; + auto ret = HAL::mm_map_page((VoidPtr) container_header->VMAddress, + (VoidPtr) HAL::mm_get_phys_address(container_blob_value), + HAL::kMMFlagsPresent | HAL::kMMFlagsUser); + + if (ret != kErrorSuccess) { + mm_delete_heap(container_blob_value); + return ErrorOr{kErrorInvalidData}; + } + + return ErrorOr{container_blob_value}; } } } mm_delete_heap(blob); - return nullptr; + return ErrorOr{kErrorInvalidData}; } /// @brief Finds the executable entrypoint. /// @return ErrorOr PEFLoader::FindStart() { - if (auto sym = this->FindSymbol(kPefStart, kPefCode); sym) return ErrorOr(sym); + if (auto sym = this->FindSymbol(kPefStart, kPefCode); sym) return sym; return ErrorOr(kErrorExecutable); } /// @brief Tells if the executable is loaded or not. -/// @return +/// @return Whether it's not bad and is cached. bool PEFLoader::IsLoaded() noexcept { return !fBad && fCachedBlob; } @@ -188,17 +198,17 @@ const Char* PEFLoader::Path() { const Char* PEFLoader::AsString() { #ifdef __32x0__ - return "32x0 PEF executable."; + return "32x0 PEF."; #elif defined(__64x0__) - return "64x0 PEF executable."; + return "64x0 PEF."; #elif defined(__x86_64__) - return "x86_64 PEF executable."; + return "x86_64 PEF."; #elif defined(__aarch64__) - return "AARCH64 PEF executable."; + return "AARCH64 PEF."; #elif defined(__powerpc64__) - return "POWER64 PEF executable."; + return "POWER64 PEF."; #else - return "???? PEF executable."; + return "???? PEF."; #endif // __32x0__ || __64x0__ || __x86_64__ || __powerpc64__ } @@ -216,16 +226,30 @@ namespace Utils { if (errOrStart.Error() != kErrorSuccess) return kSchedInvalidPID; - auto id = UserProcessScheduler::The().Spawn( - reinterpret_cast(exec.FindSymbol(kPefNameSymbol, kPefData)), - errOrStart.Leak().Leak(), exec.GetBlob().Leak().Leak()); + auto symname = exec.FindSymbol(kPefNameSymbol, kPefData); + + if (!symname) { + symname = ErrorOr{(VoidPtr) rt_alloc_string("USER_PROCESS")}; + } + + auto id = + UserProcessScheduler::The().Spawn(reinterpret_cast(symname.Leak().Leak()), + errOrStart.Leak().Leak(), exec.GetBlob().Leak().Leak()); + + mm_delete_heap(symname.Leak().Leak()); if (id != kSchedInvalidPID) { + auto stacksym = exec.FindSymbol(kPefStackSizeSymbol, kPefData); + + if (!symname) { + stacksym = ErrorOr{(VoidPtr) new UIntPtr(mib_cast(16))}; + } + UserProcessScheduler::The().CurrentTeam().AsArray()[id].Kind = process_kind; UserProcessScheduler::The().CurrentTeam().AsArray()[id].StackSize = - *(UIntPtr*) exec.FindSymbol(kPefStackSizeSymbol, kPefData); - UserProcessScheduler::The().CurrentTeam().AsArray()[id].MemoryLimit = - *(UIntPtr*) exec.FindSymbol(kPefHeapSizeSymbol, kPefData); + *(UIntPtr*) stacksym.Leak().Leak(); + + mm_delete_heap(stacksym.Leak().Leak()); } return id; diff --git a/tooling/mkfs.hefs.cc b/tooling/mkfs.hefs.cc index 4d7225fe..f19f1571 100644 --- a/tooling/mkfs.hefs.cc +++ b/tooling/mkfs.hefs.cc @@ -96,7 +96,7 @@ int main(int argc, char** argv) { bootNode.sectorSize = kSectorSize; bootNode.startIND = start_ind; bootNode.endIND = end_ind; - bootNode.indCount = 0UL; + bootNode.indCount = 0UL; bootNode.diskStatus = mkfs::hefs::kHeFSStatusUnlocked; std::memcpy(bootNode.magic, kHeFSMagic, kHeFSMagicLen - 1); -- cgit v1.2.3