Skip to contentSkip to conversation

Fixes to RDTSCP interception

PATCH Superseded Andrew Cooper 5 participants 14 messages Nov 30, 2018 – Dec 13, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fixes to RDTSCP interception

For some reason (on EPYC at least) we've gained a regression into master whereby VMs are defaulting to one of the emulated TSC modes. This may be Xen coming to the conclusion that there isn't a reliable TSC. Combined with a debug Xen, this breaks RDTSCP completely.

With this series in place and RDTSCP functioning correctly again, VMs are still unwilling to boot. I haven't managed to figure out why yet.

Andrew Cooper (2):
x86/svm: Improve diagnostics when __get_instruction_length_from_list() fails x86/hvm: Corrections to RDTSCP intercept handling

 xen/arch/x86/hvm/svm/emulate.c        | 27 ++++++++++++++++++++-------
 xen/arch/x86/hvm/svm/svm.c            | 22 +++++++++++++++++-----
 xen/arch/x86/hvm/vmx/vmx.c            |  8 ++++++++
 xen/include/asm-x86/hvm/svm/emulate.h |  1 +
 4 files changed, 46 insertions(+), 12 deletions(-)
Signature · 7 lines
2.1.4


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

PATCH 1/2 x86/svm: Improve diagnostics when __get_instruction_length_from_list() fails

Sadly, a lone:

(XEN) emulate.c:156:d2v0 __get_instruction_length_from_list: Mismatch between expected and actual instruction: eip = fffff804564139c0

on the console is of no use trying to identify what went wrong. Dump as much state as we can to help identify what went wrong.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>

RFC: __get_instruction_length_from_list() tries to cope with VMEXIT_IOIO, but IN/OUT instructions aren't in the decode list and I can't spot an entry point from the IOIO path. Am I missing something?

Also, I'm not entirely convinced that making modrm an annonymous union is going to work with older CentOS compilers, and therefore am not sure whether that part of the change is worth it. The instruction in question can be obtained from the printed INSN_ constant alone.

---
 xen/arch/x86/hvm/svm/emulate.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/svm/emulate.c +19 -7
index 3d04af0..71a1b6e 100644
@@ -56,11 +56,14 @@ static unsigned long svm_nextrip_insn_length(struct vcpu *v)
 
 static const struct {
     unsigned int opcode;
-    struct {
-        unsigned int rm:3;
-        unsigned int reg:3;
-        unsigned int mod:2;
-#define MODRM(mod, reg, rm) { rm, reg, mod }
+    union {
+        struct {
+            unsigned int rm:3;
+            unsigned int reg:3;
+            unsigned int mod:2;
+        };
+        unsigned int raw;
+#define MODRM(mod, reg, rm) {{ rm, reg, mod }}
     } modrm;
 } opc_tab[INSTR_MAX_COUNT] = {
     [INSTR_PAUSE]   = { X86EMUL_OPC_F3(0, 0x90) },
@@ -152,8 +155,17 @@ int __get_instruction_length_from_list(struct vcpu *v,
     }
 
     gdprintk(XENLOG_WARNING,
-             "%s: Mismatch between expected and actual instruction: "
-             "eip = %lx\n",  __func__, (unsigned long)vmcb->rip);
+             "%s: Mismatch between expected and actual instruction:\n",
+             __func__);
+    gdprintk(XENLOG_WARNING,
+             "  list[0] val %d, { opc %#x, modrm %#x }, list entries: %u\n",
+             list[0], opc_tab[list[0]].opcode, opc_tab[list[0]].modrm.raw,
+             list_count);
+    gdprintk(XENLOG_WARNING, "  rip 0x%lx, nextrip 0x%lx, len %lu\n",
+             vmcb->rip, vmcb->nextrip, vmcb->nextrip - vmcb->rip);
+    hvm_dump_emulation_state(XENLOG_G_WARNING, "Insn_len",
+                             &ctxt, X86EMUL_UNHANDLEABLE);
+
     hvm_inject_hw_exception(TRAP_gp_fault, 0);
     return 0;
 }
Signature · 7 lines
2.1.4


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

Re: [PATCH 1/2] x86/svm: Improve diagnostics when __get_instruction_length_from_list() fails

Quoted 9 lines · Andy Cooper

Sadly, a lone:

(XEN) emulate.c:156:d2v0 __get_instruction_length_from_list: Mismatch between expected and actual instruction: eip = fffff804564139c0

on the console is of no use trying to identify what went wrong. Dump as much state as we can to help identify what went wrong.

Signature · 6 lines
Brian Woods

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

Re: [PATCH 1/2] x86/svm: Improve diagnostics when __get_instruction_length_from_list() fails

In reply to Andrew Cooper
Quoted 32 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 30 November 2018 17:07
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monne <[email protected]>; Paul Durrant <[email protected]>; Boris Ostrovsky <[email protected]>; Suravee Suthikulpanit <[email protected]>; Brian Woods <[email protected]>
Subject: [PATCH 1/2] x86/svm: Improve diagnostics when __get_instruction_length_from_list() fails

Sadly, a lone:

(XEN) emulate.c:156:d2v0 __get_instruction_length_from_list: Mismatch
between expected and actual instruction: eip = fffff804564139c0

on the console is of no use trying to identify what went wrong. Dump as much
state as we can to help identify what went wrong.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>

Quoted 5 lines

RFC: __get_instruction_length_from_list() tries to cope with VMEXIT_IOIO, but
IN/OUT instructions aren't in the decode list and I can't spot an entry point
from the IOIO path. Am I missing something?

Yes, odd. IOIO are handled in the ifdef NDEBUG blocks but an IOIO exit does indeed not call into __get_instruction_length() but does the same calculation inline.

Quoted 6 lines

Also, I'm not entirely convinced that making modrm an annonymous union is going to work with older CentOS compilers, and therefore am not sure whether
that part of the change is worth it. The instruction in question can be obtained from the printed INSN_ constant alone.

You could just dump the bitfield values individually.

Paul

Quoted 52 lines
---
 xen/arch/x86/hvm/svm/emulate.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/svm/emulate.c +8 -5
index 3d04af0..71a1b6e 100644
@@ -56,11 +56,14 @@ static unsigned long svm_nextrip_insn_length(struct
 vcpu *v)
 
 static const struct {
     unsigned int opcode;
-    struct {
-        unsigned int rm:3;
-        unsigned int reg:3;
-        unsigned int mod:2;
-#define MODRM(mod, reg, rm) { rm, reg, mod }
+    union {
+        struct {
+            unsigned int rm:3;
+            unsigned int reg:3;
+            unsigned int mod:2;
+        };
+        unsigned int raw;
+#define MODRM(mod, reg, rm) {{ rm, reg, mod }}
     } modrm;
 } opc_tab[INSTR_MAX_COUNT] = {
     [INSTR_PAUSE]   = { X86EMUL_OPC_F3(0, 0x90) },
xen/arch/x86/hvm/svm/emulate.c +11 -2
@@ -152,8 +155,17 @@ int __get_instruction_length_from_list(struct vcpu
 *v,
     }
 
     gdprintk(XENLOG_WARNING,
-             "%s: Mismatch between expected and actual instruction: "
-             "eip = %lx\n",  __func__, (unsigned long)vmcb->rip);
+             "%s: Mismatch between expected and actual instruction:\n",
+             __func__);
+    gdprintk(XENLOG_WARNING,
+             "  list[0] val %d, { opc %#x, modrm %#x }, list entries:
 %u\n",
+             list[0], opc_tab[list[0]].opcode,
 opc_tab[list[0]].modrm.raw,
+             list_count);
+    gdprintk(XENLOG_WARNING, "  rip 0x%lx, nextrip 0x%lx, len %lu\n",
+             vmcb->rip, vmcb->nextrip, vmcb->nextrip - vmcb->rip);
+    hvm_dump_emulation_state(XENLOG_G_WARNING, "Insn_len",
+                             &ctxt, X86EMUL_UNHANDLEABLE);
+
     hvm_inject_hw_exception(TRAP_gp_fault, 0);
     return 0;
Quoted 3 lines

}
--
2.1.4

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

#4 lore raw

Re: [PATCH 1/2] x86/svm: Improve diagnostics when __get_instruction_length_from_list() fails

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

From: Andrew Cooper [mailto:[email protected]]
Sent: 30 November 2018 17:07

RFC: __get_instruction_length_from_list() tries to cope with VMEXIT_IOIO, but
IN/OUT instructions aren't in the decode list and I can't spot an entry point
from the IOIO path. Am I missing something?

Quoted 3 lines

Yes, odd. IOIO are handled in the ifdef NDEBUG blocks but an IOIO exit does indeed not call into __get_instruction_length() but does the same calculation inline.

Indeed commit 147808b8b8 introduced this without there being a visible or stated reason. Andre, any chance you can reconstruct the reason from almost 9 years ago? The __get_instruction_length() invocation the change added is VMX specific ...

Jan

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

#5 lore raw

Re: [PATCH 1/2] x86/svm: Improve diagnostics when __get_instruction_length_from_list() fails

In reply to Andrew Cooper
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 30.11.18 at 18:07, <[email protected]> wrote:

Also, I'm not entirely convinced that making modrm an annonymous union is going to work with older CentOS compilers,

It certainly won't.

Quoted 17 lines

and therefore am not sure whether
that part of the change is worth it. The instruction in question can be obtained from the printed INSN_ constant alone.

---
 xen/arch/x86/hvm/svm/emulate.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/svm/emulate.c +0 -2
index 3d04af0..71a1b6e 100644
@@ -56,11 +56,14 @@ static unsigned long svm_nextrip_insn_length(struct vcpu *v)
 
 static const struct {
     unsigned int opcode;
-    struct {
-        unsigned int rm:3;

xen/arch/x86/hvm/svm/emulate.c static unsigned long svm_nextrip_insn_length(struct vcpu *v) · line 65

Quoted 10 lines
xen/arch/x86/hvm/svm/emulate.c +7 -3
-        unsigned int reg:3;
-        unsigned int mod:2;
-#define MODRM(mod, reg, rm) { rm, reg, mod }
+    union {
+        struct {
+            unsigned int rm:3;
+            unsigned int reg:3;
+            unsigned int mod:2;
+        };
+        unsigned int raw;

Why unsigned int instead of uint8_t?

Quoted 10 lines
xen/arch/x86/hvm/svm/emulate.c +4 -2
@@ -152,8 +155,17 @@ int __get_instruction_length_from_list(struct vcpu *v,
     }
 
     gdprintk(XENLOG_WARNING,
-             "%s: Mismatch between expected and actual instruction: "
-             "eip = %lx\n",  __func__, (unsigned long)vmcb->rip);
+             "%s: Mismatch between expected and actual instruction:\n",
+             __func__);
+    gdprintk(XENLOG_WARNING,
+             "  list[0] val %d, { opc %#x, modrm %#x }, list entries: %u\n",

xen/arch/x86/hvm/svm/emulate.c int __get_instruction_length_from_list(struct vcpu *v, · line 171

Quoted 10 lines
xen/arch/x86/hvm/svm/emulate.c +7 -0
+             list[0], opc_tab[list[0]].opcode, opc_tab[list[0]].modrm.raw,
+             list_count);
+    gdprintk(XENLOG_WARNING, "  rip 0x%lx, nextrip 0x%lx, len %lu\n",
+             vmcb->rip, vmcb->nextrip, vmcb->nextrip - vmcb->rip);
+    hvm_dump_emulation_state(XENLOG_G_WARNING, "Insn_len",
+                             &ctxt, X86EMUL_UNHANDLEABLE);
+
     hvm_inject_hw_exception(TRAP_gp_fault, 0);
     return 0;
 }

The gdprintk()s all expanding to nothing in release builds I'm not fully convinced the added verbosity is worth it. In debug builds adding some debugging code like this shouldn't be a big hurdle.

In any event %#lx instead of 0x%lx please.

Jan

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

#6 lore raw

Re: [PATCH 1/2] x86/svm: Improve diagnostics when __get_instruction_length_from_list() fails

Quoted 18 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 30.11.18 at 18:07, <[email protected]> wrote:

Also, I'm not entirely convinced that making modrm an annonymous union is going to work with older CentOS compilers,

It certainly won't.

Quoted 13 lines

and therefore am not sure whether
that part of the change is worth it. The instruction in question can be obtained from the printed INSN_ constant alone.

---
 xen/arch/x86/hvm/svm/emulate.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/svm/emulate.c +0 -0
index 3d04af0..71a1b6e 100644
@@ -56,11 +56,14 @@ static unsigned long svm_nextrip_insn_length(struct vcpu *v)
 
Quoted 15 lines
Quoted 4 lines
xen/arch/x86/hvm/svm/emulate.c +0 -2
 static const struct {
     unsigned int opcode;
-    struct {
-        unsigned int rm:3;

xen/arch/x86/hvm/svm/emulate.c static unsigned long svm_nextrip_insn_length(struct vcpu *v) · line 65

Quoted 10 lines
xen/arch/x86/hvm/svm/emulate.c +7 -3
-        unsigned int reg:3;
-        unsigned int mod:2;
-#define MODRM(mod, reg, rm) { rm, reg, mod }
+    union {
+        struct {
+            unsigned int rm:3;
+            unsigned int reg:3;
+            unsigned int mod:2;
+        };
+        unsigned int raw;

Why unsigned int instead of uint8_t?

Because to being with, this was a diagnostic patch and copied the type above without thinking too much.

Quoted 17 lines
Quoted 17 lines
xen/arch/x86/hvm/svm/emulate.c +10 -2
@@ -152,8 +155,17 @@ int __get_instruction_length_from_list(struct vcpu *v,
     }
 
     gdprintk(XENLOG_WARNING,
-             "%s: Mismatch between expected and actual instruction: "
-             "eip = %lx\n",  __func__, (unsigned long)vmcb->rip);
+             "%s: Mismatch between expected and actual instruction:\n",
+             __func__);
+    gdprintk(XENLOG_WARNING,
+             "  list[0] val %d, { opc %#x, modrm %#x }, list entries: %u\n",
+             list[0], opc_tab[list[0]].opcode, opc_tab[list[0]].modrm.raw,
+             list_count);
+    gdprintk(XENLOG_WARNING, "  rip 0x%lx, nextrip 0x%lx, len %lu\n",
+             vmcb->rip, vmcb->nextrip, vmcb->nextrip - vmcb->rip);
+    hvm_dump_emulation_state(XENLOG_G_WARNING, "Insn_len",
+                             &ctxt, X86EMUL_UNHANDLEABLE);
Quoted 8 lines

xen/arch/x86/hvm/svm/emulate.c int __get_instruction_length_from_list(struct vcpu *v, · line 171

Quoted 4 lines
xen/arch/x86/hvm/svm/emulate.c +1 -0
+
     hvm_inject_hw_exception(TRAP_gp_fault, 0);
     return 0;
 }

The gdprintk()s all expanding to nothing in release builds I'm not fully convinced the added verbosity is worth it. In debug builds adding some debugging code like this shouldn't be a big hurdle.

You and I know what diagnostics to put here, but I think Pauls reaction to finding that message demonstrates that most others don't.  Nor should they - the peculiarities of first-gen AMD hardware needn't be mandatory knowledge for most contributors.

For these diagnostics, they are only reachable in debug builds, and this codepath is only even reachable in release builds on first-gen hardware.

However, the difference between what is currently present and this is enough information to actually diagnose the problem.

~Andrew

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

#7 lore raw

PATCH 2/2 x86/hvm: Corrections to RDTSCP intercept handling

In reply to Andrew Cooper

For both VT-x and SVM, the RDTSCP intercept will trigger if the pipeline supports the instruction, but the guest may have not have rdtscp in its featureset. Bring the vmexit handlers in line with the main emulator behaviour by optionally handing back #UD.

Next on the AMD side, if RDTSCP actually ends up being intercepted on a debug build, we first update regs->rcx, then call __get_instruction_length() asking for RDTSC. As the two instructions are different (and indeed, different lengths!), __get_instruction_length_from_list() fails and hands back a #GP fault.

This can demonstrated by putting a guest into tsc_mode="always emulate" and executing an rdtscp instruction:

(d1) --- Xen Test Framework ---
(d1) Environment: HVM 64bit (Long mode 4 levels)
(d1) Test rdtscp
(d1) TSC mode 1
(XEN) emulate.c:159:d1v0 __get_instruction_length_from_list: Mismatch between expected and actual instruction:
(XEN) emulate.c:163:d1v0 list[0] val 8, { opc 0xf0031, modrm 0 }, list entries: 1
(XEN) emulate.c:165:d1v0 rip 0x10475f, nextrip 0x104762, len 3
(XEN) Insn_len emulation failed (1): d1v0 64bit @ 0008:0010475f -> 0f 01 f9 5b 31 ff 31 c0 e9 c4 db ff ff 00 00 00 (d1) ******************************
(d1) PANIC: Unhandled exception at 0008:000000000010475f
(d1) Vec 13 #GP[0000]
(d1) ******************************

First, teach __get_instruction_length() to cope with RDTSCP, and improve svm_vmexit_do_rdtsc() to ask for the correct instruction. Move the regs->rcx adjustment into this function to ensure it gets done after we are done potentially raising faults.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>
CC: Juergen Gross <[email protected]>

There is a further 4.12 bug/regression here. For some reason, master and staging are now defaulting VMs into an emulated TSC mode. I have yet to figure out why.

---
 xen/arch/x86/hvm/svm/emulate.c        |  1 +
 xen/arch/x86/hvm/svm/svm.c            | 22 +++++++++++++++++-----
 xen/arch/x86/hvm/vmx/vmx.c            |  8 ++++++++
 xen/include/asm-x86/hvm/svm/emulate.h |  1 +
 4 files changed, 27 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/svm/emulate.c +1 -0
index 71a1b6e..0290264 100644
@@ -78,6 +78,7 @@ static const struct {
     [INSTR_STGI]    = { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 4) },
     [INSTR_CLGI]    = { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 5) },
     [INSTR_INVLPGA] = { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 7) },
+    [INSTR_RDTSCP]  = { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 7, 1) },
     [INSTR_INVD]    = { X86EMUL_OPC(0x0f, 0x08) },
     [INSTR_WBINVD]  = { X86EMUL_OPC(0x0f, 0x09) },
     [INSTR_WRMSR]   = { X86EMUL_OPC(0x0f, 0x30) },
xen/arch/x86/hvm/svm/svm.c +17 -5
index b9a8900..d8d3813 100644
@@ -2279,14 +2279,28 @@ static void svm_vmexit_do_hlt(struct vmcb_struct *vmcb,
     hvm_hlt(regs->eflags);
 }
 
-static void svm_vmexit_do_rdtsc(struct cpu_user_regs *regs)
+static void svm_vmexit_do_rdtsc(struct cpu_user_regs *regs, bool rdtscp)
 {
+    struct vcpu *curr = current;
+    const struct domain *currd = curr->domain;
+    enum instruction_index insn = rdtscp ? INSTR_RDTSCP : INSTR_RDTSC;
     unsigned int inst_len;
 
-    if ( (inst_len = __get_instruction_length(current, INSTR_RDTSC)) == 0 )
+    if ( rdtscp && !currd->arch.cpuid->extd.rdtscp &&
+         currd->arch.tsc_mode != TSC_MODE_PVRDTSCP )
+    {
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         return;
+    }
+
+    if ( (inst_len = __get_instruction_length(curr, insn)) == 0 )
+        return;
+
     __update_guest_eip(regs, inst_len);
 
+    if ( rdtscp )
+        regs->rcx = hvm_msr_tsc_aux(curr);
+
     hvm_rdtsc_intercept(regs);
 }
 
@@ -2968,10 +2982,8 @@ void svm_vmexit_handler(struct cpu_user_regs *regs)
         break;
 
     case VMEXIT_RDTSCP:
-        regs->rcx = hvm_msr_tsc_aux(v);
-        /* fall through */
     case VMEXIT_RDTSC:
-        svm_vmexit_do_rdtsc(regs);
+        svm_vmexit_do_rdtsc(regs, exit_reason == VMEXIT_RDTSCP);
         break;
 
     case VMEXIT_MONITOR:
xen/arch/x86/hvm/vmx/vmx.c +8 -0
index 365eeb2..a9f9b9b 100644
@@ -3589,6 +3589,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
     unsigned long exit_qualification, exit_reason, idtv_info, intr_info = 0;
     unsigned int vector = 0, mode;
     struct vcpu *v = current;
+    struct domain *currd = v->domain;
 
     __vmread(GUEST_RIP,    &regs->rip);
     __vmread(GUEST_RSP,    &regs->rsp);
@@ -3956,6 +3957,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
         vmx_invlpg_intercept(exit_qualification);
         break;
     case EXIT_REASON_RDTSCP:
+        if ( !currd->arch.cpuid->extd.rdtscp &&
+             currd->arch.tsc_mode != TSC_MODE_PVRDTSCP )
+        {
+            hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
+            break;
+        }
+
         regs->rcx = hvm_msr_tsc_aux(v);
         /* fall through */
     case EXIT_REASON_RDTSC:
xen/include/asm-x86/hvm/svm/emulate.h +1 -0
index 3de8236..ca92abb 100644
@@ -30,6 +30,7 @@ enum instruction_index {
     INSTR_HLT,
     INSTR_INT3,
     INSTR_RDTSC,
+    INSTR_RDTSCP,
     INSTR_PAUSE,
     INSTR_XSETBV,
     INSTR_VMRUN,
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] x86/hvm: Corrections to RDTSCP intercept handling

Quoted 27 lines · Andy Cooper

For both VT-x and SVM, the RDTSCP intercept will trigger if the pipeline supports the instruction, but the guest may have not have rdtscp in its featureset. Bring the vmexit handlers in line with the main emulator behaviour by optionally handing back #UD.

Next on the AMD side, if RDTSCP actually ends up being intercepted on a debug build, we first update regs->rcx, then call __get_instruction_length() asking for RDTSC. As the two instructions are different (and indeed, different lengths!), __get_instruction_length_from_list() fails and hands back a #GP fault.

This can demonstrated by putting a guest into tsc_mode="always emulate" and executing an rdtscp instruction:

(d1) --- Xen Test Framework ---
(d1) Environment: HVM 64bit (Long mode 4 levels)
(d1) Test rdtscp
(d1) TSC mode 1
(XEN) emulate.c:159:d1v0 __get_instruction_length_from_list: Mismatch between expected and actual instruction:
(XEN) emulate.c:163:d1v0 list[0] val 8, { opc 0xf0031, modrm 0 }, list entries: 1
(XEN) emulate.c:165:d1v0 rip 0x10475f, nextrip 0x104762, len 3
(XEN) Insn_len emulation failed (1): d1v0 64bit @ 0008:0010475f -> 0f 01 f9 5b 31 ff 31 c0 e9 c4 db ff ff 00 00 00 (d1) ******************************
(d1) PANIC: Unhandled exception at 0008:000000000010475f
(d1) Vec 13 #GP[0000]
(d1) ******************************

Quoted 7 lines

First, teach __get_instruction_length() to cope with RDTSCP, and improve svm_vmexit_do_rdtsc() to ask for the correct instruction. Move the regs->rcx adjustment into this function to ensure it gets done after we are done potentially raising faults.

As far as the SVM part:

Signature · 6 lines
Brian Woods

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 2/2
#9 lore raw

Re: [PATCH 2/2] x86/hvm: Corrections to RDTSCP intercept handling

In reply to Andrew Cooper
Quoted 46 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 30 November 2018 17:07
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monne <[email protected]>; Paul Durrant <[email protected]>; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>; Boris Ostrovsky <[email protected]>; Suravee Suthikulpanit <[email protected]>; Brian Woods <[email protected]>; Juergen Gross <[email protected]>
Subject: [PATCH 2/2] x86/hvm: Corrections to RDTSCP intercept handling

For both VT-x and SVM, the RDTSCP intercept will trigger if the pipeline supports the instruction, but the guest may have not have rdtscp in its featureset. Bring the vmexit handlers in line with the main emulator behaviour by optionally handing back #UD.

Next on the AMD side, if RDTSCP actually ends up being intercepted on a debug
build, we first update regs->rcx, then call __get_instruction_length() asking
for RDTSC. As the two instructions are different (and indeed, different lengths!), __get_instruction_length_from_list() fails and hands back a #GP fault.

This can demonstrated by putting a guest into tsc_mode="always emulate" and
executing an rdtscp instruction:

(d1) --- Xen Test Framework ---
(d1) Environment: HVM 64bit (Long mode 4 levels)
(d1) Test rdtscp
(d1) TSC mode 1
(XEN) emulate.c:159:d1v0 __get_instruction_length_from_list: Mismatch
between expected and actual instruction:
(XEN) emulate.c:163:d1v0 list[0] val 8, { opc 0xf0031, modrm 0 }, list
entries: 1
(XEN) emulate.c:165:d1v0 rip 0x10475f, nextrip 0x104762, len 3
(XEN) Insn_len emulation failed (1): d1v0 64bit @ 0008:0010475f -> 0f 01
f9 5b 31 ff 31 c0 e9 c4 db ff ff 00 00 00
(d1) ******************************
(d1) PANIC: Unhandled exception at 0008:000000000010475f (d1) Vec 13 #GP[0000]
(d1) ******************************

Quoted 8 lines

First, teach __get_instruction_length() to cope with RDTSCP, and improve svm_vmexit_do_rdtsc() to ask for the correct instruction. Move the regs-

Quoted 1 line

rcx

adjustment into this function to ensure it gets done after we are done potentially raising faults.

With one observation...

Quoted 89 lines

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>
CC: Juergen Gross <[email protected]>

There is a further 4.12 bug/regression here. For some reason, master and staging are now defaulting VMs into an emulated TSC mode. I have yet to figure out why.

---
 xen/arch/x86/hvm/svm/emulate.c        |  1 +
 xen/arch/x86/hvm/svm/svm.c            | 22 +++++++++++++++++-----
 xen/arch/x86/hvm/vmx/vmx.c            |  8 ++++++++
 xen/include/asm-x86/hvm/svm/emulate.h |  1 +
 4 files changed, 27 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/svm/emulate.c +1 -0
index 71a1b6e..0290264 100644
@@ -78,6 +78,7 @@ static const struct {
     [INSTR_STGI]    = { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 4) },
     [INSTR_CLGI]    = { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 5) },
     [INSTR_INVLPGA] = { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 7) },
+    [INSTR_RDTSCP]  = { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 7, 1) },
     [INSTR_INVD]    = { X86EMUL_OPC(0x0f, 0x08) },
     [INSTR_WBINVD]  = { X86EMUL_OPC(0x0f, 0x09) },
     [INSTR_WRMSR]   = { X86EMUL_OPC(0x0f, 0x30) },
xen/arch/x86/hvm/svm/svm.c +16 -2
index b9a8900..d8d3813 100644
@@ -2279,14 +2279,28 @@ static void svm_vmexit_do_hlt(struct vmcb_struct
 *vmcb,
     hvm_hlt(regs->eflags);
 }
 
-static void svm_vmexit_do_rdtsc(struct cpu_user_regs *regs)
+static void svm_vmexit_do_rdtsc(struct cpu_user_regs *regs, bool rdtscp)
 {
+    struct vcpu *curr = current;
+    const struct domain *currd = curr->domain;
+    enum instruction_index insn = rdtscp ? INSTR_RDTSCP : INSTR_RDTSC;
     unsigned int inst_len;
 
-    if ( (inst_len = __get_instruction_length(current, INSTR_RDTSC)) == 0
 )
+    if ( rdtscp && !currd->arch.cpuid->extd.rdtscp &&
+         currd->arch.tsc_mode != TSC_MODE_PVRDTSCP )
+    {
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         return;
+    }
+
+    if ( (inst_len = __get_instruction_length(curr, insn)) == 0 )
+        return;
+
     __update_guest_eip(regs, inst_len);
 
+    if ( rdtscp )
+        regs->rcx = hvm_msr_tsc_aux(curr);
+
     hvm_rdtsc_intercept(regs);

}

xen/arch/x86/hvm/svm/svm.c +1 -3
@@ -2968,10 +2982,8 @@ void svm_vmexit_handler(struct cpu_user_regs *regs)
         break;
 
     case VMEXIT_RDTSCP:
-        regs->rcx = hvm_msr_tsc_aux(v);
-        /* fall through */
     case VMEXIT_RDTSC:
-        svm_vmexit_do_rdtsc(regs);
+        svm_vmexit_do_rdtsc(regs, exit_reason == VMEXIT_RDTSCP);
         break;
 
     case VMEXIT_MONITOR:
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 365eeb2..a9f9b9b 100644

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 3593

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -3589,6 +3589,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
     unsigned long exit_qualification, exit_reason, idtv_info, intr_info =
 0;
     unsigned int vector = 0, mode;
     struct vcpu *v = current;
+    struct domain *currd = v->domain;

... following the usual rules, you should now convert all uses of v->domain in this function to use currd.

Quoted 31 lines
xen/arch/x86/hvm/vmx/vmx.c +7 -0
     __vmread(GUEST_RIP,    &regs->rip);
     __vmread(GUEST_RSP,    &regs->rsp);
@@ -3956,6 +3957,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
         vmx_invlpg_intercept(exit_qualification);
         break;
     case EXIT_REASON_RDTSCP:
+        if ( !currd->arch.cpuid->extd.rdtscp &&
+             currd->arch.tsc_mode != TSC_MODE_PVRDTSCP )
+        {
+            hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
+            break;
+        }
+
         regs->rcx = hvm_msr_tsc_aux(v);
         /* fall through */
     case EXIT_REASON_RDTSC:
xen/include/asm- renamed from xen/include/asm-x86/hvm/svm/emulate.h +0 -0

x86/hvm/svm/emulate.h
index 3de8236..ca92abb 100644

xen/include/asm-x86/hvm/svm/emulate.h +1 -1
@@ -30,6 +30,7 @@ enum instruction_index {
     INSTR_HLT,
     INSTR_INT3,
     INSTR_RDTSC,
+    INSTR_RDTSCP,
     INSTR_PAUSE,
     INSTR_XSETBV,
     INSTR_VMRUN,
--
Quoted 1 line

2.1.4

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

#10 lore raw

Re: [PATCH 2/2] x86/hvm: Corrections to RDTSCP intercept handling

Quoted 11 lines · Paul Durrant

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 3593

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -0
index 365eeb2..a9f9b9b 100644
@@ -3589,6 +3589,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
     unsigned long exit_qualification, exit_reason, idtv_info, intr_info =
 0;
     unsigned int vector = 0, mode;
     struct vcpu *v = current;
+    struct domain *currd = v->domain;

... following the usual rules, you should now convert all uses of v->domain in this function to use currd.

If this were new development work then perhaps (although it would take a series to clean vmx_vmexit_handler() up to style).

In this case however, the patch needs backporting to all the stable trees, at which point minimum perturbance is the most important aspect.

~Andrew

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

#11 lore raw

Re: [PATCH 2/2] x86/hvm: Corrections to RDTSCP intercept handling

Quoted 26 lines

-----Original Message-----
From: Andrew Cooper
Sent: 03 December 2018 11:14
To: Paul Durrant <[email protected]>; Xen-devel <xen- [email protected]>
Cc: Jan Beulich <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monne <[email protected]>; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>; Boris Ostrovsky <[email protected]>; Suravee Suthikulpanit <[email protected]>; Brian Woods <[email protected]>; Juergen Gross <[email protected]>
Subject: Re: [PATCH 2/2] x86/hvm: Corrections to RDTSCP intercept handling

Quoted 5 lines · Paul Durrant

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 365eeb2..a9f9b9b 100644
@@ -3589,6 +3589,7 @@ void vmx_vmexit_handler(struct cpu_user_regs

*regs)

Quoted 1 line

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs · line 3589

Quoted 1 line
xen/arch/x86/hvm/vmx/vmx.c +0 -0
     unsigned long exit_qualification, exit_reason, idtv_info,

intr_info =

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -0
 0;
     unsigned int vector = 0, mode;
     struct vcpu *v = current;
+    struct domain *currd = v->domain;
Quoted 8 lines
Quoted 2 lines

... following the usual rules, you should now convert all uses of v- domain in this function to use currd.

If this were new development work then perhaps (although it would take a series to clean vmx_vmexit_handler() up to style).

In this case however, the patch needs backporting to all the stable trees, at which point minimum perturbance is the most important aspect.

Fair enough.

Paul

Quoted 2 lines

~Andrew

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

Reviewed-by on v1 2/2
#12 lore raw

Re: [PATCH 2/2] x86/hvm: Corrections to RDTSCP intercept handling

In reply to Andrew Cooper
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 30.11.18 at 18:07, <[email protected]> wrote:

For both VT-x and SVM, the RDTSCP intercept will trigger if the pipeline supports the instruction, but the guest may have not have rdtscp in its featureset. Bring the vmexit handlers in line with the main emulator behaviour by optionally handing back #UD.

Next on the AMD side, if RDTSCP actually ends up being intercepted on a debug build, we first update regs->rcx, then call __get_instruction_length() asking for RDTSC. As the two instructions are different (and indeed, different lengths!), __get_instruction_length_from_list() fails and hands back a #GP fault.

This can demonstrated by putting a guest into tsc_mode="always emulate" and executing an rdtscp instruction:

(d1) --- Xen Test Framework ---
(d1) Environment: HVM 64bit (Long mode 4 levels)
(d1) Test rdtscp
(d1) TSC mode 1
(XEN) emulate.c:159:d1v0 __get_instruction_length_from_list: Mismatch between expected and actual instruction:
(XEN) emulate.c:163:d1v0 list[0] val 8, { opc 0xf0031, modrm 0 }, list entries: 1
(XEN) emulate.c:165:d1v0 rip 0x10475f, nextrip 0x104762, len 3
(XEN) Insn_len emulation failed (1): d1v0 64bit @ 0008:0010475f -> 0f 01 f9 5b 31 ff 31 c0 e9 c4 db ff ff 00 00 00 (d1) ******************************
(d1) PANIC: Unhandled exception at 0008:000000000010475f
(d1) Vec 13 #GP[0000]
(d1) ******************************

Quoted 7 lines

First, teach __get_instruction_length() to cope with RDTSCP, and improve svm_vmexit_do_rdtsc() to ask for the correct instruction. Move the regs->rcx adjustment into this function to ensure it gets done after we are done potentially raising faults.

with or without Paul's additional remark addressed.

Jan

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

Reviewed-by on v1 2/2
#13 lore raw

Re: [PATCH 2/2] x86/hvm: Corrections to RDTSCP intercept handling

In reply to Andrew Cooper
Quoted 35 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Saturday, December 1, 2018 1:07 AM

For both VT-x and SVM, the RDTSCP intercept will trigger if the pipeline supports the instruction, but the guest may have not have rdtscp in its featureset. Bring the vmexit handlers in line with the main emulator behaviour by optionally handing back #UD.

Next on the AMD side, if RDTSCP actually ends up being intercepted on a debug
build, we first update regs->rcx, then call __get_instruction_length() asking for RDTSC. As the two instructions are different (and indeed, different lengths!), __get_instruction_length_from_list() fails and hands back a #GP fault.

This can demonstrated by putting a guest into tsc_mode="always emulate" and
executing an rdtscp instruction:

(d1) --- Xen Test Framework ---
(d1) Environment: HVM 64bit (Long mode 4 levels)
(d1) Test rdtscp
(d1) TSC mode 1
(XEN) emulate.c:159:d1v0 __get_instruction_length_from_list: Mismatch
between expected and actual instruction:
(XEN) emulate.c:163:d1v0 list[0] val 8, { opc 0xf0031, modrm 0 }, list
entries: 1
(XEN) emulate.c:165:d1v0 rip 0x10475f, nextrip 0x104762, len 3
(XEN) Insn_len emulation failed (1): d1v0 64bit @ 0008:0010475f -> 0f 01
f9 5b 31 ff 31 c0 e9 c4 db ff ff 00 00 00
(d1) ******************************
(d1) PANIC: Unhandled exception at 0008:000000000010475f (d1) Vec 13 #GP[0000]
(d1) ******************************

Quoted 8 lines

First, teach __get_instruction_length() to cope with RDTSCP, and improve svm_vmexit_do_rdtsc() to ask for the correct instruction. Move the regs-

Quoted 1 line

rcx

adjustment into this function to ensure it gets done after we are done potentially raising faults.

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