From 6dcf5b87da65de2254d6102f567183eaeca03088 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sat, 26 Oct 2024 19:49:02 +0200 Subject: IMP: This commit contains fixes and improvements regarding the kernel, a next one will be done soon. Signed-off-by: Amlal --- dev/SCIKit/Foundation.h | 6 +- dev/zba/Modules/SysChk/build.json | 2 +- dev/zba/amd64-efi.make | 2 - dev/zba/src/HEL/AMD64/BootMain.cc | 27 ++-- dev/zka/CFKit/Property.h | 10 +- dev/zka/FSKit/IndexableProperty.h | 2 +- dev/zka/FSKit/NeFS.h | 4 +- dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc | 2 +- dev/zka/HALKit/AMD64/HalCPU.cc | 101 --------------- dev/zka/HALKit/AMD64/HalCPUAMD64.cc | 101 +++++++++++++++ dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm | 42 ++++++ dev/zka/HALKit/AMD64/HalDescriptorLoader.cc | 2 +- dev/zka/HALKit/AMD64/HalKernelMain.cc | 15 ++- dev/zka/HALKit/AMD64/HalMPContextSwitch.asm | 42 ------ dev/zka/HALKit/AMD64/HalPagingMgr.cc | 162 ------------------------ dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc | 159 +++++++++++++++++++++++ dev/zka/HALKit/AMD64/HalSchedulerCore.cc | 56 -------- dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc | 46 +++++++ dev/zka/HALKit/AMD64/HalTimer.cc | 86 ------------- dev/zka/HALKit/AMD64/HalTimerAMD64.cc | 86 +++++++++++++ dev/zka/HALKit/AMD64/Processor.h | 11 +- dev/zka/HALKit/ARM64/HalSchedulerCore.cc | 50 -------- dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc | 40 ++++++ dev/zka/HALKit/ARM64/Processor.h | 10 +- dev/zka/HALKit/POWER/HalVirtualMemory.cc | 24 ++-- dev/zka/HALKit/POWER/Hart.h | 2 +- dev/zka/HALKit/POWER/Processor.h | 2 + dev/zka/KernelKit/CodeMgr.h | 4 +- dev/zka/KernelKit/PEFCodeMgr.h | 2 +- dev/zka/KernelKit/UserProcessScheduler.h | 16 +-- dev/zka/amd64-efi.make | 2 +- dev/zka/src/BitMapMgr.cc | 76 +++++------ dev/zka/src/CodeMgr.cc | 8 +- dev/zka/src/GUIDWizard.cc | 6 +- dev/zka/src/PEFCodeMgr.cc | 13 +- dev/zka/src/UserProcessScheduler.cc | 31 ++--- dev/zka/src/Utils.cc | 18 +-- 37 files changed, 611 insertions(+), 657 deletions(-) delete mode 100644 dev/zka/HALKit/AMD64/HalCPU.cc create mode 100644 dev/zka/HALKit/AMD64/HalCPUAMD64.cc create mode 100644 dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm delete mode 100644 dev/zka/HALKit/AMD64/HalMPContextSwitch.asm delete mode 100644 dev/zka/HALKit/AMD64/HalPagingMgr.cc create mode 100644 dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc delete mode 100644 dev/zka/HALKit/AMD64/HalSchedulerCore.cc create mode 100644 dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc delete mode 100644 dev/zka/HALKit/AMD64/HalTimer.cc create mode 100644 dev/zka/HALKit/AMD64/HalTimerAMD64.cc delete mode 100644 dev/zka/HALKit/ARM64/HalSchedulerCore.cc create mode 100644 dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc (limited to 'dev') diff --git a/dev/SCIKit/Foundation.h b/dev/SCIKit/Foundation.h index d0ba87a1..c4851566 100644 --- a/dev/SCIKit/Foundation.h +++ b/dev/SCIKit/Foundation.h @@ -7,8 +7,8 @@ Purpose: SCI core header file (C++ only). ------------------------------------------- */ -#ifndef SCI_BASE_H -#define SCI_BASE_H +#ifndef SCIKIT_FOUNDATION_H +#define SCIKIT_FOUNDATION_H #define ATTRIBUTE(X) __attribute__((X)) #define IMPORT_CXX extern "C++" @@ -246,4 +246,4 @@ IMPORT_C Void EvtRemoveListener(_Input const Char* event_name, _Input SCIObject /// @return the event data. IMPORT_C VoidPtr EvtDispatchEvent(_Input const Char* event_name, _Input VoidPtr event_data); -#endif // ifndef SCI_BASE_H +#endif // ifndef SCIKIT_FOUNDATION_H diff --git a/dev/zba/Modules/SysChk/build.json b/dev/zba/Modules/SysChk/build.json index 7587d0a2..299b0acf 100644 --- a/dev/zba/Modules/SysChk/build.json +++ b/dev/zba/Modules/SysChk/build.json @@ -2,7 +2,7 @@ "compiler_path": "x86_64-w64-mingw32-g++", "compiler_std": "c++20", "headers_path": ["../", "../../", "../../../zka", "../../../", "./"], - "sources_path": [".cc", "*.S"], + "sources_path": ["*.cc", "*.S"], "output_name": "syschk.sys", "compiler_flags": [ "-ffreestanding", diff --git a/dev/zba/amd64-efi.make b/dev/zba/amd64-efi.make index 4c3e56dc..21ee5f96 100644 --- a/dev/zba/amd64-efi.make +++ b/dev/zba/amd64-efi.make @@ -66,8 +66,6 @@ all: compile-amd64 $(COPY) src/$(BOOTLOADER) src/Root/EFI/BOOT/ZBAOSLDR.EFI $(COPY) ../zka/$(KERNEL) src/Root/$(KERNEL) $(COPY) ./Modules/SysChk/$(SYSCHK) src/Root/$(SYSCHK) - $(COPY) ./Modules/SysChk/$(STARTUP) src/Root/zka/$(STARTUP) - $(COPY) ../crt/$(CRT) src/Root/$(CRT) $(COPY) src/$(BOOTLOADER) src/Root/$(BOOTLOADER) ifneq ($(DEBUG_SUPPORT), ) diff --git a/dev/zba/src/HEL/AMD64/BootMain.cc b/dev/zba/src/HEL/AMD64/BootMain.cc index 9924450e..8666353e 100644 --- a/dev/zba/src/HEL/AMD64/BootMain.cc +++ b/dev/zba/src/HEL/AMD64/BootMain.cc @@ -167,6 +167,7 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr ImageHandle, // format the disk. // ---------------------------------------------------- // +#ifdef __ZKA_AUTO_FORMAT__ if (!partition_factory.IsPartitionValid()) { Boot::BDiskFormatFactory::BFileDescriptor root; @@ -179,6 +180,7 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr ImageHandle, rt_reset_hardware(); } +#endif // __ZKA_AUTO_FORMAT__ BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); @@ -280,26 +282,21 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr ImageHandle, #endif // __ZKA_USE_FB__ } - Boot::BFileReader chimeWav(L"zka\\startup.wav", ImageHandle); - Boot::BFileReader readerSysDrv(L"zka\\startup.sys", ImageHandle); - Boot::BFileReader urbanistTTF(L"zka\\urbanist.ttf", ImageHandle); + Boot::BFileReader chime_wav(L"zka\\startup.wav", ImageHandle); + Boot::BFileReader ttf_font(L"zka\\urbanist.ttf", ImageHandle); - readerSysDrv.ReadAll(0); - chimeWav.ReadAll(0); - urbanistTTF.ReadAll(0); + chime_wav.ReadAll(0); + ttf_font.ReadAll(0); - if (readerSysDrv.Blob() && - chimeWav.Blob() && - urbanistTTF.Blob()) + if (chime_wav.Blob() && + ttf_font.Blob()) { - handover_hdr->f_StartupChime = chimeWav.Blob(); - handover_hdr->f_ChimeSz = chimeWav.Size(); - handover_hdr->f_StartupImage = readerSysDrv.Blob(); - handover_hdr->f_StartupSz = readerSysDrv.Size(); + handover_hdr->f_StartupChime = chime_wav.Blob(); + handover_hdr->f_ChimeSz = chime_wav.Size(); handover_hdr->f_KernelImage = reader_kernel.Blob(); handover_hdr->f_KernelSz = reader_kernel.Size(); - handover_hdr->f_TTFallbackFont = urbanistTTF.Blob(); - handover_hdr->f_FontSz = urbanistTTF.Size(); + handover_hdr->f_TTFallbackFont = ttf_font.Blob(); + handover_hdr->f_FontSz = ttf_font.Size(); } else { diff --git a/dev/zka/CFKit/Property.h b/dev/zka/CFKit/Property.h index 0628e1c2..efdb9879 100644 --- a/dev/zka/CFKit/Property.h +++ b/dev/zka/CFKit/Property.h @@ -4,15 +4,15 @@ ------------------------------------------- */ -#ifndef __INC_PROPS_H__ -#define __INC_PROPS_H__ +#ifndef CFKIT_PROPS_H +#define CFKIT_PROPS_H #include #include #include #include -#define cMaxPropLen 4096 +#define kMaxPropLen 255 namespace CFKit { @@ -38,7 +38,7 @@ namespace CFKit StringView& GetKey(); private: - StringView fName{cMaxPropLen}; + StringView fName{kMaxPropLen}; PropertyId fAction{No}; }; @@ -51,4 +51,4 @@ namespace Kernel using namespace CFKit; } -#endif // !__INC_PROPS_H__ +#endif // !CFKIT_PROPS_H diff --git a/dev/zka/FSKit/IndexableProperty.h b/dev/zka/FSKit/IndexableProperty.h index fd7082f8..fe9dea26 100644 --- a/dev/zka/FSKit/IndexableProperty.h +++ b/dev/zka/FSKit/IndexableProperty.h @@ -30,7 +30,7 @@ namespace Kernel explicit IndexableProperty() : Property() { - Kernel::StringView strProp(cMaxPropLen); + Kernel::StringView strProp(kMaxPropLen); strProp += "\\Properties\\Indexable"; this->GetKey() = strProp; diff --git a/dev/zka/FSKit/NeFS.h b/dev/zka/FSKit/NeFS.h index adcb2be3..07f667e1 100644 --- a/dev/zka/FSKit/NeFS.h +++ b/dev/zka/FSKit/NeFS.h @@ -134,7 +134,7 @@ struct PACKED NFS_CATALOG_STRUCT final /// Catalog status flag. Kernel::UInt16 Flags; /// Custom catalog flags. - Kernel::UInt16 FileFlags; + Kernel::UInt16 FilkMMFlags; /// Catalog kind. Kernel::Int32 Kind; @@ -165,7 +165,7 @@ struct PACKED NFS_FORK_STRUCT final Kernel::Int64 ResourceId; Kernel::Int32 ResourceKind; - Kernel::Int32 ResourceFlags; + Kernel::Int32 ResourckMMFlags; Kernel::Lba DataOffset; // 8 Where to look for this data? Kernel::SizeT DataSize; /// Data size according using sector count. diff --git a/dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc b/dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc index 2ed013ef..ccb5983f 100644 --- a/dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc +++ b/dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc @@ -56,7 +56,7 @@ namespace Kernel UInt8 Century; // reserved in ACPI 1.0; used since ACPI 2.0+ - UInt16 BootArchitectureFlags; + UInt16 BootArchitecturkMMFlags; UInt8 Reserved2; UInt32 Flags; diff --git a/dev/zka/HALKit/AMD64/HalCPU.cc b/dev/zka/HALKit/AMD64/HalCPU.cc deleted file mode 100644 index d24bfc0a..00000000 --- a/dev/zka/HALKit/AMD64/HalCPU.cc +++ /dev/null @@ -1,101 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: HalCPU.cc - Purpose: Platform processor routines. - -------------------------------------------- */ - -#include -#include - -/** - * @file HalCPU.cc - * @brief Common CPU API. - */ - -namespace Kernel::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 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"); - - return value; - } - - UInt16 In16(UInt16 port) - { - UInt16 value = 0UL; - asm volatile("inw %1, %%ax" - : "=a"(value) - : "Nd"(port) - : "memory"); - - return value; - } - - UInt32 In32(UInt16 port) - { - UInt32 value = 0UL; - asm volatile("inl %1, %%eax" - : "=a"(value) - : "Nd"(port) - : "memory"); - - return value; - } - - Void rt_halt() - { - asm volatile("hlt"); - } - - Void rt_cli() - { - asm volatile("cli"); - } - - Void rt_sti() - { - asm volatile("sti"); - } - - Void rt_cld() - { - asm volatile("cld"); - } - - Void rt_std() - { - asm volatile("std"); - } -} // namespace Kernel::HAL diff --git a/dev/zka/HALKit/AMD64/HalCPUAMD64.cc b/dev/zka/HALKit/AMD64/HalCPUAMD64.cc new file mode 100644 index 00000000..d24bfc0a --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalCPUAMD64.cc @@ -0,0 +1,101 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: HalCPU.cc + Purpose: Platform processor routines. + +------------------------------------------- */ + +#include +#include + +/** + * @file HalCPU.cc + * @brief Common CPU API. + */ + +namespace Kernel::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 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"); + + return value; + } + + UInt16 In16(UInt16 port) + { + UInt16 value = 0UL; + asm volatile("inw %1, %%ax" + : "=a"(value) + : "Nd"(port) + : "memory"); + + return value; + } + + UInt32 In32(UInt16 port) + { + UInt32 value = 0UL; + asm volatile("inl %1, %%eax" + : "=a"(value) + : "Nd"(port) + : "memory"); + + return value; + } + + Void rt_halt() + { + asm volatile("hlt"); + } + + Void rt_cli() + { + asm volatile("cli"); + } + + Void rt_sti() + { + asm volatile("sti"); + } + + Void rt_cld() + { + asm volatile("cld"); + } + + Void rt_std() + { + asm volatile("std"); + } +} // namespace Kernel::HAL diff --git a/dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm b/dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm new file mode 100644 index 00000000..cb054a0d --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm @@ -0,0 +1,42 @@ +;; /* +;; * ======================================================== +;; * +;; * ZKA +;; * Copyright ZKA Web Services Co., all rights reserved. +;; * +;; * ======================================================== +;; */ + +[bits 64] + +[global mp_do_task_switch] +[global mp_do_context_switch_pre] +[global mp_user_switch_proc] +[global mp_user_switch_proc_stack_begin] + +section .text + +;; @brief Switch to user mode. +mp_do_task_switch: + mov rbp, rdx + mov rsp, rdx + + mov ax, 0x18 | 3 + mov ds, ax + mov es, ax + mov gs, ax + mov fs, ax + + push 0x18 | 3 + + mov rax, rdx + push rax + + o64 pushf + + push 0x20 | 3 + + mov rax, rcx + push rax + + o64 iret diff --git a/dev/zka/HALKit/AMD64/HalDescriptorLoader.cc b/dev/zka/HALKit/AMD64/HalDescriptorLoader.cc index f0eff4e1..5f7bde36 100644 --- a/dev/zka/HALKit/AMD64/HalDescriptorLoader.cc +++ b/dev/zka/HALKit/AMD64/HalDescriptorLoader.cc @@ -82,7 +82,7 @@ namespace Kernel::HAL Void IDTLoader::Load(Register64& idt) { - const auto kPITTickForScheduler = 100; + const Int16 kPITTickForScheduler = 100; volatile ::Kernel::UIntPtr** ptr_ivt = (volatile ::Kernel::UIntPtr**)idt.Base; diff --git a/dev/zka/HALKit/AMD64/HalKernelMain.cc b/dev/zka/HALKit/AMD64/HalKernelMain.cc index eb195bcc..cd700eef 100644 --- a/dev/zka/HALKit/AMD64/HalKernelMain.cc +++ b/dev/zka/HALKit/AMD64/HalKernelMain.cc @@ -81,21 +81,28 @@ EXTERN_C void hal_init_platform( Kernel::ke_stop(RUNTIME_CHECK_BOOTSTRAP); } +EXTERN_C Kernel::Void hal_kernel_server(Kernel::Void) noexcept +{ + while (Yes) + ; +} + EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept { /* Initialize filesystem. */ - Kernel::NeFileSystemMgr* mgr = Kernel::mm_new_class(); - Kernel::NeFileSystemMgr::Mount(mgr); + Kernel::NeFileSystemMgr::Mount(new Kernel::NeFileSystemMgr()); /* Initialize scheduler. */ Kernel::UserProcessHelper::InitializeScheduler(); + const Kernel::Char kKernelServerName[255] = "KernelServer"; + + Kernel::rtl_create_process(&hal_kernel_server, kKernelServerName); + /* Start any cores. */ if (kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled) Kernel::HAL::mp_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr); - /* Load OSLdr.exe here (TODO) */ - Kernel::HAL::Register64 idt_reg; idt_reg.Base = (Kernel::UIntPtr)kInterruptVectorTable; diff --git a/dev/zka/HALKit/AMD64/HalMPContextSwitch.asm b/dev/zka/HALKit/AMD64/HalMPContextSwitch.asm deleted file mode 100644 index cb054a0d..00000000 --- a/dev/zka/HALKit/AMD64/HalMPContextSwitch.asm +++ /dev/null @@ -1,42 +0,0 @@ -;; /* -;; * ======================================================== -;; * -;; * ZKA -;; * Copyright ZKA Web Services Co., all rights reserved. -;; * -;; * ======================================================== -;; */ - -[bits 64] - -[global mp_do_task_switch] -[global mp_do_context_switch_pre] -[global mp_user_switch_proc] -[global mp_user_switch_proc_stack_begin] - -section .text - -;; @brief Switch to user mode. -mp_do_task_switch: - mov rbp, rdx - mov rsp, rdx - - mov ax, 0x18 | 3 - mov ds, ax - mov es, ax - mov gs, ax - mov fs, ax - - push 0x18 | 3 - - mov rax, rdx - push rax - - o64 pushf - - push 0x20 | 3 - - mov rax, rcx - push rax - - o64 iret diff --git a/dev/zka/HALKit/AMD64/HalPagingMgr.cc b/dev/zka/HALKit/AMD64/HalPagingMgr.cc deleted file mode 100644 index 1a7202d7..00000000 --- a/dev/zka/HALKit/AMD64/HalPagingMgr.cc +++ /dev/null @@ -1,162 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: HalPagingMgr.cc - Purpose: Platform Paging Manager.. - -------------------------------------------- */ - -#include -#include - -namespace Kernel::HAL -{ - typedef UInt32 PageTableIndex; - - /// \brief Page store type. - struct ZKA_PAGE_STORE final - { - struct - { - PDE* fPde{nullptr}; - PTE* fPte{nullptr}; - VoidPtr fVAddr{nullptr}; - } fInternalStore; - - Bool fStoreOp{No}; // Store operation in progress. - - bool IsValidPage(PTE* pte) - { - return pte && pte->Present; - } - - bool IsWRPage(PTE* pte) - { - return pte && pte->Wr; - } - - bool IsUserPage(PTE* pte) - { - return pte && pte->User; - } - - static ZKA_PAGE_STORE& The() - { - static ZKA_PAGE_STORE the; - return the; - } - }; - - /// \brief Retrieve the page status of a PTE. - STATIC Void mmi_page_status(PTE* pte) - { - kcout << (pte->Present ? "Present" : "Not Present") << endl; - kcout << (pte->Wr ? "W/R" : "Not W/R") << endl; - kcout << (pte->ExecDisable ? "NX" : "Not NX") << endl; - kcout << (pte->User ? "User" : "Not User") << endl; - } - - STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry); - - /// @brief Maps or allocates a page from virtual_address. - /// @param virtual_address 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 manipulation process. - EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, UInt32 flags) - { - if (!virtual_address || - !flags) - return 0; - - // Constants for table index bits - const UInt64 cPmlIndexMask = 0x1FFULL; // Mask for PML4, PDPT, PD, PT index (9 bits) - const UInt64 cPtIndexMask = 0xFFFULL; // Mask for page table index (12 bits) - - UInt64 cr3 = (UInt64)hal_read_cr3(); - - ZKA_PAGE_STORE& page_store = ZKA_PAGE_STORE::The(); - - // Extract the indices from the virtual address - UInt64 pml4_index = ((UIntPtr)virtual_address >> 39) & cPmlIndexMask; - UInt64 pdpt_index = ((UIntPtr)virtual_address >> 30) & cPmlIndexMask; - UInt64 pd_index = ((UIntPtr)virtual_address >> 21) & cPmlIndexMask; - UInt64 pt_index = ((UIntPtr)virtual_address >> 12) & cPmlIndexMask; - - while (page_store.fStoreOp) - ; - - page_store.fStoreOp = Yes; - - if (page_store.fInternalStore.fVAddr == virtual_address) - { - page_store.fStoreOp = No; - return mmi_map_page_table_entry(page_store.fInternalStore.fVAddr, flags, page_store.fInternalStore.fPte); - } - - const auto cPmlEntrySize = 8; - - // Read the PML4 entry from memory - UInt64 pml4_base = cr3 & ~cPtIndexMask; // CR3 points to the PML4 table base, mask off lower bits - UInt64 pml4_entry = (pml4_base + pml4_index * cPmlEntrySize); // Each entry is 8 bytes - - // Read the PDPT entry - UInt64 pdpt_base = pml4_entry & ~cPtIndexMask; // Get the PDPT base physical address - UInt64 pdpt_entry = (pdpt_base + pdpt_index * cPmlEntrySize); - - // Read the PD entry - UInt64 pd_base = pdpt_entry & ~cPtIndexMask; // Get the Page Directory base physical address - UInt64 pd_entry = (pd_base + pd_index * cPmlEntrySize); - - // Read the PT entry - UInt64 pt_base = pd_entry & ~cPtIndexMask; // Get the Page Table base physical address - UInt64 pt_entry = (pt_base + pt_index * cPmlEntrySize); - - // Lastly, grab the pte entry. - ZKA_PDE* pte_struct = reinterpret_cast(pt_base); - - return mmi_map_page_table_entry(virtual_address, flags, pte_struct->fEntries[pt_entry]); - } - - /// @brief Maps flags for a specific pte. - /// @internal Internal function. - STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry) - { - if (flags & ~eFlagsPresent) - pt_entry->Present = false; - else if (flags & eFlagsPresent) - pt_entry->Present = true; - - if (flags & eFlagsWr) - pt_entry->Wr = true; - else if (flags & ~eFlagsWr) - pt_entry->Wr = false; - - if (flags & eFlagsNX) - pt_entry->ExecDisable = true; - else if (flags & ~eFlagsNX) - pt_entry->ExecDisable = false; - - if (flags & eFlagsUser) - pt_entry->User = true; - else if (flags & ~eFlagsUser) - pt_entry->User = false; - - hal_invl_tlb(reinterpret_cast(pt_entry)); - - mmi_page_status(pt_entry); - - ZKA_PAGE_STORE& page_store = ZKA_PAGE_STORE::The(); - - // Update Internal store. - - page_store.fInternalStore.fPde = nullptr; - page_store.fInternalStore.fPte = pt_entry; - page_store.fInternalStore.fVAddr = virtual_address; - - page_store.fStoreOp = No; - - return 0; - } -} // namespace Kernel::HAL diff --git a/dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc b/dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc new file mode 100644 index 00000000..39f53fe4 --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc @@ -0,0 +1,159 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: HalPagingMgr.cc + Purpose: Platform Paging Manager.. + +------------------------------------------- */ + +#include +#include + +namespace Kernel::HAL +{ + typedef UInt32 PageTableIndex; + + /// \brief Page store type. + struct ZKA_PAGE_STORE final + { + struct + { + PDE* fPde{nullptr}; + PTE* fPte{nullptr}; + VoidPtr fVAddr{nullptr}; + } fInternalStore; + + Bool fStoreOp{No}; // Store operation in progress. + + bool IsValidPage(PTE* pte) + { + return pte && pte->Present; + } + + bool IsWRPage(PTE* pte) + { + return pte && pte->Wr; + } + + bool IsUserPage(PTE* pte) + { + return pte && pte->User; + } + + static ZKA_PAGE_STORE& The() + { + static ZKA_PAGE_STORE the; + return the; + } + }; + + /// \brief Retrieve the page status of a PTE. + STATIC Void mmi_page_status(PTE* pte) + { + kcout << (pte->Present ? "Present" : "Not Present") << endl; + kcout << (pte->Wr ? "W/R" : "Not W/R") << endl; + kcout << (pte->ExecDisable ? "NX" : "Not NX") << endl; + kcout << (pte->User ? "User" : "Not User") << endl; + } + + STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry); + + /// @brief Maps or allocates a page from virtual_address. + /// @param virtual_address 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 manipulation process. + EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, UInt32 flags) + { + if (!virtual_address || + !flags) + return 0; + + // Constants for table index bits + const UInt64 cPmlIndexMask = 0x1FFULL; // Mask for PML4, PDPT, PD, PT index (9 bits) + const UInt64 cPtIndexMask = 0xFFFULL; // Mask for page table index (12 bits) + + UInt64 cr3 = (UInt64)hal_read_cr3(); + + ZKA_PAGE_STORE& page_store = ZKA_PAGE_STORE::The(); + + // Extract the indices from the virtual address + UInt64 pml4_index = ((UIntPtr)virtual_address >> 39) & cPmlIndexMask; + UInt64 pdpt_index = ((UIntPtr)virtual_address >> 30) & cPmlIndexMask; + UInt64 pd_index = ((UIntPtr)virtual_address >> 21) & cPmlIndexMask; + UInt64 pt_index = ((UIntPtr)virtual_address >> 12) & cPmlIndexMask; + + while (page_store.fStoreOp) + ; + + page_store.fStoreOp = Yes; + + if (page_store.fInternalStore.fVAddr == virtual_address) + { + page_store.fStoreOp = No; + return mmi_map_page_table_entry(page_store.fInternalStore.fVAddr, flags, page_store.fInternalStore.fPte); + } + + const auto cPmlEntrySize = 8; + + // Read the PML4 entry from memory + UInt64 pml4_base = cr3 & ~cPtIndexMask; // CR3 points to the PML4 table base, mask off lower bits + UInt64 pml4_entry = (pml4_base + pml4_index * cPmlEntrySize); // Each entry is 8 bytes + + // Read the PDPT entry + UInt64 pdpt_base = pml4_entry & ~cPtIndexMask; // Get the PDPT base physical address + UInt64 pdpt_entry = (pdpt_base + pdpt_index * cPmlEntrySize); + + // Read the PD entry + UInt64 pd_base = pdpt_entry & ~cPtIndexMask; // Get the Page Directory base physical address + UInt64 pd_entry = (pd_base + pd_index * cPmlEntrySize); + + // Read the PT entry + UInt64 pt_base = pd_entry & ~cPtIndexMask; // Get the Page Table base physical address + UInt64 pt_entry = (pt_base + pt_index * cPmlEntrySize); + + // Lastly, grab the pte entry. + ZKA_PDE* pte_struct = reinterpret_cast(pt_base); + + return mmi_map_page_table_entry(virtual_address, flags, pte_struct->fEntries[pt_entry]); + } + + /// @brief Maps flags for a specific pte. + /// @internal Internal function. + STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry) + { + pt_entry->Present = true; + + if (flags & kMMFlagsWr) + pt_entry->Wr = true; + else if (flags & ~kMMFlagsWr) + pt_entry->Wr = false; + + if (flags & kMMFlagsNX) + pt_entry->ExecDisable = true; + else if (flags & ~kMMFlagsNX) + pt_entry->ExecDisable = false; + + if (flags & kMMFlagsUser) + pt_entry->User = true; + else if (flags & ~kMMFlagsUser) + pt_entry->User = false; + + hal_invl_tlb(reinterpret_cast(pt_entry)); + + mmi_page_status(pt_entry); + + ZKA_PAGE_STORE& page_store = ZKA_PAGE_STORE::The(); + + // Update Internal store. + + page_store.fInternalStore.fPde = nullptr; + page_store.fInternalStore.fPte = pt_entry; + page_store.fInternalStore.fVAddr = virtual_address; + + page_store.fStoreOp = No; + + return 0; + } +} // namespace Kernel::HAL diff --git a/dev/zka/HALKit/AMD64/HalSchedulerCore.cc b/dev/zka/HALKit/AMD64/HalSchedulerCore.cc deleted file mode 100644 index 214b889b..00000000 --- a/dev/zka/HALKit/AMD64/HalSchedulerCore.cc +++ /dev/null @@ -1,56 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include - -using namespace Kernel; - -Void UserProcess::SetImageStart(VoidPtr imageStart) noexcept -{ - if (imageStart == nullptr) - this->Crash(); - - this->Image = imageStart; -} - -namespace Kernel -{ - /***********************************************************************************/ - /// @brief Unimplemented function (crashes by default) - /// @param - /***********************************************************************************/ - - EXTERN_C Void __zka_pure_call(void) - { - UserProcessScheduler::The().CurrentProcess().Leak().Crash(); - } - - Bool hal_check_stack(HAL::StackFramePtr stack_ptr) - { - if (!stack_ptr) - return false; - - return true; - } - - /// @brief Wakes up thread. - /// Wakes up thread from the hang state. - Void mp_wakeup_thread(HAL::StackFrame* stack) - { - Kernel::UserProcessHelper::StartScheduling(); - } - - /// @brief makes the thread sleep on a loop. - /// hooks and hangs thread to prevent code from executing. - Void mp_hang_thread(HAL::StackFrame* stack) - { - while (Yes) - { - /* Nothing to do, code is spinning */ - } - } -} // namespace Kernel diff --git a/dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc b/dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc new file mode 100644 index 00000000..d4ca494c --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc @@ -0,0 +1,46 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include + +namespace Kernel +{ + /***********************************************************************************/ + /// @brief Unimplemented function (crashes by default) + /// @param + /***********************************************************************************/ + + EXTERN_C Void __zka_pure_call(void) + { + UserProcessScheduler::The().CurrentProcess().Leak().Crash(); + } + + Bool hal_check_stack(HAL::StackFramePtr stack_ptr) + { + if (!stack_ptr) + return false; + + return true; + } + + /// @brief Wakes up thread. + /// Wakes up thread from the hang state. + Void mp_wakeup_thread(HAL::StackFrame* stack) + { + Kernel::UserProcessHelper::StartScheduling(); + } + + /// @brief makes the thread sleep on a loop. + /// hooks and hangs thread to prevent code from executing. + Void mp_hang_thread(HAL::StackFrame* stack) + { + while (Yes) + { + /* Nothing to do, code is spinning */ + } + } +} // namespace Kernel diff --git a/dev/zka/HALKit/AMD64/HalTimer.cc b/dev/zka/HALKit/AMD64/HalTimer.cc deleted file mode 100644 index c7f6bea3..00000000 --- a/dev/zka/HALKit/AMD64/HalTimer.cc +++ /dev/null @@ -1,86 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: HalTimer.cc - Purpose: HAL timer - - Revision History: - - 07/07/24: Added file (amlel) - -------------------------------------------- */ - -#include -#include -#include - -// timer slot 0 - -#define cHPETCounterRegValue (0x00) -#define cHPETConfigRegValue (0x20) -#define cHPETCompRegValue (0x24) -#define cHPETInterruptRegValue (0x2C) - -///! BUGS: 0 -///! @file HalTimer.cc -///! @brief Hardware Timer (HPET) - -namespace Kernel::Detail -{ - struct HPET_BLOCK : public Kernel::SDT - { - Kernel::UInt8 hardware_rev_id; - Kernel::UInt8 comparator_count : 5; - Kernel::UInt8 counter_size : 1; - Kernel::UInt8 reserved : 1; - Kernel::UInt8 legacy_replacement : 1; - Kernel::UInt16 pci_vendor_id; - ACPI_ADDRESS address; - Kernel::UInt8 hpet_number; - Kernel::UInt16 minimum_tick; - Kernel::UInt8 page_protection; - } PACKED; -} // namespace Kernel::Detail - -using namespace Kernel; - -HardwareTimer::HardwareTimer(Int64 ms) - : fWaitFor(ms) -{ - auto power = PowerFactoryInterface(kHandoverHeader->f_HardwareTables.f_VendorPtr); - - auto hpet = (Detail::HPET_BLOCK*)power.Find("HPET").Leak().Leak(); - MUST_PASS(hpet); - - fDigitalTimer = (IntPtr*)hpet->address.Address; -} - -HardwareTimer::~HardwareTimer() -{ - fDigitalTimer = nullptr; - fWaitFor = 0; -} - -Int32 HardwareTimer::Wait() noexcept -{ - if (fWaitFor < 1) - return -1; - - // if not enabled yet. - if (!(*(fDigitalTimer + cHPETConfigRegValue) & (1 << 0))) - { - *(fDigitalTimer + cHPETConfigRegValue) |= (1 << 0); // enable it - *(fDigitalTimer + cHPETConfigRegValue) |= (1 << 3); // one shot conf - } - - UInt64 ticks = fWaitFor / ((*(fDigitalTimer) >> 32) & __UINT32_MAX__); - UInt64 prev = *(fDigitalTimer + cHPETCounterRegValue); - - prev += ticks; - - while (*(fDigitalTimer + cHPETCounterRegValue) < (ticks)) - ; - - return 0; -} diff --git a/dev/zka/HALKit/AMD64/HalTimerAMD64.cc b/dev/zka/HALKit/AMD64/HalTimerAMD64.cc new file mode 100644 index 00000000..c7f6bea3 --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalTimerAMD64.cc @@ -0,0 +1,86 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: HalTimer.cc + Purpose: HAL timer + + Revision History: + + 07/07/24: Added file (amlel) + +------------------------------------------- */ + +#include +#include +#include + +// timer slot 0 + +#define cHPETCounterRegValue (0x00) +#define cHPETConfigRegValue (0x20) +#define cHPETCompRegValue (0x24) +#define cHPETInterruptRegValue (0x2C) + +///! BUGS: 0 +///! @file HalTimer.cc +///! @brief Hardware Timer (HPET) + +namespace Kernel::Detail +{ + struct HPET_BLOCK : public Kernel::SDT + { + Kernel::UInt8 hardware_rev_id; + Kernel::UInt8 comparator_count : 5; + Kernel::UInt8 counter_size : 1; + Kernel::UInt8 reserved : 1; + Kernel::UInt8 legacy_replacement : 1; + Kernel::UInt16 pci_vendor_id; + ACPI_ADDRESS address; + Kernel::UInt8 hpet_number; + Kernel::UInt16 minimum_tick; + Kernel::UInt8 page_protection; + } PACKED; +} // namespace Kernel::Detail + +using namespace Kernel; + +HardwareTimer::HardwareTimer(Int64 ms) + : fWaitFor(ms) +{ + auto power = PowerFactoryInterface(kHandoverHeader->f_HardwareTables.f_VendorPtr); + + auto hpet = (Detail::HPET_BLOCK*)power.Find("HPET").Leak().Leak(); + MUST_PASS(hpet); + + fDigitalTimer = (IntPtr*)hpet->address.Address; +} + +HardwareTimer::~HardwareTimer() +{ + fDigitalTimer = nullptr; + fWaitFor = 0; +} + +Int32 HardwareTimer::Wait() noexcept +{ + if (fWaitFor < 1) + return -1; + + // if not enabled yet. + if (!(*(fDigitalTimer + cHPETConfigRegValue) & (1 << 0))) + { + *(fDigitalTimer + cHPETConfigRegValue) |= (1 << 0); // enable it + *(fDigitalTimer + cHPETConfigRegValue) |= (1 << 3); // one shot conf + } + + UInt64 ticks = fWaitFor / ((*(fDigitalTimer) >> 32) & __UINT32_MAX__); + UInt64 prev = *(fDigitalTimer + cHPETCounterRegValue); + + prev += ticks; + + while (*(fDigitalTimer + cHPETCounterRegValue) < (ticks)) + ; + + return 0; +} diff --git a/dev/zka/HALKit/AMD64/Processor.h b/dev/zka/HALKit/AMD64/Processor.h index 8dbac715..968cf5e9 100644 --- a/dev/zka/HALKit/AMD64/Processor.h +++ b/dev/zka/HALKit/AMD64/Processor.h @@ -71,11 +71,12 @@ namespace Kernel::HAL /// @brief Memory Manager mapping flags. enum { - eFlagsPresent = 1 << 0, - eFlagsWr = 1 << 1, - eFlagsUser = 1 << 2, - eFlagsNX = 1 << 3, - eFlagsCount = 3, + kMMFlagsInvalid = 0 << 0, + kMMFlagsPresent = 1 << 0, + kMMFlagsWr = 1 << 1, + kMMFlagsUser = 1 << 2, + kMMFlagsNX = 1 << 3, + kMMFlagsCount = 4, }; struct PACKED Register64 final diff --git a/dev/zka/HALKit/ARM64/HalSchedulerCore.cc b/dev/zka/HALKit/ARM64/HalSchedulerCore.cc deleted file mode 100644 index a384db22..00000000 --- a/dev/zka/HALKit/ARM64/HalSchedulerCore.cc +++ /dev/null @@ -1,50 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include - -namespace Kernel -{ - EXTERN_C Void __zka_pure_call(void) - { - UserProcessScheduler::The().CurrentProcess().Leak().Crash(); - } - - Void UserProcess::SetImageStart(VoidPtr image_start) noexcept - { - if (image_start == 0) - this->Crash(); - - this->Image = image_start; - } - - bool hal_check_stack(HAL::StackFramePtr stackPtr) - { - if (!stackPtr) - return false; - if (stackPtr->BP == 0 || stackPtr->SP == 0) - return false; - - return true; - } - - /// @brief Wakes up thread. - /// Wakes up thread from the hang state. - Void mp_wakeup_thread(HAL::StackFrame* stack) - { - ZKA_UNUSED(stack); - } - - /// @brief makes the thread sleep on a loop. - /// hooks and hangs thread to prevent code from executing. - Void mp_hang_thread(HAL::StackFrame* stack) - { - while (Yes) - { - /* Nothing to do, code is spinning */ - } - } -} // namespace Kernel diff --git a/dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc b/dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc new file mode 100644 index 00000000..49927c84 --- /dev/null +++ b/dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc @@ -0,0 +1,40 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include + +namespace Kernel +{ + EXTERN_C Void __zka_pure_call(void) + { + UserProcessScheduler::The().CurrentProcess().Leak().Crash(); + } + + bool hal_check_stack(HAL::StackFramePtr stackPtr) + { + if (!stackPtr) + return No; + + if (stackPtr->BP == 0 || stackPtr->SP == 0) + return No; + + return Yes; + } + + /// @brief Wakes up thread. + /// Wakes up thread from the hang state. + Void mp_wakeup_thread(HAL::StackFrame* stack) + { + ZKA_UNUSED(stack); + } + + /// @brief makes the thread sleep on a loop. + /// hooks and hangs thread to prevent code from executing. + Void mp_hang_thread(HAL::StackFrame* stack) + { + ZKA_UNUSUED(stack); + } +} // namespace Kernel diff --git a/dev/zka/HALKit/ARM64/Processor.h b/dev/zka/HALKit/ARM64/Processor.h index 5f00e345..793761e2 100644 --- a/dev/zka/HALKit/ARM64/Processor.h +++ b/dev/zka/HALKit/ARM64/Processor.h @@ -24,11 +24,11 @@ namespace Kernel::HAL /// @brief Memory Manager mapping flags. enum { - eFlagsPresent = 1 << 0, - eFlagsWr = 1 << 1, - eFlagsUser = 1 << 2, - eFlagsNX = 1 << 3, - eFlagsCount = 3, + kMMFlagsPresent = 1 << 0, + kMMFlagsWr = 1 << 1, + kMMFlagsUser = 1 << 2, + kMMFlagsNX = 1 << 3, + kMMFlagsCount = 3, }; /// @brief Set a PTE from pd_base. diff --git a/dev/zka/HALKit/POWER/HalVirtualMemory.cc b/dev/zka/HALKit/POWER/HalVirtualMemory.cc index 8d8db849..e21d7074 100644 --- a/dev/zka/HALKit/POWER/HalVirtualMemory.cc +++ b/dev/zka/HALKit/POWER/HalVirtualMemory.cc @@ -4,32 +4,30 @@ ------------------------------------------- */ -#include -#include - #include #include +#include -/// @note refer to the SoC documentation. +/// @note Refer to SoC documentation. using namespace Kernel; -Void hal_write_tlb(UInt32 mas0, UInt32 mas1, UInt32 mas2, UInt32 mas3, UInt32 mas7) +EXTERN_C Void hal_write_tlb(UInt32 mas0, UInt32 mas1, UInt32 mas2, UInt32 mas3, UInt32 mas7) { - mtspr(MAS0, mas0); - mtspr(MAS1, mas1); - mtspr(MAS2, mas2); - mtspr(MAS3, mas3); - mtspr(MAS7, mas7); + hal_mtspr(MAS0, mas0); + hal_mtspr(MAS1, mas1); + hal_mtspr(MAS2, mas2); + hal_mtspr(MAS3, mas3); + hal_mtspr(MAS7, mas7); hal_flush_tlb(); } -Bool hal_set_tlb(UInt8 tlb, UInt32 epn, UInt64 rpn, UInt8 perms, UInt8 wimge, UInt8 ts, UInt8 esel, UInt8 tsize, UInt8 iprot) +EXTERN_C Bool hal_set_tlb(UInt8 tlb, UInt32 epn, UInt64 rpn, UInt8 perms, UInt8 wimge, UInt8 ts, UInt8 esel, UInt8 tsize, UInt8 iprot) { - if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 && (tsize & 1)) + if ((hal_mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 && (tsize & 1)) { - // this mmu-version does not allow odd tsize values + // this MMU does not allow odd tsize values return false; } diff --git a/dev/zka/HALKit/POWER/Hart.h b/dev/zka/HALKit/POWER/Hart.h index eaf00493..2e40784e 100644 --- a/dev/zka/HALKit/POWER/Hart.h +++ b/dev/zka/HALKit/POWER/Hart.h @@ -25,7 +25,7 @@ typedef struct HAL_HARDWARE_THREAD { Kernel::UIntPtr fStartAddress; Kernel::UInt8 fPrivleged : 1; - Kernel::UInt32 fPageFlags; + Kernel::UInt32 fPagkMMFlags; PPCHartType fIdentNumber; } HAL_HARDWARE_THREAD; diff --git a/dev/zka/HALKit/POWER/Processor.h b/dev/zka/HALKit/POWER/Processor.h index 95c1f1c6..4df55fc4 100644 --- a/dev/zka/HALKit/POWER/Processor.h +++ b/dev/zka/HALKit/POWER/Processor.h @@ -29,6 +29,8 @@ namespace Kernel::HAL Reg R13{0}; Reg R14{0}; Reg R15{0}; + Reg SP{0}; + Reg BP{0}; }; typedef StackFrame* StackFramePtr; diff --git a/dev/zka/KernelKit/CodeMgr.h b/dev/zka/KernelKit/CodeMgr.h index 41a90ec5..b8fa4c37 100644 --- a/dev/zka/KernelKit/CodeMgr.h +++ b/dev/zka/KernelKit/CodeMgr.h @@ -26,6 +26,6 @@ namespace Kernel /// @brief Executes a new process from a function. Kernel code only. /// @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 sched_execute_thread(MainKind main, const Char* process_name) noexcept; + /// @return The team's process id. + SizeT rtl_create_process(MainKind main, const Char* process_name) noexcept; } // namespace Kernel diff --git a/dev/zka/KernelKit/PEFCodeMgr.h b/dev/zka/KernelKit/PEFCodeMgr.h index 73a015d3..f4e9e446 100644 --- a/dev/zka/KernelKit/PEFCodeMgr.h +++ b/dev/zka/KernelKit/PEFCodeMgr.h @@ -60,7 +60,7 @@ namespace Kernel namespace Utils { - bool execute_from_image(PEFLoader& exec, const Int32& procKind) noexcept; + SizeT execute_from_image(PEFLoader& exec, const Int32& procKind) noexcept; } // namespace Utils } // namespace Kernel diff --git a/dev/zka/KernelKit/UserProcessScheduler.h b/dev/zka/KernelKit/UserProcessScheduler.h index 21e30b9b..c819b708 100644 --- a/dev/zka/KernelKit/UserProcessScheduler.h +++ b/dev/zka/KernelKit/UserProcessScheduler.h @@ -126,9 +126,9 @@ namespace Kernel using HeapPtrKind = VoidPtr; /// @name UserProcess - /// @brief User process block. + /// @brief User process header. /// Holds information about the running process/thread. - struct UserProcess final + class UserProcess final { public: explicit UserProcess(VoidPtr startImage = nullptr) @@ -141,11 +141,10 @@ namespace Kernel ZKA_COPY_DEFAULT(UserProcess) public: - Void SetImageStart(VoidPtr imageStart) noexcept; const UInt32& GetExitCode() noexcept; public: - Char Name[kProcessLen] = {"ZKA Process"}; + Char Name[kProcessLen] = {"Process"}; ProcessSubsystem SubSystem{ProcessSubsystem::kProcessSubsystemInvalid}; User* Owner{nullptr}; HAL::StackFramePtr StackFrame{nullptr}; @@ -175,10 +174,11 @@ namespace Kernel PROCESS_MEMORY_ENTRY* MemoryEntryList{nullptr}; - UIntPtr MemoryPD{0UL}; + UIntPtr VMRegister{0UL}; enum { + kInvalidExecutableKind, kExectuableKind, kExectuableDLLKind, kExectuableKindCount, @@ -256,8 +256,8 @@ namespace Kernel using UserProcessPtr = UserProcess*; - /// @brief UserProcess scheduler class. - /// The main class which you call to schedule processes. + /// @brief Process scheduler class. + /// The main class which you call to schedule user processes. class UserProcessScheduler final : public ISchedulerObject { friend class UserProcessHelper; @@ -276,7 +276,7 @@ namespace Kernel public: SizeT Add(UserProcess process); - Bool Remove(ProcessID process_id); + const Bool Remove(ProcessID process_id); const Bool IsUser() override; const Bool IsKernel() override; diff --git a/dev/zka/amd64-efi.make b/dev/zka/amd64-efi.make index e39a55ab..b877dbc7 100644 --- a/dev/zka/amd64-efi.make +++ b/dev/zka/amd64-efi.make @@ -58,7 +58,7 @@ newos-amd64-epm: clean $(wildcard HALKit/AMD64/*.cc) $(wildcard HALKit/AMD64/*.cpp) \ $(wildcard HALKit/AMD64/*.s) $(ASM) $(ASMFLAGS) HALKit/AMD64/HalInterruptAPI.asm - $(ASM) $(ASMFLAGS) HALKit/AMD64/HalMPContextSwitch.asm + $(ASM) $(ASMFLAGS) HALKit/AMD64/HalContextSwitchAMD64.asm $(ASM) $(ASMFLAGS) HALKit/AMD64/HalBoot.asm $(ASM) $(ASMFLAGS) HALKit/AMD64/HalUtils.asm $(MOVEALL) diff --git a/dev/zka/src/BitMapMgr.cc b/dev/zka/src/BitMapMgr.cc index e97c9bfa..27158a12 100644 --- a/dev/zka/src/BitMapMgr.cc +++ b/dev/zka/src/BitMapMgr.cc @@ -17,9 +17,9 @@ #include #include -#define cBitMapMagIdx (0) -#define cBitMapSizeIdx (1) -#define cBitMapUsedIdx (2) +#define kBitMapMagIdx (0) +#define kBitMapSizeIdx (1) +#define kBitMapUsedIdx (2) namespace Kernel { @@ -43,8 +43,8 @@ namespace Kernel UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr); - if (!ptr_bit_set[cBitMapMagIdx] || - ptr_bit_set[cBitMapMagIdx] != kBitMapMagic) + if (!ptr_bit_set[kBitMapMagIdx] || + ptr_bit_set[kBitMapMagIdx] != kBitMapMagic) return No; return Yes; @@ -57,28 +57,24 @@ namespace Kernel UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr); - ptr_bit_set[cBitMapMagIdx] = kBitMapMagic; - ptr_bit_set[cBitMapUsedIdx] = No; + ptr_bit_set[kBitMapMagIdx] = kBitMapMagic; + ptr_bit_set[kBitMapUsedIdx] = No; this->GetBitMapStatus(ptr_bit_set); - mm_map_page(ptr_bit_set, ~eFlagsPresent); - mm_map_page(ptr_bit_set, ~eFlagsWr); - mm_map_page(ptr_bit_set, ~eFlagsUser); - return Yes; } - UInt32 MakeFlags(Bool wr, Bool user) + UInt32 MakeMMFlags(Bool wr, Bool user) { - UInt32 flags = eFlagsPresent; + UInt32 flags = kMMFlagsPresent; if (wr) - flags |= eFlagsWr; + flags |= kMMFlagsWr; if (user) - flags |= eFlagsUser; + flags |= kMMFlagsUser; return flags; } @@ -93,33 +89,33 @@ namespace Kernel { UIntPtr* ptr_bit_set = reinterpret_cast(base); - if (ptr_bit_set[cBitMapMagIdx] == kBitMapMagic && - ptr_bit_set[cBitMapSizeIdx] <= size) + if (ptr_bit_set[kBitMapMagIdx] == kBitMapMagic && + ptr_bit_set[kBitMapSizeIdx] <= size) { - if (ptr_bit_set[cBitMapUsedIdx] == No) + if (ptr_bit_set[kBitMapUsedIdx] == No) { - ptr_bit_set[cBitMapSizeIdx] = size; - ptr_bit_set[cBitMapUsedIdx] = Yes; + ptr_bit_set[kBitMapSizeIdx] = size; + ptr_bit_set[kBitMapUsedIdx] = Yes; this->GetBitMapStatus(ptr_bit_set); - UInt32 flags = this->MakeFlags(wr, user); + UInt32 flags = this->MakeMMFlags(wr, user); mm_map_page(ptr_bit_set, flags); return (VoidPtr)ptr_bit_set; } } - else if (ptr_bit_set[cBitMapMagIdx] != kBitMapMagic) + else if (ptr_bit_set[kBitMapMagIdx] != kBitMapMagic) { UIntPtr* ptr_bit_set = reinterpret_cast(base_ptr); - ptr_bit_set[cBitMapMagIdx] = kBitMapMagic; - ptr_bit_set[cBitMapSizeIdx] = size; - ptr_bit_set[cBitMapUsedIdx] = Yes; + ptr_bit_set[kBitMapMagIdx] = kBitMapMagic; + ptr_bit_set[kBitMapSizeIdx] = size; + ptr_bit_set[kBitMapUsedIdx] = Yes; this->GetBitMapStatus(ptr_bit_set); - UInt32 flags = this->MakeFlags(wr, user); + UInt32 flags = this->MakeMMFlags(wr, user); mm_map_page(ptr_bit_set, flags); return (VoidPtr)ptr_bit_set; @@ -143,13 +139,13 @@ namespace Kernel return; } - kcout << "Magic Number: " << hex_number(ptr_bit_set[cBitMapMagIdx]) << endl; - kcout << "Is Allocated: " << (ptr_bit_set[cBitMapUsedIdx] ? "Yes" : "No") << endl; - kcout << "Size of BitMap (B): " << number(ptr_bit_set[cBitMapSizeIdx]) << endl; - kcout << "Size of BitMap (KIB): " << number(KIB(ptr_bit_set[cBitMapSizeIdx])) << endl; - kcout << "Size of BitMap (MIB): " << number(MIB(ptr_bit_set[cBitMapSizeIdx])) << endl; - kcout << "Size of BitMap (GIB): " << number(GIB(ptr_bit_set[cBitMapSizeIdx])) << endl; - kcout << "Size of BitMap (TIB): " << number(TIB(ptr_bit_set[cBitMapSizeIdx])) << endl; + kcout << "Magic Number: " << hex_number(ptr_bit_set[kBitMapMagIdx]) << endl; + kcout << "Is Allocated: " << (ptr_bit_set[kBitMapUsedIdx] ? "Yes" : "No") << endl; + kcout << "Size of BitMap (B): " << number(ptr_bit_set[kBitMapSizeIdx]) << endl; + kcout << "Size of BitMap (KIB): " << number(KIB(ptr_bit_set[kBitMapSizeIdx])) << endl; + kcout << "Size of BitMap (MIB): " << number(MIB(ptr_bit_set[kBitMapSizeIdx])) << endl; + kcout << "Size of BitMap (GIB): " << number(GIB(ptr_bit_set[kBitMapSizeIdx])) << endl; + kcout << "Size of BitMap (TIB): " << number(TIB(ptr_bit_set[kBitMapSizeIdx])) << endl; kcout << "Address Of BitMap: " << hex_number((UIntPtr)ptr_bit_set) << endl; } }; @@ -171,15 +167,6 @@ namespace Kernel return nullptr; } - if (wr) - mm_map_page(ptr_new, eFlagsWr | eFlagsPresent); - else if (user && wr) - mm_map_page(ptr_new, eFlagsUser | eFlagsWr | eFlagsPresent); - else if (user) - mm_map_page(ptr_new, eFlagsUser | eFlagsPresent); - else - mm_map_page(ptr_new, eFlagsPresent); - return (UIntPtr*)ptr_new; } @@ -192,11 +179,6 @@ namespace Kernel Detail::IBitMapAllocator traits; Bool ret = traits.FreeBitMap(page_ptr); - if (ret) - { - mm_map_page(page_ptr, ~eFlagsPresent); - } - return ret; } } // namespace HAL diff --git a/dev/zka/src/CodeMgr.cc b/dev/zka/src/CodeMgr.cc index 130f8cfe..c855347b 100644 --- a/dev/zka/src/CodeMgr.cc +++ b/dev/zka/src/CodeMgr.cc @@ -4,8 +4,8 @@ ------------------------------------------- */ -#include #include +#include #include namespace Kernel @@ -14,20 +14,20 @@ 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 sched_execute_thread(MainKind main, const Char* process_name) noexcept + SizeT rtl_create_process(MainKind main, const Char* process_name) noexcept { if (!main) return No; UserProcess proc; - proc.SetImageStart(reinterpret_cast(main)); + proc.Image = reinterpret_cast(main); proc.Kind = UserProcess::kExectuableKind; proc.StackSize = kib_cast(32); rt_set_memory(proc.Name, 0, kProcessLen); rt_copy_memory((VoidPtr)process_name, proc.Name, rt_string_len(process_name)); - return UserProcessScheduler::The().Add(proc) > 0; + return UserProcessScheduler::The().Add(proc); } } // namespace Kernel diff --git a/dev/zka/src/GUIDWizard.cc b/dev/zka/src/GUIDWizard.cc index d72c77eb..cb4fc50b 100644 --- a/dev/zka/src/GUIDWizard.cc +++ b/dev/zka/src/GUIDWizard.cc @@ -26,9 +26,9 @@ namespace CFKit::XRN::Version1 Ref seq_ref{seq}; - seq_ref.Leak()->fMs1 = uuidSeq[0]; - seq_ref.Leak()->fMs2 = uuidSeq[1]; - seq_ref.Leak()->fMs3 = uuidSeq[2]; + seq_ref.Leak()->fMs1 = uuidSeq[0]; + seq_ref.Leak()->fMs2 = uuidSeq[1]; + seq_ref.Leak()->fMs3 = uuidSeq[2]; seq_ref.Leak()->fMs4[0] = uuidSeq[3]; seq_ref.Leak()->fMs4[1] = uuidSeq[4]; seq_ref.Leak()->fMs4[2] = uuidSeq[5]; diff --git a/dev/zka/src/PEFCodeMgr.cc b/dev/zka/src/PEFCodeMgr.cc index 593910be..44c14b42 100644 --- a/dev/zka/src/PEFCodeMgr.cc +++ b/dev/zka/src/PEFCodeMgr.cc @@ -201,16 +201,16 @@ namespace Kernel namespace Utils { - Bool execute_from_image(PEFLoader& exec, const Int32& procKind) noexcept + SizeT execute_from_image(PEFLoader& exec, const Int32& procKind) noexcept { auto errOrStart = exec.FindStart(); if (errOrStart.Error() != kErrorSuccess) - return false; + return No; UserProcess proc; - proc.SetImageStart(errOrStart.Leak().Leak()); + proc.Image = errOrStart.Leak().Leak(); proc.Kind = procKind; proc.StackSize = *(UIntPtr*)exec.FindSymbol(cPefStackSizeSymbol, kPefData); proc.MemoryLimit = *(UIntPtr*)exec.FindSymbol(cPefHeapSizeSymbol, kPefData); @@ -218,18 +218,15 @@ namespace Kernel rt_set_memory(proc.Name, 0, kProcessLen); if (exec.FindSymbol(cPefNameSymbol, kPefData)) - rt_copy_memory((VoidPtr)exec.FindSymbol(cPefNameSymbol, kPefData), proc.Name, rt_string_len((Char*)exec.FindSymbol(cPefNameSymbol, kPefData))); - else - rt_copy_memory((VoidPtr) "UNNAMED PROCESS.", proc.Name, rt_string_len("UNNAMED PROCESS.")); + rt_copy_memory(exec.FindSymbol(cPefNameSymbol, kPefData), proc.Name, rt_string_len((Char*)exec.FindSymbol(cPefNameSymbol, kPefData))); if (!proc.StackSize) { const auto cDefaultStackSizeMib = 8; - proc.StackSize = mib_cast(cDefaultStackSizeMib); } - return UserProcessScheduler::The().Add(proc) > 0; + return UserProcessScheduler::The().Add(proc); } } // namespace Utils diff --git a/dev/zka/src/UserProcessScheduler.cc b/dev/zka/src/UserProcessScheduler.cc index 05c2b35e..844afa5d 100644 --- a/dev/zka/src/UserProcessScheduler.cc +++ b/dev/zka/src/UserProcessScheduler.cc @@ -113,7 +113,7 @@ namespace Kernel { #ifdef __ZKA_AMD64__ auto pd = hal_read_cr3(); - hal_write_cr3(reinterpret_cast(this->MemoryPD)); + hal_write_cr3(reinterpret_cast(this->VMRegister)); auto ptr = mm_new_heap(sz, Yes, Yes); @@ -174,7 +174,7 @@ namespace Kernel { #ifdef __ZKA_AMD64__ auto pd = hal_read_cr3(); - hal_write_cr3(reinterpret_cast(this->MemoryPD)); + hal_write_cr3(reinterpret_cast(this->VMRegister)); auto ret = mm_delete_heap(entry->MemoryEntry); @@ -250,7 +250,7 @@ namespace Kernel { #ifdef __ZKA_AMD64__ auto pd = hal_read_cr3(); - hal_write_cr3(reinterpret_cast(this->MemoryPD)); + hal_write_cr3(reinterpret_cast(this->VMRegister)); #endif MUST_PASS(mm_delete_heap(memory_list->MemoryEntry)); @@ -269,7 +269,7 @@ namespace Kernel } //! Free the memory's page directory. - HAL::mm_free_bitmap(reinterpret_cast(this->MemoryPD)); + HAL::mm_free_bitmap(reinterpret_cast(this->VMRegister)); //! Delete image if not done already. if (this->Image && mm_is_valid_heap(this->Image)) @@ -310,14 +310,12 @@ namespace Kernel SizeT UserProcessScheduler::Add(UserProcess process) { if (mTeam.mProcessAmount > kSchedProcessLimitPerTeam) - return 0; + return -kErrorInvalidData; #ifdef __ZKA_AMD64__ - process.MemoryPD = reinterpret_cast(HAL::mm_alloc_bitmap(Yes, Yes, sizeof(PDE), Yes)); + process.VMRegister = reinterpret_cast(HAL::mm_alloc_bitmap(Yes, Yes, sizeof(PDE), Yes)); #endif // __ZKA_AMD64__ - process.Status = ProcessStatusKind::kStarting; - process.StackFrame = (HAL::StackFramePtr)mm_new_heap(sizeof(HAL::StackFrame), Yes, Yes); if (!process.StackFrame) @@ -347,22 +345,18 @@ namespace Kernel if (!process.StackReserve) { - mm_delete_heap(process.StackFrame); - process.StackFrame = nullptr; + process.Crash(); return -kErrorProcessFault; } ++mTeam.mProcessAmount; process.ProcessId = mTeam.mProcessAmount; - process.Status = ProcessStatusKind::kRunning; - - // avoid the pitfalls of moving process. - auto ret_pid = process.ProcessId; + process.Status = ProcessStatusKind::kStarting; - mTeam.AsArray()[process.ProcessId] = move(process); + mTeam.AsArray()[process.ProcessId] = process; - return ret_pid; + return process.ProcessId; } /***********************************************************************************/ @@ -384,7 +378,7 @@ namespace Kernel /***********************************************************************************/ - Bool UserProcessScheduler::Remove(ProcessID process_id) + const Bool UserProcessScheduler::Remove(ProcessID process_id) { // check if process is within range. if (process_id > mTeam.AsArray().Count()) @@ -408,6 +402,7 @@ namespace Kernel const Bool UserProcessScheduler::HasMP() { + MUST_PASS(kHandoverHeader); return kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled; } @@ -447,7 +442,7 @@ namespace Kernel this->CurrentProcess() = process; // tell helper to find a core to schedule on. - if (!UserProcessHelper::Switch(process.Image, &process.StackReserve[process.StackSize - 1], process.StackFrame, + if (!UserProcessHelper::Switch(process.Image, &process.StackReserve[process.StackSize], process.StackFrame, process.ProcessId)) { process.Crash(); diff --git a/dev/zka/src/Utils.cc b/dev/zka/src/Utils.cc index 7427fe34..53f98391 100644 --- a/dev/zka/src/Utils.cc +++ b/dev/zka/src/Utils.cc @@ -34,14 +34,15 @@ namespace Kernel Size rt_string_len(const Char* str, SizeT _len) { Size len{0}; + while (str[len] != '\0') { if (len > _len) { - return 0; + return _len; } - len++; + ++len; } return len; @@ -49,14 +50,11 @@ namespace Kernel Size rt_string_len(const Char* ptr) { - if (*ptr == 0) - return 0; + SizeT cnt{0}; - SizeT cnt = 0; - - while (ptr[cnt] != (Char)0) + while (ptr[cnt] != 0) { - cnt++; + ++cnt; } return cnt; @@ -104,7 +102,7 @@ namespace Kernel Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len) { if (len < 1) - return -2; + return 0; char* srcChr = reinterpret_cast(src); char* dstChar = reinterpret_cast(dst); @@ -125,11 +123,13 @@ namespace Kernel return nullptr; const Char* string = new Char[rt_string_len(text)]; + if (!string) return nullptr; voidPtr vText = reinterpret_cast(const_cast(text)); voidPtr vStr = reinterpret_cast(const_cast(string)); + rt_copy_memory(vText, vStr, rt_string_len(text)); return string; -- cgit v1.2.3