From 861a0b4808a10b77f62d09feb7e3a496213b4526 Mon Sep 17 00:00:00 2001 From: Nathan Whitehorn Date: Sat, 3 Feb 2018 23:49:21 +0000 Subject: [PATCH] Fix regression introduced in r328806, preventing boot at least on all PowerPC Apple hardware, and likely all Open Firmware systems. The loader would allocate memory for its heap at whatever address Open Firmware gave it, which would in general be the lowest unallocated address, usually starting a page or two above 0. As the kernel is linked at 1 MB, and loader insists on running the kernel at its link address, any heap larger than 1 MB would overlap the kernel, causing loader memory allocations to corrupt the kernel and vice versa. Although r328806 made this problem much worse by increasing the heap size to 8 MB, causing 88% of the loader heap to overlap with the kernel, the problem has always existed. The old heap size was 1 MB and, unless that started exactly at zero, which would cause other problems, some number of pages of the loader heap still overlapped with the kernel. This patch solves the issue in two ways and cleans up some related code: - Moves the loader heap inside of the loader. This guarantees that the heap will be contiguous with the loader and simplifies the heap allocation code at no cost, since the heap lives in BSS. - Moves the loader, previously at 28 MB and dangerously close to the kernel it loads, a bit higher to 44 MB. This has the effect of breaking loader on non-embedded PPC machines with < 48 MB of RAM, but we did not support those anyway. The fundamental problem is that the way loader loads ELF files is incredibly fragile, but that can't be fixed without fundamental architectural changes. MFC after: 10 days --- stand/ofw/common/main.c | 12 +++-------- stand/ofw/libofw/elf_freebsd.c | 1 - stand/ofw/libofw/libofw.h | 2 -- stand/ofw/libofw/ofw_copy.c | 2 +- stand/ofw/libofw/ofw_memory.c | 32 ---------------------------- stand/ofw/libofw/ppc64_elf_freebsd.c | 1 - stand/powerpc/ofw/ldscript.powerpc | 2 +- 7 files changed, 5 insertions(+), 47 deletions(-) diff --git a/stand/ofw/common/main.c b/stand/ofw/common/main.c index 607455e95153..b8c7a17bb1c1 100644 --- a/stand/ofw/common/main.c +++ b/stand/ofw/common/main.c @@ -43,22 +43,16 @@ u_int32_t acells, scells; static char bootargs[128]; #define HEAP_SIZE 0x800000 +static char heap[HEAP_SIZE]; // In BSS, so uses no space #define OF_puts(fd, text) OF_write(fd, text, strlen(text)) void init_heap(void) { - void *base; - ihandle_t stdout; + bzero(heap, HEAP_SIZE); - if ((base = ofw_alloc_heap(HEAP_SIZE)) == (void *)0xffffffff) { - OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)); - OF_puts(stdout, "Heap memory claim failed!\n"); - OF_enter(); - } - - setheap(base, (void *)((int)base + HEAP_SIZE)); + setheap(heap, (void *)((int)heap + HEAP_SIZE)); } uint64_t diff --git a/stand/ofw/libofw/elf_freebsd.c b/stand/ofw/libofw/elf_freebsd.c index a2e48a7a739e..456b0cd58920 100644 --- a/stand/ofw/libofw/elf_freebsd.c +++ b/stand/ofw/libofw/elf_freebsd.c @@ -87,7 +87,6 @@ __elfN(ofw_exec)(struct preloaded_file *fp) printf("Kernel entry at 0x%lx ...\n", e->e_entry); dev_cleanup(); - ofw_release_heap(); if (dtbp != 0) { OF_quiesce(); ((int (*)(u_long, u_long, u_long, void *, u_long))entry)(dtbp, 0, 0, diff --git a/stand/ofw/libofw/libofw.h b/stand/ofw/libofw/libofw.h index b8c4014387bd..24a5d08fd77a 100644 --- a/stand/ofw/libofw/libofw.h +++ b/stand/ofw/libofw/libofw.h @@ -58,8 +58,6 @@ extern int ofw_boot(void); extern int ofw_autoload(void); void ofw_memmap(int); -void *ofw_alloc_heap(unsigned int); -void ofw_release_heap(void); struct preloaded_file; struct file_format; diff --git a/stand/ofw/libofw/ofw_copy.c b/stand/ofw/libofw/ofw_copy.c index ffd698731503..d83dbeddb577 100644 --- a/stand/ofw/libofw/ofw_copy.c +++ b/stand/ofw/libofw/ofw_copy.c @@ -39,7 +39,7 @@ __FBSDID("$FreeBSD$"); #define READIN_BUF (4 * 1024) #define PAGE_SIZE 0x1000 #define PAGE_MASK 0x0fff -#define MAPMEM_PAGE_INC 16 +#define MAPMEM_PAGE_INC 128 /* Half-MB at a time */ #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) diff --git a/stand/ofw/libofw/ofw_memory.c b/stand/ofw/libofw/ofw_memory.c index 5616184e8080..f05881ee8a19 100644 --- a/stand/ofw/libofw/ofw_memory.c +++ b/stand/ofw/libofw/ofw_memory.c @@ -35,9 +35,6 @@ __FBSDID("$FreeBSD$"); #include "libofw.h" #include "openfirm.h" -static void *heap_base = NULL; -static unsigned int heap_size = 0; - struct ofw_mapping { vm_offset_t va; int len; @@ -115,32 +112,3 @@ ofw_memmap(int acells) pager_close(); } -void * -ofw_alloc_heap(unsigned int size) -{ - phandle_t memoryp, root; - cell_t available[4]; - cell_t acells; - - root = OF_finddevice("/"); - acells = 1; - OF_getprop(root, "#address-cells", &acells, sizeof(acells)); - - memoryp = OF_instance_to_package(memory); - OF_getprop(memoryp, "available", available, sizeof(available)); - - heap_base = OF_claim((void *)available[acells-1], size, - sizeof(register_t)); - - if (heap_base != (void *)-1) { - heap_size = size; - } - - return (heap_base); -} - -void -ofw_release_heap(void) -{ - OF_release(heap_base, heap_size); -} diff --git a/stand/ofw/libofw/ppc64_elf_freebsd.c b/stand/ofw/libofw/ppc64_elf_freebsd.c index 9132e9d09d63..6d850297e03e 100644 --- a/stand/ofw/libofw/ppc64_elf_freebsd.c +++ b/stand/ofw/libofw/ppc64_elf_freebsd.c @@ -90,7 +90,6 @@ ppc64_ofw_elf_exec(struct preloaded_file *fp) printf("Kernel entry at 0x%lx ...\n", entry); dev_cleanup(); - ofw_release_heap(); if (dtbp != 0) { OF_quiesce(); diff --git a/stand/powerpc/ofw/ldscript.powerpc b/stand/powerpc/ofw/ldscript.powerpc index 3a2765047dbb..e0dc5e2614a8 100644 --- a/stand/powerpc/ofw/ldscript.powerpc +++ b/stand/powerpc/ofw/ldscript.powerpc @@ -9,7 +9,7 @@ PROVIDE (__stack = 0); SECTIONS { /* Read-only sections, merged into text segment: */ - . = 0x01c00000 + SIZEOF_HEADERS; + . = 0x02c00000 + SIZEOF_HEADERS; .interp : { *(.interp) } .hash : { *(.hash) } .dynsym : { *(.dynsym) }