Skip to contentSkip to conversation

x86/hvm: Add debug exception vm_events

PATCH In review Tamas K Lengyel 3 participants 5 messages Apr 27, 2016 – Apr 29, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm: Add debug exception vm_events

Since in-guest debug exceptions are now unconditionally trapped to Xen, adding a hook for vm_event subscribers to tap into this new always-on guest event. We rename along the way hvm_event_breakpoint_type to hvm_event_type to better match the various events that can be passed with it. We also introduce the necessary monitor_op domctl's to enable subscribing to the events.

---
Cc: Razvan Cojocaru <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>

---
 tools/libxc/include/xenctrl.h       |  3 +-
 tools/libxc/xc_monitor.c            | 15 +++++++++
 tools/tests/xen-access/xen-access.c | 61 +++++++++++++++++++++++++++++++++----
 xen/arch/x86/hvm/event.c            | 26 ++++++++++++++--
 xen/arch/x86/hvm/vmx/vmx.c          | 24 ++++++++++++---
 xen/arch/x86/monitor.c              | 16 ++++++++++
 xen/include/asm-x86/domain.h        |  2 ++
 xen/include/asm-x86/hvm/event.h     |  7 +++--
 xen/include/asm-x86/vm_event.h      |  3 +-
 xen/include/public/domctl.h         |  6 ++++
 xen/include/public/vm_event.h       | 12 +++++++-
 11 files changed, 156 insertions(+), 19 deletions(-)
tools/libxc/include/xenctrl.h +2 -1
index f5a034a..8d70fed 100644
@@ -2190,7 +2190,8 @@ int xc_monitor_software_breakpoint(xc_interface *xch, domid_t domain_id,
                                    bool enable);
 int xc_monitor_guest_request(xc_interface *xch, domid_t domain_id,
                              bool enable, bool sync);
-
+int xc_monitor_debug_exceptions(xc_interface *xch, domid_t domain_id,
+                                bool enable, bool sync);
 /**
  * This function enables / disables emulation for each REP for a
  * REP-compatible instruction.
tools/libxc/xc_monitor.c +15 -0
index b1705dd..e33b40b 100644
@@ -156,3 +156,18 @@ int xc_monitor_emulate_each_rep(xc_interface *xch, domid_t domain_id,
 
     return do_domctl(xch, &domctl);
 }
+
+int xc_monitor_debug_exceptions(xc_interface *xch, domid_t domain_id,
+                                bool enable, bool sync)
+{
+    DECLARE_DOMCTL;
+
+    domctl.cmd = XEN_DOMCTL_monitor_op;
+    domctl.domain = domain_id;
+    domctl.u.monitor_op.op = enable ? XEN_DOMCTL_MONITOR_OP_ENABLE
+                                    : XEN_DOMCTL_MONITOR_OP_DISABLE;
+    domctl.u.monitor_op.event = XEN_DOMCTL_MONITOR_EVENT_DEBUG_EXCEPTION;
+    domctl.u.monitor_op.u.debug_exception.sync = sync;
+
+    return do_domctl(xch, &domctl);
+}
tools/tests/xen-access/xen-access.c +55 -6

141 lines not on this page · raw message

xen/arch/x86/hvm/event.c +23 -3
index 56c5514..0251d7d 100644
@@ -87,12 +87,14 @@ static inline unsigned long gfn_of_rip(unsigned long rip)
     return paging_gva_to_gfn(curr, sreg.base + rip, &pfec);
 }
 
-int hvm_event_breakpoint(unsigned long rip,
-                         enum hvm_event_breakpoint_type type)
+int hvm_monitor_event(unsigned long rip, enum hvm_event_type type,
+                      unsigned long trap_type, unsigned long insn_length)
 {
     struct vcpu *curr = current;
     struct arch_domain *ad = &curr->domain->arch;
     vm_event_request_t req = {};
+    bool_t sync;
+    int rc;
 
     switch ( type )
     {
@@ -101,6 +103,9 @@ int hvm_event_breakpoint(unsigned long rip,
             return 0;
         req.reason = VM_EVENT_REASON_SOFTWARE_BREAKPOINT;
         req.u.software_breakpoint.gfn = gfn_of_rip(rip);
+        req.u.software_breakpoint.type = trap_type;
+        req.u.software_breakpoint.insn_length = insn_length;
+        sync = 1;
         break;
 
     case HVM_EVENT_SINGLESTEP_BREAKPOINT:
@@ -108,6 +113,17 @@ int hvm_event_breakpoint(unsigned long rip,
             return 0;
         req.reason = VM_EVENT_REASON_SINGLESTEP;
         req.u.singlestep.gfn = gfn_of_rip(rip);
+        sync = 1;
+        break;
+
+    case HVM_EVENT_DEBUG_EXCEPTION:
+        if ( !ad->monitor.debug_exception_enabled )
+            return 0;
+        req.reason = VM_EVENT_REASON_DEBUG_EXCEPTION;
+        req.u.debug_exception.gfn = gfn_of_rip(rip);
+        req.u.debug_exception.type = trap_type;
+        req.u.debug_exception.insn_length = insn_length;
+        sync = !!ad->monitor.debug_exception_sync;
         break;
 
     default:
@@ -116,7 +132,11 @@ int hvm_event_breakpoint(unsigned long rip,
 
     req.vcpu_id = curr->vcpu_id;
 
-    return vm_event_monitor_traps(curr, 1, &req);
+    rc = vm_event_monitor_traps(curr, sync, &req);
+    if ( rc == 1 && type == HVM_EVENT_DEBUG_EXCEPTION && !sync )
+        rc = 0;
+
+    return rc;
 }
 
 /*
xen/arch/x86/hvm/vmx/vmx.c +20 -4
index bc4410f..38788ad 100644
@@ -3373,9 +3373,24 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
             HVMTRACE_1D(TRAP_DEBUG, exit_qualification);
             write_debugreg(6, exit_qualification | DR_STATUS_RESERVED_ONE);
             if ( !v->domain->debugger_attached )
-                vmx_propagate_intr(intr_info);
+            {
+                unsigned long insn_length = 0;
+                int handled;
+                unsigned long trap_type = MASK_EXTR(intr_info,
+                                                    INTR_INFO_INTR_TYPE_MASK);
+
+                if( trap_type >= X86_EVENTTYPE_SW_INTERRUPT )
+                    __vmread(VM_EXIT_INSTRUCTION_LEN, &insn_length);
+
+                handled = hvm_monitor_event(regs->eip, HVM_EVENT_DEBUG_EXCEPTION,
+                                            trap_type, insn_length);
+                if ( handled <= 0 )
+                    vmx_propagate_intr(intr_info);
+
+            }
             else
                 domain_pause_for_debugger();
+
             break;
         case TRAP_int3: 
         {
@@ -3389,8 +3404,9 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
             }
             else {
                 int handled =
-                        hvm_event_breakpoint(regs->eip,
-                                             HVM_EVENT_SOFTWARE_BREAKPOINT);
+                        hvm_monitor_event(regs->eip,
+                                          HVM_EVENT_SOFTWARE_BREAKPOINT,
+                                          X86_EVENTTYPE_SW_EXCEPTION, 1);
 
                 if ( handled < 0 ) 
                 {
@@ -3717,7 +3733,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
         vmx_update_cpu_exec_control(v);
         if ( v->arch.hvm_vcpu.single_step )
         {
-            hvm_event_breakpoint(regs->eip, HVM_EVENT_SINGLESTEP_BREAKPOINT);
+            hvm_monitor_event(regs->eip, HVM_EVENT_SINGLESTEP_BREAKPOINT, 0, 0);
             if ( v->domain->debugger_attached )
                 domain_pause_for_debugger();
         }
xen/arch/x86/monitor.c +16 -0
index 1fec412..36ccb11 100644
@@ -124,6 +124,22 @@ int arch_monitor_domctl_event(struct domain *d,
         break;
     }
 
+    case XEN_DOMCTL_MONITOR_EVENT_DEBUG_EXCEPTION:
+    {
+        bool_t old_status = ad->monitor.debug_exception_enabled;
+
+        if ( unlikely(old_status == requested_status) )
+            return -EEXIST;
+
+        domain_pause(d);
+        ad->monitor.debug_exception_enabled = requested_status;
+        ad->monitor.debug_exception_sync = requested_status ?
+                                            mop->u.debug_exception.sync :
+                                            0;
+        domain_unpause(d);
+        break;
+    }
+
     default:
         /*
          * Should not be reached unless vm_event_monitor_get_capabilities() is
xen/include/asm-x86/domain.h +2 -0
index 165e533..1cf97c3 100644
@@ -405,6 +405,8 @@ struct arch_domain
         unsigned int mov_to_msr_extended         : 1;
         unsigned int singlestep_enabled          : 1;
         unsigned int software_breakpoint_enabled : 1;
+        unsigned int debug_exception_enabled     : 1;
+        unsigned int debug_exception_sync        : 1;
     } monitor;
 
     /* Mem_access emulation control */
xen/include/asm-x86/hvm/event.h +4 -3
index 03f7fee..a8f5909 100644
@@ -23,10 +23,11 @@
 #include <xen/paging.h>
 #include <public/vm_event.h>
 
-enum hvm_event_breakpoint_type
+enum hvm_event_type
 {
     HVM_EVENT_SOFTWARE_BREAKPOINT,
     HVM_EVENT_SINGLESTEP_BREAKPOINT,
+    HVM_EVENT_DEBUG_EXCEPTION,
 };
 
 /*
@@ -39,8 +40,8 @@ bool_t hvm_event_cr(unsigned int index, unsigned long value,
 #define hvm_event_crX(cr, new, old) \
     hvm_event_cr(VM_EVENT_X86_##cr, new, old)
 void hvm_event_msr(unsigned int msr, uint64_t value);
-int hvm_event_breakpoint(unsigned long rip,
-                         enum hvm_event_breakpoint_type type);
+int hvm_monitor_event(unsigned long rip, enum hvm_event_type type,
+                      unsigned long trap_type, unsigned long insn_length);
 
 #endif /* __ASM_X86_HVM_EVENT_H__ */
 
xen/include/asm-x86/vm_event.h +2 -1
index 0470240..be6ac28 100644
@@ -58,7 +58,8 @@ static inline uint32_t vm_event_monitor_get_capabilities(struct domain *d)
     capabilities = (1U << XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG) |
                    (1U << XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR) |
                    (1U << XEN_DOMCTL_MONITOR_EVENT_SOFTWARE_BREAKPOINT) |
-                   (1U << XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST);
+                   (1U << XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST) |
+                   (1U << XEN_DOMCTL_MONITOR_EVENT_DEBUG_EXCEPTION);
 
     /* Since we know this is on VMX, we can just call the hvm func */
     if ( hvm_is_singlestep_supported() )
xen/include/public/domctl.h +6 -0
index 2457698..2050441 100644
@@ -1080,6 +1080,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t);
 #define XEN_DOMCTL_MONITOR_EVENT_SINGLESTEP            2
 #define XEN_DOMCTL_MONITOR_EVENT_SOFTWARE_BREAKPOINT   3
 #define XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST         4
+#define XEN_DOMCTL_MONITOR_EVENT_DEBUG_EXCEPTION       5
 
 struct xen_domctl_monitor_op {
     uint32_t op; /* XEN_DOMCTL_MONITOR_OP_* */
@@ -1115,6 +1116,11 @@ struct xen_domctl_monitor_op {
             /* Pause vCPU until response */
             uint8_t sync;
         } guest_request;
+
+        struct {
+            /* Pause vCPU until response */
+            uint8_t sync;
+        } debug_exception;
     } u;
 };
 typedef struct xen_domctl_monitor_op xen_domctl_monitor_op_t;
xen/include/public/vm_event.h +11 -1
index 9270d52..67387fc 100644
@@ -119,6 +119,8 @@
 #define VM_EVENT_REASON_SINGLESTEP              7
 /* An event has been requested via HVMOP_guest_request_vm_event. */
 #define VM_EVENT_REASON_GUEST_REQUEST           8
+/* A debug exception was caught */
+#define VM_EVENT_REASON_DEBUG_EXCEPTION         9
 
 /* Supported values for the vm_event_write_ctrlreg index. */
 #define VM_EVENT_X86_CR0    0
@@ -203,8 +205,15 @@ struct vm_event_write_ctrlreg {
     uint64_t old_value;
 };
 
+struct vm_event_singlestep {
+    uint64_t gfn;
+};
+
 struct vm_event_debug {
     uint64_t gfn;
+    uint8_t type;        /* HVMOP_TRAP_* */
+    uint8_t insn_length;
+    uint8_t _pad[6];
 };
 
 struct vm_event_mov_to_msr {
@@ -248,7 +257,8 @@ typedef struct vm_event_st {
         struct vm_event_write_ctrlreg         write_ctrlreg;
         struct vm_event_mov_to_msr            mov_to_msr;
         struct vm_event_debug                 software_breakpoint;
-        struct vm_event_debug                 singlestep;
+        struct vm_event_singlestep            singlestep;
+        struct vm_event_debug                 debug_exception;
     } u;
 
     union {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#1 lore raw
Quoted 5 lines · Tamas K Lengyel

Since in-guest debug exceptions are now unconditionally trapped to Xen, adding a hook for vm_event subscribers to tap into this new always-on guest event. We rename along the way hvm_event_breakpoint_type to hvm_event_type to better match the various events that can be passed with it. We also introduce the necessary monitor_op domctl's to enable subscribing to the events.

The monitor / event code looks fine, but I'm not sure about hvm_event_type / hvm_monitor_event(). Presumably even a CR3 write event coming from a HVM guest is an event, and HVM-related, hence a hvm_event
- i.e. the generic nature of the name is a bit confusing to me. Maybe not so to other reviewers?

Thanks,
Razvan

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

#2 lore raw

On Wed, Apr 27, 2016 at 11:57 PM, Razvan Cojocaru <[email protected]

Quoted 10 lines

wrote:

Quoted 1 line · Tamas K Lengyel

Since in-guest debug exceptions are now unconditionally trapped to Xen,

adding

Quoted 1 line

a hook for vm_event subscribers to tap into this new always-on guest

event. We

Quoted 1 line

rename along the way hvm_event_breakpoint_type to hvm_event_type to

better

Quoted 1 line

match the various events that can be passed with it. We also introduce

Quoted 9 lines

the

Quoted 1 line

necessary monitor_op domctl's to enable subscribing to the events.

The monitor / event code looks fine, but I'm not sure about hvm_event_type / hvm_monitor_event(). Presumably even a CR3 write event coming from a HVM guest is an event, and HVM-related, hence a hvm_event
- i.e. the generic nature of the name is a bit confusing to me. Maybe not so to other reviewers?

Yea, I'm not too happy with the naming in general for hvm/event, it all should be renamed to be clear that it's part of the monitor subsystem. I think I'll turn this into a 2-patch series, where I rename the existing stuff from hvm/event to hvm/monitor then change the name of this function from hvm_monitor_breakpoint to hvm_monitor_debug.

Tamas

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

#3 lore raw
In reply to Tamas K Lengyel
Quoted 6 lines

From: Tamas K Lengyel [mailto:[email protected]]
Sent: Thursday, April 28, 2016 4:33 AM

Since in-guest debug exceptions are now unconditionally trapped to Xen, adding a hook for vm_event subscribers to tap into this new always-on guest event. We rename along the way hvm_event_breakpoint_type to hvm_event_type to better

I don't quite understand above renaming. How do you define 'event' here? There are other functions like hvm_event_msr, hvm_event_cr, etc. The new name hvm_event_type looks need to cover all those paths too?

Quoted 4 lines

match the various events that can be passed with it. We also introduce the necessary monitor_op domctl's to enable subscribing to the events.

Thanks
Kevin

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

#4 lore raw
Quoted 5 lines · Kevin
Quoted 4 lines

From: Tamas K Lengyel [mailto:[email protected]]
Sent: Thursday, April 28, 2016 4:33 AM

Since in-guest debug exceptions are now unconditionally trapped to Xen,

adding

Quoted 9 lines
Quoted 1 line

a hook for vm_event subscribers to tap into this new always-on guest

event. We

Quoted 1 line

rename along the way hvm_event_breakpoint_type to hvm_event_type to

better

I don't quite understand above renaming. How do you define 'event' here? There are other functions like hvm_event_msr, hvm_event_cr, etc. The new name hvm_event_type looks need to cover all those paths too?

Thanks, already changed the name for v2 to be hvm_monitor_debug (with a precursor patch renaming hvm/event to hvm/monitor).

Tamas

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