diff --git a/kernel/Makefile b/kernel/Makefile index e862d51..898397b 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -19,7 +19,7 @@ $(object_files): $(object_dir)%.o : $(source_dir)% kernel.bin: $(object_files) @mkdir -p $(output_dir) @echo "Linking kernel" - @$(LD) --build-id=none --no-relax --no-warn-rwx-segments -nostdlib -o $(output_dir)/kernel.bin -T $(linker_script) $(object_files) + @$(LD) --build-id=none --no-relax --no-warn-rwx-segments --orphan-handling=error -nostdlib -o $(output_dir)/kernel.bin -T $(linker_script) $(object_files) @echo "Disassembling kernel" @objdump -dC $(output_dir)/kernel.bin > $(output_dir)/kernel.asm @objdump -DC $(output_dir)/kernel.bin > $(output_dir)/kernel_full.asm diff --git a/kernel/linker.ld b/kernel/linker.ld index 15c3d23..a511bf6 100644 --- a/kernel/linker.ld +++ b/kernel/linker.ld @@ -33,12 +33,14 @@ SECTIONS } > START_MEMORY_PV .text : AT(ADDR(.text) - high_base) { __text_start__ = .; - *(EXCLUDE_FILE(*trampoline*) .text) + *(EXCLUDE_FILE(*trampoline*) .text .text.*) + *(EXCLUDE_FILE(*trampoline*) .iplt) __text_end__ = .; } > HIGH_KERNEL_V .rodata : AT(ADDR(.rodata) - high_base) { __rodata_start__ = .; - *(EXCLUDE_FILE(*trampoline*) .rodata) + *(EXCLUDE_FILE(*trampoline*) .rodata .rodata.*) + . = ALIGN(8); __reserved_ranges_start__ = .; KEEP(*(.reserved_ranges)) __reserved_ranges_end__ = .; @@ -50,12 +52,13 @@ SECTIONS } > HIGH_KERNEL_V .data : AT(ADDR(.data) - high_base) { __data_start__ = .; - *(EXCLUDE_FILE(*trampoline*) .data) + *(EXCLUDE_FILE(*trampoline*) .data .data.*) + *(EXCLUDE_FILE(*trampoline*) .got .got.plt .igot.plt .data.rel.ro .data.rel.ro.*) __data_end__ = .; } > HIGH_KERNEL_V .bss (NOLOAD) : AT(ADDR(.bss) - high_base) { __bss_start__ = .; - *(EXCLUDE_FILE(*trampoline*) .bss) + *(EXCLUDE_FILE(*trampoline*) .bss .bss.*) __bss_end__ = .; } > HIGH_KERNEL_V @@ -83,11 +86,33 @@ SECTIONS __trampoline_bss_end__ = .; } > TRAMPOLINE_PV + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + .debug_pubtypes 0 : { *(.debug_pubtypes) } + .debug_info 0 : { *(.debug_info) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_line_str 0 : { *(.debug_line_str) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_loclists 0 : { *(.debug_loclists) } + .debug_macinfo 0 : { *(.debug_macinfo) } + .debug_macro 0 : { *(.debug_macro) } + .debug_ranges 0 : { *(.debug_ranges) } + .debug_rnglists 0 : { *(.debug_rnglists) } + .debug_addr 0 : { *(.debug_addr) } + .debug_str_offsets 0 : { *(.debug_str_offsets) } + .debug_names 0 : { *(.debug_names) } + .debug_types 0 : { *(.debug_types) } + /DISCARD/ : { *(.comment) *(.eh_frame) *(.note) *(.note.gnu.build-id) + *(.note.GNU-stack) + *(.rela*) } } diff --git a/kernel/src/init/init.cpp b/kernel/src/init/init.cpp index 02d08d6..c92347b 100644 --- a/kernel/src/init/init.cpp +++ b/kernel/src/init/init.cpp @@ -34,6 +34,33 @@ static void step(const char* name, T&& fn) { print("\n"); } +template +static void for_each_boot_reserved_range(Fn&& fn) { + fn(paddr_t{0}, page_size); + + const multiboot::info* mb = multiboot::get_multiboot_info(); + uint64_t info_phys = reinterpret_cast(mb) - high_base; + fn(paddr_t{info_phys}, mb->total_size); + + multiboot::visit_all(mb, [&](const multiboot::tag_module* module) { + fn(paddr_t{module->mod_start}, module->mod_end - module->mod_start); + }); +} + +static bool is_in_boot_reserved_range(paddr_t ptr, size_t size) { + bool res = false; + for_each_boot_reserved_range([&](paddr_t start, size_t length) { + if(length == 0) { + return; + } + if(ptr.address < start.address + length && + start.address < ptr.address + size) { + res = true; + } + }); + return res; +} + optional findInitialMemoryPages(const multiboot::tag_mmap* multiboot_info, size_t count) { const auto* base = reinterpret_cast(multiboot_info->entries); const auto* end = reinterpret_cast(multiboot_info) + multiboot_info->size; @@ -49,7 +76,8 @@ optional findInitialMemoryPages(const multiboot::tag_mmap* multiboot_in continue; } for(auto ptr = start_ptr; ptr < (end_ptr - page_size * (count - 1)); ptr += page_size) { - if(!is_in_reserved_section(paddr_t{ptr}, page_size)) { + if(!is_in_reserved_section(paddr_t{ptr}, page_size) && + !is_in_boot_reserved_range(paddr_t{ptr}, page_size)) { return paddr_t{ptr}; } } @@ -98,6 +126,17 @@ void PhysicalAllocator::init(const multiboot::tag_mmap* multiboot_info) { }); }); + step("boot_reserved", [&]() { + for_each_boot_reserved_range([&](paddr_t start, size_t length){ + if(length == 0) { + return; + } + auto start_ptr = start.address & ~(page_size - 1); + auto end_ptr = (start.address + length + page_size - 1) & ~(page_size - 1); + alloc.force({paddr_t{start_ptr}, (end_ptr - start_ptr) / page_size}, false); + }); + }); + alloc.force({page, 1}, false); PhysicalAllocator::getInstance() = alloc;