Skip to contentSkip to conversation

Fix module relocation errors on PowerPC

PATCH In review Anton Blanchard 2 participants 5 messages Oct 18, 2011 – Nov 8, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix module relocation errors on PowerPC

Hi,

I tried grub2 on a POWER6 box and got:

Welcome to GRUB!

error: relocation overflow.
Entering rescue mode...

Modules (and therefore the heap) need to be close to the executable to avoid requiring more complicated relocations. This patch uses the same method sparc does and puts the heap directly above the executable.

We use the OF stack so we don't need to reserve any memory for it. Is it time to create GRUB_MACHINE_POWERPC?

Anton

#1 lore raw

Hi,

Quoted 3 lines

Modules (and therefore the heap) need to be close to the executable to avoid requiring more complicated relocations. This patch uses the same method sparc does and puts the heap directly above the executable.

Sparc doesn't appear to claim the heap. Adding this patch on top of the relocation one fixes things for me on PowerPC.

Any suggestions on the best way to fix it? The non Sparc version of grub_claim_heap is rather complicated and I'm not sure why it just doesn't allocate a couple of MB right above the executable. Assuming we have ~4MB of free space at the address OF loads us is pretty reasonable to me.

Anton

--

grub-core/kern/ieee1275/init.c +10 -2
Index: grub/grub-core/kern/ieee1275/init.c
===================================================================
@@ -139,8 +139,16 @@ grub_machine_get_bootlocation (char **de
 static void 
 grub_claim_heap (void)
 {
-  grub_mm_init_region ((void *) (grub_modules_get_end ()
-				 + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000);
+  grub_uint64_t addr, len;
+
+  addr = grub_modules_get_end () + GRUB_KERNEL_MACHINE_STACK_SIZE;
+  len = 0x200000;
+
+  if (grub_claimmap (addr, len) < 0)
+    grub_error (GRUB_ERR_OUT_OF_MEMORY,
+		"failed to claim heap at 0x%llx, len 0x%llx",
+		addr, len);
+  grub_mm_init_region ((void *) (grub_addr_t)addr, len);
 }
 #else
 static void 
#2 lore raw
Quoted 5 lines · Anton Blanchard

Hi,

Quoted 3 lines

Modules (and therefore the heap) need to be close to the executable to avoid requiring more complicated relocations. This patch uses the same method sparc does and puts the heap directly above the executable.

Quoted 9 lines

Sparc doesn't appear to claim the heap. Adding this patch on top of the relocation one fixes things for me on PowerPC.

Any suggestions on the best way to fix it? The non Sparc version of grub_claim_heap is rather complicated and I'm not sure why it just doesn't allocate a couple of MB right above the executable. Assuming we have ~4MB of free space at the address OF loads us is pretty reasonable to me.

This is an eternal course to restrict usable space to supported relocations. It should be the other way round. I'll prepare a patch for adding necessary trampolines.

Quoted 33 lines

Anton

--

Index: grub/grub-core/kern/ieee1275/init.c
===================================================================

grub-core/kern/ieee1275/init.c +10 -2
@@ -139,8 +139,16 @@ grub_machine_get_bootlocation (char **de
 static void 
 grub_claim_heap (void)
 {
-  grub_mm_init_region ((void *) (grub_modules_get_end ()
-				 + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000);
+  grub_uint64_t addr, len;
+
+  addr = grub_modules_get_end () + GRUB_KERNEL_MACHINE_STACK_SIZE;
+  len = 0x200000;
+
+  if (grub_claimmap (addr, len) < 0)
+    grub_error (GRUB_ERR_OUT_OF_MEMORY,
+		"failed to claim heap at 0x%llx, len 0x%llx",
+		addr, len);
+  grub_mm_init_region ((void *) (grub_addr_t)addr, len);
 }
 #else
 static void 

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'φ-coder/phcoder' Serbinenko
#3 lore raw
In reply to Anton Blanchard

Please try the attached patch

Quoted 25 lines · Anton Blanchard

Hi,

I tried grub2 on a POWER6 box and got:

Welcome to GRUB!

error: relocation overflow.
Entering rescue mode...

Modules (and therefore the heap) need to be close to the executable to avoid requiring more complicated relocations. This patch uses the same method sparc does and puts the heap directly above the executable.

We use the OF stack so we don't need to reserve any memory for it. Is it time to create GRUB_MACHINE_POWERPC?

Anton

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'φ-coder/phcoder' Serbinenko

=== modified file 'grub-core/kern/dl.c'

kern/dl.c +7 -13
@@ -233,7 +233,7 @@
   unsigned i;
   Elf_Shdr *s;
   grub_size_t tsize = 0, talign = 1;
-#ifdef __ia64__
+#if defined (__ia64__) || defined (__powerpc__)
   grub_size_t tramp;
   grub_size_t got;
 #endif
@@ -248,9 +248,9 @@
 	talign = s->sh_addralign;
     }
 
-#ifdef __ia64__
+#if defined (__ia64__) || defined (__powerpc__)
   grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
-  tramp *= GRUB_IA64_DL_TRAMP_SIZE;
+  tramp *= GRUB_ARCH_DL_TRAMP_SIZE;
   got *= sizeof (grub_uint64_t);
   tsize += ALIGN_UP (tramp, GRUB_ARCH_DL_TRAMP_ALIGN);
   if (talign < GRUB_ARCH_DL_TRAMP_ALIGN)
@@ -316,7 +316,7 @@
 	  mod->segment = seg;
 	}
     }
-#ifdef __ia64__
+#if defined (__ia64__) || defined (__powerpc__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   ptr += tramp;
@@ -575,15 +575,9 @@
 static void
 grub_dl_flush_cache (grub_dl_t mod)
 {
-  grub_dl_segment_t seg;
-
-  for (seg = mod->segment; seg; seg = seg->next) {
-    if (seg->size) {
-      grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n",
-		    (unsigned long) seg->size, seg->addr);
-      grub_arch_sync_caches (seg->addr, seg->size);
-    }
-  }
+  grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n",
+		(unsigned long) mod->sz, mod->base);
+  grub_arch_sync_caches (mod->base, mod->sz);
 }
 
 /* Load a module from core memory.  */

=== modified file 'grub-core/kern/ia64/dl.c'

kern/ia64/dl.c +2 -2
@@ -104,13 +104,13 @@
     }
 }
 
-static grub_uint8_t nopm[5] =
+static const grub_uint8_t nopm[5] =
   {
     /* [MLX]       nop.m 0x0 */
     0x05, 0x00, 0x00, 0x00, 0x01
   };
 
-static grub_uint8_t jump[0x20] =
+static const grub_uint8_t jump[0x20] =
   {
     /* ld8 r16=[r15],8 */
     0x02, 0x80, 0x20, 0x1e, 0x18, 0x14,

=== modified file 'grub-core/kern/powerpc/dl.c'

kern/powerpc/dl.c +72 -0

90 lines not on this page · raw message

=== modified file 'include/grub/dl.h'

grub/dl.h +13 -2
@@ -136,11 +136,12 @@
   Elf_Sym *symtab;
   void (*init) (struct grub_dl *mod);
   void (*fini) (void);
-#ifdef __ia64__
+#if defined (__ia64__) || defined (__powerpc__)
   void *got;
   void *tramp;
 #endif
   void *base;
+  grub_size_t sz;
   struct grub_dl *next;
 };
 typedef struct grub_dl *grub_dl_t;
@@ -176,10 +177,20 @@
 grub_ia64_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
 				 grub_size_t *got);
 
-#ifdef __ia64__
+#if defined (__ia64__)
 #define GRUB_ARCH_DL_TRAMP_ALIGN 16
 #define GRUB_ARCH_DL_GOT_ALIGN 16
 #define grub_arch_dl_get_tramp_got_size grub_ia64_dl_get_tramp_got_size
+#else
+void
+grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
+				 grub_size_t *got);
+#endif
+
+#ifdef __powerpc__
+#define GRUB_ARCH_DL_TRAMP_SIZE 16
+#define GRUB_ARCH_DL_TRAMP_ALIGN 4
+#define GRUB_ARCH_DL_GOT_ALIGN 4
 #endif
 
 #endif

=== modified file 'include/grub/kernel.h'

grub/kernel.h +1 -1
@@ -34,7 +34,7 @@
 struct grub_module_header
 {
   /* The type of object.  */
-  grub_uint8_t type;
+  grub_uint32_t type;
   /* The size of object (including this header).  */
   grub_uint32_t size;
 };
#4 lore raw

Hi Vladimir,

Quoted 1 line

Please try the attached patch

Thanks! I tested it and it does fix the issue.

Anton