Skip to contentSkip to conversation

linux-user: select CPU type according ELF header values

PATCH Superseded Laurent Vivier 3 participants 11 messages Jan 13, 2018 – Jan 24, 2018

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover linux-user: select CPU type according ELF header values

This idea has been suggested to me before by Philippe Mathieu-Daudé, and recently YunQiang Su has proposed a patch to manage the MIPS r6 case.

Based on this, this series tries to clean-up the original patch, and introduces the use for m68k architecture and port the patch from YunQiang Su.

Laurent Vivier (1):
linux-user,m68k: select CPU according to ELF header values

YunQiang Su (2):
linux-user: introduce functions to detect CPU type
linux-user: MIPS set cpu to r6 CPU if binary is R6

 include/elf.h        |   4 ++
 linux-user/elfload.c |  37 ++++++++++++++++
 linux-user/main.c    | 121 +++++++++++++++++++++++++++++++--------------------
 linux-user/qemu.h    |   1 +
 4 files changed, 115 insertions(+), 48 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH 1/3 linux-user: introduce functions to detect CPU type

From: YunQiang Su <[email protected]>

Move CPU type name selection to a function,
and add a function to return ELF e_flags.

[lv: splitted the patch and some cleanup in get_elf_eflags()]

---

Notes:
YunQiang Su, please add your Signed-off-by that was missing in your original patch.

 linux-user/elfload.c |  37 +++++++++++++++++++
 linux-user/main.c    | 101 +++++++++++++++++++++++++++------------------------
 linux-user/qemu.h    |   1 +
 3 files changed, 91 insertions(+), 48 deletions(-)
linux-user/elfload.c +37 -0
index 20f3d8c2c3..03244f5e97 100644
@@ -2376,6 +2376,43 @@ give_up:
     g_free(syms);
 }
 
+int get_elf_eflags(int fd, uint32_t *eflags)
+{
+    struct elfhdr ehdr;
+    off_t offset;
+    int ret;
+
+    /* Read ELF header */
+    offset = lseek(fd, 0, SEEK_SET);
+    if (offset == (off_t) -1) {
+        return -1;
+    }
+    ret = read(fd, &ehdr, sizeof(ehdr));
+    if (ret < sizeof(ehdr)) {
+        return -1;
+    }
+    offset = lseek(fd, offset, SEEK_SET);
+    if (offset == (off_t) -1) {
+        return -1;
+    }
+
+    /* Check ELF signature */
+    if (!elf_check_ident(&ehdr)) {
+        return -1;
+    }
+
+    /* check header */
+    bswap_ehdr(&ehdr);
+    if (!elf_check_ehdr(&ehdr)) {
+        return -1;
+    }
+
+    /* return architecture id */
+    *eflags = ehdr.e_flags;
+
+    return 0;
+}
+
 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
 {
     struct image_info interp_info;
linux-user/main.c +53 -48

120 lines not on this page · raw message

linux-user/qemu.h +1 -0
index 4edd7d0c08..c2a701163c 100644
@@ -188,6 +188,7 @@ int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
              struct target_pt_regs * regs, struct image_info *infop,
              struct linux_binprm *);
 
+int get_elf_eflags(int fd, uint32_t *eflags);
 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info);
 int load_flt_binary(struct linux_binprm *bprm, struct image_info *info);
 
Signature · 1 line
2.14.3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] linux-user: introduce functions to detect CPU type

Quoted 8 lines · Laurent Vivier

From: YunQiang Su <[email protected]>

Move CPU type name selection to a function,
and add a function to return ELF e_flags.

[lv: splitted the patch and some cleanup in get_elf_eflags()]

---

This needs to be split.

Quoted 5 lines
linux-user/elfload.c +5 -0
+int get_elf_eflags(int fd, uint32_t *eflags)
+{
+    struct elfhdr ehdr;
+    off_t offset;
+    int ret;

linux-user/elfload.c give_up: · line 2393

Quoted 10 lines
linux-user/elfload.c +10 -0
+
+    /* Read ELF header */
+    offset = lseek(fd, 0, SEEK_SET);
+    if (offset == (off_t) -1) {
+        return -1;
+    }
+    ret = read(fd, &ehdr, sizeof(ehdr));
+    if (ret < sizeof(ehdr)) {
+        return -1;
+    }

There is no reason to read the elf header twice -- e_flags has already been stored in the struct image_info.

linux-user/main.c static int parse_args(int argc, char **argv) · line 4264

Quoted 8 lines
linux-user/main.c +8 -0
+static const char *get_cpu_model(int fd)
+{
+#if defined(TARGET_I386)
+#ifdef TARGET_X86_64
+    return "qemu64";
+#else
+    return "qemu32";
+#endif

This should be our opportunity to split this ifdef chain into small inline functions within linux-user/*/target_cpu.h. Pass the e_flags value directly instead of a file descriptor.

r~

#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] linux-user: introduce functions to detect CPU type

Le 15/01/2018 à 23:04, Richard Henderson a écrit :

Quoted 22 lines · Laurent Vivier
Quoted 8 lines

From: YunQiang Su <[email protected]>

Move CPU type name selection to a function,
and add a function to return ELF e_flags.

[lv: splitted the patch and some cleanup in get_elf_eflags()]

---

This needs to be split.

Quoted 10 lines
linux-user/elfload.c +10 -0
+int get_elf_eflags(int fd, uint32_t *eflags)
+{
+    struct elfhdr ehdr;
+    off_t offset;
+    int ret;
+
+    /* Read ELF header */
+    offset = lseek(fd, 0, SEEK_SET);
+    if (offset == (off_t) -1) {
+        return -1;
Quoted 8 lines

linux-user/elfload.c give_up: · line 2393

Quoted 5 lines
linux-user/elfload.c +5 -0
+    }
+    ret = read(fd, &ehdr, sizeof(ehdr));
+    if (ret < sizeof(ehdr)) {
+        return -1;
+    }

There is no reason to read the elf header twice -- e_flags has already been stored in the struct image_info.

When we set cpu_model, image_info is not initialized.

Do you propose to move cpu_init() after loader_exec()?

Quoted 4 lines
Quoted 4 lines
linux-user/main.c +4 -0
+static const char *get_cpu_model(int fd)
+{
+#if defined(TARGET_I386)
+#ifdef TARGET_X86_64
Quoted 9 lines

linux-user/main.c static int parse_args(int argc, char **argv) · line 4264

Quoted 4 lines
linux-user/main.c +4 -0
+    return "qemu64";
+#else
+    return "qemu32";
+#endif

This should be our opportunity to split this ifdef chain into small inline functions within linux-user/*/target_cpu.h. Pass the e_flags value directly instead of a file descriptor.

Good idea.

Thanks,
Laurent

#4 lore raw

Re: [Qemu-devel] [PATCH 1/3] linux-user: introduce functions to detect CPU type

Quoted 5 lines · Laurent Vivier
Quoted 2 lines

There is no reason to read the elf header twice -- e_flags has already been stored in the struct image_info.

When we set cpu_model, image_info is not initialized. Do you propose to move cpu_init() after loader_exec()?

Sure.

r~

#5 lore raw

Re: [Qemu-devel] [PATCH 1/3] linux-user: introduce functions to detect CPU type

Le 16/01/2018 à 16:54, Richard Henderson a écrit :

Quoted 8 lines · Laurent Vivier
Quoted 5 lines
Quoted 2 lines

There is no reason to read the elf header twice -- e_flags has already been stored in the struct image_info.

When we set cpu_model, image_info is not initialized. Do you propose to move cpu_init() after loader_exec()?

Sure.

After checking, I think we can't move cpu_init() after loader_exec():

load_elf_binary() needs to fill AT_HWCAP and in the case of i386 it depends on cpu->env.features[FEAT_1_EDX] that comes from the cpu model.

Thanks,
Laurent

#6 lore raw

PATCH 2/3 linux-user, m68k: select CPU according to ELF header values

In reply to Laurent Vivier

M680x0 doesn't support the same set of instructions as ColdFire, so we can't use "any" CPU type to execute m68020 instructions.
We select CPU type ("m68020" or "any" for ColdFire) according to the ELF header. If we can't, we use by default the value used until now: "any".

---
 linux-user/main.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
linux-user/main.c +10 -0
index 9ce90ae634..2fc2267fd4 100644
@@ -4267,6 +4267,16 @@ static const char *get_cpu_model(int fd)
 #elif defined(TARGET_UNICORE32)
     return "any";
 #elif defined(TARGET_M68K)
+    int ret;
+    uint32_t eflags;
+
+    ret = get_elf_eflags(fd, &eflags);
+    if (ret == 0 && eflags == 0) {
+        /* 680x0 */
+        return "m68020";
+    }
+
+    /* Coldfire */
     return "any";
 #elif defined(TARGET_SPARC)
 #ifdef TARGET_SPARC64
Signature · 1 line
2.14.3
#7 lore raw

PATCH 3/3 linux-user: MIPS set cpu to r6 CPU if binary is R6

In reply to Laurent Vivier

From: YunQiang Su <[email protected]>

So here we need to detect the version of binaries and set cpu_model for it.

[lv: original patch modified to move code into get_cpu_model()]

---

Notes:
YunQiang Su, please add your Signed-off-by that was missing in your original patch.

 include/elf.h     |  4 ++++
 linux-user/main.c | 10 ++++++++++
 2 files changed, 14 insertions(+)
include/elf.h +4 -0
index e8a515ce3d..f2104809b1 100644
@@ -40,6 +40,10 @@ typedef int64_t  Elf64_Sxword;
 #define EF_MIPS_ARCH_5		0x40000000	/* -mips5 code.  */
 #define EF_MIPS_ARCH_32		0x50000000	/* MIPS32 code.  */
 #define EF_MIPS_ARCH_64		0x60000000	/* MIPS64 code.  */
+#define EF_MIPS_ARCH_32R2       0x70000000      /* MIPS32r2 code.  */
+#define EF_MIPS_ARCH_64R2       0x80000000      /* MIPS64r2 code.  */
+#define EF_MIPS_ARCH_32R6       0x90000000      /* MIPS32r6 code.  */
+#define EF_MIPS_ARCH_64R6       0xa0000000      /* MIPS64r6 code.  */
 
 /* The ABI of a file. */
 #define EF_MIPS_ABI_O32		0x00001000	/* O32 ABI.  */
linux-user/main.c +10 -0
index 2fc2267fd4..3229ef079e 100644
@@ -4285,9 +4285,19 @@ static const char *get_cpu_model(int fd)
     return "Fujitsu MB86904";
 #endif
 #elif defined(TARGET_MIPS)
+    int ret;
+    uint32_t eflags;
+
+    ret = get_elf_eflags(fd, &eflags);
 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
+    if (ret == 0 && (eflags & EF_MIPS_ARCH_64R6) != 0) {
+        return "I6400";
+    }
     return "5KEf";
 #else
+    if (ret == 0 && (eflags & EF_MIPS_ARCH_32R6) != 0) {
+        return "mips32r6-generic";
+    }
     return "24Kf";
 #endif
 #elif defined TARGET_OPENRISC
Signature · 1 line
2.14.3
#8 lore raw

Re: [Qemu-devel] [PATCH 3/3] linux-user: MIPS set cpu to r6 CPU if binary is R6

Quoted 5 lines · Laurent Vivier

From: YunQiang Su <[email protected]>

So here we need to detect the version of binaries and set cpu_model for it.

Quoted 7 lines
[lv: original patch modified to move code into get_cpu_model()]

---

Notes:
YunQiang Su, please add your Signed-off-by that was missing in your original patch.

How to add Signed-off-by? Send a v2 for my version of patch?

Quoted 47 lines
 include/elf.h     |  4 ++++
 linux-user/main.c | 10 ++++++++++
 2 files changed, 14 insertions(+)
include/elf.h +4 -0
index e8a515ce3d..f2104809b1 100644
@@ -40,6 +40,10 @@ typedef int64_t  Elf64_Sxword;
 #define EF_MIPS_ARCH_5         0x40000000      /* -mips5 code.  */
 #define EF_MIPS_ARCH_32                0x50000000      /* MIPS32 code.  */
 #define EF_MIPS_ARCH_64                0x60000000      /* MIPS64 code.  */
+#define EF_MIPS_ARCH_32R2       0x70000000      /* MIPS32r2 code.  */
+#define EF_MIPS_ARCH_64R2       0x80000000      /* MIPS64r2 code.  */
+#define EF_MIPS_ARCH_32R6       0x90000000      /* MIPS32r6 code.  */
+#define EF_MIPS_ARCH_64R6       0xa0000000      /* MIPS64r6 code.  */
 
 /* The ABI of a file. */
 #define EF_MIPS_ABI_O32                0x00001000      /* O32 ABI.  */
linux-user/main.c +10 -1
index 2fc2267fd4..3229ef079e 100644
@@ -4285,9 +4285,19 @@ static const char *get_cpu_model(int fd)
     return "Fujitsu MB86904";
 #endif
 #elif defined(TARGET_MIPS)
+    int ret;
+    uint32_t eflags;
+
+    ret = get_elf_eflags(fd, &eflags);
 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
+    if (ret == 0 && (eflags & EF_MIPS_ARCH_64R6) != 0) {
+        return "I6400";
+    }
     return "5KEf";
 #else
+    if (ret == 0 && (eflags & EF_MIPS_ARCH_32R6) != 0) {
+        return "mips32r6-generic";
+    }
     return "24Kf";
 #endif
 #elif defined TARGET_OPENRISC
--

2.14.3

#9 lore raw

Re: [Qemu-devel] [PATCH 3/3] linux-user: MIPS set cpu to r6 CPU if binary is R6

Le 17/01/2018 à 03:26, YunQiang Su a écrit :

Quoted 10 lines · Laurent Vivier
Quoted 9 lines

From: YunQiang Su <[email protected]>

So here we need to detect the version of binaries and set cpu_model for it.

[lv: original patch modified to move code into get_cpu_model()]

---

Quoted 5 lines
Quoted 3 lines

Notes:
YunQiang Su, please add your Signed-off-by that was missing in your original patch.

How to add Signed-off-by? Send a v2 for my version of patch?

You can reply to your original patch with your "Signed-off-by". And as I did some changes in the following series, it would be good to reply with a "Reviewed-by" to the new patches if you agree with the changes.

Thanks,
Laurent

#10 lore raw

Re: [Qemu-devel] [PATCH 3/3] linux-user: MIPS set cpu to r6 CPU if binary is R6

In reply to YunQiang Su

Le 17/01/2018 à 03:26, YunQiang Su a écrit :

Quoted 10 lines · Laurent Vivier
Quoted 9 lines

From: YunQiang Su <[email protected]>

So here we need to detect the version of binaries and set cpu_model for it.

[lv: original patch modified to move code into get_cpu_model()]

---

Quoted 6 lines
Quoted 3 lines

Notes:
YunQiang Su, please add your Signed-off-by that was missing in your original patch.

How to add Signed-off-by? Send a v2 for my version of patch?

Could you reply to you original patch with your Signed-off-by?

Thanks,
Laurent