From patchwork Mon Aug 16 00:12:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12437527 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 B4872C4338F for ; Mon, 16 Aug 2021 00:24:00 +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 71A1961251 for ; Mon, 16 Aug 2021 00:24:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 71A1961251 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=Wmx2H8vsBJihxChHUavP8j2X/BXmL8opS9wBV9mB5RU=; b=b7QBLe+zVyVuQuk/BcmzhFoelB fUbvST5d9jvJTRMm+G2zoUHT6gIbhYfBz1K4GLw50pIBRa+C/Dupo9C8dw4NHFIbWVMLHPrRXnyGV cm3LMZtgE30mtD7HBInN2x3em6WGcBsvmhZd6fvu4xPaT9zBsHEynGjYtAMyXCGB5cDULaNXi6D0b J/oRAoH1wWBbYJNH88WvFvKBLyfXXXMn6PSP8UziyA8nbnQ1CkqjXzNA4MIPHS40aYk/frK1Qy0X/ 9EtgazebLUB2/T8QfOTxwprcyPOfS1upknFXPu/RgvUPLN1M9WEfWAFlp8nflWYgCYhMOfI4aiwKA ssTJgEiQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQNf-00Fonr-9I; Mon, 16 Aug 2021 00:21:32 +0000 Received: from mail-io1-xd49.google.com ([2607:f8b0:4864:20::d49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQFJ-00FlL8-Fk for linux-arm-kernel@lists.infradead.org; Mon, 16 Aug 2021 00:12:55 +0000 Received: by mail-io1-xd49.google.com with SMTP id p22-20020a5d8d160000b02905a150b13b20so8257813ioj.18 for ; Sun, 15 Aug 2021 17:12:53 -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=YJQeH9eyrSgJZU9Ny2rMLZs6xbXaCD7Zl6TzBg6qTrQ=; b=QBMsWHju9a6N+AbW89GmYdTgiBjk6UEl6el06NXKbSnD/SJP4/ber/VEgzS2HXX5Kj lKnWBahIGpHLQvIMq3MgmQJCZdMZ7GiQ70KEiB2eiD9Z7TGlj1IS1LHDXXfJ8r5GUc1o 7YcLqyYv6N5Zk4T/u0wOxe5nNhpzheWhRoEKSHugBtHqvwwCnDp2x8CSAjrhOHlg1EDk 1zQXEW+DZdwb6xPs6sI1b8y45xX7CHu2YWcfM+CUJSxALs7c8Rw5hTsborgHaYr2Num2 MePMXtK1VhYMdiZBVGIgFKlPkGPvrOXSv8QE6glINoaIiUU2KtB/R3v0XFQWTjv2ymTE uxKA== 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=YJQeH9eyrSgJZU9Ny2rMLZs6xbXaCD7Zl6TzBg6qTrQ=; b=Ak8rdETd31QHO/0G6Aq6Fy00Asg3EVRoAH9Bb80F4JQIwfukizVt6sc+ZVc5xXg89b w9XlTb535sp1HmP7aVlXI1UVGUQtTOZEfgNhwNgepajbDTjj6WUVKtAi8ubmQs1Q7f2K kz6421YgFFzZwhWGAu55535voUILHDz+Xxp6yI2iHMUhhUj4rYvKqfurIInLEHmvxKya 0pszSBymX5DOZCmgMniuqa+zOsCvNPZulnFEKSmlAA/8ahyy+5RfORnd5QpQT9JNLpw6 RYiNf0polVY1lptXcjPRHJ9UdW1cF43Nu2EuJS2PiVH8u5vy5hMkzm3oGWHD+OBQpQgB hcyQ== X-Gm-Message-State: AOAM532bUlGmAqJOdwk552pZBvHZT2zOQV4/WbPF8gWTcrJl3MYZ3bB8 lDU7aOSMBlNpVskbOLZrTC0xWMr2VEU= X-Google-Smtp-Source: ABdhPJya89o9o+hYP9pk3oEvzHiKDZnr7pJgl1dCt3ghsTzbqdn2mwULPHS4J2TPUwLa5Qo4IQqicNKlkFs= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a02:a490:: with SMTP id d16mr13519859jam.42.1629072772915; Sun, 15 Aug 2021 17:12:52 -0700 (PDT) Date: Mon, 16 Aug 2021 00:12:38 +0000 In-Reply-To: <20210816001246.3067312-1-oupton@google.com> Message-Id: <20210816001246.3067312-2-oupton@google.com> Mime-Version: 1.0 References: <20210816001246.3067312-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v7 1/9] tools: arch: x86: pull in pvclock headers 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-20210815_171253_584390_C133CDAC X-CRM114-Status: GOOD ( 21.06 ) 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 Copy over approximately clean versions of the pvclock headers into tools. Reconcile headers/symbols missing in tools that are unneeded. Signed-off-by: Oliver Upton --- tools/arch/x86/include/asm/pvclock-abi.h | 48 +++++++++++ tools/arch/x86/include/asm/pvclock.h | 103 +++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 tools/arch/x86/include/asm/pvclock-abi.h create mode 100644 tools/arch/x86/include/asm/pvclock.h diff --git a/tools/arch/x86/include/asm/pvclock-abi.h b/tools/arch/x86/include/asm/pvclock-abi.h new file mode 100644 index 000000000000..1436226efe3e --- /dev/null +++ b/tools/arch/x86/include/asm/pvclock-abi.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_PVCLOCK_ABI_H +#define _ASM_X86_PVCLOCK_ABI_H +#ifndef __ASSEMBLY__ + +/* + * These structs MUST NOT be changed. + * They are the ABI between hypervisor and guest OS. + * Both Xen and KVM are using this. + * + * pvclock_vcpu_time_info holds the system time and the tsc timestamp + * of the last update. So the guest can use the tsc delta to get a + * more precise system time. There is one per virtual cpu. + * + * pvclock_wall_clock references the point in time when the system + * time was zero (usually boot time), thus the guest calculates the + * current wall clock by adding the system time. + * + * Protocol for the "version" fields is: hypervisor raises it (making + * it uneven) before it starts updating the fields and raises it again + * (making it even) when it is done. Thus the guest can make sure the + * time values it got are consistent by checking the version before + * and after reading them. + */ + +struct pvclock_vcpu_time_info { + u32 version; + u32 pad0; + u64 tsc_timestamp; + u64 system_time; + u32 tsc_to_system_mul; + s8 tsc_shift; + u8 flags; + u8 pad[2]; +} __attribute__((__packed__)); /* 32 bytes */ + +struct pvclock_wall_clock { + u32 version; + u32 sec; + u32 nsec; +} __attribute__((__packed__)); + +#define PVCLOCK_TSC_STABLE_BIT (1 << 0) +#define PVCLOCK_GUEST_STOPPED (1 << 1) +/* PVCLOCK_COUNTS_FROM_ZERO broke ABI and can't be used anymore. */ +#define PVCLOCK_COUNTS_FROM_ZERO (1 << 2) +#endif /* __ASSEMBLY__ */ +#endif /* _ASM_X86_PVCLOCK_ABI_H */ diff --git a/tools/arch/x86/include/asm/pvclock.h b/tools/arch/x86/include/asm/pvclock.h new file mode 100644 index 000000000000..2628f9a6330b --- /dev/null +++ b/tools/arch/x86/include/asm/pvclock.h @@ -0,0 +1,103 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_PVCLOCK_H +#define _ASM_X86_PVCLOCK_H + +#include +#include + +/* some helper functions for xen and kvm pv clock sources */ +u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src); +u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src); +void pvclock_set_flags(u8 flags); +unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src); +void pvclock_resume(void); + +void pvclock_touch_watchdogs(void); + +static __always_inline +unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src) +{ + unsigned version = src->version & ~1; + /* Make sure that the version is read before the data. */ + rmb(); + return version; +} + +static __always_inline +bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src, + unsigned version) +{ + /* Make sure that the version is re-read after the data. */ + rmb(); + return version != src->version; +} + +/* + * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction, + * yielding a 64-bit result. + */ +static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift) +{ + u64 product; +#ifdef __i386__ + u32 tmp1, tmp2; +#else + unsigned long tmp; +#endif + + if (shift < 0) + delta >>= -shift; + else + delta <<= shift; + +#ifdef __i386__ + __asm__ ( + "mul %5 ; " + "mov %4,%%eax ; " + "mov %%edx,%4 ; " + "mul %5 ; " + "xor %5,%5 ; " + "add %4,%%eax ; " + "adc %5,%%edx ; " + : "=A" (product), "=r" (tmp1), "=r" (tmp2) + : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) ); +#elif defined(__x86_64__) + __asm__ ( + "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]" + : [lo]"=a"(product), + [hi]"=d"(tmp) + : "0"(delta), + [mul_frac]"rm"((u64)mul_frac)); +#else +#error implement me! +#endif + + return product; +} + +static __always_inline +u64 __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src, u64 tsc) +{ + u64 delta = tsc - src->tsc_timestamp; + u64 offset = pvclock_scale_delta(delta, src->tsc_to_system_mul, + src->tsc_shift); + return src->system_time + offset; +} + +struct pvclock_vsyscall_time_info { + struct pvclock_vcpu_time_info pvti; +} __attribute__((__aligned__(64))); + +#define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info) + +#ifdef CONFIG_PARAVIRT_CLOCK +void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti); +struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void); +#else +static inline struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void) +{ + return NULL; +} +#endif + +#endif /* _ASM_X86_PVCLOCK_H */ From patchwork Mon Aug 16 00:12:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12437529 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=-14.6 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, UNWANTED_LANGUAGE_BODY,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 060DBC4338F for ; Mon, 16 Aug 2021 00:25:13 +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 BCDE06138F for ; Mon, 16 Aug 2021 00:25:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BCDE06138F 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=QvQbydejEoK4LFAXLVNTz58J6msq3Jgjzu2i+jvaOEQ=; b=FwKMrapPfmMwHsqYq3hluGBO2f K5VC9rtQ/euAqpcymNM+izjQ5t2L5XIAjZdbULs6GPh5oXmQTE+dDA+n7CvVza1ejxTi4ieTLJi0R KEKLUNiu0tBMcd/E/4GWcKSBcvsMw8aSxYAkjqTcEJj/zV9Gfh5fUh/cTwWVwV5f9tadakB5Q39nQ GLKd4PVTAcKyNPzM9Ir60+1SP6U2ZDwU4CTinc0jfXn71kBMVcYnOxkP5E2lNoS9tQpkfeaXK0zIB awZPhSj9Q5Xcd0iN8Z3Hzu4hhZj00UB5FCUuvXgsI59iWJ4l6lTEzs1r4JHKq29OJN0zt+KGIKVBv XvGBnn3g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQOs-00FpEP-A4; Mon, 16 Aug 2021 00:22:47 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQFL-00FlLm-9g for linux-arm-kernel@lists.infradead.org; Mon, 16 Aug 2021 00:12:57 +0000 Received: by mail-yb1-xb49.google.com with SMTP id j21-20020a25d2150000b029057ac4b4e78fso14957731ybg.4 for ; Sun, 15 Aug 2021 17:12:54 -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=YHweAsH98SN0OsCurZXAoHNg0K7fgg4+yA3M4swKOxM=; b=vJqFQI8UoCqLoeq87yRka6H66iNJlscNgwbv1Q2M1aK/7k176CFZWdkyI9ri+r6aDE /LfJ0n+fvvj7+ykU2bq+tEdxoCf33nmTojIua6QCSusL9G4p7gsFMPVV7zj9Uh3Gp9cD C7pEvBcMIfUKPHmrU8iMaJYWCtLLfL0xnn0UeuAy+h/sm4Kj7Ug5jq9CCSHk2YjaH8E3 XJy2+OK/0qFDoiIVqN1sMMnqWz7fmRQt6mw3lSL8v/jD+1mrykR+X9lxbNTWmb+cVWWW td6zmDoDQl1NRow6PHijT5+ABaFFoAhiO00bRzXUjzRvo9GN6vxOZyMQ1eL1ijKrVZK3 GAMQ== 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=YHweAsH98SN0OsCurZXAoHNg0K7fgg4+yA3M4swKOxM=; b=jQv88WPQj1NKboBYZotQR73FHZp4w+OrgUlGYkqxHACVzpo6zkYywqiPyZcd3NLYNu usO7A1gqG47Smh8wRwgDDBn/N1QINxqa+Gck1k0rviXW9EIgUzI5j3q5NdVriCHFmw6X enT0ZjUK/LL8N4Fjzbw9PmAZD58KXyJ3lAWsVtBzxRsRolyaNPxQGgWq38BGzEKq+FWI N4N9bbai2na2lh3yL10UlxDM7p5vlt2LJZCpU237/mMX4GAVGQujYEEpuocrmKeMlqj/ Ndh04Uy8CUoXGkLA6PI2+UIpDVb1BuWDrnFwM/egF2X+RGAXiY/6gQ+MhE5PJ0dpghkH KbGQ== X-Gm-Message-State: AOAM530Fos4iD3SD/3XN11rP5NO3NqnIL6YWg+MV4w1UdK4YjOhrN/Uv j3HXIf39b+5JJnWoPtsI47Xk9jqAcWc= X-Google-Smtp-Source: ABdhPJySTvZCEKowSqty9guXsEBhDROpk1CWGr9OKWKMFcqQctz6iGxdx2gpJGaaWY382kH1XOfcQV5FUh4= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a25:b782:: with SMTP id n2mr18104381ybh.159.1629072773868; Sun, 15 Aug 2021 17:12:53 -0700 (PDT) Date: Mon, 16 Aug 2021 00:12:39 +0000 In-Reply-To: <20210816001246.3067312-1-oupton@google.com> Message-Id: <20210816001246.3067312-3-oupton@google.com> Mime-Version: 1.0 References: <20210816001246.3067312-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v7 2/9] selftests: KVM: Add test for KVM_{GET,SET}_CLOCK 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-20210815_171255_446083_C051DFD6 X-CRM114-Status: GOOD ( 22.61 ) 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 Add a selftest for the new KVM clock UAPI that was introduced. Ensure that the KVM clock is consistent between userspace and the guest, and that the difference in realtime will only ever cause the KVM clock to advance forward. Cc: Andrew Jones Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/include/kvm_util.h | 2 + .../selftests/kvm/x86_64/kvm_clock_test.c | 204 ++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/kvm_clock_test.c diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index 0709af0144c8..06268d6b9aca 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -11,6 +11,7 @@ /x86_64/emulator_error_test /x86_64/get_cpuid_test /x86_64/get_msr_index_features +/x86_64/kvm_clock_test /x86_64/kvm_pv_test /x86_64/hyperv_clock /x86_64/hyperv_cpuid diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 5832f510a16c..3b09133e84a4 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -46,6 +46,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/get_cpuid_test TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid TEST_GEN_PROGS_x86_64 += x86_64/hyperv_features +TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test TEST_GEN_PROGS_x86_64 += x86_64/mmu_role_test diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 010b59b13917..a8ac5d52e17b 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -19,6 +19,8 @@ #define KVM_DEV_PATH "/dev/kvm" #define KVM_MAX_VCPUS 512 +#define NSEC_PER_SEC 1000000000L + /* * Callers of kvm_util only have an incomplete/opaque description of the * structure kvm_util is using to maintain the state of a VM. diff --git a/tools/testing/selftests/kvm/x86_64/kvm_clock_test.c b/tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file mode 100644 index 000000000000..e0dcc27ae9f1 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/kvm_clock_test.c @@ -0,0 +1,204 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2021, Google LLC. + * + * Tests for adjusting the KVM clock from userspace + */ +#include +#include +#include +#include +#include +#include +#include + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" + +#define VCPU_ID 0 + +struct test_case { + uint64_t kvmclock_base; + int64_t realtime_offset; +}; + +static struct test_case test_cases[] = { + { .kvmclock_base = 0 }, + { .kvmclock_base = 180 * NSEC_PER_SEC }, + { .kvmclock_base = 0, .realtime_offset = -180 * NSEC_PER_SEC }, + { .kvmclock_base = 0, .realtime_offset = 180 * NSEC_PER_SEC }, +}; + +#define GUEST_SYNC_CLOCK(__stage, __val) \ + GUEST_SYNC_ARGS(__stage, __val, 0, 0, 0) + +static void guest_main(vm_paddr_t pvti_pa, struct pvclock_vcpu_time_info *pvti) +{ + int i; + + wrmsr(MSR_KVM_SYSTEM_TIME_NEW, pvti_pa | KVM_MSR_ENABLED); + for (i = 0; i < ARRAY_SIZE(test_cases); i++) + GUEST_SYNC_CLOCK(i, __pvclock_read_cycles(pvti, rdtsc())); +} + +#define EXPECTED_FLAGS (KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC) + +static inline void assert_flags(struct kvm_clock_data *data) +{ + TEST_ASSERT((data->flags & EXPECTED_FLAGS) == EXPECTED_FLAGS, + "unexpected clock data flags: %x (want set: %x)", + data->flags, EXPECTED_FLAGS); +} + +static void handle_sync(struct ucall *uc, struct kvm_clock_data *start, + struct kvm_clock_data *end) +{ + uint64_t obs, exp_lo, exp_hi; + + obs = uc->args[2]; + exp_lo = start->clock; + exp_hi = end->clock; + + assert_flags(start); + assert_flags(end); + + TEST_ASSERT(exp_lo <= obs && obs <= exp_hi, + "unexpected kvm-clock value: %"PRIu64" expected range: [%"PRIu64", %"PRIu64"]", + obs, exp_lo, exp_hi); + + pr_info("kvm-clock value: %"PRIu64" expected range [%"PRIu64", %"PRIu64"]\n", + obs, exp_lo, exp_hi); +} + +static void handle_abort(struct ucall *uc) +{ + TEST_FAIL("%s at %s:%ld", (const char *)uc->args[0], + __FILE__, uc->args[1]); +} + +static void setup_clock(struct kvm_vm *vm, struct test_case *test_case) +{ + struct kvm_clock_data data; + + memset(&data, 0, sizeof(data)); + + data.clock = test_case->kvmclock_base; + if (test_case->realtime_offset) { + struct timespec ts; + int r; + + data.flags |= KVM_CLOCK_REALTIME; + do { + r = clock_gettime(CLOCK_REALTIME, &ts); + if (!r) + break; + } while (errno == EINTR); + + TEST_ASSERT(!r, "clock_gettime() failed: %d\n", r); + + data.realtime = ts.tv_sec * NSEC_PER_SEC; + data.realtime += ts.tv_nsec; + data.realtime += test_case->realtime_offset; + } + + vm_ioctl(vm, KVM_SET_CLOCK, &data); +} + +static void enter_guest(struct kvm_vm *vm) +{ + struct kvm_clock_data start, end; + struct kvm_run *run; + struct ucall uc; + int i, r; + + run = vcpu_state(vm, VCPU_ID); + + for (i = 0; i < ARRAY_SIZE(test_cases); i++) { + setup_clock(vm, &test_cases[i]); + + vm_ioctl(vm, KVM_GET_CLOCK, &start); + + r = _vcpu_run(vm, VCPU_ID); + vm_ioctl(vm, KVM_GET_CLOCK, &end); + + TEST_ASSERT(!r, "vcpu_run failed: %d\n", r); + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, + "unexpected exit reason: %u (%s)", + run->exit_reason, exit_reason_str(run->exit_reason)); + + switch (get_ucall(vm, VCPU_ID, &uc)) { + case UCALL_SYNC: + handle_sync(&uc, &start, &end); + break; + case UCALL_ABORT: + handle_abort(&uc); + return; + default: + TEST_ASSERT(0, "unhandled ucall: %ld\n", + get_ucall(vm, VCPU_ID, &uc)); + } + } +} + +#define CLOCKSOURCE_PATH "/sys/devices/system/clocksource/clocksource0/current_clocksource" + +static void check_clocksource(void) +{ + char *clk_name; + struct stat st; + FILE *fp; + + fp = fopen(CLOCKSOURCE_PATH, "r"); + if (!fp) { + pr_info("failed to open clocksource file: %d; assuming TSC.\n", + errno); + return; + } + + if (fstat(fileno(fp), &st)) { + pr_info("failed to stat clocksource file: %d; assuming TSC.\n", + errno); + goto out; + } + + clk_name = malloc(st.st_size); + TEST_ASSERT(clk_name, "failed to allocate buffer to read file\n"); + + if (!fgets(clk_name, st.st_size, fp)) { + pr_info("failed to read clocksource file: %d; assuming TSC.\n", + ferror(fp)); + goto out; + } + + TEST_ASSERT(!strncmp(clk_name, "tsc\n", st.st_size), + "clocksource not supported: %s", clk_name); +out: + fclose(fp); +} + +int main(void) +{ + vm_vaddr_t pvti_gva; + vm_paddr_t pvti_gpa; + struct kvm_vm *vm; + int flags; + + flags = kvm_check_cap(KVM_CAP_ADJUST_CLOCK); + if (!(flags & KVM_CLOCK_REALTIME)) { + print_skip("KVM_CLOCK_REALTIME not supported; flags: %x", + flags); + exit(KSFT_SKIP); + } + + check_clocksource(); + + vm = vm_create_default(VCPU_ID, 0, guest_main); + + pvti_gva = vm_vaddr_alloc(vm, getpagesize(), 0x10000); + pvti_gpa = addr_gva2gpa(vm, pvti_gva); + vcpu_args_set(vm, VCPU_ID, 2, pvti_gpa, pvti_gva); + + enter_guest(vm); + kvm_vm_free(vm); +} From patchwork Mon Aug 16 00:12:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12437533 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=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 595DCC432BE for ; Mon, 16 Aug 2021 00:28:28 +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 192996137D for ; Mon, 16 Aug 2021 00:28:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 192996137D 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=zTMXQMfU/uMK2454Tyx56CuxYauLHP78y3jJZoIQHA8=; b=B3+xL6a9o+3VDCDXVRddMTwlKN CkW+n2ukOWIR4QNJcLOsRqChhUJMeod1aCkwZFU5CYVypbkU6PwRvy/7sUVgR7XP8/ThOgo0hx5qv eylqqVdxY5zeMuEG6yG5tyPU+JA/pMpSd4htCYVjqpIPDDYCnJGhRlDumtm6W6IZMS3cc36c2KlVQ XLHmijEoS2KWeyVvQCKOFXtOzrwFqIRUaJmlTjy0LQ0hGL7QxperxbSbOARCBGDMWvcXHdRQiFW9J CJtT4opqM0XJQHR40VUkNyzb8TJReKItC0WsaKidG7bUDVNMwj8DlHfEcIuHeFlUsG48sRUIq+cce ZRu2LCjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQRk-00Fq6A-Bt; Mon, 16 Aug 2021 00:25:45 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQFN-00FlMJ-6S for linux-arm-kernel@lists.infradead.org; Mon, 16 Aug 2021 00:12:58 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id p71-20020a25424a0000b029056092741626so14937444yba.19 for ; Sun, 15 Aug 2021 17:12:55 -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=QYCRxSU6hzRxEScdPZjEavlvSGqLDqGY95R82nilknU=; b=nPrPgAfdfNekGbCmGN2vEq+MyvMfwzlUHjKLDCtKxgS8K2/fHTzB2r4moPCptUD6eg 5WWLr9m6Y5nHElPsYh4I+p/6d1611nEo0QJGGr+PRdF8iTApIzLk4fA1+UHlvxMtjfkE PpVnPyjufYcn1I11QAs6aaPOnKNa9rRFlfIhFu3yAIATOYzl3BSkOSqNnGdLNr3nH7Hw YOZGu29M8HAWwFl8SS3MavUWgxnyF7GuGmy+RyZ7ihZlSHfq8BDMqyI6mlBxCj1Wjskz pBU4/XcfXM84/5LFENjJjdg3s+rA7FLTgrAzw7OrpUjZDQH7wU+vpqsDYzuYfjxWOkmP GjHQ== 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=QYCRxSU6hzRxEScdPZjEavlvSGqLDqGY95R82nilknU=; b=PyCTAGAkVVdgnnlNmQtk1e3L6Jr8R9CAp/NC8cf9/b6XBCR2z22L/XWCASBhxFLGC/ IBBaADa6lGI9rsrHKo23beIYvyGSxPz6q4lDHflddeytKs/5YJQm5oTATYPuU9KMNXgH QPbUbBZDBqPhyTZDFXzsCIARx9CNaTgQgOAMenQRCNT99I+g0SGL8aJ+D872noRl46A3 E7LTtCITCi0nFOt69ITuIAj5zyR3FgvEO7HFkbED8GQqx84fLwajfiNVH9mRK6U7Ymew XKdLCjTYCEKhHqNz1g13NzRYM3d7qtj7R1+JdY5m5DrR92aCUFBvgu4nI2xVgQtyRTVd NCKA== X-Gm-Message-State: AOAM532zuZuiVOqexC754GzspRX/3mch4o46PMTmZqnvP2rE1/DFwtRJ 5Yk0BtDiThDQOJr5VH9ytUAi5BB6D44= X-Google-Smtp-Source: ABdhPJyFSNI5zwQDtft85GbhMjZlrSiGF5KmrBvQv4f2CVXAK7KuRny2Z8kdgnXXItSvoMx7C3nhgmhqLYg= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6902:102f:: with SMTP id x15mr17251492ybt.30.1629072775027; Sun, 15 Aug 2021 17:12:55 -0700 (PDT) Date: Mon, 16 Aug 2021 00:12:40 +0000 In-Reply-To: <20210816001246.3067312-1-oupton@google.com> Message-Id: <20210816001246.3067312-4-oupton@google.com> Mime-Version: 1.0 References: <20210816001246.3067312-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v7 3/9] selftests: KVM: Fix kvm device helper ioctl assertions 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-20210815_171257_312657_F4FB1AD5 X-CRM114-Status: GOOD ( 11.74 ) 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 The KVM_CREATE_DEVICE and KVM_{GET,SET}_DEVICE_ATTR ioctls are defined to return a value of zero on success. As such, tighten the assertions in the helper functions to only pass if the return code is zero. Suggested-by: Andrew Jones Reviewed-by: Andrew Jones Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/lib/kvm_util.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 10a8ed691c66..0ffc2d39c80d 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1984,7 +1984,7 @@ int kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr) { int ret = _kvm_device_check_attr(dev_fd, group, attr); - TEST_ASSERT(ret >= 0, "KVM_HAS_DEVICE_ATTR failed, rc: %i errno: %i", ret, errno); + TEST_ASSERT(!ret, "KVM_HAS_DEVICE_ATTR failed, rc: %i errno: %i", ret, errno); return ret; } @@ -2008,7 +2008,7 @@ int kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test) ret = _kvm_create_device(vm, type, test, &fd); if (!test) { - TEST_ASSERT(ret >= 0, + TEST_ASSERT(!ret, "KVM_CREATE_DEVICE IOCTL failed, rc: %i errno: %i", ret, errno); return fd; } @@ -2036,7 +2036,7 @@ int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, { int ret = _kvm_device_access(dev_fd, group, attr, val, write); - TEST_ASSERT(ret >= 0, "KVM_SET|GET_DEVICE_ATTR IOCTL failed, rc: %i errno: %i", ret, errno); + TEST_ASSERT(!ret, "KVM_SET|GET_DEVICE_ATTR IOCTL failed, rc: %i errno: %i", ret, errno); return ret; } From patchwork Mon Aug 16 00:12:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12437531 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 69172C4338F for ; Mon, 16 Aug 2021 00:26:44 +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 323AA6137D for ; Mon, 16 Aug 2021 00:26:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 323AA6137D 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=KGxXnQnP3Q0gJnyuLWgWvzWyZkiGWNW7RKmXQZ8Tmow=; b=qw85b76s6WN5TPfpqMba98LzjU dOELjTaKKOcJYX+C56cvGyGs83hjIMpNr6/oQgvCIMnySqlXBsjcohT4qrofGzRGCkBRTI+vYMCFr t8U8aPWbG9gGMH7MmY9DJwGAxbG0mzuYS37SYIinudFUT0sjSSk/ilkpRYm5dnU1w4S2eBYJAWAnR sl3MVSYD2PYnWDUdquZsmGiU/Ua28nAq3QwdfQtwuzk/P/f79hIzi3qTFs8yPnyQ1TR6LjT8jVV6/ RSLatR4lhAXhCOVaxVNW7ftpxdAPSoOSrRjXy7NW2EDua6HNNVn6CuPIMO/vC1bP4we7stvQCZx7K lueC4q4g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQQB-00Fpdd-Qi; Mon, 16 Aug 2021 00:24:09 +0000 Received: from mail-io1-xd4a.google.com ([2607:f8b0:4864:20::d4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQFN-00FlN7-OE for linux-arm-kernel@lists.infradead.org; Mon, 16 Aug 2021 00:12:59 +0000 Received: by mail-io1-xd4a.google.com with SMTP id g14-20020a6be60e000000b005b62a0c2a41so1147985ioh.2 for ; Sun, 15 Aug 2021 17:12:56 -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=qqp/SiKQXNqb52XXOCheL4cR6htl4LO0kcJJeErQ0KM=; b=nV85gIhy5Mdy9FrQ4UM0C4NrSPFVkV71ujHfJ8rK8gp3QoY27qqM575XGRKe+58Vo2 kcDC+dHLR7nkG5AVQdTUDca2fvIU1oUAbfEzIp0AAlP+WEGTD+pPoEyg9LJuklv9maAW RjsAIwwWh1y4qKsik7fUndJerBUoqxZc/d6HCtUjoCOSsDEub+WSTdzzLCAgK6LgfChf Dbbbam0nAMIpFzZjagiCPdWQjKm1rOUo5cUqL3Dy5eqUOxCMjo7f2AyBl+LaI+wAdni1 lCyPnsv3btnqTsR4tpajBW19R23leqbTIGNT28fgUD2ryFhkLY8d51B6tbYnNYadBzSm s07A== 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=qqp/SiKQXNqb52XXOCheL4cR6htl4LO0kcJJeErQ0KM=; b=jxw7m2EnRZocagNqrqntR4cqAYPEWcvJt3wc6PdR7oL+F8tYIyNBG8cFkpO6F3ceMA z0TynTHDhyH75PD1mzdZ8++07DO2N78Mg1q+6b0PcoZh3V1ZvWkwCcMprifYZJqoWkAG X7j2nlwtnHfTCO8/6ugX8e+W3pCgazMxSNvkWW5LjOqPRsmACiuPUP6PN79JRwuSV+47 LsJFyfDlrGJmK6ac4YWkflA6MVzPXJuj0iJGjbGVvwTFvHzUlz1GJa4b/88JiCumz1RW B7hEUCHkZ0UksSY87rcSfzzwQv90KWBv+tHz7tny2IVX0EsrX0tGs/TDo3Cl4wjXa2Km QMZA== X-Gm-Message-State: AOAM532LPpgZ+bzD32LfyVjWH0qj6VH5Vu05Rm0C17Gd8EywIzIXDaEs nWNBdlCv3oj18o+buYhlu6kklSPUhQk= X-Google-Smtp-Source: ABdhPJyPI7VnyfiX7cslqPDObv36x6Z1p0K9s6gygAZTtPKm2R0kUptF5KEoa2qPPVC4Q7CE4xo3WGA2Xus= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a02:cd89:: with SMTP id l9mr11939190jap.29.1629072776109; Sun, 15 Aug 2021 17:12:56 -0700 (PDT) Date: Mon, 16 Aug 2021 00:12:41 +0000 In-Reply-To: <20210816001246.3067312-1-oupton@google.com> Message-Id: <20210816001246.3067312-5-oupton@google.com> Mime-Version: 1.0 References: <20210816001246.3067312-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v7 4/9] selftests: KVM: Add helpers for vCPU device attributes 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-20210815_171257_828555_709C2E2A X-CRM114-Status: UNSURE ( 9.66 ) X-CRM114-Notice: Please train this message. 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 vCPU file descriptors are abstracted away from test code in KVM selftests, meaning that tests cannot directly access a vCPU's device attributes. Add helpers that tests can use to get at vCPU device attributes. Reviewed-by: Andrew Jones Signed-off-by: Oliver Upton --- .../testing/selftests/kvm/include/kvm_util.h | 9 +++++ tools/testing/selftests/kvm/lib/kvm_util.c | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index a8ac5d52e17b..1b3ef5757819 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -240,6 +240,15 @@ int _kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, void *val, bool write); +int _vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr); +int vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr); +int _vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr, void *val, bool write); +int vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr, void *val, bool write); + const char *exit_reason_str(unsigned int exit_reason); void virt_pgd_alloc(struct kvm_vm *vm); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 0ffc2d39c80d..0fe66ca6139a 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -2040,6 +2040,44 @@ int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, return ret; } +int _vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr) +{ + struct vcpu *vcpu = vcpu_find(vm, vcpuid); + + TEST_ASSERT(vcpu, "nonexistent vcpu id: %d", vcpuid); + + return _kvm_device_check_attr(vcpu->fd, group, attr); +} + +int vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr) +{ + int ret = _vcpu_has_device_attr(vm, vcpuid, group, attr); + + TEST_ASSERT(!ret, "KVM_HAS_DEVICE_ATTR IOCTL failed, rc: %i errno: %i", ret, errno); + return ret; +} + +int _vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr, void *val, bool write) +{ + struct vcpu *vcpu = vcpu_find(vm, vcpuid); + + TEST_ASSERT(vcpu, "nonexistent vcpu id: %d", vcpuid); + + return _kvm_device_access(vcpu->fd, group, attr, val, write); +} + +int vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr, void *val, bool write) +{ + int ret = _vcpu_access_device_attr(vm, vcpuid, group, attr, val, write); + + TEST_ASSERT(!ret, "KVM_SET|GET_DEVICE_ATTR IOCTL failed, rc: %i errno: %i", ret, errno); + return ret; +} + /* * VM Dump * From patchwork Mon Aug 16 00:12:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12437535 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=-14.6 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, UNWANTED_LANGUAGE_BODY,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 0F1C1C4338F for ; Mon, 16 Aug 2021 00:29:21 +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 C463A6138F for ; Mon, 16 Aug 2021 00:29:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C463A6138F 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=0yO4M6Ch0L1cfyDlBereJEKc7FGJA3repfvJPlPRaBg=; b=06zKBhKvAt9Ii4ag+smFOzj9Pu 7p1fYBQmWQ9xV8jkkEzJyFaBNRW0etTmb7PRZ/3XnNX4uDTdMyqWHmMNZdsxafugqfjTu1TvToawj N3e9XLvRXe+W8VaDvxCcvsCe/uUwihAcrceURGrAGCqb3pOSlBcwVowi772EnXzrqz+WcBVX2eG7E Bw1NSFO1+kFeePJJorbrQ/bmBubG0YX3/NFu9HBKgtIZhT9PvQPmhrNe48Vow/t1PvMsymcVmhx4s Lmjrqtv+S0GyRqRvtUXtpmezebkRzO9EBDKRN9ehhQzp7OSFSU+TS/8jGp6zoCE73L357IcfF7OhR Pg2mRCYA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQT4-00FqVI-Qw; Mon, 16 Aug 2021 00:27:08 +0000 Received: from mail-io1-xd49.google.com ([2607:f8b0:4864:20::d49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQFO-00FlNf-PG for linux-arm-kernel@lists.infradead.org; Mon, 16 Aug 2021 00:13:00 +0000 Received: by mail-io1-xd49.google.com with SMTP id s22-20020a5e98160000b02905afde383110so4775804ioj.8 for ; Sun, 15 Aug 2021 17:12: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=ayfGWlWzmNUKw7VcWz1FuZK9fj+r+Odsg+LA8h7I9IM=; b=H1Ss/u73mhOjmo3QAikiF0SVH3FuV1G7ER2IFRS2+hNWogGbSAg/f9+5PUpp7w7CLi vXqB5WbthilHYgWeujWkAR+R14+OqGlMcZcycPXLDetxHxZdu2OPhvQAvB8syORoEEdI KZZIRIvcAKMTq0brsm2n06okhIb9WhgF5tsXbgbRZZml7WFhx0PLkuwIvCQ9DbcVBkxs dJ+wsaWT77cGgD2ORbvCS+tqEZsU6gNnVdndbda5unv5q5LzAATWwzQ5YD3VHRHr7MUc WZVWJlPIXMYxK3lCpE8jbm9mZNC/s/omZlW+VAzdAwtA2SwIRV/XczVF77V/t8bKz82Y moEw== 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=ayfGWlWzmNUKw7VcWz1FuZK9fj+r+Odsg+LA8h7I9IM=; b=POsHE7wbfdlHC5AHl+JqqKG6aqXwSkEZnomv4dAhKbr9eAWl203epS2huYVR5EoswR bKGEU93nXrHGpivh0bW3pjO8vzj1wWFIXE20KNUrr4HQ3h41CZhtj6LZ207geXRryLZS IXS0YzYAn2MXa75oX8EwAMo3Waf/Rq3nPLQOjr67cJHzxTdwvoNw3VBId8P2qU1cBQnH /GhmVl/EAj61v3W/aQW0FxjzGGa6uR9myUDAm0FFgrA9UH5p3hl+JJpCCxRpwVntuyk/ 8QznsJMaDtSI+w4QBXy8PsgVfjdO7xq9UumBOrcl/bQRZPVpiePfLXHGjTosr5QvwDLY dkfQ== X-Gm-Message-State: AOAM532vXCO1gjXAi7/OlQ+DeZpol8XdCEh2RLJWGKkcFt1dNkgxoby/ Pl6V56LhTecds9CNCRuDvxT5jE5ZcXo= X-Google-Smtp-Source: ABdhPJyAbRyP/VkOYLoYhUyYnwxM8RMKT3VkkLEHBZsdHF2Ww/2WTFNj/xZRn2KQkue9PJGC7sBkkcdRKf0= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6e02:17cf:: with SMTP id z15mr6809959ilu.103.1629072777088; Sun, 15 Aug 2021 17:12:57 -0700 (PDT) Date: Mon, 16 Aug 2021 00:12:42 +0000 In-Reply-To: <20210816001246.3067312-1-oupton@google.com> Message-Id: <20210816001246.3067312-6-oupton@google.com> Mime-Version: 1.0 References: <20210816001246.3067312-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v7 5/9] selftests: KVM: Introduce 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 , Will Deacon , Catalin Marinas , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210815_171258_900380_515923FF X-CRM114-Status: GOOD ( 17.98 ) 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 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. Reviewed-by: Andrew Jones Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/system_counter_offset_test.c | 132 ++++++++++++++++++ 3 files changed, 134 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 06268d6b9aca..9a73befbd8d6 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -51,3 +51,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 3b09133e84a4..2cb0a375c7db 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -84,6 +84,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..b337bbbfa41f --- /dev/null +++ b/tools/testing/selftests/kvm/system_counter_offset_test.c @@ -0,0 +1,132 @@ +// 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)); + } +} + +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; + default: + TEST_ASSERT(0, "unhandled ucall %ld\n", + get_ucall(vm, VCPU_ID, &uc)); + } + } +} + +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); +} From patchwork Mon Aug 16 00:12:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12437537 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 D73B1C4338F for ; Mon, 16 Aug 2021 00:30:22 +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 9980961261 for ; Mon, 16 Aug 2021 00:30:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9980961261 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=fNCrYWVqXh9XBE4piV5OlhcHH/jIPPtENOT8MDuwK7M=; b=i1/6hyrTnJygOvlP7Fk91vY9Qg XRzb8Wg1hWflxoYRYi71NWPjxircMSpJ3K28bP/ikGtYCTLnzMeHSB+rno8EfD2LT/lFFDL/8kt2a UlU1eqnP4fHSYBr/gEi8EZ8h5csPyH8oiLFpr+KY4eC1azuS+O7O46rjDNn3ombxnelUS83zBtCLM 0MCDIDBbKKqX13oXGCDxQmsLNXuRIl5WPkZ/SgOZiE6wxhUkNFEJN2qs54hHMu8GSYuUt0ypmO796 ZP8kGdfUn8Olj30azY2ud3uNm35yBh2cyuXzcKFjh6lDGPd0HqckZgxqof4WdZe278m6xqFBGvjlu xXdsqmsQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQU4-00Fqpg-II; Mon, 16 Aug 2021 00:28:09 +0000 Received: from mail-il1-x149.google.com ([2607:f8b0:4864:20::149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQFP-00FlOC-JM for linux-arm-kernel@lists.infradead.org; Mon, 16 Aug 2021 00:13:01 +0000 Received: by mail-il1-x149.google.com with SMTP id c20-20020a9294140000b02902141528bc7cso8732821ili.3 for ; Sun, 15 Aug 2021 17:12:58 -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=LDn71X/mteHSm80S6t26TyxwkEj27LrlTf7lB/WnK0E=; b=tFGja0lkfeepK+FkjFeq/aVGW+mKNcPoNNlqpTMnu0V3vovCmctbotfMUDQjFSAcD4 6HOZbhkVK/AJkuPU4vE4cvo6B1mF3aDEYLG107YqyjKseSoClfb8cG4UAZjnRyke0Ce+ WkIpMN+ybYGn2RRLaX7uoEOw+pTWEzeY7aTzSFVuig04pceKNl0TVXOGaDRVgK2a8Vhr 2DXYsb5DVrL2L3qN297jolFCHfD3H6kPSjk+gIA/ush9nBL2y5iojzV6Yx6PC74aeADD Cak/sRa9pm9NFS5qTT+cpb8hE/wID6LMeB6ol1Ypc7u2S47YZbKgp0UUN7W+RYGywGaI mVRw== 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=LDn71X/mteHSm80S6t26TyxwkEj27LrlTf7lB/WnK0E=; b=AX/klCrAXqWa8IKdHIz6xeBJoD/q89L7XYj6hbTgpttg+fYw9kM34F6gRZG8Wxi491 83tUXIUhIDpgUX2Lh665Ugf1PpD7kBddR6g+9Br1WvHnr07O6acYxm4fnUCvowciIzYg BBnzXxGftSk6K2uU62mmII/6Fa3frDXoahHqBdrUfk0UgYDZ7C2VUNZQdpgZh/cBZEOM Uob+LDWbK58OGz9VfEMkT50LdLnZhJ6pZGMf4uoZw9w4lhToQ79RzLxVzX1/FuFgLJY2 SaT/IVvuV5XQILl2lVS6OU8bEOrO4amct7mw3o9rSFjHJDutvLUY9vcWsPs6/HBpXkds qF0A== X-Gm-Message-State: AOAM533CyeYVIhnlith4L0ssFSQhb1QglJxoduoaTYb/+EN1WXn4HZ60 ki6tBgtV/O4mroRq0r79+akuUClTE4Q= X-Google-Smtp-Source: ABdhPJxBGSNKp7nAwuyx4V4ZkrlD3NGCg0jwXeIu/+7/9/7758jjyZKMe4CTsweb4Xrvpxd+DmoDyTn6Lyo= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a6b:fb07:: with SMTP id h7mr3843368iog.201.1629072778180; Sun, 15 Aug 2021 17:12:58 -0700 (PDT) Date: Mon, 16 Aug 2021 00:12:43 +0000 In-Reply-To: <20210816001246.3067312-1-oupton@google.com> Message-Id: <20210816001246.3067312-7-oupton@google.com> Mime-Version: 1.0 References: <20210816001246.3067312-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v7 6/9] selftests: KVM: Add helper to check for register presence 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-20210815_171259_702887_8B30FFB9 X-CRM114-Status: GOOD ( 12.29 ) 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 The KVM_GET_REG_LIST vCPU ioctl returns a list of supported registers for a given vCPU. Add a helper to check if a register exists in the list of supported registers. Signed-off-by: Oliver Upton Reviewed-by: Andrew Jones --- .../testing/selftests/kvm/include/kvm_util.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 1b3ef5757819..077082dd2ca7 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -215,6 +215,8 @@ void vcpu_fpu_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_fpu *fpu); void vcpu_fpu_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_fpu *fpu); + +bool vcpu_has_reg(struct kvm_vm *vm, uint32_t vcpuid, uint64_t reg_id); void vcpu_get_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg); void vcpu_set_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg); #ifdef __KVM_HAVE_VCPU_EVENTS diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 0fe66ca6139a..a5801d4ed37d 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1823,6 +1823,25 @@ void vcpu_fpu_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_fpu *fpu) ret, errno, strerror(errno)); } +bool vcpu_has_reg(struct kvm_vm *vm, uint32_t vcpuid, uint64_t reg_id) +{ + struct kvm_reg_list *list; + bool ret = false; + uint64_t i; + + list = vcpu_get_reg_list(vm, vcpuid); + + for (i = 0; i < list->n; i++) { + if (list->reg[i] == reg_id) { + ret = true; + break; + } + } + + free(list); + return ret; +} + void vcpu_get_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg) { int ret; From patchwork Mon Aug 16 00:12:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12437539 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 A1D3CC4338F for ; Mon, 16 Aug 2021 00:31:59 +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 6422B6135E for ; Mon, 16 Aug 2021 00:31:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6422B6135E 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=vrzm50/qZKZFxqXloENg911Ncw/BV2YoM4gTNAjGNao=; b=WzegFX4D3xnR+O6cbWDOp7e8uF ZO4XQudG42MZGyoyA5h1YYCRWHC/mgXGcW+XoD4ZXeedgW/05djloxmWSBS8Il+pBXuWmJL6jndpU 7xCJ8/CJtxt96+alhMYYLFIStAXgpG/NuKuG/eLnDQ+JRHCxsjw1paMW7r3/TvQciSPfjoQvOqe3J 1N1xFBdMAZMZJXbcST/jU02ETKzrcoOipoxllRoCq/j94Ap9Hn+9nhUI36ugR92Dy4Yv4Ymh1zK6H x60u4OSJs788eLWp1mei86GU+kg6LZVKH8a47yJ8ptseWQGEBY51z9RuB6gJLNV7gu7WffGwpsQFC QXVZxpyg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQVE-00FrDd-PV; Mon, 16 Aug 2021 00:29:21 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQFQ-00FlOy-VF for linux-arm-kernel@lists.infradead.org; Mon, 16 Aug 2021 00:13:02 +0000 Received: by mail-yb1-xb49.google.com with SMTP id f3-20020a25cf030000b029055a2303fc2dso15055414ybg.11 for ; Sun, 15 Aug 2021 17:12:59 -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=Mo5DGr28+n9iD41dMXxXDuMQdLOo6WPPoeyjsxZY9VA=; b=vEEOUVGfAVc2um+9rckN8TCJebHnpC49fB/suHi6NhcRJhm92InWkAPaxRIIq5Z6dG OYjq3aT2bc+yiASefJLXGtf22AKm7Jhv/RwMsNTCAeH4C8hiAmKQom9SN9uvQTA28D9V 88PZIMcHvA0kQODCYk5iG4A56Yozi2FDXot2+JLTi6jIWYpbPdS9TFhOSctUOMJXX4Bm 9zaVrNMOtcH/5zFgBroNV/6N5tJx1rZ8+HjXvv93nHcFh1TX3rFPPqQgUzhtkSSep7pj /z/vmpL6Y39kEMU53J/b244iVnOb4NxDhWpg8IGAoP0Bwrj0/7Xo+D1H9mmlO/W9sbb+ 0ENg== 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=Mo5DGr28+n9iD41dMXxXDuMQdLOo6WPPoeyjsxZY9VA=; b=qPpxNjdm/NAlEW4YK1wsWF6uAP5C9imqyhZygIqb1M2uHlQEIL7BVPfQyJMPWSsAE7 /oN1Gid3r4O3asTIQUtPvQlL5u0RYjO6UEVFkwWVbF38mfARzw3zCPK66xOWBYBdpjbe 0thpadbHPAUDYEiDFfd4VH8faHR0izgibLnSrjzceHU2DEcj+N0M0ix5gU/98mA4KB9c iK9GI6Mpb+IFR9Sd3p+nGeqnGLBC9TsjzZnoQR4yi07XpCZVmCIKzIxSdgNZoLnScnQC dEe33zOi/v58fWrZWQEwRH1uc54xv2QSxX58zLroK/cIVa1rMYN4v2t6vPCwDXpA3tyG BogQ== X-Gm-Message-State: AOAM530k4ybqqOI/e+OfSRbAzTy+Usq9MxwNIsuy9DrhBCidOuzNsXmj 1//vrNR4bKGmxaZu1/wVYIap1jAw0AU= X-Google-Smtp-Source: ABdhPJzXHC3/0Ly0+ZPM9bFrtsB0V8DUl++e5d4rHoe6kt1DE0MaxEh+J8w0d9UH18WuCP1ITvcbO0Tgedk= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a25:d8c7:: with SMTP id p190mr10287387ybg.481.1629072779249; Sun, 15 Aug 2021 17:12:59 -0700 (PDT) Date: Mon, 16 Aug 2021 00:12:44 +0000 In-Reply-To: <20210816001246.3067312-1-oupton@google.com> Message-Id: <20210816001246.3067312-8-oupton@google.com> Mime-Version: 1.0 References: <20210816001246.3067312-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v7 7/9] 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 , Will Deacon , Catalin Marinas , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210815_171301_051095_EF171545 X-CRM114-Status: GOOD ( 15.61 ) 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 KVM/arm64 now allows userspace to adjust the guest virtual counter-timer via a vCPU register. 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 | 56 ++++++++++++++++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 2cb0a375c7db..a74d4876c69c 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -97,6 +97,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..ac933db83d03 100644 --- a/tools/testing/selftests/kvm/system_counter_offset_test.c +++ b/tools/testing/selftests/kvm/system_counter_offset_test.c @@ -53,7 +53,61 @@ 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_reg(vm, VCPU_ID, KVM_REG_ARM_TIMER_OFFSET)) + return; + + print_skip("KVM_REG_ARM_TIMER_OFFSET not supported; skipping test"); + exit(KSFT_SKIP); +} + +static void setup_system_counter(struct kvm_vm *vm, struct test_case *test) +{ + struct kvm_one_reg reg = { + .id = KVM_REG_ARM_TIMER_OFFSET, + .addr = (__u64)&test->offset, + }; + + vcpu_set_reg(vm, VCPU_ID, ®); +} + +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! From patchwork Mon Aug 16 00:12:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12437541 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 1541AC432BE for ; Mon, 16 Aug 2021 00:33:18 +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 CD5056135E for ; Mon, 16 Aug 2021 00:33:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org CD5056135E 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=CNkYrFWnDneuqtRKlWqCvX3maUEMVnHWrBDP0FlRBXE=; b=Nby/4qjgRMvAObiOKgi3MiCS3n TDSgjodJFZAOGTwG+X3FWp9oIpHSbShxQumj+px5qap99Hihoa9LTADkp4EIz3UtxgEbl8HUYKmsJ cpDbivZMWUhlKFyZiDCfgP4+bB/ULGirUpxFWqVQ/g+d00V/I7H0dHlBsvTyBMS4ekqhAlVgasHjz tiEIZ75KMLomDcS9EWLY9nMfHEZcskMDjMwHXNoP2gcM6TqeUcSklyEM58uvS4FrbU6uWlUw2EKKn gOaXTpCk2A0lD4eLYxv4+WBfmIHv0dzbCDkNXmA064uxX/5Rr9b8E3DYI07cjfa8DN5K9cNrllU0e 4vvHhrxg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQWM-00FrfG-Kh; Mon, 16 Aug 2021 00:30:30 +0000 Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQFR-00FlPm-VZ for linux-arm-kernel@lists.infradead.org; Mon, 16 Aug 2021 00:13:03 +0000 Received: by mail-qv1-xf4a.google.com with SMTP id u8-20020a0cec880000b029035825559ec4so11762014qvo.22 for ; Sun, 15 Aug 2021 17:13:01 -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=1lYvkLvXMkZOW0Omv5DbnXGOEluFv2cT8dCWAkq6vrY=; b=ucoXmLaWopVhB4jndrMR345quHVgr2BI7iLJWrHy29tE2EOZcJZyRCkaNYoq+cCb1s nOS6S8Mo5cgsX5YvhwHLiVQNh3/pP6tRaAztg6MMwEkUJ1ZfqzukhcDfDdQS0bsR99la N6N3itdIeBBf1CxU4aoxMwyGoHsFUeqWmfTwzyXGX1ALxhANxsreVkv4GiHivCkAgMzv IBc87KPLN/5IsJt4WkC+P9dw4aVfsGiEqo3A6OIborROOWqmCBJoZTCYMX2+F0FZWQl1 2hhsiQB4JVMYGPOFoaDPb34qTUO4z2df1RFV6BjoSiz6O6JsgLNHuFsrFiFyDJFtkLch zg3A== 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=1lYvkLvXMkZOW0Omv5DbnXGOEluFv2cT8dCWAkq6vrY=; b=bqwQS1qvp4AG+k9EcbRk4Apmxcx2MjqxigTUbXACmW4R+rX7zmarATmhHYL57DB3lV nhDW7XqcmENiecO6AsgY9/JAR8seX7h+aJdVQasn9EHRBQKJ2B/QoausyDKL0eRTlaoS 5R1NyDmFxoUuLshc5egmUJyYAbgDM0JeFrF6EHaMDFMDTVj3UOWisyrzisS18Fcqkb2I WKcR7PvVjfc2izd9aSY1exuXowOnr25inpX9i06MATDW8o3T9F1XuR+hzJip1oVreVjY 3W18v9qyVJJwu5KgCjVPX2d0NF4jMps4vmgK8VGTyhN5wcQQNNi34vRHBNVl7R5N+0iK D7jA== X-Gm-Message-State: AOAM532WcAlswXHoerrMDB2GgzVYOOlMjXqlrC+QcQOR0T28a3IKpAFU /K4gwP0ciXaxRdPJUnhcbHRuXW7Pahg= X-Google-Smtp-Source: ABdhPJx2WNiG5Xm9EOAshaEpB0QvoPQw0RzciABjW2FV+0QygZ9dWPd5OcyKOs52q3Nfg8WF270Sk4LNSTM= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a0c:e609:: with SMTP id z9mr13684667qvm.37.1629072780405; Sun, 15 Aug 2021 17:13:00 -0700 (PDT) Date: Mon, 16 Aug 2021 00:12:45 +0000 In-Reply-To: <20210816001246.3067312-1-oupton@google.com> Message-Id: <20210816001246.3067312-9-oupton@google.com> Mime-Version: 1.0 References: <20210816001246.3067312-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v7 8/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-20210815_171302_087941_F6AD0674 X-CRM114-Status: GOOD ( 14.78 ) 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 Change-Id: I8a26e88df5c5bc03740c7f980c405b4d21b28be8 --- .../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..2b8b4b84a273 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_PHYS_OFFSET)) return; - print_skip("KVM_REG_ARM_TIMER_OFFSET not supported; skipping test"); + print_skip("KVM_REG_ARM_TIMER_OFFSET|KVM_ARM_VCPU_TIMER_PHYS_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_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); } From patchwork Mon Aug 16 00:12:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12437543 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 D0B60C4338F for ; Mon, 16 Aug 2021 00:33:45 +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 9BA566135E for ; Mon, 16 Aug 2021 00:33:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9BA566135E 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=3Cm9IyNNLIN7va8XuCK4D1lYTx40t+RT0zm88ACCxzg=; b=W9rUuuPXmrz68WwNgyxb/HNOpW MB3QrCstCOX1MWGGKb0h7uYQV6VIQ/28/4Yp9vxfB1Fcky/OgP/G+VEPaoPAjZi7hAf4LKt0rs3RH lqq6dm01YsEGsicD3cZru6h2Bdq/C0JF22GjCT6XNuST0kW0cU5OcQmbP5NNbWXQLpy4rR6KSoV9o 0nNFq0aQY7ZKrU5v2dgUyOzBMvAsSoYXmGXdsmCj7w5Hp7m3m6prC5syb5d+3FdWHydOST2HP9LLR rXt9Vyq310/bVgIXxYFghA8KAvei780FzfO134XPqllLZPF/JmyHkHaMKulIsrXXa2OnQjOmURXvZ 6rk5QFDA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQXL-00Fs0i-Io; Mon, 16 Aug 2021 00:31:32 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFQFT-00FlQa-4y for linux-arm-kernel@lists.infradead.org; Mon, 16 Aug 2021 00:13:05 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 203-20020a250bd40000b02905933fff7091so15035826ybl.16 for ; Sun, 15 Aug 2021 17:13:02 -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=q/e6QF13PbPmzNyeEHftd6LeZ3rm+y0LrpmmQX2iAWM=; b=mZQUuekC/Wn8aj3bTX5IwP8Q/uxfw7bpTRbmkVO6crVko3MYwmMop1um7jNsOuIA5G 2l7IQBaS8rZb2aP3GDppVo/kxpeMHSTowDCLmcOSVWFYG+toZyWpfqzwYMEkPGoP+GAr Vxxxe3eGGIT7KZ0BNLxroWGMHSDl8kEndxmKA5kojcb3OHcvNu6CfqYeSHjFzwrMaDdv jyncHkHfIGUoOx1Xj5edz3qHxQgIOwIjNbXpAMqnCMKInrbkgoGw5hCj0W+RstgOFWIg zL5Zd8JpDIXTsZB/uIGX8V0zRfma8YgV2qFMPNv8+dKZetTlsvcSiBYSxv8SoZGL5DQD JdPw== 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=q/e6QF13PbPmzNyeEHftd6LeZ3rm+y0LrpmmQX2iAWM=; b=q5ktzCoCNyEzTKvnQgnA5pIa6K9vDPeL0TK6f/itxTJ5uY3b+gRhbpbSFh0xLIOyom PwAaVUvZQu9k1aXGx1SauZt/gLClyooDyk4N8Wr1wbmuyRC9/rtZHtxHEev1gxN4kton 9pvp8bGmNzJoxZEQIMGE5DAIoSh4Lg2QCoMnd5DFC00nZbquRJF6WOgsEMKWAlsWnXT8 l8GG0fKcZKje5dtjn73Vz0c/uazbM98rjctfI7Cgnk9pBbGA58oL7jyn9nkTP1FFoXXS GhARRZvbtpt6doJVwuG4I6YS7HBf9fUt7e24+NjEamnC65Yk0QSR6M6bZoU0i/gW7Mgg tvmA== X-Gm-Message-State: AOAM530QPJrj5GMRON4BZlUt7WBeRMuK2u3RHLljMEYCvpAXa7SwwFyu aqa9pLhugoK5dSBh6CoQxjGvgm713Kw= X-Google-Smtp-Source: ABdhPJxqohNtoShnC6CGm0BF0rD3zR+u+wedYLYQEXcm4ORB58buXd3rRAuCc1dYfmncyVSTVuHAktSHhpg= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a25:810b:: with SMTP id o11mr18944380ybk.495.1629072781457; Sun, 15 Aug 2021 17:13:01 -0700 (PDT) Date: Mon, 16 Aug 2021 00:12:46 +0000 In-Reply-To: <20210816001246.3067312-1-oupton@google.com> Message-Id: <20210816001246.3067312-10-oupton@google.com> Mime-Version: 1.0 References: <20210816001246.3067312-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v7 9/9] selftests: KVM: Add counter emulation benchmark 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-20210815_171303_261942_5A122FC8 X-CRM114-Status: GOOD ( 20.87 ) 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 Add a test case for counter emulation on arm64. A side effect of how KVM handles physical counter offsetting on non-ECV systems is that the virtual counter will always hit hardware and the physical could be emulated. Force emulation by writing a nonzero offset to the physical counter and compare the elapsed cycles to a direct read of the hardware register. Reviewed-by: Ricardo Koller Signed-off-by: Oliver Upton Reviewed-by: Andrew Jones Change-Id: Iec9ed89346c3f65b7f9067a7e96a9d94afbab526 --- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/aarch64/counter_emulation_benchmark.c | 207 ++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index 9a73befbd8d6..0f3d842b82d6 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only +/aarch64/counter_emulation_benchmark /aarch64/debug-exceptions /aarch64/get-reg-list /aarch64/vgic_init diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index a74d4876c69c..3652b39e02b8 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -86,6 +86,7 @@ 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/counter_emulation_benchmark TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list TEST_GEN_PROGS_aarch64 += aarch64/vgic_init diff --git a/tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c b/tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c new file mode 100644 index 000000000000..48dbd9459102 --- /dev/null +++ b/tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * counter_emulation_benchmark.c -- test to measure the effects of counter + * emulation on guest reads of the physical counter. + * + * Copyright (c) 2021, Google LLC. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" + +#define VCPU_ID 0 + +static struct counter_values { + uint64_t cntvct_start; + uint64_t cntpct; + uint64_t cntvct_end; +} counter_values; + +static uint64_t nr_iterations = 1000; + +static void do_test(void) +{ + /* + * Open-coded approach instead of using helper methods to keep a tight + * interval around the physical counter read. + */ + asm volatile("isb\n\t" + "mrs %[cntvct_start], cntvct_el0\n\t" + "isb\n\t" + "mrs %[cntpct], cntpct_el0\n\t" + "isb\n\t" + "mrs %[cntvct_end], cntvct_el0\n\t" + "isb\n\t" + : [cntvct_start] "=r"(counter_values.cntvct_start), + [cntpct] "=r"(counter_values.cntpct), + [cntvct_end] "=r"(counter_values.cntvct_end)); +} + +static void guest_main(void) +{ + int i; + + for (i = 0; i < nr_iterations; i++) { + do_test(); + GUEST_SYNC(i); + } + + for (i = 0; i < nr_iterations; i++) { + do_test(); + GUEST_SYNC(i); + } +} + +static void enter_guest(struct kvm_vm *vm) +{ + struct ucall uc; + + vcpu_ioctl(vm, VCPU_ID, KVM_RUN, NULL); + + switch (get_ucall(vm, VCPU_ID, &uc)) { + case UCALL_SYNC: + break; + case UCALL_ABORT: + TEST_ASSERT(false, "%s at %s:%ld", (const char *)uc.args[0], + __FILE__, uc.args[1]); + break; + default: + TEST_ASSERT(false, "unexpected exit: %s", + exit_reason_str(vcpu_state(vm, VCPU_ID)->exit_reason)); + break; + } +} + +static double counter_frequency(void) +{ + uint32_t freq; + + asm volatile("mrs %0, cntfrq_el0" + : "=r" (freq)); + + return freq / 1000000.0; +} + +static void log_csv(FILE *csv, bool trapped) +{ + double freq = counter_frequency(); + + fprintf(csv, "%s,%.02f,%lu,%lu,%lu\n", + trapped ? "true" : "false", freq, + counter_values.cntvct_start, + counter_values.cntpct, + counter_values.cntvct_end); +} + +static double run_loop(struct kvm_vm *vm, FILE *csv, bool trapped) +{ + double avg = 0; + int i; + + for (i = 0; i < nr_iterations; i++) { + uint64_t delta; + + enter_guest(vm); + sync_global_from_guest(vm, counter_values); + + if (csv) + log_csv(csv, trapped); + + delta = counter_values.cntvct_end - counter_values.cntvct_start; + avg = ((avg * i) + delta) / (i + 1); + } + + return avg; +} + +static void setup_counter(struct kvm_vm *vm, uint64_t offset) +{ + vcpu_access_device_attr(vm, VCPU_ID, KVM_ARM_VCPU_TIMER_CTRL, + KVM_ARM_VCPU_TIMER_PHYS_OFFSET, &offset, + true); +} + +static void run_tests(struct kvm_vm *vm, FILE *csv) +{ + double avg_trapped, avg_native, freq; + + freq = counter_frequency(); + + if (csv) + fputs("trapped,freq_mhz,cntvct_start,cntpct,cntvct_end\n", csv); + + /* no physical offsetting; kvm allows reads of cntpct_el0 */ + setup_counter(vm, 0); + avg_native = run_loop(vm, csv, false); + + /* force emulation of the physical counter */ + setup_counter(vm, 1); + avg_trapped = run_loop(vm, csv, true); + + pr_info("%lu iterations: average cycles (@%.02fMHz) native: %.02f, trapped: %.02f\n", + nr_iterations, freq, avg_native, avg_trapped); +} + +static void usage(const char *program_name) +{ + fprintf(stderr, + "Usage: %s [-h] [-o csv_file] [-n iterations]\n" + " -h prints this message\n" + " -n number of test iterations (default: %lu)\n" + " -o csv file to write data\n", + program_name, nr_iterations); +} + +int main(int argc, char **argv) +{ + struct kvm_vm *vm; + FILE *csv = NULL; + int opt; + + while ((opt = getopt(argc, argv, "hn:o:")) != -1) { + switch (opt) { + case 'o': + csv = fopen(optarg, "w"); + if (!csv) { + fprintf(stderr, "failed to open file '%s': %d\n", + optarg, errno); + exit(1); + } + break; + case 'n': + nr_iterations = strtoul(optarg, NULL, 0); + break; + default: + fprintf(stderr, "unrecognized option: '-%c'\n", opt); + /* fallthrough */ + case 'h': + usage(argv[0]); + exit(1); + } + } + + vm = vm_create_default(VCPU_ID, 0, guest_main); + sync_global_to_guest(vm, nr_iterations); + ucall_init(vm, NULL); + + 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); + } + + run_tests(vm, csv); + kvm_vm_free(vm); + + if (csv) + fclose(csv); +}