summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/KernelKit
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-12-28 15:17:29 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-12-28 15:17:29 +0100
commita70a1fc41e4420a3db2e64467cec349d794738ff (patch)
tree33a90e58acf97d197a63e4cc034af95396135aad /src/kernel/KernelKit
parent9012c6fb7c040be92aa8f950bad4f49c5be264d8 (diff)
feat: kernel: architectural and implementation improvements.v0.1.1
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'src/kernel/KernelKit')
-rw-r--r--src/kernel/KernelKit/KPC.h6
-rw-r--r--src/kernel/KernelKit/KernelTaskScheduler.h10
-rw-r--r--src/kernel/KernelKit/ThreadLocalStorage.inl4
-rw-r--r--src/kernel/KernelKit/UserMgr.h6
-rw-r--r--src/kernel/KernelKit/UserProcessScheduler.h74
-rw-r--r--src/kernel/KernelKit/UserProcessScheduler.inl12
6 files changed, 73 insertions, 39 deletions
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;