From patchwork Thu Sep 16 18:15:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12499947 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.1 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, 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 4ACF0C433F5 for ; Thu, 16 Sep 2021 18:33:51 +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 0EEF561139 for ; Thu, 16 Sep 2021 18:33:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 0EEF561139 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=umZaS1YYixbkxjJQKhwYZ111lcIb1NqIzv8L/GWFZcs=; b=3EUuFPCOHr+1NqtG10RgwTldoZ Hl998HUu+dTOIRWFz6N7074DipEbkWu0x9vDAm/V/twqlJVYpsgjfbyoTtFi4TIC9NIjVHMYwuClC fJuaM/epWU6m7R8aSGE5AngbDikM74q3eIkIbffgcBfb8DSn952BxSVMuc4p3tdXjZ0ixRe/sTs1J 9tPRHGScgVoKhYTZFh95yfh/1dABsikDYQOLCY7lZT/ByXsaeS9DiMHUSW2lfB4/t/rl1T6xVxNHx OOmf72ZPT4sbxABTDm8SxkoEAK3nu4ilC5KmfiNSW1i6lqqtIClM/mG88bIQP2q4OlzNrgfZSe4Nz uscgXocw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mQwB1-00C8qF-H8; Thu, 16 Sep 2021 18:32:03 +0000 Received: from mail-qk1-x74a.google.com ([2607:f8b0:4864:20::74a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mQvvb-00C25M-5D for linux-arm-kernel@lists.infradead.org; Thu, 16 Sep 2021 18:16:08 +0000 Received: by mail-qk1-x74a.google.com with SMTP id w2-20020a3794020000b02903b54f40b442so45000401qkd.0 for ; Thu, 16 Sep 2021 11:16:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=BQXd3Nk8LB8EegwpdynLlvWKL/Dc9feqpx1d4DPqITg=; b=YbtLMWiwVDR1FwoRZkkH3agBSZD6yFHazXXArNyT0cxXrYta0wqypu1LN+cZSMM8NW jW7uPn5jW+QM5fp3xMG2CEU0Xs9YrQ5AMvvwrIj92KZqf2S79zNhY4JyUUuGfobpyINN daSQmUZITVjQ6Eq6eJwqhjvukYDoLsEKA+CFKX2wkSjVtB49CJLDiyVlsbmYbTo/Mmxq xizlfzC3fGmjwn1YiYKaklStfI0tKm4WeSbGKnbWu+gb4Btpo2ELxWp2OFy0xkO+5Om2 mLxwXGIj4EcbOUjkuX6tVcwJ6QE2E43ckqQ1HS+svgBMni2yvdCV8ivbwuOElk7wJvZv 8qIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=BQXd3Nk8LB8EegwpdynLlvWKL/Dc9feqpx1d4DPqITg=; b=fBbPycK7zLTItkyBEtJWVYSWkLMi7zAi0O9cBVefdTAQCvMaVX76L2BjZJcYV6BXF2 lO+RQb71PShxVSGT/TAbYwa4i7MJ9JC5Aafdbv6VqcorC7CxrxAlw27Dgu485MLL0iZ1 o2MjgEYZG3mvUdC4tO5yzUMqgYmVUNQ+w1krE8Hr0QciEdr3BaxtyafTkJ2oE1O+NbXB PSSEQz0INpa49iB9nQxr3LfDb4PdNW7gHpBz8lNs9PPJURwHJrcYAT4l5queVWL89PgV x2Z9kTkmsj19jReuAzoZbXB7Rs4FmDbnMmHjq/8muMpE9M5eQrRuw2yDQlKx1+lFX5rg 4KbQ== X-Gm-Message-State: AOAM533a/i89HbwSHytkqDprwALsr/CAzkl9N8zetaOLdx/bqT62++f5 1vWik94GoTyaZcl21TAjffwkzhOZEE8= X-Google-Smtp-Source: ABdhPJzCpZqCRtF7Y9hBqtO2vjOxdiXuQnImjvuqW2/v2+k/6ic5Q04lFEhj2CGgxRj2QOIjvU7Igaov4Wg= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6214:44a:: with SMTP id cc10mr6940242qvb.58.1631816165810; Thu, 16 Sep 2021 11:16:05 -0700 (PDT) Date: Thu, 16 Sep 2021 18:15:53 +0000 In-Reply-To: <20210916181555.973085-1-oupton@google.com> Message-Id: <20210916181555.973085-8-oupton@google.com> Mime-Version: 1.0 References: <20210916181555.973085-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.464.g1972c5931b-goog Subject: [PATCH v8 7/9] 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-20210916_111607_252146_818AD67E X-CRM114-Status: GOOD ( 14.86 ) 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 | 34 ++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/aarch64/processor.h b/tools/testing/selftests/kvm/include/aarch64/processor.h index 4c7472823df3..57b7802cf9e2 100644 --- a/tools/testing/selftests/kvm/include/aarch64/processor.h +++ b/tools/testing/selftests/kvm/include/aarch64/processor.h @@ -144,4 +144,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 7ea406fdd56f..757b5b2e960e 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,6 +71,12 @@ 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) @@ -76,32 +85,49 @@ static void check_preconditions(struct kvm_vm *vm) .cap = KVM_CAP_ARM_VTIMER_OFFSET, }; - if (vcpu_has_reg(vm, VCPU_ID, KVM_REG_ARM_TIMER_OFFSET)) - return; - if (!kvm_check_cap(KVM_CAP_ARM_VTIMER_OFFSET)) { print_skip("KVM_REG_ARM_TIMER_OFFSET not supported"); exit(KSFT_SKIP); } vm_enable_cap(vm, &cap); + + if (_vcpu_has_device_attr(vm, VCPU_ID, KVM_ARM_VCPU_TIMER_CTRL, + KVM_ARM_VCPU_TIMER_PHYS_OFFSET)) { + print_skip("KVM_ARM_VCPU_TIMER_PHYS_OFFSET not supported"); + 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_PHYS_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); }