diff mbox

[XTF,05/16] vvmx: add a general error handler for VMX instructions

Message ID 20161216134348.16236-6-haozhong.zhang@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Haozhong Zhang Dec. 16, 2016, 1:43 p.m. UTC
handle_vmxinsn_err() is added to check and output the mismatch between
errors in the execution of a VMX instruction and the expected errors.

Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
---
 tests/vvmx/util.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/vvmx/util.h |  17 +++++++++
 2 files changed, 121 insertions(+)

Comments

Andrew Cooper Dec. 16, 2016, 8:16 p.m. UTC | #1
On 16/12/16 13:43, Haozhong Zhang wrote:
> handle_vmxinsn_err() is added to check and output the mismatch between
> errors in the execution of a VMX instruction and the expected errors.
>
> Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
> ---
>  tests/vvmx/util.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  tests/vvmx/util.h |  17 +++++++++
>  2 files changed, 121 insertions(+)
>
> diff --git a/tests/vvmx/util.c b/tests/vvmx/util.c
> index 8cd35c5..74b4d01 100644
> --- a/tests/vvmx/util.c
> +++ b/tests/vvmx/util.c
> @@ -2,6 +2,110 @@
>  #include <arch/x86/hvm/vmx/vmcs.h>
>  #include "util.h"
>  
> +#define vvmx_failure(prefix, fmt, ...)                       \
> +    do {                                                     \
> +        xtf_failure("Fail: %s: "fmt, prefix, ##__VA_ARGS__); \
> +    } while (0)

I don't see this used anywhere else in the series.  Is it only for the
function below?

> +
> +bool handle_vmxinsn_err(const char *name, uint8_t err, exinfo_t fault_info,
> +                        uint8_t exp_err, exinfo_t exp_fault_info,
> +                        enum vmx_insn_errno exp_insn_errno)
> +{
> +    bool passed = true;
> +
> +    bool has_fault = !!(err & VMXERR_FAULT);

C99 bools don't need !!, as they have that property guaranteed by the
standard.

It came as a shock to me when Xen's old bool_t's didn't have that
property, which is what prompted me to fix Xen.  In particular,

static inline bool_t (void) { return x & 0x1000; }

used to truncate to 0 regardless of the value of x, which was
desperately unhelpful.

> +    unsigned int fault_vec = exinfo_vec(fault_info);
> +    unsigned int fault_ec = exinfo_ec(fault_info);
> +    bool exp_fault = !!(exp_err & VMXERR_FAULT);
> +    unsigned int exp_fault_vec = exinfo_vec(exp_fault_info);
> +    bool ec = !!(exp_fault_vec & X86_EXC_HAVE_EC);
> +    unsigned int exp_fault_ec = ec ? exinfo_ec(exp_fault_info) : 0;
> +
> +    if ( !exp_fault && has_fault )
> +    {
> +        vvmx_failure(name,
> +                     "unexpected fault #%u(%u), but no fault is expected\n",
> +                     fault_vec, fault_ec);

Please use x86_decode_exinfo(), which formats an exinfo_t with mnemonics.

I already have half a patch series to introduce a %p for exinfo_t.  I
should clean the series up and post it.

~Andrew
Haozhong Zhang Dec. 19, 2016, 3:24 a.m. UTC | #2
On 12/16/16 20:16 +0000, Andrew Cooper wrote:
>On 16/12/16 13:43, Haozhong Zhang wrote:
>> handle_vmxinsn_err() is added to check and output the mismatch between
>> errors in the execution of a VMX instruction and the expected errors.
>>
>> Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
>> ---
>>  tests/vvmx/util.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>  tests/vvmx/util.h |  17 +++++++++
>>  2 files changed, 121 insertions(+)
>>
>> diff --git a/tests/vvmx/util.c b/tests/vvmx/util.c
>> index 8cd35c5..74b4d01 100644
>> --- a/tests/vvmx/util.c
>> +++ b/tests/vvmx/util.c
>> @@ -2,6 +2,110 @@
>>  #include <arch/x86/hvm/vmx/vmcs.h>
>>  #include "util.h"
>>
>> +#define vvmx_failure(prefix, fmt, ...)                       \
>> +    do {                                                     \
>> +        xtf_failure("Fail: %s: "fmt, prefix, ##__VA_ARGS__); \
>> +    } while (0)
>
>I don't see this used anywhere else in the series.  Is it only for the
>function below?
>

Yes, only for handle_vmxinsn_err().

>> +
>> +bool handle_vmxinsn_err(const char *name, uint8_t err, exinfo_t fault_info,
>> +                        uint8_t exp_err, exinfo_t exp_fault_info,
>> +                        enum vmx_insn_errno exp_insn_errno)
>> +{
>> +    bool passed = true;
>> +
>> +    bool has_fault = !!(err & VMXERR_FAULT);
>
>C99 bools don't need !!, as they have that property guaranteed by the
>standard.
>

I'll remove !! in all these patches.

>It came as a shock to me when Xen's old bool_t's didn't have that
>property, which is what prompted me to fix Xen.  In particular,
>
>static inline bool_t (void) { return x & 0x1000; }
>
>used to truncate to 0 regardless of the value of x, which was
>desperately unhelpful.
>
>> +    unsigned int fault_vec = exinfo_vec(fault_info);
>> +    unsigned int fault_ec = exinfo_ec(fault_info);
>> +    bool exp_fault = !!(exp_err & VMXERR_FAULT);
>> +    unsigned int exp_fault_vec = exinfo_vec(exp_fault_info);
>> +    bool ec = !!(exp_fault_vec & X86_EXC_HAVE_EC);
>> +    unsigned int exp_fault_ec = ec ? exinfo_ec(exp_fault_info) : 0;
>> +
>> +    if ( !exp_fault && has_fault )
>> +    {
>> +        vvmx_failure(name,
>> +                     "unexpected fault #%u(%u), but no fault is expected\n",
>> +                     fault_vec, fault_ec);
>
>Please use x86_decode_exinfo(), which formats an exinfo_t with mnemonics.
>

Sure, will change.

>I already have half a patch series to introduce a %p for exinfo_t.  I
>should clean the series up and post it.
>
>~Andrew

Thanks,
Haozhong
diff mbox

Patch

diff --git a/tests/vvmx/util.c b/tests/vvmx/util.c
index 8cd35c5..74b4d01 100644
--- a/tests/vvmx/util.c
+++ b/tests/vvmx/util.c
@@ -2,6 +2,110 @@ 
 #include <arch/x86/hvm/vmx/vmcs.h>
 #include "util.h"
 
+#define vvmx_failure(prefix, fmt, ...)                       \
+    do {                                                     \
+        xtf_failure("Fail: %s: "fmt, prefix, ##__VA_ARGS__); \
+    } while (0)
+
+bool handle_vmxinsn_err(const char *name, uint8_t err, exinfo_t fault_info,
+                        uint8_t exp_err, exinfo_t exp_fault_info,
+                        enum vmx_insn_errno exp_insn_errno)
+{
+    bool passed = true;
+
+    bool has_fault = !!(err & VMXERR_FAULT);
+    unsigned int fault_vec = exinfo_vec(fault_info);
+    unsigned int fault_ec = exinfo_ec(fault_info);
+    bool exp_fault = !!(exp_err & VMXERR_FAULT);
+    unsigned int exp_fault_vec = exinfo_vec(exp_fault_info);
+    bool ec = !!(exp_fault_vec & X86_EXC_HAVE_EC);
+    unsigned int exp_fault_ec = ec ? exinfo_ec(exp_fault_info) : 0;
+
+    if ( !exp_fault && has_fault )
+    {
+        vvmx_failure(name,
+                     "unexpected fault #%u(%u), but no fault is expected\n",
+                     fault_vec, fault_ec);
+        passed = false;
+    }
+    else if ( exp_fault && !has_fault )
+    {
+        vvmx_failure(name, "no fault, but fault #%u(%u) is expected\n",
+                     exp_fault_vec, exp_fault_ec);
+        passed = false;
+    }
+    else if ( has_fault &&
+              (fault_vec != exp_fault_vec || (ec && fault_ec != exp_fault_ec)) )
+    {
+        vvmx_failure(name,
+                     "unexpected fault #%u(%u), but #%u(%u) is expected\n",
+                     fault_vec, fault_ec, exp_fault_vec, exp_fault_ec);
+        passed = false;
+    }
+
+    /* VMfail bits are not set by VMX instructions when a fault happens */
+    if ( has_fault )
+        goto out;
+
+    bool has_vmfail_valid = !!(err & VMXERR_VMFAIL_VALID);
+    bool exp_vmfail_valid = !!(exp_err & VMXERR_VMFAIL_VALID);
+    bool has_insn_errno = false;
+    uint64_t insn_errno = 0;
+
+    if ( has_vmfail_valid )
+    {
+        has_insn_errno = !!vmread(VM_INSTRUCTION_ERROR, &insn_errno, NULL);
+
+        if ( has_insn_errno )
+        {
+            vvmx_failure(name,
+                         "unexpected error when vmread VM_INSTRUCTION_ERROR\n");
+            passed = false;
+        }
+    }
+
+    if ( !exp_vmfail_valid && has_vmfail_valid )
+    {
+        vvmx_failure(name, "unexpected VMfailvalid(%d), "
+                     "but no VMfailvalid is expected\n",
+                     (enum vmx_insn_errno)insn_errno);
+        passed = false;
+    }
+    else if ( exp_vmfail_valid && !has_vmfail_valid )
+    {
+        vvmx_failure(name, "no VMfailvalid, but VMfailvalid(%d) is expected\n",
+                     exp_insn_errno);
+        passed = false;
+    }
+    else if ( has_vmfail_valid &&
+              has_insn_errno && insn_errno != exp_insn_errno )
+    {
+        vvmx_failure(name, "unexpected VMfailvalid(%d), "
+                     "but VMfailvalid(%d) is expected\n",
+                     (enum vmx_insn_errno)insn_errno, exp_insn_errno);
+        passed = false;
+    }
+
+    bool has_vmfail_invalid = !!(err & VMXERR_VMFAIL_INVALID);
+    bool exp_vmfail_invalid = !!(exp_err & VMXERR_VMFAIL_INVALID);
+
+    if ( !exp_vmfail_invalid && has_vmfail_invalid )
+    {
+        vvmx_failure(name, "unexpected VMfailInvalid, "
+                     "but no VMfailInvalid is expected\n");
+        passed = false;
+    }
+    else if ( exp_vmfail_invalid && !has_vmfail_invalid )
+    {
+        vvmx_failure(name,
+                     "no VMfailInvalid, but VMfailInvalid is expected\n");
+        passed = false;
+    }
+
+out:
+    return passed;
+}
+
 #define VMPTRLD_OPCODE  ".byte 0x0f,0xc7\n"             /* reg/opcode: /6 */
 #define VMREAD_OPCODE   ".byte 0x0f,0x78\n"
 #define VMXON_OPCODE    ".byte 0xf3,0x0f,0xc7\n"
diff --git a/tests/vvmx/util.h b/tests/vvmx/util.h
index 57d3398..f18b9cc 100644
--- a/tests/vvmx/util.h
+++ b/tests/vvmx/util.h
@@ -17,6 +17,23 @@ 
 #define VMXERR_FAULT          (1 << 2)
 
 /**
+ * A general function to check and output the mismatch between the
+ * error in the execution of a VMX instruction and the expected error.
+ *
+ * Parameters:
+ *  @name:      the name of the test and is included in the error messages
+ *  @err:       VMXERR_ flags returned by the execution of a VMX instruction
+ *  @fault:     the information of the fault if (@err & VMXERR_FAULT)
+ *  @exp_err:   expected VMXERR_ flags
+ *  @exp_fault: the information of the expected fault if (exp_err & VMXERR_FAULT)
+ *  @exp_errno: the expected VMX instruction error number if
+ *              (exp_err & VMXERR_VMFAIL_VALID)
+ */
+bool handle_vmxinsn_err(const char *name, uint8_t err, exinfo_t fault,
+                        uint8_t exp_err, exinfo_t exp_fault,
+                        enum vmx_insn_errno exp_errno);
+
+/**
  * vmxon
  *
  * Parameters: