Move whole kernel ELF to VMA

This commit is contained in:
YanWQ-monad 2024-05-03 03:04:22 +08:00 committed by Tate, Hongliang Tian
parent 4e1d98a323
commit ccc4e6ec6b
6 changed files with 47 additions and 44 deletions

View File

@ -17,6 +17,8 @@ ENTRYTYPE_LINUX_64 = 4
MULTIBOOT_ENTRY_MAGIC = 0x2BADB002 MULTIBOOT_ENTRY_MAGIC = 0x2BADB002
MULTIBOOT2_ENTRY_MAGIC = 0x36D76289 MULTIBOOT2_ENTRY_MAGIC = 0x36D76289
KERNEL_VMA = 0xffffffff80000000
// The Linux 32-bit Boot Protocol entry point. // The Linux 32-bit Boot Protocol entry point.
// Must be located at 0x8001000, ABI immutable! // Must be located at 0x8001000, ABI immutable!
.code32 .code32
@ -27,7 +29,7 @@ __linux32_boot:
cld cld
// Set the kernel call stack. // Set the kernel call stack.
mov esp, offset boot_stack_top mov esp, offset boot_stack_top - KERNEL_VMA
push 0 // upper 32-bits push 0 // upper 32-bits
push esi // boot_params ptr push esi // boot_params ptr
@ -43,7 +45,7 @@ __linux32_boot:
.global __linux64_boot_tag .global __linux64_boot_tag
__linux64_boot_tag: __linux64_boot_tag:
// Set the kernel call stack. // Set the kernel call stack.
lea rsp, [boot_stack_top] lea rsp, [boot_stack_top - KERNEL_VMA]
push rsi // boot_params ptr from the loader push rsi // boot_params ptr from the loader
push ENTRYTYPE_LINUX_64 push ENTRYTYPE_LINUX_64
@ -58,7 +60,7 @@ __multiboot_boot:
cld cld
// Set the kernel call stack. // Set the kernel call stack.
mov esp, offset boot_stack_top mov esp, offset boot_stack_top - KERNEL_VMA
push 0 // Upper 32-bits. push 0 // Upper 32-bits.
push eax // multiboot magic ptr push eax // multiboot magic ptr
@ -83,11 +85,11 @@ initial_boot_setup:
// Prepare for far return. We use a far return as a fence after setting GDT. // Prepare for far return. We use a far return as a fence after setting GDT.
mov eax, 24 mov eax, 24
push eax push eax
lea edx, [protected_mode] lea edx, [protected_mode - KERNEL_VMA]
push edx push edx
// Switch to our own temporary GDT. // Switch to our own temporary GDT.
lgdt [boot_gdtr] lgdt [boot_gdtr - KERNEL_VMA]
retf retf
protected_mode: protected_mode:
@ -101,8 +103,8 @@ protected_mode:
page_table_setup: page_table_setup:
// Zero out the page table. // Zero out the page table.
mov al, 0x00 mov al, 0x00
lea edi, [boot_page_table_start] lea edi, [boot_page_table_start - KERNEL_VMA]
lea ecx, [boot_page_table_end] lea ecx, [boot_page_table_end - KERNEL_VMA]
sub ecx, edi sub ecx, edi
cld cld
rep stosb rep stosb
@ -117,8 +119,8 @@ PTE_GLOBAL = (1 << 8)
// 0x00000000_40000000 ~ 0x00000000_7fffffff // 0x00000000_40000000 ~ 0x00000000_7fffffff
// 0x00000000_80000000 ~ 0x00000000_bfffffff // 0x00000000_80000000 ~ 0x00000000_bfffffff
// 0x00000000_c0000000 ~ 0x00000000_ffffffff // 0x00000000_c0000000 ~ 0x00000000_ffffffff
lea edi, [boot_pml4] lea edi, [boot_pml4 - KERNEL_VMA]
lea eax, [boot_pdpt + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)] lea eax, [boot_pdpt - KERNEL_VMA + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)]
mov dword ptr [edi], eax mov dword ptr [edi], eax
mov dword ptr [edi + 4], 0 mov dword ptr [edi + 4], 0
@ -127,56 +129,56 @@ PTE_GLOBAL = (1 << 8)
// 0xffff8000_80000000 ~ 0xffff8000_bfffffff // 0xffff8000_80000000 ~ 0xffff8000_bfffffff
// 0xffff8000_c0000000 ~ 0xffff8000_ffffffff // 0xffff8000_c0000000 ~ 0xffff8000_ffffffff
// 0xffff8008_00000000 ~ 0xffff8008_3fffffff // 0xffff8008_00000000 ~ 0xffff8008_3fffffff
lea edi, [boot_pml4 + 0x100 * 8] lea edi, [boot_pml4 - KERNEL_VMA + 0x100 * 8]
lea eax, [boot_pdpt + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)] lea eax, [boot_pdpt - KERNEL_VMA + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)]
mov dword ptr [edi], eax mov dword ptr [edi], eax
mov dword ptr [edi + 4], 0 mov dword ptr [edi + 4], 0
// PML4: 0xffffffff_80000000 ~ 0xffffffff_bfffffff // PML4: 0xffffffff_80000000 ~ 0xffffffff_bfffffff
// 0xffffffff_c0000000 ~ 0xffffffff_ffffffff // 0xffffffff_c0000000 ~ 0xffffffff_ffffffff
lea edi, [boot_pml4 + 0x1ff * 8] lea edi, [boot_pml4 - KERNEL_VMA + 0x1ff * 8]
lea eax, [boot_pdpt + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)] lea eax, [boot_pdpt - KERNEL_VMA + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)]
mov dword ptr [edi], eax mov dword ptr [edi], eax
mov dword ptr [edi + 4], 0 mov dword ptr [edi + 4], 0
// PDPT: 0x00000000_00000000 ~ 0x00000000_3fffffff // PDPT: 0x00000000_00000000 ~ 0x00000000_3fffffff
lea edi, [boot_pdpt] lea edi, [boot_pdpt - KERNEL_VMA]
lea eax, [boot_pd_0g_1g + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)] lea eax, [boot_pd_0g_1g - KERNEL_VMA + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)]
mov dword ptr [edi], eax mov dword ptr [edi], eax
mov dword ptr [edi + 4], 0 mov dword ptr [edi + 4], 0
// PDPT: 0x00000000_40000000 ~ 0x00000000_7fffffff // PDPT: 0x00000000_40000000 ~ 0x00000000_7fffffff
lea edi, [boot_pdpt + 0x1 * 8] lea edi, [boot_pdpt - KERNEL_VMA + 0x1 * 8]
lea eax, [boot_pd_1g_2g + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)] lea eax, [boot_pd_1g_2g - KERNEL_VMA + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)]
mov dword ptr [edi], eax mov dword ptr [edi], eax
mov dword ptr [edi + 4], 0 mov dword ptr [edi + 4], 0
// PDPT: 0x00000000_80000000 ~ 0x00000000_bfffffff // PDPT: 0x00000000_80000000 ~ 0x00000000_bfffffff
lea edi, [boot_pdpt + 0x2 * 8] lea edi, [boot_pdpt - KERNEL_VMA + 0x2 * 8]
lea eax, [boot_pd_2g_3g + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)] lea eax, [boot_pd_2g_3g - KERNEL_VMA + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)]
mov dword ptr [edi], eax mov dword ptr [edi], eax
mov dword ptr [edi + 4], 0 mov dword ptr [edi + 4], 0
// PDPT: 0x00000000_c0000000 ~ 0x00000000_ffffffff // PDPT: 0x00000000_c0000000 ~ 0x00000000_ffffffff
lea edi, [boot_pdpt + 0x3 * 8] lea edi, [boot_pdpt - KERNEL_VMA + 0x3 * 8]
lea eax, [boot_pd_3g_4g + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)] lea eax, [boot_pd_3g_4g - KERNEL_VMA + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)]
mov dword ptr [edi], eax mov dword ptr [edi], eax
mov dword ptr [edi + 4], 0 mov dword ptr [edi + 4], 0
// PDPT: 0xffffffff_80000000 ~ 0xffffffff_bfffffff // PDPT: 0xffffffff_80000000 ~ 0xffffffff_bfffffff
lea edi, [boot_pdpt + 0x1fe * 8] lea edi, [boot_pdpt - KERNEL_VMA + 0x1fe * 8]
lea eax, [boot_pd_0g_1g + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)] lea eax, [boot_pd_0g_1g - KERNEL_VMA + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)]
mov dword ptr [edi], eax mov dword ptr [edi], eax
mov dword ptr [edi + 4], 0 mov dword ptr [edi + 4], 0
// PDPT: 0xffffffff_c0000000 ~ 0xffffffff_ffffffff // PDPT: 0xffffffff_c0000000 ~ 0xffffffff_ffffffff
lea edi, [boot_pdpt + 0x1ff * 8] lea edi, [boot_pdpt - KERNEL_VMA + 0x1ff * 8]
lea eax, [boot_pd_1g_2g + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)] lea eax, [boot_pd_1g_2g - KERNEL_VMA + (PTE_PRESENT | PTE_WRITE | PTE_GLOBAL)]
mov dword ptr [edi], eax mov dword ptr [edi], eax
mov dword ptr [edi + 4], 0 mov dword ptr [edi + 4], 0
// Page Directory: map to low 1 GiB * 4 space // Page Directory: map to low 1 GiB * 4 space
lea edi, [boot_pd] lea edi, [boot_pd - KERNEL_VMA]
mov eax, PTE_PRESENT | PTE_WRITE | PTE_GLOBAL | PTE_HUGE mov eax, PTE_PRESENT | PTE_WRITE | PTE_GLOBAL | PTE_HUGE
mov ecx, 512 * 4 // (of entries in PD) * (number of PD) mov ecx, 512 * 4 // (of entries in PD) * (number of PD)
write_pd_entry: write_pd_entry:
@ -195,7 +197,7 @@ enable_long_mode:
mov cr4, eax mov cr4, eax
// Set the page table address. // Set the page table address.
lea eax, [boot_pml4] lea eax, [boot_pml4 - KERNEL_VMA]
mov cr3, eax mov cr3, eax
// Enable long mode. // Enable long mode.
@ -207,7 +209,7 @@ enable_long_mode:
// Prepare for far return. // Prepare for far return.
mov eax, 8 mov eax, 8
push eax push eax
lea edx, [long_mode_in_low_address] lea edx, [long_mode_in_low_address - KERNEL_VMA]
push edx push edx
// Enable paging. // Enable paging.
@ -223,7 +225,7 @@ enable_long_mode:
.global boot_gdtr .global boot_gdtr
boot_gdtr: boot_gdtr:
.word gdt_end - gdt - 1 .word gdt_end - gdt - 1
.quad gdt .quad gdt - KERNEL_VMA
.align 16 .align 16
gdt: gdt:
@ -268,9 +270,9 @@ long_mode_in_low_address:
mov gs, ax mov gs, ax
// Update RSP/RIP to use the virtual address. // Update RSP/RIP to use the virtual address.
mov rbx, 0xffffffff80000000 mov rbx, KERNEL_VMA
or rsp, rbx or rsp, rbx
lea rax, [long_mode - 0xffffffff80000000] lea rax, [long_mode - KERNEL_VMA]
or rax, rbx or rax, rbx
jmp rax jmp rax

View File

@ -10,6 +10,7 @@ MB2_MAGIC = 0xE85250D6
MB2_ARCHITECTURE = 0 // 32-bit (protected) mode of i386 MB2_ARCHITECTURE = 0 // 32-bit (protected) mode of i386
MB2_HEADERLEN = header_end - header_start MB2_HEADERLEN = header_end - header_start
MB2_CHECKSUM = -(MB2_MAGIC + MB2_ARCHITECTURE + MB2_HEADERLEN) MB2_CHECKSUM = -(MB2_MAGIC + MB2_ARCHITECTURE + MB2_HEADERLEN)
KERNEL_VMA = 0xffffffff80000000
header_start: header_start:
.align 8 .align 8
@ -25,7 +26,7 @@ entry_address_tag_start:
.short 1 // Optional .short 1 // Optional
.long entry_address_tag_end - entry_address_tag_start .long entry_address_tag_end - entry_address_tag_start
.extern __multiboot_boot .extern __multiboot_boot
.long __multiboot_boot // entry_addr .long __multiboot_boot - KERNEL_VMA // entry_addr
entry_address_tag_end: entry_address_tag_end:
// Tag: information request // Tag: information request

View File

@ -6,6 +6,8 @@
use alloc::{vec, vec::Vec}; use alloc::{vec, vec::Vec};
use core::mem::swap; use core::mem::swap;
use crate::vm::kspace::kernel_loaded_offset;
/// The type of initial memory regions that are needed for the kernel. /// The type of initial memory regions that are needed for the kernel.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum MemoryRegionType { pub enum MemoryRegionType {
@ -53,7 +55,7 @@ impl MemoryRegion {
fn __kernel_end(); fn __kernel_end();
} }
MemoryRegion { MemoryRegion {
base: __kernel_start as usize, base: __kernel_start as usize - kernel_loaded_offset(),
len: __kernel_end as usize - __kernel_start as usize, len: __kernel_end as usize - __kernel_start as usize,
typ: MemoryRegionType::Kernel, typ: MemoryRegionType::Kernel,
} }

View File

@ -12,7 +12,7 @@ repository = "https://github.com/asterinas/asterinas"
[dependencies.linux-bzimage-builder] [dependencies.linux-bzimage-builder]
git = "https://github.com/asterinas/asterinas" git = "https://github.com/asterinas/asterinas"
# Make sure it syncs with `crate::util::ASTER_GIT_REV` # Make sure it syncs with `crate::util::ASTER_GIT_REV`
rev = "cc4111c" rev = "c9b66bd"
# When publishing, the crate.io version is used, make sure # When publishing, the crate.io version is used, make sure
# the builder is published # the builder is published
# FIXME: The version is currently commented out as it is no longer in use. # FIXME: The version is currently commented out as it is no longer in use.

View File

@ -8,18 +8,16 @@ KERNEL_VMA = 0xffffffff80000000;
SECTIONS SECTIONS
{ {
. = KERNEL_LMA; . = KERNEL_LMA + KERNEL_VMA;
__kernel_start = .; __kernel_start = .;
.multiboot_header : { KEEP(*(.multiboot_header)) } .multiboot_header : AT(ADDR(.multiboot_header) - KERNEL_VMA) { KEEP(*(.multiboot_header)) }
.multiboot2_header : { KEEP(*(.multiboot2_header)) } .multiboot2_header : AT(ADDR(.multiboot2_header) - KERNEL_VMA) { KEEP(*(.multiboot2_header)) }
. = LINUX_32_ENTRY; . = LINUX_32_ENTRY + KERNEL_VMA;
.boot : { KEEP(*(.boot)) } .boot : AT(ADDR(.boot) - KERNEL_VMA) { KEEP(*(.boot)) }
. += KERNEL_VMA;
.text : AT(ADDR(.text) - KERNEL_VMA) { .text : AT(ADDR(.text) - KERNEL_VMA) {
*(.text .text.*) *(.text .text.*)
@ -71,5 +69,5 @@ SECTIONS
. = DATA_SEGMENT_END(.); . = DATA_SEGMENT_END(.);
__kernel_end = . - KERNEL_VMA; __kernel_end = .;
} }

View File

@ -15,7 +15,7 @@ use quote::ToTokens;
/// and use the published version in the generated Cargo.toml. /// and use the published version in the generated Cargo.toml.
pub const ASTER_GIT_LINK: &str = "https://github.com/asterinas/asterinas"; pub const ASTER_GIT_LINK: &str = "https://github.com/asterinas/asterinas";
/// Make sure it syncs with the builder dependency in Cargo.toml. /// Make sure it syncs with the builder dependency in Cargo.toml.
pub const ASTER_GIT_REV: &str = "cc4111c"; pub const ASTER_GIT_REV: &str = "c9b66bd";
pub fn aster_crate_dep(crate_name: &str) -> String { pub fn aster_crate_dep(crate_name: &str) -> String {
format!( format!(
"{} = {{ git = \"{}\", rev = \"{}\" }}", "{} = {{ git = \"{}\", rev = \"{}\" }}",