Skip to contentSkip to conversation

fake ACPI C2 emulation v2

PATCH In review Marcelo Tosatti 5 participants 27 messages May 29, 2008 – Jun 5, 2008

Subsystems (5)
lore
Share & tools
lore
#14 lore raw

PATCH 5/12 KVM: in-kernel ACPI timer emulation

In reply to Marcelo Tosatti

C1 emulation reads the pmtimer very often, so move ACPI pmtimer to kernel space.

arch/x86/kvm/Makefile +1 -1
Index: kvm/arch/x86/kvm/Makefile
===================================================================
@@ -10,7 +10,7 @@ endif
 EXTRA_CFLAGS += -Ivirt/kvm -Iarch/x86/kvm
 
 kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o \
-	i8254.o
+	i8254.o acpi.o
 obj-$(CONFIG_KVM) += kvm.o
 kvm-intel-objs = vmx.o
 obj-$(CONFIG_KVM_INTEL) += kvm-intel.o
arch/x86/kvm/acpi.c new file +103 -0

103 lines not on this page · raw message

arch/x86/kvm/acpi.h new file +19 -0
Index: kvm/arch/x86/kvm/acpi.h
===================================================================
@@ -0,0 +1,19 @@
+#ifndef __KVM_ACPI_H
+#define __KVM_ACPI_H
+
+struct kvm_acpi_timer {
+	struct kvm_io_device dev;
+	struct kvm *kvm;
+	int pmtmr_offset;
+	struct kvm_acpi_timer_state pmtmr_state;
+};
+
+int kvm_acpi_init(struct kvm *kvm);
+
+int kvm_vm_ioctl_get_acpi_timer(struct kvm *kvm,
+				struct kvm_acpi_timer_state *pmtmr);
+
+int kvm_vm_ioctl_set_acpi_timer(struct kvm *kvm,
+				struct kvm_acpi_timer_state *pmtmr);
+
+#endif
arch/x86/kvm/x86.c +27 -0
Index: kvm/arch/x86/kvm/x86.c
===================================================================
@@ -19,6 +19,7 @@
 #include "mmu.h"
 #include "i8254.h"
 #include "tss.h"
+#include "acpi.h"
 
 #include <linux/clocksource.h>
 #include <linux/kvm.h>
@@ -794,6 +795,7 @@ int kvm_dev_ioctl_check_extension(long e
 	case KVM_CAP_NOP_IO_DELAY:
 	case KVM_CAP_MP_STATE:
 	case KVM_CAP_OPEN_IOPORT:
+	case KVM_CAP_ACPI_TIMER:
 		r = 1;
 		break;
 	case KVM_CAP_VAPIC:
@@ -1742,6 +1744,31 @@ long kvm_arch_vm_ioctl(struct file *filp
 			goto out;
 		break;
 	}
+	case KVM_CREATE_ACPI_TIMER: {
+		r = kvm_acpi_init(kvm);
+		break;
+	}
+	case KVM_GET_ACPI_TIMER: {
+		struct kvm_acpi_timer_state acpi_timer;
+		r = kvm_vm_ioctl_get_acpi_timer(kvm, &acpi_timer);
+		if (r)
+			goto out;
+		r = -EFAULT;
+		if (copy_to_user(argp, &acpi_timer, sizeof acpi_timer))
+			goto out;
+		r = 0;
+		break;
+	}
+	case KVM_SET_ACPI_TIMER: {
+		struct kvm_acpi_timer_state acpi_timer;
+		r = -EFAULT;
+		if (copy_from_user(&acpi_timer, argp, sizeof acpi_timer))
+			goto out;
+		r = kvm_vm_ioctl_set_acpi_timer(kvm, &acpi_timer);
+		if (r)
+			goto out;
+		break;
+	}
 	default:
 		;
 	}
include/linux/kvm.h +4 -0
Index: kvm/include/linux/kvm.h
===================================================================
@@ -347,6 +347,7 @@ struct kvm_trace_rec {
 #define KVM_CAP_PV_MMU 13
 #define KVM_CAP_MP_STATE 14
 #define KVM_CAP_OPEN_IOPORT 15
+#define KVM_CAP_ACPI_TIMER 16
 
 /*
  * ioctls for VM fds
@@ -373,6 +374,9 @@ struct kvm_trace_rec {
 #define KVM_GET_PIT		  _IOWR(KVMIO, 0x65, struct kvm_pit_state)
 #define KVM_SET_PIT		  _IOR(KVMIO,  0x66, struct kvm_pit_state)
 #define KVM_SET_OPEN_IOPORT	  _IOR(KVMIO,  0x67, struct kvm_ioport_list)
+#define KVM_CREATE_ACPI_TIMER	  _IO(KVMIO,  0x68)
+#define KVM_GET_ACPI_TIMER	  _IOWR(KVMIO, 0x69, struct kvm_acpi_timer_state)
+#define KVM_SET_ACPI_TIMER	  _IOR(KVMIO, 0x70, struct kvm_acpi_timer_state)
 
 /*
  * ioctls for vcpu fds
include/asm-x86/kvm.h +6 -0
Index: kvm/include/asm-x86/kvm.h
===================================================================
@@ -220,6 +220,12 @@ struct kvm_ioport_list {
 	struct kvm_ioport ioports[0];
 };
 
+/* for KVM_GET_ACPI_TIMER and KVM_SET_ACPI_TIMER */
+struct kvm_acpi_timer_state {
+	__u32 base_address;
+	__u32 timer_val;
+};
+
 #define KVM_TRC_INJ_VIRQ         (KVM_TRC_HANDLER + 0x02)
 #define KVM_TRC_REDELIVER_EVT    (KVM_TRC_HANDLER + 0x03)
 #define KVM_TRC_PEND_INTR        (KVM_TRC_HANDLER + 0x04)
include/asm-x86/kvm_host.h +1 -0
Index: kvm/include/asm-x86/kvm_host.h
===================================================================
@@ -318,6 +318,7 @@ struct kvm_arch{
 	struct kvm_pic *vpic;
 	struct kvm_ioapic *vioapic;
 	struct kvm_pit *vpit;
+	struct kvm_acpi_timer *vacpi_timer;
 
 	int round_robin_prev_vcpu;
 	unsigned int tss_addr;
#17 lore raw

PATCH 8/12 QEMU/KVM: non-virtualized ACPI PMTimer support

In reply to Marcelo Tosatti

QEMU support for direct pmtimer reads. Hopefully its safe, since its a read-only register ?

With self-disable C2 + this I'm seeing less CPU usage when idle with CONFIG_CPU_IDLE enabled. Quite noticeable on SMP guests. Windows XP is comparable to standard (never seen it consume less than 10% either way, usually 20-30%).

On migration the destination host can either lack ACPI or have the timer in a different IO port, so emulation is necessary.

Or luckily the pmtimer is in the same address. Since the 24-bit counter overflow period is only ~= 4.6 seconds, its probably worthwhile to wait for synchronization before restarting the guest. Not implemented though.

bios/rombios32.c +8 -2
Index: kvm-userspace.realtip/bios/rombios32.c
===================================================================
@@ -391,7 +391,7 @@ uint8_t bios_uuid[16];
 unsigned long ebda_cur_addr;
 #endif
 int acpi_enabled;
-uint32_t pm_io_base, smb_io_base;
+uint32_t pm_io_base, pmtmr_base, smb_io_base;
 int pm_sci_int;
 unsigned long bios_table_cur_addr;
 unsigned long bios_table_end_addr;
@@ -819,6 +819,12 @@ static void pci_bios_init_device(PCIDevi
         pci_config_writeb(d, PCI_INTERRUPT_LINE, 9);
 
         pm_io_base = PM_IO_BASE;
+        pmtmr_base = cmos_readb(0x60);
+        pmtmr_base |= cmos_readb(0x61) << 8;
+        pmtmr_base |= cmos_readb(0x62) << 16;
+        pmtmr_base |= cmos_readb(0x63) << 24;
+        if (!pmtmr_base)
+            pmtmr_base = pm_io_base + 0x08;
         pci_config_writel(d, 0x40, pm_io_base | 1);
         pci_config_writeb(d, 0x80, 0x01); /* enable PM io space */
         smb_io_base = SMB_IO_BASE;
@@ -1376,7 +1382,7 @@ void acpi_bios_init(void)
     fadt->acpi_disable = 0xf0;
     fadt->pm1a_evt_blk = cpu_to_le32(pm_io_base);
     fadt->pm1a_cnt_blk = cpu_to_le32(pm_io_base + 0x04);
-    fadt->pm_tmr_blk = cpu_to_le32(pm_io_base + 0x08);
+    fadt->pm_tmr_blk = cpu_to_le32(pmtmr_base);
     fadt->pm1_evt_len = 4;
     fadt->pm1_cnt_len = 2;
     fadt->pm_tmr_len = 4;
qemu/hw/acpi.c +67 -17

159 lines not on this page · raw message

qemu/hw/pc.c +5 -0
Index: kvm-userspace.realtip/qemu/hw/pc.c
===================================================================
@@ -253,6 +253,11 @@ static void cmos_init(ram_addr_t ram_siz
     }
     rtc_set_memory(s, 0x5f, smp_cpus - 1);
 
+    rtc_set_memory(s, 0x60, host_pmtimer_base);
+    rtc_set_memory(s, 0x61, host_pmtimer_base >> 8);
+    rtc_set_memory(s, 0x62, host_pmtimer_base >> 16);
+    rtc_set_memory(s, 0x63, host_pmtimer_base >> 24);
+
     if (ram_size > (16 * 1024 * 1024))
         val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
     else
qemu/qemu-kvm-x86.c +122 -0

140 lines not on this page · raw message

qemu/qemu-kvm.c +1 -0
Index: kvm-userspace.realtip/qemu/qemu-kvm.c
===================================================================
@@ -677,6 +677,7 @@ int kvm_qemu_create_context(void)
     r = kvm_arch_qemu_create_context();
     if(r <0)
 	kvm_qemu_destroy();
+    kvm_arch_qemu_init();
     return 0;
 }
 
qemu/qemu-kvm.h +3 -0
Index: kvm-userspace.realtip/qemu/qemu-kvm.h
===================================================================
@@ -49,6 +49,7 @@ void kvm_cpu_destroy_phys_mem(target_phy
 			      unsigned long size);
 
 int kvm_arch_qemu_create_context(void);
+int kvm_arch_qemu_init(void);
 
 void kvm_arch_save_regs(CPUState *env);
 void kvm_arch_load_regs(CPUState *env);
@@ -60,6 +61,8 @@ int kvm_arch_has_work(CPUState *env);
 int kvm_arch_try_push_interrupts(void *opaque);
 void kvm_arch_update_regs_for_sipi(CPUState *env);
 void kvm_arch_cpu_reset(CPUState *env);
+int qemu_kvm_get_pmtimer(uint32_t *value);
+int kvm_close_direct_pmtimer(void);
 
 CPUState *qemu_kvm_cpu_env(int index);
 
qemu/sysemu.h +2 -0
Index: kvm-userspace.realtip/qemu/sysemu.h
===================================================================
@@ -94,6 +94,7 @@ extern int win2k_install_hack;
 extern int alt_grab;
 extern int usb_enabled;
 extern int smp_cpus;
+extern unsigned int host_pmtimer_base;
 extern int cursor_hide;
 extern int graphic_rotate;
 extern int no_quit;
@@ -101,6 +102,7 @@ extern int semihosting_enabled;
 extern int autostart;
 extern int old_param;
 extern int hpagesize;
+extern int no_direct_pmtimer;
 extern const char *bootp_filename;
 
 
qemu/vl.c +7 -0
Index: kvm-userspace.realtip/qemu/vl.c
===================================================================
@@ -209,6 +209,7 @@ int win2k_install_hack = 0;
 int usb_enabled = 0;
 static VLANState *first_vlan;
 int smp_cpus = 1;
+unsigned int host_pmtimer_base;
 const char *vnc_display;
 #if defined(TARGET_SPARC)
 #define MAX_CPUS 16
@@ -235,6 +236,7 @@ int time_drift_fix = 0;
 unsigned int kvm_shadow_memory = 0;
 const char *mem_path = NULL;
 int hpagesize = 0;
+int no_direct_pmtimer = 0;
 const char *cpu_vendor_string;
 #ifdef TARGET_ARM
 int old_param = 0;
@@ -7931,6 +7933,7 @@ enum {
     QEMU_OPTION_tdf,
     QEMU_OPTION_kvm_shadow_memory,
     QEMU_OPTION_mempath,
+    QEMU_OPTION_no_direct_pmtimer,
 };
 
 typedef struct QEMUOption {
@@ -8058,6 +8061,7 @@ const QEMUOption qemu_options[] = {
     { "clock", HAS_ARG, QEMU_OPTION_clock },
     { "startdate", HAS_ARG, QEMU_OPTION_startdate },
     { "mem-path", HAS_ARG, QEMU_OPTION_mempath },
+    { "no-direct-pmtimer", 0, QEMU_OPTION_no_direct_pmtimer },
     { NULL },
 };
 
@@ -8962,6 +8966,9 @@ int main(int argc, char **argv)
             case QEMU_OPTION_mempath:
 		mem_path = optarg;
 		break;
+            case QEMU_OPTION_no_direct_pmtimer:
+                no_direct_pmtimer = 1;
+                break;
             case QEMU_OPTION_name:
                 qemu_name = optarg;
                 break;
#18 lore raw

PATCH 9/12 libkvm: in-kernel ACPI pmtimer interface

In reply to Marcelo Tosatti
libkvm/kvm-common.h +4 -0
Index: kvm-userspace.realtip/libkvm/kvm-common.h
===================================================================
@@ -49,6 +49,10 @@ struct kvm_context {
 	int no_pit_creation;
 	/// in-kernel pit status
 	int pit_in_kernel;
+	// do not create in-kernel acpi timer if set
+	int no_acpi_timer_creation;
+	// in-kernel acpi timer status
+	int acpi_timer_in_kernel;
 };
 
 void init_slots(void);
libkvm/libkvm-x86.c +27 -0
Index: kvm-userspace.realtip/libkvm/libkvm-x86.c
===================================================================
@@ -166,6 +166,29 @@ int kvm_create_pit(kvm_context_t kvm)
 	return 0;
 }
 
+int kvm_create_acpi_timer(kvm_context_t kvm)
+{
+#ifdef KVM_CAP_ACPI_TIMER
+	int r;
+
+	kvm->acpi_timer_in_kernel = 0;
+	if (!kvm->no_acpi_timer_creation) {
+		r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_ACPI_TIMER);
+		if (r > 0) {
+	    		r = ioctl(kvm->vm_fd, KVM_CREATE_ACPI_TIMER);
+			if (r >= 0)
+				kvm->acpi_timer_in_kernel = 1;
+			else {
+				fprintf(stderr,
+					"Create kernel pmtimer failed\n");
+				return r;
+			}
+		}
+	}
+#endif
+	return 0;
+}
+
 int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
  			void **vm_mem)
 {
@@ -179,6 +202,10 @@ int kvm_arch_create(kvm_context_t kvm, u
 	if (r < 0)
 		return r;
 
+        r = kvm_create_acpi_timer(kvm);
+        if (r < 0)
+                return r;
+
 	return 0;
 }
 
libkvm/libkvm.c +10 -0
Index: kvm-userspace.realtip/libkvm/libkvm.c
===================================================================
@@ -277,6 +277,11 @@ void kvm_disable_pit_creation(kvm_contex
 	kvm->no_pit_creation = 1;
 }
 
+void kvm_disable_acpi_timer_creation(kvm_context_t kvm)
+{
+	kvm->no_acpi_timer_creation = 1;
+}
+
 int kvm_create_vcpu(kvm_context_t kvm, int slot)
 {
 	long mmap_size;
@@ -994,3 +999,8 @@ int kvm_pit_in_kernel(kvm_context_t kvm)
 {
 	return kvm->pit_in_kernel;
 }
+
+int kvm_acpi_timer_in_kernel(kvm_context_t kvm)
+{
+	return kvm->acpi_timer_in_kernel;
+}
libkvm/libkvm.h +17 -0
Index: kvm-userspace.realtip/libkvm/libkvm.h
===================================================================
@@ -117,6 +117,16 @@ void kvm_disable_irqchip_creation(kvm_co
 void kvm_disable_pit_creation(kvm_context_t kvm);
 
 /*!
+ * \brief Disable the in-kernel ACPI timer creation
+ *
+ * In-kernel acpi timer is enabled by default. If userspace acpi timer is to be used,
+ * this should be called prior to kvm_create().
+ *
+ *  \param kvm Pointer to the kvm_context
+ */
+void kvm_disable_acpi_timer_creation(kvm_context_t kvm);
+
+/*!
  * \brief Create new virtual machine
  *
  * This creates a new virtual machine, maps physical RAM to it, and creates a
@@ -599,6 +609,13 @@ int kvm_set_pit(kvm_context_t kvm, struc
 
 #endif
 
+/*!
+ * \brief Query whether in kernel acpi timer is used
+ *
+ *  \param kvm Pointer to the current kvm_context
+ */
+int kvm_acpi_timer_in_kernel(kvm_context_t kvm);
+
 #ifdef KVM_CAP_OPEN_IOPORT
 
 /*!
#13 lore raw

PATCH 4/12 KVM: move muldiv64 to x86.c, export

In reply to Marcelo Tosatti

This should probably to go lib/

arch/x86/kvm/i8254.c +0 -20
Index: kvm/arch/x86/kvm/i8254.c
===================================================================
@@ -45,26 +45,6 @@
 #define RW_STATE_WORD0 3
 #define RW_STATE_WORD1 4
 
-/* Compute with 96 bit intermediate result: (a*b)/c */
-static u64 muldiv64(u64 a, u32 b, u32 c)
-{
-	union {
-		u64 ll;
-		struct {
-			u32 low, high;
-		} l;
-	} u, res;
-	u64 rl, rh;
-
-	u.ll = a;
-	rl = (u64)u.l.low * (u64)b;
-	rh = (u64)u.l.high * (u64)b;
-	rh += (rl >> 32);
-	res.l.high = div64_u64(rh, c);
-	res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
-	return res.ll;
-}
-
 static void pit_set_gate(struct kvm *kvm, int channel, u32 val)
 {
 	struct kvm_kpit_channel_state *c =
arch/x86/kvm/x86.c +27 -0
Index: kvm/arch/x86/kvm/x86.c
===================================================================
@@ -4116,3 +4116,30 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu
 		smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);
 	put_cpu();
 }
+
+#ifndef CONFIG_X86_64
+#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
+#else
+#define mod_64(x, y) ((x) % (y))
+#endif
+
+/* Compute with 96 bit intermediate result: (a*b)/c */
+u64 muldiv64(u64 a, u32 b, u32 c)
+{
+	union {
+		u64 ll;
+		struct {
+			u32 low, high;
+		} l;
+	} u, res;
+	u64 rl, rh;
+
+	u.ll = a;
+	rl = (u64)u.l.low * (u64)b;
+	rh = (u64)u.l.high * (u64)b;
+	rh += (rl >> 32);
+	res.l.high = div64_u64(rh, c);
+	res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
+	return res.ll;
+}
+EXPORT_SYMBOL(muldiv64);
include/asm-x86/kvm_host.h +2 -0
Index: kvm/include/asm-x86/kvm_host.h
===================================================================
@@ -660,6 +660,8 @@ static inline void kvm_inject_gp(struct 
 	kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
 }
 
+u64 muldiv64(u64, u32, u32);
+
 #define ASM_VMX_VMCLEAR_RAX       ".byte 0x66, 0x0f, 0xc7, 0x30"
 #define ASM_VMX_VMLAUNCH          ".byte 0x0f, 0x01, 0xc2"
 #define ASM_VMX_VMRESUME          ".byte 0x0f, 0x01, 0xc3"
#19 lore raw

PATCH 10/12 QEMU/KVM: add option to disable in-kernel pmtimer emulation

In reply to Marcelo Tosatti
qemu/qemu-kvm.c +4 -0
Index: kvm-userspace.realtip/qemu/qemu-kvm.c
===================================================================
@@ -11,6 +11,7 @@
 int kvm_allowed = 1;
 int kvm_irqchip = 1;
 int kvm_pit = 1;
+int kvm_acpi_timer = 1;
 
 #include <assert.h>
 #include <string.h>
@@ -670,6 +671,9 @@ int kvm_qemu_create_context(void)
     if (!kvm_pit) {
         kvm_disable_pit_creation(kvm_context);
     }
+    if (!kvm_acpi_timer) {
+        kvm_disable_acpi_timer_creation(kvm_context);
+    }
     if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
 	kvm_qemu_destroy();
 	return -1;
qemu/qemu-kvm.h +2 -0
Index: kvm-userspace.realtip/qemu/qemu-kvm.h
===================================================================
@@ -111,10 +111,12 @@ extern kvm_context_t kvm_context;
 #define kvm_enabled() (kvm_allowed)
 #define qemu_kvm_irqchip_in_kernel() kvm_irqchip_in_kernel(kvm_context)
 #define qemu_kvm_pit_in_kernel() kvm_pit_in_kernel(kvm_context)
+#define qemu_kvm_acpi_timer_in_kernel() kvm_acpi_timer_in_kernel(kvm_context)
 #else
 #define kvm_enabled() (0)
 #define qemu_kvm_irqchip_in_kernel() (0)
 #define qemu_kvm_pit_in_kernel() (0)
+#define qemu_kvm_acpi_timer_in_kernel() (0)
 #endif
 
 void kvm_mutex_unlock(void);
qemu/vl.c +8 -0
Index: kvm-userspace.realtip/qemu/vl.c
===================================================================
@@ -7792,6 +7792,7 @@ static void help(int exitcode)
 #endif
 	   "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
 	   "-no-kvm-pit	    disable KVM kernel mode PIT\n"
+	   "-no-kvm-acpi-timer	    disable KVM kernel mode ACPI timer\n"
 #endif
 #ifdef TARGET_I386
            "-std-vga        simulate a standard VGA card with VESA Bochs Extensions\n"
@@ -7916,6 +7917,7 @@ enum {
     QEMU_OPTION_no_kvm,
     QEMU_OPTION_no_kvm_irqchip,
     QEMU_OPTION_no_kvm_pit,
+    QEMU_OPTION_no_kvm_acpi_timer,
     QEMU_OPTION_no_reboot,
     QEMU_OPTION_no_shutdown,
     QEMU_OPTION_show_cursor,
@@ -8005,6 +8007,7 @@ const QEMUOption qemu_options[] = {
 #endif
     { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip },
     { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit },
+    { "no-kvm-acpi-timer", 0, QEMU_OPTION_no_kvm_acpi_timer },
 #endif
 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
     { "g", 1, QEMU_OPTION_g },
@@ -8908,6 +8911,11 @@ int main(int argc, char **argv)
 		kvm_pit = 0;
 		break;
 	    }
+            case QEMU_OPTION_no_kvm_acpi_timer: {
+                extern int kvm_acpi_timer;
+                kvm_acpi_timer = 0;
+                break;
+            }
 #endif
             case QEMU_OPTION_usb:
                 usb_enabled = 1;
#21 lore raw

PATCH 12/12 QEMU/KVM: in-kernel pmtimer save/restore support

In reply to Marcelo Tosatti
qemu/hw/acpi.c +32 -2
Index: kvm-userspace.realtip/qemu/hw/acpi.c
===================================================================
@@ -86,8 +86,16 @@ static uint32_t get_pmtmr(PIIX4PMState *
 {
     uint32_t d;
     if (!s->direct_access) {
-        d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
-        d += s->pmtimer_offset;
+	if (qemu_kvm_acpi_timer_in_kernel()) {
+#ifdef KVM_CAP_ACPI_TIMER
+            struct kvm_acpi_timer_state acpi_state;
+            kvm_get_acpi_timer(kvm_context, &acpi_state);
+            return acpi_state.timer_val;
+#endif
+        } else {
+            d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
+            d += s->pmtimer_offset;
+        }
     } else
         qemu_kvm_get_pmtimer(&d);
 
@@ -482,6 +490,22 @@ static void pm_write_config(PCIDevice *d
         pm_io_space_update((PIIX4PMState *)d, 0);
 }
 
+#ifdef KVM_CAP_ACPI_TIMER
+static int kvm_restore_acpi_timer(uint32_t pmtmr_val, uint32_t pmtimer_base)
+{
+    struct kvm_acpi_timer_state acpi_state;
+    acpi_state.timer_val = pmtmr_val;
+    acpi_state.base_address = pmtimer_base;
+
+    return kvm_set_acpi_timer(kvm_context, &acpi_state);
+}
+#else
+static int kvm_restore_acpi_timer(uint32_t pmtmr_val, uint32_t pmtimer_base)
+{
+    return -EINVAL;
+}
+#endif
+
 static void pm_save(QEMUFile* f,void *opaque)
 {
     PIIX4PMState *s = opaque;
@@ -544,6 +568,12 @@ static int pm_load(QEMUFile* f,void* opa
             host_pmtimer_base = 0;
         }
 #endif
+        if (qemu_kvm_acpi_timer_in_kernel()) {
+            ret = kvm_restore_acpi_timer(pmtmr_val, s->pmtimer_base);
+            if (ret)
+                return ret;
+        }
+
         s->pmtimer_offset = pmtmr_val - get_pmtmr(s);
     }
 
#15 lore raw

PATCH 6/12 QEMU/KVM: self-disabling C2 emulation

In reply to Marcelo Tosatti

Inform C2 state support via ACPI's CST per-processor package's, but write an invalid latency value the first time the guest attempts to idle via P_LVL2 port.

This way the TSC is considered unreliable, and we get away with the costs relative to APIC timer broadcasts on enter/exit necessary for C1+.

It would be nice to fallback to plain hlt idle instead of C1, which does not use the pmtimer for idle measurement, but Linux guests with CONFIG_CPUIDLE enabled fallback to poll_idle instead which is very inefficient.

bios/acpi-dsdt.dsl +158 -1
Index: kvm-userspace.realtip/bios/acpi-dsdt.dsl
===================================================================
@@ -33,8 +33,20 @@ DefinitionBlock (
 		PRU, 8,
 		PRD, 8,
 	}
+        OperationRegion(PWNO, SystemIO, 0xb040, 0x02)
+        Field (PWNO, WordAcc, NoLock, WriteAsZeros)
+        {
+                PWC, 16,
+        }
 
-        Processor (CPU0, 0x00, 0x0000b010, 0x06) {Method (_STA) { Return(0xF)}}
+        Processor (CPU0, 0x00, 0x0000b010, 0x06) {
+            Method (_STA) { Return(0xF)}
+                Name(_CST, Package() {
+                    1, Package() {
+                        ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                        2, 2, 300},
+                    })
+        }
         Processor (CPU1, 0x01, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x01, 0x01, 0x1, 0x0, 0x0, 0x0})
             Method(_MAT, 0) {
@@ -44,6 +56,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPU2, 0x02, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x02, 0x02, 0x1, 0x0, 0x0, 0x0})
@@ -54,6 +71,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPU3, 0x03, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x03, 0x03, 0x1, 0x0, 0x0, 0x0})
@@ -64,6 +86,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPU4, 0x04, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x04, 0x04, 0x1, 0x0, 0x0, 0x0})
@@ -74,6 +101,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPU5, 0x05, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x05, 0x05, 0x1, 0x0, 0x0, 0x0})
@@ -84,6 +116,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPU6, 0x06, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x06, 0x06, 0x1, 0x0, 0x0, 0x0})
@@ -94,6 +131,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPU7, 0x07, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x07, 0x07, 0x1, 0x0, 0x0, 0x0})
@@ -104,6 +146,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPU8, 0x08, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x08, 0x08, 0x1, 0x0, 0x0, 0x0})
@@ -114,6 +161,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPU9, 0x09, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x09, 0x09, 0x1, 0x0, 0x0, 0x0})
@@ -124,6 +176,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPUA, 0x0a, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x0A, 0x0A, 0x1, 0x0, 0x0, 0x0})
@@ -134,6 +191,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPUB, 0x0b, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x0B, 0x0B, 0x1, 0x0, 0x0, 0x0})
@@ -144,6 +206,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPUC, 0x0c, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x0C, 0x0C, 0x1, 0x0, 0x0, 0x0})
@@ -154,6 +221,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPUD, 0x0d, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x0D, 0x0D, 0x1, 0x0, 0x0, 0x0})
@@ -164,6 +236,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
         Processor (CPUE, 0x0e, 0x0000b010, 0x06) {
             Name (TMP, Buffer(0x8) {0x0, 0x8, 0x0E, 0x0E, 0x1, 0x0, 0x0, 0x0})
@@ -174,6 +251,11 @@ DefinitionBlock (
             Method (_STA) {
                 Return(0xF)
             }
+            Name(_CST, Package() {
+                1, Package() {
+                    ResourceTemplate() {Register(SystemIO, 8, 0, 0xb014)},
+                    2, 2, 300},
+            })
         }
     }
 
@@ -1544,6 +1626,81 @@ DefinitionBlock (
             Return(0x01)
         }
         Method(_L06) {
+            If (And(\_PR.PWC, 0x1)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU0._CST, 1)), 2))
+                Notify(\_PR.CPU0, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x2)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU1._CST, 1)), 2))
+                Notify(\_PR.CPU1, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x4)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU2._CST, 1)), 2))
+                Notify(\_PR.CPU2, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x8)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU3._CST, 1)), 2))
+                Notify(\_PR.CPU3, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x10)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU4._CST, 1)), 2))
+                Notify(\_PR.CPU4, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x20)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU5._CST, 1)), 2))
+                Notify(\_PR.CPU5, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x40)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU6._CST, 1)), 2))
+                Notify(\_PR.CPU6, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x80)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU7._CST, 1)), 2))
+                Notify(\_PR.CPU7, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x100)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU8._CST, 1)), 2))
+                Notify(\_PR.CPU8, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x200)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPU9._CST, 1)), 2))
+                Notify(\_PR.CPU9, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x400)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPUA._CST, 1)), 2))
+                Notify(\_PR.CPUA, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x800)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPUB._CST, 1)), 2))
+                Notify(\_PR.CPUB, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x1000)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPUC._CST, 1)), 2))
+                Notify(\_PR.CPUC, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x2000)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPUD._CST, 1)), 2))
+                Notify(\_PR.CPUD, 0x81)
+            }
+
+            If (And(\_PR.PWC, 0x4000)) {
+                Store (0xfffff, Index (DeRefOf (Index (\_PR.CPUE._CST, 1)), 2))
+                Notify(\_PR.CPUE, 0x81)
+            }
+
             Return(0x01)
         }
         Method(_L07) {
qemu/hw/acpi.c +63 -0

106 lines not on this page · raw message

#16 lore raw

PATCH 7/12 libkvm: interface to KVM_SET_OPEN_IOPORT

In reply to Marcelo Tosatti
libkvm/libkvm.c +11 -0
Index: kvm-userspace.realtip/libkvm/libkvm.c
===================================================================
@@ -680,7 +680,18 @@ int kvm_set_irqchip(kvm_context_t kvm, s
 	}
 	return r;
 }
+#endif
+
+#ifdef KVM_CAP_OPEN_IOPORT
+int kvm_set_open_ioports(kvm_context_t kvm, struct kvm_ioport_list *ioport_list)
+{
+    int r;
 
+    r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_OPEN_IOPORT);
+    if (r > 0)
+        return ioctl(kvm->vm_fd, KVM_SET_OPEN_IOPORT, ioport_list);
+    return -ENOSYS;
+}
 #endif
 
 static int handle_io(kvm_context_t kvm, struct kvm_run *run, int vcpu)
libkvm/libkvm.h +10 -0
Index: kvm-userspace.realtip/libkvm/libkvm.h
===================================================================
@@ -599,6 +599,16 @@ int kvm_set_pit(kvm_context_t kvm, struc
 
 #endif
 
+#ifdef KVM_CAP_OPEN_IOPORT
+
+/*!
+ * \brief Set direct io port access
+ */
+
+int kvm_set_open_ioports(kvm_context_t kvm, struct kvm_ioport_list *ioport_list);
+#endif
+
+
 #ifdef KVM_CAP_VAPIC
 
 /*!
#12 lore raw

PATCH 3/12 KVM: allow userspace to open access to ACPI pmtimer

In reply to Marcelo Tosatti
arch/x86/kvm/x86.c +7 -1
Index: kvm/arch/x86/kvm/x86.c
===================================================================
@@ -27,6 +27,7 @@
 #include <linux/module.h>
 #include <linux/mman.h>
 #include <linux/highmem.h>
+#include <linux/acpi_pmtmr.h>
 
 #include <asm/uaccess.h>
 #include <asm/msr.h>
@@ -1765,13 +1766,18 @@ static void kvm_init_msr_list(void)
 
 static int kvm_init_ioport_list(void)
 {
-	allowed_open_ioports = kmalloc(sizeof(struct kvm_ioport) +
+	allowed_open_ioports = kmalloc(sizeof(struct kvm_ioport) * 2 +
 				       sizeof(struct kvm_ioport_list), GFP_KERNEL);
 	if (!allowed_open_ioports)
 		return -ENOMEM;
 	allowed_open_ioports->nranges = 1;
 	allowed_open_ioports->ioports[0].addr = 0x80;
 	allowed_open_ioports->ioports[0].len = 1;
+	if (pmtmr_ioport) {
+		allowed_open_ioports->nranges++;
+		allowed_open_ioports->ioports[1].addr = pmtmr_ioport;
+		allowed_open_ioports->ioports[1].len = 4;
+	}
 
 	return 0;
 }
#1 lore raw

PATCH 1/12 expose ACPI pmtimer to userspace (/dev/pmtimer)

In reply to Marcelo Tosatti

KVM wishes to allow direct guest access to the ACPI pmtimer. In that case QEMU/KVM has to read the current value for migration, so the proper syncing can be done on the destination.

This patch will not register the device if the chipset has an unreliable timer.

CC john stultz <[email protected]>
CC Thomas Gleixner <[email protected]>
drivers/char/Kconfig +7 -0
Index: kvm/drivers/char/Kconfig
===================================================================
@@ -1057,6 +1057,13 @@ config HPET_MMAP
 	  exposed to the user.  If this applies to your hardware,
 	  say N here.
 
+config ACPI_PMTIMER_DEV
+	tristate "ACPI PM-Timer device"
+	default n
+	depends on ACPI && X86_PM_TIMER
+	help
+	 Allow userspace to read the ACPI PM-Timer value.
+
 config HANGCHECK_TIMER
 	tristate "Hangcheck timer"
 	depends on X86 || IA64 || PPC64 || S390
drivers/char/Makefile +1 -0
Index: kvm/drivers/char/Makefile
===================================================================
@@ -112,6 +112,7 @@ obj-$(CONFIG_PS3_FLASH)		+= ps3flash.o
 
 obj-$(CONFIG_JS_RTC)		+= js-rtc.o
 js-rtc-y = rtc.o
+obj-$(CONFIG_ACPI_PMTIMER_DEV)	+= acpi_pmtimer.o
 
 # Files generated that shall be removed upon make clean
 clean-files := consolemap_deftbl.c defkeymap.c
drivers/char/acpi_pmtimer.c new file +61 -0

61 lines not on this page · raw message

drivers/clocksource/acpi_pm.c +13 -0
Index: kvm/drivers/clocksource/acpi_pm.c
===================================================================
@@ -30,6 +30,8 @@
  */
 u32 pmtmr_ioport __read_mostly;
 
+static int reliable_pmtimer;
+
 static inline u32 read_pmtmr(void)
 {
 	/* mask the output to 24 bits */
@@ -208,10 +210,21 @@ pm_good:
 	if (verify_pmtmr_rate() != 0)
 		return -ENODEV;
 
+	if (clocksource_acpi_pm.read == acpi_pm_read)
+		reliable_pmtimer = 1;
+
 	return clocksource_register(&clocksource_acpi_pm);
 }
 
+int pmtimer_is_reliable(void)
+{
+	return reliable_pmtimer;
+}
+
 /* We use fs_initcall because we want the PCI fixups to have run
  * but we still need to load before device_initcall
  */
 fs_initcall(init_acpi_pm_clocksource);
+
+EXPORT_SYMBOL(pmtmr_ioport);
+EXPORT_SYMBOL(pmtimer_is_reliable);
include/linux/acpi_pmtmr.h +2 -0
Index: kvm/include/linux/acpi_pmtmr.h
===================================================================
@@ -27,6 +27,8 @@ static inline u32 acpi_pm_read_early(voi
 
 extern void pmtimer_wait(unsigned);
 
+int pmtimer_is_reliable(void);
+
 #else
 
 static inline u32 acpi_pm_read_early(void)
#20 lore raw

PATCH 11/12 libkvm: interface for pmtimer save/restore

In reply to Marcelo Tosatti
libkvm/libkvm-x86.c +31 -0
Index: kvm-userspace.realtip/libkvm/libkvm-x86.c
===================================================================
@@ -418,6 +418,37 @@ int kvm_set_pit(kvm_context_t kvm, struc
 
 #endif
 
+#ifdef KVM_CAP_ACPI_TIMER
+
+int kvm_get_acpi_timer(kvm_context_t kvm, struct kvm_acpi_timer_state *s)
+{
+	int r;
+	if (!kvm->acpi_timer_in_kernel)
+		return 0;
+	r = ioctl(kvm->vm_fd, KVM_GET_ACPI_TIMER, s);
+	if (r != 0) {
+		r = -errno;
+		perror("kvm_get_acpi_timer");
+	}
+	return r;
+}
+
+int kvm_set_acpi_timer(kvm_context_t kvm, struct kvm_acpi_timer_state *s)
+{
+	int r;
+	if (!kvm->acpi_timer_in_kernel)
+		return 0;
+	r = ioctl(kvm->vm_fd, KVM_SET_ACPI_TIMER, s);
+	if (r != 0) {
+		r = -errno;
+		perror("kvm_set_acpi_timer");
+	}
+
+	return r;
+}
+
+#endif
+
 void kvm_show_code(kvm_context_t kvm, int vcpu)
 {
 #define SHOW_CODE_LEN 50
libkvm/libkvm.h +22 -0
Index: kvm-userspace.realtip/libkvm/libkvm.h
===================================================================
@@ -616,6 +616,28 @@ int kvm_set_pit(kvm_context_t kvm, struc
  */
 int kvm_acpi_timer_in_kernel(kvm_context_t kvm);
 
+#ifdef KVM_CAP_ACPI_TIMER
+
+#if defined(__i386__) || defined(__x86_64__)
+/*!
+ * \brief Get in kernel ACPI timer of the virtual domain
+ *
+ * \param kvm Pointer to the current kvm_context
+ * \param s ACPI timer state of the virtual domain
+ */
+int kvm_get_acpi_timer(kvm_context_t kvm, struct kvm_acpi_timer_state *s);
+
+/*!
+ * \brief Set in kernel ACPI timer of the virtual domain
+ *
+ * \param kvm Pointer to the current kvm_context
+ * \param s ACPI timer state of the virtual domain
+ */
+int kvm_set_acpi_timer(kvm_context_t kvm, struct kvm_acpi_timer_state *s);
+#endif
+
+#endif
+
 #ifdef KVM_CAP_OPEN_IOPORT
 
 /*!
#11 lore raw

PATCH 2/12 KVM: allow multiple IO bitmap pages, provide userspace interface

In reply to Marcelo Tosatti

Allow multiple IO bitmap pages and provide an interface for userspace to control which ports are open for a particular guest.

Only tested on Intel VMX.

arch/x86/kvm/kvm_svm.h +1 -0
Index: kvm/arch/x86/kvm/kvm_svm.h
===================================================================
@@ -41,6 +41,7 @@ struct vcpu_svm {
 	unsigned long host_dr7;
 
 	u32 *msrpm;
+	struct kvm_io_bitmap_page *io_bitmap_page;
 };
 
 #endif
arch/x86/kvm/svm.c +120 -8

190 lines not on this page · raw message

arch/x86/kvm/vmx.c +143 -35

257 lines not on this page · raw message

arch/x86/kvm/x86.c +118 -0

186 lines not on this page · raw message

include/asm-x86/kvm.h +11 -0
Index: kvm/include/asm-x86/kvm.h
===================================================================
@@ -209,6 +209,17 @@ struct kvm_pit_state {
 	struct kvm_pit_channel_state channels[3];
 };
 
+/* for KVM_SET_OPEN_IOPORT */
+struct kvm_ioport {
+	__u32 addr;
+	__u32 len;
+};
+
+struct kvm_ioport_list {
+	__u32 nranges;
+	struct kvm_ioport ioports[0];
+};
+
 #define KVM_TRC_INJ_VIRQ         (KVM_TRC_HANDLER + 0x02)
 #define KVM_TRC_REDELIVER_EVT    (KVM_TRC_HANDLER + 0x03)
 #define KVM_TRC_PEND_INTR        (KVM_TRC_HANDLER + 0x04)
include/asm-x86/kvm_host.h +13 -0
Index: kvm/include/asm-x86/kvm_host.h
===================================================================
@@ -78,6 +78,7 @@
 #define KVM_MIN_FREE_MMU_PAGES 5
 #define KVM_REFILL_PAGES 25
 #define KVM_MAX_CPUID_ENTRIES 40
+#define KVM_MAX_IOPORT_RANGES 100
 
 extern spinlock_t kvm_lock;
 extern struct list_head vm_list;
@@ -296,6 +297,12 @@ struct kvm_mem_alias {
 	gfn_t target_gfn;
 };
 
+struct kvm_io_bitmap_page {
+	struct page *page;
+	struct list_head io_bitmap_list;
+	struct kref ref;
+};
+
 struct kvm_arch{
 	int naliases;
 	struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
@@ -320,6 +327,9 @@ struct kvm_arch{
 
 	struct page *ept_identity_pagetable;
 	bool ept_identity_pagetable_done;
+
+	struct kvm_io_bitmap_page *io_bitmap_a;
+	struct kvm_io_bitmap_page *io_bitmap_b;
 };
 
 struct kvm_vm_stat {
@@ -429,6 +439,9 @@ struct kvm_x86_ops {
 
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
+
+	int (*open_io_ports)(struct kvm *kvm, struct kvm_ioport_list *ioports);
+	void (*release_io_bitmaps)(struct kvm *kvm);
 };
 
 extern struct kvm_x86_ops *kvm_x86_ops;
include/linux/kvm.h +2 -0
Index: kvm/include/linux/kvm.h
===================================================================
@@ -346,6 +346,7 @@ struct kvm_trace_rec {
 #define KVM_CAP_NOP_IO_DELAY 12
 #define KVM_CAP_PV_MMU 13
 #define KVM_CAP_MP_STATE 14
+#define KVM_CAP_OPEN_IOPORT 15
 
 /*
  * ioctls for VM fds
@@ -371,6 +372,7 @@ struct kvm_trace_rec {
 #define KVM_CREATE_PIT		  _IO(KVMIO,  0x64)
 #define KVM_GET_PIT		  _IOWR(KVMIO, 0x65, struct kvm_pit_state)
 #define KVM_SET_PIT		  _IOR(KVMIO,  0x66, struct kvm_pit_state)
+#define KVM_SET_OPEN_IOPORT	  _IOR(KVMIO,  0x67, struct kvm_ioport_list)
 
 /*
  * ioctls for vcpu fds
#0 lore raw

cover fake ACPI C2 emulation v2

Addressing comments on the previous patchset, follows:

- Same fake C2 emulation
- /dev/pmtimer
- Support for multiple IO bitmap pages + userspace interface
- In-kernel ACPI pmtimer emulation

Tested with Linux and WinXP guests. Also tested migration.

Please review.

#22 lore raw
Quoted 9 lines · Marcelo Tosatti

Addressing comments on the previous patchset, follows:

- Same fake C2 emulation
- /dev/pmtimer
- Support for multiple IO bitmap pages + userspace interface
- In-kernel ACPI pmtimer emulation

Tested with Linux and WinXP guests. Also tested migration.

Do you have any performance numbers, comparing qemu/kernel/passthrough?

[Real review will be delayed as I am travelling; will try to do as much as I can]

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw

Re: [patch 01/12] expose ACPI pmtimer to userspace (/dev/pmtimer)

In reply to Marcelo Tosatti
Quoted 3 lines · Marcelo Tosatti

KVM wishes to allow direct guest access to the ACPI pmtimer. In that case QEMU/KVM has to read the current value for migration, so the proper syncing can be done on the destination.

I don't understand from the above which problem you are trying to solve. Which pmtimer is read out, the one of the host (physical hardware) or the one of the guest (emulated hardware) ? What is synced at the destination ?

Quoted 2 lines

This patch will not register the device if the chipset has an unreliable timer.

Can we please keep that code inside of drivers/clocksource/acpi_pm.c without creating a new disconnected file in drivers/char ?

Btw, depending on the use case we might as well have a sysfs entry for that.

drivers/char/acpi_pmtimer.c line 18

Quoted 9 lines
drivers/char/acpi_pmtimer.c new file +9 -0
+static ssize_t pmtimer_read(struct file *file, char __user *buf, size_t count,
+			    loff_t *ppos)
+{
+	int ret;
+	__u32 value;
+
+	ret = -EINVAL;
+	if (count < sizeof(u32))
+		goto out;
    		return -EINVAL;

drivers/char/acpi_pmtimer.c line 24

Quoted 6 lines
drivers/char/acpi_pmtimer.c new file +6 -0
+
+	value = inl(pmtmr_ioport) & ACPI_PM_MASK;
+
+	ret = -EFAULT;
+	if (put_user(value, (u32 __user *)buf))
+		goto out;

return -EFAULT;

drivers/char/acpi_pmtimer.c line 26

Quoted 1 line
drivers/char/acpi_pmtimer.c new file +1 -0
+	ret = sizeof(value);
  	return sizeof(value);
Quoted 20 lines
drivers/char/acpi_pmtimer.c new file +3 -0
+out:
+	return ret;
+}

Index: kvm/drivers/clocksource/acpi_pm.c
===================================================================

drivers/clocksource/acpi_pm.c +2 -0
@@ -30,6 +30,8 @@
  */
 u32 pmtmr_ioport __read_mostly;
 
+static int reliable_pmtimer;
+
 static inline u32 read_pmtmr(void)
 {
 	/* mask the output to 24 bits */
@@ -208,10 +210,21 @@ pm_good:
 	if (verify_pmtmr_rate() != 0)
 		return -ENODEV;

drivers/clocksource/acpi_pm.c pm_good: · line 221

Quoted 10 lines
drivers/clocksource/acpi_pm.c +6 -0
 
+	if (clocksource_acpi_pm.read == acpi_pm_read)
+		reliable_pmtimer = 1;
+
 	return clocksource_register(&clocksource_acpi_pm);
 }
 
+int pmtimer_is_reliable(void)
+{
+	return reliable_pmtimer;
  	return clocksource_acpi_pm.read == acpi_pm_read;

So we don't need reliable_pmtimer at all.

Thanks,
tglx

#3 lore raw

Re: [patch 01/12] expose ACPI pmtimer to userspace (/dev/pmtimer)

Quoted 4 lines · Thomas Gleixner

Can we please keep that code inside of drivers/clocksource/acpi_pm.c without creating a new disconnected file in drivers/char ?

Btw, depending on the use case we might as well have a sysfs entry for that.

I think sysfs would actually make a lot of sense for this.

Regards,

Anthony Liguori

#7 lore raw

Re: [patch 01/12] expose ACPI pmtimer to userspace (/dev/pmtimer)

In reply to Thomas Gleixner
Quoted 9 lines · Thomas Gleixner
Quoted 3 lines · Marcelo Tosatti

KVM wishes to allow direct guest access to the ACPI pmtimer. In that case QEMU/KVM has to read the current value for migration, so the proper syncing can be done on the destination.

I don't understand from the above which problem you are trying to solve. Which pmtimer is read out, the one of the host (physical hardware) or the one of the guest (emulated hardware) ? What is synced at the destination ?

Problem is this:

We want to allow guests to directly access the hosts pmtimer (by using the I/O bitmap feature in VMX/SVM hardware). The advantage of doing it is that no VMExits are necessary for guest pmtimer reads (which happen often if we inform the guest that ACPI C1 state is supported, or if the workload is gettimeofday() intensive).

If you migrate such a guest that has direct (ie. non-virtualized, using the physical hardware) pmtimer access to a different host (destination), you need to save the current host pmtimer value at the time of migration so that you can either emulate it with a proper offset or synchronize (wait for the destination hosts real hardware pmtimer value to be in sync before actually resuming guest execution).

Quoted 7 lines
Quoted 2 lines

This patch will not register the device if the chipset has an unreliable timer.

Can we please keep that code inside of drivers/clocksource/acpi_pm.c without creating a new disconnected file in drivers/char ?

Btw, depending on the use case we might as well have a sysfs entry for that.

A sysfs entry sounds fine and much simpler. Should probably be a generic clocksource interface (so userspace can read any available clocksource) rather than acpi_pm specific.

<snip>

Quoted 3 lines
  	return clocksource_acpi_pm.read == acpi_pm_read;

So we don't need reliable_pmtimer at all.

For KVM's use case, we'd rather not allow direct pmtimer access if the host has an unreliable (buggy) chipset. But then, I doubt any of those older affected chipsets have HW virtualization support, so it shouldnt be an issue.

Thanks!

#8 lore raw

Re: [patch 01/12] expose ACPI pmtimer to userspace (/dev/pmtimer)

Quoted 5 lines · Marcelo Tosatti

On Sun, Jun 01, 2008 at 06:34:27PM +0200, Thomas Gleixner wrote:

A sysfs entry sounds fine and much simpler. Should probably be a generic clocksource interface (so userspace can read any available clocksource) rather than acpi_pm specific.

Agreed.

Quoted 6 lines
Quoted 3 lines
  	return clocksource_acpi_pm.read == acpi_pm_read;

So we don't need reliable_pmtimer at all.

For KVM's use case, we'd rather not allow direct pmtimer access if the host has an unreliable (buggy) chipset.

well, "return clocksource_acpi_pm.read == acpi_pm_read;" is supposed to do that just without an additional variable "reliable_pmtimer" :)

Quoted 2 lines

But then, I doubt any of those older affected chipsets have HW virtualization support, so it shouldnt be an issue.

It's exactly one old crappy chipset, which definitely has no HW virt support and therefor we just can use read_pmtmr() w/o checking for reliable or not.

Thanks,
tglx

#23 lore raw
In reply to Avi Kivity
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Marcelo Tosatti

Addressing comments on the previous patchset, follows:

- Same fake C2 emulation
- /dev/pmtimer
- Support for multiple IO bitmap pages + userspace interface
- In-kernel ACPI pmtimer emulation

Quoted 4 lines
Quoted 2 lines

Tested with Linux and WinXP guests. Also tested migration.

Do you have any performance numbers, comparing qemu/kernel/passthrough?

Test is 1 million gettimeofday calls, Xeon 1.60GHz with 4MB L2.

guest (qemu emulation):
cycles:1189759332

guest (in-kernel emulation):
cycles:628046412

guest (direct pmtimer):
cycles:230372934

host (TSC):
cycles:14862774

Quoted 2 lines

[Real review will be delayed as I am travelling; will try to do as much as I can]

OK!

#9 lore raw

Re: [patch 01/12] expose ACPI pmtimer to userspace (/dev/pmtimer)

In reply to Marcelo Tosatti
Quoted 19 lines · Marcelo Tosatti
Quoted 9 lines · Thomas Gleixner
Quoted 3 lines · Marcelo Tosatti

KVM wishes to allow direct guest access to the ACPI pmtimer. In that case QEMU/KVM has to read the current value for migration, so the proper syncing can be done on the destination.

I don't understand from the above which problem you are trying to solve. Which pmtimer is read out, the one of the host (physical hardware) or the one of the guest (emulated hardware) ? What is synced at the destination ?

Problem is this:

We want to allow guests to directly access the hosts pmtimer (by using the I/O bitmap feature in VMX/SVM hardware). The advantage of doing it is that no VMExits are necessary for guest pmtimer reads (which happen often if we inform the guest that ACPI C1 state is supported, or if the workload is gettimeofday() intensive).

Quoted 6 lines

If you migrate such a guest that has direct (ie. non-virtualized, using the physical hardware) pmtimer access to a different host (destination), you need to save the current host pmtimer value at the time of migration so that you can either emulate it with a proper offset or synchronize (wait for the destination hosts real hardware pmtimer value to be in sync before actually resuming guest execution)

I'm a little wary on this, another thing to catch here as well is host suspend-resume cycles that might reset the pmtimer.

Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

This patch will not register the device if the chipset has an unreliable timer.

Can we please keep that code inside of drivers/clocksource/acpi_pm.c without creating a new disconnected file in drivers/char ?

Btw, depending on the use case we might as well have a sysfs entry for that.

A sysfs entry sounds fine and much simpler. Should probably be a generic clocksource interface (so userspace can read any available clocksource) rather than acpi_pm specific.

Again, I'd be hesitant to expose this stuff to userland since if the counters reset (such as in the suspend/resume case) the applications may not be aware.

And if its a generic interface, we would then have to also export frequency and mask values. It just gets messy, so I'd avoid doing anything generic in exporting clocksources (since either userland wants specific hardware and is aware of all the known troubles it may have, or userland should use the existing kernel time interfaces).

thanks
-john

#10 lore raw

Re: [patch 01/12] expose ACPI pmtimer to userspace (/dev/pmtimer)

Quoted 9 lines · John Stultz
Quoted 6 lines

If you migrate such a guest that has direct (ie. non-virtualized, using the physical hardware) pmtimer access to a different host (destination), you need to save the current host pmtimer value at the time of migration so that you can either emulate it with a proper offset or synchronize (wait for the destination hosts real hardware pmtimer value to be in sync before actually resuming guest execution)

I'm a little wary on this, another thing to catch here as well is host suspend-resume cycles that might reset the pmtimer.

In that case (host resume from S-state) we can hold guest execution until the real hw timer is in proximity to the guests expectation, or fallback to emulation (but its not unsolvable).

This problem can happen now with the TSC since kvm's suspend routine isnt saving it.

Thanks for the reminder.

Quoted 16 lines
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

This patch will not register the device if the chipset has an unreliable timer.

Can we please keep that code inside of drivers/clocksource/acpi_pm.c without creating a new disconnected file in drivers/char ?

Btw, depending on the use case we might as well have a sysfs entry for that.

A sysfs entry sounds fine and much simpler. Should probably be a generic clocksource interface (so userspace can read any available clocksource) rather than acpi_pm specific.

Again, I'd be hesitant to expose this stuff to userland since if the counters reset (such as in the suspend/resume case) the applications may not be aware.

Quoted 5 lines

And if its a generic interface, we would then have to also export frequency and mask values. It just gets messy, so I'd avoid doing anything generic in exporting clocksources (since either userland wants specific hardware and is aware of all the known troubles it may have, or userland should use the existing kernel time interfaces).

Good point. Will go for the acpi_pm's private sysfs file.

#4 lore raw

Re: [patch 01/12] expose ACPI pmtimer to userspace (/dev/pmtimer)

In reply to Anthony Liguori
Quoted 8 lines · Anthony Liguori
Quoted 4 lines · Thomas Gleixner

Can we please keep that code inside of drivers/clocksource/acpi_pm.c without creating a new disconnected file in drivers/char ?

Btw, depending on the use case we might as well have a sysfs entry for that.

I think sysfs would actually make a lot of sense for this.

It's read many thousands of times per second. You don't want a read()/sprintf()/atoi() sequence every time.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#5 lore raw

Re: [patch 01/12] expose ACPI pmtimer to userspace (/dev/pmtimer)

Quoted 5 lines · Avi Kivity
Quoted 4 lines · Anthony Liguori
Quoted 3 lines · Thomas Gleixner

Can we please keep that code inside of drivers/clocksource/acpi_pm.c without creating a new disconnected file in drivers/char ?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Btw, depending on the use case we might as well have a sysfs entry for that.

I think sysfs would actually make a lot of sense for this.

It's read many thousands of times per second. You don't want a read()/sprintf()/atoi() sequence every time.

Eek, according to Andrea it's only used for migration purpose.

Thanks,
tglx

#6 lore raw

Re: [patch 01/12] expose ACPI pmtimer to userspace (/dev/pmtimer)

Quoted 12 lines · Thomas Gleixner
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Anthony Liguori
Quoted 6 lines · Thomas Gleixner

Can we please keep that code inside of drivers/clocksource/acpi_pm.c without creating a new disconnected file in drivers/char ?

Btw, depending on the use case we might as well have a sysfs entry for that.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

I think sysfs would actually make a lot of sense for this.

It's read many thousands of times per second. You don't want a read()/sprintf()/atoi() sequence every time.

Eek, according to Andrea it's only used for migration purpose.

Oh, right. We also emulate pmtimer in qemu but it shouldn't need to read the host pmtimer.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#24 lore raw
In reply to Marcelo Tosatti
Quoted 22 lines · Marcelo Tosatti
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Marcelo Tosatti

Addressing comments on the previous patchset, follows:

- Same fake C2 emulation
- /dev/pmtimer
- Support for multiple IO bitmap pages + userspace interface
- In-kernel ACPI pmtimer emulation

Quoted 5 lines
Quoted 3 lines

Tested with Linux and WinXP guests. Also tested migration.

Do you have any performance numbers, comparing qemu/kernel/passthrough?

Test is 1 million gettimeofday calls, Xeon 1.60GHz with 4MB L2.

guest (qemu emulation):
cycles:1189759332

Quoted 10 lines

guest (in-kernel emulation):
cycles:628046412

guest (direct pmtimer):
cycles:230372934

host (TSC):
cycles:14862774

Ratio is 1:15:80

Looks like direct pmtimer is still quite slow. Are there any exits with direct pmtimer, or is it all due to the ioport latency?

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#25 lore raw
Quoted 12 lines · Avi Kivity
Quoted 12 lines

Test is 1 million gettimeofday calls, Xeon 1.60GHz with 4MB L2.

guest (qemu emulation):
cycles:1189759332

guest (in-kernel emulation):
cycles:628046412

guest (direct pmtimer):
cycles:230372934

Quoted 8 lines
Quoted 3 lines

host (TSC):
cycles:14862774

Ratio is 1:15:80

Looks like direct pmtimer is still quite slow. Are there any exits with direct pmtimer, or is it all due to the ioport latency?

host (pmtimer):
cycles:225768390

So its getting close-to-native performance. As you mentioned earlier, acpi_pm can't benefit from vsyscalls.

#26 lore raw
Quoted 19 lines · Marcelo Tosatti
Quoted 17 lines · Avi Kivity
Quoted 5 lines

Test is 1 million gettimeofday calls, Xeon 1.60GHz with 4MB L2.

guest (qemu emulation):
cycles:1189759332

Quoted 10 lines

guest (in-kernel emulation):
cycles:628046412

guest (direct pmtimer):
cycles:230372934

host (TSC):
cycles:14862774

Ratio is 1:15:80

Quoted 10 lines
Quoted 2 lines

Looks like direct pmtimer is still quite slow. Are there any exits with direct pmtimer, or is it all due to the ioport latency?

host (pmtimer):
cycles:225768390

So its getting close-to-native performance. As you mentioned earlier, acpi_pm can't benefit from vsyscalls.

Yes, but the host will use tsc (on a modern host with a stable tsc).

Signature · 1 line
error compiling committee.c: too many arguments to function