From patchwork Tue Jan 14 20:24:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Celeste Liu X-Patchwork-Id: 13939457 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 196D7E77188 for ; Tue, 14 Jan 2025 20:25:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A1077280006; Tue, 14 Jan 2025 15:25:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BF74280004; Tue, 14 Jan 2025 15:25:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E9A6280006; Tue, 14 Jan 2025 15:25:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 60520280004 for ; Tue, 14 Jan 2025 15:25:30 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 17845C0A00 for ; Tue, 14 Jan 2025 20:25:30 +0000 (UTC) X-FDA: 83007187620.08.837EE09 Received: from flow-b6-smtp.messagingengine.com (flow-b6-smtp.messagingengine.com [202.12.124.141]) by imf04.hostedemail.com (Postfix) with ESMTP id E47A940014 for ; Tue, 14 Jan 2025 20:25:27 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b=FmrjpT07; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="D kS5xN+"; spf=pass (imf04.hostedemail.com: domain of uwu@coelacanthus.name designates 202.12.124.141 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name; dmarc=pass (policy=quarantine) header.from=coelacanthus.name ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736886328; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WfiGYnphGqvudtD6RXFjAbGTohKPfJnsfNrRsHzlEL4=; b=GjOiEz8wGXJe2hb69CJ3ZhbK673rfw8HrQWMcEyGuwjGAat773NgTmUiEMNghl4zi9ZkMT oPPMc0m85aBlJ1VuNvCkA/9yAkx0isih8yeSw6lzqbfvQ8XLKAzkzZmvoIqAbgcqgJThge 7XQeDpXsWhRt5atqYkfQR4fDVu3kty0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b=FmrjpT07; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="D kS5xN+"; spf=pass (imf04.hostedemail.com: domain of uwu@coelacanthus.name designates 202.12.124.141 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name; dmarc=pass (policy=quarantine) header.from=coelacanthus.name ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736886328; a=rsa-sha256; cv=none; b=6QJ76MGH1tUCuj/9hz3ke2tmj1754hu+6/3ZpQuTQkBxDZtzGuKuW5eqLcvOBTdHltFOBU whZwQC88MmovvSQbCEPTfCRWfFRDBKHctMWqCVM/RfHlNR6LRvfiXIW7TA4FCr+LZPqQNe xFcn+7ISP3BTdMeMrA70hfFmNP2zaEo= Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailflow.stl.internal (Postfix) with ESMTP id 6CA841D40BEE; Tue, 14 Jan 2025 15:25:26 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Tue, 14 Jan 2025 15:25:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= coelacanthus.name; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1736886326; x=1736893526; bh=WfiGYnphGq vudtD6RXFjAbGTohKPfJnsfNrRsHzlEL4=; b=FmrjpT07YapVSFaY1sGnvFnz1X V3Wm4K9jF/8ZFsbjNyEHtlB+iE5FhcNl8xWf+/4OldWNfafM0TAwhl8aCBlTofh8 UqTU7IIEtFUcVJedu8CbGfZ3RYegTh+OlhwwmbCv1bmiELDc1Zyyw0v/k3xAcPTg +cpw3JxrMj3tksGRxA86I2rOXftWQdLa+sao9gXpV0Uki+oqmlXv3j8xhLtYM4kQ yD20dABste7apThlgQ9Sa1qTW9r6YOkPuj4CBEkdlrxFXeAE+XNTIJsDjPBlogSh mLtlzIKSZN94apzID/Y1LN1G1aob8JOf3aMvbvqZN1DyuwhAZiy4+X5v9HEQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1736886326; x= 1736893526; bh=WfiGYnphGqvudtD6RXFjAbGTohKPfJnsfNrRsHzlEL4=; b=D kS5xN+SrokCOeuGkeA0Rdl4Iche77/VAi327Qoasu9ZzmyB87bixfvKkTIMLX3vB nkNc/BinfuUlHmTOzlZZ7hU2L3r+KJ6lmyualOmSRHNVQXzJMqZeG+8PwDRKgS5B gXY9oklDQ4114W1eADDOTQp9U4hqLc6pTWRtdKALxkl7gYqE3Px3P1WbPI8hoyAm vhL0t/blkS+migWR31gbiU+VTYyCWoQ5aPseWYq3+gag/tE1yErRRAUUBxBUlEah DWXimM6yNxZ9eBda4zxRJ8AVZFN832dvhooTghHnOm3qvGXxysl3WpwyGUtJubxl lNn3yDn3jX0LoOJB+limg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrudehiedgudefhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthekredtredt jeenucfhrhhomhepvegvlhgvshhtvgcunfhiuhcuoehufihusegtohgvlhgrtggrnhhthh hushdrnhgrmhgvqeenucggtffrrghtthgvrhhnpeeuiefgudffleffgeeiffeffeefieel veejgfehhfejffeutedvffeiudevtdelgeenucffohhmrghinhepkhgvrhhnvghlrdhorh hgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepuhif uhestghovghlrggtrghnthhhuhhsrdhnrghmvgdpnhgspghrtghpthhtohepvdejpdhmoh guvgepshhmthhpohhuthdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghr rdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehprghlmhgvrhesuggrsggsvghlthdrtg homhdprhgtphhtthhopehfvghlihigohhnmhgrrhhssegrrhgthhhlihhnuhigrdhorhhg pdhrtghpthhtoheplhguvhesshhtrhgrtggvrdhiohdprhgtphhtthhopehtghhlgieslh hinhhuthhrohhnihigrdguvgdprhgtphhtthhopehufihusegtohgvlhgrtggrnhhthhhu shdrnhgrmhgvpdhrtghpthhtohepshhhuhgrhheskhgvrhhnvghlrdhorhhgpdhrtghpth htohepkhgvvghssehkvghrnhgvlhdrohhrghdprhgtphhtthhopeiihhhouhhquhgrnhes ihhstggrshdrrggtrdgtnh X-ME-Proxy: Feedback-ID: i95c648bc:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 14 Jan 2025 15:25:24 -0500 (EST) From: Celeste Liu Date: Wed, 15 Jan 2025 04:24:58 +0800 Subject: [PATCH v5 1/2] riscv/ptrace: add new regset to access original a0 register MIME-Version: 1.0 Message-Id: <20250115-riscv-new-regset-v5-1-d0e6ec031a23@coelacanthus.name> References: <20250115-riscv-new-regset-v5-0-d0e6ec031a23@coelacanthus.name> In-Reply-To: <20250115-riscv-new-regset-v5-0-d0e6ec031a23@coelacanthus.name> To: Oleg Nesterov , Paul Walmsley , Palmer Dabbelt , Eric Biederman , Kees Cook , Shuah Khan , Albert Ou Cc: Alexandre Ghiti , "Dmitry V. Levin" , Andrea Bolognani , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Thomas Gleixner , Ron Economos , Charlie Jenkins , Andrew Jones , Quan Zhou , Felix Yan , Ruizhe Pan , Guo Ren , Yao Zi , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Celeste Liu , stable@vger.kernel.org, =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3197; i=uwu@coelacanthus.name; h=from:subject:message-id; bh=Bd0I0lIMKMC4KxEOjUIKWSGVerkNEic9IL2ALFZ+R48=; b=owJ4nJvAy8zAJeafov85RWVtBeNptSSG9LYTahxei2+ZK7gL31/p1LKEL+S0u9TWg2pWopNOn ch+9jLbmr2jlIVBjItBVkyRJa+E5SfnpbPdezu2d8HMYWUCGcLAxSkAE1nRz8jQulbWYqJDmdS9 yH0Xd0ZtaImSZtoiOvOPRm3rh46tD2JXMjJMt7t61ztq0xrZnQ+9mjfNvhsjKVJToZO/aUXVeeO /8z/yAADK3kiB X-Developer-Key: i=uwu@coelacanthus.name; a=openpgp; fpr=892EBC7DC392DFF9C9C03F1D15F4180E73787863 X-Rspamd-Queue-Id: E47A940014 X-Stat-Signature: er5w61puc9bzzzsgwf7o8166jy3unwxp X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1736886327-669397 X-HE-Meta: U2FsdGVkX19JxrZ6MmjPjL8kNOUNNzO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eCl4/a63W525Vny4H1CovL+glGEoP5tNpv+fSSvP34Yq2BStKFwW4lwpvkSSQ4XdhHWxC/zeJFpIYw71DeF/u/CYylag1c/CXSzWrko414nESNyt5wZhpAZGD9zmkf1dhVqwtH4A50kfjRrF7AJKBNu1QzOF60Nc+E23rSvorobfE48aatnelFkEEp+oVgxuTGQxEdak9WEqW/WKy9jctcnh5k9YhKXOFyog0541WtsnZ4vY1PncafVI+Wt6NMVQBm0825j4ctbudsMaOJPkgCdLYVWBrZPWT5uqFeZm7pg2/u2GjTsmIbYgKDsOLi156pPWlg8tV578HSiH3iKsERalf0VQKaWNmfVO1BjaD6D7RUtXjCeHRZekUW5uRZHeG4oxR3LtFRlVP8EGbkDv7Gg/6NvkkxM4K7PBogyF5YfVHDOyfmGuXsq4zUsBpj+BlJQm9JTOCE/YxbbW1CT4YX0dR38rSuoPjc X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The orig_a0 is missing in struct user_regs_struct of riscv, and there is no way to add it without breaking UAPI. (See Link tag below) Like NT_ARM_SYSTEM_CALL do, we add a new regset name NT_RISCV_ORIG_A0 to access original a0 register from userspace via ptrace API. Fixes: e2c0cdfba7f6 ("RISC-V: User-facing API") Link: https://lore.kernel.org/all/59505464-c84a-403d-972f-d4b2055eeaac@gmail.com/ Cc: stable@vger.kernel.org Reviewed-by: Björn Töpel Signed-off-by: Celeste Liu --- arch/riscv/kernel/ptrace.c | 32 ++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 2 files changed, 33 insertions(+) diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index ea67e9fb7a583683b922fe2c017ea61f3bc848db..ef9ab74c8575a5c440155973b1c625c06a867c97 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -31,6 +31,7 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_SUPM REGSET_TAGGED_ADDR_CTRL, #endif + REGSET_ORIG_A0, }; static int riscv_gpr_get(struct task_struct *target, @@ -184,6 +185,29 @@ static int tagged_addr_ctrl_set(struct task_struct *target, } #endif +static int riscv_orig_a0_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + return membuf_store(&to, task_pt_regs(target)->orig_a0); +} + +static int riscv_orig_a0_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + unsigned long orig_a0 = task_pt_regs(target)->orig_a0; + int ret; + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &orig_a0, 0, -1); + if (ret) + return ret; + + task_pt_regs(target)->orig_a0 = orig_a0; + return 0; +} + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -224,6 +248,14 @@ static const struct user_regset riscv_user_regset[] = { .set = tagged_addr_ctrl_set, }, #endif + [REGSET_ORIG_A0] = { + .core_note_type = NT_RISCV_ORIG_A0, + .n = 1, + .size = sizeof(elf_greg_t), + .align = sizeof(elf_greg_t), + .regset_get = riscv_orig_a0_get, + .set = riscv_orig_a0_set, + }, }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index b44069d29cecc0f9de90ee66bfffd2137f4275a8..390060229601631da2fb27030d9fa2142e676c14 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -452,6 +452,7 @@ typedef struct elf64_shdr { #define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ #define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ #define NT_RISCV_TAGGED_ADDR_CTRL 0x902 /* RISC-V tagged address control (prctl()) */ +#define NT_RISCV_ORIG_A0 0x903 /* RISC-V original a0 register */ #define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */ From patchwork Tue Jan 14 20:24:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Celeste Liu X-Patchwork-Id: 13939458 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15E16C02183 for ; Tue, 14 Jan 2025 20:25:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8890A280004; Tue, 14 Jan 2025 15:25:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 83766280001; Tue, 14 Jan 2025 15:25:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 68A24280004; Tue, 14 Jan 2025 15:25:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 48BDA280001 for ; Tue, 14 Jan 2025 15:25:36 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0670D140D2A for ; Tue, 14 Jan 2025 20:25:36 +0000 (UTC) X-FDA: 83007187872.14.CC18E7E Received: from flow-b6-smtp.messagingengine.com (flow-b6-smtp.messagingengine.com [202.12.124.141]) by imf18.hostedemail.com (Postfix) with ESMTP id D15421C000B for ; Tue, 14 Jan 2025 20:25:33 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b=WgZDBvrE; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="H 5KT5zY"; spf=pass (imf18.hostedemail.com: domain of uwu@coelacanthus.name designates 202.12.124.141 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name; dmarc=pass (policy=quarantine) header.from=coelacanthus.name ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736886333; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=X84OC6j3D/EcHmFCrIItppTILcpGDU3BEqt5Odrm7tQ=; b=B1lBGVnEB/Yz8VqnaBSxVtKxIwGTd7FSzZmCoG4cKZeCHcAEYLiZTVN7P0c38GaI7+jKln x/vL1TG3CQvq+/C/2SbXA4aVRrB0EPQOyEVcrkK53mtZ/SI3GFkMcOe56g16wU0Lkwk05I cCOY+Vav6rfW//NJk1OR9TXdluYE5Q0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736886333; a=rsa-sha256; cv=none; b=hwTbH32jIm7SsVYqavJyNSaD65PNyhNVMyN11U9TSYOQumHhiM4PIRFu1QJGaET3rzZ9eA D0qinu6IopiZfELGs+A7GX9UVfX6XxcvWoxx6xpJ5GmGP3ycuB3dXFcgIBDFenEPbUawUW pzHJaOWI2QyfDIxjPHoU548HJMKzDUs= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b=WgZDBvrE; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="H 5KT5zY"; spf=pass (imf18.hostedemail.com: domain of uwu@coelacanthus.name designates 202.12.124.141 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name; dmarc=pass (policy=quarantine) header.from=coelacanthus.name Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailflow.stl.internal (Postfix) with ESMTP id 6FEFC1D40BF4; Tue, 14 Jan 2025 15:25:32 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Tue, 14 Jan 2025 15:25:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= coelacanthus.name; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1736886332; x=1736893532; bh=X84OC6j3D/ EcHmFCrIItppTILcpGDU3BEqt5Odrm7tQ=; b=WgZDBvrE4myYXVdo8lmC9V7go1 MBNUZAT+PvPqb5oGn6x9Kl2qG1YUJMqvH5XJ+XPxutA33rnDI7pZZZtx57DmYIib YuT54hy8qxi3+XWsBqM7U0e1GLKfGaZleF7Ejy5/5FSWckl4EvWvr8A7AT58wlvG tTWWAKVrLJCC5avhWp0dLXQ6ISFZcvPIaK4g1SRw0RlmPygLLxc2p2zZZ4cT1VDL fP5mjcKT04he4UlDi7KwhjSwqyj0Jkyt9xYb8v7waNYmBNs8rBzw0co39X5iAocG Gom4h+dGGkIgaicrLNfqShZH9+5/y3am1u/BnXWVG7moTaDfTmtrFdFe4hzw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1736886332; x= 1736893532; bh=X84OC6j3D/EcHmFCrIItppTILcpGDU3BEqt5Odrm7tQ=; b=H 5KT5zYVPKM7Nwr0zWxaG2QUwLi0MZsWF61JiqEvq8WKbOPfmItvqOF9o8ZZTb3CA h6VjYCzB9weaEq0/uZVUzwq7HdgWBbhEhs3b9y5y5WRR1hJKyKDndqtuupTHV7gw 6ept+8nqMi0uqejwWxkJsxNYoztaUgZo/1HdVILdsQo1zTa+38qukLR+vOPxbRJG yDLLPHayjZ6Q5Lh367RG/+sUEG5GxW/YIuWfi1oyCz2SiDgeTO3HmEaVpt1l8VpB HnulfY5HvNvtwGRpKfD33Tmdwl6de1+9j3i8jDlrHIeDpAXVEZEiy1j3wIa2L9Og PVe2hYZIVkt4C/jya4Qew== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrudehiedgudefhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthekredtredt jeenucfhrhhomhepvegvlhgvshhtvgcunfhiuhcuoehufihusegtohgvlhgrtggrnhhthh hushdrnhgrmhgvqeenucggtffrrghtthgvrhhnpeeiudegfffhtdeuuddufefhfefhheej jefhueekhefffeeugfeuveetieeuvddtfeenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehufihusegtohgvlhgrtggrnhhthhhushdrnhgrmhgv pdhnsggprhgtphhtthhopedviedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplh hinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohep phgrlhhmvghrsegurggssggvlhhtrdgtohhmpdhrtghpthhtohepfhgvlhhigihonhhmrg hrshesrghrtghhlhhinhhugidrohhrghdprhgtphhtthhopehlughvsehsthhrrggtvgdr ihhopdhrtghpthhtohepthhglhigsehlihhnuhhtrhhonhhigidruggvpdhrtghpthhtoh epuhifuhestghovghlrggtrghnthhhuhhsrdhnrghmvgdprhgtphhtthhopehshhhurghh sehkvghrnhgvlhdrohhrghdprhgtphhtthhopehkvggvsheskhgvrhhnvghlrdhorhhgpd hrtghpthhtohepiihhohhuqhhurghnsehishgtrghsrdgrtgdrtghn X-ME-Proxy: Feedback-ID: i95c648bc:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 14 Jan 2025 15:25:29 -0500 (EST) From: Celeste Liu Date: Wed, 15 Jan 2025 04:24:59 +0800 Subject: [PATCH v5 2/2] riscv: selftests: Add a ptrace test to verify a0 and orig_a0 access MIME-Version: 1.0 Message-Id: <20250115-riscv-new-regset-v5-2-d0e6ec031a23@coelacanthus.name> References: <20250115-riscv-new-regset-v5-0-d0e6ec031a23@coelacanthus.name> In-Reply-To: <20250115-riscv-new-regset-v5-0-d0e6ec031a23@coelacanthus.name> To: Oleg Nesterov , Paul Walmsley , Palmer Dabbelt , Eric Biederman , Kees Cook , Shuah Khan , Albert Ou Cc: Alexandre Ghiti , "Dmitry V. Levin" , Andrea Bolognani , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Thomas Gleixner , Ron Economos , Charlie Jenkins , Andrew Jones , Quan Zhou , Felix Yan , Ruizhe Pan , Guo Ren , Yao Zi , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Celeste Liu , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=7877; i=uwu@coelacanthus.name; h=from:subject:message-id; bh=JHyWpvTLVV/62CjvMtPU1ES86qpvTbLYXerWGMWdLcU=; b=kA0DAAoWT2Qv82QkrXgByyZiAGeGyCiiQ/UipLwCo1ZxlhWCvh+tEMPj8P8eGMLet11Mo7+mP Yh1BAAWCgAdFiEEbnQE+QnSzYu9iLeKT2Qv82QkrXgFAmeGyCgACgkQT2Qv82QkrXgtRAD8DQ3t m4IYDX1buEhi22FOFeG2lTxpLFGOsmFktIorlmEA/ioVOvQeEyBhz+AI3kUPJbx96RWmT6t9/op IjMuhOLIC X-Developer-Key: i=uwu@coelacanthus.name; a=openpgp; fpr=892EBC7DC392DFF9C9C03F1D15F4180E73787863 X-Rspamd-Queue-Id: D15421C000B X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: 88woixsn7s4zqhw7quphptrwae6pf1nz X-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ox5Ujb2PesDhCVDWkcxBzjsIE6drMXP1l0CmT+On8O3y9plsoyTHVjpF/hdo/g1bV0thF6mhRF56xppstd/C+WmfozFByTAW8wii4SfGixxa4kPpx28XjyDeQG5irDJrfuxfY+gQx+L9Rk6UOaziK9toLEPjkiC0P+e/mIrWZzv1A4RCD3x4E1ay2QN7hs2mCcZ90fkSxLZTXsjrHd/plG9jU01bKqhJpB1K8MC7DwmC9CfucikRQaxAVU0v3sC0e0LYr8VFjodQalwh44k7dcwo19DPMnYzG6sI0hmdey+wMQouadplb7MXhBiL4jrpRu6veI2UktRG+uHYStiIVap7mcTntHnYhoPfCL3zJYyk/krjortF0P9jTC16L4xa2Ogu4UYWy0VPTxri2F+gl7uoCAY2JdBUJ8ljCQkSoFKpnkvcWp4TvQH/XyIGQS8mqxUhetBFeqeCphMuYu1klg3xgKd+ZrsyhxSyvrRybvJLUl1SKiYXZVNzylrLmgOdzfolqQ160w3PUf+Qs9EVL83nQ2pyYu X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This test checks that orig_a0 and a0 can be modified and accessed independently. Co-developed-by: Quan Zhou Signed-off-by: Quan Zhou Co-developed-by: Charlie Jenkins Signed-off-by: Charlie Jenkins Reviewed-by: Björn Töpel Signed-off-by: Celeste Liu --- tools/testing/selftests/riscv/abi/.gitignore | 1 + tools/testing/selftests/riscv/abi/Makefile | 6 +- tools/testing/selftests/riscv/abi/ptrace.c | 201 +++++++++++++++++++++++++++ 3 files changed, 207 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/riscv/abi/.gitignore b/tools/testing/selftests/riscv/abi/.gitignore index b38358f91c4d2240ae64892871d9ca98bda1ae58..378c605919a3b3d58eec2701eb7af430cfe315d6 100644 --- a/tools/testing/selftests/riscv/abi/.gitignore +++ b/tools/testing/selftests/riscv/abi/.gitignore @@ -1 +1,2 @@ pointer_masking +ptrace diff --git a/tools/testing/selftests/riscv/abi/Makefile b/tools/testing/selftests/riscv/abi/Makefile index ed82ff9c664e7eb3f760cbab81fb957ff72579c5..359a082c88a401883fb3776b35e4dacf69beaaaa 100644 --- a/tools/testing/selftests/riscv/abi/Makefile +++ b/tools/testing/selftests/riscv/abi/Makefile @@ -1,10 +1,14 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS += -I$(top_srcdir)/tools/include +CFLAGS += $(KHDR_INCLUDES) -TEST_GEN_PROGS := pointer_masking +TEST_GEN_PROGS := pointer_masking ptrace include ../../lib.mk $(OUTPUT)/pointer_masking: pointer_masking.c $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ + +$(OUTPUT)/ptrace: ptrace.c + $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/abi/ptrace.c b/tools/testing/selftests/riscv/abi/ptrace.c new file mode 100644 index 0000000000000000000000000000000000000000..f1a0458adccdd040bfaa350e2e8d98b1ef34c0ad --- /dev/null +++ b/tools/testing/selftests/riscv/abi/ptrace.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest_harness.h" + +#ifndef sizeof_field +#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) +#endif +#ifndef offsetofend +#define offsetofend(TYPE, MEMBER) \ + (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) +#endif + + +#define ORIG_A0_MODIFY 0x01 +#define A0_MODIFY 0x02 +#define A0_OLD 0xbadbeefbeeff +#define A0_NEW 0xffeebfeebdab + + +struct a0_regs { + __s64 orig_a0; + __u64 a0; +}; + +#define perr_and_exit(fmt, ...) \ + ({ \ + char buf[256]; \ + snprintf(buf, sizeof(buf), "%s:%d:" fmt ": %m\n", \ + __func__, __LINE__, ##__VA_ARGS__); \ + ksft_exit_fail_perror(buf); \ + }) + +static void ptrace_test(int opt, struct a0_regs result[]) +{ + int status; + long rc; + pid_t pid; + struct user_regs_struct regs; + struct iovec iov = { + .iov_base = ®s, + .iov_len = sizeof(regs), + }; + + unsigned long orig_a0; + struct iovec a0_iov = { + .iov_base = &orig_a0, + .iov_len = sizeof(orig_a0), + }; + struct ptrace_syscall_info syscall_info = { + .op = 0xff, + }; + const unsigned int expected_sci_entry_size = + offsetofend(struct ptrace_syscall_info, entry.args); + const unsigned int expected_sci_exit_size = + offsetofend(struct ptrace_syscall_info, exit.is_error); + + pid = fork(); + if (pid == 0) { + /* Mark oneself being traced */ + long val = ptrace(PTRACE_TRACEME, 0, 0, 0); + + if (val < 0) + perr_and_exit("failed to request for tracer to trace me: %ld", val); + + kill(getpid(), SIGSTOP); + + /* Perform chdir syscall that will be intercepted */ + syscall(__NR_chdir, A0_OLD); + + exit(0); + } + + if (pid < 0) + ksft_exit_fail_perror("failed to fork"); + + for (int i = 0; i < 3; i++) { + if (waitpid(pid, &status, 0) != pid) + perr_and_exit("failed to wait for the tracee %d", pid); + if (WIFSTOPPED(status)) { + switch (WSTOPSIG(status)) { + case SIGSTOP: + if (ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACESYSGOOD) < 0) + perr_and_exit("failed to set PTRACE_O_TRACESYSGOOD"); + break; + case SIGTRAP|0x80: + /* Modify twice so GET_SYSCALL_INFO get modified a0 and orig_a0 */ + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + perr_and_exit("failed to get tracee registers"); + if (ptrace(PTRACE_GETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov)) + perr_and_exit("failed to get tracee registers"); + + /* Modify a0/orig_a0 for the syscall */ + switch (opt) { + case A0_MODIFY: + regs.a0 = A0_NEW; + break; + case ORIG_A0_MODIFY: + orig_a0 = A0_NEW; + break; + } + + if (ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iov)) + perr_and_exit("failed to set tracee registers"); + if (ptrace(PTRACE_SETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov)) + perr_and_exit("failed to set tracee registers"); + switch (i) { + case 1: + /* Stop at the beginning of syscall */ + rc = ptrace(PTRACE_GET_SYSCALL_INFO, pid, + sizeof(syscall_info), &syscall_info); + if (rc < 0) + perr_and_exit("failed to get syscall info of entry"); + if (rc < expected_sci_entry_size + || syscall_info.op != PTRACE_SYSCALL_INFO_ENTRY) + perr_and_exit("stop position of entry mismatched"); + result[0].orig_a0 = syscall_info.entry.args[0]; + break; + + case 2: + /* Stop at the end of syscall */ + rc = ptrace(PTRACE_GET_SYSCALL_INFO, pid, + sizeof(syscall_info), &syscall_info); + if (rc < 0) + perr_and_exit("failed to get syscall info of entry"); + if (rc < expected_sci_exit_size + || syscall_info.op != PTRACE_SYSCALL_INFO_EXIT) + perr_and_exit("stop position of exit mismatched"); + result[0].a0 = syscall_info.exit.rval; + + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + perr_and_exit("failed to get tracee registers"); + result[1].a0 = regs.a0; + if (ptrace(PTRACE_GETREGSET, pid, NT_RISCV_ORIG_A0, + &a0_iov)) + perr_and_exit("failed to get tracee registers"); + result[1].orig_a0 = orig_a0; + } + } + if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) + perr_and_exit("failed to resume tracee"); + } + } + + /* Resume the tracee */ + ptrace(PTRACE_CONT, pid, 0, 0); + if (waitpid(pid, &status, 0) != pid) + perr_and_exit("failed to wait for the tracee"); + +} + +TEST(ptrace_access_a0) +{ + struct a0_regs result[2]; + + ptrace_test(A0_MODIFY, result); + + /* Verify PTRACE_SETREGSET */ + /* The modification of a0 cannot affect the first argument of the syscall */ + EXPECT_EQ(A0_OLD, result[0].orig_a0); + EXPECT_EQ(A0_NEW, result[0].a0); + + /* Verify PTRACE_GETREGSET */ + EXPECT_EQ(result[1].orig_a0, result[0].orig_a0); + EXPECT_EQ(result[1].a0, result[0].a0); +} + +TEST(ptrace_access_orig_a0) +{ + struct a0_regs result[2]; + + ptrace_test(ORIG_A0_MODIFY, result); + + /* Verify PTRACE_SETREGSET */ + /* Only modify orig_a0 to change the first argument of the syscall */ + EXPECT_EQ(A0_NEW, result[0].orig_a0); + /* a0 will keep default value, orig_a0 or -ENOSYS, depends on internal. */ + EXPECT_NE(A0_NEW, result[0].a0); + + /* Verify PTRACE_GETREGSET */ + EXPECT_EQ(result[1].orig_a0, result[0].orig_a0); + EXPECT_EQ(result[1].a0, result[0].a0); +} + +TEST_HARNESS_MAIN