Skip to contentSkip to conversation

Vm_event memory introspection helpers

PATCH Superseded Razvan Cojocaru 4 participants 21 messages Jul 13, 2015 – Jul 14, 2015

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Vm_event memory introspection helpers

This series addresses V4 reviews (patch 1/3), and has been rebased to compile on top of Xen staging (which now includes a few patches from Tamas that prevent V4 from compiling).

I've also moved x86 logic in patch 3/3 to x86 source files, this seems to have gone unnoticed but would likely have not compiled on ARM.

I've removed George's ack from patch 1/3, but it most likely only pertained to the mm bits which have remained unchanged. George, if you stand by your ack with V5 then please re-add it, thanks!

Patches 2/3 and 3/3 do have acks.

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

Thanks in advance for your reviews,
Razvan

#1 lore raw

PATCH v5 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 VM_FLAG_ACCESS_EMULATE has been set to a vm_event response.

---
Changes since V4:
- Rebased the patch to take into account Tamas' "x86/vm_event:
toggle singlestep from vm_event response".
- Renamed MEM_ACCESS_EMULATE to VM_EVENT_FLAG_EMULATE in stale comment in vm_event.h.
- Factored out common code setting the read data in emulate.c.
- Made sure the x86-specific code doesn't leak in the common code (added two new functions to the x86 and ARM vm_event.c files).

---
 tools/tests/xen-access/xen-access.c |    2 +-
 xen/arch/x86/domain.c               |    2 +
 xen/arch/x86/hvm/emulate.c          |  106 ++++++++++++++++++++++++++++++++++-
 xen/arch/x86/hvm/event.c            |   50 ++++++++---------
 xen/arch/x86/mm/p2m.c               |   92 +++++++++++++++++-------------
 xen/arch/x86/vm_event.c             |   30 ++++++++++
 xen/common/vm_event.c               |    8 +++
 xen/include/asm-arm/vm_event.h      |   13 +++++
 xen/include/asm-x86/domain.h        |    1 +
 xen/include/asm-x86/hvm/emulate.h   |   10 +++-
 xen/include/asm-x86/vm_event.h      |    4 ++
 xen/include/public/vm_event.h       |   35 +++++++++---
 12 files changed, 275 insertions(+), 78 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/domain.c +2 -0
index 34ecd7c..6596408 100644
@@ -511,6 +511,8 @@ int vcpu_initialise(struct vcpu *v)
 
 void vcpu_destroy(struct vcpu *v)
 {
+    xfree(v->arch.vm_event.emul_read_data);
+
     if ( is_pv_32bit_vcpu(v) )
     {
         free_compat_arg_xlat(v);
xen/arch/x86/hvm/emulate.c +103 -3

170 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/arch/x86/vm_event.c +30 -0
index c390225..0515161 100644
@@ -23,6 +23,36 @@
 #include <xen/sched.h>
 #include <asm/hvm/hvm.h>
 
+int vm_event_init_domain(struct domain *d)
+{
+    struct vcpu *v;
+
+    for_each_vcpu( d, v )
+    {
+        if ( v->arch.vm_event.emul_read_data )
+            continue;
+
+        v->arch.vm_event.emul_read_data =
+            xzalloc(struct vm_event_emul_read_data);
+
+        if ( !v->arch.vm_event.emul_read_data )
+            return -ENOMEM;
+    }
+
+    return 0;
+}
+
+void vm_event_cleanup_domain(struct domain *d)
+{
+    struct vcpu *v;
+
+    for_each_vcpu( d, v )
+    {
+        xfree(v->arch.vm_event.emul_read_data);
+        v->arch.vm_event.emul_read_data = NULL;
+    }
+}
+
 void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v)
 {
     if ( !is_hvm_domain(d) || !atomic_read(&v->vm_event_pause_count) )
xen/common/vm_event.c +8 -0
index a4b9c36..0007d70 100644
@@ -64,6 +64,11 @@ static int vm_event_enable(
     vm_event_ring_lock_init(ved);
     vm_event_ring_lock(ved);
 
+    rc = vm_event_init_domain(d);
+
+    if ( rc < 0 )
+        goto err;
+
     rc = prepare_ring_for_helper(d, ring_gfn, &ved->ring_pg_struct,
                                     &ved->ring_page);
     if ( rc < 0 )
@@ -226,6 +231,9 @@ static int vm_event_disable(struct domain *d, struct vm_event_domain *ved)
 
         destroy_ring_for_helper(&ved->ring_page,
                                 ved->ring_pg_struct);
+
+        vm_event_cleanup_domain(d);
+
         vm_event_ring_unlock(ved);
     }
 
xen/include/asm-arm/vm_event.h +13 -0
index a517495..20469a8 100644
@@ -23,6 +23,19 @@
 #include <xen/sched.h>
 
 static inline
+int vm_event_init_domain(struct domain *d)
+{
+    /* Not supported on ARM. */
+    return 0;
+}
+
+static inline
+void vm_event_cleanup_domain(struct domain *d)
+{
+    /* Not supported on ARM. */
+}
+
+static inline
 void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v)
 {
     /* Not supported on ARM. */
xen/include/asm-x86/domain.h +1 -0
index 201436d..c5ad1cb 100644
@@ -512,6 +512,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/asm-x86/vm_event.h +4 -0
index 7cc3a3d..3881783 100644
@@ -22,6 +22,10 @@
 
 #include <xen/sched.h>
 
+int vm_event_init_domain(struct domain *d);
+
+void vm_event_cleanup_domain(struct domain *d);
+
 void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v);
 
 #endif /* __ASM_X86_VM_EVENT_H__ */
xen/include/public/vm_event.h +27 -8
index c756c7c..4d89c38 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 vm_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,17 +54,26 @@
  * 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
+ * Same as VM_EVENT_FLAG_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)
 /*
  * Toggle singlestepping on vm_event response.
  * Requires the vCPU to be paused already (synchronous events only).
  */
-#define VM_EVENT_FLAG_TOGGLE_SINGLESTEP (1 << 4)
+#define VM_EVENT_FLAG_TOGGLE_SINGLESTEP  (1 << 4)
+/*
+ * 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 << 5)
 
 /*
  * Reasons for the vm event request
@@ -194,6 +203,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_* */
@@ -211,8 +226,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
#2 lore raw

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

Quoted 8 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 VM_FLAG_ACCESS_EMULATE has been set to a vm_event response.

You seem to have missed my Acked-by I gave on v4 of this patch.

Cheers,
Tamas

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

#3 lore raw

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

Quoted 11 lines · Tamas

On Mon, Jul 13, 2015 at 1:14 PM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:

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 VM_FLAG_ACCESS_EMULATE has been set to a vm_event response.

Quoted 5 lines

<mailto:[email protected]>>

You seem to have missed my Acked-by I gave on v4 of this patch.

Right, sorry :)

In this case there's your ack and possibly George's for 1/3, so all the patches in the series have at least one ack.

Thanks,
Razvan

#4 lore raw

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

In reply to Razvan Cojocaru

xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v) · line 514

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:
xen/arch/x86/domain.c +1 -0
@@ -511,6 +511,8 @@ int vcpu_initialise(struct vcpu *v)
 
 void vcpu_destroy(struct vcpu *v)
 {
+    xfree(v->arch.vm_event.emul_read_data);

Is this still needed now that you have
vm_event_cleanup_domain()?

xen/arch/x86/hvm/emulate.c static int null_write(const struct hvm_io_handler *handler, · line 70

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +1 -0
@@ -67,6 +67,27 @@ static int null_write(const struct hvm_io_handler *handler,
     return X86EMUL_OKAY;
 }
 
+static int set_context_data(void *buffer, unsigned int bytes)

I think in the context of this function alone, "size" would be better than "bytes".

xen/arch/x86/hvm/emulate.c static int null_write(const struct hvm_io_handler *handler, · line 76

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +6 -0
+{
+    struct vcpu *curr = current;
+
+    if ( !curr->arch.vm_event.emul_read_data )
+        return X86EMUL_UNHANDLEABLE;
+    else

Else after the respective if() unconditionally returning is odd. Perhaps better to invert the if() condition...

xen/arch/x86/hvm/emulate.c static int null_write(const struct hvm_io_handler *handler, · line 83

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+    {
+        unsigned int safe_bytes =
+            min(bytes, curr->arch.vm_event.emul_read_data->size);
+
+        if ( safe_bytes )
+            memcpy(buffer, curr->arch.vm_event.emul_read_data->data,
+                   safe_bytes);

So why did you still keep this conditional?

Quoted 19 lines
xen/arch/x86/hvm/emulate.c +15 -0
@@ -1005,6 +1043,36 @@ static int hvmemul_rep_ins(
                                !!(ctxt->regs->eflags & X86_EFLAGS_DF), gpa);
 }
 
+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;
+    char *buf;
+    int rc;
+
+    buf = xmalloc_array(char, bytes);
+
+    if ( buf == NULL )

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

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+        return X86EMUL_UNHANDLEABLE;
+
+    rc = set_context_data(buf, bytes);
+
+    if ( rc != X86EMUL_OKAY )
+        goto out;
+
+    rc = hvmemul_do_pio_buffer(dst_port, bytes, IOREQ_WRITE, buf);
+
+out:

Labels should be indented by at least one space. But realistically the code wouldn't look worse without any goto...

Quoted 8 lines
xen/arch/x86/hvm/emulate.c +4 -0
@@ -1133,7 +1205,20 @@ static int hvmemul_rep_movs(
      */
     rc = hvm_copy_from_guest_phys(buf, sgpa, bytes);
     if ( rc == HVMCOPY_okay )
+    {
+        if ( unlikely(hvmemul_ctxt->set_context) )
+        {
+            rc = set_context_data(buf, bytes);

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

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +9 -0
+
+            if ( rc != X86EMUL_OKAY)
+            {
+                xfree(buf);
+                return rc;
+            }
+        }
+
         rc = hvm_copy_to_guest_phys(dgpa, buf, bytes);
+    }

Why do you not bypass hvm_copy_from_guest_phys() here? This way it would - afaict - become consistent with the other cases.

xen/arch/x86/vm_event.c line 30

Quoted 11 lines
xen/arch/x86/vm_event.c +5 -0
@@ -23,6 +23,36 @@
 #include <xen/sched.h>
 #include <asm/hvm/hvm.h>
 
+int vm_event_init_domain(struct domain *d)
+{
+    struct vcpu *v;
+
+    for_each_vcpu( d, v )

Either you consider for_each_vcpu a keyword-like thing, then this is missing a blank before the opening parenthesis, or you don't, in which case the blanks immediately inside the parentheses should be dropped.

Quoted 14 lines
xen/arch/x86/vm_event.c +14 -0
+    {
+        if ( v->arch.vm_event.emul_read_data )
+            continue;
+
+        v->arch.vm_event.emul_read_data =
+            xzalloc(struct vm_event_emul_read_data);
+
+        if ( !v->arch.vm_event.emul_read_data )
+            return -ENOMEM;
+    }
+
+    return 0;
+}
+

xen/arch/x86/vm_event.c line 54

Quoted 10 lines
xen/arch/x86/vm_event.c +10 -0
+void vm_event_cleanup_domain(struct domain *d)
+{
+    struct vcpu *v;
+
+    for_each_vcpu( d, v )
+    {
+        xfree(v->arch.vm_event.emul_read_data);
+        v->arch.vm_event.emul_read_data = NULL;
+    }
+}

There not being a race here is attributed to vm_event_enable() being implicitly serialized by the domctl lock, and vm_event_disable(), beyond its domctl use, only being on domain cleanup paths? Would be nice to have a comment to that effect.

Jan

#5 lore raw

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

Quoted 11 lines · Jan Beulich

xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v) · line 514

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:
xen/arch/x86/domain.c +1 -0
@@ -511,6 +511,8 @@ int vcpu_initialise(struct vcpu *v)
 
 void vcpu_destroy(struct vcpu *v)
 {
+    xfree(v->arch.vm_event.emul_read_data);

Is this still needed now that you have
vm_event_cleanup_domain()?

I had thought that there might be a code path where vm_event_cleanup_domain() doesn't get called and yet the domain is being destroyed, but I can't find anything obvious in the code except a comment in arch/x86/mm/shadow/common.c - shadow_final_teardown() - stating that "It is possible for a domain that never got domain_kill()ed to get here with its shadow allocation intact.".

Since common/domain.c's domain_kill() seems to be the only caller of vm_event_cleanup(), I took that comment to mean that it could be possible to end up in vcpu_destroy() without vm_event_cleanup_domain() having been called, so I took the better-safe-than-sorry approach.

That is also why I'm setting v->arch.vm_event.emul_read_data to NULL in the vm_event domain cleanup function, so that this xfree() does not double-free.

But this xfree() is probably not needed, so if confirmed I'll remove it.

Quoted 10 lines

xen/arch/x86/hvm/emulate.c static int null_write(const struct hvm_io_handler *handler, · line 70

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +1 -0
@@ -67,6 +67,27 @@ static int null_write(const struct hvm_io_handler *handler,
     return X86EMUL_OKAY;
 }
 
+static int set_context_data(void *buffer, unsigned int bytes)

I think in the context of this function alone, "size" would be better than "bytes".

Ack.

Quoted 9 lines

xen/arch/x86/hvm/emulate.c static int null_write(const struct hvm_io_handler *handler, · line 76

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +6 -0
+{
+    struct vcpu *curr = current;
+
+    if ( !curr->arch.vm_event.emul_read_data )
+        return X86EMUL_UNHANDLEABLE;
+    else

Else after the respective if() unconditionally returning is odd. Perhaps better to invert the if() condition...

I agree, I only used the else so that I wouldn't need to put safe_bytes on the stack if I didn't have to. I'll invert the condition.

Quoted 9 lines

xen/arch/x86/hvm/emulate.c static int null_write(const struct hvm_io_handler *handler, · line 83

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+    {
+        unsigned int safe_bytes =
+            min(bytes, curr->arch.vm_event.emul_read_data->size);
+
+        if ( safe_bytes )
+            memcpy(buffer, curr->arch.vm_event.emul_read_data->data,
+                   safe_bytes);

So why did you still keep this conditional?

I thought a memcpy() call that ends up doing nothing (copying 0 bytes) would be expensive and I've tried to optimize the code by not doing the call if safe_bytes == 0.

Since nobody else seems to think it's worth it, I'll remove it.

Quoted 24 lines
Quoted 24 lines
xen/arch/x86/hvm/emulate.c +20 -0
@@ -1005,6 +1043,36 @@ static int hvmemul_rep_ins(
                                !!(ctxt->regs->eflags & X86_EFLAGS_DF), gpa);
 }
 
+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;
+    char *buf;
+    int rc;
+
+    buf = xmalloc_array(char, bytes);
+
+    if ( buf == NULL )
+        return X86EMUL_UNHANDLEABLE;
+
+    rc = set_context_data(buf, bytes);
+
+    if ( rc != X86EMUL_OKAY )
Quoted 8 lines

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

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+        goto out;
+
+    rc = hvmemul_do_pio_buffer(dst_port, bytes, IOREQ_WRITE, buf);
+
+out:

Labels should be indented by at least one space. But realistically the code wouldn't look worse without any goto...

Understood, will remove the label completely.

Quoted 13 lines
Quoted 13 lines
xen/arch/x86/hvm/emulate.c +9 -0
@@ -1133,7 +1205,20 @@ static int hvmemul_rep_movs(
      */
     rc = hvm_copy_from_guest_phys(buf, sgpa, bytes);
     if ( rc == HVMCOPY_okay )
+    {
+        if ( unlikely(hvmemul_ctxt->set_context) )
+        {
+            rc = set_context_data(buf, bytes);
+
+            if ( rc != X86EMUL_OKAY)
+            {
+                xfree(buf);
+                return rc;
Quoted 8 lines

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

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +4 -0
+            }
+        }
+
         rc = hvm_copy_to_guest_phys(dgpa, buf, bytes);
+    }

Why do you not bypass hvm_copy_from_guest_phys() here? This way it would - afaict - become consistent with the other cases.

You're right, it's unnecessary. Will remove the hvm_copy_from_guest_phys().

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/vm_event.c +0 -0
@@ -23,6 +23,36 @@
 #include <xen/sched.h>
 #include <asm/hvm/hvm.h>
 
Quoted 10 lines

xen/arch/x86/vm_event.c line 30

Quoted 5 lines
xen/arch/x86/vm_event.c +5 -0
+int vm_event_init_domain(struct domain *d)
+{
+    struct vcpu *v;
+
+    for_each_vcpu( d, v )

Either you consider for_each_vcpu a keyword-like thing, then this is missing a blank before the opening parenthesis, or you don't, in which case the blanks immediately inside the parentheses should be dropped.

Understood, will fix it.

Quoted 21 lines
Quoted 21 lines
xen/arch/x86/vm_event.c +21 -0
+    {
+        if ( v->arch.vm_event.emul_read_data )
+            continue;
+
+        v->arch.vm_event.emul_read_data =
+            xzalloc(struct vm_event_emul_read_data);
+
+        if ( !v->arch.vm_event.emul_read_data )
+            return -ENOMEM;
+    }
+
+    return 0;
+}
+
+void vm_event_cleanup_domain(struct domain *d)
+{
+    struct vcpu *v;
+
+    for_each_vcpu( d, v )
+    {
+        xfree(v->arch.vm_event.emul_read_data);
Quoted 8 lines

xen/arch/x86/vm_event.c line 54

Quoted 3 lines
xen/arch/x86/vm_event.c +3 -0
+        v->arch.vm_event.emul_read_data = NULL;
+    }
+}

There not being a race here is attributed to vm_event_enable() being implicitly serialized by the domctl lock, and vm_event_disable(), beyond its domctl use, only being on domain cleanup paths? Would be nice to have a comment to that effect.

Indeed, that's how I understood it to happen. I'll add a comment.

Thanks,
Razvan

#6 lore raw

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

Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 15:26, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich

xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v) · line 514

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:
xen/arch/x86/domain.c +1 -0
@@ -511,6 +511,8 @@ int vcpu_initialise(struct vcpu *v)
 
 void vcpu_destroy(struct vcpu *v)
 {
+    xfree(v->arch.vm_event.emul_read_data);

Is this still needed now that you have
vm_event_cleanup_domain()?

Quoted 6 lines

I had thought that there might be a code path where vm_event_cleanup_domain() doesn't get called and yet the domain is being destroyed, but I can't find anything obvious in the code except a comment in arch/x86/mm/shadow/common.c - shadow_final_teardown() - stating that "It is possible for a domain that never got domain_kill()ed to get here with its shadow allocation intact.".

Tim?

Quoted 4 lines

Since common/domain.c's domain_kill() seems to be the only caller of vm_event_cleanup(), I took that comment to mean that it could be possible to end up in vcpu_destroy() without vm_event_cleanup_domain() having been called, so I took the better-safe-than-sorry approach.

Better-safe-than-sorry would imply you'd also have to clear the pointer in vcpu_destroy(), covering the (hypothetical?) case of vm_event_cleanup_domain() being called afterwards.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
xen/arch/x86/domain.c +7 -0
+    {
+        unsigned int safe_bytes =
+            min(bytes, curr->arch.vm_event.emul_read_data->size);
+
+        if ( safe_bytes )
+            memcpy(buffer, curr->arch.vm_event.emul_read_data->data,
+                   safe_bytes);
Quoted 5 lines
Quoted 1 line

So why did you still keep this conditional?

I thought a memcpy() call that ends up doing nothing (copying 0 bytes) would be expensive and I've tried to optimize the code by not doing the call if safe_bytes == 0.

That argumentation would then also apply to the subsequent memset().

Quoted 1 line

Since nobody else seems to think it's worth it, I'll remove it.

Thanks.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/domain.c +9 -0
@@ -1133,7 +1205,20 @@ static int hvmemul_rep_movs(
      */
     rc = hvm_copy_from_guest_phys(buf, sgpa, bytes);
     if ( rc == HVMCOPY_okay )
+    {
+        if ( unlikely(hvmemul_ctxt->set_context) )
+        {
+            rc = set_context_data(buf, bytes);
+
+            if ( rc != X86EMUL_OKAY)
+            {
+                xfree(buf);
+                return rc;
Quoted 8 lines

xen/arch/x86/domain.c static int hvmemul_rep_movs( · line 1221

Quoted 5 lines
xen/arch/x86/domain.c +4 -0
+            }
+        }
+
         rc = hvm_copy_to_guest_phys(dgpa, buf, bytes);
+    }

Why do you not bypass hvm_copy_from_guest_phys() here? This way it would - afaict - become consistent with the other cases.

Quoted 1 line

You're right, it's unnecessary. Will remove the hvm_copy_from_guest_phys().

s/remove/bypass/ hopefully.

Jan

#7 lore raw

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

Quoted 23 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 15:26, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich

xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v) · line 514

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:
xen/arch/x86/domain.c +1 -0
@@ -511,6 +511,8 @@ int vcpu_initialise(struct vcpu *v)
 
 void vcpu_destroy(struct vcpu *v)
 {
+    xfree(v->arch.vm_event.emul_read_data);

Is this still needed now that you have
vm_event_cleanup_domain()?

Quoted 6 lines

I had thought that there might be a code path where vm_event_cleanup_domain() doesn't get called and yet the domain is being destroyed, but I can't find anything obvious in the code except a comment in arch/x86/mm/shadow/common.c - shadow_final_teardown() - stating that "It is possible for a domain that never got domain_kill()ed to get here with its shadow allocation intact.".

Tim?

Quoted 8 lines
Quoted 4 lines

Since common/domain.c's domain_kill() seems to be the only caller of vm_event_cleanup(), I took that comment to mean that it could be possible to end up in vcpu_destroy() without vm_event_cleanup_domain() having been called, so I took the better-safe-than-sorry approach.

Better-safe-than-sorry would imply you'd also have to clear the pointer in vcpu_destroy(), covering the (hypothetical?) case of vm_event_cleanup_domain() being called afterwards.

True. If that can happen I'll clear the pointer there too.

Quoted 43 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
xen/arch/x86/domain.c +7 -0
+    {
+        unsigned int safe_bytes =
+            min(bytes, curr->arch.vm_event.emul_read_data->size);
+
+        if ( safe_bytes )
+            memcpy(buffer, curr->arch.vm_event.emul_read_data->data,
+                   safe_bytes);
Quoted 5 lines
Quoted 1 line

So why did you still keep this conditional?

I thought a memcpy() call that ends up doing nothing (copying 0 bytes) would be expensive and I've tried to optimize the code by not doing the call if safe_bytes == 0.

That argumentation would then also apply to the subsequent memset().

Quoted 1 line

Since nobody else seems to think it's worth it, I'll remove it.

Thanks.

Quoted 22 lines
Quoted 21 lines
Quoted 8 lines
xen/arch/x86/domain.c +4 -0
@@ -1133,7 +1205,20 @@ static int hvmemul_rep_movs(
      */
     rc = hvm_copy_from_guest_phys(buf, sgpa, bytes);
     if ( rc == HVMCOPY_okay )
+    {
+        if ( unlikely(hvmemul_ctxt->set_context) )
+        {
+            rc = set_context_data(buf, bytes);

xen/arch/x86/domain.c static int hvmemul_rep_movs( · line 1221

Quoted 10 lines
xen/arch/x86/domain.c +9 -0
+
+            if ( rc != X86EMUL_OKAY)
+            {
+                xfree(buf);
+                return rc;
+            }
+        }
+
         rc = hvm_copy_to_guest_phys(dgpa, buf, bytes);
+    }

Why do you not bypass hvm_copy_from_guest_phys() here? This way it would - afaict - become consistent with the other cases.

Quoted 3 lines
Quoted 1 line

You're right, it's unnecessary. Will remove the hvm_copy_from_guest_phys().

s/remove/bypass/ hopefully.

Yes, sorry for the typo.

Thanks,
Razvan

#8 lore raw

PATCH v5 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 V4:
- Added XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST to the capabilities in monitor.c (Tamas' patch has hit staging since V4).

---
 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          |   19 ++++++++++++++++++-
 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, 78 insertions(+), 9 deletions(-)
tools/libxc/include/xenctrl.h +2 -0
index 0bbae2a..ce9029c 100644
@@ -2378,6 +2378,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 b64bce3..d5f87da 100644
@@ -129,3 +129,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 545aa91..18d9621 100644
@@ -6001,7 +6001,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;
@@ -6415,6 +6414,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 +18 -1
index 0da855e..d35907b 100644
@@ -55,7 +55,8 @@ static inline uint32_t get_capabilities(struct domain *d)
 
     capabilities = (1 << XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG) |
                    (1 << XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR) |
-                   (1 << XEN_DOMCTL_MONITOR_EVENT_SOFTWARE_BREAKPOINT);
+                   (1 << XEN_DOMCTL_MONITOR_EVENT_SOFTWARE_BREAKPOINT) |
+                   (1 << XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST);
 
     /* Since we know this is on VMX, we can just call the hvm func */
     if ( hvm_is_singlestep_supported() )
@@ -184,6 +185,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 c5ad1cb..9fbbdd9 100644
@@ -347,13 +347,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 8b1d6b4..631935a 100644
@@ -1009,6 +1009,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_* */
@@ -1039,6 +1040,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 4d89c38..f889139 100644
@@ -95,6 +95,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
#9 lore raw

PATCH v5 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 V4:
- Rebased the patch to take into account Tamas' "x86/vm_event:
toggle singlestep from vm_event response".
- Moved the management of heap-allocated vm_event-related domain data to the specific x86 vm_event domain init / cleanup helpers.

---
 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           |   43 ++++++++++++++
 xen/common/vm_event.c             |    4 ++
 xen/include/asm-arm/vm_event.h    |    7 +++
 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    |    3 +
 xen/include/public/vm_event.h     |    5 ++
 16 files changed, 234 insertions(+), 46 deletions(-)
xen/arch/x86/domain.c +2 -0
index 6596408..b940b04 100644
@@ -667,6 +667,8 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags,
 
 void arch_domain_destroy(struct domain *d)
 {
+    xfree(d->arch.event_write_data);
+
     if ( has_hvm_container_domain(d) )
         hvm_domain_destroy(d);
 
xen/arch/x86/hvm/emulate.c +4 -4
index 30def25..9aae0df 100644
@@ -1427,14 +1427,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;
     }
@@ -1455,7 +1455,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 2653bc1..f22b7d1 100644
@@ -274,7 +274,7 @@ static 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 @@ static 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 @@ static 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 70de49e..b8bba71 100644
@@ -1949,7 +1949,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 bc3212f..d3183a8 100644
@@ -2016,9 +2016,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;
     }
@@ -2030,7 +2037,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();
@@ -3053,7 +3060,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 72dd9c8..555fdfa 100644
@@ -1034,15 +1034,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);
 
@@ -1235,15 +1236,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 +43 -0

66 lines not on this page · raw message

xen/common/vm_event.c +4 -0
index 0007d70..4c6bf98 100644
@@ -393,6 +393,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 +7 -0
index 20469a8..0833a65 100644
@@ -21,6 +21,7 @@
 #define __ASM_ARM_VM_EVENT_H__
 
 #include <xen/sched.h>
+#include <xen/vm_event.h>
 
 static inline
 int vm_event_init_domain(struct domain *d)
@@ -41,4 +42,10 @@ void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v)
     /* Not supported on ARM. */
 }
 
+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 9fbbdd9..7a9e96f 100644
@@ -247,6 +247,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;
@@ -360,6 +375,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))
@@ -516,7 +533,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 +3 -0
index 3881783..2bcfe26 100644
@@ -21,6 +21,7 @@
 #define __ASM_X86_VM_EVENT_H__
 
 #include <xen/sched.h>
+#include <xen/vm_event.h>
 
 int vm_event_init_domain(struct domain *d);
 
@@ -28,4 +29,6 @@ void vm_event_cleanup_domain(struct domain *d);
 
 void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v);
 
+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 +5 -0
index f889139..fbc76b2 100644
@@ -74,6 +74,11 @@
  * VM_EVENT_FLAG_SET_EMUL_READ_DATA are set, only the latter will be honored).
  */
 #define VM_EVENT_FLAG_SET_EMUL_READ_DATA (1 << 5)
+ /*
+  * 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 << 6)
 
 /*
  * Reasons for the vm event request
Signature · 1 line
1.7.9.5
#10 lore raw

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

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

Changes since V4:
- Rebased the patch to take into account Tamas' "x86/vm_event:
toggle singlestep from vm_event response".
- Moved the management of heap-allocated vm_event-related domain data to the specific x86 vm_event domain init / cleanup helpers.

I would have understood this to mean vm_event_cleanup_domain() instead of ...

xen/arch/x86/domain.c int arch_domain_create(struct domain *d, unsigned int domcr_flags, · line 670

Quoted 7 lines
xen/arch/x86/domain.c +1 -0
@@ -667,6 +667,8 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags,
 
 void arch_domain_destroy(struct domain *d)
 {
+    xfree(d->arch.event_write_data);

... here. And I see it is being done there, so why also here?

Quoted 5 lines
xen/arch/x86/vm_event.c +0 -0
@@ -22,11 +22,19 @@
 
 #include <xen/sched.h>

xen/arch/x86/vm_event.c line 33

Quoted 10 lines
xen/arch/x86/vm_event.c +4 -0
 #include <asm/hvm/hvm.h>
+#include <asm/vm_event.h>
 
 int vm_event_init_domain(struct domain *d)
 {
     struct vcpu *v;
 
+    if ( !d->arch.event_write_data )
+        d->arch.event_write_data = xzalloc_array(struct monitor_write_data,
+                                                 d->max_vcpus);

Looking at this again I wonder why the data isn't being made part of struct arch_vcpu's vm_event sub-structure. That would also address the complaint I have here about this not being a guaranteed maximum page size runtime allocation.

xen/arch/x86/vm_event.c void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v) · line 80

Quoted 10 lines
xen/arch/x86/vm_event.c +6 -0
@@ -61,6 +72,38 @@ void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v)
     hvm_toggle_singlestep(v);
 }
 
+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];

That would also eliminate this strange construct.

xen/include/public/vm_event.h line 81

Quoted 11 lines
xen/include/public/vm_event.h +5 -0
@@ -74,6 +74,11 @@
  * VM_EVENT_FLAG_SET_EMUL_READ_DATA are set, only the latter will be honored).
  */
 #define VM_EVENT_FLAG_SET_EMUL_READ_DATA (1 << 5)
+ /*
+  * 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 << 6)

Wouldn't this want adding to the get-capabilities sub-op too?

Jan

#11 lore raw

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

Quoted 15 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:

Changes since V4:
- Rebased the patch to take into account Tamas' "x86/vm_event:
toggle singlestep from vm_event response".
- Moved the management of heap-allocated vm_event-related domain data to the specific x86 vm_event domain init / cleanup helpers.

I would have understood this to mean vm_event_cleanup_domain() instead of ...

Quoted 4 lines
xen/arch/x86/domain.c +0 -0
@@ -667,6 +667,8 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags,
 
Quoted 5 lines

xen/arch/x86/domain.c int arch_domain_create(struct domain *d, unsigned int domcr_flags, · line 670

Quoted 3 lines
xen/arch/x86/domain.c +1 -0
 void arch_domain_destroy(struct domain *d)
 {
+    xfree(d->arch.event_write_data);

... here. And I see it is being done there, so why also here?

I was not convinced that it's safe to assume that vm_event_cleanup_domain() always gets called on domain destruction (please see also the reply to the 1/3 patch review). That's quite likely a wrong assumption, but if it's not it's safer.

Quoted 12 lines
Quoted 12 lines
xen/arch/x86/vm_event.c +1 -0
@@ -22,11 +22,19 @@
 
 #include <xen/sched.h>
 #include <asm/hvm/hvm.h>
+#include <asm/vm_event.h>
 
 int vm_event_init_domain(struct domain *d)
 {
     struct vcpu *v;
 
Quoted 8 lines

xen/arch/x86/vm_event.c line 33

Quoted 3 lines
xen/arch/x86/vm_event.c +3 -0
+    if ( !d->arch.event_write_data )
+        d->arch.event_write_data = xzalloc_array(struct monitor_write_data,
+                                                 d->max_vcpus);

Looking at this again I wonder why the data isn't being made part of struct arch_vcpu's vm_event sub-structure. That would also address the complaint I have here about this not being a guaranteed maximum page size runtime allocation.

I think this is just how the initial suggestion was worded, I'll change it.

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/vm_event.c +0 -0
@@ -61,6 +72,38 @@ void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v)
     hvm_toggle_singlestep(v);
 }
 
Quoted 8 lines

xen/arch/x86/vm_event.c void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v) · line 80

Quoted 6 lines
xen/arch/x86/vm_event.c +6 -0
+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];

That would also eliminate this strange construct.

Indeed, I'm not a fan of it either.

Quoted 5 lines
Quoted 5 lines
xen/include/public/vm_event.h +0 -0
@@ -74,6 +74,11 @@
  * VM_EVENT_FLAG_SET_EMUL_READ_DATA are set, only the latter will be honored).
  */
Quoted 8 lines

xen/include/public/vm_event.h line 81

Quoted 6 lines
xen/include/public/vm_event.h +5 -0
 #define VM_EVENT_FLAG_SET_EMUL_READ_DATA (1 << 5)
+ /*
+  * 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 << 6)

Wouldn't this want adding to the get-capabilities sub-op too?

Yes, it's best to add it. Ack.

Thanks,
Razvan

#12 lore raw

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

Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 15:45, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:
xen/arch/x86/vm_event.c +1 -0
@@ -22,11 +22,19 @@
 
 #include <xen/sched.h>
 #include <asm/hvm/hvm.h>
+#include <asm/vm_event.h>
 
 int vm_event_init_domain(struct domain *d)
 {
     struct vcpu *v;
 
Quoted 8 lines

xen/arch/x86/vm_event.c line 33

Quoted 3 lines
xen/arch/x86/vm_event.c +3 -0
+    if ( !d->arch.event_write_data )
+        d->arch.event_write_data = xzalloc_array(struct monitor_write_data,
+                                                 d->max_vcpus);

Looking at this again I wonder why the data isn't being made part of struct arch_vcpu's vm_event sub-structure. That would also address the complaint I have here about this not being a guaranteed maximum page size runtime allocation.

Quoted 1 line

I think this is just how the initial suggestion was worded, I'll change it.

Right - after having sent the reply I started wondering whether maybe I had asked for this. But if I did, then surely not with xzalloc_array(), but vzalloc().

If you moved this into struct arch_vcpu (again), then its size would likely call for the whole vm_event structure to become indirectly accessed and allocated.

Jan

#13 lore raw

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

Quoted 26 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 15:45, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:
xen/arch/x86/vm_event.c +1 -0
@@ -22,11 +22,19 @@
 
 #include <xen/sched.h>
 #include <asm/hvm/hvm.h>
+#include <asm/vm_event.h>
 
 int vm_event_init_domain(struct domain *d)
 {
     struct vcpu *v;
 
Quoted 8 lines

xen/arch/x86/vm_event.c line 33

Quoted 3 lines
xen/arch/x86/vm_event.c +3 -0
+    if ( !d->arch.event_write_data )
+        d->arch.event_write_data = xzalloc_array(struct monitor_write_data,
+                                                 d->max_vcpus);

Looking at this again I wonder why the data isn't being made part of struct arch_vcpu's vm_event sub-structure. That would also address the complaint I have here about this not being a guaranteed maximum page size runtime allocation.

Quoted 1 line

I think this is just how the initial suggestion was worded, I'll change it.

Quoted 7 lines

Right - after having sent the reply I started wondering whether maybe I had asked for this. But if I did, then surely not with xzalloc_array(), but vzalloc().

If you moved this into struct arch_vcpu (again), then its size would likely call for the whole vm_event structure to become indirectly accessed and allocated.

In that case would it suffice to just switch to vzalloc() in this case?

I'm not opposed to just placing all the data (this and the memory-content hiding data) in struct vm_event and allocate that as a whole, but that would change patch 1/3, 3/3 and also touch other code.

Thanks,
Razvan

#14 lore raw

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

Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 17:04, <[email protected]> wrote:
Quoted 30 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 15:45, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:
xen/arch/x86/vm_event.c +1 -0
@@ -22,11 +22,19 @@
 
 #include <xen/sched.h>
 #include <asm/hvm/hvm.h>
+#include <asm/vm_event.h>
 
 int vm_event_init_domain(struct domain *d)
 {
     struct vcpu *v;
 
Quoted 8 lines

xen/arch/x86/vm_event.c line 33

Quoted 3 lines
xen/arch/x86/vm_event.c +3 -0
+    if ( !d->arch.event_write_data )
+        d->arch.event_write_data = xzalloc_array(struct monitor_write_data,
+                                                 d->max_vcpus);

Looking at this again I wonder why the data isn't being made part of struct arch_vcpu's vm_event sub-structure. That would also address the complaint I have here about this not being a guaranteed maximum page size runtime allocation.

Quoted 1 line

I think this is just how the initial suggestion was worded, I'll change it.

Right - after having sent the reply I started wondering whether maybe I had asked for this. But if I did, then surely not with xzalloc_array(), but vzalloc().

Quoted 5 lines
Quoted 3 lines

If you moved this into struct arch_vcpu (again), then its size would likely call for the whole vm_event structure to become indirectly accessed and allocated.

In that case would it suffice to just switch to vzalloc() in this case?

For now, yes.

Quoted 3 lines

I'm not opposed to just placing all the data (this and the memory-content hiding data) in struct vm_event and allocate that as a whole, but that would change patch 1/3, 3/3 and also touch other code.

Would be a nice thing to clean up post-4.6.

Jan

#15 lore raw

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

Quoted 38 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 17:04, <[email protected]> wrote:
Quoted 30 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 15:45, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:
xen/arch/x86/vm_event.c +1 -0
@@ -22,11 +22,19 @@
 
 #include <xen/sched.h>
 #include <asm/hvm/hvm.h>
+#include <asm/vm_event.h>
 
 int vm_event_init_domain(struct domain *d)
 {
     struct vcpu *v;
 
Quoted 8 lines

xen/arch/x86/vm_event.c line 33

Quoted 3 lines
xen/arch/x86/vm_event.c +3 -0
+    if ( !d->arch.event_write_data )
+        d->arch.event_write_data = xzalloc_array(struct monitor_write_data,
+                                                 d->max_vcpus);

Looking at this again I wonder why the data isn't being made part of struct arch_vcpu's vm_event sub-structure. That would also address the complaint I have here about this not being a guaranteed maximum page size runtime allocation.

Quoted 1 line

I think this is just how the initial suggestion was worded, I'll change it.

Right - after having sent the reply I started wondering whether maybe I had asked for this. But if I did, then surely not with xzalloc_array(), but vzalloc().

Quoted 5 lines
Quoted 3 lines

If you moved this into struct arch_vcpu (again), then its size would likely call for the whole vm_event structure to become indirectly accessed and allocated.

In that case would it suffice to just switch to vzalloc() in this case?

Quoted 7 lines

For now, yes.

Quoted 3 lines

I'm not opposed to just placing all the data (this and the memory-content hiding data) in struct vm_event and allocate that as a whole, but that would change patch 1/3, 3/3 and also touch other code.

Would be a nice thing to clean up post-4.6.

I agree, and I will add it to my queue.

Thanks,
Razvan

#16 lore raw

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

In reply to Jan Beulich
Quoted 5 lines · Jan Beulich
Quoted 5 lines
xen/include/public/vm_event.h +0 -0
@@ -74,6 +74,11 @@
  * VM_EVENT_FLAG_SET_EMUL_READ_DATA are set, only the latter will be honored).
  */
Quoted 8 lines

xen/include/public/vm_event.h line 81

Quoted 6 lines
xen/include/public/vm_event.h +5 -0
 #define VM_EVENT_FLAG_SET_EMUL_READ_DATA (1 << 5)
+ /*
+  * 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 << 6)

Wouldn't this want adding to the get-capabilities sub-op too?

Actually, no (sorry for rushing with the answer before), the monitor.c capabilities issue refers to main event types, whereas this is a response flag.

45 static inline uint32_t get_capabilities(struct domain *d) 46 {
47 uint32_t capabilities = 0;
48
49 /*
50 * At the moment only Intel HVM domains are supported. However,
event
51 * delivery could be extended to AMD and PV domains.
52 */
53 if ( !is_hvm_domain(d) || !cpu_has_vmx )
54 return capabilities;
55
56 capabilities = (1 << XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG) |

 57                    (1 << XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR) |
 58                    (1 << XEN_DOMCTL_MONITOR_EVENT_SOFTWARE_BREAKPOINT) |
 59                    (1 << XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST);

60
61 /* Since we know this is on VMX, we can just call the hvm func */
62 if ( hvm_is_singlestep_supported() )
63 capabilities |= (1 << XEN_DOMCTL_MONITOR_EVENT_SINGLESTEP);
64
65 return capabilities;
66 }

So if we have the (1 << XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG) and (1 << XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR), that also means we can deny those writes (by setting the VM_EVENT_FLAG_DENY in the vm_event response), no additional checks are necessary.

Thanks,
Razvan

#17 lore raw
In reply to Razvan Cojocaru
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:

I've also moved x86 logic in patch 3/3 to x86 source files, this seems to have gone unnoticed but would likely have not compiled on ARM.

Which leaves open whether this time you actually checked that ARM continues to build.

Jan

#18 lore raw
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:

I've also moved x86 logic in patch 3/3 to x86 source files, this seems to have gone unnoticed but would likely have not compiled on ARM.

Which leaves open whether this time you actually checked that ARM continues to build.

I did check, and again just now. My patches don't break the ARM build, but I just found that on my ARM system, current Xen staging doesn't
build: http://pastebin.com/RnywiCX7

# cat /etc/issue
Ubuntu 14.04 LTS \n \l
# uname -a
Linux localhost.localdomain 3.15.0-rc8+ #3 SMP PREEMPT Fri Jan 30
14:08:23 EET 2015 aarch64 aarch64 aarch64 GNU/Linux
# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/aarch64-linux-gnu/4.9/lto-wrapper
Target: aarch64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.9.2-10ubuntu13'
--with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs
--enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.9 --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib
--enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--enable-gnu-unique-object --disable-libsanitizer --disable-libquadmath
--enable-plugin --with-system-zlib --disable-browser-plugin
--enable-java-awt=gtk --enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-arm64/jre
--enable-java-home
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-arm64
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-arm64
--with-arch-directory=arm64
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-multiarch
--disable-werror --enable-checking=release --build=aarch64-linux-gnu
--host=aarch64-linux-gnu --target=aarch64-linux-gnu
Thread model: posix
gcc version 4.9.2 (Ubuntu/Linaro 4.9.2-10ubuntu13)

I've temporarily fixed that by wrapping the offending #defines in "#ifndef constant" statements, so that I could continue to check that the build works, but that's likely not the proper fix.

Hope this helps,
Razvan

#19 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 13:45, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:

I've also moved x86 logic in patch 3/3 to x86 source files, this seems to have gone unnoticed but would likely have not compiled on ARM.

Quoted 6 lines
Quoted 2 lines

Which leaves open whether this time you actually checked that ARM continues to build.

I did check, and again just now. My patches don't break the ARM build, but I just found that on my ARM system, current Xen staging doesn't
build: http://pastebin.com/RnywiCX7

That's the tool stack having issues, while we're talking about the hypervisor build here.

Jan

#20 lore raw
In reply to Razvan Cojocaru
Quoted 6 lines · Razvan Cojocaru
Quoted 5 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 13.07.15 at 19:14, <[email protected]> wrote:

I've also moved x86 logic in patch 3/3 to x86 source files, this seems to have gone unnoticed but would likely have not compiled on ARM.

Quoted 6 lines
Quoted 2 lines

Which leaves open whether this time you actually checked that ARM continues to build.

I did check, and again just now. My patches don't break the ARM build, but I just found that on my ARM system, current Xen staging doesn't
build: http://pastebin.com/RnywiCX7

This is a glibc error with your distro, like this
https://bugs.launchpad.net/linaro-aarch64/+bug/1169164

Quoted 3 lines

I've temporarily fixed that by wrapping the offending #defines in "#ifndef constant" statements, so that I could continue to check that the build works, but that's likely not the proper fix.

Indeed, it the hack which works if you are unable to upgrade your libc for some reason. FWIW I just applied the patches from that bug to the installed copies of the relevant headers, which is certainly skanky...

Ian.