Skip to contentSkip to conversation

i386/hvf: x2apic support and some small fixes

PATCH New Phil Dennis-Jordan 1 participant 5 messages Oct 24, 2024

lore
Share & tools
lore
#0 lore raw

cover i386/hvf: x2apic support and some small fixes

This is a loose collection of patches against the x86 hvf accel. They can be applied/pulled independently from one another.

Patch 1 is a repost of a patch I've submitted a bunch of times already. It wires up and enables x2APIC mode in conjunction with HVF - the software APIC implementation in QEMU gained the feature earlier this year but hvf wasn't included.
The change typically improves performance with modern SMP guest OSes by a 2-digit percentage. (Exact values depend on workload.)

Patch 2 fixes a minor one-off memory leak during hvf startup.

Patch 3 ever so slightly improves APIC correctness under hvf: when setting the APICBASE MSR, if the APIC deems the new value invalid, we raise an exception (as per spec) rather than silently doing nothing. This fixes a failing kvm-unit-tests test case.

Patch 4 removes some unnecessary duplication and type-rechecking in HVF's inner loop. (No need to cast the cpu state pointer to X86CPU within, the hvf_vcp_exec function already does that once at the top.)

This work has been sponsored by Sauce Labs Inc.

Phil Dennis-Jordan (4):
i386/hvf: Integrates x2APIC support with hvf accel i386/hvf: Fixes startup memory leak (vmcs caps) i386/hvf: Raise exception on error setting APICBASE i386/hvf: Removes duplicate/shadowed variables in hvf_vcpu_exec

 target/i386/hvf/hvf.c       |  7 +++----
 target/i386/hvf/x86_cpuid.c |  4 ++--
 target/i386/hvf/x86_emu.c   | 42 +++++++++++++++++++++++++++++++++++--
 3 files changed, 45 insertions(+), 8 deletions(-)
Signature · 1 line
2.39.3 (Apple Git-145)
#1 lore raw

PATCH 1/4 i386/hvf: Integrates x2APIC support with hvf accel

Support for x2APIC mode was recently introduced in the software emulated APIC implementation for TCG. Enabling it when using macOS’s hvf accelerator is useful and significantly helps performance, as Qemu currently uses the emulated APIC when running on hvf as well.

This change wires up the read & write operations for the MSR VM exits and allow-lists the CPUID flag in the x86 hvf runtime.

---
 target/i386/hvf/x86_cpuid.c |  4 ++--
 target/i386/hvf/x86_emu.c   | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)
target/i386/hvf/x86_cpuid.c +2 -2
index e56cd8411ba..4f260d46a81 100644
@@ -64,8 +64,8 @@ uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx,
              CPUID_PAT | CPUID_PSE36 | CPUID_CLFLUSH | CPUID_MMX |
              CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS;
         ecx &= CPUID_EXT_SSE3 | CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSSE3 |
-             CPUID_EXT_FMA | CPUID_EXT_CX16 | CPUID_EXT_PCID |
-             CPUID_EXT_SSE41 | CPUID_EXT_SSE42 | CPUID_EXT_MOVBE |
+             CPUID_EXT_FMA | CPUID_EXT_CX16 | CPUID_EXT_PCID | CPUID_EXT_SSE41 |
+             CPUID_EXT_SSE42 | CPUID_EXT_X2APIC | CPUID_EXT_MOVBE |
              CPUID_EXT_POPCNT | CPUID_EXT_AES | CPUID_EXT_XSAVE |
              CPUID_EXT_AVX | CPUID_EXT_F16C | CPUID_EXT_RDRAND;
         ecx |= CPUID_EXT_HYPERVISOR;
target/i386/hvf/x86_emu.c +31 -0
index 38c782b8e3b..be675bcfb71 100644
@@ -663,6 +663,15 @@ static void exec_lods(CPUX86State *env, struct x86_decode *decode)
     env->eip += decode->len;
 }
 
+static void raise_exception(CPUX86State *env, int exception_index,
+                            int error_code)
+{
+    env->exception_nr = exception_index;
+    env->error_code = error_code;
+    env->has_error_code = true;
+    env->exception_injected = 1;
+}
+
 void simulate_rdmsr(CPUX86State *env)
 {
     X86CPU *cpu = env_archcpu(env);
@@ -677,6 +686,17 @@ void simulate_rdmsr(CPUX86State *env)
     case MSR_IA32_APICBASE:
         val = cpu_get_apic_base(cpu->apic_state);
         break;
+    case MSR_APIC_START ... MSR_APIC_END: {
+        int ret;
+        int index = (uint32_t)env->regs[R_ECX] - MSR_APIC_START;
+
+        ret = apic_msr_read(index, &val);
+        if (ret < 0) {
+            raise_exception(env, EXCP0D_GPF, 0);
+        }
+
+        break;
+    }
     case MSR_IA32_UCODE_REV:
         val = cpu->ucode_rev;
         break;
@@ -777,6 +797,17 @@ void simulate_wrmsr(CPUX86State *env)
     case MSR_IA32_APICBASE:
         cpu_set_apic_base(cpu->apic_state, data);
         break;
+    case MSR_APIC_START ... MSR_APIC_END: {
+        int ret;
+        int index = (uint32_t)env->regs[R_ECX] - MSR_APIC_START;
+
+        ret = apic_msr_write(index, data);
+        if (ret < 0) {
+            raise_exception(env, EXCP0D_GPF, 0);
+        }
+
+        break;
+    }
     case MSR_FSBASE:
         wvmcs(cs->accel->fd, VMCS_GUEST_FS_BASE, data);
         break;
Signature · 1 line
2.39.3 (Apple Git-145)
#2 lore raw

PATCH 2/4 i386/hvf: Fixes startup memory leak (vmcs caps)

In reply to Phil Dennis-Jordan

The hvf_caps data structure only exists once as part of the hvf accelerator state, but it is initialised during vCPU initialisation. This change therefore adds a check to ensure memory for it is only allocated once.

---
 target/i386/hvf/hvf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
target/i386/hvf/hvf.c +3 -1
index 68dc5d9cf75..8527bce6eef 100644
@@ -239,7 +239,9 @@ int hvf_arch_init_vcpu(CPUState *cpu)
     init_emu();
     init_decoder();
 
-    hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
+    if (hvf_state->hvf_caps == NULL) {
+        hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
+    }
     env->hvf_mmio_buf = g_new(char, 4096);
 
     if (x86cpu->vmware_cpuid_freq) {
Signature · 1 line
2.39.3 (Apple Git-145)
#3 lore raw

PATCH 3/4 i386/hvf: Raise exception on error setting APICBASE

In reply to Phil Dennis-Jordan

When setting the APICBASE MSR to an illegal value, the APIC implementation will return an error. This change forwards that report to the guest as an exception rather than ignoring it when using the hvf accelerator.

---
 target/i386/hvf/x86_emu.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
target/i386/hvf/x86_emu.c +9 -2
index be675bcfb71..015f760acb3 100644
@@ -794,9 +794,16 @@ void simulate_wrmsr(CPUX86State *env)
     switch (msr) {
     case MSR_IA32_TSC:
         break;
-    case MSR_IA32_APICBASE:
-        cpu_set_apic_base(cpu->apic_state, data);
+    case MSR_IA32_APICBASE: {
+        int r;
+
+        r = cpu_set_apic_base(cpu->apic_state, data);
+        if (r < 0) {
+            raise_exception(env, EXCP0D_GPF, 0);
+        }
+
         break;
+    }
     case MSR_APIC_START ... MSR_APIC_END: {
         int ret;
         int index = (uint32_t)env->regs[R_ECX] - MSR_APIC_START;
Signature · 1 line
2.39.3 (Apple Git-145)
#4 lore raw

PATCH 4/4 i386/hvf: Removes duplicate/shadowed variables in hvf_vcpu_exec

In reply to Phil Dennis-Jordan

Pointers to the x86 CPU state already exist at the function scope, no need to re-obtain them in individual exit reason cases.

---
 target/i386/hvf/hvf.c | 3 ---
 1 file changed, 3 deletions(-)
target/i386/hvf/hvf.c +0 -3
index 8527bce6eef..c5d025d5576 100644
@@ -586,8 +586,6 @@ int hvf_vcpu_exec(CPUState *cpu)
             break;
         }
         case EXIT_REASON_XSETBV: {
-            X86CPU *x86_cpu = X86_CPU(cpu);
-            CPUX86State *env = &x86_cpu->env;
             uint32_t eax = (uint32_t)rreg(cpu->accel->fd, HV_X86_RAX);
             uint32_t ecx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RCX);
             uint32_t edx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RDX);
@@ -644,7 +642,6 @@ int hvf_vcpu_exec(CPUState *cpu)
                 break;
             }
             case 8: {
-                X86CPU *x86_cpu = X86_CPU(cpu);
                 if (exit_qual & 0x10) {
                     RRX(env, reg) = cpu_get_apic_tpr(x86_cpu->apic_state);
                 } else {
Signature · 1 line
2.39.3 (Apple Git-145)