summaryrefslogtreecommitdiffhomepage
path: root/Private/Source
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-16 14:00:12 +0000
committerAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-16 14:00:12 +0000
commit82f0a2da77af7d79e53f5e65e46c527c1fe92765 (patch)
tree319ef7dd933367d64911c0ed9a53f91565002b00 /Private/Source
parent544d0cadfc371bcfb54d9f7ec15464bc6a79af21 (diff)
parent4c7aebf1b8964b99b89a25da0965b30fe6c7e6b3 (diff)
Merge branch 'HCR-18' into 'trunk'
HCR-18: First commit, bringing HCoreKrnl.exe into memory. See merge request mahrouss-logic/micro-kernel!6
Diffstat (limited to 'Private/Source')
-rw-r--r--Private/Source/Network/IP.cpp2
-rw-r--r--Private/Source/PEFSharedObjectRT.cxx23
-rw-r--r--Private/Source/ProcessManager.cxx43
-rw-r--r--Private/Source/String.cxx46
-rw-r--r--Private/Source/URL.cxx10
-rw-r--r--Private/Source/UserHeap.cxx8
-rw-r--r--Private/Source/Utils.cxx14
-rw-r--r--Private/Source/compile_flags.txt1
8 files changed, 76 insertions, 71 deletions
diff --git a/Private/Source/Network/IP.cpp b/Private/Source/Network/IP.cpp
index 38cd6008..b1b45521 100644
--- a/Private/Source/Network/IP.cpp
+++ b/Private/Source/Network/IP.cpp
@@ -82,7 +82,7 @@ ErrorOr<StringView> IPFactory::ToStringView(Ref<RawIPAddress> ipv4) {
bool IPFactory::IpCheckVersion4(const char* ip) {
int cnter = 0;
- for (Size base = 0; base < string_length(ip); ++base) {
+ for (Size base = 0; base < rt_string_len(ip); ++base) {
if (ip[base] == '.') {
cnter = 0;
} else {
diff --git a/Private/Source/PEFSharedObjectRT.cxx b/Private/Source/PEFSharedObjectRT.cxx
index c6bec404..60a3ea8f 100644
--- a/Private/Source/PEFSharedObjectRT.cxx
+++ b/Private/Source/PEFSharedObjectRT.cxx
@@ -20,6 +20,7 @@
01/02/24: Rework shared library ABI, except a __LibInit and __LibFini
(amlel)
+ 15/02/24: Breaking changes, changed the name of the routines. (amlel)
------------------------------------------- */
@@ -31,14 +32,13 @@ using namespace HCore;
/***********************************************************************************/
/***********************************************************************************/
-/* @brief Allocates a new library. */
+/* @brief Library runtime initializer. */
/***********************************************************************************/
-extern "C" SharedObject *__LibInit() {
- SharedObject *library = hcore_tls_new_class<SharedObject>();
+extern "C" SharedObjectPtr ke_library_init(void) {
+ SharedObjectPtr library = hcore_tls_new_class<SharedObject>();
if (!library) {
- kcout << "__LibInit: Out of Memory!\n";
ProcessManager::Shared().Leak().GetCurrent().Leak().Crash();
return nullptr;
@@ -47,7 +47,6 @@ extern "C" SharedObject *__LibInit() {
library->Mount(hcore_tls_new_class<SharedObject::SharedObjectTraits>());
if (!library->Get()) {
- kcout << "__LibInit: Out of Memory!\n";
ProcessManager::Shared().Leak().GetCurrent().Leak().Crash();
return nullptr;
@@ -57,34 +56,28 @@ extern "C" SharedObject *__LibInit() {
ProcessManager::Shared().Leak().GetCurrent().Leak().Image;
if (!library->Get()->fImageObject) {
- kcout << "__LibInit: Invalid image!\n";
ProcessManager::Shared().Leak().GetCurrent().Leak().Crash();
return nullptr;
}
library->Get()->fImageEntrypointOffset =
- library->Load<VoidPtr>(kPefStart, string_length(kPefStart, 0), kPefCode);
-
- kcout << "__LibInit: Task is successful!\n";
+ library->Load<VoidPtr>(kPefStart, rt_string_len(kPefStart, 0), kPefCode);
return library;
}
/***********************************************************************************/
-
-/***********************************************************************************/
-/* @brief Frees the library. */
+/* @brief Ends the library. */
/* @note Please check if the lib got freed! */
/* @param SharedObjectPtr the library to free. */
/***********************************************************************************/
-extern "C" Void __LibFini(SharedObjectPtr lib, bool *successful) {
+extern "C" Void ke_library_free(SharedObjectPtr lib, bool *successful) {
MUST_PASS(successful);
// sanity check (will also trigger a bug check)
if (lib == nullptr) {
- kcout << "__LibFini: Invalid image!\n";
*successful = false;
ProcessManager::Shared().Leak().GetCurrent().Leak().Crash();
}
@@ -94,8 +87,6 @@ extern "C" Void __LibFini(SharedObjectPtr lib, bool *successful) {
lib = nullptr;
- kcout << "__LibFini: Task is successful!\n";
-
*successful = true;
}
diff --git a/Private/Source/ProcessManager.cxx b/Private/Source/ProcessManager.cxx
index 82b07c42..ccf037ae 100644
--- a/Private/Source/ProcessManager.cxx
+++ b/Private/Source/ProcessManager.cxx
@@ -18,6 +18,8 @@
#include <NewKit/KernelHeap.hpp>
#include <NewKit/String.hpp>
+#include "NewKit/RuntimeCheck.hpp"
+
///! bugs = 0
/***********************************************************************************/
@@ -54,10 +56,9 @@ void Process::Wake(const bool should_wakeup) {
VoidPtr Process::New(const SizeT &sz) {
if (this->FreeMemory < 1) return nullptr;
- // RAM allocation
- if (this->PoolCursor) {
- VoidPtr ptr = this->PoolCursor;
- this->PoolCursor = (VoidPtr)((UIntPtr)this->PoolCursor + (sizeof(sz)));
+ if (this->HeapCursor) {
+ VoidPtr ptr = this->HeapCursor;
+ this->HeapCursor = (VoidPtr)((UIntPtr)this->HeapCursor + (sizeof(sz)));
++this->UsedMemory;
--this->FreeMemory;
@@ -86,13 +87,13 @@ bool rt_in_pool_region(VoidPtr pool_ptr, VoidPtr pool, const SizeT &sz) {
/* @brief free pointer from usage. */
Boolean Process::Delete(VoidPtr ptr, const SizeT &sz) {
- if (sz < 1 || this->PoolCursor == this->Pool) return false;
+ 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;
- if (rt_in_pool_region(ptr, this->PoolCursor, this->UsedMemory)) {
- this->PoolCursor = (VoidPtr)((UIntPtr)this->PoolCursor - (sizeof(sz)));
+ if (rt_in_pool_region(ptr, this->HeapCursor, this->UsedMemory)) {
+ this->HeapCursor = (VoidPtr)((UIntPtr)this->HeapCursor - (sizeof(sz)));
rt_zero_memory(ptr, sz);
++this->FreeMemory;
@@ -131,14 +132,13 @@ void Process::Exit(Int32 exit_code) {
kExitCode = exit_code;
- if (this->Ring != (Int32)ProcessSelector::kRingDriver &&
- this->Ring != (Int32)ProcessSelector::kRingKernel) {
- if (this->Pool) ke_free_heap(this->Pool);
+ if (this->Ring != (Int32)ProcessSelector::kRingDriver) {
+ if (this->HeapPtr) ke_free_heap(this->HeapPtr);
- this->Pool = nullptr;
- this->PoolCursor = nullptr;
+ this->HeapPtr = nullptr;
+ this->HeapCursor = nullptr;
- this->FreeMemory = kPoolMaxSz;
+ this->FreeMemory = 0UL; // TODO: fill available heap.
this->UsedMemory = 0UL;
}
@@ -155,11 +155,13 @@ void Process::Exit(Int32 exit_code) {
bool ProcessManager::Add(Ref<Process> &process) {
if (!process) return false;
+ if (process.Leak().Ring != (Int32)ProcessSelector::kRingKernel) return false;
+
kcout << "ProcessManager::Add(Ref<Process>& process)\r\n";
- process.Leak().Pool = ke_new_heap(kPoolUser | kPoolRw);
+ process.Leak().HeapPtr = ke_new_heap(kPoolUser | kPoolRw);
process.Leak().ProcessId = this->m_Headers.Count();
- process.Leak().PoolCursor = process.Leak().Pool;
+ process.Leak().HeapCursor = process.Leak().HeapPtr;
process.Leak().StackFrame = reinterpret_cast<HAL::StackFrame *>(
ke_new_ke_heap(sizeof(HAL::StackFrame), true, false));
@@ -172,6 +174,17 @@ bool ProcessManager::Add(Ref<Process> &process) {
this->m_Headers.Add(process);
+ if (!imageStart && process.Leak().Kind == Process::ExecutableType) {
+ process.Leak().Crash();
+ }
+
+ if (!imageStart && process.Leak().Kind == Process::DriverType) {
+ if (process.Leak().Ring == 3)
+ process.Leak().Crash();
+ else
+ ke_stop(RUNTIME_CHECK_PROCESS);
+ }
+
return true;
}
diff --git a/Private/Source/String.cxx b/Private/Source/String.cxx
index 045545cd..0e1b7f41 100644
--- a/Private/Source/String.cxx
+++ b/Private/Source/String.cxx
@@ -15,7 +15,7 @@ Char *StringView::Data() { return m_Data; }
const Char *StringView::CData() { return m_Data; }
-Size StringView::Length() const { return string_length(m_Data); }
+Size StringView::Length() const { return rt_string_len(m_Data); }
bool StringView::operator==(const StringView &rhs) const {
if (rhs.Length() != this->Length()) return false;
@@ -28,9 +28,9 @@ bool StringView::operator==(const StringView &rhs) const {
}
bool StringView::operator==(const Char *rhs) const {
- if (string_length(rhs) != this->Length()) return false;
+ if (rt_string_len(rhs) != this->Length()) return false;
- for (Size index = 0; index < string_length(rhs); ++index) {
+ for (Size index = 0; index < rt_string_len(rhs); ++index) {
if (rhs[index] != m_Data[index]) return false;
}
@@ -48,9 +48,9 @@ bool StringView::operator!=(const StringView &rhs) const {
}
bool StringView::operator!=(const Char *rhs) const {
- if (string_length(rhs) != this->Length()) return false;
+ if (rt_string_len(rhs) != this->Length()) return false;
- for (Size index = 0; index < string_length(rhs); ++index) {
+ for (Size index = 0; index < rt_string_len(rhs); ++index) {
if (rhs[index] == m_Data[index]) return false;
}
@@ -60,7 +60,7 @@ bool StringView::operator!=(const Char *rhs) const {
ErrorOr<StringView> StringBuilder::Construct(const Char *data) {
if (!data || *data == 0) return {};
- StringView view(string_length(data));
+ StringView view(rt_string_len(data));
rt_copy_memory(reinterpret_cast<voidPtr>(const_cast<Char *>(data)),
reinterpret_cast<voidPtr>(view.Data()), view.Length());
@@ -71,7 +71,7 @@ ErrorOr<StringView> StringBuilder::Construct(const Char *data) {
const char *StringBuilder::FromInt(const char *fmt, int i) {
if (!fmt) return ("-1");
- char *ret = (char *)__alloca(sizeof(char) * 8 + string_length(fmt));
+ char *ret = (char *)__alloca(sizeof(char) * 8 + rt_string_len(fmt));
if (!ret) return ("-1");
@@ -81,8 +81,8 @@ const char *StringBuilder::FromInt(const char *fmt, int i) {
return ("-1");
}
- const auto fmt_len = string_length(fmt);
- const auto res_len = string_length(result);
+ const auto fmt_len = rt_string_len(fmt);
+ const auto res_len = rt_string_len(result);
for (Size idx = 0; idx < fmt_len; ++idx) {
if (fmt[idx] == '%') {
@@ -106,12 +106,12 @@ const char *StringBuilder::FromBool(const char *fmt, bool i) {
if (!fmt) return ("?");
const char *boolean_expr = i ? "true" : "false";
- char *ret = (char *)__alloca((sizeof(char) * i) ? 4 : 5 + string_length(fmt));
+ char *ret = (char *)__alloca((sizeof(char) * i) ? 4 : 5 + rt_string_len(fmt));
if (!ret) return ("?");
- const auto fmt_len = string_length(fmt);
- const auto res_len = string_length(boolean_expr);
+ const auto fmt_len = rt_string_len(fmt);
+ const auto res_len = rt_string_len(boolean_expr);
for (Size idx = 0; idx < fmt_len; ++idx) {
if (fmt[idx] == '%') {
@@ -132,9 +132,9 @@ const char *StringBuilder::FromBool(const char *fmt, bool i) {
}
bool StringBuilder::Equals(const char *lhs, const char *rhs) {
- if (string_length(rhs) != string_length(lhs)) return false;
+ if (rt_string_len(rhs) != rt_string_len(lhs)) return false;
- for (Size index = 0; index < string_length(rhs); ++index) {
+ for (Size index = 0; index < rt_string_len(rhs); ++index) {
if (rhs[index] != lhs[index]) return false;
}
@@ -145,14 +145,14 @@ const char *StringBuilder::Format(const char *fmt, const char *fmt2) {
if (!fmt || !fmt2) return ("?");
char *ret =
- (char *)alloca(sizeof(char) * string_length(fmt2) + string_length(fmt2));
+ (char *)alloca(sizeof(char) * rt_string_len(fmt2) + rt_string_len(fmt2));
if (!ret) return ("?");
- for (Size idx = 0; idx < string_length(fmt); ++idx) {
+ for (Size idx = 0; idx < rt_string_len(fmt); ++idx) {
if (fmt[idx] == '%') {
Size result_cnt = idx;
- for (Size y_idx = 0; y_idx < string_length(fmt2); ++y_idx) {
+ for (Size y_idx = 0; y_idx < rt_string_len(fmt2); ++y_idx) {
ret[result_cnt] = fmt2[y_idx];
++result_cnt;
}
@@ -167,27 +167,27 @@ const char *StringBuilder::Format(const char *fmt, const char *fmt2) {
}
static void string_append(char *lhs, char *rhs, int cur) {
- if (lhs && rhs && cur < string_length(lhs)) {
- SizeT sz_rhs = string_length(rhs);
+ if (lhs && rhs && cur < rt_string_len(lhs)) {
+ SizeT sz_rhs = rt_string_len(rhs);
rt_copy_memory(rhs, lhs + cur, sz_rhs);
}
}
StringView &StringView::operator+=(const Char *rhs) {
- if (string_length(rhs) > string_length(this->m_Data)) return *this;
+ if (rt_string_len(rhs) > rt_string_len(this->m_Data)) return *this;
string_append(this->m_Data, const_cast<char *>(rhs), this->m_Cur);
- this->m_Cur += string_length(rhs);
+ this->m_Cur += rt_string_len(rhs);
return *this;
}
StringView &StringView::operator+=(const StringView &rhs) {
- if (string_length(rhs.m_Data) > string_length(this->m_Data)) return *this;
+ if (rt_string_len(rhs.m_Data) > rt_string_len(this->m_Data)) return *this;
string_append(this->m_Data, const_cast<char *>(rhs.m_Data), this->m_Cur);
- this->m_Cur += string_length(const_cast<char *>(rhs.m_Data));
+ this->m_Cur += rt_string_len(const_cast<char *>(rhs.m_Data));
return *this;
}
diff --git a/Private/Source/URL.cxx b/Private/Source/URL.cxx
index ab289a66..e471c0c1 100644
--- a/Private/Source/URL.cxx
+++ b/Private/Source/URL.cxx
@@ -32,19 +32,19 @@ constexpr const int kProtosCount = 8;
constexpr const int kRangeSz = 4096;
static ErrorOr<StringView> url_extract_location(const char *url) {
- if (!url || *url == 0 || string_length(url, kRangeSz) > kRangeSz)
+ if (!url || *url == 0 || rt_string_len(url, kRangeSz) > kRangeSz)
return ErrorOr<StringView>{-1};
- StringView view(string_length(url));
+ StringView view(rt_string_len(url));
SizeT i = 0;
bool scheme_found = false;
- for (; i < string_length(url); ++i) {
+ for (; i < rt_string_len(url); ++i) {
if (!scheme_found) {
for (int y = 0; kProtosCount; ++y) {
if (rt_string_in_string(view.CData(), kURLProtocols[y])) {
- i += string_length(kURLProtocols[y]) + kUrlOutSz;
+ i += rt_string_len(kURLProtocols[y]) + kUrlOutSz;
scheme_found = true;
break;
@@ -59,7 +59,7 @@ static ErrorOr<StringView> url_extract_location(const char *url) {
}
static ErrorOr<StringView> url_extract_protocol(const char *url) {
- if (!url || *url == 0 || string_length(url, kRangeSz) > kRangeSz)
+ if (!url || *url == 0 || rt_string_len(url, kRangeSz) > kRangeSz)
return ErrorOr<StringView>{-1};
ErrorOr<StringView> view{-1};
diff --git a/Private/Source/UserHeap.cxx b/Private/Source/UserHeap.cxx
index 5b086111..d3d60f6b 100644
--- a/Private/Source/UserHeap.cxx
+++ b/Private/Source/UserHeap.cxx
@@ -46,7 +46,7 @@ Boolean HeapManager::s_PoolsAreEnabled = true;
Array<Ref<PTEWrapper*>, kPoolMaxSz> HeapManager::s_Pool;
STATIC VoidPtr ke_find_unused_heap(Int flags);
-STATIC void ke_free_heap_internal(VoidPtr vaddr);
+STATIC Void ke_free_heap_internal(VoidPtr vaddr);
STATIC VoidPtr ke_make_heap(VoidPtr vaddr, Int flags);
STATIC Boolean ke_check_and_free_heap(const SizeT& index, VoidPtr ptr);
@@ -75,7 +75,7 @@ STATIC VoidPtr ke_make_heap(VoidPtr virtualAddress, Int flags) {
HeapHeader* poolHdr = reinterpret_cast<HeapHeader*>(virtualAddress);
if (!poolHdr->Free) {
- kcout << "[ke_make_heap] poolHdr->Free, Pool already exists\n";
+ kcout << "[ke_make_heap] poolHdr->Free, HeapPtr already exists\n";
return nullptr;
}
@@ -92,7 +92,7 @@ STATIC VoidPtr ke_make_heap(VoidPtr virtualAddress, Int flags) {
return nullptr;
}
-STATIC void ke_free_heap_internal(VoidPtr virtualAddress) {
+STATIC Void ke_free_heap_internal(VoidPtr virtualAddress) {
HeapHeader* poolHdr = reinterpret_cast<HeapHeader*>(
reinterpret_cast<UIntPtr>(virtualAddress) - sizeof(HeapHeader));
@@ -115,7 +115,7 @@ STATIC Boolean ke_check_and_free_heap(const SizeT& index, VoidPtr ptr) {
if (HeapManager::The()[index]) {
// ErrorOr<>::operator Boolean
/// if (address matches)
- /// -> Free Pool.
+ /// -> Free heap.
if (HeapManager::The()[index].Leak().Leak().Leak()->VirtualAddress() ==
(UIntPtr)ptr) {
HeapManager::Leak().Leak().FreePage(
diff --git a/Private/Source/Utils.cxx b/Private/Source/Utils.cxx
index 6072ccd3..01975192 100644
--- a/Private/Source/Utils.cxx
+++ b/Private/Source/Utils.cxx
@@ -10,7 +10,7 @@
#include <NewKit/Utils.hpp>
namespace HCore {
-Int string_compare(const Char *src, const Char *cmp, Size size) {
+Int rt_string_cmp(const Char *src, const Char *cmp, Size size) {
Int32 counter = 0;
for (Size index = 0; index < size; ++index) {
@@ -24,7 +24,7 @@ void rt_zero_memory(voidPtr pointer, Size len) {
rt_set_memory(pointer, 0, len);
}
-Size string_length(const Char *str, SizeT _len) {
+Size rt_string_len(const Char *str, SizeT _len) {
if (*str == '\0') return 0;
Size len{0};
@@ -39,7 +39,7 @@ Size string_length(const Char *str, SizeT _len) {
return len;
}
-Size string_length(const Char *str) {
+Size rt_string_len(const Char *str) {
if (*str == '\0') return 0;
Size len{0};
@@ -98,12 +98,12 @@ Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len) {
const Char *alloc_string(const Char *text) {
if (!text) return nullptr;
- const Char *string = new Char[string_length(text)];
+ const Char *string = new Char[rt_string_len(text)];
if (!string) return nullptr;
voidPtr vText = reinterpret_cast<voidPtr>(const_cast<char *>(text));
voidPtr vStr = reinterpret_cast<voidPtr>(const_cast<char *>(string));
- rt_copy_memory(vText, vStr, string_length(text));
+ rt_copy_memory(vText, vStr, rt_string_len(text));
return string;
}
@@ -144,8 +144,8 @@ Boolean is_space(Char chr) { return chr == ' '; }
Boolean is_newln(Char chr) { return chr == '\n'; }
voidPtr rt_string_in_string(const char *in, const char *needle) {
- for (size_t i = 0; i < string_length(in); ++i) {
- if (string_compare(in + i, needle, string_length(needle)) == 0)
+ for (size_t i = 0; i < rt_string_len(in); ++i) {
+ if (rt_string_cmp(in + i, needle, rt_string_len(needle)) == 0)
return reinterpret_cast<voidPtr>(const_cast<char *>(in + i));
}
diff --git a/Private/Source/compile_flags.txt b/Private/Source/compile_flags.txt
index 1b0ad789..b2809811 100644
--- a/Private/Source/compile_flags.txt
+++ b/Private/Source/compile_flags.txt
@@ -3,3 +3,4 @@
-std=c++20
-I../
-I$(HOME)/
+-D__USE_NEWFS__