From b55d3ac738b40677c579221b4f0dbf294dc3b017 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 23 Apr 2025 22:27:47 +0200 Subject: dev, kernel: necessary refactors regarding future changes. Signed-off-by: Amlal El Mahrouss --- dev/kernel/KernelKit/CoreProcessScheduler.h | 161 +++++++++++++++++++++++++ dev/kernel/KernelKit/HardwareThreadScheduler.h | 8 +- dev/kernel/KernelKit/IPEFDylibObject.h | 4 +- dev/kernel/KernelKit/PE.h | 13 +- dev/kernel/KernelKit/PECodeMgr.h | 55 +++++++++ dev/kernel/KernelKit/PEFCodeMgr.h | 4 +- dev/kernel/KernelKit/ProcessSchedulerCore.h | 161 ------------------------- dev/kernel/KernelKit/UserProcessScheduler.h | 2 +- dev/kernel/KernelKit/UserProcessScheduler.inl | 6 +- 9 files changed, 234 insertions(+), 180 deletions(-) create mode 100644 dev/kernel/KernelKit/CoreProcessScheduler.h delete mode 100644 dev/kernel/KernelKit/ProcessSchedulerCore.h (limited to 'dev/kernel/KernelKit') diff --git a/dev/kernel/KernelKit/CoreProcessScheduler.h b/dev/kernel/KernelKit/CoreProcessScheduler.h new file mode 100644 index 00000000..bd493140 --- /dev/null +++ b/dev/kernel/KernelKit/CoreProcessScheduler.h @@ -0,0 +1,161 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include +#include + +namespace Kernel +{ + class USER_PROCESS; + class KERNEL_PROCESS; + class UserProcessTeam; + + /***********************************************************************************/ + /// @brief Subsystem enum type. + /***********************************************************************************/ + + enum class ProcessSubsystem : Int32 + { + kProcessSubsystemSecurity = 100, + kProcessSubsystemApplication, + kProcessSubsystemService, + kProcessSubsystemDriver, + kProcessSubsystemInvalid = 256U, + kProcessSubsystemCount = 4, + }; + + typedef UInt64 PTime; + + /***********************************************************************************/ + //! @brief Local Process identifier. + /***********************************************************************************/ + typedef Int64 ProcessID; + + /***********************************************************************************/ + //! @brief Local Process status enum. + /***********************************************************************************/ + enum class ProcessStatusKind : Int32 + { + kInvalid, + kStarting, + kRunning, + kKilled, + kFrozen, + kFinished, + kCount, + }; + + /***********************************************************************************/ + //! @brief Affinity is the amount of nano-seconds this process is going to run. + /***********************************************************************************/ + enum class AffinityKind : Int32 + { + kRealTime = 500, + kVeryHigh = 250, + kHigh = 200, + kStandard = 1000, + kLowUsage = 1500, + kVeryLowUsage = 2000, + }; + + /***********************************************************************************/ + //! Operators for AffinityKind + /***********************************************************************************/ + + inline bool operator<(AffinityKind lhs, AffinityKind rhs) + { + Int32 lhs_int = static_cast(lhs); + Int32 rhs_int = static_cast(rhs); + + return lhs_int < rhs_int; + } + + inline bool operator>(AffinityKind lhs, AffinityKind rhs) + { + Int32 lhs_int = static_cast(lhs); + Int32 rhs_int = static_cast(rhs); + + return lhs_int > rhs_int; + } + + inline bool operator<=(AffinityKind lhs, AffinityKind rhs) + { + Int32 lhs_int = static_cast(lhs); + Int32 rhs_int = static_cast(rhs); + + return lhs_int <= rhs_int; + } + + inline bool operator>=(AffinityKind lhs, AffinityKind rhs) + { + Int32 lhs_int = static_cast(lhs); + Int32 rhs_int = static_cast(rhs); + + return lhs_int >= rhs_int; + } + + using ProcessTime = UInt64; + using PID = Int64; + + /***********************************************************************************/ + /// @note For User manager, tells where we run the code. + /***********************************************************************************/ + enum class ProcessLevelRing : Int32 + { + kRingStdUser = 1, + kRingSuperUser = 2, + kRingGuestUser = 5, + kRingCount = 5, + }; + + /***********************************************************************************/ + /// @brief Helper type to describe a code image. + /***********************************************************************************/ + using ImagePtr = VoidPtr; + + /***********************************************************************************/ + /// @brief Helper class to contain a process's image and blob. + /***********************************************************************************/ + struct PROCESS_IMAGE final + { + explicit PROCESS_IMAGE() = default; + + ImagePtr fCode; + ImagePtr fBlob; + + Bool HasCode() + { + return this->fCode != nullptr; + } + + Bool HasImage() + { + return this->fBlob != nullptr; + } + + ErrorOr Leak() + { + if (this->fCode) + { + return ErrorOr{this->fCode}; + } + + return ErrorOr{kErrorInvalidData}; + } + + ErrorOr LeakBlob() + { + if (this->fBlob) + { + return ErrorOr{this->fBlob}; + } + + return ErrorOr{kErrorInvalidData}; + } + }; +} // namespace Kernel diff --git a/dev/kernel/KernelKit/HardwareThreadScheduler.h b/dev/kernel/KernelKit/HardwareThreadScheduler.h index 9af22715..4d2ebc73 100644 --- a/dev/kernel/KernelKit/HardwareThreadScheduler.h +++ b/dev/kernel/KernelKit/HardwareThreadScheduler.h @@ -57,12 +57,12 @@ namespace Kernel operator bool(); public: - void Wake(const bool wakeup = false) noexcept; - void Busy(const bool busy = false) noexcept; + void Wake(const BOOL wakeup = false) noexcept; + void Busy(const BOOL busy = false) noexcept; public: - bool Switch(VoidPtr image, Ptr8 stack_ptr, HAL::StackFramePtr frame, const ThreadID& pid); - bool IsWakeup() noexcept; + BOOL Switch(VoidPtr image, Ptr8 stack_ptr, HAL::StackFramePtr frame, const ThreadID& pid); + BOOL IsWakeup() noexcept; public: HAL::StackFramePtr StackFrame() noexcept; diff --git a/dev/kernel/KernelKit/IPEFDylibObject.h b/dev/kernel/KernelKit/IPEFDylibObject.h index 82f08932..a708fb6c 100644 --- a/dev/kernel/KernelKit/IPEFDylibObject.h +++ b/dev/kernel/KernelKit/IPEFDylibObject.h @@ -99,8 +99,8 @@ namespace Kernel typedef IPEFDylibObject* IDylibRef; - EXTERN_C IDylibRef rtl_init_dylib(USER_PROCESS& header); - EXTERN_C Void rtl_fini_dylib(USER_PROCESS& header, IDylibRef lib, Bool* successful); + EXTERN_C IDylibRef rtl_init_dylib_pef(USER_PROCESS& header); + EXTERN_C Void rtl_fini_dylib_pef(USER_PROCESS& header, IDylibRef lib, Bool* successful); } // namespace Kernel #endif /* ifndef __KERNELKIT_SHARED_OBJECT_H__ */ diff --git a/dev/kernel/KernelKit/PE.h b/dev/kernel/KernelKit/PE.h index fbf8fc20..e2184918 100644 --- a/dev/kernel/KernelKit/PE.h +++ b/dev/kernel/KernelKit/PE.h @@ -16,16 +16,13 @@ #include -#define kPeSignature 0x00004550 +#define kPeSignature (0x00004550) -#define kPeDLLBase 0x4000000 -#define kPeEXEBase 0x1000000 +#define kPeMagic32 (0x010b) +#define kPeMagic64 (0x020b) -#define kPeMagic32 0x010b -#define kPeMagic64 0x020b - -#define kPeMachineAMD64 0x8664 -#define kPeMachineARM64 0xaa64 +#define kPeMachineAMD64 (0x8664) +#define kPeMachineARM64 (0xaa64) typedef struct LDR_EXEC_HEADER final { diff --git a/dev/kernel/KernelKit/PECodeMgr.h b/dev/kernel/KernelKit/PECodeMgr.h index e666c501..23ef4ecf 100644 --- a/dev/kernel/KernelKit/PECodeMgr.h +++ b/dev/kernel/KernelKit/PECodeMgr.h @@ -22,3 +22,58 @@ #include #include #include +#include +#include + +#ifndef INC_PROCESS_SCHEDULER_H +#include +#endif + +#define kPefApplicationMime "application/vnd-portable-executable" + +namespace Kernel +{ + /// + /// \name PE32Loader + /// \brief PE32+ loader class. + /// + class PE32Loader : public LoaderInterface + { + private: + explicit PE32Loader() = delete; + + public: + explicit PE32Loader(const VoidPtr blob); + explicit PE32Loader(const Char* path); + ~PE32Loader() override; + + public: + NE_COPY_DEFAULT(PE32Loader) + + public: + const Char* Path() override; + const Char* AsString() override; + const Char* MIME() override; + + public: + ErrorOr FindStart() override; + VoidPtr FindSymbol(const Char* name, Int32 kind) override; + ErrorOr GetBlob() override; + + public: + bool IsLoaded() noexcept; + + private: +#ifdef __FSKIT_INCLUDES_NEFS__ + OwnPtr> fFile; +#elif defined(__FSKIT_INCLUDES_HEFS__) + OwnPtr> fFile; +#else + OwnPtr> fFile; +#endif // __FSKIT_INCLUDES_NEFS__ + + Ref fPath; + VoidPtr fCachedBlob; + bool fBad; + }; +} // namespace Kernel \ No newline at end of file diff --git a/dev/kernel/KernelKit/PEFCodeMgr.h b/dev/kernel/KernelKit/PEFCodeMgr.h index 9adab8f6..389774fd 100644 --- a/dev/kernel/KernelKit/PEFCodeMgr.h +++ b/dev/kernel/KernelKit/PEFCodeMgr.h @@ -16,7 +16,7 @@ #include #endif -#define kPefApplicationMime "application/vnd-amlal-executable" +#define kPefApplicationMime "application/vnd-nekernel-executable" namespace Kernel { @@ -53,6 +53,8 @@ namespace Kernel private: #ifdef __FSKIT_INCLUDES_NEFS__ OwnPtr> fFile; +#elif defined(__FSKIT_INCLUDES_HEFS__) + OwnPtr> fFile; #else OwnPtr> fFile; #endif // __FSKIT_INCLUDES_NEFS__ diff --git a/dev/kernel/KernelKit/ProcessSchedulerCore.h b/dev/kernel/KernelKit/ProcessSchedulerCore.h deleted file mode 100644 index bd493140..00000000 --- a/dev/kernel/KernelKit/ProcessSchedulerCore.h +++ /dev/null @@ -1,161 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include -#include - -namespace Kernel -{ - class USER_PROCESS; - class KERNEL_PROCESS; - class UserProcessTeam; - - /***********************************************************************************/ - /// @brief Subsystem enum type. - /***********************************************************************************/ - - enum class ProcessSubsystem : Int32 - { - kProcessSubsystemSecurity = 100, - kProcessSubsystemApplication, - kProcessSubsystemService, - kProcessSubsystemDriver, - kProcessSubsystemInvalid = 256U, - kProcessSubsystemCount = 4, - }; - - typedef UInt64 PTime; - - /***********************************************************************************/ - //! @brief Local Process identifier. - /***********************************************************************************/ - typedef Int64 ProcessID; - - /***********************************************************************************/ - //! @brief Local Process status enum. - /***********************************************************************************/ - enum class ProcessStatusKind : Int32 - { - kInvalid, - kStarting, - kRunning, - kKilled, - kFrozen, - kFinished, - kCount, - }; - - /***********************************************************************************/ - //! @brief Affinity is the amount of nano-seconds this process is going to run. - /***********************************************************************************/ - enum class AffinityKind : Int32 - { - kRealTime = 500, - kVeryHigh = 250, - kHigh = 200, - kStandard = 1000, - kLowUsage = 1500, - kVeryLowUsage = 2000, - }; - - /***********************************************************************************/ - //! Operators for AffinityKind - /***********************************************************************************/ - - inline bool operator<(AffinityKind lhs, AffinityKind rhs) - { - Int32 lhs_int = static_cast(lhs); - Int32 rhs_int = static_cast(rhs); - - return lhs_int < rhs_int; - } - - inline bool operator>(AffinityKind lhs, AffinityKind rhs) - { - Int32 lhs_int = static_cast(lhs); - Int32 rhs_int = static_cast(rhs); - - return lhs_int > rhs_int; - } - - inline bool operator<=(AffinityKind lhs, AffinityKind rhs) - { - Int32 lhs_int = static_cast(lhs); - Int32 rhs_int = static_cast(rhs); - - return lhs_int <= rhs_int; - } - - inline bool operator>=(AffinityKind lhs, AffinityKind rhs) - { - Int32 lhs_int = static_cast(lhs); - Int32 rhs_int = static_cast(rhs); - - return lhs_int >= rhs_int; - } - - using ProcessTime = UInt64; - using PID = Int64; - - /***********************************************************************************/ - /// @note For User manager, tells where we run the code. - /***********************************************************************************/ - enum class ProcessLevelRing : Int32 - { - kRingStdUser = 1, - kRingSuperUser = 2, - kRingGuestUser = 5, - kRingCount = 5, - }; - - /***********************************************************************************/ - /// @brief Helper type to describe a code image. - /***********************************************************************************/ - using ImagePtr = VoidPtr; - - /***********************************************************************************/ - /// @brief Helper class to contain a process's image and blob. - /***********************************************************************************/ - struct PROCESS_IMAGE final - { - explicit PROCESS_IMAGE() = default; - - ImagePtr fCode; - ImagePtr fBlob; - - Bool HasCode() - { - return this->fCode != nullptr; - } - - Bool HasImage() - { - return this->fBlob != nullptr; - } - - ErrorOr Leak() - { - if (this->fCode) - { - return ErrorOr{this->fCode}; - } - - return ErrorOr{kErrorInvalidData}; - } - - ErrorOr LeakBlob() - { - if (this->fBlob) - { - return ErrorOr{this->fBlob}; - } - - return ErrorOr{kErrorInvalidData}; - } - }; -} // namespace Kernel diff --git a/dev/kernel/KernelKit/UserProcessScheduler.h b/dev/kernel/KernelKit/UserProcessScheduler.h index 6e1ca146..19818213 100644 --- a/dev/kernel/KernelKit/UserProcessScheduler.h +++ b/dev/kernel/KernelKit/UserProcessScheduler.h @@ -15,7 +15,7 @@ #include #include #include -#include +#include #define kSchedMinMicroTime (AffinityKind::kStandard) #define kSchedInvalidPID (-1) diff --git a/dev/kernel/KernelKit/UserProcessScheduler.inl b/dev/kernel/KernelKit/UserProcessScheduler.inl index a18af9c1..e31b5462 100644 --- a/dev/kernel/KernelKit/UserProcessScheduler.inl +++ b/dev/kernel/KernelKit/UserProcessScheduler.inl @@ -3,11 +3,11 @@ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. FILE: UserProcessScheduler.inl - PURPOSE: Low level/Ring-3 USER_PROCESS scheduler. + PURPOSE: Low level/Ring-3 process scheduler. ------------------------------------------- */ -/// @brief USER_PROCESS scheduler inline definitions. +/// @brief USER_PROCESS inline definitions. /// @author Amlal El Mahrouss (amlal@nekernel.org) /// @date Tue Apr 22 22:01:07 CEST 2025 @@ -57,7 +57,7 @@ namespace Kernel entry = entry->MemoryNext; } - kout << "Invalid Pointer: Trying to free a pointer which doesn't exist.\r"; + kout << "USER_PROCESS: Trying to free a pointer which doesn't exist.\r"; this->Crash(); -- cgit v1.2.3