Skip to contentSkip to conversation

fdt: Fix alignment issue when reading 64-bits properties from fdt

PATCH New Jean-Jacques Hiblot 3 participants 3 messages Oct 22, 2019 – Oct 29, 2019 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fdt: Fix alignment issue when reading 64-bits properties from fdt

The FDT specification [0] gives a requirement of aligning properties on 32-bits. Make sure that the compiler is aware of this constraint when accessing 64-bits properties.

[0]: https://github.com/devicetree-org/devicetree-specification/blob/master/source/flattened-format.rst

---

Here is a portion of the disassembly of ofnode_read_u64(). It show the effect of the patch on ARM64.

with the classic fdt64_t type. GCC emits LDR (load register)

  34:   54000109        b.ls    54 <ofnode_read_u64+0x54>  // b.plast
  38:   f9400000        ldr     x0, [x0]
  3c:   dac00c00        rev     x0, x0

with the unaligned_fdt64_t type. GCC emits LDP (Load Pair of registers)

  34:   54000129        b.ls    58 <ofnode_read_u64+0x58>  // b.plast
  38:   29400001        ldp     w1, w0, [x0]
  3c:   aa008020        orr     x0, x1, x0, lsl #32
  40:   dac00c00        rev     x0, x0
 common/fdt_support.c       | 2 +-
 drivers/core/ofnode.c      | 2 +-
 include/linux/libfdt_env.h | 1 +
 lib/fdtdec.c               | 2 +-
 4 files changed, 4 insertions(+), 3 deletions(-)
common/fdt_support.c +1 -1
index baf7924ff6..6834399039 100644
@@ -1566,7 +1566,7 @@ static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
 			 uint64_t *val, int cells)
 {
 	const fdt32_t *prop32 = &prop[cell_off];
-	const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
+	const unaligned_fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
 
 	if ((cell_off + cells) > prop_len)
 		return -FDT_ERR_NOSPACE;
drivers/core/ofnode.c +1 -1
index 297f0a0c7c..8f0eab2ca6 100644
@@ -57,7 +57,7 @@ int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
 
 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
 {
-	const fdt64_t *cell;
+	const unaligned_fdt64_t *cell;
 	int len;
 
 	assert(ofnode_valid(node));
include/linux/libfdt_env.h +1 -0
index e2bf79c7ee..e49fcd72bd 100644
@@ -16,6 +16,7 @@
 typedef __be16 fdt16_t;
 typedef __be32 fdt32_t;
 typedef __be64 fdt64_t;
+typedef __be64 unaligned_fdt64_t __aligned(4);
 
 #define fdt32_to_cpu(x) be32_to_cpu(x)
 #define cpu_to_fdt32(x) cpu_to_be32(x)
lib/fdtdec.c +1 -1
index 17736ce665..125d9dbf26 100644
@@ -242,7 +242,7 @@ int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
 			   uint64_t default_val)
 {
-	const uint64_t *cell64;
+	const unaligned_fdt64_t *cell64;
 	int length;
 
 	cell64 = fdt_getprop(blob, node, prop_name, &length);
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/1 fdt: Fix alignment issue when reading 64-bits properties from fdt

Quoted 25 lines · Jean-Jacques Hiblot

The FDT specification [0] gives a requirement of aligning properties on 32-bits. Make sure that the compiler is aware of this constraint when accessing 64-bits properties.

[0]: https://github.com/devicetree-org/devicetree-specification/blob/master/source/flattened-format.rst

---

Here is a portion of the disassembly of ofnode_read_u64(). It show the effect of the patch on ARM64.

with the classic fdt64_t type. GCC emits LDR (load register)

  34:   54000109        b.ls    54 <ofnode_read_u64+0x54>  // b.plast
  38:   f9400000        ldr     x0, [x0]
  3c:   dac00c00        rev     x0, x0

with the unaligned_fdt64_t type. GCC emits LDP (Load Pair of registers)

  34:   54000129        b.ls    58 <ofnode_read_u64+0x58>  // b.plast
  38:   29400001        ldp     w1, w0, [x0]
  3c:   aa008020        orr     x0, x1, x0, lsl #32
  40:   dac00c00        rev     x0, x0
Quoted 5 lines
 common/fdt_support.c       | 2 +-
 drivers/core/ofnode.c      | 2 +-
 include/linux/libfdt_env.h | 1 +
 lib/fdtdec.c               | 2 +-
 4 files changed, 4 insertions(+), 3 deletions(-)

No change log?

#2 lore raw

PATCH v2 1/1 fdt: Fix alignment issue when reading 64-bits properties from fdt

Quoted 25 lines · Jean-Jacques Hiblot

The FDT specification [0] gives a requirement of aligning properties on 32-bits. Make sure that the compiler is aware of this constraint when accessing 64-bits properties.

[0]: https://github.com/devicetree-org/devicetree-specification/blob/master/source/flattened-format.rst

---

Here is a portion of the disassembly of ofnode_read_u64(). It show the effect of the patch on ARM64.

with the classic fdt64_t type. GCC emits LDR (load register)

  34:   54000109        b.ls    54 <ofnode_read_u64+0x54>  // b.plast
  38:   f9400000        ldr     x0, [x0]
  3c:   dac00c00        rev     x0, x0

with the unaligned_fdt64_t type. GCC emits LDP (Load Pair of registers)

  34:   54000129        b.ls    58 <ofnode_read_u64+0x58>  // b.plast
  38:   29400001        ldp     w1, w0, [x0]
  3c:   aa008020        orr     x0, x1, x0, lsl #32
  40:   dac00c00        rev     x0, x0
Quoted 5 lines
 common/fdt_support.c       | 2 +-
 drivers/core/ofnode.c      | 2 +-
 include/linux/libfdt_env.h | 1 +
 lib/fdtdec.c               | 2 +-
 4 files changed, 4 insertions(+), 3 deletions(-)

No change log?

Applied to u-boot-dm, thanks!