summaryrefslogtreecommitdiffhomepage
path: root/Private/HALKit
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@el-mahrouss-logic.com>2024-03-20 14:47:08 +0100
committerAmlal El Mahrouss <amlal@el-mahrouss-logic.com>2024-03-20 14:47:08 +0100
commit4ba02280f19b8a2beb1ad8445be7df6b7f9e1805 (patch)
tree4928e93b6463dcce6e0d74120882a6ec572bae5c /Private/HALKit
parent055a896406af227e03708fa20a728259cace704a (diff)
kernel: Reworking kernel to support virtual memory.
Signed-off-by: Amlal El Mahrouss <amlal@el-mahrouss-logic.com>
Diffstat (limited to 'Private/HALKit')
-rw-r--r--Private/HALKit/AMD64/HalControlRegister.s2
-rw-r--r--Private/HALKit/AMD64/HalKernelMain.cxx28
-rw-r--r--Private/HALKit/AMD64/HalPageAlloc.cpp41
-rw-r--r--Private/HALKit/AMD64/HalPageAlloc.hpp2
4 files changed, 30 insertions, 43 deletions
diff --git a/Private/HALKit/AMD64/HalControlRegister.s b/Private/HALKit/AMD64/HalControlRegister.s
index 1f1f4512..74dda36c 100644
--- a/Private/HALKit/AMD64/HalControlRegister.s
+++ b/Private/HALKit/AMD64/HalControlRegister.s
@@ -18,7 +18,7 @@ hal_flush_tlb:
ret
hal_read_cr3:
- movq %rax, %cr3
+ movq %cr3, %rax
ret
hal_read_cr0:
diff --git a/Private/HALKit/AMD64/HalKernelMain.cxx b/Private/HALKit/AMD64/HalKernelMain.cxx
index b82dccd7..2b420bb9 100644
--- a/Private/HALKit/AMD64/HalKernelMain.cxx
+++ b/Private/HALKit/AMD64/HalKernelMain.cxx
@@ -13,26 +13,13 @@
#include <KernelKit/Rsrc/Splash.rsrc>
#include <KernelKit/Rsrc/Util.hxx>
#include <NewKit/Json.hpp>
-#include <NewKit/KernelHeap.hpp>
-#include <NewKit/UserHeap.hpp>
+#include <KernelKit/KernelHeap.hpp>
+#include <KernelKit/UserHeap.hpp>
///! @brief Disk contains HCore files.
#define kInstalledMedia 0xDD
EXTERN_C HCore::VoidPtr kInterruptVectorTable[];
-EXTERN_C HCore::Void _hal_init_mouse();
-EXTERN_C HCore::Void _hal_mouse_handler();
-EXTERN_C HCore::Void _hal_mouse_draw();
-
-namespace Detail {
-STATIC HCore::Void ke_page_protect_nullptr(HCore::Void) {
- HCore::PTEWrapper wrapper(false, false, false, 0);
- HCore::PageManager pageManager;
- HCore::Ref<HCore::PageManager> refMan(pageManager);
-
- wrapper.FlushTLB(refMan);
-}
-} // namespace Detail
EXTERN_C void RuntimeMain(
HCore::HEL::HandoverInformationHeader* HandoverHeader) {
@@ -42,9 +29,11 @@ EXTERN_C void RuntimeMain(
kKernelVirtualSize = HandoverHeader->f_VirtualSize;
kKernelVirtualStart = HandoverHeader->f_VirtualStart;
- kKernelPhysicalSize = HandoverHeader->f_PhysicalSize - kPTEAlign;
+ kKernelPhysicalSize = HandoverHeader->f_PhysicalSize;
kKernelPhysicalStart = HandoverHeader->f_PhysicalStart;
+ hal_write_cr3((HCore::UIntPtr)kKernelVirtualStart);
+
STATIC HCore::HAL::Detail::HCoreGDT GDT = {
{0, 0, 0, 0x00, 0x00, 0}, // null entry
{0, 0, 0, 0x9a, 0xaf, 0}, // kernel code
@@ -84,7 +73,6 @@ EXTERN_C void RuntimeMain(
/// START POST
HCore::HAL::Detail::_ke_power_on_self_test();
- Detail::ke_page_protect_nullptr();
/// END POST
@@ -98,11 +86,5 @@ EXTERN_C void RuntimeMain(
/// TODO: Install hcore on host.
}
- _hal_init_mouse();
-
- while (1) {
- _hal_mouse_draw();
- }
-
HCore::ke_stop(RUNTIME_CHECK_BOOTSTRAP);
}
diff --git a/Private/HALKit/AMD64/HalPageAlloc.cpp b/Private/HALKit/AMD64/HalPageAlloc.cpp
index 867f39cb..91bde617 100644
--- a/Private/HALKit/AMD64/HalPageAlloc.cpp
+++ b/Private/HALKit/AMD64/HalPageAlloc.cpp
@@ -24,27 +24,37 @@ namespace HAL {
/// @param user user flag.
/// @return the page table of it.
STATIC auto hal_try_alloc_new_page(SizeT sz, Boolean rw, Boolean user)
- -> PageTable64 * {
- kAllocationInProgress = true;
+ -> VoidPtr {
MUST_PASS(sz > 0);
- PageTable64 *pte = reinterpret_cast<PageTable64 *>(kKernelVirtualStart);
+ kAllocationInProgress = true;
+ PDE* cr3 = (PDE*)hal_read_cr3();
+
+ kcout << "HCoreKrnl.exe: CR3: " << hex_number((UIntPtr)cr3) << endl;
- pte->Rw = rw;
- pte->User = user;
- pte->Present = true;
- pte->PhysicalAddress = (UIntPtr)kKernelPhysicalStart;
+ for (size_t i = 0; i < kPTESize; ++i)
+ {
+ if (cr3->Pte[i].Present) continue;
+ kcout << "HCoreKrnl.exe: Page index: " << hex_number(i) << endl;
- kKernelVirtualStart = (VoidPtr)((UIntPtr)kKernelVirtualStart + kKernelPagingPadding);
- kKernelPhysicalStart = (VoidPtr)((UIntPtr)kKernelPhysicalStart + kKernelPagingPadding);
+ cr3->Pte[i].Rw = rw;
+ cr3->Pte[i].User = user;
+ cr3->Pte[i].Present = true;
- ++kPageCnt;
+ ++kPageCnt;
+ kAllocationInProgress = false;
+ kcout << "HCoreKrnl.exe: Allocation done for: " << hex_number(i) << endl;
+ return (VoidPtr)cr3->Pte[i].PhysicalAddress;
+ }
+
kAllocationInProgress = false;
- return pte;
+ return nullptr;
}
-auto hal_alloc_page(SizeT sz, Boolean rw, Boolean user) -> PageTable64 * {
+auto hal_alloc_page(SizeT sz, Boolean rw, Boolean user) -> VoidPtr {
+ while (kAllocationInProgress) {}
+
if (sz == 0)
++sz;
@@ -53,12 +63,7 @@ auto hal_alloc_page(SizeT sz, Boolean rw, Boolean user) -> PageTable64 * {
}
auto hal_create_page(Boolean rw, Boolean user) -> UIntPtr {
- while (kAllocationInProgress) {}
-
- PageTable64 *new_pte = hal_alloc_page(sizeof(PageTable64), rw, user);
- MUST_PASS(new_pte);
-
- return reinterpret_cast<UIntPtr>(new_pte);
+ return reinterpret_cast<UIntPtr>(hal_alloc_page(sizeof(PageTable64), rw, user));
}
} // namespace HAL
} // namespace HCore
diff --git a/Private/HALKit/AMD64/HalPageAlloc.hpp b/Private/HALKit/AMD64/HalPageAlloc.hpp
index 34d76d3d..2ce56b5a 100644
--- a/Private/HALKit/AMD64/HalPageAlloc.hpp
+++ b/Private/HALKit/AMD64/HalPageAlloc.hpp
@@ -72,7 +72,7 @@ struct PageDirectory64 final {
PageTable64 ALIGN(kPTEAlign) Pte[kPTEMax];
};
-PageTable64* hal_alloc_page(SizeT sz, Boolean rw, Boolean user);
+VoidPtr hal_alloc_page(SizeT sz, Boolean rw, Boolean user);
UIntPtr hal_create_page(Boolean rw, Boolean user);
} // namespace HCore::HAL