From patchwork Tue Jan 19 22:06:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 12030941 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.8 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,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 87EE2C433DB for ; Tue, 19 Jan 2021 22:10:24 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (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 382BD22E01 for ; Tue, 19 Jan 2021 22:10:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 382BD22E01 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=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=WVw1XLJkIRiIj3KUhMpnf1+QKVesJZr7gLPbCBsCx3Q=; b=CCXnBZ6eirYH7pJGuXOTvsZ/y Ll7djwFRj7b1mExEgsiB70/rPNtMx+zbjbC27VF9y5yrlFtg0jFk8rVNJ8vDiUXtS2Z7EsGKUbJri mttdtfeDAteybN1AYmqmnTDJZMcoHVIGozWV42oqEeGZDqI/6NtbDszxIOnQizQIAnENOXYR4YofP xveO23AEy7GBRPRKUKh2mBGoc4QvGg+GZJ5U1u8dVBIcBELBymeFX5n4FXo9/MBqL557I3MUhIlKs BcF1BNFl1dcx0IgBxYkiEwxep3mFZMdF4C+bG6itvdMtpOSLHch5F0VvjQPdy2cukArx7i3g+Cw1p 6T63ywM+w==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l1zAq-0007Tr-8r; Tue, 19 Jan 2021 22:08:28 +0000 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l1zAg-0007R3-Gs for linux-arm-kernel@lists.infradead.org; Tue, 19 Jan 2021 22:08:20 +0000 Received: by mail-pg1-x52b.google.com with SMTP id p18so13818178pgm.11 for ; Tue, 19 Jan 2021 14:08:17 -0800 (PST) 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=Fzr5ZmyqkA8PO0rKR84r5wnTkX6dyXCN2TjKwSkTcCQ=; b=YqqYzbHq8KUuNlsVV8uwX4a0tFYj85lWPFI48TJfZyr4Zmv8WL6MT/v2KFm08aiB9+ ZaTTebjTlB2CZ7O5HN7YO5bGtEn1DxCepQOl/lNmRZpgk6m1ih3VeaKfoayD7MJJ8n/r K+g7jYDBP1uGGTPuNf8MyfTVMGCmZCzq8PjbR9oWO0zuYzaUzAhsGVU/FxPVqXvc3DM2 +Z47rau7jLXrmUUlLYytzT6XhCYmi0OtLKewHxHaep4pXTsQhO0SxnkVtN4NVe91EBYW zsbHh7VrR9DP7Oy1hbTc8p/GCDqkUNhsO4d1PPeTzZE5M5ogY46bQrcQcS1ZJMvt7T8b /hrg== 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=Fzr5ZmyqkA8PO0rKR84r5wnTkX6dyXCN2TjKwSkTcCQ=; b=HIRqtWaO4G9zIdzz7E/hxr7r0YxRrlJ+agO+iK2HXUuZgEXOWgYOie6VjfbyUzJ2qw UFvKkTLapQT43KhSHGxllFMsGuhELcMCkwNzQfakLtbzI9Njv3J6VijM56o5Y5S73yBE gI+Qeikyr59CZ+yDr1Uk8fqniZWqXc+nX70RH92baOvKYNusQtgz+TAAEu6yK+QSFrRF A3hhILog79Sg23o/xrOsB76tybrDb39EtH0PsKk2vVOV3pA/ilOkr2oBTA5j1arB8mmA xjsx3gZfPEcTOong67SmnCZ52kzrwS5+zBt15tX4sT+nUgrnJn1otO5RxzPcOxGXVYc9 /elQ== X-Gm-Message-State: AOAM531yLf3QRt9tquIcVUbG1EpfanYTiBewTVt0ItSAVqE4YTa4zFq8 w1fAHwsepSxKMx9+EBfKJ3daQPHSLsaCDA== X-Google-Smtp-Source: ABdhPJy28WqsBvPwRH+gTwECasNDKDCvTk+9y/hhs1zMjQCiho52tcSsFNcJidbKFqaA0/ZV4wJE9w== X-Received: by 2002:a63:fb54:: with SMTP id w20mr6211413pgj.419.1611094096174; Tue, 19 Jan 2021 14:08:16 -0800 (PST) Received: from laptop.hsd1.wa.comcast.net ([2601:600:9b7f:872e:a655:30fb:7373:c762]) by smtp.gmail.com with ESMTPSA id 72sm95196pfw.170.2021.01.19.14.08.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jan 2021 14:08:15 -0800 (PST) From: Andrei Vagin To: Will Deacon , Catalin Marinas Subject: [PATCH 3/3] selftest/arm64/ptrace: add a test for NT_ARM_PRSTATUS Date: Tue, 19 Jan 2021 14:06:37 -0800 Message-Id: <20210119220637.494476-4-avagin@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210119220637.494476-1-avagin@gmail.com> References: <20210119220637.494476-1-avagin@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210119_170818_821616_E26D6716 X-CRM114-Status: GOOD ( 20.78 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrei Vagin , Oleg Nesterov , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Test output: TAP version 13 1..1 # selftests: arm64/ptrace: ptrace_syscall_regs_test # 1..3 # ok 1 NT_PRSTATUS: x7 0 # ok 2 NT_ARM_PRSTATUS: x7 686920776f726c64 # ok 3 The child exited with code 0. # # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0 ok 1 selftests: arm64/ptrace: ptrace_syscall_regs_test Signed-off-by: Andrei Vagin --- tools/testing/selftests/arm64/Makefile | 2 +- tools/testing/selftests/arm64/ptrace/Makefile | 6 + .../arm64/ptrace/ptrace_syscall_regs_test.c | 147 ++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/arm64/ptrace/Makefile create mode 100644 tools/testing/selftests/arm64/ptrace/ptrace_syscall_regs_test.c diff --git a/tools/testing/selftests/arm64/Makefile b/tools/testing/selftests/arm64/Makefile index 2c9d012797a7..704770a60ece 100644 --- a/tools/testing/selftests/arm64/Makefile +++ b/tools/testing/selftests/arm64/Makefile @@ -4,7 +4,7 @@ ARCH ?= $(shell uname -m 2>/dev/null || echo not) ifneq (,$(filter $(ARCH),aarch64 arm64)) -ARM64_SUBTARGETS ?= tags signal pauth fp mte +ARM64_SUBTARGETS ?= tags signal pauth fp mte ptrace else ARM64_SUBTARGETS := endif diff --git a/tools/testing/selftests/arm64/ptrace/Makefile b/tools/testing/selftests/arm64/ptrace/Makefile new file mode 100644 index 000000000000..ca906ce3a581 --- /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_syscall_regs_test + +include ../../lib.mk diff --git a/tools/testing/selftests/arm64/ptrace/ptrace_syscall_regs_test.c b/tools/testing/selftests/arm64/ptrace/ptrace_syscall_regs_test.c new file mode 100644 index 000000000000..601378b7591d --- /dev/null +++ b/tools/testing/selftests/arm64/ptrace/ptrace_syscall_regs_test.c @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest.h" + +#define TEST_VAL 0x686920776f726c64UL + +#define pr_p(func, fmt, ...) func(fmt ": %m", ##__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 long loop(void *val) +{ + register long x0 __asm__("x0"); + register void *x1 __asm__("x1") = val; + register long x8 __asm__("x8") = 555; + + __asm__ ( + "again:\n" + "ldr x7, [x1, 0]\n" + "svc 0\n" + "str x7, [x1, 0]\n" + : "=r"(x0) + : "r"(x1), "r"(x8) + : + ); + return 0; +} + +static int child(void) +{ + long val = TEST_VAL; + + loop(&val); + if (val != ~TEST_VAL) { + ksft_print_msg("Unexpected x7: %lx\n", val); + return 1; + } + + return 0; +} + +#ifndef PTRACE_SYSEMU +#define PTRACE_SYSEMU 31 +#endif + +#ifndef NT_ARM_PRSTATUS +#define NT_ARM_PRSTATUS 0x410 +#endif + +int main(int argc, void **argv) +{ + struct user_regs_struct regs = {}; + struct iovec iov = { + .iov_base = ®s, + .iov_len = sizeof(struct user_regs_struct), + }; + int status; + pid_t pid; + + ksft_set_plan(3); + + pid = fork(); + if (pid == 0) { + kill(getpid(), SIGSTOP); + child(); + _exit(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(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); + + /* Resume the child to the next system call. */ + if (ptrace(PTRACE_SYSEMU, 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) != SIGTRAP) + return pr_err("Unexpected status: %d", status); + + /* Check that x7 is zero in the case of NT_PRSTATUS. */ + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + return pr_perror("Can't get child registers"); + if (regs.regs[7] != 0) + return pr_fail("NT_PRSTATUS: unexpected x7: %lx", regs.regs[7]); + ksft_test_result_pass("NT_PRSTATUS: x7 %llx\n", regs.regs[7]); + + /* Check that x7 isnt't clobbered in the case of NT_ARM_PRSTATUS. */ + if (ptrace(PTRACE_GETREGSET, pid, NT_ARM_PRSTATUS, &iov)) + return pr_perror("Can't get child registers"); + if (regs.regs[7] != TEST_VAL) + return pr_fail("NT_ARM_PRSTATUS: unexpected x7: %lx", regs.regs[7]); + ksft_test_result_pass("NT_ARM_PRSTATUS: x7 %llx\n", regs.regs[7]); + + /* Check that the child will see a new value of x7. */ + regs.regs[0] = 0; + regs.regs[7] = ~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; +} +