Skip to contentSkip to conversation

viridian: implement more enlightenments

PATCH Superseded Paul Durrant 2 participants 32 messages Mar 11, 2019 – Mar 14, 2019

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover viridian: implement more enlightenments

This series adds three new enlightenments:

- Synthetic timers, which depends on the...
- Synthetic interrupt controller (or SynIC)
- Synthetic cluster IPI

All these enlightenments are implemented in current versions of QEMU/KVM so this series closes the gap.

Paul Durrant (11):
viridian: add init hooks
viridian: separately allocate domain and vcpu structures
viridian: use stack variables for viridian_vcpu and viridian_domain...
viridian: make 'fields' struct anonymous...
viridian: extend init/deinit hooks into synic and time modules
viridian: add missing context save helpers into synic and time modules
viridian: use viridian_map/unmap_guest_page() for reference tsc page
viridian: stop directly calling
viridian_time_ref_count_freeze/thaw()...
viridian: add implementation of synthetic interrupt MSRs
viridian: add implementation of synthetic timers
viridian: add implementation of the HvSendSyntheticClusterIpi
hypercall

 docs/man/xl.cfg.5.pod.in               |  18 +-
 tools/libxl/libxl.h                    |  18 +
 tools/libxl/libxl_dom.c                |  10 +
 tools/libxl/libxl_types.idl            |   3 +
 xen/arch/x86/domain.c                  |  12 +-
 xen/arch/x86/hvm/hvm.c                 |  10 +
 xen/arch/x86/hvm/viridian/private.h    |  31 +-
 xen/arch/x86/hvm/viridian/synic.c      | 367 +++++++++++++++--
 xen/arch/x86/hvm/viridian/time.c       | 519 ++++++++++++++++++++++---
 xen/arch/x86/hvm/viridian/viridian.c   | 243 ++++++++++--
 xen/arch/x86/hvm/vlapic.c              |  32 +-
 xen/include/asm-x86/hvm/domain.h       |   2 +-
 xen/include/asm-x86/hvm/hvm.h          |   7 +
 xen/include/asm-x86/hvm/vcpu.h         |   2 +-
 xen/include/asm-x86/hvm/viridian.h     |  74 +++-
 xen/include/asm-x86/hvm/vlapic.h       |   1 +
 xen/include/public/arch-x86/hvm/save.h |   4 +
 xen/include/public/hvm/params.h        |  17 +-
 18 files changed, 1231 insertions(+), 139 deletions(-)

v5:
- Fix stuck domains (in patch #1) and unscaled TSC (in patch #10)

v4:
- Add two cleanup patches (#3 and #4) and re-order #8 and #9

v3:
- Add the synthetic cluster IPI patch (#11)

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v5 1/11 viridian: add init hooks

This patch adds domain and vcpu init hooks for viridian features. The init hooks do not yet do anything; the functionality will be added to by subsequent patches.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v5:
- Put the call to viridian_domain_deinit() back into hvm_domain_relinquish_resources() where it should be

v3:
- Re-instate call from domain deinit to vcpu deinit
- Move deinit calls to avoid introducing new labels

v2:
- Remove call from domain deinit to vcpu deinit

---
 xen/arch/x86/hvm/hvm.c               | 10 ++++++++++
 xen/arch/x86/hvm/viridian/viridian.c | 10 ++++++++++
 xen/include/asm-x86/hvm/viridian.h   |  3 +++
 3 files changed, 23 insertions(+)
xen/arch/x86/hvm/hvm.c +10 -0
index 8adbb61b57..11ce21fc08 100644
@@ -666,6 +666,10 @@ int hvm_domain_initialise(struct domain *d)
     if ( hvm_tsc_scaling_supported )
         d->arch.hvm.tsc_scaling_ratio = hvm_default_tsc_scaling_ratio;
 
+    rc = viridian_domain_init(d);
+    if ( rc )
+        goto fail2;
+
     rc = hvm_funcs.domain_initialise(d);
     if ( rc != 0 )
         goto fail2;
@@ -687,6 +691,7 @@ int hvm_domain_initialise(struct domain *d)
     hvm_destroy_cacheattr_region_list(d);
     destroy_perdomain_mapping(d, PERDOMAIN_VIRT_START, 0);
  fail:
+    viridian_domain_deinit(d);
     return rc;
 }
 
@@ -1526,6 +1531,10 @@ int hvm_vcpu_initialise(struct vcpu *v)
          && (rc = nestedhvm_vcpu_initialise(v)) < 0 ) /* teardown: nestedhvm_vcpu_destroy */
         goto fail5;
 
+    rc = viridian_vcpu_init(v);
+    if ( rc )
+        goto fail5;
+
     rc = hvm_all_ioreq_servers_add_vcpu(d, v);
     if ( rc != 0 )
         goto fail6;
@@ -1553,6 +1562,7 @@ int hvm_vcpu_initialise(struct vcpu *v)
  fail2:
     hvm_vcpu_cacheattr_destroy(v);
  fail1:
+    viridian_vcpu_deinit(v);
     return rc;
 }
 
xen/arch/x86/hvm/viridian/viridian.c +10 -0
index 425af56856..5b0eb8a8c7 100644
@@ -417,6 +417,16 @@ int guest_rdmsr_viridian(const struct vcpu *v, uint32_t idx, uint64_t *val)
     return X86EMUL_OKAY;
 }
 
+int viridian_vcpu_init(struct vcpu *v)
+{
+    return 0;
+}
+
+int viridian_domain_init(struct domain *d)
+{
+    return 0;
+}
+
 void viridian_vcpu_deinit(struct vcpu *v)
 {
     viridian_synic_wrmsr(v, HV_X64_MSR_VP_ASSIST_PAGE, 0);
xen/include/asm-x86/hvm/viridian.h +3 -0
index ec5ef8d3f9..f072838955 100644
@@ -80,6 +80,9 @@ viridian_hypercall(struct cpu_user_regs *regs);
 void viridian_time_ref_count_freeze(struct domain *d);
 void viridian_time_ref_count_thaw(struct domain *d);
 
+int viridian_vcpu_init(struct vcpu *v);
+int viridian_domain_init(struct domain *d);
+
 void viridian_vcpu_deinit(struct vcpu *v);
 void viridian_domain_deinit(struct domain *d);
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v5 1/11
#2 lore raw

Re: [PATCH v5 01/11] viridian: add init hooks

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:

This patch adds domain and vcpu init hooks for viridian features. The init hooks do not yet do anything; the functionality will be added to by subsequent patches.

Non-Viridian parts

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

PATCH v5 2/11 viridian: separately allocate domain and vcpu structures

In reply to Paul Durrant

Currently the viridian_domain and viridian_vcpu structures are inline in the hvm_domain and hvm_vcpu structures respectively. Subsequent patches will need to add sizable extra fields to the viridian structures which will cause the PAGE_SIZE limit of the overall vcpu structure to be exceeded. This patch, therefore, uses the new init hooks to separately allocate the structures and converts the 'viridian' fields in hvm_domain and hvm_cpu to be pointers to these allocations. These separate allocations also allow some vcpu and domain pointers to become const.

Ideally, now that they are no longer inline, the allocations of the viridian structures could be made conditional on whether the toolstack is going to configure the viridian enlightenments. However the toolstack is currently unable to convey this information to the domain creation code so such an enhancement is deferred until that becomes possible.

NOTE: The patch also introduced the 'is_viridian_vcpu' macro to avoid
introducing a second evaluation of 'is_viridian_domain' with an open-coded 'v->domain' argument. This macro will also be further used in a subsequent patch.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v4:
- Const-ify some vcpu and domain pointers

v2:
- use XFREE()
- expand commit comment to point out why allocations are unconditional

---
 xen/arch/x86/hvm/viridian/private.h  |  2 +-
 xen/arch/x86/hvm/viridian/synic.c    | 46 ++++++++---------
 xen/arch/x86/hvm/viridian/time.c     | 38 +++++++-------
 xen/arch/x86/hvm/viridian/viridian.c | 75 ++++++++++++++++++----------
 xen/include/asm-x86/hvm/domain.h     |  2 +-
 xen/include/asm-x86/hvm/hvm.h        |  4 ++
 xen/include/asm-x86/hvm/vcpu.h       |  2 +-
 xen/include/asm-x86/hvm/viridian.h   | 10 ++--
 8 files changed, 101 insertions(+), 78 deletions(-)
xen/arch/x86/hvm/viridian/private.h +1 -1
index 398b22f12d..46174f48cd 100644
@@ -89,7 +89,7 @@ void viridian_time_load_domain_ctxt(
 
 void viridian_dump_guest_page(const struct vcpu *v, const char *name,
                               const struct viridian_page *vp);
-void viridian_map_guest_page(struct vcpu *v, struct viridian_page *vp);
+void viridian_map_guest_page(const struct vcpu *v, struct viridian_page *vp);
 void viridian_unmap_guest_page(struct viridian_page *vp);
 
 #endif /* X86_HVM_VIRIDIAN_PRIVATE_H */
xen/arch/x86/hvm/viridian/synic.c +23 -23

109 lines not on this page · raw message

xen/arch/x86/hvm/viridian/time.c +18 -20
index 840a82b457..a7e94aadf0 100644
@@ -27,7 +27,7 @@ typedef struct _HV_REFERENCE_TSC_PAGE
 
 static void dump_reference_tsc(const struct domain *d)
 {
-    const union viridian_page_msr *rt = &d->arch.hvm.viridian.reference_tsc;
+    const union viridian_page_msr *rt = &d->arch.hvm.viridian->reference_tsc;
 
     if ( !rt->fields.enabled )
         return;
@@ -38,7 +38,7 @@ static void dump_reference_tsc(const struct domain *d)
 
 static void update_reference_tsc(struct domain *d, bool initialize)
 {
-    unsigned long gmfn = d->arch.hvm.viridian.reference_tsc.fields.pfn;
+    unsigned long gmfn = d->arch.hvm.viridian->reference_tsc.fields.pfn;
     struct page_info *page = get_page_from_gfn(d, gmfn, NULL, P2M_ALLOC);
     HV_REFERENCE_TSC_PAGE *p;
 
@@ -107,7 +107,7 @@ static void update_reference_tsc(struct domain *d, bool initialize)
     put_page_and_type(page);
 }
 
-static int64_t raw_trc_val(struct domain *d)
+static int64_t raw_trc_val(const struct domain *d)
 {
     uint64_t tsc;
     struct time_scale tsc_to_ns;
@@ -119,21 +119,19 @@ static int64_t raw_trc_val(struct domain *d)
     return scale_delta(tsc, &tsc_to_ns) / 100ul;
 }
 
-void viridian_time_ref_count_freeze(struct domain *d)
+void viridian_time_ref_count_freeze(const struct domain *d)
 {
-    struct viridian_time_ref_count *trc;
-
-    trc = &d->arch.hvm.viridian.time_ref_count;
+    struct viridian_time_ref_count *trc =
+        &d->arch.hvm.viridian->time_ref_count;
 
     if ( test_and_clear_bit(_TRC_running, &trc->flags) )
         trc->val = raw_trc_val(d) + trc->off;
 }
 
-void viridian_time_ref_count_thaw(struct domain *d)
+void viridian_time_ref_count_thaw(const struct domain *d)
 {
-    struct viridian_time_ref_count *trc;
-
-    trc = &d->arch.hvm.viridian.time_ref_count;
+    struct viridian_time_ref_count *trc =
+        &d->arch.hvm.viridian->time_ref_count;
 
     if ( !d->is_shutting_down &&
          !test_and_set_bit(_TRC_running, &trc->flags) )
@@ -150,9 +148,9 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
         if ( !(viridian_feature_mask(d) & HVMPV_reference_tsc) )
             return X86EMUL_EXCEPTION;
 
-        d->arch.hvm.viridian.reference_tsc.raw = val;
+        d->arch.hvm.viridian->reference_tsc.raw = val;
         dump_reference_tsc(d);
-        if ( d->arch.hvm.viridian.reference_tsc.fields.enabled )
+        if ( d->arch.hvm.viridian->reference_tsc.fields.enabled )
             update_reference_tsc(d, true);
         break;
 
@@ -189,13 +187,13 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
         if ( !(viridian_feature_mask(d) & HVMPV_reference_tsc) )
             return X86EMUL_EXCEPTION;
 
-        *val = d->arch.hvm.viridian.reference_tsc.raw;
+        *val = d->arch.hvm.viridian->reference_tsc.raw;
         break;
 
     case HV_X64_MSR_TIME_REF_COUNT:
     {
         struct viridian_time_ref_count *trc =
-            &d->arch.hvm.viridian.time_ref_count;
+            &d->arch.hvm.viridian->time_ref_count;
 
         if ( !(viridian_feature_mask(d) & HVMPV_time_ref_count) )
             return X86EMUL_EXCEPTION;
@@ -219,17 +217,17 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
 void viridian_time_save_domain_ctxt(
     const struct domain *d, struct hvm_viridian_domain_context *ctxt)
 {
-    ctxt->time_ref_count = d->arch.hvm.viridian.time_ref_count.val;
-    ctxt->reference_tsc = d->arch.hvm.viridian.reference_tsc.raw;
+    ctxt->time_ref_count = d->arch.hvm.viridian->time_ref_count.val;
+    ctxt->reference_tsc = d->arch.hvm.viridian->reference_tsc.raw;
 }
 
 void viridian_time_load_domain_ctxt(
     struct domain *d, const struct hvm_viridian_domain_context *ctxt)
 {
-    d->arch.hvm.viridian.time_ref_count.val = ctxt->time_ref_count;
-    d->arch.hvm.viridian.reference_tsc.raw = ctxt->reference_tsc;
+    d->arch.hvm.viridian->time_ref_count.val = ctxt->time_ref_count;
+    d->arch.hvm.viridian->reference_tsc.raw = ctxt->reference_tsc;
 
-    if ( d->arch.hvm.viridian.reference_tsc.fields.enabled )
+    if ( d->arch.hvm.viridian->reference_tsc.fields.enabled )
         update_reference_tsc(d, false);
 }
 
xen/arch/x86/hvm/viridian/viridian.c +48 -27

193 lines not on this page · raw message

xen/include/asm-x86/hvm/domain.h +1 -1
index 3e7331817f..6c7c4f5aa6 100644
@@ -154,7 +154,7 @@ struct hvm_domain {
     /* hypervisor intercepted msix table */
     struct list_head       msixtbl_list;
 
-    struct viridian_domain viridian;
+    struct viridian_domain *viridian;
 
     bool_t                 hap_enabled;
     bool_t                 mem_sharing_enabled;
xen/include/asm-x86/hvm/hvm.h +4 -0
index 53ffebb2c5..37c3567a57 100644
@@ -463,6 +463,9 @@ static inline bool hvm_get_guest_bndcfgs(struct vcpu *v, u64 *val)
 #define is_viridian_domain(d) \
     (is_hvm_domain(d) && (viridian_feature_mask(d) & HVMPV_base_freq))
 
+#define is_viridian_vcpu(v) \
+    is_viridian_domain((v)->domain)
+
 #define has_viridian_time_ref_count(d) \
     (is_viridian_domain(d) && (viridian_feature_mask(d) & HVMPV_time_ref_count))
 
@@ -762,6 +765,7 @@ static inline bool hvm_has_set_descriptor_access_exiting(void)
 }
 
 #define is_viridian_domain(d) ((void)(d), false)
+#define is_viridian_vcpu(v) ((void)(v), false)
 #define has_viridian_time_ref_count(d) ((void)(d), false)
 #define hvm_long_mode_active(v) ((void)(v), false)
 #define hvm_get_guest_time(v) ((void)(v), 0)
xen/include/asm-x86/hvm/vcpu.h +1 -1
index 6c84d5a5a6..d1589f3a96 100644
@@ -205,7 +205,7 @@ struct hvm_vcpu {
     /* Pending hw/sw interrupt (.vector = -1 means nothing pending). */
     struct x86_event     inject_event;
 
-    struct viridian_vcpu viridian;
+    struct viridian_vcpu *viridian;
 };
 
 #endif /* __ASM_X86_HVM_VCPU_H__ */
xen/include/asm-x86/hvm/viridian.h +5 -5
index f072838955..c562424332 100644
@@ -77,8 +77,8 @@ int guest_rdmsr_viridian(const struct vcpu *v, uint32_t idx, uint64_t *val);
 int
 viridian_hypercall(struct cpu_user_regs *regs);
 
-void viridian_time_ref_count_freeze(struct domain *d);
-void viridian_time_ref_count_thaw(struct domain *d);
+void viridian_time_ref_count_freeze(const struct domain *d);
+void viridian_time_ref_count_thaw(const struct domain *d);
 
 int viridian_vcpu_init(struct vcpu *v);
 int viridian_domain_init(struct domain *d);
@@ -86,9 +86,9 @@ int viridian_domain_init(struct domain *d);
 void viridian_vcpu_deinit(struct vcpu *v);
 void viridian_domain_deinit(struct domain *d);
 
-void viridian_apic_assist_set(struct vcpu *v);
-bool viridian_apic_assist_completed(struct vcpu *v);
-void viridian_apic_assist_clear(struct vcpu *v);
+void viridian_apic_assist_set(const struct vcpu *v);
+bool viridian_apic_assist_completed(const struct vcpu *v);
+void viridian_apic_assist_clear(const struct vcpu *v);
 
 #endif /* __ASM_X86_HVM_VIRIDIAN_H__ */
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v5 2/11
#4 lore raw

Re: [PATCH v5 02/11] viridian: separately allocate domain and vcpu structures

Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:

Currently the viridian_domain and viridian_vcpu structures are inline in the hvm_domain and hvm_vcpu structures respectively. Subsequent patches will need to add sizable extra fields to the viridian structures which will cause the PAGE_SIZE limit of the overall vcpu structure to be exceeded. This patch, therefore, uses the new init hooks to separately allocate the structures and converts the 'viridian' fields in hvm_domain and hvm_cpu to be pointers to these allocations. These separate allocations also allow some vcpu and domain pointers to become const.

Ideally, now that they are no longer inline, the allocations of the viridian structures could be made conditional on whether the toolstack is going to configure the viridian enlightenments. However the toolstack is currently unable to convey this information to the domain creation code so such an enhancement is deferred until that becomes possible.

Quoted 7 lines

NOTE: The patch also introduced the 'is_viridian_vcpu' macro to avoid
introducing a second evaluation of 'is_viridian_domain' with an open-coded 'v->domain' argument. This macro will also be further used in a subsequent patch.

Non-Viridian parts

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

PATCH v5 3/11 viridian: use stack variables for viridian_vcpu and viridian_domain...

In reply to Paul Durrant

...where there is more than one dereference inside a function.

This shortens the code and makes it more readable. No functional change.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v4:
- New in v4

---
 xen/arch/x86/hvm/viridian/synic.c    | 49 ++++++++++++++++------------
 xen/arch/x86/hvm/viridian/time.c     | 27 ++++++++-------
 xen/arch/x86/hvm/viridian/viridian.c | 47 +++++++++++++-------------
 3 files changed, 69 insertions(+), 54 deletions(-)
xen/arch/x86/hvm/viridian/synic.c +28 -21
index 28eda7798c..f3d9f7ae74 100644
@@ -30,7 +30,8 @@ typedef union _HV_VP_ASSIST_PAGE
 
 void viridian_apic_assist_set(const struct vcpu *v)
 {
-    HV_VP_ASSIST_PAGE *ptr = v->arch.hvm.viridian->vp_assist.ptr;
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    HV_VP_ASSIST_PAGE *ptr = vv->vp_assist.ptr;
 
     if ( !ptr )
         return;
@@ -40,25 +41,25 @@ void viridian_apic_assist_set(const struct vcpu *v)
      * wrong and the VM will most likely hang so force a crash now
      * to make the problem clear.
      */
-    if ( v->arch.hvm.viridian->apic_assist_pending )
+    if ( vv->apic_assist_pending )
         domain_crash(v->domain);
 
-    v->arch.hvm.viridian->apic_assist_pending = true;
+    vv->apic_assist_pending = true;
     ptr->ApicAssist.no_eoi = 1;
 }
 
 bool viridian_apic_assist_completed(const struct vcpu *v)
 {
-    HV_VP_ASSIST_PAGE *ptr = v->arch.hvm.viridian->vp_assist.ptr;
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    HV_VP_ASSIST_PAGE *ptr = vv->vp_assist.ptr;
 
     if ( !ptr )
         return false;
 
-    if ( v->arch.hvm.viridian->apic_assist_pending &&
-         !ptr->ApicAssist.no_eoi )
+    if ( vv->apic_assist_pending && !ptr->ApicAssist.no_eoi )
     {
         /* An EOI has been avoided */
-        v->arch.hvm.viridian->apic_assist_pending = false;
+        vv->apic_assist_pending = false;
         return true;
     }
 
@@ -67,17 +68,20 @@ bool viridian_apic_assist_completed(const struct vcpu *v)
 
 void viridian_apic_assist_clear(const struct vcpu *v)
 {
-    HV_VP_ASSIST_PAGE *ptr = v->arch.hvm.viridian->vp_assist.ptr;
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    HV_VP_ASSIST_PAGE *ptr = vv->vp_assist.ptr;
 
     if ( !ptr )
         return;
 
     ptr->ApicAssist.no_eoi = 0;
-    v->arch.hvm.viridian->apic_assist_pending = false;
+    vv->apic_assist_pending = false;
 }
 
 int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+
     switch ( idx )
     {
     case HV_X64_MSR_EOI:
@@ -95,12 +99,11 @@ int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 
     case HV_X64_MSR_VP_ASSIST_PAGE:
         /* release any previous mapping */
-        viridian_unmap_guest_page(&v->arch.hvm.viridian->vp_assist);
-        v->arch.hvm.viridian->vp_assist.msr.raw = val;
-        viridian_dump_guest_page(v, "VP_ASSIST",
-                                 &v->arch.hvm.viridian->vp_assist);
-        if ( v->arch.hvm.viridian->vp_assist.msr.fields.enabled )
-            viridian_map_guest_page(v, &v->arch.hvm.viridian->vp_assist);
+        viridian_unmap_guest_page(&vv->vp_assist);
+        vv->vp_assist.msr.raw = val;
+        viridian_dump_guest_page(v, "VP_ASSIST", &vv->vp_assist);
+        if ( vv->vp_assist.msr.fields.enabled )
+            viridian_map_guest_page(v, &vv->vp_assist);
         break;
 
     default:
@@ -146,18 +149,22 @@ int viridian_synic_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
 void viridian_synic_save_vcpu_ctxt(const struct vcpu *v,
                                    struct hvm_viridian_vcpu_context *ctxt)
 {
-    ctxt->apic_assist_pending = v->arch.hvm.viridian->apic_assist_pending;
-    ctxt->vp_assist_msr = v->arch.hvm.viridian->vp_assist.msr.raw;
+    const struct viridian_vcpu *vv = v->arch.hvm.viridian;
+
+    ctxt->apic_assist_pending = vv->apic_assist_pending;
+    ctxt->vp_assist_msr = vv->vp_assist.msr.raw;
 }
 
 void viridian_synic_load_vcpu_ctxt(
     struct vcpu *v, const struct hvm_viridian_vcpu_context *ctxt)
 {
-    v->arch.hvm.viridian->vp_assist.msr.raw = ctxt->vp_assist_msr;
-    if ( v->arch.hvm.viridian->vp_assist.msr.fields.enabled )
-        viridian_map_guest_page(v, &v->arch.hvm.viridian->vp_assist);
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+
+    vv->vp_assist.msr.raw = ctxt->vp_assist_msr;
+    if ( vv->vp_assist.msr.fields.enabled )
+        viridian_map_guest_page(v, &vv->vp_assist);
 
-    v->arch.hvm.viridian->apic_assist_pending = ctxt->apic_assist_pending;
+    vv->apic_assist_pending = ctxt->apic_assist_pending;
 }
 
 /*
xen/arch/x86/hvm/viridian/time.c +16 -11
index a7e94aadf0..76f9612001 100644
@@ -141,6 +141,7 @@ void viridian_time_ref_count_thaw(const struct domain *d)
 int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 {
     struct domain *d = v->domain;
+    struct viridian_domain *vd = d->arch.hvm.viridian;
 
     switch ( idx )
     {
@@ -148,9 +149,9 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
         if ( !(viridian_feature_mask(d) & HVMPV_reference_tsc) )
             return X86EMUL_EXCEPTION;
 
-        d->arch.hvm.viridian->reference_tsc.raw = val;
+        vd->reference_tsc.raw = val;
         dump_reference_tsc(d);
-        if ( d->arch.hvm.viridian->reference_tsc.fields.enabled )
+        if ( vd->reference_tsc.fields.enabled )
             update_reference_tsc(d, true);
         break;
 
@@ -165,7 +166,8 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
 {
-    struct domain *d = v->domain;
+    const struct domain *d = v->domain;
+    struct viridian_domain *vd = d->arch.hvm.viridian;
 
     switch ( idx )
     {
@@ -187,13 +189,12 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
         if ( !(viridian_feature_mask(d) & HVMPV_reference_tsc) )
             return X86EMUL_EXCEPTION;
 
-        *val = d->arch.hvm.viridian->reference_tsc.raw;
+        *val = vd->reference_tsc.raw;
         break;
 
     case HV_X64_MSR_TIME_REF_COUNT:
     {
-        struct viridian_time_ref_count *trc =
-            &d->arch.hvm.viridian->time_ref_count;
+        struct viridian_time_ref_count *trc = &vd->time_ref_count;
 
         if ( !(viridian_feature_mask(d) & HVMPV_time_ref_count) )
             return X86EMUL_EXCEPTION;
@@ -217,17 +218,21 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
 void viridian_time_save_domain_ctxt(
     const struct domain *d, struct hvm_viridian_domain_context *ctxt)
 {
-    ctxt->time_ref_count = d->arch.hvm.viridian->time_ref_count.val;
-    ctxt->reference_tsc = d->arch.hvm.viridian->reference_tsc.raw;
+    const struct viridian_domain *vd = d->arch.hvm.viridian;
+
+    ctxt->time_ref_count = vd->time_ref_count.val;
+    ctxt->reference_tsc = vd->reference_tsc.raw;
 }
 
 void viridian_time_load_domain_ctxt(
     struct domain *d, const struct hvm_viridian_domain_context *ctxt)
 {
-    d->arch.hvm.viridian->time_ref_count.val = ctxt->time_ref_count;
-    d->arch.hvm.viridian->reference_tsc.raw = ctxt->reference_tsc;
+    struct viridian_domain *vd = d->arch.hvm.viridian;
+
+    vd->time_ref_count.val = ctxt->time_ref_count;
+    vd->reference_tsc.raw = ctxt->reference_tsc;
 
-    if ( d->arch.hvm.viridian->reference_tsc.fields.enabled )
+    if ( vd->reference_tsc.fields.enabled )
         update_reference_tsc(d, false);
 }
 
xen/arch/x86/hvm/viridian/viridian.c +25 -22

138 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v5 3/11
#6 lore raw

Re: [PATCH v5 03/11] viridian: use stack variables for viridian_vcpu and viridian_domain...

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:

...where there is more than one dereference inside a function.

This shortens the code and makes it more readable. No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

PATCH v5 4/11 viridian: make 'fields' struct anonymous...

In reply to Paul Durrant

...inside viridian_page_msr and viridian_guest_os_id_msr unions.

There's no need to name it and the code is shortened by not doing so. No functional change.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v4:
- New in v4

---
 xen/arch/x86/hvm/viridian/synic.c    |  4 ++--
 xen/arch/x86/hvm/viridian/time.c     | 10 +++++-----
 xen/arch/x86/hvm/viridian/viridian.c | 20 +++++++++-----------
 xen/include/asm-x86/hvm/viridian.h   |  4 ++--
 4 files changed, 18 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/viridian/synic.c +2 -2
index f3d9f7ae74..05d971b365 100644
@@ -102,7 +102,7 @@ int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
         viridian_unmap_guest_page(&vv->vp_assist);
         vv->vp_assist.msr.raw = val;
         viridian_dump_guest_page(v, "VP_ASSIST", &vv->vp_assist);
-        if ( vv->vp_assist.msr.fields.enabled )
+        if ( vv->vp_assist.msr.enabled )
             viridian_map_guest_page(v, &vv->vp_assist);
         break;
 
@@ -161,7 +161,7 @@ void viridian_synic_load_vcpu_ctxt(
     struct viridian_vcpu *vv = v->arch.hvm.viridian;
 
     vv->vp_assist.msr.raw = ctxt->vp_assist_msr;
-    if ( vv->vp_assist.msr.fields.enabled )
+    if ( vv->vp_assist.msr.enabled )
         viridian_map_guest_page(v, &vv->vp_assist);
 
     vv->apic_assist_pending = ctxt->apic_assist_pending;
xen/arch/x86/hvm/viridian/time.c +5 -5
index 76f9612001..909a3fb9e3 100644
@@ -29,16 +29,16 @@ static void dump_reference_tsc(const struct domain *d)
 {
     const union viridian_page_msr *rt = &d->arch.hvm.viridian->reference_tsc;
 
-    if ( !rt->fields.enabled )
+    if ( !rt->enabled )
         return;
 
     printk(XENLOG_G_INFO "d%d: VIRIDIAN REFERENCE_TSC: pfn: %lx\n",
-           d->domain_id, (unsigned long)rt->fields.pfn);
+           d->domain_id, (unsigned long)rt->pfn);
 }
 
 static void update_reference_tsc(struct domain *d, bool initialize)
 {
-    unsigned long gmfn = d->arch.hvm.viridian->reference_tsc.fields.pfn;
+    unsigned long gmfn = d->arch.hvm.viridian->reference_tsc.pfn;
     struct page_info *page = get_page_from_gfn(d, gmfn, NULL, P2M_ALLOC);
     HV_REFERENCE_TSC_PAGE *p;
 
@@ -151,7 +151,7 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 
         vd->reference_tsc.raw = val;
         dump_reference_tsc(d);
-        if ( vd->reference_tsc.fields.enabled )
+        if ( vd->reference_tsc.enabled )
             update_reference_tsc(d, true);
         break;
 
@@ -232,7 +232,7 @@ void viridian_time_load_domain_ctxt(
     vd->time_ref_count.val = ctxt->time_ref_count;
     vd->reference_tsc.raw = ctxt->reference_tsc;
 
-    if ( vd->reference_tsc.fields.enabled )
+    if ( vd->reference_tsc.enabled )
         update_reference_tsc(d, false);
 }
 
xen/arch/x86/hvm/viridian/viridian.c +9 -11
index 710470fed7..1a20d68aaf 100644
@@ -192,7 +192,7 @@ void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf,
 
     case 4:
         /* Recommended hypercall usage. */
-        if ( vd->guest_os_id.raw == 0 || vd->guest_os_id.fields.os < 4 )
+        if ( vd->guest_os_id.raw == 0 || vd->guest_os_id.os < 4 )
             break;
         res->a = CPUID4A_RELAX_TIMER_INT;
         if ( viridian_feature_mask(d) & HVMPV_hcall_remote_tlb_flush )
@@ -228,10 +228,8 @@ static void dump_guest_os_id(const struct domain *d)
 
     printk(XENLOG_G_INFO
            "d%d: VIRIDIAN GUEST_OS_ID: vendor: %x os: %x major: %x minor: %x sp: %x build: %x\n",
-           d->domain_id,
-           goi->fields.vendor, goi->fields.os,
-           goi->fields.major, goi->fields.minor,
-           goi->fields.service_pack, goi->fields.build_number);
+           d->domain_id, goi->vendor, goi->os, goi->major, goi->minor,
+           goi->service_pack, goi->build_number);
 }
 
 static void dump_hypercall(const struct domain *d)
@@ -242,12 +240,12 @@ static void dump_hypercall(const struct domain *d)
 
     printk(XENLOG_G_INFO "d%d: VIRIDIAN HYPERCALL: enabled: %x pfn: %lx\n",
            d->domain_id,
-           hg->fields.enabled, (unsigned long)hg->fields.pfn);
+           hg->enabled, (unsigned long)hg->pfn);
 }
 
 static void enable_hypercall_page(struct domain *d)
 {
-    unsigned long gmfn = d->arch.hvm.viridian->hypercall_gpa.fields.pfn;
+    unsigned long gmfn = d->arch.hvm.viridian->hypercall_gpa.pfn;
     struct page_info *page = get_page_from_gfn(d, gmfn, NULL, P2M_ALLOC);
     uint8_t *p;
 
@@ -297,7 +295,7 @@ int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val)
     case HV_X64_MSR_HYPERCALL:
         vd->hypercall_gpa.raw = val;
         dump_hypercall(d);
-        if ( vd->hypercall_gpa.fields.enabled )
+        if ( vd->hypercall_gpa.enabled )
             enable_hypercall_page(d);
         break;
 
@@ -606,17 +604,17 @@ out:
 void viridian_dump_guest_page(const struct vcpu *v, const char *name,
                               const struct viridian_page *vp)
 {
-    if ( !vp->msr.fields.enabled )
+    if ( !vp->msr.enabled )
         return;
 
     printk(XENLOG_G_INFO "%pv: VIRIDIAN %s: pfn: %lx\n",
-           v, name, (unsigned long)vp->msr.fields.pfn);
+           v, name, (unsigned long)vp->msr.pfn);
 }
 
 void viridian_map_guest_page(const struct vcpu *v, struct viridian_page *vp)
 {
     struct domain *d = v->domain;
-    unsigned long gmfn = vp->msr.fields.pfn;
+    unsigned long gmfn = vp->msr.pfn;
     struct page_info *page;
 
     if ( vp->ptr )
xen/include/asm-x86/hvm/viridian.h +2 -2
index c562424332..abbbb36092 100644
@@ -17,7 +17,7 @@ union viridian_page_msr
         uint64_t enabled:1;
         uint64_t reserved_preserved:11;
         uint64_t pfn:48;
-    } fields;
+    };
 };
 
 struct viridian_page
@@ -44,7 +44,7 @@ union viridian_guest_os_id_msr
         uint64_t major:8;
         uint64_t os:8;
         uint64_t vendor:16;
-    } fields;
+    };
 };
 
 struct viridian_time_ref_count
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v5 4/11
#8 lore raw

Re: [PATCH v5 04/11] viridian: make 'fields' struct anonymous...

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:

...inside viridian_page_msr and viridian_guest_os_id_msr unions.

There's no need to name it and the code is shortened by not doing so. No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

PATCH v5 5/11 viridian: extend init/deinit hooks into synic and time modules

In reply to Paul Durrant

This patch simply adds domain and vcpu init/deinit hooks into the synic and time modules and wires them into viridian_[domain|vcpu]_[init|deinit](). Only one of the hooks is currently needed (to unmap the 'VP Assist' page) but subsequent patches will make use of the others.

NOTE: To perform the unmap of the VP Assist page,
viridian_unmap_guest_page() is now directly called in the new viridian_synic_vcpu_deinit() function (which is safe even if is_viridian_vcpu() evaluates to false). This replaces the slightly hacky mechanism of faking a zero write to the HV_X64_MSR_VP_ASSIST_PAGE MSR in viridian_cpu_deinit().

---
Cc: Andrew Cooper <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v4:
- Constify vcpu and domain pointers

v2:
- Pay attention to sync and time init hook return values

---
 xen/arch/x86/hvm/viridian/private.h  | 12 +++++++++
 xen/arch/x86/hvm/viridian/synic.c    | 19 ++++++++++++++
 xen/arch/x86/hvm/viridian/time.c     | 18 ++++++++++++++
 xen/arch/x86/hvm/viridian/viridian.c | 37 ++++++++++++++++++++++++++--
 4 files changed, 84 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/viridian/private.h +12 -0
index 46174f48cd..8c029f62c6 100644
@@ -74,6 +74,12 @@
 int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val);
 int viridian_synic_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val);
 
+int viridian_synic_vcpu_init(const struct vcpu *v);
+int viridian_synic_domain_init(const struct domain *d);
+
+void viridian_synic_vcpu_deinit(const struct vcpu *v);
+void viridian_synic_domain_deinit(const struct domain *d);
+
 void viridian_synic_save_vcpu_ctxt(const struct vcpu *v,
                                    struct hvm_viridian_vcpu_context *ctxt);
 void viridian_synic_load_vcpu_ctxt(
@@ -82,6 +88,12 @@ void viridian_synic_load_vcpu_ctxt(
 int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val);
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val);
 
+int viridian_time_vcpu_init(const struct vcpu *v);
+int viridian_time_domain_init(const struct domain *d);
+
+void viridian_time_vcpu_deinit(const struct vcpu *v);
+void viridian_time_domain_deinit(const struct domain *d);
+
 void viridian_time_save_domain_ctxt(
     const struct domain *d, struct hvm_viridian_domain_context *ctxt);
 void viridian_time_load_domain_ctxt(
xen/arch/x86/hvm/viridian/synic.c +19 -0
index 05d971b365..4b00dbe1b3 100644
@@ -146,6 +146,25 @@ int viridian_synic_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
     return X86EMUL_OKAY;
 }
 
+int viridian_synic_vcpu_init(const struct vcpu *v)
+{
+    return 0;
+}
+
+int viridian_synic_domain_init(const struct domain *d)
+{
+    return 0;
+}
+
+void viridian_synic_vcpu_deinit(const struct vcpu *v)
+{
+    viridian_unmap_guest_page(&v->arch.hvm.viridian->vp_assist);
+}
+
+void viridian_synic_domain_deinit(const struct domain *d)
+{
+}
+
 void viridian_synic_save_vcpu_ctxt(const struct vcpu *v,
                                    struct hvm_viridian_vcpu_context *ctxt)
 {
xen/arch/x86/hvm/viridian/time.c +18 -0
index 909a3fb9e3..48aca7e0ab 100644
@@ -215,6 +215,24 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
     return X86EMUL_OKAY;
 }
 
+int viridian_time_vcpu_init(const struct vcpu *v)
+{
+    return 0;
+}
+
+int viridian_time_domain_init(const struct domain *d)
+{
+    return 0;
+}
+
+void viridian_time_vcpu_deinit(const struct vcpu *v)
+{
+}
+
+void viridian_time_domain_deinit(const struct domain *d)
+{
+}
+
 void viridian_time_save_domain_ctxt(
     const struct domain *d, struct hvm_viridian_domain_context *ctxt)
 {
xen/arch/x86/hvm/viridian/viridian.c +35 -2
index 1a20d68aaf..f9a509d918 100644
@@ -418,22 +418,52 @@ int guest_rdmsr_viridian(const struct vcpu *v, uint32_t idx, uint64_t *val)
 
 int viridian_vcpu_init(struct vcpu *v)
 {
+    int rc;
+
     ASSERT(!v->arch.hvm.viridian);
     v->arch.hvm.viridian = xzalloc(struct viridian_vcpu);
     if ( !v->arch.hvm.viridian )
         return -ENOMEM;
 
+    rc = viridian_synic_vcpu_init(v);
+    if ( rc )
+        goto fail;
+
+    rc = viridian_time_vcpu_init(v);
+    if ( rc )
+        goto fail;
+
     return 0;
+
+ fail:
+    viridian_vcpu_deinit(v);
+
+    return rc;
 }
 
 int viridian_domain_init(struct domain *d)
 {
+    int rc;
+
     ASSERT(!d->arch.hvm.viridian);
     d->arch.hvm.viridian = xzalloc(struct viridian_domain);
     if ( !d->arch.hvm.viridian )
         return -ENOMEM;
 
+    rc = viridian_synic_domain_init(d);
+    if ( rc )
+        goto fail;
+
+    rc = viridian_time_domain_init(d);
+    if ( rc )
+        goto fail;
+
     return 0;
+
+ fail:
+    viridian_domain_deinit(d);
+
+    return rc;
 }
 
 void viridian_vcpu_deinit(struct vcpu *v)
@@ -441,8 +471,8 @@ void viridian_vcpu_deinit(struct vcpu *v)
     if ( !v->arch.hvm.viridian )
         return;
 
-    if ( is_viridian_vcpu(v) )
-        viridian_synic_wrmsr(v, HV_X64_MSR_VP_ASSIST_PAGE, 0);
+    viridian_time_vcpu_deinit(v);
+    viridian_synic_vcpu_deinit(v);
 
     XFREE(v->arch.hvm.viridian);
 }
@@ -457,6 +487,9 @@ void viridian_domain_deinit(struct domain *d)
     if ( !d->arch.hvm.viridian )
         return;
 
+    viridian_time_domain_deinit(d);
+    viridian_synic_domain_deinit(d);
+
     XFREE(d->arch.hvm.viridian);
 }
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw

PATCH v5 6/11 viridian: add missing context save helpers into synic and time modules

In reply to Paul Durrant

Currently the time module lacks vcpu context save helpers and the synic module lacks domain context save helpers. These helpers are not yet required but subsequent patches will require at least some of them so this patch completes the set to avoid introducing them in an ad-hoc way.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v3:
- Add missing callers so that they are not added in an ad-hoc way

---
 xen/arch/x86/hvm/viridian/private.h  | 10 ++++++++++
 xen/arch/x86/hvm/viridian/synic.c    | 10 ++++++++++
 xen/arch/x86/hvm/viridian/time.c     | 10 ++++++++++
 xen/arch/x86/hvm/viridian/viridian.c |  4 ++++
 4 files changed, 34 insertions(+)
xen/arch/x86/hvm/viridian/private.h +10 -0
index 8c029f62c6..5078b2d2ab 100644
@@ -85,6 +85,11 @@ void viridian_synic_save_vcpu_ctxt(const struct vcpu *v,
 void viridian_synic_load_vcpu_ctxt(
     struct vcpu *v, const struct hvm_viridian_vcpu_context *ctxt);
 
+void viridian_synic_save_domain_ctxt(
+    const struct domain *d, struct hvm_viridian_domain_context *ctxt);
+void viridian_synic_load_domain_ctxt(
+    struct domain *d, const struct hvm_viridian_domain_context *ctxt);
+
 int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val);
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val);
 
@@ -94,6 +99,11 @@ int viridian_time_domain_init(const struct domain *d);
 void viridian_time_vcpu_deinit(const struct vcpu *v);
 void viridian_time_domain_deinit(const struct domain *d);
 
+void viridian_time_save_vcpu_ctxt(
+    const struct vcpu *v, struct hvm_viridian_vcpu_context *ctxt);
+void viridian_time_load_vcpu_ctxt(
+    struct vcpu *v, const struct hvm_viridian_vcpu_context *ctxt);
+
 void viridian_time_save_domain_ctxt(
     const struct domain *d, struct hvm_viridian_domain_context *ctxt);
 void viridian_time_load_domain_ctxt(
xen/arch/x86/hvm/viridian/synic.c +10 -0
index 4b00dbe1b3..b8dab4b246 100644
@@ -186,6 +186,16 @@ void viridian_synic_load_vcpu_ctxt(
     vv->apic_assist_pending = ctxt->apic_assist_pending;
 }
 
+void viridian_synic_save_domain_ctxt(
+    const struct domain *d, struct hvm_viridian_domain_context *ctxt)
+{
+}
+
+void viridian_synic_load_domain_ctxt(
+    struct domain *d, const struct hvm_viridian_domain_context *ctxt)
+{
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/hvm/viridian/time.c +10 -0
index 48aca7e0ab..4399e62f54 100644
@@ -233,6 +233,16 @@ void viridian_time_domain_deinit(const struct domain *d)
 {
 }
 
+void viridian_time_save_vcpu_ctxt(
+    const struct vcpu *v, struct hvm_viridian_vcpu_context *ctxt)
+{
+}
+
+void viridian_time_load_vcpu_ctxt(
+    struct vcpu *v, const struct hvm_viridian_vcpu_context *ctxt)
+{
+}
+
 void viridian_time_save_domain_ctxt(
     const struct domain *d, struct hvm_viridian_domain_context *ctxt)
 {
xen/arch/x86/hvm/viridian/viridian.c +4 -0
index f9a509d918..742a988252 100644
@@ -707,6 +707,7 @@ static int viridian_save_domain_ctxt(struct vcpu *v,
         return 0;
 
     viridian_time_save_domain_ctxt(d, &ctxt);
+    viridian_synic_save_domain_ctxt(d, &ctxt);
 
     return (hvm_save_entry(VIRIDIAN_DOMAIN, 0, h, &ctxt) != 0);
 }
@@ -723,6 +724,7 @@ static int viridian_load_domain_ctxt(struct domain *d,
     vd->hypercall_gpa.raw = ctxt.hypercall_gpa;
     vd->guest_os_id.raw = ctxt.guest_os_id;
 
+    viridian_synic_load_domain_ctxt(d, &ctxt);
     viridian_time_load_domain_ctxt(d, &ctxt);
 
     return 0;
@@ -738,6 +740,7 @@ static int viridian_save_vcpu_ctxt(struct vcpu *v, hvm_domain_context_t *h)
     if ( !is_viridian_vcpu(v) )
         return 0;
 
+    viridian_time_save_vcpu_ctxt(v, &ctxt);
     viridian_synic_save_vcpu_ctxt(v, &ctxt);
 
     return hvm_save_entry(VIRIDIAN_VCPU, v->vcpu_id, h, &ctxt);
@@ -764,6 +767,7 @@ static int viridian_load_vcpu_ctxt(struct domain *d,
         return -EINVAL;
 
     viridian_synic_load_vcpu_ctxt(v, &ctxt);
+    viridian_time_load_vcpu_ctxt(v, &ctxt);
 
     return 0;
 }
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#11 lore raw

PATCH v5 7/11 viridian: use viridian_map/unmap_guest_page() for reference tsc page

In reply to Paul Durrant

Whilst the reference tsc page does not currently need to be kept mapped after it is initially set up (or updated after migrate), the code can be simplified by using the common guest page map/unmap and dump functions. New functionality added by a subsequent patch will also require the page to kept mapped for the lifetime of the domain.

NOTE: Because the reference tsc page is per-domain rather than per-vcpu
this patch also changes viridian_map_guest_page() to take a domain pointer rather than a vcpu pointer. The domain pointer cannot be const, unlike the vcpu pointer.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

---
 xen/arch/x86/hvm/viridian/private.h  |  2 +-
 xen/arch/x86/hvm/viridian/synic.c    |  6 ++-
 xen/arch/x86/hvm/viridian/time.c     | 56 +++++++++-------------------
 xen/arch/x86/hvm/viridian/viridian.c |  3 +-
 xen/include/asm-x86/hvm/viridian.h   |  2 +-
 5 files changed, 25 insertions(+), 44 deletions(-)
xen/arch/x86/hvm/viridian/private.h +1 -1
index 5078b2d2ab..96a784b840 100644
@@ -111,7 +111,7 @@ void viridian_time_load_domain_ctxt(
 
 void viridian_dump_guest_page(const struct vcpu *v, const char *name,
                               const struct viridian_page *vp);
-void viridian_map_guest_page(const struct vcpu *v, struct viridian_page *vp);
+void viridian_map_guest_page(struct domain *d, struct viridian_page *vp);
 void viridian_unmap_guest_page(struct viridian_page *vp);
 
 #endif /* X86_HVM_VIRIDIAN_PRIVATE_H */
xen/arch/x86/hvm/viridian/synic.c +4 -2
index b8dab4b246..fb560bc162 100644
@@ -81,6 +81,7 @@ void viridian_apic_assist_clear(const struct vcpu *v)
 int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 {
     struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    struct domain *d = v->domain;
 
     switch ( idx )
     {
@@ -103,7 +104,7 @@ int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
         vv->vp_assist.msr.raw = val;
         viridian_dump_guest_page(v, "VP_ASSIST", &vv->vp_assist);
         if ( vv->vp_assist.msr.enabled )
-            viridian_map_guest_page(v, &vv->vp_assist);
+            viridian_map_guest_page(d, &vv->vp_assist);
         break;
 
     default:
@@ -178,10 +179,11 @@ void viridian_synic_load_vcpu_ctxt(
     struct vcpu *v, const struct hvm_viridian_vcpu_context *ctxt)
 {
     struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    struct domain *d = v->domain;
 
     vv->vp_assist.msr.raw = ctxt->vp_assist_msr;
     if ( vv->vp_assist.msr.enabled )
-        viridian_map_guest_page(v, &vv->vp_assist);
+        viridian_map_guest_page(d, &vv->vp_assist);
 
     vv->apic_assist_pending = ctxt->apic_assist_pending;
 }
xen/arch/x86/hvm/viridian/time.c +18 -38

109 lines not on this page · raw message

xen/arch/x86/hvm/viridian/viridian.c +1 -2
index 742a988252..2b045ed88f 100644
@@ -644,9 +644,8 @@ void viridian_dump_guest_page(const struct vcpu *v, const char *name,
            v, name, (unsigned long)vp->msr.pfn);
 }
 
-void viridian_map_guest_page(const struct vcpu *v, struct viridian_page *vp)
+void viridian_map_guest_page(struct domain *d, struct viridian_page *vp)
 {
-    struct domain *d = v->domain;
     unsigned long gmfn = vp->msr.pfn;
     struct page_info *page;
 
xen/include/asm-x86/hvm/viridian.h +1 -1
index abbbb36092..c65c044191 100644
@@ -65,7 +65,7 @@ struct viridian_domain
     union viridian_guest_os_id_msr guest_os_id;
     union viridian_page_msr hypercall_gpa;
     struct viridian_time_ref_count time_ref_count;
-    union viridian_page_msr reference_tsc;
+    struct viridian_page reference_tsc;
 };
 
 void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf,
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#12 lore raw

PATCH v5 8/11 viridian: stop directly calling viridian_time_ref_count_freeze/thaw()...

In reply to Paul Durrant

...from arch_domain_shutdown/pause/unpause().

A subsequent patch will introduce an implementaion of synthetic timers which will also need freeze/thaw hooks, so make the exported hooks more generic and call through to (re-named and static) time_ref_count_freeze/thaw functions.

NOTE: This patch also introduces a new time_ref_count() helper to return
the current counter value. This is currently only used by the MSR read handler but the synthetic timer code will also need to use it.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

---
 xen/arch/x86/domain.c              | 12 ++++++------
 xen/arch/x86/hvm/viridian/time.c   | 24 +++++++++++++++++++++---
 xen/include/asm-x86/hvm/viridian.h |  4 ++--
 3 files changed, 29 insertions(+), 11 deletions(-)
xen/arch/x86/domain.c +6 -6
index 8d579e2cf9..02afa7518e 100644
@@ -657,20 +657,20 @@ void arch_domain_destroy(struct domain *d)
 
 void arch_domain_shutdown(struct domain *d)
 {
-    if ( has_viridian_time_ref_count(d) )
-        viridian_time_ref_count_freeze(d);
+    if ( is_viridian_domain(d) )
+        viridian_time_domain_freeze(d);
 }
 
 void arch_domain_pause(struct domain *d)
 {
-    if ( has_viridian_time_ref_count(d) )
-        viridian_time_ref_count_freeze(d);
+    if ( is_viridian_domain(d) )
+        viridian_time_domain_freeze(d);
 }
 
 void arch_domain_unpause(struct domain *d)
 {
-    if ( has_viridian_time_ref_count(d) )
-        viridian_time_ref_count_thaw(d);
+    if ( is_viridian_domain(d) )
+        viridian_time_domain_thaw(d);
 }
 
 int arch_domain_soft_reset(struct domain *d)
xen/arch/x86/hvm/viridian/time.c +21 -3
index 16fe41d411..71291d921c 100644
@@ -91,7 +91,7 @@ static int64_t raw_trc_val(const struct domain *d)
     return scale_delta(tsc, &tsc_to_ns) / 100ul;
 }
 
-void viridian_time_ref_count_freeze(const struct domain *d)
+static void time_ref_count_freeze(const struct domain *d)
 {
     struct viridian_time_ref_count *trc =
         &d->arch.hvm.viridian->time_ref_count;
@@ -100,7 +100,7 @@ void viridian_time_ref_count_freeze(const struct domain *d)
         trc->val = raw_trc_val(d) + trc->off;
 }
 
-void viridian_time_ref_count_thaw(const struct domain *d)
+static void time_ref_count_thaw(const struct domain *d)
 {
     struct viridian_time_ref_count *trc =
         &d->arch.hvm.viridian->time_ref_count;
@@ -110,6 +110,24 @@ void viridian_time_ref_count_thaw(const struct domain *d)
         trc->off = (int64_t)trc->val - raw_trc_val(d);
 }
 
+static int64_t time_ref_count(const struct domain *d)
+{
+    struct viridian_time_ref_count *trc =
+        &d->arch.hvm.viridian->time_ref_count;
+
+    return raw_trc_val(d) + trc->off;
+}
+
+void viridian_time_domain_freeze(const struct domain *d)
+{
+    time_ref_count_freeze(d);
+}
+
+void viridian_time_domain_thaw(const struct domain *d)
+{
+    time_ref_count_thaw(d);
+}
+
 int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 {
     struct domain *d = v->domain;
@@ -179,7 +197,7 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
             printk(XENLOG_G_INFO "d%d: VIRIDIAN MSR_TIME_REF_COUNT: accessed\n",
                    d->domain_id);
 
-        *val = raw_trc_val(d) + trc->off;
+        *val = time_ref_count(d);
         break;
     }
 
xen/include/asm-x86/hvm/viridian.h +2 -2
index c65c044191..8146e2fc46 100644
@@ -77,8 +77,8 @@ int guest_rdmsr_viridian(const struct vcpu *v, uint32_t idx, uint64_t *val);
 int
 viridian_hypercall(struct cpu_user_regs *regs);
 
-void viridian_time_ref_count_freeze(const struct domain *d);
-void viridian_time_ref_count_thaw(const struct domain *d);
+void viridian_time_domain_freeze(const struct domain *d);
+void viridian_time_domain_thaw(const struct domain *d);
 
 int viridian_vcpu_init(struct vcpu *v);
 int viridian_domain_init(struct domain *d);
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v5 8/11
#13 lore raw

Re: [PATCH v5 08/11] viridian: stop directly calling viridian_time_ref_count_freeze/thaw()...

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:

...from arch_domain_shutdown/pause/unpause().

A subsequent patch will introduce an implementaion of synthetic timers which will also need freeze/thaw hooks, so make the exported hooks more generic and call through to (re-named and static) time_ref_count_freeze/thaw functions.

NOTE: This patch also introduces a new time_ref_count() helper to return
the current counter value. This is currently only used by the MSR read handler but the synthetic timer code will also need to use it.

Quoted 8 lines

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

---
 xen/arch/x86/domain.c              | 12 ++++++------

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#14 lore raw

PATCH v5 9/11 viridian: add implementation of synthetic interrupt MSRs

In reply to Paul Durrant

This patch introduces an implementation of the SCONTROL, SVERSION, SIEFP, SIMP, EOM and SINT0-15 SynIC MSRs. No message source is added and, as such, nothing will yet generate a synthetic interrupt. A subsequent patch will add an implementation of synthetic timers which will need the infrastructure added by this patch to deliver expiry messages to the guest.

NOTE: A 'synic' option is added to the toolstack viridian enlightenments
enumeration but is deliberately not documented as enabling these SynIC registers without a message source is only useful for debugging.

---
Cc: Ian Jackson <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v4:
- Address comments from Jan

v3:
- Add the 'SintPollingModeAvailable' bit in CPUID leaf 3

---
 tools/libxl/libxl.h                    |   6 +
 tools/libxl/libxl_dom.c                |   3 +
 tools/libxl/libxl_types.idl            |   1 +
 xen/arch/x86/hvm/viridian/synic.c      | 234 ++++++++++++++++++++++++-
 xen/arch/x86/hvm/viridian/viridian.c   |  19 ++
 xen/arch/x86/hvm/vlapic.c              |  32 +++-
 xen/include/asm-x86/hvm/hvm.h          |   3 +
 xen/include/asm-x86/hvm/viridian.h     |  25 +++
 xen/include/asm-x86/hvm/vlapic.h       |   1 +
 xen/include/public/arch-x86/hvm/save.h |   2 +
 xen/include/public/hvm/params.h        |   7 +-
 11 files changed, 326 insertions(+), 7 deletions(-)
tools/libxl/libxl.h +6 -0
index a38e5cdba2..a923a380d3 100644
@@ -318,6 +318,12 @@
  */
 #define LIBXL_HAVE_VIRIDIAN_CRASH_CTL 1
 
+/*
+ * LIBXL_HAVE_VIRIDIAN_SYNIC indicates that the 'synic' value
+ * is present in the viridian enlightenment enumeration.
+ */
+#define LIBXL_HAVE_VIRIDIAN_SYNIC 1
+
 /*
  * LIBXL_HAVE_BUILDINFO_HVM_ACPI_LAPTOP_SLATE indicates that
  * libxl_domain_build_info has the u.hvm.acpi_laptop_slate field.
tools/libxl/libxl_dom.c +3 -0
index 6160991af3..fb758d2ac3 100644
@@ -317,6 +317,9 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,
     if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_CRASH_CTL))
         mask |= HVMPV_crash_ctl;
 
+    if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_SYNIC))
+        mask |= HVMPV_synic;
+
     if (mask != 0 &&
         xc_hvm_param_set(CTX->xch,
                          domid,
tools/libxl/libxl_types.idl +1 -0
index b685ac47ac..9860bcaf5f 100644
@@ -235,6 +235,7 @@ libxl_viridian_enlightenment = Enumeration("viridian_enlightenment", [
     (4, "hcall_remote_tlb_flush"),
     (5, "apic_assist"),
     (6, "crash_ctl"),
+    (7, "synic"),
     ])
 
 libxl_hdtype = Enumeration("hdtype", [
xen/arch/x86/hvm/viridian/synic.c +232 -2

311 lines not on this page · raw message

xen/arch/x86/hvm/viridian/viridian.c +19 -0
index 2b045ed88f..67d0121924 100644
@@ -89,6 +89,7 @@ typedef union _HV_CRASH_CTL_REG_CONTENTS
 
 /* Viridian CPUID leaf 3, Hypervisor Feature Indication */
 #define CPUID3D_CRASH_MSRS (1 << 10)
+#define CPUID3D_SINT_POLLING (1 << 17)
 
 /* Viridian CPUID leaf 4: Implementation Recommendations. */
 #define CPUID4A_HCALL_REMOTE_TLB_FLUSH (1 << 2)
@@ -178,6 +179,8 @@ void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf,
             mask.AccessPartitionReferenceCounter = 1;
         if ( viridian_feature_mask(d) & HVMPV_reference_tsc )
             mask.AccessPartitionReferenceTsc = 1;
+        if ( viridian_feature_mask(d) & HVMPV_synic )
+            mask.AccessSynicRegs = 1;
 
         u.mask = mask;
 
@@ -186,6 +189,8 @@ void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf,
 
         if ( viridian_feature_mask(d) & HVMPV_crash_ctl )
             res->d = CPUID3D_CRASH_MSRS;
+        if ( viridian_feature_mask(d) & HVMPV_synic )
+            res->d = CPUID3D_SINT_POLLING;
 
         break;
     }
@@ -306,8 +311,16 @@ int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val)
     case HV_X64_MSR_ICR:
     case HV_X64_MSR_TPR:
     case HV_X64_MSR_VP_ASSIST_PAGE:
+    case HV_X64_MSR_SCONTROL:
+    case HV_X64_MSR_SVERSION:
+    case HV_X64_MSR_SIEFP:
+    case HV_X64_MSR_SIMP:
+    case HV_X64_MSR_EOM:
+    case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
         return viridian_synic_wrmsr(v, idx, val);
 
+    case HV_X64_MSR_TSC_FREQUENCY:
+    case HV_X64_MSR_APIC_FREQUENCY:
     case HV_X64_MSR_REFERENCE_TSC:
         return viridian_time_wrmsr(v, idx, val);
 
@@ -378,6 +391,12 @@ int guest_rdmsr_viridian(const struct vcpu *v, uint32_t idx, uint64_t *val)
     case HV_X64_MSR_ICR:
     case HV_X64_MSR_TPR:
     case HV_X64_MSR_VP_ASSIST_PAGE:
+    case HV_X64_MSR_SCONTROL:
+    case HV_X64_MSR_SVERSION:
+    case HV_X64_MSR_SIEFP:
+    case HV_X64_MSR_SIMP:
+    case HV_X64_MSR_EOM:
+    case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
         return viridian_synic_rdmsr(v, idx, val);
 
     case HV_X64_MSR_TSC_FREQUENCY:
xen/arch/x86/hvm/vlapic.c +28 -4
index a1a43cd792..5a732858ca 100644
@@ -461,10 +461,15 @@ void vlapic_EOI_set(struct vlapic *vlapic)
 
 void vlapic_handle_EOI(struct vlapic *vlapic, u8 vector)
 {
+    struct vcpu *v = vlapic_vcpu(vlapic);
     struct domain *d = vlapic_domain(vlapic);
 
+    /* All synic SINTx vectors are edge triggered */
+
     if ( vlapic_test_vector(vector, &vlapic->regs->data[APIC_TMR]) )
         vioapic_update_EOI(d, vector);
+    else if ( has_viridian_synic(v->domain) )
+        viridian_synic_ack_sint(v, vector);
 
     hvm_dpci_msi_eoi(d, vector);
 }
@@ -1301,13 +1306,23 @@ int vlapic_has_pending_irq(struct vcpu *v)
     if ( !vlapic_enabled(vlapic) )
         return -1;
 
+    /*
+     * Poll the viridian message queues before checking the IRR since
+     * a sythetic interrupt may be asserted during the poll.
+     */
+    if ( !vlapic->polled_synic && has_viridian_synic(v->domain) )
+    {
+        viridian_synic_poll_messages(v);
+        vlapic->polled_synic = true;
+    }
+
     irr = vlapic_find_highest_irr(vlapic);
     if ( irr == -1 )
-        return -1;
+        goto out;
 
     if ( hvm_funcs.virtual_intr_delivery_enabled &&
          !nestedhvm_vcpu_in_guestmode(v) )
-        return irr;
+        goto out;
 
     /*
      * If APIC assist was set then an EOI may have been avoided.
@@ -1328,9 +1343,13 @@ int vlapic_has_pending_irq(struct vcpu *v)
          (irr & 0xf0) <= (isr & 0xf0) )
     {
         viridian_apic_assist_clear(v);
-        return -1;
+        irr = -1;
     }
 
+out:
+    if (irr == -1)
+        vlapic->polled_synic = false;
+
     return irr;
 }
 
@@ -1360,8 +1379,13 @@ int vlapic_ack_pending_irq(struct vcpu *v, int vector, bool_t force_ack)
     }
 
  done:
-    vlapic_set_vector(vector, &vlapic->regs->data[APIC_ISR]);
+    if ( !has_viridian_synic(v->domain) ||
+         !viridian_synic_is_auto_eoi_sint(v, vector) )
+        vlapic_set_vector(vector, &vlapic->regs->data[APIC_ISR]);
+
     vlapic_clear_irr(vector, vlapic);
+    vlapic->polled_synic = false;
+
     return 1;
 }
 
xen/include/asm-x86/hvm/hvm.h +3 -0
index 37c3567a57..f67e9dbd12 100644
@@ -472,6 +472,9 @@ static inline bool hvm_get_guest_bndcfgs(struct vcpu *v, u64 *val)
 #define has_viridian_apic_assist(d) \
     (is_viridian_domain(d) && (viridian_feature_mask(d) & HVMPV_apic_assist))
 
+#define has_viridian_synic(d) \
+    (is_viridian_domain(d) && (viridian_feature_mask(d) & HVMPV_synic))
+
 static inline void hvm_inject_exception(
     unsigned int vector, unsigned int type,
     unsigned int insn_len, int error_code)
xen/include/asm-x86/hvm/viridian.h +25 -0
index 8146e2fc46..7446be492b 100644
@@ -26,10 +26,30 @@ struct viridian_page
     void *ptr;
 };
 
+union viridian_sint_msr
+{
+    uint64_t raw;
+    struct
+    {
+        uint64_t vector:8;
+        uint64_t reserved_preserved1:8;
+        uint64_t mask:1;
+        uint64_t auto_eoi:1;
+        uint64_t polling:1;
+        uint64_t reserved_preserved2:45;
+    };
+};
+
 struct viridian_vcpu
 {
     struct viridian_page vp_assist;
     bool apic_assist_pending;
+    uint64_t scontrol;
+    uint64_t siefp;
+    struct viridian_page simp;
+    union viridian_sint_msr sint[16];
+    uint8_t vector_to_sintx[256];
+    unsigned long msg_pending;
     uint64_t crash_param[5];
 };
 
@@ -90,6 +110,11 @@ void viridian_apic_assist_set(const struct vcpu *v);
 bool viridian_apic_assist_completed(const struct vcpu *v);
 void viridian_apic_assist_clear(const struct vcpu *v);
 
+bool viridian_synic_is_auto_eoi_sint(const struct vcpu *v,
+                                     unsigned int vector);
+void viridian_synic_poll_messages(const struct vcpu *v);
+void viridian_synic_ack_sint(const struct vcpu *v, unsigned int vector);
+
 #endif /* __ASM_X86_HVM_VIRIDIAN_H__ */
 
 /*
xen/include/asm-x86/hvm/vlapic.h +1 -0
index dde66b4f0f..7b2195c4eb 100644
@@ -91,6 +91,7 @@ struct vlapic {
         uint32_t             icr, dest;
         struct tasklet       tasklet;
     } init_sipi;
+    bool                     polled_synic;
 };
 
 /* vlapic's frequence is 100 MHz */
xen/include/public/arch-x86/hvm/save.h +2 -0
index 40be84ecda..ec3e4df12c 100644
@@ -602,6 +602,8 @@ struct hvm_viridian_vcpu_context {
     uint64_t vp_assist_msr;
     uint8_t  apic_assist_pending;
     uint8_t  _pad[7];
+    uint64_t simp_msr;
+    uint64_t sint_msr[16];
 };
 
 DECLARE_HVM_SAVE_TYPE(VIRIDIAN_VCPU, 17, struct hvm_viridian_vcpu_context);
xen/include/public/hvm/params.h +6 -1
index 72f633ef2d..e7e3c7c892 100644
@@ -146,6 +146,10 @@
 #define _HVMPV_crash_ctl 6
 #define HVMPV_crash_ctl (1 << _HVMPV_crash_ctl)
 
+/* Enable SYNIC MSRs */
+#define _HVMPV_synic 7
+#define HVMPV_synic (1 << _HVMPV_synic)
+
 #define HVMPV_feature_mask \
         (HVMPV_base_freq | \
          HVMPV_no_freq | \
@@ -153,7 +157,8 @@
          HVMPV_reference_tsc | \
          HVMPV_hcall_remote_tlb_flush | \
          HVMPV_apic_assist | \
-         HVMPV_crash_ctl)
+         HVMPV_crash_ctl | \
+         HVMPV_synic)
 
 #endif
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#15 lore raw

Re: [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:
xen/arch/x86/hvm/viridian/synic.c +10 -0
@@ -28,6 +29,32 @@ typedef union _HV_VP_ASSIST_PAGE
     uint8_t ReservedZBytePadding[PAGE_SIZE];
 } HV_VP_ASSIST_PAGE;
 
+typedef enum HV_MESSAGE_TYPE {
+    HvMessageTypeNone,
+    HvMessageTimerExpired = 0x80000010,
+} HV_MESSAGE_TYPE;
+
+typedef struct HV_MESSAGE_FLAGS {
+    uint8_t MessagePending:1;
+    uint8_t Reserved:7;
+} HV_MESSAGE_FLAGS;
+

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 51

Quoted 10 lines
xen/arch/x86/hvm/viridian/synic.c +10 -0
+typedef struct HV_MESSAGE_HEADER {
+    HV_MESSAGE_TYPE MessageType;
+    uint16_t Reserved1;
+    HV_MESSAGE_FLAGS MessageFlags;
+    uint8_t PayloadSize;
+    uint64_t Reserved2;
+} HV_MESSAGE_HEADER;
+
+#define HV_MESSAGE_SIZE 256
+#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT 30

Is this defined this way, or (given ...

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 56

Quoted 4 lines
xen/arch/x86/hvm/viridian/synic.c +4 -0
+typedef struct HV_MESSAGE {
+    HV_MESSAGE_HEADER Header;
+    uint64_t Payload[HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT];
+} HV_MESSAGE;

... this) isn't it rather

#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT \
((HV_MESSAGE_SIZE - sizeof(HV_MESSAGE_HEADER) / 8)

xen/arch/x86/hvm/viridian/synic.c int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 177

Quoted 4 lines
xen/arch/x86/hvm/viridian/synic.c +4 -0
+    case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
+    {
+        unsigned int sintx = array_index_nospec(idx - HV_X64_MSR_SINT0,
+                                                ARRAY_SIZE(vv->sint));

While here I can see the usefulness of using the local variable (but you're aware of the remaining issue with going this route?), ...

xen/arch/x86/hvm/viridian/synic.c int viridian_synic_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val) · line 296

Quoted 10 lines
xen/arch/x86/hvm/viridian/synic.c +10 -0
+    case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
+    {
+        unsigned int sintx = array_index_nospec(idx - HV_X64_MSR_SINT0,
+                                                ARRAY_SIZE(vv->sint));
+
+        if ( !(viridian_feature_mask(d) & HVMPV_synic) )
+            return X86EMUL_EXCEPTION;
+
+        *val = vv->sint[sintx].raw;
+        break;

... I think you would better use array_access_nospec() here, to avoid that very risk.

xen/arch/x86/hvm/viridian/synic.c int viridian_synic_domain_init(const struct domain *d) · line 358

Quoted 11 lines
xen/arch/x86/hvm/viridian/synic.c +11 -0
+bool viridian_synic_is_auto_eoi_sint(const struct vcpu *v,
+                                     unsigned int vector)
+{
+    const struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int idx = vv->vector_to_sintx[vector];
+    unsigned int sintx = array_index_nospec(idx, ARRAY_SIZE(vv->sint));
+
+    if ( idx >= ARRAY_SIZE(vv->sint) )
+        return false;
+
+    return vv->sint[sintx].auto_eoi;

Same here then.

xen/arch/x86/hvm/vlapic.c void vlapic_EOI_set(struct vlapic *vlapic) · line 465

Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +1 -0
@@ -461,10 +461,15 @@ void vlapic_EOI_set(struct vlapic *vlapic)
 
 void vlapic_handle_EOI(struct vlapic *vlapic, u8 vector)
 {
+    struct vcpu *v = vlapic_vcpu(vlapic);
     struct domain *d = vlapic_domain(vlapic);

Please use v->domain here.

xen/arch/x86/hvm/vlapic.c void vlapic_EOI_set(struct vlapic *vlapic) · line 471

Quoted 5 lines
xen/arch/x86/hvm/vlapic.c +3 -0
+    /* All synic SINTx vectors are edge triggered */
+
     if ( vlapic_test_vector(vector, &vlapic->regs->data[APIC_TMR]) )
         vioapic_update_EOI(d, vector);
+    else if ( has_viridian_synic(v->domain) )

And please use d here.

xen/arch/x86/hvm/vlapic.c int vlapic_has_pending_irq(struct vcpu *v) · line 1311

Quoted 7 lines
xen/arch/x86/hvm/vlapic.c +3 -0
@@ -1301,13 +1306,23 @@ int vlapic_has_pending_irq(struct vcpu *v)
     if ( !vlapic_enabled(vlapic) )
         return -1;
 
+    /*
+     * Poll the viridian message queues before checking the IRR since
+     * a sythetic interrupt may be asserted during the poll.

synthetic

xen/arch/x86/hvm/vlapic.c int vlapic_has_pending_irq(struct vcpu *v) · line 1350

Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +3 -1
@@ -1328,9 +1343,13 @@ int vlapic_has_pending_irq(struct vcpu *v)
          (irr & 0xf0) <= (isr & 0xf0) )
     {
         viridian_apic_assist_clear(v);
-        return -1;
+        irr = -1;
     }
 
+out:
+    if (irr == -1)

Style: label indentation and spaces inside the parentheses.

xen/arch/x86/hvm/vlapic.c int vlapic_has_pending_irq(struct vcpu *v) · line 1351

Quoted 1 line
xen/arch/x86/hvm/vlapic.c +1 -0
+        vlapic->polled_synic = false;

I'm struggling to understand the purpose of this flag, and the situation is no helped by viridian_synic_poll_messages() currently doing nothing. It would be really nice if maintenance of a flag like this - if needed in the first place - could be kept local to Viridian code (but of course not at the expense of adding various new hooks for that purpose).

Quoted 19 lines
xen/include/asm-x86/hvm/viridian.h +13 -0
@@ -26,10 +26,30 @@ struct viridian_page
     void *ptr;
 };
 
+union viridian_sint_msr
+{
+    uint64_t raw;
+    struct
+    {
+        uint64_t vector:8;
+        uint64_t reserved_preserved1:8;
+        uint64_t mask:1;
+        uint64_t auto_eoi:1;
+        uint64_t polling:1;
+        uint64_t reserved_preserved2:45;
+    };
+};

xen/include/asm-x86/hvm/viridian.h struct viridian_page · line 51

Quoted 10 lines
xen/include/asm-x86/hvm/viridian.h +6 -0
+
 struct viridian_vcpu
 {
     struct viridian_page vp_assist;
     bool apic_assist_pending;
+    uint64_t scontrol;
+    uint64_t siefp;
+    struct viridian_page simp;
+    union viridian_sint_msr sint[16];
+    uint8_t vector_to_sintx[256];

I've been wondering about the wasted initial 16 bytes here, but looking at the code trying to save that space would apparently complicate some of the array accesses in undue fashion.

xen/include/asm-x86/hvm/viridian.h struct viridian_page · line 52

Quoted 1 line
xen/include/asm-x86/hvm/viridian.h +1 -0
+    unsigned long msg_pending;

Does this really need to be unsigned long? There are only 16 bits used here, and bitops ought to work fine on an unsigned int. Once reduced, the field could then fill the gap between apic_assist_pending and scontrol.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#16 lore raw

Re: [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 49 lines

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Jan Beulich
Sent: 13 March 2019 13:15
To: Paul Durrant <[email protected]>
Cc: Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; George Dunlap <[email protected]>; Andrew Cooper <[email protected]>; Ian Jackson <[email protected]>; Tim (Xen.org) <[email protected]>; Julien Grall <[email protected]>; xen-devel <[email protected]>; Roger Pau Monne <[email protected]>
Subject: Re: [Xen-devel] [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:
xen/arch/x86/hvm/viridian/synic.c +10 -0
@@ -28,6 +29,32 @@ typedef union _HV_VP_ASSIST_PAGE
     uint8_t ReservedZBytePadding[PAGE_SIZE];
 } HV_VP_ASSIST_PAGE;
 
+typedef enum HV_MESSAGE_TYPE {
+    HvMessageTypeNone,
+    HvMessageTimerExpired = 0x80000010,
+} HV_MESSAGE_TYPE;
+
+typedef struct HV_MESSAGE_FLAGS {
+    uint8_t MessagePending:1;
+    uint8_t Reserved:7;
+} HV_MESSAGE_FLAGS;
+

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 51

Quoted 10 lines
xen/arch/x86/hvm/viridian/synic.c +10 -0
+typedef struct HV_MESSAGE_HEADER {
+    HV_MESSAGE_TYPE MessageType;
+    uint16_t Reserved1;
+    HV_MESSAGE_FLAGS MessageFlags;
+    uint8_t PayloadSize;
+    uint64_t Reserved2;
+} HV_MESSAGE_HEADER;
+
+#define HV_MESSAGE_SIZE 256
+#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT 30

Is this defined this way, or (given ...

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 56

Quoted 4 lines
xen/arch/x86/hvm/viridian/synic.c +4 -0
+typedef struct HV_MESSAGE {
+    HV_MESSAGE_HEADER Header;
+    uint64_t Payload[HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT];
+} HV_MESSAGE;

... this) isn't it rather

#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT \
((HV_MESSAGE_SIZE - sizeof(HV_MESSAGE_HEADER) / 8)

Quoted 7 lines

xen/arch/x86/hvm/viridian/synic.c int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 177

Quoted 4 lines
xen/arch/x86/hvm/viridian/synic.c +4 -0
+    case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
+    {
+        unsigned int sintx = array_index_nospec(idx - HV_X64_MSR_SINT0,
+                                                ARRAY_SIZE(vv->sint));

While here I can see the usefulness of using the local variable (but you're aware of the remaining issue with going this route?), ...

I guess I'm not aware. Given that using sintx cannot lead to an out-of-bounds access, what is the risk?

Quoted 40 lines

xen/arch/x86/hvm/viridian/synic.c int viridian_synic_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val) · line 296

Quoted 11 lines
xen/arch/x86/hvm/viridian/synic.c +10 -0
+    case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
+    {
+        unsigned int sintx = array_index_nospec(idx - HV_X64_MSR_SINT0,
+                                                ARRAY_SIZE(vv->sint));
+
+        if ( !(viridian_feature_mask(d) & HVMPV_synic) )
+            return X86EMUL_EXCEPTION;
+
+        *val = vv->sint[sintx].raw;
+        break;

... I think you would better use array_access_nospec() here, to avoid that very risk.

xen/arch/x86/hvm/viridian/synic.c int viridian_synic_domain_init(const struct domain *d) · line 358

Quoted 11 lines
xen/arch/x86/hvm/viridian/synic.c +11 -0
+bool viridian_synic_is_auto_eoi_sint(const struct vcpu *v,
+                                     unsigned int vector)
+{
+    const struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int idx = vv->vector_to_sintx[vector];
+    unsigned int sintx = array_index_nospec(idx, ARRAY_SIZE(vv->sint));
+
+    if ( idx >= ARRAY_SIZE(vv->sint) )
+        return false;
+
+    return vv->sint[sintx].auto_eoi;

Same here then.

xen/arch/x86/hvm/vlapic.c void vlapic_EOI_set(struct vlapic *vlapic) · line 465

Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +1 -0
@@ -461,10 +461,15 @@ void vlapic_EOI_set(struct vlapic *vlapic)
 
 void vlapic_handle_EOI(struct vlapic *vlapic, u8 vector)
 {
+    struct vcpu *v = vlapic_vcpu(vlapic);
     struct domain *d = vlapic_domain(vlapic);

Please use v->domain here.

Quoted 7 lines

xen/arch/x86/hvm/vlapic.c void vlapic_EOI_set(struct vlapic *vlapic) · line 471

Quoted 5 lines
xen/arch/x86/hvm/vlapic.c +3 -0
+    /* All synic SINTx vectors are edge triggered */
+
     if ( vlapic_test_vector(vector, &vlapic->regs->data[APIC_TMR]) )
         vioapic_update_EOI(d, vector);
+    else if ( has_viridian_synic(v->domain) )

And please use d here.

Sorry, yes. I changed it and then apparently lost the change.

Quoted 19 lines

xen/arch/x86/hvm/vlapic.c int vlapic_has_pending_irq(struct vcpu *v) · line 1311

Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +3 -0
@@ -1301,13 +1306,23 @@ int vlapic_has_pending_irq(struct vcpu *v)
     if ( !vlapic_enabled(vlapic) )
         return -1;
 
+    /*
+     * Poll the viridian message queues before checking the IRR since
+     * a sythetic interrupt may be asserted during the poll.

synthetic

Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +1 -1
@@ -1328,9 +1343,13 @@ int vlapic_has_pending_irq(struct vcpu *v)
          (irr & 0xf0) <= (isr & 0xf0) )
     {
         viridian_apic_assist_clear(v);
-        return -1;
+        irr = -1;
     }
 
Quoted 4 lines

xen/arch/x86/hvm/vlapic.c int vlapic_has_pending_irq(struct vcpu *v) · line 1350

Quoted 2 lines
xen/arch/x86/hvm/vlapic.c +2 -0
+out:
+    if (irr == -1)

Style: label indentation and spaces inside the parentheses.

Oops.

Quoted 9 lines

xen/arch/x86/hvm/vlapic.c int vlapic_has_pending_irq(struct vcpu *v) · line 1351

Quoted 2 lines
xen/arch/x86/hvm/vlapic.c +1 -0
+        vlapic->polled_synic = false;

I'm struggling to understand the purpose of this flag, and the situation is no helped by viridian_synic_poll_messages() currently doing nothing. It would be really nice if maintenance of a flag like this - if needed in the first place - could be kept local to Viridian code (but of course not at the expense of adding various new hooks for that purpose).

The flag is there to stop viridian_synic_poll_messages() being called multiple times as you requested. I can move the flag into the viridian code but I'll have to add add extra call to unblock the poll in this case and in the ack function.

Quoted 35 lines
Quoted 20 lines
xen/include/asm-x86/hvm/viridian.h +13 -0
@@ -26,10 +26,30 @@ struct viridian_page
     void *ptr;
 };
 
+union viridian_sint_msr
+{
+    uint64_t raw;
+    struct
+    {
+        uint64_t vector:8;
+        uint64_t reserved_preserved1:8;
+        uint64_t mask:1;
+        uint64_t auto_eoi:1;
+        uint64_t polling:1;
+        uint64_t reserved_preserved2:45;
+    };
+};

xen/include/asm-x86/hvm/viridian.h struct viridian_page · line 51

Quoted 10 lines
xen/include/asm-x86/hvm/viridian.h +6 -0
+
 struct viridian_vcpu
 {
     struct viridian_page vp_assist;
     bool apic_assist_pending;
+    uint64_t scontrol;
+    uint64_t siefp;
+    struct viridian_page simp;
+    union viridian_sint_msr sint[16];
+    uint8_t vector_to_sintx[256];

I've been wondering about the wasted initial 16 bytes here, but looking at the code trying to save that space would apparently complicate some of the array accesses in undue fashion.

Quoted 5 lines

xen/include/asm-x86/hvm/viridian.h struct viridian_page · line 52

Quoted 1 line
xen/include/asm-x86/hvm/viridian.h +1 -0
+    unsigned long msg_pending;

Does this really need to be unsigned long? There are only 16 bits used here, and bitops ought to work fine on an unsigned int. Once reduced, the field could then fill the gap between apic_assist_pending and scontrol.

Ok.

Paul

Quoted 8 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#17 lore raw

Re: [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 9 lines
Quoted 9 lines
Quoted 1 line · On 13.03.19 at 14:25, <[email protected]> wrote:

From: Xen-devel [mailto:[email protected]] On Behalf Of Jan Beulich
Sent: 13 March 2019 13:15

Quoted 4 lines
xen/arch/x86/hvm/viridian/synic.c +4 -0
+    case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
+    {
+        unsigned int sintx = array_index_nospec(idx - HV_X64_MSR_SINT0,
+                                                ARRAY_SIZE(vv->sint));
Quoted 5 lines
Quoted 2 lines

While here I can see the usefulness of using the local variable (but you're aware of the remaining issue with going this route?), ...

I guess I'm not aware. Given that using sintx cannot lead to an out-of-bounds access, what is the risk?

The workaround is effective only as long as the variable stays in a register. If it gets read from memory before use, mis-speculation is possible again from all we can tell.

Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +1 -1
@@ -1328,9 +1343,13 @@ int vlapic_has_pending_irq(struct vcpu *v)
          (irr & 0xf0) <= (isr & 0xf0) )
     {
         viridian_apic_assist_clear(v);
-        return -1;
+        irr = -1;
     }
 
Quoted 10 lines

xen/arch/x86/hvm/vlapic.c int vlapic_has_pending_irq(struct vcpu *v) · line 1351

Quoted 3 lines
xen/arch/x86/hvm/vlapic.c +3 -0
+out:
+    if (irr == -1)
+        vlapic->polled_synic = false;

I'm struggling to understand the purpose of this flag, and the situation is no helped by viridian_synic_poll_messages() currently doing nothing. It would be really nice if maintenance of a flag like this - if needed in the first place - could be kept local to Viridian code (but of course not at the expense of adding various new hooks for that purpose).

Quoted 4 lines

The flag is there to stop viridian_synic_poll_messages() being called multiple times as you requested. I can move the flag into the viridian code but I'll have to add add extra call to unblock the poll in this case and in the ack function.

Well, in that case it's perhaps better to keep as is.

As to me having requested this - in an abstract way, yes, but to be honest I couldn't have deduced that connection from the name you've chosen. "polled_synic" reads to me like reflecting some guest property. I admit though that I'm also having difficulty suggesting a better alternative: "synic_polled", "synic_was_polled", or "sync_poll_pending" come to mind.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw

Re: [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 23 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 13 March 2019 14:23
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Roger Pau Monne <[email protected]>; Wei Liu <[email protected]>; Stefano Stabellini <[email protected]>; xen-devel <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: RE: [Xen-devel] [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 12 lines
Quoted 11 lines
Quoted 1 line · On 13.03.19 at 14:25, <[email protected]> wrote:

From: Xen-devel [mailto:[email protected]] On Behalf Of Jan Beulich
Sent: 13 March 2019 13:15

xen/arch/x86/hvm/viridian/synic.c int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 177

Quoted 4 lines
xen/arch/x86/hvm/viridian/synic.c +4 -0
+    case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
+    {
+        unsigned int sintx = array_index_nospec(idx - HV_X64_MSR_SINT0,
+                                                ARRAY_SIZE(vv->sint));

While here I can see the usefulness of using the local variable (but you're aware of the remaining issue with going this route?), ...

Quoted 6 lines
Quoted 2 lines

I guess I'm not aware. Given that using sintx cannot lead to an out-of-bounds access, what is the risk?

The workaround is effective only as long as the variable stays in a register. If it gets read from memory before use, mis-speculation is possible again from all we can tell.

Ah, ok. So, having talked to Andrew it would seem better to immediately calculate the pointer into the array and use that. I can do that here. In other cases, as long as the stack variable is only used once, it would seem the better to way to construct the code.

Quoted 25 lines
Quoted 20 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/hvm/vlapic.c +1 -1
@@ -1328,9 +1343,13 @@ int vlapic_has_pending_irq(struct vcpu *v)
          (irr & 0xf0) <= (isr & 0xf0) )
     {
         viridian_apic_assist_clear(v);
-        return -1;
+        irr = -1;
     }
 
Quoted 10 lines

xen/arch/x86/hvm/vlapic.c int vlapic_has_pending_irq(struct vcpu *v) · line 1351

Quoted 3 lines
xen/arch/x86/hvm/vlapic.c +3 -0
+out:
+    if (irr == -1)
+        vlapic->polled_synic = false;

I'm struggling to understand the purpose of this flag, and the situation is no helped by viridian_synic_poll_messages() currently doing nothing. It would be really nice if maintenance of a flag like this - if needed in the first place - could be kept local to Viridian code (but of course not at the expense of adding various new hooks for that purpose).

Quoted 4 lines

The flag is there to stop viridian_synic_poll_messages() being called multiple times as you requested. I can move the flag into the viridian code but I'll have to add add extra call to unblock the poll in this case and in the ack function.

Quoted 9 lines

Well, in that case it's perhaps better to keep as is.

As to me having requested this - in an abstract way, yes, but to be honest I couldn't have deduced that connection from the name you've chosen. "polled_synic" reads to me like reflecting some guest property. I admit though that I'm also having difficulty suggesting a better alternative: "synic_polled", "synic_was_polled", or "sync_poll_pending" come to mind.

Given the confusion, I think keeping the flag within the viridian code may well be better.

Paul

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#19 lore raw

Re: [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

In reply to Jan Beulich
Quoted 28 lines

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Jan Beulich
Sent: 13 March 2019 13:15
To: Paul Durrant <[email protected]>
Cc: Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; George Dunlap <[email protected]>; Andrew Cooper <[email protected]>; Ian Jackson <[email protected]>; Tim (Xen.org) <[email protected]>; Julien Grall <[email protected]>; xen-devel <[email protected]>; Roger Pau Monne <[email protected]>
Subject: Re: [Xen-devel] [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:
xen/arch/x86/hvm/viridian/synic.c +12 -0
@@ -28,6 +29,32 @@ typedef union _HV_VP_ASSIST_PAGE
     uint8_t ReservedZBytePadding[PAGE_SIZE];
 } HV_VP_ASSIST_PAGE;
 
+typedef enum HV_MESSAGE_TYPE {
+    HvMessageTypeNone,
+    HvMessageTimerExpired = 0x80000010,
+} HV_MESSAGE_TYPE;
+
+typedef struct HV_MESSAGE_FLAGS {
+    uint8_t MessagePending:1;
+    uint8_t Reserved:7;
+} HV_MESSAGE_FLAGS;
+
+typedef struct HV_MESSAGE_HEADER {
+    HV_MESSAGE_TYPE MessageType;
Quoted 9 lines

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 51

Quoted 8 lines
xen/arch/x86/hvm/viridian/synic.c +8 -0
+    uint16_t Reserved1;
+    HV_MESSAGE_FLAGS MessageFlags;
+    uint8_t PayloadSize;
+    uint64_t Reserved2;
+} HV_MESSAGE_HEADER;
+
+#define HV_MESSAGE_SIZE 256
+#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT 30

Missed this one before...

Quoted 9 lines

Is this defined this way, or (given ...

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 56

Quoted 4 lines
xen/arch/x86/hvm/viridian/synic.c +4 -0
+typedef struct HV_MESSAGE {
+    HV_MESSAGE_HEADER Header;
+    uint64_t Payload[HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT];
+} HV_MESSAGE;

... this) isn't it rather

Quoted 3 lines

#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT \
((HV_MESSAGE_SIZE - sizeof(HV_MESSAGE_HEADER) / 8)

I need the definition for the array in the struct so that sizeof(HV_MESSAGE) == HV_MESSAGE_SIZE (for which there is a BUILD_BUG_ON()) later. It's also written that way in the spec. so I'd rather leave it as-is.

Paul

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#20 lore raw

Re: [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 56 lines
Quoted 3 lines
Quoted 1 line · On 13.03.19 at 17:13, <[email protected]> wrote:

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of

Jan Beulich

Quoted 3 lines

Sent: 13 March 2019 13:15
To: Paul Durrant <[email protected]>
Cc: Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>;

Konrad Rzeszutek Wilk

Quoted 1 line

<[email protected]>; George Dunlap <[email protected]>; Andrew

Cooper

Quoted 1 line

<[email protected]>; Ian Jackson <[email protected]>; Tim (Xen.org)

<[email protected]>; Julien

Quoted 1 line

Grall <[email protected]>; xen-devel <[email protected]>; Roger

Pau Monne

Quoted 2 lines

<[email protected]>
Subject: Re: [Xen-devel] [PATCH v5 09/11] viridian: add implementation of

synthetic interrupt MSRs

Quoted 18 lines
Quoted 18 lines
Quoted 2 lines
Quoted 2 lines · On 11.03.19 at 14:41, <[email protected]> wrote:
xen/arch/x86/hvm/viridian/synic.c +12 -0
@@ -28,6 +29,32 @@ typedef union _HV_VP_ASSIST_PAGE
     uint8_t ReservedZBytePadding[PAGE_SIZE];
 } HV_VP_ASSIST_PAGE;
 
+typedef enum HV_MESSAGE_TYPE {
+    HvMessageTypeNone,
+    HvMessageTimerExpired = 0x80000010,
+} HV_MESSAGE_TYPE;
+
+typedef struct HV_MESSAGE_FLAGS {
+    uint8_t MessagePending:1;
+    uint8_t Reserved:7;
+} HV_MESSAGE_FLAGS;
+
+typedef struct HV_MESSAGE_HEADER {
+    HV_MESSAGE_TYPE MessageType;
Quoted 9 lines

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 51

Quoted 8 lines
xen/arch/x86/hvm/viridian/synic.c +8 -0
+    uint16_t Reserved1;
+    HV_MESSAGE_FLAGS MessageFlags;
+    uint8_t PayloadSize;
+    uint64_t Reserved2;
+} HV_MESSAGE_HEADER;
+
+#define HV_MESSAGE_SIZE 256
+#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT 30

Missed this one before...

Quoted 9 lines

Is this defined this way, or (given ...

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 56

Quoted 4 lines
xen/arch/x86/hvm/viridian/synic.c +4 -0
+typedef struct HV_MESSAGE {
+    HV_MESSAGE_HEADER Header;
+    uint64_t Payload[HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT];
+} HV_MESSAGE;

... this) isn't it rather

Quoted 6 lines
Quoted 3 lines

#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT \
((HV_MESSAGE_SIZE - sizeof(HV_MESSAGE_HEADER) / 8)

I need the definition for the array in the struct so that sizeof(HV_MESSAGE) == HV_MESSAGE_SIZE (for which there is a BUILD_BUG_ON()) later.

I don't understand this part - I'm not asking to ditch the #define. As to the BUILD_BUG_ON() - I see now, but that's only in patch 10, and in a specific message handler. I think this would belong here, and in the main viridian.c file.

Quoted 1 line

It's also written that way in the spec. so I'd rather leave it as-is.

Well, okay then - I was sort of expecting this to be spelled out there in such a way. That doesn't change my overall opinion, but I can see your point of wanting to match the spec, and you being the maintainer I have no basis to insist anyway.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#21 lore raw

Re: [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 71 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 14 March 2019 07:48
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Roger Pau Monne <[email protected]>; Wei Liu <[email protected]>; Stefano Stabellini <[email protected]>; xen-devel <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: RE: [Xen-devel] [PATCH v5 09/11] viridian: add implementation of synthetic interrupt MSRs

Quoted 60 lines
Quoted 3 lines
Quoted 1 line · On 13.03.19 at 17:13, <[email protected]> wrote:

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of

Jan Beulich

Quoted 3 lines

Sent: 13 March 2019 13:15
To: Paul Durrant <[email protected]>
Cc: Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>;

Konrad Rzeszutek Wilk

Quoted 1 line

<[email protected]>; George Dunlap <[email protected]>; Andrew

Cooper

Quoted 1 line

<[email protected]>; Ian Jackson <[email protected]>; Tim (Xen.org)

<[email protected]>; Julien

Quoted 1 line

Grall <[email protected]>; xen-devel <[email protected]>; Roger

Pau Monne

Quoted 2 lines

<[email protected]>
Subject: Re: [Xen-devel] [PATCH v5 09/11] viridian: add implementation of

synthetic interrupt MSRs

Quoted 18 lines
Quoted 18 lines
Quoted 2 lines
Quoted 2 lines · On 11.03.19 at 14:41, <[email protected]> wrote:
xen/arch/x86/hvm/viridian/synic.c +12 -0
@@ -28,6 +29,32 @@ typedef union _HV_VP_ASSIST_PAGE
     uint8_t ReservedZBytePadding[PAGE_SIZE];
 } HV_VP_ASSIST_PAGE;
 
+typedef enum HV_MESSAGE_TYPE {
+    HvMessageTypeNone,
+    HvMessageTimerExpired = 0x80000010,
+} HV_MESSAGE_TYPE;
+
+typedef struct HV_MESSAGE_FLAGS {
+    uint8_t MessagePending:1;
+    uint8_t Reserved:7;
+} HV_MESSAGE_FLAGS;
+
+typedef struct HV_MESSAGE_HEADER {
+    HV_MESSAGE_TYPE MessageType;
Quoted 9 lines

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 51

Quoted 8 lines
xen/arch/x86/hvm/viridian/synic.c +8 -0
+    uint16_t Reserved1;
+    HV_MESSAGE_FLAGS MessageFlags;
+    uint8_t PayloadSize;
+    uint64_t Reserved2;
+} HV_MESSAGE_HEADER;
+
+#define HV_MESSAGE_SIZE 256
+#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT 30

Missed this one before...

Quoted 12 lines

Is this defined this way, or (given ...

xen/arch/x86/hvm/viridian/synic.c typedef union _HV_VP_ASSIST_PAGE · line 56

Quoted 4 lines
xen/arch/x86/hvm/viridian/synic.c +4 -0
+typedef struct HV_MESSAGE {
+    HV_MESSAGE_HEADER Header;
+    uint64_t Payload[HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT];
+} HV_MESSAGE;

... this) isn't it rather

#define HV_MESSAGE_MAX_PAYLOAD_QWORD_COUNT \
((HV_MESSAGE_SIZE - sizeof(HV_MESSAGE_HEADER) / 8)

Quoted 7 lines
Quoted 2 lines

I need the definition for the array in the struct so that sizeof(HV_MESSAGE) == HV_MESSAGE_SIZE (for which there is a BUILD_BUG_ON()) later.

I don't understand this part - I'm not asking to ditch the #define. As to the BUILD_BUG_ON() - I see now, but that's only in patch 10, and in a specific message handler. I think this would belong here, and in the main viridian.c file.

Ok, I can see that it is more logical to co-locate the BUILD_BUG_ON() with the definitions.

Paul

Quoted 10 lines
Quoted 2 lines

It's also written that way in the spec. so I'd rather leave it as-is.

Well, okay then - I was sort of expecting this to be spelled out there in such a way. That doesn't change my overall opinion, but I can see your point of wanting to match the spec, and you being the maintainer I have no basis to insist anyway.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#22 lore raw

PATCH v5 10/11 viridian: add implementation of synthetic timers

In reply to Paul Durrant

This patch introduces an implementation of the STIMER0-15_CONFIG/COUNT MSRs and hence a the first SynIC message source.

The new (and documented) 'stimer' viridian enlightenment group may be specified to enable this feature.

While in the neighbourhood, this patch adds a missing check for an attempt to write the time reference count MSR, which should result in an exception (but not be reported as an unimplemented MSR).

NOTE: It is necessary for correct operation that timer expiration and
message delivery time-stamping use the same time source as the guest. The specification is ambiguous but testing with a Windows 10 1803 guest has shown that using the partition reference counter as a source whilst the guest is using RDTSC and the reference tsc page does not work correctly. Therefore the time_now() function is used. This implements the algorithm for acquiring partition reference time that is documented in the specifiction.

---
Cc: Ian Jackson <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v5:
- Fix time_now() to read TSC as the guest would see it

v4:
- Address comments from Jan

v3:
- Re-worked missed ticks calculation

---
 docs/man/xl.cfg.5.pod.in               |  12 +-
 tools/libxl/libxl.h                    |   6 +
 tools/libxl/libxl_dom.c                |   4 +
 tools/libxl/libxl_types.idl            |   1 +
 xen/arch/x86/hvm/viridian/private.h    |   9 +-
 xen/arch/x86/hvm/viridian/synic.c      |  53 +++-
 xen/arch/x86/hvm/viridian/time.c       | 386 ++++++++++++++++++++++++-
 xen/arch/x86/hvm/viridian/viridian.c   |  19 ++
 xen/include/asm-x86/hvm/viridian.h     |  32 +-
 xen/include/public/arch-x86/hvm/save.h |   2 +
 xen/include/public/hvm/params.h        |   7 +-
 11 files changed, 523 insertions(+), 8 deletions(-)
docs/man/xl.cfg.5.pod.in +10 -2
index ad81af1ed8..355c654693 100644
@@ -2167,11 +2167,19 @@ This group incorporates the crash control MSRs. These enlightenments
 allow Windows to write crash information such that it can be logged
 by Xen.
 
+=item B<stimer>
+
+This set incorporates the SynIC and synthetic timer MSRs. Windows will
+use synthetic timers in preference to emulated HPET for a source of
+ticks and hence enabling this group will ensure that ticks will be
+consistent with use of an enlightened time source (B<time_ref_count> or
+B<reference_tsc>).
+
 =item B<defaults>
 
 This is a special value that enables the default set of groups, which
-is currently the B<base>, B<freq>, B<time_ref_count>, B<apic_assist>
-and B<crash_ctl> groups.
+is currently the B<base>, B<freq>, B<time_ref_count>, B<apic_assist>,
+B<crash_ctl> and B<stimer> groups.
 
 =item B<all>
 
tools/libxl/libxl.h +6 -0
index a923a380d3..c8f219b0d3 100644
@@ -324,6 +324,12 @@
  */
 #define LIBXL_HAVE_VIRIDIAN_SYNIC 1
 
+/*
+ * LIBXL_HAVE_VIRIDIAN_STIMER indicates that the 'stimer' value
+ * is present in the viridian enlightenment enumeration.
+ */
+#define LIBXL_HAVE_VIRIDIAN_STIMER 1
+
 /*
  * LIBXL_HAVE_BUILDINFO_HVM_ACPI_LAPTOP_SLATE indicates that
  * libxl_domain_build_info has the u.hvm.acpi_laptop_slate field.
tools/libxl/libxl_dom.c +4 -0
index fb758d2ac3..2ee0f82ee7 100644
@@ -269,6 +269,7 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,
         libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_TIME_REF_COUNT);
         libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_APIC_ASSIST);
         libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_CRASH_CTL);
+        libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_STIMER);
     }
 
     libxl_for_each_set_bit(v, info->u.hvm.viridian_enable) {
@@ -320,6 +321,9 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,
     if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_SYNIC))
         mask |= HVMPV_synic;
 
+    if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_STIMER))
+        mask |= HVMPV_time_ref_count | HVMPV_synic | HVMPV_stimer;
+
     if (mask != 0 &&
         xc_hvm_param_set(CTX->xch,
                          domid,
tools/libxl/libxl_types.idl +1 -0
index 9860bcaf5f..1cce249de4 100644
@@ -236,6 +236,7 @@ libxl_viridian_enlightenment = Enumeration("viridian_enlightenment", [
     (5, "apic_assist"),
     (6, "crash_ctl"),
     (7, "synic"),
+    (8, "stimer"),
     ])
 
 libxl_hdtype = Enumeration("hdtype", [
xen/arch/x86/hvm/viridian/private.h +8 -1
index 96a784b840..c272c34cda 100644
@@ -74,6 +74,11 @@
 int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val);
 int viridian_synic_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val);
 
+bool viridian_synic_deliver_timer_msg(struct vcpu *v, unsigned int sintx,
+                                      unsigned int index,
+                                      uint64_t expiration,
+                                      uint64_t delivery);
+
 int viridian_synic_vcpu_init(const struct vcpu *v);
 int viridian_synic_domain_init(const struct domain *d);
 
@@ -93,7 +98,9 @@ void viridian_synic_load_domain_ctxt(
 int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val);
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val);
 
-int viridian_time_vcpu_init(const struct vcpu *v);
+void viridian_time_poll_timers(struct vcpu *v);
+
+int viridian_time_vcpu_init(struct vcpu *v);
 int viridian_time_domain_init(const struct domain *d);
 
 void viridian_time_vcpu_deinit(const struct vcpu *v);
xen/arch/x86/hvm/viridian/synic.c +51 -2

60 lines not on this page · raw message

xen/arch/x86/hvm/viridian/time.c +385 -1

458 lines not on this page · raw message

xen/arch/x86/hvm/viridian/viridian.c +19 -0
index 67d0121924..ec79cd9f15 100644
@@ -181,6 +181,8 @@ void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf,
             mask.AccessPartitionReferenceTsc = 1;
         if ( viridian_feature_mask(d) & HVMPV_synic )
             mask.AccessSynicRegs = 1;
+        if ( viridian_feature_mask(d) & HVMPV_stimer )
+            mask.AccessSyntheticTimerRegs = 1;
 
         u.mask = mask;
 
@@ -322,6 +324,15 @@ int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val)
     case HV_X64_MSR_TSC_FREQUENCY:
     case HV_X64_MSR_APIC_FREQUENCY:
     case HV_X64_MSR_REFERENCE_TSC:
+    case HV_X64_MSR_TIME_REF_COUNT:
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    case HV_X64_MSR_STIMER3_COUNT:
         return viridian_time_wrmsr(v, idx, val);
 
     case HV_X64_MSR_CRASH_P0:
@@ -403,6 +414,14 @@ int guest_rdmsr_viridian(const struct vcpu *v, uint32_t idx, uint64_t *val)
     case HV_X64_MSR_APIC_FREQUENCY:
     case HV_X64_MSR_REFERENCE_TSC:
     case HV_X64_MSR_TIME_REF_COUNT:
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    case HV_X64_MSR_STIMER3_COUNT:
         return viridian_time_rdmsr(v, idx, val);
 
     case HV_X64_MSR_CRASH_P0:
xen/include/asm-x86/hvm/viridian.h +31 -1
index 7446be492b..c9cab32e06 100644
@@ -40,6 +40,33 @@ union viridian_sint_msr
     };
 };
 
+union viridian_stimer_config_msr
+{
+    uint64_t raw;
+    struct
+    {
+        uint64_t enabled:1;
+        uint64_t periodic:1;
+        uint64_t lazy:1;
+        uint64_t auto_enable:1;
+        uint64_t vector:8;
+        uint64_t direct_mode:1;
+        uint64_t reserved_zero1:3;
+        uint64_t sintx:4;
+        uint64_t reserved_zero2:44;
+    } fields;
+};
+
+struct viridian_stimer {
+    struct vcpu *v;
+    struct timer timer;
+    union viridian_stimer_config_msr config;
+    uint64_t count;
+    uint64_t expiration;
+    s_time_t timeout;
+    bool started;
+};
+
 struct viridian_vcpu
 {
     struct viridian_page vp_assist;
@@ -50,6 +77,9 @@ struct viridian_vcpu
     union viridian_sint_msr sint[16];
     uint8_t vector_to_sintx[256];
     unsigned long msg_pending;
+    struct viridian_stimer stimer[4];
+    unsigned long stimer_enabled;
+    unsigned long stimer_pending;
     uint64_t crash_param[5];
 };
 
@@ -112,7 +142,7 @@ void viridian_apic_assist_clear(const struct vcpu *v);
 
 bool viridian_synic_is_auto_eoi_sint(const struct vcpu *v,
                                      unsigned int vector);
-void viridian_synic_poll_messages(const struct vcpu *v);
+void viridian_synic_poll_messages(struct vcpu *v);
 void viridian_synic_ack_sint(const struct vcpu *v, unsigned int vector);
 
 #endif /* __ASM_X86_HVM_VIRIDIAN_H__ */
xen/include/public/arch-x86/hvm/save.h +2 -0
index ec3e4df12c..8344aa471f 100644
@@ -604,6 +604,8 @@ struct hvm_viridian_vcpu_context {
     uint8_t  _pad[7];
     uint64_t simp_msr;
     uint64_t sint_msr[16];
+    uint64_t stimer_config_msr[4];
+    uint64_t stimer_count_msr[4];
 };
 
 DECLARE_HVM_SAVE_TYPE(VIRIDIAN_VCPU, 17, struct hvm_viridian_vcpu_context);
xen/include/public/hvm/params.h +6 -1
index e7e3c7c892..e06b0942d0 100644
@@ -150,6 +150,10 @@
 #define _HVMPV_synic 7
 #define HVMPV_synic (1 << _HVMPV_synic)
 
+/* Enable STIMER MSRs */
+#define _HVMPV_stimer 8
+#define HVMPV_stimer (1 << _HVMPV_stimer)
+
 #define HVMPV_feature_mask \
         (HVMPV_base_freq | \
          HVMPV_no_freq | \
@@ -158,7 +162,8 @@
          HVMPV_hcall_remote_tlb_flush | \
          HVMPV_apic_assist | \
          HVMPV_crash_ctl | \
-         HVMPV_synic)
+         HVMPV_synic | \
+         HVMPV_stimer)
 
 #endif
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#23 lore raw

Re: [PATCH v5 10/11] viridian: add implementation of synthetic timers

Quoted 764 lines

-----Original Message-----
From: Paul Durrant [mailto:[email protected]]
Sent: 11 March 2019 13:42
To: [email protected]
Cc: Paul Durrant <[email protected]>; Wei Liu <[email protected]>; Ian Jackson <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Jan Beulich <[email protected]>; Julien Grall <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Stefano Stabellini <[email protected]>; Tim (Xen.org) <[email protected]>; Roger Pau Monne <[email protected]>
Subject: [PATCH v5 10/11] viridian: add implementation of synthetic timers

This patch introduces an implementation of the STIMER0-15_CONFIG/COUNT MSRs and hence a the first SynIC message source.

The new (and documented) 'stimer' viridian enlightenment group may be specified to enable this feature.

While in the neighbourhood, this patch adds a missing check for an attempt to write the time reference count MSR, which should result in an exception (but not be reported as an unimplemented MSR).

NOTE: It is necessary for correct operation that timer expiration and
message delivery time-stamping use the same time source as the guest. The specification is ambiguous but testing with a Windows 10 1803 guest has shown that using the partition reference counter as a source whilst the guest is using RDTSC and the reference tsc page does not work correctly. Therefore the time_now() function is used. This implements the algorithm for acquiring partition reference time that is documented in the specifiction.

---
Cc: Ian Jackson <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v5:
- Fix time_now() to read TSC as the guest would see it

v4:
- Address comments from Jan

v3:
- Re-worked missed ticks calculation

---
 docs/man/xl.cfg.5.pod.in               |  12 +-
 tools/libxl/libxl.h                    |   6 +
 tools/libxl/libxl_dom.c                |   4 +
 tools/libxl/libxl_types.idl            |   1 +
 xen/arch/x86/hvm/viridian/private.h    |   9 +-
 xen/arch/x86/hvm/viridian/synic.c      |  53 +++-
 xen/arch/x86/hvm/viridian/time.c       | 386 ++++++++++++++++++++++++-
 xen/arch/x86/hvm/viridian/viridian.c   |  19 ++
 xen/include/asm-x86/hvm/viridian.h     |  32 +-
 xen/include/public/arch-x86/hvm/save.h |   2 +
 xen/include/public/hvm/params.h        |   7 +-
 11 files changed, 523 insertions(+), 8 deletions(-)
docs/man/xl.cfg.5.pod.in +10 -2
index ad81af1ed8..355c654693 100644
@@ -2167,11 +2167,19 @@ This group incorporates the crash control MSRs. These enlightenments
 allow Windows to write crash information such that it can be logged
 by Xen.
 
+=item B<stimer>
+
+This set incorporates the SynIC and synthetic timer MSRs. Windows will
+use synthetic timers in preference to emulated HPET for a source of
+ticks and hence enabling this group will ensure that ticks will be
+consistent with use of an enlightened time source (B<time_ref_count> or
+B<reference_tsc>).
+
 =item B<defaults>
 
 This is a special value that enables the default set of groups, which
-is currently the B<base>, B<freq>, B<time_ref_count>, B<apic_assist>
-and B<crash_ctl> groups.
+is currently the B<base>, B<freq>, B<time_ref_count>, B<apic_assist>,
+B<crash_ctl> and B<stimer> groups.
 
 =item B<all>
 
tools/libxl/libxl.h +6 -0
index a923a380d3..c8f219b0d3 100644
@@ -324,6 +324,12 @@
  */
 #define LIBXL_HAVE_VIRIDIAN_SYNIC 1
 
+/*
+ * LIBXL_HAVE_VIRIDIAN_STIMER indicates that the 'stimer' value
+ * is present in the viridian enlightenment enumeration.
+ */
+#define LIBXL_HAVE_VIRIDIAN_STIMER 1
+
 /*
  * LIBXL_HAVE_BUILDINFO_HVM_ACPI_LAPTOP_SLATE indicates that
  * libxl_domain_build_info has the u.hvm.acpi_laptop_slate field.
tools/libxl/libxl_dom.c +4 -0
index fb758d2ac3..2ee0f82ee7 100644
@@ -269,6 +269,7 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,
         libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_TIME_REF_COUNT);
         libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_APIC_ASSIST);
         libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_CRASH_CTL);
+        libxl_bitmap_set(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_STIMER);
     }
 
     libxl_for_each_set_bit(v, info->u.hvm.viridian_enable) {
@@ -320,6 +321,9 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,
     if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_SYNIC))
         mask |= HVMPV_synic;
 
+    if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_STIMER))
+        mask |= HVMPV_time_ref_count | HVMPV_synic | HVMPV_stimer;
+
     if (mask != 0 &&
         xc_hvm_param_set(CTX->xch,
                          domid,
tools/libxl/libxl_types.idl +1 -0
index 9860bcaf5f..1cce249de4 100644
@@ -236,6 +236,7 @@ libxl_viridian_enlightenment = Enumeration("viridian_enlightenment", [
     (5, "apic_assist"),
     (6, "crash_ctl"),
     (7, "synic"),
+    (8, "stimer"),
     ])
 
 libxl_hdtype = Enumeration("hdtype", [
xen/arch/x86/hvm/viridian/private.h +8 -1
index 96a784b840..c272c34cda 100644
@@ -74,6 +74,11 @@
 int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val);
 int viridian_synic_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val);
 
+bool viridian_synic_deliver_timer_msg(struct vcpu *v, unsigned int sintx,
+                                      unsigned int index,
+                                      uint64_t expiration,
+                                      uint64_t delivery);
+
 int viridian_synic_vcpu_init(const struct vcpu *v);
 int viridian_synic_domain_init(const struct domain *d);
 
@@ -93,7 +98,9 @@ void viridian_synic_load_domain_ctxt(
 int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val);
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val);
 
-int viridian_time_vcpu_init(const struct vcpu *v);
+void viridian_time_poll_timers(struct vcpu *v);
+
+int viridian_time_vcpu_init(struct vcpu *v);
 int viridian_time_domain_init(const struct domain *d);
 
 void viridian_time_vcpu_deinit(const struct vcpu *v);
xen/arch/x86/hvm/viridian/synic.c +51 -2
index f4510d3829..b5f3a79556 100644
@@ -340,9 +340,58 @@ void viridian_synic_domain_deinit(const struct domain *d)
 {
 }
 
-void viridian_synic_poll_messages(const struct vcpu *v)
+void viridian_synic_poll_messages(struct vcpu *v)
 {
-    /* There are currently no message sources */
+    viridian_time_poll_timers(v);
+}
+
+bool viridian_synic_deliver_timer_msg(struct vcpu *v, unsigned int sintx,
+                                      unsigned int index,
+                                      uint64_t expiration,
+                                      uint64_t delivery)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    const union viridian_sint_msr *vs = &vv->sint[sintx];
+    HV_MESSAGE *msg = vv->simp.ptr;
+    struct {
+        uint32_t TimerIndex;
+        uint32_t Reserved;
+        uint64_t ExpirationTime;
+        uint64_t DeliveryTime;
+    } payload = {
+        .TimerIndex = index,
+        .ExpirationTime = expiration,
+        .DeliveryTime = delivery,
+    };
+
+    if ( test_bit(sintx, &vv->msg_pending) )
+        return false;
+
+    BUILD_BUG_ON(sizeof(*msg) != HV_MESSAGE_SIZE);
+    msg += sintx;
+
+    /*
+     * To avoid using an atomic test-and-set, and barrier before calling
+     * vlapic_set_irq(), this function must be called in context of the
+     * vcpu receiving the message.
+     */
+    ASSERT(v == current);
+    if ( msg->Header.MessageType != HvMessageTypeNone )
+    {
+        msg->Header.MessageFlags.MessagePending = 1;
+        __set_bit(sintx, &vv->msg_pending);
+        return false;
+    }
+
+    msg->Header.MessageType = HvMessageTimerExpired;
+    msg->Header.MessageFlags.MessagePending = 0;
+    msg->Header.PayloadSize = sizeof(payload);
+    memcpy(msg->Payload, &payload, sizeof(payload));
+
+    if ( !vs->mask )
+        vlapic_set_irq(vcpu_vlapic(v), vs->vector, 0);
+
+    return true;
 }
 
 bool viridian_synic_is_auto_eoi_sint(const struct vcpu *v,
xen/arch/x86/hvm/viridian/time.c +385 -1
index 71291d921c..12ce6c8f01 100644
@@ -12,6 +12,7 @@
 #include <xen/version.h>
 
 #include <asm/apic.h>
+#include <asm/event.h>
 #include <asm/hvm/support.h>
 
 #include "private.h"
@@ -72,6 +73,7 @@ static void update_reference_tsc(struct domain *d, bool initialize)
      * ticks per 100ns shifted left by 64.
      */
     p->TscScale = ((10000ul << 32) / d->arch.tsc_khz) << 32;
+    smp_wmb();
 
     p->TscSequence++;
     if ( p->TscSequence == 0xFFFFFFFF ||
@@ -118,18 +120,265 @@ static int64_t time_ref_count(const struct domain *d)
     return raw_trc_val(d) + trc->off;
 }
 
+/*
+ * The specification says: "The partition reference time is computed
+ * by the following formula:
+ *
+ * ReferenceTime = ((VirtualTsc * TscScale) >> 64) + TscOffset
+ *
+ * The multiplication is a 64 bit multiplication, which results in a
+ * 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)
+{
+    uint64_t result;
+
+    /*
+     * Quadword MUL takes an implicit operand in RAX, and puts the result
+     * in RDX:RAX. Because we only want the result of the multiplication
+     * after shifting right by 64 bits, we therefore only need the content
+     * of RDX.
+     */
+    asm ( "mulq %[scale]"
+          : "+a" (tsc), "=d" (result)
+          : [scale] "rm" (scale) );
+
+    return result + offset;
+}
+
+static uint64_t time_now(struct domain *d)
+{
+    const struct viridian_page *rt = &d->arch.hvm.viridian->reference_tsc;
+    HV_REFERENCE_TSC_PAGE *p = rt->ptr;
+    uint32_t start, end;
+    uint64_t tsc;
+    uint64_t scale;
+    uint64_t offset;
+
+    /*
+     * If the reference TSC page is not enabled, or has been invalidated
+     * fall back to the partition reference counter.
+     */
+    if ( !p || !p->TscSequence )
+        return time_ref_count(d);
+
+    /*
+     * The following sampling algorithm for tsc, scale and offset is
+     * documented in the specifiction.
+     */
+    do {
+        start = p->TscSequence;
+        smp_rmb();
+
+        tsc = hvm_get_guest_tsc(pt_global_vcpu_target(d));
+        scale = p->TscScale;
+        offset = p->TscOffset;
+
+        smp_rmb();
+        end = p->TscSequence;
+    } while (end != start);
+
+    return scale_tsc(tsc, scale, offset);
+}
+
+static void stop_stimer(struct viridian_stimer *vs)
+{
+    const struct vcpu *v = vs->v;
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int stimerx = vs - &vv->stimer[0];
+
+    if ( !vs->started )
+        return;
+
+    stop_timer(&vs->timer);
+    clear_bit(stimerx, &vv->stimer_pending);
+    vs->started = false;
+}
+
+static void stimer_expire(void *data)
+{
+    const struct viridian_stimer *vs = data;
+    struct vcpu *v = vs->v;
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int stimerx = vs - &vv->stimer[0];
+
+    if ( !vs->config.fields.enabled )
+        return;
+
+    set_bit(stimerx, &vv->stimer_pending);
+    vcpu_kick(v);
+}
+
+static void start_stimer(struct viridian_stimer *vs)
+{
+    const struct vcpu *v = vs->v;
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int stimerx = vs - &vv->stimer[0];
+    int64_t now = time_now(v->domain);
+    s_time_t timeout;
+
+    if ( !test_and_set_bit(stimerx, &vv->stimer_enabled) )
+        printk(XENLOG_G_INFO "%pv: VIRIDIAN STIMER%u: enabled\n", v,
+               stimerx);
+
+    if ( vs->config.fields.periodic )
+    {
+        unsigned int missed = 0;
+        int64_t next;
+
+        /*
+         * The specification says that if the timer is lazy then we
+         * skip over any missed expirations so we can treat this case
+         * as the same as if the timer is currently stopped, i.e. we
+         * just schedule expiration to be 'count' ticks from now.
+         */
+        if ( !vs->started || vs->config.fields.lazy )
+            next = now + vs->count;
+        else
+        {
+            /*
+             * The timer is already started, so we're re-scheduling.
+             * Hence advance the timer expiration by one tick.
+             */
+            next = vs->expiration + vs->count;
+
+            /* Now check to see if any expirations have been missed */
+            if ( now - next > 0 )
+                missed = (now - next) / vs->count;
+
+            /*
+             * The specification says that if the timer is not lazy then
+             * a non-zero missed count should be used to reduce the period
+             * of the timer until it catches up, unless the count has
+             * reached a 'significant number', in which case the timer
+             * should be treated as lazy. Unfortunately the specification
+             * does not state what that number is so the choice of number
+             * here is a pure guess.
+             */
+            if ( missed > 3 )
+            {
+                missed = 0;
+                next = now + vs->count;
+            }
+        }
+
+        vs->expiration = next;
+        timeout = ((next - now) * 100ull) / (missed + 1);
+    }
+    else
+    {
+        vs->expiration = vs->count;
+        if ( vs->count - now <= 0 )
+        {
+            set_bit(stimerx, &vv->stimer_pending);
+            return;
+        }
+
+        timeout = (vs->expiration - now) * 100ull;
+    }
+
+    vs->started = true;
+    migrate_timer(&vs->timer, smp_processor_id());
+    set_timer(&vs->timer, timeout + NOW());
+}
+
+static void poll_stimer(struct vcpu *v, unsigned int stimerx)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    struct viridian_stimer *vs = &vv->stimer[stimerx];
+
+    if ( !test_bit(stimerx, &vv->stimer_pending) )
+        return;
+
+    if ( !viridian_synic_deliver_timer_msg(v, vs->config.fields.sintx,
+                                           stimerx, vs->expiration,
+                                           time_now(v->domain)) )
+        return;
+
+    clear_bit(stimerx, &vv->stimer_pending);
+
+    if ( vs->config.fields.periodic )
+        start_stimer(vs);
+    else
+        vs->config.fields.enabled = 0;
+}
+
+void viridian_time_poll_timers(struct vcpu *v)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int i;
+
+    if ( !vv->stimer_pending )
+       return;
+
+    for ( i = 0; i < ARRAY_SIZE(vv->stimer); i++ )
+        poll_stimer(v, i);
+}
+
+void viridian_time_vcpu_freeze(struct vcpu *v)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int i;
+
+    if ( !is_viridian_vcpu(v) )
+        return;
+
+    for ( i = 0; i < ARRAY_SIZE(vv->stimer); i++ )
+    {
+        struct viridian_stimer *vs = &vv->stimer[i];
+
+        if ( vs->started )
+            stop_timer(&vs->timer);
+    }
+}
+
+void viridian_time_vcpu_thaw(struct vcpu *v)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int i;
+
+    if ( !is_viridian_vcpu(v) )
+        return;
+
+    for ( i = 0; i < ARRAY_SIZE(vv->stimer); i++ )
+    {
+        struct viridian_stimer *vs = &vv->stimer[i];
+
+        if ( vs->config.fields.enabled )
+            start_stimer(vs);
+    }
+}
+
 void viridian_time_domain_freeze(const struct domain *d)
 {
+    struct vcpu *v;
+
+    if ( !is_viridian_domain(d) )
+        return;
+
+    for_each_vcpu ( d, v )
+        viridian_time_vcpu_freeze(v);
+
     time_ref_count_freeze(d);
 }
 
 void viridian_time_domain_thaw(const struct domain *d)
 {
+    struct vcpu *v;
+
+    if ( !is_viridian_domain(d) )
+        return;
+
     time_ref_count_thaw(d);
+
+    for_each_vcpu ( d, v )
+        viridian_time_vcpu_thaw(v);
 }
 
 int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
     struct domain *d = v->domain;
     struct viridian_domain *vd = d->arch.hvm.viridian;
 
@@ -149,6 +398,63 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
         }
         break;
 
+    case HV_X64_MSR_TIME_REF_COUNT:
+        return X86EMUL_EXCEPTION;
+
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    {
+        unsigned int stimerx =
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));
+        struct viridian_stimer *vs = &vv->stimer[stimerx];
+
+        if ( !(viridian_feature_mask(d) & HVMPV_stimer) )
+            return X86EMUL_EXCEPTION;
+
+        stop_stimer(vs);
+
+        vs->config.raw = val;
+
+        if ( !vs->config.fields.sintx )
+            vs->config.fields.enabled = 0;
+
+        if ( vs->config.fields.enabled )
+            start_stimer(vs);
+
+        break;
+    }
+
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_COUNT:
+    {
+        unsigned int stimerx =
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));
+        struct viridian_stimer *vs = &vv->stimer[stimerx];
+
+        if ( !(viridian_feature_mask(d) & HVMPV_stimer) )
+            return X86EMUL_EXCEPTION;
+
+        stop_stimer(vs);
+
+        vs->count = val;
+
+        if ( !vs->count  )
+            vs->config.fields.enabled = 0;
+        else if ( vs->config.fields.auto_enable )
+            vs->config.fields.enabled = 1;
+
+        if ( vs->config.fields.enabled )
+            start_stimer(vs);
+
+        break;
+    }
+
     default:
         gdprintk(XENLOG_INFO, "%s: unimplemented MSR %#x (%016"PRIx64")\n",
                  __func__, idx, val);
@@ -160,6 +466,7 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
     const struct domain *d = v->domain;
     struct viridian_domain *vd = d->arch.hvm.viridian;
 
@@ -201,6 +508,37 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
         break;
     }
 
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    {
+        unsigned int stimerx =
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));;
+
+        if ( !(viridian_feature_mask(d) & HVMPV_stimer) )
+            return X86EMUL_EXCEPTION;
+
+        *val = vv->stimer[stimerx].config.raw;
+        break;
+    }
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_COUNT:
+    {
+        unsigned int stimerx =
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));;
+
+        if ( !(viridian_feature_mask(d) & HVMPV_stimer) )
+            return X86EMUL_EXCEPTION;
+
+        *val = vv->stimer[stimerx].count;
+        break;
+    }
+
     default:
         gdprintk(XENLOG_INFO, "%s: unimplemented MSR %#x\n", __func__, idx);
         return X86EMUL_EXCEPTION;
@@ -209,8 +547,19 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
     return X86EMUL_OKAY;
 }
 
-int viridian_time_vcpu_init(const struct vcpu *v)
+int viridian_time_vcpu_init(struct vcpu *v)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int i;
+
+    for ( i = 0; i < ARRAY_SIZE(vv->stimer); i++ )
+    {
+        struct viridian_stimer *vs = &vv->stimer[i];
+
+        vs->v = v;
+        init_timer(&vs->timer, stimer_expire, vs, v->processor);
+    }
+
     return 0;
 }
 
@@ -221,6 +570,16 @@ int viridian_time_domain_init(const struct domain *d)
 
 void viridian_time_vcpu_deinit(const struct vcpu *v)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int i;
+
+    for ( i = 0; i < ARRAY_SIZE(vv->stimer); i++ )
+    {
+        struct viridian_stimer *vs = &vv->stimer[i];
+
+        kill_timer(&vs->timer);
+        vs->v = NULL;
+    }
 }
 
 void viridian_time_domain_deinit(const struct domain *d)
@@ -231,11 +590,36 @@ void viridian_time_domain_deinit(const struct domain *d)
 void viridian_time_save_vcpu_ctxt(
     const struct vcpu *v, struct hvm_viridian_vcpu_context *ctxt)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int i;
+
+    BUILD_BUG_ON(ARRAY_SIZE(vv->stimer) !=
+                 ARRAY_SIZE(ctxt->stimer_config_msr));
+    BUILD_BUG_ON(ARRAY_SIZE(vv->stimer) !=
+                 ARRAY_SIZE(ctxt->stimer_count_msr));
+
+    for ( i = 0; i < ARRAY_SIZE(vv->stimer); i++ )
+    {
+        struct viridian_stimer *vs = &vv->stimer[i];
+
+        ctxt->stimer_config_msr[i] = vs->config.raw;
+        ctxt->stimer_count_msr[i] = vs->count;
+    }
 }
 
 void viridian_time_load_vcpu_ctxt(
     struct vcpu *v, const struct hvm_viridian_vcpu_context *ctxt)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int i;
+
+    for ( i = 0; i < ARRAY_SIZE(vv->stimer); i++ )
+    {
+        struct viridian_stimer *vs = &vv->stimer[i];
+
+        vs->config.raw = ctxt->stimer_config_msr[i];
+        vs->count = ctxt->stimer_count_msr[i];
+    }
 }
 
 void viridian_time_save_domain_ctxt(
xen/arch/x86/hvm/viridian/viridian.c +19 -0
index 67d0121924..ec79cd9f15 100644
@@ -181,6 +181,8 @@ void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf,
             mask.AccessPartitionReferenceTsc = 1;
         if ( viridian_feature_mask(d) & HVMPV_synic )
             mask.AccessSynicRegs = 1;
+        if ( viridian_feature_mask(d) & HVMPV_stimer )
+            mask.AccessSyntheticTimerRegs = 1;
 
         u.mask = mask;
 
@@ -322,6 +324,15 @@ int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val)
     case HV_X64_MSR_TSC_FREQUENCY:
     case HV_X64_MSR_APIC_FREQUENCY:
     case HV_X64_MSR_REFERENCE_TSC:
+    case HV_X64_MSR_TIME_REF_COUNT:
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    case HV_X64_MSR_STIMER3_COUNT:
         return viridian_time_wrmsr(v, idx, val);
 
     case HV_X64_MSR_CRASH_P0:
@@ -403,6 +414,14 @@ int guest_rdmsr_viridian(const struct vcpu *v, uint32_t idx, uint64_t *val)
     case HV_X64_MSR_APIC_FREQUENCY:
     case HV_X64_MSR_REFERENCE_TSC:
     case HV_X64_MSR_TIME_REF_COUNT:
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    case HV_X64_MSR_STIMER3_COUNT:
         return viridian_time_rdmsr(v, idx, val);
 
     case HV_X64_MSR_CRASH_P0:
xen/include/asm-x86/hvm/viridian.h +5 -0
index 7446be492b..c9cab32e06 100644
@@ -40,6 +40,33 @@ union viridian_sint_msr
     };
 };
 
+union viridian_stimer_config_msr
+{
+    uint64_t raw;
+    struct
+    {

xen/include/asm-x86/hvm/viridian.h union viridian_sint_msr · line 57

Quoted 10 lines
xen/include/asm-x86/hvm/viridian.h +10 -0
+        uint64_t enabled:1;
+        uint64_t periodic:1;
+        uint64_t lazy:1;
+        uint64_t auto_enable:1;
+        uint64_t vector:8;
+        uint64_t direct_mode:1;
+        uint64_t reserved_zero1:3;
+        uint64_t sintx:4;
+        uint64_t reserved_zero2:44;
+    } fields;

Looks like I missed this. I'll send a v6 with it removed.

Paul

Quoted 73 lines
xen/include/asm-x86/hvm/viridian.h +16 -1
+};
+
+struct viridian_stimer {
+    struct vcpu *v;
+    struct timer timer;
+    union viridian_stimer_config_msr config;
+    uint64_t count;
+    uint64_t expiration;
+    s_time_t timeout;
+    bool started;
+};
+
 struct viridian_vcpu
 {
     struct viridian_page vp_assist;
@@ -50,6 +77,9 @@ struct viridian_vcpu
     union viridian_sint_msr sint[16];
     uint8_t vector_to_sintx[256];
     unsigned long msg_pending;
+    struct viridian_stimer stimer[4];
+    unsigned long stimer_enabled;
+    unsigned long stimer_pending;
     uint64_t crash_param[5];
 };
 
@@ -112,7 +142,7 @@ void viridian_apic_assist_clear(const struct vcpu *v);
 
 bool viridian_synic_is_auto_eoi_sint(const struct vcpu *v,
                                      unsigned int vector);
-void viridian_synic_poll_messages(const struct vcpu *v);
+void viridian_synic_poll_messages(struct vcpu *v);
 void viridian_synic_ack_sint(const struct vcpu *v, unsigned int vector);
 
 #endif /* __ASM_X86_HVM_VIRIDIAN_H__ */
xen/include/public/arch-x86/hvm/save.h +2 -0
index ec3e4df12c..8344aa471f 100644
@@ -604,6 +604,8 @@ struct hvm_viridian_vcpu_context {
     uint8_t  _pad[7];
     uint64_t simp_msr;
     uint64_t sint_msr[16];
+    uint64_t stimer_config_msr[4];
+    uint64_t stimer_count_msr[4];
 };
 
 DECLARE_HVM_SAVE_TYPE(VIRIDIAN_VCPU, 17, struct hvm_viridian_vcpu_context);
xen/include/public/hvm/params.h +6 -2
index e7e3c7c892..e06b0942d0 100644
@@ -150,6 +150,10 @@
 #define _HVMPV_synic 7
 #define HVMPV_synic (1 << _HVMPV_synic)
 
+/* Enable STIMER MSRs */
+#define _HVMPV_stimer 8
+#define HVMPV_stimer (1 << _HVMPV_stimer)
+
 #define HVMPV_feature_mask \
         (HVMPV_base_freq | \
          HVMPV_no_freq | \
@@ -158,7 +162,8 @@
          HVMPV_hcall_remote_tlb_flush | \
          HVMPV_apic_assist | \
          HVMPV_crash_ctl | \
-         HVMPV_synic)
+         HVMPV_synic | \
+         HVMPV_stimer)
 
 #endif
 
--
Quoted 1 line

2.20.1

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#24 lore raw

Re: [PATCH v5 10/11] viridian: add implementation of synthetic timers

Quoted 19 lines
Quoted 19 lines
Quoted 1 line · On 13.03.19 at 14:06, <[email protected]> wrote:

From: Paul Durrant [mailto:[email protected]]
Sent: 11 March 2019 13:42

xen/include/asm-x86/hvm/viridian.h +9 -0
@@ -40,6 +40,33 @@ union viridian_sint_msr
     };
 };
 
+union viridian_stimer_config_msr
+{
+    uint64_t raw;
+    struct
+    {
+        uint64_t enabled:1;
+        uint64_t periodic:1;
+        uint64_t lazy:1;
+        uint64_t auto_enable:1;
Quoted 8 lines

xen/include/asm-x86/hvm/viridian.h union viridian_sint_msr · line 57

Quoted 6 lines
xen/include/asm-x86/hvm/viridian.h +6 -0
+        uint64_t vector:8;
+        uint64_t direct_mode:1;
+        uint64_t reserved_zero1:3;
+        uint64_t sintx:4;
+        uint64_t reserved_zero2:44;
+    } fields;

Looks like I missed this. I'll send a v6 with it removed.

Oh, and I didn't really notice.

But the real reason for my reply: Please trim the quoting in your replies.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#25 lore raw

Re: [PATCH v5 10/11] viridian: add implementation of synthetic timers

In reply to Paul Durrant
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:
xen/arch/x86/hvm/viridian/synic.c +32 -0
+bool viridian_synic_deliver_timer_msg(struct vcpu *v, unsigned int sintx,
+                                      unsigned int index,
+                                      uint64_t expiration,
+                                      uint64_t delivery)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    const union viridian_sint_msr *vs = &vv->sint[sintx];
+    HV_MESSAGE *msg = vv->simp.ptr;
+    struct {
+        uint32_t TimerIndex;
+        uint32_t Reserved;
+        uint64_t ExpirationTime;
+        uint64_t DeliveryTime;
+    } payload = {
+        .TimerIndex = index,
+        .ExpirationTime = expiration,
+        .DeliveryTime = delivery,
+    };
+
+    if ( test_bit(sintx, &vv->msg_pending) )
+        return false;
+
+    BUILD_BUG_ON(sizeof(*msg) != HV_MESSAGE_SIZE);
+    msg += sintx;
+
+    /*
+     * To avoid using an atomic test-and-set, and barrier before calling
+     * vlapic_set_irq(), this function must be called in context of the
+     * vcpu receiving the message.
+     */
+    ASSERT(v == current);
+    if ( msg->Header.MessageType != HvMessageTypeNone )

xen/arch/x86/hvm/viridian/synic.c void viridian_synic_domain_deinit(const struct domain *d) · line 389

Quoted 10 lines
xen/arch/x86/hvm/viridian/synic.c +10 -0
+    {
+        msg->Header.MessageFlags.MessagePending = 1;
+        __set_bit(sintx, &vv->msg_pending);
+        return false;
+    }
+
+    msg->Header.MessageType = HvMessageTimerExpired;
+    msg->Header.MessageFlags.MessagePending = 0;
+    msg->Header.PayloadSize = sizeof(payload);
+    memcpy(msg->Payload, &payload, sizeof(payload));

Since you can't use plain assignment here, how about a BUILD_BUG_ON(sizeof(payload) <= sizeof(msg->payload))?

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 157

Quoted 8 lines
xen/arch/x86/hvm/viridian/time.c +8 -0
+static uint64_t time_now(struct domain *d)
+{
+    const struct viridian_page *rt = &d->arch.hvm.viridian->reference_tsc;
+    HV_REFERENCE_TSC_PAGE *p = rt->ptr;
+    uint32_t start, end;
+    uint64_t tsc;
+    uint64_t scale;
+    uint64_t offset;

Would mind grouping them all on one line, just like you do for start and end?

Quoted 12 lines
xen/arch/x86/hvm/viridian/time.c +12 -0
+    /*
+     * If the reference TSC page is not enabled, or has been invalidated
+     * fall back to the partition reference counter.
+     */
+    if ( !p || !p->TscSequence )
+        return time_ref_count(d);
+
+    /*
+     * The following sampling algorithm for tsc, scale and offset is
+     * documented in the specifiction.
+     */
+    do {

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 180

Quoted 10 lines
xen/arch/x86/hvm/viridian/time.c +10 -0
+        start = p->TscSequence;
+        smp_rmb();
+
+        tsc = hvm_get_guest_tsc(pt_global_vcpu_target(d));
+        scale = p->TscScale;
+        offset = p->TscOffset;
+
+        smp_rmb();
+        end = p->TscSequence;
+    } while (end != start);

Blanks immediately inside the parentheses please.

As to safety of this, I have two concerns:

1) TscSequence gets updated as a result of a guest action (an MSR write). This makes it non-obvious that the loop above will get exited in due course.

2) The way update_reference_tsc() deals with the two "invalid" values suggests ~0 and 0 should be special cased in general. I _think_ this is not necessary here, but it also seems to me as if the VM ever having a way to observe either of those two values would be wrong too. Shouldn't the function avoid to ever store ~0 into that field, i.e. increment into a local variable, update that local variable to skip the two "invalid" values, and only then store into the field?

Otoh, making it into that function being a result of an MSR write, it may welll be that the spec precludes the guest from reading the reference page while an update was invoked from one of its vCPU-s. If this was the case, then I also wouldn't have to wonder any longer how this entire mechanism can be race free in the first place (without a double increment like we do in the pv-clock protocol).

Quoted 45 lines
xen/arch/x86/hvm/viridian/time.c +45 -0
+static void start_stimer(struct viridian_stimer *vs)
+{
+    const struct vcpu *v = vs->v;
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int stimerx = vs - &vv->stimer[0];
+    int64_t now = time_now(v->domain);
+    s_time_t timeout;
+
+    if ( !test_and_set_bit(stimerx, &vv->stimer_enabled) )
+        printk(XENLOG_G_INFO "%pv: VIRIDIAN STIMER%u: enabled\n", v,
+               stimerx);
+
+    if ( vs->config.fields.periodic )
+    {
+        unsigned int missed = 0;
+        int64_t next;
+
+        /*
+         * The specification says that if the timer is lazy then we
+         * skip over any missed expirations so we can treat this case
+         * as the same as if the timer is currently stopped, i.e. we
+         * just schedule expiration to be 'count' ticks from now.
+         */
+        if ( !vs->started || vs->config.fields.lazy )
+            next = now + vs->count;
+        else
+        {
+            /*
+             * The timer is already started, so we're re-scheduling.
+             * Hence advance the timer expiration by one tick.
+             */
+            next = vs->expiration + vs->count;
+
+            /* Now check to see if any expirations have been missed */
+            if ( now - next > 0 )
+                missed = (now - next) / vs->count;
+
+            /*
+             * The specification says that if the timer is not lazy then
+             * a non-zero missed count should be used to reduce the period
+             * of the timer until it catches up, unless the count has
+             * reached a 'significant number', in which case the timer
+             * should be treated as lazy. Unfortunately the specification
+             * does not state what that number is so the choice of number
+             * here is a pure guess.

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 267

Quoted 10 lines
xen/arch/x86/hvm/viridian/time.c +10 -0
+             */
+            if ( missed > 3 )
+            {
+                missed = 0;
+                next = now + vs->count;
+            }
+        }
+
+        vs->expiration = next;
+        timeout = ((next - now) * 100ull) / (missed + 1);

The logic above guarantees next > now only if missed > 3. Yet with next < now, the multiplication by 100ull will produce a huge 64-bit value, and division by a value larger than 1 will make it a huge 62- or 63-bit value (sign bit lost). That's hardly the timeout you mean.

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 272

Quoted 5 lines
xen/arch/x86/hvm/viridian/time.c +5 -0
+    }
+    else
+    {
+        vs->expiration = vs->count;
+        if ( vs->count - now <= 0 )

Unless you really mean != 0 you have an issue here, due to vs->count being uint64_t. Perhaps, taking also ...

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 278

Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +6 -0
+        {
+            set_bit(stimerx, &vv->stimer_pending);
+            return;
+        }
+
+        timeout = (vs->expiration - now) * 100ull;

... this, you want to calculate the difference into a local variable of type int64_t instead, and use it in both places?

Quoted 4 lines
xen/arch/x86/hvm/viridian/time.c +4 -0
+static void poll_stimer(struct vcpu *v, unsigned int stimerx)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    struct viridian_stimer *vs = &vv->stimer[stimerx];

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 299

Quoted 10 lines
xen/arch/x86/hvm/viridian/time.c +10 -0
+
+    if ( !test_bit(stimerx, &vv->stimer_pending) )
+        return;
+
+    if ( !viridian_synic_deliver_timer_msg(v, vs->config.fields.sintx,
+                                           stimerx, vs->expiration,
+                                           time_now(v->domain)) )
+        return;
+
+    clear_bit(stimerx, &vv->stimer_pending);

While perhaps benign, wouldn't it be better to clear the pending bit before delivering the message?

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 325

Quoted 7 lines
xen/arch/x86/hvm/viridian/time.c +7 -0
+void viridian_time_vcpu_freeze(struct vcpu *v)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int i;
+
+    if ( !is_viridian_vcpu(v) )
+        return;

Don't you also want/need to check the HVMPV_stimer bit here (and the also in the thaw counterpart)?

Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +2 -0
@@ -149,6 +398,63 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
         }
         break;
 
+    case HV_X64_MSR_TIME_REF_COUNT:
+        return X86EMUL_EXCEPTION;

xen/arch/x86/hvm/viridian/time.c int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 412

Quoted 10 lines
xen/arch/x86/hvm/viridian/time.c +10 -0
+
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    {
+        unsigned int stimerx =
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));
+        struct viridian_stimer *vs = &vv->stimer[stimerx];

I again think you'd better use array_access_nospec() here (also for the rdmsr counterparts).

xen/arch/x86/hvm/viridian/time.c int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 469

Quoted 5 lines
xen/arch/x86/hvm/viridian/time.c +1 -0
@@ -160,6 +466,7 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;

const?

Quoted 11 lines
xen/arch/x86/hvm/viridian/time.c +6 -0
@@ -201,6 +508,37 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t 
 idx, uint64_t *val)
         break;
     }
 
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    {
+        unsigned int stimerx =

xen/arch/x86/hvm/viridian/time.c int viridian_time_rdmsr(const struct vcpu *v, uint32_t · line 527

Quoted 10 lines
xen/arch/x86/hvm/viridian/time.c +10 -0
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));;
+
+        if ( !(viridian_feature_mask(d) & HVMPV_stimer) )
+            return X86EMUL_EXCEPTION;
+
+        *val = vv->stimer[stimerx].config.raw;
+        break;
+    }
+    case HV_X64_MSR_STIMER0_COUNT:

Blank line between case blocks please.

xen/arch/x86/hvm/viridian/time.c void viridian_time_domain_deinit(const struct domain *d) · line 593

Quoted 5 lines
xen/arch/x86/hvm/viridian/time.c +1 -0
@@ -231,11 +590,36 @@ void viridian_time_domain_deinit(const struct domain *d)
 void viridian_time_save_vcpu_ctxt(
     const struct vcpu *v, struct hvm_viridian_vcpu_context *ctxt)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;

const?

xen/arch/x86/hvm/viridian/time.c void viridian_time_domain_deinit(const struct domain *d) · line 603

Quoted 10 lines
xen/arch/x86/hvm/viridian/time.c +10 -0
+    unsigned int i;
+
+    BUILD_BUG_ON(ARRAY_SIZE(vv->stimer) !=
+                 ARRAY_SIZE(ctxt->stimer_config_msr));
+    BUILD_BUG_ON(ARRAY_SIZE(vv->stimer) !=
+                 ARRAY_SIZE(ctxt->stimer_count_msr));
+
+    for ( i = 0; i < ARRAY_SIZE(vv->stimer); i++ )
+    {
+        struct viridian_stimer *vs = &vv->stimer[i];

const (if you really consider the variable useful in the first place)?

xen/arch/x86/hvm/viridian/viridian.c int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val) · line 335

Quoted 13 lines
xen/arch/x86/hvm/viridian/viridian.c +9 -0
@@ -322,6 +324,15 @@ int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val)
     case HV_X64_MSR_TSC_FREQUENCY:
     case HV_X64_MSR_APIC_FREQUENCY:
     case HV_X64_MSR_REFERENCE_TSC:
+    case HV_X64_MSR_TIME_REF_COUNT:
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    case HV_X64_MSR_STIMER3_COUNT:

For readability / brevity

    case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:

?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#26 lore raw

Re: [PATCH v5 10/11] viridian: add implementation of synthetic timers

Quoted 49 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 13 March 2019 14:06
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monne <[email protected]>; Wei Liu <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Stefano Stabellini <[email protected]>; xen-devel <xen- [email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: Re: [PATCH v5 10/11] viridian: add implementation of synthetic timers

Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:
xen/arch/x86/hvm/viridian/synic.c +37 -0
+bool viridian_synic_deliver_timer_msg(struct vcpu *v, unsigned int sintx,
+                                      unsigned int index,
+                                      uint64_t expiration,
+                                      uint64_t delivery)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    const union viridian_sint_msr *vs = &vv->sint[sintx];
+    HV_MESSAGE *msg = vv->simp.ptr;
+    struct {
+        uint32_t TimerIndex;
+        uint32_t Reserved;
+        uint64_t ExpirationTime;
+        uint64_t DeliveryTime;
+    } payload = {
+        .TimerIndex = index,
+        .ExpirationTime = expiration,
+        .DeliveryTime = delivery,
+    };
+
+    if ( test_bit(sintx, &vv->msg_pending) )
+        return false;
+
+    BUILD_BUG_ON(sizeof(*msg) != HV_MESSAGE_SIZE);
+    msg += sintx;
+
+    /*
+     * To avoid using an atomic test-and-set, and barrier before calling
+     * vlapic_set_irq(), this function must be called in context of the
+     * vcpu receiving the message.
+     */
+    ASSERT(v == current);
+    if ( msg->Header.MessageType != HvMessageTypeNone )
+    {
+        msg->Header.MessageFlags.MessagePending = 1;
+        __set_bit(sintx, &vv->msg_pending);
+        return false;
+    }
Quoted 8 lines

xen/arch/x86/hvm/viridian/synic.c void viridian_synic_domain_deinit(const struct domain *d) · line 389

Quoted 5 lines
xen/arch/x86/hvm/viridian/synic.c +5 -0
+
+    msg->Header.MessageType = HvMessageTimerExpired;
+    msg->Header.MessageFlags.MessagePending = 0;
+    msg->Header.PayloadSize = sizeof(payload);
+    memcpy(msg->Payload, &payload, sizeof(payload));

Since you can't use plain assignment here, how about a BUILD_BUG_ON(sizeof(payload) <= sizeof(msg->payload))?

Surely '>' rather than '<='?

Quoted 12 lines

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 157

Quoted 9 lines
xen/arch/x86/hvm/viridian/time.c +8 -0
+static uint64_t time_now(struct domain *d)
+{
+    const struct viridian_page *rt = &d->arch.hvm.viridian->reference_tsc;
+    HV_REFERENCE_TSC_PAGE *p = rt->ptr;
+    uint32_t start, end;
+    uint64_t tsc;
+    uint64_t scale;
+    uint64_t offset;

Would mind grouping them all on one line, just like you do for start and end?

Sure.

Quoted 17 lines
Quoted 17 lines
xen/arch/x86/hvm/viridian/time.c +16 -0
+    /*
+     * If the reference TSC page is not enabled, or has been invalidated
+     * fall back to the partition reference counter.
+     */
+    if ( !p || !p->TscSequence )
+        return time_ref_count(d);
+
+    /*
+     * The following sampling algorithm for tsc, scale and offset is
+     * documented in the specifiction.
+     */
+    do {
+        start = p->TscSequence;
+        smp_rmb();
+
+        tsc = hvm_get_guest_tsc(pt_global_vcpu_target(d));
Quoted 9 lines

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 180

Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +6 -0
+        scale = p->TscScale;
+        offset = p->TscOffset;
+
+        smp_rmb();
+        end = p->TscSequence;
+    } while (end != start);

Blanks immediately inside the parentheses please.

Yes, missed that.

Quoted 6 lines

As to safety of this, I have two concerns:

1) TscSequence gets updated as a result of a guest action (an MSR write). This makes it non-obvious that the loop above will get exited in due course.

True. The domain could try to DoS this call. This could be avoided by doing a domain_pause() if we test continuously fails for a number of iterations, or maybe just one iteration.

Quoted 9 lines

2) The way update_reference_tsc() deals with the two "invalid" values suggests ~0 and 0 should be special cased in general. I _think_ this is not necessary here, but it also seems to me as if the VM ever having a way to observe either of those two values would be wrong too. Shouldn't the function avoid to ever store ~0 into that field, i.e. increment into a local variable, update that local variable to skip the two "invalid" values, and only then store into the field?

Quoted 7 lines

Otoh, making it into that function being a result of an MSR write, it may welll be that the spec precludes the guest from reading the reference page while an update was invoked from one of its vCPU-s. If this was the case, then I also wouldn't have to wonder any longer how this entire mechanism can be race free in the first place (without a double increment like we do in the pv-clock protocol).

From observation, it looks like Windows initializes the reference tsc page before it brings secondary CPUs online and then doesn't touch the MSR again, so we should probably only tolerate one mismatch in time_now() before doing domain_pause(). I also agree that the 'invalid' values should not be exposed to the guest, even though it is highly unlikely to ever observe one in practice.

Quoted 54 lines
Quoted 54 lines
xen/arch/x86/hvm/viridian/time.c +53 -0
+static void start_stimer(struct viridian_stimer *vs)
+{
+    const struct vcpu *v = vs->v;
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int stimerx = vs - &vv->stimer[0];
+    int64_t now = time_now(v->domain);
+    s_time_t timeout;
+
+    if ( !test_and_set_bit(stimerx, &vv->stimer_enabled) )
+        printk(XENLOG_G_INFO "%pv: VIRIDIAN STIMER%u: enabled\n", v,
+               stimerx);
+
+    if ( vs->config.fields.periodic )
+    {
+        unsigned int missed = 0;
+        int64_t next;
+
+        /*
+         * The specification says that if the timer is lazy then we
+         * skip over any missed expirations so we can treat this case
+         * as the same as if the timer is currently stopped, i.e. we
+         * just schedule expiration to be 'count' ticks from now.
+         */
+        if ( !vs->started || vs->config.fields.lazy )
+            next = now + vs->count;
+        else
+        {
+            /*
+             * The timer is already started, so we're re-scheduling.
+             * Hence advance the timer expiration by one tick.
+             */
+            next = vs->expiration + vs->count;
+
+            /* Now check to see if any expirations have been missed */
+            if ( now - next > 0 )
+                missed = (now - next) / vs->count;
+
+            /*
+             * The specification says that if the timer is not lazy then
+             * a non-zero missed count should be used to reduce the period
+             * of the timer until it catches up, unless the count has
+             * reached a 'significant number', in which case the timer
+             * should be treated as lazy. Unfortunately the specification
+             * does not state what that number is so the choice of number
+             * here is a pure guess.
+             */
+            if ( missed > 3 )
+            {
+                missed = 0;
+                next = now + vs->count;
+            }
+        }
+
Quoted 8 lines

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 267

Quoted 2 lines
xen/arch/x86/hvm/viridian/time.c +2 -0
+        vs->expiration = next;
+        timeout = ((next - now) * 100ull) / (missed + 1);

The logic above guarantees next > now only if missed > 3. Yet with next < now, the multiplication by 100ull will produce a huge 64-bit value, and division by a value larger than 1 will make it a huge 62- or 63-bit value (sign bit lost). That's hardly the timeout you mean.

Indeed, I'll need to re-think that.

Quoted 17 lines

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 272

Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +5 -0
+    }
+    else
+    {
+        vs->expiration = vs->count;
+        if ( vs->count - now <= 0 )

Unless you really mean != 0 you have an issue here, due to vs->count being uint64_t. Perhaps, taking also ...

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 278

Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +6 -0
+        {
+            set_bit(stimerx, &vv->stimer_pending);
+            return;
+        }
+
+        timeout = (vs->expiration - now) * 100ull;
Quoted 2 lines

... this, you want to calculate the difference into a local variable of type int64_t instead, and use it in both places?

Yes, this was broken when expiration and now became unsigned.

Quoted 10 lines
Quoted 10 lines
xen/arch/x86/hvm/viridian/time.c +9 -0
+static void poll_stimer(struct vcpu *v, unsigned int stimerx)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    struct viridian_stimer *vs = &vv->stimer[stimerx];
+
+    if ( !test_bit(stimerx, &vv->stimer_pending) )
+        return;
+
+    if ( !viridian_synic_deliver_timer_msg(v, vs->config.fields.sintx,
Quoted 8 lines

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 299

Quoted 5 lines
xen/arch/x86/hvm/viridian/time.c +5 -0
+                                           stimerx, vs->expiration,
+                                           time_now(v->domain)) )
+        return;
+
+    clear_bit(stimerx, &vv->stimer_pending);

While perhaps benign, wouldn't it be better to clear the pending bit before delivering the message?

No, because I only want to clear it if the delivery is successful.

Quoted 11 lines

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 325

Quoted 8 lines
xen/arch/x86/hvm/viridian/time.c +7 -0
+void viridian_time_vcpu_freeze(struct vcpu *v)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    unsigned int i;
+
+    if ( !is_viridian_vcpu(v) )
+        return;

Don't you also want/need to check the HVMPV_stimer bit here (and the also in the thaw counterpart)?

Yes, I probably do.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +0 -0
@@ -149,6 +398,63 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
         }
         break;
 
Quoted 15 lines

xen/arch/x86/hvm/viridian/time.c int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 412

Quoted 12 lines
xen/arch/x86/hvm/viridian/time.c +12 -0
+    case HV_X64_MSR_TIME_REF_COUNT:
+        return X86EMUL_EXCEPTION;
+
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    {
+        unsigned int stimerx =
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));
+        struct viridian_stimer *vs = &vv->stimer[stimerx];

I again think you'd better use array_access_nospec() here (also for the rdmsr counterparts).

I don't follow. I *am* using array_index_nospec().

Quoted 9 lines

xen/arch/x86/hvm/viridian/time.c int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 469

Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +1 -0
@@ -160,6 +466,7 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;

const?

I don't think so. A read of the reference TSC MSR updates a flag.

Quoted 15 lines
Quoted 15 lines
xen/arch/x86/hvm/viridian/time.c +10 -0
@@ -201,6 +508,37 @@ int viridian_time_rdmsr(const struct vcpu *v, uint32_t
 idx, uint64_t *val)
         break;
     }
 
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    {
+        unsigned int stimerx =
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));;
+
+        if ( !(viridian_feature_mask(d) & HVMPV_stimer) )
Quoted 8 lines

xen/arch/x86/hvm/viridian/time.c int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val) · line 526

Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +6 -0
+            return X86EMUL_EXCEPTION;
+
+        *val = vv->stimer[stimerx].config.raw;
+        break;
+    }
+    case HV_X64_MSR_STIMER0_COUNT:

Blank line between case blocks please.

Ok.

Quoted 8 lines

xen/arch/x86/hvm/viridian/time.c void viridian_time_domain_deinit(const struct domain *d) · line 593

Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +1 -0
@@ -231,11 +590,36 @@ void viridian_time_domain_deinit(const struct domain *d)
 void viridian_time_save_vcpu_ctxt(
     const struct vcpu *v, struct hvm_viridian_vcpu_context *ctxt)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;

const?

Possibly.

Quoted 14 lines

xen/arch/x86/hvm/viridian/time.c void viridian_time_domain_deinit(const struct domain *d) · line 603

Quoted 11 lines
xen/arch/x86/hvm/viridian/time.c +10 -0
+    unsigned int i;
+
+    BUILD_BUG_ON(ARRAY_SIZE(vv->stimer) !=
+                 ARRAY_SIZE(ctxt->stimer_config_msr));
+    BUILD_BUG_ON(ARRAY_SIZE(vv->stimer) !=
+                 ARRAY_SIZE(ctxt->stimer_count_msr));
+
+    for ( i = 0; i < ARRAY_SIZE(vv->stimer); i++ )
+    {
+        struct viridian_stimer *vs = &vv->stimer[i];

const (if you really consider the variable useful in the first place)?

Ok.

Quoted 16 lines

xen/arch/x86/hvm/viridian/viridian.c int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val) · line 335

Quoted 13 lines
xen/arch/x86/hvm/viridian/viridian.c +9 -0
@@ -322,6 +324,15 @@ int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val)
     case HV_X64_MSR_TSC_FREQUENCY:
     case HV_X64_MSR_APIC_FREQUENCY:
     case HV_X64_MSR_REFERENCE_TSC:
+    case HV_X64_MSR_TIME_REF_COUNT:
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    case HV_X64_MSR_STIMER3_COUNT:

For readability / brevity

Quoted 3 lines
    case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:

?

Certainly brevity, but I'm not sure about readability. I'll make the change.

Paul

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#27 lore raw

Re: [PATCH v5 10/11] viridian: add implementation of synthetic timers

Quoted 51 lines
Quoted 42 lines
Quoted 1 line · On 13.03.19 at 15:37, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 13 March 2019 14:06

Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:
xen/arch/x86/hvm/viridian/synic.c +37 -0
+bool viridian_synic_deliver_timer_msg(struct vcpu *v, unsigned int sintx,
+                                      unsigned int index,
+                                      uint64_t expiration,
+                                      uint64_t delivery)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    const union viridian_sint_msr *vs = &vv->sint[sintx];
+    HV_MESSAGE *msg = vv->simp.ptr;
+    struct {
+        uint32_t TimerIndex;
+        uint32_t Reserved;
+        uint64_t ExpirationTime;
+        uint64_t DeliveryTime;
+    } payload = {
+        .TimerIndex = index,
+        .ExpirationTime = expiration,
+        .DeliveryTime = delivery,
+    };
+
+    if ( test_bit(sintx, &vv->msg_pending) )
+        return false;
+
+    BUILD_BUG_ON(sizeof(*msg) != HV_MESSAGE_SIZE);
+    msg += sintx;
+
+    /*
+     * To avoid using an atomic test-and-set, and barrier before calling
+     * vlapic_set_irq(), this function must be called in context of the
+     * vcpu receiving the message.
+     */
+    ASSERT(v == current);
+    if ( msg->Header.MessageType != HvMessageTypeNone )
+    {
+        msg->Header.MessageFlags.MessagePending = 1;
+        __set_bit(sintx, &vv->msg_pending);
+        return false;
+    }
Quoted 8 lines

xen/arch/x86/hvm/viridian/synic.c void viridian_synic_domain_deinit(const struct domain *d) · line 389

Quoted 5 lines
xen/arch/x86/hvm/viridian/synic.c +5 -0
+
+    msg->Header.MessageType = HvMessageTimerExpired;
+    msg->Header.MessageFlags.MessagePending = 0;
+    msg->Header.PayloadSize = sizeof(payload);
+    memcpy(msg->Payload, &payload, sizeof(payload));

Since you can't use plain assignment here, how about a BUILD_BUG_ON(sizeof(payload) <= sizeof(msg->payload))?

Quoted 1 line

Surely '>' rather than '<='?

Oops, yes - I was apparently thinking the ASSERT() way.

Quoted 10 lines
Quoted 6 lines

As to safety of this, I have two concerns:

1) TscSequence gets updated as a result of a guest action (an MSR write). This makes it non-obvious that the loop above will get exited in due course.

True. The domain could try to DoS this call. This could be avoided by doing a domain_pause() if we test continuously fails for a number of iterations, or maybe just one iteration.

As per what you say further down, one iteration ought to be enough indeed. Otherwise I would have suggested a handful.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/hvm/viridian/time.c +9 -0
+static void poll_stimer(struct vcpu *v, unsigned int stimerx)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    struct viridian_stimer *vs = &vv->stimer[stimerx];
+
+    if ( !test_bit(stimerx, &vv->stimer_pending) )
+        return;
+
+    if ( !viridian_synic_deliver_timer_msg(v, vs->config.fields.sintx,
Quoted 8 lines

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 299

Quoted 5 lines
xen/arch/x86/hvm/viridian/time.c +5 -0
+                                           stimerx, vs->expiration,
+                                           time_now(v->domain)) )
+        return;
+
+    clear_bit(stimerx, &vv->stimer_pending);

While perhaps benign, wouldn't it be better to clear the pending bit before delivering the message?

Quoted 1 line

No, because I only want to clear it if the delivery is successful.

Ah, I see.

Quoted 20 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -149,6 +398,63 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
         }
         break;
 
Quoted 15 lines

xen/arch/x86/hvm/viridian/time.c int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 412

Quoted 12 lines
xen/arch/x86/hvm/viridian/time.c +12 -0
+    case HV_X64_MSR_TIME_REF_COUNT:
+        return X86EMUL_EXCEPTION;
+
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    {
+        unsigned int stimerx =
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));
+        struct viridian_stimer *vs = &vv->stimer[stimerx];

I again think you'd better use array_access_nospec() here (also for the rdmsr counterparts).

Quoted 1 line

I don't follow. I *am* using array_index_nospec().

But "index" != "access".

Quoted 10 lines
Quoted 8 lines

xen/arch/x86/hvm/viridian/time.c int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 469

Quoted 5 lines
xen/arch/x86/hvm/viridian/time.c +1 -0
@@ -160,6 +466,7 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;

const?

I don't think so. A read of the reference TSC MSR updates a flag.

But you don't make any existing code use vv in this patch. And the new code you add doesn't look to require it to be non-const. I can see why vd (introduced by an earlier patch in the series) can't be constified for the reason you name.

Quoted 16 lines
Quoted 16 lines

xen/arch/x86/hvm/viridian/viridian.c int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val) · line 335

Quoted 13 lines
xen/arch/x86/hvm/viridian/viridian.c +9 -0
@@ -322,6 +324,15 @@ int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val)
     case HV_X64_MSR_TSC_FREQUENCY:
     case HV_X64_MSR_APIC_FREQUENCY:
     case HV_X64_MSR_REFERENCE_TSC:
+    case HV_X64_MSR_TIME_REF_COUNT:
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    case HV_X64_MSR_STIMER3_COUNT:

For readability / brevity

Quoted 5 lines
Quoted 3 lines
    case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:

?

Certainly brevity, but I'm not sure about readability. I'll make the change.

Well, you're the maintainer, so I don't want to talk you into something you're really opposed to.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#28 lore raw

Re: [PATCH v5 10/11] viridian: add implementation of synthetic timers

Quoted 122 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 13 March 2019 15:37
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Roger Pau Monne <[email protected]>; Wei Liu <[email protected]>; Stefano Stabellini <[email protected]>; xen-devel <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: RE: [PATCH v5 10/11] viridian: add implementation of synthetic timers

Quoted 51 lines
Quoted 42 lines
Quoted 1 line · On 13.03.19 at 15:37, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 13 March 2019 14:06

Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:
xen/arch/x86/hvm/viridian/synic.c +37 -0
+bool viridian_synic_deliver_timer_msg(struct vcpu *v, unsigned int sintx,
+                                      unsigned int index,
+                                      uint64_t expiration,
+                                      uint64_t delivery)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    const union viridian_sint_msr *vs = &vv->sint[sintx];
+    HV_MESSAGE *msg = vv->simp.ptr;
+    struct {
+        uint32_t TimerIndex;
+        uint32_t Reserved;
+        uint64_t ExpirationTime;
+        uint64_t DeliveryTime;
+    } payload = {
+        .TimerIndex = index,
+        .ExpirationTime = expiration,
+        .DeliveryTime = delivery,
+    };
+
+    if ( test_bit(sintx, &vv->msg_pending) )
+        return false;
+
+    BUILD_BUG_ON(sizeof(*msg) != HV_MESSAGE_SIZE);
+    msg += sintx;
+
+    /*
+     * To avoid using an atomic test-and-set, and barrier before calling
+     * vlapic_set_irq(), this function must be called in context of the
+     * vcpu receiving the message.
+     */
+    ASSERT(v == current);
+    if ( msg->Header.MessageType != HvMessageTypeNone )
+    {
+        msg->Header.MessageFlags.MessagePending = 1;
+        __set_bit(sintx, &vv->msg_pending);
+        return false;
+    }
Quoted 8 lines

xen/arch/x86/hvm/viridian/synic.c void viridian_synic_domain_deinit(const struct domain *d) · line 389

Quoted 5 lines
xen/arch/x86/hvm/viridian/synic.c +5 -0
+
+    msg->Header.MessageType = HvMessageTimerExpired;
+    msg->Header.MessageFlags.MessagePending = 0;
+    msg->Header.PayloadSize = sizeof(payload);
+    memcpy(msg->Payload, &payload, sizeof(payload));

Since you can't use plain assignment here, how about a BUILD_BUG_ON(sizeof(payload) <= sizeof(msg->payload))?

Quoted 1 line

Surely '>' rather than '<='?

Oops, yes - I was apparently thinking the ASSERT() way.

Quoted 10 lines
Quoted 6 lines

As to safety of this, I have two concerns:

1) TscSequence gets updated as a result of a guest action (an MSR write). This makes it non-obvious that the loop above will get exited in due course.

True. The domain could try to DoS this call. This could be avoided by doing a domain_pause() if we test continuously fails for a number of iterations, or maybe just one iteration.

As per what you say further down, one iteration ought to be enough indeed. Otherwise I would have suggested a handful.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/hvm/viridian/time.c +9 -0
+static void poll_stimer(struct vcpu *v, unsigned int stimerx)
+{
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;
+    struct viridian_stimer *vs = &vv->stimer[stimerx];
+
+    if ( !test_bit(stimerx, &vv->stimer_pending) )
+        return;
+
+    if ( !viridian_synic_deliver_timer_msg(v, vs->config.fields.sintx,
Quoted 8 lines

xen/arch/x86/hvm/viridian/time.c static int64_t time_ref_count(const struct domain *d) · line 299

Quoted 5 lines
xen/arch/x86/hvm/viridian/time.c +5 -0
+                                           stimerx, vs->expiration,
+                                           time_now(v->domain)) )
+        return;
+
+    clear_bit(stimerx, &vv->stimer_pending);

While perhaps benign, wouldn't it be better to clear the pending bit before delivering the message?

Quoted 1 line

No, because I only want to clear it if the delivery is successful.

Ah, I see.

Quoted 20 lines
Quoted 19 lines
Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +2 -0
@@ -149,6 +398,63 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
         }
         break;
 
+    case HV_X64_MSR_TIME_REF_COUNT:
+        return X86EMUL_EXCEPTION;

xen/arch/x86/hvm/viridian/time.c int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 412

Quoted 10 lines
xen/arch/x86/hvm/viridian/time.c +10 -0
+
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    {
+        unsigned int stimerx =
+            array_index_nospec((idx - HV_X64_MSR_STIMER0_CONFIG) / 2,
+                               ARRAY_SIZE(vv->stimer));
+        struct viridian_stimer *vs = &vv->stimer[stimerx];

I again think you'd better use array_access_nospec() here (also for the rdmsr counterparts).

Quoted 3 lines
Quoted 1 line

I don't follow. I *am* using array_index_nospec().

But "index" != "access".

Ah, I was blinkered by the 'nospec'... yes, I'll use that rather than rolling my own.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

xen/arch/x86/hvm/viridian/time.c int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val) · line 469

Quoted 6 lines
xen/arch/x86/hvm/viridian/time.c +1 -0
@@ -160,6 +466,7 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)
 
 int viridian_time_rdmsr(const struct vcpu *v, uint32_t idx, uint64_t *val)
 {
+    struct viridian_vcpu *vv = v->arch.hvm.viridian;

const?

Quoted 6 lines
Quoted 1 line

I don't think so. A read of the reference TSC MSR updates a flag.

But you don't make any existing code use vv in this patch. And the new code you add doesn't look to require it to be non-const. I can see why vd (introduced by an earlier patch in the series) can't be constified for the reason you name.

Ah, true, I was thinking of vd. Although I'm sure when I tried to const vv I got a compiler error. I'll try again... something else might have changed.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
xen/arch/x86/hvm/viridian/viridian.c +0 -0
@@ -322,6 +324,15 @@ int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val)
     case HV_X64_MSR_TSC_FREQUENCY:
     case HV_X64_MSR_APIC_FREQUENCY:

xen/arch/x86/hvm/viridian/viridian.c int guest_wrmsr_viridian(struct vcpu *v, uint32_t idx, uint64_t val) · line 335

Quoted 10 lines
xen/arch/x86/hvm/viridian/viridian.c +9 -0
     case HV_X64_MSR_REFERENCE_TSC:
+    case HV_X64_MSR_TIME_REF_COUNT:
+    case HV_X64_MSR_STIMER0_CONFIG:
+    case HV_X64_MSR_STIMER0_COUNT:
+    case HV_X64_MSR_STIMER1_CONFIG:
+    case HV_X64_MSR_STIMER1_COUNT:
+    case HV_X64_MSR_STIMER2_CONFIG:
+    case HV_X64_MSR_STIMER2_COUNT:
+    case HV_X64_MSR_STIMER3_CONFIG:
+    case HV_X64_MSR_STIMER3_COUNT:

For readability / brevity

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
    case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:

?

Certainly brevity, but I'm not sure about readability. I'll make the change.

Well, you're the maintainer, so I don't want to talk you into something you're really opposed to.

I'm not that bothered so I'll make the change.

Paul

Quoted 1 line

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#29 lore raw

Re: [PATCH v5 10/11] viridian: add implementation of synthetic timers

In reply to Paul Durrant
Quoted 11 lines

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Paul Durrant
Sent: 13 March 2019 14:37
To: 'Jan Beulich' <[email protected]>
Cc: Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Andrew Cooper <[email protected]>; Tim (Xen.org) <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; xen-devel <xen- [email protected]>; Ian Jackson <[email protected]>; Roger Pau Monne <[email protected]>
Subject: Re: [Xen-devel] [PATCH v5 10/11] viridian: add implementation of synthetic timers

[snip]

Quoted 28 lines
Quoted 7 lines

As to safety of this, I have two concerns:

1) TscSequence gets updated as a result of a guest action (an MSR write). This makes it non-obvious that the loop above will get exited in due course.

True. The domain could try to DoS this call. This could be avoided by doing a domain_pause() if we test continuously fails for a number of iterations, or maybe just one iteration.

Quoted 9 lines

2) The way update_reference_tsc() deals with the two "invalid" values suggests ~0 and 0 should be special cased in general. I _think_ this is not necessary here, but it also seems to me as if the VM ever having a way to observe either of those two values would be wrong too. Shouldn't the function avoid to ever store ~0 into that field, i.e. increment into a local variable, update that local variable to skip the two "invalid" values, and only then store into the field?

Quoted 7 lines

Otoh, making it into that function being a result of an MSR write, it may welll be that the spec precludes the guest from reading the reference page while an update was invoked from one of its vCPU-s. If this was the case, then I also wouldn't have to wonder any longer how this entire mechanism can be race free in the first place (without a double increment like we do in the pv-clock protocol).

Quoted 3 lines

From observation, it looks like Windows initializes the reference tsc page before it brings secondary CPUs online and then doesn't touch the MSR again, so we should probably only tolerate one mismatch in time_now() before doing domain_pause().

Actually it occurred to me last night that I'm being completely thick by coding it this way. The viridian code sets TscScale, not the guest, so we don't even need to reference the HV_REFERENCE_TSC_PAGE struct. Looking again, I'm also concerned that there's a small TOCTOU race in testing whether the reference tsc page is valid where the guest could unmap it on another CPU and cause a NULL pointer deref in time_now(), so I'll re-work this entirely.

Paul

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#30 lore raw

PATCH v5 11/11 viridian: add implementation of the HvSendSyntheticClusterIpi hypercall

In reply to Paul Durrant

This patch adds an implementation of the hypercall as documented in the specification [1], section 10.5.2. This enlightenment, as with others, is advertised by CPUID leaf 0x40000004 and is under control of a new 'hcall_ipi' option in libxl.

If used, this enlightenment should mean the guest only takes a single VMEXIT to issue IPIs to multiple vCPUs rather than the multiple VMEXITs that would result from using the emulated local APIC.

[1] https://github.com/MicrosoftDocs/Virtualization-Documentation/raw/live/tlfs/Hypervisor%20Top%20Level%20Functional%20Specification%20v5.0C.pdf

---
Cc: Ian Jackson <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v4:
- Address comments from Jan

v3:
- New in v3

---
 docs/man/xl.cfg.5.pod.in             |  6 +++
 tools/libxl/libxl.h                  |  6 +++
 tools/libxl/libxl_dom.c              |  3 ++
 tools/libxl/libxl_types.idl          |  1 +
 xen/arch/x86/hvm/viridian/viridian.c | 63 ++++++++++++++++++++++++++++
 xen/include/public/hvm/params.h      |  7 +++-
 6 files changed, 85 insertions(+), 1 deletion(-)
docs/man/xl.cfg.5.pod.in +6 -0
index 355c654693..c7d70e618b 100644
@@ -2175,6 +2175,12 @@ ticks and hence enabling this group will ensure that ticks will be
 consistent with use of an enlightened time source (B<time_ref_count> or
 B<reference_tsc>).
 
+=item B<hcall_ipi>
+
+This set incorporates use of a hypercall for interprocessor interrupts.
+This enlightenment may improve performance of Windows guests with multiple
+virtual CPUs.
+
 =item B<defaults>
 
 This is a special value that enables the default set of groups, which
tools/libxl/libxl.h +6 -0
index c8f219b0d3..482499a6c0 100644
@@ -330,6 +330,12 @@
  */
 #define LIBXL_HAVE_VIRIDIAN_STIMER 1
 
+/*
+ * LIBXL_HAVE_VIRIDIAN_HCALL_IPI indicates that the 'hcall_ipi' value
+ * is present in the viridian enlightenment enumeration.
+ */
+#define LIBXL_HAVE_VIRIDIAN_HCALL_IPI 1
+
 /*
  * LIBXL_HAVE_BUILDINFO_HVM_ACPI_LAPTOP_SLATE indicates that
  * libxl_domain_build_info has the u.hvm.acpi_laptop_slate field.
tools/libxl/libxl_dom.c +3 -0
index 2ee0f82ee7..879c806139 100644
@@ -324,6 +324,9 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,
     if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_STIMER))
         mask |= HVMPV_time_ref_count | HVMPV_synic | HVMPV_stimer;
 
+    if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_HCALL_IPI))
+        mask |= HVMPV_hcall_ipi;
+
     if (mask != 0 &&
         xc_hvm_param_set(CTX->xch,
                          domid,
tools/libxl/libxl_types.idl +1 -0
index 1cce249de4..cb4702fd7a 100644
@@ -237,6 +237,7 @@ libxl_viridian_enlightenment = Enumeration("viridian_enlightenment", [
     (6, "crash_ctl"),
     (7, "synic"),
     (8, "stimer"),
+    (9, "hcall_ipi"),
     ])
 
 libxl_hdtype = Enumeration("hdtype", [
xen/arch/x86/hvm/viridian/viridian.c +63 -0

87 lines not on this page · raw message

xen/include/public/hvm/params.h +6 -1
index e06b0942d0..36832e4b94 100644
@@ -154,6 +154,10 @@
 #define _HVMPV_stimer 8
 #define HVMPV_stimer (1 << _HVMPV_stimer)
 
+/* Use Synthetic Cluster IPI Hypercall */
+#define _HVMPV_hcall_ipi 9
+#define HVMPV_hcall_ipi (1 << _HVMPV_hcall_ipi)
+
 #define HVMPV_feature_mask \
         (HVMPV_base_freq | \
          HVMPV_no_freq | \
@@ -163,7 +167,8 @@
          HVMPV_apic_assist | \
          HVMPV_crash_ctl | \
          HVMPV_synic | \
-         HVMPV_stimer)
+         HVMPV_stimer | \
+         HVMPV_hcall_ipi)
 
 #endif
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v5 11/11
#31 lore raw

Re: [PATCH v5 11/11] viridian: add implementation of the HvSendSyntheticClusterIpi hypercall

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 11.03.19 at 14:41, <[email protected]> wrote:

This patch adds an implementation of the hypercall as documented in the specification [1], section 10.5.2. This enlightenment, as with others, is advertised by CPUID leaf 0x40000004 and is under control of a new 'hcall_ipi' option in libxl.

If used, this enlightenment should mean the guest only takes a single VMEXIT to issue IPIs to multiple vCPUs rather than the multiple VMEXITs that would result from using the emulated local APIC.

Quoted 5 lines

[1]
https://github.com/MicrosoftDocs/Virtualization-Documentation/raw/live/tlfs/Hypervisor%20Top%20Level%20Functional%20Specification%20v5.0C.pdf

Hypervisor parts

with one remark:

Quoted 37 lines
xen/arch/x86/hvm/viridian/viridian.c +33 -0
@@ -642,6 +646,65 @@ int viridian_hypercall(struct cpu_user_regs *regs)
         break;
     }
 
+    case HvSendSyntheticClusterIpi:
+    {
+        struct vcpu *v;
+        uint32_t vector;
+        uint64_t vcpu_mask;
+
+        status = HV_STATUS_INVALID_PARAMETER;
+
+        /* Get input parameters. */
+        if ( input.fast )
+        {
+            if ( input_params_gpa >> 32 )
+                break;
+
+            vector = input_params_gpa;
+            vcpu_mask = output_params_gpa;
+        }
+        else
+        {
+            struct {
+                uint32_t vector;
+                uint8_t target_vtl;
+                uint8_t reserved_zero[3];
+                uint64_t vcpu_mask;
+            } input_params;
+
+            if ( hvm_copy_from_guest_phys(&input_params, input_params_gpa,
+                                          sizeof(input_params)) !=
+                 HVMTRANS_okay )
+                break;
+
+            if ( input_params.target_vtl ||
+                 input_params.reserved_zero[0] ||

xen/arch/x86/hvm/viridian/viridian.c int viridian_hypercall(struct cpu_user_regs *regs) · line 691

Quoted 10 lines
xen/arch/x86/hvm/viridian/viridian.c +10 -0
+                 input_params.reserved_zero[1] ||
+                 input_params.reserved_zero[2] )
+                break;
+
+            vector = input_params.vector;
+            vcpu_mask = input_params.vcpu_mask;
+        }
+
+        if ( vector < 0x10 || vector > 0xff )
+            break;

Elsewhere you've been using decimal.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel