* [PATCH 0/4] i386/hvf: x2apic support and some small fixes
@ 2024-10-24 19:42 Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 1/4] i386/hvf: Integrates x2APIC support with hvf accel Phil Dennis-Jordan
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Phil Dennis-Jordan @ 2024-10-24 19:42 UTC (permalink / raw)
To: qemu-devel; +Cc: dirty, rbolshakov, Phil Dennis-Jordan
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(-)
--
2.39.3 (Apple Git-145)
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/4] i386/hvf: Integrates x2APIC support with hvf accel
2024-10-24 19:42 [PATCH 0/4] i386/hvf: x2apic support and some small fixes Phil Dennis-Jordan
@ 2024-10-24 19:43 ` Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 2/4] i386/hvf: Fixes startup memory leak (vmcs caps) Phil Dennis-Jordan
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Phil Dennis-Jordan @ 2024-10-24 19:43 UTC (permalink / raw)
To: qemu-devel; +Cc: dirty, rbolshakov, Phil Dennis-Jordan
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.
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
---
target/i386/hvf/x86_cpuid.c | 4 ++--
target/i386/hvf/x86_emu.c | 31 +++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/target/i386/hvf/x86_cpuid.c b/target/i386/hvf/x86_cpuid.c
index e56cd8411ba..4f260d46a81 100644
--- a/target/i386/hvf/x86_cpuid.c
+++ b/target/i386/hvf/x86_cpuid.c
@@ -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;
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/hvf/x86_emu.c
index 38c782b8e3b..be675bcfb71 100644
--- a/target/i386/hvf/x86_emu.c
+++ b/target/i386/hvf/x86_emu.c
@@ -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;
--
2.39.3 (Apple Git-145)
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/4] i386/hvf: Fixes startup memory leak (vmcs caps)
2024-10-24 19:42 [PATCH 0/4] i386/hvf: x2apic support and some small fixes Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 1/4] i386/hvf: Integrates x2APIC support with hvf accel Phil Dennis-Jordan
@ 2024-10-24 19:43 ` Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 3/4] i386/hvf: Raise exception on error setting APICBASE Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 4/4] i386/hvf: Removes duplicate/shadowed variables in hvf_vcpu_exec Phil Dennis-Jordan
3 siblings, 0 replies; 5+ messages in thread
From: Phil Dennis-Jordan @ 2024-10-24 19:43 UTC (permalink / raw)
To: qemu-devel; +Cc: dirty, rbolshakov, 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.
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
---
target/i386/hvf/hvf.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c
index 68dc5d9cf75..8527bce6eef 100644
--- a/target/i386/hvf/hvf.c
+++ b/target/i386/hvf/hvf.c
@@ -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) {
--
2.39.3 (Apple Git-145)
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/4] i386/hvf: Raise exception on error setting APICBASE
2024-10-24 19:42 [PATCH 0/4] i386/hvf: x2apic support and some small fixes Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 1/4] i386/hvf: Integrates x2APIC support with hvf accel Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 2/4] i386/hvf: Fixes startup memory leak (vmcs caps) Phil Dennis-Jordan
@ 2024-10-24 19:43 ` Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 4/4] i386/hvf: Removes duplicate/shadowed variables in hvf_vcpu_exec Phil Dennis-Jordan
3 siblings, 0 replies; 5+ messages in thread
From: Phil Dennis-Jordan @ 2024-10-24 19:43 UTC (permalink / raw)
To: qemu-devel; +Cc: dirty, rbolshakov, 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.
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
---
target/i386/hvf/x86_emu.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/hvf/x86_emu.c
index be675bcfb71..015f760acb3 100644
--- a/target/i386/hvf/x86_emu.c
+++ b/target/i386/hvf/x86_emu.c
@@ -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;
--
2.39.3 (Apple Git-145)
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 4/4] i386/hvf: Removes duplicate/shadowed variables in hvf_vcpu_exec
2024-10-24 19:42 [PATCH 0/4] i386/hvf: x2apic support and some small fixes Phil Dennis-Jordan
` (2 preceding siblings ...)
2024-10-24 19:43 ` [PATCH 3/4] i386/hvf: Raise exception on error setting APICBASE Phil Dennis-Jordan
@ 2024-10-24 19:43 ` Phil Dennis-Jordan
3 siblings, 0 replies; 5+ messages in thread
From: Phil Dennis-Jordan @ 2024-10-24 19:43 UTC (permalink / raw)
To: qemu-devel; +Cc: dirty, rbolshakov, 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.
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
---
target/i386/hvf/hvf.c | 3 ---
1 file changed, 3 deletions(-)
diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c
index 8527bce6eef..c5d025d5576 100644
--- a/target/i386/hvf/hvf.c
+++ b/target/i386/hvf/hvf.c
@@ -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 {
--
2.39.3 (Apple Git-145)
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-10-24 19:44 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-24 19:42 [PATCH 0/4] i386/hvf: x2apic support and some small fixes Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 1/4] i386/hvf: Integrates x2APIC support with hvf accel Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 2/4] i386/hvf: Fixes startup memory leak (vmcs caps) Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 3/4] i386/hvf: Raise exception on error setting APICBASE Phil Dennis-Jordan
2024-10-24 19:43 ` [PATCH 4/4] i386/hvf: Removes duplicate/shadowed variables in hvf_vcpu_exec Phil Dennis-Jordan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).