Skip to contentSkip to conversation

rproc: Add elf64 support in elf loader

RFC PATCH In review Clément Leger 6 participants 80 messages Apr 2, 2019 – Mar 27, 2020 Page 1 of 2

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 rproc: Add elf64 support in elf loader

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 drivers/remoteproc/remoteproc_elf_loader.c | 130 +++++++++++++++++------------
 drivers/remoteproc/remoteproc_elf_loader.h |  78 +++++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 4 files changed, 159 insertions(+), 55 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
drivers/remoteproc/remoteproc_elf_loader.c +78 -52
index b17d72ec8603..f6aefa46e45c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -44,7 +45,10 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +62,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
@@ -75,26 +83,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }
 EXPORT_SYMBOL(rproc_elf_sanity_check);
@@ -110,11 +121,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +154,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +197,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
 		 * Zero out remaining memory for this segment.
@@ -208,24 +222,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
 
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +301,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +348,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +78 -0

78 lines not on this page · raw message

drivers/remoteproc/remoteproc_internal.h +1 -1
index b02b15f74a5e..6d4a5ffcb28b 100644
@@ -55,7 +55,7 @@ void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
include/linux/remoteproc.h +2 -2
index a3edd981220e..9a7c4c3f1c92 100644
@@ -381,7 +381,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -497,7 +497,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
Signature · 1 line
2.15.0.276.g89ea799
#1 lore raw

Ping ?

----- Original Message -----
From: "Clément Leger" <[email protected]>
To: "linux-remoteproc" <[email protected]>, "Ohad Ben-Cohen" <[email protected]>, "Bjorn Andersson" <[email protected]>
Cc: "Loic PALLARDY" <[email protected]>
Sent: Tuesday, 2 April, 2019 10:28:56
Subject: [RFC PATCH] rproc: Add elf64 support in elf loader

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 drivers/remoteproc/remoteproc_elf_loader.c | 130 +++++++++++++++++------------
 drivers/remoteproc/remoteproc_elf_loader.h |  78 +++++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 4 files changed, 159 insertions(+), 55 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
drivers/remoteproc/remoteproc_elf_loader.c +78 -52
index b17d72ec8603..f6aefa46e45c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -44,7 +45,10 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +62,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
@@ -75,26 +83,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }
 EXPORT_SYMBOL(rproc_elf_sanity_check);
@@ -110,11 +121,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +154,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +197,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
 		 * Zero out remaining memory for this segment.
@@ -208,24 +222,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
 
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +301,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +348,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +78 -0

78 lines not on this page · raw message

drivers/remoteproc/remoteproc_internal.h +1 -1
index b02b15f74a5e..6d4a5ffcb28b 100644
@@ -55,7 +55,7 @@ void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
include/linux/remoteproc.h +2 -2
index a3edd981220e..9a7c4c3f1c92 100644
@@ -381,7 +381,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -497,7 +497,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
Signature · 1 line
2.15.0.276.g89ea799
#2 lore raw

Hi Clément,

I tested your patch on stm32MP1, no regression observed when loading my ELF32 firmware.
Now i'm not enough expert on the loader to ack your patch. For stm32 MP1 platform:

Please find few remarks in your code

Regards
Arnaud

Quoted 38 lines · Clément Leger

Ping ?

----- Original Message -----
From: "Clément Leger" <[email protected]>
To: "linux-remoteproc" <[email protected]>, "Ohad Ben-Cohen" <[email protected]>, "Bjorn Andersson" <[email protected]>
Cc: "Loic PALLARDY" <[email protected]>
Sent: Tuesday, 2 April, 2019 10:28:56
Subject: [RFC PATCH] rproc: Add elf64 support in elf loader

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 drivers/remoteproc/remoteproc_elf_loader.c | 130 +++++++++++++++++------------
 drivers/remoteproc/remoteproc_elf_loader.h |  78 +++++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 4 files changed, 159 insertions(+), 55 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
drivers/remoteproc/remoteproc_elf_loader.c +1 -0
index b17d72ec8603..f6aefa46e45c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 47

Quoted 3 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
@@ -44,7 +45,10 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
 	struct elf32_hdr *ehdr;

This is confusing because this function handles elf64 and elf32, i suggest using e_ident table pointer directly, or adding a comment as in remoteproc_elf_loader.h

Quoted 250 lines
drivers/remoteproc/remoteproc_elf_loader.c +77 -52
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +62,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
@@ -75,26 +83,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }
 EXPORT_SYMBOL(rproc_elf_sanity_check);
@@ -110,11 +121,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +154,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +197,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
 		 * Zero out remaining memory for this segment.
@@ -208,24 +222,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
 
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +301,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +348,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +0 -0
new file mode 100644
index 000000000000..e37a55c61eae

drivers/remoteproc/remoteproc_elf_loader.h line 5

Quoted 5 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +5 -0
@@ -0,0 +1,78 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.

The rest of the header should be cleaned

Quoted 100 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +73 -0
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index b02b15f74a5e..6d4a5ffcb28b 100644
@@ -55,7 +55,7 @@ void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
include/linux/remoteproc.h +1 -1
index a3edd981220e..9a7c4c3f1c92 100644
@@ -381,7 +381,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**

include/linux/remoteproc.h struct rproc { · line 500

Quoted 5 lines
include/linux/remoteproc.h +1 -1
@@ -497,7 +497,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;

This generates a warning in remote proc:
drivers/remoteproc/st_remoteproc.c:196:24: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘u64’ {aka ‘long long unsigned int’} [-Wformat=]
dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);

Quoted 4 lines
include/linux/remoteproc.h +0 -0
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
#3 lore raw

Thanks Arnaud,

I will apply your comment in patch V2.

Regards,

Clément

Quoted 437 lines · Arnaud Pouliquen

Hi Clément,

I tested your patch on stm32MP1, no regression observed when loading my ELF32 firmware.
Now i'm not enough expert on the loader to ack your patch. For stm32 MP1 platform:

Please find few remarks in your code

Regards
Arnaud

Quoted 40 lines · Clément Leger

Ping ?

----- Original Message -----
From: "Clément Leger" <[email protected]>
To: "linux-remoteproc" <[email protected]>, "Ohad Ben-Cohen" <[email protected]>, "Bjorn Andersson" <[email protected]>
Cc: "Loic PALLARDY" <[email protected]>
Sent: Tuesday, 2 April, 2019 10:28:56
Subject: [RFC PATCH] rproc: Add elf64 support in elf loader

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 drivers/remoteproc/remoteproc_elf_loader.c | 130 +++++++++++++++++------------
 drivers/remoteproc/remoteproc_elf_loader.h |  78 +++++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 4 files changed, 159 insertions(+), 55 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
drivers/remoteproc/remoteproc_elf_loader.c +1 -0
index b17d72ec8603..f6aefa46e45c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 48

Quoted 4 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
@@ -44,7 +45,10 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
 	struct elf32_hdr *ehdr;

This is confusing because this function handles elf64 and elf32, i suggest using e_ident table pointer directly, or adding a comment as in remoteproc_elf_loader.h

Quoted 256 lines
drivers/remoteproc/remoteproc_elf_loader.c +3 -0
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +62,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
drivers/remoteproc/remoteproc_elf_loader.c +11 -8
 	}
@@ -75,26 +83,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +121,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +154,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +197,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
drivers/remoteproc/remoteproc_elf_loader.c +18 -10
 		 * Zero out remaining memory for this segment.
@@ -208,24 +222,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
 
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
drivers/remoteproc/remoteproc_elf_loader.c +7 -7
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +301,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
drivers/remoteproc/remoteproc_elf_loader.c +8 -4
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +348,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +0 -0
new file mode 100644
index 000000000000..e37a55c61eae

drivers/remoteproc/remoteproc_elf_loader.h line 5

Quoted 5 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +5 -0
@@ -0,0 +1,78 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.

The rest of the header should be cleaned

Quoted 101 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +73 -0
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index b02b15f74a5e..6d4a5ffcb28b 100644
@@ -55,7 +55,7 @@ void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
include/linux/remoteproc.h +1 -1
index a3edd981220e..9a7c4c3f1c92 100644
@@ -381,7 +381,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**

include/linux/remoteproc.h struct rproc { · line 500

Quoted 5 lines
include/linux/remoteproc.h +1 -1
@@ -497,7 +497,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;

This generates a warning in remote proc:
drivers/remoteproc/st_remoteproc.c:196:24: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘u64’ {aka ‘long long unsigned int’} [-Wformat=]
dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);

Quoted 3 lines
include/linux/remoteproc.h +0 -0
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
#4 lore raw

PATCH 1/1 rproc: Add elf64 support in elf loader

In reply to Arnaud Pouliquen

From: Clément Leger <[email protected]>

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 5 files changed, 156 insertions(+), 56 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
drivers/remoteproc/remoteproc_elf_loader.c +83 -52
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }
 EXPORT_SYMBOL(rproc_elf_sanity_check);
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
 		 * Zero out remaining memory for this segment.
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
 
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0

69 lines not on this page · raw message

drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +2 -2
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
Signature · 1 line
2.15.0.276.g89ea799
#5 lore raw

Ping ?

Quoted 421 lines · ----- On 19 Aug, 2019, at 13:45, Clément Leger [email protected] wrote:

From: Clément Leger <[email protected]>

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---

drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
drivers/remoteproc/remoteproc_internal.h   |   2 +-
drivers/remoteproc/st_remoteproc.c         |   2 +-
include/linux/remoteproc.h                 |   4 +-
5 files changed, 156 insertions(+), 56 deletions(-)

create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h

drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
 * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
		dev_err(dev, "failed to load %s\n", name);
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
	}
drivers/remoteproc/remoteproc_elf_loader.c +11 -8
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
 * Note that the boot address is not a configurable property of all remote
 * processors. Some will always boot at a specific hard-coded address.
 */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*

* Zero out remaining memory for this segment.

drivers/remoteproc/remoteproc_elf_loader.c +18 -10
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
 
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
		table = (struct resource_table *)(elf_data + offset);
drivers/remoteproc/remoteproc_elf_loader.c +7 -7
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
 */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*

* Create a copy of the resource table. When a virtio device starts

drivers/remoteproc/remoteproc_elf_loader.c +8 -4
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +2 -3
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
--

2.15.0.276.g89ea799

#6 lore raw

Ping ?

Quoted 425 lines · ----- On 13 Sep, 2019, at 12:58, Clément Leger [email protected] wrote:

Ping ?

Quoted 421 lines · ----- On 19 Aug, 2019, at 13:45, Clément Leger [email protected] wrote:

From: Clément Leger <[email protected]>

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---

drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
drivers/remoteproc/remoteproc_internal.h   |   2 +-
drivers/remoteproc/st_remoteproc.c         |   2 +-
include/linux/remoteproc.h                 |   4 +-
5 files changed, 156 insertions(+), 56 deletions(-)

create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h

drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
 * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
		dev_err(dev, "failed to load %s\n", name);
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
	}
drivers/remoteproc/remoteproc_elf_loader.c +11 -8
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
 * Note that the boot address is not a configurable property of all remote
 * processors. Some will always boot at a specific hard-coded address.
 */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*

* Zero out remaining memory for this segment.

drivers/remoteproc/remoteproc_elf_loader.c +18 -10
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
 
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
		table = (struct resource_table *)(elf_data + offset);
drivers/remoteproc/remoteproc_elf_loader.c +7 -7
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
 */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*

* Create a copy of the resource table. When a virtio device starts

drivers/remoteproc/remoteproc_elf_loader.c +8 -4
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +2 -3
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
--

2.15.0.276.g89ea799

#7 lore raw

PATCH v2 1/1 remoteproc: Add elf64 support in elf loader

In reply to Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
Changes in v2:
- Add ELF64 support in documentation

---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 157 insertions(+), 57 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +83 -52

252 lines not on this page · raw message

drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0

69 lines not on this page · raw message

drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +2 -2
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
Signature · 1 line
2.15.0.276.g89ea799
#8 lore raw

Re: [PATCH v2] remoteproc: Add elf64 support in elf loader

Ping ?

Quoted 437 lines · ----- On 4 Oct, 2019, at 20:42, Clément Leger [email protected] wrote:

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
Changes in v2:
- Add ELF64 support in documentation

---

Documentation/remoteproc.txt               |   2 +-
drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
drivers/remoteproc/remoteproc_internal.h   |   2 +-
drivers/remoteproc/st_remoteproc.c         |   2 +-
include/linux/remoteproc.h                 |   4 +-
6 files changed, 157 insertions(+), 57 deletions(-)

create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h

Documentation/remoteproc.txt +0 -0
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.

Binary Firmware Structure
=========================

Documentation/remoteproc.txt +1 -1
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,

it is quite expected that other platforms/devices which we'd want to support with this framework will be based on different binary formats.

drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
 * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
		dev_err(dev, "failed to load %s\n", name);
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
	}
drivers/remoteproc/remoteproc_elf_loader.c +11 -8
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
 * Note that the boot address is not a configurable property of all remote
 * processors. Some will always boot at a specific hard-coded address.
 */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*

* Zero out remaining memory for this segment.

drivers/remoteproc/remoteproc_elf_loader.c +18 -10
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
 
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
		table = (struct resource_table *)(elf_data + offset);
drivers/remoteproc/remoteproc_elf_loader.c +7 -7
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
 */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*

* Create a copy of the resource table. When a virtio device starts

drivers/remoteproc/remoteproc_elf_loader.c +8 -4
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +2 -3
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
--

2.15.0.276.g89ea799

maintainer
#9 lore raw

Re: [PATCH v2] remoteproc: Add elf64 support in elf loader

In reply to Clement Leger

Hi Clement,

Quoted 7 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

Quoted 6 lines

---
Changes in v2:
- Add ELF64 support in documentation

First and foremost please address the complaints from checkpatch.

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)

Quoted 53 lines
---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 157 insertions(+), 57 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)

In the current code [1] fw->size is compared against the size of an elf32_hdr. If support for elf64 is added that code needs to be modified to check for the right header size using fw_elf_get_class().

[1] https://elixir.bootlin.com/linux/v5.5-rc6/source/drivers/remoteproc/remoteproc_elf_loader.c#L46

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 74

Quoted 9 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -1
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+

Is there a reason to move this check up where? If not please bring it back to its original location, that is below:

"if (fw->size < ehdr->e_shoff + sizeof(structelf32_shdr))"

Quoted 31 lines
drivers/remoteproc/remoteproc_elf_loader.c +8 -8
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 113

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +4 -1
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+

Yes, this is useful.

Quoted 93 lines
drivers/remoteproc/remoteproc_elf_loader.c +24 -21
 	return 0;
 }
 EXPORT_SYMBOL(rproc_elf_sanity_check);
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
 		 * Zero out remaining memory for this segment.

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 230

Quoted 6 lines
drivers/remoteproc/remoteproc_elf_loader.c +1 -2
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *

Not sure I understand the "const" - was the compiler complaining?

Quoted 12 lines
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 248

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);

It took me a while but I figured out what is happening here. To save me (and other people) from going through the same mental process every time I look at this code, please add a comment for each of the above 3 lines.

Quoted 53 lines
drivers/remoteproc/remoteproc_elf_loader.c +15 -13
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 

drivers/remoteproc/remoteproc_elf_loader.c EXPORT_SYMBOL(rproc_elf_load_rsc_table); · line 367

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -2
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);

The prototype for the above is as follow:

void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)

But sh_size is a u64, which will cause problem that are hard to debug. I think it is better to move 'len' to an 8 byte type along with the refactoring of the existing code that is implied. I suggest to split this work in a preparatory patch (that will still be part of this set).

Quoted 53 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +45 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+

drivers/remoteproc/remoteproc_elf_loader.h line 55

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +10 -0
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)

I like how you did this.

Quoted 54 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +14 -0
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**

include/linux/remoteproc.h struct rproc { · line 484

Quoted 8 lines
include/linux/remoteproc.h +1 -1
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;

Please hold off before doing another respin of this patch. While doing something completely different I noticed TI also did some work in this area. I'd like to take some time to look at their implementation and see if they carry features that haven't been included here. I intend to do this tomorrow.

Thanks,
Mathieu

Quoted 3 lines
Signature · 1 line
2.15.0.276.g89ea799
#10 lore raw

Re: [PATCH v2] remoteproc: Add elf64 support in elf loader

Hi Mathieu,

Quoted 10 lines · ----- On 24 Jan, 2020, at 01:53, Mathieu Poirier [email protected]…

Hi Clement,

Quoted 7 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

Quoted 8 lines
Quoted 6 lines

---
Changes in v2:
- Add ELF64 support in documentation

First and foremost please address the complaints from checkpatch.

I fixed one typo in accommodate. The other checkpatch complaint is about missing parenthesis for macros with complex values which is unfortunately not possible since I'm generating inline functions.

Did you have any other one ?

Quoted 53 lines
Quoted 53 lines
---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 157 insertions(+), 57 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
Quoted 8 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 67

Quoted 4 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 	if (!fw) {
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)

In the current code [1] fw->size is compared against the size of an elf32_hdr. If support for elf64 is added that code needs to be modified to check for the right header size using fw_elf_get_class().

Actually, the elf32 header is smaller than the elf64 one so this check is there to ensure that we have at least a minimal elf header (elf32). And since the class is derived from the header, you better have to check the header size before accessing it.

To be more clear, I could compare it to min(sizeof(struct elf32_hdr), sizeof(struct elf64_hdr)) or add a comment at least stating that since elf header contains the same fields for identification, we can use the elf32 one.

Quoted 17 lines

[1]
https://elixir.bootlin.com/linux/v5.5-rc6/source/drivers/remoteproc/remoteproc_elf_loader.c#L46

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 74

Quoted 9 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -1
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+

Is there a reason to move this check up where? If not please bring it back to its original location, that is below:

Quoted 2 lines

"if (fw->size < ehdr->e_shoff + sizeof(structelf32_shdr))"

This is because the new check for size uses elf_shdr_size which is derived from the class. And since the class is extracted from the elf header, we need to check the header to be correct first.

Quoted 143 lines
Quoted 32 lines
drivers/remoteproc/remoteproc_elf_loader.c +8 -8
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 114

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +4 -1
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+

Yes, this is useful.

Quoted 98 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 		 * Zero out remaining memory for this segment.
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
Quoted 5 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 230

Quoted 3 lines
drivers/remoteproc/remoteproc_elf_loader.c +1 -2
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *

Not sure I understand the "const" - was the compiler complaining?

It's actually caused by the fact I used a const void* shdr in the caller. I will check if this is mandatory.

Quoted 17 lines
Quoted 17 lines
drivers/remoteproc/remoteproc_elf_loader.c +9 -2
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
Quoted 10 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 248

Quoted 6 lines
drivers/remoteproc/remoteproc_elf_loader.c +3 -2
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);

It took me a while but I figured out what is happening here. To save me (and other people) from going through the same mental process every time I look at this code, please add a comment for each of the above 3 lines.

Indeed.

Quoted 68 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +12 -11
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
drivers/remoteproc/remoteproc_elf_loader.c +3 -2
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 

drivers/remoteproc/remoteproc_elf_loader.c EXPORT_SYMBOL(rproc_elf_load_rsc_table); · line 367

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -2
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);

The prototype for the above is as follow:

Quoted 6 lines

void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)

But sh_size is a u64, which will cause problem that are hard to debug. I think it is better to move 'len' to an 8 byte type along with the refactoring of the existing code that is implied. I suggest to split this work in a preparatory patch (that will still be part of this set).

Nice catch ! I will do that.

Quoted 122 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +45 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+

drivers/remoteproc/remoteproc_elf_loader.h line 55

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +10 -0
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)

I like how you did this.

Quoted 54 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +14 -0
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
Quoted 15 lines

include/linux/remoteproc.h struct rproc { · line 484

Quoted 10 lines
include/linux/remoteproc.h +1 -1
 /**
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;

Please hold off before doing another respin of this patch. While doing something completely different I noticed TI also did some work in this area. I'd like to take some time to look at their implementation and see if they carry features that haven't been included here. I intend to do this tomorrow.

Ok,

Thanks for your review,

Clément

Quoted 6 lines

Thanks,
Mathieu

Quoted 2 lines

--
2.15.0.276.g89ea799

maintainer
#11 lore raw

Re: [PATCH v2] remoteproc: Add elf64 support in elf loader

Quoted 21 lines · Cl�ment Leger

Hi Mathieu,

Quoted 17 lines · ----- On 24 Jan, 2020, at 01:53, Mathieu Poirier [email protected]…

Hi Clement,

Quoted 13 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
Changes in v2:
- Add ELF64 support in documentation

Quoted 7 lines
Quoted 1 line

First and foremost please address the complaints from checkpatch.

I fixed one typo in accommodate. The other checkpatch complaint is about missing parenthesis for macros with complex values which is unfortunately not possible since I'm generating inline functions.

Did you have any other one ?

No

Quoted 63 lines
Quoted 54 lines
Quoted 54 lines

---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 157 insertions(+), 57 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
Quoted 8 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 67

Quoted 4 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 	if (!fw) {
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)

In the current code [1] fw->size is compared against the size of an elf32_hdr. If support for elf64 is added that code needs to be modified to check for the right header size using fw_elf_get_class().

Quoted 4 lines

Actually, the elf32 header is smaller than the elf64 one so this check is there to ensure that we have at least a minimal elf header (elf32). And since the class is derived from the header, you better have to check the header size before accessing it.

This is a chicken-and-egg issue... We can't derive the class from the header if we don't check the header and we can't check the header if we don't have the class. Since ehdr->e_ident[] is the same for both, it is probably safe to access e_ident[EI_CLASS] without further checks.

Quoted 5 lines

To be more clear, I could compare it to min(sizeof(struct elf32_hdr), sizeof(struct elf64_hdr)) or add a comment at least stating that since elf header contains the same fields for identification, we can use the elf32 one.

I am concerned about cases where we deal with an elf64 and fw->size is >= sizeof(struct elf32_hdr) and < sizeof(struct elf64_hdr). With the current code we won't catch the error. It might be that we do something like this:

        size_t header_size;

        header_size = (fw_elf_get_class(fw) == ELFCLASS32 ?
                       sizeof(structelf32_hdr):
                       sizeof(structelf64_hdr);

        if (fw->size < header_size ) {
                ...
        }
Quoted 434 lines
Quoted 18 lines

[1]
https://elixir.bootlin.com/linux/v5.5-rc6/source/drivers/remoteproc/remoteproc_elf_loader.c#L46

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 74

Quoted 9 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -1
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+

Is there a reason to move this check up where? If not please bring it back to its original location, that is below:

Quoted 2 lines

"if (fw->size < ehdr->e_shoff + sizeof(structelf32_shdr))"

This is because the new check for size uses elf_shdr_size which is derived from the class. And since the class is extracted from the elf header, we need to check the header to be correct first.

Quoted 143 lines
Quoted 32 lines
drivers/remoteproc/remoteproc_elf_loader.c +8 -8
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 114

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +4 -1
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+

Yes, this is useful.

Quoted 98 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 		 * Zero out remaining memory for this segment.
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
Quoted 5 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 230

Quoted 3 lines
drivers/remoteproc/remoteproc_elf_loader.c +1 -2
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *

Not sure I understand the "const" - was the compiler complaining?

It's actually caused by the fact I used a const void* shdr in the caller. I will check if this is mandatory.

Quoted 17 lines
Quoted 17 lines
drivers/remoteproc/remoteproc_elf_loader.c +9 -2
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
Quoted 10 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 248

Quoted 6 lines
drivers/remoteproc/remoteproc_elf_loader.c +3 -2
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);

It took me a while but I figured out what is happening here. To save me (and other people) from going through the same mental process every time I look at this code, please add a comment for each of the above 3 lines.

Indeed.

Quoted 68 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +12 -11
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
drivers/remoteproc/remoteproc_elf_loader.c +3 -2
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 

drivers/remoteproc/remoteproc_elf_loader.c EXPORT_SYMBOL(rproc_elf_load_rsc_table); · line 367

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -2
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);

The prototype for the above is as follow:

Quoted 6 lines

void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)

But sh_size is a u64, which will cause problem that are hard to debug. I think it is better to move 'len' to an 8 byte type along with the refactoring of the existing code that is implied. I suggest to split this work in a preparatory patch (that will still be part of this set).

Nice catch ! I will do that.

Quoted 122 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +45 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+

drivers/remoteproc/remoteproc_elf_loader.h line 55

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +10 -0
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)

I like how you did this.

Quoted 54 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +14 -0
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
Quoted 15 lines

include/linux/remoteproc.h struct rproc { · line 484

Quoted 10 lines
include/linux/remoteproc.h +1 -1
 /**
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;

Please hold off before doing another respin of this patch. While doing something completely different I noticed TI also did some work in this area. I'd like to take some time to look at their implementation and see if they carry features that haven't been included here. I intend to do this tomorrow.

Ok,

Thanks for your review,

Cl�ment

Quoted 6 lines

Thanks,
Mathieu

Quoted 2 lines

--
2.15.0.276.g89ea799

maintainer
#12 lore raw

Re: [PATCH v2] remoteproc: Add elf64 support in elf loader

In reply to Clément Leger
Quoted 522 lines · Cl�ment Leger

Hi Mathieu,

Quoted 10 lines · ----- On 24 Jan, 2020, at 01:53, Mathieu Poirier [email protected]…

Hi Clement,

Quoted 7 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

Quoted 8 lines
Quoted 6 lines

---
Changes in v2:
- Add ELF64 support in documentation

First and foremost please address the complaints from checkpatch.

I fixed one typo in accommodate. The other checkpatch complaint is about missing parenthesis for macros with complex values which is unfortunately not possible since I'm generating inline functions.

Did you have any other one ?

Quoted 53 lines
Quoted 53 lines
---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 157 insertions(+), 57 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
Quoted 8 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 67

Quoted 4 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 	if (!fw) {
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)

In the current code [1] fw->size is compared against the size of an elf32_hdr. If support for elf64 is added that code needs to be modified to check for the right header size using fw_elf_get_class().

Actually, the elf32 header is smaller than the elf64 one so this check is there to ensure that we have at least a minimal elf header (elf32). And since the class is derived from the header, you better have to check the header size before accessing it.

To be more clear, I could compare it to min(sizeof(struct elf32_hdr), sizeof(struct elf64_hdr)) or add a comment at least stating that since elf header contains the same fields for identification, we can use the elf32 one.

Quoted 17 lines

[1]
https://elixir.bootlin.com/linux/v5.5-rc6/source/drivers/remoteproc/remoteproc_elf_loader.c#L46

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 74

Quoted 9 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -1
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+

Is there a reason to move this check up where? If not please bring it back to its original location, that is below:

Quoted 2 lines

"if (fw->size < ehdr->e_shoff + sizeof(structelf32_shdr))"

This is because the new check for size uses elf_shdr_size which is derived from the class. And since the class is extracted from the elf header, we need to check the header to be correct first.

Quoted 143 lines
Quoted 32 lines
drivers/remoteproc/remoteproc_elf_loader.c +8 -8
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 114

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +4 -1
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+

Yes, this is useful.

Quoted 98 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 		 * Zero out remaining memory for this segment.
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
Quoted 5 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 230

Quoted 3 lines
drivers/remoteproc/remoteproc_elf_loader.c +1 -2
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *

Not sure I understand the "const" - was the compiler complaining?

It's actually caused by the fact I used a const void* shdr in the caller. I will check if this is mandatory.

Quoted 17 lines
Quoted 17 lines
drivers/remoteproc/remoteproc_elf_loader.c +9 -2
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
Quoted 10 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 248

Quoted 6 lines
drivers/remoteproc/remoteproc_elf_loader.c +3 -2
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);

It took me a while but I figured out what is happening here. To save me (and other people) from going through the same mental process every time I look at this code, please add a comment for each of the above 3 lines.

Indeed.

Quoted 68 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +12 -11
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
drivers/remoteproc/remoteproc_elf_loader.c +3 -2
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 

drivers/remoteproc/remoteproc_elf_loader.c EXPORT_SYMBOL(rproc_elf_load_rsc_table); · line 367

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -2
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);

The prototype for the above is as follow:

Quoted 6 lines

void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)

But sh_size is a u64, which will cause problem that are hard to debug. I think it is better to move 'len' to an 8 byte type along with the refactoring of the existing code that is implied. I suggest to split this work in a preparatory patch (that will still be part of this set).

Nice catch ! I will do that.

Quoted 122 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +45 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+

drivers/remoteproc/remoteproc_elf_loader.h line 55

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +10 -0
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)

I like how you did this.

Quoted 54 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +14 -0
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
Quoted 15 lines

include/linux/remoteproc.h struct rproc { · line 484

Quoted 10 lines
include/linux/remoteproc.h +1 -1
 /**
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;

Please hold off before doing another respin of this patch. While doing something completely different I noticed TI also did some work in this area. I'd like to take some time to look at their implementation and see if they carry features that haven't been included here. I intend to do this tomorrow.

Quoted 3 lines

Ok,

Thanks for your review,

As promised I looked at what Suman had done on his side [1] to support 64-bit ELF files. His approach to offer the same functionality but for 64 bit in a new file is quick, simple and flexible. On the flip side it introduces code duplication, something that is seriously frowned upon upstream.

I did some soul searching in the kernel code and found very little in terms of implementation that deals with both 32 and 64 bit ELF format. The most convincing approach was set forth by the MIPS guys [2]. They too have decided to support both types in the same functions, but I don't see us adding an if() statement (and the code duplication that comes with it) every time we need to deal with file types.

Given the above I'm in favour of moving forward with your approach. One could rightly argue the macros make the code harder to read but given the alternatives, it seems to be the best solution.

Mathieu

[1]. https://bit.ly/2Rpmb4E
[2]. https://elixir.bootlin.com/linux/v5.5-rc6/source/arch/mips/kernel/elf.c#L75

Quoted 9 lines

Cl�ment

Quoted 6 lines

Thanks,
Mathieu

Quoted 2 lines

--
2.15.0.276.g89ea799

#13 lore raw

Re: [PATCH v2] remoteproc: Add elf64 support in elf loader

Hi Mathieu,

Thanks for your thorough review. While thinking about it,there is at least another option which would consist in splitting all elf specific functions into a separate .h file and then include it in a .c to "instantiate" the functions with correct types. For instance, it would look like this:

#define ELF_TYPE 32
#include "elf_functions.h"
#undef ELF_TYPE
#define ELF_TYPE 64
#include "elf_functions.h"

pros: More readable and type-checking ok
cons: A bit hackish

I would say this might be a better optino than my current patch. However, I'm not sure this kind of thing of well accepted in the kernel.

Clément

Quoted 559 lines · ----- On 24 Jan, 2020, at 22:58, Mathieu Poirier [email protected]…
Quoted 524 lines · Clément Leger

Hi Mathieu,

----- On 24 Jan, 2020, at 01:53, Mathieu Poirier [email protected]

Quoted 10 lines · wrote:

Hi Clement,

Quoted 7 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

Quoted 8 lines
Quoted 6 lines

---
Changes in v2:
- Add ELF64 support in documentation

First and foremost please address the complaints from checkpatch.

I fixed one typo in accommodate. The other checkpatch complaint is about missing parenthesis for macros with complex values which is unfortunately not possible since I'm generating inline functions.

Did you have any other one ?

Quoted 53 lines
Quoted 53 lines
---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 157 insertions(+), 57 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
Quoted 8 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 67

Quoted 4 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 	if (!fw) {
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 		dev_err(dev, "failed to load %s\n", name);
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)

In the current code [1] fw->size is compared against the size of an elf32_hdr. If support for elf64 is added that code needs to be modified to check for the right header size using fw_elf_get_class().

Actually, the elf32 header is smaller than the elf64 one so this check is there to ensure that we have at least a minimal elf header (elf32). And since the class is derived from the header, you better have to check the header size before accessing it.

To be more clear, I could compare it to min(sizeof(struct elf32_hdr), sizeof(struct elf64_hdr)) or add a comment at least stating that since elf header contains the same fields for identification, we can use the elf32 one.

Quoted 17 lines

[1]
https://elixir.bootlin.com/linux/v5.5-rc6/source/drivers/remoteproc/remoteproc_elf_loader.c#L46

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 74

Quoted 9 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -1
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+

Is there a reason to move this check up where? If not please bring it back to its original location, that is below:

Quoted 2 lines

"if (fw->size < ehdr->e_shoff + sizeof(structelf32_shdr))"

This is because the new check for size uses elf_shdr_size which is derived from the class. And since the class is extracted from the elf header, we need to check
the header to be correct first.

Quoted 143 lines
Quoted 32 lines
drivers/remoteproc/remoteproc_elf_loader.c +8 -8
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 114

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +4 -1
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+

Yes, this is useful.

Quoted 98 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 		 * Zero out remaining memory for this segment.
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
Quoted 5 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 230

Quoted 3 lines
drivers/remoteproc/remoteproc_elf_loader.c +1 -2
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *

Not sure I understand the "const" - was the compiler complaining?

It's actually caused by the fact I used a const void* shdr in the caller. I will check if this is mandatory.

Quoted 17 lines
Quoted 17 lines
drivers/remoteproc/remoteproc_elf_loader.c +9 -2
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
Quoted 10 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 248

Quoted 6 lines
drivers/remoteproc/remoteproc_elf_loader.c +3 -2
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);

It took me a while but I figured out what is happening here. To save me (and other people) from going through the same mental process every time I look at this code, please add a comment for each of the above 3 lines.

Indeed.

Quoted 68 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +12 -11
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
 
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
drivers/remoteproc/remoteproc_elf_loader.c +3 -2
 	 * Create a copy of the resource table. When a virtio device starts
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 

drivers/remoteproc/remoteproc_elf_loader.c EXPORT_SYMBOL(rproc_elf_load_rsc_table); · line 367

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -2
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);

The prototype for the above is as follow:

Quoted 6 lines

void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)

But sh_size is a u64, which will cause problem that are hard to debug. I think it is better to move 'len' to an 8 byte type along with the refactoring of the existing code that is implied. I suggest to split this work in a preparatory patch (that will still be part of this set).

Nice catch ! I will do that.

Quoted 122 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +45 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+

drivers/remoteproc/remoteproc_elf_loader.h line 55

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +10 -0
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)

I like how you did this.

Quoted 54 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +14 -0
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
Quoted 15 lines

include/linux/remoteproc.h struct rproc { · line 484

Quoted 10 lines
include/linux/remoteproc.h +1 -1
 /**
@@ -478,7 +478,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;

Please hold off before doing another respin of this patch. While doing something completely different I noticed TI also did some work in this area. I'd like to take some time to look at their implementation and see if they carry features that haven't been included here. I intend to do this tomorrow.

Quoted 3 lines

Ok,

Thanks for your review,

As promised I looked at what Suman had done on his side [1] to support 64-bit ELF
files. His approach to offer the same functionality but for 64 bit in a new file is quick, simple and flexible. On the flip side it introduces code duplication, something that is seriously frowned upon upstream.

I did some soul searching in the kernel code and found very little in terms of implementation that deals with both 32 and 64 bit ELF format. The most convincing approach was set forth by the MIPS guys [2]. They too have decided to support both types in the same functions, but I don't see us adding an if() statement (and the code duplication that comes with it) every time we need to deal with file types.

Given the above I'm in favour of moving forward with your approach. One could rightly argue the macros make the code harder to read but given the alternatives, it seems to be the best solution.

Mathieu

[1]. https://bit.ly/2Rpmb4E
[2]. https://elixir.bootlin.com/linux/v5.5-rc6/source/arch/mips/kernel/elf.c#L75

Quoted 9 lines

Clément

Quoted 6 lines

Thanks,
Mathieu

Quoted 2 lines

--
2.15.0.276.g89ea799

maintainer
#14 lore raw

Re: [PATCH v2] remoteproc: Add elf64 support in elf loader

Quoted 14 lines · Clément Leger

Hi Mathieu,

Thanks for your thorough review. While thinking about it,there is at least another option which would consist in splitting all elf specific functions into a separate .h file and then include it in a .c to "instantiate" the functions with correct types. For instance, it would look like this:

#define ELF_TYPE 32
#include "elf_functions.h"
#undef ELF_TYPE
#define ELF_TYPE 64
#include "elf_functions.h"

Quoted 5 lines

pros: More readable and type-checking ok
cons: A bit hackish

I would say this might be a better optino than my current patch. However, I'm not sure this kind of thing of well accepted in the kernel.

I won't claim to fully understand your suggestion above, but if it is suspicious enough to look hackish to you than it will probably look hackish to other people. Nonetheless there might be a case for exception if the approach yields clear advantages. Can you point me to an example somewhere in the kernel code where something similar would have been done?

Quoted 564 lines

Clément

Quoted 559 lines · ----- On 24 Jan, 2020, at 22:58, Mathieu Poirier [email protected]…
Quoted 524 lines · Clément Leger

Hi Mathieu,

----- On 24 Jan, 2020, at 01:53, Mathieu Poirier [email protected]

Quoted 10 lines · wrote:

Hi Clement,

Quoted 7 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

Quoted 8 lines
Quoted 6 lines

---
Changes in v2:
- Add ELF64 support in documentation

First and foremost please address the complaints from checkpatch.

I fixed one typo in accommodate. The other checkpatch complaint is about missing parenthesis for macros with complex values which is unfortunately not possible since I'm generating inline functions.

Did you have any other one ?

Quoted 53 lines
Quoted 53 lines
---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 157 insertions(+), 57 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
  const char *name = rproc->firmware;
  struct device *dev = &rproc->dev;
+ /*
+  * Elf files are beginning with the same structure. Thus, to simplify
+  * header parsing, we can use the elf32_hdr one for both elf64 and
+  * elf32.
+  */
  struct elf32_hdr *ehdr;
+ u32 elf_shdr_size;
+ u64 phoff, shoff;
  char class;
+ u16 phnum;
 
Quoted 8 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 67

Quoted 4 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
  if (!fw) {
          dev_err(dev, "failed to load %s\n", name);
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)

In the current code [1] fw->size is compared against the size of an elf32_hdr. If support for elf64 is added that code needs to be modified to check for the right header size using fw_elf_get_class().

Actually, the elf32 header is smaller than the elf64 one so this check is there to ensure that we have at least a minimal elf header (elf32). And since the class is derived from the header, you better have to check the header size before accessing it.

To be more clear, I could compare it to min(sizeof(struct elf32_hdr), sizeof(struct elf64_hdr)) or add a comment at least stating that since elf header contains the same fields for identification, we can use the elf32 one.

Quoted 14 lines

[1]
https://elixir.bootlin.com/linux/v5.5-rc6/source/drivers/remoteproc/remoteproc_elf_loader.c#L46

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 74

Quoted 9 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -1
  ehdr = (struct elf32_hdr *)fw->data;
 
- /* We only support ELF32 at this point */
+ if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+         dev_err(dev, "Image is corrupted (bad magic)\n");
+         return -EINVAL;
+ }
+
Quoted 5 lines

Is there a reason to move this check up where? If not please bring it back to its original location, that is below:

"if (fw->size < ehdr->e_shoff + sizeof(structelf32_shdr))"

This is because the new check for size uses elf_shdr_size which is derived from the class. And since the class is extracted from the elf header, we need to check
the header to be correct first.

Quoted 143 lines
Quoted 32 lines
drivers/remoteproc/remoteproc_elf_loader.c +8 -8
  class = ehdr->e_ident[EI_CLASS];
- if (class != ELFCLASS32) {
+ if (class != ELFCLASS32 && class != ELFCLASS64) {
          dev_err(dev, "Unsupported class: %d\n", class);
          return -EINVAL;
  }
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
          return -EINVAL;
  }
 
- if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-         dev_err(dev, "Image is too small\n");
-         return -EINVAL;
- }
+ phoff = elf_hdr_e_phoff(class, fw->data);
+ shoff = elf_hdr_e_shoff(class, fw->data);
+ phnum =  elf_hdr_e_phnum(class, fw->data);
+ elf_shdr_size = elf_size_of_shdr(class);
 
- if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-         dev_err(dev, "Image is corrupted (bad magic)\n");
+ if (fw->size < shoff + elf_shdr_size) {
+         dev_err(dev, "Image is too small\n");
          return -EINVAL;
  }
 
- if (ehdr->e_phnum == 0) {
+ if (phnum == 0) {
          dev_err(dev, "No loadable segments\n");
          return -EINVAL;
  }

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 114

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +4 -1
 
- if (ehdr->e_phoff > fw->size) {
+ if (phoff > fw->size) {
          dev_err(dev, "Firmware size is too small\n");
          return -EINVAL;
  }
 
+ dev_dbg(dev, "Firmware is an elf%d file\n",
+         class == ELFCLASS32 ? 32 : 64);
+

Yes, this is useful.

Quoted 98 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
  return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
- struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
- return ehdr->e_entry;
+ return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
  struct device *dev = &rproc->dev;
- struct elf32_hdr *ehdr;
- struct elf32_phdr *phdr;
+ const void *ehdr, *phdr;
  int i, ret = 0;
+ u16 phnum;
  const u8 *elf_data = fw->data;
+ u8 class = fw_elf_get_class(fw);
+ u32 elf_phdr_size = elf_size_of_phdr(class);
 
- ehdr = (struct elf32_hdr *)elf_data;
- phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+ ehdr = elf_data;
+ phnum = elf_hdr_e_phnum(class, ehdr);
+ phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
  /* go through the available ELF segments */
- for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-         u32 da = phdr->p_paddr;
-         u32 memsz = phdr->p_memsz;
-         u32 filesz = phdr->p_filesz;
-         u32 offset = phdr->p_offset;
+ for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+         u64 da = elf_phdr_p_paddr(class, phdr);
+         u64 memsz = elf_phdr_p_memsz(class, phdr);
+         u64 filesz = elf_phdr_p_filesz(class, phdr);
+         u64 offset = elf_phdr_p_offset(class, phdr);
+         u32 type = elf_phdr_p_type(class, phdr);
          void *ptr;
 
-         if (phdr->p_type != PT_LOAD)
+         if (type != PT_LOAD)
                  continue;
 
-         dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-                 phdr->p_type, da, memsz, filesz);
+         dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+                 type, da, memsz, filesz);
 
          if (filesz > memsz) {
-                 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+                 dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
                          filesz, memsz);
                  ret = -EINVAL;
                  break;
          }
 
          if (offset + filesz > fw->size) {
-                 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+                 dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
                          offset + filesz, fw->size);
                  ret = -EINVAL;
                  break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
          /* grab the kernel address for this device address */
          ptr = rproc_da_to_va(rproc, da, memsz);
          if (!ptr) {
-                 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+                 dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+                         memsz);
                  ret = -EINVAL;
                  break;
          }
 
          /* put the segment where the remote processor expects it */
-         if (phdr->p_filesz)
-                 memcpy(ptr, elf_data + phdr->p_offset, filesz);
+         if (filesz)
+                 memcpy(ptr, elf_data + offset, filesz);
 
          /*

* Zero out remaining memory for this segment.

drivers/remoteproc/remoteproc_elf_loader.c +0 -0
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
Quoted 5 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 230

Quoted 3 lines
drivers/remoteproc/remoteproc_elf_loader.c +1 -2
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *

Not sure I understand the "const" - was the compiler complaining?

It's actually caused by the fact I used a const void* shdr in the caller. I will check if this is mandatory.

Quoted 24 lines
Quoted 13 lines
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
+find_table(struct device *dev, const struct firmware *fw)
 {
- struct elf32_shdr *shdr;
+ const void *shdr, *name_table_shdr;
  int i;
  const char *name_table;
  struct resource_table *table = NULL;
- const u8 *elf_data = (void *)ehdr;
+ const u8 *elf_data = (void *)fw->data;
+ u8 class = fw_elf_get_class(fw);
+ size_t fw_size = fw->size;
+ const void *ehdr = elf_data;

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 248

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
+ u16 shnum = elf_hdr_e_shnum(class, ehdr);
+ u32 elf_shdr_size = elf_size_of_shdr(class);
+ u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
  /* look for the resource table and handle it */
- shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
- name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+ shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+ name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+ name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
Quoted 3 lines

It took me a while but I figured out what is happening here. To save me (and other people) from going through the same mental process every time I look at this code, please add a comment for each of the above 3 lines.

Indeed.

Quoted 68 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +12 -11
- for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-         u32 size = shdr->sh_size;
-         u32 offset = shdr->sh_offset;
+ for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+         u64 size = elf_shdr_sh_size(class, shdr);
+         u64 offset = elf_shdr_sh_offset(class, shdr);
+         u32 name = elf_shdr_sh_name(class, shdr);
 
-         if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+         if (strcmp(name_table + name, ".resource_table"))
                  continue;
 
          table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
- struct elf32_hdr *ehdr;
- struct elf32_shdr *shdr;
+ const void *shdr;
  struct device *dev = &rproc->dev;
  struct resource_table *table = NULL;
  const u8 *elf_data = fw->data;
  size_t tablesz;
+ u8 class = fw_elf_get_class(fw);
+ u64 sh_offset;
 
- ehdr = (struct elf32_hdr *)elf_data;
-
- shdr = find_table(dev, ehdr, fw->size);
+ shdr = find_table(dev, fw);
  if (!shdr)
          return -EINVAL;
 
- table = (struct resource_table *)(elf_data + shdr->sh_offset);
- tablesz = shdr->sh_size;
+ sh_offset = elf_shdr_sh_offset(class, shdr);
+ table = (struct resource_table *)(elf_data + sh_offset);
+ tablesz = elf_shdr_sh_size(class, shdr);
 
  /*

* Create a copy of the resource table. When a virtio device starts

drivers/remoteproc/remoteproc_elf_loader.c +3 -2
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
                                                 const struct firmware *fw)
 {
- struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
- struct elf32_shdr *shdr;
+ const void *shdr;
+ u64 sh_addr, sh_size;
+ u8 class = fw_elf_get_class(fw);
 

drivers/remoteproc/remoteproc_elf_loader.c EXPORT_SYMBOL(rproc_elf_load_rsc_table); · line 367

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -2
- shdr = find_table(&rproc->dev, ehdr, fw->size);
+ shdr = find_table(&rproc->dev, fw);
  if (!shdr)
          return NULL;
 
- return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+ sh_addr = elf_shdr_sh_addr(class, shdr);
+ sh_size = elf_shdr_sh_size(class, shdr);
+
+ return rproc_da_to_va(rproc, sh_addr, sh_size);

The prototype for the above is as follow:

Quoted 6 lines

void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)

But sh_size is a u64, which will cause problem that are hard to debug. I think it is better to move 'len' to an 8 byte type along with the refactoring of the existing code that is implied. I suggest to split this work in a preparatory patch (that will still be part of this set).

Nice catch ! I will do that.

Quoted 122 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +45 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+ struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+ return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+ if (class == ELFCLASS32) \
+         return (__type) ((const struct elf32_##__s *) arg)->__field; \
+ else \
+         return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+

drivers/remoteproc/remoteproc_elf_loader.h line 55

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +10 -0
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)

I like how you did this.

Quoted 54 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +14 -0
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+ if (class == ELFCLASS32)\
+         return sizeof(struct elf32_##__s); \
+ else \
+         return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
          }
  }
 
- dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+ dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
  return 0;
 
include/linux/remoteproc.h +1 -1
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
                          struct rproc *rproc, const struct firmware *fw);
  int (*load)(struct rproc *rproc, const struct firmware *fw);
  int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
- u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+ u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
Quoted 15 lines

include/linux/remoteproc.h struct rproc { · line 484

Quoted 10 lines
include/linux/remoteproc.h +1 -1
 /**
@@ -478,7 +478,7 @@ struct rproc {
  int num_traces;
  struct list_head carveouts;
  struct list_head mappings;
- u32 bootaddr;
+ u64 bootaddr;
  struct list_head rvdevs;
  struct list_head subdevs;
  struct idr notifyids;

Please hold off before doing another respin of this patch. While doing something completely different I noticed TI also did some work in this area. I'd like to take some time to look at their implementation and see if they carry features that haven't been included here. I intend to do this tomorrow.

Quoted 3 lines

Ok,

Thanks for your review,

As promised I looked at what Suman had done on his side [1] to support 64-bit ELF
files. His approach to offer the same functionality but for 64 bit in a new file is quick, simple and flexible. On the flip side it introduces code duplication, something that is seriously frowned upon upstream.

I did some soul searching in the kernel code and found very little in terms of implementation that deals with both 32 and 64 bit ELF format. The most convincing approach was set forth by the MIPS guys [2]. They too have decided to support both types in the same functions, but I don't see us adding an if() statement (and the code duplication that comes with it) every time we need to deal with file types.

Given the above I'm in favour of moving forward with your approach. One could rightly argue the macros make the code harder to read but given the alternatives, it seems to be the best solution.

Mathieu

[1]. https://bit.ly/2Rpmb4E
[2]. https://elixir.bootlin.com/linux/v5.5-rc6/source/arch/mips/kernel/elf.c#L75

Quoted 9 lines

Clément

Quoted 6 lines

Thanks,
Mathieu

Quoted 2 lines

--
2.15.0.276.g89ea799

#15 lore raw

Re: [PATCH v2] remoteproc: Add elf64 support in elf loader

Ok, here is the example to be more clear:

Content of elfxx_loader.h:

int elf##ELF_TYPE##_load(...) {
	struct elf##ELF_TYPE##_shdr *shdr = data;
	...
}

Then in elf_loader.c file:

#define ELF_TYPE 32
#include "elfxx_loader.h"
#undef ELF_TYPE
#define ELF_TYPE 64
#include "elfxx_loader.h"

int elf_load(...) {
	if (class == elf64)
		elf64_load(...);
	else
		elf32_load(...);
}

This is used for syscall_table.c in arch for instance. A macro must be defined and then the ehader is included and "generates" code.

#undef __SYSCALL
#define __SYSCALL(nr, call) [nr] = (call),

void *sys_call_table[__NR_syscalls] = {
	[0 ... __NR_syscalls - 1] = sys_ni_syscall,

#include <asm/unistd.h>
};

Hope this shed some light on my suggestion.

Clément

Quoted 593 lines · ----- On 28 Jan, 2020, at 18:14, Mathieu Poirier [email protected]…
Quoted 14 lines · Clément Leger

Hi Mathieu,

Thanks for your thorough review. While thinking about it,there is at least another option which would consist in splitting all elf specific functions into a separate .h file and then include it in a .c to "instantiate" the functions with correct types. For instance, it would look like this:

#define ELF_TYPE 32
#include "elf_functions.h"
#undef ELF_TYPE
#define ELF_TYPE 64
#include "elf_functions.h"

Quoted 5 lines

pros: More readable and type-checking ok
cons: A bit hackish

I would say this might be a better optino than my current patch. However, I'm not sure this kind of thing of well accepted in the kernel.

I won't claim to fully understand your suggestion above, but if it is suspicious enough to look hackish to you than it will probably look hackish to other people. Nonetheless there might be a case for exception if the approach yields clear advantages. Can you point me to an example somewhere in the kernel code where something similar would have been done?

Quoted 565 lines

Clément

----- On 24 Jan, 2020, at 22:58, Mathieu Poirier [email protected]

Quoted 559 lines · wrote:
Quoted 524 lines · Clément Leger

Hi Mathieu,

----- On 24 Jan, 2020, at 01:53, Mathieu Poirier [email protected]

Quoted 10 lines · wrote:

Hi Clement,

Quoted 7 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

Quoted 8 lines
Quoted 6 lines

---
Changes in v2:
- Add ELF64 support in documentation

First and foremost please address the complaints from checkpatch.

I fixed one typo in accommodate. The other checkpatch complaint is about missing parenthesis for macros with complex values which is unfortunately not possible since I'm generating inline functions.

Did you have any other one ?

Quoted 53 lines
Quoted 53 lines
---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 157 insertions(+), 57 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
  const char *name = rproc->firmware;
  struct device *dev = &rproc->dev;
+ /*
+  * Elf files are beginning with the same structure. Thus, to simplify
+  * header parsing, we can use the elf32_hdr one for both elf64 and
+  * elf32.
+  */
  struct elf32_hdr *ehdr;
+ u32 elf_shdr_size;
+ u64 phoff, shoff;
  char class;
+ u16 phnum;
 
Quoted 8 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 67

Quoted 4 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
  if (!fw) {
          dev_err(dev, "failed to load %s\n", name);
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)

In the current code [1] fw->size is compared against the size of an elf32_hdr. If support for elf64 is added that code needs to be modified to check for the right header size using fw_elf_get_class().

Actually, the elf32 header is smaller than the elf64 one so this check is there to ensure that we have at least a minimal elf header (elf32). And since the class is derived from the header, you better have to check the header size before accessing it.

To be more clear, I could compare it to min(sizeof(struct elf32_hdr), sizeof(struct elf64_hdr)) or add a comment at least stating that since elf header contains the same fields for identification, we can use the elf32 one.

Quoted 14 lines

[1]
https://elixir.bootlin.com/linux/v5.5-rc6/source/drivers/remoteproc/remoteproc_elf_loader.c#L46

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 74

Quoted 9 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -1
  ehdr = (struct elf32_hdr *)fw->data;
 
- /* We only support ELF32 at this point */
+ if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+         dev_err(dev, "Image is corrupted (bad magic)\n");
+         return -EINVAL;
+ }
+
Quoted 5 lines

Is there a reason to move this check up where? If not please bring it back to its original location, that is below:

"if (fw->size < ehdr->e_shoff + sizeof(structelf32_shdr))"

This is because the new check for size uses elf_shdr_size which is derived from the class. And since the class is extracted from the elf header, we need to check
the header to be correct first.

Quoted 143 lines
Quoted 32 lines
drivers/remoteproc/remoteproc_elf_loader.c +8 -8
  class = ehdr->e_ident[EI_CLASS];
- if (class != ELFCLASS32) {
+ if (class != ELFCLASS32 && class != ELFCLASS64) {
          dev_err(dev, "Unsupported class: %d\n", class);
          return -EINVAL;
  }
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
          return -EINVAL;
  }
 
- if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-         dev_err(dev, "Image is too small\n");
-         return -EINVAL;
- }
+ phoff = elf_hdr_e_phoff(class, fw->data);
+ shoff = elf_hdr_e_shoff(class, fw->data);
+ phnum =  elf_hdr_e_phnum(class, fw->data);
+ elf_shdr_size = elf_size_of_shdr(class);
 
- if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-         dev_err(dev, "Image is corrupted (bad magic)\n");
+ if (fw->size < shoff + elf_shdr_size) {
+         dev_err(dev, "Image is too small\n");
          return -EINVAL;
  }
 
- if (ehdr->e_phnum == 0) {
+ if (phnum == 0) {
          dev_err(dev, "No loadable segments\n");
          return -EINVAL;
  }

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 114

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +4 -1
 
- if (ehdr->e_phoff > fw->size) {
+ if (phoff > fw->size) {
          dev_err(dev, "Firmware size is too small\n");
          return -EINVAL;
  }
 
+ dev_dbg(dev, "Firmware is an elf%d file\n",
+         class == ELFCLASS32 ? 32 : 64);
+

Yes, this is useful.

Quoted 98 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
  return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
- struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
- return ehdr->e_entry;
+ return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
  struct device *dev = &rproc->dev;
- struct elf32_hdr *ehdr;
- struct elf32_phdr *phdr;
+ const void *ehdr, *phdr;
  int i, ret = 0;
+ u16 phnum;
  const u8 *elf_data = fw->data;
+ u8 class = fw_elf_get_class(fw);
+ u32 elf_phdr_size = elf_size_of_phdr(class);
 
- ehdr = (struct elf32_hdr *)elf_data;
- phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+ ehdr = elf_data;
+ phnum = elf_hdr_e_phnum(class, ehdr);
+ phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
  /* go through the available ELF segments */
- for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-         u32 da = phdr->p_paddr;
-         u32 memsz = phdr->p_memsz;
-         u32 filesz = phdr->p_filesz;
-         u32 offset = phdr->p_offset;
+ for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+         u64 da = elf_phdr_p_paddr(class, phdr);
+         u64 memsz = elf_phdr_p_memsz(class, phdr);
+         u64 filesz = elf_phdr_p_filesz(class, phdr);
+         u64 offset = elf_phdr_p_offset(class, phdr);
+         u32 type = elf_phdr_p_type(class, phdr);
          void *ptr;
 
-         if (phdr->p_type != PT_LOAD)
+         if (type != PT_LOAD)
                  continue;
 
-         dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-                 phdr->p_type, da, memsz, filesz);
+         dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+                 type, da, memsz, filesz);
 
          if (filesz > memsz) {
-                 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+                 dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
                          filesz, memsz);
                  ret = -EINVAL;
                  break;
          }
 
          if (offset + filesz > fw->size) {
-                 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+                 dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
                          offset + filesz, fw->size);
                  ret = -EINVAL;
                  break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
          /* grab the kernel address for this device address */
          ptr = rproc_da_to_va(rproc, da, memsz);
          if (!ptr) {
-                 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+                 dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+                         memsz);
                  ret = -EINVAL;
                  break;
          }
 
          /* put the segment where the remote processor expects it */
-         if (phdr->p_filesz)
-                 memcpy(ptr, elf_data + phdr->p_offset, filesz);
+         if (filesz)
+                 memcpy(ptr, elf_data + offset, filesz);
 
          /*

* Zero out remaining memory for this segment.

drivers/remoteproc/remoteproc_elf_loader.c +0 -0
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
Quoted 5 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 230

Quoted 3 lines
drivers/remoteproc/remoteproc_elf_loader.c +1 -2
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *

Not sure I understand the "const" - was the compiler complaining?

It's actually caused by the fact I used a const void* shdr in the caller. I will check if this is mandatory.

Quoted 24 lines
Quoted 13 lines
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
+find_table(struct device *dev, const struct firmware *fw)
 {
- struct elf32_shdr *shdr;
+ const void *shdr, *name_table_shdr;
  int i;
  const char *name_table;
  struct resource_table *table = NULL;
- const u8 *elf_data = (void *)ehdr;
+ const u8 *elf_data = (void *)fw->data;
+ u8 class = fw_elf_get_class(fw);
+ size_t fw_size = fw->size;
+ const void *ehdr = elf_data;

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) · line 248

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
+ u16 shnum = elf_hdr_e_shnum(class, ehdr);
+ u32 elf_shdr_size = elf_size_of_shdr(class);
+ u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
  /* look for the resource table and handle it */
- shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
- name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+ shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+ name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+ name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
Quoted 3 lines

It took me a while but I figured out what is happening here. To save me (and other people) from going through the same mental process every time I look at this code, please add a comment for each of the above 3 lines.

Indeed.

Quoted 68 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +12 -11
- for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-         u32 size = shdr->sh_size;
-         u32 offset = shdr->sh_offset;
+ for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+         u64 size = elf_shdr_sh_size(class, shdr);
+         u64 offset = elf_shdr_sh_offset(class, shdr);
+         u32 name = elf_shdr_sh_name(class, shdr);
 
-         if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+         if (strcmp(name_table + name, ".resource_table"))
                  continue;
 
          table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
- struct elf32_hdr *ehdr;
- struct elf32_shdr *shdr;
+ const void *shdr;
  struct device *dev = &rproc->dev;
  struct resource_table *table = NULL;
  const u8 *elf_data = fw->data;
  size_t tablesz;
+ u8 class = fw_elf_get_class(fw);
+ u64 sh_offset;
 
- ehdr = (struct elf32_hdr *)elf_data;
-
- shdr = find_table(dev, ehdr, fw->size);
+ shdr = find_table(dev, fw);
  if (!shdr)
          return -EINVAL;
 
- table = (struct resource_table *)(elf_data + shdr->sh_offset);
- tablesz = shdr->sh_size;
+ sh_offset = elf_shdr_sh_offset(class, shdr);
+ table = (struct resource_table *)(elf_data + sh_offset);
+ tablesz = elf_shdr_sh_size(class, shdr);
 
  /*

* Create a copy of the resource table. When a virtio device starts

drivers/remoteproc/remoteproc_elf_loader.c +3 -2
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
                                                 const struct firmware *fw)
 {
- struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
- struct elf32_shdr *shdr;
+ const void *shdr;
+ u64 sh_addr, sh_size;
+ u8 class = fw_elf_get_class(fw);
 

drivers/remoteproc/remoteproc_elf_loader.c EXPORT_SYMBOL(rproc_elf_load_rsc_table); · line 367

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -2
- shdr = find_table(&rproc->dev, ehdr, fw->size);
+ shdr = find_table(&rproc->dev, fw);
  if (!shdr)
          return NULL;
 
- return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+ sh_addr = elf_shdr_sh_addr(class, shdr);
+ sh_size = elf_shdr_sh_size(class, shdr);
+
+ return rproc_da_to_va(rproc, sh_addr, sh_size);

The prototype for the above is as follow:

Quoted 6 lines

void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)

But sh_size is a u64, which will cause problem that are hard to debug. I think it is better to move 'len' to an 8 byte type along with the refactoring of the existing code that is implied. I suggest to split this work in a preparatory patch (that will still be part of this set).

Nice catch ! I will do that.

Quoted 122 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +45 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+ struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+ return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+ if (class == ELFCLASS32) \
+         return (__type) ((const struct elf32_##__s *) arg)->__field; \
+ else \
+         return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+

drivers/remoteproc/remoteproc_elf_loader.h line 55

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +10 -0
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)

I like how you did this.

Quoted 54 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +14 -0
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+ if (class == ELFCLASS32)\
+         return sizeof(struct elf32_##__s); \
+ else \
+         return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
          }
  }
 
- dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+ dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
  return 0;
 
include/linux/remoteproc.h +1 -1
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
                          struct rproc *rproc, const struct firmware *fw);
  int (*load)(struct rproc *rproc, const struct firmware *fw);
  int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
- u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+ u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
Quoted 15 lines

include/linux/remoteproc.h struct rproc { · line 484

Quoted 10 lines
include/linux/remoteproc.h +1 -1
 /**
@@ -478,7 +478,7 @@ struct rproc {
  int num_traces;
  struct list_head carveouts;
  struct list_head mappings;
- u32 bootaddr;
+ u64 bootaddr;
  struct list_head rvdevs;
  struct list_head subdevs;
  struct idr notifyids;

Please hold off before doing another respin of this patch. While doing something completely different I noticed TI also did some work in this area. I'd like to take some time to look at their implementation and see if they carry features that haven't been included here. I intend to do this tomorrow.

Quoted 3 lines

Ok,

Thanks for your review,

As promised I looked at what Suman had done on his side [1] to support 64-bit ELF
files. His approach to offer the same functionality but for 64 bit in a new file is quick, simple and flexible. On the flip side it introduces code duplication, something that is seriously frowned upon upstream.

I did some soul searching in the kernel code and found very little in terms of implementation that deals with both 32 and 64 bit ELF format. The most convincing approach was set forth by the MIPS guys [2]. They too have decided to support both types in the same functions, but I don't see us adding an if() statement (and the code duplication that comes with it) every time we need to deal with file types.

Given the above I'm in favour of moving forward with your approach. One could rightly argue the macros make the code harder to read but given the alternatives, it seems to be the best solution.

Mathieu

[1]. https://bit.ly/2Rpmb4E
[2]. https://elixir.bootlin.com/linux/v5.5-rc6/source/arch/mips/kernel/elf.c#L75

Quoted 9 lines

Clément

Quoted 6 lines

Thanks,
Mathieu

Quoted 2 lines

--
2.15.0.276.g89ea799

maintainer
#16 lore raw

Re: [PATCH v2] remoteproc: Add elf64 support in elf loader

Quoted 31 lines · Cl�ment Leger

Ok, here is the example to be more clear:

Content of elfxx_loader.h:

int elf##ELF_TYPE##_load(...) {
	struct elf##ELF_TYPE##_shdr *shdr = data;
	...
}

Then in elf_loader.c file:

#define ELF_TYPE 32
#include "elfxx_loader.h"
#undef ELF_TYPE
#define ELF_TYPE 64
#include "elfxx_loader.h"

int elf_load(...) {
	if (class == elf64)
		elf64_load(...);
	else
		elf32_load(...);
}

This is used for syscall_table.c in arch for instance. A macro must be defined and then the ehader is included and "generates" code.

#undef __SYSCALL
#define __SYSCALL(nr, call) [nr] = (call),

Quoted 6 lines
void *sys_call_table[__NR_syscalls] = {
	[0 ... __NR_syscalls - 1] = sys_ni_syscall,

#include <asm/unistd.h>
};

Hope this shed some light on my suggestion.

Thank you for taking the time to do this.

I fear elfxx_loader.h would become very cryptic and hard to debug. I think your original approach strikes the right balance between readabiliy and code duplication.

Quoted 598 lines

Cl�ment

Quoted 593 lines · ----- On 28 Jan, 2020, at 18:14, Mathieu Poirier [email protected]…
Quoted 14 lines · Cl�ment Leger

Hi Mathieu,

Thanks for your thorough review. While thinking about it,there is at least another option which would consist in splitting all elf specific functions into a separate .h file and then include it in a .c to "instantiate" the functions with correct types. For instance, it would look like this:

#define ELF_TYPE 32
#include "elf_functions.h"
#undef ELF_TYPE
#define ELF_TYPE 64
#include "elf_functions.h"

Quoted 5 lines

pros: More readable and type-checking ok
cons: A bit hackish

I would say this might be a better optino than my current patch. However, I'm not sure this kind of thing of well accepted in the kernel.

I won't claim to fully understand your suggestion above, but if it is suspicious enough to look hackish to you than it will probably look hackish to other people. Nonetheless there might be a case for exception if the approach yields clear advantages. Can you point me to an example somewhere in the kernel code where something similar would have been done?

Quoted 565 lines

Cl�ment

----- On 24 Jan, 2020, at 22:58, Mathieu Poirier [email protected]

Quoted 559 lines · wrote:
Quoted 524 lines · Cl�ment Leger

Hi Mathieu,

----- On 24 Jan, 2020, at 01:53, Mathieu Poirier [email protected]

Quoted 10 lines · wrote:

Hi Clement,

Quoted 7 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accomodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

Quoted 8 lines
Quoted 6 lines

---
Changes in v2:
- Add ELF64 support in documentation

First and foremost please address the complaints from checkpatch.

I fixed one typo in accommodate. The other checkpatch complaint is about missing parenthesis for macros with complex values which is unfortunately not possible since I'm generating inline functions.

Did you have any other one ?

Quoted 53 lines
Quoted 53 lines
---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 135 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 +++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 157 insertions(+), 57 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 77fb03acdbb4..bf4f0c41ec4e 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index b17d72ec8603..6a2d31d6092c 100644
@@ -31,6 +31,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -43,8 +44,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
  const char *name = rproc->firmware;
  struct device *dev = &rproc->dev;
+ /*
+  * Elf files are beginning with the same structure. Thus, to simplify
+  * header parsing, we can use the elf32_hdr one for both elf64 and
+  * elf32.
+  */
  struct elf32_hdr *ehdr;
+ u32 elf_shdr_size;
+ u64 phoff, shoff;
  char class;
+ u16 phnum;
 
Quoted 8 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 67

Quoted 4 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
  if (!fw) {
          dev_err(dev, "failed to load %s\n", name);
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
@@ -58,9 +67,13 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)

In the current code [1] fw->size is compared against the size of an elf32_hdr. If support for elf64 is added that code needs to be modified to check for the right header size using fw_elf_get_class().

Actually, the elf32 header is smaller than the elf64 one so this check is there to ensure that we have at least a minimal elf header (elf32). And since the class is derived from the header, you better have to check the header size before accessing it.

To be more clear, I could compare it to min(sizeof(struct elf32_hdr), sizeof(struct elf64_hdr)) or add a comment at least stating that since elf header contains the same fields for identification, we can use the elf32 one.

Quoted 14 lines

[1]
https://elixir.bootlin.com/linux/v5.5-rc6/source/drivers/remoteproc/remoteproc_elf_loader.c#L46

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 74

Quoted 9 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -1
  ehdr = (struct elf32_hdr *)fw->data;
 
- /* We only support ELF32 at this point */
+ if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+         dev_err(dev, "Image is corrupted (bad magic)\n");
+         return -EINVAL;
+ }
+
Quoted 5 lines

Is there a reason to move this check up where? If not please bring it back to its original location, that is below:

"if (fw->size < ehdr->e_shoff + sizeof(structelf32_shdr))"

This is because the new check for size uses elf_shdr_size which is derived from the class. And since the class is extracted from the elf header, we need to check
the header to be correct first.

Quoted 143 lines
Quoted 32 lines
drivers/remoteproc/remoteproc_elf_loader.c +8 -8
  class = ehdr->e_ident[EI_CLASS];
- if (class != ELFCLASS32) {
+ if (class != ELFCLASS32 && class != ELFCLASS64) {
          dev_err(dev, "Unsupported class: %d\n", class);
          return -EINVAL;
  }
@@ -75,26 +88,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
          return -EINVAL;
  }
 
- if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-         dev_err(dev, "Image is too small\n");
-         return -EINVAL;
- }
+ phoff = elf_hdr_e_phoff(class, fw->data);
+ shoff = elf_hdr_e_shoff(class, fw->data);
+ phnum =  elf_hdr_e_phnum(class, fw->data);
+ elf_shdr_size = elf_size_of_shdr(class);
 
- if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-         dev_err(dev, "Image is corrupted (bad magic)\n");
+ if (fw->size < shoff + elf_shdr_size) {
+         dev_err(dev, "Image is too small\n");
          return -EINVAL;
  }
 
- if (ehdr->e_phnum == 0) {
+ if (phnum == 0) {
          dev_err(dev, "No loadable segments\n");
          return -EINVAL;
  }

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct · line 114

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +4 -1
 
- if (ehdr->e_phoff > fw->size) {
+ if (phoff > fw->size) {
          dev_err(dev, "Firmware size is too small\n");
          return -EINVAL;
  }
 
+ dev_dbg(dev, "Firmware is an elf%d file\n",
+         class == ELFCLASS32 ? 32 : 64);
+

Yes, this is useful.

Quoted 98 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
  return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -110,11 +126,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
- struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
- return ehdr->e_entry;
+ return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -145,37 +159,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
  struct device *dev = &rproc->dev;
- struct elf32_hdr *ehdr;
- struct elf32_phdr *phdr;
+ const void *ehdr, *phdr;
  int i, ret = 0;
+ u16 phnum;
  const u8 *elf_data = fw->data;
+ u8 class = fw_elf_get_class(fw);
+ u32 elf_phdr_size = elf_size_of_phdr(class);
 
- ehdr = (struct elf32_hdr *)elf_data;
- phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+ ehdr = elf_data;
+ phnum = elf_hdr_e_phnum(class, ehdr);
+ phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
  /* go through the available ELF segments */
- for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-         u32 da = phdr->p_paddr;
-         u32 memsz = phdr->p_memsz;
-         u32 filesz = phdr->p_filesz;
-         u32 offset = phdr->p_offset;
+ for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+         u64 da = elf_phdr_p_paddr(class, phdr);
+         u64 memsz = elf_phdr_p_memsz(class, phdr);
+         u64 filesz = elf_phdr_p_filesz(class, phdr);
+         u64 offset = elf_phdr_p_offset(class, phdr);
+         u32 type = elf_phdr_p_type(class, phdr);
          void *ptr;
 
-         if (phdr->p_type != PT_LOAD)
+         if (type != PT_LOAD)
                  continue;
 
-         dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-                 phdr->p_type, da, memsz, filesz);
+         dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+                 type, da, memsz, filesz);
 
          if (filesz > memsz) {
-                 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+                 dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
                          filesz, memsz);
                  ret = -EINVAL;
                  break;
          }
 
          if (offset + filesz > fw->size) {
-                 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+                 dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
                          offset + filesz, fw->size);
                  ret = -EINVAL;
                  break;
@@ -184,14 +202,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
          /* grab the kernel address for this device address */
          ptr = rproc_da_to_va(rproc, da, memsz);
          if (!ptr) {
-                 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+                 dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+                         memsz);
                  ret = -EINVAL;
                  break;
          }
 
          /* put the segment where the remote processor expects it */
-         if (phdr->p_filesz)
-                 memcpy(ptr, elf_data + phdr->p_offset, filesz);
+         if (filesz)
+                 memcpy(ptr, elf_data + offset, filesz);
 
          /*

* Zero out remaining memory for this segment.

drivers/remoteproc/remoteproc_elf_loader.c +0 -0
@@ -208,24 +227,32 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
Quoted 5 lines

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const · line 231

Quoted 3 lines
drivers/remoteproc/remoteproc_elf_loader.c +1 -2
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *

Not sure I understand the "const" - was the compiler complaining?

It's actually caused by the fact I used a const void* shdr in the caller. I will check if this is mandatory.

Quoted 24 lines
Quoted 13 lines
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
+find_table(struct device *dev, const struct firmware *fw)
 {
- struct elf32_shdr *shdr;
+ const void *shdr, *name_table_shdr;
  int i;
  const char *name_table;
  struct resource_table *table = NULL;
- const u8 *elf_data = (void *)ehdr;
+ const u8 *elf_data = (void *)fw->data;
+ u8 class = fw_elf_get_class(fw);
+ size_t fw_size = fw->size;
+ const void *ehdr = elf_data;

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_load_segments(struct rproc *rproc, const · line 249

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +6 -2
+ u16 shnum = elf_hdr_e_shnum(class, ehdr);
+ u32 elf_shdr_size = elf_size_of_shdr(class);
+ u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
  /* look for the resource table and handle it */
- shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
- name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
+ shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+ name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+ name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
Quoted 3 lines

It took me a while but I figured out what is happening here. To save me (and other people) from going through the same mental process every time I look at this code, please add a comment for each of the above 3 lines.

Indeed.

Quoted 68 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +12 -11
- for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-         u32 size = shdr->sh_size;
-         u32 offset = shdr->sh_offset;
+ for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+         u64 size = elf_shdr_sh_size(class, shdr);
+         u64 offset = elf_shdr_sh_offset(class, shdr);
+         u32 name = elf_shdr_sh_name(class, shdr);
 
-         if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+         if (strcmp(name_table + name, ".resource_table"))
                  continue;
 
          table = (struct resource_table *)(elf_data + offset);
@@ -279,21 +306,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
- struct elf32_hdr *ehdr;
- struct elf32_shdr *shdr;
+ const void *shdr;
  struct device *dev = &rproc->dev;
  struct resource_table *table = NULL;
  const u8 *elf_data = fw->data;
  size_t tablesz;
+ u8 class = fw_elf_get_class(fw);
+ u64 sh_offset;
 
- ehdr = (struct elf32_hdr *)elf_data;
-
- shdr = find_table(dev, ehdr, fw->size);
+ shdr = find_table(dev, fw);
  if (!shdr)
          return -EINVAL;
 
- table = (struct resource_table *)(elf_data + shdr->sh_offset);
- tablesz = shdr->sh_size;
+ sh_offset = elf_shdr_sh_offset(class, shdr);
+ table = (struct resource_table *)(elf_data + sh_offset);
+ tablesz = elf_shdr_sh_size(class, shdr);
 
  /*

* Create a copy of the resource table. When a virtio device starts

drivers/remoteproc/remoteproc_elf_loader.c +3 -2
@@ -326,13 +353,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
                                                 const struct firmware *fw)
 {
- struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
- struct elf32_shdr *shdr;
+ const void *shdr;
+ u64 sh_addr, sh_size;
+ u8 class = fw_elf_get_class(fw);
 

drivers/remoteproc/remoteproc_elf_loader.c EXPORT_SYMBOL(rproc_elf_load_rsc_table); · line 367

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +5 -2
- shdr = find_table(&rproc->dev, ehdr, fw->size);
+ shdr = find_table(&rproc->dev, fw);
  if (!shdr)
          return NULL;
 
- return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+ sh_addr = elf_shdr_sh_addr(class, shdr);
+ sh_size = elf_shdr_sh_size(class, shdr);
+
+ return rproc_da_to_va(rproc, sh_addr, sh_size);

The prototype for the above is as follow:

Quoted 6 lines

void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)

But sh_size is a u64, which will cause problem that are hard to debug. I think it is better to move 'len' to an 8 byte type along with the refactoring of the existing code that is implied. I suggest to split this work in a preparatory patch (that will still be part of this set).

Nice catch ! I will do that.

Quoted 122 lines
Quoted 55 lines
drivers/remoteproc/remoteproc_elf_loader.c +0 -0
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +45 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+ struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+ return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+ if (class == ELFCLASS32) \
+         return (__type) ((const struct elf32_##__s *) arg)->__field; \
+ else \
+         return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+

drivers/remoteproc/remoteproc_elf_loader.h line 55

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +10 -0
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)

I like how you did this.

Quoted 54 lines
drivers/remoteproc/remoteproc_elf_loader.h new file +14 -0
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+ if (class == ELFCLASS32)\
+         return sizeof(struct elf32_##__s); \
+ else \
+         return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 45ff76a06c72..4ef745e3a1bc 100644
@@ -63,7 +63,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index 51049d17b1e5..e23abd8a96b0 100644
@@ -193,7 +193,7 @@ static int st_rproc_start(struct rproc *rproc)
          }
  }
 
- dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+ dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
  return 0;
 
include/linux/remoteproc.h +1 -1
index 04d04709f2bd..512de9a2590c 100644
@@ -362,7 +362,7 @@ struct rproc_ops {
                          struct rproc *rproc, const struct firmware *fw);
  int (*load)(struct rproc *rproc, const struct firmware *fw);
  int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
- u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+ u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
Quoted 15 lines

include/linux/remoteproc.h struct rproc { · line 484

Quoted 10 lines
include/linux/remoteproc.h +1 -1
 /**
@@ -478,7 +478,7 @@ struct rproc {
  int num_traces;
  struct list_head carveouts;
  struct list_head mappings;
- u32 bootaddr;
+ u64 bootaddr;
  struct list_head rvdevs;
  struct list_head subdevs;
  struct idr notifyids;

Please hold off before doing another respin of this patch. While doing something completely different I noticed TI also did some work in this area. I'd like to take some time to look at their implementation and see if they carry features that haven't been included here. I intend to do this tomorrow.

Quoted 3 lines

Ok,

Thanks for your review,

As promised I looked at what Suman had done on his side [1] to support 64-bit ELF
files. His approach to offer the same functionality but for 64 bit in a new file is quick, simple and flexible. On the flip side it introduces code duplication, something that is seriously frowned upon upstream.

I did some soul searching in the kernel code and found very little in terms of implementation that deals with both 32 and 64 bit ELF format. The most convincing approach was set forth by the MIPS guys [2]. They too have decided to support both types in the same functions, but I don't see us adding an if() statement (and the code duplication that comes with it) every time we need to deal with file types.

Given the above I'm in favour of moving forward with your approach. One could rightly argue the macros make the code harder to read but given the alternatives, it seems to be the best solution.

Mathieu

[1]. https://bit.ly/2Rpmb4E
[2]. https://elixir.bootlin.com/linux/v5.5-rc6/source/arch/mips/kernel/elf.c#L75

Quoted 9 lines

Cl�ment

Quoted 6 lines

Thanks,
Mathieu

Quoted 2 lines

--
2.15.0.276.g89ea799

#17 lore raw

PATCH v2 1/2 remoteproc: Use u64 len for da_to_va

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
V2:
- Change len type from int to u64

 drivers/remoteproc/imx_rproc.c           | 5 +++--
 drivers/remoteproc/keystone_remoteproc.c | 2 +-
 drivers/remoteproc/qcom_q6v5_adsp.c      | 2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      | 2 +-
 drivers/remoteproc/qcom_wcnss.c          | 2 +-
 drivers/remoteproc/remoteproc_core.c     | 2 +-
 drivers/remoteproc/remoteproc_internal.h | 2 +-
 drivers/remoteproc/st_slim_rproc.c       | 2 +-
 drivers/remoteproc/wkup_m3_rproc.c       | 2 +-
 include/linux/remoteproc.h               | 2 +-
 12 files changed, 14 insertions(+), 13 deletions(-)
drivers/remoteproc/imx_rproc.c +3 -2
index 3e72b6f38d4b..7ec79be5eb5d 100644
@@ -208,7 +208,7 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n", da, len,
+		va);
 
 	return va;
 }
drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..25c01df47eba 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
 	void __iomem *va = NULL;
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +1 -1
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +1 -1
index 04492fead3c8..2fd14afb3157 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
drivers/remoteproc/wkup_m3_rproc.c +1 -1
index 3984e585c847..0e8082948489 100644
@@ -80,7 +80,7 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
Signature · 1 line
2.15.0.276.g89ea799
#18 lore raw

PATCH v2 2/2 remoteproc: Add elf64 support in elf loader

From: Clément Leger <[email protected]>

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accommodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
V2:
- Add check for elf64 header size
- Add comments for name table parsing
- Fix typo in "accommodate"

 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 147 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 ++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 167 insertions(+), 59 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 03c3d2e568b0..2be1147256e0 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +93 -54

266 lines not on this page · raw message

drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0

69 lines not on this page · raw message

drivers/remoteproc/remoteproc_internal.h +1 -1
index 004867061721..eeb26434220e 100644
@@ -55,7 +55,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index ee13d23b43a9..a3268d95a50e 100644
@@ -190,7 +190,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +2 -2
index f84bd5fe0211..82cebca9344c 100644
@@ -382,7 +382,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -498,7 +498,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
Signature · 1 line
2.15.0.276.g89ea799
#19 lore raw

Re: [PATCH v2 1/2] remoteproc: Use u64 len for da_to_va

In reply to Clement Leger

Hi Clément,

Is it v2 or V3?
I also suggest you add a cover letter when you post more than one patch in a thread.

Quoted 30 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
V2:
- Change len type from int to u64

 drivers/remoteproc/imx_rproc.c           | 5 +++--
 drivers/remoteproc/keystone_remoteproc.c | 2 +-
 drivers/remoteproc/qcom_q6v5_adsp.c      | 2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      | 2 +-
 drivers/remoteproc/qcom_wcnss.c          | 2 +-
 drivers/remoteproc/remoteproc_core.c     | 2 +-
 drivers/remoteproc/remoteproc_internal.h | 2 +-
 drivers/remoteproc/st_slim_rproc.c       | 2 +-
 drivers/remoteproc/wkup_m3_rproc.c       | 2 +-
 include/linux/remoteproc.h               | 2 +-
 12 files changed, 14 insertions(+), 13 deletions(-)
drivers/remoteproc/imx_rproc.c +0 -0
index 3e72b6f38d4b..7ec79be5eb5d 100644

drivers/remoteproc/imx_rproc.c static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da, · line 211

Quoted 5 lines
drivers/remoteproc/imx_rproc.c +1 -1
@@ -208,7 +208,7 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

I wonder if you could use size_t instead?
Does it make sense for 32-bit system to have a physical address space higher? it's an open question I won't pretend to have the answer...:)

drivers/remoteproc/imx_rproc.c static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)

Quoted 4 lines
drivers/remoteproc/imx_rproc.c +0 -0
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)

len type to update

Regards,
Arnaud

Quoted 153 lines
drivers/remoteproc/imx_rproc.c +2 -1
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n", da, len,
+		va);
 
 	return va;
 }
drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..25c01df47eba 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
 	void __iomem *va = NULL;
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +1 -1
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +1 -1
index 04492fead3c8..2fd14afb3157 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
drivers/remoteproc/wkup_m3_rproc.c +1 -1
index 3984e585c847..0e8082948489 100644
@@ -80,7 +80,7 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
#20 lore raw

Re: [PATCH v2 1/2] remoteproc: Use u64 len for da_to_va

Hi Arnaud,

My bad, I thought it was only V1.
I will resend it as a V3 with a cover letter.

Quoted 41 lines · ----- On 4 Feb, 2020, at 17:27, Arnaud Pouliquen [email protected] wrote:

Hi Clément,

Is it v2 or V3?
I also suggest you add a cover letter when you post more than one patch in a thread.

Quoted 34 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
V2:
- Change len type from int to u64

 drivers/remoteproc/imx_rproc.c           | 5 +++--
 drivers/remoteproc/keystone_remoteproc.c | 2 +-
 drivers/remoteproc/qcom_q6v5_adsp.c      | 2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      | 2 +-
 drivers/remoteproc/qcom_wcnss.c          | 2 +-
 drivers/remoteproc/remoteproc_core.c     | 2 +-
 drivers/remoteproc/remoteproc_internal.h | 2 +-
 drivers/remoteproc/st_slim_rproc.c       | 2 +-
 drivers/remoteproc/wkup_m3_rproc.c       | 2 +-
 include/linux/remoteproc.h               | 2 +-
 12 files changed, 14 insertions(+), 13 deletions(-)
drivers/remoteproc/imx_rproc.c +0 -0
index 3e72b6f38d4b..7ec79be5eb5d 100644
@@ -208,7 +208,7 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64
 da,
 	return -ENOENT;
 }
 
Quoted 3 lines

drivers/remoteproc/imx_rproc.c static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da, · line 211

Quoted 2 lines
drivers/remoteproc/imx_rproc.c +1 -1
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

I wonder if you could use size_t instead?

I wondered too but size_t is a unsigned int on 32 bits system. So if they load a elf64 binary (which might be in 32bits zone), the segment size will be a u64 type.

Quoted 2 lines

Does it make sense for 32-bit system to have a physical address space higher? it's an open question I won't pretend to have the answer...:)

I agree that it does not make a lot of sense... But when we discussed it in OpenAMP WG, it was mentionned that this should be supported.

Quoted 8 lines

drivers/remoteproc/imx_rproc.c static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, · line 235

Quoted 7 lines
drivers/remoteproc/imx_rproc.c +0 -0
 {
 	struct imx_rproc *priv = rproc->priv;
drivers/remoteproc/imx_rproc.c +0 -0
 	void *va = NULL;
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da,
 int len)

len type to update

I missed this one and I will double check all other callers

Thanks,

Clément.

Quoted 165 lines

Regards,
Arnaud

Quoted 162 lines
drivers/remoteproc/imx_rproc.c +2 -1
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n", da, len,
+		va);
 
 	return va;

}

drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..25c01df47eba 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int
 vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
drivers/remoteproc/keystone_remoteproc.c +0 -0
 	void __iomem *va = NULL;
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +1 -1
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +1 -1
index 04492fead3c8..2fd14afb3157 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
drivers/remoteproc/wkup_m3_rproc.c +1 -1
index 3984e585c847..0e8082948489 100644
@@ -80,7 +80,7 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
#21 lore raw

Re: [PATCH v2 1/2] remoteproc: Use u64 len for da_to_va

Quoted 48 lines · Clément Leger

Hi Arnaud,

My bad, I thought it was only V1.
I will resend it as a V3 with a cover letter.

Quoted 41 lines · ----- On 4 Feb, 2020, at 17:27, Arnaud Pouliquen [email protected] wrote:

Hi Clément,

Is it v2 or V3?
I also suggest you add a cover letter when you post more than one patch in a thread.

Quoted 34 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
V2:
- Change len type from int to u64

 drivers/remoteproc/imx_rproc.c           | 5 +++--
 drivers/remoteproc/keystone_remoteproc.c | 2 +-
 drivers/remoteproc/qcom_q6v5_adsp.c      | 2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      | 2 +-
 drivers/remoteproc/qcom_wcnss.c          | 2 +-
 drivers/remoteproc/remoteproc_core.c     | 2 +-
 drivers/remoteproc/remoteproc_internal.h | 2 +-
 drivers/remoteproc/st_slim_rproc.c       | 2 +-
 drivers/remoteproc/wkup_m3_rproc.c       | 2 +-
 include/linux/remoteproc.h               | 2 +-
 12 files changed, 14 insertions(+), 13 deletions(-)
drivers/remoteproc/imx_rproc.c +0 -0
index 3e72b6f38d4b..7ec79be5eb5d 100644
@@ -208,7 +208,7 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64
 da,
 	return -ENOENT;
 }
 
Quoted 7 lines
Quoted 3 lines

drivers/remoteproc/imx_rproc.c static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da, · line 211

Quoted 2 lines
drivers/remoteproc/imx_rproc.c +1 -1
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

I wonder if you could use size_t instead?

I wondered too but size_t is a unsigned int on 32 bits system. So if they load a elf64 binary (which might be in 32bits zone), the segment size will be a u64 type.

If we consider that 32-bit processor can not address a memory space size higher than sizeof(size_t), this could be solved by adding a test and a cast.

Quoted 6 lines
Quoted 3 lines

Does it make sense for 32-bit system to have a physical address space higher? it's an open question I won't pretend to have the answer...:)

I agree that it does not make a lot of sense... But when we discussed it in OpenAMP WG, it was mentionned that this should be supported.

My apologize, i don't remember the discussion around the size. So if a requirement exists and size_t does not fit, let's use u64. Nevertheless i'm interested to know the rational, if you remember...

Regards
Arnaud

Quoted 181 lines
Quoted 9 lines

drivers/remoteproc/imx_rproc.c static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, · line 235

Quoted 8 lines
drivers/remoteproc/imx_rproc.c +0 -0
 {
 	struct imx_rproc *priv = rproc->priv;
drivers/remoteproc/imx_rproc.c +0 -0
 	void *va = NULL;
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da,
 int len)

len type to update

I missed this one and I will double check all other callers

Thanks,

Clément.

Quoted 165 lines

Regards,
Arnaud

Quoted 162 lines
drivers/remoteproc/imx_rproc.c +2 -1
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n", da, len,
+		va);
 
 	return va;

}

drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..25c01df47eba 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int
 vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
drivers/remoteproc/keystone_remoteproc.c +0 -0
 	void __iomem *va = NULL;
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +1 -1
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +1 -1
index 04492fead3c8..2fd14afb3157 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
drivers/remoteproc/wkup_m3_rproc.c +1 -1
index 3984e585c847..0e8082948489 100644
@@ -80,7 +80,7 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
#22 lore raw

v3 cover remoteproc: Add elf64 support to elf loader

In reply to Mathieu Poirier

This serie add support for elf64 in remoteproc elf loader. First patch modifies the type of len argument (in da_to_va) in order to allow loading elf64 segment with a u64 size.
Second patch is the actual modification in the elf loader to support elf64 type by using a set of generic macros.

Changes from V2:
- da_to_va len type changed from int to u64
- Add check for elf64 header size
- Add comments for name table parsing
- Fix typo in "accommodate"

Clement Leger (2):
remoteproc: Use u64 len for da_to_va
remoteproc: Add elf64 support in elf loader

 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/imx_rproc.c             |   9 +-
 drivers/remoteproc/keystone_remoteproc.c   |   2 +-
 drivers/remoteproc/qcom_q6v5_adsp.c        |   2 +-
 drivers/remoteproc/qcom_q6v5_mss.c         |   2 +-
 drivers/remoteproc/qcom_q6v5_pas.c         |   2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c        |   2 +-
 drivers/remoteproc/qcom_wcnss.c            |   2 +-
 drivers/remoteproc/remoteproc_core.c       |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 147 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 ++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   4 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 drivers/remoteproc/st_slim_rproc.c         |   4 +-
 drivers/remoteproc/wkup_m3_rproc.c         |   2 +-
 include/linux/remoteproc.h                 |   6 +-
 16 files changed, 184 insertions(+), 75 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Signature · 1 line
2.15.0.276.g89ea799
#23 lore raw

PATCH v3 1/2 remoteproc: Use u64 len for da_to_va

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 9 +++++----
 drivers/remoteproc/keystone_remoteproc.c | 2 +-
 drivers/remoteproc/qcom_q6v5_adsp.c      | 2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      | 2 +-
 drivers/remoteproc/qcom_wcnss.c          | 2 +-
 drivers/remoteproc/remoteproc_core.c     | 2 +-
 drivers/remoteproc/remoteproc_internal.h | 2 +-
 drivers/remoteproc/st_slim_rproc.c       | 4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       | 2 +-
 include/linux/remoteproc.h               | 2 +-
 12 files changed, 17 insertions(+), 16 deletions(-)
drivers/remoteproc/imx_rproc.c +5 -4
index 3e72b6f38d4b..1e895d5cf918 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-			       int len, u64 *sys)
+			       u64 len, u64 *sys)
 {
 	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 	int i;
@@ -203,12 +203,12 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
 		}
 	}
 
-	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
 		 da, len);
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+		da, len, va);
 
 	return va;
 }
drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..25c01df47eba 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
 	void __iomem *va = NULL;
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +1 -1
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
 		da, len, va);
 
 	return va;
drivers/remoteproc/wkup_m3_rproc.c +1 -1
index 3984e585c847..0e8082948489 100644
@@ -80,7 +80,7 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
Signature · 1 line
2.15.0.276.g89ea799
maintainer
#24 lore raw

Re: [PATCH v3 1/2] remoteproc: Use u64 len for da_to_va

Hi Clement,

Quoted 40 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 9 +++++----
 drivers/remoteproc/keystone_remoteproc.c | 2 +-
 drivers/remoteproc/qcom_q6v5_adsp.c      | 2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       | 2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      | 2 +-
 drivers/remoteproc/qcom_wcnss.c          | 2 +-
 drivers/remoteproc/remoteproc_core.c     | 2 +-
 drivers/remoteproc/remoteproc_internal.h | 2 +-
 drivers/remoteproc/st_slim_rproc.c       | 4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       | 2 +-
 include/linux/remoteproc.h               | 2 +-
 12 files changed, 17 insertions(+), 16 deletions(-)
drivers/remoteproc/imx_rproc.c +1 -2
index 3e72b6f38d4b..1e895d5cf918 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-			       int len, u64 *sys)
+			       u64 len, u64 *sys)
 {
 	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 	int i;
@@ -203,12 +203,12 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
 		}
 	}
 
-	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",

drivers/remoteproc/imx_rproc.c static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da, · line 214

Quoted 10 lines
drivers/remoteproc/imx_rproc.c +2 -1
+	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
 		 da, len);
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;

The condition "if (len <= 0)" at the beginning of the function needs to be fixed.

Quoted 15 lines
drivers/remoteproc/imx_rproc.c +2 -1
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+		da, len, va);
 
 	return va;
 }
drivers/remoteproc/keystone_remoteproc.c +0 -0
index 5c4658f00b3d..25c01df47eba 100644

drivers/remoteproc/keystone_remoteproc.c static void keystone_rproc_kick(struct rproc *rproc, int vqid) · line 250

Quoted 6 lines
drivers/remoteproc/keystone_remoteproc.c +1 -1
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {

Same comment as above.

Quoted 120 lines
drivers/remoteproc/keystone_remoteproc.c +0 -0
 	struct keystone_rproc *ksproc = rproc->priv;
 	void __iomem *va = NULL;
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +1 -1
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
 		da, len, va);
 
 	return va;
drivers/remoteproc/wkup_m3_rproc.c +0 -0
index 3984e585c847..0e8082948489 100644

drivers/remoteproc/wkup_m3_rproc.c static int wkup_m3_rproc_stop(struct rproc *rproc) · line 84

Quoted 6 lines
drivers/remoteproc/wkup_m3_rproc.c +1 -1
@@ -80,7 +80,7 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {

Same comment as above.

Quoted 18 lines
drivers/remoteproc/wkup_m3_rproc.c +0 -0
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
Signature · 1 line
2.15.0.276.g89ea799
#25 lore raw

PATCH v3 2/2 remoteproc: Add elf64 support in elf loader

In reply to Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accommodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 147 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 ++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 167 insertions(+), 59 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 03c3d2e568b0..2be1147256e0 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +93 -54

266 lines not on this page · raw message

drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0

69 lines not on this page · raw message

drivers/remoteproc/remoteproc_internal.h +1 -1
index 004867061721..eeb26434220e 100644
@@ -55,7 +55,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index ee13d23b43a9..a3268d95a50e 100644
@@ -190,7 +190,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +2 -2
index f84bd5fe0211..82cebca9344c 100644
@@ -382,7 +382,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -498,7 +498,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
Signature · 1 line
2.15.0.276.g89ea799
maintainer
#26 lore raw

Re: [PATCH v3 2/2] remoteproc: Add elf64 support in elf loader

Quoted 427 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accommodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 147 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 ++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 167 insertions(+), 59 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 03c3d2e568b0..2be1147256e0 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +93 -54
index 606aae166eba..21fd2b2fe5ae 100644
@@ -23,6 +23,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -35,8 +36,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
 		dev_err(dev, "failed to load %s\n", name);
@@ -50,13 +59,22 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
 
+	if (class == ELFCLASS64 && fw->size < sizeof(struct elf64_hdr)) {
+		dev_err(dev, "elf64 header is too small\n");
+		return -EINVAL;
+	}
+
 	/* We assume the firmware has the same endianness as the host */
 # ifdef __LITTLE_ENDIAN
 	if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
@@ -67,26 +85,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }
 EXPORT_SYMBOL(rproc_elf_sanity_check);
@@ -102,11 +123,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -137,37 +156,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -176,14 +199,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
 		 * Zero out remaining memory for this segment.
@@ -200,24 +224,35 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
-
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
-
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+	/* First, get the section header according to the elf class */
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	/* Compute name table section header entry in shdr array */
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	/* Finally, compute the name table section address in elf */
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
+
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
+
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
 		table = (struct resource_table *)(elf_data + offset);
@@ -270,21 +305,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
 	 * Create a copy of the resource table. When a virtio device starts
@@ -317,13 +352,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 004867061721..eeb26434220e 100644
@@ -55,7 +55,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index ee13d23b43a9..a3268d95a50e 100644
@@ -190,7 +190,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index f84bd5fe0211..82cebca9344c 100644
@@ -382,7 +382,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**

include/linux/remoteproc.h struct rproc { · line 501

Quoted 5 lines
include/linux/remoteproc.h +1 -1
@@ -498,7 +498,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;

That will cause problems for rproc_coredump()[1] and fixing it properly likely means that a 32 or 64 elf should be generated based on the type of image that was loaded. This is also true if ->p_vaddr and ->p_paddr (also in the same function) are to be handled properly.

I'm interested in your opinion on this.

Thanks,
Mathieu

[1]. https://elixir.bootlin.com/linux/latest/source/drivers/remoteproc/remoteproc_core.c#L1600

Quoted 6 lines
include/linux/remoteproc.h +0 -0
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
Signature · 1 line
2.15.0.276.g89ea799
#27 lore raw

Re: [PATCH v3 2/2] remoteproc: Add elf64 support in elf loader

Hi Mathieu,

Quoted 449 lines · ----- On 5 Feb, 2020, at 23:49, Mathieu Poirier [email protected]…
Quoted 437 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accommodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 147 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 ++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 167 insertions(+), 59 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 03c3d2e568b0..2be1147256e0 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index 606aae166eba..21fd2b2fe5ae 100644
@@ -23,6 +23,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -35,8 +36,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
drivers/remoteproc/remoteproc_elf_loader.c +11 -2
 		dev_err(dev, "failed to load %s\n", name);
@@ -50,13 +59,22 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
 
+	if (class == ELFCLASS64 && fw->size < sizeof(struct elf64_hdr)) {
+		dev_err(dev, "elf64 header is too small\n");
+		return -EINVAL;
+	}
+
 	/* We assume the firmware has the same endianness as the host */
 # ifdef __LITTLE_ENDIAN
drivers/remoteproc/remoteproc_elf_loader.c +11 -8
 	if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
@@ -67,26 +85,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -102,11 +123,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -137,37 +156,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -176,14 +199,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
drivers/remoteproc/remoteproc_elf_loader.c +23 -12
 		 * Zero out remaining memory for this segment.
@@ -200,24 +224,35 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
-
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
-
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+	/* First, get the section header according to the elf class */
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	/* Compute name table section header entry in shdr array */
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	/* Finally, compute the name table section address in elf */
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
+
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
+
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
drivers/remoteproc/remoteproc_elf_loader.c +7 -7
 		table = (struct resource_table *)(elf_data + offset);
@@ -270,21 +305,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
drivers/remoteproc/remoteproc_elf_loader.c +8 -4
 	 * Create a copy of the resource table. When a virtio device starts
@@ -317,13 +352,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 004867061721..eeb26434220e 100644
@@ -55,7 +55,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index ee13d23b43a9..a3268d95a50e 100644
@@ -190,7 +190,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index f84bd5fe0211..82cebca9344c 100644
@@ -382,7 +382,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**

include/linux/remoteproc.h struct rproc { · line 501

Quoted 5 lines
include/linux/remoteproc.h +1 -1
@@ -498,7 +498,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;

That will cause problems for rproc_coredump()[1] and fixing it properly likely means that a 32 or 64 elf should be generated based on the type of image that was loaded. This is also true if ->p_vaddr and ->p_paddr (also in the same function) are to be handled properly.

Quoted 1 line

I'm interested in your opinion on this.

Indeed, you are right !

I'm "afraid" I will have to fix that !
Most sane thing to do is to dump an elf with the input elf class. I will make a V4 with a patch for that.

Thanks,

Clément

Quoted 12 lines

Thanks,
Mathieu

[1].
https://elixir.bootlin.com/linux/latest/source/drivers/remoteproc/remoteproc_core.c#L1600

Quoted 5 lines
include/linux/remoteproc.h +0 -1
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
--

2.15.0.276.g89ea799

#28 lore raw

Re: [PATCH v3 2/2] remoteproc: Add elf64 support in elf loader

Hi Mathieu,

Quoted 454 lines · ----- On 6 Feb, 2020, at 09:37, Clément Leger [email protected] wrote:

Hi Mathieu,

----- On 5 Feb, 2020, at 23:49, Mathieu Poirier [email protected]

Quoted 449 lines · wrote:
Quoted 437 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accommodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 147 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 ++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 167 insertions(+), 59 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 03c3d2e568b0..2be1147256e0 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index 606aae166eba..21fd2b2fe5ae 100644
@@ -23,6 +23,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -35,8 +36,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
drivers/remoteproc/remoteproc_elf_loader.c +11 -2
 		dev_err(dev, "failed to load %s\n", name);
@@ -50,13 +59,22 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
 
+	if (class == ELFCLASS64 && fw->size < sizeof(struct elf64_hdr)) {
+		dev_err(dev, "elf64 header is too small\n");
+		return -EINVAL;
+	}
+
 	/* We assume the firmware has the same endianness as the host */
 # ifdef __LITTLE_ENDIAN
drivers/remoteproc/remoteproc_elf_loader.c +11 -8
 	if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
@@ -67,26 +85,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -102,11 +123,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -137,37 +156,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -176,14 +199,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
drivers/remoteproc/remoteproc_elf_loader.c +23 -12
 		 * Zero out remaining memory for this segment.
@@ -200,24 +224,35 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
-
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
-
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+	/* First, get the section header according to the elf class */
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	/* Compute name table section header entry in shdr array */
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	/* Finally, compute the name table section address in elf */
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
+
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
+
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
drivers/remoteproc/remoteproc_elf_loader.c +7 -7
 		table = (struct resource_table *)(elf_data + offset);
@@ -270,21 +305,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
drivers/remoteproc/remoteproc_elf_loader.c +8 -4
 	 * Create a copy of the resource table. When a virtio device starts
@@ -317,13 +352,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 004867061721..eeb26434220e 100644
@@ -55,7 +55,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index ee13d23b43a9..a3268d95a50e 100644
@@ -190,7 +190,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index f84bd5fe0211..82cebca9344c 100644
@@ -382,7 +382,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**

include/linux/remoteproc.h struct rproc { · line 501

Quoted 5 lines
include/linux/remoteproc.h +1 -1
@@ -498,7 +498,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;

That will cause problems for rproc_coredump()[1] and fixing it properly likely means that a 32 or 64 elf should be generated based on the type of image that was loaded. This is also true if ->p_vaddr and ->p_paddr (also in the same function) are to be handled properly.

Quoted 8 lines
Quoted 1 line

I'm interested in your opinion on this.

Indeed, you are right !

I'm "afraid" I will have to fix that !
Most sane thing to do is to dump an elf with the input elf class. I will make a V4 with a patch for that.

Actually, this does not seems directly related to elf loading. The coredump mecanism dumps segment that have been registered using rproc_coredump_add_*_segment and this is not done directly by the elf loader.
I'm not 100% sure but the coredump elf format is not tied to any machine (EM_NONE) and as a special type (ET_CORE) so the elf type is probably not relevant adn elf64 would be generic. If some coredump user could speak for that part, that would be nice !

If needed, then we could add a rproc_coredump_set_format() function to specify various parameters (machine, elf type, lsb/msb, etc).

Let me know what you think about it.

Clément

Quoted 16 lines

Thanks,

Clément

Quoted 12 lines

Thanks,
Mathieu

[1].
https://elixir.bootlin.com/linux/latest/source/drivers/remoteproc/remoteproc_core.c#L1600

Quoted 5 lines
include/linux/remoteproc.h +0 -1
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
--

2.15.0.276.g89ea799

maintainer
#29 lore raw

Re: [PATCH v3 2/2] remoteproc: Add elf64 support in elf loader

Quoted 467 lines · Cl�ment Leger

Hi Mathieu,

Quoted 454 lines · ----- On 6 Feb, 2020, at 09:37, Cl�ment Leger [email protected] wrote:

Hi Mathieu,

----- On 5 Feb, 2020, at 23:49, Mathieu Poirier [email protected]

Quoted 449 lines · wrote:
Quoted 437 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accommodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 147 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 ++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 167 insertions(+), 59 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 03c3d2e568b0..2be1147256e0 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index 606aae166eba..21fd2b2fe5ae 100644
@@ -23,6 +23,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -35,8 +36,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
drivers/remoteproc/remoteproc_elf_loader.c +11 -2
 		dev_err(dev, "failed to load %s\n", name);
@@ -50,13 +59,22 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
 
+	if (class == ELFCLASS64 && fw->size < sizeof(struct elf64_hdr)) {
+		dev_err(dev, "elf64 header is too small\n");
+		return -EINVAL;
+	}
+
 	/* We assume the firmware has the same endianness as the host */
 # ifdef __LITTLE_ENDIAN
drivers/remoteproc/remoteproc_elf_loader.c +11 -8
 	if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
@@ -67,26 +85,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -102,11 +123,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -137,37 +156,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -176,14 +199,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
drivers/remoteproc/remoteproc_elf_loader.c +23 -12
 		 * Zero out remaining memory for this segment.
@@ -200,24 +224,35 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
-
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
-
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+	/* First, get the section header according to the elf class */
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	/* Compute name table section header entry in shdr array */
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	/* Finally, compute the name table section address in elf */
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
+
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
+
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
drivers/remoteproc/remoteproc_elf_loader.c +7 -7
 		table = (struct resource_table *)(elf_data + offset);
@@ -270,21 +305,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
drivers/remoteproc/remoteproc_elf_loader.c +8 -4
 	 * Create a copy of the resource table. When a virtio device starts
@@ -317,13 +352,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 004867061721..eeb26434220e 100644
@@ -55,7 +55,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index ee13d23b43a9..a3268d95a50e 100644
@@ -190,7 +190,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index f84bd5fe0211..82cebca9344c 100644
@@ -382,7 +382,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**

include/linux/remoteproc.h struct rproc { · line 501

Quoted 5 lines
include/linux/remoteproc.h +1 -1
@@ -498,7 +498,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;

That will cause problems for rproc_coredump()[1] and fixing it properly likely means that a 32 or 64 elf should be generated based on the type of image that was loaded. This is also true if ->p_vaddr and ->p_paddr (also in the same function) are to be handled properly.

Quoted 8 lines
Quoted 1 line

I'm interested in your opinion on this.

Indeed, you are right !

I'm "afraid" I will have to fix that !
Most sane thing to do is to dump an elf with the input elf class. I will make a V4 with a patch for that.

Quoted 4 lines

Actually, this does not seems directly related to elf loading. The coredump mecanism dumps segment that have been registered using rproc_coredump_add_*_segment and this is not done directly by the elf loader.

Correct, but it is fair to assume rpoc_coredump_add_*_segment() is called for the image that will run on the MCU (otherwiser doing so would be pointless).

Quoted 3 lines

I'm not 100% sure but the coredump elf format is not tied to any machine (EM_NONE) and as a special type (ET_CORE) so the elf type is probably not relevant adn elf64 would be generic.

That is true, we could generate an elf64 core dump for both 32 and 64 bit MCU image and be done with it. But that is almost guaranteed to break some user space that isn't tailored to deal with an elf64.

Quoted 6 lines

If some coredump user could speak for that part, that would be nice !

If needed, then we could add a rproc_coredump_set_format() function to specify various parameters (machine, elf type, lsb/msb, etc).

For this specific patchset I don't think you need to cover cases where this much flexibility is needed (up to now it hasn't been a requiment).

Keeping track of the type of image being loaded in something like rproc::elfclass and use that information to generate the right elf core in rproc_coredump() is probably sufficient for the time being.

Quoted 20 lines

Let me know what you think about it.

Cl�ment

Quoted 16 lines

Thanks,

Cl�ment

Quoted 12 lines

Thanks,
Mathieu

[1].
https://elixir.bootlin.com/linux/latest/source/drivers/remoteproc/remoteproc_core.c#L1600

Quoted 5 lines
include/linux/remoteproc.h +0 -1
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
--

2.15.0.276.g89ea799

#30 lore raw

Re: [PATCH v3 2/2] remoteproc: Add elf64 support in elf loader

Quoted 491 lines · ----- On 6 Feb, 2020, at 18:48, Mathieu Poirier [email protected]…
Quoted 467 lines · Clément Leger

Hi Mathieu,

Quoted 454 lines · ----- On 6 Feb, 2020, at 09:37, Clément Leger [email protected] wrote:

Hi Mathieu,

----- On 5 Feb, 2020, at 23:49, Mathieu Poirier [email protected]

Quoted 449 lines · wrote:
Quoted 437 lines · Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accommodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of both elf class.

---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 147 ++++++++++++++++++-----------
 drivers/remoteproc/remoteproc_elf_loader.h |  69 ++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   2 +-
 drivers/remoteproc/st_remoteproc.c         |   2 +-
 include/linux/remoteproc.h                 |   4 +-
 6 files changed, 167 insertions(+), 59 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_loader.h
Documentation/remoteproc.txt +1 -1
index 03c3d2e568b0..2be1147256e0 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +9 -0
index 606aae166eba..21fd2b2fe5ae 100644
@@ -23,6 +23,7 @@
 #include <linux/elf.h>
 
 #include "remoteproc_internal.h"
+#include "remoteproc_elf_loader.h"
 
 /**
  * rproc_elf_sanity_check() - Sanity Check ELF firmware image
@@ -35,8 +36,16 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 {
 	const char *name = rproc->firmware;
 	struct device *dev = &rproc->dev;
+	/*
+	 * Elf files are beginning with the same structure. Thus, to simplify
+	 * header parsing, we can use the elf32_hdr one for both elf64 and
+	 * elf32.
+	 */
 	struct elf32_hdr *ehdr;
+	u32 elf_shdr_size;
+	u64 phoff, shoff;
 	char class;
+	u16 phnum;
 
 	if (!fw) {
drivers/remoteproc/remoteproc_elf_loader.c +11 -2
 		dev_err(dev, "failed to load %s\n", name);
@@ -50,13 +59,22 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 
 	ehdr = (struct elf32_hdr *)fw->data;
 
-	/* We only support ELF32 at this point */
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+		dev_err(dev, "Image is corrupted (bad magic)\n");
+		return -EINVAL;
+	}
+
 	class = ehdr->e_ident[EI_CLASS];
-	if (class != ELFCLASS32) {
+	if (class != ELFCLASS32 && class != ELFCLASS64) {
 		dev_err(dev, "Unsupported class: %d\n", class);
 		return -EINVAL;
 	}
 
+	if (class == ELFCLASS64 && fw->size < sizeof(struct elf64_hdr)) {
+		dev_err(dev, "elf64 header is too small\n");
+		return -EINVAL;
+	}
+
 	/* We assume the firmware has the same endianness as the host */
 # ifdef __LITTLE_ENDIAN
drivers/remoteproc/remoteproc_elf_loader.c +11 -8
 	if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
@@ -67,26 +85,29 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct
 firmware *fw)
 		return -EINVAL;
 	}
 
-	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
-		dev_err(dev, "Image is too small\n");
-		return -EINVAL;
-	}
+	phoff = elf_hdr_e_phoff(class, fw->data);
+	shoff = elf_hdr_e_shoff(class, fw->data);
+	phnum =  elf_hdr_e_phnum(class, fw->data);
+	elf_shdr_size = elf_size_of_shdr(class);
 
-	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
-		dev_err(dev, "Image is corrupted (bad magic)\n");
+	if (fw->size < shoff + elf_shdr_size) {
+		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phnum == 0) {
+	if (phnum == 0) {
 		dev_err(dev, "No loadable segments\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_phoff > fw->size) {
+	if (phoff > fw->size) {
 		dev_err(dev, "Firmware size is too small\n");
 		return -EINVAL;
 	}
 
+	dev_dbg(dev, "Firmware is an elf%d file\n",
+		class == ELFCLASS32 ? 32 : 64);
+
 	return 0;
 }

EXPORT_SYMBOL(rproc_elf_sanity_check);

drivers/remoteproc/remoteproc_elf_loader.c +24 -21
@@ -102,11 +123,9 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
-
-	return ehdr->e_entry;
+	return elf_hdr_e_entry(fw_elf_get_class(fw), fw->data);
 }
 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 
@@ -137,37 +156,41 @@ EXPORT_SYMBOL(rproc_elf_get_boot_addr);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 {
 	struct device *dev = &rproc->dev;
-	struct elf32_hdr *ehdr;
-	struct elf32_phdr *phdr;
+	const void *ehdr, *phdr;
 	int i, ret = 0;
+	u16 phnum;
 	const u8 *elf_data = fw->data;
+	u8 class = fw_elf_get_class(fw);
+	u32 elf_phdr_size = elf_size_of_phdr(class);
 
-	ehdr = (struct elf32_hdr *)elf_data;
-	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+	ehdr = elf_data;
+	phnum = elf_hdr_e_phnum(class, ehdr);
+	phdr = elf_data + elf_hdr_e_phoff(class, ehdr);
 
 	/* go through the available ELF segments */
-	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
-		u32 da = phdr->p_paddr;
-		u32 memsz = phdr->p_memsz;
-		u32 filesz = phdr->p_filesz;
-		u32 offset = phdr->p_offset;
+	for (i = 0; i < phnum; i++, phdr += elf_phdr_size) {
+		u64 da = elf_phdr_p_paddr(class, phdr);
+		u64 memsz = elf_phdr_p_memsz(class, phdr);
+		u64 filesz = elf_phdr_p_filesz(class, phdr);
+		u64 offset = elf_phdr_p_offset(class, phdr);
+		u32 type = elf_phdr_p_type(class, phdr);
 		void *ptr;
 
-		if (phdr->p_type != PT_LOAD)
+		if (type != PT_LOAD)
 			continue;
 
-		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
-			phdr->p_type, da, memsz, filesz);
+		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
+			type, da, memsz, filesz);
 
 		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
 				filesz, memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
 				offset + filesz, fw->size);
 			ret = -EINVAL;
 			break;
@@ -176,14 +199,15 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 		/* grab the kernel address for this device address */
 		ptr = rproc_da_to_va(rproc, da, memsz);
 		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
+			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+				memsz);
 			ret = -EINVAL;
 			break;
 		}
 
 		/* put the segment where the remote processor expects it */
-		if (phdr->p_filesz)
-			memcpy(ptr, elf_data + phdr->p_offset, filesz);
+		if (filesz)
+			memcpy(ptr, elf_data + offset, filesz);
 
 		/*
drivers/remoteproc/remoteproc_elf_loader.c +23 -12
 		 * Zero out remaining memory for this segment.
@@ -200,24 +224,35 @@ int rproc_elf_load_segments(struct rproc *rproc, const
 struct firmware *fw)
 }
 EXPORT_SYMBOL(rproc_elf_load_segments);
 
-static struct elf32_shdr *
-find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
+static const void *
+find_table(struct device *dev, const struct firmware *fw)
 {
-	struct elf32_shdr *shdr;
+	const void *shdr, *name_table_shdr;
 	int i;
 	const char *name_table;
 	struct resource_table *table = NULL;
-	const u8 *elf_data = (void *)ehdr;
+	const u8 *elf_data = (void *)fw->data;
+	u8 class = fw_elf_get_class(fw);
+	size_t fw_size = fw->size;
+	const void *ehdr = elf_data;
+	u16 shnum = elf_hdr_e_shnum(class, ehdr);
+	u32 elf_shdr_size = elf_size_of_shdr(class);
+	u16 shstrndx = elf_hdr_e_shstrndx(class, ehdr);
 
 	/* look for the resource table and handle it */
-	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
-	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
-
-	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
-		u32 size = shdr->sh_size;
-		u32 offset = shdr->sh_offset;
-
-		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+	/* First, get the section header according to the elf class */
+	shdr = elf_data + elf_hdr_e_shoff(class, ehdr);
+	/* Compute name table section header entry in shdr array */
+	name_table_shdr = shdr + (shstrndx * elf_shdr_size);
+	/* Finally, compute the name table section address in elf */
+	name_table = elf_data + elf_shdr_sh_offset(class, name_table_shdr);
+
+	for (i = 0; i < shnum; i++, shdr += elf_shdr_size) {
+		u64 size = elf_shdr_sh_size(class, shdr);
+		u64 offset = elf_shdr_sh_offset(class, shdr);
+		u32 name = elf_shdr_sh_name(class, shdr);
+
+		if (strcmp(name_table + name, ".resource_table"))
 			continue;
 
drivers/remoteproc/remoteproc_elf_loader.c +7 -7
 		table = (struct resource_table *)(elf_data + offset);
@@ -270,21 +305,21 @@ find_table(struct device *dev, struct elf32_hdr *ehdr,
 size_t fw_size)
  */
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr;
-	struct elf32_shdr *shdr;
+	const void *shdr;
 	struct device *dev = &rproc->dev;
 	struct resource_table *table = NULL;
 	const u8 *elf_data = fw->data;
 	size_t tablesz;
+	u8 class = fw_elf_get_class(fw);
+	u64 sh_offset;
 
-	ehdr = (struct elf32_hdr *)elf_data;
-
-	shdr = find_table(dev, ehdr, fw->size);
+	shdr = find_table(dev, fw);
 	if (!shdr)
 		return -EINVAL;
 
-	table = (struct resource_table *)(elf_data + shdr->sh_offset);
-	tablesz = shdr->sh_size;
+	sh_offset = elf_shdr_sh_offset(class, shdr);
+	table = (struct resource_table *)(elf_data + sh_offset);
+	tablesz = elf_shdr_sh_size(class, shdr);
 
 	/*
drivers/remoteproc/remoteproc_elf_loader.c +8 -4
 	 * Create a copy of the resource table. When a virtio device starts
@@ -317,13 +352,17 @@ EXPORT_SYMBOL(rproc_elf_load_rsc_table);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 						       const struct firmware *fw)
 {
-	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
-	struct elf32_shdr *shdr;
+	const void *shdr;
+	u64 sh_addr, sh_size;
+	u8 class = fw_elf_get_class(fw);
 
-	shdr = find_table(&rproc->dev, ehdr, fw->size);
+	shdr = find_table(&rproc->dev, fw);
 	if (!shdr)
 		return NULL;
 
-	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
+	sh_addr = elf_shdr_sh_addr(class, shdr);
+	sh_size = elf_shdr_sh_size(class, shdr);
+
+	return rproc_da_to_va(rproc, sh_addr, sh_size);
 }
 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
drivers/remoteproc/remoteproc_elf_loader.h new file +69 -0
new file mode 100644
index 000000000000..fac3565734f9
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf loader defines
+ *
+ * Copyright (C) 2019 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_shnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_shoff, u64)
+ELF_GET_FIELD(hdr, e_shstrndx, u16)
+
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+ELF_GET_FIELD(shdr, sh_size, u64)
+ELF_GET_FIELD(shdr, sh_offset, u64)
+ELF_GET_FIELD(shdr, sh_name, u32)
+ELF_GET_FIELD(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
drivers/remoteproc/remoteproc_internal.h +1 -1
index 004867061721..eeb26434220e 100644
@@ -55,7 +55,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index ee13d23b43a9..a3268d95a50e 100644
@@ -190,7 +190,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +1 -1
index f84bd5fe0211..82cebca9344c 100644
@@ -382,7 +382,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**

include/linux/remoteproc.h struct rproc { · line 501

Quoted 5 lines
include/linux/remoteproc.h +1 -1
@@ -498,7 +498,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;

That will cause problems for rproc_coredump()[1] and fixing it properly likely means that a 32 or 64 elf should be generated based on the type of image that was loaded. This is also true if ->p_vaddr and ->p_paddr (also in the same function) are to be handled properly.

Quoted 8 lines
Quoted 1 line

I'm interested in your opinion on this.

Indeed, you are right !

I'm "afraid" I will have to fix that !
Most sane thing to do is to dump an elf with the input elf class. I will make a V4 with a patch for that.

Quoted 4 lines

Actually, this does not seems directly related to elf loading. The coredump mecanism dumps segment that have been registered using rproc_coredump_add_*_segment and this is not done directly by the elf loader.

Correct, but it is fair to assume rpoc_coredump_add_*_segment() is called for the image that will run on the MCU (otherwiser doing so would be pointless).

Quoted 3 lines

I'm not 100% sure but the coredump elf format is not tied to any machine (EM_NONE) and as a special type (ET_CORE) so the elf type is probably not relevant adn elf64 would be generic.

That is true, we could generate an elf64 core dump for both 32 and 64 bit MCU image and be done with it. But that is almost guaranteed to break some user space that isn't tailored to deal with an elf64.

Quoted 6 lines

If some coredump user could speak for that part, that would be nice !

If needed, then we could add a rproc_coredump_set_format() function to specify various parameters (machine, elf type, lsb/msb, etc).

Quoted 6 lines

For this specific patchset I don't think you need to cover cases where this much flexibility is needed (up to now it hasn't been a requiment).

Keeping track of the type of image being loaded in something like rproc::elfclass and use that information to generate the right elf core in rproc_coredump() is probably sufficient for the time being.

Ok, let's go this way.

Quoted 21 lines
Quoted 21 lines

Let me know what you think about it.

Clément

Quoted 16 lines

Thanks,

Clément

Quoted 12 lines

Thanks,
Mathieu

[1].
https://elixir.bootlin.com/linux/latest/source/drivers/remoteproc/remoteproc_core.c#L1600

Quoted 5 lines
include/linux/remoteproc.h +0 -1
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
--

2.15.0.276.g89ea799

#31 lore raw

v4 cover remoteproc: Add elf64 support

This serie add support for elf64 in remoteproc (elf loader, coredump). First two patches modifies the type of len argument (in da_to_va) and boot_addr in order to allow loading elf64 segment with a u64 size and a u64 entry point.
Next patch introduce a set of macros to access elf64 and elf32 transparently.
Last two patches are the actual modification in the elf loader and remoteproc coredump support to add elf64 support.

Changes from V3:
- Adapt coredump to elf64 file format
- Rename remoteproc_elf_loader.h to remoteproc_elf_helpers.h
- Update copyright year in remoteproc_elf_helpers.h
- Rename macros elf_hdr_* to elf_get_hdr_* for coherency with elf_hdr_set_*
- Split elf64 loader patch in 3:
- boot_addr u64 change
- remoteproc_elf_helpers.h creation
- elf64 loading

Clement Leger (5):
remoteproc: Use u64 len for da_to_va
remoteproc: Use u64 type for boot_addr
remoteproc: Add elf helpers to access elf64 and elf32 fields
remoteproc: Add elf64 support in elf loader
remoteproc: Adapt coredump to generate correct elf type

 Documentation/remoteproc.txt                |   2 +-
 drivers/remoteproc/imx_rproc.c              |  11 +-
 drivers/remoteproc/keystone_remoteproc.c    |   4 +-
 drivers/remoteproc/qcom_q6v5_adsp.c         |   2 +-
 drivers/remoteproc/qcom_q6v5_mss.c          |   2 +-
 drivers/remoteproc/qcom_q6v5_pas.c          |   2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c         |   2 +-
 drivers/remoteproc/qcom_wcnss.c             |   2 +-
 drivers/remoteproc/remoteproc_core.c        |  69 +++++++------
 drivers/remoteproc/remoteproc_elf_helpers.h |  95 ++++++++++++++++++
 drivers/remoteproc/remoteproc_elf_loader.c  | 150 ++++++++++++++++++----------
 drivers/remoteproc/remoteproc_internal.h    |   4 +-
 drivers/remoteproc/st_remoteproc.c          |   2 +-
 drivers/remoteproc/st_slim_rproc.c          |   4 +-
 drivers/remoteproc/wkup_m3_rproc.c          |   4 +-
 include/linux/remoteproc.h                  |   7 +-
 16 files changed, 252 insertions(+), 110 deletions(-)
 create mode 100644 drivers/remoteproc/remoteproc_elf_helpers.h
Signature · 1 line
2.15.0.276.g89ea799
#32 lore raw

PATCH v4 1/5 remoteproc: Use u64 len for da_to_va

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 11 ++++++-----
 drivers/remoteproc/keystone_remoteproc.c |  4 ++--
 drivers/remoteproc/qcom_q6v5_adsp.c      |  2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      |  2 +-
 drivers/remoteproc/qcom_wcnss.c          |  2 +-
 drivers/remoteproc/remoteproc_core.c     |  2 +-
 drivers/remoteproc/remoteproc_internal.h |  2 +-
 drivers/remoteproc/st_slim_rproc.c       |  4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       |  4 ++--
 include/linux/remoteproc.h               |  2 +-
 12 files changed, 20 insertions(+), 19 deletions(-)
drivers/remoteproc/imx_rproc.c +6 -5
index 3e72b6f38d4b..f497f5b49b18 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-			       int len, u64 *sys)
+			       u64 len, u64 *sys)
 {
 	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 	int i;
@@ -203,19 +203,19 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
 		}
 	}
 
-	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
 		 da, len);
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;
 	u64 sys;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	/*
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+		da, len, va);
 
 	return va;
 }
drivers/remoteproc/keystone_remoteproc.c +2 -2
index 5c4658f00b3d..466093f48814 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
 	void __iomem *va = NULL;
@@ -255,7 +255,7 @@ static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 	size_t size;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	for (i = 0; i < ksproc->num_mems; i++) {
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +1 -1
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
 		da, len, va);
 
 	return va;
drivers/remoteproc/wkup_m3_rproc.c +2 -2
index 3984e585c847..91485b467407 100644
@@ -80,14 +80,14 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
 	int i;
 	u32 offset;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	for (i = 0; i < WKUPM3_MEM_MAX; i++) {
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
Signature · 1 line
2.15.0.276.g89ea799
#33 lore raw

Re: [PATCH v4 1/5] remoteproc: Use u64 len for da_to_va

Quoted 160 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 11 ++++++-----
 drivers/remoteproc/keystone_remoteproc.c |  4 ++--
 drivers/remoteproc/qcom_q6v5_adsp.c      |  2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      |  2 +-
 drivers/remoteproc/qcom_wcnss.c          |  2 +-
 drivers/remoteproc/remoteproc_core.c     |  2 +-
 drivers/remoteproc/remoteproc_internal.h |  2 +-
 drivers/remoteproc/st_slim_rproc.c       |  4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       |  4 ++--
 include/linux/remoteproc.h               |  2 +-
 12 files changed, 20 insertions(+), 19 deletions(-)
drivers/remoteproc/imx_rproc.c +6 -5
index 3e72b6f38d4b..f497f5b49b18 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-			       int len, u64 *sys)
+			       u64 len, u64 *sys)
 {
 	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 	int i;
@@ -203,19 +203,19 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
 		}
 	}
 
-	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
 		 da, len);
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;
 	u64 sys;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	/*
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+		da, len, va);
 
 	return va;
 }
drivers/remoteproc/keystone_remoteproc.c +2 -2
index 5c4658f00b3d..466093f48814 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
 	void __iomem *va = NULL;
@@ -255,7 +255,7 @@ static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 	size_t size;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	for (i = 0; i < ksproc->num_mems; i++) {
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +0 -0
index 307df98347ba..9e6d3c6a60ee 100644

drivers/remoteproc/remoteproc_core.c EXPORT_SYMBOL(rproc_va_to_pa); · line 188

Quoted 5 lines
drivers/remoteproc/remoteproc_core.c +1 -1
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

This function is exported, don't see any update in consequence...
references:
https://elixir.bootlin.com/linux/v5.6-rc1/ident/rproc_da_to_va
For instance the function rproc_trace_read use it. it quite strange that my gcc does not warns for the cast but i suppose that some could. An indirect consequence is that the len field in rproc_mem_entry struct should probably been updated to u64 to be aligned.

I'm still wondering about the use of size_t instead,which seems more rational from my window. So i you or Mathieu remember it was decided to use u64, please could remind me the arguments? As an alternative a check should be added for 32 bits processors to ensure that the size is not higher than its address range capability...

Regards
Arnaud

Quoted 73 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
 		da, len, va);
 
 	return va;
drivers/remoteproc/wkup_m3_rproc.c +2 -2
index 3984e585c847..91485b467407 100644
@@ -80,14 +80,14 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
 	int i;
 	u32 offset;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	for (i = 0; i < WKUPM3_MEM_MAX; i++) {
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
#34 lore raw

Re: [PATCH v4 1/5] remoteproc: Use u64 len for da_to_va

Hi Arnaud,

Quoted 174 lines · ----- On 11 Feb, 2020, at 16:53, Arnaud Pouliquen [email protected] wrote:
Quoted 173 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 11 ++++++-----
 drivers/remoteproc/keystone_remoteproc.c |  4 ++--
 drivers/remoteproc/qcom_q6v5_adsp.c      |  2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      |  2 +-
 drivers/remoteproc/qcom_wcnss.c          |  2 +-
 drivers/remoteproc/remoteproc_core.c     |  2 +-
 drivers/remoteproc/remoteproc_internal.h |  2 +-
 drivers/remoteproc/st_slim_rproc.c       |  4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       |  4 ++--
 include/linux/remoteproc.h               |  2 +-
 12 files changed, 20 insertions(+), 19 deletions(-)
drivers/remoteproc/imx_rproc.c +4 -4
index 3e72b6f38d4b..f497f5b49b18 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-			       int len, u64 *sys)
+			       u64 len, u64 *sys)
 {
 	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 	int i;
@@ -203,19 +203,19 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64
 da,
 		}
 	}
 
-	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
 		 da, len);
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;
 	u64 sys;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
drivers/remoteproc/imx_rproc.c +2 -1
 	/*
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da,
 int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+		da, len, va);
 
 	return va;

}

drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..466093f48814 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int
 vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
drivers/remoteproc/keystone_remoteproc.c +1 -1
 	void __iomem *va = NULL;
@@ -255,7 +255,7 @@ static void *keystone_rproc_da_to_va(struct rproc *rproc,
 u64 da, int len)
 	size_t size;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
drivers/remoteproc/keystone_remoteproc.c +0 -0
 	for (i = 0; i < ksproc->num_mems; i++) {
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +0 -0
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
Quoted 8 lines

drivers/remoteproc/remoteproc_core.c EXPORT_SYMBOL(rproc_va_to_pa); · line 188

Quoted 2 lines
drivers/remoteproc/remoteproc_core.c +1 -1
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

This function is exported, don't see any update in consequence... references:
https://elixir.bootlin.com/linux/v5.6-rc1/ident/rproc_da_to_va
For instance the function rproc_trace_read use it. it quite strange that my gcc does not warns for the cast but i suppose that some could.

Agreed, even if len should never have been a signed type since it can't be negative. I will try to fix all callers.

Quoted 2 lines

An indirect consequence is that the len field in rproc_mem_entry struct should probably been updated to u64 to be aligned.

Ok, I will do that once we settle on the type of len.

Quoted 5 lines

I'm still wondering about the use of size_t instead,which seems more rational from my window.
So i you or Mathieu remember it was decided to use u64, please could remind me the arguments?

I tried to find the notes of a meeting we had for OpenAMP but I did not found them. Anyway, the argument was coming from Tomas or someone else, (I can't remember) talking about a 32 bits CPU executing code on a 64 bits accelerator. In that case, the size_t type could fail due to being only 32bits on the host CPU but larger than 4G.

However, I can't say if it's a real usecase or not... All I can say is that keeping it open is probably better if one day somebody comes with such architecture.

Quoted 3 lines

As an alternative a check should be added for 32 bits processors to ensure that the size is not higher than
its address range capability...

Agreed.
I was even thinking about a mecanism for remoteproc drivers to declare the type of supported elfs files (such as EM_*, ELFCLASS* and other needed thing). Or should it be supported by overriding .sanity_check in drivers to reject elf64 for instance ?

Since elf is a "specific format" and that rproc can support other formats, I did not want to add a specific elf_sanity_check field to rproc ops.

Regards,

Clément

Quoted 80 lines

Regards
Arnaud

Quoted 76 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64
 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
 		da, len, va);
 
drivers/remoteproc/st_slim_rproc.c +0 -0
 	return va;
drivers/remoteproc/wkup_m3_rproc.c +2 -2
index 3984e585c847..91485b467407 100644
@@ -80,14 +80,14 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
 	int i;
 	u32 offset;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	for (i = 0; i < WKUPM3_MEM_MAX; i++) {
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
#35 lore raw

Re: [PATCH v4 1/5] remoteproc: Use u64 len for da_to_va

Quoted 215 lines · Clément Leger

Hi Arnaud,

Quoted 174 lines · ----- On 11 Feb, 2020, at 16:53, Arnaud Pouliquen [email protected] wrote:
Quoted 173 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 11 ++++++-----
 drivers/remoteproc/keystone_remoteproc.c |  4 ++--
 drivers/remoteproc/qcom_q6v5_adsp.c      |  2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      |  2 +-
 drivers/remoteproc/qcom_wcnss.c          |  2 +-
 drivers/remoteproc/remoteproc_core.c     |  2 +-
 drivers/remoteproc/remoteproc_internal.h |  2 +-
 drivers/remoteproc/st_slim_rproc.c       |  4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       |  4 ++--
 include/linux/remoteproc.h               |  2 +-
 12 files changed, 20 insertions(+), 19 deletions(-)
drivers/remoteproc/imx_rproc.c +4 -4
index 3e72b6f38d4b..f497f5b49b18 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-			       int len, u64 *sys)
+			       u64 len, u64 *sys)
 {
 	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 	int i;
@@ -203,19 +203,19 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64
 da,
 		}
 	}
 
-	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
 		 da, len);
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;
 	u64 sys;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
drivers/remoteproc/imx_rproc.c +2 -1
 	/*
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da,
 int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+		da, len, va);
 
 	return va;

}

drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..466093f48814 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int
 vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
drivers/remoteproc/keystone_remoteproc.c +1 -1
 	void __iomem *va = NULL;
@@ -255,7 +255,7 @@ static void *keystone_rproc_da_to_va(struct rproc *rproc,
 u64 da, int len)
 	size_t size;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
drivers/remoteproc/keystone_remoteproc.c +0 -0
 	for (i = 0; i < ksproc->num_mems; i++) {
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +0 -0
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
Quoted 8 lines

drivers/remoteproc/remoteproc_core.c EXPORT_SYMBOL(rproc_va_to_pa); · line 188

Quoted 2 lines
drivers/remoteproc/remoteproc_core.c +1 -1
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

This function is exported, don't see any update in consequence... references:
https://elixir.bootlin.com/linux/v5.6-rc1/ident/rproc_da_to_va
For instance the function rproc_trace_read use it. it quite strange that my gcc does not warns for the cast but i suppose that some could.

Agreed, even if len should never have been a signed type since it can't be negative. I will try to fix all callers.

Quoted 2 lines

An indirect consequence is that the len field in rproc_mem_entry struct should probably been updated to u64 to be aligned.

Ok, I will do that once we settle on the type of len.

Quoted 5 lines

I'm still wondering about the use of size_t instead,which seems more rational from my window.
So i you or Mathieu remember it was decided to use u64, please could remind me the arguments?

I tried to find the notes of a meeting we had for OpenAMP but I did not found them. Anyway, the argument was coming from Tomas or someone else, (I can't remember) talking about a 32 bits CPU executing code on a 64 bits accelerator. In that case, the size_t type could fail due to being only 32bits on the host CPU but larger than 4G.

However, I can't say if it's a real usecase or not... All I can say is that keeping it open is probably better if one day somebody comes with such architecture.

Quoted 3 lines

As an alternative a check should be added for 32 bits processors to ensure that the size is not higher than
its address range capability...

Quoted 8 lines

Agreed.
I was even thinking about a mecanism for remoteproc drivers to declare the type of supported elfs files (such as EM_*, ELFCLASS* and other needed thing). Or should it be supported by overriding .sanity_check in drivers to reject elf64 for instance ?

Since elf is a "specific format" and that rproc can support other formats, I did not want to add a specific elf_sanity_check field to rproc ops.

Agree, platform driver as to check the format it can support, elf, bin, properitay format.... To check the size, the da_to_va ops seems a good candidate as called for loaded segments and memories. So ok to delegate the check to the platform driver.

Regards
Arnaud

Quoted 85 lines

Regards,

Clément

Quoted 80 lines

Regards
Arnaud

Quoted 76 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64
 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
 		da, len, va);
 
drivers/remoteproc/st_slim_rproc.c +0 -0
 	return va;
drivers/remoteproc/wkup_m3_rproc.c +2 -2
index 3984e585c847..91485b467407 100644
@@ -80,14 +80,14 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
 	int i;
 	u32 offset;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	for (i = 0; i < WKUPM3_MEM_MAX; i++) {
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
maintainer
#36 lore raw

Re: [PATCH v4 1/5] remoteproc: Use u64 len for da_to_va

In reply to Clément Leger
Quoted 207 lines · Cl�ment Leger

Hi Arnaud,

Quoted 174 lines · ----- On 11 Feb, 2020, at 16:53, Arnaud Pouliquen [email protected] wrote:
Quoted 173 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 11 ++++++-----
 drivers/remoteproc/keystone_remoteproc.c |  4 ++--
 drivers/remoteproc/qcom_q6v5_adsp.c      |  2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      |  2 +-
 drivers/remoteproc/qcom_wcnss.c          |  2 +-
 drivers/remoteproc/remoteproc_core.c     |  2 +-
 drivers/remoteproc/remoteproc_internal.h |  2 +-
 drivers/remoteproc/st_slim_rproc.c       |  4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       |  4 ++--
 include/linux/remoteproc.h               |  2 +-
 12 files changed, 20 insertions(+), 19 deletions(-)
drivers/remoteproc/imx_rproc.c +4 -4
index 3e72b6f38d4b..f497f5b49b18 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-			       int len, u64 *sys)
+			       u64 len, u64 *sys)
 {
 	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 	int i;
@@ -203,19 +203,19 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64
 da,
 		}
 	}
 
-	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
 		 da, len);
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;
 	u64 sys;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
drivers/remoteproc/imx_rproc.c +2 -1
 	/*
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da,
 int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+		da, len, va);
 
 	return va;

}

drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..466093f48814 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int
 vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
drivers/remoteproc/keystone_remoteproc.c +1 -1
 	void __iomem *va = NULL;
@@ -255,7 +255,7 @@ static void *keystone_rproc_da_to_va(struct rproc *rproc,
 u64 da, int len)
 	size_t size;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
drivers/remoteproc/keystone_remoteproc.c +0 -0
 	for (i = 0; i < ksproc->num_mems; i++) {
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +0 -0
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
Quoted 8 lines

drivers/remoteproc/remoteproc_core.c EXPORT_SYMBOL(rproc_va_to_pa); · line 188

Quoted 2 lines
drivers/remoteproc/remoteproc_core.c +1 -1
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

This function is exported, don't see any update in consequence... references:
https://elixir.bootlin.com/linux/v5.6-rc1/ident/rproc_da_to_va
For instance the function rproc_trace_read use it. it quite strange that my gcc does not warns for the cast but i suppose that some could.

Agreed, even if len should never have been a signed type since it can't be negative. I will try to fix all callers.

Quoted 2 lines

An indirect consequence is that the len field in rproc_mem_entry struct should probably been updated to u64 to be aligned.

Ok, I will do that once we settle on the type of len.

Quoted 5 lines

I'm still wondering about the use of size_t instead,which seems more rational from my window.
So i you or Mathieu remember it was decided to use u64, please could remind me the arguments?

I tried to find the notes of a meeting we had for OpenAMP but I did not found them. Anyway, the argument was coming from Tomas or someone else, (I can't remember) talking about a 32 bits CPU executing code on a 64 bits accelerator. In that case, the size_t type could fail due to being only 32bits on the host CPU but larger than 4G.

Quoted 3 lines

However, I can't say if it's a real usecase or not... All I can say is that keeping it open is probably better if one day somebody comes with such architecture.

In order to support a 32bit AP with a 64bit MCU we'd also have to deal with all the dma_attr_t in the structure we use.

Also something that became very clear to me while thinking about this patchset is that supporting elf64 does __not__ mean we support 64bit MCU. As long as the addresses conveyed by the elf64 image fit within 32 bits we are fine. Supporting 64bit MCUs is a completely different topic, one that will demand serious refactoring.

So moving from "int len" to "u64 len" doesn't give us much. It doesn't hurt to do it but if @len ever becomes bigger than 31 bits we'll have other problems to deal with.

Quoted 98 lines
Quoted 4 lines

As an alternative a check should be added for 32 bits processors to ensure that the size is not higher than
its address range capability...

Agreed.
I was even thinking about a mecanism for remoteproc drivers to declare the type of supported elfs files (such as EM_*, ELFCLASS* and other needed thing). Or should it be supported by overriding .sanity_check in drivers to reject elf64 for instance ?

Since elf is a "specific format" and that rproc can support other formats, I did not want to add a specific elf_sanity_check field to rproc ops.

Regards,

Cl�ment

Quoted 80 lines

Regards
Arnaud

Quoted 76 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64
 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
 		da, len, va);
 
drivers/remoteproc/st_slim_rproc.c +0 -0
 	return va;
drivers/remoteproc/wkup_m3_rproc.c +2 -2
index 3984e585c847..91485b467407 100644
@@ -80,14 +80,14 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
 	int i;
 	u32 offset;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	for (i = 0; i < WKUPM3_MEM_MAX; i++) {
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
#37 lore raw

Re: [PATCH v4 1/5] remoteproc: Use u64 len for da_to_va

Hi Mathieu,

Quoted 208 lines · ----- On 11 Feb, 2020, at 23:37, Mathieu Poirier [email protected]…
Quoted 207 lines · Clément Leger

Hi Arnaud,

Quoted 174 lines · ----- On 11 Feb, 2020, at 16:53, Arnaud Pouliquen [email protected] wrote:
Quoted 173 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 11 ++++++-----
 drivers/remoteproc/keystone_remoteproc.c |  4 ++--
 drivers/remoteproc/qcom_q6v5_adsp.c      |  2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      |  2 +-
 drivers/remoteproc/qcom_wcnss.c          |  2 +-
 drivers/remoteproc/remoteproc_core.c     |  2 +-
 drivers/remoteproc/remoteproc_internal.h |  2 +-
 drivers/remoteproc/st_slim_rproc.c       |  4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       |  4 ++--
 include/linux/remoteproc.h               |  2 +-
 12 files changed, 20 insertions(+), 19 deletions(-)
drivers/remoteproc/imx_rproc.c +4 -4
index 3e72b6f38d4b..f497f5b49b18 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-			       int len, u64 *sys)
+			       u64 len, u64 *sys)
 {
 	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 	int i;
@@ -203,19 +203,19 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64
 da,
 		}
 	}
 
-	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
 		 da, len);
 	return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct imx_rproc *priv = rproc->priv;
 	void *va = NULL;
 	u64 sys;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
drivers/remoteproc/imx_rproc.c +2 -1
 	/*
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da,
 int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+		da, len, va);
 
 	return va;

}

drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..466093f48814 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int
 vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct keystone_rproc *ksproc = rproc->priv;
drivers/remoteproc/keystone_remoteproc.c +1 -1
 	void __iomem *va = NULL;
@@ -255,7 +255,7 @@ static void *keystone_rproc_da_to_va(struct rproc *rproc,
 u64 da, int len)
 	size_t size;
 	int i;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
drivers/remoteproc/keystone_remoteproc.c +0 -0
 	for (i = 0; i < ksproc->num_mems; i++) {
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5 *qproc = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
 	int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct q6v5_wcss *wcss = rproc->priv;
 	int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
 	return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
 	int offset;
drivers/remoteproc/remoteproc_core.c +0 -0
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
Quoted 8 lines

drivers/remoteproc/remoteproc_core.c EXPORT_SYMBOL(rproc_va_to_pa); · line 188

Quoted 2 lines
drivers/remoteproc/remoteproc_core.c +1 -1
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

This function is exported, don't see any update in consequence... references:
https://elixir.bootlin.com/linux/v5.6-rc1/ident/rproc_da_to_va
For instance the function rproc_trace_read use it. it quite strange that my gcc does not warns for the cast but i suppose that some could.

Agreed, even if len should never have been a signed type since it can't be negative. I will try to fix all callers.

Quoted 2 lines

An indirect consequence is that the len field in rproc_mem_entry struct should probably been updated to u64 to be aligned.

Ok, I will do that once we settle on the type of len.

Quoted 5 lines

I'm still wondering about the use of size_t instead,which seems more rational from my window.
So i you or Mathieu remember it was decided to use u64, please could remind me the arguments?

I tried to find the notes of a meeting we had for OpenAMP but I did not found them. Anyway, the argument was coming from Tomas or someone else, (I can't remember) talking about a 32 bits CPU executing code on a 64 bits accelerator. In that case, the size_t type could fail due to being only 32bits on the host CPU but larger than 4G.

Quoted 6 lines
Quoted 3 lines

However, I can't say if it's a real usecase or not... All I can say is that keeping it open is probably better if one day somebody comes with such architecture.

In order to support a 32bit AP with a 64bit MCU we'd also have to deal with all the dma_attr_t in the structure we use.

Totally ok with that...

Quoted 6 lines

Also something that became very clear to me while thinking about this patchset is that supporting elf64 does __not__ mean we support 64bit MCU. As long as the addresses conveyed by the elf64 image fit within 32 bits we are fine. Supporting 64bit MCUs is a completely different topic, one that will demand serious refactoring.

Exactly, an elf64 can potentially contain an executable fitting in 32 bits.

Quoted 4 lines

So moving from "int len" to "u64 len" doesn't give us much. It doesn't hurt to do it but if @len ever becomes bigger than 31 bits we'll have other problems to deal with.

Agreed, so what would be your recommendation reagrding the type of len ? I'm ok with Arnaud statement too and using a size_t is probably more "type-safe" than a u64. At least it adds some information.

Thanks,

Clément

Quoted 99 lines
Quoted 99 lines
Quoted 5 lines

As an alternative a check should be added for 32 bits processors to ensure that the size is not higher than
its address range capability...

Agreed.
I was even thinking about a mecanism for remoteproc drivers to declare the type of supported elfs files (such as EM_*, ELFCLASS* and other needed thing). Or should it be supported by overriding .sanity_check in drivers to reject elf64 for instance ?

Since elf is a "specific format" and that rproc can support other formats, I did not want to add a specific elf_sanity_check field to rproc ops.

Regards,

Clément

Quoted 80 lines

Regards
Arnaud

Quoted 76 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 {
 	struct rproc_mem_entry *carveout;
 	void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct st_slim_rproc *slim_rproc = rproc->priv;
 	void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64
 da, int len)
 		}
 	}
 
-	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
 		da, len, va);
 
drivers/remoteproc/st_slim_rproc.c +0 -0
 	return va;
drivers/remoteproc/wkup_m3_rproc.c +2 -2
index 3984e585c847..91485b467407 100644
@@ -80,14 +80,14 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
 	return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
 	void *va = NULL;
 	int i;
 	u32 offset;
 
-	if (len <= 0)
+	if (len == 0)
 		return NULL;
 
 	for (i = 0; i < WKUPM3_MEM_MAX; i++) {
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
 	int (*start)(struct rproc *rproc);
 	int (*stop)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
-	void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+	void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
maintainer
#38 lore raw

Re: [PATCH v4 1/5] remoteproc: Use u64 len for da_to_va

Quoted 232 lines · Clément Leger

Hi Mathieu,

Quoted 208 lines · ----- On 11 Feb, 2020, at 23:37, Mathieu Poirier [email protected]…
Quoted 207 lines · Clément Leger

Hi Arnaud,

Quoted 174 lines · ----- On 11 Feb, 2020, at 16:53, Arnaud Pouliquen [email protected] wrote:
Quoted 173 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 11 ++++++-----
 drivers/remoteproc/keystone_remoteproc.c |  4 ++--
 drivers/remoteproc/qcom_q6v5_adsp.c      |  2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      |  2 +-
 drivers/remoteproc/qcom_wcnss.c          |  2 +-
 drivers/remoteproc/remoteproc_core.c     |  2 +-
 drivers/remoteproc/remoteproc_internal.h |  2 +-
 drivers/remoteproc/st_slim_rproc.c       |  4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       |  4 ++--
 include/linux/remoteproc.h               |  2 +-
 12 files changed, 20 insertions(+), 19 deletions(-)
drivers/remoteproc/imx_rproc.c +4 -4
index 3e72b6f38d4b..f497f5b49b18 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-                        int len, u64 *sys)
+                        u64 len, u64 *sys)
 {
  const struct imx_rproc_dcfg *dcfg = priv->dcfg;
  int i;
@@ -203,19 +203,19 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64
 da,
          }
  }
 
- dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+ dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
           da, len);
  return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct imx_rproc *priv = rproc->priv;
  void *va = NULL;
  u64 sys;
  int i;
 
- if (len <= 0)
+ if (len == 0)
          return NULL;
 

/*

drivers/remoteproc/imx_rproc.c +2 -1
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da,
 int len)
          }
  }
 
- dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+ dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+         da, len, va);
 
  return va;

}

drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..466093f48814 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int
 vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct keystone_rproc *ksproc = rproc->priv;
  void __iomem *va = NULL;
drivers/remoteproc/keystone_remoteproc.c +1 -1
@@ -255,7 +255,7 @@ static void *keystone_rproc_da_to_va(struct rproc *rproc,
 u64 da, int len)
  size_t size;
  int i;
 
- if (len <= 0)
+ if (len == 0)
          return NULL;
 
  for (i = 0; i < ksproc->num_mems; i++) {
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
  return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct q6v5 *qproc = rproc->priv;
  int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
  return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct q6v5_wcss *wcss = rproc->priv;
  int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
  return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
  int offset;
drivers/remoteproc/remoteproc_core.c +0 -0
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
Quoted 8 lines

drivers/remoteproc/remoteproc_core.c EXPORT_SYMBOL(rproc_va_to_pa); · line 188

Quoted 2 lines
drivers/remoteproc/remoteproc_core.c +1 -1
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

This function is exported, don't see any update in consequence... references:
https://elixir.bootlin.com/linux/v5.6-rc1/ident/rproc_da_to_va
For instance the function rproc_trace_read use it. it quite strange that my gcc does not warns for the cast but i suppose that some could.

Agreed, even if len should never have been a signed type since it can't be negative. I will try to fix all callers.

Quoted 2 lines

An indirect consequence is that the len field in rproc_mem_entry struct should probably been updated to u64 to be aligned.

Ok, I will do that once we settle on the type of len.

Quoted 5 lines

I'm still wondering about the use of size_t instead,which seems more rational from my window.
So i you or Mathieu remember it was decided to use u64, please could remind me the arguments?

I tried to find the notes of a meeting we had for OpenAMP but I did not found them. Anyway, the argument was coming from Tomas or someone else, (I can't remember) talking about a 32 bits CPU executing code on a 64 bits accelerator. In that case, the size_t type could fail due to being only 32bits on the host CPU but larger than 4G.

Quoted 6 lines
Quoted 3 lines

However, I can't say if it's a real usecase or not... All I can say is that keeping it open is probably better if one day somebody comes with such architecture.

In order to support a 32bit AP with a 64bit MCU we'd also have to deal with all the dma_attr_t in the structure we use.

Totally ok with that...

Quoted 6 lines

Also something that became very clear to me while thinking about this patchset is that supporting elf64 does __not__ mean we support 64bit MCU. As long as the addresses conveyed by the elf64 image fit within 32 bits we are fine. Supporting 64bit MCUs is a completely different topic, one that will demand serious refactoring.

Exactly, an elf64 can potentially contain an executable fitting in 32 bits.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

So moving from "int len" to "u64 len" doesn't give us much. It doesn't hurt to do it but if @len ever becomes bigger than 31 bits we'll have other problems to deal with.

Agreed, so what would be your recommendation reagrding the type of len ? I'm ok with Arnaud statement too and using a size_t is probably more "type-safe" than a u64. At least it adds some information.

If @len becomes big enough that it doesn't fit in 31bit then it is very likely that things will break even before we get to call rproc_da_to_va(). Fixing it here is possible but will introduce a fair amount of ripple effect that we probably don't want to deal with right now.

Other people might feel more opinionated on this but as far as I'm concerned, I would keep it as it is and fix it for real when the time comes to add support for 64bit MCUs.

Quoted 104 lines

Thanks,

Clément

Quoted 99 lines
Quoted 99 lines
Quoted 5 lines

As an alternative a check should be added for 32 bits processors to ensure that the size is not higher than
its address range capability...

Agreed.
I was even thinking about a mecanism for remoteproc drivers to declare the type of supported elfs files (such as EM_*, ELFCLASS* and other needed thing). Or should it be supported by overriding .sanity_check in drivers to reject elf64 for instance ?

Since elf is a "specific format" and that rproc can support other formats, I did not want to add a specific elf_sanity_check field to rproc ops.

Regards,

Clément

Quoted 80 lines

Regards
Arnaud

Quoted 76 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 {
  struct rproc_mem_entry *carveout;
  void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct st_slim_rproc *slim_rproc = rproc->priv;
  void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64
 da, int len)
          }
  }
 
- dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+ dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
          da, len, va);
 

return va;

drivers/remoteproc/wkup_m3_rproc.c +2 -2
index 3984e585c847..91485b467407 100644
@@ -80,14 +80,14 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct wkup_m3_rproc *wkupm3 = rproc->priv;
  void *va = NULL;
  int i;
  u32 offset;
 
- if (len <= 0)
+ if (len == 0)
          return NULL;
 
  for (i = 0; i < WKUPM3_MEM_MAX; i++) {
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
  int (*start)(struct rproc *rproc);
  int (*stop)(struct rproc *rproc);
  void (*kick)(struct rproc *rproc, int vqid);
- void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+ void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
  int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
  int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
                           int offset, int avail);
#39 lore raw

Re: [PATCH v4 1/5] remoteproc: Use u64 len for da_to_va

Hi Mathieu,

Quoted 242 lines · ----- On 12 Feb, 2020, at 22:59, Mathieu Poirier [email protected]…
Quoted 233 lines · Clément Leger

Hi Mathieu,

----- On 11 Feb, 2020, at 23:37, Mathieu Poirier [email protected]

Quoted 208 lines · wrote:
Quoted 207 lines · Clément Leger

Hi Arnaud,

Quoted 174 lines · ----- On 11 Feb, 2020, at 16:53, Arnaud Pouliquen [email protected] wrote:
Quoted 173 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 11 ++++++-----
 drivers/remoteproc/keystone_remoteproc.c |  4 ++--
 drivers/remoteproc/qcom_q6v5_adsp.c      |  2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      |  2 +-
 drivers/remoteproc/qcom_wcnss.c          |  2 +-
 drivers/remoteproc/remoteproc_core.c     |  2 +-
 drivers/remoteproc/remoteproc_internal.h |  2 +-
 drivers/remoteproc/st_slim_rproc.c       |  4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       |  4 ++--
 include/linux/remoteproc.h               |  2 +-
 12 files changed, 20 insertions(+), 19 deletions(-)
drivers/remoteproc/imx_rproc.c +4 -4
index 3e72b6f38d4b..f497f5b49b18 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-                        int len, u64 *sys)
+                        u64 len, u64 *sys)
 {
  const struct imx_rproc_dcfg *dcfg = priv->dcfg;
  int i;
@@ -203,19 +203,19 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64
 da,
          }
  }
 
- dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+ dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
           da, len);
  return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct imx_rproc *priv = rproc->priv;
  void *va = NULL;
  u64 sys;
  int i;
 
- if (len <= 0)
+ if (len == 0)
          return NULL;
 

/*

drivers/remoteproc/imx_rproc.c +2 -1
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da,
 int len)
          }
  }
 
- dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+ dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+         da, len, va);
 
  return va;

}

drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..466093f48814 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int
 vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct keystone_rproc *ksproc = rproc->priv;
  void __iomem *va = NULL;
drivers/remoteproc/keystone_remoteproc.c +1 -1
@@ -255,7 +255,7 @@ static void *keystone_rproc_da_to_va(struct rproc *rproc,
 u64 da, int len)
  size_t size;
  int i;
 
- if (len <= 0)
+ if (len == 0)
          return NULL;
 
  for (i = 0; i < ksproc->num_mems; i++) {
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
  return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct q6v5 *qproc = rproc->priv;
  int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
  return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct q6v5_wcss *wcss = rproc->priv;
  int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
  return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
  int offset;
drivers/remoteproc/remoteproc_core.c +0 -0
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
Quoted 8 lines

drivers/remoteproc/remoteproc_core.c EXPORT_SYMBOL(rproc_va_to_pa); · line 188

Quoted 2 lines
drivers/remoteproc/remoteproc_core.c +1 -1
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

This function is exported, don't see any update in consequence... references:
https://elixir.bootlin.com/linux/v5.6-rc1/ident/rproc_da_to_va
For instance the function rproc_trace_read use it. it quite strange that my gcc does not warns for the cast but i suppose that some could.

Agreed, even if len should never have been a signed type since it can't be negative. I will try to fix all callers.

Quoted 2 lines

An indirect consequence is that the len field in rproc_mem_entry struct should probably been updated to u64 to be aligned.

Ok, I will do that once we settle on the type of len.

Quoted 5 lines

I'm still wondering about the use of size_t instead,which seems more rational from my window.
So i you or Mathieu remember it was decided to use u64, please could remind me the arguments?

I tried to find the notes of a meeting we had for OpenAMP but I did not found them. Anyway, the argument was coming from Tomas or someone else, (I can't remember) talking about a 32 bits CPU executing code on a 64 bits accelerator. In that case, the size_t type could fail due to being only 32bits on the host CPU but larger than 4G.

Quoted 6 lines
Quoted 3 lines

However, I can't say if it's a real usecase or not... All I can say is that keeping it open is probably better if one day somebody comes with such architecture.

In order to support a 32bit AP with a 64bit MCU we'd also have to deal with all the dma_attr_t in the structure we use.

Totally ok with that...

Quoted 6 lines

Also something that became very clear to me while thinking about this patchset is that supporting elf64 does __not__ mean we support 64bit MCU. As long as the addresses conveyed by the elf64 image fit within 32 bits we are fine. Supporting 64bit MCUs is a completely different topic, one that will demand serious refactoring.

Exactly, an elf64 can potentially contain an executable fitting in 32 bits.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

So moving from "int len" to "u64 len" doesn't give us much. It doesn't hurt to do it but if @len ever becomes bigger than 31 bits we'll have other problems to deal with.

Agreed, so what would be your recommendation reagrding the type of len ? I'm ok with Arnaud statement too and using a size_t is probably more "type-safe" than a u64. At least it adds some information.

Quoted 5 lines

If @len becomes big enough that it doesn't fit in 31bit then it is very likely that things will break even before we get to call rproc_da_to_va(). Fixing it here is possible but will introduce a fair amount of ripple effect that we probably don't want to deal with right now.

I did the modification using u64 and tried to follow various code path. Some end up in dma_alloc_coherent which uses a size_t member. Since these might be called by rproc with a u64 len, I would be more inclined to use a size_t. I can probably also add a check in elf loader which verifies that if sizeof(size_t) < sizeof(u64), then the len must fit in 32bits. This seems more clean IMHO.

Clément.

Quoted 109 lines

Other people might feel more opinionated on this but as far as I'm concerned, I would keep it as it is and fix it for real when the time comes to add support for 64bit MCUs.

Quoted 104 lines

Thanks,

Clément

Quoted 99 lines
Quoted 99 lines
Quoted 5 lines

As an alternative a check should be added for 32 bits processors to ensure that the size is not higher than
its address range capability...

Agreed.
I was even thinking about a mecanism for remoteproc drivers to declare the type of supported elfs files (such as EM_*, ELFCLASS* and other needed thing). Or should it be supported by overriding .sanity_check in drivers to reject elf64 for instance ?

Since elf is a "specific format" and that rproc can support other formats, I did not want to add a specific elf_sanity_check field to rproc ops.

Regards,

Clément

Quoted 80 lines

Regards
Arnaud

Quoted 76 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 {
  struct rproc_mem_entry *carveout;
  void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct st_slim_rproc *slim_rproc = rproc->priv;
  void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64
 da, int len)
          }
  }
 
- dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+ dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
          da, len, va);
 

return va;

drivers/remoteproc/wkup_m3_rproc.c +2 -2
index 3984e585c847..91485b467407 100644
@@ -80,14 +80,14 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct wkup_m3_rproc *wkupm3 = rproc->priv;
  void *va = NULL;
  int i;
  u32 offset;
 
- if (len <= 0)
+ if (len == 0)
          return NULL;
 
  for (i = 0; i < WKUPM3_MEM_MAX; i++) {
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
  int (*start)(struct rproc *rproc);
  int (*stop)(struct rproc *rproc);
  void (*kick)(struct rproc *rproc, int vqid);
- void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+ void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
  int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
  int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
                           int offset, int avail);
maintainer
#40 lore raw

Re: [PATCH v4 1/5] remoteproc: Use u64 len for da_to_va

Quoted 253 lines · Clément Leger

Hi Mathieu,

Quoted 242 lines · ----- On 12 Feb, 2020, at 22:59, Mathieu Poirier [email protected]…
Quoted 233 lines · Clément Leger

Hi Mathieu,

----- On 11 Feb, 2020, at 23:37, Mathieu Poirier [email protected]

Quoted 208 lines · wrote:
Quoted 207 lines · Clément Leger

Hi Arnaud,

Quoted 174 lines · ----- On 11 Feb, 2020, at 16:53, Arnaud Pouliquen [email protected] wrote:
Quoted 173 lines · Clement Leger

With upcoming changes in elf loader for elf64 support, section size will be a u64. When used with da_to_va, this will potentially lead to overflow if using the current "int" type for len argument. Change da_to_va prototype to use a u64 for len and fix all users of this function.

---
 drivers/remoteproc/imx_rproc.c           | 11 ++++++-----
 drivers/remoteproc/keystone_remoteproc.c |  4 ++--
 drivers/remoteproc/qcom_q6v5_adsp.c      |  2 +-
 drivers/remoteproc/qcom_q6v5_mss.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_pas.c       |  2 +-
 drivers/remoteproc/qcom_q6v5_wcss.c      |  2 +-
 drivers/remoteproc/qcom_wcnss.c          |  2 +-
 drivers/remoteproc/remoteproc_core.c     |  2 +-
 drivers/remoteproc/remoteproc_internal.h |  2 +-
 drivers/remoteproc/st_slim_rproc.c       |  4 ++--
 drivers/remoteproc/wkup_m3_rproc.c       |  4 ++--
 include/linux/remoteproc.h               |  2 +-
 12 files changed, 20 insertions(+), 19 deletions(-)
drivers/remoteproc/imx_rproc.c +4 -4
index 3e72b6f38d4b..f497f5b49b18 100644
@@ -186,7 +186,7 @@ static int imx_rproc_stop(struct rproc *rproc)
 }
 
 static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
-                        int len, u64 *sys)
+                        u64 len, u64 *sys)
 {
  const struct imx_rproc_dcfg *dcfg = priv->dcfg;
  int i;
@@ -203,19 +203,19 @@ static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64
 da,
          }
  }
 
- dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%x\n",
+ dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%llx\n",
           da, len);
  return -ENOENT;
 }
 
-static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct imx_rproc *priv = rproc->priv;
  void *va = NULL;
  u64 sys;
  int i;
 
- if (len <= 0)
+ if (len == 0)
          return NULL;
 

/*

drivers/remoteproc/imx_rproc.c +2 -1
@@ -235,7 +235,8 @@ static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da,
 int len)
          }
  }
 
- dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
+ dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%p\n",
+         da, len, va);
 
  return va;

}

drivers/remoteproc/keystone_remoteproc.c +1 -1
index 5c4658f00b3d..466093f48814 100644
@@ -246,7 +246,7 @@ static void keystone_rproc_kick(struct rproc *rproc, int
 vqid)
  * can be used either by the remoteproc core for loading (when using kernel
  * remoteproc loader), or by any rpmsg bus drivers.
  */
-static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct keystone_rproc *ksproc = rproc->priv;
  void __iomem *va = NULL;
drivers/remoteproc/keystone_remoteproc.c +1 -1
@@ -255,7 +255,7 @@ static void *keystone_rproc_da_to_va(struct rproc *rproc,
 u64 da, int len)
  size_t size;
  int i;
 
- if (len <= 0)
+ if (len == 0)
          return NULL;
 
  for (i = 0; i < ksproc->num_mems; i++) {
drivers/remoteproc/qcom_q6v5_adsp.c +1 -1
index e953886b2eb7..7518e67a49e5 100644
@@ -270,7 +270,7 @@ static int adsp_stop(struct rproc *rproc)
  return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  int offset;
drivers/remoteproc/qcom_q6v5_mss.c +1 -1
index 471128a2e723..248febde6fc1 100644
@@ -1148,7 +1148,7 @@ static int q6v5_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct q6v5 *qproc = rproc->priv;
  int offset;
drivers/remoteproc/qcom_q6v5_pas.c +1 -1
index db4b3c4bacd7..cf2cd609c90d 100644
@@ -159,7 +159,7 @@ static int adsp_stop(struct rproc *rproc)
  return ret;
 }
 
-static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *adsp_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  int offset;
drivers/remoteproc/qcom_q6v5_wcss.c +1 -1
index f93e1e4a1cc0..3a6b82a16961 100644
@@ -406,7 +406,7 @@ static int q6v5_wcss_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *q6v5_wcss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct q6v5_wcss *wcss = rproc->priv;
  int offset;
drivers/remoteproc/qcom_wcnss.c +1 -1
index dc135754bb9c..f893219e45a8 100644
@@ -287,7 +287,7 @@ static int wcnss_stop(struct rproc *rproc)
  return ret;
 }
 
-static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wcnss_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
  int offset;
drivers/remoteproc/remoteproc_core.c +0 -0
index 307df98347ba..9e6d3c6a60ee 100644
@@ -185,7 +185,7 @@ EXPORT_SYMBOL(rproc_va_to_pa);
  * here the output of the DMA API for the carveouts, which should be more
  * correct.
  */
Quoted 8 lines

drivers/remoteproc/remoteproc_core.c EXPORT_SYMBOL(rproc_va_to_pa); · line 188

Quoted 2 lines
drivers/remoteproc/remoteproc_core.c +1 -1
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)

This function is exported, don't see any update in consequence... references:
https://elixir.bootlin.com/linux/v5.6-rc1/ident/rproc_da_to_va
For instance the function rproc_trace_read use it. it quite strange that my gcc does not warns for the cast but i suppose that some could.

Agreed, even if len should never have been a signed type since it can't be negative. I will try to fix all callers.

Quoted 2 lines

An indirect consequence is that the len field in rproc_mem_entry struct should probably been updated to u64 to be aligned.

Ok, I will do that once we settle on the type of len.

Quoted 5 lines

I'm still wondering about the use of size_t instead,which seems more rational from my window.
So i you or Mathieu remember it was decided to use u64, please could remind me the arguments?

I tried to find the notes of a meeting we had for OpenAMP but I did not found them. Anyway, the argument was coming from Tomas or someone else, (I can't remember) talking about a 32 bits CPU executing code on a 64 bits accelerator. In that case, the size_t type could fail due to being only 32bits on the host CPU but larger than 4G.

Quoted 6 lines
Quoted 3 lines

However, I can't say if it's a real usecase or not... All I can say is that keeping it open is probably better if one day somebody comes with such architecture.

In order to support a 32bit AP with a 64bit MCU we'd also have to deal with all the dma_attr_t in the structure we use.

Totally ok with that...

Quoted 6 lines

Also something that became very clear to me while thinking about this patchset is that supporting elf64 does __not__ mean we support 64bit MCU. As long as the addresses conveyed by the elf64 image fit within 32 bits we are fine. Supporting 64bit MCUs is a completely different topic, one that will demand serious refactoring.

Exactly, an elf64 can potentially contain an executable fitting in 32 bits.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

So moving from "int len" to "u64 len" doesn't give us much. It doesn't hurt to do it but if @len ever becomes bigger than 31 bits we'll have other problems to deal with.

Agreed, so what would be your recommendation reagrding the type of len ? I'm ok with Arnaud statement too and using a size_t is probably more "type-safe" than a u64. At least it adds some information.

Quoted 5 lines

If @len becomes big enough that it doesn't fit in 31bit then it is very likely that things will break even before we get to call rproc_da_to_va(). Fixing it here is possible but will introduce a fair amount of ripple effect that we probably don't want to deal with right now.

Quoted 7 lines

I did the modification using u64 and tried to follow various code path. Some end up in dma_alloc_coherent which uses a size_t member. Since these might be called by rproc with a u64 len, I would be more inclined to use a size_t. I can probably also add a check in elf loader which verifies that if sizeof(size_t) < sizeof(u64), then the len must fit in 32bits. This seems more clean IMHO.

I'm all good with that.

Quoted 111 lines

Clément.

Quoted 109 lines

Other people might feel more opinionated on this but as far as I'm concerned, I would keep it as it is and fix it for real when the time comes to add support for 64bit MCUs.

Quoted 104 lines

Thanks,

Clément

Quoted 99 lines
Quoted 99 lines
Quoted 5 lines

As an alternative a check should be added for 32 bits processors to ensure that the size is not higher than
its address range capability...

Agreed.
I was even thinking about a mecanism for remoteproc drivers to declare the type of supported elfs files (such as EM_*, ELFCLASS* and other needed thing). Or should it be supported by overriding .sanity_check in drivers to reject elf64 for instance ?

Since elf is a "specific format" and that rproc can support other formats, I did not want to add a specific elf_sanity_check field to rproc ops.

Regards,

Clément

Quoted 80 lines

Regards
Arnaud

Quoted 76 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 {
  struct rproc_mem_entry *carveout;
  void *ptr = NULL;
drivers/remoteproc/remoteproc_internal.h +1 -1
index 493ef9262411..004867061721 100644
@@ -50,7 +50,7 @@ void rproc_exit_sysfs(void);
 void rproc_free_vring(struct rproc_vring *rvring);
 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
 
-void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
+void *rproc_da_to_va(struct rproc *rproc, u64 da, u64 len);
 phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
drivers/remoteproc/st_slim_rproc.c +2 -2
index 04492fead3c8..fc01cd879b60 100644
@@ -174,7 +174,7 @@ static int slim_rproc_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct st_slim_rproc *slim_rproc = rproc->priv;
  void *va = NULL;
@@ -191,7 +191,7 @@ static void *slim_rproc_da_to_va(struct rproc *rproc, u64
 da, int len)
          }
  }
 
- dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%pK\n",
+ dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%llx va = 0x%pK\n",
          da, len, va);
 

return va;

drivers/remoteproc/wkup_m3_rproc.c +2 -2
index 3984e585c847..91485b467407 100644
@@ -80,14 +80,14 @@ static int wkup_m3_rproc_stop(struct rproc *rproc)
  return 0;
 }
 
-static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, u64 len)
 {
  struct wkup_m3_rproc *wkupm3 = rproc->priv;
  void *va = NULL;
  int i;
  u32 offset;
 
- if (len <= 0)
+ if (len == 0)
          return NULL;
 
  for (i = 0; i < WKUPM3_MEM_MAX; i++) {
include/linux/remoteproc.h +1 -1
index 16ad66683ad0..f84bd5fe0211 100644
@@ -374,7 +374,7 @@ struct rproc_ops {
  int (*start)(struct rproc *rproc);
  int (*stop)(struct rproc *rproc);
  void (*kick)(struct rproc *rproc, int vqid);
- void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
+ void * (*da_to_va)(struct rproc *rproc, u64 da, u64 len);
  int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
  int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
                           int offset, int avail);
#41 lore raw

PATCH v4 2/5 remoteproc: Use u64 type for boot_addr

In reply to Clement Leger

elf64 entry is defined as a u64. Since boot_addr is used to store the elf entry point, change boot_addr type to u64 to support both elf32 and elf64. In the same time, fix users that were using this variable.

---
 drivers/remoteproc/remoteproc_elf_loader.c | 2 +-
 drivers/remoteproc/remoteproc_internal.h   | 2 +-
 drivers/remoteproc/st_remoteproc.c         | 2 +-
 include/linux/remoteproc.h                 | 4 ++--
 4 files changed, 5 insertions(+), 5 deletions(-)
drivers/remoteproc/remoteproc_elf_loader.c +1 -1
index 606aae166eba..c2a9783cfb9a 100644
@@ -102,7 +102,7 @@ EXPORT_SYMBOL(rproc_elf_sanity_check);
  * Note that the boot address is not a configurable property of all remote
  * processors. Some will always boot at a specific hard-coded address.
  */
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 {
 	struct elf32_hdr *ehdr  = (struct elf32_hdr *)fw->data;
 
drivers/remoteproc/remoteproc_internal.h +1 -1
index 004867061721..eeb26434220e 100644
@@ -55,7 +55,7 @@ phys_addr_t rproc_va_to_pa(void *cpu_addr);
 int rproc_trigger_recovery(struct rproc *rproc);
 
 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw);
-u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
+u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw);
 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw);
 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
drivers/remoteproc/st_remoteproc.c +1 -1
index ee13d23b43a9..a3268d95a50e 100644
@@ -190,7 +190,7 @@ static int st_rproc_start(struct rproc *rproc)
 		}
 	}
 
-	dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
+	dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
 
 	return 0;
 
include/linux/remoteproc.h +2 -2
index f84bd5fe0211..82cebca9344c 100644
@@ -382,7 +382,7 @@ struct rproc_ops {
 				struct rproc *rproc, const struct firmware *fw);
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
-	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
 };
 
 /**
@@ -498,7 +498,7 @@ struct rproc {
 	int num_traces;
 	struct list_head carveouts;
 	struct list_head mappings;
-	u32 bootaddr;
+	u64 bootaddr;
 	struct list_head rvdevs;
 	struct list_head subdevs;
 	struct idr notifyids;
Signature · 1 line
2.15.0.276.g89ea799
#42 lore raw

PATCH v4 3/5 remoteproc: Add elf helpers to access elf64 and elf32 fields

In reply to Clement Leger

elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis.
In order to accommodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of the field for both elf class.

---
 drivers/remoteproc/remoteproc_elf_helpers.h | 95 +++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
 create mode 100644 drivers/remoteproc/remoteproc_elf_helpers.h
drivers/remoteproc/remoteproc_elf_helpers.h new file +95 -0
new file mode 100644
index 000000000000..a29c17aaedb7
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Remote processor elf helpers defines
+ *
+ * Copyright (C) 2020 Kalray, Inc.
+ */
+
+#ifndef REMOTEPROC_ELF_LOADER_H
+#define REMOTEPROC_ELF_LOADER_H
+
+#include <linux/elf.h>
+#include <linux/types.h>
+
+/**
+ * fw_elf_get_class - Get elf class
+ * @fw: the ELF firmware image
+ *
+ * Note that we use and elf32_hdr to access the class since the start of the
+ * struct is the same for both elf class
+ *
+ * Return: elf class of the firmware
+ */
+static inline u8 fw_elf_get_class(const struct firmware *fw)
+{
+	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+	return ehdr->e_ident[EI_CLASS];
+}
+
+static inline void elf_hdr_init_ident(struct elf32_hdr *hdr, u8 class)
+{
+	memcpy(hdr->e_ident, ELFMAG, SELFMAG);
+	hdr->e_ident[EI_CLASS] = class;
+	hdr->e_ident[EI_DATA] = ELFDATA2LSB;
+	hdr->e_ident[EI_VERSION] = EV_CURRENT;
+	hdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
+}
+
+/* Generate getter and setter for a specific elf struct/field */
+#define ELF_GEN_FIELD_GET_SET(__s, __field, __type) \
+static inline __type elf_##__s##_get_##__field(u8 class, const void *arg) \
+{ \
+	if (class == ELFCLASS32) \
+		return (__type) ((const struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((const struct elf64_##__s *) arg)->__field; \
+} \
+static inline void elf_##__s##_set_##__field(u8 class, void *arg, __type value) \
+{ \
+	if (class == ELFCLASS32) \
+		((struct elf32_##__s *) arg)->__field = (__type) value; \
+	else \
+		((struct elf64_##__s *) arg)->__field = (__type) value; \
+}
+
+ELF_GEN_FIELD_GET_SET(hdr, e_entry, u64)
+ELF_GEN_FIELD_GET_SET(hdr, e_phnum, u16)
+ELF_GEN_FIELD_GET_SET(hdr, e_shnum, u16)
+ELF_GEN_FIELD_GET_SET(hdr, e_phoff, u64)
+ELF_GEN_FIELD_GET_SET(hdr, e_shoff, u64)
+ELF_GEN_FIELD_GET_SET(hdr, e_shstrndx, u16)
+ELF_GEN_FIELD_GET_SET(hdr, e_machine, u16)
+ELF_GEN_FIELD_GET_SET(hdr, e_type, u16)
+ELF_GEN_FIELD_GET_SET(hdr, e_version, u32)
+ELF_GEN_FIELD_GET_SET(hdr, e_ehsize, u32)
+ELF_GEN_FIELD_GET_SET(hdr, e_phentsize, u16)
+
+ELF_GEN_FIELD_GET_SET(phdr, p_paddr, u64)
+ELF_GEN_FIELD_GET_SET(phdr, p_vaddr, u64)
+ELF_GEN_FIELD_GET_SET(phdr, p_filesz, u64)
+ELF_GEN_FIELD_GET_SET(phdr, p_memsz, u64)
+ELF_GEN_FIELD_GET_SET(phdr, p_type, u32)
+ELF_GEN_FIELD_GET_SET(phdr, p_offset, u64)
+ELF_GEN_FIELD_GET_SET(phdr, p_flags, u32)
+ELF_GEN_FIELD_GET_SET(phdr, p_align, u64)
+
+ELF_GEN_FIELD_GET_SET(shdr, sh_size, u64)
+ELF_GEN_FIELD_GET_SET(shdr, sh_offset, u64)
+ELF_GEN_FIELD_GET_SET(shdr, sh_name, u32)
+ELF_GEN_FIELD_GET_SET(shdr, sh_addr, u64)
+
+#define ELF_STRUCT_SIZE(__s) \
+static inline unsigned long elf_size_of_##__s(u8 class) \
+{ \
+	if (class == ELFCLASS32)\
+		return sizeof(struct elf32_##__s); \
+	else \
+		return sizeof(struct elf64_##__s); \
+}
+
+ELF_STRUCT_SIZE(shdr)
+ELF_STRUCT_SIZE(phdr)
+ELF_STRUCT_SIZE(hdr)
+
+#endif /* REMOTEPROC_ELF_LOADER_H */
Signature · 1 line
2.15.0.276.g89ea799
#43 lore raw

PATCH v4 4/5 remoteproc: Add elf64 support in elf loader

In reply to Clement Leger

In order to support elf64, use macros from remoteproc_elf_helpers.h to access elf headers depending on elf class.

---
 Documentation/remoteproc.txt               |   2 +-
 drivers/remoteproc/remoteproc_elf_loader.c | 145 ++++++++++++++++++-----------
 2 files changed, 93 insertions(+), 54 deletions(-)
Documentation/remoteproc.txt +1 -1
index 03c3d2e568b0..2be1147256e0 100644
@@ -230,7 +230,7 @@ in the used rings.
 Binary Firmware Structure
 =========================
 
-At this point remoteproc only supports ELF32 firmware binaries. However,
+At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
 it is quite expected that other platforms/devices which we'd want to
 support with this framework will be based on different binary formats.
 
drivers/remoteproc/remoteproc_elf_loader.c +92 -53

263 lines not on this page · raw message

Signature · 1 line
2.15.0.276.g89ea799