Skip to contentSkip to conversation

x86/viridian: Add Partition Reference Time enlightenment

PATCH Superseded Paul Durrant 2 participants 4 messages Sep 25, 2014 – Sep 26, 2014

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/viridian: Add Partition Reference Time enlightenment

The presence of the partition reference time enlightenment persuades newer versions of Windows to prefer the TSC as their primary time source. Hence, if rdtsc is not being emulated and is invariant then many vmexits (for alternative time sources such as the HPET or reference counter MSR) can be avoided.

If the VM is migrated to a host where TSC is no longer invariant and/or TSC is emulated then the enlightenment is disabled.

See section 15.4 of the Microsoft Hypervisor Top Level Functional Specification v4.0a for details.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Christoph Egger <[email protected]>
---
 docs/man/xl.cfg.pod.5                  |    9 ++-
 tools/libxl/libxl_dom.c                |    4 +
 tools/libxl/libxl_types.idl            |    1 +
 xen/arch/x86/hvm/viridian.c            |  130 +++++++++++++++++++++++++++++++-
 xen/include/asm-x86/hvm/viridian.h     |   35 +++++++++
 xen/include/public/arch-x86/hvm/save.h |   11 +++
 xen/include/public/hvm/params.h        |    7 +-
 7 files changed, 191 insertions(+), 6 deletions(-)
docs/man/xl.cfg.pod.5 +8 -1
index 8bba21c..c5251fe 100644
@@ -1163,10 +1163,17 @@ This group incorporates Partition Time Reference Counter MSR. This
 enlightenment can improve performance of Windows 8 and Windows
 Server 2012 onwards.
 
+=item B<reference_tsc>
+
+This set incorporates the Partition Reference TSC MSR. This
+enlightenment can improve performance of Windows 7 and Windows
+Server 2008 R2 onwards.
+
 =item B<defaults>
 
 This is a special value that enables the default set of groups, which
-is currently the B<base>, B<freq> and B<time_ref_count> groups.
+is currently the B<base>, B<freq>, B<time_ref_count>, and
+B<reference_tsc> groups.
 
 =item B<all>
 
tools/libxl/libxl_dom.c +4 -0
index bd21841..d0298be 100644
@@ -226,6 +226,7 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,
         libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_BASE);
         libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_FREQ);
         libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_TIME_REF_COUNT);
+        libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_REFERENCE_TSC);
     }
 
     libxl_for_each_set_bit(v, info->u.hvm.viridian_enable) {
@@ -262,6 +263,9 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,
     if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_TIME_REF_COUNT))
         mask |= HVMPV_time_ref_count;
 
+    if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_REFERENCE_TSC))
+        mask |= HVMPV_reference_tsc;
+
     if (mask != 0 &&
         xc_hvm_param_set(CTX->xch,
                          domid,
tools/libxl/libxl_types.idl +1 -0
index 90d152f..da75a40 100644
@@ -183,6 +183,7 @@ libxl_viridian_enlightenment = Enumeration("viridian_enlightenment", [
     (0, "base"),
     (1, "freq"),
     (2, "time_ref_count"),
+    (3, "reference_tsc"),
     ])
 
 #
xen/arch/x86/hvm/viridian.c +126 -4

188 lines not on this page · raw message

xen/include/asm-x86/hvm/viridian.h +35 -0
index 496da33..35a22f5 100644
@@ -48,10 +48,35 @@ union viridian_hypercall_gpa
     } fields;
 };
 
+union viridian_reference_tsc
+{
+    uint64_t raw;
+    struct
+    {
+        uint64_t enabled:1;
+        uint64_t reserved_preserved:11;
+        uint64_t pfn:48;
+    } fields;
+};
+
+/*
+ * Type defintion as in Microsoft Hypervisor Top-Level Functional
+ * Specification v4.0a, section 15.4.2.
+ */
+typedef struct _HV_REFERENCE_TSC_PAGE
+{
+    uint32_t TscSequence;
+    uint32_t Reserved1;
+    uint64_t TscScale;
+    int64_t  TscOffset;
+    uint64_t Reserved2[509];
+} HV_REFERENCE_TSC_PAGE, *PHV_REFERENCE_TSC_PAGE;
+
 struct viridian_domain
 {
     union viridian_guest_os_id guest_os_id;
     union viridian_hypercall_gpa hypercall_gpa;
+    union viridian_reference_tsc reference_tsc;
 };
 
 int
@@ -76,3 +101,13 @@ int
 viridian_hypercall(struct cpu_user_regs *regs);
 
 #endif /* __ASM_X86_HVM_VIRIDIAN_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/public/arch-x86/hvm/save.h +11 -0
index 16d85a3..006924b 100644
@@ -568,6 +568,7 @@ struct hvm_hw_cpu_xsave {
 struct hvm_viridian_domain_context {
     uint64_t hypercall_gpa;
     uint64_t guest_os_id;
+    uint64_t reference_tsc;
 };
 
 DECLARE_HVM_SAVE_TYPE(VIRIDIAN_DOMAIN, 15, struct hvm_viridian_domain_context);
@@ -616,3 +617,13 @@ struct hvm_msr {
 #define HVM_SAVE_CODE_MAX 20
 
 #endif /* __XEN_PUBLIC_HVM_SAVE_X86_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/public/hvm/params.h +6 -1
index 3c51072..a2d43bc 100644
@@ -92,10 +92,15 @@
 #define _HVMPV_time_ref_count 2
 #define HVMPV_time_ref_count  (1 << _HVMPV_time_ref_count)
 
+/* Enable Reference TSC Page (HV_X64_MSR_REFERENCE_TSC) */
+#define _HVMPV_reference_tsc 3
+#define HVMPV_reference_tsc  (1 << _HVMPV_reference_tsc)
+
 #define HVMPV_feature_mask \
 	(HVMPV_base_freq | \
 	 HVMPV_no_freq | \
-	 HVMPV_time_ref_count)
+	 HVMPV_time_ref_count | \
+	 HVMPV_reference_tsc)
 
 #endif
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 25.09.14 at 17:50, <[email protected]> wrote:

The presence of the partition reference time enlightenment persuades newer versions of Windows to prefer the TSC as their primary time source. Hence, if rdtsc is not being emulated and is invariant then many vmexits (for alternative time sources such as the HPET or reference counter MSR) can be avoided.

If the VM is migrated to a host where TSC is no longer invariant and/or TSC is emulated then the enlightenment is disabled.

Which means performance of a guest post migration can change dramatically. Is that really a good thing?

xen/arch/x86/hvm/viridian.c static void initialize_apic_assist(struct vcpu *v) · line 280

Quoted 4 lines
xen/arch/x86/hvm/viridian.c +4 -0
+    do
+        p->TscSequence++;
+    while ( p->TscSequence == 0xFFFFFFFF ||
+            p->TscSequence == 0 ); /* Avoid both 'invalid' values */

I don't think we ever use "naked" do/while constructs - please add figure braces here.

Quoted 8 lines
xen/arch/x86/hvm/viridian.c +8 -0
+static void initialize_reference_tsc(struct domain *d)
+{
+    unsigned long gmfn = d->arch.hvm_domain.viridian.reference_tsc.fields.pfn;
+    struct page_info *page = get_page_from_gfn(d, gmfn, NULL, P2M_ALLOC);
+    HV_REFERENCE_TSC_PAGE *p;
+
+    if ( !page || !get_page_type(page, PGT_writable_page) )
+    {

xen/arch/x86/hvm/viridian.c static void initialize_apic_assist(struct vcpu *v) · line 300

Quoted 10 lines
xen/arch/x86/hvm/viridian.c +10 -0
+        if ( page )
+            put_page(page);
+        gdprintk(XENLOG_WARNING, "Bad GMFN %lx (MFN %lx)\n", gmfn,
+                 page ? page_to_mfn(page) : INVALID_MFN);
+        return;
+    }
+
+    p = __map_domain_page(page);
+
+    memset(p, 0, sizeof(*p));

clear_page()

Quoted 4 lines
xen/arch/x86/hvm/viridian.c +0 -0
@@ -341,9 +433,12 @@ int rdmsr_viridian_regs(uint32_t idx, uint64_t *val)
         *val = vlapic_get_reg(vcpu_vlapic(v), APIC_TASKPRI);
         break;
 

xen/arch/x86/hvm/viridian.c int rdmsr_viridian_regs(uint32_t idx, uint64_t *val) · line 442

Quoted 10 lines
xen/arch/x86/hvm/viridian.c +6 -3
-    case VIRIDIAN_MSR_APIC_ASSIST:
-        perfc_incr(mshv_rdmsr_apic_msr);
-        *val = v->arch.hvm_vcpu.viridian.apic_assist.raw;
+    case VIRIDIAN_MSR_REFERENCE_TSC:
+        if ( !(viridian_feature_mask(d) & HVMPV_reference_tsc) )
+            return 0;
+
+        perfc_incr(mshv_rdmsr_tsc_msr);
+        *val = d->arch.hvm_domain.viridian.reference_tsc.raw;
         break;

Did you really mean to delete the VIRIDIAN_MSR_APIC_ASSIST code?

Quoted 27 lines
xen/arch/x86/hvm/viridian.c +17 -1
@@ -460,11 +556,37 @@ static int viridian_load_domain_ctxt(struct domain *d, 
 hvm_domain_context_t *h)
 {
     struct hvm_viridian_domain_context ctxt;
 
-    if ( hvm_load_entry(VIRIDIAN_DOMAIN, h, &ctxt) != 0 )
+    if ( hvm_load_entry_zeroextend(VIRIDIAN_DOMAIN, h, &ctxt) != 0 )
         return -EINVAL;
 
     d->arch.hvm_domain.viridian.hypercall_gpa.raw = ctxt.hypercall_gpa;
     d->arch.hvm_domain.viridian.guest_os_id.raw   = ctxt.guest_os_id;
+    d->arch.hvm_domain.viridian.reference_tsc.raw = ctxt.reference_tsc;
+
+    if ( d->arch.hvm_domain.viridian.reference_tsc.fields.enabled )
+    {
+        unsigned long gmfn;
+        struct page_info *page;
+        HV_REFERENCE_TSC_PAGE *p;
+
+        gmfn = d->arch.hvm_domain.viridian.reference_tsc.fields.pfn;
+        page = get_page_from_gfn(d, gmfn, NULL, P2M_ALLOC);
+
+        if ( !page || !get_page_type(page, PGT_writable_page) )
+        {
+            if ( page )
+                put_page(page);
+            return -EINVAL;

xen/arch/x86/hvm/viridian.c static int viridian_load_domain_ctxt(struct domain *d, · line 590

Quoted 10 lines
xen/arch/x86/hvm/viridian.c +10 -0
+        }
+
+        p = __map_domain_page(page);
+
+        update_reference_tsc(d, p);
+
+        unmap_domain_page(p);
+
+        put_page_and_type(page);
+    }

Isn't this initialize_reference_tsc() without the memset()?

Jan

#2 lore raw
Quoted 17 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 25 September 2014 17:11
To: Paul Durrant
Cc: Christoph Egger; Ian Campbell; Ian Jackson; Stefano Stabellini; xen- [email protected]; Keir (Xen.org)
Subject: Re: [PATCH] x86/viridian: Add Partition Reference Time enlightenment

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 25.09.14 at 17:50, <[email protected]> wrote:

The presence of the partition reference time enlightenment persuades

newer

Quoted 5 lines

versions of Windows to prefer the TSC as their primary time source. Hence, if rdtsc is not being emulated and is invariant then many vmexits (for alternative time sources such as the HPET or reference counter MSR) can be avoided.

Quoted 5 lines
Quoted 2 lines

If the VM is migrated to a host where TSC is no longer invariant and/or TSC is emulated then the enlightenment is disabled.

Which means performance of a guest post migration can change dramatically. Is that really a good thing?

I guess that's debatable. It is possible to use this enlightenment to scale TSC, which means that TSC emulation could be avoided even if the tsc_khz changes, but I still need to figure out how best to do that part. Nothing can be done in the case of migration to a non iTSC host though. I could leave this out the default enlightenment set for now.

Quoted 8 lines

xen/arch/x86/hvm/viridian.c static void initialize_apic_assist(struct vcpu *v) · line 280

Quoted 5 lines
xen/arch/x86/hvm/viridian.c +4 -0
+    do
+        p->TscSequence++;
+    while ( p->TscSequence == 0xFFFFFFFF ||
+            p->TscSequence == 0 ); /* Avoid both 'invalid' values */

I don't think we ever use "naked" do/while constructs - please add figure braces here.

Ok.

Quoted 7 lines
Quoted 6 lines
xen/arch/x86/hvm/viridian.c +3 -0
+static void initialize_reference_tsc(struct domain *d)
+{
+    unsigned long gmfn = d-

arch.hvm_domain.viridian.reference_tsc.fields.pfn;
+ struct page_info *page = get_page_from_gfn(d, gmfn, NULL,

P2M_ALLOC);

Quoted 16 lines
Quoted 4 lines
xen/arch/x86/hvm/viridian.c +4 -0
+    HV_REFERENCE_TSC_PAGE *p;
+
+    if ( !page || !get_page_type(page, PGT_writable_page) )
+    {

xen/arch/x86/hvm/viridian.c static void initialize_apic_assist(struct vcpu *v) · line 300

Quoted 10 lines
xen/arch/x86/hvm/viridian.c +10 -0
+        if ( page )
+            put_page(page);
+        gdprintk(XENLOG_WARNING, "Bad GMFN %lx (MFN %lx)\n", gmfn,
+                 page ? page_to_mfn(page) : INVALID_MFN);
+        return;
+    }
+
+    p = __map_domain_page(page);
+
+    memset(p, 0, sizeof(*p));

clear_page()

Indeed.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/hvm/viridian.c +0 -0
@@ -341,9 +433,12 @@ int rdmsr_viridian_regs(uint32_t idx, uint64_t *val)
         *val = vlapic_get_reg(vcpu_vlapic(v), APIC_TASKPRI);
         break;
 
Quoted 13 lines

xen/arch/x86/hvm/viridian.c int rdmsr_viridian_regs(uint32_t idx, uint64_t *val) · line 442

Quoted 10 lines
xen/arch/x86/hvm/viridian.c +6 -3
-    case VIRIDIAN_MSR_APIC_ASSIST:
-        perfc_incr(mshv_rdmsr_apic_msr);
-        *val = v->arch.hvm_vcpu.viridian.apic_assist.raw;
+    case VIRIDIAN_MSR_REFERENCE_TSC:
+        if ( !(viridian_feature_mask(d) & HVMPV_reference_tsc) )
+            return 0;
+
+        perfc_incr(mshv_rdmsr_tsc_msr);
+        *val = d->arch.hvm_domain.viridian.reference_tsc.raw;
         break;

Did you really mean to delete the VIRIDIAN_MSR_APIC_ASSIST code?

Gah - don't know how that happened.

Quoted 32 lines

Review static int viridian_load_domain_ctxt(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -460,11 +556,37 @@ static int viridian_load_domain_ctxt(struct

domain *d,

Quoted 30 lines
xen/arch/x86/hvm/viridian.c +21 -1
 hvm_domain_context_t *h)
 {
     struct hvm_viridian_domain_context ctxt;
 
-    if ( hvm_load_entry(VIRIDIAN_DOMAIN, h, &ctxt) != 0 )
+    if ( hvm_load_entry_zeroextend(VIRIDIAN_DOMAIN, h, &ctxt) != 0 )
         return -EINVAL;
 
     d->arch.hvm_domain.viridian.hypercall_gpa.raw = ctxt.hypercall_gpa;
     d->arch.hvm_domain.viridian.guest_os_id.raw   = ctxt.guest_os_id;
+    d->arch.hvm_domain.viridian.reference_tsc.raw = ctxt.reference_tsc;
+
+    if ( d->arch.hvm_domain.viridian.reference_tsc.fields.enabled )
+    {
+        unsigned long gmfn;
+        struct page_info *page;
+        HV_REFERENCE_TSC_PAGE *p;
+
+        gmfn = d->arch.hvm_domain.viridian.reference_tsc.fields.pfn;
+        page = get_page_from_gfn(d, gmfn, NULL, P2M_ALLOC);
+
+        if ( !page || !get_page_type(page, PGT_writable_page) )
+        {
+            if ( page )
+                put_page(page);
+            return -EINVAL;
+        }
+
+        p = __map_domain_page(page);
+
Quoted 9 lines

xen/arch/x86/hvm/viridian.c static void initialize_apic_assist(struct vcpu *v) · line 306

Quoted 6 lines
xen/arch/x86/hvm/viridian.c +6 -0
+        update_reference_tsc(d, p);
+
+        unmap_domain_page(p);
+
+        put_page_and_type(page);
+    }

Isn't this initialize_reference_tsc() without the memset()?

Yes - I guess I should dedup.

Paul

Quoted 1 line

Jan

#3 lore raw
Quoted 21 lines
Quoted 21 lines
Quoted 1 line · On 25.09.14 at 18:18, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 25 September 2014 17:11
To: Paul Durrant
Cc: Christoph Egger; Ian Campbell; Ian Jackson; Stefano Stabellini; xen- [email protected]; Keir (Xen.org)
Subject: Re: [PATCH] x86/viridian: Add Partition Reference Time enlightenment

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 25.09.14 at 17:50, <[email protected]> wrote:

The presence of the partition reference time enlightenment persuades

newer

Quoted 7 lines

versions of Windows to prefer the TSC as their primary time source. Hence, if rdtsc is not being emulated and is invariant then many vmexits (for alternative time sources such as the HPET or reference counter MSR) can be avoided.

If the VM is migrated to a host where TSC is no longer invariant and/or TSC is emulated then the enlightenment is disabled.

Quoted 8 lines
Quoted 2 lines

Which means performance of a guest post migration can change dramatically. Is that really a good thing?

I guess that's debatable. It is possible to use this enlightenment to scale TSC, which means that TSC emulation could be avoided even if the tsc_khz changes, but I still need to figure out how best to do that part. Nothing can be done in the case of migration to a non iTSC host though.
I could leave this out the default enlightenment set for now.

Yes please, perhaps with a brief code comment as to why.

Jan