Skip to contentSkip to conversation

use explicit 64bit storage for sysenter values

PATCH Applied Andre Przywara 2 participants 6 messages May 28, 2009 – Jun 7, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 use explicit 64bit storage for sysenter values

Since AMD does not support sysenter in 64bit mode, the VMCB fields storing the MSRs are truncated to 32bit upon VMRUN/#VMEXIT. So store the values in a separate 64bit storage to avoid truncation.

---
 arch/x86/kvm/kvm_svm.h |    4 ++++
 arch/x86/kvm/svm.c     |   12 ++++++------
 2 files changed, 10 insertions(+), 6 deletions(-)
arch/x86/kvm/kvm_svm.h +4 -0
index ed66e4c..4129dc1 100644
@@ -27,6 +27,10 @@ struct vcpu_svm {
 	unsigned long vmcb_pa;
 	struct svm_cpu_data *svm_data;
 	uint64_t asid_generation;
+	uint64_t sysenter_cs;
+	uint64_t sysenter_esp;
+	uint64_t sysenter_eip;
+	struct kvm_segment user_cs; /* used in sysenter/sysexit emulation */
 
 	u64 next_rip;
 
arch/x86/kvm/svm.c +6 -6
index dd667dd..f0f2885 100644
@@ -1978,13 +1978,13 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
 		break;
 #endif
 	case MSR_IA32_SYSENTER_CS:
-		*data = svm->vmcb->save.sysenter_cs;
+		*data = svm->sysenter_cs;
 		break;
 	case MSR_IA32_SYSENTER_EIP:
-		*data = svm->vmcb->save.sysenter_eip;
+		*data = svm->sysenter_eip;
 		break;
 	case MSR_IA32_SYSENTER_ESP:
-		*data = svm->vmcb->save.sysenter_esp;
+		*data = svm->sysenter_esp;
 		break;
 	/* Nobody will change the following 5 values in the VMCB so
 	   we can safely return them on rdmsr. They will always be 0
@@ -2068,13 +2068,13 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
 		break;
 #endif
 	case MSR_IA32_SYSENTER_CS:
-		svm->vmcb->save.sysenter_cs = data;
+		svm->sysenter_cs = data;
 		break;
 	case MSR_IA32_SYSENTER_EIP:
-		svm->vmcb->save.sysenter_eip = data;
+		svm->sysenter_eip = data;
 		break;
 	case MSR_IA32_SYSENTER_ESP:
-		svm->vmcb->save.sysenter_esp = data;
+		svm->sysenter_esp = data;
 		break;
 	case MSR_IA32_DEBUGCTLMSR:
 		if (!svm_has(SVM_FEATURE_LBRV)) {
Signature · 1 line
1.6.1.3
#1 lore raw

PATCH 2/2 add sysenter/syscall emulation for 32bit compat mode

sysenter/sysexit are not supported on AMD's 32bit compat mode, whereas syscall is not supported on Intel's 32bit compat mode. To allow cross vendor migration we emulate the missing instructions by setting up the processor state according to the other call.
The sysenter code was originally sketched by Amit Shah, it was completed, debugged, syscall added and made-to-work by Christoph Egger and polished up by Andre Przywara.
Please note that sysret does not need to be emulated, because it will be exectued in 64bit mode and returning to 32bit compat mode works on Intel.

---
 arch/x86/kvm/x86.c         |   37 ++++-
 arch/x86/kvm/x86_emulate.c |  349 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 380 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +32 -5
index 6d44dd5..dae7726 100644
@@ -2593,11 +2593,38 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
 		/* Reject the instructions other than VMCALL/VMMCALL when
 		 * try to emulate invalid opcode */
 		c = &vcpu->arch.emulate_ctxt.decode;
-		if ((emulation_type & EMULTYPE_TRAP_UD) &&
-		    (!(c->twobyte && c->b == 0x01 &&
-		      (c->modrm_reg == 0 || c->modrm_reg == 3) &&
-		       c->modrm_mod == 3 && c->modrm_rm == 1)))
-			return EMULATE_FAIL;
+
+		if (emulation_type & EMULTYPE_TRAP_UD) {
+			if (!c->twobyte)
+				return EMULATE_FAIL;
+			switch (c->b) {
+			case 0x01: /* VMMCALL */
+				if (c->modrm_mod != 3)
+					return EMULATE_FAIL;
+				if (c->modrm_rm != 1)
+					return EMULATE_FAIL;
+				break;
+			case 0x34: /* sysenter */
+			case 0x35: /* sysexit */
+				if (c->modrm_mod != 0)
+					return EMULATE_FAIL;
+				if (c->modrm_rm != 0)
+					return EMULATE_FAIL;
+				break;
+			case 0x05: /* syscall */
+				r = 0;
+				if (c->modrm_mod != 0)
+					return EMULATE_FAIL;
+				if (c->modrm_rm != 0)
+					return EMULATE_FAIL;
+				break;
+			default:
+				return EMULATE_FAIL;
+			}
+
+			if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
+				return EMULATE_FAIL;
+		}
 
 		++vcpu->stat.insn_emulation;
 		if (r)  {
arch/x86/kvm/x86_emulate.c +348 -1

385 lines not on this page · raw message

Signature · 1 line
1.6.1.3
#2 lore raw

Re: [PATCH 2/2] add sysenter/syscall emulation for 32bit compat mode

Quoted 16 lines · Andre Przywara

sysenter/sysexit are not supported on AMD's 32bit compat mode, whereas syscall is not supported on Intel's 32bit compat mode. To allow cross vendor migration we emulate the missing instructions by setting up the processor state according to the other call.
The sysenter code was originally sketched by Amit Shah, it was completed, debugged, syscall added and made-to-work by Christoph Egger and polished up by Andre Przywara.
Please note that sysret does not need to be emulated, because it will be exectued in 64bit mode and returning to 32bit compat mode works on Intel.

arch/x86/kvm/x86.c +0 -0
 		++vcpu->stat.insn_emulation;
 		if (r)  {
arch/x86/kvm/x86_emulate.c +0 -0
index 22c765d..41b78fa 100644

arch/x86/kvm/x86_emulate.c line 37

Quoted 6 lines
arch/x86/kvm/x86_emulate.c +2 -0
@@ -32,6 +32,8 @@
 #include <linux/module.h>
 #include <asm/kvm_x86_emulate.h>
 
+#include "mmu.h"
+
 

I think this is unneeded?

Quoted 16 lines
arch/x86/kvm/x86_emulate.c +12 -0
@@ -1985,10 +1992,114 @@ twobyte_insn:
 			goto cannot_emulate;
 		}
 		break;
+	case 0x05: { /* syscall */
+		unsigned long cr0 = ctxt->vcpu->arch.cr0;
+		struct kvm_segment cs, ss;
+
+		memset(&cs, 0, sizeof(struct kvm_segment));
+		memset(&ss, 0, sizeof(struct kvm_segment));
+
+		/* inject #UD if
+		 * 1. we are in real mode
+		 * 2. protected mode is not enabled
+		 * 3. LOCK prefix is used
+		 */

arch/x86/kvm/x86_emulate.c twobyte_insn: · line 2016

Quoted 10 lines
arch/x86/kvm/x86_emulate.c +9 -0
+		if ((ctxt->mode == X86EMUL_MODE_REAL)
+			|| (!(cr0 & X86_CR0_PE))
+			|| (c->lock_prefix)) {
+			/* we don't need to inject #UD here, because
+			 * when emulate_instruction() returns something else
+			 * than EMULATE_DONE, then svm.c:ud_interception()
+			 * will do that for us.
+			 */
+			goto cannot_emulate;
 

I prefer explicit injection, relying on the caller is tricky and may change.

arch/x86/kvm/x86_emulate.c twobyte_insn: · line 2018

Quoted 2 lines
arch/x86/kvm/x86_emulate.c +1 -0
+	case 0x07:		/* sysret */
 

Since we don't emulate sysret, it should be dropped here.

arch/x86/kvm/x86_emulate.c twobyte_insn:

Quoted 4 lines
arch/x86/kvm/x86_emulate.c +3 -0
+			cs.limit = 0xffffffff;
+			ss.base = 0;
+			ss.limit = 0xffffffff;
 

Once is enough.

Please move the code out of the switch and into separate functions.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw

Re: [PATCH 2/2] add sysenter/syscall emulation for 32bit compat mode

Hi Avi,

Quoted 11 lines

arch/x86/kvm/x86_emulate.c line 37

Quoted 9 lines
arch/x86/kvm/x86_emulate.c +2 -0
@@ -32,6 +32,8 @@
 #include <linux/module.h>
 #include <asm/kvm_x86_emulate.h>
 
+#include "mmu.h"
+
 

I think this is unneeded?

Seems so. Probably a left-over from debugging.

Quoted 21 lines
Quoted 21 lines
arch/x86/kvm/x86_emulate.c +16 -0
@@ -1985,10 +1992,114 @@ twobyte_insn:
             goto cannot_emulate;
         }
         break;
+    case 0x05: { /* syscall */
+        unsigned long cr0 = ctxt->vcpu->arch.cr0;
+        struct kvm_segment cs, ss;
+
+        memset(&cs, 0, sizeof(struct kvm_segment));
+        memset(&ss, 0, sizeof(struct kvm_segment));
+
+        /* inject #UD if
+         * 1. we are in real mode
+         * 2. protected mode is not enabled
+         * 3. LOCK prefix is used
+         */
+        if ((ctxt->mode == X86EMUL_MODE_REAL)
+            || (!(cr0 & X86_CR0_PE))
+            || (c->lock_prefix)) {
+            /* we don't need to inject #UD here, because
Quoted 8 lines

arch/x86/kvm/x86_emulate.c twobyte_insn:

Quoted 6 lines
arch/x86/kvm/x86_emulate.c +5 -0
+             * when emulate_instruction() returns something else
+             * than EMULATE_DONE, then svm.c:ud_interception()
+             * will do that for us.
+             */
+            goto cannot_emulate;
 

I prefer explicit injection, relying on the caller is tricky and may change.

I don't agree. If this function cannot emulate an instruction, it returns -1 and lets the upper levels handle this. If we cannot rely on this, what else can we rely on? I could remove the comment in case this is confusing. The same functionality (return -1 to inject UD into the guest) is used in other places in this same file.

Quoted 5 lines

arch/x86/kvm/x86_emulate.c twobyte_insn: · line 2018

Quoted 3 lines
arch/x86/kvm/x86_emulate.c +1 -0
+    case 0x07:        /* sysret */
 

Since we don't emulate sysret, it should be dropped here.

OK, will do.

Quoted 7 lines

arch/x86/kvm/x86_emulate.c twobyte_insn:

Quoted 5 lines
arch/x86/kvm/x86_emulate.c +3 -0
+            cs.limit = 0xffffffff;
+            ss.base = 0;
+            ss.limit = 0xffffffff;
 

Once is enough.

You are right about the ss.base assignment. But the limit goes from five f's to eight f's. On a first glance this should not matter (as the granularity bit is set), but exactly here are differences between VMX and SVM, so I'd like to leave it this way.

Quoted 2 lines

Please move the code out of the switch and into separate functions.

Ok, will do.

Thanks for the review!
Renewed patch will follow.

Regards,
Andre.

Signature · 3 lines
Andre Przywara
AMD-OSRC (Dresden)
Tel: x29712
#4 lore raw

Re: [PATCH 2/2] add sysenter/syscall emulation for 32bit compat mode

Quoted 30 lines · Andre Przywara
Quoted 22 lines
Quoted 22 lines
arch/x86/kvm/x86_emulate.c +16 -0
@@ -1985,10 +1992,114 @@ twobyte_insn:
             goto cannot_emulate;
         }
         break;
+    case 0x05: { /* syscall */
+        unsigned long cr0 = ctxt->vcpu->arch.cr0;
+        struct kvm_segment cs, ss;
+
+        memset(&cs, 0, sizeof(struct kvm_segment));
+        memset(&ss, 0, sizeof(struct kvm_segment));
+
+        /* inject #UD if
+         * 1. we are in real mode
+         * 2. protected mode is not enabled
+         * 3. LOCK prefix is used
+         */
+        if ((ctxt->mode == X86EMUL_MODE_REAL)
+            || (!(cr0 & X86_CR0_PE))
+            || (c->lock_prefix)) {
+            /* we don't need to inject #UD here, because
Quoted 8 lines

arch/x86/kvm/x86_emulate.c twobyte_insn:

Quoted 6 lines
arch/x86/kvm/x86_emulate.c +5 -0
+             * when emulate_instruction() returns something else
+             * than EMULATE_DONE, then svm.c:ud_interception()
+             * will do that for us.
+             */
+            goto cannot_emulate;
 

I prefer explicit injection, relying on the caller is tricky and may change.

Quoted 1 line

I don't agree. If this function cannot emulate an instruction, it returns -1 and lets the upper levels handle this. If we cannot rely on this, what else can we rely on? I could remove the comment in case this is confusing. The same functionality (return -1 to inject UD into the guest) is used in other places in this same file.

We return -1, but that doesn't mean we inject #UD. For some cases (page table emulation), we unshadow the page and retry (letting the guest execute natively).

We only inject #UD from that specific call site. If we somehow emulated from another call site, we'd get different behaviour.

Quoted 9 lines
Quoted 8 lines

arch/x86/kvm/x86_emulate.c twobyte_insn:

Quoted 6 lines
arch/x86/kvm/x86_emulate.c +3 -0
+            cs.limit = 0xffffffff;
+            ss.base = 0;
+            ss.limit = 0xffffffff;
 

Once is enough.

You are right about the ss.base assignment. But the limit goes from five f's to eight f's. On a first glance this should not matter (as the granularity bit is set), but exactly here are differences between VMX and SVM, so I'd like to leave it this way.

I misread, I thought you were setting ss.limit twice. Certainly the code is correct as is and should not be modified. Sorry about the confusion.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Applied
#5 lore raw
In reply to Andre Przywara
Quoted 4 lines · Andre Przywara

Since AMD does not support sysenter in 64bit mode, the VMCB fields storing the MSRs are truncated to 32bit upon VMRUN/#VMEXIT. So store the values in a separate 64bit storage to avoid truncation.

Applied, thanks.

arch/x86/kvm/kvm_svm.h struct vcpu_svm { · line 33

Quoted 2 lines
arch/x86/kvm/kvm_svm.h +1 -0
+	struct kvm_segment user_cs; /* used in sysenter/sysexit emulation */

Dropped this, unused.

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