From nobody Sun May 5 19:51:47 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1560759916; cv=none; d=zoho.com; s=zohoarc; b=atpor9E+Muxp9osZIK+rwmCWr382ZixGpAHACUbpDdlWGRjkFMQu8GMLRnYuRXTxACjn7kng8mm0kRbNhrROhFA/r5tDcAdwSqzwSBT3HVn8Rlwj3irWT8som7EzUM0PFa7EgvA4BjflXkenGAI6bs9rq1fBj9WqacHvJR5RJh8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560759916; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=OCI4V/R6PZNFGvY/JSOPU0KQ8U1m9oOc0jt8QeVPmG8=; b=X5Iq/N99EtXm6MXvsQOKQu6qCpx8+uu0UzeABBecku3adV4BNMSavn0IY4doVP60Zzr87220IF70MUMRdDZpuYk4So7DTnaJpVhf8ytboL7wQwcfPcieK/jJrENxFU1/SzRH8P8Z5b+frqqCz/vv01d0bND4Tqw/ZpQ0Dl5Txp0= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1560759916746292.54256669379185; Mon, 17 Jun 2019 01:25:16 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hcmvu-0004hP-Ms; Mon, 17 Jun 2019 08:24:06 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hcmvs-0004hI-Lp for xen-devel@lists.xenproject.org; Mon, 17 Jun 2019 08:24:04 +0000 Received: from esa4.hc3370-68.iphmx.com (unknown [216.71.155.144]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 42db302a-90d9-11e9-8980-bc764e045a96; Mon, 17 Jun 2019 08:24:01 +0000 (UTC) X-Inumbo-ID: 42db302a-90d9-11e9-8980-bc764e045a96 Authentication-Results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=paul.durrant@citrix.com; spf=Pass smtp.mailfrom=Paul.Durrant@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of paul.durrant@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="paul.durrant@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa4.hc3370-68.iphmx.com: domain of Paul.Durrant@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="Paul.Durrant@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: 2EJ5GOIkHBLLq5YY1OEt7ImCyh73qego/JJNJ4PBmhWY05Qr2ez4u8MrWE41ECTfzLIdRPGpam o2TiMD/p94Q3bLsSQCFn/t4cWaVDTklcifJ6GgWIUH9+B1nFwd52KuekZkAS7FPorTo7phk7hT u519j+n/Rjh5cT80rN9pIIsU4rBbOGak5ZbX+846JlCiZCFQiiymEGsks36Omam24R9NGp+4cK gFFqEWD6KpvrnyGW/2BZAGVqzxq1TgS8VlF2z99HoDV7b+sknas76pWZ2W4xkgFgE0xCTMPHxF LAI= X-SBRS: 2.7 X-MesageID: 1823119 X-Ironport-Server: esa4.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,384,1557201600"; d="scan'208";a="1823119" From: Paul Durrant To: Date: Mon, 17 Jun 2019 09:23:58 +0100 Message-ID: <20190617082358.2734-1-paul.durrant@citrix.com> X-Mailer: git-send-email 2.20.1.2.gb21ebb671 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v2] viridian: unify time sources X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Paul Durrant , Wei Liu , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Currently, the time_ref_count enlightened time source maintains an offset such that time is frozen when the domain paused, but the reference_tsc enlightened time source does not. After migrate, the reference_tsc source may become invalidated (e.g. because of host cpu frequency mismatch) which will cause Windows to fall back to time_ref_count. Thus, the guest will observe a jump in time equivalent to the offset. This patch unifies the two enlightened time sources such that the same offset applies to both of them. Also, it's not really necessary to have two different functions to calculating a 10MHz counter value, time_now() and raw_trc_val(), so this patch removes the latter implementation. The unification also allows removal of the reference_tsc_valid flag. Whilst in the area, this patch also takes the opportunity to constify a few pointers which were missed in earlier patches. Signed-off-by: Paul Durrant Acked-by: Jan Beulich --- Cc: Jan Beulich Cc: Andrew Cooper Cc: Wei Liu Cc: "Roger Pau Monn=C3=A9" v2: - Expand comments above newly added TscOffset setting --- xen/arch/x86/hvm/viridian/time.c | 105 +++++++++++++---------------- xen/include/asm-x86/hvm/viridian.h | 1 - 2 files changed, 45 insertions(+), 61 deletions(-) diff --git a/xen/arch/x86/hvm/viridian/time.c b/xen/arch/x86/hvm/viridian/t= ime.c index 2a3c9697d7..28e3d88ca9 100644 --- a/xen/arch/x86/hvm/viridian/time.c +++ b/xen/arch/x86/hvm/viridian/time.c @@ -26,9 +26,10 @@ typedef struct _HV_REFERENCE_TSC_PAGE uint64_t Reserved2[509]; } HV_REFERENCE_TSC_PAGE, *PHV_REFERENCE_TSC_PAGE; =20 -static void update_reference_tsc(struct domain *d, bool initialize) +static void update_reference_tsc(const struct domain *d, bool initialize) { struct viridian_domain *vd =3D d->arch.hvm.viridian; + const struct viridian_time_ref_count *trc =3D &vd->time_ref_count; const struct viridian_page *rt =3D &vd->reference_tsc; HV_REFERENCE_TSC_PAGE *p =3D rt->ptr; uint32_t seq; @@ -44,7 +45,9 @@ static void update_reference_tsc(struct domain *d, bool i= nitialize) * with this, allowing vtsc to be turned off, but support for this is * not yet present in the hypervisor. Thus is it is possible that * migrating a Windows VM between hosts of differing TSC frequencies - * may result in large differences in guest performance. + * may result in large differences in guest performance. Any jump in + * TSC due to migration down-time can, however, be compensated for by + * setting the TscOffset value (see below). */ if ( !host_tsc_is_safe() || d->arch.vtsc ) { @@ -62,8 +65,6 @@ static void update_reference_tsc(struct domain *d, bool i= nitialize) =20 printk(XENLOG_G_INFO "d%d: VIRIDIAN REFERENCE_TSC: invalidated\n", d->domain_id); - - vd->reference_tsc_valid =3D false; return; } =20 @@ -75,8 +76,11 @@ static void update_reference_tsc(struct domain *d, bool = initialize) * * Windows uses a 100ns tick, so we need a scale which is cpu * ticks per 100ns shifted left by 64. + * The offset value is calculated on restore after migration and + * ensures that Windows will not see a large jump in ReferenceTime. */ p->TscScale =3D ((10000ul << 32) / d->arch.tsc_khz) << 32; + p->TscOffset =3D trc->off; smp_wmb(); =20 seq =3D p->TscSequence + 1; @@ -84,46 +88,6 @@ static void update_reference_tsc(struct domain *d, bool = initialize) seq =3D 1; =20 p->TscSequence =3D seq; - vd->reference_tsc_valid =3D true; -} - -static int64_t raw_trc_val(const struct domain *d) -{ - uint64_t tsc; - struct time_scale tsc_to_ns; - - tsc =3D hvm_get_guest_tsc(pt_global_vcpu_target(d)); - - /* convert tsc to count of 100ns periods */ - set_time_scale(&tsc_to_ns, d->arch.tsc_khz * 1000ul); - return scale_delta(tsc, &tsc_to_ns) / 100ul; -} - -static void time_ref_count_freeze(const struct domain *d) -{ - struct viridian_time_ref_count *trc =3D - &d->arch.hvm.viridian->time_ref_count; - - if ( test_and_clear_bit(_TRC_running, &trc->flags) ) - trc->val =3D raw_trc_val(d) + trc->off; -} - -static void time_ref_count_thaw(const struct domain *d) -{ - struct viridian_time_ref_count *trc =3D - &d->arch.hvm.viridian->time_ref_count; - - if ( !d->is_shutting_down && - !test_and_set_bit(_TRC_running, &trc->flags) ) - trc->off =3D (int64_t)trc->val - raw_trc_val(d); -} - -static int64_t time_ref_count(const struct domain *d) -{ - struct viridian_time_ref_count *trc =3D - &d->arch.hvm.viridian->time_ref_count; - - return raw_trc_val(d) + trc->off; } =20 /* @@ -136,7 +100,7 @@ static int64_t time_ref_count(const struct domain *d) * 128 bit number which is then shifted 64 times to the right to obtain * the high 64 bits." */ -static uint64_t scale_tsc(uint64_t tsc, uint64_t scale, uint64_t offset) +static uint64_t scale_tsc(uint64_t tsc, uint64_t scale, int64_t offset) { uint64_t result; =20 @@ -153,22 +117,46 @@ static uint64_t scale_tsc(uint64_t tsc, uint64_t scal= e, uint64_t offset) return result + offset; } =20 -static uint64_t time_now(struct domain *d) +static uint64_t trc_val(const struct domain *d, int64_t offset) { uint64_t tsc, scale; =20 - /* - * If the reference TSC page is not enabled, or has been invalidated - * fall back to the partition reference counter. - */ - if ( !d->arch.hvm.viridian->reference_tsc_valid ) - return time_ref_count(d); - - /* Otherwise compute reference time in the same way the guest would */ tsc =3D hvm_get_guest_tsc(pt_global_vcpu_target(d)); scale =3D ((10000ul << 32) / d->arch.tsc_khz) << 32; =20 - return scale_tsc(tsc, scale, 0); + return scale_tsc(tsc, scale, offset); +} + +static void time_ref_count_freeze(const struct domain *d) +{ + struct viridian_time_ref_count *trc =3D + &d->arch.hvm.viridian->time_ref_count; + + if ( test_and_clear_bit(_TRC_running, &trc->flags) ) + trc->val =3D trc_val(d, trc->off); +} + +static void time_ref_count_thaw(const struct domain *d) +{ + struct viridian_domain *vd =3D d->arch.hvm.viridian; + struct viridian_time_ref_count *trc =3D &vd->time_ref_count; + + if ( d->is_shutting_down || + test_and_set_bit(_TRC_running, &trc->flags) ) + return; + + trc->off =3D (int64_t)trc->val - trc_val(d, 0); + + if ( vd->reference_tsc.msr.enabled ) + update_reference_tsc(d, false); +} + +static int64_t time_ref_count(const struct domain *d) +{ + const struct viridian_time_ref_count *trc =3D + &d->arch.hvm.viridian->time_ref_count; + + return trc_val(d, trc->off); } =20 static void stop_stimer(struct viridian_stimer *vs) @@ -196,7 +184,7 @@ static void start_stimer(struct viridian_stimer *vs) const struct vcpu *v =3D vs->v; struct viridian_vcpu *vv =3D v->arch.hvm.viridian; unsigned int stimerx =3D vs - &vv->stimer[0]; - int64_t now =3D time_now(v->domain); + int64_t now =3D time_ref_count(v->domain); int64_t expiration; s_time_t timeout; =20 @@ -285,7 +273,7 @@ static void poll_stimer(struct vcpu *v, unsigned int st= imerx) =20 if ( !viridian_synic_deliver_timer_msg(v, vs->config.sintx, stimerx, vs->expiration, - time_now(v->domain)) ) + time_ref_count(v->domain)) ) return; =20 clear_bit(stimerx, &vv->stimer_pending); @@ -641,10 +629,7 @@ void viridian_time_load_domain_ctxt( vd->reference_tsc.msr.raw =3D ctxt->reference_tsc; =20 if ( vd->reference_tsc.msr.enabled ) - { viridian_map_guest_page(d, &vd->reference_tsc); - update_reference_tsc(d, false); - } } =20 /* diff --git a/xen/include/asm-x86/hvm/viridian.h b/xen/include/asm-x86/hvm/v= iridian.h index 54e46cc4c4..010c8b58d4 100644 --- a/xen/include/asm-x86/hvm/viridian.h +++ b/xen/include/asm-x86/hvm/viridian.h @@ -116,7 +116,6 @@ struct viridian_domain union viridian_page_msr hypercall_gpa; struct viridian_time_ref_count time_ref_count; struct viridian_page reference_tsc; - bool reference_tsc_valid; }; =20 void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf, --=20 2.20.1.2.gb21ebb671 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel