Skip to contentSkip to conversation

x86/svm: Fix svm_nextrip_insn_length() when crossing the virtual boundary to 0

PATCH In review Andrew Cooper 4 participants 7 messages Nov 16, 2016 – Nov 21, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/svm: Fix svm_nextrip_insn_length() when crossing the virtual boundary to 0

vmcb->nextrip can legitimately be less than vmcb->rip when execution wraps back around to 0. Instead, complain if the reported length is greater than 15 and use x86_decode_insn() as a fallback.

While making changes here, fix two whitespace issues with the case labels.

---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/hvm/svm/emulate.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/svm/emulate.c +9 -6
index 36585b0..ca670bf 100644
@@ -30,18 +30,18 @@ static unsigned long svm_nextrip_insn_length(struct vcpu *v)
 {
     struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb;
 
-    if ( !cpu_has_svm_nrips || (vmcb->nextrip <= vmcb->rip) )
+    if ( !cpu_has_svm_nrips )
         return 0;
 
 #ifndef NDEBUG
     switch ( vmcb->exitcode )
     {
-    case VMEXIT_CR0_READ... VMEXIT_DR15_WRITE:
+    case VMEXIT_CR0_READ ... VMEXIT_DR15_WRITE:
         /* faults due to instruction intercepts */
         /* (exitcodes 84-95) are reserved */
     case VMEXIT_IDTR_READ ... VMEXIT_TR_WRITE:
     case VMEXIT_RDTSC ... VMEXIT_MSR:
-    case VMEXIT_VMRUN ...  VMEXIT_XSETBV:
+    case VMEXIT_VMRUN ... VMEXIT_XSETBV:
         /* ...and the rest of the #VMEXITs */
     case VMEXIT_CR0_SEL_WRITE:
     case VMEXIT_EXCEPTION_BP:
@@ -88,7 +88,8 @@ int __get_instruction_length_from_list(struct vcpu *v,
     struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb;
     struct hvm_emulate_ctxt ctxt;
     struct x86_emulate_state *state;
-    unsigned int inst_len, j, modrm_rm, modrm_reg;
+    unsigned long inst_len, j;
+    unsigned int modrm_rm, modrm_reg;
     int modrm_mod;
 
     /*
@@ -96,7 +97,9 @@ int __get_instruction_length_from_list(struct vcpu *v,
      * hardware.
      */
 #ifdef NDEBUG
-    if ( (inst_len = svm_nextrip_insn_length(v)) != 0 )
+    if ( (inst_len = svm_nextrip_insn_length(v)) > MAX_INST_LEN )
+        gprintk(XENLOG_WARNING, "NRip reported insn_len %lu\n", insn_len);
+    else if ( insn_len != 0 )
         return inst_len;
 
     if ( vmcb->exitcode == VMEXIT_IOIO )
@@ -120,7 +123,7 @@ int __get_instruction_length_from_list(struct vcpu *v,
         j = svm_nextrip_insn_length(v);
     if ( j && j != inst_len )
     {
-        gprintk(XENLOG_WARNING, "insn-len[%02x]=%u (exp %u)\n",
+        gprintk(XENLOG_WARNING, "insn-len[%02x]=%lu (exp %lu)\n",
                 ctxt.ctxt.opcode, inst_len, j);
         return j;
     }
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 16.11.16 at 11:51, <[email protected]> wrote:

vmcb->nextrip can legitimately be less than vmcb->rip when execution wraps back around to 0. Instead, complain if the reported length is greater than 15 and use x86_decode_insn() as a fallback.

While making changes here, fix two whitespace issues with the case labels.

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

#2 lore raw
Quoted 11 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 16.11.16 at 11:51, <[email protected]> wrote:

vmcb->nextrip can legitimately be less than vmcb->rip when execution wraps back around to 0. Instead, complain if the reported length is greater than 15 and use x86_decode_insn() as a fallback.

While making changes here, fix two whitespace issues with the case labels.

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

#3 lore raw
In reply to Andrew Cooper
Quoted 4 lines · Andrew Cooper

vmcb->nextrip can legitimately be less than vmcb->rip when execution wraps back around to 0. Instead, complain if the reported length is greater than 15 and use x86_decode_insn() as a fallback.

Quoted 8 lines

While making changes here, fix two whitespace issues with the case labels.

---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Wei Liu <[email protected]>

SVM maintainers, ping?

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

#4 lore raw
Quoted 4 lines · Andrew Cooper
Quoted 3 lines · Andrew Cooper

vmcb->nextrip can legitimately be less than vmcb->rip when execution wraps back around to 0. Instead, complain if the reported length is greater than 15 and use x86_decode_insn() as a fallback.

Why do we need to complain? In the case that you are addressing by this patch wouldn't that be the expected result (length>15)?

-boris

Quoted 10 lines
Quoted 9 lines

While making changes here, fix two whitespace issues with the case labels.

---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Wei Liu <[email protected]>

SVM maintainers, ping?

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

#5 lore raw
Quoted 7 lines · Boris Ostrovsky
Quoted 4 lines · Andrew Cooper
Quoted 3 lines · Andrew Cooper

vmcb->nextrip can legitimately be less than vmcb->rip when execution wraps back around to 0. Instead, complain if the reported length is greater than 15 and use x86_decode_insn() as a fallback.

Why do we need to complain? In the case that you are addressing by this patch wouldn't that be the expected result (length>15)?

No. An instruction crossing the boundary looks like:

e.g. nextrip = 0x3, rip = 0xfffffffffffffffe

As this is all evaluated in unsigned long arithmetic, nextrip - rip evaluates to 5, which is correct.

~Andrew

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

Reviewed-by
#6 lore raw
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Boris Ostrovsky
Quoted 4 lines · Andrew Cooper
Quoted 3 lines · Andrew Cooper

vmcb->nextrip can legitimately be less than vmcb->rip when execution wraps back around to 0. Instead, complain if the reported length is greater than 15 and use x86_decode_insn() as a fallback.

Quoted 8 lines
Quoted 2 lines

Why do we need to complain? In the case that you are addressing by this patch wouldn't that be the expected result (length>15)?

No. An instruction crossing the boundary looks like:

e.g. nextrip = 0x3, rip = 0xfffffffffffffffe

As this is all evaluated in unsigned long arithmetic, nextrip - rip evaluates to 5, which is correct.

Oh, right.

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