Skip to contentSkip to conversation

powerpc/boot/fdt: Use unsigned long for pointer casts

PATCH In review Jeremy Kerr 2 participants 6 messages Feb 11, 2015 – Feb 16, 2015 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/5 powerpc/boot/fdt: Use unsigned long for pointer casts

Now that the wrapper supports 64-bit builds, we see warnings when attempting to cast pointers to int. Use unsigned long instead.

---
 arch/powerpc/boot/libfdt-wrapper.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/powerpc/boot/libfdt-wrapper.c +3 -3
index bb8b9b3..535e8fd 100644
@@ -44,12 +44,12 @@
 
 #define offset_devp(off)	\
 	({ \
-		int _offset = (off); \
+		unsigned long _offset = (off); \
 		check_err(_offset) ? NULL : (void *)(_offset+1); \
 	})
 
-#define devp_offset_find(devp)	(((int)(devp))-1)
-#define devp_offset(devp)	(devp ? ((int)(devp))-1 : 0)
+#define devp_offset_find(devp)	(((unsigned long)(devp))-1)
+#define devp_offset(devp)	(devp ? ((unsigned long)(devp))-1 : 0)
 
 static void *fdt;
 static void *buf; /* = NULL */
#1 lore raw

PATCH v2 2/5 powerpc/boot/fdt: Add little-endian support to libfdt wrappers

For epapr-style boot, we may be little-endian. This change implements the proper conversion for fdt*_to_cpu and cpu_to_fdt*. We also need the full cpu_to_* and *_to_cpu macros for this.

---
 arch/powerpc/boot/libfdt_env.h |   14 ++++++++------
 arch/powerpc/boot/of.h         |    8 ++++++++
 2 files changed, 16 insertions(+), 6 deletions(-)
arch/powerpc/boot/libfdt_env.h +8 -6
index c89fdb1..8dcd744 100644
@@ -4,15 +4,17 @@
 #include <types.h>
 #include <string.h>
 
+#include "of.h"
+
 typedef u32 uint32_t;
 typedef u64 uint64_t;
 typedef unsigned long uintptr_t;
 
-#define fdt16_to_cpu(x)		(x)
-#define cpu_to_fdt16(x)		(x)
-#define fdt32_to_cpu(x)		(x)
-#define cpu_to_fdt32(x)		(x)
-#define fdt64_to_cpu(x)		(x)
-#define cpu_to_fdt64(x)		(x)
+#define fdt16_to_cpu(x)		be16_to_cpu(x)
+#define cpu_to_fdt16(x)		cpu_to_be16(x)
+#define fdt32_to_cpu(x)		be32_to_cpu(x)
+#define cpu_to_fdt32(x)		cpu_to_be32(x)
+#define fdt64_to_cpu(x)		be64_to_cpu(x)
+#define cpu_to_fdt64(x)		cpu_to_be64(x)
 
 #endif /* _ARCH_POWERPC_BOOT_LIBFDT_ENV_H */
arch/powerpc/boot/of.h +8 -0
index c8c1750..5603320 100644
@@ -24,11 +24,19 @@ void of_console_init(void);
 typedef u32			__be32;
 
 #ifdef __LITTLE_ENDIAN__
+#define cpu_to_be16(x) swab16(x)
+#define be16_to_cpu(x) swab16(x)
 #define cpu_to_be32(x) swab32(x)
 #define be32_to_cpu(x) swab32(x)
+#define cpu_to_be64(x) swab64(x)
+#define be64_to_cpu(x) swab64(x)
 #else
+#define cpu_to_be16(x) (x)
+#define be16_to_cpu(x) (x)
 #define cpu_to_be32(x) (x)
 #define be32_to_cpu(x) (x)
+#define cpu_to_be64(x) (x)
+#define be64_to_cpu(x) (x)
 #endif
 
 #define PROM_ERROR (-1u)
#2 lore raw

Re: [PATCH 2/5 v2] powerpc/boot/fdt: Add little-endian support to libfdt wrappers

Quoted 6 lines · Jeremy Kerr

For epapr-style boot, we may be little-endian. This change implements the proper conversion for fdt*_to_cpu and cpu_to_fdt*. We also need the full cpu_to_* and *_to_cpu macros for this.

I did not work on the little endian epapr wrapper :/

To test the patchset, I used the 'le' branch of git://github.com/jk-ozlabs/linux
and successfully booted a tuleta and an open-power system using the latest skiboot and a LE zImage.epapr. Looks good to me.

You might want to add CONFIG_IPMI_WATCHDOG to openpower_defconfig

Thanks.

C.

#3 lore raw

PATCH v2 3/5 powerpc/boot/wrapper: use the pseries wrapper for zImage.epapr

In reply to Jeremy Kerr

We'll likely be entering the zImage.epapr as BE, so include the pseries implementation of _zimage_start, which adds the endian fixup magic.

Although the endian fixup won't work on Book III-E machines starting LE, the current entry point doesn't support LE anyway, so we shouldn't be breaking anything.

---
 arch/powerpc/boot/wrapper |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/boot/wrapper +1 -1
index ae0f88e..3f50c27 100755
@@ -277,7 +277,7 @@ treeboot-iss4xx-mpic)
     platformo="$object/treeboot-iss4xx.o"
     ;;
 epapr)
-    platformo="$object/epapr.o $object/epapr-wrapper.o"
+    platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
     link_address='0x20000000'
     pie=-pie
     ;;
#4 lore raw

PATCH v2 4/5 powerpc/boot: Fix stack corruption in epapr entry point

In reply to Jeremy Kerr

Currently, a 64-bit little-endian zImage.epapr won't boot in epapr mode, as we never return from platform_init.

Before entering C, we initialise our stack by setting r1 16 bytes below the end of the _bss_stack:

stwu r0,-16(r1) /* establish a stack frame */

However, the called function will save the caller's lr in the caller's frame's lr save area, at -16(r1) to -32(r1).

This means that writes to the fdt variable will corrupt the saved link register:

 0000000020c06018 l     O .bss   0000000000001000 _bss_stack
 0000000020c07018 l     O .bss   0000000000000008 fdt

We'll need at least 32 bytes in the initial stack frame, to handle the LR save area. We bump this to 112 bytes, as that'll be the max required by ABIv1.

Thanks to Alistair Popple for debugging help.

---
 arch/powerpc/boot/crt0.S |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/boot/crt0.S +1 -1
index 14de4f8..e004062 100644
@@ -218,7 +218,7 @@ p_base:	mflr	r10		/* r10 now points to runtime addr of p_base */
 	beq	6f
 	ld	r1,0(r8)
 	li	r0,0
-	stdu	r0,-16(r1)	/* establish a stack frame */
+	stdu	r0,-112(r1)	/* establish a stack frame */
 6:
 #endif  /* __powerpc64__ */
 	/* Call platform_init() */
#5 lore raw

PATCH v2 5/5 powerpc/boot: don't clobber r6 and r7 in epapr boot

In reply to Jeremy Kerr

We use r6 and r7 for epapr boot, but the current pre-C init will clobber both of these.

This change does a simple replacement, of r6 -> r12 and r7 -> r13, so that we hit platform init with these registers intact.

---
 arch/powerpc/boot/crt0.S |   24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
arch/powerpc/boot/crt0.S +12 -12
index e004062..12866cc 100644
@@ -155,29 +155,29 @@ p_base:	mflr	r10		/* r10 now points to runtime addr of p_base */
 	ld	r9,(p_rela-p_base)(r10)
 	add	r9,r9,r10
 
-	li	r7,0
+	li	r13,0
 	li	r8,0
-9:	ld	r6,0(r11)       /* get tag */
-	cmpdi	r6,0
+9:	ld	r12,0(r11)       /* get tag */
+	cmpdi	r12,0
 	beq	12f              /* end of list */
-	cmpdi	r6,RELA
+	cmpdi	r12,RELA
 	bne	10f
-	ld	r7,8(r11)       /* get RELA pointer in r7 */
+	ld	r13,8(r11)       /* get RELA pointer in r13 */
 	b	11f
-10:	addis	r6,r6,(-RELACOUNT)@ha
-	cmpdi	r6,RELACOUNT@l
+10:	addis	r12,r12,(-RELACOUNT)@ha
+	cmpdi	r12,RELACOUNT@l
 	bne	11f
 	ld	r8,8(r11)       /* get RELACOUNT value in r8 */
 11:	addi	r11,r11,16
 	b	9b
 12:
-	cmpdi	r7,0            /* check we have both RELA and RELACOUNT */
+	cmpdi	r13,0            /* check we have both RELA and RELACOUNT */
 	cmpdi	cr1,r8,0
 	beq	3f
 	beq	cr1,3f
 
 	/* Calcuate the runtime offset. */
-	subf	r7,r7,r9
+	subf	r13,r13,r9
 
 	/* Run through the list of relocations and process the
 	 * R_PPC64_RELATIVE ones. */
@@ -185,10 +185,10 @@ p_base:	mflr	r10		/* r10 now points to runtime addr of p_base */
 13:	ld	r0,8(r9)        /* ELF64_R_TYPE(reloc->r_info) */
 	cmpdi	r0,22           /* R_PPC64_RELATIVE */
 	bne	3f
-	ld	r6,0(r9)        /* reloc->r_offset */
+	ld	r12,0(r9)        /* reloc->r_offset */
 	ld	r0,16(r9)       /* reloc->r_addend */
-	add	r0,r0,r7
-	stdx	r0,r7,r6
+	add	r0,r0,r13
+	stdx	r0,r13,r12
 	addi	r9,r9,24
 	bdnz	13b