From acf8f2a2a3894304fa7e9e48f4fb104b65298f12 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 16 Jul 2021 09:37:45 +0200 Subject: [PATCH] Kernel: Support specifying a 64-bit KERNEL_BASE address The kernel doesn't currently boot when using an address other than 0xc0000000 because the page tables aren't set up properly for that but this at least lets us build the kernel. --- Kernel/Arch/x86/common/Boot/boot.S | 38 ++++++++++++++++++++------ Kernel/Arch/x86/x86_64/Boot/ap_setup.S | 6 +++- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Kernel/Arch/x86/common/Boot/boot.S b/Kernel/Arch/x86/common/Boot/boot.S index 2eadc29e26..28d3c348d6 100644 --- a/Kernel/Arch/x86/common/Boot/boot.S +++ b/Kernel/Arch/x86/common/Boot/boot.S @@ -519,20 +519,32 @@ pae_supported: movl %eax, %cr0 /* set up stack */ - mov $stack_top, %esp + mov $(stack_top - KERNEL_BASE), %esp and $-16, %esp - /* jump into C++ land */ - addl $KERNEL_BASE, %ebx - movl %ebx, multiboot_info_ptr - #if ARCH(X86_64) /* Now we are in 32-bit compatibility mode, We still need to load a 64-bit GDT */ - lgdt gdt64ptr - ljmpl $code64_sel, $1f + mov $(gdt64ptr - KERNEL_BASE), %eax + lgdt (%eax) + ljmpl $code64_sel, $(1f - KERNEL_BASE) .code64 1: + movl %ebx, %ebx + movabs $KERNEL_BASE, %rax + addq %rax, %rbx + movabs $multiboot_info_ptr, %rax + movq %rbx, (%rax) + + movabs $gdt64ptr, %rax + lgdt (%rax) + movabs $1f, %rax + jmp *%rax + +1: + movabs $KERNEL_BASE, %rax + addq %rax, %rsp + mov $0, %ax mov %ax, %ss mov %ax, %ds @@ -542,11 +554,17 @@ pae_supported: mov %cr3, %rax mov %rax, %cr3 + #else + addl $KERNEL_BASE, %ebx + movl %ebx, multiboot_info_ptr + /* jmp to an address above the 3GB mark */ - movl $1f,%eax + movl $1f, %eax jmp *%eax 1: + add $KERNEL_BASE, %esp + movl %cr3, %eax movl %eax, %cr3 #endif @@ -561,10 +579,12 @@ pae_supported: addl $8, %edi loop 1b - call init #if ARCH(X86_64) + movabs $init, %rax + call *%rax add $4, %rsp #else + call init add $4, %esp #endif diff --git a/Kernel/Arch/x86/x86_64/Boot/ap_setup.S b/Kernel/Arch/x86/x86_64/Boot/ap_setup.S index b43f216a92..714642dd79 100644 --- a/Kernel/Arch/x86/x86_64/Boot/ap_setup.S +++ b/Kernel/Arch/x86/x86_64/Boot/ap_setup.S @@ -92,12 +92,16 @@ apic_ap_start32: movl %eax, %cr0 /* load the temporary 64-bit gdt from boot that points above 3GB */ - lgdt gdt64ptr + mov $(gdt64ptr - KERNEL_BASE), %eax + lgdt (%eax) /* jump above 3GB into our identity mapped area now */ ljmpl $code64_sel, $(apic_ap_start64 - apic_ap_start + 0xc0008000) .code64 apic_ap_start64: + movabs $gdt64ptr, %rax + lgdt (%rax) + mov $0, %ax mov %ax, %ss mov %ax, %ds