From patchwork Wed May 20 17:28:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 186999 Delivered-To: patch@linaro.org Received: by 2002:a92:5b0a:0:0:0:0:0 with SMTP id p10csp820278ilb; Wed, 20 May 2020 10:32:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKvdHes1mX5Tt3ql4Hni3tKwaXb8GrofpXZMIkZOwwuy8Sybr7i3VDV+YzdTnbVctApbaO X-Received: by 2002:a05:6214:186b:: with SMTP id eh11mr5896103qvb.35.1589995936692; Wed, 20 May 2020 10:32:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589995936; cv=none; d=google.com; s=arc-20160816; b=Vgw98ALyyIq/6MTMHV+L+poRKb2Ocf6pPaVXmXdHZLZpHCkvvyB1/XqmzhEb/UpO5s aLl2o6/CXm2mtx5jtWH0dvZfphl3MNZRyMWEQOVRp7Xk73rh07BEfyKzMu67w3eNBDZ0 d9JYjBmuC4QVjjpW6MV2NZNreMz393zRrpxigctIMQv9KfSuYg3JfU4pCRyOhyj1SI5u 2FSWikD78R9G9zzJIC4bepezGgPsGLnzwi6a2mVGIM12NeHerMfF77czjRGwKLG9OuTi DvTFlHDTW1UxnpHDMn8f2s16ydlhytuMqqq0ipFTXvKI+zS60KIuJmNQYhwINA9jiq4l ZCwg== 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=aq4v7IxgSTWwXHpmoImcRMGs5taMUuju/WMDtCLJnVA=; b=fwKdMY50AQ0aEwKoR1uivGNxfDnYiX0bo0aaTfvdtj7L5+rMKYc0rPsCbnFVtY8Q1F ULcmSsnOstzw9JA6YJUmJqEZ8esjPxKh6CkjatLOhMxD+97yg//f9qnranBwtJCc8bs7 VYaWHqwxiHW5/F3ZzB1D6YXHXfsj5Jo0j8jOmmpmqt2gEOzfEUR9nwtwn4fPc8jReyun wfR4mqiqg3ycupUg0qOEuUjLdEGYPDbx6zX3xEFkEWoxgQI7HfbQC6KZHsFxl5uWO/eE aWCuPB3GYy/Lvwb7DHmR00vvZPAhaT4on4LUcBs6HUb9+1+pqQ/1oHruK/sx6pE1rAEe 8rDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=saSDNi8L; 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=pass (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 k22si2065721qki.209.2020.05.20.10.32.16 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 20 May 2020 10:32:16 -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=pass header.i=@linaro.org header.s=google header.b=saSDNi8L; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:44912 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jbSZk-0007qa-38 for patch@linaro.org; Wed, 20 May 2020 13:32:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53582) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jbSVn-0001oo-PM for qemu-devel@nongnu.org; Wed, 20 May 2020 13:28:11 -0400 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]:38307) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jbSVm-0004L6-ND for qemu-devel@nongnu.org; Wed, 20 May 2020 13:28:11 -0400 Received: by mail-pg1-x541.google.com with SMTP id u5so1754805pgn.5 for ; Wed, 20 May 2020 10:28:10 -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=aq4v7IxgSTWwXHpmoImcRMGs5taMUuju/WMDtCLJnVA=; b=saSDNi8LLrzwncRHhVgKiAnvV6o6W6isX/L1jINEzyABAeYqIjH1tkDCYxx3VPzYrS 9tx+PDxN1q/SLgr7iRi/G8IYZ+XUQMOLKWXwQ+5xl42AwwnWcfBq94YfD9yVSNpbWmsS nImuU9bYXZScWvN3V0hCWs6NKAXOfkWAjTPmNaQC0BSZuIj6UQ4gKZfiHOrsTEDPMU1O b9TlLZz4P9Y49MGA9O1C2wo/2lo3tesIblGA3y3y/UDKmUXh1x+mxh0ROxNY7JrjNxXc 9ZwggUrXq9BF9bndaeDkR22YyvwcUG/GruD/ad84xP5OFFbMLKQUNrHMzC2k70vkoECQ QfKw== 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=aq4v7IxgSTWwXHpmoImcRMGs5taMUuju/WMDtCLJnVA=; b=QsKXx0XUwAKR5VdPTWKNNyWn0m+1TcEtqrzlvYpz4qEejQvIwIL7DdIPO93YivSxML KLkpjUj9ctkZW/opMIOIvTMMtCPp6XOcHGHd1q8nu7bqPAdc0ipt84mPoCszuPnNQPHa uPQHkKrZRpFPLT6QrIG8zyjk1U48rH3pAKJtMFvLMbXBIbPOXh8LOC/NVeHsV4SpK7KS JOdgYETdNP5RKxF3+e0YiRVcXcI7gE7kY0YARiBNrRduSoiopI60NPiux66GnNmlx55O uxuewA9bFT9PrMph4SodnFff1nVh72O7zNchUMKhHUN9kFmmJk1MktUYtl3uUq3VmBnS lSyg== X-Gm-Message-State: AOAM530nf2U1CZhDLGow6GOF5FzKsE2t3Bc9Vuch3ZgnkI+I9LjgODwk bBwZRDbjfrs+Q33jDmP35N6iI/rzUeA= X-Received: by 2002:a62:4dc3:: with SMTP id a186mr5204501pfb.269.1589995688856; Wed, 20 May 2020 10:28:08 -0700 (PDT) Received: from localhost.localdomain (174-21-143-238.tukw.qwest.net. [174.21.143.238]) by smtp.gmail.com with ESMTPSA id k18sm2643672pfg.217.2020.05.20.10.28.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2020 10:28:08 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v9 5/5] tests/tcg/aarch64: Add bti smoke test Date: Wed, 20 May 2020 10:28:00 -0700 Message-Id: <20200520172800.8499-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200520172800.8499-1-richard.henderson@linaro.org> References: <20200520172800.8499-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::541; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x541.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, alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This test requires gcc 10 for -mbranch-protection=standard. Signed-off-by: Richard Henderson --- v9: Expect and require gcc 10. --- tests/tcg/aarch64/bti-1.c | 62 +++++++++++++++++++++++++++++++ tests/tcg/aarch64/bti-crt.inc.c | 51 +++++++++++++++++++++++++ tests/tcg/aarch64/Makefile.target | 7 ++++ tests/tcg/configure.sh | 4 ++ 4 files changed, 124 insertions(+) create mode 100644 tests/tcg/aarch64/bti-1.c create mode 100644 tests/tcg/aarch64/bti-crt.inc.c -- 2.20.1 Reviewed-by: Peter Maydell diff --git a/tests/tcg/aarch64/bti-1.c b/tests/tcg/aarch64/bti-1.c new file mode 100644 index 0000000000..61924f0d7a --- /dev/null +++ b/tests/tcg/aarch64/bti-1.c @@ -0,0 +1,62 @@ +/* + * Branch target identification, basic notskip cases. + */ + +#include "bti-crt.inc.c" + +static void skip2_sigill(int sig, siginfo_t *info, ucontext_t *uc) +{ + uc->uc_mcontext.pc += 8; + uc->uc_mcontext.pstate = 1; +} + +#define NOP "nop" +#define BTI_N "hint #32" +#define BTI_C "hint #34" +#define BTI_J "hint #36" +#define BTI_JC "hint #38" + +#define BTYPE_1(DEST) \ + asm("mov %0,#1; adr x16, 1f; br x16; 1: " DEST "; mov %0,#0" \ + : "=r"(skipped) : : "x16") + +#define BTYPE_2(DEST) \ + asm("mov %0,#1; adr x16, 1f; blr x16; 1: " DEST "; mov %0,#0" \ + : "=r"(skipped) : : "x16", "x30") + +#define BTYPE_3(DEST) \ + asm("mov %0,#1; adr x15, 1f; br x15; 1: " DEST "; mov %0,#0" \ + : "=r"(skipped) : : "x15") + +#define TEST(WHICH, DEST, EXPECT) \ + do { WHICH(DEST); fail += skipped ^ EXPECT; } while (0) + + +int main() +{ + int fail = 0; + int skipped; + + /* Signal-like with SA_SIGINFO. */ + signal_info(SIGILL, skip2_sigill); + + TEST(BTYPE_1, NOP, 1); + TEST(BTYPE_1, BTI_N, 1); + TEST(BTYPE_1, BTI_C, 0); + TEST(BTYPE_1, BTI_J, 0); + TEST(BTYPE_1, BTI_JC, 0); + + TEST(BTYPE_2, NOP, 1); + TEST(BTYPE_2, BTI_N, 1); + TEST(BTYPE_2, BTI_C, 0); + TEST(BTYPE_2, BTI_J, 1); + TEST(BTYPE_2, BTI_JC, 0); + + TEST(BTYPE_3, NOP, 1); + TEST(BTYPE_3, BTI_N, 1); + TEST(BTYPE_3, BTI_C, 1); + TEST(BTYPE_3, BTI_J, 0); + TEST(BTYPE_3, BTI_JC, 0); + + return fail; +} diff --git a/tests/tcg/aarch64/bti-crt.inc.c b/tests/tcg/aarch64/bti-crt.inc.c new file mode 100644 index 0000000000..47805f4e35 --- /dev/null +++ b/tests/tcg/aarch64/bti-crt.inc.c @@ -0,0 +1,51 @@ +/* + * Minimal user-environment for testing BTI. + * + * Normal libc is not (yet) built with BTI support enabled, + * and so could generate a BTI TRAP before ever reaching main. + */ + +#include +#include +#include +#include + +int main(void); + +void _start(void) +{ + exit(main()); +} + +void exit(int ret) +{ + register int x0 __asm__("x0") = ret; + register int x8 __asm__("x8") = __NR_exit; + + asm volatile("svc #0" : : "r"(x0), "r"(x8)); + __builtin_unreachable(); +} + +/* + * Irritatingly, the user API struct sigaction does not match the + * kernel API struct sigaction. So for simplicity, isolate the + * kernel ABI here, and make this act like signal. + */ +void signal_info(int sig, void (*fn)(int, siginfo_t *, ucontext_t *)) +{ + struct kernel_sigaction { + void (*handler)(int, siginfo_t *, ucontext_t *); + unsigned long flags; + unsigned long restorer; + unsigned long mask; + } sa = { fn, SA_SIGINFO, 0, 0 }; + + register int x0 __asm__("x0") = sig; + register void *x1 __asm__("x1") = &sa; + register void *x2 __asm__("x2") = 0; + register int x3 __asm__("x3") = sizeof(unsigned long); + register int x8 __asm__("x8") = __NR_rt_sigaction; + + asm volatile("svc #0" + : : "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x8) : "memory"); +} diff --git a/tests/tcg/aarch64/Makefile.target b/tests/tcg/aarch64/Makefile.target index 312f36cde5..cf84787eb6 100644 --- a/tests/tcg/aarch64/Makefile.target +++ b/tests/tcg/aarch64/Makefile.target @@ -24,6 +24,13 @@ run-pauth-%: QEMU_OPTS += -cpu max pauth-%: CFLAGS += -march=armv8.3-a endif +# BTI Tests +ifneq ($(DOCKER_IMAGE)$(CROSS_CC_HAS_ARMV8_BTI),) +AARCH64_TESTS += bti-1 +bti-%: CFLAGS += -mbranch-protection=standard +bti-%: LDFLAGS += -nostdlib +endif + # Semihosting smoke test for linux-user AARCH64_TESTS += semihosting run-semihosting: semihosting diff --git a/tests/tcg/configure.sh b/tests/tcg/configure.sh index 2326f97856..ebfe3e2ce5 100755 --- a/tests/tcg/configure.sh +++ b/tests/tcg/configure.sh @@ -231,6 +231,10 @@ for target in $target_list; do -march=armv8.3-a -o $TMPE $TMPC; then echo "CROSS_CC_HAS_ARMV8_3=y" >> $config_target_mak fi + if do_compiler "$target_compiler" $target_compiler_cflags \ + -mbranch-protection=standard -o $TMPE $TMPC; then + echo "CROSS_CC_HAS_ARMV8_BTI=y" >> $config_target_mak + fi ;; esac