From nobody Tue Jan 21 03:59:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1737032806815733.1946061592612; Thu, 16 Jan 2025 05:06:46 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id F29B018F9; Thu, 16 Jan 2025 08:06:45 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id D47F8190B; Thu, 16 Jan 2025 08:06:26 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id C0F0B16F0; Thu, 16 Jan 2025 08:06:23 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 430B716D5 for ; Thu, 16 Jan 2025 08:06:23 -0500 (EST) Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-452-tFrl34_zPBeKnp6DmUYwmw-1; Thu, 16 Jan 2025 08:06:21 -0500 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 67068190F9AE for ; Thu, 16 Jan 2025 13:06:11 +0000 (UTC) Received: from orkuz (unknown [10.43.3.115]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E91071955F1B for ; Thu, 16 Jan 2025 13:06:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737032782; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=294sZYfBE5Tk/MTOF+1AvPrJYqv3J/sA8qJMfkGNM50=; b=VwRvK1fMBT6K6O7ibxunIO+VbukZ5JFqO+xmdPl0yg64RlTfW7gMnoUkjnN8dLAYTq2ciW 2Vfobqk88SOWHO56Ket9Qw3hrDy1CW82pP2fYW4ec7A5BMpjs1EJi7gTn1Z9/OY2uChTMt xLcyCEPR1Y5BdWXcvVf/iM1bBSTCX3A= X-MC-Unique: tFrl34_zPBeKnp6DmUYwmw-1 X-Mimecast-MFC-AGG-ID: tFrl34_zPBeKnp6DmUYwmw From: Jiri Denemark To: devel@lists.libvirt.org Subject: [PATCH] cpu_x86: Probe host CPU for all MSR features Date: Thu, 16 Jan 2025 14:05:59 +0100 Message-ID: <974a75917cd44563d147eaa28f9722c6e66b002e.1737032759.git.jdenemar@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: giT61r5bwHXq_bCaI61fhuDEJSDdXp9v98IBAVkPpu8_1737032780 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: H2NHGFR36XDBSJSIZVBTFKTSIBUD4IYF X-Message-ID-Hash: H2NHGFR36XDBSJSIZVBTFKTSIBUD4IYF X-MailFrom: jdenemar@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1737032807944019000 Content-Type: text/plain; charset="utf-8" The list of CPU features we probe from various MSR grew significantly over time and the CPU map currently mentions 11 distinct MSR indexes. But the code for directly probing host CPU features was still reading only the original 0x10a index. Thus the CPU model in host capabilities was missing a lot of features. Instead of specifying a static list of indexes to read (which we would forget to update in the future), let's just read all indexes found in the CPU map. Signed-off-by: Jiri Denemark Reviewed-by: Michal Privoznik --- src/cpu/cpu_x86.c | 62 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index 6d72d446c9..1ce97d9dcf 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -2836,15 +2836,61 @@ cpuidSet(uint32_t base, virCPUData *data) } =20 =20 +static size_t +virCPUx86ListMSRs(virCPUx86Map *map, + unsigned long **msrs) +{ + size_t n =3D 0; + size_t i, j, k; + + /* These three nested loops look horrible, but data->len is always 1 h= ere + * and thanks to grouping features in the CPU map by their MSR index t= he + * third loop will run more than one iteration only once for each dist= inct + * index. + */ + for (i =3D 0; i < map->nfeatures; i++) { + virCPUx86Data *data =3D &map->features[i]->data; + + for (j =3D 0; j < data->len; j++) { + virCPUx86DataItem *item =3D &data->items[j]; + bool found =3D false; + unsigned long msr; + + if (item->type !=3D VIR_CPU_X86_DATA_MSR) + continue; + + msr =3D item->data.msr.index; + + for (k =3D n; k > 0; k--) { + if ((*msrs)[k - 1] =3D=3D msr) { + found =3D true; + break; + } + } + + if (!found) + VIR_APPEND_ELEMENT(*msrs, n, msr); + } + } + + return n; +} + + static int virCPUx86GetHost(virCPUDef *cpu, virDomainCapsCPUModels *models) { g_autoptr(virCPUData) cpuData =3D NULL; unsigned int addrsz; + virCPUx86Map *map; + g_autofree unsigned long *msrs =3D NULL; + size_t nmsrs; + uint64_t msr; + size_t i; int ret; =20 - if (virCPUx86DriverInitialize() < 0) + if (!(map =3D virCPUx86GetMap())) return -1; =20 if (!(cpuData =3D virCPUDataNew(archs[0]))) @@ -2854,18 +2900,16 @@ virCPUx86GetHost(virCPUDef *cpu, cpuidSet(CPUX86_EXTENDED, cpuData) < 0) return -1; =20 - /* Read the IA32_ARCH_CAPABILITIES MSR (0x10a) if supported. - * This is best effort since there might be no way to read the MSR - * when we are not running as root. */ - if (virCPUx86DataCheckFeature(cpuData, "arch-capabilities") =3D=3D 1) { - uint64_t msr; - unsigned long index =3D 0x10a; + nmsrs =3D virCPUx86ListMSRs(map, &msrs); =20 - if (virHostCPUGetMSR(index, &msr) =3D=3D 0) { + /* This is best effort since there might be no way to read the MSR + * when we are not running as root. */ + for (i =3D 0; i < nmsrs; i++) { + if (virHostCPUGetMSR(msrs[i], &msr) =3D=3D 0) { virCPUx86DataItem item =3D { .type =3D VIR_CPU_X86_DATA_MSR, .data.msr =3D { - .index =3D index, + .index =3D msrs[i], .eax =3D msr & 0xffffffff, .edx =3D msr >> 32, }, --=20 2.47.1