Skip to contentSkip to conversation

KVM: SVM: Implement LBRV virtualization

PATCH In review Joerg Roedel 4 participants 11 messages Feb 12, 2008 – Feb 13, 2008

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

This little patchset implements virtualization of the Last Branch Record feature (LBRV) into the KVM-AMD module where this is available in hardware. For every non-LBRV capable hardware it only enables access to the 5 LBR MSRs. Without this access Windows XP 64 bit crashes in the installation routine.

Joerg

diffstat:

 arch/x86/kvm/kvm_svm.h |    2 +
 arch/x86/kvm/svm.c     |  125 ++++++++++++++++++++++++++++++++----------------
 2 files changed, 86 insertions(+), 41 deletions(-)

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

#1 lore raw

PATCH 1/3 KVM: SVM: let init_vmcb() take struct vcpu_svm as parameter

Change the parameter of the init_vmcb() function in the kvm-amd module from struct vmcb to struct vcpu_svm.

---
 arch/x86/kvm/svm.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/kvm/svm.c +6 -6
index 13765e9..6e0c6bd 100644
@@ -471,10 +471,10 @@ static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
 	seg->base = 0;
 }
 
-static void init_vmcb(struct vmcb *vmcb)
+static void init_vmcb(struct vcpu_svm *svm)
 {
-	struct vmcb_control_area *control = &vmcb->control;
-	struct vmcb_save_area *save = &vmcb->save;
+	struct vmcb_control_area *control = &svm->vmcb->control;
+	struct vmcb_save_area *save = &svm->vmcb->save;
 
 	control->intercept_cr_read = 	INTERCEPT_CR0_MASK |
 					INTERCEPT_CR3_MASK |
@@ -600,7 +600,7 @@ static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
-	init_vmcb(svm->vmcb);
+	init_vmcb(svm);
 
 	if (vcpu->vcpu_id != 0) {
 		svm->vmcb->save.rip = 0;
@@ -638,7 +638,7 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
 	svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
 	svm->asid_generation = 0;
 	memset(svm->db_regs, 0, sizeof(svm->db_regs));
-	init_vmcb(svm->vmcb);
+	init_vmcb(svm);
 
 	fx_init(&svm->vcpu);
 	svm->vcpu.fpu_active = 1;
@@ -1024,7 +1024,7 @@ static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
 	 * so reinitialize it.
 	 */
 	clear_page(svm->vmcb);
-	init_vmcb(svm->vmcb);
+	init_vmcb(svm);
 
 	kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
 	return 0;
Signature · 9 lines
1.5.3.7




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

PATCH 2/3 KVM: SVM: allocate the MSR permission map per VCPU

In reply to Joerg Roedel

This patch changes the kvm-amd module to allocate the SVM MSR permission map per VCPU instead of a global map for all VCPUs. With this we have more flexibility allowing specific guests to access virtualized MSRs. This is required for LBRV virtualization.

---
 arch/x86/kvm/kvm_svm.h |    2 +
 arch/x86/kvm/svm.c     |   67 +++++++++++++++++++++++-------------------------
 2 files changed, 34 insertions(+), 35 deletions(-)
arch/x86/kvm/kvm_svm.h +2 -0
index ecdfe97..65ef0fc 100644
@@ -39,6 +39,8 @@ struct vcpu_svm {
 	unsigned long host_db_regs[NUM_DB_REGS];
 	unsigned long host_dr6;
 	unsigned long host_dr7;
+
+	u32 *msrpm;
 };
 
 #endif
arch/x86/kvm/svm.c +32 -35

132 lines not on this page · raw message

Signature · 9 lines
1.5.3.7




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

PATCH 3/3 KVM: SVM: enable LBRV virtualization

In reply to Joerg Roedel

This patch implements the Last Branch Record Virtualization (LBRV) feature of the AMD Barcelona and Phenom processors into the kvm-amd module. It will only be enabled if the guest enables last branch recording in the DEBUG_CTL MSR. It also gives the guest access to the according MSRs which fixes the installation of Windows XP 64 bit on SVM.

---
 arch/x86/kvm/svm.c |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)
arch/x86/kvm/svm.c +46 -0
index 50ee223..2a71491 100644
@@ -387,6 +387,28 @@ static void svm_vcpu_init_msrpm(u32 *msrpm)
 	set_msr_interception(msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
 }
 
+static void svm_enable_lbrv(struct vcpu_svm *svm)
+{
+	u32 *msrpm = svm->msrpm;
+
+	svm->vmcb->control.lbr_ctl = 1;
+	set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
+	set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
+	set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
+	set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
+}
+
+static void svm_disable_lbrv(struct vcpu_svm *svm)
+{
+	u32 *msrpm = svm->msrpm;
+
+	svm->vmcb->control.lbr_ctl = 0;
+	set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
+	set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
+	set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
+	set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
+}
+
 static __init int svm_hardware_setup(void)
 {
 	int cpu;
@@ -1152,6 +1174,21 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
 	case MSR_IA32_SYSENTER_ESP:
 		*data = svm->vmcb->save.sysenter_esp;
 		break;
+	case MSR_IA32_DEBUGCTLMSR:
+		*data = svm->vmcb->save.dbgctl;
+		break;
+	case MSR_IA32_LASTBRANCHFROMIP:
+		*data = svm->vmcb->save.br_from;
+		break;
+	case MSR_IA32_LASTBRANCHTOIP:
+		*data = svm->vmcb->save.br_to;
+		break;
+	case MSR_IA32_LASTINTFROMIP:
+		*data = svm->vmcb->save.last_excp_from;
+		break;
+	case MSR_IA32_LASTINTTOIP:
+		*data = svm->vmcb->save.last_excp_to;
+		break;
 	default:
 		return kvm_get_msr_common(vcpu, ecx, data);
 	}
@@ -1224,6 +1261,15 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
 		if (data != 0)
 			goto unhandled;
 		break;
+	case MSR_IA32_DEBUGCTLMSR:
+		svm->vmcb->save.dbgctl = data;
+		if (!svm_has(SVM_FEATURE_LBRV))
+			break;
+		if (data & (1ULL<<0))
+			svm_enable_lbrv(svm);
+		else
+			svm_disable_lbrv(svm);
+		break;
 	default:
 	unhandled:
 		return kvm_set_msr_common(vcpu, ecx, data);
Signature · 9 lines
1.5.3.7




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

Re: [PATCH 3/3] KVM: SVM: enable LBRV virtualization

Quoted 7 lines · Joerg Roedel
arch/x86/kvm/svm.c +2 -0
@@ -1224,6 +1261,15 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
 		if (data != 0)
 			goto unhandled;
 		break;
+	case MSR_IA32_DEBUGCTLMSR:
+		svm->vmcb->save.dbgctl = data;

arch/x86/kvm/svm.c static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data) · line 1275

Quoted 10 lines
arch/x86/kvm/svm.c +7 -0
+		if (!svm_has(SVM_FEATURE_LBRV))
+			break;
+		if (data & (1ULL<<0))
+			svm_enable_lbrv(svm);
+		else
+			svm_disable_lbrv(svm);
+		break;
 	default:
 	unhandled:
 		return kvm_set_msr_common(vcpu, ecx, data);

This still has the same issue as the previous patchset: if the guest enables some other bit in MSR_IA32_DEBUCTLMSR, we silently ignore it. We should either pr_unimpl() on such bits or not handle them (ultimately injecting a #GP).

Also, I'd like a simple patch for 2.6.25 to add support for Windows x86 on AMD. So if the first patch in the series can add support for the bits that Windows sets in MSR_IA32_DEBUGCTLMSR (I imagine it just writes zero?) then I can queue that for 2.6.25 and the rest for 2.6.26.

Signature · 7 lines
Any sufficiently difficult bug is indistinguishable from a feature.


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

Re: [PATCH 3/3] KVM: SVM: enable LBRV virtualization

Quoted 14 lines · Avi Kivity
Quoted 13 lines · Joerg Roedel
arch/x86/kvm/svm.c +9 -0
@@ -1224,6 +1261,15 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
 		if (data != 0)
 			goto unhandled;
 		break;
+	case MSR_IA32_DEBUGCTLMSR:
+		svm->vmcb->save.dbgctl = data;
+		if (!svm_has(SVM_FEATURE_LBRV))
+			break;
+		if (data & (1ULL<<0))
+			svm_enable_lbrv(svm);
+		else
+			svm_disable_lbrv(svm);
+		break;
Quoted 8 lines

arch/x86/kvm/svm.c static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data) · line 1275

Quoted 4 lines
arch/x86/kvm/svm.c +0 -0
 	default:
 	unhandled:
 		return kvm_set_msr_common(vcpu, ecx, data);

This still has the same issue as the previous patchset: if the guest enables some other bit in MSR_IA32_DEBUCTLMSR, we silently ignore it. We should either pr_unimpl() on such bits or not handle them (ultimately injecting a #GP).

Thats not true. The patch saves the MSR value in vmcb->save.dbgctl. This value is returned on reads of that MSR. So no bit is ignored. This value in the VMCB is also used as the guests copy of that MSR if LBR virtualization is enabled. But another issue, I should ensure the guest does not set reserved bits in that MSR.

Quoted 4 lines

Also, I'd like a simple patch for 2.6.25 to add support for Windows x86 on AMD. So if the first patch in the series can add support for the bits that Windows sets in MSR_IA32_DEBUGCTLMSR (I imagine it just writes zero?) then I can queue that for 2.6.25 and the rest for 2.6.26.

Ok, I will work that into the patchset.

Joerg

Signature · 13 lines
           |           AMD Saxony Limited Liability Company & Co. KG
 Operating |         Wilschdorfer Landstr. 101, 01109 Dresden, Germany
 System    |                  Register Court Dresden: HRA 4896
 Research  |              General Partner authorized to represent:
 Center    |             AMD Saxony LLC (Wilmington, Delaware, US)
           | General Manager of AMD Saxony LLC: Dr. Hans-R. Deppe, Thomas McCoy



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

Re: [PATCH 3/3] KVM: SVM: enable LBRV virtualization

Quoted 11 lines · Joerg Roedel
Quoted 6 lines
Quoted 2 lines

This still has the same issue as the previous patchset: if the guest enables some other bit in MSR_IA32_DEBUCTLMSR, we silently ignore it. We should either pr_unimpl() on such bits or not handle them (ultimately injecting a #GP).

Thats not true. The patch saves the MSR value in vmcb->save.dbgctl. This value is returned on reads of that MSR. So no bit is ignored. This value in the VMCB is also used as the guests copy of that MSR if LBR virtualization is enabled.

Right, my mistake.

Signature · 7 lines
Any sufficiently difficult bug is indistinguishable from a feature.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#7 lore raw
In reply to Joerg Roedel
Quoted 6 lines · Joerg Roedel

This little patchset implements virtualization of the Last Branch Record feature (LBRV) into the KVM-AMD module where this is available in hardware. For
every non-LBRV capable hardware it only enables access to the 5 LBR MSRs. Without
this access Windows XP 64 bit crashes in the installation routine.

Does it mean that it's impossible to install Windows XP x64 on older AMD processors under KVM?
Or maybe just newer models crashed during install?

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

#8 lore raw
Quoted 10 lines · Maciej Kowalczyk
Quoted 4 lines · Joerg Roedel

This little patchset implements virtualization of the Last Branch Record feature (LBRV) into the KVM-AMD module where this is available in hardware. For every non-LBRV capable hardware it only enables access to the 5 LBR MSRs. Without this access Windows XP 64 bit crashes in the installation routine.

Does it mean that it's impossible to install Windows XP x64 on older AMD processors under KVM? Or maybe just newer models crashed during install?

Installation of XP 64 bit should work now on every AMD system which has support for KVM. The critical point here was the access to the MSRs. These MSRs are virtualized in newer AMD processors (Fam10h) and therefore I implemented this feature in this patchset too.

Joerg

Signature · 13 lines
           |           AMD Saxony Limited Liability Company & Co. KG
 Operating |         Wilschdorfer Landstr. 101, 01109 Dresden, Germany
 System    |                  Register Court Dresden: HRA 4896
 Research  |              General Partner authorized to represent:
 Center    |             AMD Saxony LLC (Wilmington, Delaware, US)
           | General Manager of AMD Saxony LLC: Dr. Hans-R. Deppe, Thomas McCoy



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

Hi all,

Can you explain a bit more about "LBRV virtualization" or give links, that explain this ?

-Alexey "Technologov"

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

_______________________________________________
kvm-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

#10 lore raw
Quoted 4 lines · Alexey Eremenko

Hi all,

Can you explain a bit more about "LBRV virtualization" or give links, that explain this ?

LBRV stands for Last Branch Record Virtualization. The LBR consists basically of 4 MSRs: The RIPs of the last branch (from and to RIP) and the RIPs of the last exception (from and to RIP). The recording of these MSRs need to enabled in a fifth MSR (DEBUG_CTL). On K8 with SVM the guest has no own copys of these MSRs. On Barcelona, there is a new SVM feature which gives the guest its own copies of these MSRs. This needs to be enabled setting lbr_ctl to 1 in the VMCB. If this is done, the hardware switches to the guest copies of these MSRs when doing a VMRUN. You can find more details about this in the AMD Architecture Programmers Manual Volume 2, Section 15.22.
My patch only enables LBRV if the guest enables Last Branch Recording in its copy of the DEBUG_CTL MSR.

Joerg

Signature · 13 lines
           |           AMD Saxony Limited Liability Company & Co. KG
 Operating |         Wilschdorfer Landstr. 101, 01109 Dresden, Germany
 System    |                  Register Court Dresden: HRA 4896
 Research  |              General Partner authorized to represent:
 Center    |             AMD Saxony LLC (Wilmington, Delaware, US)
           | General Manager of AMD Saxony LLC: Dr. Hans-R. Deppe, Thomas McCoy



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