From patchwork Mon Mar 22 22:50:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 12156285 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6E46C433C1 for ; Mon, 22 Mar 2021 22:55:14 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3743961984 for ; Mon, 22 Mar 2021 22:55:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3743961984 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: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:List-Owner; bh=uGldS0LBNXZ3zOgphb2UoZyYLWtapd6k+3oa4QbneR8=; b=bCAwMtpjevr5YBloh0ZOFRy0f Drp9O5hylm7lNsfBgAfxzHOyNYkpDLHcpElaXvde3tLmd7N9m2Hao4kjZbPUrdncQo0TnGJX1SKh7 tglbCq1yBwvm7CoX9zaby1Mr9hWv5cltmdoOC5ZT3j3M8YG4jnlekxZhsmyOH0lyTsEmpfiT6GDpa RPKK0DRgLqfFyBdp2to0a5WxDilXgOsj9mNP1rpqTMfoPPaNEJJtgT0afsqzo7Q8JwKlfP2fxO8Vc 6/pDLE7LH/V+bN/vGDD4Vc2Xalk8iyFvjmwG7FdnvEASFKC/ztxjAJKg+tJXJfkJA2tisU5Wpgg5F Pt0ZVBN/g==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQP-00ClCw-IU; Mon, 22 Mar 2021 22:53:29 +0000 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQD-00Cl9E-Bq for linux-arm-kernel@lists.infradead.org; Mon, 22 Mar 2021 22:53:19 +0000 Received: by mail-pf1-x431.google.com with SMTP id l3so12254658pfc.7 for ; Mon, 22 Mar 2021 15:53:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=r+51m+w++y282pEpZcxM+4YOYzwIDkEbVJ47KIbTcl4=; b=QfXGEo5ueK2zr6oSJZYJYzRNkYnWQhD8Sox7sDDDttvRWM3EmyOUol7F4aqSEgP9NP GHQnkPJruXk3pdfbHAo3tir2mS9ZA+cqMqUYRBtFasSBiTyNXM6kW5xisTwJ4HKI0/SG Uhh0LjBwDYD72X3FfxRExA1v97vyHlTnxd8Twrvz/8nr5vPakPsWmkvzpeDp1FJzmMut ndWh0ftdPenGyc2vKPW3wQPc7/rAEs2LD5kFnJsANnSapLQ/Hp5PT+1MbvPgSryZe3FR bBzJrrxt8kQtMJG2L0bksMtfdja64mazMcYG7Hi2tzHeSn76MoHgnEYU1Piswjozpi5E rybw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=r+51m+w++y282pEpZcxM+4YOYzwIDkEbVJ47KIbTcl4=; b=CaCqupg0FTqNxejpMZJeS2Rfxf/NAsZQa0pXb7YEn8eADs/jqOW5mUc56CIAGy4LFB 09q4SPQgDcp/bwbnQYFpHYeqvyHXrGE9mTNF0+w3H9fhYiC6dNzm/ihEpeVUJQxc1ff/ OOyl2ed41rotBZN0t+mfWAN/nktKVbG4gFO5KVb7H4OWcfKr97UmG4KPFWFEtoNdQpZd BMT30TOzcqD1Gepu7Ps8K8M9D9OQ3yeW12piKUb4T8J6GsB2Hk5eZUCN87RcldWOZm7X 0vvw4cLGDw+Ib7w5iTWYLp7ATCeqiSvVAEDPKtMtK5y7bUA2AtiEZSgkTWA6UplqbmG7 c+Dg== X-Gm-Message-State: AOAM532SOUC/GqlK9nrvBd9/XJGivxlFAjKEFN7IPfjjdZn1AO9AzIMb xyF4R8sEkzIgpvlnXNv4MWw= X-Google-Smtp-Source: ABdhPJxcPnFhka+fUqaouhyTukjRJoxO4TigJeQgAm0gNpQv6Kfi5Gbox/7siMadBCOwpDd+Ln8aUA== X-Received: by 2002:a05:6a00:72b:b029:218:6603:a6a9 with SMTP id 11-20020a056a00072bb02902186603a6a9mr1900419pfm.78.1616453595715; Mon, 22 Mar 2021 15:53:15 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id j13sm8878211pfn.103.2021.03.22.15.53.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 15:53:14 -0700 (PDT) From: Andrei Vagin To: Will Deacon , Catalin Marinas Cc: Oleg Nesterov , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Andrei Vagin , Dave Martin , Keno Fischer Subject: [PATCH 1/4] arm64: expose orig_x0 in the user_pt_regs structure Date: Mon, 22 Mar 2021 15:50:50 -0700 Message-Id: <20210322225053.428615-2-avagin@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210322225053.428615-1-avagin@gmail.com> References: <20210322225053.428615-1-avagin@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210322_225317_599024_15D7171A X-CRM114-Status: GOOD ( 14.30 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org orig_x0 is recorded at the start of the syscall entry and then it is used for resetting the argument back to its original value during syscall restarts. If orig_x0 isn't available from user-space, this makes it tricky to manage arguments of restarted system calls. Cc: Keno Fischer Signed-off-by: Andrei Vagin --- arch/arm64/include/asm/ptrace.h | 2 +- arch/arm64/include/uapi/asm/ptrace.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/ptrace.h b/arch/arm64/include/asm/ptrace.h index e58bca832dff..d4cdf98ac003 100644 --- a/arch/arm64/include/asm/ptrace.h +++ b/arch/arm64/include/asm/ptrace.h @@ -183,9 +183,9 @@ struct pt_regs { u64 sp; u64 pc; u64 pstate; + u64 orig_x0; }; }; - u64 orig_x0; #ifdef __AARCH64EB__ u32 unused2; s32 syscallno; diff --git a/arch/arm64/include/uapi/asm/ptrace.h b/arch/arm64/include/uapi/asm/ptrace.h index 758ae984ff97..3c118c5b0893 100644 --- a/arch/arm64/include/uapi/asm/ptrace.h +++ b/arch/arm64/include/uapi/asm/ptrace.h @@ -90,6 +90,7 @@ struct user_pt_regs { __u64 sp; __u64 pc; __u64 pstate; + __u64 orig_x0; }; struct user_fpsimd_state { From patchwork Mon Mar 22 22:50:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 12156289 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F429C433C1 for ; Mon, 22 Mar 2021 22:55:19 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1A83861972 for ; Mon, 22 Mar 2021 22:55:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1A83861972 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: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:List-Owner; bh=/o0JBoeY0Tiphz/V2Hi9xBxg15Ov6uFyexq4tyhNArE=; b=k+9QK8syxRoERuczA5IwE5Fgw wj0ZApipkhtN+2nXEHGtowjsB+EupE5nkxyFUs7No6DmvWE++qoBUCx4/bM9O1l6zSNMZac8jFtQN ngCMSkEpunhlgc5P22H9C+mglt1TtVMnDDWJlxemDKCvpaKLuCmgsa+c3HRENye3cB/ABz/Ah4Lyh 4JnRWeBmyKwYD84RJgT5nHS+PYw8mpOWAsE//BJV1cfchbc4TCzmuVsc1kKmqxDINGJg8daSE5L1W zOzlAA4ONSIJxJqmqZ0GvpNqSmlryq6BQ4W9q29wAOJj1HaqDmqrNYfHcTA7DL478Q9LB4/RLwgBU GUFIDETyQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQW-00ClE0-4J; Mon, 22 Mar 2021 22:53:36 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQE-00Cl9p-Q2 for linux-arm-kernel@lists.infradead.org; Mon, 22 Mar 2021 22:53:20 +0000 Received: by mail-pf1-x436.google.com with SMTP id 11so12249284pfn.9 for ; Mon, 22 Mar 2021 15:53:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xdPAri8WoNsvA6cnhWTwn4LuVR0p31oI0eOUDse1ymU=; b=o9q20IOjrMTNEMELiAE+McnFDmEU2NeXPn2ko/gUHyiZgS1jUYp0Xu1rmqBioWYI5z YqVjqqs5dNckbDiyZSqacUd8um6FoRBsTp2hbx85JPUrxvRgsuJ8mAR8PFuZBI0am4cB rij/jU3nk8puii6J6vNO+/3nPsnxFRyPjw1cjBDaRvPFYKU2bmVNVLvpcJ+uFrRzCtd6 5Oz7gbP4gcTORSAAXu2uzsAGrSH3VZoxjONDroHWXMsPl5yCKqpvfh0cIOKwtTIfYpup 0neIfRYYMhJ980XYJgfXLoGj/vSF/ywjBs1CLudb+59g7uwa6S+liubZ5Ls4/wMkUfLN F6Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xdPAri8WoNsvA6cnhWTwn4LuVR0p31oI0eOUDse1ymU=; b=hzSCrYLeIbdIwzE8FKLHU32YK3PX/qQ3G5jEmK82iBH8BKPCO4uQuet9vHjuamdtes 9JfcmqcPDO7Oe9yRRx4aaqLSQOD/PErUKIhMZLPrRweWClL5oAV/1TQHhjYahfV3ROnn JDkOj8ydWKHwJCO0/+TpcHcVAxrUJcpgTn0pSDQYqgs9ot5o18YBxYCfNYOSxjtF1MFz u1kwKAzW8+ps/dicu/bfoWUWLXd2iTG6PhW+XiTH4iit+ZFw8NPXEmuI0mXJhsIy3iHe F2fp/3qCaFoLd0jA22dIWt2w1Fq+SKM/nIw4i27IcXL3Wqs4vvc35BqSgDWn5ZS1Qj2j ujiA== X-Gm-Message-State: AOAM5328hEybukEBk9usAPI71TFBjcJq+QWmgMRldikNpTryHPhhg4rl 51KtNnJEw2K8tidlid3RMCw= X-Google-Smtp-Source: ABdhPJyCo1Tsxnu9rig2t2KgMAcB3e7DRoJWJFvNaZoTvo5cXK5H0K/AscZbaDqkmYLKr0E6zpyEHg== X-Received: by 2002:a17:902:8497:b029:e6:f01d:9c9f with SMTP id c23-20020a1709028497b02900e6f01d9c9fmr462781plo.7.1616453597180; Mon, 22 Mar 2021 15:53:17 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id j13sm8878211pfn.103.2021.03.22.15.53.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 15:53:16 -0700 (PDT) From: Andrei Vagin To: Will Deacon , Catalin Marinas Cc: Oleg Nesterov , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Andrei Vagin , Dave Martin , Keno Fischer Subject: [PATCH 2/4] arm64/ptrace: introduce orig_x7 in the user_pt_regs structure Date: Mon, 22 Mar 2021 15:50:51 -0700 Message-Id: <20210322225053.428615-3-avagin@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210322225053.428615-1-avagin@gmail.com> References: <20210322225053.428615-1-avagin@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210322_225318_969943_801B6E11 X-CRM114-Status: GOOD ( 16.62 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org We have some ABI weirdness in the way that we handle syscall exit stops because we indicate whether or not the stop has been signalled from syscall entry or syscall exit by clobbering a general purpose register x7 in the tracee and restoring its old value after the stop. This behavior was inherited from ARM and it isn't common for other architectures. Now, we have PTRACE_GET_SYSCALL_INFO that gives all required information about system calls, so the hack with clobbering registers isn't needed anymore. This change instroduces orig_x7 in the user_pt_regs structure that will contains an origin value of the x7 register if the tracee is stopped in a system call.. Signed-off-by: Andrei Vagin --- arch/arm64/include/asm/ptrace.h | 1 + arch/arm64/include/uapi/asm/ptrace.h | 1 + arch/arm64/kernel/ptrace.c | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/arch/arm64/include/asm/ptrace.h b/arch/arm64/include/asm/ptrace.h index d4cdf98ac003..1008f0fbc5ea 100644 --- a/arch/arm64/include/asm/ptrace.h +++ b/arch/arm64/include/asm/ptrace.h @@ -184,6 +184,7 @@ struct pt_regs { u64 pc; u64 pstate; u64 orig_x0; + u64 orig_x7; }; }; #ifdef __AARCH64EB__ diff --git a/arch/arm64/include/uapi/asm/ptrace.h b/arch/arm64/include/uapi/asm/ptrace.h index 3c118c5b0893..be7583ff5f4d 100644 --- a/arch/arm64/include/uapi/asm/ptrace.h +++ b/arch/arm64/include/uapi/asm/ptrace.h @@ -91,6 +91,7 @@ struct user_pt_regs { __u64 pc; __u64 pstate; __u64 orig_x0; + __u64 orig_x7; }; struct user_fpsimd_state { diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c index 170f42fd6101..1ed5b4aa986b 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -1750,7 +1750,7 @@ static void tracehook_report_syscall(struct pt_regs *regs, enum ptrace_syscall_dir dir) { int regno; - unsigned long saved_reg; + u64 _saved_reg, *saved_reg; /* * We have some ABI weirdness here in the way that we handle syscall @@ -1768,19 +1768,25 @@ static void tracehook_report_syscall(struct pt_regs *regs, * - Syscall stops behave differently to seccomp and pseudo-step traps * (the latter do not nobble any registers). */ - regno = (is_compat_task() ? 12 : 7); - saved_reg = regs->regs[regno]; + if (is_compat_task()) { + regno = 12; + saved_reg = &_saved_reg; + } else { + regno = 7; + saved_reg = ®s->orig_x7; + } + *saved_reg = regs->regs[regno]; regs->regs[regno] = dir; if (dir == PTRACE_SYSCALL_ENTER) { if (tracehook_report_syscall_entry(regs)) forget_syscall(regs); - regs->regs[regno] = saved_reg; + regs->regs[regno] = *saved_reg; } else if (!test_thread_flag(TIF_SINGLESTEP)) { tracehook_report_syscall_exit(regs, 0); - regs->regs[regno] = saved_reg; + regs->regs[regno] = *saved_reg; } else { - regs->regs[regno] = saved_reg; + regs->regs[regno] = *saved_reg; /* * Signal a pseudo-step exception since we are stepping but From patchwork Mon Mar 22 22:50:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 12156291 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51946C433C1 for ; Mon, 22 Mar 2021 22:55:30 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D112861972 for ; Mon, 22 Mar 2021 22:55:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D112861972 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: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:List-Owner; bh=248UsfHFL1f5x/dMldky7syCRB1RXPjpyy4QX0kswl8=; b=IDvNV9GFHrUPN5zpxOLj+sSVs tg3F4LqnwWZoOmW50LAMhnntTxxgmc6bNJkiT3nYzUbsIhCSm5iCVImm9yp3JPOJzd3SRRdZi7Om6 ya2mJa+t8/5jgjKRlWQxiaf7ymHDDMo+lV9+HTXzwOToxhYwFNTuIopen0LzW7uKmqB9lFt3N+SNt /h4hilnGA8eOl7k3LE0cJ2gnrY5wtIKCtSJJV68wlBfcwOMUyjmdwIAGJAMnPety+G9rS3aqzyT9O Ac0MJCUPwXbF0l42opLhvR9IQlHUK1cm76+McqKZZNidJzSSvYhBnzjecZneJGiVexo6pcn9Pblim AehAIbHOQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQd-00ClFz-HX; Mon, 22 Mar 2021 22:53:43 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQG-00ClAN-9C for linux-arm-kernel@lists.infradead.org; Mon, 22 Mar 2021 22:53:22 +0000 Received: by mail-pf1-x433.google.com with SMTP id c17so5341776pfn.6 for ; Mon, 22 Mar 2021 15:53:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=972JgSIF2IIo8wEKlvKAl0BK+0XizAKsSmH5/nQePh4=; b=kyFRzjmyQNZ2Bjps4L5M0EhjRIv/RBgAPaxob3JGH3c9LC2wAXReZaM5iNFpVBAp+p oJM5qZQjZZQwFcR8eFRhltYD68lbnwWZPjn28HPgNI2OgK13kT77b9RMobozUskZL8MW 41QvaM7ZoQUL+1lZpR1oJUqMhFDZWNFhAn5LCVnX5Sk6kiirqiUGjVTtwP3ncP5eXqM1 RGiuniUiRDmJ3gzjoZ/SvDXMr7ezlJ5hrJ6qwZTa9J/1niNMtKNhVe+1My9NiKTvGl6g t/wBDHOpO97ENzhqOhJVJ8JmiVo3IT2drAG4lpt/YSbN9yEi7VJfmFuA+58ZSoTuDpmC F1WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=972JgSIF2IIo8wEKlvKAl0BK+0XizAKsSmH5/nQePh4=; b=CQvi8VsUfzadhfsmmyteABM4CuBOuzktDFhFKRFqQs1tSjjtxG9FtuUCd3VCVxs/8D 0ue8dhE6uS6AUXDgokCD1iGsBOzqCwRwXTHYYc4EDoIBDMlHC/nOah/CErrX8dFtW+Hr 2BfyG6dK0uA9K5DyjsZ0m0L4KIok5LIakAUMrkQqBGVOKh4GVuQ5TQw0pKMpk610/JnZ dB7wkc+t+K2HW/8TeF7nKjfEONx+9vVZdrpRaaBa9mRqu3OTemSvDAP0PYH4u42F29yj SYBT66txV7INPC8ITXZqxfsjNEHz7PUWYrDLIQ/OhpSzQGsJxmBiyiY+kX5tLLW4eU0O 1I0A== X-Gm-Message-State: AOAM531+ka55arx40xem87Q2jv6RE91CcsaP4dHI/rtkv/4onLqMHaxf sLVt8DAGbJW2DnYt3qq5jgy5lt5Cku8mew== X-Google-Smtp-Source: ABdhPJz52tT2nDR6QszbXNEnhRIexqy8hazMx7QWwYZkK74s9rdjv9EC4jYt7FLr0O944sg0Cr404w== X-Received: by 2002:a17:903:1c3:b029:e6:a15:751f with SMTP id e3-20020a17090301c3b02900e60a15751fmr1888188plh.44.1616453598632; Mon, 22 Mar 2021 15:53:18 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id j13sm8878211pfn.103.2021.03.22.15.53.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 15:53:17 -0700 (PDT) From: Andrei Vagin To: Will Deacon , Catalin Marinas Cc: Oleg Nesterov , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Andrei Vagin , Dave Martin , Keno Fischer Subject: [PATCH 3/4] selftest/arm64/ptrace: add a test for orig_x0 Date: Mon, 22 Mar 2021 15:50:52 -0700 Message-Id: <20210322225053.428615-4-avagin@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210322225053.428615-1-avagin@gmail.com> References: <20210322225053.428615-1-avagin@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210322_225320_398543_99861D23 X-CRM114-Status: GOOD ( 20.99 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The test creates two processes where one traces another one. The tracee executes a system call, the tracer traps it, changes orig_x0, triggers a signal and checks that the syscall is restarted with the setted argument. Test output: $ ./ptrace_restart_syscall_test 1..3 ok 1 orig_x0: 0x3 ok 2 x0: 0x5 ok 3 The child exited with code 0. # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0 Signed-off-by: Andrei Vagin --- tools/testing/selftests/arm64/ptrace/Makefile | 6 + tools/testing/selftests/arm64/ptrace/lib.h | 36 ++++++ .../ptrace/ptrace_restart_syscall_test.c | 122 ++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 tools/testing/selftests/arm64/ptrace/Makefile create mode 100644 tools/testing/selftests/arm64/ptrace/lib.h create mode 100644 tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c diff --git a/tools/testing/selftests/arm64/ptrace/Makefile b/tools/testing/selftests/arm64/ptrace/Makefile new file mode 100644 index 000000000000..1bc10e2d2ac8 --- /dev/null +++ b/tools/testing/selftests/arm64/ptrace/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 + +CFLAGS += -g -I../../../../../usr/include/ +TEST_GEN_PROGS := ptrace_restart_syscall_test + +include ../../lib.mk diff --git a/tools/testing/selftests/arm64/ptrace/lib.h b/tools/testing/selftests/arm64/ptrace/lib.h new file mode 100644 index 000000000000..14f4737188a3 --- /dev/null +++ b/tools/testing/selftests/arm64/ptrace/lib.h @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0-only +#ifndef __PTRACE_TEST_LOG_H +#define __PTRACE_TEST_LOG_H + +#define pr_p(func, fmt, ...) func("%s:%d: " fmt ": %m", \ + __func__, __LINE__, ##__VA_ARGS__) + +#define pr_err(fmt, ...) \ + ({ \ + ksft_test_result_error(fmt "\n", ##__VA_ARGS__); \ + -1; \ + }) + +#define pr_fail(fmt, ...) \ + ({ \ + ksft_test_result_fail(fmt "\n", ##__VA_ARGS__); \ + -1; \ + }) + +#define pr_perror(fmt, ...) pr_p(pr_err, fmt, ##__VA_ARGS__) + +static inline int ptrace_and_wait(pid_t pid, int cmd, int sig) +{ + int status; + + /* Stop on syscall-exit. */ + if (ptrace(cmd, pid, 0, 0)) + return pr_perror("Can't resume the child %d", pid); + if (waitpid(pid, &status, 0) != pid) + return pr_perror("Can't wait for the child %d", pid); + if (!WIFSTOPPED(status) || WSTOPSIG(status) != sig) + return pr_err("Unexpected status: %x", status); + return 0; +} + +#endif diff --git a/tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c b/tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c new file mode 100644 index 000000000000..ce59657f41be --- /dev/null +++ b/tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest.h" +#include "lib.h" + +static int child(int fd) +{ + char c; + + if (read(fd, &c, 1) != 1) + return 1; + + return 0; +} + +int main(int argc, void **argv) +{ + union { + struct user_regs_struct r; + struct { + char __regs[272]; + unsigned long long orig_x0; + unsigned long long orig_x7; + }; + } regs = {}; + struct iovec iov = { + .iov_base = ®s, + .iov_len = sizeof(regs), + }; + int status; + pid_t pid; + int p[2], fdzero; + + ksft_set_plan(3); + + if (pipe(p)) + return pr_perror("Can't create a pipe"); + fdzero = open("/dev/zero", O_RDONLY); + if (fdzero < 0) + return pr_perror("Can't open /dev/zero"); + + pid = fork(); + if (pid == 0) { + kill(getpid(), SIGSTOP); + return child(p[0]); + } + if (pid < 0) + return 1; + + if (ptrace(PTRACE_ATTACH, pid, 0, 0)) + return pr_perror("Can't attach to the child %d", pid); + if (waitpid(pid, &status, 0) != pid) + return pr_perror("Can't wait for the child %d", pid); + /* Skip SIGSTOP */ + if (ptrace_and_wait(pid, PTRACE_CONT, SIGSTOP)) + return 1; + + /* Resume the child to the next system call. */ + if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGTRAP)) + return 1; + + /* Send a signal to interrupt the system call. */ + kill(pid, SIGUSR1); + + /* Stop on syscall-exit. */ + if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGTRAP)) + return 1; + + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + return pr_perror("Can't get child registers"); + if (regs.orig_x0 != p[0]) + return pr_fail("Unexpected x0: 0x%lx", regs.r.regs[0]); + ksft_test_result_pass("orig_x0: 0x%llx\n", regs.orig_x0); + + /* Change orig_x0 that will be x0 for the restarted system call. */ + regs.orig_x0 = fdzero; + if (ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iov)) + return pr_perror("Can't get child registers"); + + /* Trap the signal and skip it. */ + if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGUSR1)) + return 1; + + /* Trap the restarted system call. */ + if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGTRAP)) + return 1; + + /* Check that the syscall is started with the right first argument. */ + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + return pr_perror("Can't get child registers"); + if (regs.r.regs[0] != fdzero) + return pr_fail("unexpected x0: %lx", regs.r.regs[0]); + ksft_test_result_pass("x0: 0x%llx\n", regs.r.regs[0]); + + if (ptrace(PTRACE_CONT, pid, 0, 0)) + return pr_perror("Can't resume the child %d", pid); + if (waitpid(pid, &status, 0) != pid) + return pr_perror("Can't wait for the child %d", pid); + if (status != 0) + return pr_fail("Child exited with code %d.", status); + + ksft_test_result_pass("The child exited with code 0.\n"); + ksft_exit_pass(); + return 0; +} + From patchwork Mon Mar 22 22:50:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 12156293 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EDEDC433DB for ; Mon, 22 Mar 2021 22:55:35 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A694C6196F for ; Mon, 22 Mar 2021 22:55:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A694C6196F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: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:List-Owner; bh=MXVZSqeyFEfZXzukc3iO8QIa29b5hi+Wfe/ZK1+aWTY=; b=IpdUQHSFHm8CrqGr56+JATEN9 D9Fv/hB+J0aTjrrFIQyhF7dNFONxIgxcVyj2RoiDXvlAIYLNuwJQ19fvNDGCrx9f0Lhca8S/Af6+b dw9y35gvzyt8Io64CnMvgBPyoC3nokKN/5XMh6LRi6fyDjMBLnCZBGmx8kDKQ8i5HWuvZZcB4kq8y kCaWsyd9ZYj5W9m/cSvAktxEnJWFWgANhzYxl5XblmIEkzF5KT1/BQBnZu0q425vR75U9cexZY9U3 2uFDvV03Z4GY5HVv/EmUj7Gq3stEqmjcrW4Imi15Gz0yJr3FMAF56h5cB8LVug6jZRwT79QpAGk7s +cOEwTncg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQk-00ClHp-EN; Mon, 22 Mar 2021 22:53:50 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQH-00ClAy-Ka for linux-arm-kernel@lists.infradead.org; Mon, 22 Mar 2021 22:53:24 +0000 Received: by mail-pj1-x1035.google.com with SMTP id a22-20020a17090aa516b02900c1215e9b33so11319921pjq.5 for ; Mon, 22 Mar 2021 15:53:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HIOBeYfsLRdlk6IE9Mox6wnpSlCp4ziKMxvOh3LQGV0=; b=f+gHS/k3aPWr0JJ8C4fAHd/OxMpzlfqzOtzSMSDqaUhVVmzLuW6gCh/jfK5pEBM28k PL9Fis9GwrhhAs98EaAvcVPY1EikAsiEiT3wGtDdfs4dhc81aQHXOw+oFDbM5otgMJG/ dxeCzFyoKFZJdpVWVf4OpheSnOI1slZE9ZJEBdBZ4l/aeSmtU8Rh7Sv0EL2+obSXXepe C3f+S3ygbdikFbs0Ejzq2S+agnW3+sqcgmH8g9PYbA9E1o2c+oquk3OuqLVPcWU9QBff hcsz2Zv7JGBwNdnR6LEA3SOiWIjUFUZrt9G8lXP0aNJ6QSQG4YLtxMp4tVbGAxHiJLXb 9eBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HIOBeYfsLRdlk6IE9Mox6wnpSlCp4ziKMxvOh3LQGV0=; b=GIehJC5XO0jVnmUGV3sYEXXQk2nNkh+IuCf/fkFJtzfHcIO4atA82WEbTP7F21486L pQcOZpywzRsNX6WcTpNOfeZzoTuSTK90CQPirtzLEc89MgXQFuafRPTq7XSY6h1JjBw3 246/Gd+A9b4Jwfk7gTc/+ws/NJvHMvbR/U7TCKUE7/pmskyICTe+gpsibymUIFtlgUZS hV4xfkLLM/n9sR3qneV0/LAvA39Vbh9xDPfP6ZCbaQTpHMCnvXUldICiXxt/wgbQC6NC pjZVr273VV9zDJc7wp35LE9ZN3NxXtpeJ4E5Dur+ycYgl2ruGI1IyQyVnExX41iCXT3U 6aDQ== X-Gm-Message-State: AOAM5333zgbdHYPcwBP537eRNaCbP/m1maIIv6wiNWhMmw/QrXV05NWG 8ekl7To/LEKsj8SojornJ24= X-Google-Smtp-Source: ABdhPJxe/yfdkp/SdhyhsEJL6O/SCgA66U19AgdI04G5R9seC5UAN6x44Ebnsx4PQkM2Bgdl2U0usQ== X-Received: by 2002:a17:90b:1082:: with SMTP id gj2mr1299519pjb.155.1616453599974; Mon, 22 Mar 2021 15:53:19 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id j13sm8878211pfn.103.2021.03.22.15.53.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 15:53:19 -0700 (PDT) From: Andrei Vagin To: Will Deacon , Catalin Marinas Cc: Oleg Nesterov , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Andrei Vagin , Dave Martin , Keno Fischer Subject: [PATCH 4/4] selftest/arm64/ptrace: add a test for orig_x7 Date: Mon, 22 Mar 2021 15:50:53 -0700 Message-Id: <20210322225053.428615-5-avagin@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210322225053.428615-1-avagin@gmail.com> References: <20210322225053.428615-1-avagin@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210322_225322_424484_BC34A9CD X-CRM114-Status: GOOD ( 22.22 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In system calls, x7 is used to indicate whether a tracee has been stopped on syscall-enter or syscall-exit and the origin value of x7 is saved in orig_x7. Test output: $ ./ptrace_syscall_test 1..4 ok 1 x7: 0 ok 2 x7: 1 ok 3 x7: 686920776f726c64 ok 4 The child exited with code 0. # Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0 Signed-off-by: Andrei Vagin --- tools/testing/selftests/arm64/ptrace/Makefile | 2 +- .../arm64/ptrace/ptrace_syscall_test.c | 158 ++++++++++++++++++ 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/arm64/ptrace/ptrace_syscall_test.c diff --git a/tools/testing/selftests/arm64/ptrace/Makefile b/tools/testing/selftests/arm64/ptrace/Makefile index 1bc10e2d2ac8..ea02c1a63806 100644 --- a/tools/testing/selftests/arm64/ptrace/Makefile +++ b/tools/testing/selftests/arm64/ptrace/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS += -g -I../../../../../usr/include/ -TEST_GEN_PROGS := ptrace_restart_syscall_test +TEST_GEN_PROGS := ptrace_restart_syscall_test ptrace_syscall_test include ../../lib.mk diff --git a/tools/testing/selftests/arm64/ptrace/ptrace_syscall_test.c b/tools/testing/selftests/arm64/ptrace/ptrace_syscall_test.c new file mode 100644 index 000000000000..ad55b44ae9f5 --- /dev/null +++ b/tools/testing/selftests/arm64/ptrace/ptrace_syscall_test.c @@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest.h" +#include "lib.h" + +#define X7_TEST_VAL 0x686920776f726c64UL + +static long test_syscall(long *x7) +{ + register long x0 __asm__("x0") = 0; + register long *x1 __asm__("x1") = x7; + register long x8 __asm__("x8") = 0x555; + + __asm__ ( + "ldr x7, [x1, 0]\n" + "svc 0\n" + "str x7, [x1, 0]\n" + : "=r"(x0) + : "r"(x0), "r"(x1), "r"(x8) + : + ); + return x0; +} + +static int child(void) +{ + long val = X7_TEST_VAL; + + if (test_syscall(&val)) { + ksft_print_msg("The test syscall failed\n"); + return 1; + } + if (val != X7_TEST_VAL) { + ksft_print_msg("Unexpected x7: %lx\n", val); + return 1; + } + + if (test_syscall(&val)) { + ksft_print_msg("The test syscall failed\n"); + return 1; + } + if (val != ~X7_TEST_VAL) { + ksft_print_msg("Unexpected x7: %lx\n", val); + return 1; + } + + return 0; +} + +#ifndef PTRACE_SYSEMU +#define PTRACE_SYSEMU 31 +#endif + +int main(int argc, void **argv) +{ + union { + struct user_regs_struct r; + struct { + char __regs[272]; + unsigned long long orig_x0; + unsigned long long orig_x7; + }; + } regs = {}; + struct iovec iov = { + .iov_base = ®s, + .iov_len = sizeof(regs), + }; + int status; + pid_t pid; + + ksft_set_plan(4); + + pid = fork(); + if (pid == 0) { + kill(getpid(), SIGSTOP); + _exit(child()); + } + if (pid < 0) + return 1; + + if (ptrace_and_wait(pid, PTRACE_ATTACH, SIGSTOP)) + return 1; + /* skip SIGSTOP */ + if (ptrace_and_wait(pid, PTRACE_CONT, SIGSTOP)) + return 1; + + /* Resume the child to the next system call. */ + if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGTRAP)) + return 1; + + /* Check that x7 is 0 on syscall-enter. */ + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + return pr_perror("Can't get child registers"); + if (regs.orig_x7 != X7_TEST_VAL) + return pr_fail("Unexpected orig_x7: %lx", regs.orig_x7); + if (regs.r.regs[7] != 0) + return pr_fail("Unexpected x7: %lx", regs.r.regs[7]); + ksft_test_result_pass("x7: %llx\n", regs.r.regs[7]); + + if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGTRAP)) + return 1; + + /* Check that x7 is 1 on syscall-exit. */ + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + return pr_perror("Can't get child registers"); + if (regs.r.regs[7] != 1) + return pr_fail("Unexpected x7: %lx", regs.r.regs[7]); + ksft_test_result_pass("x7: %llx\n", regs.r.regs[7]); + + /* Check that the child will not see a new value of x7. */ + regs.r.regs[0] = 0; + regs.r.regs[7] = ~X7_TEST_VAL; + if (ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iov)) + return pr_perror("Can't set child registers"); + + /* Resume the child to the next system call. */ + if (ptrace_and_wait(pid, PTRACE_SYSEMU, SIGTRAP)) + return 1; + + /* Check that orig_x7 contains the actual value of x7. */ + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + return pr_perror("Can't get child registers"); + if (regs.orig_x7 != X7_TEST_VAL) + return pr_fail("Unexpected orig_x7: %lx", regs.orig_x7); + ksft_test_result_pass("x7: %llx\n", regs.orig_x7); + + /* Check that the child will see a new value of x7. */ + regs.r.regs[0] = 0; + regs.orig_x7 = ~X7_TEST_VAL; + if (ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iov)) + return pr_perror("Can't set child registers"); + + if (ptrace(PTRACE_CONT, pid, 0, 0)) + return pr_perror("Can't resume the child %d", pid); + if (waitpid(pid, &status, 0) != pid) + return pr_perror("Can't wait for the child %d", pid); + if (status != 0) + return pr_fail("Child exited with code %d.", status); + + ksft_test_result_pass("The child exited with code 0.\n"); + ksft_exit_pass(); + return 0; +} +