From d87eb24271671f4d7d1298c6c4bbdf4e73f774f3 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 30 Aug 2024 08:43:46 +0200 Subject: A lot of fixes regarding the kernel: + Either choose NewFS or generic filesystem interface for FileStream class. + Fixed PEF code manager executable probing method, inside it's Loader class. + Add function mm_update_page for virtual memory purposes. - Remove logging on HardwareTimer class. --- dev/ZKA/HALKit/AMD64/HalTimer.cxx | 3 +- dev/ZKA/HALKit/AMD64/Processor.hxx | 70 ++++++++++++------------------------ dev/ZKA/KernelKit/CodeManager.hxx | 2 +- dev/ZKA/KernelKit/FileManager.hxx | 2 +- dev/ZKA/KernelKit/PEFCodeManager.hxx | 13 ++++--- dev/ZKA/Sources/DLLMain.cxx | 64 ++++++++++++++++++++++----------- dev/ZKA/Sources/FileManager.cxx | 6 ---- dev/ZKA/Sources/PEFCodeManager.cxx | 57 ++++++++++++++--------------- dev/ZKA/arm64-efi.make | 2 +- 9 files changed, 105 insertions(+), 114 deletions(-) (limited to 'dev') diff --git a/dev/ZKA/HALKit/AMD64/HalTimer.cxx b/dev/ZKA/HALKit/AMD64/HalTimer.cxx index 33b0e00f..f88d1143 100644 --- a/dev/ZKA/HALKit/AMD64/HalTimer.cxx +++ b/dev/ZKA/HALKit/AMD64/HalTimer.cxx @@ -79,8 +79,7 @@ Int32 HardwareTimer::Wait() noexcept *(fDigitalTimer + cHPETCompValue) = prev + ticks; - while (*(fDigitalTimer + cHPETCounterValue) < (ticks)) - kcout << "MS: " << number(*(fDigitalTimer + cHPETCounterValue)) << endl; + while (*(fDigitalTimer + cHPETCounterValue) < (ticks)); return 0; } diff --git a/dev/ZKA/HALKit/AMD64/Processor.hxx b/dev/ZKA/HALKit/AMD64/Processor.hxx index d1bda9ef..93540c34 100644 --- a/dev/ZKA/HALKit/AMD64/Processor.hxx +++ b/dev/ZKA/HALKit/AMD64/Processor.hxx @@ -35,10 +35,10 @@ EXTERN_C #define IsActiveLow(FLG) (FLG & 2) #define IsLevelTriggered(FLG) (FLG & 8) -#define kInterruptGate (0x8E) -#define kTrapGate (0xEF) -#define kTaskGate (0b10001100) -#define kGdtCodeSelector (0x08) +#define kInterruptGate (0x8E) +#define kTrapGate (0xEF) +#define kTaskGate (0b10001100) +#define kGdtCodeSelector (0x08) #define kGdtUserCodeSelector (0x10) namespace Kernel @@ -64,42 +64,35 @@ namespace Kernel::HAL /// @brief Virtual memory flags. enum { + eFlagsPresent, eFlagsUser, eFlagsRw, - eFlagsExecDisable + eFlagsExecDisable, + eFlagsSetPhysAddress, + eFlagsCount, }; - /// @brief Map address to PDE. - /// @param pde a valid page directory. + /// @brief Updates a PTE from pd_base. + /// @param pd_base a valid PDE address. /// @param phys_addr a valid phyiscal address. /// @param virt_addr a valid virtual address. /// @param flags the flags to put on the page. - inline Int32 ke_map_address(PDE* pde, UIntPtr phys_addr, UIntPtr virt_addr, UInt32 flags) + inline Int32 mm_update_page(VoidPtr pd_base, UIntPtr phys_addr, UIntPtr virt_addr, UInt32 flags) { - UInt16 pml4_index = (virt_addr >> 39) & 0x1FF; + UIntPtr pde_idx = (UIntPtr)virt_addr >> 22; + UIntPtr pte_idx = (UIntPtr)virt_addr >> 12 & 0x03FF; - if (pde && !pde->Pte[pml4_index].Present) - { - pde->Pte[pml4_index].Present = true; - - pde->Pte[pml4_index].PhysicalAddress = phys_addr; - pde->Pte[pml4_index].Rw = flags & eFlagsRw; - pde->Pte[pml4_index].User = flags & eFlagsUser; - pde->Pte[pml4_index].ExecDisable = flags & eFlagsExecDisable; - - kcout << "newoskrnl: PTE is present now.\r"; + volatile PTE* pte = (volatile PTE*)((UIntPtr)pd_base + (kPTEAlign * pde_idx)); - return 0; - } - else + if (pte) { - kcout << "newoskrnl: PM is already present.\r"; - - kcout << "PhysicalAddress: " << hex_number(pde->Pte[pml4_index].PhysicalAddress); - kcout << "\r"; + if ((flags & eFlagsSetPhysAddress)) + pte->PhysicalAddress = phys_addr; - kcout << "User: " << (pde->Pte[pml4_index].User ? "true" : "false") << "\r"; - kcout << "RW: " << (pde->Pte[pml4_index].Rw ? "true" : "false") << "\r"; + pte->Present = flags & eFlagsPresent; + pte->Rw = flags & eFlagsRw; + pte->User = flags & eFlagsUser; + pte->ExecDisable = flags & eFlagsExecDisable; return 0; } @@ -107,25 +100,6 @@ namespace Kernel::HAL return 1; } - /// @brief Map address to PDE. - /// @param pde - /// @param phys_addr - /// @param virt_addr - /// @param flags - inline Void ke_unmap_address(PDE* pde, UIntPtr phys_addr, UIntPtr virt_addr, UInt32 flags) - { - UInt16 pml4_index = (virt_addr >> 39) & 0x1FF; - - if (pde->Pte[pml4_index].Present) - { - pde->Pte[pml4_index].Present = false; - pde->Pte[pml4_index].PhysicalAddress = 0; - pde->Pte[pml4_index].Rw = 0; - pde->Pte[pml4_index].User = 0; - pde->Pte[pml4_index].ExecDisable = 0; - } - } - EXTERN_C UChar In8(UInt16 port); EXTERN_C UShort In16(UInt16 port); EXTERN_C UInt In32(UInt16 port); @@ -340,7 +314,7 @@ EXTERN_C Kernel::Void hal_load_gdt(Kernel::HAL::RegisterGDT ptr); #define kKernelIdtSize 0x100 #define kKernelInterruptId 0x32 -inline Kernel::VoidPtr kKernelVMHStart = nullptr; +inline Kernel::VoidPtr kKernelVMHStart = nullptr; inline Kernel::VoidPtr kKernelVirtualStart = nullptr; inline Kernel::UIntPtr kKernelVirtualSize = 0UL; diff --git a/dev/ZKA/KernelKit/CodeManager.hxx b/dev/ZKA/KernelKit/CodeManager.hxx index b551815e..513e65b7 100644 --- a/dev/ZKA/KernelKit/CodeManager.hxx +++ b/dev/ZKA/KernelKit/CodeManager.hxx @@ -27,5 +27,5 @@ namespace Kernel /// @note This sets up a new stack, anything on the main function that calls the kernel will not be accessible. /// @param main the start of the process. /// @return if the process was started or not. - bool execute_from_image(MainKind main, const Char* processName) noexcept; + bool execute_from_image(MainKind main, const Char* process_name) noexcept; } // namespace Kernel diff --git a/dev/ZKA/KernelKit/FileManager.hxx b/dev/ZKA/KernelKit/FileManager.hxx index 67f2f501..cbfc1e30 100644 --- a/dev/ZKA/KernelKit/FileManager.hxx +++ b/dev/ZKA/KernelKit/FileManager.hxx @@ -188,7 +188,7 @@ namespace Kernel * @tparam Encoding file encoding (char, wchar_t...) * @tparam FSClass Filesystem contract who takes care of it. */ - template class FileStream final { diff --git a/dev/ZKA/KernelKit/PEFCodeManager.hxx b/dev/ZKA/KernelKit/PEFCodeManager.hxx index 2935f709..dd6daca5 100644 --- a/dev/ZKA/KernelKit/PEFCodeManager.hxx +++ b/dev/ZKA/KernelKit/PEFCodeManager.hxx @@ -46,11 +46,16 @@ namespace Kernel bool IsLoaded() noexcept; private: +#ifdef __FSKIT_USE_NEWFS__ + OwnPtr> fFile; +#else OwnPtr> fFile; - Ref fPath; - VoidPtr fCachedBlob; - bool fFatBinary; - bool fBad; +#endif // __FSKIT_USE_NEWFS__ + + Ref fPath; + VoidPtr fCachedBlob; + bool fFatBinary; + bool fBad; }; namespace Utils diff --git a/dev/ZKA/Sources/DLLMain.cxx b/dev/ZKA/Sources/DLLMain.cxx index b35922bf..a82d0b97 100644 --- a/dev/ZKA/Sources/DLLMain.cxx +++ b/dev/ZKA/Sources/DLLMain.cxx @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include #include @@ -52,22 +52,22 @@ namespace Kernel::Detail { if (Kernel::FilesystemManagerInterface::GetMounted()) { - CG::CGDrawStringToWnd(cKernelWnd, "NewOSKrnl: NewFS already mounted by HAL...", 10, 10, RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: NewFS IFS already mounted by HAL (A:)...", 10, 10, RGB(0, 0, 0)); fNewFS = reinterpret_cast(Kernel::FilesystemManagerInterface::GetMounted()); } else { - CG::CGDrawStringToWnd(cKernelWnd, "NewOSKrnl: Mounted NewFS filesystem...", 10, 10, RGB(0, 0, 0)); - // Mounts a NewFS from main drive. fNewFS = new Kernel::NewFilesystemManager(); Kernel::FilesystemManagerInterface::Mount(fNewFS); + + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Mounted NewFS IFS (A:)...", 10, 10, RGB(0, 0, 0)); } - const auto cDirCount = 7; + const Kernel::SizeT cDirCount = 7UL; - const char* cDirStr[cDirCount] = { + const Kernel::Char* cDirStr[cDirCount] = { "\\Boot\\", "\\System\\", "\\Support\\", "\\Applications\\", "\\Users\\", "\\Library\\", "\\Mount\\"}; @@ -79,10 +79,8 @@ namespace Kernel::Detail if (catalogDir) { - Kernel::kcout << "newoskrnl: Already exists.\r"; - - CG::CGDrawStringToWnd(cKernelWnd, "NewOSKrnl: Catalog directory already exists: ", 10 + (10 * (dirIndx + 1)), 10, RGB(0, 0, 0)); - CG::CGDrawStringToWnd(cKernelWnd, catalogDir->Name, 10 + (10 * (dirIndx + 1)), 10 + (FONT_SIZE_X * rt_string_len("NewOSKrnl: Catalog directory already exists: ")), RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Catalog directory already exists: ", 10 + (10 * (dirIndx + 1)), 10, RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, catalogDir->Name, 10 + (10 * (dirIndx + 1)), 10 + (FONT_SIZE_X * rt_string_len("newoskrnl.dll: Catalog directory already exists: ")), RGB(0, 0, 0)); delete catalogDir; continue; @@ -91,8 +89,8 @@ namespace Kernel::Detail catalogDir = fNewFS->GetParser()->CreateCatalog(cDirStr[dirIndx], 0, kNewFSCatalogKindDir); - CG::CGDrawStringToWnd(cKernelWnd, "NewOSKrnl: Catalog has been created: ", 10 + (10 * (dirIndx + 1)), 10, RGB(0, 0, 0)); - CG::CGDrawStringToWnd(cKernelWnd, catalogDir->Name, 10 + (10 * (dirIndx + 1)), 10 + (FONT_SIZE_X * rt_string_len("NewOSKrnl: Catalog has been created: ")), RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Catalog directory has been created: ", 10 + (10 * (dirIndx + 1)), 10, RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, catalogDir->Name, 10 + (10 * (dirIndx + 1)), 10 + (FONT_SIZE_X * rt_string_len("newoskrnl.dll: Catalog has been created: ")), RGB(0, 0, 0)); delete catalogDir; } @@ -104,17 +102,15 @@ namespace Kernel::Detail if (catalogDisk) { - - CG::CGDrawStringToWnd(cKernelWnd, "NewOSKrnl: Catalog swap file already exists: ", 10 + (10 * (cDirCount + 1)), 10, RGB(0, 0, 0)); - CG::CGDrawStringToWnd(cKernelWnd, kSysPage, 10 + (10 * (cDirCount + 1)), 10 + (FONT_SIZE_X * rt_string_len("NewOSKrnl: Catalog swap file already exists: ")), RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Catalog swap file already exists: ", 10 + (10 * (cDirCount + 1)), 10, RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, kSysPage, 10 + (10 * (cDirCount + 1)), 10 + (FONT_SIZE_X * rt_string_len("newoskrnl.dll: Catalog swap file already exists: ")), RGB(0, 0, 0)); delete catalogDisk; } else { - - CG::CGDrawStringToWnd(cKernelWnd, "NewOSKrnl: Catalog swap file created: ", 10 + (10 * (cDirCount + 1)), 10, RGB(0, 0, 0)); - CG::CGDrawStringToWnd(cKernelWnd, kSysPage, 10 + (10 * (cDirCount + 1)), 10 + (FONT_SIZE_X * rt_string_len("NewOSKrnl: Catalog swap file created: ")), RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Catalog swap file created: ", 10 + (10 * (cDirCount + 1)), 10, RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, kSysPage, 10 + (10 * (cDirCount + 1)), 10 + (FONT_SIZE_X * rt_string_len("newoskrnl.dll: Catalog swap file created: ")), RGB(0, 0, 0)); catalogDisk = (NFS_CATALOG_STRUCT*)this->Leak()->CreateSwapFile(kSysPage); @@ -180,16 +176,42 @@ EXTERN_C Kernel::Void ke_dll_entrypoint(Kernel::Void) /// Now run kernel loop, until no process are running. Kernel::Detail::FilesystemInstaller(); // automatic filesystem creation. - cKernelWnd->w_sub_type = CG::cWndFlagCloseControlSelect; + cKernelWnd->w_sub_type = CG::cWndFlagCloseControlSelect; cKernelWnd->w_needs_repaint = Yes; CG::CGDrawWindowList(&cKernelWnd, 1); - CG::CGDrawStringToWnd(cKernelWnd, "NewOSKrnl: Running System Component: ", 10, 10, RGB(0, 0, 0)); - CG::CGDrawStringToWnd(cKernelWnd, kSysDrv, 10, 10 + (FONT_SIZE_X * Kernel::rt_string_len("NewOSKrnl: Running System Component: ")), RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Running System Component: ", 10, 10, RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, kSysDrv, 10, 10 + (FONT_SIZE_X * Kernel::rt_string_len("newoskrnl.dll: Running System Component: ")), RGB(0, 0, 0)); /// @note BThread doesn't parse the symbols so doesn't nullify them, .bss is though. Kernel::cProcessScheduler = nullptr; + Kernel::ProcessHelper::StartScheduling(); + + Kernel::PEFLoader sys_user_drv(kSysDrv); + + if (sys_user_drv.IsLoaded()) + { + auto err_start = sys_user_drv.FindStart(); + + if (!err_start.Error()) + { + auto start = err_start.Leak().Leak(); + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: System component successfully loaded...", 20, 10, RGB(0, 0, 0)); + } + else + { + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Missing entrypoint symbol: ", 20, 10, RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, kPefStart, 20, 10 + (FONT_SIZE_X * Kernel::rt_string_len("newoskrnl.dll: Missing entrypoint symbol: ")), RGB(0, 0, 0)); + } + } + else + { + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Missing catalog: ", 20, 10, RGB(0, 0, 0)); + CG::CGDrawStringToWnd(cKernelWnd, kSysDrv, 20, 10 + (FONT_SIZE_X * Kernel::rt_string_len("newoskrnl.dll: Missing catalog: ")), RGB(0, 0, 0)); + } + + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Starting the scheduler...", 30, 10, RGB(0, 0, 0)); while (Yes) { diff --git a/dev/ZKA/Sources/FileManager.cxx b/dev/ZKA/Sources/FileManager.cxx index 0912da27..8c8fb83a 100644 --- a/dev/ZKA/Sources/FileManager.cxx +++ b/dev/ZKA/Sources/FileManager.cxx @@ -65,12 +65,6 @@ namespace Kernel auto catalog = fImpl->GetCatalog(path); - if (catalog->Kind != kNewFSCatalogKindFile) - { - fImpl->CloseCatalog(catalog); - return nullptr; - } - return node_cast(catalog); } diff --git a/dev/ZKA/Sources/PEFCodeManager.cxx b/dev/ZKA/Sources/PEFCodeManager.cxx index 365f01c2..1b4adcfc 100644 --- a/dev/ZKA/Sources/PEFCodeManager.cxx +++ b/dev/ZKA/Sources/PEFCodeManager.cxx @@ -51,44 +51,41 @@ namespace Kernel : fCachedBlob(nullptr), fBad(false), fFatBinary(false) { fFile.New(const_cast(path), cRestrictRB); + fPath = StringBuilder::Construct(path).Leak(); - if (StringBuilder::Equals(fFile->MIME(), this->MIME())) - { - fPath = StringBuilder::Construct(path).Leak(); - - auto cPefHeader = "PEF_CONTAINER"; + auto cPefHeader = "PEF_CONTAINER"; - fCachedBlob = fFile->Read(cPefHeader); + fCachedBlob = fFile->Read(cPefHeader); - PEFContainer* container = reinterpret_cast(fCachedBlob); + PEFContainer* container = reinterpret_cast(fCachedBlob); - if (container->Cpu == Detail::rt_get_pef_platform() && - container->Magic[0] == kPefMagic[0] && - container->Magic[1] == kPefMagic[1] && - container->Magic[2] == kPefMagic[2] && - container->Magic[3] == kPefMagic[3] && - container->Magic[4] == kPefMagic[4] && container->Abi == kPefAbi) - { - return; - } - else if (container->Magic[4] == kPefMagic[0] && - container->Magic[3] == kPefMagic[1] && - container->Magic[2] == kPefMagic[2] && - container->Magic[1] == kPefMagic[3] && - container->Magic[0] == kPefMagic[0] && container->Abi == kPefAbi) - { - /// This is a fat binary. - this->fFatBinary = true; - return; - } + if (container->Cpu == Detail::rt_get_pef_platform() && + container->Magic[0] == kPefMagic[0] && + container->Magic[1] == kPefMagic[1] && + container->Magic[2] == kPefMagic[2] && + container->Magic[3] == kPefMagic[3] && + container->Magic[4] == kPefMagic[4] && container->Abi == kPefAbi) + { + return; + } + else if (container->Magic[4] == kPefMagic[0] && + container->Magic[3] == kPefMagic[1] && + container->Magic[2] == kPefMagic[2] && + container->Magic[1] == kPefMagic[3] && + container->Magic[0] == kPefMagic[0] && container->Abi == kPefAbi) + { + /// This is a fat binary. + this->fFatBinary = true; + return; + } - kcout << "CodeManagerPEF: Warning: Executable format error!\n"; - fBad = true; + kcout << "CodeManagerPEF: Warning: Executable format error!\r"; + fBad = true; + if (fCachedBlob) mm_delete_ke_heap(fCachedBlob); - fCachedBlob = nullptr; - } + fCachedBlob = nullptr; } /// @brief PEF destructor. diff --git a/dev/ZKA/arm64-efi.make b/dev/ZKA/arm64-efi.make index 59bed1d5..f168def3 100644 --- a/dev/ZKA/arm64-efi.make +++ b/dev/ZKA/arm64-efi.make @@ -54,7 +54,7 @@ link-arm64-epm: .PHONY: all all: newos-arm64-epm link-arm64-epm - @echo "NewOSKrnl => OK." + @echo "Krnl => OK." .PHONY: help help: -- cgit v1.2.3