Skip to contentSkip to conversation

target/i386/hvf: add vmware-cpuid-freq cpu feature

PATCH Superseded [email protected] 5 participants 11 messages Jan 13, 2021 – Feb 9, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/i386/hvf: add vmware-cpuid-freq cpu feature

From: Vladislav Yaroshchuk <[email protected]>

For -accel hvf cpu_x86_cpuid() is wrapped with hvf_cpu_x86_cpuid() to add paravirtualization cpuid leaf 0x40000010
https://lkml.org/lkml/2008/10/1/246

Leaf 0x40000010, Timing Information:
EAX: (Virtual) TSC frequency in kHz.
EBX: (Virtual) Bus (local apic timer) frequency in kHz. ECX, EDX: RESERVED (Per above, reserved fields are set to zero).

On macOS TSC and APIC Bus frequencies can be readed by sysctl call with names machdep.tsc.frequency and hw.busfrequency

This options is required for Darwin-XNU guest to be synchronized with host

---
 target/i386/hvf/hvf.c | 92 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 91 insertions(+), 1 deletion(-)
target/i386/hvf/hvf.c +91 -1
index ed9356565c..0873e4969e 100644
@@ -65,6 +65,7 @@
 
 #include <Hypervisor/hv.h>
 #include <Hypervisor/hv_vmx.h>
+#include <sys/sysctl.h>
 
 #include "exec/address-spaces.h"
 #include "hw/i386/apic_internal.h"
@@ -456,6 +457,50 @@ static void dummy_signal(int sig)
 {
 }
 
+static void init_tsc_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t tsc_freq;
+
+    if (env->tsc_khz != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) {
+        /* fprintf(stderr, "%s: sysctl machdep.tsc.frequency errored\n", __func__); */
+        return;
+    }
+    env->tsc_khz = tsc_freq / 1000;  /* Hz to KHz */
+}
+
+static void init_apic_bus_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t bus_freq;
+
+    if (env->apic_bus_freq != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) {
+        /* fprintf(stderr, "%s: sysctl hw.busfrequency errored\n", __func__); */
+        return;
+    }
+    env->apic_bus_freq = bus_freq;
+}
+
+static inline bool tsc_is_known(CPUX86State *env)
+{
+    return env->tsc_khz != 0;
+}
+
+static inline bool apic_bus_freq_is_known(CPUX86State *env)
+{
+    return env->apic_bus_freq != 0;
+}
+
 int hvf_init_vcpu(CPUState *cpu)
 {
 
@@ -480,6 +525,15 @@ int hvf_init_vcpu(CPUState *cpu)
     hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
     env->hvf_mmio_buf = g_new(char, 4096);
 
+    if (x86cpu->vmware_cpuid_freq) {
+        init_tsc_freq(env);
+        init_apic_bus_freq(env);
+
+        if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+            error_report("vmware-cpuid-freq: feature couldn't be enabled");
+        }
+    }
+
     r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf_fd, HV_VCPU_DEFAULT);
     cpu->vcpu_dirty = 1;
     assert_hvf_ok(r);
@@ -597,6 +651,42 @@ static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in
     }
 }
 
+static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
+                              uint32_t *eax, uint32_t *ebx,
+                              uint32_t *ecx, uint32_t *edx)
+{
+    /*
+     * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010 leafs
+     * Provides vmware-cpuid-freq support to hvf
+     */
+
+    uint32_t signature[3];
+
+    if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        return;
+    }
+
+    switch (index) {
+        case 0x40000000:
+            memcpy(signature, "TCGTCGTCGTCG", 12); /* QEMU Signature */
+            *eax = 0x40000010;                     /* Max available cpuid leaf */
+            *ebx = signature[0];
+            *ecx = signature[1];
+            *edx = signature[2];
+            break;
+        case 0x40000010:
+            *eax = env->tsc_khz;
+            *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */
+            *ecx = 0;
+            *edx = 0;
+            break;
+        default:
+            cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+            break;
+    }
+}
+
 int hvf_vcpu_exec(CPUState *cpu)
 {
     X86CPU *x86_cpu = X86_CPU(cpu);
@@ -734,7 +824,7 @@ int hvf_vcpu_exec(CPUState *cpu)
             uint32_t rcx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX);
             uint32_t rdx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX);
 
-            cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
+            hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
 
             wreg(cpu->hvf_fd, HV_X86_RAX, rax);
             wreg(cpu->hvf_fd, HV_X86_RBX, rbx);
Signature · 1 line
2.28.0
#1 lore raw
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [PATCH] target/i386/hvf: add vmware-cpuid-freq cpu feature

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 Switched to a new branch 'test'
f4f6e29 target/i386/hvf: add vmware-cpuid-freq cpu feature

=== OUTPUT BEGIN ===
WARNING: line over 80 characters
#52: FILE: target/i386/hvf/hvf.c:471:
+ /* fprintf(stderr, "%s: sysctl machdep.tsc.frequency errored\n", __func__); */

ERROR: switch and case should be at the same indent #124: FILE: target/i386/hvf/hvf.c:670:
+ switch (index) {
+ case 0x40000000:
[...]
+ case 0x40000010:
[...]
+ default:

WARNING: line over 80 characters
#127: FILE: target/i386/hvf/hvf.c:673:
+ *eax = 0x40000010; /* Max available cpuid leaf */

total: 1 errors, 2 warnings, 122 lines checked

Commit f4f6e29c5d8c (target/i386/hvf: add vmware-cpuid-freq cpu feature) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#2 lore raw

PATCH v2 1/1 target/i386/hvf: add vmware-cpuid-freq cpu feature

In reply to [email protected]

From: Vladislav Yaroshchuk <[email protected]>

For -accel hvf cpu_x86_cpuid() is wrapped with hvf_cpu_x86_cpuid() to add paravirtualization cpuid leaf 0x40000010
https://lkml.org/lkml/2008/10/1/246

Leaf 0x40000010, Timing Information:
EAX: (Virtual) TSC frequency in kHz.
EBX: (Virtual) Bus (local apic timer) frequency in kHz. ECX, EDX: RESERVED (Per above, reserved fields are set to zero).

On macOS TSC and APIC Bus frequencies can be readed by sysctl call with names machdep.tsc.frequency and hw.busfrequency

This options is required for Darwin-XNU guest to be synchronized with host

---
 target/i386/hvf/hvf.c | 90 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 89 insertions(+), 1 deletion(-)
target/i386/hvf/hvf.c +89 -1
index ed9356565c..a5daafe202 100644
@@ -65,6 +65,7 @@
 
 #include <Hypervisor/hv.h>
 #include <Hypervisor/hv_vmx.h>
+#include <sys/sysctl.h>
 
 #include "exec/address-spaces.h"
 #include "hw/i386/apic_internal.h"
@@ -456,6 +457,48 @@ static void dummy_signal(int sig)
 {
 }
 
+static void init_tsc_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t tsc_freq;
+
+    if (env->tsc_khz != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->tsc_khz = tsc_freq / 1000;  /* Hz to KHz */
+}
+
+static void init_apic_bus_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t bus_freq;
+
+    if (env->apic_bus_freq != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->apic_bus_freq = bus_freq;
+}
+
+static inline bool tsc_is_known(CPUX86State *env)
+{
+    return env->tsc_khz != 0;
+}
+
+static inline bool apic_bus_freq_is_known(CPUX86State *env)
+{
+    return env->apic_bus_freq != 0;
+}
+
 int hvf_init_vcpu(CPUState *cpu)
 {
 
@@ -480,6 +523,15 @@ int hvf_init_vcpu(CPUState *cpu)
     hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
     env->hvf_mmio_buf = g_new(char, 4096);
 
+    if (x86cpu->vmware_cpuid_freq) {
+        init_tsc_freq(env);
+        init_apic_bus_freq(env);
+
+        if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+            error_report("vmware-cpuid-freq: feature couldn't be enabled");
+        }
+    }
+
     r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf_fd, HV_VCPU_DEFAULT);
     cpu->vcpu_dirty = 1;
     assert_hvf_ok(r);
@@ -597,6 +649,42 @@ static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in
     }
 }
 
+static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
+                              uint32_t *eax, uint32_t *ebx,
+                              uint32_t *ecx, uint32_t *edx)
+{
+    /*
+     * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010 leafs
+     * Provides vmware-cpuid-freq support to hvf
+     */
+
+    uint32_t signature[3];
+
+    if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        return;
+    }
+
+    switch (index) {
+    case 0x40000000:
+        memcpy(signature, "TCGTCGTCGTCG", 12); /* QEMU Signature */
+        *eax = 0x40000010;                     /* Max available cpuid leaf */
+        *ebx = signature[0];
+        *ecx = signature[1];
+        *edx = signature[2];
+        break;
+    case 0x40000010:
+        *eax = env->tsc_khz;
+        *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */
+        *ecx = 0;
+        *edx = 0;
+        break;
+    default:
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        break;
+    }
+}
+
 int hvf_vcpu_exec(CPUState *cpu)
 {
     X86CPU *x86_cpu = X86_CPU(cpu);
@@ -734,7 +822,7 @@ int hvf_vcpu_exec(CPUState *cpu)
             uint32_t rcx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX);
             uint32_t rdx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX);
 
-            cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
+            hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
 
             wreg(cpu->hvf_fd, HV_X86_RAX, rax);
             wreg(cpu->hvf_fd, HV_X86_RBX, rbx);
Signature · 1 line
2.28.0
#3 lore raw
Quoted 112 lines · On Thu, Jan 14, 2021 at 10:47:03PM +0300, [email protected] wrote:

From: Vladislav Yaroshchuk <[email protected]>

For -accel hvf cpu_x86_cpuid() is wrapped with hvf_cpu_x86_cpuid() to add paravirtualization cpuid leaf 0x40000010
https://lkml.org/lkml/2008/10/1/246

Leaf 0x40000010, Timing Information:
EAX: (Virtual) TSC frequency in kHz.
EBX: (Virtual) Bus (local apic timer) frequency in kHz. ECX, EDX: RESERVED (Per above, reserved fields are set to zero).

On macOS TSC and APIC Bus frequencies can be readed by sysctl call with names machdep.tsc.frequency and hw.busfrequency

This options is required for Darwin-XNU guest to be synchronized with host

---
 target/i386/hvf/hvf.c | 90 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 89 insertions(+), 1 deletion(-)
target/i386/hvf/hvf.c +61 -0
index ed9356565c..a5daafe202 100644
@@ -65,6 +65,7 @@
 
 #include <Hypervisor/hv.h>
 #include <Hypervisor/hv_vmx.h>
+#include <sys/sysctl.h>
 
 #include "exec/address-spaces.h"
 #include "hw/i386/apic_internal.h"
@@ -456,6 +457,48 @@ static void dummy_signal(int sig)
 {
 }
 
+static void init_tsc_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t tsc_freq;
+
+    if (env->tsc_khz != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->tsc_khz = tsc_freq / 1000;  /* Hz to KHz */
+}
+
+static void init_apic_bus_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t bus_freq;
+
+    if (env->apic_bus_freq != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->apic_bus_freq = bus_freq;
+}
+
+static inline bool tsc_is_known(CPUX86State *env)
+{
+    return env->tsc_khz != 0;
+}
+
+static inline bool apic_bus_freq_is_known(CPUX86State *env)
+{
+    return env->apic_bus_freq != 0;
+}
+
 int hvf_init_vcpu(CPUState *cpu)
 {
 
@@ -480,6 +523,15 @@ int hvf_init_vcpu(CPUState *cpu)
     hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
     env->hvf_mmio_buf = g_new(char, 4096);
 
+    if (x86cpu->vmware_cpuid_freq) {
+        init_tsc_freq(env);
+        init_apic_bus_freq(env);
+
+        if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+            error_report("vmware-cpuid-freq: feature couldn't be enabled");
+        }
+    }
+
     r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf_fd, HV_VCPU_DEFAULT);
     cpu->vcpu_dirty = 1;
     assert_hvf_ok(r);
@@ -597,6 +649,42 @@ static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in
     }
 }
 
+static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
+                              uint32_t *eax, uint32_t *ebx,
+                              uint32_t *ecx, uint32_t *edx)
+{
+    /*
+     * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010 leafs
+     * Provides vmware-cpuid-freq support to hvf
+     */
+

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in · line 670

Quoted 10 lines
target/i386/hvf/hvf.c +10 -0
+    uint32_t signature[3];
+
+    if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        return;
+    }
+
+    switch (index) {
+    case 0x40000000:
+        memcpy(signature, "TCGTCGTCGTCG", 12); /* QEMU Signature */

Hi Vladislav,

TCG belongs to TCG accel identification, for HVF it should be HVFHVFHVFHVF.

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in · line 674

Quoted 4 lines
target/i386/hvf/hvf.c +4 -0
+        *eax = 0x40000010;                     /* Max available cpuid leaf */
+        *ebx = signature[0];
+        *ecx = signature[1];
+        *edx = signature[2];

TCG and KVM don't report their identity unless kvm or tcg-cpuid properties are set. I wonder if we need to guard it likewise?

But as of now QEMU is not consistent in that regard. Two parameters are needed for KVM - kvm=on,vmware-cpuid-freq=on. vmware-cpuid-freq is sufficient for WHPX but WHPX doesn't expose itself (ebx=ecx=edx=0). TCG doesn't seem to support vmware-cpuid-freq but reports it's name only if tcg-cpuid property is set.

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in · line 675

Quoted 1 line
target/i386/hvf/hvf.c +1 -0
+        break;

CPUID for not implemented hypervisor-specific leafs from 0x40000001 up to 0x4000000f should be all zeroes but cpu_x86_cpuid() only returns zero values for 0x40000001. Likely, you need to reset return values for the leafs here or in cpu_x86_cpuid(). In the latter case you'll also fix a similar bug in WHPX accel.

Otherwise, looks good.

Thanks,
Roman

Quoted 27 lines
target/i386/hvf/hvf.c +13 -1
+    case 0x40000010:
+        *eax = env->tsc_khz;
+        *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */
+        *ecx = 0;
+        *edx = 0;
+        break;
+    default:
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        break;
+    }
+}
+
 int hvf_vcpu_exec(CPUState *cpu)
 {
     X86CPU *x86_cpu = X86_CPU(cpu);
@@ -734,7 +822,7 @@ int hvf_vcpu_exec(CPUState *cpu)
             uint32_t rcx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX);
             uint32_t rdx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX);
 
-            cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
+            hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
 
             wreg(cpu->hvf_fd, HV_X86_RAX, rax);
             wreg(cpu->hvf_fd, HV_X86_RBX, rbx);
Signature · 1 line
2.28.0
#4 lore raw
Quoted 7 lines · Roman Bolshakov

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in · line 670

Quoted 3 lines
target/i386/hvf/hvf.c +3 -0
+    switch (index) {
+    case 0x40000000:
+        memcpy(signature, "TCGTCGTCGTCG", 12); /* QEMU Signature */

Hi Vladislav,

TCG belongs to TCG accel identification, for HVF it should be HVFHVFHVFHVF.

You can use the KVM identification just fine, as long as you comply with the rest of the KVM CPUID specification.

Paolo

#5 lore raw
In reply to Roman Bolshakov

Hi Roman,

вт, 19 янв. 2021 г. в 21:01, Roman Bolshakov <[email protected]>:

Quoted 142 lines · On Thu, Jan 14, 2021 at 10:47:03PM +0300, [email protected] wrote:
Quoted 39 lines

From: Vladislav Yaroshchuk <[email protected]>

For -accel hvf cpu_x86_cpuid() is wrapped with hvf_cpu_x86_cpuid() to add paravirtualization cpuid leaf 0x40000010
https://lkml.org/lkml/2008/10/1/246

Leaf 0x40000010, Timing Information:
EAX: (Virtual) TSC frequency in kHz.
EBX: (Virtual) Bus (local apic timer) frequency in kHz. ECX, EDX: RESERVED (Per above, reserved fields are set to zero).

On macOS TSC and APIC Bus frequencies can be readed by sysctl call with names machdep.tsc.frequency and hw.busfrequency

This options is required for Darwin-XNU guest to be synchronized with host

---
 target/i386/hvf/hvf.c | 90 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 89 insertions(+), 1 deletion(-)
target/i386/hvf/hvf.c +2 -0
index ed9356565c..a5daafe202 100644
@@ -65,6 +65,7 @@
 
 #include <Hypervisor/hv.h>
 #include <Hypervisor/hv_vmx.h>
+#include <sys/sysctl.h>
 
 #include "exec/address-spaces.h"
 #include "hw/i386/apic_internal.h"
@@ -456,6 +457,48 @@ static void dummy_signal(int sig)
 {
 }
 
+static void init_tsc_freq(CPUX86State *env)

target/i386/hvf/hvf.c static void dummy_signal(int sig) · line 470

Quoted 10 lines
target/i386/hvf/hvf.c +10 -0
+{
+    size_t length;
+    uint64_t tsc_freq;
+
+    if (env->tsc_khz != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL,

0)) {

Quoted 35 lines
target/i386/hvf/hvf.c +31 -0
+        return;
+    }
+    env->tsc_khz = tsc_freq / 1000;  /* Hz to KHz */
+}
+
+static void init_apic_bus_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t bus_freq;
+
+    if (env->apic_bus_freq != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->apic_bus_freq = bus_freq;
+}
+
+static inline bool tsc_is_known(CPUX86State *env)
+{
+    return env->tsc_khz != 0;
+}
+
+static inline bool apic_bus_freq_is_known(CPUX86State *env)
+{
+    return env->apic_bus_freq != 0;
+}
+
 int hvf_init_vcpu(CPUState *cpu)
 {
 

target/i386/hvf/hvf.c int hvf_init_vcpu(CPUState *cpu) · line 531

Quoted 9 lines
target/i386/hvf/hvf.c +6 -0
@@ -480,6 +523,15 @@ int hvf_init_vcpu(CPUState *cpu)
     hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
     env->hvf_mmio_buf = g_new(char, 4096);
 
+    if (x86cpu->vmware_cpuid_freq) {
+        init_tsc_freq(env);
+        init_apic_bus_freq(env);
+
+        if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+            error_report("vmware-cpuid-freq: feature couldn't be

enabled");

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu,

Quoted 7 lines
target/i386/hvf/hvf.c +3 -0
+        }
+    }
+
     r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf_fd, HV_VCPU_DEFAULT);
     cpu->vcpu_dirty = 1;
     assert_hvf_ok(r);
@@ -597,6 +649,42 @@ static void hvf_store_events(CPUState *cpu,

uint32_t ins_len, uint64_t idtvec_in

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, · line 652

Quoted 4 lines
target/i386/hvf/hvf.c +1 -0
     }
 }
 
+static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index,

uint32_t count,

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in

Quoted 5 lines
target/i386/hvf/hvf.c +5 -0
+                              uint32_t *eax, uint32_t *ebx,
+                              uint32_t *ecx, uint32_t *edx)
+{
+    /*
+     * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010

leafs

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in · line 670

Quoted 13 lines
target/i386/hvf/hvf.c +13 -0
+     * Provides vmware-cpuid-freq support to hvf
+     */
+
+    uint32_t signature[3];
+
+    if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        return;
+    }
+
+    switch (index) {
+    case 0x40000000:
+        memcpy(signature, "TCGTCGTCGTCG", 12); /* QEMU Signature */

Hi Vladislav,

TCG belongs to TCG accel identification, for HVF it should be HVFHVFHVFHVF.

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, · line 671

Quoted 1 line
target/i386/hvf/hvf.c +1 -0
+        *eax = 0x40000010;                     /* Max available cpuid

leaf */

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in · line 674

Quoted 3 lines
target/i386/hvf/hvf.c +3 -0
+        *ebx = signature[0];
+        *ecx = signature[1];
+        *edx = signature[2];
Quoted 10 lines

TCG and KVM don't report their identity unless kvm or tcg-cpuid properties are set. I wonder if we need to guard it likewise?

But as of now QEMU is not consistent in that regard. Two parameters are needed for KVM - kvm=on,vmware-cpuid-freq=on. vmware-cpuid-freq is sufficient for WHPX but WHPX doesn't expose itself (ebx=ecx=edx=0). TCG doesn't seem to support vmware-cpuid-freq but reports it's name only if tcg-cpuid property is set.

Ok, I understood the mistake. HVFHVFHVFHVF sounds more suitable, but hypervisor signature is not a necessary thing for vmware-cpuid-freq (especially unknown to anyone HVFHVFHVFHVF). Seems it can be dropped by filling ebx=ecx=edx=0, as WHPX does. No reason to expose the HVF in this case. If needed, it can be added as another feature.

Quoted 9 lines

+ break;

CPUID for not implemented hypervisor-specific leafs from 0x40000001 up to 0x4000000f should be all zeroes but cpu_x86_cpuid() only returns zero values for 0x40000001. Likely, you need to reset return values for the leafs here or in cpu_x86_cpuid(). In the latter case you'll also fix a similar bug in WHPX accel.

I'll fix it in-place, resetting values in hvf_cpu_x86_cpuid()

Thank you for the review!

Sincerely,

Vladislav Yaroshchuk

Quoted 33 lines

Otherwise, looks good.

Thanks,
Roman

Quoted 27 lines
target/i386/hvf/hvf.c +13 -2
+    case 0x40000010:
+        *eax = env->tsc_khz;
+        *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */
+        *ecx = 0;
+        *edx = 0;
+        break;
+    default:
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        break;
+    }
+}
+
 int hvf_vcpu_exec(CPUState *cpu)
 {
     X86CPU *x86_cpu = X86_CPU(cpu);
@@ -734,7 +822,7 @@ int hvf_vcpu_exec(CPUState *cpu)
             uint32_t rcx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX);
             uint32_t rdx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX);
 
-            cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
+            hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
 
             wreg(cpu->hvf_fd, HV_X86_RAX, rax);
             wreg(cpu->hvf_fd, HV_X86_RBX, rbx);
--

2.28.0

#6 lore raw
In reply to [email protected]
Quoted 104 lines · On Jan 14, 2021, at 11:47 AM, [email protected] wrote:

From: Vladislav Yaroshchuk <[email protected]>

For -accel hvf cpu_x86_cpuid() is wrapped with hvf_cpu_x86_cpuid() to add paravirtualization cpuid leaf 0x40000010
https://lkml.org/lkml/2008/10/1/246

Leaf 0x40000010, Timing Information:
EAX: (Virtual) TSC frequency in kHz.
EBX: (Virtual) Bus (local apic timer) frequency in kHz. ECX, EDX: RESERVED (Per above, reserved fields are set to zero).

On macOS TSC and APIC Bus frequencies can be readed by sysctl call with names machdep.tsc.frequency and hw.busfrequency

This options is required for Darwin-XNU guest to be synchronized with host

---
target/i386/hvf/hvf.c | 90 ++++++++++++++++++++++++++++++++++++++++++-

1 file changed, 89 insertions(+), 1 deletion(-)
target/i386/hvf/hvf.c +52 -0
index ed9356565c..a5daafe202 100644
@@ -65,6 +65,7 @@
 
 #include <Hypervisor/hv.h>
 #include <Hypervisor/hv_vmx.h>
+#include <sys/sysctl.h>
 
 #include "exec/address-spaces.h"
 #include "hw/i386/apic_internal.h"
@@ -456,6 +457,48 @@ static void dummy_signal(int sig)
 {
 }
 
+static void init_tsc_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t tsc_freq;
+
+    if (env->tsc_khz != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->tsc_khz = tsc_freq / 1000;  /* Hz to KHz */
+}
+
+static void init_apic_bus_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t bus_freq;
+
+    if (env->apic_bus_freq != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->apic_bus_freq = bus_freq;
+}
+
+static inline bool tsc_is_known(CPUX86State *env)
+{
+    return env->tsc_khz != 0;
+}
+
+static inline bool apic_bus_freq_is_known(CPUX86State *env)
+{
+    return env->apic_bus_freq != 0;
+}
+
 int hvf_init_vcpu(CPUState *cpu)
 {
 
@@ -480,6 +523,15 @@ int hvf_init_vcpu(CPUState *cpu)
    hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
    env->hvf_mmio_buf = g_new(char, 4096);
 
+    if (x86cpu->vmware_cpuid_freq) {
+        init_tsc_freq(env);
+        init_apic_bus_freq(env);
+
+        if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+            error_report("vmware-cpuid-freq: feature couldn't be enabled");
+        }
+    }
+
    r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf_fd, HV_VCPU_DEFAULT);
    cpu->vcpu_dirty = 1;
    assert_hvf_ok(r);

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in · line 651

Quoted 3 lines
target/i386/hvf/hvf.c +0 -0
@@ -597,6 +649,42 @@ static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in
    }
 }
 

We already have hvf/x86_cpuid.c. Can we put hvf_cpu_x86_cpuid() in there?

Quoted 9 lines
target/i386/hvf/hvf.c +9 -0
+static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
+                              uint32_t *eax, uint32_t *ebx,
+                              uint32_t *ecx, uint32_t *edx)
+{
+    /*
+     * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010 leafs
+     * Provides vmware-cpuid-freq support to hvf
+     */
+

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in · line 670

Quoted 10 lines
target/i386/hvf/hvf.c +10 -0
+    uint32_t signature[3];
+
+    if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        return;
+    }
+
+    switch (index) {
+    case 0x40000000:
+        memcpy(signature, "TCGTCGTCGTCG", 12); /* QEMU Signature */

I agree with Roman, using "HVFHVFHVFHVF" is better.

Quoted 33 lines
target/i386/hvf/hvf.c +18 -1
+        *eax = 0x40000010;                     /* Max available cpuid leaf */
+        *ebx = signature[0];
+        *ecx = signature[1];
+        *edx = signature[2];
+        break;
+    case 0x40000010:
+        *eax = env->tsc_khz;
+        *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */
+        *ecx = 0;
+        *edx = 0;
+        break;
+    default:
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        break;
+    }
+}
+
 int hvf_vcpu_exec(CPUState *cpu)
 {
    X86CPU *x86_cpu = X86_CPU(cpu);
@@ -734,7 +822,7 @@ int hvf_vcpu_exec(CPUState *cpu)
            uint32_t rcx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX);
            uint32_t rdx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX);
 
-            cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
+            hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
 
            wreg(cpu->hvf_fd, HV_X86_RAX, rax);
            wreg(cpu->hvf_fd, HV_X86_RBX, rbx);
Signature · 1 line
2.28.0

Looks good.

Cameron Esfahani
[email protected]

#7 lore raw

Hi Cameron,

ср, 20 янв. 2021 г. в 01:37, Cameron Esfahani <[email protected]>:

Quoted 111 lines
Quoted 43 lines · On Jan 14, 2021, at 11:47 AM, [email protected] wrote:

From: Vladislav Yaroshchuk <[email protected]>

For -accel hvf cpu_x86_cpuid() is wrapped with hvf_cpu_x86_cpuid() to add paravirtualization cpuid leaf 0x40000010
https://lkml.org/lkml/2008/10/1/246

Leaf 0x40000010, Timing Information:
EAX: (Virtual) TSC frequency in kHz.
EBX: (Virtual) Bus (local apic timer) frequency in kHz. ECX, EDX: RESERVED (Per above, reserved fields are set to zero).

On macOS TSC and APIC Bus frequencies can be readed by sysctl call with names machdep.tsc.frequency and hw.busfrequency

This options is required for Darwin-XNU guest to be synchronized with host

---
target/i386/hvf/hvf.c | 90 ++++++++++++++++++++++++++++++++++++++++++-

1 file changed, 89 insertions(+), 1 deletion(-)
target/i386/hvf/hvf.c +2 -0
index ed9356565c..a5daafe202 100644
@@ -65,6 +65,7 @@
 
 #include <Hypervisor/hv.h>
 #include <Hypervisor/hv_vmx.h>
+#include <sys/sysctl.h>
 
 #include "exec/address-spaces.h"
 #include "hw/i386/apic_internal.h"
@@ -456,6 +457,48 @@ static void dummy_signal(int sig)
 {
 }
 
+static void init_tsc_freq(CPUX86State *env)

target/i386/hvf/hvf.c static void dummy_signal(int sig) · line 470

Quoted 10 lines
target/i386/hvf/hvf.c +10 -0
+{
+    size_t length;
+    uint64_t tsc_freq;
+
+    if (env->tsc_khz != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL,

0)) {

Quoted 35 lines
target/i386/hvf/hvf.c +31 -0
+        return;
+    }
+    env->tsc_khz = tsc_freq / 1000;  /* Hz to KHz */
+}
+
+static void init_apic_bus_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t bus_freq;
+
+    if (env->apic_bus_freq != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->apic_bus_freq = bus_freq;
+}
+
+static inline bool tsc_is_known(CPUX86State *env)
+{
+    return env->tsc_khz != 0;
+}
+
+static inline bool apic_bus_freq_is_known(CPUX86State *env)
+{
+    return env->apic_bus_freq != 0;
+}
+
 int hvf_init_vcpu(CPUState *cpu)
 {
 

target/i386/hvf/hvf.c int hvf_init_vcpu(CPUState *cpu) · line 531

Quoted 9 lines
target/i386/hvf/hvf.c +6 -0
@@ -480,6 +523,15 @@ int hvf_init_vcpu(CPUState *cpu)
    hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
    env->hvf_mmio_buf = g_new(char, 4096);
 
+    if (x86cpu->vmware_cpuid_freq) {
+        init_tsc_freq(env);
+        init_apic_bus_freq(env);
+
+        if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+            error_report("vmware-cpuid-freq: feature couldn't be

enabled");

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu,

Quoted 7 lines
target/i386/hvf/hvf.c +3 -0
+        }
+    }
+
    r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf_fd, HV_VCPU_DEFAULT);
    cpu->vcpu_dirty = 1;
    assert_hvf_ok(r);
@@ -597,6 +649,42 @@ static void hvf_store_events(CPUState *cpu,

uint32_t ins_len, uint64_t idtvec_in

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, · line 651

Quoted 3 lines
target/i386/hvf/hvf.c +0 -0
    }
 }
 
Quoted 5 lines

Seems that

We already have hvf/x86_cpuid.c. Can we put hvf_cpu_x86_cpuid() in there?

hvf/x86_cpuid.c is about host features, hvf_cpu_x86_cpuid() does not fit

it, as for me.
Also, KVM extends cpuid in target/i386/kvm/kvm.c. Same to WHPX:
target/i386/whpx/whpx-all.c.
I wanted to meet this rule: target/i386/hvf/hvf.c

Thank you!

Vladislav Yaroshchuk

Quoted 65 lines

+static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in

Quoted 5 lines
target/i386/hvf/hvf.c +5 -0
+                              uint32_t *eax, uint32_t *ebx,
+                              uint32_t *ecx, uint32_t *edx)
+{
+    /*
+     * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010

leafs

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in · line 670

Quoted 13 lines
target/i386/hvf/hvf.c +13 -0
+     * Provides vmware-cpuid-freq support to hvf
+     */
+
+    uint32_t signature[3];
+
+    if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        return;
+    }
+
+    switch (index) {
+    case 0x40000000:
+        memcpy(signature, "TCGTCGTCGTCG", 12); /* QEMU Signature */

I agree with Roman, using "HVFHVFHVFHVF" is better.

target/i386/hvf/hvf.c static void hvf_store_events(CPUState *cpu, · line 670

Quoted 1 line
target/i386/hvf/hvf.c +1 -0
+        *eax = 0x40000010;                     /* Max available cpuid

leaf */

Quoted 29 lines
target/i386/hvf/hvf.c +17 -2
+        *ebx = signature[0];
+        *ecx = signature[1];
+        *edx = signature[2];
+        break;
+    case 0x40000010:
+        *eax = env->tsc_khz;
+        *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */
+        *ecx = 0;
+        *edx = 0;
+        break;
+    default:
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        break;
+    }
+}
+
 int hvf_vcpu_exec(CPUState *cpu)
 {
    X86CPU *x86_cpu = X86_CPU(cpu);
@@ -734,7 +822,7 @@ int hvf_vcpu_exec(CPUState *cpu)
            uint32_t rcx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX);
            uint32_t rdx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX);
 
-            cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
+            hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
 
            wreg(cpu->hvf_fd, HV_X86_RAX, rax);
            wreg(cpu->hvf_fd, HV_X86_RBX, rbx);
--
Quoted 3 lines

2.28.0

Looks good.

Cameron Esfahani
[email protected]

#8 lore raw

PATCH v3 1/1 target/i386/hvf: add vmware-cpuid-freq cpu feature

In reply to [email protected]

From: Vladislav Yaroshchuk <[email protected]>

For -accel hvf cpu_x86_cpuid() is wrapped with hvf_cpu_x86_cpuid() to add paravirtualization cpuid leaf 0x40000010
https://lkml.org/lkml/2008/10/1/246

Leaf 0x40000010, Timing Information:
EAX: (Virtual) TSC frequency in kHz.
EBX: (Virtual) Bus (local apic timer) frequency in kHz. ECX, EDX: RESERVED (Per above, reserved fields are set to zero).

On macOS TSC and APIC Bus frequencies can be readed by sysctl call with names machdep.tsc.frequency and hw.busfrequency

This options is required for Darwin-XNU guest to be synchronized with host

Leaf 0x40000000 not exposes HVF leaving hypervisor signature empty

---
 target/i386/hvf/hvf.c | 96 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 95 insertions(+), 1 deletion(-)
target/i386/hvf/hvf.c +95 -1
index ed9356565c..5a8914564b 100644
@@ -65,6 +65,7 @@
 
 #include <Hypervisor/hv.h>
 #include <Hypervisor/hv_vmx.h>
+#include <sys/sysctl.h>
 
 #include "exec/address-spaces.h"
 #include "hw/i386/apic_internal.h"
@@ -456,6 +457,48 @@ static void dummy_signal(int sig)
 {
 }
 
+static void init_tsc_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t tsc_freq;
+
+    if (env->tsc_khz != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->tsc_khz = tsc_freq / 1000;  /* Hz to KHz */
+}
+
+static void init_apic_bus_freq(CPUX86State *env)
+{
+    size_t length;
+    uint64_t bus_freq;
+
+    if (env->apic_bus_freq != 0) {
+        return;
+    }
+
+    length = sizeof(uint64_t);
+    if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) {
+        return;
+    }
+    env->apic_bus_freq = bus_freq;
+}
+
+static inline bool tsc_is_known(CPUX86State *env)
+{
+    return env->tsc_khz != 0;
+}
+
+static inline bool apic_bus_freq_is_known(CPUX86State *env)
+{
+    return env->apic_bus_freq != 0;
+}
+
 int hvf_init_vcpu(CPUState *cpu)
 {
 
@@ -480,6 +523,15 @@ int hvf_init_vcpu(CPUState *cpu)
     hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
     env->hvf_mmio_buf = g_new(char, 4096);
 
+    if (x86cpu->vmware_cpuid_freq) {
+        init_tsc_freq(env);
+        init_apic_bus_freq(env);
+
+        if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+            error_report("vmware-cpuid-freq: feature couldn't be enabled");
+        }
+    }
+
     r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf_fd, HV_VCPU_DEFAULT);
     cpu->vcpu_dirty = 1;
     assert_hvf_ok(r);
@@ -597,6 +649,48 @@ static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_in
     }
 }
 
+static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
+                              uint32_t *eax, uint32_t *ebx,
+                              uint32_t *ecx, uint32_t *edx)
+{
+    /*
+     * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010 leafs,
+     * leafs 0x40000001-0x4000000F are filled with zeros
+     * Provides vmware-cpuid-freq support to hvf
+     *
+     * Note: leaf 0x40000000 not exposes HVF,
+     * leaving hypervisor signature empty
+     */
+
+    if (index < 0x40000000 || index > 0x40000010 ||
+        !tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
+
+        cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
+        return;
+    }
+
+    switch (index) {
+    case 0x40000000:
+        *eax = 0x40000010;    /* Max available cpuid leaf */
+        *ebx = 0;             /* Leave signature empty */
+        *ecx = 0;
+        *edx = 0;
+        break;
+    case 0x40000010:
+        *eax = env->tsc_khz;
+        *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */
+        *ecx = 0;
+        *edx = 0;
+        break;
+    default:
+        *eax = 0;
+        *ebx = 0;
+        *ecx = 0;
+        *edx = 0;
+        break;
+    }
+}
+
 int hvf_vcpu_exec(CPUState *cpu)
 {
     X86CPU *x86_cpu = X86_CPU(cpu);
@@ -734,7 +828,7 @@ int hvf_vcpu_exec(CPUState *cpu)
             uint32_t rcx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX);
             uint32_t rdx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX);
 
-            cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
+            hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
 
             wreg(cpu->hvf_fd, HV_X86_RAX, rax);
             wreg(cpu->hvf_fd, HV_X86_RBX, rbx);
Signature · 1 line
2.23.0
Reviewed-by Tested-by on v3 1/1
#9 lore raw
Quoted 17 lines · On Fri, Jan 22, 2021 at 06:05:18PM +0300, [email protected] wrote:

From: Vladislav Yaroshchuk <[email protected]>

For -accel hvf cpu_x86_cpuid() is wrapped with hvf_cpu_x86_cpuid() to add paravirtualization cpuid leaf 0x40000010
https://lkml.org/lkml/2008/10/1/246

Leaf 0x40000010, Timing Information:
EAX: (Virtual) TSC frequency in kHz.
EBX: (Virtual) Bus (local apic timer) frequency in kHz. ECX, EDX: RESERVED (Per above, reserved fields are set to zero).

On macOS TSC and APIC Bus frequencies can be readed by sysctl call with names machdep.tsc.frequency and hw.busfrequency

This options is required for Darwin-XNU guest to be synchronized with host

Quoted 7 lines

Leaf 0x40000000 not exposes HVF leaving hypervisor signature empty

---
 target/i386/hvf/hvf.c | 96 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 95 insertions(+), 1 deletion(-)

I'd prefer to have generic expose-accel option for CPU and vmware-cpuid-freq=on would depend on expose-accel=on.

Regardless of that,

Thanks,
Roman

#10 lore raw
In reply to [email protected]
Quoted 17 lines · On Fri, Jan 22, 2021 at 06:05:18PM +0300, [email protected] wrote:

From: Vladislav Yaroshchuk <[email protected]>

For -accel hvf cpu_x86_cpuid() is wrapped with hvf_cpu_x86_cpuid() to add paravirtualization cpuid leaf 0x40000010
https://lkml.org/lkml/2008/10/1/246

Leaf 0x40000010, Timing Information:
EAX: (Virtual) TSC frequency in kHz.
EBX: (Virtual) Bus (local apic timer) frequency in kHz. ECX, EDX: RESERVED (Per above, reserved fields are set to zero).

On macOS TSC and APIC Bus frequencies can be readed by sysctl call with names machdep.tsc.frequency and hw.busfrequency

This options is required for Darwin-XNU guest to be synchronized with host

Quoted 7 lines

Leaf 0x40000000 not exposes HVF leaving hypervisor signature empty

---
 target/i386/hvf/hvf.c | 96 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 95 insertions(+), 1 deletion(-)

Queued, thanks!

-Roman