From 0568a813f27344c12f00f65168a02d1d1c698592 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 25 Mar 2022 11:53:01 -0400 Subject: [PATCH] HELLO: Zero out HULK bss section --- src/hello/hello.d | 51 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/src/hello/hello.d b/src/hello/hello.d index 62bebbf..424a1fa 100644 --- a/src/hello/hello.d +++ b/src/hello/hello.d @@ -16,6 +16,7 @@ __gshared EFI_SYSTEM_TABLE * st; private __gshared BootInfo bootinfo; private __gshared UINTN memory_map_key; extern extern(C) __gshared ubyte hulk_bin_start; +extern extern(C) __gshared ubyte hulk_bin_end; /** * Detect if we're running in QEMU. @@ -229,6 +230,31 @@ private void map2m(ulong source_page, ulong dest_page, PageTableEntry * pt_base) } } +/** + * Allocate a memory region for the HULK bss section. + * + * @param bss_size Size of the HULK bss section. + * + * @return Physical memory address. + */ +private ulong alloc_hulk_bss(size_t bss_size) +{ + for (size_t i = 0u; i < bootinfo.memory_map_count; i++) + { + if ((bootinfo.memory_map[i].type == EfiConventionalMemory) && + (bootinfo.memory_map[i].size >= bss_size)) + { + memset64(cast(void *)bootinfo.memory_map[i].base, 0u, bss_size / 8u); + return bootinfo.memory_map[i].base; + } + } + + /* We failed to find free memory. */ + for (;;) + { + } +} + /** * Map HULK virtual addresses to physical kernel location. * @@ -237,14 +263,27 @@ private void map2m(ulong source_page, ulong dest_page, PageTableEntry * pt_base) private void map_hulk(PageTableEntry * pt_base) { ulong virt = HULK_VIRTUAL_START; - ulong phys = cast(ulong)&hulk_bin_start; - HulkHeader * hulk_header = cast(HulkHeader *)&hulk_bin_start; - size_t end_phys = phys + cast(size_t)hulk_header.total_size; - while (phys < end_phys) + ulong hulk_bin_phys_start = cast(ulong)&hulk_bin_start; + ulong hulk_bin_phys_end = cast(ulong)&hulk_bin_end; + ulong phys_iter = hulk_bin_phys_start; + while (phys_iter < hulk_bin_phys_end) { - map4k(virt, phys, pt_base); + map4k(virt, phys_iter, pt_base); virt += 4096u; - phys += 4096u; + phys_iter += 4096u; + } + /* Now the binary has been mapped, but the bss section still needs to be + * allocated, zeroed and mapped. */ + HulkHeader * hulk_header = cast(HulkHeader *)&hulk_bin_start; + size_t hulk_bin_phys_size = hulk_bin_phys_end - hulk_bin_phys_end; + size_t bss_size = cast(size_t)hulk_header.total_size - hulk_bin_phys_size; + ulong bss_phys = alloc_hulk_bss(bss_size); + ulong bss_phys_end = bss_phys + bss_size; + while (bss_phys < bss_phys_end) + { + map4k(virt, bss_phys, pt_base); + virt += 4096u; + bss_phys += 4096u; } }