diff options
| -rw-r--r-- | doc/specs/SPECIFICATION_OS.md | 3 | ||||
| -rw-r--r-- | src/kernel/FirmwareKit/EFI/EFI.h | 2 | ||||
| -rw-r--r-- | src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc | 48 | ||||
| -rw-r--r-- | src/kernel/HALKit/AMD64/HalKernelMain.cc | 2 | ||||
| -rw-r--r-- | src/kernel/KernelKit/KPC.h | 6 | ||||
| -rw-r--r-- | src/kernel/KernelKit/KernelTaskScheduler.h | 10 | ||||
| -rw-r--r-- | src/kernel/KernelKit/ThreadLocalStorage.inl | 4 | ||||
| -rw-r--r-- | src/kernel/KernelKit/UserMgr.h | 6 | ||||
| -rw-r--r-- | src/kernel/KernelKit/UserProcessScheduler.h | 74 | ||||
| -rw-r--r-- | src/kernel/KernelKit/UserProcessScheduler.inl | 12 | ||||
| -rw-r--r-- | src/kernel/src/CodeMgr.cc | 3 | ||||
| -rw-r--r-- | src/kernel/src/UserProcessScheduler.cc | 39 | ||||
| -rw-r--r-- | src/kernel/src/UserProcessTeam.cc | 2 |
13 files changed, 115 insertions, 96 deletions
diff --git a/doc/specs/SPECIFICATION_OS.md b/doc/specs/SPECIFICATION_OS.md index 08c2b326..ef66c062 100644 --- a/doc/specs/SPECIFICATION_OS.md +++ b/doc/specs/SPECIFICATION_OS.md @@ -74,9 +74,6 @@ ================================== - Filesystem to mountpoint interface abstraction. -- VFS-like subsystem inspired by NT/OS2 IFS. - Multi-drive support (A, B, C, D indices). -- Ext2 support via IFS layer. - Packet-based I/O operations. - Separation of read/write operations per drive. - diff --git a/src/kernel/FirmwareKit/EFI/EFI.h b/src/kernel/FirmwareKit/EFI/EFI.h index 149c39e1..912be6eb 100644 --- a/src/kernel/FirmwareKit/EFI/EFI.h +++ b/src/kernel/FirmwareKit/EFI/EFI.h @@ -351,7 +351,7 @@ typedef struct EFI_SIMPLE_NETWORK_PROTOCOL { VoidPtr Statistics; VoidPtr MCastIpToMac; VoidPtr NvData; - VoidPtr GetStatus; + VoidPtr Status; EFI_SIMPLE_NETWORK_TRANSMIT Transmit; EFI_SIMPLE_NETWORK_RECEIVE Receive; VoidPtr WaitForPacket; diff --git a/src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc b/src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc index c4209870..8f9e2e45 100644 --- a/src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc +++ b/src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc @@ -31,26 +31,26 @@ static void hal_idt_send_eoi(UInt8 vector) { /// @param rsp EXTERN_C Kernel::Void idt_handle_gpf(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); - process.Leak().Crash(); + process.Crash(); hal_idt_send_eoi(13); - process.Leak().Signal.SignalArg = rsp; - process.Leak().Signal.SignalID = SIGKILL; - process.Leak().Signal.Status = process.Leak().Status; + process.Signal.SignalArg = rsp; + process.Signal.SignalID = SIGKILL; + process.Signal.Status = process.Status; } /// @brief Handle page fault. /// @param rsp EXTERN_C void idt_handle_pf(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); - process.Leak().Crash(); + process.Crash(); hal_idt_send_eoi(14); - process.Leak().Signal.SignalArg = rsp; - process.Leak().Signal.SignalID = SIGKILL; - process.Leak().Signal.Status = process.Leak().Status; + process.Signal.SignalArg = rsp; + process.Signal.SignalID = SIGKILL; + process.Signal.Status = process.Status; } /// @brief Handle scheduler interrupt. @@ -72,30 +72,30 @@ EXTERN_C void idt_handle_scheduler(Kernel::UIntPtr rsp) { /// @param rsp EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); - process.Leak().Crash(); + process.Crash(); hal_idt_send_eoi(8); - process.Leak().Signal.SignalArg = rsp; - process.Leak().Signal.SignalID = sig_generate_unique<SIGKILL>(); + process.Signal.SignalArg = rsp; + process.Signal.SignalID = sig_generate_unique<SIGKILL>(); ; - process.Leak().Signal.Status = process.Leak().Status; + process.Signal.Status = process.Status; } /// @brief Handle any generic fault. /// @param rsp EXTERN_C void idt_handle_generic(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); - process.Leak().Crash(); + process.Crash(); hal_idt_send_eoi(30); Kernel::kout << "Kernel: Generic Process Fault.\r"; - process.Leak().Signal.SignalArg = rsp; - process.Leak().Signal.SignalID = sig_generate_unique<SIGSEG>(); + process.Signal.SignalArg = rsp; + process.Signal.SignalID = sig_generate_unique<SIGSEG>(); ; - process.Leak().Signal.Status = process.Leak().Status; + process.Signal.Status = process.Status; Kernel::kout << "Kernel: SIGKILL status.\r"; } @@ -105,25 +105,25 @@ EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip) { hal_idt_send_eoi(3); - process.Leak().Signal.SignalArg = rip; - process.Leak().Signal.SignalID = sig_generate_unique<SIGTRAP>(); + process.Signal.SignalArg = rip; + process.Signal.SignalID = sig_generate_unique<SIGTRAP>(); - process.Leak().Signal.Status = process.Leak().Status; + process.Signal.Status = process.Status; - process.Leak().Status = Kernel::ProcessStatusKind::kFrozen; + process.Status = Kernel::ProcessStatusKind::kFrozen; } /// @brief Handle #UD fault. /// @param rsp EXTERN_C void idt_handle_ud(Kernel::UIntPtr rsp) { auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); - process.Leak().Crash(); + process.Crash(); hal_idt_send_eoi(6); - process.Leak().Signal.SignalArg = rsp; - process.Leak().Signal.SignalID = sig_generate_unique<SIGKILL>(); - process.Leak().Signal.Status = process.Leak().Status; + process.Signal.SignalArg = rsp; + process.Signal.SignalID = sig_generate_unique<SIGKILL>(); + process.Signal.Status = process.Status; } /// @brief Enter syscall from assembly (libSystem only) diff --git a/src/kernel/HALKit/AMD64/HalKernelMain.cc b/src/kernel/HALKit/AMD64/HalKernelMain.cc index 2e2cca44..4cf7d608 100644 --- a/src/kernel/HALKit/AMD64/HalKernelMain.cc +++ b/src/kernel/HALKit/AMD64/HalKernelMain.cc @@ -145,8 +145,6 @@ EXTERN_C Kernel::Void hal_real_init(Kernel::Void) { // AMLALE: TODO: Prosan, Process sanitizer. rtl_create_user_process([]() -> void { while (YES); }, "ProSan"); - UserProcessScheduler::The().SwitchTeam(kMidUserTeam); - // AMLALE: TODO, Vet sanitizer. rtl_create_user_process([]() -> void { while (YES); }, "VetSan"); diff --git a/src/kernel/KernelKit/KPC.h b/src/kernel/KernelKit/KPC.h index 62ce1d68..e2040903 100644 --- a/src/kernel/KernelKit/KPC.h +++ b/src/kernel/KernelKit/KPC.h @@ -11,13 +11,13 @@ /// @brief Kernel Procedure Code. #define err_local_ok() \ - (Kernel::UserProcessScheduler::The().TheCurrentProcess().Leak().GetLocalCode() == \ + (Kernel::UserProcessScheduler::The().TheCurrentProcess().GetLocalCode() == \ Kernel::kErrorSuccess) #define err_local_fail() \ - (Kernel::UserProcessScheduler::The().TheCurrentProcess().Leak().GetLocalCode() != \ + (Kernel::UserProcessScheduler::The().TheCurrentProcess().GetLocalCode() != \ Kernel::kErrorSuccess) #define err_local_get() \ - (Kernel::UserProcessScheduler::The().TheCurrentProcess().Leak().GetLocalCode()) + (Kernel::UserProcessScheduler::The().TheCurrentProcess().GetLocalCode()) #define err_global_ok() (Kernel::kErrorLocalNumber == Kernel::kErrorSuccess) #define err_global_fail() (Kernel::kErrorLocalNumber != Kernel::kErrorSuccess) diff --git a/src/kernel/KernelKit/KernelTaskScheduler.h b/src/kernel/KernelKit/KernelTaskScheduler.h index 1db3df50..f566dfc1 100644 --- a/src/kernel/KernelKit/KernelTaskScheduler.h +++ b/src/kernel/KernelKit/KernelTaskScheduler.h @@ -14,13 +14,14 @@ #include <KernelKit/LockDelegate.h> namespace Kernel { + class KernelTaskHelper; using KID = ProcessID; /// @brief Equivalent of UserProcess, but for kernel tasks. /// @author Amlal -class KernelTask final { +class KernelTask { public: Char Name[kSchedNameLen] = {"KernelTask"}; ProcessSubsystem SubSystem{ProcessSubsystem::kProcessSubsystemKernel}; @@ -28,9 +29,9 @@ class KernelTask final { UInt8* StackReserve{nullptr}; SizeT StackSize{kSchedMaxStackSz}; ProcessImage Image{}; - /// @brief a KID is a Kernel ID, it is used to find a task running within - /// the kernel. - KID Kid{0}; + + /// @brief a KID is a Kernel ID, it is used to find a task running within the kernel. + KID Kid{}; NE_NON_VETTABLE; }; @@ -45,6 +46,7 @@ class KernelTaskHelper final { STATIC ErrorOr<KID> TheCurrentKID(); STATIC SizeT StartScheduling(); }; + } // namespace Kernel #endif diff --git a/src/kernel/KernelKit/ThreadLocalStorage.inl b/src/kernel/KernelKit/ThreadLocalStorage.inl index b0775db2..30d81e8d 100644 --- a/src/kernel/KernelKit/ThreadLocalStorage.inl +++ b/src/kernel/KernelKit/ThreadLocalStorage.inl @@ -15,7 +15,7 @@ inline T* tls_new_ptr(void) { auto ref_process = UserProcessScheduler::The().TheCurrentProcess(); - auto pointer = ref_process.Leak().New(sizeof(T)); + auto pointer = ref_process.New(sizeof(T)); if (pointer.Error()) return nullptr; @@ -34,7 +34,7 @@ inline Kernel::Bool tls_delete_ptr(T* obj) { ErrorOr<T*> obj_wrapped{obj}; - return ref_process.Leak().Delete(obj_wrapped); + return ref_process.Delete(obj_wrapped); } //! @brief Delete process pointer. diff --git a/src/kernel/KernelKit/UserMgr.h b/src/kernel/KernelKit/UserMgr.h index 7193a4df..e698bd7b 100644 --- a/src/kernel/KernelKit/UserMgr.h +++ b/src/kernel/KernelKit/UserMgr.h @@ -9,7 +9,7 @@ Revision History: - 04/03/25: Set users directory as /libSystem/ instead of /usr/ + 04/03/25: Set users directory as /users/ instead of /usr/ ======================================== */ @@ -40,8 +40,8 @@ enum struct UserRingKind : Int32 { kRingCount = 3, }; -typedef Char* UserPublicKey; -typedef Char UserPublicKeyType; +using UserPublicKey = Char*; +using UserPublicKeyType = Char; /// @brief System User class. class User final { diff --git a/src/kernel/KernelKit/UserProcessScheduler.h b/src/kernel/KernelKit/UserProcessScheduler.h index 44e5072e..e4d86a19 100644 --- a/src/kernel/KernelKit/UserProcessScheduler.h +++ b/src/kernel/KernelKit/UserProcessScheduler.h @@ -30,7 +30,7 @@ class UserProcessHelper; /// @name UserProcess /// @brief UserProcess class, holds information about the running process/thread. /***********************************************************************************/ -class UserProcess final { +class UserProcess { public: UserProcess(); ~UserProcess(); @@ -44,7 +44,12 @@ class UserProcess final { NE_VETTABLE; - public: + struct UserProcessSignal { + UIntPtr SignalArg{0}; + ProcessStatusKind Status{ProcessStatusKind::kKilled}; + UIntPtr SignalID{0}; + }; + Char Name[kSchedNameLen] = {"UserProcess"}; ProcessSubsystem SubSystem{ProcessSubsystem::kProcessSubsystemUser}; User* Owner{nullptr}; @@ -52,28 +57,21 @@ class UserProcess final { AffinityKind Affinity{AffinityKind::kStandard}; ProcessStatusKind Status{ProcessStatusKind::kKilled}; UInt8 StackReserve[kSchedMaxStackSz]; - ProcessImage Image{}; SizeT StackSize{kSchedMaxStackSz}; IDylibObject* DylibDelegate{nullptr}; SizeT MemoryCursor{0UL}; SizeT MemoryLimit{kSchedMaxMemoryLimit}; SizeT UsedMemory{0UL}; + UserProcessSignal Signal; + ProcessImage Image; - struct UserProcessSignal { - UIntPtr SignalArg{0}; - ProcessStatusKind Status{ProcessStatusKind::kKilled}; - UIntPtr SignalID{0}; - }; - - UserProcessSignal Signal; - ProcessFileTree<VoidPtr>* FileTree{nullptr}; - ProcessHeapTree<VoidPtr>* HeapTree{nullptr}; - UserProcessTeam* ParentTeam; + private: + ProcessFileTree<Any>* FileTree{nullptr}; + ProcessHeapTree<Any>* HeapTree{nullptr}; + UserProcessTeam* Parent; public: - using VMReg = VoidPtr; - - VoidPtr VMRegister{0UL}; + Any VMRegister{0UL}; enum struct ExecutableKind { kInvalidExecutableKind, @@ -101,6 +99,12 @@ class UserProcess final { Void Crash(); /***********************************************************************************/ + ///! @brief Gets the parent team of the process. + ///! @return The parent team. + /***********************************************************************************/ + UserProcessTeam* GetParentTeam(); + + /***********************************************************************************/ ///! @brief Spawns a dynamic library handle if dylib. /***********************************************************************************/ Bool InitDylib(); @@ -149,11 +153,7 @@ class UserProcess final { /***********************************************************************************/ KPCError& GetLocalCode(); - const User* GetOwner(); - - const ProcessStatusKind& GetStatus(); - - const AffinityKind& GetAffinity(); + const Ref<User*> GetOwner(); private: KPCError LastExitCode{0}; @@ -163,6 +163,34 @@ class UserProcess final { friend UserProcessHelper; }; +inline bool operator<(UserProcess::ExecutableKind lhs, UserProcess::ExecutableKind rhs) { + Int32 lhs_int = static_cast<Int32>(lhs); + Int32 rhs_int = static_cast<Int32>(rhs); + + return lhs_int < rhs_int; +} + +inline bool operator>(UserProcess::ExecutableKind lhs, UserProcess::ExecutableKind rhs) { + Int32 lhs_int = static_cast<Int32>(lhs); + Int32 rhs_int = static_cast<Int32>(rhs); + + return lhs_int > rhs_int; +} + +inline bool operator<=(UserProcess::ExecutableKind lhs, UserProcess::ExecutableKind rhs) { + Int32 lhs_int = static_cast<Int>(lhs); + Int32 rhs_int = static_cast<Int32>(rhs); + + return lhs_int <= rhs_int; +} + +inline bool operator>=(UserProcess::ExecutableKind lhs, UserProcess::ExecutableKind rhs) { + Int32 lhs_int = static_cast<Int32>(lhs); + Int32 rhs_int = static_cast<Int32>(rhs); + + return lhs_int >= rhs_int; +} + using UserProcessArray = Array<UserProcess, kSchedProcessLimitPerTeam>; using UserProcessRef = Ref<UserProcess>; @@ -217,8 +245,8 @@ class UserProcessScheduler final : public ISchedulable { Bool HasMP() override; public: - Ref<UserProcess> TheCurrentProcess(); - SizeT Run(); + UserProcess& TheCurrentProcess(); + SizeT Run(); public: STATIC UserProcessScheduler& The(); diff --git a/src/kernel/KernelKit/UserProcessScheduler.inl b/src/kernel/KernelKit/UserProcessScheduler.inl index 625cadc8..e9cf3135 100644 --- a/src/kernel/KernelKit/UserProcessScheduler.inl +++ b/src/kernel/KernelKit/UserProcessScheduler.inl @@ -20,16 +20,19 @@ namespace Kernel { /** @brief Free pointer/file from usage. */ /***********************************************************************************/ -template <typename T> +template <class T> BOOL UserProcess::Delete(ErrorOr<T*> ptr) { - if (!ptr) return No; + if (!ptr) { + kout << "UserProcess: Ptr is nils.\r"; + return No; + } if (!this->HeapTree) { kout << "UserProcess: Heap is empty.\r"; return No; } - ProcessHeapTree<VoidPtr>* entry = this->HeapTree; + ProcessHeapTree<Any>* entry = this->HeapTree; while (entry != nullptr) { if (entry->Entry == ptr.Leak().Leak()) { @@ -51,8 +54,9 @@ BOOL UserProcess::Delete(ErrorOr<T*> ptr) { entry = entry->Next; } - kout << "UserProcess: Trying to free a pointer which doesn't exist.\r"; + (Void)(kout << "UserProcess: Pointer not found in heap." << kendl); + // crash. this->Crash(); return No; diff --git a/src/kernel/src/CodeMgr.cc b/src/kernel/src/CodeMgr.cc index 5bf1bbea..21ca4c11 100644 --- a/src/kernel/src/CodeMgr.cc +++ b/src/kernel/src/CodeMgr.cc @@ -12,7 +12,8 @@ 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 The team's process id. +/// @param kid the Kernel ID of the new task. +/// @return The process started or not. BOOL rtl_create_kernel_task(HAL::StackFramePtr task, const KID kid) { if (!kid || task == nullptr) return FALSE; return KernelTaskHelper::Add(task, kid); diff --git a/src/kernel/src/UserProcessScheduler.cc b/src/kernel/src/UserProcessScheduler.cc index 8be55fa0..fb2cd954 100644 --- a/src/kernel/src/UserProcessScheduler.cc +++ b/src/kernel/src/UserProcessScheduler.cc @@ -200,23 +200,13 @@ const Char* UserProcess::GetName() { /// @brief Gets the owner of the process. /***********************************************************************************/ -const User* UserProcess::GetOwner() { - return this->Owner; +const Ref<User*> UserProcess::GetOwner() { + return {this->Owner}; } -/// @brief UserProcess status getter. -const ProcessStatusKind& UserProcess::GetStatus() { - return this->Status; -} - -/***********************************************************************************/ -/** -@brief Affinity is the time slot allowed for the process. -*/ -/***********************************************************************************/ - -const AffinityKind& UserProcess::GetAffinity() { - return this->Affinity; +/// @brief Parent team getter. +UserProcessTeam* UserProcess::GetParentTeam() { + return this->Parent; } /***********************************************************************************/ @@ -277,13 +267,11 @@ Void UserProcess::Exit(const Int32& exit_code) { //! Delete image if not done already. if (this->Image.fCode && mm_is_valid_ptr(this->Image.fCode)) mm_free_ptr(this->Image.fCode); - //! Delete blob too. if (this->Image.fBlob && mm_is_valid_ptr(this->Image.fBlob)) mm_free_ptr(this->Image.fBlob); //! Delete stack frame. - if (this->StackFrame && mm_is_valid_ptr(this->StackFrame)) - mm_free_ptr((VoidPtr) this->StackFrame); + if (this->StackFrame && mm_is_valid_ptr(this->StackFrame)) mm_free_ptr((VoidPtr) this->StackFrame); //! Avoid use after free. this->Image.fBlob = nullptr; @@ -305,7 +293,7 @@ Void UserProcess::Exit(const Int32& exit_code) { this->ProcessId = 0UL; this->Status = ProcessStatusKind::kFinished; - --this->ParentTeam->mProcessCur; + --this->Parent->mProcessCur; } /***********************************************************************************/ @@ -394,7 +382,7 @@ ProcessID UserProcessScheduler::Spawn(const Char* name, VoidPtr code, VoidPtr im process.StackFrame->IP = reinterpret_cast<UIntPtr>(code); process.StackFrame->SP = reinterpret_cast<UIntPtr>(&process.StackReserve[0] + process.StackSize); -#ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ +#if defined(__NE_VIRTUAL_MEMORY_SUPPORT__) HAL::mm_map_page((VoidPtr) process.StackFrame->IP, (VoidPtr) HAL::mm_get_page_addr((VoidPtr) process.StackFrame->IP), HAL::kMMFlagsUser | HAL::kMMFlagsPresent); @@ -407,7 +395,7 @@ ProcessID UserProcessScheduler::Spawn(const Char* name, VoidPtr code, VoidPtr im rt_set_memory(process.StackReserve, 0, process.StackSize); - process.ParentTeam = &mTeam; + process.Parent = &mTeam; process.ProcessId = pid; process.Status = ProcessStatusKind::kRunning; @@ -426,7 +414,8 @@ ProcessID UserProcessScheduler::Spawn(const Char* name, VoidPtr code, VoidPtr im /// @todo File Tree allocation and dispose methods (amlal) } - (Void)(kout << "ProcessID: " << number(process.ProcessId) << kendl); + (Void)(kout << "ProcessCur: " << number(this->mTeam.mProcessCur) << kendl); + (Void)(kout << "ProcessID: " << number(pid) << kendl); (Void)(kout << "ProcesName: " << process.Name << kendl); return pid; @@ -551,8 +540,8 @@ BOOL UserProcessScheduler::SwitchTeam(UserProcessTeam& team) { /// @brief Gets current running process. /// @return -Ref<UserProcess> UserProcessScheduler::TheCurrentProcess() { - return mTeam.AsRef(); +UserProcess& UserProcessScheduler::TheCurrentProcess() { + return mTeam.mCurrentProcess.Leak(); } /// @brief Current proccess id getter. @@ -562,7 +551,7 @@ ErrorOr<ProcessID> UserProcessHelper::TheCurrentPID() { return ErrorOr<ProcessID>{-kErrorProcessFault}; kout << "UserProcessHelper::TheCurrentPID: Leaking ProcessId...\r"; - return ErrorOr<ProcessID>{UserProcessScheduler::The().TheCurrentProcess().Leak().ProcessId}; + return ErrorOr<ProcessID>{UserProcessScheduler::The().TheCurrentProcess().ProcessId}; } /// @brief Check if process can be schedulded. diff --git a/src/kernel/src/UserProcessTeam.cc b/src/kernel/src/UserProcessTeam.cc index eb6063da..de265ce1 100644 --- a/src/kernel/src/UserProcessTeam.cc +++ b/src/kernel/src/UserProcessTeam.cc @@ -12,7 +12,7 @@ UserProcessTeam::UserProcessTeam() { this->mProcessList[i].RTime = 0; this->mProcessList[i].UTime = 0; this->mProcessList[i].Status = ProcessStatusKind::kKilled; - this->mProcessList[i].ParentTeam = this; + *this->mProcessList[i].GetParentTeam() = *this; } this->mProcessCur = 0UL; |
