Compare commits

...

2 commits

3 changed files with 70 additions and 6 deletions

View file

@ -19,7 +19,7 @@ $(object_files): $(object_dir)%.o : $(source_dir)%
kernel.bin: $(object_files) kernel.bin: $(object_files)
@mkdir -p $(output_dir) @mkdir -p $(output_dir)
@echo "Linking kernel" @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" @echo "Disassembling kernel"
@objdump -dC $(output_dir)/kernel.bin > $(output_dir)/kernel.asm @objdump -dC $(output_dir)/kernel.bin > $(output_dir)/kernel.asm
@objdump -DC $(output_dir)/kernel.bin > $(output_dir)/kernel_full.asm @objdump -DC $(output_dir)/kernel.bin > $(output_dir)/kernel_full.asm

View file

@ -33,12 +33,14 @@ SECTIONS
} > START_MEMORY_PV } > START_MEMORY_PV
.text : AT(ADDR(.text) - high_base) { .text : AT(ADDR(.text) - high_base) {
__text_start__ = .; __text_start__ = .;
*(EXCLUDE_FILE(*trampoline*) .text) *(EXCLUDE_FILE(*trampoline*) .text .text.*)
*(EXCLUDE_FILE(*trampoline*) .iplt)
__text_end__ = .; __text_end__ = .;
} > HIGH_KERNEL_V } > HIGH_KERNEL_V
.rodata : AT(ADDR(.rodata) - high_base) { .rodata : AT(ADDR(.rodata) - high_base) {
__rodata_start__ = .; __rodata_start__ = .;
*(EXCLUDE_FILE(*trampoline*) .rodata) *(EXCLUDE_FILE(*trampoline*) .rodata .rodata.*)
. = ALIGN(8);
__reserved_ranges_start__ = .; __reserved_ranges_start__ = .;
KEEP(*(.reserved_ranges)) KEEP(*(.reserved_ranges))
__reserved_ranges_end__ = .; __reserved_ranges_end__ = .;
@ -50,12 +52,13 @@ SECTIONS
} > HIGH_KERNEL_V } > HIGH_KERNEL_V
.data : AT(ADDR(.data) - high_base) { .data : AT(ADDR(.data) - high_base) {
__data_start__ = .; __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__ = .; __data_end__ = .;
} > HIGH_KERNEL_V } > HIGH_KERNEL_V
.bss (NOLOAD) : AT(ADDR(.bss) - high_base) { .bss (NOLOAD) : AT(ADDR(.bss) - high_base) {
__bss_start__ = .; __bss_start__ = .;
*(EXCLUDE_FILE(*trampoline*) .bss) *(EXCLUDE_FILE(*trampoline*) .bss .bss.*)
__bss_end__ = .; __bss_end__ = .;
} > HIGH_KERNEL_V } > HIGH_KERNEL_V
@ -83,11 +86,33 @@ SECTIONS
__trampoline_bss_end__ = .; __trampoline_bss_end__ = .;
} > TRAMPOLINE_PV } > 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/ : { /DISCARD/ : {
*(.comment) *(.comment)
*(.eh_frame) *(.eh_frame)
*(.note) *(.note)
*(.note.gnu.build-id) *(.note.gnu.build-id)
*(.note.GNU-stack)
*(.rela*)
} }
} }

View file

@ -34,6 +34,33 @@ static void step(const char* name, T&& fn) {
print("\n"); print("\n");
} }
template<typename Fn>
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<uint64_t>(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<paddr_t> findInitialMemoryPages(const multiboot::tag_mmap* multiboot_info, size_t count) { optional<paddr_t> findInitialMemoryPages(const multiboot::tag_mmap* multiboot_info, size_t count) {
const auto* base = reinterpret_cast<const uint8_t*>(multiboot_info->entries); const auto* base = reinterpret_cast<const uint8_t*>(multiboot_info->entries);
const auto* end = reinterpret_cast<const uint8_t*>(multiboot_info) + multiboot_info->size; const auto* end = reinterpret_cast<const uint8_t*>(multiboot_info) + multiboot_info->size;
@ -49,7 +76,8 @@ optional<paddr_t> findInitialMemoryPages(const multiboot::tag_mmap* multiboot_in
continue; continue;
} }
for(auto ptr = start_ptr; ptr < (end_ptr - page_size * (count - 1)); ptr += page_size) { 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}; 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); alloc.force({page, 1}, false);
PhysicalAllocator::getInstance() = alloc; PhysicalAllocator::getInstance() = alloc;