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)                }
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to