From def46ab40f8feffa167ab9c6e893dd6157cd57a2 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 11 Dec 2025 13:08:32 +0100 Subject: chore: refactor: codebase improvements and more usage of Ref<>. Signed-off-by: Amlal El Mahrouss --- src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc | 18 +-- src/kernel/KernelKit/KPC.h | 2 +- src/kernel/KernelKit/UserProcessScheduler.h | 8 +- src/kernel/KernelKit/ZXD.h | 14 +-- src/kernel/NeKit/Vettable.h | 2 +- src/kernel/src/PE32CodeMgr.cc | 4 +- src/kernel/src/PEFCodeMgr.cc | 4 +- src/kernel/src/ThreadLocalStorage.cc | 4 +- src/kernel/src/Timer.cc | 4 +- src/kernel/src/UserMgr+User.cc | 131 +++++++++++++++++++++ src/kernel/src/UserMgr.cc | 131 --------------------- src/kernel/src/UserProcessScheduler.cc | 8 +- src/kernel/src/UserProcessTeam.cc | 2 +- 13 files changed, 166 insertions(+), 166 deletions(-) create mode 100644 src/kernel/src/UserMgr+User.cc delete mode 100644 src/kernel/src/UserMgr.cc (limited to 'src/kernel') diff --git a/src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc b/src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc index 3745fe3c..ae36746e 100644 --- a/src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc +++ b/src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cc @@ -9,16 +9,16 @@ #include #include #include +#include EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip); - EXTERN_C Kernel::UIntPtr kApicBaseAddress; -STATIC BOOL kIsRunning = NO; +static bool kIsRunning = NO; /// @brief Notify APIC and PIC that we're done with the interrupt. /// @note -STATIC void hal_idt_send_eoi(UInt8 vector) { +static void hal_idt_send_eoi(UInt8 vector) { ((volatile UInt32*) kApicBaseAddress)[0xB0 / 4] = 0; if (vector >= kPICCommand && vector <= 0x2F) { @@ -32,7 +32,7 @@ STATIC void hal_idt_send_eoi(UInt8 vector) { /// @brief Handle GPF fault. /// @param rsp EXTERN_C Kernel::Void idt_handle_gpf(Kernel::UIntPtr rsp) { - auto& process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); + auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); process.Leak().Crash(); hal_idt_send_eoi(13); @@ -45,7 +45,7 @@ EXTERN_C Kernel::Void idt_handle_gpf(Kernel::UIntPtr rsp) { /// @brief Handle page fault. /// @param rsp EXTERN_C void idt_handle_pf(Kernel::UIntPtr rsp) { - auto& process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); + auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); process.Leak().Crash(); hal_idt_send_eoi(14); @@ -73,7 +73,7 @@ EXTERN_C void idt_handle_scheduler(Kernel::UIntPtr rsp) { /// @brief Handle math fault. /// @param rsp EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp) { - auto& process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); + auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); process.Leak().Crash(); hal_idt_send_eoi(8); @@ -87,7 +87,7 @@ EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp) { /// @brief Handle any generic fault. /// @param rsp EXTERN_C void idt_handle_generic(Kernel::UIntPtr rsp) { - auto& process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); + auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); process.Leak().Crash(); hal_idt_send_eoi(30); @@ -103,7 +103,7 @@ EXTERN_C void idt_handle_generic(Kernel::UIntPtr rsp) { } EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip) { - auto& process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); + auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); hal_idt_send_eoi(3); @@ -118,7 +118,7 @@ EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip) { /// @brief Handle #UD fault. /// @param rsp EXTERN_C void idt_handle_ud(Kernel::UIntPtr rsp) { - auto& process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); + auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess(); process.Leak().Crash(); hal_idt_send_eoi(6); diff --git a/src/kernel/KernelKit/KPC.h b/src/kernel/KernelKit/KPC.h index 8c67d6f1..4809f19f 100644 --- a/src/kernel/KernelKit/KPC.h +++ b/src/kernel/KernelKit/KPC.h @@ -25,7 +25,7 @@ #define err_global_get() (Kernel::kErrorLocalNumber) namespace Kernel { -using ErrorT = Int32; +using ErrorT = Int32; using KPCError = ErrorT; inline KPCError kErrorLocalNumber = 0UL; diff --git a/src/kernel/KernelKit/UserProcessScheduler.h b/src/kernel/KernelKit/UserProcessScheduler.h index 6caf3167..8b3c63ff 100644 --- a/src/kernel/KernelKit/UserProcessScheduler.h +++ b/src/kernel/KernelKit/UserProcessScheduler.h @@ -200,8 +200,8 @@ class UserProcessScheduler final : public ISchedulable { bool operator!(); public: - UserProcessTeam& TheCurrentTeam(); - BOOL SwitchTeam(UserProcessTeam& team); + Ref TheCurrentTeam(); + BOOL SwitchTeam(UserProcessTeam& team); public: ProcessID Spawn(const Char* name, VoidPtr code, VoidPtr image); @@ -212,8 +212,8 @@ class UserProcessScheduler final : public ISchedulable { Bool HasMP() override; public: - UserProcessRef& TheCurrentProcess(); - SizeT Run(); + Ref TheCurrentProcess(); + SizeT Run(); public: STATIC UserProcessScheduler& The(); diff --git a/src/kernel/KernelKit/ZXD.h b/src/kernel/KernelKit/ZXD.h index 5bb4665b..705a50d4 100644 --- a/src/kernel/KernelKit/ZXD.h +++ b/src/kernel/KernelKit/ZXD.h @@ -12,10 +12,10 @@ #define kZXDVersion (0x0001) namespace Kernel { -struct ZXD_EXEC_HEADER; -struct ZXD_STUB_HEADER; +struct ZxdExec; +struct ZxdStub; -enum ZXD_FLAGS { +enum struct ZxdFlags { kZXDFlagsInvalid = 0, kZXDFlagsDriver = 120, kZXDFlagsBoot, @@ -25,7 +25,7 @@ enum ZXD_FLAGS { /// @brief ZXD executable header /// @details This header is used to identify ZXD executable files. -struct PACKED ZXD_EXEC_HEADER final { +struct PACKED ZxdExec final { UInt32 fMagic; UInt32 fVersion; UInt32 fFlags; @@ -44,12 +44,12 @@ struct PACKED ZXD_EXEC_HEADER final { /// @brief ZXD stub header /// @details This header is used to identify ZXD stub files. It contains the size of the stub, the /// offset of the stub, and the CRC32 checksum of the stub. -struct PACKED ZXD_STUB_HEADER final { +struct PACKED ZxdStub final { UInt32 fStubSize; UInt32 fStubOffset; UInt32 fStubCRC32; }; -using ZXD_EXEC_HEADER_PTR = ZXD_EXEC_HEADER*; -using ZXD_STUB_HEADER_PTR = ZXD_STUB_HEADER*; +using ZxdExecPtr = ZxdExec*; +using ZxdStubPtr = ZxdStub*; } // namespace Kernel diff --git a/src/kernel/NeKit/Vettable.h b/src/kernel/NeKit/Vettable.h index d2479bf0..52528847 100644 --- a/src/kernel/NeKit/Vettable.h +++ b/src/kernel/NeKit/Vettable.h @@ -56,4 +56,4 @@ concept IsNotVettable = requires(OnFallback fallback) { }; } // namespace Kernel -#endif // !__NE_KIT_VETTABLE_H__ \ No newline at end of file +#endif // !__NE_KIT_VETTABLE_H__ \ No newline at end of file diff --git a/src/kernel/src/PE32CodeMgr.cc b/src/kernel/src/PE32CodeMgr.cc index 1cba2ac0..1aef8b0d 100644 --- a/src/kernel/src/PE32CodeMgr.cc +++ b/src/kernel/src/PE32CodeMgr.cc @@ -244,8 +244,8 @@ namespace Utils { *(volatile UIntPtr*) stacksym.Leak().Leak() = kSchedMaxStackSz; } - UserProcessScheduler::The().TheCurrentTeam().AsArray()[id].Kind = process_kind; - UserProcessScheduler::The().TheCurrentTeam().AsArray()[id].StackSize = + UserProcessScheduler::The().TheCurrentTeam().Leak().AsArray()[id].Kind = process_kind; + UserProcessScheduler::The().TheCurrentTeam().Leak().AsArray()[id].StackSize = *(UIntPtr*) stacksym.Leak().Leak(); mm_free_ptr(stacksym.Leak().Leak()); diff --git a/src/kernel/src/PEFCodeMgr.cc b/src/kernel/src/PEFCodeMgr.cc index 4b914566..25d270b8 100644 --- a/src/kernel/src/PEFCodeMgr.cc +++ b/src/kernel/src/PEFCodeMgr.cc @@ -322,8 +322,8 @@ namespace Utils { *(volatile UIntPtr*) stacksym.Leak().Leak() = kSchedMaxStackSz; } - UserProcessScheduler::The().TheCurrentTeam().AsArray()[id].Kind = process_kind; - UserProcessScheduler::The().TheCurrentTeam().AsArray()[id].StackSize = + UserProcessScheduler::The().TheCurrentTeam().Leak().AsArray()[id].Kind = process_kind; + UserProcessScheduler::The().TheCurrentTeam().Leak().AsArray()[id].StackSize = *(UIntPtr*) stacksym.Leak().Leak(); mm_free_ptr(stacksym.Leak().Leak()); diff --git a/src/kernel/src/ThreadLocalStorage.cc b/src/kernel/src/ThreadLocalStorage.cc index 8e010b12..81675156 100644 --- a/src/kernel/src/ThreadLocalStorage.cc +++ b/src/kernel/src/ThreadLocalStorage.cc @@ -28,6 +28,7 @@ using namespace Kernel; */ Boolean tls_check_tib(THREAD_INFORMATION_BLOCK* tib_ptr) { + MUST_PASS(tib_ptr); if (!tib_ptr) return false; return tib_ptr->Cookie[kCookieMag0Idx] == kCookieMag0 && @@ -46,7 +47,6 @@ EXTERN_C Bool tls_check_syscall_impl(Kernel::VoidPtr tib_ptr) { return No; } - THREAD_INFORMATION_BLOCK* tib = reinterpret_cast(tib_ptr); - + THREAD_INFORMATION_BLOCK* tib = static_cast(tib_ptr); return tls_check_tib(tib); } diff --git a/src/kernel/src/Timer.cc b/src/kernel/src/Timer.cc index 6f8cfc5a..3e6ee305 100644 --- a/src/kernel/src/Timer.cc +++ b/src/kernel/src/Timer.cc @@ -11,9 +11,9 @@ ///! @brief Software Timer implementation ///! @author Amlal El Mahrouss (amlal@nekernel.org) -using namespace Kernel; - +namespace Kernel { /// @brief Unimplemented as it is an interface. BOOL ITimer::Wait() { return NO; } +} // namespace Kernel \ No newline at end of file diff --git a/src/kernel/src/UserMgr+User.cc b/src/kernel/src/UserMgr+User.cc new file mode 100644 index 00000000..52730201 --- /dev/null +++ b/src/kernel/src/UserMgr+User.cc @@ -0,0 +1,131 @@ +/* + * ======================================================== + * + * NeKernel + * Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. + * + * File: UserMgr.cc + * Purpose: User Manager, used to provide authentication and security. + * + * ======================================================== + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define kStdUserType (0xEE) +#define kSuperUserType (0xEF) + +/// @file UserMgr.cc +/// @brief Multi-user support. + +namespace Kernel { +namespace Detail { + //////////////////////////////////////////////////////////// + /// \brief Constructs a password by hashing the password. + /// \param password password to hash. + /// \return the hashed password + //////////////////////////////////////////////////////////// + STATIC UInt64 user_fnv_generator(const Char* password, User* user) { + if (!password || !user) return 0; + if (*password == 0) return 0; + + kout << "user_fnv_generator: Hashing user password...\r"; + + const UInt64 kFnvOffsetBasis = 0xcbf29ce484222325ULL; + const UInt64 kFnvPrime = 0x100000001b3ULL; + + UInt64 hash = kFnvOffsetBasis; + + while (*password) { + hash ^= (Char) (*password++); + hash *= kFnvPrime; + } + + kout << "user_fnv_generator: Hashed user password.\r"; + + return hash; + } +} // namespace Detail + +//////////////////////////////////////////////////////////// +/// @brief User ring constructor. +//////////////////////////////////////////////////////////// +User::User(const Int32& sel, const Char* user_name) : mUserRing((UserRingKind) sel) { + MUST_PASS(sel >= 0); + rt_copy_memory_safe((VoidPtr) user_name, this->mUserName, rt_string_len(user_name), + kMaxUserNameLen); +} + +//////////////////////////////////////////////////////////// +/// @brief User ring constructor. +//////////////////////////////////////////////////////////// +User::User(const UserRingKind& ring_kind, const Char* user_name) : mUserRing(ring_kind) { + rt_copy_memory_safe((VoidPtr) user_name, this->mUserName, rt_string_len(user_name), + kMaxUserNameLen); +} + +//////////////////////////////////////////////////////////// +/// @brief User destructor class. +//////////////////////////////////////////////////////////// +User::~User() = default; + +Bool User::Save(const UserPublicKey password) { + if (!password || *password == 0) return No; + + this->mUserFNV = Detail::user_fnv_generator(password, this); + + kout << "User::Save: Saved password successfully...\r"; + + return Yes; +} + +Bool User::Login(const UserPublicKey password) { + if (!password || !*password) return No; + + auto ret = this->mUserFNV == Detail::user_fnv_generator(password, this); + + // now check if the password matches. + kout << (ret ? "User::Login: Password matches.\r" : "User::Login: Password doesn't match.\r"); + return ret; +} + +Bool User::operator==(const User& lhs) { + return lhs.mUserRing == this->mUserRing; +} + +Bool User::operator!=(const User& lhs) { + return lhs.mUserRing != this->mUserRing; +} + +//////////////////////////////////////////////////////////// +/// @brief Returns the user's name. +//////////////////////////////////////////////////////////// + +Char* User::Name() { + return this->mUserName; +} + +//////////////////////////////////////////////////////////// +/// @brief Returns the user's ring. +/// @return The king of ring the user is attached to. +//////////////////////////////////////////////////////////// + +const UserRingKind& User::Ring() { + return this->mUserRing; +} + +Bool User::IsStdUser() { + return this->Ring() == UserRingKind::kRingStdUser; +} + +Bool User::IsSuperUser() { + return this->Ring() == UserRingKind::kRingSuperUser; +} +} // namespace Kernel diff --git a/src/kernel/src/UserMgr.cc b/src/kernel/src/UserMgr.cc deleted file mode 100644 index 52730201..00000000 --- a/src/kernel/src/UserMgr.cc +++ /dev/null @@ -1,131 +0,0 @@ -/* - * ======================================================== - * - * NeKernel - * Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - * - * File: UserMgr.cc - * Purpose: User Manager, used to provide authentication and security. - * - * ======================================================== - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#define kStdUserType (0xEE) -#define kSuperUserType (0xEF) - -/// @file UserMgr.cc -/// @brief Multi-user support. - -namespace Kernel { -namespace Detail { - //////////////////////////////////////////////////////////// - /// \brief Constructs a password by hashing the password. - /// \param password password to hash. - /// \return the hashed password - //////////////////////////////////////////////////////////// - STATIC UInt64 user_fnv_generator(const Char* password, User* user) { - if (!password || !user) return 0; - if (*password == 0) return 0; - - kout << "user_fnv_generator: Hashing user password...\r"; - - const UInt64 kFnvOffsetBasis = 0xcbf29ce484222325ULL; - const UInt64 kFnvPrime = 0x100000001b3ULL; - - UInt64 hash = kFnvOffsetBasis; - - while (*password) { - hash ^= (Char) (*password++); - hash *= kFnvPrime; - } - - kout << "user_fnv_generator: Hashed user password.\r"; - - return hash; - } -} // namespace Detail - -//////////////////////////////////////////////////////////// -/// @brief User ring constructor. -//////////////////////////////////////////////////////////// -User::User(const Int32& sel, const Char* user_name) : mUserRing((UserRingKind) sel) { - MUST_PASS(sel >= 0); - rt_copy_memory_safe((VoidPtr) user_name, this->mUserName, rt_string_len(user_name), - kMaxUserNameLen); -} - -//////////////////////////////////////////////////////////// -/// @brief User ring constructor. -//////////////////////////////////////////////////////////// -User::User(const UserRingKind& ring_kind, const Char* user_name) : mUserRing(ring_kind) { - rt_copy_memory_safe((VoidPtr) user_name, this->mUserName, rt_string_len(user_name), - kMaxUserNameLen); -} - -//////////////////////////////////////////////////////////// -/// @brief User destructor class. -//////////////////////////////////////////////////////////// -User::~User() = default; - -Bool User::Save(const UserPublicKey password) { - if (!password || *password == 0) return No; - - this->mUserFNV = Detail::user_fnv_generator(password, this); - - kout << "User::Save: Saved password successfully...\r"; - - return Yes; -} - -Bool User::Login(const UserPublicKey password) { - if (!password || !*password) return No; - - auto ret = this->mUserFNV == Detail::user_fnv_generator(password, this); - - // now check if the password matches. - kout << (ret ? "User::Login: Password matches.\r" : "User::Login: Password doesn't match.\r"); - return ret; -} - -Bool User::operator==(const User& lhs) { - return lhs.mUserRing == this->mUserRing; -} - -Bool User::operator!=(const User& lhs) { - return lhs.mUserRing != this->mUserRing; -} - -//////////////////////////////////////////////////////////// -/// @brief Returns the user's name. -//////////////////////////////////////////////////////////// - -Char* User::Name() { - return this->mUserName; -} - -//////////////////////////////////////////////////////////// -/// @brief Returns the user's ring. -/// @return The king of ring the user is attached to. -//////////////////////////////////////////////////////////// - -const UserRingKind& User::Ring() { - return this->mUserRing; -} - -Bool User::IsStdUser() { - return this->Ring() == UserRingKind::kRingStdUser; -} - -Bool User::IsSuperUser() { - return this->Ring() == UserRingKind::kRingSuperUser; -} -} // namespace Kernel diff --git a/src/kernel/src/UserProcessScheduler.cc b/src/kernel/src/UserProcessScheduler.cc index e1b47c59..97445251 100644 --- a/src/kernel/src/UserProcessScheduler.cc +++ b/src/kernel/src/UserProcessScheduler.cc @@ -541,8 +541,8 @@ SizeT UserProcessScheduler::Run() { /// @brief Gets the current scheduled team. /// @return -UserProcessTeam& UserProcessScheduler::TheCurrentTeam() { - return mTeam; +Ref UserProcessScheduler::TheCurrentTeam() { + return {mTeam}; } /***********************************************************************************/ @@ -562,7 +562,7 @@ BOOL UserProcessScheduler::SwitchTeam(UserProcessTeam& team) { /// @brief Gets current running process. /// @return -Ref& UserProcessScheduler::TheCurrentProcess() { +Ref UserProcessScheduler::TheCurrentProcess() { return mTeam.AsRef(); } @@ -649,7 +649,7 @@ Bool UserProcessHelper::Switch(HAL::StackFramePtr frame_ptr, ProcessID new_pid) UserProcessHelper::TheCurrentPID().Leak().Leak() = new_pid; HardwareThreadScheduler::The()[index].Leak()->fPTime = - UserProcessScheduler::The().TheCurrentTeam().AsArray()[new_pid].PTime; + UserProcessScheduler::The().TheCurrentTeam().Leak().AsArray()[new_pid].PTime; (Void)(kout << "AP_" << hex_number(index)); kout << " is now running a new task!\r"; diff --git a/src/kernel/src/UserProcessTeam.cc b/src/kernel/src/UserProcessTeam.cc index 4413ad09..99bc30f7 100644 --- a/src/kernel/src/UserProcessTeam.cc +++ b/src/kernel/src/UserProcessTeam.cc @@ -15,7 +15,7 @@ namespace Kernel { UserProcessTeam::UserProcessTeam() { for (SizeT i = 0U; i < this->mProcessList.Count(); ++i) { - this->mProcessList[i] = UserProcess(); + this->mProcessList[i] = UserProcess{}; this->mProcessList[i].PTime = 0; this->mProcessList[i].RTime = 0; this->mProcessList[i].UTime = 0; -- cgit v1.2.3