From nobody Tue Feb 10 20:46:44 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D71BC76196 for ; Mon, 3 Apr 2023 06:52:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231461AbjDCGwT (ORCPT ); Mon, 3 Apr 2023 02:52:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229797AbjDCGwQ (ORCPT ); Mon, 3 Apr 2023 02:52:16 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEE4F2727 for ; Sun, 2 Apr 2023 23:52:15 -0700 (PDT) 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 2FA50614FB for ; 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?q?Bj=C3=B6rn=20T=C3=B6pel?= To: Albert Ou , Paul Walmsley , Palmer Dabbelt , linux-riscv@lists.infradead.org, Guo Ren Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , =?UTF-8?q?Heiko=20St=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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bj=C3=B6rn T=C3=B6pel 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=C3=B6rn T=C3=B6pel Reported-by: Conor Dooley Reviewed-by: Conor Dooley Tested-by: Andy Chiu Tested-by: Conor Dooley Tested-by: Geert Uytterhoeven --- arch/riscv/kernel/traps.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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_ecal= l_u(struct pt_regs *regs) if (user_mode(regs)) { ulong syscall =3D regs->a7; =20 - syscall =3D syscall_enter_from_user_mode(regs, syscall); - regs->epc +=3D 4; regs->orig_a0 =3D regs->a0; =20 + syscall =3D syscall_enter_from_user_mode(regs, syscall); + if (syscall < NR_syscalls) syscall_handler(regs, syscall); else base-commit: d34a6b715a23ccd9c9d0bc7a475bea59dc3e28b2 --=20 2.37.2