riscv: compat: Add hw capability check for elf
authorGuo Ren <guoren@linux.alibaba.com>
Tue, 5 Apr 2022 07:13:09 +0000 (15:13 +0800)
committerPalmer Dabbelt <palmer@rivosinc.com>
Tue, 26 Apr 2022 20:36:47 +0000 (13:36 -0700)
Detect hardware COMPAT (32bit U-mode) capability in rv64. If not
support COMPAT mode in hw, compat_elf_check_arch would return
false by compat_binfmt_elf.c

Add CLASS to enhance (compat_)elf_check_arch to distinguish
32BIT/64BIT elf.

Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
Signed-off-by: Guo Ren <guoren@kernel.org>
Tested-by: Heiko Stuebner <heiko@sntech.de>
Link: https://lore.kernel.org/r/20220405071314.3225832-16-guoren@kernel.org
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/include/asm/elf.h
arch/riscv/kernel/process.c

index a234656..754fdb8 100644 (file)
 /*
  * This is used to ensure we don't load something for the wrong architecture.
  */
-#define elf_check_arch(x) ((x)->e_machine == EM_RISCV)
+#define elf_check_arch(x) (((x)->e_machine == EM_RISCV) && \
+                          ((x)->e_ident[EI_CLASS] == ELF_CLASS))
 
-#define compat_elf_check_arch(x) ((x)->e_machine == EM_RISCV)
+extern bool compat_elf_check_arch(Elf32_Ehdr *hdr);
+#define compat_elf_check_arch  compat_elf_check_arch
 
 #define CORE_DUMP_USE_REGSET
 #define ELF_EXEC_PAGESIZE      (PAGE_SIZE)
index b4421c1..1c7be86 100644 (file)
@@ -84,6 +84,34 @@ void show_regs(struct pt_regs *regs)
                dump_backtrace(regs, NULL, KERN_DEFAULT);
 }
 
+#ifdef CONFIG_COMPAT
+static bool compat_mode_supported __read_mostly;
+
+bool compat_elf_check_arch(Elf32_Ehdr *hdr)
+{
+       return compat_mode_supported &&
+              hdr->e_machine == EM_RISCV &&
+              hdr->e_ident[EI_CLASS] == ELFCLASS32;
+}
+
+static int __init compat_mode_detect(void)
+{
+       unsigned long tmp = csr_read(CSR_STATUS);
+
+       csr_write(CSR_STATUS, (tmp & ~SR_UXL) | SR_UXL_32);
+       compat_mode_supported =
+                       (csr_read(CSR_STATUS) & SR_UXL) == SR_UXL_32;
+
+       csr_write(CSR_STATUS, tmp);
+
+       pr_info("riscv: ELF compat mode %s",
+                       compat_mode_supported ? "supported" : "failed");
+
+       return 0;
+}
+early_initcall(compat_mode_detect);
+#endif
+
 void start_thread(struct pt_regs *regs, unsigned long pc,
        unsigned long sp)
 {