KVM: VMX: Only reload guest cr2 if different from host cr2
authorAvi Kivity <avi@redhat.com>
Tue, 16 Jun 2009 09:33:56 +0000 (12:33 +0300)
committerAvi Kivity <avi@redhat.com>
Thu, 10 Sep 2009 05:32:57 +0000 (08:32 +0300)
cr2 changes only rarely, and writing it is expensive.  Avoid the costly cr2
writes by checking if it does not already hold the desired value.

Shaves 70 cycles off the vmexit latency.

Signed-off-by: Avi Kivity <avi@redhat.com>
arch/x86/kvm/vmx.c

index fc8d49c6bc51606dbe8825d922239d455e8685fc..1a84ca191cd11c180c1d9cb6276ced5eef2f7069 100644 (file)
@@ -3651,11 +3651,16 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
                "mov %%"R"sp, %c[host_rsp](%0) \n\t"
                __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
                "1: \n\t"
+               /* Reload cr2 if changed */
+               "mov %c[cr2](%0), %%"R"ax \n\t"
+               "mov %%cr2, %%"R"dx \n\t"
+               "cmp %%"R"ax, %%"R"dx \n\t"
+               "je 2f \n\t"
+               "mov %%"R"ax, %%cr2 \n\t"
+               "2: \n\t"
                /* Check if vmlaunch of vmresume is needed */
                "cmpl $0, %c[launched](%0) \n\t"
                /* Load guest registers.  Don't clobber flags. */
-               "mov %c[cr2](%0), %%"R"ax \n\t"
-               "mov %%"R"ax, %%cr2 \n\t"
                "mov %c[rax](%0), %%"R"ax \n\t"
                "mov %c[rbx](%0), %%"R"bx \n\t"
                "mov %c[rdx](%0), %%"R"dx \n\t"