util: virhostcpu: Extract filtering of the returned data from virHostCPUGetCPUID

Move the filtering code into virHostCPUGetCPUIDFilterVolatile.

This also removes a safe but very questionable reuse of 'i' iterator in
the both the top level and nested loop. It's safe for now as the to
level loop will not iterate any more in the current state.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2022-04-25 13:20:14 +02:00
parent 25327c9d69
commit 231a6db96d

View File

@ -1316,6 +1316,32 @@ virHostCPUGetMSR(unsigned long index,
}
/**
* virHostCPUGetCPUIDFilterVolatile:
*
* Filters the 'kvm_cpuid2' struct and removes data which may change depending
* on the CPU core this was run on.
*
* Currently filtered fields:
* - local APIC ID
*/
static void
virHostCPUGetCPUIDFilterVolatile(struct kvm_cpuid2 *kvm_cpuid)
{
size_t i;
for (i = 0; i < kvm_cpuid->nent; ++i) {
struct kvm_cpuid_entry2 *entry = &kvm_cpuid->entries[i];
/* filter out local apic id */
if (entry->function == 0x01 && entry->index == 0x00)
entry->ebx &= 0x00ffffff;
if (entry->function == 0x0b)
entry->edx &= 0xffffff00;
}
}
struct kvm_cpuid2 *
virHostCPUGetCPUID(void)
{
@ -1341,15 +1367,7 @@ virHostCPUGetCPUID(void)
kvm_cpuid->nent = i;
if (ioctl(fd, KVM_GET_SUPPORTED_CPUID, kvm_cpuid) == 0) {
/* filter out local apic id */
for (i = 0; i < kvm_cpuid->nent; ++i) {
struct kvm_cpuid_entry2 *entry = &kvm_cpuid->entries[i];
if (entry->function == 0x01 && entry->index == 0x00)
entry->ebx &= 0x00ffffff;
if (entry->function == 0x0b)
entry->edx &= 0xffffff00;
}
virHostCPUGetCPUIDFilterVolatile(kvm_cpuid);
return g_steal_pointer(&kvm_cpuid);
}
}