From owner-svn-src-all@freebsd.org Sat Feb 3 23:49:23 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E1C56ED3179; Sat, 3 Feb 2018 23:49:22 +0000 (UTC) (envelope-from nwhitehorn@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 8B26D81B30; Sat, 3 Feb 2018 23:49:22 +0000 (UTC) (envelope-from nwhitehorn@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8555011EB9; Sat, 3 Feb 2018 23:49:22 +0000 (UTC) (envelope-from nwhitehorn@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w13NnMCS009792; Sat, 3 Feb 2018 23:49:22 GMT (envelope-from nwhitehorn@FreeBSD.org) Received: (from nwhitehorn@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w13NnLIN009785; Sat, 3 Feb 2018 23:49:21 GMT (envelope-from nwhitehorn@FreeBSD.org) Message-Id: <201802032349.w13NnLIN009785@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: nwhitehorn set sender to nwhitehorn@FreeBSD.org using -f From: Nathan Whitehorn Date: Sat, 3 Feb 2018 23:49:21 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r328835 - in head/stand: ofw/common ofw/libofw powerpc/ofw X-SVN-Group: head X-SVN-Commit-Author: nwhitehorn X-SVN-Commit-Paths: in head/stand: ofw/common ofw/libofw powerpc/ofw X-SVN-Commit-Revision: 328835 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 03 Feb 2018 23:49:23 -0000 Author: nwhitehorn Date: Sat Feb 3 23:49:21 2018 New Revision: 328835 URL: https://svnweb.freebsd.org/changeset/base/328835 Log: 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 Modified: head/stand/ofw/common/main.c head/stand/ofw/libofw/elf_freebsd.c head/stand/ofw/libofw/libofw.h head/stand/ofw/libofw/ofw_copy.c head/stand/ofw/libofw/ofw_memory.c head/stand/ofw/libofw/ppc64_elf_freebsd.c head/stand/powerpc/ofw/ldscript.powerpc Modified: head/stand/ofw/common/main.c ============================================================================== --- head/stand/ofw/common/main.c Sat Feb 3 23:14:11 2018 (r328834) +++ head/stand/ofw/common/main.c Sat Feb 3 23:49:21 2018 (r328835) @@ -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 Modified: head/stand/ofw/libofw/elf_freebsd.c ============================================================================== --- head/stand/ofw/libofw/elf_freebsd.c Sat Feb 3 23:14:11 2018 (r328834) +++ head/stand/ofw/libofw/elf_freebsd.c Sat Feb 3 23:49:21 2018 (r328835) @@ -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, Modified: head/stand/ofw/libofw/libofw.h ============================================================================== --- head/stand/ofw/libofw/libofw.h Sat Feb 3 23:14:11 2018 (r328834) +++ head/stand/ofw/libofw/libofw.h Sat Feb 3 23:49:21 2018 (r328835) @@ -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; Modified: head/stand/ofw/libofw/ofw_copy.c ============================================================================== --- head/stand/ofw/libofw/ofw_copy.c Sat Feb 3 23:14:11 2018 (r328834) +++ head/stand/ofw/libofw/ofw_copy.c Sat Feb 3 23:49:21 2018 (r328835) @@ -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)) Modified: head/stand/ofw/libofw/ofw_memory.c ============================================================================== --- head/stand/ofw/libofw/ofw_memory.c Sat Feb 3 23:14:11 2018 (r328834) +++ head/stand/ofw/libofw/ofw_memory.c Sat Feb 3 23:49:21 2018 (r328835) @@ -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); -} Modified: head/stand/ofw/libofw/ppc64_elf_freebsd.c ============================================================================== --- head/stand/ofw/libofw/ppc64_elf_freebsd.c Sat Feb 3 23:14:11 2018 (r328834) +++ head/stand/ofw/libofw/ppc64_elf_freebsd.c Sat Feb 3 23:49:21 2018 (r328835) @@ -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(); Modified: head/stand/powerpc/ofw/ldscript.powerpc ============================================================================== --- head/stand/powerpc/ofw/ldscript.powerpc Sat Feb 3 23:14:11 2018 (r328834) +++ head/stand/powerpc/ofw/ldscript.powerpc Sat Feb 3 23:49:21 2018 (r328835) @@ -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) }