From patchwork Fri Jun 5 04:17:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 187490 Delivered-To: patch@linaro.org Received: by 2002:a92:cf06:0:0:0:0:0 with SMTP id c6csp2028879ilo; Thu, 4 Jun 2020 21:27:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwuOT4NuqwiRQXsbYQ4rmNtYh4SdTtUK97jEuBGpFSLDsfVsUiAEoaQQcG0xuicovifkXKD X-Received: by 2002:a5b:3c2:: with SMTP id t2mr11727784ybp.209.1591331268611; Thu, 04 Jun 2020 21:27:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591331268; cv=none; d=google.com; s=arc-20160816; b=xz3qvZ3AfkQ8tZr3goqECWclEk7f8zztgotXz1wk5gLOz7SYlcJA6gViBJwHyLwgwC bEq7R8PFETzUb2Gn8iR0kE7oQEZ4aX1hIG2eWMPZF2fw0FPXvH3lI/grMXcZi/hOT7v6 atOKWhuSR5zsGaUZqWwhjnxWVyC4E7DDMa9HOoGZQ3lAUJqlK/gZG6544RK6Jm0FDz9q 8qU4Ck4CLNixRAXj3DMRsQYsOWBcNsQYGzlVABZ84+awHNEc1Jpd83Rtp4KlUfMmP1Zu HHvlMoS65/Qr7JVxw2pjmZ2WHDv0p7v/YOAwKewzPTYaAxCDZmpOEMDUhshbyXDDJCug NURg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=Kb6rkAj53RRMvC2rZIPmdOIeGY5NNQOnyCUHmHsi04g=; b=quEx/oN72/XpM/72qzQ0FRMNkmxxMrxs5dqujFSnKzKcLbcLZ51eWjCjSYLyM71hS+ tR6vee7M9gKE4Zdn7UKuvnhcZbGeHCP9bnKke0UlnheUhMdPHErr4m+/uT1MpzJg2Xbn oJfwSWT30pT4Zus+Z3Jsx+NufvSKVBxuRbUOvMAnSIgs44F4jCVlXZfm9UwS3823Z64n DHvPXHiZWX2sZDucDsT+SOK5sdcC7gBXDBUCBePa6VakObuVdksizHY33QrpIxtYctiU JkRIu+rSO6FdAmBCGNPr5L7NtvLJyxkl74Adu2Vvo1ez6gaExRiTUmLTvXJZjoaKDaW1 b1Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=yvH8g8df; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id s68si6794838ybc.235.2020.06.04.21.27.48 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 04 Jun 2020 21:27:48 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=yvH8g8df; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:49050 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jh3xM-0004z5-2g for patch@linaro.org; Fri, 05 Jun 2020 00:27:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39632) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jh3ns-0004q4-0G for qemu-devel@nongnu.org; Fri, 05 Jun 2020 00:18:00 -0400 Received: from mail-pj1-x1044.google.com ([2607:f8b0:4864:20::1044]:53908) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jh3nq-0000nF-Nl for qemu-devel@nongnu.org; Fri, 05 Jun 2020 00:17:59 -0400 Received: by mail-pj1-x1044.google.com with SMTP id i12so2055966pju.3 for ; Thu, 04 Jun 2020 21:17:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Kb6rkAj53RRMvC2rZIPmdOIeGY5NNQOnyCUHmHsi04g=; b=yvH8g8dfduLa1IpIp3sKKOzLXGe/K6K2dXH9Ga8tayx7j68WrkKCZGMaYxhBAq3dUl WUXs+tBf1IwPtwK29PrMEBPS2Z6CY+/duUtFRMnS3Bd1aKxBMlprfS76J7qDErNg0ag0 /kKG6iAx+cTcbENv2mTP8k3M39cujyLwIZs/e98FpvOkgBCi/BpOgzdAYmrsFOXvwZBl NX8x1XnLp48IfyZjkFgvlamDu9YbE3WsSdMAn0LpTwAfsAYgYSpmadxbQFpbJtQZyQBf FCLxqTiQ/iKRTJlMsGO1dSn3Nu6TgP2a17s7O4DDBiZqR4rWBfsFDYJYy8llXuRnrBpY tsHg== 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=Kb6rkAj53RRMvC2rZIPmdOIeGY5NNQOnyCUHmHsi04g=; b=FEUYjgXLg9+9r2I/9Wn89v28ZT+Z0n05yJ87azfcbnTszQRM2Y/pXv9BPRpMEneggq EK0mmJr87llBRml7FR9CYnqe3Fe6aK6MBz3F6yeOovT9tPuoHhpS7PfFDox0BZegerQK gXXg+p2Jr7ZpNcqYa4BQCbHUVEorBc/+EjDiFmh914Lc7tXU8NC5v71BgjdFKA9ahHMb Icj09gU7WiciFHjZgZVEZISWW7wu5+l54WCjN8P++9oV4kcP2CddH7Gqgi/S6X0s1aJ7 CuytX0UEe+Ugdhrh26bnBv5sXuOnmqxjxuV4h/K70Wj6mGRw7nyKIlaLM5Zx9lz0NIZM H3mQ== X-Gm-Message-State: AOAM530Zirwfkp3zN1lmvJWuHNrY3x7tv1mRXSlsmj2zGMzW4712QAcK JaGxU59ko2EdlNfzOISW3/v1TtDbFPM= X-Received: by 2002:a17:902:b718:: with SMTP id d24mr7690094pls.185.1591330676832; Thu, 04 Jun 2020 21:17:56 -0700 (PDT) Received: from localhost.localdomain (174-21-143-238.tukw.qwest.net. [174.21.143.238]) by smtp.gmail.com with ESMTPSA id h15sm5440902pgl.12.2020.06.04.21.17.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jun 2020 21:17:56 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 17/17] tests/tcg/aarch64: Add mte smoke tests Date: Thu, 4 Jun 2020 21:17:33 -0700 Message-Id: <20200605041733.415188-18-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200605041733.415188-1-richard.henderson@linaro.org> References: <20200605041733.415188-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1044; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1044.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org, steplong@quicinc.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Richard Henderson --- tests/tcg/aarch64/mte.h | 54 +++++++++++++++++++++++++++++++ tests/tcg/aarch64/mte-1.c | 25 ++++++++++++++ tests/tcg/aarch64/mte-2.c | 42 ++++++++++++++++++++++++ tests/tcg/aarch64/mte-3.c | 47 +++++++++++++++++++++++++++ tests/tcg/aarch64/Makefile.target | 4 +++ 5 files changed, 172 insertions(+) create mode 100644 tests/tcg/aarch64/mte.h create mode 100644 tests/tcg/aarch64/mte-1.c create mode 100644 tests/tcg/aarch64/mte-2.c create mode 100644 tests/tcg/aarch64/mte-3.c -- 2.25.1 diff --git a/tests/tcg/aarch64/mte.h b/tests/tcg/aarch64/mte.h new file mode 100644 index 0000000000..7bcc416e6a --- /dev/null +++ b/tests/tcg/aarch64/mte.h @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef PR_SET_TAGGED_ADDR_CTRL +# define PR_SET_TAGGED_ADDR_CTRL 55 +#endif +#ifndef PR_TAGGED_ADDR_ENABLE +# define PR_TAGGED_ADDR_ENABLE (1UL << 0) +#endif +#ifndef PR_MTE_TCF_SHIFT +# define PR_MTE_TCF_SHIFT 1 +# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TAG_SHIFT 3 +#endif + +#ifndef PROT_MTE +# define PROT_MTE 0x20 +#endif + +#ifndef SEGV_MTEAERR +# define SEGV_MTEAERR 8 +# define SEGV_MTESERR 9 +#endif + +static void enable_mte(int tcf) +{ + int r = prctl(PR_SET_TAGGED_ADDR_CTRL, + PR_TAGGED_ADDR_ENABLE | tcf | (0xfffe << PR_MTE_TAG_SHIFT), + 0, 0, 0); + if (r < 0) { + perror("PR_SET_TAGGED_ADDR_CTRL"); + exit(2); + } +} + +static void *alloc_mte_page(void) +{ + void *p = mmap(NULL, getpagesize(), + PROT_READ | PROT_WRITE | PROT_MTE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (p == MAP_FAILED) { + perror("mmap PROT_MTE"); + exit(2); + } + return p; +} diff --git a/tests/tcg/aarch64/mte-1.c b/tests/tcg/aarch64/mte-1.c new file mode 100644 index 0000000000..e62c842e85 --- /dev/null +++ b/tests/tcg/aarch64/mte-1.c @@ -0,0 +1,25 @@ +/* + * Memory tagging, basic pass cases. + */ + +#include "mte.h" + +int main(int ac, char **av) +{ + int *p0, *p1, *p2; + long c; + + enable_mte(PR_MTE_TCF_NONE); + p0 = alloc_mte_page(); + + asm("irg %0,%1,%2" : "=r"(p1) : "r"(p0), "r"(1)); + assert(p1 != p0); + asm("subp %0,%1,%2" : "=r"(c) : "r"(p0), "r"(p1)); + assert(c == 0); + + asm("stg %0, [%0]" : : "r"(p1)); + asm("ldg %0, [%1]" : "=r"(p2) : "r"(p0), "0"(p0)); + assert(p1 == p2); + + return 0; +} diff --git a/tests/tcg/aarch64/mte-2.c b/tests/tcg/aarch64/mte-2.c new file mode 100644 index 0000000000..68b5708462 --- /dev/null +++ b/tests/tcg/aarch64/mte-2.c @@ -0,0 +1,42 @@ +/* + * Memory tagging, basic fail cases. + */ + +#include "mte.h" + +void pass(int sig, siginfo_t *info, void *uc) +{ + assert(info->si_code == SEGV_MTESERR); + exit(0); +} + +int main(int ac, char **av) +{ + struct sigaction sa; + int *p0, *p1, *p2; + long excl = 1; + + enable_mte(PR_MTE_TCF_SYNC); + p0 = alloc_mte_page(); + + /* Create two differently tagged pointers. */ + asm("irg %0,%1,%2" : "=r"(p1) : "r"(p0), "r"(excl)); + asm("gmi %0,%1,%0" : "+r"(excl) : "r" (p1)); + assert(excl != 1); + asm("irg %0,%1,%2" : "=r"(p2) : "r"(p0), "r"(excl)); + assert(p1 != p2); + + /* Store the tag from the first pointer. */ + asm("stg %0, [%0]" : : "r"(p1)); + + *p1 = 0; + + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = pass; + sa.sa_flags = SA_SIGINFO; + sigaction(SIGSEGV, &sa, NULL); + + *p2 = 0; + + abort(); +} diff --git a/tests/tcg/aarch64/mte-3.c b/tests/tcg/aarch64/mte-3.c new file mode 100644 index 0000000000..6f034cdb01 --- /dev/null +++ b/tests/tcg/aarch64/mte-3.c @@ -0,0 +1,47 @@ +/* + * Memory tagging, basic fail cases. + */ + +#include "mte.h" + +void pass(int sig, siginfo_t *info, void *uc) +{ + assert(info->si_code == SEGV_MTEAERR); + exit(0); +} + +int main(int ac, char **av) +{ + struct sigaction sa; + int *p0, *p1, *p2; + long excl = 1; + + enable_mte(PR_MTE_TCF_ASYNC); + p0 = alloc_mte_page(); + + /* Create two differently tagged pointers. */ + asm("irg %0,%1,%2" : "=r"(p1) : "r"(p0), "r"(excl)); + asm("gmi %0,%1,%0" : "+r"(excl) : "r" (p1)); + assert(excl != 1); + asm("irg %0,%1,%2" : "=r"(p2) : "r"(p0), "r"(excl)); + assert(p1 != p2); + + /* Store the tag from the first pointer. */ + asm("stg %0, [%0]" : : "r"(p1)); + + *p1 = 0; + + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = pass; + sa.sa_flags = SA_SIGINFO; + sigaction(SIGSEGV, &sa, NULL); + + /* + * Signal for async error will happen eventually. + * For a real kernel this should be after the next IRQ (e.g. timer). + * For qemu linux-user, we kick the cpu and exit at the next TB. + * In either case, loop until this happens (or killed by timeout). + */ + *p2 = 0; + while (1); +} diff --git a/tests/tcg/aarch64/Makefile.target b/tests/tcg/aarch64/Makefile.target index cf84787eb6..2269c264df 100644 --- a/tests/tcg/aarch64/Makefile.target +++ b/tests/tcg/aarch64/Makefile.target @@ -31,6 +31,10 @@ bti-%: CFLAGS += -mbranch-protection=standard bti-%: LDFLAGS += -nostdlib endif +# MTE Tests +AARCH64_TESTS += mte-1 mte-2 mte-3 +mte-%: CFLAGS += -march=armv8.5-a+memtag + # Semihosting smoke test for linux-user AARCH64_TESTS += semihosting run-semihosting: semihosting