Skip to contentSkip to conversation

x86/kvm/emulate.c: simplify NOP (opcode 0x90) check

PATCH Superseded Denys Vlasenko 3 participants 5 messages Jul 4, 2013

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/kvm/emulate.c: simplify NOP (opcode 0x90) check

The check sits in switch() statement which itself can check for opcode 0x90 far more efficiently.

On assembler level, this change simply eliminates the following bit of code:

4c 8b a3 d8 00 00 00    mov    0xd8(%rbx),%r12
31 f6                   xor    %esi,%esi
48 89 df                mov    %rbx,%rdi
e8 32 8c ff ff          callq  490 <reg_read>
48 8d 83 78 01 00 00    lea    0x178(%rbx),%rax
83 8b 70 01 00 00 01    orl    $0x1,0x170(%rbx)
83 8b 74 01 00 00 01    orl    $0x1,0x174(%rbx)
49 39 c4                cmp    %rax,%r12
0f 84 88 fa ff ff       je     7304 <x86_emulate_insn+0x204>
CC Paolo Bonzini <[email protected]>
CC Avi Kivity <[email protected]>
---
 arch/x86/kvm/emulate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +3 -3
index 2bc1e81..7e6e74a 100644
@@ -4544,9 +4544,9 @@ special_insn:
 	case 0x8d: /* lea r16/r32, m */
 		ctxt->dst.val = ctxt->src.addr.mem.ea;
 		break;
-	case 0x90 ... 0x97: /* nop / xchg reg, rax */
-		if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
-			break;
+	case 0x90: /* nop */
+		break;
+	case 0x91 ... 0x97: /* xchg reg, rax */
 		rc = em_xchg(ctxt);
 		break;
 	case 0x98: /* cbw/cwde/cdqe */
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 28 lines · Denys Vlasenko

The check sits in switch() statement which itself can check for opcode 0x90 far more efficiently.

On assembler level, this change simply eliminates the following bit of code:

4c 8b a3 d8 00 00 00    mov    0xd8(%rbx),%r12
31 f6                   xor    %esi,%esi
48 89 df                mov    %rbx,%rdi
e8 32 8c ff ff          callq  490 <reg_read>
48 8d 83 78 01 00 00    lea    0x178(%rbx),%rax
83 8b 70 01 00 00 01    orl    $0x1,0x170(%rbx)
83 8b 74 01 00 00 01    orl    $0x1,0x174(%rbx)
49 39 c4                cmp    %rax,%r12
0f 84 88 fa ff ff       je     7304 <x86_emulate_insn+0x204>
CC Paolo Bonzini <[email protected]>
CC Avi Kivity <[email protected]>
---
 arch/x86/kvm/emulate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +0 -0
index 2bc1e81..7e6e74a 100644

arch/x86/kvm/emulate.c special_insn: · line 4548

Quoted 8 lines
arch/x86/kvm/emulate.c +2 -3
@@ -4544,9 +4544,9 @@ special_insn:
 	case 0x8d: /* lea r16/r32, m */
 		ctxt->dst.val = ctxt->src.addr.mem.ea;
 		break;
-	case 0x90 ... 0x97: /* nop / xchg reg, rax */
-		if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
-			break;
+	case 0x90: /* nop */
+		break;

This does not work on 64bit and REX prefix.

Quoted 6 lines
arch/x86/kvm/emulate.c +1 -0
+	case 0x91 ... 0x97: /* xchg reg, rax */
 		rc = em_xchg(ctxt);
 		break;
 	case 0x98: /* cbw/cwde/cdqe */
Signature · 1 line
1.8.1.4

--
Gleb.

#2 lore raw
Quoted 24 lines · Gleb Natapov
Quoted 23 lines · Denys Vlasenko

The check sits in switch() statement which itself can check for opcode 0x90 far more efficiently.

On assembler level, this change simply eliminates the following bit of code:

4c 8b a3 d8 00 00 00    mov    0xd8(%rbx),%r12
31 f6                   xor    %esi,%esi
48 89 df                mov    %rbx,%rdi
e8 32 8c ff ff          callq  490 <reg_read>
48 8d 83 78 01 00 00    lea    0x178(%rbx),%rax
83 8b 70 01 00 00 01    orl    $0x1,0x170(%rbx)
83 8b 74 01 00 00 01    orl    $0x1,0x174(%rbx)
49 39 c4                cmp    %rax,%r12
0f 84 88 fa ff ff       je     7304 <x86_emulate_insn+0x204>
CC Paolo Bonzini <[email protected]>
CC Avi Kivity <[email protected]>
---
 arch/x86/kvm/emulate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 14 lines

arch/x86/kvm/emulate.c special_insn: · line 4548

Quoted 13 lines
arch/x86/kvm/emulate.c +2 -3
index 2bc1e81..7e6e74a 100644
@@ -4544,9 +4544,9 @@ special_insn:
 	case 0x8d: /* lea r16/r32, m */
 		ctxt->dst.val = ctxt->src.addr.mem.ea;
 		break;
-	case 0x90 ... 0x97: /* nop / xchg reg, rax */
-		if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
-			break;
+	case 0x90: /* nop */
+		break;

This does not work on 64bit and REX prefix.

Can you elaborate?

0x90 is special-cased in CPU to be a NOP regardless of bit width. IOW, xchg %eax,%eax ordinarily would clear upper 32 bits of %rax, but 0x90 doesn't do that.

Do you mean that with REX.R==1, 0x90 will refer to R8?

#3 lore raw
Quoted 40 lines · Denys Vlasenko
Quoted 24 lines · Gleb Natapov
Quoted 23 lines · Denys Vlasenko

The check sits in switch() statement which itself can check for opcode 0x90 far more efficiently.

On assembler level, this change simply eliminates the following bit of code:

4c 8b a3 d8 00 00 00    mov    0xd8(%rbx),%r12
31 f6                   xor    %esi,%esi
48 89 df                mov    %rbx,%rdi
e8 32 8c ff ff          callq  490 <reg_read>
48 8d 83 78 01 00 00    lea    0x178(%rbx),%rax
83 8b 70 01 00 00 01    orl    $0x1,0x170(%rbx)
83 8b 74 01 00 00 01    orl    $0x1,0x174(%rbx)
49 39 c4                cmp    %rax,%r12
0f 84 88 fa ff ff       je     7304 <x86_emulate_insn+0x204>
CC Paolo Bonzini <[email protected]>
CC Avi Kivity <[email protected]>
---
 arch/x86/kvm/emulate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 14 lines

arch/x86/kvm/emulate.c special_insn: · line 4548

Quoted 13 lines
arch/x86/kvm/emulate.c +2 -3
index 2bc1e81..7e6e74a 100644
@@ -4544,9 +4544,9 @@ special_insn:
 	case 0x8d: /* lea r16/r32, m */
 		ctxt->dst.val = ctxt->src.addr.mem.ea;
 		break;
-	case 0x90 ... 0x97: /* nop / xchg reg, rax */
-		if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
-			break;
+	case 0x90: /* nop */
+		break;

This does not work on 64bit and REX prefix.

Quoted 7 lines

Can you elaborate?

0x90 is special-cased in CPU to be a NOP regardless of bit width. IOW, xchg %eax,%eax ordinarily would clear upper 32 bits of %rax, but 0x90 doesn't do that.

Do you mean that with REX.R==1, 0x90 will refer to R8?

Yes.

--
Gleb.

maintainer
#4 lore raw
In reply to Denys Vlasenko

Il 04/07/2013 13:06, Denys Vlasenko ha scritto:

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

arch/x86/kvm/emulate.c special_insn: · line 4548

Quoted 5 lines
arch/x86/kvm/emulate.c +2 -3
-	case 0x90 ... 0x97: /* nop / xchg reg, rax */
-		if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
-			break;
+	case 0x90: /* nop */
+		break;

This does not work on 64bit and REX prefix.

Quoted 7 lines

Can you elaborate?

0x90 is special-cased in CPU to be a NOP regardless of bit width. IOW, xchg %eax,%eax ordinarily would clear upper 32 bits of %rax, but 0x90 doesn't do that.

Do you mean that with REX.R==1, 0x90 will refer to R8?

Yes.

$ echo 'xchg %rax,%r8' | as
$ objdump -d a.out

a.out: file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <.text>:
0: 49 90 xchg %rax,%r8

Paolo