Skip to contentSkip to conversation

Vm_event memory introspection helpers

PATCH Superseded Razvan Cojocaru 4 participants 12 messages Jul 8, 2015 – Jul 10, 2015

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Vm_event memory introspection helpers

Version 4 of the series addresses V3 reviews, and consists of:

[PATCH 1/3] xen/mem_access: Support for memory-content hiding [PATCH 2/3] xen/vm_event: Support for guest-requested events [PATCH 3/3] xen/vm_event: Deny register writes if refused by vm_event reply

All the patches in this version have been acked by at least one person. For [PATCH 3/3], Tamas has suggested that I move the DENY logic from p2m.c to dedicated files, which I've done here. Since this is simply a trivial move without any modifications to the logic itself, I've kept both acks received for the patch; George's ack should in any case not be an issue, as it only concerned the mm parts which are unchanged, but if I shouldn't have kept Jan's ack then please disregard it.

This version of the series assumes the patch "vm_event: Rename MEM_ACCESS_EMULATE and MEM_ACCESS_EMULATE_NOWRITE" that I've submitted yesterday. I've not added that patch to this series because I wanted it to be available for Tamas as well, as he's working on a parallel series and I had hoped that this way would be better than him having to wait for this whole series to go in.

Thank you,
Razvan

#1 lore raw

PATCH v4 1/3 xen/mem_access: Support for memory-content hiding

This patch adds support for memory-content hiding, by modifying the value returned by emulated instructions that read certain memory addresses that contain sensitive data. The patch only applies to cases where MEM_ACCESS_EMULATE or MEM_ACCESS_EMULATE_NOWRITE have been set to a vm_event response.

---
Changes since V3:
- Renamed MEM_ACCESS_SET_EMUL_READ_DATA to VM_EVENT_FLAG_SET_EMUL_READ_DATA and updated its comment.
- Removed xfree(v->arch.vm_event.emul_read_data) from free_vcpu_struct().
- Returning X86EMUL_UNHANDLEABLE from hvmemul_cmpxchg() when !curr->arch.vm_event.emul_read_data.
- Replaced in xmalloc_bytes() with xmalloc_array() in hvmemul_rep_outs_set_context().
- Setting the rest of the buffer to zero in hvmemul_rep_movs() (no longer leaking heap contents).
- No longer memset()ing the whole buffer before copy (just zeroing out the rest).
- Moved hvmemul_ctxt->set_context = 0 to hvm_emulate_prepare() and removed hvm_emulate_one_set_context().

---
 tools/tests/xen-access/xen-access.c |    2 +-
 xen/arch/x86/hvm/emulate.c          |  138 ++++++++++++++++++++++++++++++++++-
 xen/arch/x86/hvm/event.c            |   50 ++++++-------
 xen/arch/x86/mm/p2m.c               |   92 +++++++++++++----------
 xen/common/domain.c                 |    2 +
 xen/common/vm_event.c               |   23 ++++++
 xen/include/asm-x86/domain.h        |    2 +
 xen/include/asm-x86/hvm/emulate.h   |   10 ++-
 xen/include/public/vm_event.h       |   31 ++++++--
 9 files changed, 274 insertions(+), 76 deletions(-)
tools/tests/xen-access/xen-access.c +1 -1
index 12ab921..e6ca9ba 100644
@@ -530,7 +530,7 @@ int main(int argc, char *argv[])
                 break;
             case VM_EVENT_REASON_SOFTWARE_BREAKPOINT:
                 printf("Breakpoint: rip=%016"PRIx64", gfn=%"PRIx64" (vcpu %d)\n",
-                       req.regs.x86.rip,
+                       req.data.regs.x86.rip,
                        req.u.software_breakpoint.gfn,
                        req.vcpu_id);
 
xen/arch/x86/hvm/emulate.c +135 -3

196 lines not on this page · raw message

xen/arch/x86/hvm/event.c +25 -25

56 lines not on this page · raw message

xen/arch/x86/mm/p2m.c +52 -40

116 lines not on this page · raw message

xen/common/domain.c +2 -0
index 3bc52e6..ff86266 100644
@@ -166,6 +166,7 @@ struct vcpu *alloc_vcpu(
         free_cpumask_var(v->cpu_hard_affinity_saved);
         free_cpumask_var(v->cpu_soft_affinity);
         free_cpumask_var(v->vcpu_dirty_cpumask);
+        xfree(v->arch.vm_event.emul_read_data);
         free_vcpu_struct(v);
         return NULL;
     }
@@ -821,6 +822,7 @@ static void complete_domain_destroy(struct rcu_head *head)
             free_cpumask_var(v->cpu_hard_affinity_saved);
             free_cpumask_var(v->cpu_soft_affinity);
             free_cpumask_var(v->vcpu_dirty_cpumask);
+            xfree(v->arch.vm_event.emul_read_data);
             free_vcpu_struct(v);
         }
 
xen/common/vm_event.c +23 -0
index 120a78a..11438da 100644
@@ -48,6 +48,7 @@ static int vm_event_enable(
 {
     int rc;
     unsigned long ring_gfn = d->arch.hvm_domain.params[param];
+    struct vcpu *v;
 
     /* Only one helper at a time. If the helper crashed,
      * the ring is in an undefined state and so is the guest.
@@ -63,6 +64,21 @@ static int vm_event_enable(
     vm_event_ring_lock_init(ved);
     vm_event_ring_lock(ved);
 
+    for_each_vcpu( d, v )
+    {
+        if ( v->arch.vm_event.emul_read_data )
+            continue;
+
+        v->arch.vm_event.emul_read_data =
+            xmalloc(struct vm_event_emul_read_data);
+
+        if ( !v->arch.vm_event.emul_read_data )
+        {
+            rc = -ENOMEM;
+            goto err;
+        }
+    }
+
     rc = prepare_ring_for_helper(d, ring_gfn, &ved->ring_pg_struct,
                                     &ved->ring_page);
     if ( rc < 0 )
@@ -225,6 +241,13 @@ static int vm_event_disable(struct domain *d, struct vm_event_domain *ved)
 
         destroy_ring_for_helper(&ved->ring_page,
                                 ved->ring_pg_struct);
+
+        for_each_vcpu( d, v )
+        {
+            xfree(v->arch.vm_event.emul_read_data);
+            v->arch.vm_event.emul_read_data = NULL;
+        }
+
         vm_event_ring_unlock(ved);
     }
 
xen/include/asm-x86/domain.h +2 -0
index 96bde65..7908844 100644
@@ -10,6 +10,7 @@
 #include <asm/mce.h>
 #include <public/vcpu.h>
 #include <public/hvm/hvm_info_table.h>
+#include <public/vm_event.h>
 
 #define has_32bit_shinfo(d)    ((d)->arch.has_32bit_shinfo)
 #define is_pv_32bit_domain(d)  ((d)->arch.is_32bit_pv)
@@ -508,6 +509,7 @@ struct arch_vcpu
         uint32_t emulate_flags;
         unsigned long gpa;
         unsigned long eip;
+        struct vm_event_emul_read_data *emul_read_data;
     } vm_event;
 
 };
xen/include/asm-x86/hvm/emulate.h +9 -1
index 594be38..49134b5 100644
@@ -32,13 +32,21 @@ struct hvm_emulate_ctxt {
     struct hvm_trap trap;
 
     uint32_t intr_shadow;
+
+    bool_t set_context;
+};
+
+enum emul_kind {
+    EMUL_KIND_NORMAL,
+    EMUL_KIND_NOWRITE,
+    EMUL_KIND_SET_CONTEXT
 };
 
 int hvm_emulate_one(
     struct hvm_emulate_ctxt *hvmemul_ctxt);
 int hvm_emulate_one_no_write(
     struct hvm_emulate_ctxt *hvmemul_ctxt);
-void hvm_mem_access_emulate_one(bool_t nowrite,
+void hvm_mem_access_emulate_one(enum emul_kind kind,
     unsigned int trapnr,
     unsigned int errcode);
 void hvm_emulate_prepare(
xen/include/public/vm_event.h +25 -6
index aa22052..c6f69af 100644
@@ -44,9 +44,9 @@
  *  paused
  * VCPU_PAUSED in a response signals to unpause the vCPU
  */
-#define VM_EVENT_FLAG_VCPU_PAUSED     (1 << 0)
+#define VM_EVENT_FLAG_VCPU_PAUSED        (1 << 0)
 /* Flags to aid debugging mem_event */
-#define VM_EVENT_FLAG_FOREIGN         (1 << 1)
+#define VM_EVENT_FLAG_FOREIGN            (1 << 1)
 /*
  * The following flags can be set in response to a mem_access event.
  *
@@ -54,12 +54,21 @@
  * This will allow the guest to continue execution without lifting the page
  * access restrictions.
  */
-#define VM_EVENT_FLAG_EMULATE         (1 << 2)
+#define VM_EVENT_FLAG_EMULATE            (1 << 2)
 /*
  * Same as MEM_ACCESS_EMULATE, but with write operations or operations
  * potentially having side effects (like memory mapped or port I/O) disabled.
  */
-#define VM_EVENT_FLAG_EMULATE_NOWRITE (1 << 3)
+#define VM_EVENT_FLAG_EMULATE_NOWRITE    (1 << 3)
+/*
+ * Data is being sent back to the hypervisor in the event response, to be
+ * returned by the read function when emulating an instruction.
+ * This flag is only useful when combined with VM_EVENT_FLAG_EMULATE
+ * and takes precedence if combined with VM_EVENT_FLAG_EMULATE_NOWRITE
+ * (i.e. if both VM_EVENT_FLAG_EMULATE_NOWRITE and
+ * VM_EVENT_FLAG_SET_EMUL_READ_DATA are set, only the latter will be honored).
+ */
+#define VM_EVENT_FLAG_SET_EMUL_READ_DATA (1 << 4)
 
 /*
  * Reasons for the vm event request
@@ -189,6 +198,12 @@ struct vm_event_sharing {
     uint32_t _pad;
 };
 
+struct vm_event_emul_read_data {
+    uint32_t size;
+    /* The struct is used in a union with vm_event_regs_x86. */
+    uint8_t  data[sizeof(struct vm_event_regs_x86) - sizeof(uint32_t)];
+};
+
 typedef struct vm_event_st {
     uint32_t version;   /* VM_EVENT_INTERFACE_VERSION */
     uint32_t flags;     /* VM_EVENT_FLAG_* */
@@ -206,8 +221,12 @@ typedef struct vm_event_st {
     } u;
 
     union {
-        struct vm_event_regs_x86 x86;
-    } regs;
+        union {
+            struct vm_event_regs_x86 x86;
+        } regs;
+
+        struct vm_event_emul_read_data emul_read_data;
+    } data;
 } vm_event_request_t, vm_event_response_t;
 
 DEFINE_RING_TYPES(vm_event, vm_event_request_t, vm_event_response_t);
Signature · 1 line
1.7.9.5
Acked-by on v4 1/3
#2 lore raw

Re: [PATCH V4 1/3] xen/mem_access: Support for memory-content hiding

Quoted 25 lines · Razvan Cojocaru

This patch adds support for memory-content hiding, by modifying the value returned by emulated instructions that read certain memory addresses that contain sensitive data. The patch only applies to cases where MEM_ACCESS_EMULATE or MEM_ACCESS_EMULATE_NOWRITE have been set to a vm_event response.

---
Changes since V3:
- Renamed MEM_ACCESS_SET_EMUL_READ_DATA to VM_EVENT_FLAG_SET_EMUL_READ_DATA and updated its comment.
- Removed xfree(v->arch.vm_event.emul_read_data) from free_vcpu_struct().
- Returning X86EMUL_UNHANDLEABLE from hvmemul_cmpxchg() when !curr->arch.vm_event.emul_read_data.
- Replaced in xmalloc_bytes() with xmalloc_array() in hvmemul_rep_outs_set_context().
- Setting the rest of the buffer to zero in hvmemul_rep_movs() (no longer leaking heap contents).
- No longer memset()ing the whole buffer before copy (just zeroing out the rest).
- Moved hvmemul_ctxt->set_context = 0 to hvm_emulate_prepare() and removed hvm_emulate_one_set_context().

Quoted 13 lines
---
 tools/tests/xen-access/xen-access.c |    2 +-
 xen/arch/x86/hvm/emulate.c          |  138
++++++++++++++++++++++++++++++++++-
 xen/arch/x86/hvm/event.c            |   50 ++++++-------
 xen/arch/x86/mm/p2m.c               |   92 +++++++++++++----------
 xen/common/domain.c                 |    2 +
 xen/common/vm_event.c               |   23 ++++++
 xen/include/asm-x86/domain.h        |    2 +
 xen/include/asm-x86/hvm/emulate.h   |   10 ++-
 xen/include/public/vm_event.h       |   31 ++++++--
 9 files changed, 274 insertions(+), 76 deletions(-)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw

Re: [PATCH V4 1/3] xen/mem_access: Support for memory-content hiding

In reply to Razvan Cojocaru
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 08.07.15 at 12:22, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +3 -0
@@ -653,6 +653,31 @@ static int hvmemul_read(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
+    struct hvm_emulate_ctxt *hvmemul_ctxt =
+        container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
+

xen/arch/x86/hvm/emulate.c static int hvmemul_read( · line 668

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+    if ( unlikely(hvmemul_ctxt->set_context) )
+    {
+        struct vcpu *curr = current;
+        unsigned int safe_bytes;
+
+        if ( !curr->arch.vm_event.emul_read_data )
+            return X86EMUL_UNHANDLEABLE;
+
+        safe_bytes = min_t(unsigned int,
+                           bytes, curr->arch.vm_event.emul_read_data->size);

Afaict min() would work here.

xen/arch/x86/hvm/emulate.c static int hvmemul_read( · line 672

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +3 -0
+        if ( safe_bytes )
+        {
+            memcpy(p_data, curr->arch.vm_event.emul_read_data->data, safe_bytes);

Why is this conditional? Doesn't ->size being zero mean all data should be zeroed? Otherwise this isn't really consistent behavior...

Also - long line.

xen/arch/x86/hvm/emulate.c static int hvmemul_read( · line 675

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +3 -0
+
+            if ( bytes > safe_bytes )
+                memset(p_data + safe_bytes, 0, bytes - safe_bytes);

No need for the conditional here.

Quoted 14 lines
xen/arch/x86/hvm/emulate.c +10 -0
@@ -893,6 +918,28 @@ static int hvmemul_cmpxchg(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
+    struct hvm_emulate_ctxt *hvmemul_ctxt =
+        container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
+
+    if ( unlikely(hvmemul_ctxt->set_context) )
+    {
+        struct vcpu *curr = current;
+
+        if ( curr->arch.vm_event.emul_read_data )
+        {
+            unsigned int safe_bytes = min_t(unsigned int, bytes,

xen/arch/x86/hvm/emulate.c static int hvmemul_cmpxchg( · line 940

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+                curr->arch.vm_event.emul_read_data->size);
+
+            memcpy(p_new, curr->arch.vm_event.emul_read_data->data,
+                   safe_bytes);
+
+            if ( bytes > safe_bytes )
+                memset(p_new + safe_bytes, 0, bytes - safe_bytes);
+        }
+        else
+            return X86EMUL_UNHANDLEABLE;

Please make this look as similar to hvmemul_read() as possible (invert the if() condition etc). Once done, you'll be seeing that this would better be factored out...

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +2 -0
@@ -935,6 +982,43 @@ static int hvmemul_rep_ins(
                                !!(ctxt->regs->eflags & X86_EFLAGS_DF), gpa);
 }
 
+static int hvmemul_rep_outs_set_context(
+    enum x86_segment src_seg,

xen/arch/x86/hvm/emulate.c static int hvmemul_rep_ins( · line 996

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+    unsigned long src_offset,
+    uint16_t dst_port,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    struct x86_emulate_ctxt *ctxt)
+{
+    unsigned int bytes = *reps * bytes_per_rep;
+    struct vcpu *curr = current;
+    unsigned int safe_bytes;
+    char *buf = NULL;

Pointless initializer.

xen/arch/x86/hvm/emulate.c static int hvmemul_rep_movs( · line 1158

Quoted 9 lines
xen/arch/x86/hvm/emulate.c +5 -0
@@ -1063,7 +1151,23 @@ static int hvmemul_rep_movs(
      */
     rc = hvm_copy_from_guest_phys(buf, sgpa, bytes);
     if ( rc == HVMCOPY_okay )
+    {
+        struct vcpu *curr = current;
+
+        if ( unlikely(hvmemul_ctxt->set_context) &&
+             curr->arch.vm_event.emul_read_data )

Factoring out will also make obvious that here you're _still_ not handling things consistently with the other cases.

xen/arch/x86/hvm/emulate.c void p2m_mem_access_emulate_check(struct vcpu *v, · line 1470

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +2 -0
@@ -1466,6 +1466,10 @@ void p2m_mem_access_emulate_check(struct vcpu *v,
         }
 
         v->arch.vm_event.emulate_flags = violation ? rsp->flags : 0;
+
+        if ( rsp->flags & VM_EVENT_FLAG_SET_EMUL_READ_DATA &&

Please parenthesize the &.

Quoted 11 lines
xen/common/domain.c +1 -0
@@ -166,6 +166,7 @@ struct vcpu *alloc_vcpu(
         free_cpumask_var(v->cpu_hard_affinity_saved);
         free_cpumask_var(v->cpu_soft_affinity);
         free_cpumask_var(v->vcpu_dirty_cpumask);
+        xfree(v->arch.vm_event.emul_read_data);
         free_vcpu_struct(v);
         return NULL;
     }

xen/common/domain.c static void complete_domain_destroy(struct rcu_head *head) · line 825

Quoted 4 lines
xen/common/domain.c +1 -0
@@ -821,6 +822,7 @@ static void complete_domain_destroy(struct rcu_head *head)
             free_cpumask_var(v->cpu_hard_affinity_saved);
             free_cpumask_var(v->cpu_soft_affinity);
             free_cpumask_var(v->vcpu_dirty_cpumask);
+            xfree(v->arch.vm_event.emul_read_data);

Common code fiddling with arch-specific bits. I can't see how this would build on ARM.

Quoted 8 lines
xen/common/domain.c +4 -0
@@ -63,6 +64,21 @@ static int vm_event_enable(
     vm_event_ring_lock_init(ved);
     vm_event_ring_lock(ved);
 
+    for_each_vcpu( d, v )
+    {
+        if ( v->arch.vm_event.emul_read_data )
+            continue;

xen/common/domain.c static int vm_event_enable( · line 80

Quoted 10 lines
xen/common/domain.c +10 -0
+
+        v->arch.vm_event.emul_read_data =
+            xmalloc(struct vm_event_emul_read_data);
+
+        if ( !v->arch.vm_event.emul_read_data )
+        {
+            rc = -ENOMEM;
+            goto err;
+        }
+    }

Same here. You need to decide whether this is to be a generic feature (then the pointer needs to move to struct vcpu) or x86 specific (then the code needs to move to x86-specific files).

xen/common/domain.c struct vm_event_sharing { · line 204

Quoted 8 lines
xen/common/domain.c +4 -0
@@ -189,6 +198,12 @@ struct vm_event_sharing {
     uint32_t _pad;
 };
 
+struct vm_event_emul_read_data {
+    uint32_t size;
+    /* The struct is used in a union with vm_event_regs_x86. */
+    uint8_t  data[sizeof(struct vm_event_regs_x86) - sizeof(uint32_t)];

Again, albeit here I can see no good alternative at this point.

Jan

#4 lore raw

Re: [PATCH V4 1/3] xen/mem_access: Support for memory-content hiding

Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 08.07.15 at 12:22, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +7 -0
@@ -653,6 +653,31 @@ static int hvmemul_read(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
+    struct hvm_emulate_ctxt *hvmemul_ctxt =
+        container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
+
+    if ( unlikely(hvmemul_ctxt->set_context) )
+    {
+        struct vcpu *curr = current;
+        unsigned int safe_bytes;
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_read( · line 668

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +6 -0
+
+        if ( !curr->arch.vm_event.emul_read_data )
+            return X86EMUL_UNHANDLEABLE;
+
+        safe_bytes = min_t(unsigned int,
+                           bytes, curr->arch.vm_event.emul_read_data->size);

Afaict min() would work here.

Ack.

Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_read( · line 672

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +3 -0
+        if ( safe_bytes )
+        {
+            memcpy(p_data, curr->arch.vm_event.emul_read_data->data, safe_bytes);

Why is this conditional? Doesn't ->size being zero mean all data should be zeroed? Otherwise this isn't really consistent behavior...

Also - long line.

Ack.

Quoted 5 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_read( · line 675

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +3 -0
+
+            if ( bytes > safe_bytes )
+                memset(p_data + safe_bytes, 0, bytes - safe_bytes);

No need for the conditional here.

Ack.

Quoted 20 lines
Quoted 20 lines
xen/arch/x86/hvm/emulate.c +16 -0
@@ -893,6 +918,28 @@ static int hvmemul_cmpxchg(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
+    struct hvm_emulate_ctxt *hvmemul_ctxt =
+        container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
+
+    if ( unlikely(hvmemul_ctxt->set_context) )
+    {
+        struct vcpu *curr = current;
+
+        if ( curr->arch.vm_event.emul_read_data )
+        {
+            unsigned int safe_bytes = min_t(unsigned int, bytes,
+                curr->arch.vm_event.emul_read_data->size);
+
+            memcpy(p_new, curr->arch.vm_event.emul_read_data->data,
+                   safe_bytes);
+
+            if ( bytes > safe_bytes )
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_cmpxchg( · line 940

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +4 -0
+                memset(p_new + safe_bytes, 0, bytes - safe_bytes);
+        }
+        else
+            return X86EMUL_UNHANDLEABLE;

Please make this look as similar to hvmemul_read() as possible (invert the if() condition etc). Once done, you'll be seeing that this would better be factored out...

Very true, I should have factored it out from the beginning, I only have not done so because there's been a somewhat convoluted trip to the current state of the code. Ack.

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/hvm/emulate.c +0 -0
@@ -935,6 +982,43 @@ static int hvmemul_rep_ins(
                                !!(ctxt->regs->eflags & X86_EFLAGS_DF), gpa);
 }
 
Quoted 14 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_rep_ins( · line 996

Quoted 12 lines
xen/arch/x86/hvm/emulate.c +12 -0
+static int hvmemul_rep_outs_set_context(
+    enum x86_segment src_seg,
+    unsigned long src_offset,
+    uint16_t dst_port,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    struct x86_emulate_ctxt *ctxt)
+{
+    unsigned int bytes = *reps * bytes_per_rep;
+    struct vcpu *curr = current;
+    unsigned int safe_bytes;
+    char *buf = NULL;

Pointless initializer.

Ack.

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1063,7 +1151,23 @@ static int hvmemul_rep_movs(
      */
     rc = hvm_copy_from_guest_phys(buf, sgpa, bytes);
     if ( rc == HVMCOPY_okay )
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_rep_movs( · line 1158

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+    {
+        struct vcpu *curr = current;
+
+        if ( unlikely(hvmemul_ctxt->set_context) &&
+             curr->arch.vm_event.emul_read_data )

Factoring out will also make obvious that here you're _still_ not handling things consistently with the other cases.

True. Will fix it.

Quoted 8 lines

xen/arch/x86/hvm/emulate.c void p2m_mem_access_emulate_check(struct vcpu *v, · line 1470

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +2 -0
@@ -1466,6 +1466,10 @@ void p2m_mem_access_emulate_check(struct vcpu *v,
         }
 
         v->arch.vm_event.emulate_flags = violation ? rsp->flags : 0;
+
+        if ( rsp->flags & VM_EVENT_FLAG_SET_EMUL_READ_DATA &&

Please parenthesize the &.

Ack.

Quoted 10 lines
Quoted 10 lines
xen/common/domain.c +1 -0
@@ -166,6 +166,7 @@ struct vcpu *alloc_vcpu(
         free_cpumask_var(v->cpu_hard_affinity_saved);
         free_cpumask_var(v->cpu_soft_affinity);
         free_cpumask_var(v->vcpu_dirty_cpumask);
+        xfree(v->arch.vm_event.emul_read_data);
         free_vcpu_struct(v);
         return NULL;
     }
Quoted 8 lines

xen/common/domain.c static void complete_domain_destroy(struct rcu_head *head) · line 825

Quoted 5 lines
xen/common/domain.c +1 -0
@@ -821,6 +822,7 @@ static void complete_domain_destroy(struct rcu_head *head)
             free_cpumask_var(v->cpu_hard_affinity_saved);
             free_cpumask_var(v->cpu_soft_affinity);
             free_cpumask_var(v->vcpu_dirty_cpumask);
+            xfree(v->arch.vm_event.emul_read_data);

Common code fiddling with arch-specific bits. I can't see how this would build on ARM.

Indeed, I should have been more careful with the xfree() move.

Quoted 15 lines
Quoted 15 lines
xen/common/domain.c +10 -0
@@ -63,6 +64,21 @@ static int vm_event_enable(
     vm_event_ring_lock_init(ved);
     vm_event_ring_lock(ved);
 
+    for_each_vcpu( d, v )
+    {
+        if ( v->arch.vm_event.emul_read_data )
+            continue;
+
+        v->arch.vm_event.emul_read_data =
+            xmalloc(struct vm_event_emul_read_data);
+
+        if ( !v->arch.vm_event.emul_read_data )
+        {
Quoted 8 lines

xen/common/domain.c static int vm_event_enable( · line 80

Quoted 4 lines
xen/common/domain.c +4 -0
+            rc = -ENOMEM;
+            goto err;
+        }
+    }

Same here. You need to decide whether this is to be a generic feature (then the pointer needs to move to struct vcpu) or x86 specific (then the code needs to move to x86-specific files).

I think the best bet is probably to move it to struct vcpu, especially since Tamas intends to use the feature in new scenarios, so it's probably best to not restrict it to x86. Tamas, what do you think?

Thanks,
Razvan

#5 lore raw

PATCH v4 2/3 xen/vm_event: Support for guest-requested events

In reply to Razvan Cojocaru

Added support for a new class of vm_events: VM_EVENT_REASON_REQUEST, sent via HVMOP_request_vm_event. The guest can request that a generic vm_event (containing only the vm_event-filled guest registers as information) be sent to userspace by setting up the correct registers and doing a VMCALL. For example, for a 32-bit guest, this
means: EAX = 34 (hvmop), EBX = 24 (HVMOP_guest_request_vm_event), ECX = 0 (NULL required for the hypercall parameter, reserved).

---
Changes since V3:
- None, just addded acks.

---
 tools/libxc/include/xenctrl.h   |    2 ++
 tools/libxc/xc_monitor.c        |   15 +++++++++++++++
 xen/arch/x86/hvm/event.c        |   16 ++++++++++++++++
 xen/arch/x86/hvm/hvm.c          |    8 +++++++-
 xen/arch/x86/monitor.c          |   16 ++++++++++++++++
 xen/include/asm-x86/domain.h    |   16 +++++++++-------
 xen/include/asm-x86/hvm/event.h |    1 +
 xen/include/public/domctl.h     |    6 ++++++
 xen/include/public/hvm/hvm_op.h |    2 ++
 xen/include/public/vm_event.h   |    2 ++
 10 files changed, 76 insertions(+), 8 deletions(-)
tools/libxc/include/xenctrl.h +2 -0
index d1d2ab3..4ce519a 100644
@@ -2384,6 +2384,8 @@ int xc_monitor_mov_to_msr(xc_interface *xch, domid_t domain_id, bool enable,
 int xc_monitor_singlestep(xc_interface *xch, domid_t domain_id, bool enable);
 int xc_monitor_software_breakpoint(xc_interface *xch, domid_t domain_id,
                                    bool enable);
+int xc_monitor_guest_request(xc_interface *xch, domid_t domain_id,
+                             bool enable, bool sync);
 
 /***
  * Memory sharing operations.
tools/libxc/xc_monitor.c +15 -0
index 63013de..d979122 100644
@@ -105,3 +105,18 @@ int xc_monitor_singlestep(xc_interface *xch, domid_t domain_id,
 
     return do_domctl(xch, &domctl);
 }
+
+int xc_monitor_guest_request(xc_interface *xch, domid_t domain_id, bool enable,
+                             bool sync)
+{
+    DECLARE_DOMCTL;
+
+    domctl.cmd = XEN_DOMCTL_monitor_op;
+    domctl.domain = domain_id;
+    domctl.u.monitor_op.op = enable ? XEN_DOMCTL_MONITOR_OP_ENABLE
+                                    : XEN_DOMCTL_MONITOR_OP_DISABLE;
+    domctl.u.monitor_op.event = XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST;
+    domctl.u.monitor_op.u.guest_request.sync = sync;
+
+    return do_domctl(xch, &domctl);
+}
xen/arch/x86/hvm/event.c +16 -0
index 5341937..17638ea 100644
@@ -126,6 +126,22 @@ void hvm_event_msr(unsigned int msr, uint64_t value)
         hvm_event_traps(1, &req);
 }
 
+void hvm_event_guest_request(void)
+{
+    struct vcpu *curr = current;
+    struct arch_domain *currad = &curr->domain->arch;
+
+    if ( currad->monitor.guest_request_enabled )
+    {
+        vm_event_request_t req = {
+            .reason = VM_EVENT_REASON_GUEST_REQUEST,
+            .vcpu_id = curr->vcpu_id,
+        };
+
+        hvm_event_traps(currad->monitor.guest_request_sync, &req);
+    }
+}
+
 int hvm_event_int3(unsigned long gla)
 {
     int rc = 0;
xen/arch/x86/hvm/hvm.c +7 -1
index 535d622..536d1c8 100644
@@ -5974,7 +5974,6 @@ static int hvmop_get_param(
 #define HVMOP_op_mask 0xff
 
 long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
-
 {
     unsigned long start_iter, mask;
     long rc = 0;
@@ -6388,6 +6387,13 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 
+    case HVMOP_guest_request_vm_event:
+        if ( guest_handle_is_null(arg) )
+            hvm_event_guest_request();
+        else
+            rc = -EINVAL;
+        break;
+
     default:
     {
         gdprintk(XENLOG_DEBUG, "Bad HVM op %ld.\n", op);
xen/arch/x86/monitor.c +16 -0
index 896acf7..f8df7d2 100644
@@ -161,6 +161,22 @@ int monitor_domctl(struct domain *d, struct xen_domctl_monitor_op *mop)
         break;
     }
 
+    case XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST:
+    {
+        bool_t status = ad->monitor.guest_request_enabled;
+
+        rc = status_check(mop, status);
+        if ( rc )
+            return rc;
+
+        ad->monitor.guest_request_sync = mop->u.guest_request.sync;
+
+        domain_pause(d);
+        ad->monitor.guest_request_enabled = !status;
+        domain_unpause(d);
+        break;
+    }
+
     default:
         return -EOPNOTSUPP;
 
xen/include/asm-x86/domain.h +9 -7
index 7908844..f712caa 100644
@@ -346,13 +346,15 @@ struct arch_domain
 
     /* Monitor options */
     struct {
-        uint16_t write_ctrlreg_enabled       : 4;
-        uint16_t write_ctrlreg_sync          : 4;
-        uint16_t write_ctrlreg_onchangeonly  : 4;
-        uint16_t mov_to_msr_enabled          : 1;
-        uint16_t mov_to_msr_extended         : 1;
-        uint16_t singlestep_enabled          : 1;
-        uint16_t software_breakpoint_enabled : 1;
+        unsigned int write_ctrlreg_enabled       : 4;
+        unsigned int write_ctrlreg_sync          : 4;
+        unsigned int write_ctrlreg_onchangeonly  : 4;
+        unsigned int mov_to_msr_enabled          : 1;
+        unsigned int mov_to_msr_extended         : 1;
+        unsigned int singlestep_enabled          : 1;
+        unsigned int software_breakpoint_enabled : 1;
+        unsigned int guest_request_enabled       : 1;
+        unsigned int guest_request_sync          : 1;
     } monitor;
 
     /* Mem_access emulation control */
xen/include/asm-x86/hvm/event.h +1 -0
index 819ef96..ab5abd0 100644
@@ -26,6 +26,7 @@ void hvm_event_msr(unsigned int msr, uint64_t value);
 /* Called for current VCPU: returns -1 if no listener */
 int hvm_event_int3(unsigned long gla);
 int hvm_event_single_step(unsigned long gla);
+void hvm_event_guest_request(void);
 
 #endif /* __ASM_X86_HVM_EVENT_H__ */
 
xen/include/public/domctl.h +6 -0
index bc45ea5..6812016 100644
@@ -1012,6 +1012,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t);
 #define XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR            1
 #define XEN_DOMCTL_MONITOR_EVENT_SINGLESTEP            2
 #define XEN_DOMCTL_MONITOR_EVENT_SOFTWARE_BREAKPOINT   3
+#define XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST         4
 
 struct xen_domctl_monitor_op {
     uint32_t op; /* XEN_DOMCTL_MONITOR_OP_* */
@@ -1034,6 +1035,11 @@ struct xen_domctl_monitor_op {
             /* Enable the capture of an extended set of MSRs */
             uint8_t extended_capture;
         } mov_to_msr;
+
+        struct {
+            /* Pause vCPU until response */
+            uint8_t sync;
+        } guest_request;
     } u;
 };
 typedef struct xen_domctl_monitor_op xen_domctl_monitor_op_t;
xen/include/public/hvm/hvm_op.h +2 -0
index 9b84e84..d053db9 100644
@@ -396,6 +396,8 @@ DEFINE_XEN_GUEST_HANDLE(xen_hvm_evtchn_upcall_vector_t);
 
 #endif /* defined(__i386__) || defined(__x86_64__) */
 
+#define HVMOP_guest_request_vm_event 24
+
 #endif /* __XEN_PUBLIC_HVM_HVM_OP_H__ */
 
 /*
xen/include/public/vm_event.h +2 -0
index c6f69af..11e65c4 100644
@@ -90,6 +90,8 @@
 #define VM_EVENT_REASON_SOFTWARE_BREAKPOINT     6
 /* Single-step (e.g. MTF) */
 #define VM_EVENT_REASON_SINGLESTEP              7
+/* An event has been requested via HVMOP_guest_request_vm_event. */
+#define VM_EVENT_REASON_GUEST_REQUEST           8
 
 /* Supported values for the vm_event_write_ctrlreg index. */
 #define VM_EVENT_X86_CR0    0
Signature · 1 line
1.7.9.5
Acked-by on v4 2/3
#6 lore raw

Re: [PATCH V4 2/3] xen/vm_event: Support for guest-requested events

Quoted 8 lines · Razvan Cojocaru

Added support for a new class of vm_events: VM_EVENT_REASON_REQUEST, sent via HVMOP_request_vm_event. The guest can request that a generic vm_event (containing only the vm_event-filled guest registers as information) be sent to userspace by setting up the correct registers and doing a VMCALL. For example, for a 32-bit guest, this
means: EAX = 34 (hvmop), EBX = 24 (HVMOP_guest_request_vm_event), ECX = 0 (NULL required for the hypercall parameter, reserved).

Quoted 8 lines

---
Changes since V3:
- None, just addded acks.

And Razvan answered my question, so FWIW:

Quoted 219 lines
---
 tools/libxc/include/xenctrl.h   |    2 ++
 tools/libxc/xc_monitor.c        |   15 +++++++++++++++
 xen/arch/x86/hvm/event.c        |   16 ++++++++++++++++
 xen/arch/x86/hvm/hvm.c          |    8 +++++++-
 xen/arch/x86/monitor.c          |   16 ++++++++++++++++
 xen/include/asm-x86/domain.h    |   16 +++++++++-------
 xen/include/asm-x86/hvm/event.h |    1 +
 xen/include/public/domctl.h     |    6 ++++++
 xen/include/public/hvm/hvm_op.h |    2 ++
 xen/include/public/vm_event.h   |    2 ++
 10 files changed, 76 insertions(+), 8 deletions(-)
tools/libxc/include/xenctrl.h +2 -0
index d1d2ab3..4ce519a 100644
@@ -2384,6 +2384,8 @@ int xc_monitor_mov_to_msr(xc_interface *xch, domid_t domain_id, bool enable,
 int xc_monitor_singlestep(xc_interface *xch, domid_t domain_id, bool enable);
 int xc_monitor_software_breakpoint(xc_interface *xch, domid_t domain_id,
                                    bool enable);
+int xc_monitor_guest_request(xc_interface *xch, domid_t domain_id,
+                             bool enable, bool sync);
 
 /***
  * Memory sharing operations.
tools/libxc/xc_monitor.c +15 -0
index 63013de..d979122 100644
@@ -105,3 +105,18 @@ int xc_monitor_singlestep(xc_interface *xch, domid_t domain_id,
 
     return do_domctl(xch, &domctl);
 }
+
+int xc_monitor_guest_request(xc_interface *xch, domid_t domain_id, bool enable,
+                             bool sync)
+{
+    DECLARE_DOMCTL;
+
+    domctl.cmd = XEN_DOMCTL_monitor_op;
+    domctl.domain = domain_id;
+    domctl.u.monitor_op.op = enable ? XEN_DOMCTL_MONITOR_OP_ENABLE
+                                    : XEN_DOMCTL_MONITOR_OP_DISABLE;
+    domctl.u.monitor_op.event = XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST;
+    domctl.u.monitor_op.u.guest_request.sync = sync;
+
+    return do_domctl(xch, &domctl);
+}
xen/arch/x86/hvm/event.c +16 -0
index 5341937..17638ea 100644
@@ -126,6 +126,22 @@ void hvm_event_msr(unsigned int msr, uint64_t value)
         hvm_event_traps(1, &req);
 }
 
+void hvm_event_guest_request(void)
+{
+    struct vcpu *curr = current;
+    struct arch_domain *currad = &curr->domain->arch;
+
+    if ( currad->monitor.guest_request_enabled )
+    {
+        vm_event_request_t req = {
+            .reason = VM_EVENT_REASON_GUEST_REQUEST,
+            .vcpu_id = curr->vcpu_id,
+        };
+
+        hvm_event_traps(currad->monitor.guest_request_sync, &req);
+    }
+}
+
 int hvm_event_int3(unsigned long gla)
 {
     int rc = 0;
xen/arch/x86/hvm/hvm.c +7 -1
index 535d622..536d1c8 100644
@@ -5974,7 +5974,6 @@ static int hvmop_get_param(
 #define HVMOP_op_mask 0xff
 
 long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
-
 {
     unsigned long start_iter, mask;
     long rc = 0;
@@ -6388,6 +6387,13 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 
+    case HVMOP_guest_request_vm_event:
+        if ( guest_handle_is_null(arg) )
+            hvm_event_guest_request();
+        else
+            rc = -EINVAL;
+        break;
+
     default:
     {
         gdprintk(XENLOG_DEBUG, "Bad HVM op %ld.\n", op);
xen/arch/x86/monitor.c +16 -0
index 896acf7..f8df7d2 100644
@@ -161,6 +161,22 @@ int monitor_domctl(struct domain *d, struct xen_domctl_monitor_op *mop)
         break;
     }
 
+    case XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST:
+    {
+        bool_t status = ad->monitor.guest_request_enabled;
+
+        rc = status_check(mop, status);
+        if ( rc )
+            return rc;
+
+        ad->monitor.guest_request_sync = mop->u.guest_request.sync;
+
+        domain_pause(d);
+        ad->monitor.guest_request_enabled = !status;
+        domain_unpause(d);
+        break;
+    }
+
     default:
         return -EOPNOTSUPP;
 
xen/include/asm-x86/domain.h +9 -7
index 7908844..f712caa 100644
@@ -346,13 +346,15 @@ struct arch_domain
 
     /* Monitor options */
     struct {
-        uint16_t write_ctrlreg_enabled       : 4;
-        uint16_t write_ctrlreg_sync          : 4;
-        uint16_t write_ctrlreg_onchangeonly  : 4;
-        uint16_t mov_to_msr_enabled          : 1;
-        uint16_t mov_to_msr_extended         : 1;
-        uint16_t singlestep_enabled          : 1;
-        uint16_t software_breakpoint_enabled : 1;
+        unsigned int write_ctrlreg_enabled       : 4;
+        unsigned int write_ctrlreg_sync          : 4;
+        unsigned int write_ctrlreg_onchangeonly  : 4;
+        unsigned int mov_to_msr_enabled          : 1;
+        unsigned int mov_to_msr_extended         : 1;
+        unsigned int singlestep_enabled          : 1;
+        unsigned int software_breakpoint_enabled : 1;
+        unsigned int guest_request_enabled       : 1;
+        unsigned int guest_request_sync          : 1;
     } monitor;
 
     /* Mem_access emulation control */
xen/include/asm-x86/hvm/event.h +1 -0
index 819ef96..ab5abd0 100644
@@ -26,6 +26,7 @@ void hvm_event_msr(unsigned int msr, uint64_t value);
 /* Called for current VCPU: returns -1 if no listener */
 int hvm_event_int3(unsigned long gla);
 int hvm_event_single_step(unsigned long gla);
+void hvm_event_guest_request(void);
 
 #endif /* __ASM_X86_HVM_EVENT_H__ */
 
xen/include/public/domctl.h +6 -0
index bc45ea5..6812016 100644
@@ -1012,6 +1012,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t);
 #define XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR            1
 #define XEN_DOMCTL_MONITOR_EVENT_SINGLESTEP            2
 #define XEN_DOMCTL_MONITOR_EVENT_SOFTWARE_BREAKPOINT   3
+#define XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST         4
 
 struct xen_domctl_monitor_op {
     uint32_t op; /* XEN_DOMCTL_MONITOR_OP_* */
@@ -1034,6 +1035,11 @@ struct xen_domctl_monitor_op {
             /* Enable the capture of an extended set of MSRs */
             uint8_t extended_capture;
         } mov_to_msr;
+
+        struct {
+            /* Pause vCPU until response */
+            uint8_t sync;
+        } guest_request;
     } u;
 };
 typedef struct xen_domctl_monitor_op xen_domctl_monitor_op_t;
xen/include/public/hvm/hvm_op.h +2 -0
index 9b84e84..d053db9 100644
@@ -396,6 +396,8 @@ DEFINE_XEN_GUEST_HANDLE(xen_hvm_evtchn_upcall_vector_t);
 
 #endif /* defined(__i386__) || defined(__x86_64__) */
 
+#define HVMOP_guest_request_vm_event 24
+
 #endif /* __XEN_PUBLIC_HVM_HVM_OP_H__ */
 
 /*
xen/include/public/vm_event.h +2 -0
index c6f69af..11e65c4 100644
@@ -90,6 +90,8 @@
 #define VM_EVENT_REASON_SOFTWARE_BREAKPOINT     6
 /* Single-step (e.g. MTF) */
 #define VM_EVENT_REASON_SINGLESTEP              7
+/* An event has been requested via HVMOP_guest_request_vm_event. */
+#define VM_EVENT_REASON_GUEST_REQUEST           8
 
 /* Supported values for the vm_event_write_ctrlreg index. */
 #define VM_EVENT_X86_CR0    0
#7 lore raw

PATCH v4 3/3 xen/vm_event: Deny register writes if refused by vm_event reply

In reply to Razvan Cojocaru

Deny register writes if a vm_client subscribed to mov_to_msr or control register write events forbids them. Currently supported for MSR, CR0, CR3 and CR4 events.

---
Changes since V3:
- Renamed MEM_ACCESS_FLAG_DENY to VM_EVENT_FLAG_DENY (and fixed the bit shift appropriately).
- Moved the DENY vm_event response logic from p2m.c to newly added dedicated files for vm_event handling, as suggested by Tamas Lengyel.

---
 MAINTAINERS                       |    1 +
 xen/arch/x86/Makefile             |    1 +
 xen/arch/x86/domain.c             |    2 +
 xen/arch/x86/hvm/emulate.c        |    8 +--
 xen/arch/x86/hvm/event.c          |    5 +-
 xen/arch/x86/hvm/hvm.c            |  118 ++++++++++++++++++++++++++++++++-----
 xen/arch/x86/hvm/svm/nestedsvm.c  |   14 ++---
 xen/arch/x86/hvm/svm/svm.c        |    2 +-
 xen/arch/x86/hvm/vmx/vmx.c        |   15 +++--
 xen/arch/x86/hvm/vmx/vvmx.c       |   18 +++---
 xen/arch/x86/vm_event.c           |   33 +++++++++++
 xen/common/vm_event.c             |    9 +++
 xen/include/asm-arm/vm_event.h    |   12 ++++
 xen/include/asm-x86/domain.h      |   18 +++++-
 xen/include/asm-x86/hvm/event.h   |    9 ++-
 xen/include/asm-x86/hvm/support.h |    9 +--
 xen/include/asm-x86/vm_event.h    |    8 +++
 xen/include/public/vm_event.h     |    6 ++
 18 files changed, 242 insertions(+), 46 deletions(-)
 create mode 100644 xen/arch/x86/vm_event.c
 create mode 100644 xen/include/asm-arm/vm_event.h
 create mode 100644 xen/include/asm-x86/vm_event.h
MAINTAINERS +1 -0
index 6b1068e..59c0822 100644
@@ -383,6 +383,7 @@ F:	xen/common/vm_event.c
 F:	xen/common/mem_access.c
 F:	xen/arch/x86/hvm/event.c
 F:	xen/arch/x86/monitor.c
+F:	xen/arch/x86/vm_event.c
 
 XENTRACE
 M:	George Dunlap <[email protected]>
xen/arch/x86/Makefile +1 -0
index 37e547c..5f24951 100644
@@ -60,6 +60,7 @@ obj-y += machine_kexec.o
 obj-y += crash.o
 obj-y += tboot.o
 obj-y += hpet.o
+obj-y += vm_event.o
 obj-y += xstate.o
 
 obj-$(crash_debug) += gdbstub.o
xen/arch/x86/domain.c +2 -0
index a8fe046..c688ab9 100644
@@ -678,6 +678,8 @@ void arch_domain_destroy(struct domain *d)
     cleanup_domain_irq_mapping(d);
 
     psr_free_rmid(d);
+
+    xfree(d->arch.event_write_data);
 }
 
 void arch_domain_shutdown(struct domain *d)
xen/arch/x86/hvm/emulate.c +4 -4
index c6ccb1f..780adb4 100644
@@ -1389,14 +1389,14 @@ static int hvmemul_write_cr(
     switch ( reg )
     {
     case 0:
-        return hvm_set_cr0(val);
+        return hvm_set_cr0(val, 1);
     case 2:
         current->arch.hvm_vcpu.guest_cr[2] = val;
         return X86EMUL_OKAY;
     case 3:
-        return hvm_set_cr3(val);
+        return hvm_set_cr3(val, 1);
     case 4:
-        return hvm_set_cr4(val);
+        return hvm_set_cr4(val, 1);
     default:
         break;
     }
@@ -1417,7 +1417,7 @@ static int hvmemul_write_msr(
     uint64_t val,
     struct x86_emulate_ctxt *ctxt)
 {
-    return hvm_msr_write_intercept(reg, val);
+    return hvm_msr_write_intercept(reg, val, 1);
 }
 
 static int hvmemul_wbinvd(
xen/arch/x86/hvm/event.c +4 -1
index 17638ea..042e583 100644
@@ -90,7 +90,7 @@ static int hvm_event_traps(uint8_t sync, vm_event_request_t *req)
     return 1;
 }
 
-void hvm_event_cr(unsigned int index, unsigned long value, unsigned long old)
+bool_t hvm_event_cr(unsigned int index, unsigned long value, unsigned long old)
 {
     struct arch_domain *currad = &current->domain->arch;
     unsigned int ctrlreg_bitmask = monitor_ctrlreg_bitmask(index);
@@ -109,7 +109,10 @@ void hvm_event_cr(unsigned int index, unsigned long value, unsigned long old)
 
         hvm_event_traps(currad->monitor.write_ctrlreg_sync & ctrlreg_bitmask,
                         &req);
+        return 1;
     }
+
+    return 0;
 }
 
 void hvm_event_msr(unsigned int msr, uint64_t value)
xen/arch/x86/hvm/hvm.c +104 -14

219 lines not on this page · raw message

xen/arch/x86/hvm/svm/nestedsvm.c +7 -7
index be5797a..07e3cad 100644
@@ -274,7 +274,7 @@ int nsvm_vcpu_hostrestore(struct vcpu *v, struct cpu_user_regs *regs)
 
     /* CR4 */
     v->arch.hvm_vcpu.guest_cr[4] = n1vmcb->_cr4;
-    rc = hvm_set_cr4(n1vmcb->_cr4);
+    rc = hvm_set_cr4(n1vmcb->_cr4, 1);
     if (rc != X86EMUL_OKAY)
         gdprintk(XENLOG_ERR, "hvm_set_cr4 failed, rc: %u\n", rc);
 
@@ -283,7 +283,7 @@ int nsvm_vcpu_hostrestore(struct vcpu *v, struct cpu_user_regs *regs)
         svm->ns_cr0, v->arch.hvm_vcpu.guest_cr[0]);
     v->arch.hvm_vcpu.guest_cr[0] = n1vmcb->_cr0 | X86_CR0_PE;
     n1vmcb->rflags &= ~X86_EFLAGS_VM;
-    rc = hvm_set_cr0(n1vmcb->_cr0 | X86_CR0_PE);
+    rc = hvm_set_cr0(n1vmcb->_cr0 | X86_CR0_PE, 1);
     if (rc != X86EMUL_OKAY)
         gdprintk(XENLOG_ERR, "hvm_set_cr0 failed, rc: %u\n", rc);
     svm->ns_cr0 = v->arch.hvm_vcpu.guest_cr[0];
@@ -309,7 +309,7 @@ int nsvm_vcpu_hostrestore(struct vcpu *v, struct cpu_user_regs *regs)
         v->arch.guest_table = pagetable_null();
         /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us. */
     }
-    rc = hvm_set_cr3(n1vmcb->_cr3);
+    rc = hvm_set_cr3(n1vmcb->_cr3, 1);
     if (rc != X86EMUL_OKAY)
         gdprintk(XENLOG_ERR, "hvm_set_cr3 failed, rc: %u\n", rc);
 
@@ -534,7 +534,7 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
 
     /* CR4 */
     v->arch.hvm_vcpu.guest_cr[4] = ns_vmcb->_cr4;
-    rc = hvm_set_cr4(ns_vmcb->_cr4);
+    rc = hvm_set_cr4(ns_vmcb->_cr4, 1);
     if (rc != X86EMUL_OKAY)
         gdprintk(XENLOG_ERR, "hvm_set_cr4 failed, rc: %u\n", rc);
 
@@ -542,7 +542,7 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
     svm->ns_cr0 = v->arch.hvm_vcpu.guest_cr[0];
     cr0 = nestedsvm_fpu_vmentry(svm->ns_cr0, ns_vmcb, n1vmcb, n2vmcb);
     v->arch.hvm_vcpu.guest_cr[0] = ns_vmcb->_cr0;
-    rc = hvm_set_cr0(cr0);
+    rc = hvm_set_cr0(cr0, 1);
     if (rc != X86EMUL_OKAY)
         gdprintk(XENLOG_ERR, "hvm_set_cr0 failed, rc: %u\n", rc);
 
@@ -558,7 +558,7 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
         nestedsvm_vmcb_set_nestedp2m(v, ns_vmcb, n2vmcb);
 
         /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us. */
-        rc = hvm_set_cr3(ns_vmcb->_cr3);
+        rc = hvm_set_cr3(ns_vmcb->_cr3, 1);
         if (rc != X86EMUL_OKAY)
             gdprintk(XENLOG_ERR, "hvm_set_cr3 failed, rc: %u\n", rc);
     } else if (paging_mode_hap(v->domain)) {
@@ -570,7 +570,7 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
          * we assume it intercepts page faults.
          */
         /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us. */
-        rc = hvm_set_cr3(ns_vmcb->_cr3);
+        rc = hvm_set_cr3(ns_vmcb->_cr3, 1);
         if (rc != X86EMUL_OKAY)
             gdprintk(XENLOG_ERR, "hvm_set_cr3 failed, rc: %u\n", rc);
     } else {
xen/arch/x86/hvm/svm/svm.c +1 -1
index a02f983..5e39b88 100644
@@ -1945,7 +1945,7 @@ static void svm_do_msr_access(struct cpu_user_regs *regs)
         if ( (inst_len = __get_instruction_length(v, INSTR_WRMSR)) == 0 )
             return;
         msr_content = ((uint64_t)regs->edx << 32) | (uint32_t)regs->eax;
-        rc = hvm_msr_write_intercept(regs->ecx, msr_content);
+        rc = hvm_msr_write_intercept(regs->ecx, msr_content, 1);
     }
 
     if ( rc == X86EMUL_OKAY )
xen/arch/x86/hvm/vmx/vmx.c +11 -4
index fc29b89..96c55ec 100644
@@ -2010,9 +2010,16 @@ static int vmx_cr_access(unsigned long exit_qualification)
     }
     case VMX_CONTROL_REG_ACCESS_TYPE_CLTS: {
         unsigned long old = curr->arch.hvm_vcpu.guest_cr[0];
-        curr->arch.hvm_vcpu.guest_cr[0] &= ~X86_CR0_TS;
+        unsigned long value = old & ~X86_CR0_TS;
+
+        /*
+         * Special case unlikely to be interesting to a
+         * VM_EVENT_FLAG_DENY-capable application, so the hvm_event_crX()
+         * return value is ignored for now.
+         */
+        hvm_event_crX(CR0, value, old);
+        curr->arch.hvm_vcpu.guest_cr[0] = value;
         vmx_update_guest_cr(curr, 0);
-        hvm_event_crX(CR0, curr->arch.hvm_vcpu.guest_cr[0], old);
         HVMTRACE_0D(CLTS);
         break;
     }
@@ -2024,7 +2031,7 @@ static int vmx_cr_access(unsigned long exit_qualification)
                 (VMX_CONTROL_REG_ACCESS_DATA(exit_qualification) &
                  (X86_CR0_PE|X86_CR0_MP|X86_CR0_EM|X86_CR0_TS));
         HVMTRACE_LONG_1D(LMSW, value);
-        return hvm_set_cr0(value);
+        return hvm_set_cr0(value, 1);
     }
     default:
         BUG();
@@ -3035,7 +3042,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
     {
         uint64_t msr_content;
         msr_content = ((uint64_t)regs->edx << 32) | (uint32_t)regs->eax;
-        if ( hvm_msr_write_intercept(regs->ecx, msr_content) == X86EMUL_OKAY )
+        if ( hvm_msr_write_intercept(regs->ecx, msr_content, 1) == X86EMUL_OKAY )
             update_guest_eip(); /* Safe: WRMSR */
         break;
     }
xen/arch/x86/hvm/vmx/vvmx.c +10 -8
index ac6e3b3..52bf39c 100644
@@ -1048,15 +1048,16 @@ static void load_shadow_guest_state(struct vcpu *v)
 
     nvcpu->guest_cr[0] = __get_vvmcs(vvmcs, CR0_READ_SHADOW);
     nvcpu->guest_cr[4] = __get_vvmcs(vvmcs, CR4_READ_SHADOW);
-    hvm_set_cr0(__get_vvmcs(vvmcs, GUEST_CR0));
-    hvm_set_cr4(__get_vvmcs(vvmcs, GUEST_CR4));
-    hvm_set_cr3(__get_vvmcs(vvmcs, GUEST_CR3));
+    hvm_set_cr0(__get_vvmcs(vvmcs, GUEST_CR0), 1);
+    hvm_set_cr4(__get_vvmcs(vvmcs, GUEST_CR4), 1);
+    hvm_set_cr3(__get_vvmcs(vvmcs, GUEST_CR3), 1);
 
     control = __get_vvmcs(vvmcs, VM_ENTRY_CONTROLS);
     if ( control & VM_ENTRY_LOAD_GUEST_PAT )
         hvm_set_guest_pat(v, __get_vvmcs(vvmcs, GUEST_PAT));
     if ( control & VM_ENTRY_LOAD_PERF_GLOBAL_CTRL )
-        hvm_msr_write_intercept(MSR_CORE_PERF_GLOBAL_CTRL, __get_vvmcs(vvmcs, GUEST_PERF_GLOBAL_CTRL));
+        hvm_msr_write_intercept(MSR_CORE_PERF_GLOBAL_CTRL,
+                                __get_vvmcs(vvmcs, GUEST_PERF_GLOBAL_CTRL), 0);
 
     hvm_funcs.set_tsc_offset(v, v->arch.hvm_vcpu.cache_tsc_offset, 0);
 
@@ -1249,15 +1250,16 @@ static void load_vvmcs_host_state(struct vcpu *v)
         __vmwrite(vmcs_h2g_field[i].guest_field, r);
     }
 
-    hvm_set_cr0(__get_vvmcs(vvmcs, HOST_CR0));
-    hvm_set_cr4(__get_vvmcs(vvmcs, HOST_CR4));
-    hvm_set_cr3(__get_vvmcs(vvmcs, HOST_CR3));
+    hvm_set_cr0(__get_vvmcs(vvmcs, HOST_CR0), 1);
+    hvm_set_cr4(__get_vvmcs(vvmcs, HOST_CR4), 1);
+    hvm_set_cr3(__get_vvmcs(vvmcs, HOST_CR3), 1);
 
     control = __get_vvmcs(vvmcs, VM_EXIT_CONTROLS);
     if ( control & VM_EXIT_LOAD_HOST_PAT )
         hvm_set_guest_pat(v, __get_vvmcs(vvmcs, HOST_PAT));
     if ( control & VM_EXIT_LOAD_PERF_GLOBAL_CTRL )
-        hvm_msr_write_intercept(MSR_CORE_PERF_GLOBAL_CTRL, __get_vvmcs(vvmcs, HOST_PERF_GLOBAL_CTRL));
+        hvm_msr_write_intercept(MSR_CORE_PERF_GLOBAL_CTRL,
+                                __get_vvmcs(vvmcs, HOST_PERF_GLOBAL_CTRL), 1);
 
     hvm_funcs.set_tsc_offset(v, v->arch.hvm_vcpu.cache_tsc_offset, 0);
 
xen/arch/x86/vm_event.c new file +33 -0
new file mode 100644
index 0000000..aca731d
@@ -0,0 +1,33 @@
+#include <asm/vm_event.h>
+
+void vm_event_register_write_resume(struct vcpu *v, vm_event_response_t *rsp)
+{
+    if ( rsp->flags & VM_EVENT_FLAG_DENY )
+    {
+        struct monitor_write_data *w =
+            &v->domain->arch.event_write_data[v->vcpu_id];
+
+        ASSERT(v->domain->arch.event_write_data != NULL);
+
+        switch ( rsp->reason )
+        {
+        case VM_EVENT_REASON_MOV_TO_MSR:
+            w->do_write.msr = 0;
+            break;
+        case VM_EVENT_REASON_WRITE_CTRLREG:
+            switch ( rsp->u.write_ctrlreg.index )
+            {
+            case VM_EVENT_X86_CR0:
+                w->do_write.cr0 = 0;
+                break;
+            case VM_EVENT_X86_CR3:
+                w->do_write.cr3 = 0;
+                break;
+            case VM_EVENT_X86_CR4:
+                w->do_write.cr4 = 0;
+                break;
+            }
+            break;
+        }
+    }
+}
xen/common/vm_event.c +9 -0
index 11438da..eea035c 100644
@@ -26,6 +26,7 @@
 #include <xen/wait.h>
 #include <xen/vm_event.h>
 #include <xen/mem_access.h>
+#include <asm/vm_event.h>
 #include <asm/p2m.h>
 #include <xsm/xsm.h>
 
@@ -79,6 +80,10 @@ static int vm_event_enable(
         }
     }
 
+    if ( !d->arch.event_write_data )
+        d->arch.event_write_data = xzalloc_array(struct monitor_write_data,
+                                                 d->max_vcpus);
+
     rc = prepare_ring_for_helper(d, ring_gfn, &ved->ring_pg_struct,
                                     &ved->ring_page);
     if ( rc < 0 )
@@ -407,6 +412,10 @@ void vm_event_resume(struct domain *d, struct vm_event_domain *ved)
          */
         switch ( rsp.reason )
         {
+        case VM_EVENT_REASON_MOV_TO_MSR:
+        case VM_EVENT_REASON_WRITE_CTRLREG:
+            vm_event_register_write_resume(v, &rsp);
+            break;
 
 #ifdef HAS_MEM_ACCESS
         case VM_EVENT_REASON_MEM_ACCESS:
xen/include/asm-arm/vm_event.h new file +12 -0
new file mode 100644
index 0000000..5205ee8
@@ -0,0 +1,12 @@
+#ifndef __ASM_ARM_VM_EVENT_H__
+#define __ASM_ARM_VM_EVENT_H__
+
+#include <xen/vm_event.h>
+
+static inline
+void vm_event_register_write_resume(struct vcpu *v, vm_event_response_t *rsp)
+{
+    /* Not supported on ARM. */
+}
+
+#endif /* __ASM_ARM_VM_EVENT_H__ */
xen/include/asm-x86/domain.h +17 -1
index f712caa..8990277 100644
@@ -249,6 +249,21 @@ struct pv_domain
     struct mapcache_domain mapcache;
 };
 
+struct monitor_write_data {
+    struct {
+        unsigned int msr : 1;
+        unsigned int cr0 : 1;
+        unsigned int cr3 : 1;
+        unsigned int cr4 : 1;
+    } do_write;
+
+    uint32_t msr;
+    uint64_t value;
+    uint64_t cr0;
+    uint64_t cr3;
+    uint64_t cr4;
+};
+
 struct arch_domain
 {
     struct page_info *perdomain_l3_pg;
@@ -359,6 +374,8 @@ struct arch_domain
 
     /* Mem_access emulation control */
     bool_t mem_access_emulate_enabled;
+
+    struct monitor_write_data *event_write_data;
 } __cacheline_aligned;
 
 #define has_arch_pdevs(d)    (!list_empty(&(d)->arch.pdev_list))
@@ -513,7 +530,6 @@ struct arch_vcpu
         unsigned long eip;
         struct vm_event_emul_read_data *emul_read_data;
     } vm_event;
-
 };
 
 smap_check_policy_t smap_policy_change(struct vcpu *v,
xen/include/asm-x86/hvm/event.h +7 -2
index ab5abd0..c082c20 100644
@@ -18,8 +18,13 @@
 #ifndef __ASM_X86_HVM_EVENT_H__
 #define __ASM_X86_HVM_EVENT_H__
 
-/* Called for current VCPU on crX/MSR changes by guest */
-void hvm_event_cr(unsigned int index, unsigned long value, unsigned long old);
+/*
+ * Called for current VCPU on crX/MSR changes by guest.
+ * The event might not fire if the client has subscribed to it in onchangeonly
+ * mode, hence the bool_t return type for control register write events.
+ */
+bool_t hvm_event_cr(unsigned int index, unsigned long value,
+                    unsigned long old);
 #define hvm_event_crX(what, new, old) \
     hvm_event_cr(VM_EVENT_X86_##what, new, old)
 void hvm_event_msr(unsigned int msr, uint64_t value);
xen/include/asm-x86/hvm/support.h +5 -4
index 05ef5c5..95d3bb2 100644
@@ -124,11 +124,12 @@ void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value);
 
 /* These functions all return X86EMUL return codes. */
 int hvm_set_efer(uint64_t value);
-int hvm_set_cr0(unsigned long value);
-int hvm_set_cr3(unsigned long value);
-int hvm_set_cr4(unsigned long value);
+int hvm_set_cr0(unsigned long value, bool_t may_defer);
+int hvm_set_cr3(unsigned long value, bool_t may_defer);
+int hvm_set_cr4(unsigned long value, bool_t may_defer);
 int hvm_msr_read_intercept(unsigned int msr, uint64_t *msr_content);
-int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content);
+int hvm_msr_write_intercept(
+    unsigned int msr, uint64_t msr_content, bool_t may_defer);
 int hvm_mov_to_cr(unsigned int cr, unsigned int gpr);
 int hvm_mov_from_cr(unsigned int cr, unsigned int gpr);
 
xen/include/asm-x86/vm_event.h new file +8 -0
new file mode 100644
index 0000000..bc6e9f7
@@ -0,0 +1,8 @@
+#ifndef __ASM_X86_VM_EVENT_H__
+#define __ASM_X86_VM_EVENT_H__
+
+#include <xen/vm_event.h>
+
+void vm_event_register_write_resume(struct vcpu *v, vm_event_response_t *rsp);
+
+#endif /* __ASM_X86_VM_EVENT_H__ */
xen/include/public/vm_event.h +6 -0
index 11e65c4..0e175cc 100644
@@ -69,6 +69,12 @@
  * VM_EVENT_FLAG_SET_EMUL_READ_DATA are set, only the latter will be honored).
  */
 #define VM_EVENT_FLAG_SET_EMUL_READ_DATA (1 << 4)
+ /*
+  * Deny completion of the operation that triggered the event.
+  * Currently only useful for MSR, CR0, CR3 and CR4 write events.
+  */
+#define VM_EVENT_FLAG_DENY               (1 << 5)
+
 
 /*
  * Reasons for the vm event request
Signature · 1 line
1.7.9.5
Acked-by on v4 3/3
#8 lore raw

Re: [PATCH V4 3/3] xen/vm_event: Deny register writes if refused by vm_event reply

Quoted 8 lines · Razvan Cojocaru

Deny register writes if a vm_client subscribed to mov_to_msr or control register write events forbids them. Currently supported for MSR, CR0, CR3 and CR4 events.

Quoted 8 lines

---
Changes since V3:
- Renamed MEM_ACCESS_FLAG_DENY to VM_EVENT_FLAG_DENY (and fixed the bit shift appropriately).
- Moved the DENY vm_event response logic from p2m.c to newly added dedicated files for vm_event handling, as suggested by Tamas Lengyel.

This looks good to me. It will have to be rebased on staging once the other series is merged as couple things will conflict. If this series lands first however, the newly added asm/vm_event files lack the required license header.

With that:

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

Re: [PATCH V4 3/3] xen/vm_event: Deny register writes if refused by vm_event reply

Quoted 24 lines · Tamas

On Wed, Jul 8, 2015 at 6:22 AM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:

Deny register writes if a vm_client subscribed to mov_to_msr or control register write events forbids them. Currently supported for MSR, CR0, CR3 and CR4 events.

<mailto:[email protected]>>

<mailto:[email protected]>>

Acked-by Jan Beulich <[email protected] <mailto:[email protected]>>

---
Changes since V3:
- Renamed MEM_ACCESS_FLAG_DENY to VM_EVENT_FLAG_DENY (and fixed the bit shift appropriately).
- Moved the DENY vm_event response logic from p2m.c to newly added dedicated files for vm_event handling, as suggested by Tamas Lengyel.

Quoted 7 lines

This looks good to me. It will have to be rebased on staging once the other series is merged as couple things will conflict. If this series lands first however, the newly added asm/vm_event files lack the required license header.

With that:

Thanks Tamas!

Are the license headers required? I just tried to make the change as small as possible, and looking at the other headers (for example in xen/include/asm-arm), at least half of them have no license header. I'm guessing this is something we'd now like to start correcting in new patches?

Thanks,
Razvan

#10 lore raw

Re: [PATCH V4 3/3] xen/vm_event: Deny register writes if refused by vm_event reply

Quoted 11 lines

Are the license headers required? I just tried to make the change as small as possible, and looking at the other headers (for example in xen/include/asm-arm), at least half of them have no license header. I'm guessing this is something we'd now like to start correcting in new patches?

Thanks,
Razvan

The wiki's definition that goes with the Signed-off-by tag goes: "The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file;". So, the open source license should be indicated in the file. If it's a new file being created, I would say it's the creators responsibility to add the license. But I haven't seen any discussion/documentation on the matter so I'm just guessing.

Cheers,
Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#11 lore raw

Re: [PATCH V4 3/3] xen/vm_event: Deny register writes if refused by vm_event reply

Quoted 13 lines · Tamas

Are the license headers required? I just tried to make the change as small as possible, and looking at the other headers (for example in xen/include/asm-arm), at least half of them have no license header. I'm guessing this is something we'd now like to start correcting in new patches?

Thanks,
Razvan

Quoted 7 lines

The wiki's definition that goes with the Signed-off-by tag goes: "The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file;". So, the open source license should be indicated in the file. If it's a new file being created, I would say it's the creators responsibility to add the license. But I haven't seen any discussion/documentation on the matter so I'm just guessing.

That makes sense, thanks for pointing it out. I'm happy to resubmit with the license header if it is required. I'm also obviously OK with the license header you've provided, so if this series does land first and you don't mind, feel free to add the original license text to the files.

Not a problem either way.

Thanks,
Razvan