From patchwork Mon Apr 3 06:52:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 13197695 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 11BFBC76196 for ; Mon, 3 Apr 2023 06:52:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=hQNXHQkME8JkxUPVYzDj+ClvCfU64oIIC9bZ2NIb49U=; b=2YtstTYvh4stX6 g4+vplpvN5FhbwkGkumxK6GaeiNyTUZFYEg2GK+ZyFxIuUY62extfUY7wOTvosQo+/TEUMAkUH5Ln rHQVjxazaFijpw+r89+hbzDs4dLCnBLK3r2z4AoXUqt0s0i1rzgQyJoBU5pGUyjp7/J4WxPPQcjUc F2SCCDQ72cn91jhO1bPqtCyWB9BmIiahdGAMdB+1tj/q9qXpaKVthumm+r90votNC/WyGzO+s6Z8m ItUybXMD6B3sNHQb31gY+lMfF2rHx8RmQ65l0VlIut7mhkC6UEZz60aHaLrKmTuFiEi/BrQckAigp xvrz4sG6h9czwYxvek1w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pjE39-00EIqG-0c; Mon, 03 Apr 2023 06:52:19 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pjE36-00EIpA-23 for linux-riscv@lists.infradead.org; Mon, 03 Apr 2023 06:52:17 +0000 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 245986133F; Mon, 3 Apr 2023 06:52:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4493EC433EF; Mon, 3 Apr 2023 06:52:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680504734; bh=a/R10jH30vYnh+1wVVgBZesVEWx9VEabSFxoWE4WGaQ=; h=From:To:Cc:Subject:Date:From; b=PZjCGGdy918fsCIV2X7ji6gb3Dy7kLUZvge78w6piYQ0DQT26vfQkZHg1ffZkorWO Ep6okNEzf7o5LlKEoBg1ercBGYLyUlE4CNG9/sJ/RlIkr7ADq0Yeov3preu9D67xGd A/Fkj3TPKWa+uU/fkqkeeIODnngpg6TYD0M94porHImd6B20kTGQeIwW9yPuATQd27 pg+kp41sRKtr+SaqbATMRdx4khZSNtThQnE0EwW89wGCNUmnquYEW/HQnjl9W2wCLf O9HhPb7WXLGbnP62cMrVTQui4zNO6YrU+zxA5HVieNzHsMNOzS7f71gUYvMGFKbrcs 0vPeN8NRU/FWw== From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: Albert Ou , Paul Walmsley , Palmer Dabbelt , linux-riscv@lists.infradead.org, Guo Ren Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , =?utf-8?q?Heiko_S?= =?utf-8?q?t=C3=BCbner?= , Conor Dooley , Yipeng Zou , linux-kernel@vger.kernel.org, Palmer Dabbelt , Heiko Stuebner Subject: [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode() Date: Mon, 3 Apr 2023 08:52:07 +0200 Message-Id: <20230403065207.1070974-1-bjorn@kernel.org> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230402_235216_734801_E5B560AD X-CRM114-Status: GOOD ( 12.13 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Björn Töpel The RISC-V calling convention passes the first argument, and the return value in the a0 register. For this reason, the a0 register needs some extra care; When handling syscalls, the a0 register is saved into regs->orig_a0, so a0 can be properly restored for, e.g. interrupted syscalls. This functionality was broken with the introduction of the generic entry patches. Here, a0 was saved into orig_a0 after calling syscall_enter_from_user_mode(), which can change regs->a0 for some paths, incorrectly restoring a0. This is resolved, by saving a0 prior doing the syscall_enter_from_user_mode() call. Fixes: f0bddf50586d ("riscv: entry: Convert to generic entry") Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner Signed-off-by: Björn Töpel Reported-by: Conor Dooley Reviewed-by: Conor Dooley Tested-by: Conor Dooley Tested-by: Geert Uytterhoeven Tested-by: Andy Chiu --- arch/riscv/kernel/traps.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) base-commit: d34a6b715a23ccd9c9d0bc7a475bea59dc3e28b2 diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 1f4e37be7eb3..8c258b78c925 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -270,11 +270,11 @@ asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs) if (user_mode(regs)) { ulong syscall = regs->a7; - syscall = syscall_enter_from_user_mode(regs, syscall); - regs->epc += 4; regs->orig_a0 = regs->a0; + syscall = syscall_enter_from_user_mode(regs, syscall); + if (syscall < NR_syscalls) syscall_handler(regs, syscall); else