Skip to contentSkip to conversation

x86: Move the pv hypercall into C

PATCH In review Andrew Cooper 3 participants 16 messages Sep 6, 2016

lore
Share & tools
lore
#0 lore raw

v2 cover x86: Move the pv hypercall into C

I decided to dust off this series, and found it had been accumulating dust for 18 months. Oops.

There is no practical change from the point of view of guests, but:

* There is a net reduction in LoC
* There is a net reduction in compiled size (according to bloat-o-meter)
* Easier to maintain/edit/hotpatch

I think there is also now room to simplify how continuations are handled, but I have tried not to get too carried away yet.

Changes in v2:
* Mostly reviewed/acked.
* Misc corrections in patch 4

Andrew Cooper (9):
x86/hypercall: Move some of the hvm hypercall infrastructure into hypercall.h x86/pv: Support do_set_segment_base() for compat guests x86/hypercall: Move the hypercall arg tables into C
x86/pv: Implement pv_hypercall() in C
x86/hypercall: Move the hypercall tables into C
xen/multicall: Rework arch multicall handling
x86/pv: Merge the pv hypercall tables
x86/hypercall: Merge the hypercall arg tables
x86/hypercall: Reduce the size of the hypercall tables

 xen/arch/arm/traps.c               |   3 +-
 xen/arch/x86/Makefile              |   1 +
 xen/arch/x86/hvm/hvm.c             |  35 ++---
 xen/arch/x86/hypercall.c           | 297 +++++++++++++++++++++++++++++++++++++
 xen/arch/x86/trace.c               |  27 ----
 xen/arch/x86/x86_64/asm-offsets.c  |   1 -
 xen/arch/x86/x86_64/compat.c       |   1 -
 xen/arch/x86/x86_64/compat/entry.S | 185 +----------------------
 xen/arch/x86/x86_64/compat/traps.c |   2 -
 xen/arch/x86/x86_64/entry.S        | 175 +---------------------
 xen/arch/x86/x86_64/mm.c           |   3 +
 xen/arch/x86/x86_64/traps.c        |   1 +
 xen/common/kernel.c                |   6 -
 xen/common/multicall.c             |   2 +-
 xen/include/asm-arm/multicall.h    |  14 --
 xen/include/asm-x86/hypercall.h    |  65 +++++++-
 xen/include/asm-x86/multicall.h    |  72 ---------
 xen/include/xen/hypercall.h        |  18 ++-
 xen/include/xen/multicall.h        |   3 +-
 19 files changed, 403 insertions(+), 508 deletions(-)
 create mode 100644 xen/arch/x86/hypercall.c
 delete mode 100644 xen/include/asm-arm/multicall.h
 delete mode 100644 xen/include/asm-x86/multicall.h
Signature · 7 lines
2.1.4


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

PATCH v2 1/9 x86/hypercall: Move some of the hvm hypercall infrastructure into hypercall.h

It will be reused for PV hypercalls in subsequent changes.

* Rename hvm_hypercall_t to hypercall_fn_t
* Introduce hypercall_table_t

Finally, rework the #includes for hypercall.h so it may be included in isolation.

---
 xen/arch/x86/hvm/hvm.c          | 19 +++++--------------
 xen/include/asm-x86/hypercall.h | 14 +++++++++++++-
 2 files changed, 18 insertions(+), 15 deletions(-)
xen/arch/x86/hvm/hvm.c +5 -14
index 58a40b6..9002e3c 100644
@@ -4117,17 +4117,13 @@ static long hvm_physdev_op_compat32(
     }
 }
 
-typedef unsigned long hvm_hypercall_t(
-    unsigned long, unsigned long, unsigned long, unsigned long, unsigned long,
-    unsigned long);
-
 #define HYPERCALL(x)                                         \
-    [ __HYPERVISOR_ ## x ] = { (hvm_hypercall_t *) do_ ## x, \
-                               (hvm_hypercall_t *) do_ ## x }
+    [ __HYPERVISOR_ ## x ] = { (hypercall_fn_t *) do_ ## x,  \
+                               (hypercall_fn_t *) do_ ## x }
 
 #define COMPAT_CALL(x)                                       \
-    [ __HYPERVISOR_ ## x ] = { (hvm_hypercall_t *) do_ ## x, \
-                               (hvm_hypercall_t *) compat_ ## x }
+    [ __HYPERVISOR_ ## x ] = { (hypercall_fn_t *) do_ ## x,  \
+                               (hypercall_fn_t *) compat_ ## x }
 
 #define do_memory_op          hvm_memory_op
 #define compat_memory_op      hvm_memory_op_compat32
@@ -4137,10 +4133,7 @@ typedef unsigned long hvm_hypercall_t(
 #define compat_grant_table_op hvm_grant_table_op_compat32
 #define do_arch_1             paging_domctl_continuation
 
-static const struct {
-    hvm_hypercall_t *native;
-    hvm_hypercall_t *compat;
-} hvm_hypercall_table[NR_hypercalls] = {
+static const hypercall_table_t hvm_hypercall_table[NR_hypercalls] = {
     COMPAT_CALL(memory_op),
     COMPAT_CALL(grant_table_op),
     COMPAT_CALL(vcpu_op),
@@ -4172,8 +4165,6 @@ static const struct {
 #undef HYPERCALL
 #undef COMPAT_CALL
 
-extern const uint8_t hypercall_args_table[], compat_hypercall_args_table[];
-
 int hvm_do_hypercall(struct cpu_user_regs *regs)
 {
     struct vcpu *curr = current;
xen/include/asm-x86/hypercall.h +13 -1
index 945d58a..731f3a8 100644
@@ -5,9 +5,21 @@
 #ifndef __ASM_X86_HYPERCALL_H__
 #define __ASM_X86_HYPERCALL_H__
 
+#include <xen/types.h>
 #include <public/physdev.h>
+#include <public/event_channel.h>
 #include <public/arch-x86/xen-mca.h> /* for do_mca */
-#include <xen/types.h>
+
+typedef unsigned long hypercall_fn_t(
+    unsigned long, unsigned long, unsigned long,
+    unsigned long, unsigned long, unsigned long);
+
+typedef struct {
+    hypercall_fn_t *native, *compat;
+} hypercall_table_t;
+
+extern const uint8_t hypercall_args_table[NR_hypercalls],
+  compat_hypercall_args_table[NR_hypercalls];
 
 /*
  * Both do_mmuext_op() and do_mmu_update():
Signature · 7 lines
2.1.4


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

PATCH v2 2/9 x86/pv: Support do_set_segment_base() for compat guests

In reply to Andrew Cooper

set_segment_base is the only hypercall exists in only one of the two modes guests might run in; all other hypercalls are either implemented, or unimplemented in both modes.

Remove this split, by allowing do_set_segment_base() to be called in the compat hypercall path. This change will simplify the verification logic in a later change.

No behavioural change from a guests point of view.

---
 xen/arch/x86/x86_64/compat/entry.S | 2 +-
 xen/arch/x86/x86_64/mm.c           | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)
xen/arch/x86/x86_64/compat/entry.S +1 -1
index e80c53c..1b6a482 100644
@@ -456,7 +456,7 @@ ENTRY(compat_hypercall_table)
         .quad compat_update_va_mapping_otherdomain
         .quad compat_iret
         .quad compat_vcpu_op
-        .quad compat_ni_hypercall       /* 25 */
+        .quad do_set_segment_base       /* 25 */
         .quad compat_mmuext_op
         .quad compat_xsm_op
         .quad compat_nmi_op
xen/arch/x86/x86_64/mm.c +3 -0
index 7f858fb..250d3e0 100644
@@ -1031,6 +1031,9 @@ long do_set_segment_base(unsigned int which, unsigned long base)
     struct vcpu *v = current;
     long ret = 0;
 
+    if ( is_pv_32bit_vcpu(v) )
+        return -ENOSYS; /* x86/64 only. */
+
     switch ( which )
     {
     case SEGBASE_FS:
Signature · 7 lines
2.1.4


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

PATCH v2 3/9 x86/hypercall: Move the hypercall arg tables into C

In reply to Andrew Cooper

Editing (and indeed, finding) the hypercall args tables can be tricky, especially towards the end where .rept's are used to maintain the correct layout.

Move this all into C, and let the compiler do the hard work. As 0 is the default value, drop all explicit 0's.

No functional change.

---
 xen/arch/x86/Makefile              |   1 +
 xen/arch/x86/hypercall.c           | 123 +++++++++++++++++++++++++++++++++++++
 xen/arch/x86/x86_64/compat/entry.S |  51 ---------------
 xen/arch/x86/x86_64/entry.S        |  51 ---------------
 4 files changed, 124 insertions(+), 102 deletions(-)
 create mode 100644 xen/arch/x86/hypercall.c
xen/arch/x86/Makefile +1 -0
index b49d7cf..96e6a02 100644
@@ -28,6 +28,7 @@ obj-y += e820.o
 obj-y += extable.o
 obj-y += flushtlb.o
 obj-$(CONFIG_CRASH_DEBUG) += gdbstub.o
+obj-y += hypercall.o
 obj-y += i387.o
 obj-y += i8259.o
 obj-y += io_apic.o
xen/arch/x86/hypercall.c new file +123 -0

123 lines not on this page · raw message

xen/arch/x86/x86_64/compat/entry.S +0 -51

54 lines not on this page · raw message

xen/arch/x86/x86_64/entry.S +0 -51

57 lines not on this page · raw message

Signature · 7 lines
2.1.4


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

PATCH v2 4/9 x86/pv: Implement pv_hypercall() in C

In reply to Andrew Cooper

In a similar style to hvm_do_hypercall(). The C version is far easier to understand and edit than the assembly versions.

There are a few small differences however. The register clobbering values have changed (to match the HVM side), and in particular clobber the upper 32bits of 64bit arguments. The hypercall and performance counter record are reordered to increase code sharing between the 32bit and 64bit cases.

The sole callers of __trace_hypercall_entry() were the assembly code. Given the new C layout, it is more convenient to fold __trace_hypercall_entry() into pv_hypercall(), and call __trace_hypercall() directly.

Finally, pv_hypercall() will treat a NULL hypercall function pointer as -ENOSYS, allowing further cleanup.

---
CC: Jan Beulich <[email protected]>

v2:
* Use guest_mode_kernel() rather than TF_kernel_mode
* Consistent use of 32bit stores
* Don't truncate rax for 64bit domains
* Move eax return assignment into C

---
 xen/arch/x86/hypercall.c           | 120 +++++++++++++++++++++++++++++++++++++
 xen/arch/x86/trace.c               |  27 ---------
 xen/arch/x86/x86_64/asm-offsets.c  |   1 -
 xen/arch/x86/x86_64/compat/entry.S |  69 +--------------------
 xen/arch/x86/x86_64/entry.S        |  61 +------------------
 5 files changed, 124 insertions(+), 154 deletions(-)
xen/arch/x86/hypercall.c +120 -0
index 4b42f86..13a89a0 100644
@@ -17,7 +17,12 @@
  * Copyright (c) 2015,2016 Citrix Systems Ltd.
  */
 
+#include <xen/compiler.h>
 #include <xen/hypercall.h>
+#include <xen/trace.h>
+
+extern hypercall_fn_t *const hypercall_table[NR_hypercalls],
+    *const compat_hypercall_table[NR_hypercalls];
 
 #define ARGS(x, n)                              \
     [ __HYPERVISOR_ ## x ] = (n)
@@ -111,6 +116,121 @@ const uint8_t compat_hypercall_args_table[NR_hypercalls] =
 
 #undef ARGS
 
+void pv_hypercall(struct cpu_user_regs *regs)
+{
+    struct vcpu *curr = current;
+#ifndef NDEBUG
+    unsigned long old_rip = regs->rip;
+#endif
+    unsigned long eax;
+
+    ASSERT(guest_kernel_mode(curr, regs));
+
+    eax = is_pv_32bit_vcpu(curr) ? regs->_eax : regs->eax;
+
+    if ( (eax >= NR_hypercalls) || !hypercall_table[eax] )
+    {
+        regs->eax = -ENOSYS;
+        return;
+    }
+
+    if ( !is_pv_32bit_vcpu(curr) )
+    {
+        unsigned long rdi = regs->rdi;
+        unsigned long rsi = regs->rsi;
+        unsigned long rdx = regs->rdx;
+        unsigned long r10 = regs->r10;
+        unsigned long r8 = regs->r8;
+        unsigned long r9 = regs->r9;
+
+#ifndef NDEBUG
+        /* Deliberately corrupt parameter regs not used by this hypercall. */
+        switch ( hypercall_args_table[eax] )
+        {
+        case 0: rdi = 0xdeadbeefdeadf00dUL;
+        case 1: rsi = 0xdeadbeefdeadf00dUL;
+        case 2: rdx = 0xdeadbeefdeadf00dUL;
+        case 3: r10 = 0xdeadbeefdeadf00dUL;
+        case 4: r8 = 0xdeadbeefdeadf00dUL;
+        case 5: r9 = 0xdeadbeefdeadf00dUL;
+        }
+#endif
+        if ( unlikely(tb_init_done) )
+        {
+            unsigned long args[6] = { rdi, rsi, rdx, r10, r8, r9 };
+
+            __trace_hypercall(TRC_PV_HYPERCALL_V2, eax, args);
+        }
+
+        regs->eax = hypercall_table[eax](rdi, rsi, rdx, r10, r8, r9);
+
+#ifndef NDEBUG
+        if ( regs->rip == old_rip )
+        {
+            /* Deliberately corrupt parameter regs used by this hypercall. */
+            switch ( hypercall_args_table[eax] )
+            {
+            case 6: regs->r9  = 0xdeadbeefdeadf00dUL;
+            case 5: regs->r8  = 0xdeadbeefdeadf00dUL;
+            case 4: regs->r10 = 0xdeadbeefdeadf00dUL;
+            case 3: regs->rdx = 0xdeadbeefdeadf00dUL;
+            case 2: regs->rsi = 0xdeadbeefdeadf00dUL;
+            case 1: regs->rdi = 0xdeadbeefdeadf00dUL;
+            }
+        }
+#endif
+    }
+    else
+    {
+        unsigned int ebx = regs->_ebx;
+        unsigned int ecx = regs->_ecx;
+        unsigned int edx = regs->_edx;
+        unsigned int esi = regs->_esi;
+        unsigned int edi = regs->_edi;
+        unsigned int ebp = regs->_ebp;
+
+#ifndef NDEBUG
+        /* Deliberately corrupt parameter regs not used by this hypercall. */
+        switch ( compat_hypercall_args_table[eax] )
+        {
+        case 0: ebx = 0xdeadf00d;
+        case 1: ecx = 0xdeadf00d;
+        case 2: edx = 0xdeadf00d;
+        case 3: esi = 0xdeadf00d;
+        case 4: edi = 0xdeadf00d;
+        case 5: ebp = 0xdeadf00d;
+        }
+#endif
+
+        if ( unlikely(tb_init_done) )
+        {
+            unsigned long args[6] = { ebx, ecx, edx, esi, edi, ebp };
+
+            __trace_hypercall(TRC_PV_HYPERCALL_V2, eax, args);
+        }
+
+        regs->_eax = compat_hypercall_table[eax](ebx, ecx, edx, esi, edi, ebp);
+
+#ifndef NDEBUG
+        if ( regs->rip == old_rip )
+        {
+            /* Deliberately corrupt parameter regs used by this hypercall. */
+            switch ( compat_hypercall_args_table[eax] )
+            {
+            case 6: regs->_ebp = 0xdeadf00d;
+            case 5: regs->_edi = 0xdeadf00d;
+            case 4: regs->_esi = 0xdeadf00d;
+            case 3: regs->_edx = 0xdeadf00d;
+            case 2: regs->_ecx = 0xdeadf00d;
+            case 1: regs->_ebx = 0xdeadf00d;
+            }
+        }
+#endif
+    }
+
+    perfc_incr(hypercalls);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/trace.c +0 -27
index bd8596c..f11b309 100644
@@ -6,33 +6,6 @@
 #include <xen/sched.h>
 #include <xen/trace.h>
 
-void __trace_hypercall_entry(void)
-{
-    struct cpu_user_regs *regs = guest_cpu_user_regs();
-    unsigned long args[6];
-
-    if ( is_pv_32bit_vcpu(current) )
-    {
-        args[0] = regs->ebx;
-        args[1] = regs->ecx;
-        args[2] = regs->edx;
-        args[3] = regs->esi;
-        args[4] = regs->edi;
-        args[5] = regs->ebp;
-    }
-    else
-    {
-        args[0] = regs->rdi;
-        args[1] = regs->rsi;
-        args[2] = regs->rdx;
-        args[3] = regs->r10;
-        args[4] = regs->r8;
-        args[5] = regs->r9;
-    }
-
-    __trace_hypercall(TRC_PV_HYPERCALL_V2, regs->eax, args);
-}
-
 void __trace_pv_trap(int trapnr, unsigned long eip,
                      int use_error_code, unsigned error_code)
 {
xen/arch/x86/x86_64/asm-offsets.c +0 -1
index 05d2b85..64905c6 100644
@@ -153,7 +153,6 @@ void __dummy__(void)
     BLANK();
 
 #ifdef CONFIG_PERF_COUNTERS
-    DEFINE(ASM_PERFC_hypercalls, PERFC_hypercalls);
     DEFINE(ASM_PERFC_exceptions, PERFC_exceptions);
     BLANK();
 #endif
xen/arch/x86/x86_64/compat/entry.S +2 -67

83 lines not on this page · raw message

xen/arch/x86/x86_64/entry.S +2 -59

73 lines not on this page · raw message

Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v2 4/9
#5 lore raw

Re: [PATCH v2 4/9] x86/pv: Implement pv_hypercall() in C

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 06.09.16 at 12:12, <[email protected]> wrote:

In a similar style to hvm_do_hypercall(). The C version is far easier to understand and edit than the assembly versions.

There are a few small differences however. The register clobbering values have changed (to match the HVM side), and in particular clobber the upper 32bits of 64bit arguments. The hypercall and performance counter record are reordered to increase code sharing between the 32bit and 64bit cases.

Quoted 8 lines

The sole callers of __trace_hypercall_entry() were the assembly code. Given the new C layout, it is more convenient to fold __trace_hypercall_entry() into pv_hypercall(), and call __trace_hypercall() directly.

Finally, pv_hypercall() will treat a NULL hypercall function pointer as -ENOSYS, allowing further cleanup.

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

#6 lore raw

Re: [PATCH v2 4/9] x86/pv: Implement pv_hypercall() in C

In reply to Andrew Cooper
Quoted 81 lines · Andrew Cooper

In a similar style to hvm_do_hypercall(). The C version is far easier to understand and edit than the assembly versions.

There are a few small differences however. The register clobbering values have changed (to match the HVM side), and in particular clobber the upper 32bits of 64bit arguments. The hypercall and performance counter record are reordered to increase code sharing between the 32bit and 64bit cases.

The sole callers of __trace_hypercall_entry() were the assembly code. Given the new C layout, it is more convenient to fold __trace_hypercall_entry() into pv_hypercall(), and call __trace_hypercall() directly.

Finally, pv_hypercall() will treat a NULL hypercall function pointer as -ENOSYS, allowing further cleanup.

---
CC: Jan Beulich <[email protected]>

v2:
* Use guest_mode_kernel() rather than TF_kernel_mode
* Consistent use of 32bit stores
* Don't truncate rax for 64bit domains
* Move eax return assignment into C

---
 xen/arch/x86/hypercall.c           | 120 +++++++++++++++++++++++++++++++++++++
 xen/arch/x86/trace.c               |  27 ---------
 xen/arch/x86/x86_64/asm-offsets.c  |   1 -
 xen/arch/x86/x86_64/compat/entry.S |  69 +--------------------
 xen/arch/x86/x86_64/entry.S        |  61 +------------------
 5 files changed, 124 insertions(+), 154 deletions(-)
xen/arch/x86/hypercall.c +33 -0
index 4b42f86..13a89a0 100644
@@ -17,7 +17,12 @@
  * Copyright (c) 2015,2016 Citrix Systems Ltd.
  */
 
+#include <xen/compiler.h>
 #include <xen/hypercall.h>
+#include <xen/trace.h>
+
+extern hypercall_fn_t *const hypercall_table[NR_hypercalls],
+    *const compat_hypercall_table[NR_hypercalls];
 
 #define ARGS(x, n)                              \
     [ __HYPERVISOR_ ## x ] = (n)
@@ -111,6 +116,121 @@ const uint8_t compat_hypercall_args_table[NR_hypercalls] =
 
 #undef ARGS
 
+void pv_hypercall(struct cpu_user_regs *regs)
+{
+    struct vcpu *curr = current;
+#ifndef NDEBUG
+    unsigned long old_rip = regs->rip;
+#endif
+    unsigned long eax;
+
+    ASSERT(guest_kernel_mode(curr, regs));
+
+    eax = is_pv_32bit_vcpu(curr) ? regs->_eax : regs->eax;
+
+    if ( (eax >= NR_hypercalls) || !hypercall_table[eax] )
+    {
+        regs->eax = -ENOSYS;
+        return;
+    }
+
+    if ( !is_pv_32bit_vcpu(curr) )
+    {
+        unsigned long rdi = regs->rdi;
+        unsigned long rsi = regs->rsi;
+        unsigned long rdx = regs->rdx;
+        unsigned long r10 = regs->r10;
+        unsigned long r8 = regs->r8;
+        unsigned long r9 = regs->r9;
+
+#ifndef NDEBUG

xen/arch/x86/hypercall.c const uint8_t compat_hypercall_args_table[NR_hypercalls] = · line 156

Quoted 10 lines
xen/arch/x86/hypercall.c +10 -0
+        /* Deliberately corrupt parameter regs not used by this hypercall. */
+        switch ( hypercall_args_table[eax] )
+        {
+        case 0: rdi = 0xdeadbeefdeadf00dUL;
+        case 1: rsi = 0xdeadbeefdeadf00dUL;
+        case 2: rdx = 0xdeadbeefdeadf00dUL;
+        case 3: r10 = 0xdeadbeefdeadf00dUL;
+        case 4: r8 = 0xdeadbeefdeadf00dUL;
+        case 5: r9 = 0xdeadbeefdeadf00dUL;
+        }

Out of curiosity, is Coverity going to complain about the lack of /* FALLTHROUGH */ in these stanzas, or is there some magic that lets it know this is intended? (Or does it ignore DEBUG code?)

-George

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

#7 lore raw

Re: [PATCH v2 4/9] x86/pv: Implement pv_hypercall() in C

Quoted 89 lines · George Dunlap
Quoted 87 lines · Andrew Cooper

In a similar style to hvm_do_hypercall(). The C version is far easier to understand and edit than the assembly versions.

There are a few small differences however. The register clobbering values have changed (to match the HVM side), and in particular clobber the upper 32bits of 64bit arguments. The hypercall and performance counter record are reordered to increase code sharing between the 32bit and 64bit cases.

The sole callers of __trace_hypercall_entry() were the assembly code. Given the new C layout, it is more convenient to fold __trace_hypercall_entry() into pv_hypercall(), and call __trace_hypercall() directly.

Finally, pv_hypercall() will treat a NULL hypercall function pointer as -ENOSYS, allowing further cleanup.

---
CC: Jan Beulich <[email protected]>

v2:
* Use guest_mode_kernel() rather than TF_kernel_mode
* Consistent use of 32bit stores
* Don't truncate rax for 64bit domains
* Move eax return assignment into C

---
 xen/arch/x86/hypercall.c           | 120 +++++++++++++++++++++++++++++++++++++
 xen/arch/x86/trace.c               |  27 ---------
 xen/arch/x86/x86_64/asm-offsets.c  |   1 -
 xen/arch/x86/x86_64/compat/entry.S |  69 +--------------------
 xen/arch/x86/x86_64/entry.S        |  61 +------------------
 5 files changed, 124 insertions(+), 154 deletions(-)
xen/arch/x86/hypercall.c +39 -0
index 4b42f86..13a89a0 100644
@@ -17,7 +17,12 @@
  * Copyright (c) 2015,2016 Citrix Systems Ltd.
  */
 
+#include <xen/compiler.h>
 #include <xen/hypercall.h>
+#include <xen/trace.h>
+
+extern hypercall_fn_t *const hypercall_table[NR_hypercalls],
+    *const compat_hypercall_table[NR_hypercalls];
 
 #define ARGS(x, n)                              \
     [ __HYPERVISOR_ ## x ] = (n)
@@ -111,6 +116,121 @@ const uint8_t compat_hypercall_args_table[NR_hypercalls] =
 
 #undef ARGS
 
+void pv_hypercall(struct cpu_user_regs *regs)
+{
+    struct vcpu *curr = current;
+#ifndef NDEBUG
+    unsigned long old_rip = regs->rip;
+#endif
+    unsigned long eax;
+
+    ASSERT(guest_kernel_mode(curr, regs));
+
+    eax = is_pv_32bit_vcpu(curr) ? regs->_eax : regs->eax;
+
+    if ( (eax >= NR_hypercalls) || !hypercall_table[eax] )
+    {
+        regs->eax = -ENOSYS;
+        return;
+    }
+
+    if ( !is_pv_32bit_vcpu(curr) )
+    {
+        unsigned long rdi = regs->rdi;
+        unsigned long rsi = regs->rsi;
+        unsigned long rdx = regs->rdx;
+        unsigned long r10 = regs->r10;
+        unsigned long r8 = regs->r8;
+        unsigned long r9 = regs->r9;
+
+#ifndef NDEBUG
+        /* Deliberately corrupt parameter regs not used by this hypercall. */
+        switch ( hypercall_args_table[eax] )
+        {
+        case 0: rdi = 0xdeadbeefdeadf00dUL;
+        case 1: rsi = 0xdeadbeefdeadf00dUL;
+        case 2: rdx = 0xdeadbeefdeadf00dUL;
Quoted 8 lines

xen/arch/x86/hypercall.c const uint8_t compat_hypercall_args_table[NR_hypercalls] = · line 156

Quoted 4 lines
xen/arch/x86/hypercall.c +4 -0
+        case 3: r10 = 0xdeadbeefdeadf00dUL;
+        case 4: r8 = 0xdeadbeefdeadf00dUL;
+        case 5: r9 = 0xdeadbeefdeadf00dUL;
+        }

Out of curiosity, is Coverity going to complain about the lack of /* FALLTHROUGH */ in these stanzas, or is there some magic that lets it know this is intended? (Or does it ignore DEBUG code?) From the docs:

Because there are many reasons why a case intentionally does not end with a break statement, the MISSING_BREAK checker does not report defects in cases that:

*

Are followed by a case that starts with a break.

*

End with a comment. The checker assumes that this comment is acknowledging a fallthrough. The comment can start anywhere on the last line, or be a multi-line C comment.

*

Are empty.

*

Have no control flow path because, for example, there is a return statement.

*

Have at least one conditional statement that contains a break statement.

*

Start and end on the same line.

*

Have a top-level statement that is a call to a function that can end the program.

*

Fall through to another case that has a similar numeric value when interpreted as ASCII. Values are considered similar when both are whitespace values (such as space, tab, or newline), or the two values are different cases (uppercase or lowercase) of the same letter.

I suspect we are hitting the "start and end on the same line" exclusion. It is worth noting that the HVM code is already like this, and passing fine.

~Andrew

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

#8 lore raw

Re: [PATCH v2 4/9] x86/pv: Implement pv_hypercall() in C

Quoted 128 lines · Andrew Cooper

On 06/09/16 18:44, George Dunlap wrote:

On Tue, Sep 6, 2016 at 11:12 AM, Andrew Cooper
<[email protected]> wrote:

In a similar style to hvm_do_hypercall(). The C version is far easier to understand and edit than the assembly versions.

There are a few small differences however. The register clobbering values have changed (to match the HVM side), and in particular clobber the upper 32bits of 64bit arguments. The hypercall and performance counter record are reordered to increase code sharing between the 32bit and 64bit cases.

The sole callers of __trace_hypercall_entry() were the assembly code. Given the new C layout, it is more convenient to fold __trace_hypercall_entry() into
pv_hypercall(), and call __trace_hypercall() directly.

Finally, pv_hypercall() will treat a NULL hypercall function pointer as -ENOSYS, allowing further cleanup.

---
CC: Jan Beulich <[email protected]>

v2:
* Use guest_mode_kernel() rather than TF_kernel_mode
* Consistent use of 32bit stores
* Don't truncate rax for 64bit domains
* Move eax return assignment into C

---
 xen/arch/x86/hypercall.c           | 120
+++++++++++++++++++++++++++++++++++++
 xen/arch/x86/trace.c               |  27 ---------
 xen/arch/x86/x86_64/asm-offsets.c  |   1 -
 xen/arch/x86/x86_64/compat/entry.S |  69 +--------------------
 xen/arch/x86/x86_64/entry.S        |  61 +------------------
 5 files changed, 124 insertions(+), 154 deletions(-)
xen/arch/x86/hypercall.c +5 -0
index 4b42f86..13a89a0 100644
@@ -17,7 +17,12 @@
  * Copyright (c) 2015,2016 Citrix Systems Ltd.
  */
 
+#include <xen/compiler.h>
 #include <xen/hypercall.h>
+#include <xen/trace.h>
+
+extern hypercall_fn_t *const hypercall_table[NR_hypercalls],
+    *const compat_hypercall_table[NR_hypercalls];
 
 #define ARGS(x, n)                              \
     [ __HYPERVISOR_ ## x ] = (n)
@@ -111,6 +116,121 @@ const uint8_t

compat_hypercall_args_table[NR_hypercalls] =

#undef ARGS

xen/arch/x86/hypercall.c +29 -0
+void pv_hypercall(struct cpu_user_regs *regs)
+{
+    struct vcpu *curr = current;
+#ifndef NDEBUG
+    unsigned long old_rip = regs->rip;
+#endif
+    unsigned long eax;
+
+    ASSERT(guest_kernel_mode(curr, regs));
+
+    eax = is_pv_32bit_vcpu(curr) ? regs->_eax : regs->eax;
+
+    if ( (eax >= NR_hypercalls) || !hypercall_table[eax] )
+    {
+        regs->eax = -ENOSYS;
+        return;
+    }
+
+    if ( !is_pv_32bit_vcpu(curr) )
+    {
+        unsigned long rdi = regs->rdi;
+        unsigned long rsi = regs->rsi;
+        unsigned long rdx = regs->rdx;
+        unsigned long r10 = regs->r10;
+        unsigned long r8 = regs->r8;
+        unsigned long r9 = regs->r9;
+
+#ifndef NDEBUG
+        /* Deliberately corrupt parameter regs not used by this hypercall.

*/

xen/arch/x86/hypercall.c +9 -0
+        switch ( hypercall_args_table[eax] )
+        {
+        case 0: rdi = 0xdeadbeefdeadf00dUL;
+        case 1: rsi = 0xdeadbeefdeadf00dUL;
+        case 2: rdx = 0xdeadbeefdeadf00dUL;
+        case 3: r10 = 0xdeadbeefdeadf00dUL;
+        case 4: r8 = 0xdeadbeefdeadf00dUL;
+        case 5: r9 = 0xdeadbeefdeadf00dUL;
+        }

Out of curiosity, is Coverity going to complain about the lack of /* FALLTHROUGH */ in these stanzas, or is there some magic that lets it know this is intended? (Or does it ignore DEBUG code?)

From the docs:

Because there are many reasons why a case intentionally does not end with a break statement, the MISSING_BREAK checker does not report defects in cases that:

Are followed by a case that starts with a break.

End with a comment. The checker assumes that this comment is acknowledging a fallthrough. The comment can start anywhere on the last line, or be a multi-line C comment.

Are empty.

Have no control flow path because, for example, there is a return statement.

Have at least one conditional statement that contains a break statement.

Start and end on the same line.

Have a top-level statement that is a call to a function that can end the program.

Quoted 8 lines

Fall through to another case that has a similar numeric value when interpreted as ASCII. Values are considered similar when both are whitespace values (such as space, tab, or newline), or the two values are different cases (uppercase or lowercase) of the same letter.

I suspect we are hitting the "start and end on the same line" exclusion. It is worth noting that the HVM code is already like this, and passing fine.

Cool, thanks.

-George

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

#9 lore raw

PATCH v2 5/9 x86/hypercall: Move the hypercall tables into C

In reply to Andrew Cooper

Editing (and indeed, finding) the hypercall tables can be tricky, especially towards the end where .rept's are used to maintain the correct layout.

Move this all into C, and let the compiler do the hard work.

To do this, xen/hypercall.h and asm-x86/hypercall.h need to contain prototypes for all the hypercalls; some were previously missing. This in turn requires some shuffling of definitions and includes.

One difference is that NULL function pointers are used instead of {,compat_}do_ni_hypercall(), which pv_hypercall() handles correctly. All ni_hypercall() infrastructure is therefore dropped.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/hvm/hvm.c             |   2 +
 xen/arch/x86/hypercall.c           | 115 ++++++++++++++++++++++++++++++++++++-
 xen/arch/x86/x86_64/compat.c       |   1 -
 xen/arch/x86/x86_64/compat/entry.S |  65 ---------------------
 xen/arch/x86/x86_64/compat/traps.c |   2 -
 xen/arch/x86/x86_64/entry.S        |  63 --------------------
 xen/arch/x86/x86_64/traps.c        |   1 +
 xen/common/kernel.c                |   6 --
 xen/include/asm-x86/hypercall.h    |  48 ++++++++++++++++
 xen/include/xen/hypercall.h        |  18 ++++--
 10 files changed, 175 insertions(+), 146 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -0
index 9002e3c..cf2248f 100644
@@ -4147,7 +4147,9 @@ static const hypercall_table_t hvm_hypercall_table[NR_hypercalls] = {
     HYPERCALL(hvm_op),
     HYPERCALL(sysctl),
     HYPERCALL(domctl),
+#ifdef CONFIG_TMEM
     HYPERCALL(tmem_op),
+#endif
     COMPAT_CALL(platform_op),
     COMPAT_CALL(mmuext_op),
     HYPERCALL(xenpmu_op),
xen/arch/x86/hypercall.c +112 -3

127 lines not on this page · raw message

xen/arch/x86/x86_64/compat.c +0 -1
index f8f633b..edc3115 100644
@@ -8,7 +8,6 @@ asm(".file \"" __FILE__ "\"");
 #include <compat/xen.h>
 #include <compat/physdev.h>
 
-DEFINE_XEN_GUEST_HANDLE(physdev_op_compat_t);
 #define physdev_op                    compat_physdev_op
 #define physdev_op_t                  physdev_op_compat_t
 #define do_physdev_op                 compat_physdev_op
xen/arch/x86/x86_64/compat/entry.S +0 -65

68 lines not on this page · raw message

xen/arch/x86/x86_64/compat/traps.c +0 -2
index a6afb26..95c5cb3 100644
@@ -329,8 +329,6 @@ long compat_set_callbacks(unsigned long event_selector,
     return 0;
 }
 
-DEFINE_XEN_GUEST_HANDLE(trap_info_compat_t);
-
 int compat_set_trap_table(XEN_GUEST_HANDLE(trap_info_compat_t) traps)
 {
     struct compat_trap_info cur;
xen/arch/x86/x86_64/entry.S +0 -63

69 lines not on this page · raw message

xen/arch/x86/x86_64/traps.c +1 -0
index 2d8ecf5..16de0be 100644
@@ -14,6 +14,7 @@
 #include <xen/nmi.h>
 #include <xen/guest_access.h>
 #include <xen/watchdog.h>
+#include <xen/hypercall.h>
 #include <asm/current.h>
 #include <asm/flushtlb.h>
 #include <asm/traps.h>
xen/common/kernel.c +0 -6
index 2d3db64..d0edb13 100644
@@ -449,12 +449,6 @@ DO(vm_assist)(unsigned int cmd, unsigned int type)
 }
 #endif
 
-DO(ni_hypercall)(void)
-{
-    /* No-op hypercall. */
-    return -ENOSYS;
-}
-
 /*
  * Local variables:
  * mode: C
xen/include/asm-x86/hypercall.h +48 -0

76 lines not on this page · raw message

xen/include/xen/hypercall.h +12 -6
index 0c8ae0e..207a0e8 100644
@@ -19,10 +19,6 @@
 #include <xsm/xsm.h>
 
 extern long
-do_ni_hypercall(
-    void);
-
-extern long
 do_sched_op(
     int cmd,
     XEN_GUEST_HANDLE_PARAM(void) arg);
@@ -137,8 +133,6 @@ do_xsm_op(
 extern long
 do_tmem_op(
     XEN_GUEST_HANDLE_PARAM(tmem_op_t) uops);
-#else
-#define do_tmem_op do_ni_hypercall
 #endif
 
 extern long
@@ -184,6 +178,18 @@ compat_set_timer_op(
     u32 lo,
     s32 hi);
 
+extern int compat_xsm_op(
+    XEN_GUEST_HANDLE_PARAM(xsm_op_t) op);
+
+extern int compat_kexec_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) uarg);
+
+extern int compat_vm_assist(unsigned int cmd, unsigned int type);
+
+DEFINE_XEN_GUEST_HANDLE(multicall_entry_compat_t);
+extern int compat_multicall(
+    XEN_GUEST_HANDLE_PARAM(multicall_entry_compat_t) call_list,
+    uint32_t nr_calls);
+
 #endif
 
 void arch_get_xen_caps(xen_capabilities_info_t *info);
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v2 5/9
#10 lore raw

Re: [PATCH v2 5/9] x86/hypercall: Move the hypercall tables into C

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 06.09.16 at 12:12, <[email protected]> wrote:

Editing (and indeed, finding) the hypercall tables can be tricky, especially towards the end where .rept's are used to maintain the correct layout.

Move this all into C, and let the compiler do the hard work.

To do this, xen/hypercall.h and asm-x86/hypercall.h need to contain prototypes
for all the hypercalls; some were previously missing. This in turn requires some shuffling of definitions and includes.

Quoted 5 lines

One difference is that NULL function pointers are used instead of {,compat_}do_ni_hypercall(), which pv_hypercall() handles correctly. All ni_hypercall() infrastructure is therefore dropped.

But please don't commit this without also committing patch 7 in the same batch.

Jan

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

#11 lore raw

Re: [PATCH v2 5/9] x86/hypercall: Move the hypercall tables into C

Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 06.09.16 at 12:12, <[email protected]> wrote:

Editing (and indeed, finding) the hypercall tables can be tricky, especially towards the end where .rept's are used to maintain the correct layout.

Move this all into C, and let the compiler do the hard work.

To do this, xen/hypercall.h and asm-x86/hypercall.h need to contain prototypes
for all the hypercalls; some were previously missing. This in turn requires some shuffling of definitions and includes.

One difference is that NULL function pointers are used instead of {,compat_}do_ni_hypercall(), which pv_hypercall() handles correctly. All ni_hypercall() infrastructure is therefore dropped.

Quoted 5 lines
Quoted 1 line

But please don't commit this without also committing patch 7 in the same batch.

With this review, the entire series is now ready. I will apply it all in one go, along with adjusting the commit message in patch 8 to refer to the changeset of patch 7.

~Andrew

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

#12 lore raw

PATCH v2 6/9 xen/multicall: Rework arch multicall handling

In reply to Andrew Cooper

The x86 multicall handling was previously some very hairy inline assembly, and is hard to follow and maintain.

Replace the existing do_multicall_call() with arch_do_multicall_call(). The x86 side needs to handle both compat and non-compat calls, so pass the full multicall state, rather than just the multicall_entry sub-structure.

On the ARM side, alter the prototype to match, but there is no resulting functional change. On the x86 side, the implementation is now in plain C.

This allows the removal of both asm/multicall.h header files.

---
 xen/arch/arm/traps.c            |  3 +-
 xen/arch/x86/hypercall.c        | 28 ++++++++++++++++
 xen/common/multicall.c          |  2 +-
 xen/include/asm-arm/multicall.h | 14 --------
 xen/include/asm-x86/multicall.h | 72 -----------------------------------------
 xen/include/xen/multicall.h     |  3 +-
 6 files changed, 33 insertions(+), 89 deletions(-)
 delete mode 100644 xen/include/asm-arm/multicall.h
 delete mode 100644 xen/include/asm-x86/multicall.h
xen/arch/arm/traps.c +2 -1
index 683bcb2..9353ee7 100644
@@ -1516,8 +1516,9 @@ static bool_t check_multicall_32bit_clean(struct multicall_entry *multi)
     return true;
 }
 
-void do_multicall_call(struct multicall_entry *multi)
+void arch_do_multicall_call(struct mc_state *state)
 {
+    struct multicall_entry *multi = &state->call;
     arm_hypercall_fn_t call = NULL;
 
     if ( multi->op >= ARRAY_SIZE(arm_hypercall_table) )
xen/arch/x86/hypercall.c +28 -0
index faff260..b814c0b 100644
@@ -340,6 +340,34 @@ void pv_hypercall(struct cpu_user_regs *regs)
     perfc_incr(hypercalls);
 }
 
+void arch_do_multicall_call(struct mc_state *state)
+{
+    if ( !is_pv_32bit_vcpu(current) )
+    {
+        struct multicall_entry *call = &state->call;
+
+        if ( (call->op < NR_hypercalls) && hypercall_table[call->op] )
+            call->result = hypercall_table[call->op](
+                call->args[0], call->args[1], call->args[2],
+                call->args[3], call->args[4], call->args[5]);
+        else
+            call->result = -ENOSYS;
+    }
+#ifdef CONFIG_COMPAT
+    else
+    {
+        struct compat_multicall_entry *call = &state->compat_call;
+
+        if ( (call->op < NR_hypercalls) && compat_hypercall_table[call->op] )
+            call->result = compat_hypercall_table[call->op](
+                call->args[0], call->args[1], call->args[2],
+                call->args[3], call->args[4], call->args[5]);
+        else
+            call->result = -ENOSYS;
+    }
+#endif
+}
+
 /*
  * Local variables:
  * mode: C
xen/common/multicall.c +1 -1
index 21661ee..524c9bf 100644
@@ -63,7 +63,7 @@ do_multicall(
 
         trace_multicall_call(&mcs->call);
 
-        do_multicall_call(&mcs->call);
+        arch_do_multicall_call(mcs);
 
 #ifndef NDEBUG
         {
xen/include/asm-arm/multicall.h deleted +0 -14
deleted file mode 100644
index b959262..0000000
@@ -1,14 +0,0 @@
-#ifndef __ASM_ARM_MULTICALL_H__
-#define __ASM_ARM_MULTICALL_H__
-
-extern void do_multicall_call(struct multicall_entry *call);
-
-#endif /* __ASM_ARM_MULTICALL_H__ */
-/*
- * Local variables:
- * mode: C
- * c-file-style: "BSD"
- * c-basic-offset: 4
- * indent-tabs-mode: nil
- * End:
- */
xen/include/asm-x86/multicall.h deleted +0 -72

72 lines not on this page · raw message

xen/include/xen/multicall.h +2 -1
index 0e8d8bb..fff15eb 100644
@@ -6,7 +6,6 @@
 #define __XEN_MULTICALL_H__
 
 #include <xen/percpu.h>
-#include <asm/multicall.h>
 #ifdef CONFIG_COMPAT
 #include <compat/xen.h>
 #endif
@@ -25,4 +24,6 @@ struct mc_state {
     };
 };
 
+void arch_do_multicall_call(struct mc_state *mc);
+
 #endif /* __XEN_MULTICALL_H__ */
Signature · 7 lines
2.1.4


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

PATCH v2 7/9 x86/pv: Merge the pv hypercall tables

In reply to Andrew Cooper

For the same reason as c/s 33a231e3f "x86/HVM: fold hypercall tables", this removes the risk of accidentally updating only one of the tables.

---
 xen/arch/x86/hypercall.c | 75 +++++++++---------------------------------------
 1 file changed, 13 insertions(+), 62 deletions(-)
xen/arch/x86/hypercall.c +13 -62
index b814c0b..4a96f23 100644
@@ -114,64 +114,15 @@ const uint8_t compat_hypercall_args_table[NR_hypercalls] =
 #undef ARGS
 
 #define HYPERCALL(x)                                                \
-    [ __HYPERVISOR_ ## x ] = (hypercall_fn_t *) do_ ## x
+    [ __HYPERVISOR_ ## x ] = { (hypercall_fn_t *) do_ ## x,         \
+                               (hypercall_fn_t *) do_ ## x }
+#define COMPAT_CALL(x)                                              \
+    [ __HYPERVISOR_ ## x ] = { (hypercall_fn_t *) do_ ## x,         \
+                               (hypercall_fn_t *) compat_ ## x }
 
 #define do_arch_1             paging_domctl_continuation
 
-hypercall_fn_t *const hypercall_table[NR_hypercalls] = {
-    HYPERCALL(set_trap_table),
-    HYPERCALL(mmu_update),
-    HYPERCALL(set_gdt),
-    HYPERCALL(stack_switch),
-    HYPERCALL(set_callbacks),
-    HYPERCALL(fpu_taskswitch),
-    HYPERCALL(sched_op_compat),
-    HYPERCALL(platform_op),
-    HYPERCALL(set_debugreg),
-    HYPERCALL(get_debugreg),
-    HYPERCALL(update_descriptor),
-    HYPERCALL(memory_op),
-    HYPERCALL(multicall),
-    HYPERCALL(update_va_mapping),
-    HYPERCALL(set_timer_op),
-    HYPERCALL(event_channel_op_compat),
-    HYPERCALL(xen_version),
-    HYPERCALL(console_io),
-    HYPERCALL(physdev_op_compat),
-    HYPERCALL(grant_table_op),
-    HYPERCALL(vm_assist),
-    HYPERCALL(update_va_mapping_otherdomain),
-    HYPERCALL(iret),
-    HYPERCALL(vcpu_op),
-    HYPERCALL(set_segment_base),
-    HYPERCALL(mmuext_op),
-    HYPERCALL(xsm_op),
-    HYPERCALL(nmi_op),
-    HYPERCALL(sched_op),
-    HYPERCALL(callback_op),
-#ifdef CONFIG_XENOPROF
-    HYPERCALL(xenoprof_op),
-#endif
-    HYPERCALL(event_channel_op),
-    HYPERCALL(physdev_op),
-    HYPERCALL(hvm_op),
-    HYPERCALL(sysctl),
-    HYPERCALL(domctl),
-#ifdef CONFIG_KEXEC
-    HYPERCALL(kexec_op),
-#endif
-#ifdef CONFIG_TMEM
-    HYPERCALL(tmem_op),
-#endif
-    HYPERCALL(xenpmu_op),
-    HYPERCALL(mca),
-    HYPERCALL(arch_1),
-};
-
-#define COMPAT_CALL(x)                                              \
-    [ __HYPERVISOR_ ## x ] = (hypercall_fn_t *) compat_ ## x
-
-hypercall_fn_t *const compat_hypercall_table[NR_hypercalls] = {
+static const hypercall_table_t pv_hypercall_table[NR_hypercalls] = {
     COMPAT_CALL(set_trap_table),
     HYPERCALL(mmu_update),
     COMPAT_CALL(set_gdt),
@@ -237,7 +188,7 @@ void pv_hypercall(struct cpu_user_regs *regs)
 
     eax = is_pv_32bit_vcpu(curr) ? regs->_eax : regs->eax;
 
-    if ( (eax >= NR_hypercalls) || !hypercall_table[eax] )
+    if ( (eax >= NR_hypercalls) || !pv_hypercall_table[eax].native )
     {
         regs->eax = -ENOSYS;
         return;
@@ -271,7 +222,7 @@ void pv_hypercall(struct cpu_user_regs *regs)
             __trace_hypercall(TRC_PV_HYPERCALL_V2, eax, args);
         }
 
-        regs->eax = hypercall_table[eax](rdi, rsi, rdx, r10, r8, r9);
+        regs->eax = pv_hypercall_table[eax].native(rdi, rsi, rdx, r10, r8, r9);
 
 #ifndef NDEBUG
         if ( regs->rip == old_rip )
@@ -318,7 +269,7 @@ void pv_hypercall(struct cpu_user_regs *regs)
             __trace_hypercall(TRC_PV_HYPERCALL_V2, eax, args);
         }
 
-        regs->_eax = compat_hypercall_table[eax](ebx, ecx, edx, esi, edi, ebp);
+        regs->_eax = pv_hypercall_table[eax].compat(ebx, ecx, edx, esi, edi, ebp);
 
 #ifndef NDEBUG
         if ( regs->rip == old_rip )
@@ -346,8 +297,8 @@ void arch_do_multicall_call(struct mc_state *state)
     {
         struct multicall_entry *call = &state->call;
 
-        if ( (call->op < NR_hypercalls) && hypercall_table[call->op] )
-            call->result = hypercall_table[call->op](
+        if ( (call->op < NR_hypercalls) && pv_hypercall_table[call->op].native )
+            call->result = pv_hypercall_table[call->op].native(
                 call->args[0], call->args[1], call->args[2],
                 call->args[3], call->args[4], call->args[5]);
         else
@@ -358,8 +309,8 @@ void arch_do_multicall_call(struct mc_state *state)
     {
         struct compat_multicall_entry *call = &state->compat_call;
 
-        if ( (call->op < NR_hypercalls) && compat_hypercall_table[call->op] )
-            call->result = compat_hypercall_table[call->op](
+        if ( (call->op < NR_hypercalls) && pv_hypercall_table[call->op].compat )
+            call->result = pv_hypercall_table[call->op].compat(
                 call->args[0], call->args[1], call->args[2],
                 call->args[3], call->args[4], call->args[5]);
         else
Signature · 7 lines
2.1.4


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

PATCH v2 8/9 x86/hypercall: Merge the hypercall arg tables

In reply to Andrew Cooper

For the same reason as c/s 33a231e3f "x86/HVM: fold hypercall tables" and (TODO - changeset) "x86/pv: Merge the pv hypercall tables", this removes the risk of accidentally updating only one of the tables.

---
 xen/arch/x86/hvm/hvm.c          |  8 ++---
 xen/arch/x86/hypercall.c        | 70 +++++++++--------------------------------
 xen/include/asm-x86/hypercall.h |  7 +++--
 3 files changed, 24 insertions(+), 61 deletions(-)
xen/arch/x86/hvm/hvm.c +4 -4
index cf2248f..23b0d17 100644
@@ -4216,7 +4216,7 @@ int hvm_do_hypercall(struct cpu_user_regs *regs)
 
 #ifndef NDEBUG
         /* Deliberately corrupt parameter regs not used by this hypercall. */
-        switch ( hypercall_args_table[eax] )
+        switch ( hypercall_args_table[eax].native )
         {
         case 0: rdi = 0xdeadbeefdeadf00dUL;
         case 1: rsi = 0xdeadbeefdeadf00dUL;
@@ -4237,7 +4237,7 @@ int hvm_do_hypercall(struct cpu_user_regs *regs)
         if ( !curr->arch.hvm_vcpu.hcall_preempted )
         {
             /* Deliberately corrupt parameter regs used by this hypercall. */
-            switch ( hypercall_args_table[eax] )
+            switch ( hypercall_args_table[eax].native )
             {
             case 6: regs->r9  = 0xdeadbeefdeadf00dUL;
             case 5: regs->r8  = 0xdeadbeefdeadf00dUL;
@@ -4263,7 +4263,7 @@ int hvm_do_hypercall(struct cpu_user_regs *regs)
 
 #ifndef NDEBUG
         /* Deliberately corrupt parameter regs not used by this hypercall. */
-        switch ( compat_hypercall_args_table[eax] )
+        switch ( hypercall_args_table[eax].compat )
         {
         case 0: ebx = 0xdeadf00d;
         case 1: ecx = 0xdeadf00d;
@@ -4281,7 +4281,7 @@ int hvm_do_hypercall(struct cpu_user_regs *regs)
         if ( !curr->arch.hvm_vcpu.hcall_preempted )
         {
             /* Deliberately corrupt parameter regs used by this hypercall. */
-            switch ( compat_hypercall_args_table[eax] )
+            switch ( hypercall_args_table[eax].compat )
             {
             case 6: regs->ebp = 0xdeadf00d;
             case 5: regs->edi = 0xdeadf00d;
xen/arch/x86/hypercall.c +15 -55

126 lines not on this page · raw message

xen/include/asm-x86/hypercall.h +5 -2
index c00de2b..c59aa69 100644
@@ -19,8 +19,11 @@ typedef struct {
     hypercall_fn_t *native, *compat;
 } hypercall_table_t;
 
-extern const uint8_t hypercall_args_table[NR_hypercalls],
-  compat_hypercall_args_table[NR_hypercalls];
+typedef struct {
+    uint8_t native, compat;
+} hypercall_args_t;
+
+extern const hypercall_args_t hypercall_args_table[NR_hypercalls];
 
 /*
  * Both do_mmuext_op() and do_mmu_update():
Signature · 7 lines
2.1.4


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

PATCH v2 9/9 x86/hypercall: Reduce the size of the hypercall tables

In reply to Andrew Cooper

The highest populated entry in each hypercall table is currently at index 49. There is no need to extend both to tables to 64 entries.

Range check eax against the hypercall table array size, and use a BUILD_BUG_ON() to ensure that the hypercall tables don't grow larger than the args table.

---
 xen/arch/x86/hvm/hvm.c   |  8 ++++++--
 xen/arch/x86/hypercall.c | 14 ++++++++++----
 2 files changed, 16 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/hvm.c +6 -2
index 23b0d17..ddb12cc 100644
@@ -4133,7 +4133,7 @@ static long hvm_physdev_op_compat32(
 #define compat_grant_table_op hvm_grant_table_op_compat32
 #define do_arch_1             paging_domctl_continuation
 
-static const hypercall_table_t hvm_hypercall_table[NR_hypercalls] = {
+static const hypercall_table_t hvm_hypercall_table[] = {
     COMPAT_CALL(memory_op),
     COMPAT_CALL(grant_table_op),
     COMPAT_CALL(vcpu_op),
@@ -4194,7 +4194,11 @@ int hvm_do_hypercall(struct cpu_user_regs *regs)
     if ( (eax & 0x80000000) && is_viridian_domain(currd) )
         return viridian_hypercall(regs);
 
-    if ( (eax >= NR_hypercalls) || !hvm_hypercall_table[eax].native )
+    BUILD_BUG_ON(ARRAY_SIZE(hvm_hypercall_table) >
+                 ARRAY_SIZE(hypercall_args_table));
+
+    if ( (eax >= ARRAY_SIZE(hvm_hypercall_table)) ||
+         !hvm_hypercall_table[eax].native )
     {
         regs->eax = -ENOSYS;
         return HVM_HCALL_completed;
xen/arch/x86/hypercall.c +10 -4
index 41be870..d2b5331 100644
@@ -82,7 +82,7 @@ const hypercall_args_t hypercall_args_table[NR_hypercalls] =
 
 #define do_arch_1             paging_domctl_continuation
 
-static const hypercall_table_t pv_hypercall_table[NR_hypercalls] = {
+static const hypercall_table_t pv_hypercall_table[] = {
     COMPAT_CALL(set_trap_table),
     HYPERCALL(mmu_update),
     COMPAT_CALL(set_gdt),
@@ -148,7 +148,11 @@ void pv_hypercall(struct cpu_user_regs *regs)
 
     eax = is_pv_32bit_vcpu(curr) ? regs->_eax : regs->eax;
 
-    if ( (eax >= NR_hypercalls) || !pv_hypercall_table[eax].native )
+    BUILD_BUG_ON(ARRAY_SIZE(pv_hypercall_table) >
+                 ARRAY_SIZE(hypercall_args_table));
+
+    if ( (eax >= ARRAY_SIZE(pv_hypercall_table)) ||
+         !pv_hypercall_table[eax].native )
     {
         regs->eax = -ENOSYS;
         return;
@@ -257,7 +261,8 @@ void arch_do_multicall_call(struct mc_state *state)
     {
         struct multicall_entry *call = &state->call;
 
-        if ( (call->op < NR_hypercalls) && pv_hypercall_table[call->op].native )
+        if ( (call->op < ARRAY_SIZE(pv_hypercall_table)) &&
+             pv_hypercall_table[call->op].native )
             call->result = pv_hypercall_table[call->op].native(
                 call->args[0], call->args[1], call->args[2],
                 call->args[3], call->args[4], call->args[5]);
@@ -269,7 +274,8 @@ void arch_do_multicall_call(struct mc_state *state)
     {
         struct compat_multicall_entry *call = &state->compat_call;
 
-        if ( (call->op < NR_hypercalls) && pv_hypercall_table[call->op].compat )
+        if ( (call->op < ARRAY_SIZE(pv_hypercall_table)) &&
+             pv_hypercall_table[call->op].compat )
             call->result = pv_hypercall_table[call->op].compat(
                 call->args[0], call->args[1], call->args[2],
                 call->args[3], call->args[4], call->args[5]);
Signature · 7 lines
2.1.4


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