Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2205323pxb; Thu, 3 Feb 2022 01:26:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJyLAa1ji+BWrbUKrOjsZH3EGo3NchW5NuprREMoEidXHrNUZdB3rFoGNNhovFRfcUc6qegK X-Received: by 2002:a17:907:7ba3:: with SMTP id ne35mr20009677ejc.128.1643880383328; Thu, 03 Feb 2022 01:26:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643880383; cv=none; d=google.com; s=arc-20160816; b=SY8VsPElFbL+060zkkBJE9qZjeRud16DUy5oIsMTbIF2SPGfT+eXCRLKOJvdq33KHS TheulILCn3bVIEFm2jgj5Et4ILYDOv9zDwDUHGyPhYfPQO+p77PLcq+Zh6hwsmKrD8Zp CzabnURFA6eNWCjkL6kndktTQGHkN2L74uUFfWgbGDbyA8f2j/CVcngV40XrjOaT7dsb 7VwwwUlvv2mECEkgzrx94wsXUyyV+AP8prvP2xbjoLXyu4xkZtb3DPqVFKgjBb0d61vr ckSUSmk8JsBstDrAge09iHYvBWDerUCkF0Yt1rOpd2zqlfIm3RXN1QV3ec2YUtpvPE1b R95g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=r+4E/fSzlNuiMRHFPDfz0YohKEilosrFTYTXs9bImh4=; b=qGAJoi4mCDhVpBHkBiMHTnIjG49WJUazzwYzAPzdaLROfkzhnzImA4KyNb0eVMs7ZY XxASHwBXlWBlQZgA87h53oBcLrvAODrbgPfPvl9WvwnjetJY3KBxaZDUu2uEadJkLI2T iBYK2MKIcWgAIFe1+1/gMgUE3ePu6UVb6LVN5NDu2z6d7yFtQaWUnbXVZ3mzEOgNZQAw +tsLFQwXvhs/PWWgeNb4dLSn0r/YHx3AY0Dt+hqm9e4NynZmfdX20oKYhRJHMyAPCPB8 zXzO1WvdHWVRWMlPJdcDTLuLPQVpEVl7RnRGwTXghTR3dUuwb3mrejgTnqLxdcAERIxi IFHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RMmlxp4U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hb34si13640868ejc.932.2022.02.03.01.25.57; Thu, 03 Feb 2022 01:26:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RMmlxp4U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240077AbiBAPHi (ORCPT + 99 others); Tue, 1 Feb 2022 10:07:38 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:34548 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240031AbiBAPHc (ORCPT ); Tue, 1 Feb 2022 10:07:32 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 961A96167D; Tue, 1 Feb 2022 15:07:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 06EBBC340EF; Tue, 1 Feb 2022 15:07:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643728051; bh=HEM6wUjW/YQojqR94RrLp8qy+d2K2OqnXt44dGkca70=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RMmlxp4UJPsMtpXs87e4s6xiaJ80wAU96MOJZHI+WK5FXJjACE6sOA7mhIezCSDbn cVVy6Dpx1UPePeJgda2QrNUuIhD13UX9Ddx/gkxzZTOM7iBDVsQyAq9f5beTjmcN3M ZEsl2BI5891/5TdJmWhqOQneQUhuuoMklcHjp1EICjRMX/B2dRNawxC6X9flryBXEf UMGy0guzFd496xjEzWLCKPNxsUOAih/7+/NVQjvbaYR82M2ad4UoxMpNqKkOqcIgL9 V5SdYuK4vcL1DcazWpHElMSfus+Gl4xAJ+/euvJde///B2VWJ2F4xHnWOobqXtVLoB Ojq7wWGiVAZow== From: guoren@kernel.org To: guoren@kernel.org, palmer@dabbelt.com, arnd@arndb.de, anup@brainfault.org, gregkh@linuxfoundation.org, liush@allwinnertech.com, wefu@redhat.com, drew@beagleboard.org, wangjunqiang@iscas.ac.cn, hch@lst.de Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-csky@vger.kernel.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-parisc@vger.kernel.org, linux-mips@vger.kernel.org, linux-arm-kernel@lists.infradead.org, x86@kernel.org, Guo Ren Subject: [PATCH V5 15/21] riscv: compat: Add hw capability check for elf Date: Tue, 1 Feb 2022 23:05:39 +0800 Message-Id: <20220201150545.1512822-16-guoren@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220201150545.1512822-1-guoren@kernel.org> References: <20220201150545.1512822-1-guoren@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guo Ren 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 Signed-off-by: Guo Ren Signed-off-by: Guo Ren Cc: Arnd Bergmann Cc: Christoph Hellwig --- arch/riscv/include/asm/elf.h | 3 ++- arch/riscv/kernel/process.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/elf.h b/arch/riscv/include/asm/elf.h index aee40040917b..3a4293dc7229 100644 --- a/arch/riscv/include/asm/elf.h +++ b/arch/riscv/include/asm/elf.h @@ -40,7 +40,8 @@ * elf64_hdr e_machine's offset are different. The checker is * a little bit simple compare to other architectures. */ -#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) diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 1a666ad299b4..758847cba391 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -83,6 +83,38 @@ void show_regs(struct pt_regs *regs) dump_backtrace(regs, NULL, KERN_DEFAULT); } +#ifdef CONFIG_COMPAT +static bool compat_mode_support __read_mostly; + +bool compat_elf_check_arch(Elf32_Ehdr *hdr) +{ + if (compat_mode_support && (hdr->e_machine == EM_RISCV)) + return true; + else + return false; +} + +static int compat_mode_detect(void) +{ + unsigned long tmp = csr_read(CSR_STATUS); + + csr_write(CSR_STATUS, (tmp & ~SR_UXL) | SR_UXL_32); + + if ((csr_read(CSR_STATUS) & SR_UXL) != SR_UXL_32) { + pr_info("riscv: 32bit compat mode detect failed\n"); + compat_mode_support = false; + } else { + compat_mode_support = true; + pr_info("riscv: 32bit compat mode detected\n"); + } + + csr_write(CSR_STATUS, tmp); + + return 0; +} +arch_initcall(compat_mode_detect); +#endif + void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp) { -- 2.25.1