Skip to contentSkip to conversation

KVM: x86 emulator: reject SYSENTER in compatibility mode on AMD guests

PATCH Superseded Avi Kivity 3 participants 7 messages Feb 1, 2012

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH 1/1 KVM: x86 emulator: reject SYSENTER in compatibility mode on AMD guests

If the guest thinks it's an AMD, it will not have prepared the SYSENTER MSRs, and if the guest executes SYSENTER in compatibility mode, it will fails.

Detect this condition and #UD instead, like the spec says.

---
 arch/x86/kvm/emulate.c |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
arch/x86/kvm/emulate.c +18 -0
index 6eaedac..367bd06 100644
@@ -1892,6 +1892,16 @@ static int em_lseg(struct x86_emulate_ctxt *ctxt)
 	ss->p = 1;
 }
 
+static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
+{
+	u32 eax, ebx, ecx, edx;
+
+	return ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx)
+		&& ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
+		&& ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
+		&& edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
+}
+
 static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
 {
 	struct x86_emulate_ops *ops = ctxt->ops;
@@ -2008,6 +2018,14 @@ static int em_sysenter(struct x86_emulate_ctxt *ctxt)
 	if (ctxt->mode == X86EMUL_MODE_REAL)
 		return emulate_gp(ctxt, 0);
 
+	/*
+	 * Not recognized on AMD in compat mode (but is recognized in legacy
+	 * mode).
+	 */
+	if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
+	    && !vendor_intel(ctxt))
+		return emulate_ud(ctxt);
+
 	/* XXX sysenter/sysexit have not been tested in 64bit mode.
 	* Therefore, we inject an #UD.
 	*/
Signature · 1 line
1.7.9
#1 lore raw

arch/x86/kvm/emulate.c static int em_lseg(struct x86_emulate_ctxt *ctxt) · line 1897

Quoted 4 lines · Avi Kivity
arch/x86/kvm/emulate.c +3 -0
+static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
+{
+	u32 eax, ebx, ecx, edx;

Just to be clean:

Shouldn't eax and ecx be initialized to zero?

regards Stephan

Quoted 7 lines
arch/x86/kvm/emulate.c +7 -0
+
+	return ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx)
+		&& ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
+		&& ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
+		&& edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
+}
+
author
#2 lore raw
Quoted 8 lines · Stephan Bärwolf

arch/x86/kvm/emulate.c static int em_lseg(struct x86_emulate_ctxt *ctxt) · line 1897

Quoted 4 lines · Avi Kivity
arch/x86/kvm/emulate.c +3 -0
+static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
+{
+	u32 eax, ebx, ecx, edx;

Just to be clean:

Shouldn't eax and ecx be initialized to zero?

Either get_cpuid() initializes them and returns true, or it returns false and we never evaluate them.

Quoted 9 lines

regards Stephan

Quoted 7 lines
arch/x86/kvm/emulate.c +7 -0
+
+	return ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx)
+		&& ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
+		&& ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
+		&& edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
+}
+
Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw
Quoted 11 lines · Avi Kivity
Quoted 8 lines · Stephan Bärwolf

arch/x86/kvm/emulate.c static int em_lseg(struct x86_emulate_ctxt *ctxt) · line 1897

Quoted 4 lines · Avi Kivity
arch/x86/kvm/emulate.c +3 -0
+static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
+{
+	u32 eax, ebx, ecx, edx;

Just to be clean:

Shouldn't eax and ecx be initialized to zero?

Either get_cpuid() initializes them and returns true, or it returns false and we never evaluate them.

This isn't what I mean.

CPUID will only return back the VENDOR (in ebx..edx) if you call it with eax=0x00000000 and ecx=0x00000000 (see cpu docs).

Of course the compiler should set these two vars to zero (?) But what if somebody wants to read this code sometime later ?? (The compiler will optimize the "set to zero" away...)

regards Stephan

author
#4 lore raw
Quoted 12 lines · Stephan Bärwolf
Quoted 11 lines · Avi Kivity
Quoted 8 lines · Stephan Bärwolf

arch/x86/kvm/emulate.c static int em_lseg(struct x86_emulate_ctxt *ctxt) · line 1897

Quoted 4 lines · Avi Kivity
arch/x86/kvm/emulate.c +3 -0
+static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
+{
+	u32 eax, ebx, ecx, edx;

Just to be clean:

Shouldn't eax and ecx be initialized to zero?

Either get_cpuid() initializes them and returns true, or it returns false and we never evaluate them.

Quoted 9 lines

This isn't what I mean.

CPUID will only return back the VENDOR (in ebx..edx) if you call it with eax=0x00000000 and ecx=0x00000000 (see cpu docs).

Of course the compiler should set these two vars to zero (?) But what if somebody wants to read this code sometime later ?? (The compiler will optimize the "set to zero" away...)

Ah, right of course. Will update.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
In reply to Avi Kivity
Quoted 4 lines · Avi Kivity

If the guest thinks it's an AMD, it will not have prepared the SYSENTER MSRs, and if the guest executes SYSENTER in compatibility mode, it will fails.

Detect this condition and #UD instead, like the spec says.

Note the SYSENTER_CS & 0xfffc == 0 check protects against a fatal error (unlike the SYSCALL bug), in case of zeroed MSRs.

author
#6 lore raw
Quoted 8 lines · Marcelo Tosatti
Quoted 4 lines · Avi Kivity

If the guest thinks it's an AMD, it will not have prepared the SYSENTER MSRs, and if the guest executes SYSENTER in compatibility mode, it will fails.

Detect this condition and #UD instead, like the spec says.

Note the SYSENTER_CS & 0xfffc == 0 check protects against a fatal error (unlike the SYSCALL bug), in case of zeroed MSRs.

That's a relief. It's still possible for this to fail (say kexec from 32-bit guest to 64-bit guest) but it's incredibly unlikely.

Signature · 1 line
error compiling committee.c: too many arguments to function