Skip to contentSkip to conversation

xen: Emulate with no writes

RFC PATCH Superseded Razvan Cojocaru 3 participants 21 messages Aug 27, 2014 – Aug 28, 2014

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v8 1/5 xen: Emulate with no writes

Added support for emulating an instruction with no memory writes. Additionally, introduced hvm_emulate_one_full(), which inspects possible return values from the hvm_emulate_one() functions (EXCEPTION, UNHANDLEABLE) and acts on them.

Changes since V1:
- Removed the Linux code that computes the length of an instruction.
- Unused function parameters are no longer marked.
- Refactored the code to eliminate redundancy.
- Made the exception passed on to the guest by hvm_emulate_one_full() configurable.

Changes since V2:
- Renamed hvmemul_write_dummy() to hvmemul_write_discard().
- Fixed a comment (Xen coding style rules).
- Renamed hvm_emulate_one_with_ops() to _hvm_emulate_one().
- Changed stack variable hvm_emulate_ops_no_write into a static const.
- Modified struct hvm_emulate_ctxt ctx initialization syntax.
- Renamed unhandleable_trapnr to trapnr and unhandleable_errcode to errcode.
- Changed errcode's type to unsigned int.
- Removed hvm_emulate_one() loop that went on until it returned something other than X86EMUL_RETRY (to prevent potential blocking against the time calibration rendezvous).

Changes since V3:
- The rep_ins, rep_movs and cmpxchg handlers are
now also inactive.

Changes since V4:
- Also discarding IO reads (dummy read_io handler).

Changes since V5:
- Added discard versions of write_io and rep_outs.

Changes since V6:
- Added discard versions of write_msr and wbinvd.
- Now calling hvm_emulate_writeback() for the X86EMUL_UNHANDLEABLE case as well.

Changes since V7:
- Renamed hvm_emulate_one_full() to hvm_mem_event_emulate_one().
- Added a comment explaining how X86EMUL_RETRY is being treated.

---
 xen/arch/x86/hvm/emulate.c        |  175 ++++++++++++++++++++++++++++++++++++-
 xen/include/asm-x86/hvm/emulate.h |    5 ++
 2 files changed, 177 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/emulate.c +172 -3
index eac159f..81ba3e4 100644
@@ -688,6 +688,94 @@ static int hvmemul_write(
     return X86EMUL_OKAY;
 }
 
+static int hvmemul_write_discard(
+    enum x86_segment seg,
+    unsigned long offset,
+    void *p_data,
+    unsigned int bytes,
+    struct x86_emulate_ctxt *ctxt)
+{
+    /* Discarding the write. */
+    return X86EMUL_OKAY;
+}
+
+static int hvmemul_rep_ins_discard(
+    uint16_t src_port,
+    enum x86_segment dst_seg,
+    unsigned long dst_offset,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_OKAY;
+}
+
+static int hvmemul_rep_movs_discard(
+   enum x86_segment src_seg,
+   unsigned long src_offset,
+   enum x86_segment dst_seg,
+   unsigned long dst_offset,
+   unsigned int bytes_per_rep,
+   unsigned long *reps,
+   struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_OKAY;
+}
+
+static int hvmemul_rep_outs_discard(
+    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)
+{
+    return X86EMUL_OKAY;
+}
+
+static int hvmemul_cmpxchg_discard(
+    enum x86_segment seg,
+    unsigned long offset,
+    void *p_old,
+    void *p_new,
+    unsigned int bytes,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_OKAY;
+}
+
+static int hvmemul_read_io_discard(
+    unsigned int port,
+    unsigned int bytes,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_OKAY;
+}
+
+static int hvmemul_write_io_discard(
+    unsigned int port,
+    unsigned int bytes,
+    unsigned long val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_OKAY;
+}
+
+static int hvmemul_write_msr_discard(
+    unsigned long reg,
+    uint64_t val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_OKAY;
+}
+
+static int hvmemul_wbinvd_discard(
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_OKAY;
+}
+
 static int hvmemul_cmpxchg(
     enum x86_segment seg,
     unsigned long offset,
@@ -1138,8 +1226,33 @@ static const struct x86_emulate_ops hvm_emulate_ops = {
     .invlpg        = hvmemul_invlpg
 };
 
-int hvm_emulate_one(
-    struct hvm_emulate_ctxt *hvmemul_ctxt)
+static const struct x86_emulate_ops hvm_emulate_ops_no_write = {
+    .read          = hvmemul_read,
+    .insn_fetch    = hvmemul_insn_fetch,
+    .write         = hvmemul_write_discard,
+    .cmpxchg       = hvmemul_cmpxchg_discard,
+    .rep_ins       = hvmemul_rep_ins_discard,
+    .rep_outs      = hvmemul_rep_outs_discard,
+    .rep_movs      = hvmemul_rep_movs_discard,
+    .read_segment  = hvmemul_read_segment,
+    .write_segment = hvmemul_write_segment,
+    .read_io       = hvmemul_read_io_discard,
+    .write_io      = hvmemul_write_io_discard,
+    .read_cr       = hvmemul_read_cr,
+    .write_cr      = hvmemul_write_cr,
+    .read_msr      = hvmemul_read_msr,
+    .write_msr     = hvmemul_write_msr_discard,
+    .wbinvd        = hvmemul_wbinvd_discard,
+    .cpuid         = hvmemul_cpuid,
+    .inject_hw_exception = hvmemul_inject_hw_exception,
+    .inject_sw_interrupt = hvmemul_inject_sw_interrupt,
+    .get_fpu       = hvmemul_get_fpu,
+    .put_fpu       = hvmemul_put_fpu,
+    .invlpg        = hvmemul_invlpg
+};
+
+static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
+    const struct x86_emulate_ops *ops)
 {
     struct cpu_user_regs *regs = hvmemul_ctxt->ctxt.regs;
     struct vcpu *curr = current;
@@ -1191,7 +1304,7 @@ int hvm_emulate_one(
     vio->mmio_retrying = vio->mmio_retry;
     vio->mmio_retry = 0;
 
-    rc = x86_emulate(&hvmemul_ctxt->ctxt, &hvm_emulate_ops);
+    rc = x86_emulate(&hvmemul_ctxt->ctxt, ops);
 
     if ( rc == X86EMUL_OKAY && vio->mmio_retry )
         rc = X86EMUL_RETRY;
@@ -1239,6 +1352,62 @@ int hvm_emulate_one(
     return X86EMUL_OKAY;
 }
 
+int hvm_emulate_one(
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    return _hvm_emulate_one(hvmemul_ctxt, &hvm_emulate_ops);
+}
+
+int hvm_emulate_one_no_write(
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    return _hvm_emulate_one(hvmemul_ctxt, &hvm_emulate_ops_no_write);
+}
+
+void hvm_mem_event_emulate_one(bool_t nowrite, unsigned int trapnr,
+    unsigned int errcode)
+{
+    struct hvm_emulate_ctxt ctx = {{ 0 }};
+    int rc;
+
+    hvm_emulate_prepare(&ctx, guest_cpu_user_regs());
+
+    if ( nowrite )
+        rc = hvm_emulate_one_no_write(&ctx);
+    else
+        rc = hvm_emulate_one(&ctx);
+
+    switch ( rc )
+    {
+    case X86EMUL_RETRY:
+        /*
+         * This function is called when handling an EPT-related mem_event
+         * reply. As such, nothing else needs to be done here, since simply
+         * returning makes the current instruction cause a page fault again,
+         * consistent with X86EMUL_RETRY.
+         */
+        return;
+    case X86EMUL_UNHANDLEABLE:
+        gdprintk(XENLOG_DEBUG, "Emulation failed @ %04x:%lx: "
+               "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
+               hvmemul_get_seg_reg(x86_seg_cs, &ctx)->sel,
+               ctx.insn_buf_eip,
+               ctx.insn_buf[0], ctx.insn_buf[1],
+               ctx.insn_buf[2], ctx.insn_buf[3],
+               ctx.insn_buf[4], ctx.insn_buf[5],
+               ctx.insn_buf[6], ctx.insn_buf[7],
+               ctx.insn_buf[8], ctx.insn_buf[9]);
+        hvm_inject_hw_exception(trapnr, errcode);
+        break;
+    case X86EMUL_EXCEPTION:
+        if ( ctx.exn_pending )
+            hvm_inject_hw_exception(ctx.exn_vector, ctx.exn_error_code);
+        break;
+    }
+
+    hvm_emulate_writeback(&ctx);
+}
+
 void hvm_emulate_prepare(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs)
xen/include/asm-x86/hvm/emulate.h +5 -0
index 00a06cc..efff97e 100644
@@ -37,6 +37,11 @@ struct hvm_emulate_ctxt {
 
 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_event_emulate_one(bool_t nowrite,
+    unsigned int trapnr,
+    unsigned int errcode);
 void hvm_emulate_prepare(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs);
Signature · 1 line
1.7.9.5
#1 lore raw

RFC PATCH v8 2/5 xen: Optimize introspection access to guest state

Speed optimization for introspection purposes: a handful of registers are sent along with each mem_event. This requires enlargement of the mem_event_request / mem_event_response stuctures, and additional code to fill in relevant values. Since the EPT event processing code needs more data than CR3 or MSR event processors, hvm_mem_event_fill_regs() fills in less data than p2m_mem_event_fill_regs(), in order to avoid overhead. Struct hvm_hw_cpu has been considered instead of the custom struct mem_event_regs_st, but its size would cause quick filling up of the mem_event ring buffer.

Changes since V1:
- Replaced guest_x86_mode with cs_arbytes in the mem_event_regs_st structure.
- Removed superfluous preprocessor check for __x86_64__ in p2m_mem_event_fill_regs().

Changes since V2:
- Removed inline function compiler suggestions.
- Removed superfluous memset(&ctxt, 0, sizeof(struct hvm_hw_cpu)).
- Padded struct mem_event_regs_st.
- Renamed mem_event_regs_t to mem_event_regs.
- Readability changes.

Changes since V3:
- Changed the name of VCPU variables referring to the current VCPU to "curr".
- Renamed "mem_event_regs" to "x86_mem_event_regs" to make it explicit.

Changes since V4:
- Renamed "x86_mem_event_regs" to "mem_event_regs_x86" and removed a typedef.

Changes since V5:
- Const-correctness.

---
 xen/arch/x86/hvm/hvm.c         |   33 +++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c          |   57 ++++++++++++++++++++++++++++++++++++++++
 xen/include/public/mem_event.h |   39 +++++++++++++++++++++++++++
 3 files changed, 129 insertions(+)
xen/arch/x86/hvm/hvm.c +33 -0
index 94b18ba..05df1e5 100644
@@ -6148,6 +6148,38 @@ int hvm_debug_op(struct vcpu *v, int32_t op)
     return rc;
 }
 
+static void hvm_mem_event_fill_regs(mem_event_request_t *req)
+{
+    const struct cpu_user_regs *regs = guest_cpu_user_regs();
+    const struct vcpu *curr = current;
+
+    req->x86_regs.rax = regs->eax;
+    req->x86_regs.rcx = regs->ecx;
+    req->x86_regs.rdx = regs->edx;
+    req->x86_regs.rbx = regs->ebx;
+    req->x86_regs.rsp = regs->esp;
+    req->x86_regs.rbp = regs->ebp;
+    req->x86_regs.rsi = regs->esi;
+    req->x86_regs.rdi = regs->edi;
+
+    req->x86_regs.r8  = regs->r8;
+    req->x86_regs.r9  = regs->r9;
+    req->x86_regs.r10 = regs->r10;
+    req->x86_regs.r11 = regs->r11;
+    req->x86_regs.r12 = regs->r12;
+    req->x86_regs.r13 = regs->r13;
+    req->x86_regs.r14 = regs->r14;
+    req->x86_regs.r15 = regs->r15;
+
+    req->x86_regs.rflags = regs->eflags;
+    req->x86_regs.rip    = regs->eip;
+
+    req->x86_regs.msr_efer = curr->arch.hvm_vcpu.guest_efer;
+    req->x86_regs.cr0 = curr->arch.hvm_vcpu.guest_cr[0];
+    req->x86_regs.cr3 = curr->arch.hvm_vcpu.guest_cr[3];
+    req->x86_regs.cr4 = curr->arch.hvm_vcpu.guest_cr[4];
+}
+
 static int hvm_memory_event_traps(long p, uint32_t reason,
                                   unsigned long value, unsigned long old, 
                                   bool_t gla_valid, unsigned long gla) 
@@ -6192,6 +6224,7 @@ static int hvm_memory_event_traps(long p, uint32_t reason,
         req.gla = old;
     }
     
+    hvm_mem_event_fill_regs(&req);
     mem_event_put_request(d, &d->mem_event->access, &req);
     
     return 1;
xen/arch/x86/mm/p2m.c +57 -0

69 lines not on this page · raw message

xen/include/public/mem_event.h +39 -0
index 3831b41..bbd1636 100644
@@ -48,6 +48,44 @@
 #define MEM_EVENT_REASON_MSR         7    /* MSR was hit: gfn is MSR value, gla is MSR address;
                                              does NOT honour HVMPME_onchangeonly */
 
+/* Using a custom struct (not hvm_hw_cpu) so as to not fill
+ * the mem_event ring buffer too quickly. */
+struct mem_event_regs_x86 {
+    uint64_t rax;
+    uint64_t rcx;
+    uint64_t rdx;
+    uint64_t rbx;
+    uint64_t rsp;
+    uint64_t rbp;
+    uint64_t rsi;
+    uint64_t rdi;
+    uint64_t r8;
+    uint64_t r9;
+    uint64_t r10;
+    uint64_t r11;
+    uint64_t r12;
+    uint64_t r13;
+    uint64_t r14;
+    uint64_t r15;
+    uint64_t rflags;
+    uint64_t dr7;
+    uint64_t rip;
+    uint64_t cr0;
+    uint64_t cr2;
+    uint64_t cr3;
+    uint64_t cr4;
+    uint64_t sysenter_cs;
+    uint64_t sysenter_esp;
+    uint64_t sysenter_eip;
+    uint64_t msr_efer;
+    uint64_t msr_star;
+    uint64_t msr_lstar;
+    uint64_t fs_base;
+    uint64_t gs_base;
+    uint32_t cs_arbytes;
+    uint32_t _pad;
+};
+
 typedef struct mem_event_st {
     uint32_t flags;
     uint32_t vcpu_id;
@@ -65,6 +103,7 @@ typedef struct mem_event_st {
     uint16_t available:12;
 
     uint16_t reason;
+    struct mem_event_regs_x86 x86_regs;
 } mem_event_request_t, mem_event_response_t;
 
 DEFINE_RING_TYPES(mem_event, mem_event_request_t, mem_event_response_t);
Signature · 1 line
1.7.9.5
#2 lore raw

RFC PATCH v8 3/5 xen, libxc: Force-enable relevant MSR events

In reply to Razvan Cojocaru

Vmx_disable_intercept_for_msr() will now refuse to disable interception of MSRs needed for memory introspection. It is not possible to gate this on mem_access being active for the domain, since by the time mem_access does become active the interception for the interesting MSRs has already been disabled (vmx_disable_intercept_for_msr() runs very early on).

Changes since V1:
- Replaced printk() with gdprintk(XENLOG_DEBUG, ...).

Changes since V2:
- Split a log line differently to keep it grepable.
- Interception for relevant MSRs will be disabled only if mem_access is not enabled.
- Since they end up being disabled early on (when mem_access is not enabled yet), re-enable interception when mem_access becomes active.

Changes since V3:
- Removed log line stating that MSR interception cannot be disabled.
- Removed superfluous #include <asm/hvm/vmx/vmcs.h>.
- Moved VMX-specific code to vmx.c (as a new hvm_funcs member).

Changes since V5:
- Added xc_mem_access_enable_introspection() to libxc, which has the same parameters and semantics as xc_mem_access_enable(), but it additionally sets d->arch.hvm_domain.introspection_enabled and enables relevant MSR interception.
- Renamed vmx_enable_intro_msr_interception() to vmx_enable_msr_exit_interception().
- Simplified vmx_enable_msr_exit_interception() (const array of MSRs).

Changes since V6:
- Moved the array of interesting MSRs to common header.
- Minor code cleanup.

Changes since V7:
- Reversed if conditions (cheapest one first).
- Combined two if statements.
- Moved "bool_t introspection_enabled;" to the bool_t group above.
- Renamed msrs_exit_array to vmx_msrs_exit_array and made it "extern" in the header.

---
 tools/libxc/xc_mem_access.c        |   10 +++++++++-
 tools/libxc/xc_mem_event.c         |    7 +++++--
 tools/libxc/xc_private.h           |    2 +-
 tools/libxc/xenctrl.h              |    2 ++
 xen/arch/x86/hvm/vmx/vmcs.c        |   25 +++++++++++++++++++++++++
 xen/arch/x86/hvm/vmx/vmx.c         |   12 ++++++++++++
 xen/arch/x86/mm/mem_event.c        |   11 +++++++++++
 xen/include/asm-x86/hvm/domain.h   |    1 +
 xen/include/asm-x86/hvm/hvm.h      |    2 ++
 xen/include/asm-x86/hvm/vmx/vmcs.h |    3 +++
 xen/include/public/domctl.h        |    7 ++++---
 11 files changed, 75 insertions(+), 7 deletions(-)
tools/libxc/xc_mem_access.c +9 -1
index 461f0e9..55d0e9f 100644
@@ -26,7 +26,15 @@
 
 void *xc_mem_access_enable(xc_interface *xch, domid_t domain_id, uint32_t *port)
 {
-    return xc_mem_event_enable(xch, domain_id, HVM_PARAM_ACCESS_RING_PFN, port);
+    return xc_mem_event_enable(xch, domain_id, HVM_PARAM_ACCESS_RING_PFN,
+                               port, 0);
+}
+
+void *xc_mem_access_enable_introspection(xc_interface *xch, domid_t domain_id,
+                                         uint32_t *port)
+{
+    return xc_mem_event_enable(xch, domain_id, HVM_PARAM_ACCESS_RING_PFN,
+                               port, 1);
 }
 
 int xc_mem_access_disable(xc_interface *xch, domid_t domain_id)
tools/libxc/xc_mem_event.c +5 -2
index faf1cc6..8c0be4e 100644
@@ -57,7 +57,7 @@ int xc_mem_event_memop(xc_interface *xch, domid_t domain_id,
 }
 
 void *xc_mem_event_enable(xc_interface *xch, domid_t domain_id, int param,
-                          uint32_t *port)
+                          uint32_t *port, int enable_introspection)
 {
     void *ring_page = NULL;
     uint64_t pfn;
@@ -120,7 +120,10 @@ void *xc_mem_event_enable(xc_interface *xch, domid_t domain_id, int param,
         break;
 
     case HVM_PARAM_ACCESS_RING_PFN:
-        op = XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE;
+        if ( enable_introspection )
+            op = XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE_INTROSPECTION;
+        else
+            op = XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE;
         mode = XEN_DOMCTL_MEM_EVENT_OP_ACCESS;
         break;
 
tools/libxc/xc_private.h +1 -1
index c50a7c9..94df688 100644
@@ -381,6 +381,6 @@ int xc_mem_event_memop(xc_interface *xch, domid_t domain_id,
  * param can be HVM_PARAM_PAGING/ACCESS/SHARING_RING_PFN
  */
 void *xc_mem_event_enable(xc_interface *xch, domid_t domain_id, int param,
-                          uint32_t *port);
+                          uint32_t *port, int enable_introspection);
 
 #endif /* __XC_PRIVATE_H__ */
tools/libxc/xenctrl.h +2 -0
index 1c5d0db..28b5562 100644
@@ -2263,6 +2263,8 @@ int xc_mem_paging_load(xc_interface *xch, domid_t domain_id,
  * Caller has to unmap this page when done.
  */
 void *xc_mem_access_enable(xc_interface *xch, domid_t domain_id, uint32_t *port);
+void *xc_mem_access_enable_introspection(xc_interface *xch, domid_t domain_id,
+                                         uint32_t *port);
 int xc_mem_access_disable(xc_interface *xch, domid_t domain_id);
 int xc_mem_access_resume(xc_interface *xch, domid_t domain_id);
 
xen/arch/x86/hvm/vmx/vmcs.c +25 -0
index 4a4f4e1..44aca73 100644
@@ -39,6 +39,7 @@
 #include <xen/keyhandler.h>
 #include <asm/shadow.h>
 #include <asm/tboot.h>
+#include <asm/mem_event.h>
 
 static bool_t __read_mostly opt_vpid_enabled = 1;
 boolean_param("vpid", opt_vpid_enabled);
@@ -71,6 +72,18 @@ u32 vmx_vmexit_control __read_mostly;
 u32 vmx_vmentry_control __read_mostly;
 u64 vmx_ept_vpid_cap __read_mostly;
 
+const u32 vmx_msrs_exit_array[] = {
+    MSR_IA32_SYSENTER_EIP,
+    MSR_IA32_SYSENTER_ESP,
+    MSR_IA32_SYSENTER_CS,
+    MSR_IA32_MC0_CTL,
+    MSR_STAR,
+    MSR_LSTAR
+};
+
+const unsigned int vmx_msrs_exit_array_size =
+    ARRAY_SIZE(vmx_msrs_exit_array);
+
 static DEFINE_PER_CPU_READ_MOSTLY(struct vmcs_struct *, vmxon_region);
 static DEFINE_PER_CPU(struct vmcs_struct *, current_vmcs);
 static DEFINE_PER_CPU(struct list_head, active_vmcs_list);
@@ -695,11 +708,23 @@ static void vmx_set_host_env(struct vcpu *v)
 void vmx_disable_intercept_for_msr(struct vcpu *v, u32 msr, int type)
 {
     unsigned long *msr_bitmap = v->arch.hvm_vmx.msr_bitmap;
+    struct domain *d = v->domain;
 
     /* VMX MSR bitmap supported? */
     if ( msr_bitmap == NULL )
         return;
 
+    if ( unlikely(d->arch.hvm_domain.introspection_enabled) &&
+         mem_event_check_ring(&d->mem_event->access) )
+    {
+        unsigned int i;
+
+        /* Filter out MSR-s needed for memory introspection */
+        for ( i = 0; i < vmx_msrs_exit_array_size; i++ )
+            if ( msr == vmx_msrs_exit_array[i] )
+                return;
+    }
+
     /*
      * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
      * have the write-low and read-high bitmap offsets the wrong way round.
xen/arch/x86/hvm/vmx/vmx.c +12 -0
index dc18a72..40e6c2c 100644
@@ -1682,6 +1682,17 @@ void vmx_hypervisor_cpuid_leaf(uint32_t sub_idx,
         *eax |= XEN_HVM_CPUID_X2APIC_VIRT;
 }
 
+static void vmx_enable_msr_exit_interception(struct domain *d)
+{
+    struct vcpu *v;
+    unsigned int i;
+
+    /* Enable interception for MSRs needed for memory introspection. */
+    for_each_vcpu ( d, v )
+        for ( i = 0; i < vmx_msrs_exit_array_size; i++ )
+            vmx_enable_intercept_for_msr(v, vmx_msrs_exit_array[i], MSR_TYPE_W);
+}
+
 static struct hvm_function_table __initdata vmx_function_table = {
     .name                 = "VMX",
     .cpu_up_prepare       = vmx_cpu_up_prepare,
@@ -1740,6 +1751,7 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .handle_eoi           = vmx_handle_eoi,
     .nhvm_hap_walk_L1_p2m = nvmx_hap_walk_L1_p2m,
     .hypervisor_cpuid_leaf = vmx_hypervisor_cpuid_leaf,
+    .enable_msr_exit_interception = vmx_enable_msr_exit_interception,
 };
 
 const struct hvm_function_table * __init start_vmx(void)
xen/arch/x86/mm/mem_event.c +11 -0
index ba7e71e..fdd5ff6 100644
@@ -587,6 +587,7 @@ int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
         switch( mec->op )
         {
         case XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE:
+        case XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE_INTROSPECTION:
         {
             rc = -ENODEV;
             /* Only HAP is supported */
@@ -600,13 +601,23 @@ int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
             rc = mem_event_enable(d, mec, med, _VPF_mem_access, 
                                     HVM_PARAM_ACCESS_RING_PFN,
                                     mem_access_notification);
+
+            if ( mec->op != XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE &&
+                 rc == 0 && hvm_funcs.enable_msr_exit_interception )
+            {
+                d->arch.hvm_domain.introspection_enabled = 1;
+                hvm_funcs.enable_msr_exit_interception(d);
+            }
         }
         break;
 
         case XEN_DOMCTL_MEM_EVENT_OP_ACCESS_DISABLE:
         {
             if ( med->ring_page )
+            {
                 rc = mem_event_disable(d, med);
+                d->arch.hvm_domain.introspection_enabled = 0;
+            }
         }
         break;
 
xen/include/asm-x86/hvm/domain.h +1 -0
index 291a2e0..30d4aa3 100644
@@ -134,6 +134,7 @@ struct hvm_domain {
     bool_t                 mem_sharing_enabled;
     bool_t                 qemu_mapcache_invalidate;
     bool_t                 is_s3_suspended;
+    bool_t                 introspection_enabled;
 
     /*
      * TSC value that VCPUs use to calculate their tsc_offset value.
xen/include/asm-x86/hvm/hvm.h +2 -0
index 0ebd478..069ba39 100644
@@ -205,6 +205,8 @@ struct hvm_function_table {
     void (*hypervisor_cpuid_leaf)(uint32_t sub_idx,
                                   uint32_t *eax, uint32_t *ebx,
                                   uint32_t *ecx, uint32_t *edx);
+
+    void (*enable_msr_exit_interception)(struct domain *d);
 };
 
 extern struct hvm_function_table hvm_funcs;
xen/include/asm-x86/hvm/vmx/vmcs.h +3 -0
index 215d93c..f2e4d82 100644
@@ -471,6 +471,9 @@ enum vmcs_field {
     HOST_RIP                        = 0x00006c16,
 };
 
+extern const u32 vmx_msrs_exit_array[];
+extern const unsigned int vmx_msrs_exit_array_size;
+
 #define VMCS_VPID_WIDTH 16
 
 #define MSR_TYPE_R 1
xen/include/public/domctl.h +4 -3
index 5b11bbf..a3431ec 100644
@@ -772,10 +772,11 @@ struct xen_domctl_gdbsx_domstatus {
  * ENODEV - host lacks HAP support (EPT/NPT) or HAP is disabled in guest
  * EBUSY  - guest has or had access enabled, ring buffer still active
  */
-#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS            2
+#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS                        2
 
-#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE     0
-#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_DISABLE    1
+#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE                 0
+#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_DISABLE                1
+#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE_INTROSPECTION   2
 
 /*
  * Sharing ENOMEM helper.
Signature · 1 line
1.7.9.5
Acked-by on v8 3/5
#3 lore raw

Re: [PATCH RFC V8 3/5] xen, libxc: Force-enable relevant MSR events

Quoted 44 lines
Quoted 1 line
Quoted 1 line · On 27.08.14 at 16:01, <[email protected]> wrote:

Vmx_disable_intercept_for_msr() will now refuse to disable interception of MSRs needed for memory introspection. It is not possible to gate this on mem_access being active for the domain, since by the time mem_access does become active the interception for the interesting MSRs has already been disabled (vmx_disable_intercept_for_msr() runs very early on).

Changes since V1:
- Replaced printk() with gdprintk(XENLOG_DEBUG, ...).

Changes since V2:
- Split a log line differently to keep it grepable.
- Interception for relevant MSRs will be disabled only if mem_access is not enabled.
- Since they end up being disabled early on (when mem_access is not enabled yet), re-enable interception when mem_access becomes active.

Changes since V3:
- Removed log line stating that MSR interception cannot be disabled.
- Removed superfluous #include <asm/hvm/vmx/vmcs.h>.
- Moved VMX-specific code to vmx.c (as a new hvm_funcs member).

Changes since V5:
- Added xc_mem_access_enable_introspection() to libxc, which has the same parameters and semantics as xc_mem_access_enable(), but it additionally sets d->arch.hvm_domain.introspection_enabled and enables relevant MSR interception.
- Renamed vmx_enable_intro_msr_interception() to vmx_enable_msr_exit_interception().
- Simplified vmx_enable_msr_exit_interception() (const array of MSRs).

Changes since V6:
- Moved the array of interesting MSRs to common header.
- Minor code cleanup.

Changes since V7:
- Reversed if conditions (cheapest one first).
- Combined two if statements.
- Moved "bool_t introspection_enabled;" to the bool_t group above.
- Renamed msrs_exit_array to vmx_msrs_exit_array and made it "extern" in the header.

Quoted 13 lines
---
 tools/libxc/xc_mem_access.c        |   10 +++++++++-
 tools/libxc/xc_mem_event.c         |    7 +++++--
 tools/libxc/xc_private.h           |    2 +-
 tools/libxc/xenctrl.h              |    2 ++
 xen/arch/x86/hvm/vmx/vmcs.c        |   25 +++++++++++++++++++++++++
 xen/arch/x86/hvm/vmx/vmx.c         |   12 ++++++++++++
 xen/arch/x86/mm/mem_event.c        |   11 +++++++++++
 xen/include/asm-x86/hvm/domain.h   |    1 +
 xen/include/asm-x86/hvm/hvm.h      |    2 ++
 xen/include/asm-x86/hvm/vmx/vmcs.h |    3 +++
 xen/include/public/domctl.h        |    7 ++++---
 11 files changed, 75 insertions(+), 7 deletions(-)

For the hypervisor parts:

#4 lore raw

Re: [PATCH RFC V8 3/5] xen, libxc: Force-enable relevant MSR events

In reply to Razvan Cojocaru
Quoted 164 lines

From: Razvan Cojocaru [mailto:[email protected]]
Sent: Wednesday, August 27, 2014 7:02 AM

Vmx_disable_intercept_for_msr() will now refuse to disable interception of MSRs needed for memory introspection. It is not possible to gate this on mem_access being active for the domain, since by the time mem_access does become active the interception for the interesting MSRs has already been disabled (vmx_disable_intercept_for_msr() runs very early on).

Changes since V1:
- Replaced printk() with gdprintk(XENLOG_DEBUG, ...).

Changes since V2:
- Split a log line differently to keep it grepable.
- Interception for relevant MSRs will be disabled only if mem_access is not enabled.
- Since they end up being disabled early on (when mem_access is not enabled yet), re-enable interception when mem_access becomes active.

Changes since V3:
- Removed log line stating that MSR interception cannot be disabled.
- Removed superfluous #include <asm/hvm/vmx/vmcs.h>.
- Moved VMX-specific code to vmx.c (as a new hvm_funcs member).

Changes since V5:
- Added xc_mem_access_enable_introspection() to libxc, which has the same parameters and semantics as xc_mem_access_enable(), but it additionally sets d->arch.hvm_domain.introspection_enabled and enables relevant MSR interception.
- Renamed vmx_enable_intro_msr_interception() to vmx_enable_msr_exit_interception().
- Simplified vmx_enable_msr_exit_interception() (const array of MSRs).

Changes since V6:
- Moved the array of interesting MSRs to common header.
- Minor code cleanup.

Changes since V7:
- Reversed if conditions (cheapest one first).
- Combined two if statements.
- Moved "bool_t introspection_enabled;" to the bool_t group above.
- Renamed msrs_exit_array to vmx_msrs_exit_array and made it "extern" in the header.

---
 tools/libxc/xc_mem_access.c        |   10 +++++++++-
 tools/libxc/xc_mem_event.c         |    7 +++++--
 tools/libxc/xc_private.h           |    2 +-
 tools/libxc/xenctrl.h              |    2 ++
 xen/arch/x86/hvm/vmx/vmcs.c        |   25
+++++++++++++++++++++++++
 xen/arch/x86/hvm/vmx/vmx.c         |   12 ++++++++++++
 xen/arch/x86/mm/mem_event.c        |   11 +++++++++++
 xen/include/asm-x86/hvm/domain.h   |    1 +
 xen/include/asm-x86/hvm/hvm.h      |    2 ++
 xen/include/asm-x86/hvm/vmx/vmcs.h |    3 +++
 xen/include/public/domctl.h        |    7 ++++---
 11 files changed, 75 insertions(+), 7 deletions(-)
tools/libxc/xc_mem_access.c +5 -1
index 461f0e9..55d0e9f 100644
@@ -26,7 +26,15 @@
 
 void *xc_mem_access_enable(xc_interface *xch, domid_t domain_id,
 uint32_t *port)
 {
-    return xc_mem_event_enable(xch, domain_id,
 HVM_PARAM_ACCESS_RING_PFN, port);
+    return xc_mem_event_enable(xch, domain_id,
 HVM_PARAM_ACCESS_RING_PFN,
+                               port, 0);
+}
+
+void *xc_mem_access_enable_introspection(xc_interface *xch, domid_t

domain_id,

tools/libxc/xc_mem_access.c +3 -0
+                                         uint32_t *port)
+{
+    return xc_mem_event_enable(xch, domain_id,

HVM_PARAM_ACCESS_RING_PFN,

tools/libxc/xc_mem_access.c +1 -0
+                               port, 1);
 }
 
 int xc_mem_access_disable(xc_interface *xch, domid_t domain_id)
tools/libxc/xc_mem_event.c +1 -1
index faf1cc6..8c0be4e 100644
@@ -57,7 +57,7 @@ int xc_mem_event_memop(xc_interface *xch, domid_t
 domain_id,
 }
 
 void *xc_mem_event_enable(xc_interface *xch, domid_t domain_id, int
 param,
-                          uint32_t *port)
+                          uint32_t *port, int enable_introspection)
 {
     void *ring_page = NULL;
     uint64_t pfn;
tools/libxc/xc_mem_event.c +4 -1
@@ -120,7 +120,10 @@ void *xc_mem_event_enable(xc_interface *xch,
 domid_t domain_id, int param,
         break;
 
     case HVM_PARAM_ACCESS_RING_PFN:
-        op = XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE;
+        if ( enable_introspection )
+            op =
 XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE_INTROSPECTION;
+        else
+            op = XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE;
         mode = XEN_DOMCTL_MEM_EVENT_OP_ACCESS;
         break;
tools/libxc/xc_private.h +1 -1
index c50a7c9..94df688 100644
@@ -381,6 +381,6 @@ int xc_mem_event_memop(xc_interface *xch,
 domid_t domain_id,
  * param can be HVM_PARAM_PAGING/ACCESS/SHARING_RING_PFN
  */
 void *xc_mem_event_enable(xc_interface *xch, domid_t domain_id, int
 param,
-                          uint32_t *port);
+                          uint32_t *port, int enable_introspection);

#endif /* __XC_PRIVATE_H__ */

tools/libxc/xenctrl.h +2 -0
index 1c5d0db..28b5562 100644
@@ -2263,6 +2263,8 @@ int xc_mem_paging_load(xc_interface *xch,
 domid_t domain_id,
  * Caller has to unmap this page when done.
  */
 void *xc_mem_access_enable(xc_interface *xch, domid_t domain_id,
 uint32_t *port);
+void *xc_mem_access_enable_introspection(xc_interface *xch, domid_t
 domain_id,
+                                         uint32_t *port);

int xc_mem_access_disable(xc_interface *xch, domid_t domain_id); int xc_mem_access_resume(xc_interface *xch, domid_t domain_id);

xen/arch/x86/hvm/vmx/vmcs.c +3 -0
index 4a4f4e1..44aca73 100644
@@ -39,6 +39,7 @@
 #include <xen/keyhandler.h>
 #include <asm/shadow.h>
 #include <asm/tboot.h>
+#include <asm/mem_event.h>
 
 static bool_t __read_mostly opt_vpid_enabled = 1;
 boolean_param("vpid", opt_vpid_enabled);
@@ -71,6 +72,18 @@ u32 vmx_vmexit_control __read_mostly;
 u32 vmx_vmentry_control __read_mostly;
 u64 vmx_ept_vpid_cap __read_mostly;
 
+const u32 vmx_msrs_exit_array[] = {
+    MSR_IA32_SYSENTER_EIP,

xen/arch/x86/hvm/vmx/vmcs.c u32 vmx_vmexit_control __read_mostly; · line 86

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +10 -0
+    MSR_IA32_SYSENTER_ESP,
+    MSR_IA32_SYSENTER_CS,
+    MSR_IA32_MC0_CTL,
+    MSR_STAR,
+    MSR_LSTAR
+};
+
+const unsigned int vmx_msrs_exit_array_size =
+    ARRAY_SIZE(vmx_msrs_exit_array);
+

prefer a name including 'force' purpose or at least have a comment to describe the intention, so its meaning is explained as early as possible.

Quoted 15 lines
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
 static DEFINE_PER_CPU_READ_MOSTLY(struct vmcs_struct *,
 vmxon_region);
 static DEFINE_PER_CPU(struct vmcs_struct *, current_vmcs);

static DEFINE_PER_CPU(struct list_head, active_vmcs_list);

xen/arch/x86/hvm/vmx/vmcs.c +2 -0
@@ -695,11 +708,23 @@ static void vmx_set_host_env(struct vcpu *v)
 void vmx_disable_intercept_for_msr(struct vcpu *v, u32 msr, int type)
 {
     unsigned long *msr_bitmap = v->arch.hvm_vmx.msr_bitmap;
+    struct domain *d = v->domain;
 
     /* VMX MSR bitmap supported? */
     if ( msr_bitmap == NULL )
         return;
 
+    if ( unlikely(d->arch.hvm_domain.introspection_enabled) &&

xen/arch/x86/hvm/vmx/vmcs.c static void vmx_set_host_env(struct vcpu *v) · line 727

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +10 -0
+         mem_event_check_ring(&d->mem_event->access) )
+    {
+        unsigned int i;
+
+        /* Filter out MSR-s needed for memory introspection */
+        for ( i = 0; i < vmx_msrs_exit_array_size; i++ )
+            if ( msr == vmx_msrs_exit_array[i] )
+                return;
+    }
+

the comment is very introspection specific, while the array name is generic. better to change array name since it isn't generic under the current condition.

Quoted 14 lines
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
     /*
      * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
      * have the write-low and read-high bitmap offsets the wrong way

round.

xen/arch/x86/hvm/vmx/vmx.c +2 -0
index dc18a72..40e6c2c 100644
@@ -1682,6 +1682,17 @@ void vmx_hypervisor_cpuid_leaf(uint32_t sub_idx,
         *eax |= XEN_HVM_CPUID_X2APIC_VIRT;
 }
 
+static void vmx_enable_msr_exit_interception(struct domain *d)
+{

xen/arch/x86/hvm/vmx/vmx.c void vmx_hypervisor_cpuid_leaf(uint32_t sub_idx, · line 1696

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +9 -0
+    struct vcpu *v;
+    unsigned int i;
+
+    /* Enable interception for MSRs needed for memory introspection. */
+    for_each_vcpu ( d, v )
+        for ( i = 0; i < vmx_msrs_exit_array_size; i++ )
+            vmx_enable_intercept_for_msr(v, vmx_msrs_exit_array[i],
 MSR_TYPE_W);
+}
+

so you need same conditional check as you added for vmx_disable_intercept_for_msr

Quoted 121 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
 static struct hvm_function_table __initdata vmx_function_table = {
     .name                 = "VMX",

.cpu_up_prepare = vmx_cpu_up_prepare,

xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -1740,6 +1751,7 @@ static struct hvm_function_table __initdata
 vmx_function_table = {
     .handle_eoi           = vmx_handle_eoi,
     .nhvm_hap_walk_L1_p2m = nvmx_hap_walk_L1_p2m,
     .hypervisor_cpuid_leaf = vmx_hypervisor_cpuid_leaf,
+    .enable_msr_exit_interception = vmx_enable_msr_exit_interception,
 };
 

const struct hvm_function_table * __init start_vmx(void)

xen/arch/x86/mm/mem_event.c +1 -0
index ba7e71e..fdd5ff6 100644
@@ -587,6 +587,7 @@ int mem_event_domctl(struct domain *d,
 xen_domctl_mem_event_op_t *mec,
         switch( mec->op )
         {
         case XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE:
+        case

XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE_INTROSPECTION:

         {
             rc = -ENODEV;
             /* Only HAP is supported */
xen/arch/x86/mm/mem_event.c +2 -0
@@ -600,13 +601,23 @@ int mem_event_domctl(struct domain *d,
 xen_domctl_mem_event_op_t *mec,
             rc = mem_event_enable(d, mec, med, _VPF_mem_access,
 
 HVM_PARAM_ACCESS_RING_PFN,
                                     mem_access_notification);
+
+            if ( mec->op !=

XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE &&

xen/arch/x86/mm/mem_event.c +8 -0
+                 rc == 0 && hvm_funcs.enable_msr_exit_interception )
+            {
+                d->arch.hvm_domain.introspection_enabled = 1;
+                hvm_funcs.enable_msr_exit_interception(d);
+            }
         }
         break;
 
         case XEN_DOMCTL_MEM_EVENT_OP_ACCESS_DISABLE:
         {
             if ( med->ring_page )
+            {
                 rc = mem_event_disable(d, med);
+                d->arch.hvm_domain.introspection_enabled = 0;
+            }
         }
         break;
xen/include/asm-x86/hvm/domain.h +1 -0
index 291a2e0..30d4aa3 100644
@@ -134,6 +134,7 @@ struct hvm_domain {
     bool_t                 mem_sharing_enabled;
     bool_t                 qemu_mapcache_invalidate;
     bool_t                 is_s3_suspended;
+    bool_t                 introspection_enabled;
 
     /*
      * TSC value that VCPUs use to calculate their tsc_offset value.
xen/include/asm-x86/hvm/hvm.h +2 -0
index 0ebd478..069ba39 100644
@@ -205,6 +205,8 @@ struct hvm_function_table {
     void (*hypervisor_cpuid_leaf)(uint32_t sub_idx,
                                   uint32_t *eax, uint32_t *ebx,
                                   uint32_t *ecx, uint32_t *edx);
+
+    void (*enable_msr_exit_interception)(struct domain *d);
 };
 
 extern struct hvm_function_table hvm_funcs;
xen/include/asm-x86/hvm/vmx/vmcs.h +3 -0
index 215d93c..f2e4d82 100644
@@ -471,6 +471,9 @@ enum vmcs_field {
     HOST_RIP                        = 0x00006c16,
 };
 
+extern const u32 vmx_msrs_exit_array[];
+extern const unsigned int vmx_msrs_exit_array_size;
+
 #define VMCS_VPID_WIDTH 16
 
 #define MSR_TYPE_R 1
xen/include/public/domctl.h +1 -1
index 5b11bbf..a3431ec 100644
@@ -772,10 +772,11 @@ struct xen_domctl_gdbsx_domstatus {
  * ENODEV - host lacks HAP support (EPT/NPT) or HAP is disabled in guest
  * EBUSY  - guest has or had access enabled, ring buffer still active
  */
-#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS            2
+#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS

2

xen/include/public/domctl.h +1 -2
-#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE     0
-#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_DISABLE    1
+#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE

0
+#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_DISABLE
1
+#define XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE_INTROSPECTION 2

/*
* Sharing ENOMEM helper.
--
1.7.9.5

#5 lore raw

Re: [PATCH RFC V8 3/5] xen, libxc: Force-enable relevant MSR events

Quoted 25 lines · Kevin
Quoted 25 lines

From: Razvan Cojocaru [mailto:[email protected]]
Sent: Wednesday, August 27, 2014 7:02 AM

Vmx_disable_intercept_for_msr() will now refuse to disable interception of MSRs needed for memory introspection. It is not possible to gate this on mem_access being active for the domain, since by the time mem_access does become active the interception for the interesting MSRs has already been disabled (vmx_disable_intercept_for_msr() runs very early on).

xen/arch/x86/hvm/vmx/vmcs.c +1 -0
index 4a4f4e1..44aca73 100644
@@ -39,6 +39,7 @@
 #include <xen/keyhandler.h>
 #include <asm/shadow.h>
 #include <asm/tboot.h>
+#include <asm/mem_event.h>
 
 static bool_t __read_mostly opt_vpid_enabled = 1;
 boolean_param("vpid", opt_vpid_enabled);
@@ -71,6 +72,18 @@ u32 vmx_vmexit_control __read_mostly;
 u32 vmx_vmentry_control __read_mostly;
 u64 vmx_ept_vpid_cap __read_mostly;
 
Quoted 15 lines

xen/arch/x86/hvm/vmx/vmcs.c u32 vmx_vmexit_control __read_mostly; · line 86

Quoted 12 lines
xen/arch/x86/hvm/vmx/vmcs.c +12 -0
+const u32 vmx_msrs_exit_array[] = {
+    MSR_IA32_SYSENTER_EIP,
+    MSR_IA32_SYSENTER_ESP,
+    MSR_IA32_SYSENTER_CS,
+    MSR_IA32_MC0_CTL,
+    MSR_STAR,
+    MSR_LSTAR
+};
+
+const unsigned int vmx_msrs_exit_array_size =
+    ARRAY_SIZE(vmx_msrs_exit_array);
+

prefer a name including 'force' purpose or at least have a comment to describe the intention, so its meaning is explained as early as possible.

How would vmx_force_enabled_msrs_array (with a comment added) sound to you and Jan?

Quoted 12 lines
Quoted 12 lines
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
     /*
      * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
      * have the write-low and read-high bitmap offsets the wrong way

round.

xen/arch/x86/hvm/vmx/vmx.c +0 -0
index dc18a72..40e6c2c 100644
@@ -1682,6 +1682,17 @@ void vmx_hypervisor_cpuid_leaf(uint32_t sub_idx,
         *eax |= XEN_HVM_CPUID_X2APIC_VIRT;
 }
 
Quoted 14 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_hypervisor_cpuid_leaf(uint32_t sub_idx, · line 1696

Quoted 12 lines
xen/arch/x86/hvm/vmx/vmx.c +11 -0
+static void vmx_enable_msr_exit_interception(struct domain *d)
+{
+    struct vcpu *v;
+    unsigned int i;
+
+    /* Enable interception for MSRs needed for memory introspection. */
+    for_each_vcpu ( d, v )
+        for ( i = 0; i < vmx_msrs_exit_array_size; i++ )
+            vmx_enable_intercept_for_msr(v, vmx_msrs_exit_array[i],
 MSR_TYPE_W);
+}
+

so you need same conditional check as you added for vmx_disable_intercept_for_msr

The check part is in this code:

xen/arch/x86/mm/mem_event.c +0 -0
index ba7e71e..fdd5ff6 100644
@@ -587,6 +587,7 @@ int mem_event_domctl(struct domain *d,

xen_domctl_mem_event_op_t *mec,

         switch( mec->op )
         {
         case XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE:
+        case XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE_INTROSPECTION:
         {
             rc = -ENODEV;
             /* Only HAP is supported */

@@ -600,13 +601,23 @@ int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
rc = mem_event_enable(d, mec, med, _VPF_mem_access,
HVM_PARAM_ACCESS_RING_PFN, mem_access_notification);
+
+ if ( mec->op != XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE &&
+ rc == 0 && hvm_funcs.enable_msr_exit_interception )
+ {
+ d->arch.hvm_domain.introspection_enabled = 1;
+ hvm_funcs.enable_msr_exit_interception(d);
+ }

         }
         break;

         case XEN_DOMCTL_MEM_EVENT_OP_ACCESS_DISABLE:
         {
             if ( med->ring_page )
+            {
                 rc = mem_event_disable(d, med);
+                d->arch.hvm_domain.introspection_enabled = 0;
+            }
         }
         break;

So if we do a domctl with XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE_INTROSPECTION, then we must make sure that those MSRs are being intercepted (so only then do we call hvm_funcs.enable_msr_exit_interception(d)).

The previous check (the one you've mentioned) only makes sure that if d->arch.hvm_domain.introspection_enabled is true, interception for the interesting MSRs cannot be disabled.

Thanks,
Razvan Cojocaru

#6 lore raw

Re: [PATCH RFC V8 3/5] xen, libxc: Force-enable relevant MSR events

Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 28.08.14 at 09:40, <[email protected]> wrote:
Quoted 8 lines · Kevin
Quoted 8 lines

From: Razvan Cojocaru [mailto:[email protected]]

(quoted diff) +7 -0
+const u32 vmx_msrs_exit_array[] = {
+    MSR_IA32_SYSENTER_EIP,
+    MSR_IA32_SYSENTER_ESP,
+    MSR_IA32_SYSENTER_CS,
+    MSR_IA32_MC0_CTL,
+    MSR_STAR,
+    MSR_LSTAR
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+};
+
+const unsigned int vmx_msrs_exit_array_size =
+    ARRAY_SIZE(vmx_msrs_exit_array);
+

prefer a name including 'force' purpose or at least have a comment to describe the intention, so its meaning is explained as early as possible.

Quoted 2 lines

How would vmx_force_enabled_msrs_array (with a comment added) sound to you and Jan?

vmx_introspection_force_enabled_msrs[]?

Jan

#7 lore raw

Re: [PATCH RFC V8 3/5] xen, libxc: Force-enable relevant MSR events

Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 28.08.14 at 09:40, <[email protected]> wrote:
Quoted 16 lines · Kevin

Review

Quoted 13 lines

From: Razvan Cojocaru [mailto:[email protected]]

(quoted diff) +12 -0
+const u32 vmx_msrs_exit_array[] = {
+    MSR_IA32_SYSENTER_EIP,
+    MSR_IA32_SYSENTER_ESP,
+    MSR_IA32_SYSENTER_CS,
+    MSR_IA32_MC0_CTL,
+    MSR_STAR,
+    MSR_LSTAR
+};
+
+const unsigned int vmx_msrs_exit_array_size =
+    ARRAY_SIZE(vmx_msrs_exit_array);
+

prefer a name including 'force' purpose or at least have a comment to describe the intention, so its meaning is explained as early as possible.

Quoted 4 lines
Quoted 2 lines

How would vmx_force_enabled_msrs_array (with a comment added) sound to you and Jan?

vmx_introspection_force_enabled_msrs[]?

No problem, I'll rename it to that.

Thanks,
Razvan Cojocaru

#8 lore raw

Re: [PATCH RFC V8 3/5] xen, libxc: Force-enable relevant MSR events

In reply to Razvan Cojocaru
Quoted 10 lines

From: Razvan Cojocaru [mailto:[email protected]]
Sent: Thursday, August 28, 2014 12:40 AM

So if we do a domctl with XEN_DOMCTL_MEM_EVENT_OP_ACCESS_ENABLE_INTROSPECTION, then we must make
sure that those MSRs are being intercepted (so only then do we call hvm_funcs.enable_msr_exit_interception(d)).

Quoted 3 lines

The previous check (the one you've mentioned) only makes sure that if d->arch.hvm_domain.introspection_enabled is true, interception for the interesting MSRs cannot be disabled.

modularly you don't want a common vmx interface assuming caller's behavior but without any check, or you make that interface name introspection specific.

Quoted 4 lines

Thanks,
Razvan Cojocaru

#9 lore raw

RFC PATCH v8 4/5 xen, libxc: Request page fault injection via libxc

In reply to Razvan Cojocaru

Added new XEN_DOMCTL_set_pagefault_info hypercall, used by libxc's new xc_domain_set_pagefault_info() function to set per-domain page fault injection information. All a call does is set per-domain info, and nothing actually happens until VMENTRY time, and then only if all conditions are met (the guest is in user mode, the set value matches CR3, and there are no other pending traps).
This mechanism allows bringing in swapped-out pages for inspection.

Changes since V2:
- Removed superfluous memset(&ctxt, 0, sizeof(struct hvm_hw_cpu)).
- Now checking SS.DPL instead of CS.DPL to see if the guest is in user mode.
- Removed superfluous fault_info members initialization.
- Now returning -EINVAL if !has_hvm_container_domain(d) on XEN_DOMCTL_set_pagefault_info.
- Moved struct fault_info from struct domain to struct hvm_domain.
- Collapsed explicit initialization of the fault_info members into a one-line assignment on the tools/libxc side.
- Now using write_access as a proper bool instead of shifting and OR-ing it with PFEC_user_mode.

Changes since V3:
- Now checking that is_hvm_domain(d) in do_domctl() (consistent with the test in the actual page fault injection code).
- XEN_DOMCTL_set_pagefault_info now extends the proper range.
- Added a likely() around the d->arch.hvm_domain.fault_info.virtual_address == 0 check.
- Replaced hvm_funcs.save_cpu_ctxt() function call with lighter code.
- Split the vmx.c page fault injection function into a check function and an inject function.

Changes since V4:
- Added "valid" flag instead of relying on virtual address 0 as a special case.
- Using #defines instead of magic constants in vmx_vmcs_save().
- Padded struct xen_domctl_set_pagefault_info.
- Coding style and readability changes.

Changes since V5:
- Renamed "struct domain *d" to "currd" where appropriate.
- Using ! to test bool_t variables.
- Replaced hvm_get_segment_register() with vmx_get_segment_register() in vmx.c.
- Removed double blank line.
- Proper comparison of CR3 and address_space (taking into account LM, PAE).
- Using domain variable already set up in do_domctl().
- Removed optional parentheses.
- Removed d->arch.hvm_domain.fault_info reset (only the "valid" flag counts anyway).

Changes since V6:
- Removed the write_access flag, now passing the whole error code (still OR-ed with PFEC_user_mode).
- Replaced hex constants with #defined macros.
- Minor code / coding style cleanup.

Changes since V7:
- Removed pointless parentheses and curly braces.
- Renamed xc_domain_set_pagefault_info() to xc_domain_request_pagefault().

---
 tools/libxc/xc_domain.c          |   16 +++++++++++
 tools/libxc/xenctrl.h            |    4 +++
 xen/arch/x86/hvm/vmx/vmx.c       |   56 ++++++++++++++++++++++++++++++++++++++
 xen/common/domctl.c              |   17 ++++++++++++
 xen/include/asm-x86/hvm/domain.h |    8 ++++++
 xen/include/public/domctl.h      |   17 ++++++++++++
 6 files changed, 118 insertions(+)
tools/libxc/xc_domain.c +16 -0
index c67ac9a..3fb03be 100644
@@ -507,6 +507,22 @@ int xc_domain_hvm_setcontext(xc_interface *xch,
     return ret;
 }
 
+int xc_domain_request_pagefault(xc_interface *xch,
+                                uint32_t domid,
+                                xen_domctl_request_pagefault_info_t *info)
+{
+    DECLARE_DOMCTL;
+
+    if (info == NULL)
+        return -1;
+
+    domctl.cmd = XEN_DOMCTL_request_pagefault;
+    domctl.domain = (domid_t)domid;
+    domctl.u.request_pagefault_info = *info;
+
+    return do_domctl(xch, &domctl);
+}
+
 int xc_vcpu_getcontext(xc_interface *xch,
                        uint32_t domid,
                        uint32_t vcpu,
tools/libxc/xenctrl.h +4 -0
index 28b5562..9968e83 100644
@@ -803,6 +803,10 @@ int xc_domain_hvm_setcontext(xc_interface *xch,
 const char *xc_domain_get_native_protocol(xc_interface *xch,
                                           uint32_t domid);
 
+int xc_domain_request_pagefault(xc_interface *xch,
+                                uint32_t domid,
+                                xen_domctl_request_pagefault_info_t *info);
+
 /**
  * This function returns information about the execution context of a
  * particular vcpu of a domain.
xen/arch/x86/hvm/vmx/vmx.c +56 -0

68 lines not on this page · raw message

xen/common/domctl.c +17 -0
index c326aba..75ff6c6 100644
@@ -967,6 +967,23 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
     }
     break;
 
+    case XEN_DOMCTL_request_pagefault:
+        ret = -EINVAL;
+
+        if ( is_hvm_domain(d) )
+        {
+            d->arch.hvm_domain.fault_info.address_space =
+                op->u.request_pagefault_info.address_space;
+            d->arch.hvm_domain.fault_info.virtual_address =
+                op->u.request_pagefault_info.virtual_address;
+            d->arch.hvm_domain.fault_info.errcode =
+                op->u.request_pagefault_info.errcode;
+            d->arch.hvm_domain.fault_info.valid = 1;
+
+            ret = 0;
+        }
+        break;
+
     default:
         ret = arch_do_domctl(op, d, u_domctl);
         break;
xen/include/asm-x86/hvm/domain.h +8 -0
index 30d4aa3..30956e4 100644
@@ -142,6 +142,14 @@ struct hvm_domain {
      */
     uint64_t sync_tsc;
 
+    /* Memory introspection page fault injection data. */
+    struct {
+        uint64_t address_space;
+        uint64_t virtual_address;
+        uint32_t errcode;
+        bool_t valid;
+    } fault_info;
+
     union {
         struct vmx_domain vmx;
         struct svm_domain svm;
xen/include/public/domctl.h +17 -0
index a3431ec..69224be 100644
@@ -937,6 +937,21 @@ typedef struct xen_domctl_vcpu_msrs xen_domctl_vcpu_msrs_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_vcpu_msrs_t);
 #endif
 
+/*
+ * XEN_DOMCTL_set_pagefault_info requests that a page fault occur at
+ * the next VMENTRY.
+ */
+struct xen_domctl_request_pagefault_info {
+    uint64_t address_space;
+    uint64_t virtual_address;
+    uint32_t errcode;
+    uint32_t _pad;
+};
+typedef struct xen_domctl_request_pagefault_info
+    xen_domctl_request_pagefault_info_t;
+DEFINE_XEN_GUEST_HANDLE(xen_domctl_request_pagefault_info_t);
+
+
 struct xen_domctl {
     uint32_t cmd;
 #define XEN_DOMCTL_createdomain                   1
@@ -1009,6 +1024,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_cacheflush                    71
 #define XEN_DOMCTL_get_vcpu_msrs                 72
 #define XEN_DOMCTL_set_vcpu_msrs                 73
+#define XEN_DOMCTL_request_pagefault             74
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1069,6 +1085,7 @@ struct xen_domctl {
         struct xen_domctl_cacheflush        cacheflush;
         struct xen_domctl_gdbsx_pauseunp_vcpu gdbsx_pauseunp_vcpu;
         struct xen_domctl_gdbsx_domstatus   gdbsx_domstatus;
+        struct xen_domctl_request_pagefault_info request_pagefault_info;
         uint8_t                             pad[128];
     } u;
 };
Signature · 1 line
1.7.9.5
#10 lore raw

Re: [PATCH RFC V8 4/5] xen, libxc: Request page fault injection via libxc

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 27.08.14 at 16:01, <[email protected]> wrote:

xen/include/asm-x86/hvm/domain.h struct hvm_domain { · line 151

Quoted 10 lines
xen/include/asm-x86/hvm/domain.h +7 -0
@@ -142,6 +142,14 @@ struct hvm_domain {
      */
     uint64_t sync_tsc;
 
+    /* Memory introspection page fault injection data. */
+    struct {
+        uint64_t address_space;
+        uint64_t virtual_address;
+        uint32_t errcode;
+        bool_t valid;
+    } fault_info;

The naming of the field aside I continue to be unconvinced of the general usefulness of this when being domain rather than vCPU based.

Jan

#11 lore raw

Re: [PATCH RFC V8 4/5] xen, libxc: Request page fault injection via libxc

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 27.08.14 at 16:01, <[email protected]> wrote:
xen/include/asm-x86/hvm/domain.h +0 -0
@@ -142,6 +142,14 @@ struct hvm_domain {
      */
     uint64_t sync_tsc;
 
Quoted 11 lines

xen/include/asm-x86/hvm/domain.h struct hvm_domain { · line 151

Quoted 7 lines
xen/include/asm-x86/hvm/domain.h +7 -0
+    /* Memory introspection page fault injection data. */
+    struct {
+        uint64_t address_space;
+        uint64_t virtual_address;
+        uint32_t errcode;
+        bool_t valid;
+    } fault_info;

The naming of the field aside I continue to be unconvinced of the general usefulness of this when being domain rather than vCPU based.

Oh, I've renamed everything else but missed that one, sorry. I'll make sure to rename it and look into a way of maybe making it VCPU-based.

Thanks,
Razvan Cojocaru

#12 lore raw

Re: [PATCH RFC V8 4/5] xen, libxc: Request page fault injection via libxc

In reply to Razvan Cojocaru
Quoted 10 lines

From: Razvan Cojocaru [mailto:[email protected]]
Sent: Wednesday, August 27, 2014 7:02 AM

Added new XEN_DOMCTL_set_pagefault_info hypercall, used by libxc's new xc_domain_set_pagefault_info() function to set per-domain page fault injection information. All a call does is set per-domain info, and nothing actually happens until VMENTRY time, and then only if all conditions are met (the guest is in user mode, the set value matches CR3, and there are no other pending traps).
This mechanism allows bringing in swapped-out pages for inspection.

Is there any impact on the whole introspection process, regarding to when next VM exit on the inspected address space actually comes?

btw how is the whole flow of inspecting swapped-out pages with this interface?

Quoted 116 lines

Changes since V2:
- Removed superfluous memset(&ctxt, 0, sizeof(struct hvm_hw_cpu)).
- Now checking SS.DPL instead of CS.DPL to see if the guest is in user mode.
- Removed superfluous fault_info members initialization.
- Now returning -EINVAL if !has_hvm_container_domain(d) on XEN_DOMCTL_set_pagefault_info.
- Moved struct fault_info from struct domain to struct hvm_domain.
- Collapsed explicit initialization of the fault_info members into a one-line assignment on the tools/libxc side.
- Now using write_access as a proper bool instead of shifting and OR-ing it with PFEC_user_mode.

Changes since V3:
- Now checking that is_hvm_domain(d) in do_domctl() (consistent with the test in the actual page fault injection code).
- XEN_DOMCTL_set_pagefault_info now extends the proper range.
- Added a likely() around the d->arch.hvm_domain.fault_info.virtual_address == 0 check.
- Replaced hvm_funcs.save_cpu_ctxt() function call with lighter code.
- Split the vmx.c page fault injection function into a check function and an inject function.

Changes since V4:
- Added "valid" flag instead of relying on virtual address 0 as a special case.
- Using #defines instead of magic constants in vmx_vmcs_save().
- Padded struct xen_domctl_set_pagefault_info.
- Coding style and readability changes.

Changes since V5:
- Renamed "struct domain *d" to "currd" where appropriate.
- Using ! to test bool_t variables.
- Replaced hvm_get_segment_register() with vmx_get_segment_register() in vmx.c.
- Removed double blank line.
- Proper comparison of CR3 and address_space (taking into account LM, PAE).
- Using domain variable already set up in do_domctl().
- Removed optional parentheses.
- Removed d->arch.hvm_domain.fault_info reset (only the "valid" flag counts anyway).

Changes since V6:
- Removed the write_access flag, now passing the whole error code (still OR-ed with PFEC_user_mode).
- Replaced hex constants with #defined macros.
- Minor code / coding style cleanup.

Changes since V7:
- Removed pointless parentheses and curly braces.
- Renamed xc_domain_set_pagefault_info() to xc_domain_request_pagefault().

---
 tools/libxc/xc_domain.c          |   16 +++++++++++
 tools/libxc/xenctrl.h            |    4 +++
 xen/arch/x86/hvm/vmx/vmx.c       |   56
++++++++++++++++++++++++++++++++++++++
 xen/common/domctl.c              |   17 ++++++++++++
 xen/include/asm-x86/hvm/domain.h |    8 ++++++
 xen/include/public/domctl.h      |   17 ++++++++++++
 6 files changed, 118 insertions(+)
tools/libxc/xc_domain.c +16 -0
index c67ac9a..3fb03be 100644
@@ -507,6 +507,22 @@ int xc_domain_hvm_setcontext(xc_interface *xch,
     return ret;
 }
 
+int xc_domain_request_pagefault(xc_interface *xch,
+                                uint32_t domid,
+                                xen_domctl_request_pagefault_info_t
 *info)
+{
+    DECLARE_DOMCTL;
+
+    if (info == NULL)
+        return -1;
+
+    domctl.cmd = XEN_DOMCTL_request_pagefault;
+    domctl.domain = (domid_t)domid;
+    domctl.u.request_pagefault_info = *info;
+
+    return do_domctl(xch, &domctl);
+}
+
 int xc_vcpu_getcontext(xc_interface *xch,
                        uint32_t domid,
                        uint32_t vcpu,
tools/libxc/xenctrl.h +4 -0
index 28b5562..9968e83 100644
@@ -803,6 +803,10 @@ int xc_domain_hvm_setcontext(xc_interface *xch,
 const char *xc_domain_get_native_protocol(xc_interface *xch,
                                           uint32_t domid);
 
+int xc_domain_request_pagefault(xc_interface *xch,
+                                uint32_t domid,
+                                xen_domctl_request_pagefault_info_t
 *info);
+
 /**
  * This function returns information about the execution context of a

* particular vcpu of a domain.

xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 40e6c2c..0152f4e 100644

xen/arch/x86/hvm/vmx/vmx.c out: · line 3111

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -0
@@ -3107,6 +3107,59 @@ out:
         nvmx_idtv_handling();
 }
 
+static bool_t vmx_check_pf_injection(void)
+{

aside from structure name pointed earlier, above and later function names are also too generic while their purpose is very introspection specific. Leaving them this way will confuse other developers in vmx module in the future.

Quoted 39 lines
xen/arch/x86/hvm/vmx/vmx.c +37 -0
+    struct vcpu *curr = current;
+    const struct domain *currd = curr->domain;
+    struct segment_register seg;
+    unsigned long ev;
+    uint32_t pending_event = 0;
+    uint64_t mask;
+
+    if ( !is_hvm_domain(currd) ||
+         likely(!currd->arch.hvm_domain.fault_info.valid) )
+        return 0;
+
+    vmx_get_segment_register(curr, x86_seg_ss, &seg);
+
+    if ( seg.attr.fields.dpl != 3 ) /* Guest is not in user mode */
+        return 0;
+
+    if ( hvm_long_mode_enabled(curr) )
+        mask = PADDR_MASK & PAGE_MASK; /* Bits 51:12. */
+    else if ( hvm_pae_enabled(curr) )
+        mask = 0x00000000ffffffe0; /* Bits 31:5. */
+    else
+        mask = (uint32_t)PAGE_MASK; /* Bits 31:12. */
+
+    if ( (curr->arch.hvm_vcpu.guest_cr[3] & mask) !=
+         (currd->arch.hvm_domain.fault_info.address_space & mask) )
+        return 0;
+
+    vmx_vmcs_enter(curr);
+    __vmread(VM_ENTRY_INTR_INFO, &ev);
+    vmx_vmcs_exit(curr);
+
+    if ( (ev & INTR_INFO_VALID_MASK) &&
+         hvm_event_needs_reinjection(MASK_EXTR(ev,
 INTR_INFO_INTR_TYPE_MASK),
+                                     ev &
 INTR_INFO_VECTOR_MASK) )
+        pending_event = ev;
+
+    return pending_event == 0;

xen/arch/x86/hvm/vmx/vmx.c out: · line 3157

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +9 -0
+}
+
+static void vmx_inject_pf(void)
+{
+    struct vcpu *curr = current;
+    struct domain *currd = curr->domain;
+    uint64_t virtual_address =
 currd->arch.hvm_domain.fault_info.virtual_address;
+
+    currd->arch.hvm_domain.fault_info.valid = 0;

Do you need handle the race with next coming fault injection hypercall?

Is the memory introspection caring about how many page faults are injected at one time? two vcpus may both enter the same address space, and trigger the VM exit at the same time, then it's non-deterministic whether both vcpus will receive a page fault.

Quoted 113 lines
xen/arch/x86/hvm/vmx/vmx.c +5 -0
+
+    hvm_inject_page_fault(currd->arch.hvm_domain.fault_info.errcode |
+                          PFEC_user_mode, virtual_address);
+}
+

void vmx_vmenter_helper(const struct cpu_user_regs *regs)

 {
     struct vcpu *curr = current;
xen/arch/x86/hvm/vmx/vmx.c +3 -0
@@ -3147,6 +3200,9 @@ void vmx_vmenter_helper(const struct
 cpu_user_regs *regs)
     if ( unlikely(need_flush) )
         vpid_sync_all();
 
+    if ( vmx_check_pf_injection() )
+        vmx_inject_pf();
+
  out:
     HVMTRACE_ND(VMENTRY, 0, 1/*cycles*/, 0, 0, 0, 0, 0, 0, 0);
xen/common/domctl.c +17 -0
index c326aba..75ff6c6 100644
@@ -967,6 +967,23 @@ long
 do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
     }
     break;
 
+    case XEN_DOMCTL_request_pagefault:
+        ret = -EINVAL;
+
+        if ( is_hvm_domain(d) )
+        {
+            d->arch.hvm_domain.fault_info.address_space =
+                op->u.request_pagefault_info.address_space;
+            d->arch.hvm_domain.fault_info.virtual_address =
+                op->u.request_pagefault_info.virtual_address;
+            d->arch.hvm_domain.fault_info.errcode =
+                op->u.request_pagefault_info.errcode;
+            d->arch.hvm_domain.fault_info.valid = 1;
+
+            ret = 0;
+        }
+        break;
+
     default:
         ret = arch_do_domctl(op, d, u_domctl);
         break;
xen/include/asm-x86/hvm/domain.h +8 -0
index 30d4aa3..30956e4 100644
@@ -142,6 +142,14 @@ struct hvm_domain {
      */
     uint64_t sync_tsc;
 
+    /* Memory introspection page fault injection data. */
+    struct {
+        uint64_t address_space;
+        uint64_t virtual_address;
+        uint32_t errcode;
+        bool_t valid;
+    } fault_info;
+
     union {
         struct vmx_domain vmx;
         struct svm_domain svm;
xen/include/public/domctl.h +15 -0
index a3431ec..69224be 100644
@@ -937,6 +937,21 @@ typedef struct xen_domctl_vcpu_msrs
 xen_domctl_vcpu_msrs_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_vcpu_msrs_t);
 #endif
 
+/*
+ * XEN_DOMCTL_set_pagefault_info requests that a page fault occur at
+ * the next VMENTRY.
+ */
+struct xen_domctl_request_pagefault_info {
+    uint64_t address_space;
+    uint64_t virtual_address;
+    uint32_t errcode;
+    uint32_t _pad;
+};
+typedef struct xen_domctl_request_pagefault_info
+    xen_domctl_request_pagefault_info_t;
+DEFINE_XEN_GUEST_HANDLE(xen_domctl_request_pagefault_info_t);
+
+
 struct xen_domctl {
     uint32_t cmd;

#define XEN_DOMCTL_createdomain 1

xen/include/public/domctl.h +2 -1
@@ -1009,6 +1024,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_cacheflush                    71
 #define XEN_DOMCTL_get_vcpu_msrs                 72
 #define XEN_DOMCTL_set_vcpu_msrs                 73
+#define XEN_DOMCTL_request_pagefault             74
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1069,6 +1085,7 @@ struct xen_domctl {
         struct xen_domctl_cacheflush        cacheflush;
         struct xen_domctl_gdbsx_pauseunp_vcpu gdbsx_pauseunp_vcpu;
         struct xen_domctl_gdbsx_domstatus   gdbsx_domstatus;
+        struct xen_domctl_request_pagefault_info request_pagefault_info;
         uint8_t                             pad[128];
     } u;
 };
--

1.7.9.5

#13 lore raw

Re: [PATCH RFC V8 4/5] xen, libxc: Request page fault injection via libxc

Quoted 11 lines · Kevin
Quoted 10 lines

From: Razvan Cojocaru [mailto:[email protected]]
Sent: Wednesday, August 27, 2014 7:02 AM

Added new XEN_DOMCTL_set_pagefault_info hypercall, used by libxc's new xc_domain_set_pagefault_info() function to set per-domain page fault injection information. All a call does is set per-domain info, and nothing actually happens until VMENTRY time, and then only if all conditions are met (the guest is in user mode, the set value matches CR3, and there are no other pending traps).
This mechanism allows bringing in swapped-out pages for inspection.

Quoted 5 lines

Is there any impact on the whole introspection process, regarding to when next VM exit on the inspected address space actually comes?

btw how is the whole flow of inspecting swapped-out pages with this interface?

Andrei has explained the flow here:
http://lists.xen.org/archives/html/xen-devel/2014-07/msg03013.html

Quoted 11 lines

Review

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +5 -0
+static void vmx_inject_pf(void)
+{
+    struct vcpu *curr = current;
+    struct domain *currd = curr->domain;
+    uint64_t virtual_address =

currd->arch.hvm_domain.fault_info.virtual_address;

(quoted diff) +2 -0
+
+    currd->arch.hvm_domain.fault_info.valid = 0;

Do you need handle the race with next coming fault injection hypercall?

Quoted 4 lines

Is the memory introspection caring about how many page faults are injected at one time? two vcpus may both enter the same address space, and trigger the VM exit at the same time, then it's non-deterministic whether both vcpus will receive a page fault.

No, the application doesn't care if one or more page faults happen on one or more VCPUs, as long as the swapped out page is brought back in. Also, within our model (mem_event-based application) concurrency issues don't really happen by design.

But I do understand your concern, and if I understood things correctly I believe that this was also Jan's objection - to that end I'll look into making the Xen part more generic (VCPU-based instead of domain-based injection requests) and simply only use VCPU 0 from our application.

Thanks,
Razvan Cojocaru

#14 lore raw

Re: [PATCH RFC V8 4/5] xen, libxc: Request page fault injection via libxc

Quoted 15 lines

From: Razvan Cojocaru [mailto:[email protected]]
Sent: Thursday, August 28, 2014 12:55 AM

Quoted 11 lines · Kevin
Quoted 10 lines

From: Razvan Cojocaru [mailto:[email protected]]
Sent: Wednesday, August 27, 2014 7:02 AM

Added new XEN_DOMCTL_set_pagefault_info hypercall, used by libxc's new xc_domain_set_pagefault_info() function to set per-domain page fault injection information. All a call does is set per-domain info, and nothing actually happens until VMENTRY time, and then only if all conditions are met (the guest is in user mode, the set value matches CR3, and there are no other pending traps).
This mechanism allows bringing in swapped-out pages for inspection.

Quoted 8 lines
Quoted 5 lines

Is there any impact on the whole introspection process, regarding to when next VM exit on the inspected address space actually comes?

btw how is the whole flow of inspecting swapped-out pages with this interface?

Andrei has explained the flow here:
http://lists.xen.org/archives/html/xen-devel/2014-07/msg03013.html

Thanks for the link.

I think a vcpu kick would be better here, if we only look at this PF injection interface w/o assuming any specific knowledge into your production. That looks more sane from exception p.o.v.

Thanks,
Kevin

#15 lore raw

RFC PATCH v8 5/5 xen: Handle resumed instruction based on previous mem_event reply

In reply to Razvan Cojocaru

In a scenario where a page fault that triggered a mem_event occured, p2m_mem_access_check() will now be able to either 1) emulate the current instruction, or 2) emulate it, but don't allow it to perform any writes.

Changes since V1:
- Removed the 'skip' code which required computing the current instruction length.
- Removed the set_ad_bits() code that attempted to modify the 'accessed' and 'dirty' bits for instructions that the emulator can't handle at the moment.

Changes since V2:
- Moved the __vmread(EXIT_QUALIFICATION, &exit_qualification); code in vmx.c, accessible via hvm_funcs.
- Incorporated changes by Andrew Cooper ("[PATCH 1/2] Xen/mem_event:
Validate the response vcpu_id before acting on it."

Changes since V3:
- Collapsed verbose lines into a single "else if()".
- Changed an int to bool_t.
- Fixed a minor coding style issue.
- Now computing the first parameter to hvm_emulate_one_full() (replaced an "if" with a single call).
- Added code comments about eip and gla reset (clarity issue).
- Removed duplicate code by Andrew Cooper (introduced in V2, since committed).

Changes since V4:
- Renamed "exited_by_pagefault" to "exited_by_nested_pagefault".
- Folded two long lines.
- Combined two "if" statements.
- Moved "violation = 0;" to the XENMEM_access_rwx case.

Changes since V5:
- Fixed indentation.
- Re-ordered a set of if() conditions.

Changes since V7:
- Now calling hvm_mem_event_emulate_one() instead of hvm_emulate_one_full() (renamed in the first patch).

---
 xen/arch/x86/domain.c          |    3 ++
 xen/arch/x86/hvm/vmx/vmx.c     |   13 +++++++
 xen/arch/x86/mm/p2m.c          |   84 ++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h   |    9 +++++
 xen/include/asm-x86/hvm/hvm.h  |    2 +
 xen/include/public/mem_event.h |   12 +++---
 6 files changed, 118 insertions(+), 5 deletions(-)
xen/arch/x86/domain.c +3 -0
index f7e0e78..7b1dfe6 100644
@@ -415,6 +415,9 @@ int vcpu_initialise(struct vcpu *v)
 
     v->arch.flags = TF_kernel_mode;
 
+    /* By default, do not emulate */
+    v->arch.mem_event.emulate_flags = 0;
+
     rc = mapcache_vcpu_init(v);
     if ( rc )
         return rc;
xen/arch/x86/hvm/vmx/vmx.c +13 -0
index 0152f4e..726d0d0 100644
@@ -1693,6 +1693,18 @@ static void vmx_enable_msr_exit_interception(struct domain *d)
             vmx_enable_intercept_for_msr(v, vmx_msrs_exit_array[i], MSR_TYPE_W);
 }
 
+static bool_t vmx_exited_by_nested_pagefault(void)
+{
+    unsigned long exit_qualification;
+
+    __vmread(EXIT_QUALIFICATION, &exit_qualification);
+
+    if ( (exit_qualification & EPT_GLA_FAULT) == 0 )
+        return 0;
+
+    return 1;
+}
+
 static struct hvm_function_table __initdata vmx_function_table = {
     .name                 = "VMX",
     .cpu_up_prepare       = vmx_cpu_up_prepare,
@@ -1752,6 +1764,7 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .nhvm_hap_walk_L1_p2m = nvmx_hap_walk_L1_p2m,
     .hypervisor_cpuid_leaf = vmx_hypervisor_cpuid_leaf,
     .enable_msr_exit_interception = vmx_enable_msr_exit_interception,
+    .exited_by_nested_pagefault  = vmx_exited_by_nested_pagefault,
 };
 
 const struct hvm_function_table * __init start_vmx(void)
xen/arch/x86/mm/p2m.c +84 -0

102 lines not on this page · raw message

xen/include/asm-x86/domain.h +9 -0
index 83329ed..440aa81 100644
@@ -458,6 +458,15 @@ struct arch_vcpu
 
     /* A secondary copy of the vcpu time info. */
     XEN_GUEST_HANDLE(vcpu_time_info_t) time_info_guest;
+
+    /* Should we emulate the next matching instruction on VCPU resume
+     * after a mem_event? */
+    struct {
+        uint32_t emulate_flags;
+        unsigned long gpa;
+        unsigned long eip;
+    } mem_event;
+
 } __cacheline_aligned;
 
 smap_check_policy_t smap_policy_change(struct vcpu *v,
xen/include/asm-x86/hvm/hvm.h +2 -0
index 069ba39..8f0292c 100644
@@ -207,6 +207,8 @@ struct hvm_function_table {
                                   uint32_t *ecx, uint32_t *edx);
 
     void (*enable_msr_exit_interception)(struct domain *d);
+
+    bool_t (*exited_by_nested_pagefault)(void);
 };
 
 extern struct hvm_function_table hvm_funcs;
xen/include/public/mem_event.h +7 -5
index bbd1636..49f1b84 100644
@@ -31,11 +31,13 @@
 #include "io/ring.h"
 
 /* Memory event flags */
-#define MEM_EVENT_FLAG_VCPU_PAUSED  (1 << 0)
-#define MEM_EVENT_FLAG_DROP_PAGE    (1 << 1)
-#define MEM_EVENT_FLAG_EVICT_FAIL   (1 << 2)
-#define MEM_EVENT_FLAG_FOREIGN      (1 << 3)
-#define MEM_EVENT_FLAG_DUMMY        (1 << 4)
+#define MEM_EVENT_FLAG_VCPU_PAUSED     (1 << 0)
+#define MEM_EVENT_FLAG_DROP_PAGE       (1 << 1)
+#define MEM_EVENT_FLAG_EVICT_FAIL      (1 << 2)
+#define MEM_EVENT_FLAG_FOREIGN         (1 << 3)
+#define MEM_EVENT_FLAG_DUMMY           (1 << 4)
+#define MEM_EVENT_FLAG_EMULATE         (1 << 5)
+#define MEM_EVENT_FLAG_EMULATE_NOWRITE (1 << 6)
 
 /* Reasons for the memory event request */
 #define MEM_EVENT_REASON_UNKNOWN     0    /* typical reason */
Signature · 1 line
1.7.9.5
#16 lore raw

Re: [PATCH RFC V8 5/5] xen: Handle resumed instruction based on previous mem_event reply

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1702

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 27.08.14 at 16:01, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vmx.c +7 -0
+static bool_t vmx_exited_by_nested_pagefault(void)
+{
+    unsigned long exit_qualification;
+
+    __vmread(EXIT_QUALIFICATION, &exit_qualification);
+
+    if ( (exit_qualification & EPT_GLA_FAULT) == 0 )

It just occurred to me - Is this a valid check for any but EXIT_REASON_EPT_VIOLATION? I didn't think the bits here carry the same meaning regardless of exit kind...

Jan

#17 lore raw

Re: [PATCH RFC V8 5/5] xen: Handle resumed instruction based on previous mem_event reply

Quoted 4 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Wednesday, August 27, 2014 8:52 AM

Quoted 1 line
Quoted 1 line
Quoted 1 line · On 27.08.14 at 16:01, <[email protected]> wrote:
Quoted 12 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1702

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +7 -0
+static bool_t vmx_exited_by_nested_pagefault(void)
+{
+    unsigned long exit_qualification;
+
+    __vmread(EXIT_QUALIFICATION, &exit_qualification);
+
+    if ( (exit_qualification & EPT_GLA_FAULT) == 0 )

It just occurred to me - Is this a valid check for any but EXIT_REASON_EPT_VIOLATION? I didn't think the bits here carry the same meaning regardless of exit kind...

You're right. GLA bit definition is only for EPT violation.

Thanks
Kevin

#18 lore raw

Re: [PATCH RFC V8 5/5] xen: Handle resumed instruction based on previous mem_event reply

Quoted 17 lines · Kevin
Quoted 4 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Wednesday, August 27, 2014 8:52 AM

Quoted 1 line
Quoted 1 line
Quoted 1 line · On 27.08.14 at 16:01, <[email protected]> wrote:
Quoted 12 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1702

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +7 -0
+static bool_t vmx_exited_by_nested_pagefault(void)
+{
+    unsigned long exit_qualification;
+
+    __vmread(EXIT_QUALIFICATION, &exit_qualification);
+
+    if ( (exit_qualification & EPT_GLA_FAULT) == 0 )

It just occurred to me - Is this a valid check for any but EXIT_REASON_EPT_VIOLATION? I didn't think the bits here carry the same meaning regardless of exit kind...

Quoted 1 line

You're right. GLA bit definition is only for EPT violation.

I'll check the exit reason as well.

Thanks,
Razvan Cojocaru

Acked-by on v8 1/5
#19 lore raw
In reply to Razvan Cojocaru
Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 27.08.14 at 16:01, <[email protected]> wrote:

Added support for emulating an instruction with no memory writes. Additionally, introduced hvm_emulate_one_full(), which inspects possible return values from the hvm_emulate_one() functions (EXCEPTION, UNHANDLEABLE) and acts on them.

Changes since V1:
- Removed the Linux code that computes the length of an instruction.
- Unused function parameters are no longer marked.
- Refactored the code to eliminate redundancy.
- Made the exception passed on to the guest by hvm_emulate_one_full() configurable.

Changes since V2:
- Renamed hvmemul_write_dummy() to hvmemul_write_discard().
- Fixed a comment (Xen coding style rules).
- Renamed hvm_emulate_one_with_ops() to _hvm_emulate_one().
- Changed stack variable hvm_emulate_ops_no_write into a static const.
- Modified struct hvm_emulate_ctxt ctx initialization syntax.
- Renamed unhandleable_trapnr to trapnr and unhandleable_errcode to errcode.
- Changed errcode's type to unsigned int.
- Removed hvm_emulate_one() loop that went on until it returned something other than X86EMUL_RETRY (to prevent potential blocking against the time calibration rendezvous).

Changes since V3:
- The rep_ins, rep_movs and cmpxchg handlers are
now also inactive.

Changes since V4:
- Also discarding IO reads (dummy read_io handler).

Changes since V5:
- Added discard versions of write_io and rep_outs.

Changes since V6:
- Added discard versions of write_msr and wbinvd.
- Now calling hvm_emulate_writeback() for the X86EMUL_UNHANDLEABLE case as well.

Quoted 5 lines

Changes since V7:
- Renamed hvm_emulate_one_full() to hvm_mem_event_emulate_one().
- Added a comment explaining how X86EMUL_RETRY is being treated.

But btw., please get used to putting the version history after the first --- separator. And commonly such history is more useful if sorted in reverse chronological order.

Jan

#20 lore raw
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 27.08.14 at 16:01, <[email protected]> wrote:

Added support for emulating an instruction with no memory writes. Additionally, introduced hvm_emulate_one_full(), which inspects possible return values from the hvm_emulate_one() functions (EXCEPTION, UNHANDLEABLE) and acts on them.

Quoted 7 lines
Quoted 1 line

But btw., please get used to putting the version history after the first --- separator. And commonly such history is more useful if sorted in reverse chronological order.

Of course, sorry for missing that information in the patch publish guide. Thanks for the review and the Ack!

Razvan Cojocaru