From 81144dd05a7c01701c3bf7b04e345dccfef2bf82 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 5 Feb 2024 11:12:42 +0100 Subject: HCR-11: Kernel: Improvements and more. Bootloader: Now works on real hardware (previous commit.) Signed-off-by: Amlal El Mahrouss --- Private/KernelKit/FileManager.hpp | 12 +++-- Private/KernelKit/PEF.hpp | 4 +- Private/KernelKit/SMPManager.hpp | 8 +++- Private/KernelKit/ThreadLocalStorage.hxx | 12 ++--- Private/NewKit/OwnPtr.hpp | 77 +++++++++++++++++--------------- Private/Source/PEFCodeManager.cxx | 2 +- Private/Source/ProcessManager.cxx | 8 +++- Private/Source/SMPManager.cxx | 11 +++++ 8 files changed, 83 insertions(+), 51 deletions(-) diff --git a/Private/KernelKit/FileManager.hpp b/Private/KernelKit/FileManager.hpp index a52daa7d..d31cdf68 100644 --- a/Private/KernelKit/FileManager.hpp +++ b/Private/KernelKit/FileManager.hpp @@ -162,7 +162,7 @@ class NewFilesystemManager final : public IFilesystemManager { template class FileStream final { public: - explicit FileStream(const Encoding *path); + explicit FileStream(const Encoding *path, const Encoding *restrict_type); ~FileStream(); public: @@ -224,12 +224,18 @@ class FileStream final { const Char *fMime{"application-type/*"}; }; +#define kRestrictRW "r+" +#define kRestrictRWB "r+b" +#define kRestrictR "r" +#define kRestrictRB "rb" + using FileStreamUTF8 = FileStream; using FileStreamUTF16 = FileStream; template -FileStream::FileStream(const Encoding *path) - : fFile(Class::GetMounted()->Open(path, "r+")) {} +FileStream::FileStream(const Encoding *path, + const Encoding *restrict_type) + : fFile(Class::GetMounted()->Open(path, restrict_type)) {} template FileStream::~FileStream() = default; diff --git a/Private/KernelKit/PEF.hpp b/Private/KernelKit/PEF.hpp index 1add45f8..2ffc057f 100644 --- a/Private/KernelKit/PEF.hpp +++ b/Private/KernelKit/PEF.hpp @@ -37,8 +37,8 @@ enum { kPefArchIntel86S, kPefArchAMD64, kPefArchRISCV, - kPefArch64x0, /* 64x000. */ - kPefArch32x0, + kPefArch64x0, /* 64x0. ISA */ + kPefArch32x0, /* 32x0. ISA */ kPefArchCount = (kPefArch32x0 - kPefArchIntel86S) + 1, kPefArchInvalid = 0xFF, }; diff --git a/Private/KernelKit/SMPManager.hpp b/Private/KernelKit/SMPManager.hpp index 0a71a165..8cbe3259 100644 --- a/Private/KernelKit/SMPManager.hpp +++ b/Private/KernelKit/SMPManager.hpp @@ -14,6 +14,8 @@ #include #include +#include "NewKit/Defines.hpp" + #define kMaxHarts 8 namespace HCore { @@ -25,6 +27,8 @@ enum ThreadKind { kFallback, // fallback thread, cannot be used by user if not clear or used by // kernel. kBoot, // The core we booted from, the mama. + kInvalidThread, + kThreadCount, }; /// @@ -100,7 +104,9 @@ class SMPManager final { private: Array m_ThreadList; - ThreadID m_CurrentThread; + ThreadID m_CurrentThread{0}; + SizeT m_UsedThreads{0}; + SizeT m_MaxThreads{0}; }; // @brief wakes up thread. diff --git a/Private/KernelKit/ThreadLocalStorage.hxx b/Private/KernelKit/ThreadLocalStorage.hxx index 5db78ca3..e7c55ee7 100644 --- a/Private/KernelKit/ThreadLocalStorage.hxx +++ b/Private/KernelKit/ThreadLocalStorage.hxx @@ -27,16 +27,16 @@ bool hcore_tls_delete_ptr(T *ptr); template T *hcore_tls_new_class(Args &&...args); -typedef char rt_cookie_type[3]; +typedef HCore::Char rt_cookie_type[3]; /// @brief Thread Information Block for Local Storage. /// Located in GS on AMD64, Virtual Address 0x10000 (64x0, 32x0, ARM64) struct ThreadInformationBlock final { - HCore::Char Name[255]; // Module Name - HCore::UIntPtr StartCode; // Start Address - HCore::UIntPtr StartData; // Allocation Heap - HCore::UIntPtr StartStack; // Stack Pointer. - HCore::Int32 Arch; // Architecture and/or platform. + HCore::Char Name[kNameLen]; // Module Name + HCore::UIntPtr StartCode; // Start Address + HCore::UIntPtr StartData; // Allocation Heap + HCore::UIntPtr StartStack; // Stack Pointer. + HCore::Int32 Arch; // Architecture and/or platform. rt_cookie_type Cookie; // Not shown in public header, this is the way we tell // something went wrong. }; diff --git a/Private/NewKit/OwnPtr.hpp b/Private/NewKit/OwnPtr.hpp index 72a37244..e24eba3e 100644 --- a/Private/NewKit/OwnPtr.hpp +++ b/Private/NewKit/OwnPtr.hpp @@ -11,57 +11,60 @@ #pragma once #include -#include #include +#include -namespace HCore -{ -template class OwnPtr; +namespace HCore { +template +class OwnPtr; -template class NonNullRefPtr; +template +class NonNullRefPtr; -template class OwnPtr final -{ - public: - OwnPtr() {} - ~OwnPtr() { this->Delete(); } +template +class OwnPtr final { + public: + OwnPtr() {} + ~OwnPtr() { this->Delete(); } - OwnPtr &operator=(const OwnPtr &) = default; - OwnPtr(const OwnPtr &) = default; + OwnPtr &operator=(const OwnPtr &) = default; + OwnPtr(const OwnPtr &) = default; - public: - template bool New(Args &&...arg) - { - m_Cls = new T(arg...); - return m_Cls; + public: + template + bool New(Args &&...arg) { + if (m_Cls) { + return false; } - void Delete() - { - if (m_Cls) - delete m_Cls; + m_Cls = new T(arg...); + return m_Cls; + } - m_Cls = nullptr; - } + void Delete() { + if (m_Cls) delete m_Cls; + + m_Cls = nullptr; + } - T *operator->() const { return m_Cls; }; - T *Raw() { return m_Cls; } + T *operator->() const { return m_Cls; }; + T *Raw() { return m_Cls; } - Ref AsRef() { return Ref(m_Cls); } + Ref AsRef() { return Ref(m_Cls); } - operator bool() { return m_Cls; } - bool operator!() { return !m_Cls; } + operator bool() { return m_Cls; } + bool operator!() { return !m_Cls; } - private: - T *m_Cls; + private: + T *m_Cls; }; -template OwnPtr make_ptr(Args... args) -{ - OwnPtr ret; - ret.template New(forward(args)...); - MUST_PASS(ret); +template +OwnPtr make_ptr(Args... args) { + OwnPtr ret; + ret.template New(forward(args)...); + MUST_PASS(ret); - return ret; + return ret; } -} // namespace HCore +} // namespace HCore diff --git a/Private/Source/PEFCodeManager.cxx b/Private/Source/PEFCodeManager.cxx index efebef48..347f7f7a 100644 --- a/Private/Source/PEFCodeManager.cxx +++ b/Private/Source/PEFCodeManager.cxx @@ -43,7 +43,7 @@ PEFLoader::PEFLoader(const VoidPtr blob) : fCachedBlob(nullptr) { PEFLoader::PEFLoader(const char *path) : fCachedBlob(nullptr), fBad(false) { OwnPtr> file; - file.New(const_cast(path)); + file.New(const_cast(path), kRestrictRB); if (StringBuilder::Equals(file->MIME(), this->MIME())) { fPath = StringBuilder::Construct(path).Leak(); diff --git a/Private/Source/ProcessManager.cxx b/Private/Source/ProcessManager.cxx index 0c552b7f..ea9ba1f5 100644 --- a/Private/Source/ProcessManager.cxx +++ b/Private/Source/ProcessManager.cxx @@ -241,6 +241,9 @@ bool ProcessHelper::CanBeScheduled(Ref &process) { return process.Leak().PTime > static_cast(kMinMicroTime); } +/** + * @brief Scheduler spin code. + */ bool ProcessHelper::StartScheduling() { if (ProcessHelper::CanBeScheduled( ProcessManager::Shared().Leak().GetCurrent())) { @@ -256,7 +259,7 @@ bool ProcessHelper::StartScheduling() { SizeT ret = process_ref.Run(); kcout << StringBuilder::FromInt( - "ProcessHelper::StartScheduling() iterated over: % processes\r\n", ret); + "ProcessHelper::StartScheduling() Iterated over: % processes.\r\n", ret); return true; } @@ -265,6 +268,9 @@ bool ProcessHelper::Switch(HAL::StackFrame *the_stack, const PID &new_pid) { if (!the_stack || new_pid < 0) return false; for (SizeT index = 0UL; index < kMaxHarts; ++index) { + if (SMPManager::Shared().Leak()[index].Leak().Kind() == kInvalidThread) + continue; + if (SMPManager::Shared().Leak()[index].Leak().StackFrame() == the_stack) { SMPManager::Shared().Leak()[index].Leak().Busy(false); continue; diff --git a/Private/Source/SMPManager.cxx b/Private/Source/SMPManager.cxx index 87e82da1..d3c7a4c8 100644 --- a/Private/Source/SMPManager.cxx +++ b/Private/Source/SMPManager.cxx @@ -128,6 +128,17 @@ bool SMPManager::Switch(HAL::StackFrame* stack) { * @return the reference to the hardware thread. */ Ref SMPManager::operator[](const SizeT& idx) { + if (idx == 0) { + if (m_ThreadList[idx].Leak().Leak().Kind() != kSystemReserved) { + m_ThreadList[idx].Leak().Leak().m_Kind = kBoot; + } + } else if (idx >= kMaxHarts) { + HardwareThread fakeThread; + fakeThread.m_Kind = kInvalidThread; + + return {fakeThread}; + } + return m_ThreadList[idx].Leak(); } -- cgit v1.2.3