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; +} +