Reinitialize paging in C
diff --git a/src/kernel/boot.s b/src/kernel/boot.s
index a65e2d8..d80261a 100644
--- a/src/kernel/boot.s
+++ b/src/kernel/boot.s
@@ -18,9 +18,9 @@
[section .data align = 0x1000]
page_directory:
- dd 0b010000011 ; Identity map first 4 megs
+ dd 0b010000011 ; Identity map first 4 megs
times (KERNEL_PAGE_NUMBER - 1) dd 0
- dd 0b010000011 ; Map kernel memory to zero page too
+ dd 0b010000011 ; Map kernel memory to zero page too
times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0
[bits 32]
@@ -66,10 +66,14 @@
mov esp, (stack + STACK_SIZE)
- push ebx ; Holds multiboot header location, PHYSICAL addr
+ add ebx, 0xC0000000 ; Translate to virtual address
+ push ebx ; Holds multiboot header location
call kmain
- jmp $
+.end:
+ hlt
+ jmp .end
+
[section .bss align = 32]
stack:
diff --git a/src/kernel/main.c b/src/kernel/main.c
index eeef814..59fc65c 100644
--- a/src/kernel/main.c
+++ b/src/kernel/main.c
@@ -7,7 +7,7 @@
int kmain(void *mboot)
{
-// initialize_paging();
+ init_paging();
vga_clear();
vga_set_color(LIGHT_BLUE, BLACK);
diff --git a/src/kernel/paging.c b/src/kernel/paging.c
index 1d50ce5..458de21 100644
--- a/src/kernel/paging.c
+++ b/src/kernel/paging.c
@@ -12,9 +12,7 @@
static ulong num_frames;
static uint first_page_table[1024] __attribute__((aligned(4096)));
-static uint *page_directory[1024] __attribute__((aligned(4096)));
-
-static uint ***current_directory;
+static uint page_directory[1024] __attribute__((aligned(4096)));
void *_kmalloc(size_t size, bool align, void **phys)
{
@@ -111,25 +109,27 @@
clear_frame(page / 0x1000);
}
+void map_4mb(size_t virt_start, size_t phys_start, bool user, bool rw)
+{
+ uint page = virt_start / 0x1000;
+ uint table = virt_start >> 22;
+
+ for (uint i = 0; i < 1024 * 0x1000; i += 0x1000)
+ {
+ set_frame(page + i);
+ }
+
+ page_directory[table] = 0b10000011;
+}
+
/* paging stuff */
-void initialize_paging()
+void init_paging()
{
- for (int i = 0; i < 1024; i++)
- {
- page_directory[i] = (uint *) 0b010; // kernel, rw, not present
- }
+ memset(page_directory, 0, 1024 * 4);
+ page_directory[KERNEL_PAGE_NUMBER] = 0b10000011;
- for (int i = 0; i < 1024; i++)
- {
- first_page_table[i] = (i * 0x1000) | 3;
- }
-
- page_directory[0] = (uint *) (((size_t) (uint *) first_page_table) | 3);
-
- load_page_directory((uint) page_directory);
- enable_paging();
-
+ load_page_directory((uint)page_directory - 0xC0000000);
add_interrupt_handler(14, page_fault);
}
diff --git a/src/kernel/paging.h b/src/kernel/paging.h
index 7c5000e..921524f 100644
--- a/src/kernel/paging.h
+++ b/src/kernel/paging.h
@@ -3,6 +3,11 @@
#include "kint.h"
#include "registers.h"
+#define VIRT_TO_PHYS(virt) ((uint)(virt) - 0xC0000000)
+#define PHYS_TO_VIRT(phys) ((uint)(phys) + 0xC0000000)
+#define KERNEL_VIRTUAL_BASE 0xC0000000
+#define KERNEL_PAGE_NUMBER (KERNEL_VIRTUAL_BASE >> 22)
+
/* defined in switch_table.s */
extern uint load_page_directory(uint table_address);
extern void enable_paging();
@@ -12,6 +17,6 @@
void *kmalloc_a(size_t size);
void *kmalloc_ap(size_t size, void **p);
-void initialize_paging();
+void init_paging();
void page_fault(struct registers *regs);
diff --git a/src/kernel/switch_table.s b/src/kernel/switch_table.s
index d04629b..c8e6260 100644
--- a/src/kernel/switch_table.s
+++ b/src/kernel/switch_table.s
@@ -1,25 +1,15 @@
[section .text]
[bits 32]
-;;; TODO: remove useless frame pointer stuff
-
[global load_page_directory]
load_page_directory:
- push ebp ; Save frame pointer
- mov ebp, esp
- mov eax, [esp + 8] ; Pointer to directory
- mov cr3, eax
- mov esp, ebp
- pop ebp
+ mov ecx, [esp + 4] ; Pointer to directory
+ mov cr3, ecx
ret
[global enable_paging]
enable_paging:
- push ebp
- mov ebp, esp
- mov eax, cr0
+ mov ecx, cr0
or eax, 0x80000000
- mov cr0, eax
- mov esp, ebp
- pop ebp
+ mov cr0, ecx
ret