[PATCH] KVM: x86: fix LAPIC pending count calculation

2009-01-22 Thread Avi Kivity
From: Marcelo Tosatti 

Simplify LAPIC TMCCT calculation by using hrtimer provided
function to query remaining time until expiration.

Fixes host hang with nested ESX.

Signed-off-by: Marcelo Tosatti 
Signed-off-by: Alexander Graf 
Signed-off-by: Avi Kivity 

diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c
index c019b8e..cf17ed5 100644
--- a/arch/x86/kvm/irq.c
+++ b/arch/x86/kvm/irq.c
@@ -87,13 +87,6 @@ void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_GPL(kvm_inject_pending_timer_irqs);
 
-void kvm_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
-{
-   kvm_apic_timer_intr_post(vcpu, vec);
-   /* TODO: PIT, RTC etc. */
-}
-EXPORT_SYMBOL_GPL(kvm_timer_intr_post);
-
 void __kvm_migrate_timers(struct kvm_vcpu *vcpu)
 {
__kvm_migrate_apic_timer(vcpu);
diff --git a/arch/x86/kvm/irq.h b/arch/x86/kvm/irq.h
index 2bf32a0..82579ee 100644
--- a/arch/x86/kvm/irq.h
+++ b/arch/x86/kvm/irq.h
@@ -89,7 +89,6 @@ static inline int irqchip_in_kernel(struct kvm *kvm)
 
 void kvm_pic_reset(struct kvm_kpic_state *s);
 
-void kvm_timer_intr_post(struct kvm_vcpu *vcpu, int vec);
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu);
 void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu);
 void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu);
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index afac68c..d8adc50 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -511,52 +511,22 @@ static void apic_send_ipi(struct kvm_lapic *apic)
 
 static u32 apic_get_tmcct(struct kvm_lapic *apic)
 {
-   u64 counter_passed;
-   ktime_t passed, now;
+   ktime_t remaining;
+   s64 ns;
u32 tmcct;
 
ASSERT(apic != NULL);
 
-   now = apic->timer.dev.base->get_time();
-   tmcct = apic_get_reg(apic, APIC_TMICT);
-
/* if initial count is 0, current count should also be 0 */
-   if (tmcct == 0)
+   if (apic_get_reg(apic, APIC_TMICT) == 0)
return 0;
 
-   if (unlikely(ktime_to_ns(now) <=
-   ktime_to_ns(apic->timer.last_update))) {
-   /* Wrap around */
-   passed = ktime_add(( {
-   (ktime_t) {
-   .tv64 = KTIME_MAX -
-   (apic->timer.last_update).tv64}; }
-  ), now);
-   apic_debug("time elapsed\n");
-   } else
-   passed = ktime_sub(now, apic->timer.last_update);
-
-   counter_passed = div64_u64(ktime_to_ns(passed),
-  (APIC_BUS_CYCLE_NS * 
apic->timer.divide_count));
-
-   if (counter_passed > tmcct) {
-   if (unlikely(!apic_lvtt_period(apic))) {
-   /* one-shot timers stick at 0 until reset */
-   tmcct = 0;
-   } else {
-   /*
-* periodic timers reset to APIC_TMICT when they
-* hit 0. The while loop simulates this happening N
-* times. (counter_passed %= tmcct) would also work,
-* but might be slower or not work on 32-bit??
-*/
-   while (counter_passed > tmcct)
-   counter_passed -= tmcct;
-   tmcct -= counter_passed;
-   }
-   } else {
-   tmcct -= counter_passed;
-   }
+   remaining = hrtimer_expires_remaining(&apic->timer.dev);
+   if (ktime_to_ns(remaining) < 0)
+   remaining = ktime_set(0, 0);
+
+   ns = ktime_to_ns(remaining) % apic->timer.period;
+   tmcct = div64_u64(ns, (APIC_BUS_CYCLE_NS * apic->timer.divide_count));
 
return tmcct;
 }
@@ -653,8 +623,6 @@ static void start_apic_timer(struct kvm_lapic *apic)
 {
ktime_t now = apic->timer.dev.base->get_time();
 
-   apic->timer.last_update = now;
-
apic->timer.period = apic_get_reg(apic, APIC_TMICT) *
APIC_BUS_CYCLE_NS * apic->timer.divide_count;
atomic_set(&apic->timer.pending, 0);
@@ -1110,16 +1078,6 @@ void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
}
 }
 
-void kvm_apic_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
-{
-   struct kvm_lapic *apic = vcpu->arch.apic;
-
-   if (apic && apic_lvt_vector(apic, APIC_LVTT) == vec)
-   apic->timer.last_update = ktime_add_ns(
-   apic->timer.last_update,
-   apic->timer.period);
-}
-
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
 {
int vector = kvm_apic_has_interrupt(vcpu);
diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h
index 8185888..45ab6ee 100644
--- a/arch/x86/kvm/lapic.h
+++ b/arch/x86/kvm/lapic.h
@@ -12,7 +12,6 @@ struct kvm_lapic {
atomic_t pending;
s64 period; /* unit: ns */
u32 divide_count;
-   ktime_t last_u

[PATCH] kvm: external module: compatibility for hrtimer_expires_remaining()

2009-01-22 Thread Avi Kivity
From: Avi Kivity 

Signed-off-by: Avi Kivity 

diff --git a/kernel/external-module-compat-comm.h 
b/kernel/external-module-compat-comm.h
index 5cb70b0..981dc96 100644
--- a/kernel/external-module-compat-comm.h
+++ b/kernel/external-module-compat-comm.h
@@ -613,6 +613,20 @@ static inline void kvm_hrtimer_start_expires(struct 
hrtimer *timer, int mode)
 
 #endif
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
+
+static inline ktime_t kvm_hrtimer_expires_remaining(const struct hrtimer 
*timer)
+{
+return ktime_sub(timer->expires, timer->base->get_time());
+}
+
+#else
+
+#define kvm_hrtimer_expires_remaining hrtimer_expires_remaining
+
+#endif
+
+
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
 
 static inline int pci_reset_function(struct pci_dev *dev)
diff --git a/kernel/ia64/hack-module.awk b/kernel/ia64/hack-module.awk
index a26d567..d0ef130 100644
--- a/kernel/ia64/hack-module.awk
+++ b/kernel/ia64/hack-module.awk
@@ -1,6 +1,7 @@
 BEGIN { split("INIT_WORK on_each_cpu smp_call_function " \
  "hrtimer_add_expires_ns hrtimer_get_expires " \
  "hrtimer_get_expires_ns hrtimer_start_expires " \
+ "hrtimer_expires_remaining " \
  "request_irq", compat_apis); }
 
 /MODULE_AUTHOR/ {
diff --git a/kernel/x86/hack-module.awk b/kernel/x86/hack-module.awk
index 1840c47..cc50856 100644
--- a/kernel/x86/hack-module.awk
+++ b/kernel/x86/hack-module.awk
@@ -1,6 +1,7 @@
 BEGIN { split("INIT_WORK tsc_khz desc_struct ldttss_desc64 desc_ptr " \
  "hrtimer_add_expires_ns hrtimer_get_expires " \
  "hrtimer_get_expires_ns hrtimer_start_expires " \
+ "hrtimer_expires_remaining " \
  "on_each_cpu relay_open request_irq" , compat_apis); }
 
 /^int kvm_init\(/ { anon_inodes = 1 }
--
To unsubscribe from this list: send the line "unsubscribe kvm-commits" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH] kvm: external module: Fix build for VT-d/AMD IOMMU

2009-01-22 Thread Avi Kivity
From: Sheng Yang 

The vtd.c has renamed to iommu.c, and config option has changed to
CONFIG_IOMMU_API.

Notice now the host kernel before 2.6.29 can't work with VT-d due to API
changed... At least this patch enabled building with host kernel before 2.6.29
with CONFIG_DMAR.

Signed-off-by: Wei Huang 
Signed-off-by: Sheng Yang 
Signed-off-by: Avi Kivity 

diff --git a/kernel/ia64/Kbuild b/kernel/ia64/Kbuild
index 130ec45..5bc6098 100644
--- a/kernel/ia64/Kbuild
+++ b/kernel/ia64/Kbuild
@@ -3,8 +3,8 @@ obj-m := kvm.o kvm-intel.o
 kvm-objs := kvm_main.o ioapic.o coalesced_mmio.o kvm-ia64.o kvm_fw.o \
irq_comm.o ../anon_inodes.o ../external-module-compat.o
 
-ifeq ($(CONFIG_DMAR),y)
-kvm-objs += vtd.o
+ifeq ($(CONFIG_IOMMU_API),y)
+kvm-objs += iommu.o
 endif
 
 EXTRA_CFLAGS_vcpu.o += -mfixed-range=f2-f5,f12-f127
diff --git a/kernel/x86/Kbuild b/kernel/x86/Kbuild
index c4723b1..4ef1168 100644
--- a/kernel/x86/Kbuild
+++ b/kernel/x86/Kbuild
@@ -9,8 +9,8 @@ kvm-objs := kvm_main.o x86.o mmu.o x86_emulate.o 
../anon_inodes.o irq.o i8259.o
 ifeq ($(EXT_CONFIG_KVM_TRACE),y)
 kvm-objs += kvm_trace.o
 endif
-ifeq ($(CONFIG_DMAR),y)
-kvm-objs += vtd.o
+ifeq ($(CONFIG_IOMMU_API),y)
+kvm-objs += iommu.o
 endif
 kvm-intel-objs := vmx.o vmx-debug.o ../external-module-compat.o
 kvm-amd-objs := svm.o ../external-module-compat.o
--
To unsubscribe from this list: send the line "unsubscribe kvm-commits" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH] Merge branch 'qemu-cvs'

2009-01-22 Thread Avi Kivity
From: Avi Kivity 

Conflicts:
qemu/Makefile.target
qemu/hw/cirrus_vga.c
qemu/hw/ide.c
qemu/pc-bios/bios.bin
qemu/vl.c

Signed-off-by: Avi Kivity 
--
To unsubscribe from this list: send the line "unsubscribe kvm-commits" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html