Skip to contentSkip to conversation

KVM: x86: emulate reserved nops from 0f/18 to 0f/1f

PATCH In review Paolo Bonzini 2 participants 8 messages May 15, 2020 – May 19, 2020

Subsystems (3)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86: emulate reserved nops from 0f/18 to 0f/1f

Instructions starting with 0f18 up to 0f1f are reserved nops, except those that were assigned to MPX. These include the endbr markers used by CET. List them correctly in the opcode table.

---
 arch/x86/kvm/emulate.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +6 -2
index de5476f8683e..d0e2825ae617 100644
@@ -4800,8 +4800,12 @@ static const struct opcode twobyte_table[256] = {
 	GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
 	GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
 	N, N, N, N, N, N,
-	D(ImplicitOps | ModRM | SrcMem | NoAccess),
-	N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 4 * prefetch + 4 * reserved NOP */
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* NOP + 7 * reserved NOP */
 	/* 0x20 - 0x2F */
 	DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
 	DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
Signature · 1 line
2.18.2
#1 lore raw
Quoted 2 lines · Paolo Bonzini

Instructions starting with 0f18 up to 0f1f are reserved nops, except those that were assigned to MPX.

Well, they're probably reserved NOPs again :-D.

Quoted 1 line

These include the endbr markers used by CET.

And RDSPP. Wouldn't it make sense to treat RDSPP as a #UD even though it's a NOP if CET is disabled? The logic being that a sane guest will execute RDSSP iff CET is enabled, and in that case it'd be better to inject a #UD than to silently break the guest.

Extending that logic to future features, wouldn't it then make sense to keep the current #UD behavior for all opcodes to avoid silently breakage? I.e. change only the opcodes for endbr (which consume only 2 of 255 ModRMs for 0f 1e) to be NOPs.

Quoted 28 lines

List them correctly in the opcode table.

---
 arch/x86/kvm/emulate.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +6 -2
index de5476f8683e..d0e2825ae617 100644
@@ -4800,8 +4800,12 @@ static const struct opcode twobyte_table[256] = {
 	GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
 	GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
 	N, N, N, N, N, N,
-	D(ImplicitOps | ModRM | SrcMem | NoAccess),
-	N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 4 * prefetch + 4 * reserved NOP */
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
+	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* NOP + 7 * reserved NOP */
 	/* 0x20 - 0x2F */
 	DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
 	DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
Signature · 1 line
2.18.2
author maintainer
#2 lore raw
Quoted 4 lines · Sean Christopherson
Quoted 2 lines · Paolo Bonzini

Instructions starting with 0f18 up to 0f1f are reserved nops, except those that were assigned to MPX.

Well, they're probably reserved NOPs again :-D.

So are you suggesting adding them back to the list as well?

Quoted 5 lines
Quoted 1 line

These include the endbr markers used by CET.

And RDSPP. Wouldn't it make sense to treat RDSPP as a #UD even though it's a NOP if CET is disabled? The logic being that a sane guest will execute RDSSP iff CET is enabled, and in that case it'd be better to inject a #UD than to silently break the guest.

We cannot assume that guests will bother checking CPUID before invoking RDSPP. This is especially true userspace, which needs to check if CET is enable for itself and can only use RDSPP to do so.

Paolo

#3 lore raw
Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Sean Christopherson
Quoted 2 lines · Paolo Bonzini

Instructions starting with 0f18 up to 0f1f are reserved nops, except those that were assigned to MPX.

Well, they're probably reserved NOPs again :-D.

So are you suggesting adding them back to the list as well?

Doesn't KVM still support MPX?

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

These include the endbr markers used by CET.

And RDSPP. Wouldn't it make sense to treat RDSPP as a #UD even though it's a NOP if CET is disabled? The logic being that a sane guest will execute RDSSP iff CET is enabled, and in that case it'd be better to inject a #UD than to silently break the guest.

We cannot assume that guests will bother checking CPUID before invoking RDSPP. This is especially true userspace, which needs to check if CET is enable for itself and can only use RDSPP to do so.

Ugh, yeah, just read through the CET enabling thread that showed code snippets that do exactly this.

I assume it would be best to make SHSTK dependent on unrestricted guest? Emulating RDSPP by reading vmcs.GUEST_SSP seems pointless as it will become statle apart on the first emulated CALL/RET.

author maintainer
#4 lore raw
Quoted 21 lines · Sean Christopherson
Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Sean Christopherson
Quoted 2 lines · Paolo Bonzini

Instructions starting with 0f18 up to 0f1f are reserved nops, except those that were assigned to MPX.

Well, they're probably reserved NOPs again :-D.

So are you suggesting adding them back to the list as well?

Doesn't KVM still support MPX?

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

These include the endbr markers used by CET.

And RDSPP. Wouldn't it make sense to treat RDSPP as a #UD even though it's a NOP if CET is disabled? The logic being that a sane guest will execute RDSSP iff CET is enabled, and in that case it'd be better to inject a #UD than to silently break the guest.

We cannot assume that guests will bother checking CPUID before invoking RDSPP. This is especially true userspace, which needs to check if CET is enable for itself and can only use RDSPP to do so.

Quoted 6 lines

Ugh, yeah, just read through the CET enabling thread that showed code snippets that do exactly this.

I assume it would be best to make SHSTK dependent on unrestricted guest? Emulating RDSPP by reading vmcs.GUEST_SSP seems pointless as it will become statle apart on the first emulated CALL/RET.

Running arbitrary code under the emulator is problematic anyway with CET, since you won't be checking ENDBR markers or updating the state machine. So perhaps in addition to what you say we should have a mode where, unless unrestricted guest is disabled, the emulator only accepts I/O, MOV and ALU instructions.

Paolo

#5 lore raw
Quoted 29 lines · Paolo Bonzini
Quoted 21 lines · Sean Christopherson
Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Sean Christopherson
Quoted 2 lines · Paolo Bonzini

Instructions starting with 0f18 up to 0f1f are reserved nops, except those that were assigned to MPX.

Well, they're probably reserved NOPs again :-D.

So are you suggesting adding them back to the list as well?

Doesn't KVM still support MPX?

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

These include the endbr markers used by CET.

And RDSPP. Wouldn't it make sense to treat RDSPP as a #UD even though it's a NOP if CET is disabled? The logic being that a sane guest will execute RDSSP iff CET is enabled, and in that case it'd be better to inject a #UD than to silently break the guest.

We cannot assume that guests will bother checking CPUID before invoking RDSPP. This is especially true userspace, which needs to check if CET is enable for itself and can only use RDSPP to do so.

Quoted 6 lines

Ugh, yeah, just read through the CET enabling thread that showed code snippets that do exactly this.

I assume it would be best to make SHSTK dependent on unrestricted guest? Emulating RDSPP by reading vmcs.GUEST_SSP seems pointless as it will become statle apart on the first emulated CALL/RET.

Quoted 5 lines

Running arbitrary code under the emulator is problematic anyway with CET, since you won't be checking ENDBR markers or updating the state machine. So perhaps in addition to what you say we should have a mode where, unless unrestricted guest is disabled, the emulator only accepts I/O, MOV and ALU instructions.

Doh, I forgot all about those pesky ENDBR markers. I think a slimmed down emulator makes sense?

Tangentially related, isn't the whole fastop thing doomed once CET kernel support lands?

author maintainer
#6 lore raw
Quoted 8 lines · Sean Christopherson
Quoted 5 lines

Running arbitrary code under the emulator is problematic anyway with CET, since you won't be checking ENDBR markers or updating the state machine. So perhaps in addition to what you say we should have a mode where, unless unrestricted guest is disabled, the emulator only accepts I/O, MOV and ALU instructions.

Doh, I forgot all about those pesky ENDBR markers. I think a slimmed down emulator makes sense?

Or just slimmed down opcode tables.

Quoted 2 lines

Tangentially related, isn't the whole fastop thing doomed once CET kernel support lands?

Why? You do need to add endbr markers and some of the fastop handlers won't fit in 8 bytes, but that should be it.

Paolo

#7 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 2 lines · Sean Christopherson

Tangentially related, isn't the whole fastop thing doomed once CET kernel support lands?

Why? You do need to add endbr markers and some of the fastop handlers won't fit in 8 bytes, but that should be it.

Never mind, had a brain fart and forgot CALL_NOSPEC shouldn't get patched to a retpoline on CET capable CPUs.