Skip to contentSkip to conversation

x86/hvm: Allow guest_request vm_events coming from userspace

PATCH Superseded Alexandru Isaila 3 participants 3 messages Aug 3, 2017

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 x86/hvm: Allow guest_request vm_events coming from userspace

Allow guest userspace code to request that a vm_event be sent out via VMCALL. This functionality seems to be handy for a number of Xen developers, as stated on the mailing list (thread "[Xen-devel] HVMOP_guest_request_vm_event only works from guest in ring0"). This is a use case in communication between a userspace application in the guest and the introspection application in dom0.

---
Changes since V2:
-Added a new flag to enable the vm call from the guest userspace

---
 tools/libxc/include/xenctrl.h |  2 ++
 tools/libxc/xc_monitor.c      | 14 ++++++++++++++
 xen/arch/x86/hvm/hypercall.c  |  8 ++++++++
 xen/common/monitor.c          | 13 +++++++++++++
 xen/include/public/domctl.h   | 21 +++++++++++----------
 xen/include/xen/sched.h       |  5 +++--
 6 files changed, 51 insertions(+), 12 deletions(-)
tools/libxc/include/xenctrl.h +2 -0
index bde8313..eed60db 100644
@@ -2022,6 +2022,8 @@ int xc_monitor_descriptor_access(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_guest_userspace_vmcall(xc_interface *xch, domid_t domain_id,
+                                      bool enable);
 int xc_monitor_debug_exceptions(xc_interface *xch, domid_t domain_id,
                                 bool enable, bool sync);
 int xc_monitor_cpuid(xc_interface *xch, domid_t domain_id, bool enable);
tools/libxc/xc_monitor.c +14 -0
index b44ce93..63c6320 100644
@@ -161,6 +161,20 @@ int xc_monitor_guest_request(xc_interface *xch, domid_t domain_id, bool enable,
     return do_domctl(xch, &domctl);
 }
 
+int xc_allow_guest_userspace_vmcall(xc_interface *xch, domid_t domain_id, bool enable)
+{
+    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_GUEST_USERSPACE_VMCALL;
+
+    return do_domctl(xch, &domctl);
+}
+
+
 int xc_monitor_emulate_each_rep(xc_interface *xch, domid_t domain_id,
                                 bool enable)
 {
xen/arch/x86/hvm/hypercall.c +8 -0
index e7238ce..c7fab4b 100644
@@ -152,9 +152,17 @@ int hvm_hypercall(struct cpu_user_regs *regs)
     {
     case 8:
         eax = regs->rax;
+        if ( currd->monitor.guest_request_userspace_vmcall &&
+             eax == __HYPERVISOR_hvm_op &&
+             regs->rdi == HVMOP_guest_request_vm_event )
+            break;
         /* Fallthrough to permission check. */
     case 4:
     case 2:
+        if ( mode != 8 && currd->monitor.guest_request_userspace_vmcall &&
+             eax == __HYPERVISOR_hvm_op &&
+             regs->ebx == HVMOP_guest_request_vm_event )
+            break;
         if ( unlikely(hvm_get_cpl(curr)) )
         {
     default:
xen/common/monitor.c +13 -0
index 451f42f..4011dc3 100644
@@ -78,6 +78,19 @@ int monitor_domctl(struct domain *d, struct xen_domctl_monitor_op *mop)
         domain_unpause(d);
         break;
     }
+    case XEN_DOMCTL_MONITOR_EVENT_GUEST_USERSPACE_VMCALL:
+    {
+        bool_t old_status = d->monitor.guest_request_enabled;
+
+        if ( unlikely(old_status == requested_status) )
+            return -EEXIST;
+
+        domain_pause(d);
+        d->monitor.guest_request_sync = mop->u.guest_request.sync;
+        d->monitor.guest_request_userspace_vmcall = requested_status;
+        domain_unpause(d);
+        break;
+    }
 
     default:
         /* Give arch-side the chance to handle this event */
xen/include/public/domctl.h +11 -10
index ff39762..e782517 100644
@@ -1073,16 +1073,17 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t);
 #define XEN_DOMCTL_MONITOR_OP_GET_CAPABILITIES  2
 #define XEN_DOMCTL_MONITOR_OP_EMULATE_EACH_REP  3
 
-#define XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG         0
-#define XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR            1
-#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
-#define XEN_DOMCTL_MONITOR_EVENT_CPUID                 6
-#define XEN_DOMCTL_MONITOR_EVENT_PRIVILEGED_CALL       7
-#define XEN_DOMCTL_MONITOR_EVENT_INTERRUPT             8
-#define XEN_DOMCTL_MONITOR_EVENT_DESC_ACCESS           9
+#define XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG          0
+#define XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR             1
+#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
+#define XEN_DOMCTL_MONITOR_EVENT_CPUID                  6
+#define XEN_DOMCTL_MONITOR_EVENT_PRIVILEGED_CALL        7
+#define XEN_DOMCTL_MONITOR_EVENT_INTERRUPT              8
+#define XEN_DOMCTL_MONITOR_EVENT_DESC_ACCESS            9
+#define XEN_DOMCTL_MONITOR_EVENT_GUEST_USERSPACE_VMCALL 10
 
 struct xen_domctl_monitor_op {
     uint32_t op; /* XEN_DOMCTL_MONITOR_OP_* */
xen/include/xen/sched.h +3 -2
index 6673b27..11137b0 100644
@@ -480,8 +480,9 @@ struct domain
 
     /* Common monitor options */
     struct {
-        unsigned int guest_request_enabled       : 1;
-        unsigned int guest_request_sync          : 1;
+        unsigned int guest_request_enabled          : 1;
+        unsigned int guest_request_sync             : 1;
+        unsigned int guest_request_userspace_vmcall : 1;
     } monitor;
 };
 
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 6 lines · Alexandru Isaila

Allow guest userspace code to request that a vm_event be sent out via VMCALL. This functionality seems to be handy for a number of Xen developers, as stated on the mailing list (thread "[Xen-devel] HVMOP_guest_request_vm_event only works from guest in ring0"). This is a use case in communication between a userspace application in the guest and the introspection application in dom0.

This ends up as the immutable description of the change in history, once committed.

May I recommend:

"In some introspection usecases, an in-guest agent needs to communicate with the external introspection agent. An existing mechanism is HVMOP_guest_request_vm_event, but this is restricted to kernel usecases like all other hypercalls.

Introduce a mechanism whereby the introspection agent can whitelist the use of HVMOP_guest_request_vm_event directly from userspace."

?

Quoted 59 lines

---
Changes since V2:
-Added a new flag to enable the vm call from the guest userspace

---
 tools/libxc/include/xenctrl.h |  2 ++
 tools/libxc/xc_monitor.c      | 14 ++++++++++++++
 xen/arch/x86/hvm/hypercall.c  |  8 ++++++++
 xen/common/monitor.c          | 13 +++++++++++++
 xen/include/public/domctl.h   | 21 +++++++++++----------
 xen/include/xen/sched.h       |  5 +++--
 6 files changed, 51 insertions(+), 12 deletions(-)
tools/libxc/include/xenctrl.h +2 -0
index bde8313..eed60db 100644
@@ -2022,6 +2022,8 @@ int xc_monitor_descriptor_access(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_guest_userspace_vmcall(xc_interface *xch, domid_t domain_id,
+                                      bool enable);
 int xc_monitor_debug_exceptions(xc_interface *xch, domid_t domain_id,
                                 bool enable, bool sync);
 int xc_monitor_cpuid(xc_interface *xch, domid_t domain_id, bool enable);
tools/libxc/xc_monitor.c +14 -0
index b44ce93..63c6320 100644
@@ -161,6 +161,20 @@ int xc_monitor_guest_request(xc_interface *xch, domid_t domain_id, bool enable,
     return do_domctl(xch, &domctl);
 }
 
+int xc_allow_guest_userspace_vmcall(xc_interface *xch, domid_t domain_id, bool enable)
+{
+    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_GUEST_USERSPACE_VMCALL;
+
+    return do_domctl(xch, &domctl);
+}
+
+
 int xc_monitor_emulate_each_rep(xc_interface *xch, domid_t domain_id,
                                 bool enable)
 {
xen/arch/x86/hvm/hypercall.c +0 -0
index e7238ce..c7fab4b 100644

xen/arch/x86/hvm/hypercall.c int hvm_hypercall(struct cpu_user_regs *regs) · line 158

Quoted 7 lines
xen/arch/x86/hvm/hypercall.c +4 -0
@@ -152,9 +152,17 @@ int hvm_hypercall(struct cpu_user_regs *regs)
     {
     case 8:
         eax = regs->rax;
+        if ( currd->monitor.guest_request_userspace_vmcall &&
+             eax == __HYPERVISOR_hvm_op &&
+             regs->rdi == HVMOP_guest_request_vm_event )
+            break;

Newline

xen/arch/x86/hvm/hypercall.c int hvm_hypercall(struct cpu_user_regs *regs) · line 165

Quoted 7 lines
xen/arch/x86/hvm/hypercall.c +4 -0
         /* Fallthrough to permission check. */
     case 4:
     case 2:
+        if ( mode != 8 && currd->monitor.guest_request_userspace_vmcall &&
+             eax == __HYPERVISOR_hvm_op &&
+             regs->ebx == HVMOP_guest_request_vm_event )
+            break;

Newline

xen/common/monitor.c int monitor_domctl(struct domain *d, struct xen_domctl_monitor_op *mop) · line 80

Quoted 11 lines
xen/arch/x86/hvm/hypercall.c +0 -0
         if ( unlikely(hvm_get_cpl(curr)) )
         {
     default:
xen/common/monitor.c +0 -0
index 451f42f..4011dc3 100644
@@ -78,6 +78,19 @@ int monitor_domctl(struct domain *d, struct xen_domctl_monitor_op *mop)
         domain_unpause(d);
         break;
     }

Newline

xen/common/monitor.c int monitor_domctl(struct domain *d, struct xen_domctl_monitor_op *mop) · line 83

Quoted 3 lines
xen/common/monitor.c +3 -0
+    case XEN_DOMCTL_MONITOR_EVENT_GUEST_USERSPACE_VMCALL:
+    {
+        bool_t old_status = d->monitor.guest_request_enabled;

and bool here.

All can be fixed on commit, if there are no other comments/concerns.

~Andrew

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

#2 lore raw
In reply to Alexandru Isaila
Quoted 14 lines
Quoted 1 line
Quoted 1 line

Alexandru Isaila <[email protected]> 08/03/17 5:29 PM >>>

xen/arch/x86/hvm/hypercall.c +0 -0
@@ -152,9 +152,17 @@ int hvm_hypercall(struct cpu_user_regs *regs)
 {

case 8:
eax = regs->rax;

xen/arch/x86/hvm/hypercall.c +4 -0
+        if ( currd->monitor.guest_request_userspace_vmcall &&
+             eax == __HYPERVISOR_hvm_op &&
+             regs->rdi == HVMOP_guest_request_vm_event )
+            break;

/* Fallthrough to permission check. */
case 4:
case 2:

xen/arch/x86/hvm/hypercall.c int hvm_hypercall(struct cpu_user_regs *regs) · line 165

Quoted 4 lines
xen/arch/x86/hvm/hypercall.c +4 -0
+        if ( mode != 8 && currd->monitor.guest_request_userspace_vmcall &&
+             eax == __HYPERVISOR_hvm_op &&
+             regs->ebx == HVMOP_guest_request_vm_event )
+            break;

Let's limit ugliness and redundancy as much as possible:

if ( currd->monitor.guest_request_userspace_vmcall &&

    eax == __HYPERVISOR_hvm_op &&
    (mode == 8 ? regs->rdi : regs->ebx) == HVMOP_guest_request_vm_event )

with the first half above dropped altogether.

Quoted 16 lines
xen/include/public/domctl.h +0 -10
@@ -1073,16 +1073,17 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t);
 #define XEN_DOMCTL_MONITOR_OP_GET_CAPABILITIES  2
 #define XEN_DOMCTL_MONITOR_OP_EMULATE_EACH_REP  3
 
-#define XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG         0
-#define XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR            1
-#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
-#define XEN_DOMCTL_MONITOR_EVENT_CPUID                 6
-#define XEN_DOMCTL_MONITOR_EVENT_PRIVILEGED_CALL       7
-#define XEN_DOMCTL_MONITOR_EVENT_INTERRUPT             8
-#define XEN_DOMCTL_MONITOR_EVENT_DESC_ACCESS           9

xen/include/public/domctl.h DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t); · line 1085

Quoted 10 lines
xen/include/public/domctl.h +10 -0
+#define XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG          0
+#define XEN_DOMCTL_MONITOR_EVENT_MOV_TO_MSR             1
+#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
+#define XEN_DOMCTL_MONITOR_EVENT_CPUID                  6
+#define XEN_DOMCTL_MONITOR_EVENT_PRIVILEGED_CALL        7
+#define XEN_DOMCTL_MONITOR_EVENT_INTERRUPT              8
+#define XEN_DOMCTL_MONITOR_EVENT_DESC_ACCESS            9

This isn't the first time I see this whole block being re-indented. I'd suggest to either increase indentation to a maximum (i.e. for the right side to be just below 80 cols) or to accept extremely long entries to stand out.

+#define XEN_DOMCTL_MONITOR_EVENT_GUEST_USERSPACE_VMCALL 10

I dislike the mention of VMCALL (which is an insn mnemonic after all) here, and I also think the name suggests broader access than is actually being granted. Realizing the redundancy I'd still think XEN_DOMCTL_MONITOR_EVENT_GUEST_USERSPACE_EVENT would be better

xen/include/xen/sched.h struct domain · line 485

Quoted 11 lines
xen/include/xen/sched.h +3 -2
@@ -480,8 +480,9 @@ struct domain
 
 /* Common monitor options */
 struct {
-        unsigned int guest_request_enabled       : 1;
-        unsigned int guest_request_sync          : 1;
+        unsigned int guest_request_enabled          : 1;
+        unsigned int guest_request_sync             : 1;
+        unsigned int guest_request_userspace_vmcall : 1;

Same here then.

Jan

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