Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp470998lqp; Wed, 22 May 2024 09:40:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV4tOCRzQ3BhBcog0beRSiMbwHfKO9AFtQ4iWX7CmumB1+EY9ttRadp0InQFxH4eTUB3FCTo6ApyquVR1NAy9HmasyHNQM3853wotqOKg== X-Google-Smtp-Source: AGHT+IHM7/BtLyHhkXc01Cl9fuHpmb7nW9XHF8LngJnK4ea4TSkN/QYAYdgIHKvKo07XUscSOTHu X-Received: by 2002:a17:90a:fa03:b0:2b2:b00b:a341 with SMTP id 98e67ed59e1d1-2bd9f3456b0mr2747793a91.8.1716396049131; Wed, 22 May 2024 09:40:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716396049; cv=pass; d=google.com; s=arc-20160816; b=Uz4XJ1JCrvXRc6fw1N9PzJYAUoMr1FiJwEfEq9pun3DjYYBZQeM10KemCHKRJ/THph YUkI5txDCLcjW6drs+z5AhOgnOZnn4j5r0Hmrfm2vEoyTDnH1NCC9z89s6jN4NUkSL0A qmxcXG5/UExlEPafuYQjdPBqrdyZwz55ykxEUu5ua+bty8fj0sjRol58sfC3IRv9ytxn E2aH/vij5HwOHm+fhMbA2C+puc9NIPHeVd1tLHp3k8gZtWeWMTmJCpwZ53LyFwSJ10Jk MfzOVJgPAO41+SNx5KKxZo2WtJX/ypR6cL4d6Ylh9+Y+oA9qR7HgLiYkdtiqkNMRs5Rg 00wA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:to:from:cc:in-reply-to :subject:date:dkim-signature; bh=Nh2hEBp7C+i2NTaEC7uyemcMKD8WTFnEb/ThoPY8mY0=; fh=79sg8tZobFjyMqbGloyClhZII4vF6FVh7Th1YPg7a1c=; b=HtY0LCvcd2LhNaV2NR5QZ91dkyqiupANYMIxhcrCsA/2wVG6/E5Sr6S4j8WVY9iKmw s7GxybstzJb6Ep/zY6Z5Tk5kgyaEvJyLCixyuiuX986R6PTUaqRJM/ZV+DDU1gf1n7M7 9bdx6t0KxNPU8LfJC2hu+1lIJwEXuEFydefoYm91ekEhIZ5fsrk307NGNyEYi/sQGb0o rTswJGDLTK98cQzxVrkpUrj+ue0uCm+SMROr8+vAz5mBQsp7wREWe33lDx0P9Q1ZFy89 7jFeB0ia+RqNTGaY3D2SJSX32AnEkcXJoKEcSwv9mnF3gdbUhpCOSWCmOLj0kArDxv6Q RUBA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@dabbelt-com.20230601.gappssmtp.com header.s=20230601 header.b=UQrGAa5I; arc=pass (i=1 spf=pass spfdomain=dabbelt.com dkim=pass dkdomain=dabbelt-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-186499-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186499-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2b628a69e07si28720756a91.85.2024.05.22.09.40.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 09:40:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-186499-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@dabbelt-com.20230601.gappssmtp.com header.s=20230601 header.b=UQrGAa5I; arc=pass (i=1 spf=pass spfdomain=dabbelt.com dkim=pass dkdomain=dabbelt-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-186499-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186499-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 23C65B21640 for ; Wed, 22 May 2024 16:40:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5FFF81411FE; Wed, 22 May 2024 16:40:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dabbelt-com.20230601.gappssmtp.com header.i=@dabbelt-com.20230601.gappssmtp.com header.b="UQrGAa5I" Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F101E13D612 for ; Wed, 22 May 2024 16:40:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716396036; cv=none; b=OcuPwRAdGKyX+zbDX2edy5Va91OaZCSPjvMCfyPxTry/vy2iq6AHsqtb+IKE5mMBhCZlIiTEiNt4pojvX9e5LRnLolUrL4Z86iA1crclqU0/jigNeXLbY8AyECvCIdyfL/TNiodkL3LzJ98Ho6G5126KaNOhqI/JY5wjIw0JgRw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716396036; c=relaxed/simple; bh=AQgUlV9ljyVvTXd6sdYi/VKpv3N6lC3vGB8gSB2bAQI=; h=Date:Subject:In-Reply-To:CC:From:To:Message-ID:Mime-Version: Content-Type; b=VfvwtSnWs8JzsBPVVEdfKdVnfGE+i7XcGITh4g7LN1sGecrx1Qi9svhpzx6jjU/5cRiDVKkfHmM1CQLS9cLhkS4T06HZTBnRWCiLTMVERIqNY+ClR6xDBj5BbDSEc6xyYfKBzut9hUQaP8O+0NIa49q6qaJHCpn2jjL60Fg5KLU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dabbelt.com; spf=pass smtp.mailfrom=dabbelt.com; dkim=pass (2048-bit key) header.d=dabbelt-com.20230601.gappssmtp.com header.i=@dabbelt-com.20230601.gappssmtp.com header.b=UQrGAa5I; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dabbelt.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dabbelt.com Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1f333c7e702so3031065ad.3 for ; Wed, 22 May 2024 09:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dabbelt-com.20230601.gappssmtp.com; s=20230601; t=1716396032; x=1717000832; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:to:from:cc :in-reply-to:subject:date:from:to:cc:subject:date:message-id :reply-to; bh=Nh2hEBp7C+i2NTaEC7uyemcMKD8WTFnEb/ThoPY8mY0=; b=UQrGAa5IdsZVSuVDF/zLZn7ozK11aW9vPcyK7GFDQtdJllB5juftSGJfr6OIzRqE4i m1YUfn8x58blCmJZ8ZgB3I6S7h5XhkpisxmHYkGrVcXZK+jo86B8Z/SoYycQ4Hlg7FKQ AtgVg4y76VfwJxEmac1o31cYE3gRAGuhNkQQlp/jBmBjeboCUgE1bo7cqko8q7XdnNQC F4Up2aDTYjFla2T6Za67mqdOQz4uIToIMGK5EcRNJTVc6tP4sgYl0jBXt3tVeME4lSS9 TrGXjl0iIAFaHpEqVPUe/q0QMSdWeWBP1mTQR3/fpYO8/kFS/P2wxdomI33pB+2tir9U XbvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716396032; x=1717000832; h=content-transfer-encoding:mime-version:message-id:to:from:cc :in-reply-to:subject:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Nh2hEBp7C+i2NTaEC7uyemcMKD8WTFnEb/ThoPY8mY0=; b=fyvWKD9z80qnD5eqOHR0Tc8ojC520/Qm0H9MPi7gbVhuvrAMF8b9+9p+Syd2bNq6wT 9vf4ZUvSVSV6PdSVtCASNhyf0Qi81h7nwOgmquBIwwLqQXuWJSjc0d4rzdI/YAPy/yJ/ UUpLDpnB2cz+GTf1ANKu+vf4CicUP1sp3DN+TKcM+3qRml2yzNA73M6ST0Ki9moxTaug 3kWSD5bBjqiojG7SV7u9GOemMUm6Kl+E614EctRuujsIe2wOhWbvBdgMrMjB6eYZVPxH ew4MO7Ah3zLM9Wk7gIWl3BGmLzr7WmpxmMqz34VAmbujPLkuVHYw1VF8w3/VBatfpxXQ kLDg== X-Forwarded-Encrypted: i=1; AJvYcCUqQ2IThJ4sFY1mzMoHj2Fj+KYnj29g3aLkqBzj/LmFIHAsAvyP3bJWtY3MFiEtGf/ksnJ0nTFwsqbrkfwonJt219YcqAclorfeHOCh X-Gm-Message-State: AOJu0YwfQ4njp22Gd4iwknGQ7OU1Sg8SeaSpQnqLzJfgZO/YpOGa3+U3 pwuaSV3+nq14gUPHeligO1AXiLnS3yyhIvdJITwcnmcoJJKYOWHrvXY/5RlvH70= X-Received: by 2002:a17:902:9895:b0:1f2:efdf:a410 with SMTP id d9443c01a7336-1f31c97be08mr23788185ad.15.1716396031849; Wed, 22 May 2024 09:40:31 -0700 (PDT) Received: from localhost ([192.184.165.199]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0bad7eb8sm241308015ad.90.2024.05.22.09.40.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 09:40:31 -0700 (PDT) Date: Wed, 22 May 2024 09:40:31 -0700 (PDT) X-Google-Original-Date: Wed, 22 May 2024 09:40:20 PDT (-0700) Subject: Re: [PATCH] riscv: selftests: Add signal handling vector tests In-Reply-To: <20240403-vector_sigreturn_tests-v1-1-2e68b7a3b8d7@rivosinc.com> CC: shuah@kernel.org, Paul Walmsley , aou@eecs.berkeley.edu, andy.chiu@sifive.com, bjorn@kernel.org, Vineet Gupta , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, Charlie Jenkins From: Palmer Dabbelt To: Charlie Jenkins Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 (MHng) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On Wed, 03 Apr 2024 16:50:29 PDT (-0700), Charlie Jenkins wrote: > Add two tests to check vector save/restore when a signal is received > during a vector routine. One test ensures that a value is not clobbered > during signal handling. The other verifies that vector registers > modified in the signal handler are properly reflected when the signal > handling is complete. > > Signed-off-by: Charlie Jenkins > --- > These tests came about to highlight the bug fixed in > https://lore.kernel.org/lkml/20240403072638.567446-1-bjorn@kernel.org/ > and will only pass with that fix applied. Thaks. I'm just staging this, I have a testcase too that I keep forgetting to post because I haven't gotten in into the selftests sanely yet... #include "../../kselftest.h" #include #include #include #include #define ITERATIONS (1 << 24) #define RISCV_V_MAGIC 0x53465457 struct __riscv_ctx_hdr { __u32 magic; __u32 size; }; struct __riscv_extra_ext_header { __u32 __padding[129] __attribute__((aligned(16))); /* * Reserved for expansion of sigcontext structure. Currently zeroed * upon signal, and must be zero upon sigreturn. */ __u32 reserved; struct __riscv_ctx_hdr hdr; }; struct __riscv_v_ext_state { unsigned long vstart; unsigned long vl; unsigned long vtype; unsigned long vcsr; unsigned long vlenb; void *datap; /* * In signal handler, datap will be set a correct user stack offset * and vector registers will be copied to the address of datap * pointer. */ }; /* The beginning of the actual state. */ int count; void handler(int, siginfo_t *si, void *ucontext_void) { ucontext_t *ucontext = ucontext_void; /* Skip over the SEGV by just jumping to a0. */ ucontext->uc_mcontext.__gregs[REG_PC] = ucontext->uc_mcontext.__gregs[REG_A0]; /* Increment the first element of v0. */ { struct __riscv_extra_ext_header *ext; struct __riscv_v_ext_state *v_ext_state; uint32_t val; /* Find the vector context. */ ext = (void *)(&ucontext->uc_mcontext.__fpregs); if (ext->hdr.magic != RISCV_V_MAGIC) { fprintf(stderr, "bad vector magic: %x\n", ext->hdr.magic); abort(); } v_ext_state = (void *)((char *)(ext) + sizeof(*ext)); /* Actually do the increment. */ memcpy(&val, v_ext_state->datap, sizeof(val)); val++; memcpy(v_ext_state->datap, &val, sizeof(val)); } /* Count the delivered signal, to make sure we don't miss any. */ count++; } void sigill(int, siginfo_t *si, void *ucontext_void) { fprintf(stderr, "SIGILL after %d iterations\n", count); abort(); } void check_signal(void) { int check = rand(); int out; __asm__ volatile ( "vsetvli a0, %2, e32, ta, ma\n\t" "vmv.s.x v0, %1\n\t" "la a0, 1f\n\t" "lw t0, 0(x0)\n\t" "1:\n\t" "vmv.x.s %0, v0" : "=&r"(out) : "r"(check-1), "r"(1) : "a0" #ifdef __riscv_vector , "v0" #endif ); if (out != check) { fprintf(stderr, "out: %d\ncheck: %d\n", out, check); abort(); } } int main(void) { struct sigaction sa_segv, sa_ill; memset(&sa_segv, 0, sizeof(sa_segv)); sa_segv.sa_sigaction = &handler; sa_segv.sa_flags = SA_SIGINFO; sigaction(SIGSEGV, &sa_segv, NULL); memset(&sa_ill, 0, sizeof(sa_ill)); sa_ill.sa_sigaction = &sigill; sa_ill.sa_flags = SA_SIGINFO; sigaction(SIGILL, &sa_ill, NULL); count = 0; for (int i = 0; i < ITERATIONS; ++i) check_signal(); if (count != ITERATIONS) { fprintf(stderr, "count: %d\n", count); abort(); } return 0; } > --- > tools/testing/selftests/riscv/Makefile | 2 +- > tools/testing/selftests/riscv/sigreturn/.gitignore | 1 + > tools/testing/selftests/riscv/sigreturn/Makefile | 12 ++++ > .../testing/selftests/riscv/sigreturn/sigreturn.c | 82 ++++++++++++++++++++++ > 4 files changed, 96 insertions(+), 1 deletion(-) > > diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile > index 4a9ff515a3a0..7ce03d832b64 100644 > --- a/tools/testing/selftests/riscv/Makefile > +++ b/tools/testing/selftests/riscv/Makefile > @@ -5,7 +5,7 @@ > ARCH ?= $(shell uname -m 2>/dev/null || echo not) > > ifneq (,$(filter $(ARCH),riscv)) > -RISCV_SUBTARGETS ?= hwprobe vector mm > +RISCV_SUBTARGETS ?= hwprobe vector mm sigreturn > else > RISCV_SUBTARGETS := > endif > diff --git a/tools/testing/selftests/riscv/sigreturn/.gitignore b/tools/testing/selftests/riscv/sigreturn/.gitignore > new file mode 100644 > index 000000000000..35002b8ae780 > --- /dev/null > +++ b/tools/testing/selftests/riscv/sigreturn/.gitignore > @@ -0,0 +1 @@ > +sigreturn > diff --git a/tools/testing/selftests/riscv/sigreturn/Makefile b/tools/testing/selftests/riscv/sigreturn/Makefile > new file mode 100644 > index 000000000000..eb8bac9279a8 > --- /dev/null > +++ b/tools/testing/selftests/riscv/sigreturn/Makefile > @@ -0,0 +1,12 @@ > +# SPDX-License-Identifier: GPL-2.0 > +# Copyright (C) 2021 ARM Limited > +# Originally tools/testing/arm64/abi/Makefile > + > +CFLAGS += -I$(top_srcdir)/tools/include > + > +TEST_GEN_PROGS := sigreturn > + > +include ../../lib.mk > + > +$(OUTPUT)/sigreturn: sigreturn.c > + $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ > diff --git a/tools/testing/selftests/riscv/sigreturn/sigreturn.c b/tools/testing/selftests/riscv/sigreturn/sigreturn.c > new file mode 100644 > index 000000000000..62397d5934f1 > --- /dev/null > +++ b/tools/testing/selftests/riscv/sigreturn/sigreturn.c > @@ -0,0 +1,82 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +#include > +#include > +#include > +#include > +#include > +#include "../../kselftest_harness.h" > + > +#define RISCV_V_MAGIC 0x53465457 > +#define DEFAULT_VALUE 2 > +#define SIGNAL_HANDLER_OVERRIDE 3 > + > +static void simple_handle(int sig_no, siginfo_t *info, void *vcontext) > +{ > + ucontext_t *context = vcontext; > + > + context->uc_mcontext.__gregs[REG_PC] = context->uc_mcontext.__gregs[REG_PC] + 4; > +} > + > +static void vector_override(int sig_no, siginfo_t *info, void *vcontext) > +{ > + ucontext_t *context = vcontext; > + > + // vector state > + struct __riscv_extra_ext_header *ext; > + struct __riscv_v_ext_state *v_ext_state; > + > + /* Find the vector context. */ > + ext = (void *)(&context->uc_mcontext.__fpregs); > + if (ext->hdr.magic != RISCV_V_MAGIC) { > + fprintf(stderr, "bad vector magic: %x\n", ext->hdr.magic); > + abort(); > + } > + > + v_ext_state = (void *)((char *)(ext) + sizeof(*ext)); > + > + *(int *)v_ext_state->datap = SIGNAL_HANDLER_OVERRIDE; > + > + context->uc_mcontext.__gregs[REG_PC] = context->uc_mcontext.__gregs[REG_PC] + 4; > +} > + > +static int vector_sigreturn(int data, void (*handler)(int, siginfo_t *, void *)) > +{ > + int after_sigreturn; > + struct sigaction sig_action = { > + .sa_sigaction = handler, > + .sa_flags = SA_SIGINFO > + }; > + > + sigaction(SIGSEGV, &sig_action, 0); > + > + asm(".option push \n\ > + .option arch, +v \n\ > + vsetivli x0, 1, e32, ta, ma \n\ > + vmv.s.x v0, %1 \n\ > + # Generate SIGSEGV \n\ > + lw a0, 0(x0) \n\ > + vmv.x.s %0, v0 \n\ > + .option pop" : "=r" (after_sigreturn) : "r" (data)); > + > + return after_sigreturn; > +} > + > +TEST(vector_restore) > +{ > + int result; > + > + result = vector_sigreturn(DEFAULT_VALUE, &simple_handle); > + > + EXPECT_EQ(DEFAULT_VALUE, result); > +} > + > +TEST(vector_restore_signal_handler_override) > +{ > + int result; > + > + result = vector_sigreturn(DEFAULT_VALUE, &vector_override); > + > + EXPECT_EQ(SIGNAL_HANDLER_OVERRIDE, result); > +} > + > +TEST_HARNESS_MAIN > > --- > base-commit: 4cece764965020c22cff7665b18a012006359095 > change-id: 20240403-vector_sigreturn_tests-8118f0ac54fa