From patchwork Wed Aug 4 08:58:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12418213 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=-17.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,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 1506DC4338F for ; Wed, 4 Aug 2021 09:16:32 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 C495A60EBB for ; Wed, 4 Aug 2021 09:16:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C495A60EBB Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=hwy7M/Kog3cAOXpaddnATbxWero0ywIPpAkI3A6kcp8=; b=OdbzHts2vPjLfFiFS48F1fQjJ7 dhP5XdofuBD/RGDzK3z6il5LGJT0VkUfkrJeaETcxiKTyoS4hLEfbkisWo+RLuvNg0T1hszDIh5my gcSB3yVXgD/1DwYHuT+9a52tKDf6crUWeVYBjMpH7vTpU6eLtwFSruIMm50uaAoGRlW1Cg5bHC7S+ VBfBtxlVlwIADwnsJST1Gz58NsFAfp4lhgEXucavzRgVrh3jshJxLBR/HMGJ9XR648uxdLFDcVjqs tTo0AYUhf0rvK17VoWNYt21RWZpWDFSO8+AMNln2xr0DHOpv4WoeSxiuh48mIYXPUKfQZVy2SnU4z D3wV29zg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mBCyS-005Law-SW; Wed, 04 Aug 2021 09:14:06 +0000 Received: from mail-qv1-xf49.google.com ([2607:f8b0:4864:20::f49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mBCjq-005Ff0-0A for linux-arm-kernel@lists.infradead.org; Wed, 04 Aug 2021 08:58:59 +0000 Received: by mail-qv1-xf49.google.com with SMTP id t18-20020a0cd4120000b02902fbda5d4988so1283598qvh.11 for ; Wed, 04 Aug 2021 01:58:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=P+wj7LgLjEuWRbpjKCXjEpBSkxsEIWcdrd+/OVNGjgQ=; b=U2RWOObCpS8DFZJ10Fp2o+XLbL0l1Du9Q2Q9GE9WrsZOYR4vwBJQlSHULYkGmuvObp qkHivooxec/DqyZKsQVcgoEPCJi+6nxBSJa++GLLq4UkYPeTO/PnhQU8BxXCanAd/n3b 1VAAXmzHLRgzm8XB6suWss1/HgHWX8AsDQjtvEEKe6uBRGu5tM62spN1T/IizoXmP1ZL /bcqtSUX18eaq6JGBkF1Jl6CZbI9tnVwffM2msHBTiN69mXBBWf9eO+ZEhofwM/eDIVc AgUFDx/s8u2xV/CUIjyX8OpmxWHZj6alRmINi+u/RPuPYEpQrLqUs/YiaPtSGXJY1Ysc UVRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=P+wj7LgLjEuWRbpjKCXjEpBSkxsEIWcdrd+/OVNGjgQ=; b=YG4RWrMePuTQa78hjX91fzWB2vMpUt4euhtWrPVCwCEsfzRRYRw8Y4EsoafIQQ3fGa e7spR1XxKdfYPgHw4IhcCtum8cb1IvaC5MUio2xpdD1hDKrtPVl3RKTm84T0bLylFEo+ 1MlJqovHNKngzVbPuIpOP6FyBZfLyZ+RlcqkVDvz9yeqLf1h7eqH0fuWDbLBv25Nql4+ QpzS6DnYTR4F2vCOF4MsFxRUlDH4KiBuuP2BLNxmUrNGLu4pkE+P/5F+lJ3jWa2ur1nw TnDJn0pSQzJXHTfoHt3/BRo5zf512n7H/fk5Y+iWcAGmcWc30me5SC8gVOVr7DPtvyyr HGSQ== X-Gm-Message-State: AOAM5325ko6bp1JVJnGrfvL7Bh/+jZxJRyq2P5e0Sog18avnLydM+U32 EV7ZxJ75I+Ykp8u2zWRfF5c1DAlAcFc= X-Google-Smtp-Source: ABdhPJxwuRb6Dx8CoyJvkFTha9TsHjRjV9Svfskn24Ka80JcxAcKjGHT4By5V5+EQXZYVffGDUwuCZ/2cFY= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6214:e67:: with SMTP id jz7mr23858033qvb.0.1628067536659; Wed, 04 Aug 2021 01:58:56 -0700 (PDT) Date: Wed, 4 Aug 2021 08:58:18 +0000 In-Reply-To: <20210804085819.846610-1-oupton@google.com> Message-Id: <20210804085819.846610-21-oupton@google.com> Mime-Version: 1.0 References: <20210804085819.846610-1-oupton@google.com> X-Mailer: git-send-email 2.32.0.605.g8dce9f2422-goog Subject: [PATCH v6 20/21] selftests: KVM: Test physical counter offsetting From: Oliver Upton To: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu Cc: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Peter Shier , Jim Mattson , David Matlack , Ricardo Koller , Jing Zhang , Raghavendra Rao Anata , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones , Will Deacon , Catalin Marinas , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210804_015858_103121_2138F2C7 X-CRM114-Status: GOOD ( 15.38 ) 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 Test that userspace adjustment of the guest physical counter-timer results in the correct view within the guest. Cc: Andrew Jones Signed-off-by: Oliver Upton Reviewed-by: Andrew Jones --- .../selftests/kvm/include/aarch64/processor.h | 12 +++++++ .../kvm/system_counter_offset_test.c | 31 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/include/aarch64/processor.h b/tools/testing/selftests/kvm/include/aarch64/processor.h index 3168cdbae6ee..7f53d90e9512 100644 --- a/tools/testing/selftests/kvm/include/aarch64/processor.h +++ b/tools/testing/selftests/kvm/include/aarch64/processor.h @@ -141,4 +141,16 @@ static inline uint64_t read_cntvct_ordered(void) return r; } +static inline uint64_t read_cntpct_ordered(void) +{ + uint64_t r; + + __asm__ __volatile__("isb\n\t" + "mrs %0, cntpct_el0\n\t" + "isb\n\t" + : "=r"(r)); + + return r; +} + #endif /* SELFTEST_KVM_PROCESSOR_H */ diff --git a/tools/testing/selftests/kvm/system_counter_offset_test.c b/tools/testing/selftests/kvm/system_counter_offset_test.c index ac933db83d03..82d26a45cc48 100644 --- a/tools/testing/selftests/kvm/system_counter_offset_test.c +++ b/tools/testing/selftests/kvm/system_counter_offset_test.c @@ -57,6 +57,9 @@ static uint64_t host_read_guest_system_counter(struct test_case *test) enum arch_counter { VIRTUAL, + PHYSICAL, + /* offset physical, read virtual */ + PHYSICAL_READ_VIRTUAL, }; struct test_case { @@ -68,32 +71,54 @@ static struct test_case test_cases[] = { { .counter = VIRTUAL, .offset = 0 }, { .counter = VIRTUAL, .offset = 180 * NSEC_PER_SEC }, { .counter = VIRTUAL, .offset = -180 * NSEC_PER_SEC }, + { .counter = PHYSICAL, .offset = 0 }, + { .counter = PHYSICAL, .offset = 180 * NSEC_PER_SEC }, + { .counter = PHYSICAL, .offset = -180 * NSEC_PER_SEC }, + { .counter = PHYSICAL_READ_VIRTUAL, .offset = 0 }, + { .counter = PHYSICAL_READ_VIRTUAL, .offset = 180 * NSEC_PER_SEC }, + { .counter = PHYSICAL_READ_VIRTUAL, .offset = -180 * NSEC_PER_SEC }, }; static void check_preconditions(struct kvm_vm *vm) { - if (vcpu_has_reg(vm, VCPU_ID, KVM_REG_ARM_TIMER_OFFSET)) + if (vcpu_has_reg(vm, VCPU_ID, KVM_REG_ARM_TIMER_OFFSET) && + !_vcpu_has_device_attr(vm, VCPU_ID, KVM_ARM_VCPU_TIMER_CTRL, + KVM_ARM_VCPU_TIMER_OFFSET)) return; - print_skip("KVM_REG_ARM_TIMER_OFFSET not supported; skipping test"); + print_skip("KVM_REG_ARM_TIMER_OFFSET|KVM_ARM_VCPU_TIMER_OFFSET not supported; skipping test"); exit(KSFT_SKIP); } static void setup_system_counter(struct kvm_vm *vm, struct test_case *test) { + uint64_t cntvoff, cntpoff; struct kvm_one_reg reg = { .id = KVM_REG_ARM_TIMER_OFFSET, - .addr = (__u64)&test->offset, + .addr = (__u64)&cntvoff, }; + if (test->counter == VIRTUAL) { + cntvoff = test->offset; + cntpoff = 0; + } else { + cntvoff = 0; + cntpoff = test->offset; + } + vcpu_set_reg(vm, VCPU_ID, ®); + vcpu_access_device_attr(vm, VCPU_ID, KVM_ARM_VCPU_TIMER_CTRL, + KVM_ARM_VCPU_TIMER_OFFSET, &cntpoff, true); } static uint64_t guest_read_system_counter(struct test_case *test) { switch (test->counter) { case VIRTUAL: + case PHYSICAL_READ_VIRTUAL: return read_cntvct_ordered(); + case PHYSICAL: + return read_cntpct_ordered(); default: GUEST_ASSERT(0); }