From 4ce1fdaa5ad4252bff486a824f53cb93cc156925 Mon Sep 17 00:00:00 2001 From: Amlal EL Mahrouss Date: Thu, 1 Aug 2024 09:48:24 +0200 Subject: MHR-39: Implement entrypoint probe, can now find it. Next step is to map the sections into their own addresses. Signed-off-by: Amlal EL Mahrouss --- Kernel/Sources/FS/NewFS.cxx | 2 +- Kernel/Sources/MP.cxx | 256 ++++++++++++++++++++++++++++++++++++ Kernel/Sources/MPManager.cxx | 256 ------------------------------------ Kernel/Sources/Main.cxx | 4 +- Kernel/Sources/ProcessScheduler.cxx | 20 +-- Kernel/Sources/User.cxx | 12 +- 6 files changed, 275 insertions(+), 275 deletions(-) create mode 100644 Kernel/Sources/MP.cxx delete mode 100644 Kernel/Sources/MPManager.cxx (limited to 'Kernel/Sources') diff --git a/Kernel/Sources/FS/NewFS.cxx b/Kernel/Sources/FS/NewFS.cxx index ae2013cb..82759c72 100644 --- a/Kernel/Sources/FS/NewFS.cxx +++ b/Kernel/Sources/FS/NewFS.cxx @@ -305,7 +305,7 @@ _Output NFS_CATALOG_STRUCT* NewFSParser::CreateCatalog(_Input const char* name, if (flagsList & kNewFSCatalogKindMetaFile) { - if (UserView::The()->Current() != UserView::The()->fRootUser) + if (UserManager::The()->Current() != UserManager::The()->fRootUser) { delete catalogChild; return nullptr; diff --git a/Kernel/Sources/MP.cxx b/Kernel/Sources/MP.cxx new file mode 100644 index 00000000..b83f0817 --- /dev/null +++ b/Kernel/Sources/MP.cxx @@ -0,0 +1,256 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies + +------------------------------------------- */ + +#include +#include +#include +#include + +///! BUGS: 0 + +///! @file MP.cxx +///! @brief This file handles multi processing in the kernel. +///! @brief Multi processing is needed for multi-tasking operations. + +namespace Kernel +{ + STATIC Property cSMPCoreName; + + ///! A HardwareThread class takes care of it's owned hardware thread. + ///! It has a stack for it's core. + + ///! @brief C++ constructor. + HardwareThread::HardwareThread() = default; + + ///! @brief C++ destructor. + HardwareThread::~HardwareThread() = default; + + //! @brief returns the id of the thread. + const ThreadID& HardwareThread::ID() noexcept + { + return fID; + } + + //! @brief returns the kind of thread we have. + const ThreadKind& HardwareThread::Kind() noexcept + { + return fKind; + } + + //! @brief is the thread busy? + Bool HardwareThread::IsBusy() noexcept + { + return fBusy; + } + + /// @brief Get processor stack frame. + + HAL::StackFramePtr HardwareThread::StackFrame() noexcept + { + MUST_PASS(fStack); + return fStack; + } + + Void HardwareThread::Busy(const Bool busy) noexcept + { + fBusy = busy; + } + + HardwareThread::operator bool() + { + return fStack; + } + + /// @brief Wakeup the processor. + + Void HardwareThread::Wake(const bool wakeup) noexcept + { + fWakeup = wakeup; + + if (!fWakeup) + mp_hang_thread(fStack); + else + mp_wakeup_thread(fStack); + } + + EXTERN Bool rt_check_stack(HAL::StackFramePtr stackPtr); + + /// @brief Switch to hardware thread. + /// @param stack the new hardware thread. + /// @retval true stack was changed, code is running. + /// @retval false stack is invalid, previous code is running. + Bool HardwareThread::Switch(HAL::StackFramePtr stack) + { + if (!rt_check_stack(stack)) + { + /// provide 'nullptr' to free the stack frame. + if (stack == nullptr) + { + delete fStack; + fStack = nullptr; + + return true; + } + + return false; + } + + if (fStack) + { + delete fStack; + fStack = nullptr; + } + + fStack = stack; + + rt_do_context_switch(fStack); + + return true; + } + + ///! @brief Tells if processor is waked up. + bool HardwareThread::IsWakeup() noexcept + { + return fWakeup; + } + + //! @brief Constructor and destructor + + ///! @brief Default constructor. + MPCoreScheduler::MPCoreScheduler() + { + StringView strCoreName(512); + strCoreName += "\\Class\\Smp\\MPClass"; + + cSMPCoreName.GetKey() = strCoreName; + cSMPCoreName.GetValue() = (UIntPtr)this; + + kcout << "newoskrnl: initializing " << strCoreName.CData() << endl; + } + + ///! @brief Default destructor. + MPCoreScheduler::~MPCoreScheduler() = default; + + /// @brief Shared singleton function + Ref MPCoreScheduler::The() + { + static MPCoreScheduler manager; + return {manager}; + } + + /// @brief Get Stack Frame of Core + HAL::StackFramePtr MPCoreScheduler::Leak() noexcept + { + if (fThreadList[fCurrentThread].Leak() && + ProcessHelper::TheCurrentPID() == + fThreadList[fCurrentThread].Leak().Leak()->fSourcePID) + return fThreadList[fCurrentThread].Leak().Leak()->fStack; + + return nullptr; + } + + /// @brief Finds and switch to a free core. + bool MPCoreScheduler::Switch(HAL::StackFramePtr stack) + { + if (stack == nullptr) + return false; + + for (SizeT idx = 0; idx < cMaxHWThreads; ++idx) + { + // stack != nullptr -> if core is used, then continue. + if (!fThreadList[idx].Leak() || + !fThreadList[idx].Leak().Leak()->IsWakeup() || + fThreadList[idx].Leak().Leak()->IsBusy()) + continue; + + // to avoid any null deref. + if (!fThreadList[idx].Leak().Leak()->fStack) + continue; + if (fThreadList[idx].Leak().Leak()->fStack->SP == 0) + continue; + if (fThreadList[idx].Leak().Leak()->fStack->BP == 0) + continue; + + fThreadList[idx].Leak().Leak()->Busy(true); + + fThreadList[idx].Leak().Leak()->fID = idx; + + /// I figured out this: + /// Allocate stack + /// Set APIC base to stack + /// Do stuff and relocate stack based on this code. + /// - Amlel + rt_copy_memory(stack, fThreadList[idx].Leak().Leak()->fStack, + sizeof(HAL::StackFrame)); + + fThreadList[idx].Leak().Leak()->Switch(fThreadList[idx].Leak().Leak()->fStack); + + fThreadList[idx].Leak().Leak()->fSourcePID = ProcessHelper::TheCurrentPID(); + + fThreadList[idx].Leak().Leak()->Busy(false); + + return true; + } + + return false; + } + + /** + * Index Hardware thread + * @param idx the index + * @return the reference to the hardware thread. + */ + Ref MPCoreScheduler::operator[](const SizeT& idx) + { + if (idx == 0) + { + if (fThreadList[idx].Leak().Leak()->Kind() != kHartSystemReserved) + { + fThreadList[idx].Leak().Leak()->fKind = kHartBoot; + } + } + else if (idx >= cMaxHWThreads) + { + static HardwareThread* fakeThread = new HardwareThread(); + + if (!fakeThread) + { + fakeThread = new HardwareThread(); + } + + fakeThread->fKind = kInvalidHart; + + return {fakeThread}; + } + + return fThreadList[idx].Leak(); + } + + /** + * Check if thread pool isn't empty. + * @return + */ + MPCoreScheduler::operator bool() noexcept + { + return !fThreadList.Empty(); + } + + /** + * Reverse operator bool + * @return + */ + bool MPCoreScheduler::operator!() noexcept + { + return fThreadList.Empty(); + } + + /// @brief Returns the amount of core present. + /// @return the number of cores. + SizeT MPCoreScheduler::Count() noexcept + { + return fThreadList.Count(); + } +} // namespace Kernel diff --git a/Kernel/Sources/MPManager.cxx b/Kernel/Sources/MPManager.cxx deleted file mode 100644 index 3ad1e8aa..00000000 --- a/Kernel/Sources/MPManager.cxx +++ /dev/null @@ -1,256 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies - -------------------------------------------- */ - -#include -#include -#include -#include - -///! BUGS: 0 - -///! @file MPManager.cxx -///! @brief This file handles multi processing in Kernel. -///! @brief Multi processing is needed for multi-tasking operations. - -namespace Kernel -{ - STATIC Property cSMPCoreName; - - ///! A HardwareThread class takes care of it's owned hardware thread. - ///! It has a stack for it's core. - - ///! @brief C++ constructor. - HardwareThread::HardwareThread() = default; - - ///! @brief C++ destructor. - HardwareThread::~HardwareThread() = default; - - //! @brief returns the id of the thread. - const ThreadID& HardwareThread::ID() noexcept - { - return fID; - } - - //! @brief returns the kind of thread we have. - const ThreadKind& HardwareThread::Kind() noexcept - { - return fKind; - } - - //! @brief is the thread busy? - Bool HardwareThread::IsBusy() noexcept - { - return fBusy; - } - - /// @brief Get processor stack frame. - - HAL::StackFramePtr HardwareThread::StackFrame() noexcept - { - MUST_PASS(fStack); - return fStack; - } - - Void HardwareThread::Busy(const Bool busy) noexcept - { - fBusy = busy; - } - - HardwareThread::operator bool() - { - return fStack; - } - - /// @brief Wakeup the processor. - - Void HardwareThread::Wake(const bool wakeup) noexcept - { - fWakeup = wakeup; - - if (!fWakeup) - mp_hang_thread(fStack); - else - mp_wakeup_thread(fStack); - } - - EXTERN Bool rt_check_stack(HAL::StackFramePtr stackPtr); - - /// @brief Switch to hardware thread. - /// @param stack the new hardware thread. - /// @retval true stack was changed, code is running. - /// @retval false stack is invalid, previous code is running. - Bool HardwareThread::Switch(HAL::StackFramePtr stack) - { - if (!rt_check_stack(stack)) - { - /// provide 'nullptr' to free the stack frame. - if (stack == nullptr) - { - delete fStack; - fStack = nullptr; - - return true; - } - - return false; - } - - if (fStack) - { - delete fStack; - fStack = nullptr; - } - - fStack = stack; - - rt_do_context_switch(fStack); - - return true; - } - - ///! @brief Tells if processor is waked up. - bool HardwareThread::IsWakeup() noexcept - { - return fWakeup; - } - - //! @brief Constructor and destructor - - ///! @brief Default constructor. - MPManager::MPManager() - { - StringView strCoreName(512); - strCoreName += "\\Properties\\Smp\\SchedulerClass"; - - cSMPCoreName.GetKey() = strCoreName; - cSMPCoreName.GetValue() = (UIntPtr)this; - - kcout << "newoskrnl: initializing " << strCoreName.CData() << endl; - } - - ///! @brief Default destructor. - MPManager::~MPManager() = default; - - /// @brief Shared singleton function - Ref MPManager::The() - { - static MPManager manager; - return {manager}; - } - - /// @brief Get Stack Frame of Core - HAL::StackFramePtr MPManager::Leak() noexcept - { - if (fThreadList[fCurrentThread].Leak() && - ProcessHelper::TheCurrentPID() == - fThreadList[fCurrentThread].Leak().Leak()->fSourcePID) - return fThreadList[fCurrentThread].Leak().Leak()->fStack; - - return nullptr; - } - - /// @brief Finds and switch to a free core. - bool MPManager::Switch(HAL::StackFramePtr stack) - { - if (stack == nullptr) - return false; - - for (SizeT idx = 0; idx < kMaxHarts; ++idx) - { - // stack != nullptr -> if core is used, then continue. - if (!fThreadList[idx].Leak() || - !fThreadList[idx].Leak().Leak()->IsWakeup() || - fThreadList[idx].Leak().Leak()->IsBusy()) - continue; - - // to avoid any null deref. - if (!fThreadList[idx].Leak().Leak()->fStack) - continue; - if (fThreadList[idx].Leak().Leak()->fStack->SP == 0) - continue; - if (fThreadList[idx].Leak().Leak()->fStack->BP == 0) - continue; - - fThreadList[idx].Leak().Leak()->Busy(true); - - fThreadList[idx].Leak().Leak()->fID = idx; - - /// I figured out this: - /// Allocate stack - /// Set APIC base to stack - /// Do stuff and relocate stack based on this code. - /// - Amlel - rt_copy_memory(stack, fThreadList[idx].Leak().Leak()->fStack, - sizeof(HAL::StackFrame)); - - fThreadList[idx].Leak().Leak()->Switch(fThreadList[idx].Leak().Leak()->fStack); - - fThreadList[idx].Leak().Leak()->fSourcePID = ProcessHelper::TheCurrentPID(); - - fThreadList[idx].Leak().Leak()->Busy(false); - - return true; - } - - return false; - } - - /** - * Index Hardware thread - * @param idx the index - * @return the reference to the hardware thread. - */ - Ref MPManager::operator[](const SizeT& idx) - { - if (idx == 0) - { - if (fThreadList[idx].Leak().Leak()->Kind() != kHartSystemReserved) - { - fThreadList[idx].Leak().Leak()->fKind = kHartBoot; - } - } - else if (idx >= kMaxHarts) - { - static HardwareThread* fakeThread = new HardwareThread(); - - if (!fakeThread) - { - fakeThread = new HardwareThread(); - } - - fakeThread->fKind = kInvalidHart; - - return {fakeThread}; - } - - return fThreadList[idx].Leak(); - } - - /** - * Check if thread pool isn't empty. - * @return - */ - MPManager::operator bool() noexcept - { - return !fThreadList.Empty(); - } - - /** - * Reverse operator bool - * @return - */ - bool MPManager::operator!() noexcept - { - return fThreadList.Empty(); - } - - /// @brief Returns the amount of core present. - /// @return the number of cores. - SizeT MPManager::Count() noexcept - { - return fThreadList.Count(); - } -} // namespace Kernel diff --git a/Kernel/Sources/Main.cxx b/Kernel/Sources/Main.cxx index 145598ac..a23f82c1 100644 --- a/Kernel/Sources/Main.cxx +++ b/Kernel/Sources/Main.cxx @@ -142,8 +142,8 @@ namespace Kernel::Detail STATIC Kernel::Void ke_user_switch(Kernel::Void) { - Kernel::UserView::The()->fRootUser = new User(RingKind::kRingSuperUser, kSuperUser); - Kernel::UserView::The()->LogIn(Kernel::UserView::The()->fRootUser, "root"); + Kernel::UserManager::The()->fRootUser = new User(RingKind::kRingSuperUser, kSuperUser); + Kernel::UserManager::The()->LogIn(Kernel::UserManager::The()->fRootUser, "root"); Kernel::kcout << "newoskrnl: " << cKernelVersion.GetKey().CData() << ": " << Kernel::number(cKernelVersion.GetValue()) << Kernel::endl; } diff --git a/Kernel/Sources/ProcessScheduler.cxx b/Kernel/Sources/ProcessScheduler.cxx index 94ae5fad..84fc7a1f 100644 --- a/Kernel/Sources/ProcessScheduler.cxx +++ b/Kernel/Sources/ProcessScheduler.cxx @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include #include @@ -395,29 +395,29 @@ namespace Kernel if (!the_stack || new_pid < 0) return false; - for (SizeT index = 0UL; index < MPManager::The().Leak().Count(); ++index) + for (SizeT index = 0UL; index < MPCoreScheduler::The().Leak().Count(); ++index) { - if (MPManager::The().Leak()[index].Leak()->Kind() == kInvalidHart) + if (MPCoreScheduler::The().Leak()[index].Leak()->Kind() == kInvalidHart) continue; - if (MPManager::The().Leak()[index].Leak()->StackFrame() == the_stack) + if (MPCoreScheduler::The().Leak()[index].Leak()->StackFrame() == the_stack) { - MPManager::The().Leak()[index].Leak()->Busy(false); + MPCoreScheduler::The().Leak()[index].Leak()->Busy(false); continue; } - if (MPManager::The().Leak()[index].Leak()->IsBusy()) + if (MPCoreScheduler::The().Leak()[index].Leak()->IsBusy()) continue; - if (MPManager::The().Leak()[index].Leak()->Kind() != + if (MPCoreScheduler::The().Leak()[index].Leak()->Kind() != ThreadKind::kHartBoot && - MPManager::The().Leak()[index].Leak()->Kind() != + MPCoreScheduler::The().Leak()[index].Leak()->Kind() != ThreadKind::kHartSystemReserved) { - MPManager::The().Leak()[index].Leak()->Busy(true); + MPCoreScheduler::The().Leak()[index].Leak()->Busy(true); ProcessHelper::TheCurrentPID() = new_pid; - return MPManager::The().Leak()[index].Leak()->Switch(the_stack); + return MPCoreScheduler::The().Leak()[index].Leak()->Switch(the_stack); } } diff --git a/Kernel/Sources/User.cxx b/Kernel/Sources/User.cxx index dda3d016..e546dd81 100644 --- a/Kernel/Sources/User.cxx +++ b/Kernel/Sources/User.cxx @@ -69,17 +69,17 @@ namespace Kernel return this->Ring() == RingKind::kRingSuperUser; } - UserView* UserView::The() noexcept + UserManager* UserManager::The() noexcept { - UserView* view = nullptr; + UserManager* view = nullptr; if (!view) - view = new UserView(); + view = new UserManager(); return view; } - Bool UserView::LogIn(User* user, const Char* password) noexcept + Bool UserManager::LogIn(User* user, const Char* password) noexcept { if (!password || !user) @@ -143,12 +143,12 @@ namespace Kernel return true; } - User* UserView::Current() noexcept + User* UserManager::Current() noexcept { return fCurrentUser; } - Void UserView::LogOff() noexcept + Void UserManager::LogOff() noexcept { if (!fCurrentUser) return; -- cgit v1.2.3