Skip to contentSkip to conversation

x86/hvm: Correct emulation of invlpg instruction

PATCH In review Andrew Cooper 3 participants 10 messages Apr 22, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm: Correct emulation of invlpg instruction

invlpg and invlpga are specified to be NOPs when issued on non-canonical addresses.

These instructions are not normally intercepted. They are however intercepted for HVM guests running in shadow paging mode. AMD hardware lacking decode hardware assistance uses the general instruction emulator to handle the interception.

Alter hvmemul_invlpg() to swallow the #GP exception resulting from a non-canonical address, rather than reporting it back to the guest.

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

Note: Ideally this should be caught in the instruction emulator itself, but it is the hvmemul_virtual_to_linear() which completes the memory calculation including a possible non-zero %fs/%gs base.

---
 xen/arch/x86/hvm/emulate.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/emulate.c +20 -1
index cc0b841..897724e 100644
@@ -1598,8 +1598,27 @@ static int hvmemul_invlpg(
     rc = hvmemul_virtual_to_linear(
         seg, offset, 1, &reps, hvm_access_none, hvmemul_ctxt, &addr);
 
-    if ( rc == X86EMUL_OKAY )
+    switch ( rc )
+    {
+    case X86EMUL_OKAY:
         hvm_funcs.invlpg_intercept(addr);
+        break;
+
+    case X86EMUL_EXCEPTION:
+        ASSERT(hvmemul_ctxt->trap.vector == TRAP_gp_fault);
+        /*
+         * `invlpg` and `invlpga` are specified to be NOPs when issued on a
+         * non-canonical address.  hvmemul_virtual_to_linear() latches a #GP
+         * which is the useful behaviour for most of its callers.
+         *
+         * Clear the pending exception to match avoid delivering a #GP fault
+         * to the guest.
+         */
+        hvmemul_ctxt->exn_pending = 0;
+        hvmemul_ctxt->trap = (struct hvm_trap){};
+        rc = X86EMUL_OKAY;
+        break;
+    }
 
     return rc;
 }
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#1 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:59, <[email protected]> wrote:

invlpg and invlpga are specified to be NOPs when issued on non-canonical addresses.

These instructions are not normally intercepted. They are however intercepted
for HVM guests running in shadow paging mode. AMD hardware lacking decode hardware assistance uses the general instruction emulator to handle the interception.

Quoted 7 lines

Alter hvmemul_invlpg() to swallow the #GP exception resulting from a non-canonical address, rather than reporting it back to the guest.

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

CC Paul Durrant <[email protected]>
Quoted 9 lines
xen/arch/x86/hvm/emulate.c +2 -1
@@ -1598,8 +1598,27 @@ static int hvmemul_invlpg(
     rc = hvmemul_virtual_to_linear(
         seg, offset, 1, &reps, hvm_access_none, hvmemul_ctxt, &addr);
 
-    if ( rc == X86EMUL_OKAY )
+    switch ( rc )
+    {

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1612

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +9 -0
+    case X86EMUL_OKAY:
         hvm_funcs.invlpg_intercept(addr);
+        break;
+
+    case X86EMUL_EXCEPTION:
+        ASSERT(hvmemul_ctxt->trap.vector == TRAP_gp_fault);
+        /*
+         * `invlpg` and `invlpga` are specified to be NOPs when issued on a
+         * non-canonical address.  hvmemul_virtual_to_linear() latches a #GP
+         * which is the useful behaviour for most of its callers.

Here and in the description I'd prefer you to not exclusively refer to non-canonical addresses - segment limit violations in 32-bit or compatibility modes are affected as much.

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1618

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+         * Clear the pending exception to match avoid delivering a #GP fault
+         * to the guest.
+         */
+        hvmemul_ctxt->exn_pending = 0;
+        hvmemul_ctxt->trap = (struct hvm_trap){};

memset() would in this case look more natural I think, but is this field really meaningful in the first place for exn_pending cleared?

Jan

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

#2 lore raw
Quoted 44 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 22 April 2016 10:31
To: Andrew Cooper
Cc: Paul Durrant; Wei Liu; Xen-devel
Subject: Re: [PATCH for-4.7] x86/hvm: Correct emulation of invlpg instruction

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:59, <[email protected]> wrote:

invlpg and invlpga are specified to be NOPs when issued on non-

canonical

Quoted 5 lines

addresses.

These instructions are not normally intercepted. They are however intercepted
for HVM guests running in shadow paging mode. AMD hardware lacking

decode

Quoted 10 lines

hardware assistance uses the general instruction emulator to handle the interception.

Alter hvmemul_invlpg() to swallow the #GP exception resulting from a non-canonical address, rather than reporting it back to the guest.

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

CC Paul Durrant <[email protected]>
Quoted 15 lines
xen/arch/x86/hvm/emulate.c +7 -1
@@ -1598,8 +1598,27 @@ static int hvmemul_invlpg(
     rc = hvmemul_virtual_to_linear(
         seg, offset, 1, &reps, hvm_access_none, hvmemul_ctxt, &addr);
 
-    if ( rc == X86EMUL_OKAY )
+    switch ( rc )
+    {
+    case X86EMUL_OKAY:
         hvm_funcs.invlpg_intercept(addr);
+        break;
+
+    case X86EMUL_EXCEPTION:
+        ASSERT(hvmemul_ctxt->trap.vector == TRAP_gp_fault);
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1612

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +4 -0
+        /*
+         * `invlpg` and `invlpga` are specified to be NOPs when issued on a
+         * non-canonical address.  hvmemul_virtual_to_linear() latches a #GP
+         * which is the useful behaviour for most of its callers.

Here and in the description I'd prefer you to not exclusively refer to non-canonical addresses - segment limit violations in 32-bit or compatibility modes are affected as much.

...in which case squashing the #GP would be incorrect, right?

Quoted 9 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1618

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +5 -0
+         * Clear the pending exception to match avoid delivering a #GP fault
+         * to the guest.
+         */
+        hvmemul_ctxt->exn_pending = 0;
+        hvmemul_ctxt->trap = (struct hvm_trap){};

memset() would in this case look more natural I think, but is this field really meaningful in the first place for exn_pending cleared?

Still probably good practise to clear the trap, but memset() would indeed be more readable.

Paul

Quoted 2 lines

Jan

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

#3 lore raw
Quoted 28 lines
Quoted 19 lines
Quoted 1 line · On 22.04.16 at 11:48, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 22 April 2016 10:31

Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:59, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +7 -1
@@ -1598,8 +1598,27 @@ static int hvmemul_invlpg(
     rc = hvmemul_virtual_to_linear(
         seg, offset, 1, &reps, hvm_access_none, hvmemul_ctxt, &addr);
 
-    if ( rc == X86EMUL_OKAY )
+    switch ( rc )
+    {
+    case X86EMUL_OKAY:
         hvm_funcs.invlpg_intercept(addr);
+        break;
+
+    case X86EMUL_EXCEPTION:
+        ASSERT(hvmemul_ctxt->trap.vector == TRAP_gp_fault);
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1612

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +4 -0
+        /*
+         * `invlpg` and `invlpga` are specified to be NOPs when issued on a
+         * non-canonical address.  hvmemul_virtual_to_linear() latches a #GP
+         * which is the useful behaviour for most of its callers.

Here and in the description I'd prefer you to not exclusively refer to non-canonical addresses - segment limit violations in 32-bit or compatibility modes are affected as much.

Quoted 1 line

...in which case squashing the #GP would be incorrect, right?

No, not according to the SDM.

Jan

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

#4 lore raw
Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 26 lines
Quoted 1 line · On 22.04.16 at 11:48, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 22 April 2016 10:31

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:59, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +2 -1
@@ -1598,8 +1598,27 @@ static int hvmemul_invlpg(
     rc = hvmemul_virtual_to_linear(
         seg, offset, 1, &reps, hvm_access_none, hvmemul_ctxt, &addr);
 
-    if ( rc == X86EMUL_OKAY )
+    switch ( rc )
+    {

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1612

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +9 -0
+    case X86EMUL_OKAY:
         hvm_funcs.invlpg_intercept(addr);
+        break;
+
+    case X86EMUL_EXCEPTION:
+        ASSERT(hvmemul_ctxt->trap.vector == TRAP_gp_fault);
+        /*
+         * `invlpg` and `invlpga` are specified to be NOPs when issued on a
+         * non-canonical address.  hvmemul_virtual_to_linear() latches a #GP
+         * which is the useful behaviour for most of its callers.

Here and in the description I'd prefer you to not exclusively refer to non-canonical addresses - segment limit violations in 32-bit or compatibility modes are affected as much.

Quoted 2 lines
Quoted 1 line

...in which case squashing the #GP would be incorrect, right?

No, not according to the SDM.

I should check and only squash a #GP(0)

#GP(sel) or #SS(sel) should not be squashed.

v2 on its way.

~Andrew

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

#5 lore raw
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:16, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 26 lines
Quoted 1 line · On 22.04.16 at 11:48, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 22 April 2016 10:31

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:59, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +2 -1
@@ -1598,8 +1598,27 @@ static int hvmemul_invlpg(
     rc = hvmemul_virtual_to_linear(
         seg, offset, 1, &reps, hvm_access_none, hvmemul_ctxt, &addr);
 
-    if ( rc == X86EMUL_OKAY )
+    switch ( rc )
+    {

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1612

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +9 -0
+    case X86EMUL_OKAY:
         hvm_funcs.invlpg_intercept(addr);
+        break;
+
+    case X86EMUL_EXCEPTION:
+        ASSERT(hvmemul_ctxt->trap.vector == TRAP_gp_fault);
+        /*
+         * `invlpg` and `invlpga` are specified to be NOPs when issued on a
+         * non-canonical address.  hvmemul_virtual_to_linear() latches a #GP
+         * which is the useful behaviour for most of its callers.

Here and in the description I'd prefer you to not exclusively refer to non-canonical addresses - segment limit violations in 32-bit or compatibility modes are affected as much.

Quoted 6 lines
Quoted 2 lines
Quoted 1 line

...in which case squashing the #GP would be incorrect, right?

No, not according to the SDM.

I should check and only squash a #GP(0)

#GP(sel) or #SS(sel) should not be squashed.

Which also can't happen here (these only occur when selectors get loaded via some the various mechanisms allowing that).

Jan

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

#6 lore raw
Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:16, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 26 lines
Quoted 1 line · On 22.04.16 at 11:48, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 22 April 2016 10:31

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:59, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +2 -1
@@ -1598,8 +1598,27 @@ static int hvmemul_invlpg(
     rc = hvmemul_virtual_to_linear(
         seg, offset, 1, &reps, hvm_access_none, hvmemul_ctxt, &addr);
 
-    if ( rc == X86EMUL_OKAY )
+    switch ( rc )
+    {

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1612

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +9 -0
+    case X86EMUL_OKAY:
         hvm_funcs.invlpg_intercept(addr);
+        break;
+
+    case X86EMUL_EXCEPTION:
+        ASSERT(hvmemul_ctxt->trap.vector == TRAP_gp_fault);
+        /*
+         * `invlpg` and `invlpga` are specified to be NOPs when issued on a
+         * non-canonical address.  hvmemul_virtual_to_linear() latches a #GP
+         * which is the useful behaviour for most of its callers.

Here and in the description I'd prefer you to not exclusively refer to non-canonical addresses - segment limit violations in 32-bit or compatibility modes are affected as much.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

...in which case squashing the #GP would be incorrect, right?

No, not according to the SDM.

I should check and only squash a #GP(0)

#GP(sel) or #SS(sel) should not be squashed.

Which also can't happen here (these only occur when selectors get loaded via some the various mechanisms allowing that).

#GP(sel) or #SS(sel) also occur for a memory access which causes a segment limit violation. (SDM Vol 3, 5.3 "Limit Checking")

It is not clear whether invlpg falls into this category; it does take a memory operand, but doesn't use it in the usual manor.

~Andrew

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

#7 lore raw
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 13:18, <[email protected]> wrote:
Quoted 30 lines · Jan Beulich
Quoted 30 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:16, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 26 lines
Quoted 10 lines
Quoted 1 line · On 22.04.16 at 11:48, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 22 April 2016 10:31

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:59, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1598,8 +1598,27 @@ static int hvmemul_invlpg(
     rc = hvmemul_virtual_to_linear(
         seg, offset, 1, &reps, hvm_access_none, hvmemul_ctxt, &addr);
 
Quoted 16 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1612

Quoted 13 lines
xen/arch/x86/hvm/emulate.c +11 -1
-    if ( rc == X86EMUL_OKAY )
+    switch ( rc )
+    {
+    case X86EMUL_OKAY:
         hvm_funcs.invlpg_intercept(addr);
+        break;
+
+    case X86EMUL_EXCEPTION:
+        ASSERT(hvmemul_ctxt->trap.vector == TRAP_gp_fault);
+        /*
+         * `invlpg` and `invlpga` are specified to be NOPs when issued on a
+         * non-canonical address.  hvmemul_virtual_to_linear() latches a #GP
+         * which is the useful behaviour for most of its callers.

Here and in the description I'd prefer you to not exclusively refer to non-canonical addresses - segment limit violations in 32-bit or compatibility modes are affected as much.

Quoted 1 line

...in which case squashing the #GP would be incorrect, right?

No, not according to the SDM.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I should check and only squash a #GP(0)

#GP(sel) or #SS(sel) should not be squashed.

Which also can't happen here (these only occur when selectors get loaded via some the various mechanisms allowing that).

#GP(sel) or #SS(sel) also occur for a memory access which causes a segment limit violation. (SDM Vol 3, 5.3 "Limit Checking")

I can't find any mention of error codes in this section at all. Not sure which version of it you're looking at, mine is 057US. And even if it said so, I'd be 99.999% certain this is in error, as all instruction pages always say #GP(0) and #SS(0) for limit violations.

Jan

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

#8 lore raw
Quoted 37 lines · Jan Beulich
Quoted 37 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 13:18, <[email protected]> wrote:
Quoted 35 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:16, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 26 lines
Quoted 1 line · On 22.04.16 at 11:48, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 22 April 2016 10:31

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:59, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +2 -1
@@ -1598,8 +1598,27 @@ static int hvmemul_invlpg(
     rc = hvmemul_virtual_to_linear(
         seg, offset, 1, &reps, hvm_access_none, hvmemul_ctxt, &addr);
 
-    if ( rc == X86EMUL_OKAY )
+    switch ( rc )
+    {

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1612

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +9 -0
+    case X86EMUL_OKAY:
         hvm_funcs.invlpg_intercept(addr);
+        break;
+
+    case X86EMUL_EXCEPTION:
+        ASSERT(hvmemul_ctxt->trap.vector == TRAP_gp_fault);
+        /*
+         * `invlpg` and `invlpga` are specified to be NOPs when issued on a
+         * non-canonical address.  hvmemul_virtual_to_linear() latches a #GP
+         * which is the useful behaviour for most of its callers.

Here and in the description I'd prefer you to not exclusively refer to non-canonical addresses - segment limit violations in 32-bit or compatibility modes are affected as much.

Quoted 5 lines
Quoted 2 lines
Quoted 1 line

...in which case squashing the #GP would be incorrect, right?

No, not according to the SDM.

I should check and only squash a #GP(0)

#GP(sel) or #SS(sel) should not be squashed.

Which also can't happen here (these only occur when selectors get loaded via some the various mechanisms allowing that).

Quoted 7 lines
Quoted 2 lines

#GP(sel) or #SS(sel) also occur for a memory access which causes a segment limit violation. (SDM Vol 3, 5.3 "Limit Checking")

I can't find any mention of error codes in this section at all. Not sure which version of it you're looking at, mine is 057US. And even if it said so, I'd be 99.999% certain this is in error, as all instruction pages always say #GP(0) and #SS(0) for limit violations.

Hmm - Section 6.15 is rather more clear, and does state #GP(0) and #SS(0) for limit violations.

In which case I am going to have to untangle hvmemul_virtual_to_linear() and hvm_virtual_to_linear_addr() to distinguish the two cases, and also to raise #SS for %ss limit violations.

~Andrew

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

#9 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 15:40, <[email protected]> wrote:

Hmm - Section 6.15 is rather more clear, and does state #GP(0) and #SS(0) for limit violations.

In which case I am going to have to untangle hvmemul_virtual_to_linear() and hvm_virtual_to_linear_addr() to distinguish the two cases, and also to raise #SS for %ss limit violations.

Oh, that's a good point.

Jan

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