x86/monitor: add get_capabilities to monitor_op domctl
authorTamas K Lengyel <tlengyel@novetta.com>
Fri, 10 Jul 2015 10:38:09 +0000 (12:38 +0200)
committerJan Beulich <jbeulich@suse.com>
Fri, 10 Jul 2015 10:38:09 +0000 (12:38 +0200)
Add option to monitor_op domctl to determine the monitor capabilities of the
system.

Signed-off-by: Tamas K Lengyel <tlengyel@novetta.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Razvan Cojocaru <rcojocaru@bitdefender.com>
tools/libxc/include/xenctrl.h
tools/libxc/xc_monitor.c
xen/arch/x86/hvm/vmx/vmx.c
xen/arch/x86/monitor.c
xen/common/domctl.c
xen/include/asm-x86/hvm/hvm.h
xen/include/public/domctl.h

index 5a098802ecda1f2ab0bea7b43bd3cc74ce134eab..0bbae2a9fab340c638f25d1302ec5297a3e32904 100644 (file)
@@ -2364,6 +2364,12 @@ int xc_mem_access_disable_emulate(xc_interface *xch, domid_t domain_id);
 void *xc_monitor_enable(xc_interface *xch, domid_t domain_id, uint32_t *port);
 int xc_monitor_disable(xc_interface *xch, domid_t domain_id);
 int xc_monitor_resume(xc_interface *xch, domid_t domain_id);
+/*
+ * Get a bitmap of supported monitor events in the form
+ * (1 << XEN_DOMCTL_MONITOR_EVENT_*).
+ */
+int xc_monitor_get_capabilities(xc_interface *xch, domid_t domain_id,
+                                uint32_t *capabilities);
 int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
                              uint16_t index, bool enable, bool sync,
                              bool onchangeonly);
index 63013deb89657629a61b9fd56dd8dd18bf14994d..b64bce3f57d94ec4c4340a56d3e5a82b51fb964a 100644 (file)
@@ -45,6 +45,30 @@ int xc_monitor_resume(xc_interface *xch, domid_t domain_id)
                                NULL);
 }
 
+int xc_monitor_get_capabilities(xc_interface *xch, domid_t domain_id,
+                                uint32_t *capabilities)
+{
+    int rc;
+    DECLARE_DOMCTL;
+
+    if ( !capabilities )
+    {
+        errno = EINVAL;
+        return -1;
+    }
+
+    domctl.cmd = XEN_DOMCTL_monitor_op;
+    domctl.domain = domain_id;
+    domctl.u.monitor_op.op = XEN_DOMCTL_MONITOR_OP_GET_CAPABILITIES;
+
+    rc = do_domctl(xch, &domctl);
+    if ( rc )
+        return rc;
+
+    *capabilities = domctl.u.monitor_op.event;
+    return 0;
+}
+
 int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
                              uint16_t index, bool enable, bool sync,
                              bool onchangeonly)
index e3a845878fd74a6b5ae49a2ebe163b92b866d528..bc3212fe3f6f8db2c436fcd7febe551d2f44984d 100644 (file)
@@ -1765,6 +1765,11 @@ static void vmx_enable_msr_exit_interception(struct domain *d)
                                          MSR_TYPE_W);
 }
 
+static bool_t vmx_is_singlestep_supported(void)
+{
+    return cpu_has_monitor_trap_flag;
+}
+
 static struct hvm_function_table __initdata vmx_function_table = {
     .name                 = "VMX",
     .cpu_up_prepare       = vmx_cpu_up_prepare,
@@ -1822,6 +1827,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,
+    .is_singlestep_supported = vmx_is_singlestep_supported,
 };
 
 const struct hvm_function_table * __init start_vmx(void)
index 896acf79c5eece2a19bd803ffbe1d410ad57e793..79c490aae6c21cad70b7aeb99c9ba37cf34633d5 100644 (file)
@@ -42,10 +42,29 @@ int status_check(struct xen_domctl_monitor_op *mop, bool_t status)
     return 0;
 }
 
+static inline uint32_t get_capabilities(struct domain *d)
+{
+    uint32_t capabilities = 0;
+
+    if ( !is_hvm_domain(d) || !cpu_has_vmx )
+        return capabilities;
+
+    capabilities = (1 << XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG) |
+                   (1 << XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR) |
+                   (1 << XEN_DOMCTL_MONITOR_EVENT_SOFTWARE_BREAKPOINT);
+
+    /* Since we know this is on VMX, we can just call the hvm func */
+    if ( hvm_is_singlestep_supported() )
+        capabilities |= (1 << XEN_DOMCTL_MONITOR_EVENT_SINGLESTEP);
+
+    return capabilities;
+}
+
 int monitor_domctl(struct domain *d, struct xen_domctl_monitor_op *mop)
 {
     int rc;
     struct arch_domain *ad = &d->arch;
+    uint32_t capabilities = get_capabilities(d);
 
     rc = xsm_vm_event_control(XSM_PRIV, d, mop->op, mop->event);
     if ( rc )
@@ -55,8 +74,12 @@ int monitor_domctl(struct domain *d, struct xen_domctl_monitor_op *mop)
      * At the moment only Intel HVM domains are supported. However, event
      * delivery could be extended to AMD and PV domains.
      */
-    if ( !is_hvm_domain(d) || !cpu_has_vmx )
-        return -EOPNOTSUPP;
+
+    if ( mop->op == XEN_DOMCTL_MONITOR_OP_GET_CAPABILITIES )
+    {
+        mop->event = capabilities;
+        return 0;
+    }
 
     /*
      * Sanity check
@@ -65,6 +88,10 @@ int monitor_domctl(struct domain *d, struct xen_domctl_monitor_op *mop)
          mop->op != XEN_DOMCTL_MONITOR_OP_DISABLE )
         return -EOPNOTSUPP;
 
+    /* Check if event type is available. */
+    if ( !(capabilities & (1 << mop->event)) )
+        return -EOPNOTSUPP;
+
     switch ( mop->event )
     {
     case XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG:
index a06f15c81ef4b4cf51b7854ee6d2d478273d039a..7f959f3bdf4f03a86cd1a7ac830f759d6c8138e7 100644 (file)
@@ -1167,6 +1167,8 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
             break;
 
         ret = monitor_domctl(d, &op->u.monitor_op);
+        if ( !ret )
+            copyback = 1;
         break;
 
     default:
index efb8e7d381645217c0434d69108a975576e61fb9..5181f73d1c7313224f1f0d8b3f08dac6ee3f3480 100644 (file)
@@ -202,6 +202,7 @@ struct hvm_function_table {
                                   uint32_t *ecx, uint32_t *edx);
 
     void (*enable_msr_exit_interception)(struct domain *d);
+    bool_t (*is_singlestep_supported)(void);
 };
 
 extern struct hvm_function_table hvm_funcs;
@@ -509,6 +510,11 @@ static inline enum hvm_intblk nhvm_interrupt_blocked(struct vcpu *v)
     return hvm_funcs.nhvm_intr_blocked(v);
 }
 
+static inline bool_t hvm_is_singlestep_supported(void)
+{
+    return (hvm_funcs.is_singlestep_supported &&
+            hvm_funcs.is_singlestep_supported());
+}
 
 #ifndef NDEBUG
 /* Permit use of the Forced Emulation Prefix in HVM guests */
index 212817146a74256d76cb543e990351f25a709103..8b1d6b46d111a7a56c531e0939a22527a5fa6c2a 100644 (file)
@@ -994,12 +994,16 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t);
  * via the ring buffer "MONITOR". The ring has to be first enabled
  * with the domctl XEN_DOMCTL_VM_EVENT_OP_MONITOR.
  *
+ * GET_CAPABILITIES can be used to determine which of these features is
+ * available on a given platform.
+ *
  * NOTICE: mem_access events are also delivered via the "MONITOR" ring buffer;
  * however, enabling/disabling those events is performed with the use of
  * memory_op hypercalls!
  */
-#define XEN_DOMCTL_MONITOR_OP_ENABLE   0
-#define XEN_DOMCTL_MONITOR_OP_DISABLE  1
+#define XEN_DOMCTL_MONITOR_OP_ENABLE            0
+#define XEN_DOMCTL_MONITOR_OP_DISABLE           1
+#define XEN_DOMCTL_MONITOR_OP_GET_CAPABILITIES  2
 
 #define XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG         0
 #define XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR            1
@@ -1008,7 +1012,15 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t);
 
 struct xen_domctl_monitor_op {
     uint32_t op; /* XEN_DOMCTL_MONITOR_OP_* */
-    uint32_t event; /* XEN_DOMCTL_MONITOR_EVENT_* */
+
+    /*
+     * When used with ENABLE/DISABLE this has to be set to
+     * the requested XEN_DOMCTL_MONITOR_EVENT_* value.
+     * With GET_CAPABILITIES this field returns a bitmap of
+     * events supported by the platform, in the format
+     * (1 << XEN_DOMCTL_MONITOR_EVENT_*).
+     */
+    uint32_t event;
 
     /*
      * Further options when issuing XEN_DOMCTL_MONITOR_OP_ENABLE.