From nobody Sat Feb 7 08:53:19 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1489768625689747.1090222704988; Fri, 17 Mar 2017 09:37:05 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 314248050B; Fri, 17 Mar 2017 16:37:04 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9193A17134; Fri, 17 Mar 2017 16:37:03 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 392871853D0F; Fri, 17 Mar 2017 16:37:03 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v2HGb01V007110 for ; Fri, 17 Mar 2017 12:37:00 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9124253; Fri, 17 Mar 2017 16:37:00 +0000 (UTC) Received: from mamuti.net (ovpn-204-109.brq.redhat.com [10.40.204.109]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 37D775DD67 for ; Fri, 17 Mar 2017 16:36:57 +0000 (UTC) Received: by mamuti.net (Postfix, from userid 500) id 2138510618B; Fri, 17 Mar 2017 17:36:55 +0100 (CET) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 314248050B Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 314248050B From: Jiri Denemark To: libvir-list@redhat.com Date: Fri, 17 Mar 2017 17:36:40 +0100 Message-Id: In-Reply-To: References: In-Reply-To: References: Mail-Followup-To: libvir-list@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 02/14] cpu: Introduce virCPUExpandFeatures X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 17 Mar 2017 16:37:05 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Having to use cpuBaseline with VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES flag to expand CPU features is strange. Not to mention that cpuBaseline can only expand host CPU definitions (i.e., it completely ignores feature policies). The new virCPUExpandFeatures API is designed to work with both host and guest CPU definitions. Signed-off-by: Jiri Denemark --- src/cpu/cpu.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ src/cpu/cpu.h | 8 ++++++++ src/cpu/cpu_x86.c | 53 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/libvirt_private.syms | 1 + 4 files changed, 112 insertions(+) diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c index 1461190ba..5604db1d1 100644 --- a/src/cpu/cpu.c +++ b/src/cpu/cpu.c @@ -1063,3 +1063,53 @@ virCPUConvertLegacy(virArch arch, VIR_DEBUG("model=3D%s", NULLSTR(cpu->model)); return 0; } + + +static int +virCPUFeatureCompare(const void *p1, + const void *p2) +{ + const virCPUFeatureDef *f1 =3D p1; + const virCPUFeatureDef *f2 =3D p2; + + return strcmp(f1->name, f2->name); +} + + +/** + * virCPUExpandFeatures: + * + * @arch: CPU architecture + * @cpu: CPU definition to be expanded + * + * Add all features implicitly enabled by the CPU model to the list of + * features. The @cpu is expected to be either a host or a guest represent= ation + * of a host CPU, i.e., only VIR_CPU_FEATURE_REQUIRE and + * VIR_CPU_FEATURE_DISABLE policies are supported. + * + * The updated list of features in the CPU definition is sorted. + * + * Return -1 on error, 0 on success. + */ +int +virCPUExpandFeatures(virArch arch, + virCPUDefPtr cpu) +{ + struct cpuArchDriver *driver; + + VIR_DEBUG("arch=3D%s, cpu=3D%p, model=3D%s, nfeatures=3D%zu", + virArchToString(arch), cpu, NULLSTR(cpu->model), cpu->nfeatu= res); + + if (!(driver =3D cpuGetSubDriver(arch))) + return -1; + + if (driver->expandFeatures && + driver->expandFeatures(cpu) < 0) + return -1; + + qsort(cpu->features, cpu->nfeatures, sizeof(*cpu->features), + virCPUFeatureCompare); + + VIR_DEBUG("nfeatures=3D%zu", cpu->nfeatures); + return 0; +} diff --git a/src/cpu/cpu.h b/src/cpu/cpu.h index 7d6d3e921..5a8728bce 100644 --- a/src/cpu/cpu.h +++ b/src/cpu/cpu.h @@ -116,6 +116,9 @@ typedef int typedef int (*virCPUArchConvertLegacy)(virCPUDefPtr cpu); =20 +typedef int +(*virCPUArchExpandFeatures)(virCPUDefPtr cpu); + struct cpuArchDriver { const char *name; const virArch *arch; @@ -135,6 +138,7 @@ struct cpuArchDriver { virCPUArchGetModels getModels; virCPUArchTranslate translate; virCPUArchConvertLegacy convertLegacy; + virCPUArchExpandFeatures expandFeatures; }; =20 =20 @@ -245,6 +249,10 @@ virCPUConvertLegacy(virArch arch, virCPUDefPtr cpu) ATTRIBUTE_NONNULL(2); =20 +int +virCPUExpandFeatures(virArch arch, + virCPUDefPtr cpu); + /* virCPUDataFormat and virCPUDataParse are implemented for unit tests onl= y and * have no real-life usage */ diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index 9e208b094..442f0ce44 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -2891,6 +2891,58 @@ virCPUx86Translate(virCPUDefPtr cpu, } =20 =20 +static int +virCPUx86ExpandFeatures(virCPUDefPtr cpu) +{ + virCPUx86MapPtr map; + virCPUDefPtr expanded =3D NULL; + virCPUx86ModelPtr model =3D NULL; + bool host =3D cpu->type =3D=3D VIR_CPU_TYPE_HOST; + size_t i; + int ret =3D -1; + + if (!(map =3D virCPUx86GetMap())) + goto cleanup; + + if (!(expanded =3D virCPUDefCopy(cpu))) + goto cleanup; + + virCPUDefFreeFeatures(expanded); + + if (!(model =3D x86ModelFind(map, cpu->model))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unknown CPU model %s"), cpu->model); + goto cleanup; + } + + if (!(model =3D x86ModelCopy(model)) || + x86DataToCPUFeatures(expanded, host ? -1 : VIR_CPU_FEATURE_REQUIRE, + &model->data, map) < 0) + goto cleanup; + + for (i =3D 0; i < cpu->nfeatures; i++) { + virCPUFeatureDefPtr f =3D cpu->features + i; + + if (!host && + f->policy !=3D VIR_CPU_FEATURE_REQUIRE && + f->policy !=3D VIR_CPU_FEATURE_DISABLE) + continue; + + if (virCPUDefUpdateFeature(expanded, f->name, f->policy) < 0) + goto cleanup; + } + + virCPUDefFreeModel(cpu); + + ret =3D virCPUDefCopyModel(cpu, expanded, false); + + cleanup: + virCPUDefFree(expanded); + x86ModelFree(model); + return ret; +} + + int virCPUx86DataAddCPUID(virCPUDataPtr cpuData, const virCPUx86CPUID *cpuid) @@ -2965,4 +3017,5 @@ struct cpuArchDriver cpuDriverX86 =3D { .dataParse =3D virCPUx86DataParse, .getModels =3D virCPUx86GetModels, .translate =3D virCPUx86Translate, + .expandFeatures =3D virCPUx86ExpandFeatures, }; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 7ac5e533f..21912434d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1013,6 +1013,7 @@ virCPUDataFormat; virCPUDataFree; virCPUDataNew; virCPUDataParse; +virCPUExpandFeatures; virCPUGetHost; virCPUGetModels; virCPUTranslate; --=20 2.12.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list