From 5e3a25e3479c541012e4d23be46c2e6c2fe883ef Mon Sep 17 00:00:00 2001
From: Amelia Coutard <eliottulio.coutard@gmail.com>
Date: Wed, 27 Apr 2022 22:50:15 +0200
Subject: [PATCH] Put the kernel in the upper half.

---
 linker.ld      |  2 +-
 src/boot.S     | 43 +++++++++++++++++++++----------------------
 src/kernel.cpp |  3 +--
 3 files changed, 23 insertions(+), 25 deletions(-)

diff --git a/linker.ld b/linker.ld
index 8b2f5d4..60de5eb 100644
--- a/linker.ld
+++ b/linker.ld
@@ -1,6 +1,6 @@
 ENTRY(_start)
 
-KERNEL_VMA = 0; /*0xffffffff80000000;*/
+KERNEL_VMA = 0xFFFFFFFF80000000;
 SECTIONS {
 
 	.bootstrap ALIGN(4K) : {
diff --git a/src/boot.S b/src/boot.S
index 455f86a..26a779c 100644
--- a/src/boot.S
+++ b/src/boot.S
@@ -1,6 +1,6 @@
 #include "multiboot2.hpp"
 
-.set KERNEL_VMA, 0# 0xFFFFFFFF80000000
+.set KERNEL_VMA, 0xFFFFFFFF80000000
 
 .section .multiboot_header
 multiboot_header_start:
@@ -48,14 +48,15 @@ GDT:
         .int 0x00CF8900
     GDT.PTR:
         .short . - GDT - 1
-        .quad GDT
+        .quad GDT - KERNEL_VMA
 
 .section .bss
 .align 0x1000
-PML4T: .skip 0x1000
-PDPT:  .skip 0x1000
-PDT:   .skip 0x1000
-PT:    .skip 0x1000
+PML4T:     .skip 0x1000
+PDPT_low:  .skip 0x1000
+PDPT_high: .skip 0x1000
+PDT:       .skip 0x1000
+PT:        .skip 0x1000
 .align 16
 stack_bottom:
 .skip 1024 * 16 # 16KiB
@@ -65,7 +66,7 @@ stack_top:
 .code32
 .globl _start
 _start:
-	mov $stack_top, %esp
+	mov $stack_top - KERNEL_VMA, %esp
 
 	# Check if cpuid is supported:
 	pushfl
@@ -128,16 +129,19 @@ _start:
 	mov %eax, %cr0
 
 	# Setup 64-bit paging:
-	mov $PML4T, %eax
+	mov $PML4T - KERNEL_VMA, %eax
 	mov %eax, %cr3
-	mov $PDPT + 3, %eax
-	mov %eax, PML4T + 0 * 8
-	mov $PDT + 3, %eax
-	mov %eax, PDPT  + 0 * 8
-	mov $PT + 3, %eax
-	mov %eax, PDT   + 0 * 8
+	mov $PDPT_low - KERNEL_VMA + 3, %eax
+	mov %eax, PML4T - KERNEL_VMA + 0 * 8
+	mov $PDPT_high - KERNEL_VMA + 3, %eax
+	mov %eax, PML4T - KERNEL_VMA + 511 * 8
+	mov $PDT - KERNEL_VMA + 3, %eax
+	mov %eax, PDPT_low - KERNEL_VMA + 0 * 8
+	mov %eax, PDPT_high - KERNEL_VMA + 510 * 8
+	mov $PT - KERNEL_VMA + 3, %eax
+	mov %eax, PDT - KERNEL_VMA + 0 * 8
 
-	mov $PT, %edi
+	mov $PT - KERNEL_VMA, %edi
 	mov $0x0003, %ebx
 	mov $512, %ecx
 1:	mov %ebx, (%edi)
@@ -161,7 +165,7 @@ _start:
 	mov %eax, %cr0
 
 	# Jump to 64 bits:
-	lgdt (GDT.PTR)
+	lgdt (GDT.PTR - KERNEL_VMA)
 	jmp $GDT.CODE, $.trampoline
 
 .code64
@@ -176,11 +180,6 @@ _start:
 	mov $.higher_half, %rax
 	jmp *%rax
 
+.section .text
 .higher_half:
-	movw $0x024F, 0xB8000
-	movw $0x026B, 0xB8002
-	movw $0x022E, 0xB8004
 	call kmain
-	cli
-1:	hlt
-	jmp 1b
diff --git a/src/kernel.cpp b/src/kernel.cpp
index d3132d7..e7d9d6a 100644
--- a/src/kernel.cpp
+++ b/src/kernel.cpp
@@ -1,8 +1,7 @@
 #include "multiboot2.hpp"
 
 extern "C" void kmain() {
-	// volatile short* vga_text = reinterpret_cast<volatile short*>((void*)0xFFFFFFFF800B8000);
-	volatile short* vga_text = reinterpret_cast<volatile short*>((void*)0xB8000);
+	volatile short* vga_text = reinterpret_cast<volatile short*>((void*)0xFFFFFFFF800B8000);
 	const char* str = "Success ! Took me long enough.";
 	for (unsigned long i = 0; str[i] != '\0'; i++) {
 		vga_text[i] = (0x02 << 8) + str[i];
-- 
2.46.0