From 6a30f42d5dcd0f944262147b2806db6c14fe7ffc Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 15 May 2025 13:56:17 +0200 Subject: feat(kernel): Finalizing the first version of the user scheduler. other: - Removed DmaPool into its own Kit. - ApplicationProcessor unit has been cleaned up. - Rename functions of MemoryMgr. - Use KIB instead of MIBs of stack. - Cleanup parts of the scheduler, and hw scheduler. - Use UD handler for INT 6. Signed-off-by: Amlal El Mahrouss --- dev/kernel/DmaKit/DmaPool.h | 91 ++++++++++ dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc | 39 ++-- .../HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc | 37 +--- dev/kernel/HALKit/AMD64/HalDescriptorLoader.cc | 53 ------ dev/kernel/HALKit/AMD64/HalInterruptAPI.asm | 18 +- dev/kernel/HALKit/AMD64/HalKernelMain.cc | 23 ++- .../HALKit/AMD64/Network/Generic+Basic+RTL8139.cc | 4 +- dev/kernel/HALKit/AMD64/Paging.h | 23 ++- dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 2 +- dev/kernel/KernelKit/CodeMgr.h | 2 +- dev/kernel/KernelKit/CoreProcessScheduler.h | 2 +- dev/kernel/KernelKit/FileMgr.h | 4 +- dev/kernel/KernelKit/HardwareThreadScheduler.h | 10 +- dev/kernel/KernelKit/MemoryMgr.h | 14 +- dev/kernel/KernelKit/UserProcessScheduler.h | 7 +- dev/kernel/KernelKit/UserProcessScheduler.inl | 4 +- dev/kernel/NewKit/Array.h | 5 +- dev/kernel/NewKit/Ref.h | 2 +- dev/kernel/StorageKit/DmaPool.h | 92 ---------- dev/kernel/src/BitMapMgr.cc | 2 +- dev/kernel/src/CxxAbi-AMD64.cc | 6 +- dev/kernel/src/DriveMgr.cc | 10 +- dev/kernel/src/FS/HeFS+FileSystemParser.cc | 42 ++--- dev/kernel/src/FS/NeFS+FileSystemParser.cc | 4 +- dev/kernel/src/HardwareThreadScheduler.cc | 12 +- dev/kernel/src/KPC.cc | 2 +- dev/kernel/src/MemoryMgr.cc | 59 +++--- dev/kernel/src/New+Delete.cc | 12 +- dev/kernel/src/PEFCodeMgr.cc | 16 +- dev/kernel/src/UserProcessScheduler.cc | 198 ++++++++------------- 30 files changed, 344 insertions(+), 451 deletions(-) create mode 100644 dev/kernel/DmaKit/DmaPool.h delete mode 100644 dev/kernel/StorageKit/DmaPool.h (limited to 'dev/kernel') diff --git a/dev/kernel/DmaKit/DmaPool.h b/dev/kernel/DmaKit/DmaPool.h new file mode 100644 index 00000000..5acff623 --- /dev/null +++ b/dev/kernel/DmaKit/DmaPool.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2025 Amlal El Mahrouss. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include +#include + +#ifdef __NE_AMD64__ +#define kNeDMAPoolStart (0x1000000) +#define kNeDMAPoolSize (0x1000000) + +namespace Kernel { +/// @brief DMA pool base pointer, here we're sure that AHCI or whatever tricky standard sees it. +inline UInt8* kDmaPoolPtr = (UInt8*) kNeDMAPoolStart; +inline const UInt8* kDmaPoolEnd = (UInt8*) (kNeDMAPoolStart + kNeDMAPoolSize); + +/***********************************************************************************/ +/// @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; + } + + UIntPtr addr = (UIntPtr) kDmaPoolPtr; + + /// 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) >= reinterpret_cast(kDmaPoolEnd)) { + kout << "DMA Pool is exhausted!\r"; + + err_global_get() = kErrorDmaExhausted; + + return nullptr; + } + + kDmaPoolPtr = (UInt8*) (addr + size); + 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 > kDmaPoolEnd) { + return; + } + + if (!ptr || ptr < (UInt8*) kNeDMAPoolStart) { + return; + } + + for (SizeT buf_idx = 0UL; buf_idx < size_buffer; ++buf_idx) { + HAL::mm_memory_fence((VoidPtr) ((UInt8*) ptr + buf_idx)); + } +} +} // namespace Kernel +#endif \ No newline at end of file diff --git a/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc b/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc index dd9a36ed..84c52768 100644 --- a/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc +++ b/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc @@ -152,10 +152,13 @@ EXTERN_C BOOL mp_register_process(HAL::StackFramePtr stack_frame, ProcessID pid) kHWThread[process_index].mCoreID = kAPICLocales[0]; - hal_send_sipi(kApicBaseAddress, kHWThread[process_index].mCoreID, - (UInt8) (((UIntPtr) stack_frame->BP) >> 12)); + if (mp_is_smp()) { + /// TODO: - return YES; + return YES; + } + + return NO; } /***********************************************************************************/ @@ -195,44 +198,36 @@ Void mp_init_cores(VoidPtr vendor_ptr) noexcept { kSMPAware = NO; if (kMADTBlock) { - SizeT index = 1; + SizeT index = 0; kSMPInterrupt = 0; kSMPCount = 0; - kout << "SMP: Starting APs...\r"; - kApicBaseAddress = kMADTBlock->Address; + constexpr const auto kSMPCountMax = kMaxAPInsideSched; + while (Yes) { /// @note Anything bigger than x2APIC type doesn't exist. - if (kMADTBlock->List[index].Type > 9 || kSMPCount > kSchedProcessLimitPerTeam) break; + if (kSMPCount > kSMPCountMax) break; - switch (kMADTBlock->List[index].Type) { - case 0x00: { - if (kMADTBlock->List[kSMPCount].Apic.LAPIC.ProcessorID < 1) break; + if (kMADTBlock->List[index].Type > 9) { + ++index; + continue; + } - kAPICLocales[kSMPCount] = kMADTBlock->List[kSMPCount].Apic.LAPIC.ProcessorID; - (Void)(kout << "SMP: APIC ID: " << number(kAPICLocales[kSMPCount]) << kendl); + kAPICLocales[kSMPCount] = kMADTBlock->List[kSMPCount].Apic.LAPIC.ProcessorID; + (Void)(kout << "SMP: APIC ID: " << number(kAPICLocales[kSMPCount]) << kendl); - ++kSMPCount; - break; - } - default: - break; - } + ++kSMPCount; ++index; } - (Void)(kout << "SMP: Number of APs: " << number(kSMPCount) << kendl); - // Kernel is now SMP aware. // That means that the scheduler is now available (on MP Kernels) kSMPAware = true; - - /// TODO: Notify Boot AP that it must start. } } } // namespace Kernel::HAL diff --git a/dev/kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc b/dev/kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc index 3a380a42..1e513e3f 100644 --- a/dev/kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc +++ b/dev/kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc @@ -16,10 +16,6 @@ STATIC BOOL kIsScheduling = NO; EXTERN_C void idt_handle_gpf(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().CurrentProcess(); - if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning) { - MUST_PASS(NO); - } - kIsScheduling = NO; Kernel::kout << "Kernel: General Protection Fault.\r"; @@ -40,10 +36,6 @@ EXTERN_C void idt_handle_gpf(Kernel::UIntPtr rsp) { EXTERN_C void idt_handle_pf(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().CurrentProcess(); - if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning) { - MUST_PASS(NO); - } - kIsScheduling = NO; Kernel::kout << "Kernel: Page Fault.\r"; @@ -58,27 +50,14 @@ EXTERN_C void idt_handle_pf(Kernel::UIntPtr rsp) { process.Leak().Crash(); } -namespace Kernel::Detail { -constexpr static Int32 kTimeoutCount = 100000UL; -} - /// @brief Handle scheduler interrupt. EXTERN_C void idt_handle_scheduler(Kernel::UIntPtr rsp) { NE_UNUSED(rsp); - static Kernel::Int64 try_count_before_brute = Kernel::Detail::kTimeoutCount; - - while (kIsScheduling) { - --try_count_before_brute; - - if (try_count_before_brute < 1) break; - } - - try_count_before_brute = Kernel::Detail::kTimeoutCount; - kIsScheduling = YES; + Kernel::kout << "Kernel: Scheduler interrupt.\r"; + kIsScheduling = YES; Kernel::UserProcessHelper::StartScheduling(); - kIsScheduling = NO; } @@ -87,10 +66,6 @@ EXTERN_C void idt_handle_scheduler(Kernel::UIntPtr rsp) { EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().CurrentProcess(); - if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning) { - MUST_PASS(NO); - } - kIsScheduling = NO; Kernel::kout << "Kernel: Math error (division by zero?).\r"; @@ -111,10 +86,6 @@ EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp) { EXTERN_C void idt_handle_generic(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().CurrentProcess(); - if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning) { - MUST_PASS(NO); - } - kIsScheduling = NO; Kernel::kout << "Kernel: Generic Process Fault.\r"; @@ -163,10 +134,6 @@ EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip) { EXTERN_C void idt_handle_ud(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().CurrentProcess(); - if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning) { - MUST_PASS(NO); - } - kIsScheduling = NO; Kernel::kout << "Kernel: Undefined Opcode.\r"; diff --git a/dev/kernel/HALKit/AMD64/HalDescriptorLoader.cc b/dev/kernel/HALKit/AMD64/HalDescriptorLoader.cc index 2fb6ad4c..b8eb8fe9 100644 --- a/dev/kernel/HALKit/AMD64/HalDescriptorLoader.cc +++ b/dev/kernel/HALKit/AMD64/HalDescriptorLoader.cc @@ -8,56 +8,10 @@ #include #include -#define kPITDefaultTicks (1000U) - namespace Kernel::HAL { namespace Detail { STATIC ::Kernel::Detail::AMD64::InterruptDescriptorAMD64 kInterruptVectorTable[kKernelIdtSize] = {}; - - STATIC ATTRIBUTE(unused) void hal_set_irq_mask(UInt8 irql) [[maybe_unused]] { - UInt16 port; - UInt8 value; - - if (irql < 8) { - port = kPICData; - } else { - port = kPIC2Data; - irql -= 8; - } - - value = rt_in8(port) | (1 << irql); - rt_out8(port, value); - } - - STATIC void hal_clear_irq_mask(UInt8 irql) [[maybe_unused]] { - UInt16 port; - UInt8 value; - - if (irql < 8) { - port = kPICData; - } else { - port = kPIC2Data; - irql -= 8; - } - - value = rt_in8(port) & ~(1 << irql); - rt_out8(port, value); - } - - STATIC Void hal_enable_pit(UInt16 ticks) noexcept { - if (ticks == 0) ticks = kPITDefaultTicks; - - // Configure PIT to receieve scheduler interrupts. - - UInt16 kPITCommDivisor = kPITFrequency / ticks; // 100 Hz. - - HAL::rt_out8(kPITControlPort, 0x36); // Command to PIT - HAL::rt_out8(kPITChannel0Port, kPITCommDivisor & 0xFF); // Send low byte - HAL::rt_out8(kPITChannel0Port, (kPITCommDivisor >> 8) & 0xFF); // Send high byte - - hal_clear_irq_mask(32); - } } // namespace Detail /// @brief Loads the provided Global Descriptor Table. @@ -72,8 +26,6 @@ Void GDTLoader::Load(Register64& gdt) { Void IDTLoader::Load(Register64& idt) { rt_cli(); - const Int16 kPITTickForScheduler = kPITDefaultTicks; - volatile ::Kernel::UIntPtr** ptr_ivt = (volatile ::Kernel::UIntPtr**) idt.Base; for (SizeT idt_indx = 0; idt_indx < kKernelIdtSize; ++idt_indx) { @@ -92,12 +44,7 @@ Void IDTLoader::Load(Register64& idt) { idt.Base = (UIntPtr) &Detail::kInterruptVectorTable[0]; idt.Limit = sizeof(::Kernel::Detail::AMD64::InterruptDescriptorAMD64) * (kKernelIdtSize); - Detail::hal_enable_pit(kPITTickForScheduler); - -#ifndef __NE_MODULAR_KERNEL_COMPONENTS__ hal_load_idt(idt); -#endif // __NE_MODULAR_KERNEL_COMPONENTS__ - rt_sti(); } diff --git a/dev/kernel/HALKit/AMD64/HalInterruptAPI.asm b/dev/kernel/HALKit/AMD64/HalInterruptAPI.asm index a6c6bbb5..0634b0ea 100644 --- a/dev/kernel/HALKit/AMD64/HalInterruptAPI.asm +++ b/dev/kernel/HALKit/AMD64/HalInterruptAPI.asm @@ -138,7 +138,7 @@ __NE_INT_6: out 0x20, al push rcx - call idt_handle_generic + call idt_handle_ud pop rcx std @@ -235,17 +235,14 @@ IntNormal 31 __NE_INT_32: cld - - mov al, 0x20 - out 0xA0, al - out 0x20, al push rax mov rcx, rsp call idt_handle_scheduler pop rax - std + mov al, 0x20 + out 0x20, al o64 iret @@ -416,3 +413,12 @@ kInterruptVectorTable: dq __NE_INT_%+i %assign i i+1 %endrep + +section .text + +global sched_jump_to_task + +;; Jump to the task from its stack frame. +sched_jump_to_task: + mov rsp, rcx + ret \ No newline at end of file diff --git a/dev/kernel/HALKit/AMD64/HalKernelMain.cc b/dev/kernel/HALKit/AMD64/HalKernelMain.cc index 2d213a9b..b0ac076d 100644 --- a/dev/kernel/HALKit/AMD64/HalKernelMain.cc +++ b/dev/kernel/HALKit/AMD64/HalKernelMain.cc @@ -27,6 +27,8 @@ EXTERN_C Int32 hal_init_platform(Kernel::HEL::BootInfoHeader* handover_hdr) { return kEfiFail; } + Kernel::HAL::rt_sti(); + kHandoverHeader = handover_hdr; FB::fb_clear_video(); @@ -54,7 +56,7 @@ EXTERN_C Int32 hal_init_platform(Kernel::HEL::BootInfoHeader* handover_hdr) { STATIC CONST auto kGDTEntriesCount = 6; - /* GDT, mostly descriptors for user and kernel segments. */ + /* The GDT, mostly descriptors for user and kernel segments. */ STATIC Kernel::HAL::Detail::NE_GDT_ENTRY ALIGN(0x08) kGDTArray[kGDTEntriesCount] = { {.fLimitLow = 0, .fBaseLow = 0, @@ -101,6 +103,13 @@ EXTERN_C Int32 hal_init_platform(Kernel::HEL::BootInfoHeader* handover_hdr) { return kEfiFail; } +EXTERN_C Kernel::Void rtl_ne_task(Kernel::Void) { + kout << "Hello, world!\r"; + dbg_break_point(); +} + +EXTERN_C void idt_handle_scheduler(Kernel::UIntPtr rsp); + EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept { #ifdef __FSKIT_INCLUDES_HEFS__ if (!Kernel::HeFS::fs_init_hefs()) { @@ -108,18 +117,24 @@ EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept { Kernel::NeFS::fs_init_nefs(); } #elif defined(__FSKIT_INCLUDES_NEFS__) - Kernel::NeFS::fs_init_nefs(); + if (!Kernel::NeFS::fs_init_nefs()) { + kout << "NeFS cannot be formated on the disk. Aborting\r"; + dbg_break_point(); + } #endif + Kernel::rtl_create_user_process(rtl_ne_task, "NeTask"); + Kernel::HAL::mp_init_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr); Kernel::HAL::Register64 idt_reg; - idt_reg.Base = (Kernel::UIntPtr) kInterruptVectorTable; + idt_reg.Base = reinterpret_cast(kInterruptVectorTable); Kernel::HAL::IDTLoader idt_loader; idt_loader.Load(idt_reg); - dbg_break_point(); + while (YES) + ; } #endif // ifndef __NE_MODULAR_KERNEL_COMPONENTS__ diff --git a/dev/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cc b/dev/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cc index 06c18d8b..57f64712 100644 --- a/dev/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cc +++ b/dev/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cc @@ -4,8 +4,8 @@ Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ +#include #include -#include #include using namespace Kernel; @@ -14,7 +14,7 @@ using namespace Kernel::HAL; STATIC UInt16 kIOBase = 0xFFFF; STATIC UInt32 kRXOffset = 0UL; -STATIC constexpr const UInt32 kRxBufferSize = 8192 + 16 + 1500; +STATIC constexpr CONST UInt32 kRxBufferSize = 8192 + 16 + 1500; STATIC BOOL kTXEnabled = NO; diff --git a/dev/kernel/HALKit/AMD64/Paging.h b/dev/kernel/HALKit/AMD64/Paging.h index cfba232c..061bae45 100644 --- a/dev/kernel/HALKit/AMD64/Paging.h +++ b/dev/kernel/HALKit/AMD64/Paging.h @@ -63,6 +63,25 @@ auto mm_free_bitmap(VoidPtr page_ptr) -> Bool; } // namespace Kernel::HAL namespace Kernel { -typedef VoidPtr PTE; -typedef VoidPtr PDE; +struct PTE { + UInt64 Present : 1; + UInt64 Wr : 1; + UInt64 User : 1; + UInt64 Pwt : 1; // Page-level Write-Through + UInt64 Pcd : 1; // Page-level Cache Disable + UInt64 Accessed : 1; + UInt64 Dirty : 1; + UInt64 Pat : 1; // Page Attribute Table (or PS for PDE) + UInt64 Global : 1; + UInt64 Ignored1 : 3; // Available to software + UInt64 PhysicalAddress : 40; // Physical page frame address (bits 12–51) + UInt64 Ignored2 : 7; // More software bits / reserved + UInt64 ProtectionKey : 4; // Optional (if PKU enabled) + UInt64 Reserved : 1; // Usually reserved + UInt64 Nx : 1; // No Execute +}; + +struct PDE { + ATTRIBUTE(aligned(kib_cast(4))) PTE fPTE[512]; +}; } // namespace Kernel diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index 21483560..57e77e77 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -15,6 +15,7 @@ * */ +#include #include #include #include @@ -25,7 +26,6 @@ #include #include #include -#include #include #include diff --git a/dev/kernel/KernelKit/CodeMgr.h b/dev/kernel/KernelKit/CodeMgr.h index ff70707b..f4d46b4b 100644 --- a/dev/kernel/KernelKit/CodeMgr.h +++ b/dev/kernel/KernelKit/CodeMgr.h @@ -20,7 +20,7 @@ namespace Kernel { /// @brief Main process entrypoint. -typedef void (*rtl_main_kind)(SizeT argc, Char** argv, Char** envp, SizeT envp_len); +typedef void (*rtl_main_kind)(void); /// @brief C++ Constructor entrypoint. typedef void (*rtl_ctor_kind)(void); diff --git a/dev/kernel/KernelKit/CoreProcessScheduler.h b/dev/kernel/KernelKit/CoreProcessScheduler.h index 3f55c099..f9d11459 100644 --- a/dev/kernel/KernelKit/CoreProcessScheduler.h +++ b/dev/kernel/KernelKit/CoreProcessScheduler.h @@ -15,7 +15,7 @@ #define kSchedTeamCount (256U) #define kSchedMaxMemoryLimit gib_cast(128) /* max physical memory limit */ -#define kSchedMaxStackSz mib_cast(8) /* maximum stack size */ +#define kSchedMaxStackSz (kib_cast(8)) /* maximum stack size */ #define kSchedNameLen (128U) diff --git a/dev/kernel/KernelKit/FileMgr.h b/dev/kernel/KernelKit/FileMgr.h index 6751b0a8..32c30b70 100644 --- a/dev/kernel/KernelKit/FileMgr.h +++ b/dev/kernel/KernelKit/FileMgr.h @@ -385,13 +385,13 @@ inline FileStream::FileStream(const Encoding* path, const Encod } } - kout << "new file: " << path << ".\r"; + kout << "FileMgr: New file at: " << path << ".\r"; } /// @brief destructor of the file stream. template inline FileStream::~FileStream() { - mm_delete_heap(fFile); + mm_delete_ptr(fFile); } } // namespace Kernel diff --git a/dev/kernel/KernelKit/HardwareThreadScheduler.h b/dev/kernel/KernelKit/HardwareThreadScheduler.h index d47b2994..dd8271eb 100644 --- a/dev/kernel/KernelKit/HardwareThreadScheduler.h +++ b/dev/kernel/KernelKit/HardwareThreadScheduler.h @@ -14,7 +14,7 @@ /// @note Last Rev Sun 28 Jul CET 2024 /// @note Last Rev Thu, Aug 1, 2024 9:07:38 AM -#define kMaxAPInsideSched (8U) +#define kMaxAPInsideSched (4U) namespace Kernel { class HardwareThread; @@ -58,14 +58,14 @@ class HardwareThread final { void Busy(const BOOL busy = false) noexcept; public: - BOOL Switch(VoidPtr image, Ptr8 stack_ptr, HAL::StackFramePtr frame, const ThreadID& pid); + BOOL Switch(HAL::StackFramePtr frame, const ThreadID& pid); BOOL IsWakeup() noexcept; public: HAL::StackFramePtr StackFrame() noexcept; - const ThreadKind& Kind() noexcept; - bool IsBusy() noexcept; - const ThreadID& ID() noexcept; + ThreadKind& Kind() noexcept; + BOOL IsBusy() noexcept; + ThreadID& ID() noexcept; private: HAL::StackFramePtr fStack{nullptr}; diff --git a/dev/kernel/KernelKit/MemoryMgr.h b/dev/kernel/KernelKit/MemoryMgr.h index 2274e24e..d84dc9a4 100644 --- a/dev/kernel/KernelKit/MemoryMgr.h +++ b/dev/kernel/KernelKit/MemoryMgr.h @@ -19,29 +19,29 @@ namespace Kernel { /// @brief Declare pointer as free. /// @param heap_ptr the pointer. /// @return a status code regarding the deallocation. -Int32 mm_delete_heap(VoidPtr heap_ptr); +Int32 mm_delete_ptr(VoidPtr heap_ptr); /// @brief Declare a new size for heap_ptr. /// @param heap_ptr the pointer. /// @return unsupported always returns nullptr. -VoidPtr mm_realloc_heap(VoidPtr heap_ptr, SizeT new_sz); +VoidPtr mm_realloc_ptr(VoidPtr heap_ptr, SizeT new_sz); /// @brief Check if pointer is a valid Kernel pointer. /// @param heap_ptr the pointer /// @return if it exists it returns true. -Boolean mm_is_valid_heap(VoidPtr heap_ptr); +Boolean mm_is_valid_ptr(VoidPtr heap_ptr); /// @brief Allocate chunk of memory. /// @param sz Size of pointer /// @param wr Read Write bit. /// @param user User enable bit. /// @return The newly allocated pointer, or nullptr. -VoidPtr mm_new_heap(SizeT sz, Bool wr, Bool user, SizeT pad_amount = 0); +VoidPtr mm_new_ptr(SizeT sz, Bool wr, Bool user, SizeT pad_amount = 0); /// @brief Protect the heap with a CRC value. /// @param heap_ptr pointer. /// @return if it valid: point has crc now., otherwise fail. -Boolean mm_protect_heap(VoidPtr heap_ptr); +Boolean mm_protect_ptr(VoidPtr heap_ptr); /// @brief Makes a Kernel page. /// @param heap_ptr the page pointer. @@ -51,11 +51,11 @@ Int32 mm_make_page(VoidPtr heap_ptr); /// @brief Overwrites and set the flags of a heap header. /// @param heap_ptr the pointer to update. /// @param flags the flags to set. -Int32 mm_make_flags(VoidPtr heap_ptr, UInt64 flags); +Int32 mm_make_ptr_flags(VoidPtr heap_ptr, UInt64 flags); /// @brief Gets the flags of a heap header. /// @param heap_ptr the pointer to get. -UInt64 mm_get_flags(VoidPtr heap_ptr); +UInt64 mm_get_ptr_flags(VoidPtr heap_ptr); /// @brief Allocate C++ class. /// @param cls The class to allocate. diff --git a/dev/kernel/KernelKit/UserProcessScheduler.h b/dev/kernel/KernelKit/UserProcessScheduler.h index a638f0dc..717788e9 100644 --- a/dev/kernel/KernelKit/UserProcessScheduler.h +++ b/dev/kernel/KernelKit/UserProcessScheduler.h @@ -47,7 +47,7 @@ class USER_PROCESS final { HAL::StackFramePtr StackFrame{nullptr}; AffinityKind Affinity{AffinityKind::kStandard}; ProcessStatusKind Status{ProcessStatusKind::kKilled}; - UInt8* StackReserve{nullptr}; + UInt8 StackReserve[kSchedMaxStackSz]; PROCESS_IMAGE Image{}; SizeT StackSize{kSchedMaxStackSz}; IDylibObject* DylibDelegate{nullptr}; @@ -92,6 +92,8 @@ class USER_PROCESS final { /***********************************************************************************/ Void Crash(); + Bool SpawnDylib(); + /***********************************************************************************/ ///! @brief Exits the app. /***********************************************************************************/ @@ -219,8 +221,7 @@ class UserProcessScheduler final : public ISchedulable { class UserProcessHelper final { public: - STATIC Bool Switch(VoidPtr image_ptr, UInt8* stack_ptr, HAL::StackFramePtr frame_ptr, - PID new_pid); + STATIC Bool Switch(HAL::StackFramePtr frame_ptr, PID new_pid); STATIC Bool CanBeScheduled(const USER_PROCESS& process); STATIC ErrorOr TheCurrentPID(); STATIC SizeT StartScheduling(); diff --git a/dev/kernel/KernelKit/UserProcessScheduler.inl b/dev/kernel/KernelKit/UserProcessScheduler.inl index 7bf98d78..236262e1 100644 --- a/dev/kernel/KernelKit/UserProcessScheduler.inl +++ b/dev/kernel/KernelKit/UserProcessScheduler.inl @@ -36,13 +36,13 @@ Boolean USER_PROCESS::Delete(ErrorOr ptr) { hal_write_cr3(this->VMRegister); - auto ret = mm_delete_heap(entry->Entry); + auto ret = mm_delete_ptr(entry->Entry); hal_write_cr3(pd); return ret == kErrorSuccess; #else - Bool ret = mm_delete_heap(ptr.Leak().Leak()); + Bool ret = mm_delete_ptr(ptr.Leak().Leak()); return ret == kErrorSuccess; #endif diff --git a/dev/kernel/NewKit/Array.h b/dev/kernel/NewKit/Array.h index af73d002..767d8678 100644 --- a/dev/kernel/NewKit/Array.h +++ b/dev/kernel/NewKit/Array.h @@ -20,10 +20,7 @@ class Array final { Array& operator=(const Array&) = default; Array(const Array&) = default; - T& operator[](SizeT at) { - MUST_PASS(at < this->Count()); - return fArray[at]; - } + T& operator[](SizeT at) { return fArray[at]; } Boolean Empty() { return this->Count() > 0; } diff --git a/dev/kernel/NewKit/Ref.h b/dev/kernel/NewKit/Ref.h index 6737ce09..31ad16f8 100644 --- a/dev/kernel/NewKit/Ref.h +++ b/dev/kernel/NewKit/Ref.h @@ -19,7 +19,7 @@ class Ref final { Ref() = default; ~Ref() { - if (mm_is_valid_heap(fClass)) delete fClass; + if (mm_is_valid_ptr(fClass)) delete fClass; } public: diff --git a/dev/kernel/StorageKit/DmaPool.h b/dev/kernel/StorageKit/DmaPool.h deleted file mode 100644 index a5d8c880..00000000 --- a/dev/kernel/StorageKit/DmaPool.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2025 Amlal El Mahrouss. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#pragma once - -#include - -#ifdef __NE_AMD64__ -#define NE_DMA_POOL_START (0x1000000) -#define NE_DMA_POOL_SIZE (0x1000000) - -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 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; - } - - UIntPtr addr = (UIntPtr) kDmaPoolPtr; - - /// 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) >= reinterpret_cast(kDmaPoolEnd)) { - kout << "DMA Pool is exhausted!\r"; - - err_global_get() = kErrorDmaExhausted; - - return nullptr; - } - - kDmaPoolPtr = (UInt8*) (addr + size); - 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; - } - - if (!ptr) { - return; - } - - for (SizeT i = 0; i < size_buffer; ++i) { - asm volatile("clflush (%0)" : : "r"((UInt8*) ptr + i) : "memory"); - } - - asm volatile("mfence" ::: "memory"); -} -} // namespace Kernel -#endif \ No newline at end of file diff --git a/dev/kernel/src/BitMapMgr.cc b/dev/kernel/src/BitMapMgr.cc index 7cbcf376..dfd29770 100644 --- a/dev/kernel/src/BitMapMgr.cc +++ b/dev/kernel/src/BitMapMgr.cc @@ -169,7 +169,7 @@ namespace HAL { VoidPtr ptr_new = nullptr; Detail::IBitMapProxy bitmp; - NE_UNUSED(is_page); + if (is_page) return nullptr; ptr_new = bitmp.FindBitMap(kKernelBitMpStart, size, wr, user, pad); return (UIntPtr*) ptr_new; diff --git a/dev/kernel/src/CxxAbi-AMD64.cc b/dev/kernel/src/CxxAbi-AMD64.cc index 452f23cd..c97b99f9 100644 --- a/dev/kernel/src/CxxAbi-AMD64.cc +++ b/dev/kernel/src/CxxAbi-AMD64.cc @@ -8,6 +8,7 @@ #include #include +#include #include atexit_func_entry_t __atexit_funcs[kAtExitMacDestructors]; @@ -23,10 +24,7 @@ EXTERN_C Kernel::Void __cxa_pure_virtual(void* self) { (Kernel::Void)(Kernel::kout << ", has unimplemented virtual functions.\r"); } -EXTERN_C void ___chkstk_ms(void) { - (Kernel::Void)(Kernel::kout << "Stack smashing detected!\r"); - dbg_break_point(); -} +EXTERN_C void ___chkstk_ms(void) {} EXTERN_C int atexit(void (*f)()) { if (__atexit_func_count >= kAtExitMacDestructors) return 1; diff --git a/dev/kernel/src/DriveMgr.cc b/dev/kernel/src/DriveMgr.cc index 449640f9..8bb1c930 100644 --- a/dev/kernel/src/DriveMgr.cc +++ b/dev/kernel/src/DriveMgr.cc @@ -146,7 +146,7 @@ DriveTrait io_construct_blank_drive() noexcept { trait.fInit = io_drv_unimplemented; trait.fProtocol = io_drv_kind; - kout << "Construct: " << trait.fName << "\r"; + kout << "DriveMgr: Construct: " << trait.fName << "\r"; return trait; } @@ -170,7 +170,7 @@ namespace Detail { trait.fPacket.fPacketReadOnly = NO; trait.fKind = kMassStorageDrive | kEPMDrive; - kout << "Disk is EPM formatted.\r"; + kout << "DriveMgr: Disk is EPM formatted.\r"; trait.fSectorSz = block_struct.SectorSz; trait.fLbaEnd = block_struct.LbaEnd; @@ -191,13 +191,13 @@ namespace Detail { trait.fPacket.fPacketReadOnly = NO; trait.fKind = kMassStorageDrive | kGPTDrive; - kout << "Disk is GPT formatted.\r"; + kout << "DriveMgr: Disk is GPT formatted.\r"; trait.fSectorSz = gpt_struct.SizeOfEntries; trait.fLbaEnd = gpt_struct.LastGPTEntry; trait.fLbaStart = gpt_struct.FirstGPTEntry; } else { - kout << "Disk is unformatted.\r"; + kout << "DriveMgr: Disk is unformatted.\r"; trait.fPacket.fPacketReadOnly = YES; trait.fKind = kMassStorageDrive | kUnformattedDrive | kReadOnlyDrive; @@ -233,7 +233,7 @@ DriveTrait io_construct_main_drive() noexcept { trait.fInit = io_drv_init; trait.fProtocol = io_drv_kind; - kout << "Detecting partition scheme of: " << trait.fName << ".\r"; + kout << "DriveMgr: Detecting partition scheme of: " << trait.fName << ".\r"; Detail::io_detect_drive(trait); diff --git a/dev/kernel/src/FS/HeFS+FileSystemParser.cc b/dev/kernel/src/FS/HeFS+FileSystemParser.cc index d4aa1ee3..519707d7 100644 --- a/dev/kernel/src/FS/HeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/HeFS+FileSystemParser.cc @@ -262,7 +262,7 @@ namespace Detail { const BOOL delete_or_create) { if (mnt) { HEFS_INDEX_NODE_DIRECTORY* tmpdir = - (HEFS_INDEX_NODE_DIRECTORY*) mm_new_heap(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); + (HEFS_INDEX_NODE_DIRECTORY*) mm_new_ptr(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); auto start = root->fStartIND; auto prev_location = start; @@ -294,7 +294,7 @@ namespace Detail { if (expr) { HEFS_INDEX_NODE_DIRECTORY* dirent = - (HEFS_INDEX_NODE_DIRECTORY*) mm_new_heap(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); + (HEFS_INDEX_NODE_DIRECTORY*) mm_new_ptr(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); rt_set_memory(dirent, 0, sizeof(HEFS_INDEX_NODE_DIRECTORY)); @@ -412,8 +412,8 @@ namespace Detail { err_global_get() = kErrorSuccess; - mm_delete_heap(dirent); - mm_delete_heap(tmpdir); + mm_delete_ptr(dirent); + mm_delete_ptr(tmpdir); if (!delete_or_create) ++root->fINDCount; @@ -438,7 +438,7 @@ namespace Detail { } err_global_get() = kErrorDisk; - mm_delete_heap(tmpdir); + mm_delete_ptr(tmpdir); return NO; } @@ -467,7 +467,7 @@ namespace Detail { HEFS_INDEX_NODE* node = new HEFS_INDEX_NODE(); HEFS_INDEX_NODE_DIRECTORY* dir = - (HEFS_INDEX_NODE_DIRECTORY*) mm_new_heap(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); + (HEFS_INDEX_NODE_DIRECTORY*) mm_new_ptr(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); while (YES) { if (err_global_get() == kErrorDiskIsCorrupted) { @@ -543,7 +543,7 @@ namespace Detail { if (mnt) { HEFS_INDEX_NODE_DIRECTORY* dir = - (HEFS_INDEX_NODE_DIRECTORY*) mm_new_heap(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); + (HEFS_INDEX_NODE_DIRECTORY*) mm_new_ptr(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); auto hash_file = node->fHashPath; @@ -596,7 +596,7 @@ namespace Detail { mnt->fOutput(mnt->fPacket); - mm_delete_heap(dir); + mm_delete_ptr(dir); return YES; } else if (dir->fINSlices[inode_index] != 0 && delete_or_create) { @@ -646,7 +646,7 @@ namespace Detail { mnt->fOutput(mnt->fPacket); - mm_delete_heap(dir); + mm_delete_ptr(dir); return YES; } @@ -657,7 +657,7 @@ namespace Detail { if (start > root->fEndIND || start == 0) break; } - mm_delete_heap(dir); + mm_delete_ptr(dir); err_global_get() = kErrorFileNotFound; return NO; } @@ -902,7 +902,7 @@ _Output Bool HeFileSystemParser::INodeDirectoryCtlManip(_Input DriveTrait* mnt, return NO; } - HEFS_BOOT_NODE* root = (HEFS_BOOT_NODE*) mm_new_heap(sizeof(HEFS_BOOT_NODE), Yes, No); + HEFS_BOOT_NODE* root = (HEFS_BOOT_NODE*) mm_new_ptr(sizeof(HEFS_BOOT_NODE), Yes, No); rt_copy_memory((VoidPtr) "fs/hefs-packet", mnt->fPacket.fPacketMime, rt_string_len("fs/hefs-packet")); @@ -938,11 +938,11 @@ _Output Bool HeFileSystemParser::INodeDirectoryCtlManip(_Input DriveTrait* mnt, // todo: make it smarter for high-throughput. Detail::hefsi_balance_ind(root, mnt); - mm_delete_heap((VoidPtr) root); + mm_delete_ptr((VoidPtr) root); return YES; } - mm_delete_heap((VoidPtr) root); + mm_delete_ptr((VoidPtr) root); return NO; } @@ -983,7 +983,7 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc return NO; } - HEFS_BOOT_NODE* root = (HEFS_BOOT_NODE*) mm_new_heap(sizeof(HEFS_BOOT_NODE), Yes, No); + HEFS_BOOT_NODE* root = (HEFS_BOOT_NODE*) mm_new_ptr(sizeof(HEFS_BOOT_NODE), Yes, No); if (!root) { err_global_get() = kErrorInvalidData; @@ -1001,7 +1001,7 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc if (!KStringBuilder::Equals(root->fMagic, kHeFSMagic) || root->fVersion != kHeFSVersion) { (Void)(kout << "Invalid Boot Node, HeFS partition is invalid." << kendl); - mm_delete_heap((VoidPtr) root); + mm_delete_ptr((VoidPtr) root); err_global_get() = kErrorDisk; return NO; } @@ -1021,7 +1021,7 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc mnt->fInput(mnt->fPacket); } else { if (start->fFlags & kHeFSFlagsReadOnly) { - mm_delete_heap((VoidPtr) root); + mm_delete_ptr((VoidPtr) root); delete start; kout << "Error: File is read-only\r"; @@ -1034,7 +1034,7 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc } } - mm_delete_heap((VoidPtr) root); + mm_delete_ptr((VoidPtr) root); delete start; return YES; } @@ -1058,7 +1058,7 @@ _Output Bool HeFileSystemParser::INodeCtlManip(_Input DriveTrait* mnt, _Input co return NO; } - HEFS_INDEX_NODE* node = (HEFS_INDEX_NODE*) mm_new_heap(sizeof(HEFS_INDEX_NODE), Yes, No); + HEFS_INDEX_NODE* node = (HEFS_INDEX_NODE*) mm_new_ptr(sizeof(HEFS_INDEX_NODE), Yes, No); if (!node) { err_global_get() = kErrorInvalidData; @@ -1070,7 +1070,7 @@ _Output Bool HeFileSystemParser::INodeCtlManip(_Input DriveTrait* mnt, _Input co HEFS_BOOT_NODE* root = (HEFS_BOOT_NODE*) RTL_ALLOCA(sizeof(HEFS_BOOT_NODE)); if (!root) { - mm_delete_heap((VoidPtr) node); + mm_delete_ptr((VoidPtr) node); err_global_get() = kErrorInvalidData; return NO; @@ -1124,7 +1124,7 @@ _Output Bool HeFileSystemParser::INodeCtlManip(_Input DriveTrait* mnt, _Input co node->fHashPath = Detail::hefsi_hash_64(name); if (Detail::hefsi_update_in_status(root, mnt, dir, node, delete_or_create)) { - mm_delete_heap((VoidPtr) node); + mm_delete_ptr((VoidPtr) node); Detail::hefsi_balance_ind(root, mnt); @@ -1132,7 +1132,7 @@ _Output Bool HeFileSystemParser::INodeCtlManip(_Input DriveTrait* mnt, _Input co return YES; } - mm_delete_heap((VoidPtr) node); + mm_delete_ptr((VoidPtr) node); err_global_get() = kErrorDirectoryNotFound; return NO; diff --git a/dev/kernel/src/FS/NeFS+FileSystemParser.cc b/dev/kernel/src/FS/NeFS+FileSystemParser.cc index 49673b59..dae69a21 100644 --- a/dev/kernel/src/FS/NeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/NeFS+FileSystemParser.cc @@ -240,7 +240,7 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char return nullptr; } - Char* parent_name = (Char*) mm_new_heap(sizeof(Char) * rt_string_len(name), Yes, No); + Char* parent_name = (Char*) mm_new_ptr(sizeof(Char) * rt_string_len(name), Yes, No); /// Locate parent catalog, to then allocate right after it. @@ -269,7 +269,7 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char NEFS_CATALOG_STRUCT* catalog = this->FindCatalog(parent_name, out_lba); - mm_delete_heap(parent_name); + mm_delete_ptr(parent_name); auto& drive = kMountpoint.A(); diff --git a/dev/kernel/src/HardwareThreadScheduler.cc b/dev/kernel/src/HardwareThreadScheduler.cc index c49c3081..1df5ad7a 100644 --- a/dev/kernel/src/HardwareThreadScheduler.cc +++ b/dev/kernel/src/HardwareThreadScheduler.cc @@ -41,14 +41,14 @@ HardwareThread::~HardwareThread() = default; /***********************************************************************************/ //! @brief returns the id of the thread. /***********************************************************************************/ -const ThreadID& HardwareThread::ID() noexcept { +ThreadID& HardwareThread::ID() noexcept { return fID; } /***********************************************************************************/ //! @brief returns the kind of thread we have. /***********************************************************************************/ -const ThreadKind& HardwareThread::Kind() noexcept { +ThreadKind& HardwareThread::Kind() noexcept { return fKind; } @@ -104,16 +104,10 @@ Void HardwareThread::Wake(const bool wakeup) noexcept { /// @retval true stack was changed, code is running. /// @retval false stack is invalid, previous code is running. /***********************************************************************************/ -Bool HardwareThread::Switch(VoidPtr image_ptr, Ptr8 stack_ptr, HAL::StackFramePtr frame, - const ThreadID& pid) { - if (this->IsBusy()) return NO; - +Bool HardwareThread::Switch(HAL::StackFramePtr frame, const ThreadID& pid) { this->fStack = frame; this->fPID = pid; - this->fStack->BP = reinterpret_cast(image_ptr); - this->fStack->SP = reinterpret_cast(stack_ptr); - Bool ret = mp_register_process(fStack, this->fPID); if (ret) this->Busy(YES); diff --git a/dev/kernel/src/KPC.cc b/dev/kernel/src/KPC.cc index 8937d19a..9a76c110 100644 --- a/dev/kernel/src/KPC.cc +++ b/dev/kernel/src/KPC.cc @@ -19,7 +19,7 @@ Boolean err_bug_check_raise(Void) noexcept { if (ptr == nullptr) goto bug_check_fail; - if (!mm_is_valid_heap(ptr)) goto bug_check_fail; + if (!mm_is_valid_ptr(ptr)) goto bug_check_fail; delete[] ptr; diff --git a/dev/kernel/src/MemoryMgr.cc b/dev/kernel/src/MemoryMgr.cc index 9b7bea43..f8aa14cf 100644 --- a/dev/kernel/src/MemoryMgr.cc +++ b/dev/kernel/src/MemoryMgr.cc @@ -74,14 +74,14 @@ namespace Detail { /// @brief Check for heap address validity. /// @param heap_ptr The address_ptr to check. /// @return Bool if the pointer is valid or not. - _Output auto mm_check_heap_address(VoidPtr heap_ptr) -> Bool { + _Output auto mm_check_ptr_address(VoidPtr heap_ptr) -> Bool { if (!heap_ptr) return false; - IntPtr base_heap = ((IntPtr) heap_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK); + IntPtr base_ptr = ((IntPtr) heap_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK); /// Add that check in case we're having an integer underflow. /// - if (base_heap < 0) { + if (base_ptr < 0) { return false; } @@ -91,15 +91,17 @@ namespace Detail { typedef MM_INFORMATION_BLOCK* MM_INFORMATION_BLOCK_PTR; } // namespace Detail -/// @brief Declare a new size for ptr_heap. -/// @param ptr_heap the pointer. +STATIC PageMgr kPageMgr; + +/// @brief Declare a new size for ptr_ptr. +/// @param ptr_ptr the pointer. /// @return Newly allocated heap header. -_Output auto mm_realloc_heap(VoidPtr ptr_heap, SizeT new_sz) -> VoidPtr { - if (Detail::mm_check_heap_address(ptr_heap) == No) return nullptr; +_Output auto mm_realloc_ptr(VoidPtr ptr_ptr, SizeT new_sz) -> VoidPtr { + if (Detail::mm_check_ptr_address(ptr_ptr) == No) return nullptr; - if (!ptr_heap || new_sz < 1) return nullptr; + if (!ptr_ptr || new_sz < 1) return nullptr; - kout << "This function is not implemented by the kernel yet.\r"; + kout << "MemoryMgr: This function is not implemented by the kernel yet.\r"; ke_panic(RUNTIME_CHECK_INVALID); @@ -111,15 +113,14 @@ _Output auto mm_realloc_heap(VoidPtr ptr_heap, SizeT new_sz) -> VoidPtr { /// @param wr Read Write bit. /// @param user User enable bit. /// @return The newly allocated pointer. -_Output VoidPtr mm_new_heap(SizeT sz, Bool wr, Bool user, SizeT pad_amount) { +_Output VoidPtr mm_new_ptr(SizeT sz, Bool wr, Bool user, SizeT pad_amount) { auto sz_fix = sz; if (sz_fix == 0) return nullptr; sz_fix += sizeof(Detail::MM_INFORMATION_BLOCK); - PageMgr page_mgr; - auto wrapper = page_mgr.Request(wr, user, No, sz_fix, pad_amount); + auto wrapper = kPageMgr.Request(wr, user, No, sz_fix, pad_amount); Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast(wrapper.VirtualAddress() + @@ -127,7 +128,7 @@ _Output VoidPtr mm_new_heap(SizeT sz, Bool wr, Bool user, SizeT pad_amount) { heap_info_ptr->fSize = sz_fix; heap_info_ptr->fMagic = kMemoryMgrMagic; - heap_info_ptr->fCRC32 = 0; // dont fill it for now. + heap_info_ptr->fCRC32 = 0U; // dont fill it for now. heap_info_ptr->fOffset = reinterpret_cast(heap_info_ptr) + sizeof(Detail::MM_INFORMATION_BLOCK); heap_info_ptr->fPage = No; @@ -140,8 +141,8 @@ _Output VoidPtr mm_new_heap(SizeT sz, Bool wr, Bool user, SizeT pad_amount) { auto result = reinterpret_cast(heap_info_ptr->fOffset); - (Void)(kout << "Registered heap address: " << hex_number(reinterpret_cast(heap_info_ptr)) - << kendl); + (Void)(kout << "MemoryMgr: Registered heap address: " + << hex_number(reinterpret_cast(heap_info_ptr)) << kendl); return result; } @@ -150,7 +151,7 @@ _Output VoidPtr mm_new_heap(SizeT sz, Bool wr, Bool user, SizeT pad_amount) { /// @param heap_ptr the pointer to make a page heap. /// @return kErrorSuccess if successful, otherwise an error code. _Output Int32 mm_make_page(VoidPtr heap_ptr) { - if (Detail::mm_check_heap_address(heap_ptr) == No) return kErrorHeapNotPresent; + if (Detail::mm_check_ptr_address(heap_ptr) == No) return kErrorHeapNotPresent; Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast((UIntPtr) heap_ptr - @@ -160,8 +161,8 @@ _Output Int32 mm_make_page(VoidPtr heap_ptr) { heap_info_ptr->fPage = true; - (Void)(kout << "Registered page address: " << hex_number(reinterpret_cast(heap_info_ptr)) - << kendl); + (Void)(kout << "MemoryMgr: Registered page from heap address: " + << hex_number(reinterpret_cast(heap_info_ptr)) << kendl); return kErrorSuccess; } @@ -169,8 +170,8 @@ _Output Int32 mm_make_page(VoidPtr heap_ptr) { /// @brief Overwrites and set the flags of a heap header. /// @param heap_ptr the pointer to update. /// @param flags the flags to set. -_Output Int32 mm_make_flags(VoidPtr heap_ptr, UInt64 flags) { - if (Detail::mm_check_heap_address(heap_ptr) == No) return kErrorHeapNotPresent; +_Output Int32 mm_make_ptr_flags(VoidPtr heap_ptr, UInt64 flags) { + if (Detail::mm_check_ptr_address(heap_ptr) == No) return kErrorHeapNotPresent; Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast((UIntPtr) heap_ptr - @@ -185,7 +186,7 @@ _Output Int32 mm_make_flags(VoidPtr heap_ptr, UInt64 flags) { /// @brief Gets the flags of a heap header. /// @param heap_ptr the pointer to get. -_Output UInt64 mm_get_flags(VoidPtr heap_ptr) { +_Output UInt64 mm_get_ptr_flags(VoidPtr heap_ptr) { Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast((UIntPtr) heap_ptr - sizeof(Detail::MM_INFORMATION_BLOCK)); @@ -198,8 +199,8 @@ _Output UInt64 mm_get_flags(VoidPtr heap_ptr) { /// @brief Declare pointer as free. /// @param heap_ptr the pointer. /// @return -_Output Int32 mm_delete_heap(VoidPtr heap_ptr) { - if (Detail::mm_check_heap_address(heap_ptr) == No) return kErrorHeapNotPresent; +_Output Int32 mm_delete_ptr(VoidPtr heap_ptr) { + if (Detail::mm_check_ptr_address(heap_ptr) == No) return kErrorHeapNotPresent; Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast((UIntPtr) (heap_ptr) - @@ -219,16 +220,16 @@ _Output Int32 mm_delete_heap(VoidPtr heap_ptr) { heap_info_ptr->fMagic = 0; heap_info_ptr->fPad = 0; - (Void)(kout << "Freed heap address: " << hex_number(reinterpret_cast(heap_info_ptr)) - << kendl); + (Void)(kout << "MemoryMgr: Freed heap address: " + << hex_number(reinterpret_cast(heap_info_ptr)) << kendl); PTEWrapper page_wrapper( No, No, No, reinterpret_cast(heap_info_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK)); + Ref pte_address{page_wrapper}; - PageMgr page_mgr; - page_mgr.Free(pte_address); + kPageMgr.Free(pte_address); return kErrorSuccess; } @@ -239,7 +240,7 @@ _Output Int32 mm_delete_heap(VoidPtr heap_ptr) { /// @brief Check if pointer is a valid Kernel pointer. /// @param heap_ptr the pointer /// @return if it exists. -_Output Boolean mm_is_valid_heap(VoidPtr heap_ptr) { +_Output Boolean mm_is_valid_ptr(VoidPtr heap_ptr) { if (heap_ptr && HAL::mm_is_bitmap(heap_ptr)) { Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast((UIntPtr) (heap_ptr) - @@ -254,7 +255,7 @@ _Output Boolean mm_is_valid_heap(VoidPtr heap_ptr) { /// @brief Protect the heap with a CRC value. /// @param heap_ptr HIB pointer. /// @return if it valid: point has crc now., otherwise fail. -_Output Boolean mm_protect_heap(VoidPtr heap_ptr) { +_Output Boolean mm_protect_ptr(VoidPtr heap_ptr) { if (heap_ptr) { Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast((UIntPtr) heap_ptr - diff --git a/dev/kernel/src/New+Delete.cc b/dev/kernel/src/New+Delete.cc index 96e7ab64..a6bde691 100644 --- a/dev/kernel/src/New+Delete.cc +++ b/dev/kernel/src/New+Delete.cc @@ -10,25 +10,25 @@ void* operator new[](size_t sz) { if (sz == 0) ++sz; - return Kernel::mm_new_heap(sz, true, false); + return Kernel::mm_new_ptr(sz, true, false); } void* operator new(size_t sz) { if (sz == 0) ++sz; - return Kernel::mm_new_heap(sz, true, false); + return Kernel::mm_new_ptr(sz, true, false); } void operator delete[](void* ptr) { if (ptr == nullptr) return; - Kernel::mm_delete_heap(ptr); + Kernel::mm_delete_ptr(ptr); } void operator delete(void* ptr) { if (ptr == nullptr) return; - Kernel::mm_delete_heap(ptr); + Kernel::mm_delete_ptr(ptr); } void operator delete(void* ptr, size_t sz) { @@ -36,7 +36,7 @@ void operator delete(void* ptr, size_t sz) { NE_UNUSED(sz); - Kernel::mm_delete_heap(ptr); + Kernel::mm_delete_ptr(ptr); } void operator delete[](void* ptr, size_t sz) { @@ -44,5 +44,5 @@ void operator delete[](void* ptr, size_t sz) { NE_UNUSED(sz); - Kernel::mm_delete_heap(ptr); + Kernel::mm_delete_ptr(ptr); } diff --git a/dev/kernel/src/PEFCodeMgr.cc b/dev/kernel/src/PEFCodeMgr.cc index 09a262d4..5ff54098 100644 --- a/dev/kernel/src/PEFCodeMgr.cc +++ b/dev/kernel/src/PEFCodeMgr.cc @@ -78,7 +78,7 @@ PEFLoader::PEFLoader(const Char* path) : fCachedBlob(nullptr), fFatBinary(false) fBad = true; - if (fCachedBlob) mm_delete_heap(fCachedBlob); + if (fCachedBlob) mm_delete_ptr(fCachedBlob); kout << "PEFLoader: Warning: Executable format error!\r"; @@ -89,7 +89,7 @@ PEFLoader::PEFLoader(const Char* path) : fCachedBlob(nullptr), fFatBinary(false) /// @brief PEF destructor. /***********************************************************************************/ PEFLoader::~PEFLoader() { - if (fCachedBlob) mm_delete_heap(fCachedBlob); + if (fCachedBlob) mm_delete_ptr(fCachedBlob); fFile.Delete(); } @@ -147,7 +147,7 @@ ErrorOr PEFLoader::FindSymbol(const Char* name, Int32 kind) { if (container_header->Kind == kind) { if (container_header->Cpu != Detail::ldr_get_platform()) { if (!this->fFatBinary) { - mm_delete_heap(blob); + mm_delete_ptr(blob); return ErrorOr{kErrorInvalidData}; } } @@ -156,7 +156,7 @@ ErrorOr PEFLoader::FindSymbol(const Char* name, Int32 kind) { rt_copy_memory((VoidPtr) ((Char*) blob + sizeof(PEFCommandHeader)), container_blob_value, container_header->Size); - mm_delete_heap(blob); + mm_delete_ptr(blob); kout << "PEFLoader: Information: Loaded stub: " << container_header->Name << "!\r"; @@ -165,7 +165,7 @@ ErrorOr PEFLoader::FindSymbol(const Char* name, Int32 kind) { HAL::kMMFlagsPresent | HAL::kMMFlagsUser); if (ret != kErrorSuccess) { - mm_delete_heap(container_blob_value); + mm_delete_ptr(container_blob_value); return ErrorOr{kErrorInvalidData}; } @@ -174,7 +174,7 @@ ErrorOr PEFLoader::FindSymbol(const Char* name, Int32 kind) { } } - mm_delete_heap(blob); + mm_delete_ptr(blob); return ErrorOr{kErrorInvalidData}; } @@ -236,7 +236,7 @@ namespace Utils { UserProcessScheduler::The().Spawn(reinterpret_cast(symname.Leak().Leak()), errOrStart.Leak().Leak(), exec.GetBlob().Leak().Leak()); - mm_delete_heap(symname.Leak().Leak()); + mm_delete_ptr(symname.Leak().Leak()); if (id != kSchedInvalidPID) { auto stacksym = exec.FindSymbol(kPefStackSizeSymbol, kPefData); @@ -253,7 +253,7 @@ namespace Utils { UserProcessScheduler::The().CurrentTeam().AsArray()[id].StackSize = *(UIntPtr*) stacksym.Leak().Leak(); - mm_delete_heap(stacksym.Leak().Leak()); + mm_delete_ptr(stacksym.Leak().Leak()); } return id; diff --git a/dev/kernel/src/UserProcessScheduler.cc b/dev/kernel/src/UserProcessScheduler.cc index f868a810..e44cbed4 100644 --- a/dev/kernel/src/UserProcessScheduler.cc +++ b/dev/kernel/src/UserProcessScheduler.cc @@ -25,14 +25,14 @@ ///! BUGS: 0 namespace Kernel { +EXTERN_C Void sched_jump_to_task(HAL::StackFramePtr stack_frame); + /***********************************************************************************/ /// @brief Exit Code global variable. /***********************************************************************************/ STATIC UInt32 kLastExitCode = 0U; -STATIC BOOL kCurrentlySwitching = No; - /***********************************************************************************/ /// @brief Scheduler itself. /***********************************************************************************/ @@ -101,7 +101,7 @@ Void USER_PROCESS::Wake(Bool should_wakeup) { /** @param tree The tree to calibrate */ /***********************************************************************************/ -STATIC PROCESS_HEAP_TREE* sched_try_go_upper_heap_tree(PROCESS_HEAP_TREE* tree) { +STATIC PROCESS_HEAP_TREE* sched_try_go_upper_ptr_tree(PROCESS_HEAP_TREE* tree) { if (!tree) { return nullptr; } @@ -132,11 +132,11 @@ ErrorOr USER_PROCESS::New(SizeT sz, SizeT pad_amount) { auto vm_register = kKernelCR3; hal_write_cr3(this->VMRegister); - auto ptr = mm_new_heap(sz, Yes, Yes, pad_amount); + auto ptr = mm_new_ptr(sz, Yes, Yes, pad_amount); hal_write_cr3(vm_register); #else - auto ptr = mm_new_heap(sz, Yes, Yes, pad_amount); + auto ptr = mm_new_ptr(sz, Yes, Yes, pad_amount); #endif if (!this->HeapTree) { @@ -173,7 +173,7 @@ ErrorOr USER_PROCESS::New(SizeT sz, SizeT pad_amount) { is_parent = NO; entry = entry->Next; } else { - entry = sched_try_go_upper_heap_tree(entry); + entry = sched_try_go_upper_ptr_tree(entry); if (entry && entry->Color == kBlackTreeKind) break; } } @@ -240,20 +240,20 @@ const AffinityKind& USER_PROCESS::GetAffinity() noexcept { /** @brief Free heap tree. */ /***********************************************************************************/ -STATIC Void sched_free_heap_tree(PROCESS_HEAP_TREE* memory_heap_list) { +STATIC Void sched_free_ptr_tree(PROCESS_HEAP_TREE* memory_ptr_list) { // Deleting memory lists. Make sure to free all of them. - while (memory_heap_list) { - if (memory_heap_list->Entry) { - MUST_PASS(mm_delete_heap(memory_heap_list->Entry)); + while (memory_ptr_list) { + if (memory_ptr_list->Entry) { + MUST_PASS(mm_delete_ptr(memory_ptr_list->Entry)); } - auto next = memory_heap_list->Next; + auto next = memory_ptr_list->Next; - mm_delete_heap(memory_heap_list); + mm_delete_ptr(memory_ptr_list); - if (memory_heap_list->Child) sched_free_heap_tree(memory_heap_list->Child); + if (memory_ptr_list->Child) sched_free_ptr_tree(memory_ptr_list->Child); - memory_heap_list = next; + memory_ptr_list = next; } } @@ -270,14 +270,14 @@ Void USER_PROCESS::Exit(const Int32& exit_code) { kLastExitCode = exit_code; - auto memory_heap_list = this->HeapTree; + auto memory_ptr_list = this->HeapTree; #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ auto pd = kKernelCR3; hal_write_cr3(this->VMRegister); #endif - sched_free_heap_tree(memory_heap_list); + sched_free_ptr_tree(memory_ptr_list); #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ hal_write_cr3(pd); @@ -289,24 +289,19 @@ Void USER_PROCESS::Exit(const Int32& exit_code) { #endif //! Delete image if not done already. - if (this->Image.fCode && mm_is_valid_heap(this->Image.fCode)) mm_delete_heap(this->Image.fCode); + if (this->Image.fCode && mm_is_valid_ptr(this->Image.fCode)) mm_delete_ptr(this->Image.fCode); //! Delete blob too. - if (this->Image.fBlob && mm_is_valid_heap(this->Image.fBlob)) mm_delete_heap(this->Image.fBlob); + if (this->Image.fBlob && mm_is_valid_ptr(this->Image.fBlob)) mm_delete_ptr(this->Image.fBlob); //! Delete stack frame. - if (this->StackFrame && mm_is_valid_heap(this->StackFrame)) - mm_delete_heap((VoidPtr) this->StackFrame); - - //! Delete stack reserve. - if (this->StackReserve && mm_is_valid_heap(this->StackReserve)) - mm_delete_heap(reinterpret_cast(this->StackReserve)); + if (this->StackFrame && mm_is_valid_ptr(this->StackFrame)) + mm_delete_ptr((VoidPtr) this->StackFrame); //! Avoid use after free. - this->Image.fBlob = nullptr; - this->Image.fCode = nullptr; - this->StackFrame = nullptr; - this->StackReserve = nullptr; + this->Image.fBlob = nullptr; + this->Image.fCode = nullptr; + this->StackFrame = nullptr; if (this->Kind == kExecutableDylibKind) { Bool success = false; @@ -326,6 +321,34 @@ Void USER_PROCESS::Exit(const Int32& exit_code) { --this->ParentTeam->mProcessCount; } +/***********************************************************************************/ +/// @brief Add dylib to process. +/***********************************************************************************/ + +Bool USER_PROCESS::SpawnDylib() { + // React according to process kind. + switch (this->Kind) { + case USER_PROCESS::kExecutableDylibKind: { + this->DylibDelegate = rtl_init_dylib_pef(*this); + + if (!this->DylibDelegate) { + this->Crash(); + return NO; + } + + return YES; + } + case USER_PROCESS::kExecutableKind: { + return NO; + } + default: { + (Void)(kout << "Unknown process kind: " << hex_number(this->Kind) << kendl); + this->Crash(); + return NO; + } + } +} + /***********************************************************************************/ /// @brief Add process to team. /// @param process the process *Ref* class. @@ -363,18 +386,9 @@ ProcessID UserProcessScheduler::Spawn(const Char* name, VoidPtr code, VoidPtr im rt_copy_memory(reinterpret_cast(const_cast(name)), process.Name, len); #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - process.VMRegister = new PDE(); - - if (!process.VMRegister) { - process.Crash(); - return -kErrorProcessFault; - } - - UInt32 flags = HAL::kMMFlagsPresent; - flags |= HAL::kMMFlagsWr; - flags |= HAL::kMMFlagsUser; - - HAL::mm_map_page((VoidPtr) process.VMRegister, process.VMRegister, flags); + process.VMRegister = kKernelCR3; +#else + process.VMRegister = 0UL; #endif // ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ process.StackFrame = new HAL::StackFrame(); @@ -386,52 +400,17 @@ ProcessID UserProcessScheduler::Spawn(const Char* name, VoidPtr code, VoidPtr im rt_set_memory(process.StackFrame, 0, sizeof(HAL::StackFrame)); -#ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - flags = HAL::kMMFlagsPresent; - flags |= HAL::kMMFlagsWr; - flags |= HAL::kMMFlagsUser; + process.StackFrame->BP = reinterpret_cast(code); - HAL::mm_map_page((VoidPtr) process.StackFrame, process.StackFrame, flags); -#endif // ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - - // React according to process kind. - switch (process.Kind) { - case USER_PROCESS::kExecutableDylibKind: { - process.DylibDelegate = rtl_init_dylib_pef(process); - MUST_PASS(process.DylibDelegate); - break; - } - case USER_PROCESS::kExecutableKind: { - break; - } - default: { - (Void)(kout << "Unknown process kind: " << hex_number(process.Kind) << kendl); - break; - } - } - - process.StackReserve = new UInt8[process.StackSize]; - - if (!process.StackReserve) { - process.Crash(); - return -kErrorProcessFault; - } + process.StackSize = kSchedMaxStackSz; rt_set_memory(process.StackReserve, 0, process.StackSize); -#ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - flags = HAL::kMMFlagsPresent; - flags |= HAL::kMMFlagsWr; - flags |= HAL::kMMFlagsUser; - - HAL::mm_map_page((VoidPtr) process.StackReserve, process.StackReserve, flags); -#endif // ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - process.ParentTeam = &mTeam; process.ProcessId = pid; - process.Status = ProcessStatusKind::kStarting; - process.PTime = (UIntPtr) AffinityKind::kStandard; + process.Status = ProcessStatusKind::kRunning; + process.PTime = 0; process.RTime = 0; (Void)(kout << "PID: " << number(process.ProcessId) << kendl); @@ -498,10 +477,6 @@ SizeT UserProcessScheduler::Run() noexcept { return 0UL; } - if (kCurrentlySwitching) return 0UL; - - kCurrentlySwitching = Yes; - SizeT process_index = 0UL; //! we store this guy to tell the scheduler how many //! things we have scheduled. @@ -516,53 +491,38 @@ SizeT UserProcessScheduler::Run() noexcept { continue; } - // Set current process header. + kout << "The process: " << process.Name << " is being scheduled to run...\r"; + this->CurrentProcess() = process; process.PTime = static_cast(process.Affinity); + process.RTime = 0UL; - // tell helper to find a core to schedule on. - BOOL ret = UserProcessHelper::Switch(process.Image.Leak().Leak().Leak(), - &process.StackReserve[process.StackSize - 1], - process.StackFrame, process.ProcessId); - - if (!ret) { - kout << "The process: " << process.Name << ", is not valid! Crashing it...\r"; - process.Crash(); + // tell helper to find a core to schedule on, otherwise run on this core directly. + if (!UserProcessHelper::Switch(process.StackFrame, process.ProcessId)) { + sched_jump_to_task(process.StackFrame); } - } else { - if (process.ProcessId == this->CurrentProcess().Leak().ProcessId) { + + if (process.ProcessId == this->CurrentProcess().Leak().ProcessId && + process.PTime > (Int32) AffinityKind::kStandard) { if (process.PTime < process.RTime) { - if (process.RTime < (Int32) AffinityKind::kRealTime) - process.PTime = (Int32) AffinityKind::kVeryLowUsage; - else if (process.RTime < (Int32) AffinityKind::kVeryHigh) + if (process.RTime < (Int32) AffinityKind::kVeryHigh) process.PTime = (Int32) AffinityKind::kLowUsage; else if (process.RTime < (Int32) AffinityKind::kHigh) process.PTime = (Int32) AffinityKind::kStandard; else if (process.RTime < (Int32) AffinityKind::kStandard) process.PTime = (Int32) AffinityKind::kHigh; - process.RTime = static_cast(process.Affinity); - - BOOL ret = UserProcessHelper::Switch(process.Image.Leak().Leak().Leak(), - &process.StackReserve[process.StackSize - 1], - process.StackFrame, process.ProcessId); - - if (!ret) { - kout << "The process: " << process.Name << ", is not valid! Crashing it...\r"; - process.Crash(); - } + process.RTime = 0UL; } else { ++process.RTime; } } - + } else { --process.PTime; } } - kCurrentlySwitching = No; - return process_index; } @@ -582,8 +542,6 @@ UserProcessTeam& UserProcessScheduler::CurrentTeam() { BOOL UserProcessScheduler::SwitchTeam(UserProcessTeam& team) { if (team.AsArray().Count() < 1) return No; - if (kCurrentlySwitching) return No; - kScheduler.mTeam = team; return Yes; @@ -617,8 +575,6 @@ Bool UserProcessHelper::CanBeScheduled(const USER_PROCESS& process) { if (process.Status == ProcessStatusKind::kInvalid) return No; - if (!process.Image.HasCode()) return No; - if (!process.Name[0]) return No; // real time processes shouldn't wait that much. @@ -645,8 +601,7 @@ SizeT UserProcessHelper::StartScheduling() { */ /***********************************************************************************/ -Bool UserProcessHelper::Switch(VoidPtr image, UInt8* stack, HAL::StackFramePtr frame_ptr, - PID new_pid) { +Bool UserProcessHelper::Switch(HAL::StackFramePtr frame_ptr, PID new_pid) { for (SizeT index = 0UL; index < HardwareThreadScheduler::The().Capacity(); ++index) { if (HardwareThreadScheduler::The()[index].Leak()->Kind() == kAPInvalid || HardwareThreadScheduler::The()[index].Leak()->Kind() == kAPBoot) @@ -658,13 +613,13 @@ Bool UserProcessHelper::Switch(VoidPtr image, UInt8* stack, HAL::StackFramePtr f AffinityKind::kRealTime) continue; - if (HardwareThreadScheduler::The()[index].Leak()->Switch(image, stack, frame_ptr, new_pid)) { + if (HardwareThreadScheduler::The()[index].Leak()->Switch(frame_ptr, new_pid)) { HardwareThreadScheduler::The()[index].Leak()->fPTime = UserProcessScheduler::The().CurrentTeam().AsArray()[new_pid].PTime; UserProcessHelper::TheCurrentPID().Leak().Leak() = UserProcessHelper::TheCurrentPID(); - return YES; + break; } continue; @@ -678,8 +633,7 @@ Bool UserProcessHelper::Switch(VoidPtr image, UInt8* stack, HAL::StackFramePtr f /// Prepare task switch. /// //////////////////////////////////////////////////////////// - Bool ret = - HardwareThreadScheduler::The()[index].Leak()->Switch(image, stack, frame_ptr, new_pid); + Bool ret = HardwareThreadScheduler::The()[index].Leak()->Switch(frame_ptr, new_pid); //////////////////////////////////////////////////////////// /// Rollback on fail. /// @@ -693,10 +647,10 @@ Bool UserProcessHelper::Switch(VoidPtr image, UInt8* stack, HAL::StackFramePtr f UserProcessScheduler::The().CurrentTeam().AsArray()[new_pid].PTime; HardwareThreadScheduler::The()[index].Leak()->Wake(YES); - return Yes; + return YES; } - return No; + return NO; } //////////////////////////////////////////////////////////// -- cgit v1.2.3