Received: by 2002:adf:e804:0:0:0:0:0 with SMTP id o4csp7234112wrm; Wed, 16 Oct 2019 07:34:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqw0o7+dTiZ6cp4B1ARGMYMlEqkXRmPu0zKkPyKCTat+b+xvlGe2XEr7pOJ2pBs0HI7czcwu X-Received: by 2002:a05:6214:725:: with SMTP id c5mr41117178qvz.175.1571236492346; Wed, 16 Oct 2019 07:34:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571236492; cv=none; d=google.com; s=arc-20160816; b=oKSvR5bwKl30IcpJ0Oas4VTsCHMrBuNbSRsmuacGYHn19UeKvnslivIJ4V8XyPxJMr w5L+aGk2FHIXJfT1br5arxlPiXedj4Z56M4c/R83tyffO7W2Njml77yXdKuk1rlCZIKu 18UU+Yqllv6DI9+R6bnGz67Dep9w2VHtJF8DLRAAhZNUNRDpzjwZq4SOkUHJFpzMtoKf cwOgKEiiOhmkkuUD5ix9xpih3vMK7jlkABafHw6GabN9bAa+okZ1MvV6CHENnW2ahxVe +UCsih9mQXboF3BzvpRf20fs4wCs/9hpPAUNkvIC+IKfYF1vuhdoamXR76U9wOPnfX9X Efrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from; bh=bjPgRvM3FEBGI5BBaeFEAqrJkdolBhpc+7FM9CEwi6w=; b=Kzj9NVAmrLwka+n8zNNb514MGBteHAZi6gUtaiQbrPua5hswHT/Xq6ZOdNjeqJwtSm L4P+VZPtiyiv+GZRdhzdAPCspvNnVAIHYSFORGALBAQMUFACvrz4o/UgMq5YLpM2pn7R zIZA7M2nAJqQXvhr36VDe5nBF5864G/IfSd2zCzsn0n4aF5ckGnncszx22fpf783AF3U xW1On91+unaGLSvSgte7Y/495+lyaqRAZQNVI9WbK7xgEb8Dt1b6RQI8pi3IG36MYm18 Rz6z40dPE+B3A3hVXE2hcrbzPczrubLQEPMqPR+nMvcpwGilQC1ClL8jpuf8oc/ljZ6T chBw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 17si26029881qkg.40.2019.10.16.07.34.52 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 16 Oct 2019 07:34:52 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from localhost ([::1]:43396 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iKkO3-0005Cg-0l for alex.bennee@linaro.org; Wed, 16 Oct 2019 10:34:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41719) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iKkNZ-00055Z-Hh for qemu-arm@nongnu.org; Wed, 16 Oct 2019 10:34:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iKkNY-0000id-61 for qemu-arm@nongnu.org; Wed, 16 Oct 2019 10:34:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37766) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iKkNX-0000iG-UU; Wed, 16 Oct 2019 10:34:20 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2AE7F8553A; Wed, 16 Oct 2019 14:34:19 +0000 (UTC) Received: from kamzik.brq.redhat.com (unknown [10.43.2.160]) by smtp.corp.redhat.com (Postfix) with ESMTP id ACF3E19C68; Wed, 16 Oct 2019 14:34:17 +0000 (UTC) From: Andrew Jones To: qemu-devel@nongnu.org, qemu-arm@nongnu.org Subject: [PATCH v1 3/5] target/arm/kvm: Implement cpu feature kvm-adjvtime Date: Wed, 16 Oct 2019 16:34:08 +0200 Message-Id: <20191016143410.5023-4-drjones@redhat.com> In-Reply-To: <20191016143410.5023-1-drjones@redhat.com> References: <20191016143410.5023-1-drjones@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 16 Oct 2019 14:34:19 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 X-BeenThere: qemu-arm@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, richard.henderson@linaro.org, bijan.mottahedeh@oracle.com, maz@kernel.org Errors-To: qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org Sender: "Qemu-arm" X-TUID: 8vnZCBAkcp4Y When kvm-adjvtime is enabled the guest's cntvct[_el0] won't count the time when the VM is stopped. That time is skipped by updating cntvoff[_el2] on each transition to vm_running using the current QEMU_CLOCK_VIRTUAL time. QEMU_CLOCK_VIRTUAL only ticks when the VM is running. This patch only provides the implementation. A subsequent patch will provide the CPU property allowing the feature to be enabled. Reported-by: Bijan Mottahedeh Suggested-by: Bijan Mottahedeh Signed-off-by: Andrew Jones Reviewed-by: Richard Henderson --- target/arm/cpu.h | 3 +++ target/arm/kvm.c | 32 ++++++++++++++++++++++++++++++++ target/arm/kvm32.c | 3 +++ target/arm/kvm64.c | 3 +++ target/arm/kvm_arm.h | 14 ++++++++++++++ 5 files changed, 55 insertions(+) diff --git a/target/arm/cpu.h b/target/arm/cpu.h index b3092e5213e6..7c07c7c5272e 100644 --- a/target/arm/cpu.h +++ b/target/arm/cpu.h @@ -821,6 +821,9 @@ struct ARMCPU { /* KVM init features for this CPU */ uint32_t kvm_init_features[7]; =20 + /* KVM CPU features */ + bool kvm_adjvtime; + /* Uniprocessor system with MP extensions */ bool mp_is_up; =20 diff --git a/target/arm/kvm.c b/target/arm/kvm.c index 5b82cefef608..373b868dc248 100644 --- a/target/arm/kvm.c +++ b/target/arm/kvm.c @@ -348,6 +348,18 @@ void kvm_arm_register_device(MemoryRegion *mr, uint6= 4_t devid, uint64_t group, memory_region_ref(kd->mr); } =20 +void kvm_arm_vm_state_change(void *opaque, int running, RunState state) +{ + CPUState *cs =3D opaque; + ARMCPU *cpu =3D ARM_CPU(cs); + + if (running) { + if (cpu->kvm_adjvtime) { + kvm_arm_set_virtual_time(cs); + } + } +} + static int compare_u64(const void *a, const void *b) { if (*(uint64_t *)a > *(uint64_t *)b) { @@ -579,6 +591,26 @@ int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu) return 0; } =20 +void kvm_arm_set_virtual_time(CPUState *cs) +{ + uint64_t cnt; + struct kvm_one_reg reg =3D { + .id =3D KVM_REG_ARM_TIMER_CNT, + .addr =3D (uintptr_t)&cnt, + }; + int ret; + + cnt =3D muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), + cpu_get_host_tick_frequency(), + NANOSECONDS_PER_SECOND); + + ret =3D kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); + if (ret) { + error_report("Failed to set KVM_REG_ARM_TIMER_CNT"); + abort(); + } +} + int kvm_put_vcpu_events(ARMCPU *cpu) { CPUARMState *env =3D &cpu->env; diff --git a/target/arm/kvm32.c b/target/arm/kvm32.c index 32bf8d6757c4..3a8b437eef0b 100644 --- a/target/arm/kvm32.c +++ b/target/arm/kvm32.c @@ -16,6 +16,7 @@ #include "qemu-common.h" #include "cpu.h" #include "qemu/timer.h" +#include "sysemu/runstate.h" #include "sysemu/kvm.h" #include "kvm_arm.h" #include "internals.h" @@ -198,6 +199,8 @@ int kvm_arch_init_vcpu(CPUState *cs) return -EINVAL; } =20 + qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cs); + /* Determine init features for this CPU */ memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features)); if (cpu->start_powered_off) { diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c index 666a81a5ce6f..d368189fbce6 100644 --- a/target/arm/kvm64.c +++ b/target/arm/kvm64.c @@ -23,6 +23,7 @@ #include "qemu/host-utils.h" #include "qemu/main-loop.h" #include "exec/gdbstub.h" +#include "sysemu/runstate.h" #include "sysemu/kvm.h" #include "sysemu/kvm_int.h" #include "kvm_arm.h" @@ -735,6 +736,8 @@ int kvm_arch_init_vcpu(CPUState *cs) return -EINVAL; } =20 + qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cs); + /* Determine init features for this CPU */ memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features)); if (cpu->start_powered_off) { diff --git a/target/arm/kvm_arm.h b/target/arm/kvm_arm.h index 8e14d400e8ab..3bb7e331aa06 100644 --- a/target/arm/kvm_arm.h +++ b/target/arm/kvm_arm.h @@ -232,6 +232,16 @@ void kvm_arm_sve_get_vls(CPUState *cs, unsigned long= *map); */ void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu); =20 +/** + * void kvm_arm_set_virtual_time: + * @cs: CPUState + * + * Sets the guest's virtual counter offset to the difference of the host= 's + * current time and QEMU's QEMU_CLOCK_VIRTUAL time. This allows the + * guest's virtual counter to only reflect VM running time. + */ +void kvm_arm_set_virtual_time(CPUState *cs); + /** * kvm_arm_aarch32_supported: * @cs: CPUState @@ -288,6 +298,8 @@ void kvm_arm_pmu_set_irq(CPUState *cs, int irq); void kvm_arm_pmu_init(CPUState *cs); int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level); =20 +void kvm_arm_vm_state_change(void *opaque, int running, RunState state); + #else =20 static inline void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu) @@ -324,6 +336,8 @@ static inline int kvm_arm_vgic_probe(void) return 0; } =20 +static inline void kvm_arm_set_virtual_time(CPUState *cs) {} + static inline void kvm_arm_pmu_set_irq(CPUState *cs, int irq) {} static inline void kvm_arm_pmu_init(CPUState *cs) {} =20 --=20 2.21.0