summaryrefslogtreecommitdiffhomepage
path: root/dev/ZKA/Sources
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-09-04 22:20:17 +0200
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-09-04 22:20:17 +0200
commit1069f411d413e2185f6536b01b8993187056fcd8 (patch)
tree2b8bbfd6f2e497bd0e8a533f1fbf1ecd3fef29b6 /dev/ZKA/Sources
parent0aaf98a4875893914b3c75cb0eb1eb0971412377 (diff)
[ IMP ] BMP allocator needs more tweaking and fixes, to be usable.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/ZKA/Sources')
-rw-r--r--dev/ZKA/Sources/CodeMgr.cxx2
-rw-r--r--dev/ZKA/Sources/DLLMain.cxx2
-rw-r--r--dev/ZKA/Sources/Heap.cxx16
-rw-r--r--dev/ZKA/Sources/IDLLObject.cxx (renamed from dev/ZKA/Sources/DLLInterface.cxx)2
-rw-r--r--dev/ZKA/Sources/IPEFDLLObject.cxx (renamed from dev/ZKA/Sources/PEFDLLInterface.cxx)10
-rw-r--r--dev/ZKA/Sources/UserProcessScheduler.cxx169
6 files changed, 97 insertions, 104 deletions
diff --git a/dev/ZKA/Sources/CodeMgr.cxx b/dev/ZKA/Sources/CodeMgr.cxx
index df5b0917..c4ff2ee3 100644
--- a/dev/ZKA/Sources/CodeMgr.cxx
+++ b/dev/ZKA/Sources/CodeMgr.cxx
@@ -23,7 +23,7 @@ namespace Kernel
proc.SetImageStart(reinterpret_cast<VoidPtr>(main));
proc.Kind = UserProcess::kExeKind;
- proc.StackSize = kib_cast(8);
+ proc.StackSize = kib_cast(4);
rt_copy_memory((VoidPtr)processName, proc.Name, rt_string_len(processName));
diff --git a/dev/ZKA/Sources/DLLMain.cxx b/dev/ZKA/Sources/DLLMain.cxx
index 88887ec3..75fb24ff 100644
--- a/dev/ZKA/Sources/DLLMain.cxx
+++ b/dev/ZKA/Sources/DLLMain.cxx
@@ -189,6 +189,8 @@ EXTERN_C Kernel::Void ke_dll_entrypoint(Kernel::Void)
CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.exe: Starting ZKA System...", 20, 10, RGB(0, 0, 0));
+ Kernel::UserProcessHelper::Init();
+
Kernel::sched_execute_thread(HangCPU, "HANG TEST");
while (Yes)
diff --git a/dev/ZKA/Sources/Heap.cxx b/dev/ZKA/Sources/Heap.cxx
index 615f5664..0b54cbcd 100644
--- a/dev/ZKA/Sources/Heap.cxx
+++ b/dev/ZKA/Sources/Heap.cxx
@@ -63,26 +63,26 @@ namespace Kernel
Detail::HEAP_INFORMATION_BLOCK_PTR kLatestAllocation = nullptr;
- /// @brief Declare a new size for allocatedPtr.
- /// @param allocatedPtr the pointer.
+ /// @brief Declare a new size for ptr_heap.
+ /// @param ptr_heap the pointer.
/// @return
- voidPtr mm_realloc_ke_heap(voidPtr allocatedPtr, SizeT newSz)
+ voidPtr mm_realloc_ke_heap(voidPtr ptr_heap, SizeT new_sz)
{
- if (!allocatedPtr || newSz < 1)
+ if (!ptr_heap || new_sz < 1)
return nullptr;
Detail::HEAP_INFORMATION_BLOCK_PTR heap_blk =
reinterpret_cast<Detail::HEAP_INFORMATION_BLOCK_PTR>(
- (UIntPtr)allocatedPtr - sizeof(Detail::HEAP_INFORMATION_BLOCK));
+ (UIntPtr)ptr_heap - sizeof(Detail::HEAP_INFORMATION_BLOCK));
- heap_blk->fHeapSize = newSz;
+ heap_blk->fHeapSize = new_sz;
if (heap_blk->fCRC32 > 0)
{
- MUST_PASS(mm_protect_ke_heap(allocatedPtr));
+ MUST_PASS(mm_protect_ke_heap(ptr_heap));
}
- return allocatedPtr;
+ return ptr_heap;
}
/// @brief Allocate chunk of memory.
diff --git a/dev/ZKA/Sources/DLLInterface.cxx b/dev/ZKA/Sources/IDLLObject.cxx
index c18f2f00..b6b6a348 100644
--- a/dev/ZKA/Sources/DLLInterface.cxx
+++ b/dev/ZKA/Sources/IDLLObject.cxx
@@ -7,7 +7,7 @@
* ========================================================
*/
-#include <KernelKit/DLLInterface.hxx>
+#include <KernelKit/IDLLObject.hxx>
#include <KernelKit/DebugOutput.hxx>
#include <KernelKit/UserProcessScheduler.hxx>
diff --git a/dev/ZKA/Sources/PEFDLLInterface.cxx b/dev/ZKA/Sources/IPEFDLLObject.cxx
index 70c68783..913912a4 100644
--- a/dev/ZKA/Sources/PEFDLLInterface.cxx
+++ b/dev/ZKA/Sources/IPEFDLLObject.cxx
@@ -9,7 +9,7 @@
#include <KernelKit/DebugOutput.hxx>
#include <KernelKit/PEF.hxx>
-#include <KernelKit/PEFDLLInterface.hxx>
+#include <KernelKit/IPEFDLLObject.hxx>
#include <KernelKit/UserProcessScheduler.hxx>
#include <KernelKit/ThreadLocalStorage.hxx>
#include <NewKit/Defines.hxx>
@@ -37,9 +37,9 @@ using namespace Kernel;
/** @brief Library initializer. */
/***********************************************************************************/
-EXTERN_C DLLInterfacePtr rtl_init_shared_object(UserProcess* header)
+EXTERN_C IDLL rtl_init_shared_object(UserProcess* header)
{
- DLLInterfacePtr sharedObj = tls_new_class<PEFDLLInterface>();
+ IDLL sharedObj = tls_new_class<IPEFDLLObject>();
if (!sharedObj)
{
@@ -48,7 +48,7 @@ EXTERN_C DLLInterfacePtr rtl_init_shared_object(UserProcess* header)
return nullptr;
}
- sharedObj->Mount(tls_new_class<PEFDLLInterface::DLL_TRAITS>());
+ sharedObj->Mount(tls_new_class<IPEFDLLObject::DLL_TRAITS>());
if (!sharedObj->Get())
{
@@ -80,7 +80,7 @@ EXTERN_C DLLInterfacePtr rtl_init_shared_object(UserProcess* header)
/** @param successful Reports if successful or not. */
/***********************************************************************************/
-EXTERN_C Void rtl_fini_shared_object(UserProcess* header, DLLInterfacePtr lib, Bool* successful)
+EXTERN_C Void rtl_fini_shared_object(UserProcess* header, IDLL lib, Bool* successful)
{
MUST_PASS(successful);
diff --git a/dev/ZKA/Sources/UserProcessScheduler.cxx b/dev/ZKA/Sources/UserProcessScheduler.cxx
index 48988e57..7c576226 100644
--- a/dev/ZKA/Sources/UserProcessScheduler.cxx
+++ b/dev/ZKA/Sources/UserProcessScheduler.cxx
@@ -6,11 +6,11 @@
/***********************************************************************************/
/// @file UserProcessScheduler.cxx
-/// @brief User UserProcess scheduler.
+/// @brief User Process scheduler.
/***********************************************************************************/
#include <KernelKit/UserProcessScheduler.hxx>
-#include <KernelKit/PEFDLLInterface.hxx>
+#include <KernelKit/IPEFDLLObject.hxx>
#include <KernelKit/MP.hxx>
#include <KernelKit/Heap.hxx>
#include <NewKit/String.hxx>
@@ -19,7 +19,7 @@
///! BUGS: 0
/***********************************************************************************/
-/* TODO: Document more the Kernel, sdk and kits. */
+/* TODO: Document the Kernel, SDK and kits. */
/***********************************************************************************/
namespace Kernel
@@ -53,7 +53,7 @@ namespace Kernel
if (this->Name == 0)
return;
- kcout << this->Name << ": crashed, ID = " << number(kErrorProcessFault) << endl;
+ kcout << this->Name << ": crashed, ID = " << number(kErrorProcessFault) << endl;
this->Exit(kErrorProcessFault);
}
@@ -83,27 +83,45 @@ namespace Kernel
/***********************************************************************************/
+ /** @brief Add pointer to entry. */
VoidPtr UserProcess::New(const SizeT& sz)
{
- if (this->HeapCursor)
+#ifdef __ZKA_AMD64__
+ auto pd = hal_read_cr3();
+ hal_write_cr3(this->MemoryPD);
+
+ auto ptr = mm_new_ke_heap(sz, Yes, Yes);
+
+ hal_write_cr3(reinterpret_cast<UIntPtr>(pd));
+#else
+ auto ptr = mm_new_ke_heap(sz, Yes, Yes);
+#endif
+
+ if (!this->MemoryEntryList)
{
- if (this->FreeMemory < 1)
- {
- ErrLocal() = kErrorHeapOutOfMemory;
+ this->MemoryEntryList = new UserProcess::PROCESS_MEMORY_ENTRY();
+ this->MemoryEntryList->MemoryEntry = ptr;
- /* We're going out of memory! crash... */
- this->Crash();
+ this->MemoryEntryList->MemoryPrev = nullptr;
+ this->MemoryEntryList->MemoryNext = nullptr;
- return nullptr;
- }
+ return ptr;
+ }
+ else
+ {
+ auto entry = this->MemoryEntryList;
- this->HeapCursor = reinterpret_cast<VoidPtr>((UIntPtr)this->HeapCursor + (sizeof(sz)));
- VoidPtr cursor = this->HeapCursor;
+ while (entry->MemoryNext)
+ {
+ if (entry->MemoryNext)
+ entry = entry->MemoryNext;
+ }
- ++this->UsedMemory;
- --this->FreeMemory;
+ entry->MemoryNext = new UserProcess::PROCESS_MEMORY_ENTRY();
+ entry->MemoryNext->MemoryEntry = ptr;
- return cursor;
+ entry->MemoryNext->MemoryPrev = entry;
+ entry->MemoryNext->MemoryNext = nullptr;
}
return nullptr;
@@ -111,39 +129,31 @@ namespace Kernel
/***********************************************************************************/
- /* @brief checks if runtime pointer is in region. */
- bool rt_is_in_pool(VoidPtr pool_ptr, VoidPtr pool, const SizeT& pool_ptr_cur_sz, const SizeT& pool_ptr_used_sz)
- {
- if (pool == nullptr ||
- pool_ptr == nullptr)
- return false;
-
- UIntPtr* uint_pool_ptr = (UIntPtr*)pool_ptr;
- UIntPtr* uint_pool = (UIntPtr*)pool;
-
- return (UIntPtr)&uint_pool > (UIntPtr)&uint_pool_ptr &&
- pool_ptr_cur_sz > pool_ptr_used_sz;
- }
-
- /* @brief free pointer from usage. */
+ /** @brief Free pointer from usage. */
Boolean UserProcess::Delete(VoidPtr ptr, const SizeT& sz)
{
- if (sz < 1 || this->HeapCursor == this->HeapPtr)
- return false;
-
- // also check for the amount of allocations we've done so far.
- if (this->UsedMemory < 1)
- return false;
+ auto entry = this->MemoryEntryList;
- if (rt_is_in_pool(ptr, this->HeapCursor, this->UsedMemory, this->FreeMemory))
+ while (entry->MemoryNext)
{
- this->HeapCursor = (VoidPtr)((UIntPtr)this->HeapCursor - (sizeof(sz)));
- rt_zero_memory(ptr, sz);
+ if (entry->MemoryEntry == ptr)
+ {
+#ifdef __ZKA_AMD64__
+ auto pd = hal_read_cr3();
+ hal_write_cr3(this->MemoryPD);
- ++this->FreeMemory;
- --this->UsedMemory;
+ bool ret = mm_delete_ke_heap(ptr);
+ hal_write_cr3(reinterpret_cast<UIntPtr>(pd));
- return true;
+ return ret;
+#else
+ bool ret = mm_delete_ke_heap(ptr);
+ return ret;
+#endif
+ }
+
+ if (entry->MemoryNext)
+ entry = entry->MemoryNext;
}
return false;
@@ -183,7 +193,7 @@ namespace Kernel
void UserProcess::Exit(const Int32& exit_code)
{
this->Status = ProcessStatusKind::kDead;
-
+
fLastExitCode = exit_code;
cLastExitCode = exit_code;
@@ -211,7 +221,8 @@ namespace Kernel
if (this->StackReserve)
delete[] this->StackReserve;
- cProcessScheduler->Remove(this->ProcessId);
+ if (this->ProcessId > 0)
+ UserProcessScheduler::The().Remove(this->ProcessId);
}
/// @brief Add process to list.
@@ -219,73 +230,50 @@ namespace Kernel
/// @return the process index inside the team.
SizeT UserProcessScheduler::Add(UserProcess& process)
{
- if (!process.Image)
- {
- return -kErrorInvalidData;
- }
+#ifdef __ZKA_AMD64__
+ process.MemoryPD = reinterpret_cast<UIntPtr>(hal_read_cr3());
+#endif // __ZKA_AMD64__
- kcout << "UserProcessScheduler: Adding process to team...\r";
+ process.StackFrame = (HAL::StackFrame*)process.New(sizeof(HAL::StackFrame));
- // Create heap according to type of process.
- if (process.Kind == UserProcess::kExeKind)
- {
- process.HeapPtr = mm_new_ke_heap(process.SizeMemory, true, true);
- }
- else if (process.Kind == UserProcess::kDLLKind)
- {
- process.DLLPtr = rtl_init_shared_object(&process);
- process.HeapPtr = mm_new_ke_heap(process.SizeMemory, true, true);
- }
- else
+ if (!process.StackFrame)
{
- // Something went wrong, do not continue, process may be incorrect.
- process.Crash();
return -kErrorProcessFault;
}
- process.StackFrame = new HAL::StackFrame();
-
- if (!process.StackFrame)
+ // Create heap according to type of process.
+ if (process.Kind == UserProcess::kDLLKind)
{
- process.Crash();
- return -kErrorProcessFault;
+ process.DLLPtr = rtl_init_shared_object(&process);
}
-
+
if (process.Image)
{
// get preferred stack size by app.
const auto cMaxStackSize = process.StackSize;
-
- process.StackReserve = (UInt8*)mm_new_ke_heap(cMaxStackSize, Yes, Yes);
+ process.StackReserve = (UInt8*)process.New(sizeof(UInt8) * cMaxStackSize);
// if stack pointer isn't valid.
if (!process.StackReserve)
{
- process.StackReserve = (UInt8*)mm_new_ke_heap(kSchedMaxStackSz, Yes, Yes);
- kcout << "newoskrnl.exe: Use fallback reserve size.\r";
+ return -kErrorProcessFault;
}
}
else
{
if (process.Kind != UserProcess::kDLLKind)
{
- process.Crash();
return -kErrorProcessFault;
}
}
- process.Status = ProcessStatusKind::kStarting;
-
+ process.Status = ProcessStatusKind::kStarting;
process.ProcessId = mTeam.mProcessAmount;
++mTeam.mProcessAmount;
- process.HeapCursor = process.HeapPtr;
-
mTeam.AsArray()[process.ProcessId] = process;
- kcout << "UserProcessScheduler: Adding process to team [ OK ]...\r";
-
return process.ProcessId;
}
@@ -293,11 +281,6 @@ namespace Kernel
UserProcessScheduler& UserProcessScheduler::The()
{
- if (!cProcessScheduler)
- {
- cProcessScheduler = new UserProcessScheduler();
- }
-
MUST_PASS(cProcessScheduler);
return *cProcessScheduler;
}
@@ -341,7 +324,7 @@ namespace Kernel
if (UserProcessHelper::CanBeScheduled(process))
{
// set the current process.
- mTeam.AsRef() = process;
+ mTeam.AsRef() = mTeam.AsArray()[process.ProcessId];
process.PTime = static_cast<Int32>(process.Affinity);
@@ -349,7 +332,7 @@ namespace Kernel
// tell helper to find a core to schedule on.
if (!UserProcessHelper::Switch(process.Image, &process.StackReserve[process.StackSize], process.StackFrame,
- process.ProcessId))
+ process.ProcessId))
{
process.Crash();
continue;
@@ -393,6 +376,14 @@ namespace Kernel
return cProcessScheduler->CurrentProcess().Leak().ProcessId;
}
+ Void UserProcessHelper::Init()
+ {
+ if (!cProcessScheduler)
+ {
+ cProcessScheduler = new UserProcessScheduler();
+ }
+ }
+
/// @brief Check if process can be schedulded.
/// @param process the process reference.
/// @retval true can be schedulded.
@@ -408,7 +399,7 @@ namespace Kernel
if (auto start = process.DLLPtr->Load<VoidPtr>(kPefStart, rt_string_len(kPefStart), kPefCode);
start)
{
- process.Image = start;
+ process.Image = start;
}
}