Skip to contentSkip to conversation

linux-user/elfload: Implement ELF_HWCAP for RISC-V

PATCH Superseded Kito Cheng 2 participants 2 messages Jul 5, 2021 – Jul 6, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kito Cheng

author
#0 lore raw

PATCH v2 1/1 linux-user/elfload: Implement ELF_HWCAP for RISC-V

Set I, M, A, F, D and C bit for hwcap if misa is set.

V2 Changes:
- Only set imafdc bits, sync with upstream linux kernel.

---
 linux-user/elfload.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
linux-user/elfload.c +29 -1
index 598ab8aa13..3cdc7d06e1 100644
@@ -1426,7 +1426,7 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs,
 #ifdef TARGET_RISCV
 
 #define ELF_START_MMAP 0x80000000
-#define ELF_ARCH  EM_RISCV
+#define ELF_ARCH EM_RISCV
 
 #ifdef TARGET_RISCV32
 #define ELF_CLASS ELFCLASS32
@@ -1434,6 +1434,34 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs,
 #define ELF_CLASS ELFCLASS64
 #endif
 
+#define ELF_HWCAP get_elf_hwcap()
+
+static uint32_t get_elf_hwcap(void)
+{
+    RISCVCPU *cpu = RISCV_CPU(thread_cpu);
+    uint32_t hwcap = 0;
+
+#define MISA_BIT(EXT) (1 << (EXT - 'A'))
+#define GET_EXT(EXT)				\
+    do {					\
+        if (cpu->env.misa & MISA_BIT(EXT)) {	\
+            hwcap |= MISA_BIT(EXT);		\
+        }					\
+    } while (0)
+
+    GET_EXT('I');
+    GET_EXT('M');
+    GET_EXT('A');
+    GET_EXT('F');
+    GET_EXT('D');
+    GET_EXT('C');
+
+#undef MISA_BIT
+#undef GET_EXT
+
+    return hwcap;
+}
+
 static inline void init_thread(struct target_pt_regs *regs,
                                struct image_info *infop)
 {
Signature · 1 line
2.31.1
#1 lore raw
Quoted 9 lines · Kito Cheng
linux-user/elfload.c +9 -0
+static uint32_t get_elf_hwcap(void)
+{
+    RISCVCPU *cpu = RISCV_CPU(thread_cpu);
+    uint32_t hwcap = 0;
+
+#define MISA_BIT(EXT) (1 << (EXT - 'A'))
+#define GET_EXT(EXT)				\
+    do {					\
+        if (cpu->env.misa & MISA_BIT(EXT)) {	\

linux-user/elfload.c static void elf_core_copy_regs(target_elf_gregset_t *regs, · line 1457

Quoted 10 lines
linux-user/elfload.c +10 -0
+            hwcap |= MISA_BIT(EXT);		\
+        }					\
+    } while (0)
+
+    GET_EXT('I');
+    GET_EXT('M');
+    GET_EXT('A');
+    GET_EXT('F');
+    GET_EXT('D');
+    GET_EXT('C');

You're not transforming the bits; there's no reason to be so around-the-bush about this. Just use

    uint32_t mask = MISA_BIT('I') | ...
    return cpu->env.misa & mask;

r~