From mboxrd@z Thu Jan 1 00:00:00 1970 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 42516C43214 for ; Thu, 29 Jul 2021 00:10:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2C39361053 for ; Thu, 29 Jul 2021 00:10:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233000AbhG2AKn (ORCPT ); Wed, 28 Jul 2021 20:10:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233071AbhG2AKg (ORCPT ); Wed, 28 Jul 2021 20:10:36 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F3E1C061765 for ; Wed, 28 Jul 2021 17:10:33 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id f22-20020a25b0960000b029055ed6ffbea6so4826271ybj.14 for ; Wed, 28 Jul 2021 17:10:33 -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=e1bCoHWm27rhJDUQhB5Prsjt54mG72fssvd6s+HeReo=; b=Bl7MndPh/cQswgUzmOJBm+3FS0n/4lUyCLmT3KeTIeW6pJZwHd6BBoUZemeJRW6V+M a1PybYUvzUvpLu7CG5Q9a/WLwQkxfWsDtiocYLmbikUvsMiwDOmS6F3d8+RysZeQps67 qJ89h8fJDL4c6qxhfVVBH0s4yQd7wImK4E0JAGZCuPS4A5MQ+FV5Uue8z111D/r3rWM9 ouXcwOdzDk9LP/yRDhUf08MFVcas/1eWpAG0QmClIErqDkvFTH1DDZEINKdHlHI1K03P RgIVW5AxXHUKx963XSU0H7zkOu9Xxlt1tWHfxJrK3IGeAQJWv/iKzWJkcLdNTe++bJWx z7xw== 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=e1bCoHWm27rhJDUQhB5Prsjt54mG72fssvd6s+HeReo=; b=k//tYgXEvx7OjmUUrcPvzeu2M7ywjTjTqgiP0mT1U1gRngmXTFMA0PJ4dcfpSUHHZA /oswN+Sj+9ypqkv0uwOvMTIflO/Jb+afStjux+hK6Jgf6CInSRAXy7ewAc9pSBlUYKZr qc/OgP+aVtWgVQZZlDxZGvRfwH1Cl08F8lKuaKNArgk4u/IRmlkkF7UsfqqW5zxZtZJn 5esIv26r6iOsBpWrV179/XqyPl+nwoyXox5EUyUuUWLYuleLPFYGtf0XvxmUXZKNBRsc B/UrMVu8MwFIpTF6ByeYIolKUujkkKP8VO4GHprLGobZ3Y4FRl2iA5AMf6P35cNNxGQC u0gA== X-Gm-Message-State: AOAM531nwDXvqsU4snOWU21sb2a6d38dkPVlAVoPMChk6H8tba/HXOT4 Wn57z6PerkUjNxVtOBx9HHpBQVCpJIC8/Iljhu8NdfS6D0ZxqO5j/bDoct/5gapuvPvvDodyaCh WTpbzGKXr7SlKmB1rpI6ZoZuBJhm0//H19sN1YKvKdS8cyG/nNK6EoTta2Q== X-Google-Smtp-Source: ABdhPJwNqPLE8hAgSOpVcZg83iw+iOV9Sml+8O+fDLJ2VVOtw4cKrBtI7dzT08h75xXidYYuTfdjmV6LUOs= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a25:8a:: with SMTP id 132mr3117287yba.280.1627517432215; Wed, 28 Jul 2021 17:10:32 -0700 (PDT) Date: Thu, 29 Jul 2021 00:10:09 +0000 In-Reply-To: <20210729001012.70394-1-oupton@google.com> Message-Id: <20210729001012.70394-11-oupton@google.com> Mime-Version: 1.0 References: <20210729001012.70394-1-oupton@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [PATCH v4 10/13] selftests: KVM: Add support for aarch64 to system_counter_offset_test 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 , Oliver Upton Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org KVM/arm64 now allows userspace to adjust the guest virtual counter-timer via a vCPU device attribute. Test that changes to the virtual counter-timer offset result in the correct view being presented to the guest. Reviewed-by: Andrew Jones Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/aarch64/processor.h | 12 +++++ .../kvm/system_counter_offset_test.c | 54 ++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 9f7060c02668..fab42e7c23ee 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -98,6 +98,7 @@ TEST_GEN_PROGS_aarch64 += kvm_page_table_test TEST_GEN_PROGS_aarch64 += set_memory_region_test TEST_GEN_PROGS_aarch64 += steal_time TEST_GEN_PROGS_aarch64 += kvm_binary_stats_test +TEST_GEN_PROGS_aarch64 += system_counter_offset_test TEST_GEN_PROGS_s390x = s390x/memop TEST_GEN_PROGS_s390x += s390x/resets diff --git a/tools/testing/selftests/kvm/include/aarch64/processor.h b/tools/testing/selftests/kvm/include/aarch64/processor.h index 27dc5c2e56b9..3168cdbae6ee 100644 --- a/tools/testing/selftests/kvm/include/aarch64/processor.h +++ b/tools/testing/selftests/kvm/include/aarch64/processor.h @@ -129,4 +129,16 @@ void vm_install_sync_handler(struct kvm_vm *vm, #define isb() asm volatile("isb" : : : "memory") +static inline uint64_t read_cntvct_ordered(void) +{ + uint64_t r; + + __asm__ __volatile__("isb\n\t" + "mrs %0, cntvct_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 b337bbbfa41f..25806cdd31ef 100644 --- a/tools/testing/selftests/kvm/system_counter_offset_test.c +++ b/tools/testing/selftests/kvm/system_counter_offset_test.c @@ -53,7 +53,59 @@ static uint64_t host_read_guest_system_counter(struct test_case *test) return rdtsc() + test->tsc_offset; } -#else /* __x86_64__ */ +#elif __aarch64__ /* __x86_64__ */ + +enum arch_counter { + VIRTUAL, +}; + +struct test_case { + enum arch_counter counter; + uint64_t offset; +}; + +static struct test_case test_cases[] = { + { .counter = VIRTUAL, .offset = 0 }, + { .counter = VIRTUAL, .offset = 180 * NSEC_PER_SEC }, + { .counter = VIRTUAL, .offset = -180 * NSEC_PER_SEC }, +}; + +static void check_preconditions(struct kvm_vm *vm) +{ + if (!_vcpu_has_device_attr(vm, VCPU_ID, KVM_ARM_VCPU_TIMER_CTRL, + KVM_ARM_VCPU_TIMER_OFFSET_VTIMER)) + return; + + print_skip("KVM_ARM_VCPU_TIMER_OFFSET_VTIMER not supported; skipping test"); + exit(KSFT_SKIP); +} + +static void setup_system_counter(struct kvm_vm *vm, struct test_case *test) +{ + vcpu_access_device_attr(vm, VCPU_ID, KVM_ARM_VCPU_TIMER_CTRL, + KVM_ARM_VCPU_TIMER_OFFSET_VTIMER, &test->offset, + true); +} + +static uint64_t guest_read_system_counter(struct test_case *test) +{ + switch (test->counter) { + case VIRTUAL: + return read_cntvct_ordered(); + default: + GUEST_ASSERT(0); + } + + /* unreachable */ + return 0; +} + +static uint64_t host_read_guest_system_counter(struct test_case *test) +{ + return read_cntvct_ordered() - test->offset; +} + +#else /* __aarch64__ */ #error test not implemented for this architecture! -- 2.32.0.432.gabb21c7263-goog