From patchwork Mon Jan 28 17:39:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 156813 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3700330jaa; Mon, 28 Jan 2019 09:44:29 -0800 (PST) X-Google-Smtp-Source: ALg8bN5JIQ8fI5Lyr/N6H841/o5za3AL7AVYzl2/+05WukiWMRWhoyAtJJZZX+bXOXiw1fqlz7ca X-Received: by 2002:adf:d0c9:: with SMTP id z9mr21711406wrh.317.1548697469741; Mon, 28 Jan 2019 09:44:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548697469; cv=none; d=google.com; s=arc-20160816; b=AD8ovp63sefj74lpcXORBpUGGW7vGKQ2CHjy8loe5NiiNEbwGftMSxWEPLnUKyto9p Bh1EOYl63Mucl/acjwjaH2s4opmeHnSrXUSYpeWrN4yc8X/T78tQicTunssgLaU4nAk7 9iYBYb32fvaa/0+Stzmss/fq7oll4L1AclY4tfafFahQHg/6iiY0sN9Ykqj/FRjzocqF uhR0FlntiUwa3JUB2EWmnwV3UDcYCRRbYoW96fTDYmmrL07V6KPVgmPNli9Z6hljzIV+ BJB7KHaUOlzLkui9G85HOPrbpQL65N3RHEzkEnbtcCTM9qUksQIGZ40kn7IFQeG5Yxqb kolw== 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:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=uNZOim1otVCdMzCZrGI3QsadJO/8xlW0Xrr3vFSwptQ=; b=uliKUePlVJ7NGHUkg0WTRySQ0axatbaIcBUFMMB96KlyQgylIsUbqSFyZopAn7LBrV VeFSZmc+RENPmrYr5jgPpVVL4Yh12hjy3rgfcEpmbWBAMVewrP/1QntDw77tBbNlBFN0 imleW083CrTFA+HWNXlrTvBjqytQf8gSconcgQMfW1z8xnY2bst0JIgey5yd6UwMC7yx IP0za1LGvA+7Pb/85dDnMdpgygUsew/pPhejq3NXOBPgsLUOGt+qRXds96A+ALAQppfB lXZ6AYrZ+i+M8pztFqNmzWLHwK/eGT/Cm0rY35CHDF4VF4VHqahLLJIpIwtZ8de9OcvR 261w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Ip5aWhzi; 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 x22si42412wmi.101.2019.01.28.09.44.29 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 28 Jan 2019 09:44:29 -0800 (PST) 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=Ip5aWhzi; 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 ([127.0.0.1]:35983 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1goAxQ-00054o-Hu for patch@linaro.org; Mon, 28 Jan 2019 12:44:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60239) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1goAsw-00027u-W8 for qemu-devel@nongnu.org; Mon, 28 Jan 2019 12:39:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1goAss-0003XJ-Cs for qemu-devel@nongnu.org; Mon, 28 Jan 2019 12:39:50 -0500 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]:43177) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1goAsr-0003VA-Su for qemu-devel@nongnu.org; Mon, 28 Jan 2019 12:39:46 -0500 Received: by mail-wr1-x441.google.com with SMTP id r10so19003391wrs.10 for ; Mon, 28 Jan 2019 09:39:45 -0800 (PST) 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=uNZOim1otVCdMzCZrGI3QsadJO/8xlW0Xrr3vFSwptQ=; b=Ip5aWhziAeS9F4J6mgEhiaBhHqZeZ3TfQJvRd2NF0ACc6AHMFkRd+CKpQl8na5755b 3W3tIYmDlCm2wtNp6YIV79qrZg6W1Mye1yufgqRNwN0I2yivuy2dNi52hVEMl0Nt0vc4 CaLWiaFZkCyiAmj9jfPxnQk23zGIhQP5sKdcs= 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=uNZOim1otVCdMzCZrGI3QsadJO/8xlW0Xrr3vFSwptQ=; b=qwKDNuzkyWgNNl5IM3v+ARhZFNnxXQqfy1ylcLqMKNihkgO1HMjlMuKzEvnVVZLAU2 7O7x43O5UCHHDQd5iWL0RbrrmGG43i0VolTZbaBNeMgrkwysxX0FTf+khyU6r2Tx5PCf 0sS0U5ojhBnvSY8It0H7bU7ZMgz8PpkapWhUMpObbLlCfVzqLCTAT6XCKaiSEw3viA3S HDoolafwasxyx2CjxwFIzLpmzoMOArr3LLFBGYRKAokOUkXiLrU/W9mRiepxoI1zZqzM hUhGoPup/Xwa86zJWd4VkWKRoHNrIk+wHMzi4Oxxq65YopJCfWVi5M7XQV0/X10fkxvG 2BQg== X-Gm-Message-State: AHQUAuaKLP4PQZr/PJ0lrgVqlP0IqadJJeg+UXnValuzAxG8beKKwX/J mnau6vv2u9NJcPB2/Pn9juROGg== X-Received: by 2002:adf:a58a:: with SMTP id g10mr1329386wrc.3.1548697184278; Mon, 28 Jan 2019 09:39:44 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id f14sm97017077wrv.56.2019.01.28.09.39.41 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 09:39:42 -0800 (PST) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 1BA6E3E07DF; Mon, 28 Jan 2019 17:39:41 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Mon, 28 Jan 2019 17:39:40 +0000 Message-Id: <20190128173940.25813-5-alex.bennee@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190128173940.25813-1-alex.bennee@linaro.org> References: <20190128173940.25813-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::441 Subject: [Qemu-devel] [PATCH v1 4/4] tests/tcg/aarch64: userspace system register test X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , qemu-arm@nongnu.org, =?utf-8?q?Alex_Benn=C3=A9e?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This tests a bunch of registers that the kernel allows userspace to read including the CPUID registers. Signed-off-by: Alex Bennée --- v4 - also test for extra bits that shouldn't be exposed --- tests/tcg/aarch64/Makefile.target | 2 +- tests/tcg/aarch64/sysregs.c | 120 ++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/aarch64/sysregs.c -- 2.17.1 diff --git a/tests/tcg/aarch64/Makefile.target b/tests/tcg/aarch64/Makefile.target index 08c45b8470..cc1a7eb486 100644 --- a/tests/tcg/aarch64/Makefile.target +++ b/tests/tcg/aarch64/Makefile.target @@ -7,7 +7,7 @@ VPATH += $(AARCH64_SRC) # we don't build any of the ARM tests AARCH64_TESTS=$(filter-out $(ARM_TESTS), $(TESTS)) -AARCH64_TESTS+=fcvt +AARCH64_TESTS+=fcvt sysregs TESTS:=$(AARCH64_TESTS) fcvt: LDFLAGS+=-lm diff --git a/tests/tcg/aarch64/sysregs.c b/tests/tcg/aarch64/sysregs.c new file mode 100644 index 0000000000..8e11288ee3 --- /dev/null +++ b/tests/tcg/aarch64/sysregs.c @@ -0,0 +1,120 @@ +/* + * Check emulated system register access for linux-user mode. + * + * See: https://www.kernel.org/doc/Documentation/arm64/cpu-feature-registers.txt + */ + +#include +#include +#include +#include +#include +#include + +int failed_mask_count; + +#define get_cpu_reg(id) ({ \ + unsigned long __val = 0xdeadbeef; \ + asm("mrs %0, "#id : "=r" (__val)); \ + printf("%-20s: 0x%016lx\n", #id, __val); \ + __val; \ + }) + +#define get_cpu_reg_check_mask(id, mask) ({ \ + unsigned long __cval = get_cpu_reg(id); \ + unsigned long __extra = __cval & ~mask; \ + if (__extra) { \ + printf("%-20s: 0x%016lx\n", " !!extra bits!!", __extra); \ + failed_mask_count++; \ + } \ +}) + +bool should_fail; +int should_fail_count; +int should_not_fail_count; +uintptr_t failed_pc[10]; + +void sigill_handler(int signo, siginfo_t *si, void *data) +{ + ucontext_t *uc = (ucontext_t *)data; + + if (should_fail) { + should_fail_count++; + } else { + uintptr_t pc = (uintptr_t) uc->uc_mcontext.pc; + failed_pc[should_not_fail_count++] = pc; + } + uc->uc_mcontext.pc += 4; +} + +int main(void) +{ + struct sigaction sa; + + /* Hook in a SIGILL handler */ + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_flags = SA_SIGINFO; + sa.sa_sigaction = &sigill_handler; + sigemptyset(&sa.sa_mask); + + if (sigaction(SIGILL, &sa, 0) != 0) { + perror("sigaction"); + return 1; + } + + /* since 4.12 */ + printf("Checking CNT registers\n"); + + get_cpu_reg(ctr_el0); + get_cpu_reg(cntvct_el0); + get_cpu_reg(cntfrq_el0); + + /* when (getauxval(AT_HWCAP) & HWCAP_CPUID), since 4.11*/ + if (!(getauxval(AT_HWCAP) & HWCAP_CPUID)) { + printf("CPUID registers unavailable\n"); + return 1; + } else { + printf("Checking CPUID registers\n"); + } + + /* + * Some registers only expose some bits to user-space. Anything + * that is IMDEF is exported as 0 to user-space. + */ + get_cpu_reg_check_mask(id_aa64isar0_el1, 0x000fffffff0ffff0ULL); + get_cpu_reg_check_mask(id_aa64isar1_el1, 0x00000000ffffffffULL); + get_cpu_reg(id_aa64mmfr0_el1); + get_cpu_reg(id_aa64mmfr1_el1); + get_cpu_reg_check_mask(id_aa64pfr0_el1, 0x000f000f0ff0000ULL); + get_cpu_reg(id_aa64pfr1_el1); + get_cpu_reg(id_aa64dfr0_el1); + get_cpu_reg(id_aa64dfr1_el1); + + get_cpu_reg_check_mask(midr_el1, 0x00000000ffffffffULL); + get_cpu_reg(mpidr_el1); + /* REVIDR is all IMPDEF so should be all zeros to user-space */ + get_cpu_reg_check_mask(revidr_el1, 0x0); + + printf("Remaining registers should fail\n"); + should_fail = true; + + /* Unexposed register access causes SIGILL */ + get_cpu_reg(id_mmfr0_el1); + + if (should_not_fail_count > 0) { + int i; + for (i = 0; i < should_not_fail_count; i++) { + uintptr_t pc = failed_pc[i]; + uint32_t insn = *(uint32_t *) pc; + printf("insn %#x @ %#lx unexpected FAIL\n", insn, pc); + } + return 1; + } + + if (failed_mask_count > 0) { + printf("Extra information leaked to user-space!\n"); + return 1; + } + + return should_fail_count == 1 ? 0 : 1; +}