Skip to contentSkip to conversation

write_ctrlreg event masking

PATCH Superseded Petre Pircalabu 5 participants 30 messages May 26, 2017 – Jun 21, 2017

lore
Share & tools
lore
#0 lore raw

Hello,

This patchset enables masking the reception of write_ctrlreg events depending on the value of certain bits in that register.
The most representative example is filtering out events when the CR4.PGE bit is being flipped (global TLB flushes)

Best regards,
Petre

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

#1 lore raw

PATCH v3 1/2 x86/monitor: add masking support for write_ctrlreg events

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

---
 tools/libxc/include/xenctrl.h | 2 +-
 tools/libxc/xc_monitor.c      | 3 ++-
 xen/arch/x86/hvm/monitor.c    | 3 ++-
 xen/arch/x86/monitor.c        | 6 ++++++
 xen/include/asm-x86/domain.h  | 1 +
 xen/include/public/domctl.h   | 6 +++++-
 xen/include/public/vm_event.h | 9 +++++++++
 7 files changed, 26 insertions(+), 4 deletions(-)
tools/libxc/include/xenctrl.h +1 -1
index 1629f41..8c26cb4 100644
@@ -1999,7 +1999,7 @@ 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);
+                             uint64_t bitmask, bool onchangeonly);
 /*
  * A list of MSR indices can usually be found in /usr/include/asm/msr-index.h.
  * Please consult the Intel/AMD manuals for more information on
tools/libxc/xc_monitor.c +2 -1
index f99b6e3..70648d7 100644
@@ -70,7 +70,7 @@ int xc_monitor_get_capabilities(xc_interface *xch, domid_t domain_id,
 
 int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
                              uint16_t index, bool enable, bool sync,
-                             bool onchangeonly)
+                             uint64_t bitmask, bool onchangeonly)
 {
     DECLARE_DOMCTL;
 
@@ -82,6 +82,7 @@ int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
     domctl.u.monitor_op.u.mov_to_cr.index = index;
     domctl.u.monitor_op.u.mov_to_cr.sync = sync;
     domctl.u.monitor_op.u.mov_to_cr.onchangeonly = onchangeonly;
+    domctl.u.monitor_op.u.mov_to_cr.bitmask = bitmask;
 
     return do_domctl(xch, &domctl);
 }
xen/arch/x86/hvm/monitor.c +2 -1
index bde5fd0..a7ccfc4 100644
@@ -38,7 +38,8 @@ bool_t hvm_monitor_cr(unsigned int index, unsigned long value, unsigned long old
 
     if ( (ad->monitor.write_ctrlreg_enabled & ctrlreg_bitmask) &&
          (!(ad->monitor.write_ctrlreg_onchangeonly & ctrlreg_bitmask) ||
-          value != old) )
+          value != old) &&
+         (!((value ^ old) & ad->monitor.write_ctrlreg_mask[index])) )
     {
         bool_t sync = !!(ad->monitor.write_ctrlreg_sync & ctrlreg_bitmask);
 
xen/arch/x86/monitor.c +6 -0
index 449c64c..d02d2ea 100644
@@ -155,9 +155,15 @@ int arch_monitor_domctl_event(struct domain *d,
             ad->monitor.write_ctrlreg_onchangeonly &= ~ctrlreg_bitmask;
 
         if ( requested_status )
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = mop->u.mov_to_cr.bitmask;
             ad->monitor.write_ctrlreg_enabled |= ctrlreg_bitmask;
+        }
         else
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = 0;
             ad->monitor.write_ctrlreg_enabled &= ~ctrlreg_bitmask;
+        }
 
         if ( VM_EVENT_X86_CR3 == mop->u.mov_to_cr.index )
         {
xen/include/asm-x86/domain.h +1 -0
index 924caac..27d80ee 100644
@@ -406,6 +406,7 @@ struct arch_domain
         unsigned int cpuid_enabled               : 1;
         unsigned int descriptor_access_enabled   : 1;
         struct monitor_msr_bitmap *msr_bitmap;
+        uint64_t write_ctrlreg_mask[4];
     } monitor;
 
     /* Mem_access emulation control */
xen/include/public/domctl.h +5 -1
index e6cf211..2bf5c5b 100644
@@ -37,7 +37,7 @@
 #include "hvm/save.h"
 #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000d
+#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000e
 
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
@@ -1107,6 +1107,10 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /* Send event only if the changed bit in the control register
+             * is not masked
+             */
+            unsigned long bitmask;
         } mov_to_cr;
 
         struct {
xen/include/public/vm_event.h +9 -0
index f01e471..8a77d4d 100644
@@ -155,6 +155,15 @@
 #define VM_EVENT_X86_CR4    2
 #define VM_EVENT_X86_XCR0   3
 
+/* vm_event_write_ctrlreg default bit mask
+ * If the changed bit in the control register is masked the event is
+ * not triggered
+ */
+#define VM_EVENT_X86_CR0_DEFAULT_MASK	        0x00000000
+#define VM_EVENT_X86_CR3_DEFAULT_MASK	        0x00000000
+#define VM_EVENT_X86_CR4_DEFAULT_MASK	        0x00000080
+#define VM_EVENT_X86_XCR0_DEFAULT_MASK	        0x00000000
+
 /*
  * Using custom vCPU structs (i.e. not hvm_hw_cpu) for both x86 and ARM
  * so as to not fill the vm_event ring buffer too quickly.
Signature · 7 lines
2.7.4


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

Re: [PATCH 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 26.05.17 at 15:41, <[email protected]> wrote:
xen/include/public/domctl.h +1 -1
@@ -37,7 +37,7 @@
 #include "hvm/save.h"
 #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000d
+#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000e
 
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.

xen/include/public/domctl.h struct xen_domctl_monitor_op { · line 1113

Quoted 7 lines
xen/include/public/domctl.h +4 -0
@@ -1107,6 +1107,10 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /* Send event only if the changed bit in the control register
+             * is not masked
+             */
+            unsigned long bitmask;

You can't use "unsigned long" in public headers. Also beware of alignment issues between 32-bit and 64-bit callers (I didn't check whether there actually is any, I'm merely hinting towards the use of uint64_aligned_t).

xen/include/public/vm_event.h line 161

Quoted 10 lines
xen/include/public/vm_event.h +4 -0
@@ -155,6 +155,15 @@
 #define VM_EVENT_X86_CR4    2
 #define VM_EVENT_X86_XCR0   3
 
+/* vm_event_write_ctrlreg default bit mask
+ * If the changed bit in the control register is masked the event is
+ * not triggered
+ */

Comment style.

xen/include/public/vm_event.h line 165

Quoted 4 lines
xen/include/public/vm_event.h +4 -0
+#define VM_EVENT_X86_CR0_DEFAULT_MASK	        0x00000000
+#define VM_EVENT_X86_CR3_DEFAULT_MASK	        0x00000000
+#define VM_EVENT_X86_CR4_DEFAULT_MASK	        0x00000080
+#define VM_EVENT_X86_XCR0_DEFAULT_MASK	        0x00000000

These are unused - what are they good for? And why would you want to special case CR4.PGE (and even without any comment)?

Jan

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

#3 lore raw

Re: [PATCH 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines · On 26.05.17 at 15:41, <[email protected]> wrote:
xen/include/public/domctl.h +1 -1
@@ -37,7 +37,7 @@
 #include "hvm/save.h"
 #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000d
+#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000e
 
Quoted 17 lines

xen/include/public/domctl.h struct xen_domctl_monitor_op { · line 1113

Quoted 12 lines
xen/include/public/domctl.h +0 -0
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this

operation.

xen/include/public/domctl.h +1 -0
@@ -1107,6 +1107,10 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /* Send event only if the changed bit in the control

register

xen/include/public/domctl.h +3 -0
+             * is not masked
+             */
+            unsigned long bitmask;

You can't use "unsigned long" in public headers. Also beware of alignment issues between 32-bit and 64-bit callers (I didn't check whether there actually is any, I'm merely hinting towards the use of uint64_aligned_t).

I will change it to uint64_aligned_t and resubmit the patch.

Quoted 23 lines
Quoted 9 lines
xen/include/public/vm_event.h +2 -0
@@ -155,6 +155,15 @@
 #define VM_EVENT_X86_CR4    2
 #define VM_EVENT_X86_XCR0   3
 
+/* vm_event_write_ctrlreg default bit mask
+ * If the changed bit in the control register is masked the event
Quoted 3 lines

is
+ * not triggered
+ */

Comment style.

xen/include/public/vm_event.h line 165

Quoted 5 lines
xen/include/public/vm_event.h +4 -0
+#define VM_EVENT_X86_CR0_DEFAULT_MASK        0x00000000
+#define VM_EVENT_X86_CR3_DEFAULT_MASK        0x00000000
+#define VM_EVENT_X86_CR4_DEFAULT_MASK        0x00000080
+#define VM_EVENT_X86_XCR0_DEFAULT_MASK        0x00000000

These are unused - what are they good for? And why would you want to special case CR4.PGE (and even without any comment)?

Quoted 2 lines

Jan

The defaults were added because when the introspection engine requests CR4 events it will get too many as CR4.PGE is used for global TLB flushes. However, every client can set-up it's own event masking policy so these macros are not needed globally.

Quoted 3 lines

________________________
This email was scanned by Bitdefender

Many thanks for your comments,
Petre

________________________
This email was scanned by Bitdefender
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw

PATCH v3 2/2 xen-access: write_ctrlreg_c4 test

In reply to Petre Pircalabu

Add test for write_ctrlreg event handling.

---
 tools/tests/xen-access/xen-access.c | 43 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)
tools/tests/xen-access/xen-access.c +42 -1
index 238011e..0a1873e 100644
@@ -314,6 +314,22 @@ static void get_request(vm_event_t *vm_event, vm_event_request_t *req)
 }
 
 /*
+ * X86 control register names
+ */
+static const char* get_x86_ctrl_reg_name(uint32_t index)
+{
+    static const char* names[] = {
+        "CR0",
+        "CR3",
+        "CR4",
+        "XCR0",
+    };
+
+    return (index > 3) ? "" : names[index];
+}
+
+
+/*
  * Note that this function is not thread safe.
  */
 static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp)
@@ -337,7 +353,7 @@ void usage(char* progname)
 {
     fprintf(stderr, "Usage: %s [-m] <domain_id> write|exec", progname);
 #if defined(__i386__) || defined(__x86_64__)
-            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access");
+            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access|write_ctrlreg_cr4");
 #elif defined(__arm__) || defined(__aarch64__)
             fprintf(stderr, "|privcall");
 #endif
@@ -369,6 +385,7 @@ int main(int argc, char *argv[])
     int debug = 0;
     int cpuid = 0;
     int desc_access = 0;
+    int write_ctrlreg_cr4 = 1;
     uint16_t altp2m_view_id = 0;
 
     char* progname = argv[0];
@@ -439,6 +456,10 @@ int main(int argc, char *argv[])
     {
         desc_access = 1;
     }
+    else if ( !strcmp(argv[0], "write_ctrlreg_cr4") )
+    {
+        write_ctrlreg_cr4 = 1;
+    }
 #elif defined(__arm__) || defined(__aarch64__)
     else if ( !strcmp(argv[0], "privcall") )
     {
@@ -596,6 +617,17 @@ int main(int argc, char *argv[])
         }
     }
 
+    if ( write_ctrlreg_cr4 )
+    {
+        rc = xc_monitor_write_ctrlreg(xch, domain_id, VM_EVENT_X86_CR4, 1, 1,
+                                      VM_EVENT_X86_CR4_DEFAULT_MASK, 1);
+        if ( rc < 0 )
+        {
+            ERROR("Error %d setting write control register trapping with vm_event\n", rc);
+            goto exit;
+        }
+    }
+
     /* Wait for access */
     for (;;)
     {
@@ -806,6 +838,15 @@ int main(int argc, char *argv[])
                        req.u.desc_access.is_write);
                 rsp.flags |= VM_EVENT_FLAG_EMULATE;
                 break;
+            case VM_EVENT_REASON_WRITE_CTRLREG:
+                printf("Control register written: rip=%016"PRIx64", vcpu %d: "
+                       "reg=%s, old_value=%016"PRIx64", new_value=%016"PRIx64"\n",
+                       req.data.regs.x86.rip,
+                       req.vcpu_id,
+                       get_x86_ctrl_reg_name(req.u.write_ctrlreg.index),
+                       req.u.write_ctrlreg.old_value,
+                       req.u.write_ctrlreg.new_value);
+                break;
             default:
                 fprintf(stderr, "UNKNOWN REASON CODE %d\n", req.reason);
             }
Signature · 7 lines
2.7.4


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

PATCH v2 1/2 x86/monitor: add masking support for write_ctrlreg events

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

---
 tools/libxc/include/xenctrl.h | 2 +-
 tools/libxc/xc_monitor.c      | 3 ++-
 xen/arch/x86/hvm/monitor.c    | 3 ++-
 xen/arch/x86/monitor.c        | 6 ++++++
 xen/include/asm-x86/domain.h  | 1 +
 xen/include/public/domctl.h   | 7 ++++++-
 6 files changed, 18 insertions(+), 4 deletions(-)
tools/libxc/include/xenctrl.h +1 -1
index 1629f41..8c26cb4 100644
@@ -1999,7 +1999,7 @@ 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);
+                             uint64_t bitmask, bool onchangeonly);
 /*
  * A list of MSR indices can usually be found in /usr/include/asm/msr-index.h.
  * Please consult the Intel/AMD manuals for more information on
tools/libxc/xc_monitor.c +2 -1
index f99b6e3..70648d7 100644
@@ -70,7 +70,7 @@ int xc_monitor_get_capabilities(xc_interface *xch, domid_t domain_id,
 
 int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
                              uint16_t index, bool enable, bool sync,
-                             bool onchangeonly)
+                             uint64_t bitmask, bool onchangeonly)
 {
     DECLARE_DOMCTL;
 
@@ -82,6 +82,7 @@ int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
     domctl.u.monitor_op.u.mov_to_cr.index = index;
     domctl.u.monitor_op.u.mov_to_cr.sync = sync;
     domctl.u.monitor_op.u.mov_to_cr.onchangeonly = onchangeonly;
+    domctl.u.monitor_op.u.mov_to_cr.bitmask = bitmask;
 
     return do_domctl(xch, &domctl);
 }
xen/arch/x86/hvm/monitor.c +2 -1
index bde5fd0..a7ccfc4 100644
@@ -38,7 +38,8 @@ bool_t hvm_monitor_cr(unsigned int index, unsigned long value, unsigned long old
 
     if ( (ad->monitor.write_ctrlreg_enabled & ctrlreg_bitmask) &&
          (!(ad->monitor.write_ctrlreg_onchangeonly & ctrlreg_bitmask) ||
-          value != old) )
+          value != old) &&
+         (!((value ^ old) & ad->monitor.write_ctrlreg_mask[index])) )
     {
         bool_t sync = !!(ad->monitor.write_ctrlreg_sync & ctrlreg_bitmask);
 
xen/arch/x86/monitor.c +6 -0
index 449c64c..d02d2ea 100644
@@ -155,9 +155,15 @@ int arch_monitor_domctl_event(struct domain *d,
             ad->monitor.write_ctrlreg_onchangeonly &= ~ctrlreg_bitmask;
 
         if ( requested_status )
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = mop->u.mov_to_cr.bitmask;
             ad->monitor.write_ctrlreg_enabled |= ctrlreg_bitmask;
+        }
         else
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = 0;
             ad->monitor.write_ctrlreg_enabled &= ~ctrlreg_bitmask;
+        }
 
         if ( VM_EVENT_X86_CR3 == mop->u.mov_to_cr.index )
         {
xen/include/asm-x86/domain.h +1 -0
index 924caac..27d80ee 100644
@@ -406,6 +406,7 @@ struct arch_domain
         unsigned int cpuid_enabled               : 1;
         unsigned int descriptor_access_enabled   : 1;
         struct monitor_msr_bitmap *msr_bitmap;
+        uint64_t write_ctrlreg_mask[4];
     } monitor;
 
     /* Mem_access emulation control */
xen/include/public/domctl.h +6 -1
index e6cf211..652fb17 100644
@@ -37,7 +37,7 @@
 #include "hvm/save.h"
 #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000d
+#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000e
 
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
@@ -1107,6 +1107,11 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /*
+             * Send event only if the changed bit in the control register
+             * is not masked.
+             */
+            uint64_aligned_t bitmask;
         } mov_to_cr;
 
         struct {
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v2 1/2
#7 lore raw

Re: [PATCH v2 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 7 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

Quoted 115 lines
---
 tools/libxc/include/xenctrl.h | 2 +-
 tools/libxc/xc_monitor.c      | 3 ++-
 xen/arch/x86/hvm/monitor.c    | 3 ++-
 xen/arch/x86/monitor.c        | 6 ++++++
 xen/include/asm-x86/domain.h  | 1 +
 xen/include/public/domctl.h   | 7 ++++++-
 6 files changed, 18 insertions(+), 4 deletions(-)
tools/libxc/include/xenctrl.h +1 -1
index 1629f41..8c26cb4 100644
@@ -1999,7 +1999,7 @@ 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);
+                             uint64_t bitmask, bool onchangeonly);
 /*
  * A list of MSR indices can usually be found in /usr/include/asm/msr-index.h.
  * Please consult the Intel/AMD manuals for more information on
tools/libxc/xc_monitor.c +2 -1
index f99b6e3..70648d7 100644
@@ -70,7 +70,7 @@ int xc_monitor_get_capabilities(xc_interface *xch, domid_t domain_id,
 
 int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
                              uint16_t index, bool enable, bool sync,
-                             bool onchangeonly)
+                             uint64_t bitmask, bool onchangeonly)
 {
     DECLARE_DOMCTL;
 
@@ -82,6 +82,7 @@ int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
     domctl.u.monitor_op.u.mov_to_cr.index = index;
     domctl.u.monitor_op.u.mov_to_cr.sync = sync;
     domctl.u.monitor_op.u.mov_to_cr.onchangeonly = onchangeonly;
+    domctl.u.monitor_op.u.mov_to_cr.bitmask = bitmask;
 
     return do_domctl(xch, &domctl);
 }
xen/arch/x86/hvm/monitor.c +2 -1
index bde5fd0..a7ccfc4 100644
@@ -38,7 +38,8 @@ bool_t hvm_monitor_cr(unsigned int index, unsigned long value, unsigned long old
 
     if ( (ad->monitor.write_ctrlreg_enabled & ctrlreg_bitmask) &&
          (!(ad->monitor.write_ctrlreg_onchangeonly & ctrlreg_bitmask) ||
-          value != old) )
+          value != old) &&
+         (!((value ^ old) & ad->monitor.write_ctrlreg_mask[index])) )
     {
         bool_t sync = !!(ad->monitor.write_ctrlreg_sync & ctrlreg_bitmask);
 
xen/arch/x86/monitor.c +6 -0
index 449c64c..d02d2ea 100644
@@ -155,9 +155,15 @@ int arch_monitor_domctl_event(struct domain *d,
             ad->monitor.write_ctrlreg_onchangeonly &= ~ctrlreg_bitmask;
 
         if ( requested_status )
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = mop->u.mov_to_cr.bitmask;
             ad->monitor.write_ctrlreg_enabled |= ctrlreg_bitmask;
+        }
         else
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = 0;
             ad->monitor.write_ctrlreg_enabled &= ~ctrlreg_bitmask;
+        }
 
         if ( VM_EVENT_X86_CR3 == mop->u.mov_to_cr.index )
         {
xen/include/asm-x86/domain.h +1 -0
index 924caac..27d80ee 100644
@@ -406,6 +406,7 @@ struct arch_domain
         unsigned int cpuid_enabled               : 1;
         unsigned int descriptor_access_enabled   : 1;
         struct monitor_msr_bitmap *msr_bitmap;
+        uint64_t write_ctrlreg_mask[4];
     } monitor;
 
     /* Mem_access emulation control */
xen/include/public/domctl.h +6 -2
index e6cf211..652fb17 100644
@@ -37,7 +37,7 @@
 #include "hvm/save.h"
 #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000d
+#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000e
 
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
@@ -1107,6 +1107,11 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /*
+             * Send event only if the changed bit in the control register
+             * is not masked.
+             */
+            uint64_aligned_t bitmask;
         } mov_to_cr;
 
         struct {
--
Quoted 1 line

2.7.4

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

#8 lore raw

Re: [PATCH v2 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 16.06.17 at 16:26, <[email protected]> wrote:
Quoted 6 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

Quoted 3 lines
Quoted 1 line

Are you btw in agreement with ...

Quoted 9 lines

xen/include/public/domctl.h struct xen_domctl_monitor_op { · line 1114

Quoted 9 lines
xen/include/public/domctl.h +5 -0
@@ -1107,6 +1107,11 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /*
+             * Send event only if the changed bit in the control register
+             * is not masked.
+             */
+            uint64_aligned_t bitmask;

... the 5-byte gap being introduced here, using of which won't be possible without bumping XEN_DOMCTL_INTERFACE_VERSION again? Generally we aim at making padding explicit and checking it to be zero on input / providing zero on output.

Jan

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

#9 lore raw

Re: [PATCH v2 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 15 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 16.06.17 at 16:26, <[email protected]> wrote:
Quoted 6 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

Quoted 3 lines
Quoted 1 line

Are you btw in agreement with ...

Quoted 14 lines
Quoted 9 lines

xen/include/public/domctl.h struct xen_domctl_monitor_op { · line 1114

Quoted 9 lines
xen/include/public/domctl.h +5 -0
@@ -1107,6 +1107,11 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /*
+             * Send event only if the changed bit in the control register
+             * is not masked.
+             */
+            uint64_aligned_t bitmask;

... the 5-byte gap being introduced here, using of which won't be possible without bumping XEN_DOMCTL_INTERFACE_VERSION again? Generally we aim at making padding explicit and checking it to be zero on input / providing zero on output.

Yes, making the padding explicit would be better (but I'm not the maintainer of the domctl interface so my ack doesn't cover that).

Tamas

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

#10 lore raw

Re: [PATCH v2 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 16.06.17 at 17:28, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 16.06.17 at 16:26, <[email protected]> wrote:
Quoted 6 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

Quoted 3 lines
Quoted 1 line

Are you btw in agreement with ...

Quoted 14 lines
Quoted 9 lines

xen/include/public/domctl.h struct xen_domctl_monitor_op { · line 1114

Quoted 9 lines
xen/include/public/domctl.h +5 -0
@@ -1107,6 +1107,11 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /*
+             * Send event only if the changed bit in the control register
+             * is not masked.
+             */
+            uint64_aligned_t bitmask;

... the 5-byte gap being introduced here, using of which won't be possible without bumping XEN_DOMCTL_INTERFACE_VERSION again? Generally we aim at making padding explicit and checking it to be zero on input / providing zero on output.

Quoted 2 lines

Yes, making the padding explicit would be better (but I'm not the maintainer of the domctl interface so my ack doesn't cover that).

Well, I have been waiting for a monitor maintainer ack to perhaps give my own for the little bit of it where a REST maintainer one would be needed. Irrespective of file level maintainership I think the public interface parts still belong to their subsystems, and the REST maintainer ack should normally be a purely mechanical last step.

Jan

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

#11 lore raw

Re: [PATCH v2 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 35 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 16.06.17 at 17:28, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 16.06.17 at 16:26, <[email protected]> wrote:
Quoted 6 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

Quoted 3 lines
Quoted 1 line

Are you btw in agreement with ...

Quoted 14 lines
Quoted 9 lines

xen/include/public/domctl.h struct xen_domctl_monitor_op { · line 1114

Quoted 9 lines
xen/include/public/domctl.h +5 -0
@@ -1107,6 +1107,11 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /*
+             * Send event only if the changed bit in the control register
+             * is not masked.
+             */
+            uint64_aligned_t bitmask;

... the 5-byte gap being introduced here, using of which won't be possible without bumping XEN_DOMCTL_INTERFACE_VERSION again? Generally we aim at making padding explicit and checking it to be zero on input / providing zero on output.

Quoted 2 lines

Yes, making the padding explicit would be better (but I'm not the maintainer of the domctl interface so my ack doesn't cover that).

Quoted 6 lines

Well, I have been waiting for a monitor maintainer ack to perhaps give my own for the little bit of it where a REST maintainer one would be needed. Irrespective of file level maintainership I think the public interface parts still belong to their subsystems, and the REST maintainer ack should normally be a purely mechanical last step.

I can see the logic behind that. However, I would feel better if it wasn't just a mechanical last step as for example I'm not as well versed in the ABI side of things. Like in here, the padding bits have completely went by me without me noticing.

Tamas

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

#12 lore raw

PATCH v2 2/2 xen-access: write_ctrlreg_c4 test

In reply to Petre Pircalabu

Add test for write_ctrlreg event handling.

---
 tools/tests/xen-access/xen-access.c | 47 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
tools/tests/xen-access/xen-access.c +46 -1
index 238011e..29b60e8 100644
@@ -57,6 +57,9 @@
 #define X86_TRAP_DEBUG  1
 #define X86_TRAP_INT3   3
 
+/* From xen/include/asm-x86/x86-defns.h */
+#define X86_CR4_PGE        0x00000080 /* enable global pages */
+
 typedef struct vm_event {
     domid_t domain_id;
     xenevtchn_handle *xce_handle;
@@ -314,6 +317,22 @@ static void get_request(vm_event_t *vm_event, vm_event_request_t *req)
 }
 
 /*
+ * X86 control register names
+ */
+static const char* get_x86_ctrl_reg_name(uint32_t index)
+{
+    static const char* names[] = {
+        "CR0",
+        "CR3",
+        "CR4",
+        "XCR0",
+    };
+
+    return (index > 3) ? "" : names[index];
+}
+
+
+/*
  * Note that this function is not thread safe.
  */
 static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp)
@@ -337,7 +356,7 @@ void usage(char* progname)
 {
     fprintf(stderr, "Usage: %s [-m] <domain_id> write|exec", progname);
 #if defined(__i386__) || defined(__x86_64__)
-            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access");
+            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access|write_ctrlreg_cr4");
 #elif defined(__arm__) || defined(__aarch64__)
             fprintf(stderr, "|privcall");
 #endif
@@ -369,6 +388,7 @@ int main(int argc, char *argv[])
     int debug = 0;
     int cpuid = 0;
     int desc_access = 0;
+    int write_ctrlreg_cr4 = 1;
     uint16_t altp2m_view_id = 0;
 
     char* progname = argv[0];
@@ -439,6 +459,10 @@ int main(int argc, char *argv[])
     {
         desc_access = 1;
     }
+    else if ( !strcmp(argv[0], "write_ctrlreg_cr4") )
+    {
+        write_ctrlreg_cr4 = 1;
+    }
 #elif defined(__arm__) || defined(__aarch64__)
     else if ( !strcmp(argv[0], "privcall") )
     {
@@ -596,6 +620,18 @@ int main(int argc, char *argv[])
         }
     }
 
+    if ( write_ctrlreg_cr4 )
+    {
+        /* Mask the CR4.PGE bit so no events will be generated for global TLB flushes. */
+        rc = xc_monitor_write_ctrlreg(xch, domain_id, VM_EVENT_X86_CR4, 1, 1,
+                                      X86_CR4_PGE, 1);
+        if ( rc < 0 )
+        {
+            ERROR("Error %d setting write control register trapping with vm_event\n", rc);
+            goto exit;
+        }
+    }
+
     /* Wait for access */
     for (;;)
     {
@@ -806,6 +842,15 @@ int main(int argc, char *argv[])
                        req.u.desc_access.is_write);
                 rsp.flags |= VM_EVENT_FLAG_EMULATE;
                 break;
+            case VM_EVENT_REASON_WRITE_CTRLREG:
+                printf("Control register written: rip=%016"PRIx64", vcpu %d: "
+                       "reg=%s, old_value=%016"PRIx64", new_value=%016"PRIx64"\n",
+                       req.data.regs.x86.rip,
+                       req.vcpu_id,
+                       get_x86_ctrl_reg_name(req.u.write_ctrlreg.index),
+                       req.u.write_ctrlreg.old_value,
+                       req.u.write_ctrlreg.new_value);
+                break;
             default:
                 fprintf(stderr, "UNKNOWN REASON CODE %d\n", req.reason);
             }
Signature · 7 lines
2.7.4


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

Re: [PATCH v2 2/2] xen-access: write_ctrlreg_c4 test

Quoted 22 lines · Petre Pircalabu

Add test for write_ctrlreg event handling.

---
 tools/tests/xen-access/xen-access.c | 47 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
tools/tests/xen-access/xen-access.c +3 -0
index 238011e..29b60e8 100644
@@ -57,6 +57,9 @@
 #define X86_TRAP_DEBUG  1
 #define X86_TRAP_INT3   3
 
+/* From xen/include/asm-x86/x86-defns.h */
+#define X86_CR4_PGE        0x00000080 /* enable global pages */
+
 typedef struct vm_event {
     domid_t domain_id;
     xenevtchn_handle *xce_handle;

tools/tests/xen-access/xen-access.c static void get_request(vm_event_t *vm_event, vm_event_request_t *req) · line 324

Quoted 8 lines
tools/tests/xen-access/xen-access.c +5 -0
@@ -314,6 +317,22 @@ static void get_request(vm_event_t *vm_event, vm_event_request_t *req)
 }
 
 /*
+ * X86 control register names
+ */
+static const char* get_x86_ctrl_reg_name(uint32_t index)
+{
+    static const char* names[] = {

I would prefer to see this being defined in the following form so that it is clear where the indexes come from:
[VM_EVENT_X86_CR0] = "CR0",
...

tools/tests/xen-access/xen-access.c static void get_request(vm_event_t *vm_event, vm_event_request_t *req) · line 329

Quoted 5 lines
tools/tests/xen-access/xen-access.c +5 -0
+        "CR0",
+        "CR3",
+        "CR4",
+        "XCR0",
+    };

And this check to be index > VM_EVENT_X86_XCR0

Quoted 72 lines
tools/tests/xen-access/xen-access.c +32 -2
+    return (index > 3) ? "" : names[index];
+}
+
+
+/*
  * Note that this function is not thread safe.
  */
 static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp)
@@ -337,7 +356,7 @@ void usage(char* progname)
 {
     fprintf(stderr, "Usage: %s [-m] <domain_id> write|exec", progname);
 #if defined(__i386__) || defined(__x86_64__)
-            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access");
+            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access|write_ctrlreg_cr4");
 #elif defined(__arm__) || defined(__aarch64__)
             fprintf(stderr, "|privcall");
 #endif
@@ -369,6 +388,7 @@ int main(int argc, char *argv[])
     int debug = 0;
     int cpuid = 0;
     int desc_access = 0;
+    int write_ctrlreg_cr4 = 1;
     uint16_t altp2m_view_id = 0;
 
     char* progname = argv[0];
@@ -439,6 +459,10 @@ int main(int argc, char *argv[])
     {
         desc_access = 1;
     }
+    else if ( !strcmp(argv[0], "write_ctrlreg_cr4") )
+    {
+        write_ctrlreg_cr4 = 1;
+    }
 #elif defined(__arm__) || defined(__aarch64__)
     else if ( !strcmp(argv[0], "privcall") )
     {
@@ -596,6 +620,18 @@ int main(int argc, char *argv[])
         }
     }
 
+    if ( write_ctrlreg_cr4 )
+    {
+        /* Mask the CR4.PGE bit so no events will be generated for global TLB flushes. */
+        rc = xc_monitor_write_ctrlreg(xch, domain_id, VM_EVENT_X86_CR4, 1, 1,
+                                      X86_CR4_PGE, 1);
+        if ( rc < 0 )
+        {
+            ERROR("Error %d setting write control register trapping with vm_event\n", rc);
+            goto exit;
+        }
+    }
+
     /* Wait for access */
     for (;;)
     {
@@ -806,6 +842,15 @@ int main(int argc, char *argv[])
                        req.u.desc_access.is_write);
                 rsp.flags |= VM_EVENT_FLAG_EMULATE;
                 break;
+            case VM_EVENT_REASON_WRITE_CTRLREG:
+                printf("Control register written: rip=%016"PRIx64", vcpu %d: "
+                       "reg=%s, old_value=%016"PRIx64", new_value=%016"PRIx64"\n",
+                       req.data.regs.x86.rip,
+                       req.vcpu_id,
+                       get_x86_ctrl_reg_name(req.u.write_ctrlreg.index),
+                       req.u.write_ctrlreg.old_value,
+                       req.u.write_ctrlreg.new_value);
+                break;
             default:
                 fprintf(stderr, "UNKNOWN REASON CODE %d\n", req.reason);
             }
--
Quoted 1 line

2.7.4

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

#14 lore raw

Re: [PATCH v2 2/2] xen-access: write_ctrlreg_c4 test

Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 16.06.17 at 16:32, <[email protected]> wrote:

tools/tests/xen-access/xen-access.c static void get_request(vm_event_t *vm_event, vm_event_request_t *req) · line 324

Quoted 9 lines · Petre Pircalabu
tools/tests/xen-access/xen-access.c +5 -0
@@ -314,6 +317,22 @@ static void get_request(vm_event_t *vm_event, vm_event_request_t *req)
 }
 
 /*
+ * X86 control register names
+ */
+static const char* get_x86_ctrl_reg_name(uint32_t index)
+{
+    static const char* names[] = {

I would prefer to see this being defined in the following form so that it is clear where the indexes come from:
[VM_EVENT_X86_CR0] = "CR0",
...

Quoted 7 lines

tools/tests/xen-access/xen-access.c static void get_request(vm_event_t *vm_event, vm_event_request_t *req) · line 329

Quoted 5 lines
tools/tests/xen-access/xen-access.c +5 -0
+        "CR0",
+        "CR3",
+        "CR4",
+        "XCR0",
+    };

And this check to be index > VM_EVENT_X86_XCR0

Or perhaps even better >= ARRAY_SIZE()?

Quoted 5 lines

tools/tests/xen-access/xen-access.c static void get_request(vm_event_t *vm_event, vm_event_request_t *req) · line 335

Quoted 5 lines
tools/tests/xen-access/xen-access.c +5 -0
+    return (index > 3) ? "" : names[index];
+}
+
+
+/*

I also notice there are two successive blank lines here, which we generally try to avoid.

Jan

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

#15 lore raw

Re: [PATCH v2 2/2] xen-access: write_ctrlreg_c4 test

Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 16.06.17 at 16:32, <[email protected]> wrote:

tools/tests/xen-access/xen-access.c static void get_request(vm_event_t *vm_event, vm_event_request_t *req) · line 324

Quoted 9 lines · Petre Pircalabu
tools/tests/xen-access/xen-access.c +5 -0
@@ -314,6 +317,22 @@ static void get_request(vm_event_t *vm_event, vm_event_request_t *req)
 }
 
 /*
+ * X86 control register names
+ */
+static const char* get_x86_ctrl_reg_name(uint32_t index)
+{
+    static const char* names[] = {

I would prefer to see this being defined in the following form so that it is clear where the indexes come from:
[VM_EVENT_X86_CR0] = "CR0",
...

Quoted 5 lines
tools/tests/xen-access/xen-access.c +5 -0
+        "CR0",
+        "CR3",
+        "CR4",
+        "XCR0",
+    };
Quoted 3 lines
Quoted 1 line

And this check to be index > VM_EVENT_X86_XCR0

Or perhaps even better >= ARRAY_SIZE()?

Yeap, even better.

Tamas

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

#16 lore raw
In reply to Petre Pircalabu

Hello,

Any comments for this series?

Thanks,
Petre

Quoted 7 lines · Petre Pircalabu

This patchset enables masking the reception of write_ctrlreg events depending
on the value of certain bits in that register.
The most representative example is filtering out events when the CR4.PGE
bit is being flipped (global TLB flushes)

Quoted 7 lines

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

________________________
This email was scanned by Bitdefender

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

#17 lore raw

v3 cover write_ctrlreg event masking

In reply to Petre Pircalabu

This patchset enables masking the reception of write_ctrlreg events depending on the value of certain bits in that register.
The most representative example is filtering out events when the CR4.PGE bit is being flipped (global TLB flushes)

---
Changed since v2
* fix coding style.
* use ARRAY_SIZE and named indexes for x86 ctrl register resolution.
* add allignment padding for xen_domctl_monitor_op.

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

#18 lore raw

PATCH v3 1/2 x86/monitor: add masking support for write_ctrlreg events

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

---
 tools/libxc/include/xenctrl.h | 2 +-
 tools/libxc/xc_monitor.c      | 5 ++++-
 xen/arch/x86/hvm/monitor.c    | 3 ++-
 xen/arch/x86/monitor.c        | 9 +++++++++
 xen/include/asm-x86/domain.h  | 1 +
 xen/include/public/domctl.h   | 8 ++++++++
 6 files changed, 25 insertions(+), 3 deletions(-)
tools/libxc/include/xenctrl.h +1 -1
index 1629f41..8c26cb4 100644
@@ -1999,7 +1999,7 @@ 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);
+                             uint64_t bitmask, bool onchangeonly);
 /*
  * A list of MSR indices can usually be found in /usr/include/asm/msr-index.h.
  * Please consult the Intel/AMD manuals for more information on
tools/libxc/xc_monitor.c +4 -1
index f99b6e3..b44ce93 100644
@@ -70,7 +70,7 @@ int xc_monitor_get_capabilities(xc_interface *xch, domid_t domain_id,
 
 int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
                              uint16_t index, bool enable, bool sync,
-                             bool onchangeonly)
+                             uint64_t bitmask, bool onchangeonly)
 {
     DECLARE_DOMCTL;
 
@@ -82,6 +82,9 @@ int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
     domctl.u.monitor_op.u.mov_to_cr.index = index;
     domctl.u.monitor_op.u.mov_to_cr.sync = sync;
     domctl.u.monitor_op.u.mov_to_cr.onchangeonly = onchangeonly;
+    domctl.u.monitor_op.u.mov_to_cr.bitmask = bitmask;
+    domctl.u.monitor_op.u.mov_to_cr.pad1 = 0;
+    domctl.u.monitor_op.u.mov_to_cr.pad2 = 0;
 
     return do_domctl(xch, &domctl);
 }
xen/arch/x86/hvm/monitor.c +2 -1
index bde5fd0..a7ccfc4 100644
@@ -38,7 +38,8 @@ bool_t hvm_monitor_cr(unsigned int index, unsigned long value, unsigned long old
 
     if ( (ad->monitor.write_ctrlreg_enabled & ctrlreg_bitmask) &&
          (!(ad->monitor.write_ctrlreg_onchangeonly & ctrlreg_bitmask) ||
-          value != old) )
+          value != old) &&
+         (!((value ^ old) & ad->monitor.write_ctrlreg_mask[index])) )
     {
         bool_t sync = !!(ad->monitor.write_ctrlreg_sync & ctrlreg_bitmask);
 
xen/arch/x86/monitor.c +9 -0
index 449c64c..bedf13c 100644
@@ -136,6 +136,9 @@ int arch_monitor_domctl_event(struct domain *d,
         if ( unlikely(mop->u.mov_to_cr.index > 31) )
             return -EINVAL;
 
+        if ( unlikely(mop->u.mov_to_cr.pad1 || mop->u.mov_to_cr.pad2) )
+            return -EINVAL;
+
         ctrlreg_bitmask = monitor_ctrlreg_bitmask(mop->u.mov_to_cr.index);
         old_status = !!(ad->monitor.write_ctrlreg_enabled & ctrlreg_bitmask);
 
@@ -155,9 +158,15 @@ int arch_monitor_domctl_event(struct domain *d,
             ad->monitor.write_ctrlreg_onchangeonly &= ~ctrlreg_bitmask;
 
         if ( requested_status )
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = mop->u.mov_to_cr.bitmask;
             ad->monitor.write_ctrlreg_enabled |= ctrlreg_bitmask;
+        }
         else
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = 0;
             ad->monitor.write_ctrlreg_enabled &= ~ctrlreg_bitmask;
+        }
 
         if ( VM_EVENT_X86_CR3 == mop->u.mov_to_cr.index )
         {
xen/include/asm-x86/domain.h +1 -0
index 924caac..27d80ee 100644
@@ -406,6 +406,7 @@ struct arch_domain
         unsigned int cpuid_enabled               : 1;
         unsigned int descriptor_access_enabled   : 1;
         struct monitor_msr_bitmap *msr_bitmap;
+        uint64_t write_ctrlreg_mask[4];
     } monitor;
 
     /* Mem_access emulation control */
xen/include/public/domctl.h +8 -0
index f7cbc0a..ff39762 100644
@@ -1107,6 +1107,14 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /* Allignment padding */
+            uint8_t pad1;
+            uint32_t pad2;
+            /*
+             * Send event only if the changed bit in the control register
+             * is not masked.
+             */
+            uint64_aligned_t bitmask;
         } mov_to_cr;
 
         struct {
Signature · 7 lines
2.7.4


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

PATCH v3 2/2 xen-access: write_ctrlreg_c4 test

In reply to Petre Pircalabu

Add test for write_ctrlreg event handling.

---
 tools/tests/xen-access/xen-access.c | 53 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
tools/tests/xen-access/xen-access.c +52 -1
index 238011e..bbf5047 100644
@@ -57,6 +57,13 @@
 #define X86_TRAP_DEBUG  1
 #define X86_TRAP_INT3   3
 
+/* From xen/include/asm-x86/x86-defns.h */
+#define X86_CR4_PGE        0x00000080 /* enable global pages */
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
 typedef struct vm_event {
     domid_t domain_id;
     xenevtchn_handle *xce_handle;
@@ -314,6 +321,24 @@ static void get_request(vm_event_t *vm_event, vm_event_request_t *req)
 }
 
 /*
+ * X86 control register names
+ */
+static const char* get_x86_ctrl_reg_name(uint32_t index)
+{
+    static const char* names[] = {
+        [VM_EVENT_X86_CR0]  = "CR0",
+        [VM_EVENT_X86_CR3]  = "CR3",
+        [VM_EVENT_X86_CR4]  = "CR4",
+        [VM_EVENT_X86_XCR0] = "XCR0",
+    };
+
+    if ( index > ARRAY_SIZE(names) || names[index] == NULL )
+        return "";
+
+    return names[index];
+}
+
+/*
  * Note that this function is not thread safe.
  */
 static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp)
@@ -337,7 +362,7 @@ void usage(char* progname)
 {
     fprintf(stderr, "Usage: %s [-m] <domain_id> write|exec", progname);
 #if defined(__i386__) || defined(__x86_64__)
-            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access");
+            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access|write_ctrlreg_cr4");
 #elif defined(__arm__) || defined(__aarch64__)
             fprintf(stderr, "|privcall");
 #endif
@@ -369,6 +394,7 @@ int main(int argc, char *argv[])
     int debug = 0;
     int cpuid = 0;
     int desc_access = 0;
+    int write_ctrlreg_cr4 = 1;
     uint16_t altp2m_view_id = 0;
 
     char* progname = argv[0];
@@ -439,6 +465,10 @@ int main(int argc, char *argv[])
     {
         desc_access = 1;
     }
+    else if ( !strcmp(argv[0], "write_ctrlreg_cr4") )
+    {
+        write_ctrlreg_cr4 = 1;
+    }
 #elif defined(__arm__) || defined(__aarch64__)
     else if ( !strcmp(argv[0], "privcall") )
     {
@@ -596,6 +626,18 @@ int main(int argc, char *argv[])
         }
     }
 
+    if ( write_ctrlreg_cr4 )
+    {
+        /* Mask the CR4.PGE bit so no events will be generated for global TLB flushes. */
+        rc = xc_monitor_write_ctrlreg(xch, domain_id, VM_EVENT_X86_CR4, 1, 1,
+                                      X86_CR4_PGE, 1);
+        if ( rc < 0 )
+        {
+            ERROR("Error %d setting write control register trapping with vm_event\n", rc);
+            goto exit;
+        }
+    }
+
     /* Wait for access */
     for (;;)
     {
@@ -806,6 +848,15 @@ int main(int argc, char *argv[])
                        req.u.desc_access.is_write);
                 rsp.flags |= VM_EVENT_FLAG_EMULATE;
                 break;
+            case VM_EVENT_REASON_WRITE_CTRLREG:
+                printf("Control register written: rip=%016"PRIx64", vcpu %d: "
+                       "reg=%s, old_value=%016"PRIx64", new_value=%016"PRIx64"\n",
+                       req.data.regs.x86.rip,
+                       req.vcpu_id,
+                       get_x86_ctrl_reg_name(req.u.write_ctrlreg.index),
+                       req.u.write_ctrlreg.old_value,
+                       req.u.write_ctrlreg.new_value);
+                break;
             default:
                 fprintf(stderr, "UNKNOWN REASON CODE %d\n", req.reason);
             }
Signature · 7 lines
2.7.4


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

Re: [PATCH v3 2/2] xen-access: write_ctrlreg_c4 test

Quoted 31 lines · Petre Pircalabu

Add test for write_ctrlreg event handling.

---
 tools/tests/xen-access/xen-access.c | 53 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
tools/tests/xen-access/xen-access.c +9 -0
index 238011e..bbf5047 100644
@@ -57,6 +57,13 @@
 #define X86_TRAP_DEBUG  1
 #define X86_TRAP_INT3   3
 
+/* From xen/include/asm-x86/x86-defns.h */
+#define X86_CR4_PGE        0x00000080 /* enable global pages */
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
 typedef struct vm_event {
     domid_t domain_id;
     xenevtchn_handle *xce_handle;
@@ -314,6 +321,24 @@ static void get_request(vm_event_t *vm_event, vm_event_request_t *req)
 }
 
 /*
+ * X86 control register names
+ */

tools/tests/xen-access/xen-access.c static void get_request(vm_event_t *vm_event, vm_event_request_t *req) · line 335

Quoted 10 lines
tools/tests/xen-access/xen-access.c +10 -0
+static const char* get_x86_ctrl_reg_name(uint32_t index)
+{
+    static const char* names[] = {
+        [VM_EVENT_X86_CR0]  = "CR0",
+        [VM_EVENT_X86_CR3]  = "CR3",
+        [VM_EVENT_X86_CR4]  = "CR4",
+        [VM_EVENT_X86_XCR0] = "XCR0",
+    };
+
+    if ( index > ARRAY_SIZE(names) || names[index] == NULL )

I think this probably wants to be index >= ARRAY_SIZE(names).

Thanks,
Razvan

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

#21 lore raw

v4 cover write_ctrlreg event masking

In reply to Petre Pircalabu

This patchset enables masking the reception of write_ctrlreg events depending on the value of certain bits in that register.
The most representative example is filtering out events when the CR4.PGE bit is being flipped (global TLB flushes)

---
Changed since v2
* fix coding style
* use ARRAY_SIZE and named indexes for x86 ctrl register resolution
* add allignment padding for xen_domctl_monitor_op

Changed since v3
* Fix index condition in get_x86_ctrl_reg_name

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

#22 lore raw

PATCH v4 1/2 x86/monitor: add masking support for write_ctrlreg events

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

---
 tools/libxc/include/xenctrl.h | 2 +-
 tools/libxc/xc_monitor.c      | 5 ++++-
 xen/arch/x86/hvm/monitor.c    | 3 ++-
 xen/arch/x86/monitor.c        | 9 +++++++++
 xen/include/asm-x86/domain.h  | 1 +
 xen/include/public/domctl.h   | 8 ++++++++
 6 files changed, 25 insertions(+), 3 deletions(-)
tools/libxc/include/xenctrl.h +1 -1
index 1629f41..8c26cb4 100644
@@ -1999,7 +1999,7 @@ 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);
+                             uint64_t bitmask, bool onchangeonly);
 /*
  * A list of MSR indices can usually be found in /usr/include/asm/msr-index.h.
  * Please consult the Intel/AMD manuals for more information on
tools/libxc/xc_monitor.c +4 -1
index f99b6e3..b44ce93 100644
@@ -70,7 +70,7 @@ int xc_monitor_get_capabilities(xc_interface *xch, domid_t domain_id,
 
 int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
                              uint16_t index, bool enable, bool sync,
-                             bool onchangeonly)
+                             uint64_t bitmask, bool onchangeonly)
 {
     DECLARE_DOMCTL;
 
@@ -82,6 +82,9 @@ int xc_monitor_write_ctrlreg(xc_interface *xch, domid_t domain_id,
     domctl.u.monitor_op.u.mov_to_cr.index = index;
     domctl.u.monitor_op.u.mov_to_cr.sync = sync;
     domctl.u.monitor_op.u.mov_to_cr.onchangeonly = onchangeonly;
+    domctl.u.monitor_op.u.mov_to_cr.bitmask = bitmask;
+    domctl.u.monitor_op.u.mov_to_cr.pad1 = 0;
+    domctl.u.monitor_op.u.mov_to_cr.pad2 = 0;
 
     return do_domctl(xch, &domctl);
 }
xen/arch/x86/hvm/monitor.c +2 -1
index bde5fd0..a7ccfc4 100644
@@ -38,7 +38,8 @@ bool_t hvm_monitor_cr(unsigned int index, unsigned long value, unsigned long old
 
     if ( (ad->monitor.write_ctrlreg_enabled & ctrlreg_bitmask) &&
          (!(ad->monitor.write_ctrlreg_onchangeonly & ctrlreg_bitmask) ||
-          value != old) )
+          value != old) &&
+         (!((value ^ old) & ad->monitor.write_ctrlreg_mask[index])) )
     {
         bool_t sync = !!(ad->monitor.write_ctrlreg_sync & ctrlreg_bitmask);
 
xen/arch/x86/monitor.c +9 -0
index 449c64c..bedf13c 100644
@@ -136,6 +136,9 @@ int arch_monitor_domctl_event(struct domain *d,
         if ( unlikely(mop->u.mov_to_cr.index > 31) )
             return -EINVAL;
 
+        if ( unlikely(mop->u.mov_to_cr.pad1 || mop->u.mov_to_cr.pad2) )
+            return -EINVAL;
+
         ctrlreg_bitmask = monitor_ctrlreg_bitmask(mop->u.mov_to_cr.index);
         old_status = !!(ad->monitor.write_ctrlreg_enabled & ctrlreg_bitmask);
 
@@ -155,9 +158,15 @@ int arch_monitor_domctl_event(struct domain *d,
             ad->monitor.write_ctrlreg_onchangeonly &= ~ctrlreg_bitmask;
 
         if ( requested_status )
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = mop->u.mov_to_cr.bitmask;
             ad->monitor.write_ctrlreg_enabled |= ctrlreg_bitmask;
+        }
         else
+        {
+            ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = 0;
             ad->monitor.write_ctrlreg_enabled &= ~ctrlreg_bitmask;
+        }
 
         if ( VM_EVENT_X86_CR3 == mop->u.mov_to_cr.index )
         {
xen/include/asm-x86/domain.h +1 -0
index 924caac..27d80ee 100644
@@ -406,6 +406,7 @@ struct arch_domain
         unsigned int cpuid_enabled               : 1;
         unsigned int descriptor_access_enabled   : 1;
         struct monitor_msr_bitmap *msr_bitmap;
+        uint64_t write_ctrlreg_mask[4];
     } monitor;
 
     /* Mem_access emulation control */
xen/include/public/domctl.h +8 -0
index f7cbc0a..ff39762 100644
@@ -1107,6 +1107,14 @@ struct xen_domctl_monitor_op {
             uint8_t sync;
             /* Send event only on a change of value */
             uint8_t onchangeonly;
+            /* Allignment padding */
+            uint8_t pad1;
+            uint32_t pad2;
+            /*
+             * Send event only if the changed bit in the control register
+             * is not masked.
+             */
+            uint64_aligned_t bitmask;
         } mov_to_cr;
 
         struct {
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v4 1/2
#23 lore raw

Re: [PATCH v4 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 11 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

---
 tools/libxc/include/xenctrl.h | 2 +-
 tools/libxc/xc_monitor.c      | 5 ++++-

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

#24 lore raw

Re: [PATCH v4 1/2] x86/monitor: add masking support for write_ctrlreg events

In reply to Petre Pircalabu
Quoted 8 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

Coverity isn't happy with this patch.

It seems to me there is indeed a risk to overrun the buffer (4 in size) because the caller can specify index up to 31.

** CID 1412966: Memory - corruptions (OVERRUN) /xen/arch/x86/monitor.c: 162 in arch_monitor_domctl_event()

________________________________________________________________________________________________________
*** CID 1412966: Memory - corruptions (OVERRUN) /xen/arch/x86/monitor.c: 162 in arch_monitor_domctl_event()
156 ad->monitor.write_ctrlreg_onchangeonly |= ctrlreg_bitmask;
157 else
158 ad->monitor.write_ctrlreg_onchangeonly &= ~ctrlreg_bitmask;
159
160 if ( requested_status )
161 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1412966: Memory - corruptions (OVERRUN)
Overrunning array "ad->monitor.write_ctrlreg_mask" of 4 8-byte elements at element index 31 (byte offset 248) using index "mop->u.mov_to_cr.index"

(which evaluates to 31).
162 ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = mop->u.mov_to_cr.bitmask;
163 ad->monitor.write_ctrlreg_enabled |= ctrlreg_bitmask;

164             }                                                                                                                                             
165             else                                                                                                                                          
166             {                                                                                                                                             

167 ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = 0;

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

#25 lore raw

Re: [PATCH v4 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 7 lines · Wei Liu
Quoted 6 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

Quoted 7 lines
Quoted 2 lines

Coverity isn't happy with this patch.

It seems to me there is indeed a risk to overrun the buffer (4 in size) because the caller can specify index up to 31.

Indeed. We have a sanity check earlier in here that checks whether index > 31 but it would make more sense to check it against the max valid value of index to begin with (which at the moment is VM_EVENT_X86_XCR0 = 3).

Quoted 16 lines

** CID 1412966: Memory - corruptions (OVERRUN) /xen/arch/x86/monitor.c: 162 in arch_monitor_domctl_event()

________________________________________________________________________________________________________
*** CID 1412966: Memory - corruptions (OVERRUN) /xen/arch/x86/monitor.c: 162 in arch_monitor_domctl_event()
156 ad->monitor.write_ctrlreg_onchangeonly |= ctrlreg_bitmask;
157 else
158 ad->monitor.write_ctrlreg_onchangeonly &= ~ctrlreg_bitmask;
159
160 if ( requested_status )
161 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1412966: Memory - corruptions (OVERRUN)
Overrunning array "ad->monitor.write_ctrlreg_mask" of 4 8-byte elements at element index 31 (byte offset 248) using index "mop->u.mov_to_cr.index"

Quoted 7 lines

(which evaluates to 31).
162 ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = mop->u.mov_to_cr.bitmask;
163 ad->monitor.write_ctrlreg_enabled |= ctrlreg_bitmask;

164             }
165             else
166             {

167 ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = 0;

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

#26 lore raw

Re: [PATCH v4 1/2] x86/monitor: add masking support for write_ctrlreg events

In reply to Wei Liu
Quoted 30 lines · Wei Liu
Quoted 8 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

Coverity isn't happy with this patch.

It seems to me there is indeed a risk to overrun the buffer (4 in size) because the caller can specify index up to 31.

** CID 1412966: Memory - corruptions (OVERRUN) /xen/arch/x86/monitor.c: 162 in arch_monitor_domctl_event()

________________________________________________________________________________________________________
*** CID 1412966: Memory - corruptions (OVERRUN) /xen/arch/x86/monitor.c: 162 in arch_monitor_domctl_event()
156 ad->monitor.write_ctrlreg_onchangeonly |= ctrlreg_bitmask;
157 else
158 ad->monitor.write_ctrlreg_onchangeonly &= ~ctrlreg_bitmask;
159
160 if ( requested_status )
161 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1412966: Memory - corruptions (OVERRUN)
Overrunning array "ad->monitor.write_ctrlreg_mask" of 4 8-byte elements at element index 31 (byte offset 248) using index "mop->u.mov_to_cr.index"

Quoted 7 lines

(which evaluates to 31).
162 ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = mop->u.mov_to_cr.bitmask;
163 ad->monitor.write_ctrlreg_enabled |= ctrlreg_bitmask;

164             }                                                                                                                                             
165             else                                                                                                                                          
166             {                                                                                                                                             

167 ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = 0;

I vaguely remember that 31 was introduced simply as a "reserved" precaution - we can probably safely please Coverity by simply patching that code to not go over 3 as an index.

To Petre's credit, he did notice and propose that we change this value but I've suggested that we keep the check as-is for the future. My bad. :)

Thanks,
Razvan

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

#27 lore raw

Re: [PATCH v4 1/2] x86/monitor: add masking support for write_ctrlreg events

Quoted 39 lines · Razvan Cojocaru
Quoted 30 lines · Wei Liu
Quoted 8 lines · Petre Pircalabu

Add support for filtering out the write_ctrlreg monitor events if they are generated only by changing certains bits.
A new parameter (bitmask) was added to the xc_monitor_write_ctrlreg function in order to mask the event generation if the changed bits are set.

Coverity isn't happy with this patch.

It seems to me there is indeed a risk to overrun the buffer (4 in size) because the caller can specify index up to 31.

** CID 1412966: Memory - corruptions (OVERRUN) /xen/arch/x86/monitor.c: 162 in arch_monitor_domctl_event()

________________________________________________________________________________________________________
*** CID 1412966: Memory - corruptions (OVERRUN) /xen/arch/x86/monitor.c: 162 in arch_monitor_domctl_event()
156 ad->monitor.write_ctrlreg_onchangeonly |= ctrlreg_bitmask;
157 else
158 ad->monitor.write_ctrlreg_onchangeonly &= ~ctrlreg_bitmask;
159
160 if ( requested_status )
161 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1412966: Memory - corruptions (OVERRUN)
Overrunning array "ad->monitor.write_ctrlreg_mask" of 4 8-byte elements at element index 31 (byte offset 248) using index "mop->u.mov_to_cr.index"

Quoted 7 lines

(which evaluates to 31).
162 ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = mop->u.mov_to_cr.bitmask;
163 ad->monitor.write_ctrlreg_enabled |= ctrlreg_bitmask;

164             }                                                                                                                                             
165             else                                                                                                                                          
166             {                                                                                                                                             

167 ad->monitor.write_ctrlreg_mask[mop->u.mov_to_cr.index] = 0;

Quoted 7 lines

I vaguely remember that 31 was introduced simply as a "reserved" precaution - we can probably safely please Coverity by simply patching that code to not go over 3 as an index.

To Petre's credit, he did notice and propose that we change this value but I've suggested that we keep the check as-is for the future. My bad. :)

OK. Please submit a patch to fix this. Using 3 should be fine. Please include

Coverity-ID: 1412966

in the commit message.

Quoted 9 lines

Thanks,
Razvan

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

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

#28 lore raw

PATCH v4 2/2 xen-access: write_ctrlreg_c4 test

In reply to Petre Pircalabu

Add test for write_ctrlreg event handling.

---
 tools/tests/xen-access/xen-access.c | 53 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
tools/tests/xen-access/xen-access.c +52 -1
index 238011e..1e69e25 100644
@@ -57,6 +57,13 @@
 #define X86_TRAP_DEBUG  1
 #define X86_TRAP_INT3   3
 
+/* From xen/include/asm-x86/x86-defns.h */
+#define X86_CR4_PGE        0x00000080 /* enable global pages */
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
 typedef struct vm_event {
     domid_t domain_id;
     xenevtchn_handle *xce_handle;
@@ -314,6 +321,24 @@ static void get_request(vm_event_t *vm_event, vm_event_request_t *req)
 }
 
 /*
+ * X86 control register names
+ */
+static const char* get_x86_ctrl_reg_name(uint32_t index)
+{
+    static const char* names[] = {
+        [VM_EVENT_X86_CR0]  = "CR0",
+        [VM_EVENT_X86_CR3]  = "CR3",
+        [VM_EVENT_X86_CR4]  = "CR4",
+        [VM_EVENT_X86_XCR0] = "XCR0",
+    };
+
+    if ( index >= ARRAY_SIZE(names) || names[index] == NULL )
+        return "";
+
+    return names[index];
+}
+
+/*
  * Note that this function is not thread safe.
  */
 static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp)
@@ -337,7 +362,7 @@ void usage(char* progname)
 {
     fprintf(stderr, "Usage: %s [-m] <domain_id> write|exec", progname);
 #if defined(__i386__) || defined(__x86_64__)
-            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access");
+            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access|write_ctrlreg_cr4");
 #elif defined(__arm__) || defined(__aarch64__)
             fprintf(stderr, "|privcall");
 #endif
@@ -369,6 +394,7 @@ int main(int argc, char *argv[])
     int debug = 0;
     int cpuid = 0;
     int desc_access = 0;
+    int write_ctrlreg_cr4 = 1;
     uint16_t altp2m_view_id = 0;
 
     char* progname = argv[0];
@@ -439,6 +465,10 @@ int main(int argc, char *argv[])
     {
         desc_access = 1;
     }
+    else if ( !strcmp(argv[0], "write_ctrlreg_cr4") )
+    {
+        write_ctrlreg_cr4 = 1;
+    }
 #elif defined(__arm__) || defined(__aarch64__)
     else if ( !strcmp(argv[0], "privcall") )
     {
@@ -596,6 +626,18 @@ int main(int argc, char *argv[])
         }
     }
 
+    if ( write_ctrlreg_cr4 )
+    {
+        /* Mask the CR4.PGE bit so no events will be generated for global TLB flushes. */
+        rc = xc_monitor_write_ctrlreg(xch, domain_id, VM_EVENT_X86_CR4, 1, 1,
+                                      X86_CR4_PGE, 1);
+        if ( rc < 0 )
+        {
+            ERROR("Error %d setting write control register trapping with vm_event\n", rc);
+            goto exit;
+        }
+    }
+
     /* Wait for access */
     for (;;)
     {
@@ -806,6 +848,15 @@ int main(int argc, char *argv[])
                        req.u.desc_access.is_write);
                 rsp.flags |= VM_EVENT_FLAG_EMULATE;
                 break;
+            case VM_EVENT_REASON_WRITE_CTRLREG:
+                printf("Control register written: rip=%016"PRIx64", vcpu %d: "
+                       "reg=%s, old_value=%016"PRIx64", new_value=%016"PRIx64"\n",
+                       req.data.regs.x86.rip,
+                       req.vcpu_id,
+                       get_x86_ctrl_reg_name(req.u.write_ctrlreg.index),
+                       req.u.write_ctrlreg.old_value,
+                       req.u.write_ctrlreg.new_value);
+                break;
             default:
                 fprintf(stderr, "UNKNOWN REASON CODE %d\n", req.reason);
             }
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v4 2/2
#29 lore raw

Re: [PATCH v4 2/2] xen-access: write_ctrlreg_c4 test

Quoted 3 lines · Petre Pircalabu

Add test for write_ctrlreg event handling.

Quoted 111 lines
---
 tools/tests/xen-access/xen-access.c | 53 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
tools/tests/xen-access/xen-access.c +52 -2
index 238011e..1e69e25 100644
@@ -57,6 +57,13 @@
 #define X86_TRAP_DEBUG  1
 #define X86_TRAP_INT3   3
 
+/* From xen/include/asm-x86/x86-defns.h */
+#define X86_CR4_PGE        0x00000080 /* enable global pages */
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
 typedef struct vm_event {
     domid_t domain_id;
     xenevtchn_handle *xce_handle;
@@ -314,6 +321,24 @@ static void get_request(vm_event_t *vm_event, vm_event_request_t *req)
 }
 
 /*
+ * X86 control register names
+ */
+static const char* get_x86_ctrl_reg_name(uint32_t index)
+{
+    static const char* names[] = {
+        [VM_EVENT_X86_CR0]  = "CR0",
+        [VM_EVENT_X86_CR3]  = "CR3",
+        [VM_EVENT_X86_CR4]  = "CR4",
+        [VM_EVENT_X86_XCR0] = "XCR0",
+    };
+
+    if ( index >= ARRAY_SIZE(names) || names[index] == NULL )
+        return "";
+
+    return names[index];
+}
+
+/*
  * Note that this function is not thread safe.
  */
 static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp)
@@ -337,7 +362,7 @@ void usage(char* progname)
 {
     fprintf(stderr, "Usage: %s [-m] <domain_id> write|exec", progname);
 #if defined(__i386__) || defined(__x86_64__)
-            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access");
+            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid|desc_access|write_ctrlreg_cr4");
 #elif defined(__arm__) || defined(__aarch64__)
             fprintf(stderr, "|privcall");
 #endif
@@ -369,6 +394,7 @@ int main(int argc, char *argv[])
     int debug = 0;
     int cpuid = 0;
     int desc_access = 0;
+    int write_ctrlreg_cr4 = 1;
     uint16_t altp2m_view_id = 0;
 
     char* progname = argv[0];
@@ -439,6 +465,10 @@ int main(int argc, char *argv[])
     {
         desc_access = 1;
     }
+    else if ( !strcmp(argv[0], "write_ctrlreg_cr4") )
+    {
+        write_ctrlreg_cr4 = 1;
+    }
 #elif defined(__arm__) || defined(__aarch64__)
     else if ( !strcmp(argv[0], "privcall") )
     {
@@ -596,6 +626,18 @@ int main(int argc, char *argv[])
         }
     }
 
+    if ( write_ctrlreg_cr4 )
+    {
+        /* Mask the CR4.PGE bit so no events will be generated for global TLB flushes. */
+        rc = xc_monitor_write_ctrlreg(xch, domain_id, VM_EVENT_X86_CR4, 1, 1,
+                                      X86_CR4_PGE, 1);
+        if ( rc < 0 )
+        {
+            ERROR("Error %d setting write control register trapping with vm_event\n", rc);
+            goto exit;
+        }
+    }
+
     /* Wait for access */
     for (;;)
     {
@@ -806,6 +848,15 @@ int main(int argc, char *argv[])
                        req.u.desc_access.is_write);
                 rsp.flags |= VM_EVENT_FLAG_EMULATE;
                 break;
+            case VM_EVENT_REASON_WRITE_CTRLREG:
+                printf("Control register written: rip=%016"PRIx64", vcpu %d: "
+                       "reg=%s, old_value=%016"PRIx64", new_value=%016"PRIx64"\n",
+                       req.data.regs.x86.rip,
+                       req.vcpu_id,
+                       get_x86_ctrl_reg_name(req.u.write_ctrlreg.index),
+                       req.u.write_ctrlreg.old_value,
+                       req.u.write_ctrlreg.new_value);
+                break;
             default:
                 fprintf(stderr, "UNKNOWN REASON CODE %d\n", req.reason);
             }
--
Quoted 1 line

2.7.4

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