|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [PATCH v4 27/30] KVM: x86: Add KVM_VCPU_TSC_EFFECTIVE_FREQ attribute
From: David Woodhouse <dwmw@xxxxxxxxxxxx>
Add a read-only per-vCPU attribute that reports the effective TSC and
APIC bus frequencies as seen by the guest, after hardware TSC scaling
is applied.
This allows userspace to populate CPUID leaf 0x40000010 (the "generic"
timing information leaf used by FreeBSD, XNU, and VMware) with correct
values, without KVM needing to modify guest CPUID at runtime.
The effective TSC frequency differs from what userspace requested via
KVM_SET_TSC_KHZ due to the granularity of hardware scaling and the
host kernel's measurement of its own TSC frequency.
The relationship between the attributes in KVM_VCPU_TSC_CTRL:
KVM_VCPU_TSC_OFFSET: the offset added to the scaled host TSC
KVM_VCPU_TSC_SCALE: the raw hardware scaling ratio and frac_bits,
for VMClock and precise arithmetic
KVM_VCPU_TSC_EFFECTIVE_FREQ: the resulting frequencies in kHz,
for populating CPUID leaves
Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
Documentation/virt/kvm/devices/vcpu.rst | 33 +++++++++++++++++++++++++
arch/x86/include/uapi/asm/kvm.h | 6 +++++
arch/x86/kvm/x86.c | 23 +++++++++++++++++
3 files changed, 62 insertions(+)
diff --git a/Documentation/virt/kvm/devices/vcpu.rst
b/Documentation/virt/kvm/devices/vcpu.rst
index 56562b932280..75d1c2bbb8bc 100644
--- a/Documentation/virt/kvm/devices/vcpu.rst
+++ b/Documentation/virt/kvm/devices/vcpu.rst
@@ -326,3 +326,36 @@ host TSC values are converted to guest TSC using the
formula:
Userspace can use this to precisely calculate the guest TSC from the host
TSC at any given moment. This is needed for accurate migration of guests,
as described in the documentation for the KVM_VCPU_TSC_OFFSET attribute.
+
+4.3 ATTRIBUTE: KVM_VCPU_TSC_EFFECTIVE_FREQ
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+:Parameters: struct kvm_vcpu_tsc_effective_freq
+
+Returns:
+
+ ======= ======================================
+ -EFAULT Error reading the provided parameter
+ address.
+ -ENXIO Attribute not supported (no constant TSC)
+ ======= ======================================
+
+This read-only attribute reports the effective TSC and APIC bus timer
+frequencies as observed by the guest, after hardware TSC scaling is
+applied::
+
+ struct kvm_vcpu_tsc_effective_freq {
+ __u32 tsc_khz;
+ __u32 bus_khz;
+ };
+
+The tsc_khz field is the guest's effective TSC frequency in kHz. This
+may differ slightly from what userspace requested via KVM_SET_TSC_KHZ
+due to the granularity of hardware scaling and the host kernel's
+measurement of its own TSC frequency.
+
+The bus_khz field is the APIC bus timer frequency in kHz.
+
+Userspace can use these values to populate CPUID timing leaves for the
+guest, such as the generic timing leaf at 0x40000010 (EAX=tsc_khz,
+EBX=bus_khz) or hypervisor-specific equivalents.
diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h
index 384be9a53395..196899296f84 100644
--- a/arch/x86/include/uapi/asm/kvm.h
+++ b/arch/x86/include/uapi/asm/kvm.h
@@ -962,12 +962,18 @@ struct kvm_hyperv_eventfd {
#define KVM_VCPU_TSC_CTRL 0 /* control group for the timestamp counter (TSC) */
#define KVM_VCPU_TSC_OFFSET 0 /* attribute for the TSC offset */
#define KVM_VCPU_TSC_SCALE 1 /* attribute for TSC scaling factor */
+#define KVM_VCPU_TSC_EFFECTIVE_FREQ 2 /* attribute for effective frequencies
*/
struct kvm_vcpu_tsc_scale {
__u64 tsc_ratio;
__u64 tsc_frac_bits;
};
+struct kvm_vcpu_tsc_effective_freq {
+ __u32 tsc_khz;
+ __u32 bus_khz;
+};
+
/* x86-specific KVM_EXIT_HYPERCALL flags. */
#define KVM_EXIT_HYPERCALL_LONG_MODE _BITULL(0)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 77dfd4455a4e..c15303963686 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6079,6 +6079,9 @@ static int kvm_arch_tsc_has_attr(struct kvm_vcpu *vcpu,
case KVM_VCPU_TSC_SCALE:
r = kvm_caps.has_tsc_control ? 0 : -ENXIO;
break;
+ case KVM_VCPU_TSC_EFFECTIVE_FREQ:
+ r = boot_cpu_has(X86_FEATURE_CONSTANT_TSC) ? 0 : -ENXIO;
+ break;
default:
r = -ENXIO;
}
@@ -6115,6 +6118,25 @@ static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu,
r = 0;
break;
}
+ case KVM_VCPU_TSC_EFFECTIVE_FREQ: {
+ struct kvm_vcpu_tsc_effective_freq freq;
+
+ if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
+ r = -ENXIO;
+ break;
+ }
+
+ if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu))
+ kvm_guest_time_update(vcpu);
+
+ freq.tsc_khz = div_u64(vcpu->arch.hw_tsc_hz, 1000);
+ freq.bus_khz = 1000000 / vcpu->kvm->arch.apic_bus_cycle_ns;
+ r = -EFAULT;
+ if (copy_to_user(uaddr, &freq, sizeof(freq)))
+ break;
+ r = 0;
+ break;
+ }
default:
r = -ENXIO;
}
@@ -6155,6 +6177,7 @@ static int kvm_arch_tsc_set_attr(struct kvm_vcpu *vcpu,
break;
}
case KVM_VCPU_TSC_SCALE:
+ case KVM_VCPU_TSC_EFFECTIVE_FREQ:
r = -EINVAL; /* Read only */
break;
default:
--
2.51.0
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |