From ffbfbb12263e732272fee4eeec3afed0593482c7 Mon Sep 17 00:00:00 2001
From: Sheng Yang <sheng.yang@intel.com>
Date: Wed, 15 Aug 2007 16:23:36 +0800
Subject: [PATCH] Fix CR8 support in kvm-userspace

This patch fixs CR8 support for x64 Windows. Now when writing to CR8 in the
guest OS, we exit to userspace and update tpr in qemu apic emulation.
We can boot x64 Windows XP with this patch now.

Signed-off-by: Sheng Yang <sheng.yang@intel.com>
---
 qemu/qemu-kvm.c |    3 ++-
 user/kvmctl.c   |    7 +++++++
 user/kvmctl.h   |   11 +++++++++++
 3 files changed, 20 insertions(+), 1 deletions(-)

diff --git a/qemu/qemu-kvm.c b/qemu/qemu-kvm.c
index e23f477..59cba64 100644
--- a/qemu/qemu-kvm.c
+++ b/qemu/qemu-kvm.c
@@ -497,7 +497,8 @@ static void post_kvm_run(void *opaque, int vcpu)
 	? env->eflags | IF_MASK : env->eflags & ~IF_MASK;
     env->ready_for_interrupt_injection
 	= kvm_is_ready_for_interrupt_injection(kvm_context, vcpu);
-    //cpu_set_apic_tpr(env, kvm_run->cr8);
+
+    cpu_set_apic_tpr(env, kvm_get_cr8(kvm_context, vcpu));
     cpu_set_apic_base(env, kvm_get_apic_base(kvm_context, vcpu));
 }
 
diff --git a/user/kvmctl.c b/user/kvmctl.c
index 43b374d..e211626 100644
--- a/user/kvmctl.c
+++ b/user/kvmctl.c
@@ -868,6 +868,11 @@ void kvm_set_cr8(kvm_context_t kvm, int vcpu, uint64_t cr8)
 	run->cr8 = cr8;
 }
 
+__u64 kvm_get_cr8(kvm_context_t kvm, int vcpu)
+{
+	return kvm->run[vcpu]->cr8;
+}
+
 static int kvm_run_abi10(kvm_context_t kvm, int vcpu)
 {
 	int r;
@@ -1007,6 +1012,8 @@ again:
 		case KVM_EXIT_SHUTDOWN:
 			r = handle_shutdown(kvm, vcpu);
 			break;
+		case KVM_EXIT_SET_TPR:
+			break;
 		default:
 			fprintf(stderr, "unhandled vm exit: 0x%x\n", run->exit_reason);
 			kvm_show_regs(kvm, vcpu);
diff --git a/user/kvmctl.h b/user/kvmctl.h
index f3f807e..b7af128 100644
--- a/user/kvmctl.h
+++ b/user/kvmctl.h
@@ -191,6 +191,17 @@ int kvm_is_ready_for_interrupt_injection(kvm_context_t kvm, int vcpu);
 void kvm_set_cr8(kvm_context_t kvm, int vcpu, uint64_t cr8);
 
 /*!
+ * \brief Get cr8 for sync tpr in qemu apic emulation
+ *
+ * This is a getter for cr8, which used to sync with the tpr in qemu
+ * apic emualtion.
+ *
+ * \param kvm Pointer to the current kvm_context
+ * \param vcpu Which virtual CPU should get dumped
+ */
+__u64 kvm_get_cr8(kvm_context_t kvm, int vcpu);
+
+/*!
  * \brief Read VCPU registers
  *
  * This gets the GP registers from the VCPU and outputs them
-- 
1.5.2

