Skip to contentSkip to conversation

ARM branch

PATCH Vladimir 'φ-coder/phcoder' Serbinenko 3 participants 6 messages Apr 12, 2013 – May 7, 2013 Kbuild

lore
Share & tools
lore
#0 lore raw

Hello, all. I've uploaded Leif's and my ARM code to
http://bzr.savannah.gnu.org/lh/grub/branches/arm/changes. Francesco's code will be added as well once his papers are done. The only dirty part for Raspberry pi that I didn't upload is:
=== modified file 'conf/Makefile.common'

Makefile.common +0 -0
@@ -40,8 +40,7 @@
 if COND_arm
 # Image entry point always in ARM (A32) state - ensure proper

functionality if

 # the rest is built for the Thumb (T32) state.
-  CFLAGS_PLATFORM += -mthumb-interwork -mno-unaligned-access -mlong-calls
-  CCASFLAGS_PLATFORM = -Wa,-mimplicit-it=thumb
+  CFLAGS_PLATFORM += -mthumb-interwork -march=armv6 -mlong-calls
   LDFLAGS_PLATFORM = -Wl,--wrap=__clear_cache
 endif

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

kern/dl.c +1 -1
@@ -588,7 +588,7 @@
 {
   grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n",
 		(unsigned long) mod->sz, mod->base);
-  grub_arch_sync_caches (mod->base, mod->sz);
+  //  grub_arch_sync_caches (mod->base, mod->sz);
 }
 
 /* Load a module from core memory.  */
#1 lore raw
Quoted 4 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Hello, all. I've uploaded Leif's and my ARM code to
http://bzr.savannah.gnu.org/lh/grub/branches/arm/changes. Francesco's code will be added as well once his papers are done. The only dirty part for Raspberry pi that I didn't upload is:

I have attached a patch (against this branch) that contains build system fixes and changes to the assembly files in order to permit the same target-platform combination to build correctly on both ARMv6 and ARMv7, with no special options required.

It does also revert the removal of uboot_get_real_bss_start, since this causes GRUB to fail even reaching "Welcome to GRUB!" on my Trimslice. It may not be the best solution, but it is in there for a reason.

There are also some "collateral" changes in this patch, holding other fixes I have in my launchpad branch. Sorry about those.

This _should_ make your out-of-tree patch unnecessary.

Quoted 32 lines

=== modified file 'conf/Makefile.common'

Makefile.common +0 -0
@@ -40,8 +40,7 @@
 if COND_arm
 # Image entry point always in ARM (A32) state - ensure proper

functionality if

 # the rest is built for the Thumb (T32) state.
Makefile.common +1 -2
-  CFLAGS_PLATFORM += -mthumb-interwork -mno-unaligned-access -mlong-calls
-  CCASFLAGS_PLATFORM = -Wa,-mimplicit-it=thumb
+  CFLAGS_PLATFORM += -mthumb-interwork -march=armv6 -mlong-calls
   LDFLAGS_PLATFORM = -Wl,--wrap=__clear_cache
 endif

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

kern/dl.c +1 -1
@@ -588,7 +588,7 @@
 {
   grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n",
 		(unsigned long) mod->sz, mod->base);
-  grub_arch_sync_caches (mod->base, mod->sz);
+  //  grub_arch_sync_caches (mod->base, mod->sz);
 }
 
 /* Load a module from core memory.  */
Quoted 4 lines

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

=== modified file 'acinclude.m4'

acinclude.m4 +20 -0
@@ -458,3 +458,23 @@
 AC_DEFUN([grub_TRANSFORM],[dnl
 AC_SUBST(AS_TR_SH([$1]), [`AS_ECHO([$1]) | sed "$program_transform_name"`])dnl
 ])
+
+dnl Check if the C compiler supports `-mno-unaligned-access'.
+AC_DEFUN([grub_CHECK_NO_UNALIGNED_ACCESS],[
+[# foobar
+nua_possible=yes]
+AC_MSG_CHECKING([whether `$CC' supports `-mno-unaligned-access'])
+AC_LANG_CONFTEST([AC_LANG_SOURCE([[
+int main() {
+	return 0;
+}
+]])])
+
+[if eval "$ac_compile -S -mno-unaligned-access -o conftest.s" 2> /dev/null; then]
+  AC_MSG_RESULT([yes])
+  [rm -f conftest.s
+else
+  nua_possible=no]
+  AC_MSG_RESULT([no])
+[fi]
+])

=== modified file 'conf/Makefile.common'

Makefile.common +1 -4
@@ -38,10 +38,7 @@
   LDFLAGS_PLATFORM = -Wl,-melf64_sparc -mno-relax
 endif
 if COND_arm
-# Image entry point always in ARM (A32) state - ensure proper functionality if
-# the rest is built for the Thumb (T32) state.
-  CFLAGS_PLATFORM += -mthumb-interwork -mno-unaligned-access -mlong-calls
-  CCASFLAGS_PLATFORM = -Wa,-mimplicit-it=thumb
+  CFLAGS_PLATFORM += -mthumb-interwork -mlong-calls
   LDFLAGS_PLATFORM = -Wl,--wrap=__clear_cache
 endif
 

=== modified file 'configure.ac'

configure.ac +8 -0
@@ -656,6 +656,14 @@
   TARGET_CFLAGS="$TARGET_CFLAGS -mno-stack-arg-probe"
 fi
 
+# -mno-unaligned-access
+if test "$platform" = uboot; then
+  grub_CHECK_NO_UNALIGNED_ACCESS
+  if test x"$nua_possible" = xyes; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -mno-unaligned-access"
+  fi
+fi
+
 AC_ARG_ENABLE([werror],
 	      [AS_HELP_STRING([--disable-werror],
                              [do not use -Werror when building GRUB])])

=== modified file 'grub-core/kern/arm/cache.S'

kern/arm/cache.S +56 -63

231 lines not on this page · raw message

=== modified file 'grub-core/kern/arm/uboot/startup.S'

kern/arm/uboot/startup.S +17 -11
@@ -73,20 +73,13 @@
 	@ Modules have been stored as a blob in BSS,
 	@ they need to be manually relocated to _end or
 	@ (__bss_start + grub_total_module_size), whichever greater.
-	ldr	r0, =EXT_C(__bss_start)		@ src
-	add	r0, r0, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
-	mvn	r1, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
-	and	r0, r0, r1
-
+	bl	uboot_get_real_bss_start	@ r0 = src
 	ldr	r1, =EXT_C(_end)		@ dst = End of BSS
 	ldr	r2, grub_total_module_size	@ blob size
 	add	r3, r0, r2			@ blob end
 	cmp	r1, r3				@ _end < blob end?
 	movlt	r1, r3				@ dst = blob end + blob size
-
-	ldr     r12, =EXT_C(grub_modbase)
-	str     r1, [r12]
-
+	
 1:	ldr	r3, [r0], #4 			@ r3 = *src++ 
 	str	r3, [r1], #4			@ *dst++ = r3 
 	subs	r2, #4				@ remaining -= 4
@@ -99,16 +92,29 @@
 
 	@ Since we _are_ the C run-time, we need to manually zero the BSS
 	@ region before continuing
-	ldr	r0, =EXT_C(__bss_start)	@ zero from here
+	bl	uboot_get_real_bss_start	@ zero from here
 	ldr	r1, =EXT_C(_end)		@ to here
 	mov	r2, #0
 1:	str	r2, [r0], #4
 	cmp	r0, r1
 	bne	1b
-	
+
 	b	EXT_C(grub_main)
 
 	/*
+	 * __bss_start does not actually point to the start of the runtime
+	 * BSS, but rather to the next byte following the preceding data.
+	 */
+FUNCTION (uboot_get_real_bss_start)
+	ldr	r0, =EXT_C(__bss_start)		@ src
+	tst	r0, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
+	beq	1f
+	mvn	r1, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
+	and	r0, r0, r1
+	add	r0, r0, #(GRUB_KERNEL_MACHINE_MOD_ALIGN)
+1:	bx	lr
+
+	/*
 	 * uboot_syscall():
 	 *   This function is effectively a veneer, so it cannot
 	 *   modify the stack or corrupt any registers other than

=== modified file 'grub-core/kern/uboot/init.c'

kern/uboot/init.c +17 -6
@@ -69,6 +69,7 @@
 void
 grub_machine_init (void)
 {
+  grub_addr_t end, real_bss_start;
   int ver;
 
   /* First of all - establish connection with U-Boot */
@@ -84,14 +85,26 @@
       uboot_puts ("invalid U-Boot API version\n");
     }
 
+  /*
+   * Modules were relocated to _end, or __bss_start + grub_total_module_size,
+   * whichever greater. (And __bss_start may not point to actual BSS start...)
+   */
+  real_bss_start = uboot_get_real_bss_start ();
+  end = real_bss_start + grub_total_module_size;
+  if (end < (grub_addr_t) _end)
+    end = (grub_addr_t) _end;
+  grub_modbase = end;
+
   /* Initialize the console so that GRUB can display messages.  */
   grub_console_init_early ();
 
   /* Enumerate memory and initialize the memory management system. */
   grub_uboot_mm_init ();
 
-  grub_dprintf ("init", "__bss_start: %p\n", __bss_start);
-  grub_dprintf ("init", "_end: %p\n", _end);
+  grub_dprintf ("init", "__bss_start: 0x%08x, real_bss_start: 0x%08x\n",
+		(grub_addr_t) __bss_start, real_bss_start);
+  grub_dprintf ("init", "end: 0x%08x, _end: 0x%08x\n",
+		(grub_addr_t) end, (grub_addr_t) _end);
   grub_dprintf ("init", "grub_modbase: %p\n", (void *) grub_modbase);
   grub_dprintf ("init", "grub_modules_get_end(): %p\n",
 		(void *) grub_modules_get_end ());
@@ -130,10 +143,9 @@
   tmp = uboot_env_get ("grub_bootdev");
   if (tmp)
     {
-      *device = grub_malloc (grub_strlen (tmp) + 1);
+      *device = grub_strdup (tmp);
       if (*device == NULL)
 	return;
-      grub_strncpy (*device, tmp, grub_strlen (tmp) + 1);
     }
   else
     *device = NULL;
@@ -141,10 +153,9 @@
   tmp = uboot_env_get ("grub_bootpath");
   if (tmp)
     {
-      *path = grub_malloc (grub_strlen (tmp) + 1);
+      *path = grub_strdup (tmp);
       if (*path == NULL)
 	return;
-      grub_strncpy (*path, tmp, grub_strlen (tmp) + 1);
     }
   else
     *path = NULL;

=== modified file 'grub-core/lib/arm/setjmp.S'

lib/arm/setjmp.S +2 -15
@@ -17,19 +17,10 @@
  */
 
 #include <grub/symbol.h>
-#include <grub/dl.h>
 
 	.file	"setjmp.S"
 	.syntax	unified
-#if !defined (__thumb2__)
 	.arm
-#define ARM(x...)	x
-#define THUMB(x...)
-#else
-	.thumb
-#define THUMB(x...)	x
-#define ARM(x...)
-#endif
 
 	.text
 
@@ -37,9 +28,7 @@
  * int grub_setjmp (grub_jmp_buf env)
  */
 FUNCTION(grub_setjmp)
- THUMB(	mov	ip, sp			)
- THUMB(	stm	r0, { r4-r11, ip, lr }	)
- ARM(	stm	r0, { r4-r11, sp, lr }	)
+	stm	r0, { r4-r11, sp, lr }
 	mov	r0, #0
 	bx	lr
 
@@ -47,9 +36,7 @@
  * int grub_longjmp (grub_jmp_buf env, int val)
  */
 FUNCTION(grub_longjmp)
- THUMB(	ldm	r0, { r4-r11, ip, lr }	)
- THUMB(	mov	sp, ip			)
- ARM(	ldm	r0, { r4-r11, sp, lr }	)
+ 	ldm	r0, { r4-r11, sp, lr }
 	movs	r0, r1
 	moveq	r0, #1
 	bx	lr

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

grub/libgcc.h +0 -1
@@ -121,6 +121,5 @@
 void EXPORT_FUNC (__aeabi_llsr) (void);
 void EXPORT_FUNC (__aeabi_uidiv) (void);
 void EXPORT_FUNC (__aeabi_uidivmod) (void);
-void EXPORT_FUNC (__wrap___clear_cache) (void *, void *);
 void EXPORT_FUNC (__aeabi_ulcmp) (void);
 #endif

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

grub/symbol.h +1 -5
@@ -29,11 +29,7 @@
 
 #if HAVE_ASM_USCORE
 #ifdef ASM_FILE
-# ifndef (__arm__)
-#  define EXT_C(sym)	_ ## sym
-# else
-#  define EXT_C(sym)	% ## sym
-# endif
+# define EXT_C(sym)	_ ## sym
 #else
 # define EXT_C(sym)	"_" sym
 #endif
#2 lore raw
Quoted 10 lines · Leif Lindholm
Quoted 4 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 4 lines

Hello, all. I've uploaded Leif's and my ARM code to
http://bzr.savannah.gnu.org/lh/grub/branches/arm/changes. Francesco's code will be added as well once his papers are done. The only dirty part for Raspberry pi that I didn't upload is:

I have attached a patch (against this branch) that contains build system fixes and changes to the assembly files in order to permit the same target-platform combination to build correctly on both ARMv6 and ARMv7, with no special options required.

Quoted 4 lines

It does also revert the removal of uboot_get_real_bss_start, since this causes GRUB to fail even reaching "Welcome to GRUB!" on my Trimslice. It may not be the best solution, but it is in there for a reason.

Why was this revert necessary? The C code and asm code seem to mirror each other

#4 lore raw
In reply to Leif Lindholm

Hi,

Quoted 10 lines · Leif Lindholm
Quoted 4 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Hello, all. I've uploaded Leif's and my ARM code to
http://bzr.savannah.gnu.org/lh/grub/branches/arm/changes. Francesco's code will be added as well once his papers are done. The only dirty part for Raspberry pi that I didn't upload is:

I have attached a patch (against this branch) that contains build system fixes and changes to the assembly files in order to permit the same target-platform combination to build correctly on both ARMv6 and ARMv7, with no special options required.

[...]

Quoted 3 lines

=== modified file 'grub-core/kern/arm/cache.S'

kern/arm/cache.S +0 -0

[...]

Quoted 14 lines
kern/arm/cache.S +3 -2
@@ -149,21 +137,26 @@
 clean_invalidate_dcache:
 	push	{r4-r12, lr}
 	mrc 	p15, 1, r0, c0, c0, 1	@ Read CLIDR
-	ubfx	r1, r0, #24, #3		@ Extract LoC
-	
+	lsr	r1, r0, #24		@ Extract LoC
+	and	r1, r1, #0x7
+
 	mov	r2, #0			@ First level, L1
 2:	and	r8, r0, #7		@ cache type at current level
 	cmp	r8, #2
 	blt	5f			@ instruction only, or none, skip level
 

kern/arm/cache.S line 153

Quoted 10 lines
kern/arm/cache.S +2 -4
-	@ set current cache level/type (for CSSIDR read)
+	@ set current cache level/type (for CCSIDR read)
 	lsl	r8, r2, #1
 	mcr	p15, 2, r8, c0, c0, 0	@ Write CSSELR (level, type: data/uni)
 
 	@ read current cache information
-	mrc	p15, 1, r8, c0, c0, 0	@ Read CSSIDR
-	ubfx	r3, r8, #13, #14	@ Number of sets -1
-	ubfx	r4, r8, #3, #9		@ Number of ways -1
+	mrc	p15, 1, r8, c0, c0, 0	@ Read CCSIDR

Unfortunately, in ARMv6 there are no such things as CLIDR, CSSELR and CCSIDR.

--
Francesco

#3 lore raw
In reply to Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 7 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 4 lines

It does also revert the removal of uboot_get_real_bss_start, since this causes GRUB to fail even reaching "Welcome to GRUB!" on my Trimslice. It may not be the best solution, but it is in there for a reason.

Why was this revert necessary? The C code and asm code seem to mirror each other

The diference is in the assigning of grub_modbase, which ends up pointing at an unaligned address, since __bss_start is not pointing to the first variable allocated in BSS, but at the byte immediately following the preceding segment.
Since the preceding segment is the license one, containing only strings, that segment may end on any alignment - and currently my build includes license statements in the kernel, since (for example) term/terminfo.c has a GRUB_MOD_LICENSE unconditionally included, even when built into the kernel.

I should however simply have moved this assignment into startup.S. Patch attached.

/
Leif

=== modified file 'grub-core/kern/arm/uboot/startup.S'

kern/arm/uboot/startup.S +2 -0
@@ -79,6 +79,8 @@
 	add	r3, r0, r2			@ blob end
 	cmp	r1, r3				@ _end < blob end?
 	movlt	r1, r3				@ dst = blob end + blob size
+	ldr	r12, =EXT_C(grub_modbase)
+	str	r1, [r12]			@ grub_modbase = dst
 	
 1:	ldr	r3, [r0], #4 			@ r3 = *src++ 
 	str	r3, [r1], #4			@ *dst++ = r3 

=== modified file 'grub-core/kern/uboot/init.c'

kern/uboot/init.c +0 -17
@@ -31,8 +31,6 @@
 #include <grub/uboot/uboot.h>
 #include <grub/uboot/api_public.h>
 
-extern char __bss_start[];
-extern char _end[];
 extern grub_size_t grub_total_module_size;
 extern int (*grub_uboot_syscall_ptr) (int, int *, ...);
 
@@ -70,7 +68,6 @@
 void
 grub_machine_init (void)
 {
-  grub_addr_t end, real_bss_start;
   int ver;
 
   /* First of all - establish connection with U-Boot */
@@ -86,26 +83,12 @@
       grub_uboot_puts ("invalid U-Boot API version\n");
     }
 
-  /*
-   * Modules were relocated to _end, or __bss_start + grub_total_module_size,
-   * whichever greater. (And __bss_start may not point to actual BSS start...)
-   */
-  real_bss_start = grub_uboot_get_real_bss_start ();
-  end = real_bss_start + grub_total_module_size;
-  if (end < (grub_addr_t) _end)
-    end = (grub_addr_t) _end;
-  grub_modbase = end;
-
   /* Initialize the console so that GRUB can display messages.  */
   grub_console_init_early ();
 
   /* Enumerate memory and initialize the memory management system. */
   grub_uboot_mm_init ();
 
-  grub_dprintf ("init", "__bss_start: 0x%08x, real_bss_start: 0x%08x\n",
-		(grub_addr_t) __bss_start, real_bss_start);
-  grub_dprintf ("init", "end: 0x%08x, _end: 0x%08x\n",
-		(grub_addr_t) end, (grub_addr_t) _end);
   grub_dprintf ("init", "grub_modbase: %p\n", (void *) grub_modbase);
   grub_dprintf ("init", "grub_modules_get_end(): %p\n",
 		(void *) grub_modules_get_end ());
#5 lore raw
In reply to Francesco Lavra
Quoted 4 lines · Francesco Lavra
Quoted 4 lines

I have attached a patch (against this branch) that contains build system fixes and changes to the assembly files in order to permit the same target-platform combination to build correctly on both ARMv6 and ARMv7, with no special options required.

[...]

Quoted 2 lines

Unfortunately, in ARMv6 there are no such things as CLIDR, CSSELR and CCSIDR.

Doh!
On the plus side, there is a magic "clean/invalidate the whole D-cache" instruction...

I have no ARMv6 platform handy, but attached is a conditionalised version, including some additional changes, that _should_ work on ARMv6. Could someone give it a go?

/
Leif

=== modified file 'grub-core/kern/arm/cache.S'

kern/arm/cache.S +37 -18
@@ -51,7 +51,11 @@
 	and	r0, r0, r3
 1:	cmp	r0, r1
 	bge	2f
+#if (__ARM_ARCH_6__ == 1)
+	mcr	p15, 0, r0, c7, c10, 1	@ Clean data cache line by MVA
+#else
 	mcr	p15, 0, r0, c7, c11, 1	@ DCCMVAU
+#endif
 	add	r0, r0, r2		@ Next line
 	b	1b
 2:	DSB
@@ -77,37 +81,47 @@
 	bx	lr
 
 sync_caches:
-	DMB
 	DSB
-	push	{r4-r6, lr}
+	push	{r0-r1, r4-r6, lr}
 	ldr	r2, probed	@ If first call, probe cache sizes
 	cmp	r2, #0
-	bleq	probe_caches	@ This call corrupts r3
-	mov	r4, r0
-	mov	r5, r1
+	bleq	probe_caches
+	ldrdeq	r0, r1, [sp]
 	bl	clean_dcache_range
-	mov	r0, r4
-	mov	r1, r5
+	pop	{r0, r1}
 	bl	invalidate_icache_range
 	pop	{r4-r6, pc}
 
 probe_caches:
 	push	{r4-r6, lr}
-	mrc 	p15, 0, r4, c0, c0, 1	@ Read Cache Type Register
-	mov	r5, #1
-	lsr	r6, r4, #16		@ Extract min D-cache num word log2
-	and	r6, r6, #0xf
-	add	r6, r6, #2		@ words->bytes
-	lsl	r6, r5, r6		@ Convert to num bytes
+	mrc 	p15, 0, r0, c0, c0, 1	@ Read Cache Type Register
+	mov	r1, #1
+@ Cache Type Register format changed in ARMv7
+@ r5 - dlinesz
+@ r6 - ilinesz
+#if (__ARM_ARCH_6__ == 1)
+	lsl	r2, r0, #12
+	and	r2, r2, #3		@ Dsize 'len'
+	lsl	r2, r1, r2		@ Convert to num 8-byte blocks
+	lsl	r5, r2, #3		@ Convert to num bytes
+	and	r2, r0, #3		@ Isize 'len'
+	lsl	r2, r1, r2		@ Convert to num 8-byte blocks
+	lsl	r6, r2, #3		@ Convert to num bytes
+#else
+	lsr	r2, r0, #16		@ Extract min D-cache num word log2
+	and	r2, r2, #0xf
+	add	r2, r2, #2		@ words->bytes
+	lsl	r5, r1, r2		@ Convert to num bytes
+	and	r2, r0, #0xf		@ Extract min I-cache num word log2
+	add	r2, r2, #2		@ words->bytes
+	lsl	r6, r1, r2		@ Convert to num bytes
+#endif
 	ldr	r3, =dlinesz
-	str	r6, [r3]
-	and	r6, r4, #0xf		@ Extract min I-cache num word log2
-	add	r6, r6, #2		@ words->bytes
-	lsl	r6, r5, r6		@ Convert to num bytes
+	str	r5, [r3]
 	ldr	r3, =ilinesz
 	str	r6, [r3]
 	ldr	r3, =probed		@ Flag cache probing done
-	str	r5, [r3]
+	str	r1, [r3]
 	pop	{r4-r6, pc}
 
 	.align	3
@@ -135,6 +149,10 @@
 	@ r10 - scratch
 	@ r11 - scratch
 clean_invalidate_dcache:
+#if (__ARM_ARCH_6__ == 1)
+	mcr	p15, 0, r0, c7, c14, 0	@ Clean/Invalidate D-cache
+	bx	lr
+#elif (__ARM_ARCH_7A__ == 1)
 	push	{r4-r12, lr}
 	mrc 	p15, 1, r0, c0, c0, 1	@ Read CLIDR
 	lsr	r1, r0, #24		@ Extract LoC
@@ -204,6 +222,7 @@
 6:	DSB
 	ISB
 	pop	{r4-r12, pc}
+#endif
 
 FUNCTION(grub_arm_disable_caches_mmu)
 	push	{r4, lr}