From nobody Sun Sep 28 15:58:45 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1758874766832809.0397390443601; Fri, 26 Sep 2025 01:19:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v23dC-0006kQ-Uc; Fri, 26 Sep 2025 04:16:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v23d9-0006hW-Ds; Fri, 26 Sep 2025 04:16:40 -0400 Received: from isrv.corpit.ru ([212.248.84.144]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v23cw-0001o0-FK; Fri, 26 Sep 2025 04:16:37 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id 4B699157D73; Fri, 26 Sep 2025 11:10:34 +0300 (MSK) Received: from think4mjt.origo (mjtthink.wg.tls.msk.ru [192.168.177.146]) by tsrv.corpit.ru (Postfix) with ESMTP id 59821290C57; Fri, 26 Sep 2025 11:10:35 +0300 (MSK) From: Michael Tokarev To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Xiaoyao Li , Kirill Martynov , Zhao Liu , Paolo Bonzini , Michael Tokarev Subject: [Stable-10.1.1 43/60] i386/cpu: Enable SMM cpu address space under KVM Date: Fri, 26 Sep 2025 11:10:11 +0300 Message-ID: <20250926081031.2214971-43-mjt@tls.msk.ru> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=212.248.84.144; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_PASS=-0.001, T_SPF_HELO_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1758874768872116600 Content-Type: text/plain; charset="utf-8" From: 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 Reviewed-by: Zhao Liu Tested-by: Kirill Martynov Signed-off-by: Xiaoyao Li Link: https://lore.kernel.org/r/20250730095253.1833411-2-xiaoyao.li@intel.c= om Signed-off-by: Paolo Bonzini (cherry picked from commit 0516f4b70264b9710a25718d21bd35ef463c875e) Signed-off-by: Michael Tokarev diff --git a/system/physmem.c b/system/physmem.c index e5dd760e0b..8705fadc01 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -793,9 +793,6 @@ void cpu_address_space_init(CPUState *cpu, int asidx, cpu->as =3D as; } =20 - /* KVM cannot currently support multiple address spaces. */ - assert(asidx =3D=3D 0 || !kvm_enabled()); - if (!cpu->cpu_ases) { cpu->cpu_ases =3D g_new0(CPUAddressSpace, cpu->num_ases); cpu->cpu_ases_count =3D cpu->num_ases; @@ -818,8 +815,6 @@ void cpu_address_space_destroy(CPUState *cpu, int asidx) =20 assert(cpu->cpu_ases); assert(asidx >=3D 0 && asidx < cpu->num_ases); - /* KVM cannot currently support multiple address spaces. */ - assert(asidx =3D=3D 0 || !kvm_enabled()); =20 cpuas =3D &cpu->cpu_ases[asidx]; if (tcg_enabled()) { diff --git a/target/i386/kvm/kvm-cpu.c b/target/i386/kvm/kvm-cpu.c index 89a7953659..1dc1ba9b48 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" =20 #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); } =20 + /* + * 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 =3D x86_machine_is_smm_enabled(X86_MACHINE(current_machin= e)) ? 2 : 1; + cpu_address_space_init(cs, 0, "cpu-memory", cs->memory); + return true; } =20 diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 369626f8c8..47fb5c673c 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -2704,6 +2704,7 @@ static MemoryRegion smram_as_mem; =20 static void register_smram_listener(Notifier *n, void *unused) { + CPUState *cpu; MemoryRegion *smram =3D (MemoryRegion *) object_resolve_path("/machine/smram", NULL); =20 @@ -2728,6 +2729,10 @@ static void register_smram_listener(Notifier *n, voi= d *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); + } } =20 static void *kvm_msr_energy_thread(void *data) --=20 2.47.3