1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:37:34 +00:00

Kernel/x86: Bake the Prekernel and the Kernel into one image

The new baked image is a Prekernel and a Kernel baked together now, so
essentially we no longer need to pass the Prekernel as -kernel and the
actual  kernel image as -initrd to QEMU, leaving the option to pass an
actual initrd or initramfs module later on with multiboot.
This commit is contained in:
Liav A 2023-03-24 20:31:53 +03:00 committed by Jelle Raaijmakers
parent 2a1e58f8cc
commit 897c4e5145
10 changed files with 81 additions and 50 deletions

View file

@ -8,14 +8,21 @@ set(SOURCES
)
if ("${SERENITY_ARCH}" STREQUAL "x86_64")
set(PREKERNEL_TARGET Prekernel64)
set(PREKERNEL_TARGET kernel_x86-64)
elseif("${SERENITY_ARCH}" STREQUAL "aarch64")
message(SEND_ERROR "Prekernel is not needed on aarch64 and should not be compiled!")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
add_executable(${PREKERNEL_TARGET} ${SOURCES})
add_library(KernelObject OBJECT IMPORTED)
set_property(TARGET KernelObject PROPERTY
IMPORTED_OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/../Kernel.o
)
add_executable(${PREKERNEL_TARGET} ${SOURCES} $<TARGET_OBJECTS:KernelObject>)
add_dependencies(${PREKERNEL_TARGET} Kernel)
target_compile_options(${PREKERNEL_TARGET} PRIVATE -no-pie -fno-pic -fno-threadsafe-statics)
target_link_options(${PREKERNEL_TARGET} PRIVATE LINKER:-T ${CMAKE_CURRENT_SOURCE_DIR}/linker.ld -nostdlib LINKER:--no-pie)
@ -27,13 +34,20 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
target_link_libraries(${PREKERNEL_TARGET} PRIVATE clang_rt.builtins)
endif()
if ("${SERENITY_ARCH}" STREQUAL "x86_64")
set(ELF_OBJCOPY_TARGET "elf32-i386")
elseif("${SERENITY_ARCH}" STREQUAL "aarch64")
message(SEND_ERROR "Prekernel is not needed on aarch64 and should not be compiled!")
endif()
add_custom_command(
TARGET ${PREKERNEL_TARGET} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O elf32-i386 ${CMAKE_CURRENT_BINARY_DIR}/${PREKERNEL_TARGET} ${CMAKE_CURRENT_BINARY_DIR}/Prekernel
COMMAND ${CMAKE_OBJCOPY} -O ${ELF_OBJCOPY_TARGET} ${CMAKE_CURRENT_BINARY_DIR}/${PREKERNEL_TARGET} ${CMAKE_CURRENT_BINARY_DIR}/../Kernel
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/Prekernel
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/Prekernel" DESTINATION boot)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/../Kernel" DESTINATION boot)
# Remove options which the Prekernel environment doesn't support.
get_target_property(PREKERNEL_TARGET_OPTIONS ${PREKERNEL_TARGET} COMPILE_OPTIONS)

View file

@ -41,8 +41,11 @@ struct [[gnu::packed]] BootInfo {
u32 multiboot_flags;
u64 multiboot_memory_map;
u32 multiboot_memory_map_count;
u64 multiboot_modules;
u64 multiboot_modules_physical_ptr;
u32 multiboot_modules_count;
u32 multiboot_module_ramdisk_physical_start;
u32 multiboot_module_ramdisk_physical_end;
u32 multiboot_module_ramdisk_physical_string_addr;
u64 multiboot_framebuffer_addr;
u32 multiboot_framebuffer_pitch;
u32 multiboot_framebuffer_width;

View file

@ -27,6 +27,9 @@ extern "C" [[noreturn]] void __stack_chk_fail();
extern "C" u8 start_of_prekernel_image[];
extern "C" u8 end_of_prekernel_image[];
extern "C" u8 _binary_Kernel_standalone_start[];
extern "C" u8 end_of_prekernel_image_after_kernel_image[];
extern "C" u8 gdt64ptr[];
extern "C" u16 code64_sel;
extern "C" u64 boot_pml4t[512];
@ -76,12 +79,7 @@ u64 generate_secure_seed();
extern "C" [[noreturn]] void init()
{
if (multiboot_info_ptr->mods_count < 1)
halt();
multiboot_module_entry_t* kernel_module = (multiboot_module_entry_t*)(FlatPtr)multiboot_info_ptr->mods_addr;
u8* kernel_image = (u8*)(FlatPtr)kernel_module->start;
u8* kernel_image = _binary_Kernel_standalone_start;
// copy the ELF header and program headers because we might end up overwriting them
ElfW(Ehdr) kernel_elf_header = *(ElfW(Ehdr)*)kernel_image;
ElfW(Phdr) kernel_program_headers[16];
@ -89,8 +87,8 @@ extern "C" [[noreturn]] void init()
halt();
__builtin_memcpy(kernel_program_headers, kernel_image + kernel_elf_header.e_phoff, sizeof(ElfW(Phdr)) * kernel_elf_header.e_phnum);
FlatPtr kernel_physical_base = 0x200000;
FlatPtr default_kernel_load_base = KERNEL_MAPPING_BASE + 0x200000;
FlatPtr kernel_physical_base = (FlatPtr)kernel_image;
FlatPtr default_kernel_load_base = KERNEL_MAPPING_BASE + kernel_physical_base;
FlatPtr kernel_load_base = default_kernel_load_base;
@ -168,9 +166,6 @@ extern "C" [[noreturn]] void init()
__builtin_memset((u8*)kernel_load_base + kernel_program_header.p_vaddr + kernel_program_header.p_filesz, 0, kernel_program_header.p_memsz - kernel_program_header.p_filesz);
}
multiboot_info_ptr->mods_count--;
multiboot_info_ptr->mods_addr += sizeof(multiboot_module_entry_t);
auto adjust_by_mapping_base = [kernel_mapping_base](auto ptr) {
return (decltype(ptr))((FlatPtr)ptr + kernel_mapping_base);
};
@ -194,8 +189,16 @@ extern "C" [[noreturn]] void init()
info.multiboot_flags = multiboot_info_ptr->flags;
info.multiboot_memory_map = adjust_by_mapping_base((FlatPtr)multiboot_info_ptr->mmap_addr);
info.multiboot_memory_map_count = multiboot_info_ptr->mmap_length / sizeof(multiboot_memory_map_t);
info.multiboot_modules = adjust_by_mapping_base((FlatPtr)multiboot_info_ptr->mods_addr);
info.multiboot_modules_physical_ptr = (FlatPtr)multiboot_info_ptr->mods_addr;
info.multiboot_modules_count = multiboot_info_ptr->mods_count;
if (info.multiboot_modules_count > 0) {
auto* entry = reinterpret_cast<multiboot_module_entry*>(multiboot_info_ptr->mods_addr);
info.multiboot_module_ramdisk_physical_start = entry->start;
info.multiboot_module_ramdisk_physical_end = entry->end;
info.multiboot_module_ramdisk_physical_string_addr = entry->string_addr;
}
if ((multiboot_info_ptr->flags & MULTIBOOT_INFO_FRAMEBUFFER_INFO) != 0) {
info.multiboot_framebuffer_addr = multiboot_info_ptr->framebuffer_addr;
info.multiboot_framebuffer_pitch = multiboot_info_ptr->framebuffer_pitch;

View file

@ -44,4 +44,13 @@ SECTIONS
} :bss
end_of_prekernel_image = .;
.Kernel_image ALIGN(4K) : AT (ADDR(.Kernel_image))
{
_binary_Kernel_standalone_start = .;
KEEP(*(.Kernel_image))
}
end_of_prekernel_image_after_kernel_image = .;
}