aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/include/mm/virtual_mm.h22
-rw-r--r--kernel/mm/virtual_mm/virtual_mm.c16
2 files changed, 30 insertions, 8 deletions
diff --git a/kernel/include/mm/virtual_mm.h b/kernel/include/mm/virtual_mm.h
index 9806c4c..fce10bc 100644
--- a/kernel/include/mm/virtual_mm.h
+++ b/kernel/include/mm/virtual_mm.h
@@ -19,12 +19,15 @@
#ifndef __mm_virtual_mm_h
#define __mm_virtual_mm_h
+#include <stdbool.h>
+#include <stdint.h>
+
#define PAGE_SIZE (4096 * KiB)
#define PAGE_DIRECTORY_SIZE 1024
#define PAGE_TABLE_SIZE 1024
#define PDE_PRESENT 1
-#define PDE_WRITABLE (1 << 2)
+#define PDE_WRITABLE (1 << 1)
#define PDE_USER (1 << 2)
#define PDE_WRITETHROUGH (1 << 3)
#define PDE_CACHE_DISABLE (1 << 4)
@@ -34,12 +37,12 @@
#define PDE_PAGE_SIZE (1 << 7)
#define PDE_GLOBAL (1 << 8)
/* NOTE: Unused by the CPU, free to be used by us! */
-#define PDE_UNUSED(x) (x << 11)
+#define PDE_UNUSED(x) (x << 9)
/* Page table address */
-#define PDE_FRAME(x) (x << 31)
+#define PDE_FRAME(x) (x << 11)
#define PTE_PRESENT 1
-#define PTE_WRITABLE (1 << 2)
+#define PTE_WRITABLE (1 << 1)
#define PTE_USER (1 << 2)
#define PTE_WRITETHROUGH (1 << 3)
#define PTE_CACHE_DISABLE (1 << 4)
@@ -48,11 +51,14 @@
#define PTE_PAT (1 << 7)
#define PTE_GLOBAL (1 << 8)
/* NOTE: Unused by the CPU, free to be used by us! */
-#define PTE_UNUSED(x) (x << 11)
+#define PTE_UNUSED(x) (x << 9)
/* MAX: 0xFFFFF000 */
-#define PTE_FRAME(x) (x << 31)
+#define PTE_FRAME(x) (x << 11)
+
+#define ADD_ATTRIB(entry, attribute) (*entry |= (attribute))
+#define SET_FRAME(entry, frame) \
+ (ADD_ATTRIB(entry, PTE_FRAME((uint32_t) frame)))
-#define ADD_ATTRIB(entry, attribute) (entry |= attribute)
-#define SET_FRAME(entry, frame) (PTE_ADD_ATRIB(PTE_FRAME(frame)))
+bool virtual_mm_allocate_page(uint32_t *pt_entry);
#endif
diff --git a/kernel/mm/virtual_mm/virtual_mm.c b/kernel/mm/virtual_mm/virtual_mm.c
index 278a491..1e85e0c 100644
--- a/kernel/mm/virtual_mm/virtual_mm.c
+++ b/kernel/mm/virtual_mm/virtual_mm.c
@@ -18,6 +18,9 @@
#include <stdint.h>
+#include <libk/stdio.h>
+
+#include <mm/physical_mm.h>
#include <mm/virtual_mm.h>
extern uint32_t kernel_start;
@@ -25,3 +28,16 @@ extern uint32_t kernel_end;
uint32_t page_directory[PAGE_DIRECTORY_SIZE];
uint32_t initial_page_table[PAGE_TABLE_SIZE];
+
+bool
+virtual_mm_allocate_page(uint32_t *pt_entry)
+{
+ void *ptr = physical_mm_allocate_block();
+ if (!ptr)
+ return false;
+
+ SET_FRAME(pt_entry, ptr);
+ ADD_ATTRIB(pt_entry, PTE_PRESENT);
+
+ return true;
+}