From nobody Mon Feb 9 13:40:04 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1648036439045608.773821772619; Wed, 23 Mar 2022 04:53:59 -0700 (PDT) Received: from localhost ([::1]:54936 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nWzYs-0005gY-06 for importer@patchew.org; Wed, 23 Mar 2022 07:53:58 -0400 Received: from eggs.gnu.org ([209.51.188.92]:42302) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nWzOe-0000UB-Ay for qemu-devel@nongnu.org; Wed, 23 Mar 2022 07:43:26 -0400 Received: from [2a00:1450:4864:20::633] (port=44778 helo=mail-ej1-x633.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nWzOc-0001D9-34 for qemu-devel@nongnu.org; Wed, 23 Mar 2022 07:43:23 -0400 Received: by mail-ej1-x633.google.com with SMTP id r22so2228287ejs.11 for ; Wed, 23 Mar 2022 04:43:21 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:1c09:f536:3de6:228c]) by smtp.gmail.com with ESMTPSA id d1-20020a50fe81000000b004197f2ecdc2sm519690edt.89.2022.03.23.04.43.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Mar 2022 04:43:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=NXBftiV6s8HuLt4je98lM8SPphqtzzoXIWEbFS2bKSc=; b=N569PxRZni+IKqfP3Epu6t2AcMeqbHyIdowfaJvFplCVB71XJF8fAoFvipMF8Rh5JG 5uyj1GstKbTlOF6gPyvsvxlOAixDEXWQC+KYgpy4XfoxEi2+pW06MlaXsQJyPnwdXIh/ J2A+FXYrr1/2e/dTPFZeVBBAX1a2SYuciFEC7+1ph2244w+KasmdWt84Mhyo+F5WY9y4 ZROkHYIZAMW2rES/LuVVPd8zbbJXSIjviQTEr7DGR5n/b0JOKtWmKr8faL+xOKgVK5GM fyStJBx482Jg4ZquR/qQ0Scv9w+o3k2lpIEIHCEkUkLEDebkO9yLB6uS2GqTO8LvvmyZ IOrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=NXBftiV6s8HuLt4je98lM8SPphqtzzoXIWEbFS2bKSc=; b=DnOnDWYh/jHdm6dgJy8GhzrleouLu5TSRqybR5TdDTSzrg0GEg00m5angosxDlRhwr mCY3lO2eOVbsJXF4VtDlKLZdz6ttyV+svDpjXdMSgDgFC+ttimcjAKAkWZjwY3ETKN64 ltofLDihpvOO8zCmHqqshhi3RYQtCJsB+lNW2MJ4PUmOqD9IttLY0P3HaEAPG6r9C5k2 VH8CC90dLhWmwfTMtW0sSC5/wF8XGTR93OBU8k+ltvY8RRQ2sj5vRAz9yjKzKPdGEhmM zgcl8LNHc3nTOqb1pofbgvf5sFnMO4Yob2TxGL00e57+ImxgR4k3f3vPNzsWCC8b82UC +f+g== X-Gm-Message-State: AOAM531HYAyjxzUIsvR4OIz6uWtOqt+s3+sT3F4VOaxy4akZUVrU5w6P nJTKfoxT1Wo8Ngt5/yTkiQfnMhqUfds= X-Google-Smtp-Source: ABdhPJwmmcOJ+QuHJ3iC5lMgBZWjJ6cV74EfYoiYwLHc8KAsKPKRhAIOlmV7hqiVH2C5QIc6YcpqIw== X-Received: by 2002:a17:906:1e94:b0:6b9:6fcc:53fd with SMTP id e20-20020a1709061e9400b006b96fcc53fdmr31175819ejj.450.1648035800112; Wed, 23 Mar 2022 04:43:20 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH] KVM: x86: workaround invalid CPUID[0xD, 9] info on some AMD processors Date: Wed, 23 Mar 2022 12:43:15 +0100 Message-Id: <20220323114315.22594-1-pbonzini@redhat.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::633 (failed) 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=2a00:1450:4864:20::633; envelope-from=paolo.bonzini@gmail.com; helo=mail-ej1-x633.google.com X-Spam_score_int: 0 X-Spam_score: -0.1 X-Spam_bar: / X-Spam_report: (-0.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: , Cc: Yang Zhong , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1648036440641100001 Some AMD processors expose the PKRU extended save state even if they do not= have the related PKU feature in CPUID. Worse, when they do they report a size of 64, whereas the expected size of the PKRU extended save state is 8, therefo= re the esa->size =3D=3D eax assertion does not hold. The state is already ignored by KVM_GET_SUPPORTED_CPUID because it was not enabled in the host XCR0. However, QEMU kvm_cpu_xsave_init() runs before QEMU invokes arch_prctl() to enable dynamically-enabled save states such as XTILEDATA, and KVM_GET_SUPPORTED_CPUID hides save states that have yet to be enabled. Therefore, kvm_cpu_xsave_init() needs to consult the host CPUID instead of KVM_GET_SUPPORTED_CPUID, and dies with an assertion failure. When setting up the ExtSaveArea array to match the host, ignore features th= at KVM does not report as supported. This will cause QEMU to skip the incorre= ct CPUID leaf instead of tripping the assertion. Reported-by: Daniel P. Berrang=C3=A9 Analyzed-by: Yang Zhong Signed-off-by: Paolo Bonzini Tested-by: Peter Krempa --- target/i386/cpu.c | 4 ++-- target/i386/cpu.h | 2 ++ target/i386/kvm/kvm-cpu.c | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/target/i386/cpu.c b/target/i386/cpu.c index a88d6554c8..ec3b50bf6e 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -4981,8 +4981,8 @@ CpuDefinitionInfoList *qmp_query_cpu_definitions(Erro= r **errp) return cpu_list; } =20 -static uint64_t x86_cpu_get_supported_feature_word(FeatureWord w, - bool migratable_only) +uint64_t x86_cpu_get_supported_feature_word(FeatureWord w, + bool migratable_only) { FeatureWordInfo *wi =3D &feature_word_info[w]; uint64_t r =3D 0; diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 5e406088a9..e31e6bd8b8 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -606,6 +606,8 @@ typedef enum FeatureWord { } FeatureWord; =20 typedef uint64_t FeatureWordArray[FEATURE_WORDS]; +uint64_t x86_cpu_get_supported_feature_word(FeatureWord w, + bool migratable_only); =20 /* cpuid_features bits */ #define CPUID_FP87 (1U << 0) diff --git a/target/i386/kvm/kvm-cpu.c b/target/i386/kvm/kvm-cpu.c index a35a1bf9fe..5eb955ce9a 100644 --- a/target/i386/kvm/kvm-cpu.c +++ b/target/i386/kvm/kvm-cpu.c @@ -99,13 +99,18 @@ static void kvm_cpu_xsave_init(void) for (i =3D XSTATE_SSE_BIT + 1; i < XSAVE_STATE_AREA_COUNT; i++) { ExtSaveArea *esa =3D &x86_ext_save_areas[i]; =20 - if (esa->size) { - host_cpuid(0xd, i, &eax, &ebx, &ecx, &edx); - if (eax !=3D 0) { - assert(esa->size =3D=3D eax); - esa->offset =3D ebx; - esa->ecx =3D ecx; - } + if (!esa->size) { + continue; + } + if ((x86_cpu_get_supported_feature_word(esa->feature, false) & esa= ->bits) + !=3D esa->bits) { + continue; + } + host_cpuid(0xd, i, &eax, &ebx, &ecx, &edx); + if (eax !=3D 0) { + assert(esa->size =3D=3D eax); + esa->offset =3D ebx; + esa->ecx =3D ecx; } } } --=20 2.35.1