Skip to contentSkip to conversation

C2 "emulation"

PATCH In review Marcelo Tosatti 2 participants 18 messages May 25, 2008 – May 31, 2008

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

cover C2 "emulation"

As discussed before the TSC is unreliable as a clocksource for KVM guests, especially with CPU migration.

The following patchset changes the QEMU ACPI tables to report a valid C2 state temporarily to force guests to switch to more reliable ones.

Also allow direct access to the PMTimer port which is constantly used in C1 idle.

Tested with Linux and Windows XP.

#1 lore raw

PATCH 1/4 QEMU/KVM: self-disabling C2 emulation

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.tip/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

#2 lore raw

PATCH 2/4 libkvm: KVM_GET_PMTIMER ioctl support

In reply to Marcelo Tosatti

Unfortunately there seems to be no standard interface to read the pmtimer...

libkvm/libkvm.c +11 -0
Index: kvm-userspace.tip/libkvm/libkvm.c
===================================================================
@@ -794,6 +794,17 @@ int kvm_set_mpstate(kvm_context_t kvm, i
     return -ENOSYS;
 }
 #endif
+#ifdef KVM_CAP_GET_PMTIMER
+int kvm_get_pmtimer(kvm_context_t kvm, struct kvm_pmtimer *pmtmr)
+{
+    int r;
+
+    r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_GET_PMTIMER);
+    if (r > 0)
+        return ioctl(kvm->vm_fd, KVM_GET_PMTIMER, pmtmr);
+    return -ENOSYS;
+}
+#endif
 
 static int handle_mmio(kvm_context_t kvm, struct kvm_run *kvm_run)
 {
libkvm/libkvm.h +7 -0
Index: kvm-userspace.tip/libkvm/libkvm.h
===================================================================
@@ -325,6 +325,13 @@ static inline int kvm_reset_mpstate(kvm_
 }
 #endif
 
+#ifdef KVM_CAP_GET_PMTIMER
+/*!
+ * \brief Read host pmtimer value
+ */
+int kvm_get_pmtimer(kvm_context_t kvm, struct kvm_pmtimer *pmtmr);
+#endif
+
 /*!
  * \brief Simulate an external vectored interrupt
  *
#3 lore raw

PATCH 3/4 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.

Also simplify and fix the overflow emulation, which was happening every 2.3 seconds instead of the expected 4.6s.

bios/rombios32.c +8 -2
Index: kvm-userspace.tip/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;
@@ -1381,7 +1387,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 +75 -43

220 lines not on this page · raw message

qemu/hw/pc.c +5 -0
Index: kvm-userspace.tip/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 +58 -0

76 lines not on this page · raw message

qemu/qemu-kvm.c +1 -0
Index: kvm-userspace.tip/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.tip/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);
+
 
 CPUState *qemu_kvm_cpu_env(int index);
 
qemu/sysemu.h +2 -0
Index: kvm-userspace.tip/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.tip/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;
#4 lore raw

Re: [patch 3/4] QEMU/KVM: non-virtualized ACPI PMTimer support

Quoted 8 lines · 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%).

This is because the TPR optimization hack is disabled on smp. You need a machine with FlexPriority to get usable smp Windows.

Quoted 24 lines

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.

Also simplify and fix the overflow emulation, which was happening every 2.3 seconds instead of the expected 4.6s.

Index: kvm-userspace.tip/bios/rombios32.c
===================================================================

bios/rombios32.c +1 -1
@@ -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;

bios/rombios32.c static void pci_bios_init_device(PCIDevi · line 828

Quoted 10 lines
bios/rombios32.c +6 -0
@@ -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;
 

You're splitting the ACPI ioport range into two. I think the correct fix here is to have qemu supply a PMBA hint to the BIOS. If the hint is present, the bios should locate pm_io_base there, and should also avoid placing other pio resources there.

bios/rombios32.c static void pci_bios_init_device(PCIDevi

Quoted 10 lines
bios/rombios32.c +9 -0
+static void schedule_pmtmr_sci(PIIX4PMState *s)
+{
+    int64_t expire_time;
+    uint32_t pmtmr, left;
+
+    if (s->direct_access)
+        qemu_kvm_get_pmtimer(&pmtmr);
+    else
+        pmtmr = get_pmtmr(s);

get_pmtmr() should have this logic.

bios/rombios32.c

Quoted 3 lines
bios/rombios32.c +2 -0
+
+    left = (1 << 24) - pmtmr;

The docs say that SCI is generated when bit 23 toggles, not on overflow. See TMROF_STS PIIX4 documentation.

In any case, this should be in a separate patch.

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

Re: [patch 3/4] QEMU/KVM: non-virtualized ACPI PMTimer support

Quoted 9 lines · Avi Kivity
Quoted 9 lines

int acpi_enabled;

bios/rombios32.c +1 -1
-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;

bios/rombios32.c +0 -0
@@ -819,6 +819,12 @@ static void pci_bios_init_device(PCIDevi
        pci_config_writeb(d, PCI_INTERRUPT_LINE, 9);
 
Quoted 12 lines

bios/rombios32.c static void pci_bios_init_device(PCIDevi

Quoted 8 lines
bios/rombios32.c +6 -0
        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;
 

You're splitting the ACPI ioport range into two. I think the correct fix here is to have qemu supply a PMBA hint to the BIOS. If the hint is placing other pio resources there.

What is PMBA?

Quoted 1 line

From my understand ACPI supports an address for each register block, and

the PMTimer resides in a separate block. So what is the problem with having different ACPI blocks in different ports?

Note that the GPE0 registers are in a different port range than PM1EVT/PM1CNT/PMTimer already.

Quoted 13 lines

qemu/hw/acpi.c static uint32_t get_pmtmr(PIIX4PMState * · line 107

Quoted 10 lines
qemu/hw/acpi.c +9 -0
+static void schedule_pmtmr_sci(PIIX4PMState *s)
+{
+    int64_t expire_time;
+    uint32_t pmtmr, left;
+
+    if (s->direct_access)
+        qemu_kvm_get_pmtimer(&pmtmr);
+    else
+        pmtmr = get_pmtmr(s);

get_pmtmr() should have this logic.

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+    left = (1 << 24) - pmtmr;

The docs say that SCI is generated when bit 23 toggles, not on overflow. See TMROF_STS PIIX4 documentation.

Yeah, misread the docs, as usual.

Quoted 5 lines

In any case, this should be in a separate patch.

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

Re: [patch 3/4] QEMU/KVM: non-virtualized ACPI PMTimer support

Quoted 23 lines · Marcelo Tosatti
Quoted 9 lines · Avi Kivity
Quoted 9 lines

int acpi_enabled;

bios/rombios32.c +1 -1
-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;

bios/rombios32.c +0 -0
@@ -819,6 +819,12 @@ static void pci_bios_init_device(PCIDevi
        pci_config_writeb(d, PCI_INTERRUPT_LINE, 9);
 
Quoted 11 lines

bios/rombios32.c static void pci_bios_init_device(PCIDevi

Quoted 9 lines
bios/rombios32.c +6 -0
        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;
 
 

You're splitting the ACPI ioport range into two. I think the correct fix here is to have qemu supply a PMBA hint to the BIOS. If the hint is placing other pio resources there.

Quoted 3 lines

What is PMBA?

Power Management Base Address, which must equal the value of pm_io_base above.

Quoted 4 lines

From my understand ACPI supports an address for each register block, and the PMTimer resides in a separate block. So what is the problem with having different ACPI blocks in different ports?

The particular chipset we emulate has all blocks in one contiguous region starting at the PMBA.

Quoted 4 lines

Note that the GPE0 registers are in a different port range than PM1EVT/PM1CNT/PMTimer already.

That's sucky. piix4 supports GPIO pins, we should have emulated them instead of inventing our own.

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

Re: [patch 3/4] QEMU/KVM: non-virtualized ACPI PMTimer support

In reply to Avi Kivity
Quoted 9 lines · Avi Kivity

bios/rombios32.c static void pci_bios_init_device(PCIDevi · line 827

Quoted 8 lines
        pm_io_base = PM_IO_BASE;
bios/rombios32.c +6 -0
+        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;
Quoted 4 lines

You're splitting the ACPI ioport range into two. I think the correct fix here is to have qemu supply a PMBA hint to the BIOS. If the hint is present, the bios should locate pm_io_base there, and should also avoid placing other pio resources there.

PBLK_BASE (processor block) is statically defined in acpi-dsdt.dsl, and I don't see any easy way to change that dynamically.

In practice I don't see any problem with not having the PMBA registers in a contiguous range, since I doubt any ACPI implementation will assume PIIX4 specific details (ACPI driver looks for the register addresses in FADT, so does not matter if they're contiguous or not).

#8 lore raw

Re: [patch 3/4] QEMU/KVM: non-virtualized ACPI PMTimer support

Quoted 14 lines · Marcelo Tosatti
Quoted 11 lines · Avi Kivity

bios/rombios32.c static void pci_bios_init_device(PCIDevi · line 827

Quoted 9 lines
        pm_io_base = PM_IO_BASE;
bios/rombios32.c +6 -0
+        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;

You're splitting the ACPI ioport range into two. I think the correct fix here is to have qemu supply a PMBA hint to the BIOS. If the hint is present, the bios should locate pm_io_base there, and should also avoid placing other pio resources there.

Quoted 8 lines

PBLK_BASE (processor block) is statically defined in acpi-dsdt.dsl, and I don't see any easy way to change that dynamically.

In practice I don't see any problem with not having the PMBA registers in a contiguous range, since I doubt any ACPI implementation will assume PIIX4 specific details (ACPI driver looks for the register addresses in FADT, so does not matter if they're contiguous or not).

Right, it's a cleanliness issue, not correctness.

The only guest I can think of that will care about the actual layout is linuxbios/coreboot.

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

Re: [patch 3/4] QEMU/KVM: non-virtualized ACPI PMTimer support

In reply to Marcelo Tosatti
Quoted 3 lines · Marcelo Tosatti

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

ISTR that pmtimer can be either 24 bit or 32 bit. Can this cause a problem?

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

Re: [patch 3/4] QEMU/KVM: non-virtualized ACPI PMTimer support

Quoted 6 lines · Avi Kivity
Quoted 3 lines · Marcelo Tosatti

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

ISTR that pmtimer can be either 24 bit or 32 bit. Can this cause a problem?

Don't think so, since QEMU/KVM reports a 24-bit timer (tmr_val_ext field of fadt is zero). If the host has a 32-bit timer, the higher bits are ignored, and TMR_OF is emulated.

#11 lore raw

Re: [patch 3/4] QEMU/KVM: non-virtualized ACPI PMTimer support

Quoted 8 lines · Marcelo Tosatti
Quoted 6 lines · Avi Kivity
Quoted 4 lines · Marcelo Tosatti

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

Quoted 7 lines
Quoted 2 lines

ISTR that pmtimer can be either 24 bit or 32 bit. Can this cause a problem?

Don't think so, since QEMU/KVM reports a 24-bit timer (tmr_val_ext field of fadt is zero). If the host has a 32-bit timer, the higher bits are ignored, and TMR_OF is emulated.

Linux seems to mask the high bits properly, yes. Hopefully other OSes do too.

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

PATCH 4/4 KVM: allow direct access to PMTimer port

In reply to Marcelo Tosatti

There's not much point in exiting for pmtimer reads, since it runs at a fixed clock rate and its start value is undefined.

The KVM-specific ioctl to read the counter from userspace is not nice though. Ideas?

CC Len Brown <[email protected]>
arch/x86/kvm/svm.c +5 -0
index baf9607..f7e44d8 100644
@@ -24,6 +24,7 @@
 #include <linux/vmalloc.h>
 #include <linux/highmem.h>
 #include <linux/sched.h>
+#include <linux/acpi_pmtmr.h>
 
 #include <asm/desc.h>
 
@@ -424,6 +425,10 @@ static __init int svm_hardware_setup(void)
 	iopm_va = page_address(iopm_pages);
 	memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
 	clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
+	if (pmtmr_ioport)
+		for (r = 0; r < 4; r++)
+			clear_bit(pmtmr_ioport+r, iopm_va);
+            
 	iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
 
 	if (boot_cpu_has(X86_FEATURE_NX))
arch/x86/kvm/vmx.c +5 -0
index aaa99ed..4f77fd7 100644
@@ -26,6 +26,7 @@
 #include <linux/highmem.h>
 #include <linux/sched.h>
 #include <linux/moduleparam.h>
+#include <linux/acpi_pmtmr.h>
 
 #include <asm/io.h>
 #include <asm/desc.h>
@@ -3261,6 +3262,10 @@ static int __init vmx_init(void)
 	va = kmap(vmx_io_bitmap_a);
 	memset(va, 0xff, PAGE_SIZE);
 	clear_bit(0x80, va);
+	if (pmtmr_ioport)
+		for (r = 0; r < 4; r++)
+			clear_bit(pmtmr_ioport+r, va);
+
 	kunmap(vmx_io_bitmap_a);
 
 	va = kmap(vmx_io_bitmap_b);
arch/x86/kvm/x86.c +14 -0
index e537005..9769f52 100644
@@ -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>
@@ -790,6 +791,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_PIT:
 	case KVM_CAP_NOP_IO_DELAY:
 	case KVM_CAP_MP_STATE:
+	case KVM_CAP_GET_PMTIMER:
 		r = 1;
 		break;
 	case KVM_CAP_VAPIC:
@@ -1678,6 +1680,18 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		r = 0;
 		break;
 	}
+	case KVM_GET_PMTIMER: {
+		struct kvm_pmtimer pmtmr;
+		r = -ENODEV;
+		if (!pmtmr_ioport)
+			goto out;
+		pmtmr.val = inl(pmtmr_ioport);
+		r = -EFAULT;
+		if (copy_to_user(argp, &pmtmr, sizeof pmtmr))
+			goto out;
+		r = 0;
+		break;
+	}
 	default:
 		;
 	}
drivers/clocksource/acpi_pm.c +1 -0
index 7b46faf..d3d4294 100644
@@ -29,6 +29,7 @@
  * in arch/i386/kernel/acpi/boot.c
  */
 u32 pmtmr_ioport __read_mostly;
+EXPORT_SYMBOL(pmtmr_ioport);
 
 static inline u32 read_pmtmr(void)
 {
include/linux/kvm.h +6 -0
index a281afe..3672890 100644
@@ -247,6 +247,10 @@ struct kvm_mp_state {
 	__u32 mp_state;
 };
 
+struct kvm_pmtimer {
+	__u32 val;
+};
+
 struct kvm_s390_psw {
 	__u64 mask;
 	__u64 addr;
@@ -346,6 +350,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_GET_PMTIMER 15
 
 /*
  * ioctls for VM fds
@@ -371,6 +376,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_GET_PMTIMER 	  _IOR(KVMIO,  0x67, struct kvm_pmtimer)
 
 /*
  * ioctls for vcpu fds
#13 lore raw

Re: [patch 4/4] KVM: allow direct access to PMTimer port

Quoted 22 lines · Marcelo Tosatti

There's not much point in exiting for pmtimer reads, since it runs at a fixed clock rate and its start value is undefined.

The KVM-specific ioctl to read the counter from userspace is not nice though. Ideas?

CC Len Brown <[email protected]>
arch/x86/kvm/svm.c +1 -0
index baf9607..f7e44d8 100644
@@ -24,6 +24,7 @@
 #include <linux/vmalloc.h>
 #include <linux/highmem.h>
 #include <linux/sched.h>
+#include <linux/acpi_pmtmr.h>
 
 #include <asm/desc.h>
 

arch/x86/kvm/svm.c static __init int svm_hardware_setup(void) · line 432

Quoted 8 lines
arch/x86/kvm/svm.c +4 -0
@@ -424,6 +425,10 @@ static __init int svm_hardware_setup(void)
 	iopm_va = page_address(iopm_pages);
 	memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
 	clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
+	if (pmtmr_ioport)
+		for (r = 0; r < 4; r++)
+			clear_bit(pmtmr_ioport+r, iopm_va);
+            
 

What if the port conflicts with a virtualized port?

Quoted 16 lines
arch/x86/kvm/svm.c +0 -0
 	iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
 
arch/x86/kvm/svm.c +0 -0
 	if (boot_cpu_has(X86_FEATURE_NX))
arch/x86/kvm/vmx.c +1 -0
index aaa99ed..4f77fd7 100644
@@ -26,6 +26,7 @@
 #include <linux/highmem.h>
 #include <linux/sched.h>
 #include <linux/moduleparam.h>
+#include <linux/acpi_pmtmr.h>
 
 #include <asm/io.h>
 #include <asm/desc.h>

arch/x86/kvm/vmx.c static int __init vmx_init(void) · line 3269

Quoted 8 lines
arch/x86/kvm/vmx.c +4 -0
@@ -3261,6 +3262,10 @@ static int __init vmx_init(void)
 	va = kmap(vmx_io_bitmap_a);
 	memset(va, 0xff, PAGE_SIZE);
 	clear_bit(0x80, va);
+	if (pmtmr_ioport)
+		for (r = 0; r < 4; r++)
+			clear_bit(pmtmr_ioport+r, va);
+
 

Instead of duplicating the code, how about an kvm_x86_ops interface?

Quoted 31 lines
arch/x86/kvm/vmx.c +0 -0
 	kunmap(vmx_io_bitmap_a);
 
arch/x86/kvm/vmx.c +0 -0
 	va = kmap(vmx_io_bitmap_b);
arch/x86/kvm/x86.c +6 -0
index e537005..9769f52 100644
@@ -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>
@@ -790,6 +791,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_PIT:
 	case KVM_CAP_NOP_IO_DELAY:
 	case KVM_CAP_MP_STATE:
+	case KVM_CAP_GET_PMTIMER:
 		r = 1;
 		break;
 	case KVM_CAP_VAPIC:
@@ -1678,6 +1680,18 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		r = 0;
 		break;
 	}
+	case KVM_GET_PMTIMER: {
+		struct kvm_pmtimer pmtmr;
+		r = -ENODEV;
+		if (!pmtmr_ioport)

arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 1696

Quoted 10 lines
arch/x86/kvm/x86.c +8 -0
+			goto out;
+		pmtmr.val = inl(pmtmr_ioport);
+		r = -EFAULT;
+		if (copy_to_user(argp, &pmtmr, sizeof pmtmr))
+			goto out;
+		r = 0;
+		break;
+	}
 	default:
 

This definitely doesn't belong in kvm. /dev/pmtimer?

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

Re: [patch 4/4] KVM: allow direct access to PMTimer port

Quoted 11 lines · Avi Kivity

arch/x86/kvm/svm.c static __init int svm_hardware_setup(void) · line 432

Quoted 9 lines
arch/x86/kvm/svm.c +4 -0
@@ -424,6 +425,10 @@ static __init int svm_hardware_setup(void)
 	iopm_va = page_address(iopm_pages);
 	memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
 	clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
+	if (pmtmr_ioport)
+		for (r = 0; r < 4; r++)
+			clear_bit(pmtmr_ioport+r, iopm_va);
+            
 

What if the port conflicts with a virtualized port?

Userspace registers the port range in QEMU even if it can be accessed directly, so if there is a conflict it can be handled there.

Quoted 7 lines

arch/x86/kvm/vmx.c static int __init vmx_init(void) · line 3268

Quoted 5 lines
arch/x86/kvm/vmx.c +4 -0
+	if (pmtmr_ioport)
+		for (r = 0; r < 4; r++)
+			clear_bit(pmtmr_ioport+r, va);
+

Instead of duplicating the code, how about an kvm_x86_ops interface?

Yes sounds much better.

Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/x86.c +8 -0
+	case KVM_GET_PMTIMER: {
+		struct kvm_pmtimer pmtmr;
+		r = -ENODEV;
+		if (!pmtmr_ioport)
+			goto out;
+		pmtmr.val = inl(pmtmr_ioport);
+		r = -EFAULT;
+		if (copy_to_user(argp, &pmtmr, sizeof pmtmr))
Quoted 8 lines
Quoted 6 lines
arch/x86/kvm/x86.c +4 -0
+			goto out;
+		r = 0;
+		break;
+	}

default:

This definitely doesn't belong in kvm. /dev/pmtimer?

Think so.

#15 lore raw

Re: [patch 4/4] KVM: allow direct access to PMTimer port

In reply to Marcelo Tosatti
Quoted 3 lines · Marcelo Tosatti

There's not much point in exiting for pmtimer reads, since it runs at a fixed clock rate and its start value is undefined.

On a migration farm with lots of different hardware, eventually all guests will have migrated and none of them will be able to use the passthrough port.

What about emulating pmtimer in the host kernel instead of userspace? It increases the hit from 1 exit to 3 exits, but they're much faster exits.

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

Re: [patch 4/4] KVM: allow direct access to PMTimer port

Quoted 11 lines · Avi Kivity
Quoted 3 lines · Marcelo Tosatti

There's not much point in exiting for pmtimer reads, since it runs at a fixed clock rate and its start value is undefined.

On a migration farm with lots of different hardware, eventually all guests will have migrated and none of them will be able to use the passthrough port.

What about emulating pmtimer in the host kernel instead of userspace? It increases the hit from 1 exit to 3 exits, but they're much faster exits.

I agree, even began writing it, but I think the non-passthrough case could be optimized later.

#17 lore raw

Re: [patch 4/4] KVM: allow direct access to PMTimer port

Quoted 8 lines · Marcelo Tosatti
Quoted 6 lines · Avi Kivity
Quoted 4 lines · Marcelo Tosatti

There's not much point in exiting for pmtimer reads, since it runs at a fixed clock rate and its start value is undefined.

Quoted 8 lines
Quoted 4 lines

On a migration farm with lots of different hardware, eventually all guests will have migrated and none of them will be able to use the passthrough port.

What about emulating pmtimer in the host kernel instead of userspace? It increases the hit from 1 exit to 3 exits, but they're much faster exits.

I agree, even began writing it, but I think the non-passthrough case could be optimized later.

But, I claim above that the passthrough case will never be in use on long-running guests due to migration.

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