From patchwork Thu Jun 27 03:02:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quan Zhou X-Patchwork-Id: 13713772 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 B1736C27C4F for ; Thu, 27 Jun 2024 03:02:56 +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=kh5I2G6sXRDE1O9c9eS6nAKBHWQ4yhA0Nv3btt0i9P8=; b=ioYJ8gJI7BbyM8 xRygRYri9mL1JDJtR7MsBlvMkhpqD+h1JQaxSba5esX6aomev2yDEEDqUJpMynDaSSCNWOMsOP+/N N/BtxngFmIamEwFoVGFZDwDLXzr9+3XRxJMwDijtnTCnFkm3o14y6WmEz+WesN3RcajFI4PkSIfNy O20DfMeX4FM8zJXM5HXEeVt4andM4Y9Am5zggo//yoRDUTad5mvkCHJSjKeJcx6+XpU0Ya/on9H+I axnggamXvDV/rN9PW/038kY/yYXsNwkRyhxcNqAHyrvt5O1ivP9HyrSM1g63a/qfdj7NVPQevXWy6 wiOFGdiCF/Oukb2BSQjg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMfPJ-000000093ar-2EI1; Thu, 27 Jun 2024 03:02:45 +0000 Received: from smtp84.cstnet.cn ([159.226.251.84] helo=cstnet.cn) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMfPE-000000093ZN-37Io for linux-riscv@lists.infradead.org; Thu, 27 Jun 2024 03:02:43 +0000 Received: from ThinkPad-T480s.. (unknown [180.110.114.157]) by APP-05 (Coremail) with SMTP id zQCowACXneU91nxmmgIiAA--.7124S2; Thu, 27 Jun 2024 11:02:23 +0800 (CST) From: zhouquan@iscas.ac.cn To: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: oleg@redhat.com, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, andy.chiu@sifive.com, shuah@kernel.org, charlie@rivosinc.com, zhouquan@iscas.ac.cn Subject: [PATCH v1 0/2] riscv: Expose orig_a0 to userspace for ptrace to set the actual a0 Date: Thu, 27 Jun 2024 11:02:21 +0800 Message-Id: X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CM-TRANSID: zQCowACXneU91nxmmgIiAA--.7124S2 X-Coremail-Antispam: 1UD129KBjvJXoW7tF43Ar1fXryUtFW7GFWDCFg_yoW8tFy3pa 95Kwn8Kr1kJFy7t3WxXw4UZrWrAa4kGrW3G3WxZw13Z3y0yryvqr4xKa15t3sxA3y8XryS vF12k3W5C3W7Aa7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvK14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r4j6ryUM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4j 6F4UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Cr 1j6rxdM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj 6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr 0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E 8cxan2IY04v7M4kE6xkIj40Ew7xC0wCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbV WUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF 67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42 IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF 0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxh VjvjDU0xZFpf9x0JUSiihUUUUU= X-Originating-IP: [180.110.114.157] X-CM-SenderInfo: 52kr31xxdqqxpvfd2hldfou0/1tbiCQ4BBmZ8nayl6gAAsk X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240626_200241_208130_6692B9B2 X-CRM114-Status: GOOD ( 10.24 ) 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: Quan Zhou Due to the path that modifies a0 in syscall_enter_from_user_mode before the actual execution of syscall_handler [1], the kernel currently saves a0 to orig_a0 at the entry point of do_trap_ecall_u as an original copy of a0. Once the syscall is interrupted and later resumed, the restarted syscall will use orig_a0 to continue execution. The above rules generally apply except for ptrace(PTRACE_SETREGSET,), where the kernel will ignore the tracer's setting of tracee/a0 and will restart with the tracee/orig_a0. For the current kernel implementation of ptrace, projects like CRIU/Proot will encounter issues where the a0 setting becomes ineffective when performing ptrace(PTRACE_SETREGSET,). Here is a suggested solution, expose orig_a0 to userspace so that ptrace can choose whether to set orig_a0 based on the actual scenario. In fact, x86/orig_eax and loongArch/orig_a0 have adopted similar solutions. [1] link: https://lore.kernel.org/lkml/20230403-crisping-animosity-04ed8a45c625@spud/T/ --- Changes from RFC->v1: - Rebased on Linux 6.10-rc5. - Updated the patch description. - Adjust MAX_REG_OFFSET to match the new bottom of pt_regs (Charlie). - Simplify selftest to verify if a0 can be set (Charlie). - Fix .gitignore error (Charlie). --- RFC link: https://lore.kernel.org/all/cover.1718693532.git.zhouquan@iscas.ac.cn/ Quan Zhou (2): riscv: Expose orig_a0 in the user_regs_struct structure riscv: selftests: Add a ptrace test to verify syscall parameter modification arch/riscv/include/asm/ptrace.h | 7 +- arch/riscv/include/uapi/asm/ptrace.h | 2 + tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/abi/.gitignore | 1 + tools/testing/selftests/riscv/abi/Makefile | 12 ++ tools/testing/selftests/riscv/abi/ptrace.c | 124 +++++++++++++++++++ 6 files changed, 144 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/riscv/abi/.gitignore create mode 100644 tools/testing/selftests/riscv/abi/Makefile create mode 100644 tools/testing/selftests/riscv/abi/ptrace.c base-commit: f2661062f16b2de5d7b6a5c42a9a5c96326b8454