From nobody Sat Apr 27 13:32:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149563909113798.81060058671642; Wed, 24 May 2017 08:18:11 -0700 (PDT) Received: from localhost ([::1]:55456 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dDY33-00059e-EP for importer@patchew.org; Wed, 24 May 2017 11:18:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39685) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dDWwn-0008W1-NP for qemu-devel@nongnu.org; Wed, 24 May 2017 10:07:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dDWwk-000574-IO for qemu-devel@nongnu.org; Wed, 24 May 2017 10:07:33 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:12797 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dDWwk-00054H-7f for qemu-devel@nongnu.org; Wed, 24 May 2017 10:07:30 -0400 Received: from dptest2.qa.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4OE7OQc029889; Wed, 24 May 2017 17:07:24 +0300 (MSK) From: Denis Plotnikov To: pbonzini@redhat.com Date: Wed, 24 May 2017 17:07:24 +0300 Message-Id: <1495634844-14777-1-git-send-email-dplotnikov@virtuozzo.com> X-Mailer: git-send-email 1.8.3.1 X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 X-Mailman-Approved-At: Wed, 24 May 2017 11:16:28 -0400 Subject: [Qemu-devel] [PATCH] kvmclock: update system_time_msr address forcibly X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: svt-core@lists.sw.ru, mtosatti@redhat.com, qemu-devel@nongnu.org, den@virtuozzo.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Do an update of system_time_msr address every time before reading the value of tsc_timestamp from guest's kvmclock page. It should be done in a forcible manner because there is a situation when system_time_msr has been set by kvm but qemu doesn't aware of it. This leads to updates of kvmclock_offset without respect of guest's kvmclock values. The situation appears when L2 linux guest runs over L1 linux guest and the action inducing system_time_msr update is tpr access reporting. Some L1 linux guests turn off processing TPR access and when L0 gets an L2 exit induced by TPR MSR access it doesn't enter L1 and processed it by itself. Thus, L1 kvm doesn't know about that TPR access happening and doesn't exit to qemu which in turn doesn't set system_time_msr address. This patch fixes this by making sure it knows the correct address every time it is needed. Signed-off-by: Denis Plotnikov --- hw/i386/kvm/clock.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c index e713162..035196a 100644 --- a/hw/i386/kvm/clock.c +++ b/hw/i386/kvm/clock.c @@ -48,11 +48,38 @@ struct pvclock_vcpu_time_info { uint8_t pad[2]; } __attribute__((__packed__)); /* 32 bytes */ =20 +static void update_all_system_time_msr(void) +{ + CPUState *cpu; + CPUX86State *env; + struct { + struct kvm_msrs info; + struct kvm_msr_entry entries[1]; + } msr_data; + int ret; + + msr_data.info.nmsrs =3D 1; + msr_data.entries[0].index =3D MSR_KVM_SYSTEM_TIME; + + CPU_FOREACH(cpu) { + ret =3D kvm_vcpu_ioctl(cpu, KVM_GET_MSRS, &msr_data); + + if (ret < 0) { + fprintf(stderr, "KVM_GET_MSRS failed: %s\n", strerror(ret)); + abort(); + } + + assert(ret =3D=3D 1); + env =3D cpu->env_ptr; + env->system_time_msr =3D msr_data.entries[0].data; + } +} + static uint64_t kvmclock_current_nsec(KVMClockState *s) { CPUState *cpu =3D first_cpu; CPUX86State *env =3D cpu->env_ptr; - hwaddr kvmclock_struct_pa =3D env->system_time_msr & ~1ULL; + hwaddr kvmclock_struct_pa; uint64_t migration_tsc =3D env->tsc; struct pvclock_vcpu_time_info time; uint64_t delta; @@ -60,6 +87,9 @@ static uint64_t kvmclock_current_nsec(KVMClockState *s) uint64_t nsec_hi; uint64_t nsec; =20 + update_all_system_time_msr(); + kvmclock_struct_pa =3D env->system_time_msr & ~1ULL; + if (!(env->system_time_msr & 1ULL)) { /* KVM clock not active */ return 0; --=20 2.7.4