* [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu
@ 2025-07-30 9:52 Xiaoyao Li
2025-07-30 9:52 ` [PATCH v2 1/2] i386/cpu: Enable SMM cpu address space under KVM Xiaoyao Li
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Xiaoyao Li @ 2025-07-30 9:52 UTC (permalink / raw)
To: Paolo Bonzini, Philippe Mathieu-Daudé
Cc: Kirill Martynov, Zhao Liu, Marcelo Tosatti, Richard Henderson,
qemu-devel, Xiaoyao Li
Patch 1 enables the SMM address space i386 cpu under KVM.
Patch 2 gives name for each address space index.
---
changes in v2:
- collect Reviewed-by and Tested-by;
- fix typos and refine changelog;
Xiaoyao Li (2):
i386/cpu: Enable SMM cpu address space under KVM
target/i386: Define enum X86ASIdx for x86's address spaces
accel/kvm/kvm-all.c | 2 +-
system/physmem.c | 5 -----
target/i386/cpu.h | 5 +++++
target/i386/kvm/kvm-cpu.c | 10 ++++++++++
target/i386/kvm/kvm.c | 7 ++++++-
target/i386/tcg/system/tcg-cpu.c | 4 ++--
6 files changed, 24 insertions(+), 9 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] i386/cpu: Enable SMM cpu address space under KVM
2025-07-30 9:52 [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu Xiaoyao Li
@ 2025-07-30 9:52 ` Xiaoyao Li
2025-07-30 9:52 ` [PATCH v2 2/2] target/i386: Define enum X86ASIdx for x86's address spaces Xiaoyao Li
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Xiaoyao Li @ 2025-07-30 9:52 UTC (permalink / raw)
To: Paolo Bonzini, Philippe Mathieu-Daudé
Cc: Kirill Martynov, Zhao Liu, Marcelo Tosatti, Richard Henderson,
qemu-devel, Xiaoyao Li
Kirill Martynov reported assertation in cpu_asidx_from_attrs() being hit
when x86_cpu_dump_state() is called to dump the CPU state[*]. It happens
when the CPU is in SMM and KVM emulation failure due to misbehaving
guest.
The root cause is that QEMU i386 never enables the SMM address space for
cpu since KVM SMM support has been added.
Enable the SMM cpu address space under KVM when the SMM is enabled for
the x86machine.
[*] https://lore.kernel.org/qemu-devel/20250523154431.506993-1-stdcalllevi@yandex-team.ru/
Reported-by: Kirill Martynov <stdcalllevi@yandex-team.ru>
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Tested-by: Kirill Martynov <stdcalllevi@yandex-team.ru>
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
Changes in v2:
- fix the typos; (Philippe)
---
system/physmem.c | 5 -----
target/i386/kvm/kvm-cpu.c | 10 ++++++++++
target/i386/kvm/kvm.c | 5 +++++
3 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/system/physmem.c b/system/physmem.c
index 130c148ffb5c..76e1c33aab5c 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -795,9 +795,6 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
cpu->as = as;
}
- /* KVM cannot currently support multiple address spaces. */
- assert(asidx == 0 || !kvm_enabled());
-
if (!cpu->cpu_ases) {
cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases);
cpu->cpu_ases_count = cpu->num_ases;
@@ -820,8 +817,6 @@ void cpu_address_space_destroy(CPUState *cpu, int asidx)
assert(cpu->cpu_ases);
assert(asidx >= 0 && asidx < cpu->num_ases);
- /* KVM cannot currently support multiple address spaces. */
- assert(asidx == 0 || !kvm_enabled());
cpuas = &cpu->cpu_ases[asidx];
if (tcg_enabled()) {
diff --git a/target/i386/kvm/kvm-cpu.c b/target/i386/kvm/kvm-cpu.c
index 89a795365945..1dc1ba9b4869 100644
--- a/target/i386/kvm/kvm-cpu.c
+++ b/target/i386/kvm/kvm-cpu.c
@@ -13,6 +13,7 @@
#include "qapi/error.h"
#include "system/system.h"
#include "hw/boards.h"
+#include "hw/i386/x86.h"
#include "kvm_i386.h"
#include "accel/accel-cpu-target.h"
@@ -91,6 +92,15 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
kvm_set_guest_phys_bits(cs);
}
+ /*
+ * When SMM is enabled, there is 2 address spaces. Otherwise only 1.
+ *
+ * Only initialize address space 0 here, the second one for SMM is
+ * initialized at register_smram_listener() after machine init done.
+ */
+ cs->num_ases = x86_machine_is_smm_enabled(X86_MACHINE(current_machine)) ? 2 : 1;
+ cpu_address_space_init(cs, 0, "cpu-memory", cs->memory);
+
return true;
}
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 369626f8c8d7..47fb5c673c8e 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -2704,6 +2704,7 @@ static MemoryRegion smram_as_mem;
static void register_smram_listener(Notifier *n, void *unused)
{
+ CPUState *cpu;
MemoryRegion *smram =
(MemoryRegion *) object_resolve_path("/machine/smram", NULL);
@@ -2728,6 +2729,10 @@ static void register_smram_listener(Notifier *n, void *unused)
address_space_init(&smram_address_space, &smram_as_root, "KVM-SMRAM");
kvm_memory_listener_register(kvm_state, &smram_listener,
&smram_address_space, 1, "kvm-smram");
+
+ CPU_FOREACH(cpu) {
+ cpu_address_space_init(cpu, 1, "cpu-smm", &smram_as_root);
+ }
}
static void *kvm_msr_energy_thread(void *data)
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] target/i386: Define enum X86ASIdx for x86's address spaces
2025-07-30 9:52 [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu Xiaoyao Li
2025-07-30 9:52 ` [PATCH v2 1/2] i386/cpu: Enable SMM cpu address space under KVM Xiaoyao Li
@ 2025-07-30 9:52 ` Xiaoyao Li
2025-09-12 5:51 ` [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu Xiaoyao Li
2025-09-13 4:32 ` Paolo Bonzini
3 siblings, 0 replies; 5+ messages in thread
From: Xiaoyao Li @ 2025-07-30 9:52 UTC (permalink / raw)
To: Paolo Bonzini, Philippe Mathieu-Daudé
Cc: Kirill Martynov, Zhao Liu, Marcelo Tosatti, Richard Henderson,
qemu-devel, Xiaoyao Li
Define X86ASIdx as enum, like ARM's ARMASIdx, so that it's clear index 0
is for memory and index 1 is for SMM.
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Tested-By: Kirill Martynov <stdcalllevi@yandex-team.ru>
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
accel/kvm/kvm-all.c | 2 +-
target/i386/cpu.h | 5 +++++
target/i386/kvm/kvm-cpu.c | 2 +-
target/i386/kvm/kvm.c | 4 ++--
target/i386/tcg/system/tcg-cpu.c | 4 ++--
5 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 890d5ea9f865..e56c217a5a0d 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -2797,7 +2797,7 @@ static int kvm_init(AccelState *as, MachineState *ms)
s->memory_listener.listener.coalesced_io_del = kvm_uncoalesce_mmio_region;
kvm_memory_listener_register(s, &s->memory_listener,
- &address_space_memory, 0, "kvm-memory");
+ &address_space_memory, X86ASIdx_MEM, "kvm-memory");
memory_listener_register(&kvm_io_listener,
&address_space_io);
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index f977fc49a774..e0be7a740685 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -2574,6 +2574,11 @@ static inline bool x86_has_cpuid_0x1f(X86CPU *cpu)
void x86_cpu_set_a20(X86CPU *cpu, int a20_state);
void cpu_sync_avx_hflag(CPUX86State *env);
+typedef enum X86ASIdx {
+ X86ASIdx_MEM = 0,
+ X86ASIdx_SMM = 1,
+} X86ASIdx;
+
#ifndef CONFIG_USER_ONLY
static inline int x86_asidx_from_attrs(CPUState *cs, MemTxAttrs attrs)
{
diff --git a/target/i386/kvm/kvm-cpu.c b/target/i386/kvm/kvm-cpu.c
index 1dc1ba9b4869..9c25b5583955 100644
--- a/target/i386/kvm/kvm-cpu.c
+++ b/target/i386/kvm/kvm-cpu.c
@@ -99,7 +99,7 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
* initialized at register_smram_listener() after machine init done.
*/
cs->num_ases = x86_machine_is_smm_enabled(X86_MACHINE(current_machine)) ? 2 : 1;
- cpu_address_space_init(cs, 0, "cpu-memory", cs->memory);
+ cpu_address_space_init(cs, X86ASIdx_MEM, "cpu-memory", cs->memory);
return true;
}
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 47fb5c673c8e..5621200be0f0 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -2728,10 +2728,10 @@ static void register_smram_listener(Notifier *n, void *unused)
address_space_init(&smram_address_space, &smram_as_root, "KVM-SMRAM");
kvm_memory_listener_register(kvm_state, &smram_listener,
- &smram_address_space, 1, "kvm-smram");
+ &smram_address_space, X86ASIdx_SMM, "kvm-smram");
CPU_FOREACH(cpu) {
- cpu_address_space_init(cpu, 1, "cpu-smm", &smram_as_root);
+ cpu_address_space_init(cpu, X86ASIdx_SMM, "cpu-smm", &smram_as_root);
}
}
diff --git a/target/i386/tcg/system/tcg-cpu.c b/target/i386/tcg/system/tcg-cpu.c
index 0538a4fd51a3..7255862c2449 100644
--- a/target/i386/tcg/system/tcg-cpu.c
+++ b/target/i386/tcg/system/tcg-cpu.c
@@ -74,8 +74,8 @@ bool tcg_cpu_realizefn(CPUState *cs, Error **errp)
memory_region_set_enabled(cpu->cpu_as_mem, true);
cs->num_ases = 2;
- cpu_address_space_init(cs, 0, "cpu-memory", cs->memory);
- cpu_address_space_init(cs, 1, "cpu-smm", cpu->cpu_as_root);
+ cpu_address_space_init(cs, X86ASIdx_MEM, "cpu-memory", cs->memory);
+ cpu_address_space_init(cs, X86ASIdx_SMM, "cpu-smm", cpu->cpu_as_root);
/* ... SMRAM with higher priority, linked from /machine/smram. */
cpu->machine_done.notify = tcg_cpu_machine_done;
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu
2025-07-30 9:52 [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu Xiaoyao Li
2025-07-30 9:52 ` [PATCH v2 1/2] i386/cpu: Enable SMM cpu address space under KVM Xiaoyao Li
2025-07-30 9:52 ` [PATCH v2 2/2] target/i386: Define enum X86ASIdx for x86's address spaces Xiaoyao Li
@ 2025-09-12 5:51 ` Xiaoyao Li
2025-09-13 4:32 ` Paolo Bonzini
3 siblings, 0 replies; 5+ messages in thread
From: Xiaoyao Li @ 2025-09-12 5:51 UTC (permalink / raw)
To: Paolo Bonzini, Philippe Mathieu-Daudé
Cc: Kirill Martynov, Zhao Liu, Marcelo Tosatti, Richard Henderson,
qemu-devel
On 7/30/2025 5:52 PM, Xiaoyao Li wrote:
> Patch 1 enables the SMM address space i386 cpu under KVM.
>
> Patch 2 gives name for each address space index.
Gentle ping.
> ---
> changes in v2:
> - collect Reviewed-by and Tested-by;
> - fix typos and refine changelog;
>
> Xiaoyao Li (2):
> i386/cpu: Enable SMM cpu address space under KVM
> target/i386: Define enum X86ASIdx for x86's address spaces
>
> accel/kvm/kvm-all.c | 2 +-
> system/physmem.c | 5 -----
> target/i386/cpu.h | 5 +++++
> target/i386/kvm/kvm-cpu.c | 10 ++++++++++
> target/i386/kvm/kvm.c | 7 ++++++-
> target/i386/tcg/system/tcg-cpu.c | 4 ++--
> 6 files changed, 24 insertions(+), 9 deletions(-)
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu
2025-07-30 9:52 [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu Xiaoyao Li
` (2 preceding siblings ...)
2025-09-12 5:51 ` [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu Xiaoyao Li
@ 2025-09-13 4:32 ` Paolo Bonzini
3 siblings, 0 replies; 5+ messages in thread
From: Paolo Bonzini @ 2025-09-13 4:32 UTC (permalink / raw)
To: Xiaoyao Li
Cc: Philippe Mathieu-Daudé, Kirill Martynov, Zhao Liu,
Marcelo Tosatti, Richard Henderson, qemu-devel
Queued, thanks.
Paolo
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-09-13 4:33 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-30 9:52 [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu Xiaoyao Li
2025-07-30 9:52 ` [PATCH v2 1/2] i386/cpu: Enable SMM cpu address space under KVM Xiaoyao Li
2025-07-30 9:52 ` [PATCH v2 2/2] target/i386: Define enum X86ASIdx for x86's address spaces Xiaoyao Li
2025-09-12 5:51 ` [PATCH v2 0/2] i386/kvm: Enable SMM address space for i386 cpu Xiaoyao Li
2025-09-13 4:32 ` Paolo Bonzini
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).