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=-12.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY 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 7870EC636C9 for ; Wed, 21 Jul 2021 15:42:50 +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 46DAC6120A for ; Wed, 21 Jul 2021 15:42:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 46DAC6120A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@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:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=qhGGLnjcX0K0E8Ru1O7gZdSEYh4tGaDSnS7Z32WBMTA=; b=BNNtJ5ArTSxxiD kxNqyY+wsNc5FKCaPESe2dv6oBs4vMEkLz6k+JnEcP7pwz7hOGhr0ZKkHpVuTRciR8RbFNOoItRvS +MMokH5SVc16f4F8F035oBssB/rgvnl+xzEyix8L8e6ry+x9BVbC//DQBKonQ20jeiY3gIppmGMHJ f89GmwMKB3JQSdKX0iv5Z9nKoupJw6so70vnhSp4wHPcyVL8txdR/oLJhvNv6WvQC4SkvyRYjq53J /A/GzircnUkvIP7/msnfX3czitrjgzEM5XyPaQRDqBKeTwLrJh07RA0gdm3jZX6t98yJ8FWzGX7JJ 4UBQZ0gjUtHe3ustDvjA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m6EL7-00GNU5-Es; Wed, 21 Jul 2021 15:40:54 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m6Dyf-00GFVc-JD for linux-arm-kernel@lists.infradead.org; Wed, 21 Jul 2021 15:17:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626880660; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=cjz/qY9rq3iPuIQlWrJrcTCNPSV381YIh1gROWvzlnA=; b=XrMNAayn6Yeabbwn8QYVERwyGWiRGvXTs5XxMXnqypJVf5Ucama9xh4cYSj4UR0HB4Wg1K a4egj2rdYR5S92znaZJUhGU8NdaFD2KhdMZPkJ5zV7L8EzofMcEw8jLQwLcKvLQXS9RKzW aQL9XsSNRuioM50DnS4hX4AwcXxiv9c= Received: from mail-io1-f70.google.com (mail-io1-f70.google.com [209.85.166.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-246-9pRx6Ki4MtSkSYdWCAVZqw-1; Wed, 21 Jul 2021 11:17:37 -0400 X-MC-Unique: 9pRx6Ki4MtSkSYdWCAVZqw-1 Received: by mail-io1-f70.google.com with SMTP id x21-20020a5d99150000b02904e00bb129f0so1781702iol.18 for ; Wed, 21 Jul 2021 08:17:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=cjz/qY9rq3iPuIQlWrJrcTCNPSV381YIh1gROWvzlnA=; b=jFinA/yUbez2mAzJ8KSWFcrOJKMxCBCg6Q5ndo9ew1h7wycGR5xfKWSv2wRVTB6YUS 70sU+ahXKhorTAqcUYrkdfwH4YAp0jTuRsk1aTBCYo3mMIqlor+Z0Cdv9rx/VUtXAxEf xm/9xiBd7geOBzmJ8km8e9fkjXc/tjGBLupBycFgh+BNje+996COI/bIoB7TEyhCFjyV R4j7Qtvox3Hfu1MMbRX0HKznFaBp+CvNX/aZZi2bS6VwM0xq+wqmCfI+w4yF/nqLhEXU pXKCqHFG4d5t1QoGAlicIUqt5HgWD3pD1Hk6wQPMVzYRVr5D9kPVadbHzmlyqR/P2uJ0 eEGA== X-Gm-Message-State: AOAM533VLVtqpZro/yONaeHwR2axZa8ONG+4plXjyMi32tqcEgv0NAHa JcmIu0uBmGI1wi6FyaY44tf+Vd18R1YQqU78qHe2bRos0ZrdWfhxzpmFyugySTT1AZAxDwTw7XU STrAXfbj/+BVvdXOooCxBf2tZjVJtVBYT+ME= X-Received: by 2002:a5e:d508:: with SMTP id e8mr14388594iom.101.1626880656747; Wed, 21 Jul 2021 08:17:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJ0u39et2NnLlV6EWy3+1YIXupxf89Pzxvb2E5MW4TyugGAON0zndCG+fRuGJzVGbOohzoIQ== X-Received: by 2002:a5e:d508:: with SMTP id e8mr14388578iom.101.1626880656539; Wed, 21 Jul 2021 08:17:36 -0700 (PDT) Received: from gator ([140.82.166.162]) by smtp.gmail.com with ESMTPSA id x16sm12926397ila.84.2021.07.21.08.17.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jul 2021 08:17:35 -0700 (PDT) Date: Wed, 21 Jul 2021 17:17:33 +0200 From: Andrew Jones To: Oliver Upton Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, Marc Zyngier , Raghavendra Rao Anata , Peter Shier , Sean Christopherson , David Matlack , Paolo Bonzini , linux-arm-kernel@lists.infradead.org, Jim Mattson Subject: Re: [PATCH v2 07/12] selftests: KVM: Introduce system counter offset test Message-ID: <20210721151733.6yj3lm3h2amwmgmf@gator> References: <20210716212629.2232756-1-oupton@google.com> <20210716212629.2232756-8-oupton@google.com> MIME-Version: 1.0 In-Reply-To: <20210716212629.2232756-8-oupton@google.com> Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=drjones@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210721_081741_779140_873AB980 X-CRM114-Status: GOOD ( 26.20 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Jul 16, 2021 at 09:26:24PM +0000, Oliver Upton wrote: > Introduce a KVM selftest to verify that userspace manipulation of the > TSC (via the new vCPU attribute) results in the correct behavior within > the guest. > > Signed-off-by: Oliver Upton > --- > tools/testing/selftests/kvm/.gitignore | 1 + > tools/testing/selftests/kvm/Makefile | 1 + > .../kvm/system_counter_offset_test.c | 133 ++++++++++++++++++ > 3 files changed, 135 insertions(+) > create mode 100644 tools/testing/selftests/kvm/system_counter_offset_test.c > > diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore > index d0877d01e771..2752813d5090 100644 > --- a/tools/testing/selftests/kvm/.gitignore > +++ b/tools/testing/selftests/kvm/.gitignore > @@ -50,3 +50,4 @@ > /set_memory_region_test > /steal_time > /kvm_binary_stats_test > +/system_counter_offset_test > diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile > index f7e24f334c6e..7bf2e5fb1d5a 100644 > --- a/tools/testing/selftests/kvm/Makefile > +++ b/tools/testing/selftests/kvm/Makefile > @@ -83,6 +83,7 @@ TEST_GEN_PROGS_x86_64 += memslot_perf_test > TEST_GEN_PROGS_x86_64 += set_memory_region_test > TEST_GEN_PROGS_x86_64 += steal_time > TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test > +TEST_GEN_PROGS_x86_64 += system_counter_offset_test > > TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions > TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list > diff --git a/tools/testing/selftests/kvm/system_counter_offset_test.c b/tools/testing/selftests/kvm/system_counter_offset_test.c > new file mode 100644 > index 000000000000..7e9015770759 > --- /dev/null > +++ b/tools/testing/selftests/kvm/system_counter_offset_test.c > @@ -0,0 +1,133 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Copyright (C) 2021, Google LLC. > + * > + * Tests for adjusting the system counter from userspace > + */ > +#include > +#include > +#include > +#include > +#include > + > +#include "test_util.h" > +#include "kvm_util.h" > +#include "processor.h" > + > +#define VCPU_ID 0 > + > +#ifdef __x86_64__ > + > +struct test_case { > + uint64_t tsc_offset; > +}; > + > +static struct test_case test_cases[] = { > + { 0 }, > + { 180 * NSEC_PER_SEC }, > + { -180 * NSEC_PER_SEC }, > +}; > + > +static void check_preconditions(struct kvm_vm *vm) > +{ > + if (!_vcpu_has_device_attr(vm, VCPU_ID, KVM_VCPU_TSC_CTRL, KVM_VCPU_TSC_OFFSET)) > + return; > + > + print_skip("KVM_VCPU_TSC_OFFSET 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_VCPU_TSC_CTRL, > + KVM_VCPU_TSC_OFFSET, &test->tsc_offset, true); > +} > + > +static uint64_t guest_read_system_counter(struct test_case *test) > +{ > + return rdtsc(); > +} > + > +static uint64_t host_read_guest_system_counter(struct test_case *test) > +{ > + return rdtsc() + test->tsc_offset; > +} > + > +#else /* __x86_64__ */ > + > +#error test not implemented for this architecture! > + > +#endif > + > +#define GUEST_SYNC_CLOCK(__stage, __val) \ > + GUEST_SYNC_ARGS(__stage, __val, 0, 0, 0) > + > +static void guest_main(void) > +{ > + int i; > + > + for (i = 0; i < ARRAY_SIZE(test_cases); i++) { > + struct test_case *test = &test_cases[i]; > + > + GUEST_SYNC_CLOCK(i, guest_read_system_counter(test)); > + } > + > + GUEST_DONE(); > +} > + > +static void handle_sync(struct ucall *uc, uint64_t start, uint64_t end) > +{ > + uint64_t obs = uc->args[2]; > + > + TEST_ASSERT(start <= obs && obs <= end, > + "unexpected system counter value: %"PRIu64" expected range: [%"PRIu64", %"PRIu64"]", > + obs, start, end); > + > + pr_info("system counter value: %"PRIu64" expected range [%"PRIu64", %"PRIu64"]\n", > + obs, start, end); > +} > + > +static void handle_abort(struct ucall *uc) > +{ > + TEST_FAIL("%s at %s:%ld", (const char *)uc->args[0], > + __FILE__, uc->args[1]); > +} > + > +static void enter_guest(struct kvm_vm *vm) > +{ > + uint64_t start, end; > + struct ucall uc; > + int i; > + > + for (i = 0; i < ARRAY_SIZE(test_cases); i++) { > + struct test_case *test = &test_cases[i]; > + > + setup_system_counter(vm, test); > + start = host_read_guest_system_counter(test); > + vcpu_run(vm, VCPU_ID); > + end = host_read_guest_system_counter(test); > + > + switch (get_ucall(vm, VCPU_ID, &uc)) { > + case UCALL_SYNC: > + handle_sync(&uc, start, end); > + break; > + case UCALL_ABORT: > + handle_abort(&uc); > + return; > + case UCALL_DONE: > + return; > + } > + } > +} > + > +int main(void) > +{ > + struct kvm_vm *vm; > + > + vm = vm_create_default(VCPU_ID, 0, guest_main); > + check_preconditions(vm); > + ucall_init(vm, NULL); > + > + enter_guest(vm); > + kvm_vm_free(vm); > +} > -- > 2.32.0.402.g57bb445576-goog > > _______________________________________________ > kvmarm mailing list > kvmarm@lists.cs.columbia.edu > https://lists.cs.columbia.edu/mailman/listinfo/kvmarm > Reviewed-by: Andrew Jones _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel