Skip to contentSkip to conversation

Add vmmcall/vmcall to x86_emulate (v3)

PATCH In review Anthony Liguori 6 participants 9 messages Sep 17, 2007 – Sep 18, 2007 2 messages not in the archive

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

PATCH 1/3 Add vmmcall/vmcall to x86_emulate (v3)

Add vmmcall/vmcall to x86_emulate. Future patch will implement functionality for these instructions.

drivers/kvm/x86_emulate.c +17 -6
index 18c2b2c..a16bbd6 100644
@@ -1301,6 +1301,12 @@ twobyte_insn:
 			u16 size;
 			unsigned long address;
 
+		case 0: /* vmcall */
+			if (modrm_mod != 3 || modrm_rm != 1)
+				goto cannot_emulate;
+
+			/* nop */
+			break;
 		case 2: /* lgdt */
 			rc = read_descriptor(ctxt, ops, src.ptr,
 					     &size, &address, op_bytes);
@@ -1308,12 +1314,17 @@ twobyte_insn:
 				goto done;
 			realmode_lgdt(ctxt->vcpu, size, address);
 			break;
-		case 3: /* lidt */
-			rc = read_descriptor(ctxt, ops, src.ptr,
-					     &size, &address, op_bytes);
-			if (rc)
-				goto done;
-			realmode_lidt(ctxt->vcpu, size, address);
+		case 3: /* lidt/vmmcall */
+			if (modrm_mod == 3 && modrm_rm == 1) {
+				/* nop */
+			} else {
+				rc = read_descriptor(ctxt, ops, src.ptr,
+						     &size, &address,
+						     op_bytes);
+				if (rc)
+					goto done;
+				realmode_lidt(ctxt->vcpu, size, address);
+			}
 			break;
 		case 4: /* smsw */
 			if (modrm_mod != 3)

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#1 lore raw

PATCH 2/3 Refactor hypercall infrastructure (v3)

This patch refactors the current hypercall infrastructure to better support live migration and SMP. It eliminates the hypercall page by trapping the UD exception that would occur if you used the wrong hypercall instruction for the underlying architecture and replacing it with the right one lazily.

It also introduces the infrastructure to probe for hypercall available via CPUID leaves 0x40000000. CPUID leaf 0x40000001 should be filled out by userspace.

A fall-out of this patch is that the unhandled hypercalls no longer trap to userspace. There is very little reason though to use a hypercall to communicate with userspace as PIO or MMIO can be used. There is no code in tree that uses userspace hypercalls.

drivers/kvm/kvm.h +4 -4
index ad08138..1cde572 100644
@@ -46,6 +46,7 @@
 #define KVM_MAX_CPUID_ENTRIES 40
 
 #define DE_VECTOR 0
+#define UD_VECTOR 6
 #define NM_VECTOR 7
 #define DF_VECTOR 8
 #define TS_VECTOR 10
@@ -317,9 +318,6 @@ struct kvm_vcpu {
 	unsigned long cr0;
 	unsigned long cr2;
 	unsigned long cr3;
-	gpa_t para_state_gpa;
-	struct page *para_state_page;
-	gpa_t hypercall_gpa;
 	unsigned long cr4;
 	unsigned long cr8;
 	u64 pdptrs[4]; /* pae */
@@ -622,7 +620,9 @@ void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
 int kvm_mmu_load(struct kvm_vcpu *vcpu);
 void kvm_mmu_unload(struct kvm_vcpu *vcpu);
 
-int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run);
+int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
+
+int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
 
 static inline int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
 				     u32 error_code)
drivers/kvm/kvm_main.c +46 -110

190 lines not on this page · raw message

drivers/kvm/svm.c +16 -3
index 729f1cd..ecb8f06 100644
@@ -476,7 +476,8 @@ static void init_vmcb(struct vmcb *vmcb)
 					INTERCEPT_DR5_MASK |
 					INTERCEPT_DR7_MASK;
 
-	control->intercept_exceptions = 1 << PF_VECTOR;
+	control->intercept_exceptions = (1 << PF_VECTOR) |
+					(1 << UD_VECTOR);
 
 
 	control->intercept = 	(1ULL << INTERCEPT_INTR) |
@@ -970,6 +971,17 @@ static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
 	return 0;
 }
 
+static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
+{
+	int er;
+
+	er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0);
+	if (er != EMULATE_DONE)
+		inject_ud(&svm->vcpu);
+
+	return 1;
+}
+
 static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
 {
 	svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
@@ -1036,7 +1048,8 @@ static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
 {
 	svm->next_rip = svm->vmcb->save.rip + 3;
 	skip_emulated_instruction(&svm->vcpu);
-	return kvm_hypercall(&svm->vcpu, kvm_run);
+	kvm_emulate_hypercall(&svm->vcpu);
+	return 1;
 }
 
 static int invalid_op_interception(struct vcpu_svm *svm,
@@ -1232,6 +1245,7 @@ static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
 	[SVM_EXIT_WRITE_DR3]			= emulate_on_interception,
 	[SVM_EXIT_WRITE_DR5]			= emulate_on_interception,
 	[SVM_EXIT_WRITE_DR7]			= emulate_on_interception,
+	[SVM_EXIT_EXCP_BASE + UD_VECTOR]	= ud_interception,
 	[SVM_EXIT_EXCP_BASE + PF_VECTOR] 	= pf_interception,
 	[SVM_EXIT_EXCP_BASE + NM_VECTOR] 	= nm_interception,
 	[SVM_EXIT_INTR] 			= nop_on_interception,
@@ -1664,7 +1678,6 @@ svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
 	hypercall[0] = 0x0f;
 	hypercall[1] = 0x01;
 	hypercall[2] = 0xd9;
-	hypercall[3] = 0xc3;
 }
 
 static void svm_check_processor_compat(void *rtn)
drivers/kvm/vmx.c +27 -3
index 4f115a8..a71564c 100644
@@ -164,6 +164,13 @@ static inline int is_no_device(u32 intr_info)
 		(INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
 }
 
+static inline int is_invalid_opcode(u32 intr_info)
+{
+	return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
+			     INTR_INFO_VALID_MASK)) ==
+		(INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
+}
+
 static inline int is_external_interrupt(u32 intr_info)
 {
 	return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
@@ -315,7 +322,7 @@ static void update_exception_bitmap(struct kvm_vcpu *vcpu)
 {
 	u32 eb;
 
-	eb = 1u << PF_VECTOR;
+	eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
 	if (!vcpu->fpu_active)
 		eb |= 1u << NM_VECTOR;
 	if (vcpu->guest_debug.enabled)
@@ -558,6 +565,15 @@ static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
 		     INTR_INFO_VALID_MASK);
 }
 
+static void vmx_inject_ud(struct kvm_vcpu *vcpu)
+{
+	vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
+		     GP_VECTOR |
+		     INTR_TYPE_EXCEPTION |
+		     INTR_INFO_DELIEVER_CODE_MASK |
+		     INTR_INFO_VALID_MASK);
+}
+
 /*
  * Swap MSR entry in host/guest MSR entry array.
  */
@@ -1770,6 +1786,14 @@ static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 		return 1;
 	}
 
+	if (is_invalid_opcode(intr_info)) {
+		er = emulate_instruction(vcpu, kvm_run, 0, 0);
+		if (er != EMULATE_DONE)
+			vmx_inject_ud(vcpu);
+
+		return 1;
+	}
+
 	error_code = 0;
 	rip = vmcs_readl(GUEST_RIP);
 	if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
@@ -1872,7 +1896,6 @@ vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
 	hypercall[0] = 0x0f;
 	hypercall[1] = 0x01;
 	hypercall[2] = 0xc1;
-	hypercall[3] = 0xc3;
 }
 
 static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
@@ -2058,7 +2081,8 @@ static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 {
 	skip_emulated_instruction(vcpu);
-	return kvm_hypercall(vcpu, kvm_run);
+	kvm_emulate_hypercall(vcpu);
+	return 1;
 }
 
 /*
drivers/kvm/x86_emulate.c +9 -2
index a16bbd6..1362082 100644
@@ -1305,7 +1305,11 @@ twobyte_insn:
 			if (modrm_mod != 3 || modrm_rm != 1)
 				goto cannot_emulate;
 
-			/* nop */
+			rc = kvm_fix_hypercall(ctxt->vcpu);
+			if (rc)
+				goto done;
+
+			kvm_emulate_hypercall(ctxt->vcpu);
 			break;
 		case 2: /* lgdt */
 			rc = read_descriptor(ctxt, ops, src.ptr,
@@ -1316,7 +1320,10 @@ twobyte_insn:
 			break;
 		case 3: /* lidt/vmmcall */
 			if (modrm_mod == 3 && modrm_rm == 1) {
-				/* nop */
+				rc = kvm_fix_hypercall(ctxt->vcpu);
+				if (rc)
+					goto done;
+				kvm_emulate_hypercall(ctxt->vcpu);
 			} else {
 				rc = read_descriptor(ctxt, ops, src.ptr,
 						     &size, &address,
include/linux/kvm_para.h +98 -61

171 lines not on this page · raw message

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

A message that is not in the archive (sent off-list or not yet received): 11900590723438-git-send-email-aliguori-r/[email protected]

#3 lore raw

PATCH 3/3 Add the paravirtualization CPUID entry to QEMU (v3)

This adds CPUID support for paravirtualization. We can do this unconditionally because there are no features defined at the moment. Future code that introduces features, however, should validate that the kernel supports those features before setting the bits.

qemu/qemu-kvm.c +17 -0
index 4843a74..7572cef 100644
@@ -1113,12 +1113,29 @@ static void do_cpuid_ent(struct kvm_cpuid_entry *e, uint32_t function,
 int kvm_qemu_init_env(CPUState *cenv)
 {
     struct kvm_cpuid_entry cpuid_ent[100];
+    struct kvm_cpuid_entry *pv_ent;
+    uint32_t signature[3];
     int cpuid_nent = 0;
     CPUState copy;
     uint32_t i, limit;
 
     copy = *cenv;
 
+    /* Paravirtualization CPUIDs */
+    memcpy(signature, "KVMKVMKVM", 12);
+    pv_ent = &cpuid_ent[cpuid_nent++];
+    memset(pv_ent, 0, sizeof(*pv_ent));
+    pv_ent->function = KVM_CPUID_SIGNATURE;
+    pv_ent->eax = 0;
+    pv_ent->ebx = signature[0];
+    pv_ent->ecx = signature[1];
+    pv_ent->edx = signature[2];
+
+    pv_ent = &cpuid_ent[cpuid_nent++];
+    memset(pv_ent, 0, sizeof(*pv_ent));
+    pv_ent->function = KVM_CPUID_FEATURES;
+    pv_ent->eax = 0;
+
     copy.regs[R_EAX] = 0;
     qemu_kvm_cpuid_on_env(&copy);
     limit = copy.regs[R_EAX];
user/kvmctl.h +1 -0
index 109d81c..74db1ba 100644
@@ -10,6 +10,7 @@
 #endif
 
 #include <linux/kvm.h>
+#include <linux/kvm_para.h>
 #include <stdint.h>
 #include <signal.h>
 

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#4 lore raw

RE: [kvm-devel] [PATCH 2/3] Refactor hypercall infrastructure (v3)

In reply to Anthony Liguori
Quoted 1 line · Anthony Liguori

This patch refactors the current hypercall infrastructure to better

support

Quoted 2 lines

live
migration and SMP. It eliminates the hypercall page by trapping the

UD

Quoted 1 line

exception that would occur if you used the wrong hypercall instruction

for the

Quoted 3 lines

underlying architecture and replacing it with the right one lazily.

It also introduces the infrastructure to probe for hypercall available

via

Quoted 1 line

CPUID leaves 0x40000000. CPUID leaf 0x40000001 should be filled out

by

Quoted 3 lines

userspace.

A fall-out of this patch is that the unhandled hypercalls no longer

trap to

Quoted 3 lines

userspace. There is very little reason though to use a hypercall to communicate
with userspace as PIO or MMIO can be used. There is no code in tree

that uses

Quoted 9 lines

userspace hypercalls.

include/linux/kvm_para.h +0 -0
index 3b29256..cc5dfb4 100644

include/linux/kvm_para.h line 7

Quoted 7 lines
include/linux/kvm_para.h +0 -4
@@ -1,73 +1,110 @@
 #ifndef __LINUX_KVM_PARA_H
 #define __LINUX_KVM_PARA_H
 
-/*
- * Guest OS interface for KVM paravirtualization
- *
- * Note: this interface is totally experimental, and is certain to

change

include/linux/kvm_para.h line 4

Quoted 2 lines
include/linux/kvm_para.h +1 -1
- *       as we make progress.
+/* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx.

It

Quoted 1 line
include/linux/kvm_para.h +1 -0
+ * should be used to determine that a VM is running under KVM.

<snip>

Quoted 8 lines
include/linux/kvm_para.h +8 -0
+
+static inline int kvm_para_available(void)
+{
+	unsigned int eax, ebx, ecx, edx;
+	char signature[13];
+
+	cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
+	memcpy(signature + 0, &ebx, 4);

include/linux/kvm_para.h

Quoted 10 lines
include/linux/kvm_para.h +9 -0
+	memcpy(signature + 4, &ecx, 4);
+	memcpy(signature + 8, &edx, 4);
+	signature[12] = 0;
+
+	if (strcmp(signature, "KVMKVMKVM") == 0)
 
+		return 1;
+
+	return 0;
+}

I think we should compare 12 characters (not just 9, as far as my eyes tell), and can we use some cute one, like "FantasticKVM"? ;-)

Jun
---
Intel Open Source Technology Center

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#5 lore raw

Re: [kvm-devel] [PATCH 2/3] Refactor hypercall infrastructure (v3)

In reply to Anthony Liguori
Quoted 5 lines · Anthony Liguori

This patch refactors the current hypercall infrastructure to better support live migration and SMP. It eliminates the hypercall page by trapping the UD exception that would occur if you used the wrong hypercall instruction for the underlying architecture and replacing it with the right one lazily.

Quoted 10 lines

It also introduces the infrastructure to probe for hypercall available via CPUID leaves 0x40000000. CPUID leaf 0x40000001 should be filled out by userspace.

A fall-out of this patch is that the unhandled hypercalls no longer trap to userspace. There is very little reason though to use a hypercall to communicate with userspace as PIO or MMIO can be used. There is no code in tree that uses userspace hypercalls.

Surprisingly, this patch kills Windows XP (ACPI HAL). I'll try to find out why.

Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#6 lore raw

Re: [kvm-devel] [PATCH 2/3] Refactor hypercall infrastructure (v3)

Quoted 20 lines · Avi Kivity
Quoted 5 lines · Anthony Liguori

This patch refactors the current hypercall infrastructure to better support live migration and SMP. It eliminates the hypercall page by trapping the UD exception that would occur if you used the wrong hypercall instruction for the underlying architecture and replacing it with the right one lazily.

Quoted 11 lines

It also introduces the infrastructure to probe for hypercall available via CPUID leaves 0x40000000. CPUID leaf 0x40000001 should be filled out by userspace.

A fall-out of this patch is that the unhandled hypercalls no longer trap to userspace. There is very little reason though to use a hypercall to communicate with userspace as PIO or MMIO can be used. There is no code in tree that uses userspace hypercalls.

Quoted 4 lines

Surprisingly, this patch kills Windows XP (ACPI HAL). I'll try to find out why.

Not trapping #UD brings things back to normal. So, Windows likes to execute undefined instructions, and we don'd handle these well.

I really should start asking for unit tests for these kinds of things (execute undefined instruction, see the #UD, say ok).

Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#7 lore raw

Re: [kvm-devel] [PATCH 2/3] Refactor hypercall infrastructure (v3)

Quoted 26 lines · Avi Kivity
Quoted 25 lines · Avi Kivity
Quoted 21 lines · Anthony Liguori

This patch refactors the current hypercall infrastructure to better support live
migration and SMP. It eliminates the hypercall page by trapping the UD exception that would occur if you used the wrong hypercall instruction for the
underlying architecture and replacing it with the right one lazily.

It also introduces the infrastructure to probe for hypercall available via
CPUID leaves 0x40000000. CPUID leaf 0x40000001 should be filled out by userspace.

A fall-out of this patch is that the unhandled hypercalls no longer trap to
userspace. There is very little reason though to use a hypercall to communicate
with userspace as PIO or MMIO can be used. There is no code in tree that uses
userspace hypercalls.

Quoted 8 lines
Quoted 4 lines

Surprisingly, this patch kills Windows XP (ACPI HAL). I'll try to find out why.

Not trapping #UD brings things back to normal. So, Windows likes to execute undefined instructions, and we don'd handle these well.

Okay, vmx_inject_ud() was broken. Fixed now.

Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#8 lore raw

Re: [kvm-devel] [PATCH 2/3] Refactor hypercall infrastructure (v3)

Quoted 23 lines · Avi Kivity
Quoted 21 lines · Avi Kivity
Quoted 19 lines · Avi Kivity
Quoted 16 lines · Anthony Liguori

This patch refactors the current hypercall infrastructure to better support live migration and SMP. It eliminates the hypercall page by trapping the UD exception that would occur if you used the wrong hypercall instruction for the underlying architecture and replacing it with the right one lazily.

It also introduces the infrastructure to probe for hypercall available via CPUID leaves 0x40000000. CPUID leaf 0x40000001 should be filled out by userspace.

A fall-out of this patch is that the unhandled hypercalls no longer trap to userspace. There is very little reason though to use a hypercall to communicate with userspace as PIO or MMIO can be used. There is no code in tree that uses userspace hypercalls.

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

Surprisingly, this patch kills Windows XP (ACPI HAL). I'll try to find out why.

Not trapping #UD brings things back to normal. So, Windows likes to execute undefined instructions, and we don'd handle these well.

Okay, vmx_inject_ud() was broken. Fixed now.

Yeah, I was just about to send the patch for that. Sorry about that, I didn't even think to test Windows...

Regards,

Anthony Liguori

A message that is not in the archive (sent off-list or not yet received): 1190059071649-git-send-email-aliguori-r/[email protected]

#10 lore raw
Quoted 4 lines · Anthony Liguori

Add vmmcall/vmcall to x86_emulate. Future patch will implement functionality for these instructions.

Applied 1-3, thanks.

Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/