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
This commit is contained in:
Nathan Whitehorn 2018-02-03 23:49:21 +00:00
parent 41fc6f680b
commit 861a0b4808
7 changed files with 5 additions and 47 deletions

View File

@ -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

View File

@ -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,

View File

@ -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;

View File

@ -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))

View File

@ -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);
}

View File

@ -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();

View File

@ -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) }