Skip to contentSkip to conversation

x86/emulate: Move hvmemul_linear_to_phys

RFC PATCH Superseded Alexandru Stefan ISAILA 6 participants 15 messages Jan 11, 2019 – Feb 6, 2019

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/2 x86/emulate: Move hvmemul_linear_to_phys

This is done so hvmemul_linear_to_phys() can be called from hvmemul_map_linear_addr()

---
 xen/arch/x86/hvm/emulate.c | 181 ++++++++++++++++++-------------------
 1 file changed, 90 insertions(+), 91 deletions(-)
xen/arch/x86/hvm/emulate.c +90 -91
index 2d02ef1521..a766eecc8e 100644
@@ -530,6 +530,95 @@ static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,
     return hvmemul_do_io_addr(1, mmio_gpa, reps, size, dir, df, ram_gpa);
 }
 
+/*
+ * Convert addr from linear to physical form, valid over the range
+ * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
+ * the valid computed range. It is always >0 when X86EMUL_OKAY is returned.
+ * @pfec indicates the access checks to be performed during page-table walks.
+ */
+static int hvmemul_linear_to_phys(
+    unsigned long addr,
+    paddr_t *paddr,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    uint32_t pfec,
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct vcpu *curr = current;
+    unsigned long pfn, npfn, done, todo, i, offset = addr & ~PAGE_MASK;
+    int reverse;
+
+    /*
+     * Clip repetitions to a sensible maximum. This avoids extensive looping in
+     * this function while still amortising the cost of I/O trap-and-emulate.
+     */
+    *reps = min_t(unsigned long, *reps, 4096);
+
+    /* With no paging it's easy: linear == physical. */
+    if ( !(curr->arch.hvm.guest_cr[0] & X86_CR0_PG) )
+    {
+        *paddr = addr;
+        return X86EMUL_OKAY;
+    }
+
+    /* Reverse mode if this is a backwards multi-iteration string operation. */
+    reverse = (hvmemul_ctxt->ctxt.regs->eflags & X86_EFLAGS_DF) && (*reps > 1);
+
+    if ( reverse && ((PAGE_SIZE - offset) < bytes_per_rep) )
+    {
+        /* Do page-straddling first iteration forwards via recursion. */
+        paddr_t _paddr;
+        unsigned long one_rep = 1;
+        int rc = hvmemul_linear_to_phys(
+            addr, &_paddr, bytes_per_rep, &one_rep, pfec, hvmemul_ctxt);
+        if ( rc != X86EMUL_OKAY )
+            return rc;
+        pfn = _paddr >> PAGE_SHIFT;
+    }
+    else if ( (pfn = paging_gva_to_gfn(curr, addr, &pfec)) == gfn_x(INVALID_GFN) )
+    {
+        if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
+            return X86EMUL_RETRY;
+        *reps = 0;
+        x86_emul_pagefault(pfec, addr, &hvmemul_ctxt->ctxt);
+        return X86EMUL_EXCEPTION;
+    }
+
+    done = reverse ? bytes_per_rep + offset : PAGE_SIZE - offset;
+    todo = *reps * bytes_per_rep;
+    for ( i = 1; done < todo; i++ )
+    {
+        /* Get the next PFN in the range. */
+        addr += reverse ? -PAGE_SIZE : PAGE_SIZE;
+        npfn = paging_gva_to_gfn(curr, addr, &pfec);
+
+        /* Is it contiguous with the preceding PFNs? If not then we're done. */
+        if ( (npfn == gfn_x(INVALID_GFN)) ||
+             (npfn != (pfn + (reverse ? -i : i))) )
+        {
+            if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
+                return X86EMUL_RETRY;
+            done /= bytes_per_rep;
+            if ( done == 0 )
+            {
+                ASSERT(!reverse);
+                if ( npfn != gfn_x(INVALID_GFN) )
+                    return X86EMUL_UNHANDLEABLE;
+                *reps = 0;
+                x86_emul_pagefault(pfec, addr & PAGE_MASK, &hvmemul_ctxt->ctxt);
+                return X86EMUL_EXCEPTION;
+            }
+            *reps = done;
+            break;
+        }
+
+        done += PAGE_SIZE;
+    }
+
+    *paddr = ((paddr_t)pfn << PAGE_SHIFT) | offset;
+    return X86EMUL_OKAY;
+}
+
 /*
  * Map the frame(s) covering an individual linear access, for writeable
  * access.  May return NULL for MMIO, or ERR_PTR(~X86EMUL_*) for other errors
@@ -692,97 +781,7 @@ static void hvmemul_unmap_linear_addr(
         *mfn++ = _mfn(0);
     }
 #endif
-}
-
-/*
- * Convert addr from linear to physical form, valid over the range
- * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
- * the valid computed range. It is always >0 when X86EMUL_OKAY is returned.
- * @pfec indicates the access checks to be performed during page-table walks.
- */
-static int hvmemul_linear_to_phys(
-    unsigned long addr,
-    paddr_t *paddr,
-    unsigned int bytes_per_rep,
-    unsigned long *reps,
-    uint32_t pfec,
-    struct hvm_emulate_ctxt *hvmemul_ctxt)
-{
-    struct vcpu *curr = current;
-    unsigned long pfn, npfn, done, todo, i, offset = addr & ~PAGE_MASK;
-    int reverse;
-
-    /*
-     * Clip repetitions to a sensible maximum. This avoids extensive looping in
-     * this function while still amortising the cost of I/O trap-and-emulate.
-     */
-    *reps = min_t(unsigned long, *reps, 4096);
-
-    /* With no paging it's easy: linear == physical. */
-    if ( !(curr->arch.hvm.guest_cr[0] & X86_CR0_PG) )
-    {
-        *paddr = addr;
-        return X86EMUL_OKAY;
-    }
-
-    /* Reverse mode if this is a backwards multi-iteration string operation. */
-    reverse = (hvmemul_ctxt->ctxt.regs->eflags & X86_EFLAGS_DF) && (*reps > 1);
-
-    if ( reverse && ((PAGE_SIZE - offset) < bytes_per_rep) )
-    {
-        /* Do page-straddling first iteration forwards via recursion. */
-        paddr_t _paddr;
-        unsigned long one_rep = 1;
-        int rc = hvmemul_linear_to_phys(
-            addr, &_paddr, bytes_per_rep, &one_rep, pfec, hvmemul_ctxt);
-        if ( rc != X86EMUL_OKAY )
-            return rc;
-        pfn = _paddr >> PAGE_SHIFT;
-    }
-    else if ( (pfn = paging_gva_to_gfn(curr, addr, &pfec)) == gfn_x(INVALID_GFN) )
-    {
-        if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
-            return X86EMUL_RETRY;
-        *reps = 0;
-        x86_emul_pagefault(pfec, addr, &hvmemul_ctxt->ctxt);
-        return X86EMUL_EXCEPTION;
-    }
-
-    done = reverse ? bytes_per_rep + offset : PAGE_SIZE - offset;
-    todo = *reps * bytes_per_rep;
-    for ( i = 1; done < todo; i++ )
-    {
-        /* Get the next PFN in the range. */
-        addr += reverse ? -PAGE_SIZE : PAGE_SIZE;
-        npfn = paging_gva_to_gfn(curr, addr, &pfec);
-
-        /* Is it contiguous with the preceding PFNs? If not then we're done. */
-        if ( (npfn == gfn_x(INVALID_GFN)) ||
-             (npfn != (pfn + (reverse ? -i : i))) )
-        {
-            if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
-                return X86EMUL_RETRY;
-            done /= bytes_per_rep;
-            if ( done == 0 )
-            {
-                ASSERT(!reverse);
-                if ( npfn != gfn_x(INVALID_GFN) )
-                    return X86EMUL_UNHANDLEABLE;
-                *reps = 0;
-                x86_emul_pagefault(pfec, addr & PAGE_MASK, &hvmemul_ctxt->ctxt);
-                return X86EMUL_EXCEPTION;
-            }
-            *reps = done;
-            break;
-        }
-
-        done += PAGE_SIZE;
-    }
-
-    *paddr = ((paddr_t)pfn << PAGE_SHIFT) | offset;
-    return X86EMUL_OKAY;
-}
-    
+}  
 
 static int hvmemul_virtual_to_linear(
     enum x86_segment seg,
Signature · 7 lines
2.17.1


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

RFC PATCH v2 2/2 x86/emulate: Send vm_event from emulate

This patch aims to have mem access vm events sent from the emulator. This is useful in the case of page-walks that have to emulate instructions in access denied pages.

We use hvmemul_map_linear_addr() ro intercept r/w access and hvmemul_insn_fetch() to intercept exec access.

First we try to send a vm event and if the event is sent then emulation returns X86EMUL_ACCESS_EXCEPTION. If the event is not sent then the emulation goes on as expected.

---
Changes since V1:
- Add newlines in hvmemul_send_vm_event()
- Dropped p2m->get_entry() for p2m_get_mem_access()
- Use a simplified return for hvmemul_send_vm_event.

---
 xen/arch/x86/hvm/emulate.c             | 104 ++++++++++++++++++++++++-
 xen/arch/x86/hvm/vm_event.c            |   2 +-
 xen/arch/x86/mm/mem_access.c           |   3 +-
 xen/arch/x86/x86_emulate/x86_emulate.h |   1 +
 xen/include/asm-x86/hvm/emulate.h      |   4 +-
 5 files changed, 109 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/emulate.c +102 -2
index a766eecc8e..3a6bca32fe 100644
@@ -15,6 +15,7 @@
 #include <xen/paging.h>
 #include <xen/trace.h>
 #include <xen/vm_event.h>
+#include <xen/monitor.h>
 #include <asm/event.h>
 #include <asm/i387.h>
 #include <asm/xstate.h>
@@ -26,6 +27,7 @@
 #include <asm/hvm/support.h>
 #include <asm/hvm/svm/svm.h>
 #include <asm/vm_event.h>
+#include <asm/altp2m.h>
 
 static void hvmtrace_io_assist(const ioreq_t *p)
 {
@@ -530,6 +532,56 @@ static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,
     return hvmemul_do_io_addr(1, mmio_gpa, reps, size, dir, df, ram_gpa);
 }
 
+static bool hvmemul_send_vm_event(paddr_t gpa, unsigned long gla, gfn_t gfn,
+                                  uint32_t pfec, struct hvm_emulate_ctxt *ctxt)
+{
+    xenmem_access_t access;
+    vm_event_request_t req = {};
+
+    if ( !ctxt->send_event || !pfec )
+        return false;
+
+    if ( p2m_get_mem_access(current->domain, gfn, &access,
+                            altp2m_vcpu_idx(current)) != 0 )
+        return false;
+
+    switch ( access ) {
+    case XENMEM_access_x:
+    case XENMEM_access_rx:
+        if ( pfec & PFEC_write_access )
+            req.u.mem_access.flags = MEM_ACCESS_R | MEM_ACCESS_W;
+        break;
+
+    case XENMEM_access_w:
+    case XENMEM_access_rw:
+        if ( pfec & PFEC_insn_fetch )
+            req.u.mem_access.flags = MEM_ACCESS_X;
+        break;
+
+    case XENMEM_access_r:
+    case XENMEM_access_n:
+        if ( pfec & PFEC_write_access )
+            req.u.mem_access.flags |= MEM_ACCESS_R | MEM_ACCESS_W;
+        if ( pfec & PFEC_insn_fetch )
+            req.u.mem_access.flags |= MEM_ACCESS_X;
+        break;
+
+    default:
+        return false;
+    }
+
+    if ( !req.u.mem_access.flags )
+        return false; /* No violation. */
+
+    req.reason = VM_EVENT_REASON_MEM_ACCESS;
+    req.u.mem_access.gfn = gfn_x(gfn);
+    req.u.mem_access.flags |= MEM_ACCESS_FAULT_WITH_GLA | MEM_ACCESS_GLA_VALID;
+    req.u.mem_access.gla = gla;
+    req.u.mem_access.offset = gpa & ((1 << PAGE_SHIFT) - 1);
+
+    return monitor_traps(current, true, &req) >= 0;
+}
+
 /*
  * Convert addr from linear to physical form, valid over the range
  * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
@@ -636,6 +688,7 @@ static void *hvmemul_map_linear_addr(
     unsigned int nr_frames = ((linear + bytes - !!bytes) >> PAGE_SHIFT) -
         (linear >> PAGE_SHIFT) + 1;
     unsigned int i;
+    gfn_t gfn;
 
     /*
      * mfn points to the next free slot.  All used slots have a page reference
@@ -674,7 +727,7 @@ static void *hvmemul_map_linear_addr(
         ASSERT(mfn_x(*mfn) == 0);
 
         res = hvm_translate_get_page(curr, addr, true, pfec,
-                                     &pfinfo, &page, NULL, &p2mt);
+                                     &pfinfo, &page, &gfn, &p2mt);
 
         switch ( res )
         {
@@ -704,6 +757,23 @@ static void *hvmemul_map_linear_addr(
 
         if ( pfec & PFEC_write_access )
         {
+            unsigned long reps = 1;
+            struct hvm_emulate_ctxt old;
+            int rc = 0;
+            paddr_t gpa;
+
+            old = *hvmemul_ctxt;
+            rc = hvmemul_linear_to_phys(
+                 addr, &gpa, bytes, &reps, pfec, hvmemul_ctxt);
+            if ( rc == X86EMUL_EXCEPTION )
+                *hvmemul_ctxt = old;
+
+            if ( hvmemul_send_vm_event(gpa, addr, gfn, pfec, hvmemul_ctxt) )
+            {
+                err = ERR_PTR(~X86EMUL_ACCESS_EXCEPTION);
+                goto out;
+            }
+
             if ( p2m_is_discard_write(p2mt) )
             {
                 err = ERR_PTR(~X86EMUL_OKAY);
@@ -1224,7 +1294,36 @@ int hvmemul_insn_fetch(
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     /* Careful, as offset can wrap or truncate WRT insn_buf_eip. */
     uint8_t insn_off = offset - hvmemul_ctxt->insn_buf_eip;
+    paddr_t gpa;
+    uint32_t pfec = PFEC_page_present | PFEC_insn_fetch;
+    unsigned long addr, reps = 1;
+    int rc =0;
+    struct hvm_emulate_ctxt old;
+
+    rc = hvmemul_virtual_to_linear(
+        seg, offset, bytes, &reps, hvm_access_insn_fetch, hvmemul_ctxt, &addr);
+    if ( rc == X86EMUL_EXCEPTION )
+    {
+       x86_emul_reset_event(ctxt);
+        rc = X86EMUL_OKAY;
+    }
+
+    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
+        pfec |= PFEC_user_mode;
+
+    old = *hvmemul_ctxt;
+    rc = hvmemul_linear_to_phys(
+        addr, &gpa, bytes, &reps, pfec, hvmemul_ctxt);
+    if ( rc == X86EMUL_EXCEPTION )
+    {
+        *hvmemul_ctxt = old;
+        rc = X86EMUL_OKAY;
+    }
 
+    if ( gpa )
+        if ( hvmemul_send_vm_event(gpa, addr, gaddr_to_gfn(gpa),
+                                   pfec, hvmemul_ctxt) )
+            return X86EMUL_ACCESS_EXCEPTION;
     /*
      * Fall back if requested bytes are not in the prefetch cache.
      * But always perform the (fake) read when bytes == 0.
@@ -2492,12 +2591,13 @@ int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla)
 }
 
 void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr,
-    unsigned int errcode)
+    unsigned int errcode, bool send_event)
 {
     struct hvm_emulate_ctxt ctx = {{ 0 }};
     int rc;
 
     hvm_emulate_init_once(&ctx, NULL, guest_cpu_user_regs());
+    ctx.send_event = send_event;
 
     switch ( kind )
     {
xen/arch/x86/hvm/vm_event.c +1 -1
index 0df8ab40e6..bdc65da3ed 100644
@@ -87,7 +87,7 @@ void hvm_vm_event_do_resume(struct vcpu *v)
             kind = EMUL_KIND_SET_CONTEXT_INSN;
 
         hvm_emulate_one_vm_event(kind, TRAP_invalid_op,
-                                 X86_EVENT_NO_EC);
+                                 X86_EVENT_NO_EC, false);
 
         v->arch.vm_event->emulate_flags = 0;
     }
xen/arch/x86/mm/mem_access.c +2 -1
index 56c06a4fc6..536ad6367b 100644
@@ -214,7 +214,8 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gla,
          d->arch.monitor.inguest_pagefault_disabled &&
          npfec.kind != npfec_kind_with_gla ) /* don't send a mem_event */
     {
-        hvm_emulate_one_vm_event(EMUL_KIND_NORMAL, TRAP_invalid_op, X86_EVENT_NO_EC);
+        hvm_emulate_one_vm_event(EMUL_KIND_NORMAL, TRAP_invalid_op,
+                                 X86_EVENT_NO_EC, true);
 
         return true;
     }
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
index 55a9e0ed51..a9829913a4 100644
@@ -162,6 +162,7 @@ struct x86_emul_fpu_aux {
 #define X86EMUL_UNRECOGNIZED   X86EMUL_UNIMPLEMENTED
  /* (cmpxchg accessor): CMPXCHG failed. */
 #define X86EMUL_CMPXCHG_FAILED 7
+#define X86EMUL_ACCESS_EXCEPTION 8
 
 /* FPU sub-types which may be requested via ->get_fpu(). */
 enum x86_emulate_fpu_type {
xen/include/asm-x86/hvm/emulate.h +3 -1
index 26a01e83a4..721e175b04 100644
@@ -47,6 +47,7 @@ struct hvm_emulate_ctxt {
     uint32_t intr_shadow;
 
     bool_t set_context;
+    bool send_event;
 };
 
 enum emul_kind {
@@ -63,7 +64,8 @@ int hvm_emulate_one(
     struct hvm_emulate_ctxt *hvmemul_ctxt);
 void hvm_emulate_one_vm_event(enum emul_kind kind,
     unsigned int trapnr,
-    unsigned int errcode);
+    unsigned int errcode,
+    bool send_event);
 /* Must be called once to set up hvmemul state. */
 void hvm_emulate_init_once(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
Signature · 7 lines
2.17.1


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

Re: [PATCH RFC v2 2/2] x86/emulate: Send vm_event from emulate

Quoted 5 lines · Alexandru Stefan ISAILA

This patch aims to have mem access vm events sent from the emulator. This is useful in the case of page-walks that have to emulate instructions in access denied pages.

I'm a little confused about the scenario you mention here. You mark pages where the pagetables are non-readable/writable in EPT and you expect the emulated instruction would also violate access permissions of the guest pagetable itself?

Thanks,
Tamas

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

#3 lore raw

Re: [PATCH RFC v2 2/2] x86/emulate: Send vm_event from emulate

Quoted 8 lines · Tamas K Lengyel
Quoted 5 lines · Alexandru Stefan ISAILA

This patch aims to have mem access vm events sent from the emulator. This is useful in the case of page-walks that have to emulate instructions in access denied pages.

Quoted 4 lines

I'm a little confused about the scenario you mention here. You mark pages where the pagetables are non-readable/writable in EPT and you expect the emulated instruction would also violate access permissions of the guest pagetable itself?

Hello Tamas,

The scenario is this: the pagetables are read-only. At some point, a walk tries to write the accessed bit, or the dirty bit somewhere in that read-only memory, causing an EPT fault, so we end up in p2m_mem_access_check().

Understandably, we don't care about sending this event out to the introspection application (we could if we wanted to, which is why this behaviour is configurable, but I think it's safe to say that for most introspection use-cases this is something we don't care about, and hence a perfect opportunity for optimization).

Now, emulating the current instruction helps, and it works. But, what if that instruction would have tried to write to another protected page? Emulating it, as things stand now, means that we will lose _that_ event, and that's potentially a very important EPT event.

We've tried to attack this problem by only writing the A/D bits and almost came to a satisfactory solution but there's still some debate on whether it's architecturally correct or not - that approach needs more studying.

The alternative we've come up with is to instead, at least for the time being, attempt to send out vm_events from the emulator code only in this case: where we want to emulate the page walk without consulting the EPT, but want to consult it when actually emulating the current instruction.

I hope that made sense.

Thanks,
Razvan

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

#4 lore raw

Re: [PATCH RFC v2 2/2] x86/emulate: Send vm_event from emulate

Quoted 38 lines · Razvan Cojocaru
Quoted 8 lines · Tamas K Lengyel
Quoted 5 lines · Alexandru Stefan ISAILA

This patch aims to have mem access vm events sent from the emulator. This is useful in the case of page-walks that have to emulate instructions in access denied pages.

Quoted 4 lines

I'm a little confused about the scenario you mention here. You mark pages where the pagetables are non-readable/writable in EPT and you expect the emulated instruction would also violate access permissions of the guest pagetable itself?

Hello Tamas,

The scenario is this: the pagetables are read-only. At some point, a walk tries to write the accessed bit, or the dirty bit somewhere in that read-only memory, causing an EPT fault, so we end up in p2m_mem_access_check().

Understandably, we don't care about sending this event out to the introspection application (we could if we wanted to, which is why this behaviour is configurable, but I think it's safe to say that for most introspection use-cases this is something we don't care about, and hence a perfect opportunity for optimization).

Now, emulating the current instruction helps, and it works. But, what if that instruction would have tried to write to another protected page? Emulating it, as things stand now, means that we will lose _that_ event, and that's potentially a very important EPT event.

We've tried to attack this problem by only writing the A/D bits and almost came to a satisfactory solution but there's still some debate on whether it's architecturally correct or not - that approach needs more studying.

Quoted 6 lines

The alternative we've come up with is to instead, at least for the time being, attempt to send out vm_events from the emulator code only in this
case: where we want to emulate the page walk without consulting the EPT, but want to consult it when actually emulating the current instruction.

I hope that made sense.

I'm still confused :) In the pagetable walking case, didn't the instruction you are emulating just trip by writing to a page you want to allow it writing to (the A/D bits)? Or are you saying there is an unrelated trap happening with an execute-violation but you don't know what other write-protected page it would have tripped if it actually executed, and by emulating you effectively miss that write event? The latter makes sense, it's the pagetable walking case I have a hard time putting together.

Tamas

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

#5 lore raw

Re: [PATCH RFC v2 2/2] x86/emulate: Send vm_event from emulate

Quoted 47 lines · Tamas K Lengyel
Quoted 38 lines · Razvan Cojocaru
Quoted 8 lines · Tamas K Lengyel
Quoted 5 lines · Alexandru Stefan ISAILA

This patch aims to have mem access vm events sent from the emulator. This is useful in the case of page-walks that have to emulate instructions in access denied pages.

Quoted 4 lines

I'm a little confused about the scenario you mention here. You mark pages where the pagetables are non-readable/writable in EPT and you expect the emulated instruction would also violate access permissions of the guest pagetable itself?

Hello Tamas,

The scenario is this: the pagetables are read-only. At some point, a walk tries to write the accessed bit, or the dirty bit somewhere in that read-only memory, causing an EPT fault, so we end up in p2m_mem_access_check().

Understandably, we don't care about sending this event out to the introspection application (we could if we wanted to, which is why this behaviour is configurable, but I think it's safe to say that for most introspection use-cases this is something we don't care about, and hence a perfect opportunity for optimization).

Now, emulating the current instruction helps, and it works. But, what if that instruction would have tried to write to another protected page? Emulating it, as things stand now, means that we will lose _that_ event, and that's potentially a very important EPT event.

We've tried to attack this problem by only writing the A/D bits and almost came to a satisfactory solution but there's still some debate on whether it's architecturally correct or not - that approach needs more studying.

Quoted 6 lines

The alternative we've come up with is to instead, at least for the time being, attempt to send out vm_events from the emulator code only in this
case: where we want to emulate the page walk without consulting the EPT, but want to consult it when actually emulating the current instruction.

I hope that made sense.

Quoted 8 lines

I'm still confused :) In the pagetable walking case, didn't the instruction you are emulating just trip by writing to a page you want to allow it writing to (the A/D bits)? Or are you saying there is an unrelated trap happening with an execute-violation but you don't know what other write-protected page it would have tripped if it actually executed, and by emulating you effectively miss that write event? The latter makes sense, it's the pagetable walking case I have a hard time putting together.

OK, assume we have instruction I that accesses page X, which access in turn causes the A/D bits to be set in pagetable page Y (because it triggers a page walk). We want to allow the write to Y (setting whatever A/D bits), but if X is write-protected and I tries to write into it, we want a vm_event for that write.

With the current code, if we emulate I to write to Y, we lose a potential vm_event for the write to X. This doesn't happen often, but it does happen.

Thanks,
Razvan

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

#6 lore raw

Re: [PATCH RFC v2 2/2] x86/emulate: Send vm_event from emulate

Quoted 64 lines · Razvan Cojocaru
Quoted 47 lines · Tamas K Lengyel
Quoted 38 lines · Razvan Cojocaru
Quoted 8 lines · Tamas K Lengyel
Quoted 5 lines · Alexandru Stefan ISAILA

This patch aims to have mem access vm events sent from the emulator. This is useful in the case of page-walks that have to emulate instructions in access denied pages.

Quoted 4 lines

I'm a little confused about the scenario you mention here. You mark pages where the pagetables are non-readable/writable in EPT and you expect the emulated instruction would also violate access permissions of the guest pagetable itself?

Hello Tamas,

The scenario is this: the pagetables are read-only. At some point, a walk tries to write the accessed bit, or the dirty bit somewhere in that read-only memory, causing an EPT fault, so we end up in p2m_mem_access_check().

Understandably, we don't care about sending this event out to the introspection application (we could if we wanted to, which is why this behaviour is configurable, but I think it's safe to say that for most introspection use-cases this is something we don't care about, and hence a perfect opportunity for optimization).

Now, emulating the current instruction helps, and it works. But, what if that instruction would have tried to write to another protected page? Emulating it, as things stand now, means that we will lose _that_ event, and that's potentially a very important EPT event.

We've tried to attack this problem by only writing the A/D bits and almost came to a satisfactory solution but there's still some debate on whether it's architecturally correct or not - that approach needs more studying.

Quoted 6 lines

The alternative we've come up with is to instead, at least for the time being, attempt to send out vm_events from the emulator code only in this
case: where we want to emulate the page walk without consulting the EPT, but want to consult it when actually emulating the current instruction.

I hope that made sense.

Quoted 8 lines

I'm still confused :) In the pagetable walking case, didn't the instruction you are emulating just trip by writing to a page you want to allow it writing to (the A/D bits)? Or are you saying there is an unrelated trap happening with an execute-violation but you don't know what other write-protected page it would have tripped if it actually executed, and by emulating you effectively miss that write event? The latter makes sense, it's the pagetable walking case I have a hard time putting together.

OK, assume we have instruction I that accesses page X, which access in turn causes the A/D bits to be set in pagetable page Y (because it triggers a page walk). We want to allow the write to Y (setting whatever A/D bits), but if X is write-protected and I tries to write into it, we want a vm_event for that write.

Quoted 3 lines

With the current code, if we emulate I to write to Y, we lose a potential vm_event for the write to X. This doesn't happen often, but it does happen.

Ah, got it. Thanks, makes sense.

Tamas

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

#7 lore raw

Re: [PATCH RFC v2 2/2] x86/emulate: Send vm_event from emulate

In reply to Alexandru Stefan ISAILA
Quoted 51 lines · Alexandru Stefan ISAILA

This patch aims to have mem access vm events sent from the emulator. This is useful in the case of page-walks that have to emulate instructions in access denied pages.

We use hvmemul_map_linear_addr() ro intercept r/w access and hvmemul_insn_fetch() to intercept exec access.

First we try to send a vm event and if the event is sent then emulation returns X86EMUL_ACCESS_EXCEPTION. If the event is not sent then the emulation goes on as expected.

---
Changes since V1:
- Add newlines in hvmemul_send_vm_event()
- Dropped p2m->get_entry() for p2m_get_mem_access()
- Use a simplified return for hvmemul_send_vm_event.

---
 xen/arch/x86/hvm/emulate.c             | 104 ++++++++++++++++++++++++-
 xen/arch/x86/hvm/vm_event.c            |   2 +-
 xen/arch/x86/mm/mem_access.c           |   3 +-
 xen/arch/x86/x86_emulate/x86_emulate.h |   1 +
 xen/include/asm-x86/hvm/emulate.h      |   4 +-
 5 files changed, 109 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/emulate.c +3 -0
index a766eecc8e..3a6bca32fe 100644
@@ -15,6 +15,7 @@
 #include <xen/paging.h>
 #include <xen/trace.h>
 #include <xen/vm_event.h>
+#include <xen/monitor.h>
 #include <asm/event.h>
 #include <asm/i387.h>
 #include <asm/xstate.h>
@@ -26,6 +27,7 @@
 #include <asm/hvm/support.h>
 #include <asm/hvm/svm/svm.h>
 #include <asm/vm_event.h>
+#include <asm/altp2m.h>
 
 static void hvmtrace_io_assist(const ioreq_t *p)
 {
@@ -530,6 +532,56 @@ static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,
     return hvmemul_do_io_addr(1, mmio_gpa, reps, size, dir, df, ram_gpa);
 }
 
+static bool hvmemul_send_vm_event(paddr_t gpa, unsigned long gla, gfn_t gfn,

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 545

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+                                  uint32_t pfec, struct hvm_emulate_ctxt *ctxt)
+{
+    xenmem_access_t access;
+    vm_event_request_t req = {};
+
+    if ( !ctxt->send_event || !pfec )
+        return false;
+
+    if ( p2m_get_mem_access(current->domain, gfn, &access,
+                            altp2m_vcpu_idx(current)) != 0 )

You can join the ifs:

if ( !ctxt->send_event || !pfec ||

      p2m_get_mem_access(current->domain, gfn, &access,
                         altp2m_vcpu_idx(current)) )
    return false;

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 548

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +3 -0
+        return false;
+
+    switch ( access ) {

The braces should be on a newline.

Quoted 58 lines
xen/arch/x86/hvm/emulate.c +38 -1
+    case XENMEM_access_x:
+    case XENMEM_access_rx:
+        if ( pfec & PFEC_write_access )
+            req.u.mem_access.flags = MEM_ACCESS_R | MEM_ACCESS_W;
+        break;
+
+    case XENMEM_access_w:
+    case XENMEM_access_rw:
+        if ( pfec & PFEC_insn_fetch )
+            req.u.mem_access.flags = MEM_ACCESS_X;
+        break;
+
+    case XENMEM_access_r:
+    case XENMEM_access_n:
+        if ( pfec & PFEC_write_access )
+            req.u.mem_access.flags |= MEM_ACCESS_R | MEM_ACCESS_W;
+        if ( pfec & PFEC_insn_fetch )
+            req.u.mem_access.flags |= MEM_ACCESS_X;
+        break;
+
+    default:
+        return false;
+    }
+
+    if ( !req.u.mem_access.flags )
+        return false; /* No violation. */
+
+    req.reason = VM_EVENT_REASON_MEM_ACCESS;
+    req.u.mem_access.gfn = gfn_x(gfn);
+    req.u.mem_access.flags |= MEM_ACCESS_FAULT_WITH_GLA | MEM_ACCESS_GLA_VALID;
+    req.u.mem_access.gla = gla;
+    req.u.mem_access.offset = gpa & ((1 << PAGE_SHIFT) - 1);
+
+    return monitor_traps(current, true, &req) >= 0;
+}
+
 /*
  * Convert addr from linear to physical form, valid over the range
  * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
@@ -636,6 +688,7 @@ static void *hvmemul_map_linear_addr(
     unsigned int nr_frames = ((linear + bytes - !!bytes) >> PAGE_SHIFT) -
         (linear >> PAGE_SHIFT) + 1;
     unsigned int i;
+    gfn_t gfn;
 
     /*
      * mfn points to the next free slot.  All used slots have a page reference
@@ -674,7 +727,7 @@ static void *hvmemul_map_linear_addr(
         ASSERT(mfn_x(*mfn) == 0);
 
         res = hvm_translate_get_page(curr, addr, true, pfec,
-                                     &pfinfo, &page, NULL, &p2mt);
+                                     &pfinfo, &page, &gfn, &p2mt);
 
         switch ( res )
         {
@@ -704,6 +757,23 @@ static void *hvmemul_map_linear_addr(
 

xen/arch/x86/hvm/emulate.c static void *hvmemul_map_linear_addr( · line 767

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +8 -0
         if ( pfec & PFEC_write_access )
         {
+            unsigned long reps = 1;
+            struct hvm_emulate_ctxt old;
+            int rc = 0;
+            paddr_t gpa;
+
+            old = *hvmemul_ctxt;
+            rc = hvmemul_linear_to_phys(
+                 addr, &gpa, bytes, &reps, pfec, hvmemul_ctxt);

The indentation is weird here, it should be:

rc = hvmemul_linear_to_phys(addr, &gpa, bytes, &reps,

                            pfec, hvmemul_ctxt);
Quoted 13 lines
xen/arch/x86/hvm/emulate.c +9 -0
+            if ( rc == X86EMUL_EXCEPTION )
+                *hvmemul_ctxt = old;
+
+            if ( hvmemul_send_vm_event(gpa, addr, gfn, pfec, hvmemul_ctxt) )
+            {
+                err = ERR_PTR(~X86EMUL_ACCESS_EXCEPTION);
+                goto out;
+            }
+
             if ( p2m_is_discard_write(p2mt) )
             {
                 err = ERR_PTR(~X86EMUL_OKAY);

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 1300

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +4 -0
@@ -1224,7 +1294,36 @@ int hvmemul_insn_fetch(
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     /* Careful, as offset can wrap or truncate WRT insn_buf_eip. */
     uint8_t insn_off = offset - hvmemul_ctxt->insn_buf_eip;
+    paddr_t gpa;
+    uint32_t pfec = PFEC_page_present | PFEC_insn_fetch;
+    unsigned long addr, reps = 1;
+    int rc =0;

^ missing space

And I don't see the need to initialize rc to 0, since you are assigning it to the return value of hvmemul_virtual_to_linear just below.

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 1304

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +4 -0
+    struct hvm_emulate_ctxt old;
+
+    rc = hvmemul_virtual_to_linear(
+        seg, offset, bytes, &reps, hvm_access_insn_fetch, hvmemul_ctxt, &addr);

Weird indentation, see above.

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 1305

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    if ( rc == X86EMUL_EXCEPTION )

What about other errors returned from hvmemul_virtual_to_linear apart from X86EMUL_EXCEPTION? Are you sure you should continue execution here if an error different than EXCEPTION is returned?

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 1307

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +2 -0
+    {
+       x86_emul_reset_event(ctxt);

Indentation is wrong here, you are missing one space.

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 1316

Quoted 9 lines
xen/arch/x86/hvm/emulate.c +9 -0
+        rc = X86EMUL_OKAY;
+    }
+
+    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
+        pfec |= PFEC_user_mode;
+
+    old = *hvmemul_ctxt;
+    rc = hvmemul_linear_to_phys(
+        addr, &gpa, bytes, &reps, pfec, hvmemul_ctxt);

Same here regarding indentation of function parameters.

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 1326

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +9 -0
+    if ( rc == X86EMUL_EXCEPTION )
+    {
+        *hvmemul_ctxt = old;
+        rc = X86EMUL_OKAY;
+    }
 
+    if ( gpa )
+        if ( hvmemul_send_vm_event(gpa, addr, gaddr_to_gfn(gpa),
+                                   pfec, hvmemul_ctxt) )
+            return X86EMUL_ACCESS_EXCEPTION;

You can join the ifs:

if ( gpa && hvmemul_send_vm_event(...) )

    return X86EMUL_ACCESS_EXCEPTION;

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 1329

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +0 -0
     /*
      * Fall back if requested bytes are not in the prefetch cache.
      * But always perform the (fake) read when bytes == 0.

There's an inner rc variable declared below which you can now remove.

Quoted 59 lines
xen/arch/x86/hvm/emulate.c +2 -1
@@ -2492,12 +2591,13 @@ int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla)
 }
 
 void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr,
-    unsigned int errcode)
+    unsigned int errcode, bool send_event)
 {
     struct hvm_emulate_ctxt ctx = {{ 0 }};
     int rc;
 
     hvm_emulate_init_once(&ctx, NULL, guest_cpu_user_regs());
+    ctx.send_event = send_event;
 
     switch ( kind )
     {
xen/arch/x86/hvm/vm_event.c +1 -1
index 0df8ab40e6..bdc65da3ed 100644
@@ -87,7 +87,7 @@ void hvm_vm_event_do_resume(struct vcpu *v)
             kind = EMUL_KIND_SET_CONTEXT_INSN;
 
         hvm_emulate_one_vm_event(kind, TRAP_invalid_op,
-                                 X86_EVENT_NO_EC);
+                                 X86_EVENT_NO_EC, false);
 
         v->arch.vm_event->emulate_flags = 0;
     }
xen/arch/x86/mm/mem_access.c +2 -1
index 56c06a4fc6..536ad6367b 100644
@@ -214,7 +214,8 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gla,
          d->arch.monitor.inguest_pagefault_disabled &&
          npfec.kind != npfec_kind_with_gla ) /* don't send a mem_event */
     {
-        hvm_emulate_one_vm_event(EMUL_KIND_NORMAL, TRAP_invalid_op, X86_EVENT_NO_EC);
+        hvm_emulate_one_vm_event(EMUL_KIND_NORMAL, TRAP_invalid_op,
+                                 X86_EVENT_NO_EC, true);
 
         return true;
     }
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
index 55a9e0ed51..a9829913a4 100644
@@ -162,6 +162,7 @@ struct x86_emul_fpu_aux {
 #define X86EMUL_UNRECOGNIZED   X86EMUL_UNIMPLEMENTED
  /* (cmpxchg accessor): CMPXCHG failed. */
 #define X86EMUL_CMPXCHG_FAILED 7
+#define X86EMUL_ACCESS_EXCEPTION 8
 
 /* FPU sub-types which may be requested via ->get_fpu(). */
 enum x86_emulate_fpu_type {
xen/include/asm-x86/hvm/emulate.h +0 -0
index 26a01e83a4..721e175b04 100644

xen/include/asm-x86/hvm/emulate.h struct hvm_emulate_ctxt { · line 50

Quoted 4 lines
xen/include/asm-x86/hvm/emulate.h +1 -0
@@ -47,6 +47,7 @@ struct hvm_emulate_ctxt {
     uint32_t intr_shadow;
 
     bool_t set_context;
+    bool send_event;

I'm not sure I see why a send_event field needs to be added to the ctxt struct, isn't is possible to pass this parameter to the relevant functions that care about it?

Thanks, Roger.

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

#8 lore raw

Re: [PATCH RFC v2 2/2] x86/emulate: Send vm_event from emulate

Quoted 11 lines
Quoted 11 lines

/* FPU sub-types which may be requested via ->get_fpu(). */ enum x86_emulate_fpu_type {

xen/include/asm-x86/hvm/emulate.h +0 -0
index 26a01e83a4..721e175b04 100644
@@ -47,6 +47,7 @@ struct hvm_emulate_ctxt {
      uint32_t intr_shadow;
 
Quoted 7 lines

xen/include/asm-x86/hvm/emulate.h struct hvm_emulate_ctxt { · line 50

Quoted 2 lines
xen/include/asm-x86/hvm/emulate.h +1 -0
      bool_t set_context;
+    bool send_event;

I'm not sure I see why a send_event field needs to be added to the ctxt struct, isn't is possible to pass this parameter to the relevant functions that care about it?

This was done to have as little code change as possible and it has a precedent with the set_context bool. If a send_event parameter would be used then it will have to be passed down the line and then a lot of function signatures have to change. I think the current way is the best way to have this done.

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

#9 lore raw
In reply to Alexandru Stefan ISAILA
Quoted 2 lines · Alexandru Stefan ISAILA

This is done so hvmemul_linear_to_phys() can be called from hvmemul_map_linear_addr()

This can also be done with a forward declaration to reduce code churn. But that's my personal taste anyway.

Assuming this is pure code movement, please add "No functional change" to the commit message.

Wei.

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

#10 lore raw
In reply to Alexandru Stefan ISAILA

Ping, any thoughts on this are appreciated.

Regards,
Alex

Quoted 198 lines · Alexandru Stefan ISAILA

This is done so hvmemul_linear_to_phys() can be called from hvmemul_map_linear_addr()

---
  xen/arch/x86/hvm/emulate.c | 181 ++++++++++++++++++-------------------
  1 file changed, 90 insertions(+), 91 deletions(-)
xen/arch/x86/hvm/emulate.c +89 -85
index 2d02ef1521..a766eecc8e 100644
@@ -530,6 +530,95 @@ static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,
      return hvmemul_do_io_addr(1, mmio_gpa, reps, size, dir, df, ram_gpa);
  }
 
+/*
+ * Convert addr from linear to physical form, valid over the range
+ * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
+ * the valid computed range. It is always >0 when X86EMUL_OKAY is returned.
+ * @pfec indicates the access checks to be performed during page-table walks.
+ */
+static int hvmemul_linear_to_phys(
+    unsigned long addr,
+    paddr_t *paddr,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    uint32_t pfec,
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct vcpu *curr = current;
+    unsigned long pfn, npfn, done, todo, i, offset = addr & ~PAGE_MASK;
+    int reverse;
+
+    /*
+     * Clip repetitions to a sensible maximum. This avoids extensive looping in
+     * this function while still amortising the cost of I/O trap-and-emulate.
+     */
+    *reps = min_t(unsigned long, *reps, 4096);
+
+    /* With no paging it's easy: linear == physical. */
+    if ( !(curr->arch.hvm.guest_cr[0] & X86_CR0_PG) )
+    {
+        *paddr = addr;
+        return X86EMUL_OKAY;
+    }
+
+    /* Reverse mode if this is a backwards multi-iteration string operation. */
+    reverse = (hvmemul_ctxt->ctxt.regs->eflags & X86_EFLAGS_DF) && (*reps > 1);
+
+    if ( reverse && ((PAGE_SIZE - offset) < bytes_per_rep) )
+    {
+        /* Do page-straddling first iteration forwards via recursion. */
+        paddr_t _paddr;
+        unsigned long one_rep = 1;
+        int rc = hvmemul_linear_to_phys(
+            addr, &_paddr, bytes_per_rep, &one_rep, pfec, hvmemul_ctxt);
+        if ( rc != X86EMUL_OKAY )
+            return rc;
+        pfn = _paddr >> PAGE_SHIFT;
+    }
+    else if ( (pfn = paging_gva_to_gfn(curr, addr, &pfec)) == gfn_x(INVALID_GFN) )
+    {
+        if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
+            return X86EMUL_RETRY;
+        *reps = 0;
+        x86_emul_pagefault(pfec, addr, &hvmemul_ctxt->ctxt);
+        return X86EMUL_EXCEPTION;
+    }
+
+    done = reverse ? bytes_per_rep + offset : PAGE_SIZE - offset;
+    todo = *reps * bytes_per_rep;
+    for ( i = 1; done < todo; i++ )
+    {
+        /* Get the next PFN in the range. */
+        addr += reverse ? -PAGE_SIZE : PAGE_SIZE;
+        npfn = paging_gva_to_gfn(curr, addr, &pfec);
+
+        /* Is it contiguous with the preceding PFNs? If not then we're done. */
+        if ( (npfn == gfn_x(INVALID_GFN)) ||
+             (npfn != (pfn + (reverse ? -i : i))) )
+        {
+            if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
+                return X86EMUL_RETRY;
+            done /= bytes_per_rep;
+            if ( done == 0 )
+            {
+                ASSERT(!reverse);
+                if ( npfn != gfn_x(INVALID_GFN) )
+                    return X86EMUL_UNHANDLEABLE;
+                *reps = 0;
+                x86_emul_pagefault(pfec, addr & PAGE_MASK, &hvmemul_ctxt->ctxt);
+                return X86EMUL_EXCEPTION;
+            }
+            *reps = done;
+            break;
+        }
+
+        done += PAGE_SIZE;
+    }
+
+    *paddr = ((paddr_t)pfn << PAGE_SHIFT) | offset;
+    return X86EMUL_OKAY;
+}
+
  /*
   * Map the frame(s) covering an individual linear access, for writeable
   * access.  May return NULL for MMIO, or ERR_PTR(~X86EMUL_*) for other errors
@@ -692,97 +781,7 @@ static void hvmemul_unmap_linear_addr(
          *mfn++ = _mfn(0);
      }
  #endif
-}
-
-/*
- * Convert addr from linear to physical form, valid over the range
- * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
- * the valid computed range. It is always >0 when X86EMUL_OKAY is returned.
- * @pfec indicates the access checks to be performed during page-table walks.
- */
-static int hvmemul_linear_to_phys(
-    unsigned long addr,
-    paddr_t *paddr,
-    unsigned int bytes_per_rep,
-    unsigned long *reps,
-    uint32_t pfec,
-    struct hvm_emulate_ctxt *hvmemul_ctxt)
-{
-    struct vcpu *curr = current;
-    unsigned long pfn, npfn, done, todo, i, offset = addr & ~PAGE_MASK;
-    int reverse;
-
-    /*
-     * Clip repetitions to a sensible maximum. This avoids extensive looping in
-     * this function while still amortising the cost of I/O trap-and-emulate.
-     */
-    *reps = min_t(unsigned long, *reps, 4096);
-
-    /* With no paging it's easy: linear == physical. */
-    if ( !(curr->arch.hvm.guest_cr[0] & X86_CR0_PG) )
-    {
-        *paddr = addr;
-        return X86EMUL_OKAY;
-    }
-
-    /* Reverse mode if this is a backwards multi-iteration string operation. */
-    reverse = (hvmemul_ctxt->ctxt.regs->eflags & X86_EFLAGS_DF) && (*reps > 1);
-
-    if ( reverse && ((PAGE_SIZE - offset) < bytes_per_rep) )
-    {
-        /* Do page-straddling first iteration forwards via recursion. */
-        paddr_t _paddr;
-        unsigned long one_rep = 1;
-        int rc = hvmemul_linear_to_phys(
-            addr, &_paddr, bytes_per_rep, &one_rep, pfec, hvmemul_ctxt);
-        if ( rc != X86EMUL_OKAY )
-            return rc;
-        pfn = _paddr >> PAGE_SHIFT;
-    }
-    else if ( (pfn = paging_gva_to_gfn(curr, addr, &pfec)) == gfn_x(INVALID_GFN) )
-    {
-        if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
-            return X86EMUL_RETRY;
-        *reps = 0;
-        x86_emul_pagefault(pfec, addr, &hvmemul_ctxt->ctxt);
-        return X86EMUL_EXCEPTION;
-    }
-
-    done = reverse ? bytes_per_rep + offset : PAGE_SIZE - offset;
-    todo = *reps * bytes_per_rep;
-    for ( i = 1; done < todo; i++ )
-    {
-        /* Get the next PFN in the range. */
-        addr += reverse ? -PAGE_SIZE : PAGE_SIZE;
-        npfn = paging_gva_to_gfn(curr, addr, &pfec);
-
-        /* Is it contiguous with the preceding PFNs? If not then we're done. */
-        if ( (npfn == gfn_x(INVALID_GFN)) ||
-             (npfn != (pfn + (reverse ? -i : i))) )
-        {
-            if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
-                return X86EMUL_RETRY;
-            done /= bytes_per_rep;
-            if ( done == 0 )
-            {
-                ASSERT(!reverse);
-                if ( npfn != gfn_x(INVALID_GFN) )
-                    return X86EMUL_UNHANDLEABLE;
-                *reps = 0;
-                x86_emul_pagefault(pfec, addr & PAGE_MASK, &hvmemul_ctxt->ctxt);
-                return X86EMUL_EXCEPTION;
-            }
-            *reps = done;
-            break;
-        }
-
-        done += PAGE_SIZE;

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr( · line 787

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +1 -6
-    }
-
-    *paddr = ((paddr_t)pfn << PAGE_SHIFT) | offset;
-    return X86EMUL_OKAY;
-}
-
+}
 
  static int hvmemul_virtual_to_linear(
      enum x86_segment seg,

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

#11 lore raw
Quoted 10 lines

-----Original Message-----
From: Alexandru Stefan ISAILA [mailto:[email protected]]
Sent: 04 February 2019 11:37
To: [email protected]
Cc: Paul Durrant <[email protected]>; [email protected]; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monne <[email protected]>; [email protected]; [email protected]; George Dunlap <[email protected]>
Subject: Re: [PATCH RFC v2 1/2] x86/emulate: Move hvmemul_linear_to_phys

Quoted 9 lines

Ping, any thoughts on this are appreciated.

Regards,
Alex

Quoted 3 lines · Alexandru Stefan ISAILA

This is done so hvmemul_linear_to_phys() can be called from hvmemul_map_linear_addr()

I have no objection to pure code movement for this purpose. I certainly prefer it to forward declaration of statics.

Paul

Quoted 227 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 530

Quoted 11 lines
---
  xen/arch/x86/hvm/emulate.c | 181 ++++++++++++++++++-------------------
  1 file changed, 90 insertions(+), 91 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -0
index 2d02ef1521..a766eecc8e 100644
@@ -530,6 +530,95 @@ static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,
      return hvmemul_do_io_addr(1, mmio_gpa, reps, size, dir, df,

ram_gpa);

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +4 -0
  }
 
+/*
+ * Convert addr from linear to physical form, valid over the range
+ * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
+ * the valid computed range. It is always >0 when X86EMUL_OKAY is

returned.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 537

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+ * @pfec indicates the access checks to be performed during page-table

walks.

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+ */
+static int hvmemul_linear_to_phys(
+    unsigned long addr,
+    paddr_t *paddr,
+    unsigned int bytes_per_rep,

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+    unsigned long *reps,
+    uint32_t pfec,
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct vcpu *curr = current;
+    unsigned long pfn, npfn, done, todo, i, offset = addr & ~PAGE_MASK;
+    int reverse;
+
+    /*
+     * Clip repetitions to a sensible maximum. This avoids extensive

looping in

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 553

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+     * this function while still amortising the cost of I/O trap-and-

emulate.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,

Quoted 11 lines
xen/arch/x86/hvm/emulate.c +11 -0
+     */
+    *reps = min_t(unsigned long, *reps, 4096);
+
+    /* With no paging it's easy: linear == physical. */
+    if ( !(curr->arch.hvm.guest_cr[0] & X86_CR0_PG) )
+    {
+        *paddr = addr;
+        return X86EMUL_OKAY;
+    }
+
+    /* Reverse mode if this is a backwards multi-iteration string

operation. */

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 565

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    reverse = (hvmemul_ctxt->ctxt.regs->eflags & X86_EFLAGS_DF) &&

(*reps > 1);

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +4 -0
+
+    if ( reverse && ((PAGE_SIZE - offset) < bytes_per_rep) )
+    {
+        /* Do page-straddling first iteration forwards via recursion.

*/

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +4 -0
+        paddr_t _paddr;
+        unsigned long one_rep = 1;
+        int rc = hvmemul_linear_to_phys(
+            addr, &_paddr, bytes_per_rep, &one_rep, pfec,

hvmemul_ctxt);

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+        if ( rc != X86EMUL_OKAY )
+            return rc;
+        pfn = _paddr >> PAGE_SHIFT;
+    }
+    else if ( (pfn = paging_gva_to_gfn(curr, addr, &pfec)) ==

gfn_x(INVALID_GFN) )

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+    {
+        if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
+            return X86EMUL_RETRY;
+        *reps = 0;
+        x86_emul_pagefault(pfec, addr, &hvmemul_ctxt->ctxt);
+        return X86EMUL_EXCEPTION;
+    }

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+
+    done = reverse ? bytes_per_rep + offset : PAGE_SIZE - offset;
+    todo = *reps * bytes_per_rep;
+    for ( i = 1; done < todo; i++ )
+    {
+        /* Get the next PFN in the range. */
+        addr += reverse ? -PAGE_SIZE : PAGE_SIZE;
+        npfn = paging_gva_to_gfn(curr, addr, &pfec);
+
+        /* Is it contiguous with the preceding PFNs? If not then we're

done. */

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,

Quoted 13 lines
xen/arch/x86/hvm/emulate.c +13 -0
+        if ( (npfn == gfn_x(INVALID_GFN)) ||
+             (npfn != (pfn + (reverse ? -i : i))) )
+        {
+            if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
+                return X86EMUL_RETRY;
+            done /= bytes_per_rep;
+            if ( done == 0 )
+            {
+                ASSERT(!reverse);
+                if ( npfn != gfn_x(INVALID_GFN) )
+                    return X86EMUL_UNHANDLEABLE;
+                *reps = 0;
+                x86_emul_pagefault(pfec, addr & PAGE_MASK,

&hvmemul_ctxt->ctxt);

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+                return X86EMUL_EXCEPTION;
+            }
+            *reps = done;
+            break;
+        }

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +8 -0
+
+        done += PAGE_SIZE;
+    }
+
+    *paddr = ((paddr_t)pfn << PAGE_SHIFT) | offset;
+    return X86EMUL_OKAY;
+}
+
  /*
   * Map the frame(s) covering an individual linear access, for

writeable

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 624

Quoted 1 line
xen/arch/x86/hvm/emulate.c +0 -0
   * access.  May return NULL for MMIO, or ERR_PTR(~X86EMUL_*) for other

errors

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr( · line 700

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +0 -6
@@ -692,97 +781,7 @@ static void hvmemul_unmap_linear_addr(
          *mfn++ = _mfn(0);
      }
  #endif
-}
-
-/*
- * Convert addr from linear to physical form, valid over the range
- * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
- * the valid computed range. It is always >0 when X86EMUL_OKAY is

returned.

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr( · line 701

Quoted 1 line
xen/arch/x86/hvm/emulate.c +0 -1
- * @pfec indicates the access checks to be performed during page-table

walks.

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +0 -5
- */
-static int hvmemul_linear_to_phys(
-    unsigned long addr,
-    paddr_t *paddr,
-    unsigned int bytes_per_rep,

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr(

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +0 -10
-    unsigned long *reps,
-    uint32_t pfec,
-    struct hvm_emulate_ctxt *hvmemul_ctxt)
-{
-    struct vcpu *curr = current;
-    unsigned long pfn, npfn, done, todo, i, offset = addr & ~PAGE_MASK;
-    int reverse;
-
-    /*
-     * Clip repetitions to a sensible maximum. This avoids extensive

looping in

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr( · line 717

Quoted 1 line
xen/arch/x86/hvm/emulate.c +0 -1
-     * this function while still amortising the cost of I/O trap-and-

emulate.

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr(

Quoted 11 lines
xen/arch/x86/hvm/emulate.c +0 -11
-     */
-    *reps = min_t(unsigned long, *reps, 4096);
-
-    /* With no paging it's easy: linear == physical. */
-    if ( !(curr->arch.hvm.guest_cr[0] & X86_CR0_PG) )
-    {
-        *paddr = addr;
-        return X86EMUL_OKAY;
-    }
-
-    /* Reverse mode if this is a backwards multi-iteration string

operation. */

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr( · line 729

Quoted 1 line
xen/arch/x86/hvm/emulate.c +0 -1
-    reverse = (hvmemul_ctxt->ctxt.regs->eflags & X86_EFLAGS_DF) &&

(*reps > 1);

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr(

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +0 -4
-
-    if ( reverse && ((PAGE_SIZE - offset) < bytes_per_rep) )
-    {
-        /* Do page-straddling first iteration forwards via recursion.

*/

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr(

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +0 -4
-        paddr_t _paddr;
-        unsigned long one_rep = 1;
-        int rc = hvmemul_linear_to_phys(
-            addr, &_paddr, bytes_per_rep, &one_rep, pfec,

hvmemul_ctxt);

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr(

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +0 -5
-        if ( rc != X86EMUL_OKAY )
-            return rc;
-        pfn = _paddr >> PAGE_SHIFT;
-    }
-    else if ( (pfn = paging_gva_to_gfn(curr, addr, &pfec)) ==

gfn_x(INVALID_GFN) )

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +0 -7
-    {
-        if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
-            return X86EMUL_RETRY;
-        *reps = 0;
-        x86_emul_pagefault(pfec, addr, &hvmemul_ctxt->ctxt);
-        return X86EMUL_EXCEPTION;
-    }

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr(

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +0 -10
-
-    done = reverse ? bytes_per_rep + offset : PAGE_SIZE - offset;
-    todo = *reps * bytes_per_rep;
-    for ( i = 1; done < todo; i++ )
-    {
-        /* Get the next PFN in the range. */
-        addr += reverse ? -PAGE_SIZE : PAGE_SIZE;
-        npfn = paging_gva_to_gfn(curr, addr, &pfec);
-
-        /* Is it contiguous with the preceding PFNs? If not then we're

done. */

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr(

Quoted 13 lines
xen/arch/x86/hvm/emulate.c +0 -13
-        if ( (npfn == gfn_x(INVALID_GFN)) ||
-             (npfn != (pfn + (reverse ? -i : i))) )
-        {
-            if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
-                return X86EMUL_RETRY;
-            done /= bytes_per_rep;
-            if ( done == 0 )
-            {
-                ASSERT(!reverse);
-                if ( npfn != gfn_x(INVALID_GFN) )
-                    return X86EMUL_UNHANDLEABLE;
-                *reps = 0;
-                x86_emul_pagefault(pfec, addr & PAGE_MASK,

&hvmemul_ctxt->ctxt);

Quoted 15 lines
xen/arch/x86/hvm/emulate.c +1 -13
-                return X86EMUL_EXCEPTION;
-            }
-            *reps = done;
-            break;
-        }
-
-        done += PAGE_SIZE;
-    }
-
-    *paddr = ((paddr_t)pfn << PAGE_SHIFT) | offset;
-    return X86EMUL_OKAY;
-}
-
+}
 
Quoted 3 lines

xen/arch/x86/hvm/emulate.c static void hvmemul_unmap_linear_addr( · line 787

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +0 -0
  static int hvmemul_virtual_to_linear(
      enum x86_segment seg,

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

#12 lore raw
Quoted 15 lines · Paul Durrant
Quoted 15 lines

-----Original Message-----
From: Alexandru Stefan ISAILA [mailto:[email protected]]
Sent: 04 February 2019 11:37
To: [email protected]
Cc: Paul Durrant <[email protected]>; [email protected]; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monne <[email protected]>; [email protected]; [email protected]; George Dunlap <[email protected]>
Subject: Re: [PATCH RFC v2 1/2] x86/emulate: Move hvmemul_linear_to_phys

Ping, any thoughts on this are appreciated.

Regards,
Alex

Quoted 6 lines
Quoted 4 lines · Alexandru Stefan ISAILA
Quoted 3 lines

This is done so hvmemul_linear_to_phys() can be called from hvmemul_map_linear_addr()

I have no objection to pure code movement for this purpose. I certainly prefer it to forward declaration of statics.

Thanks for the reply!

We're also after thoughts on the whole series (replying to this specific patch instead of the cover letter was an accident).

We expect the second patch to be more controversial and in some need of detailed scrutiny.

Thanks,
Razvan

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

#13 lore raw
Quoted 21 lines · Razvan Cojocaru
Quoted 20 lines · Paul Durrant
Quoted 19 lines

-----Original Message-----
From: Alexandru Stefan ISAILA [mailto:[email protected]]
Sent: 04 February 2019 11:37
To: [email protected]
Cc: Paul Durrant <[email protected]>; [email protected]; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monne <[email protected]>; [email protected]; [email protected]; George Dunlap <[email protected]>
Subject: Re: [PATCH RFC v2 1/2] x86/emulate: Move hvmemul_linear_to_phys

Ping, any thoughts on this are appreciated.

Regards,
Alex

Quoted 3 lines · Alexandru Stefan ISAILA

This is done so hvmemul_linear_to_phys() can be called from hvmemul_map_linear_addr()

Quoted 8 lines
Quoted 1 line

I have no objection to pure code movement for this purpose. I certainly prefer it to forward declaration of statics.

Thanks for the reply!

We're also after thoughts on the whole series (replying to this specific patch instead of the cover letter was an accident).

We expect the second patch to be more controversial and in some need of detailed scrutiny.

I've taken a look at patch 2 and provided some review comments, sorry for the delay. Keep in mind we are in the middle of the release process and most developers and reviewers are busy trying to fix bugs or reviewing bugfixes.

Thanks, Roger.

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

#14 lore raw
Quoted 28 lines · Roger Pau Monné
Quoted 27 lines · Razvan Cojocaru
Quoted 15 lines · Paul Durrant
Quoted 15 lines

-----Original Message-----
From: Alexandru Stefan ISAILA [mailto:[email protected]]
Sent: 04 February 2019 11:37
To: [email protected]
Cc: Paul Durrant <[email protected]>; [email protected]; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monne <[email protected]>; [email protected]; [email protected]; George Dunlap <[email protected]>
Subject: Re: [PATCH RFC v2 1/2] x86/emulate: Move hvmemul_linear_to_phys

Ping, any thoughts on this are appreciated.

Regards,
Alex

Quoted 6 lines
Quoted 4 lines · Alexandru Stefan ISAILA
Quoted 3 lines

This is done so hvmemul_linear_to_phys() can be called from hvmemul_map_linear_addr()

I have no objection to pure code movement for this purpose. I certainly prefer it to forward declaration of statics.

Thanks for the reply!

We're also after thoughts on the whole series (replying to this specific patch instead of the cover letter was an accident).

Quoted 7 lines
Quoted 2 lines

We expect the second patch to be more controversial and in some need of detailed scrutiny.

I've taken a look at patch 2 and provided some review comments, sorry for the delay. Keep in mind we are in the middle of the release process and most developers and reviewers are busy trying to fix bugs or reviewing bugfixes.

Very much appreciated!

Thanks,
Razvan

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