[pve-devel] [PATCH v3 qemu-server 4/7] cpuconfig: add get_host_cpu_flags

Alexandre Derumier aderumier at odiso.com
Mon May 22 12:25:25 CEST 2023


parse cpuinfo flags && msr and create an hash with all flags

convert to qemu naming if needed

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/QemuServer/CPUConfig.pm | 70 +++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)

diff --git a/PVE/QemuServer/CPUConfig.pm b/PVE/QemuServer/CPUConfig.pm
index 64be1f4..45bf26f 100644
--- a/PVE/QemuServer/CPUConfig.pm
+++ b/PVE/QemuServer/CPUConfig.pm
@@ -13,6 +13,7 @@ use base qw(PVE::SectionConfig Exporter);
 our @EXPORT_OK = qw(
 print_cpu_device
 get_cpu_options
+get_host_cpu_flags
 );
 
 # under certain race-conditions, this module might be loaded before pve-cluster
@@ -626,6 +627,75 @@ sub get_cpu_from_running_vm {
     return $1;
 }
 
+sub get_host_cpu_flags {
+    my ($cpuinfo) = @_;
+
+    my $res = {};
+
+    #some qemu flag have different name than cpuinfo
+    my $map = {
+	'sse4_1' => 'sse4.1',
+	'sse4_2' => 'sse4.2',
+	'tsc_deadline_timer' => 'tsc-deadline',
+	'pclmulqdq' => 'pclmuldq',
+	'arch_capabilities' => 'arch-capabilities',
+	'avx512_vnni' => 'avx512vnni',
+	'avx512_bf16' => 'avx512-bf16',
+	'avx512_vpopcntdq' => 'avx512-vpopcntdq',
+	'avx512_vbmi2' => 'avx512vbmi2',
+	'avx512_bitalg' => 'avx512bitalg',
+	'avx512_fp16' => 'avx512-fp16',
+	'sha_ni' => 'sha-ni',
+	'cr8_legacy' => 'cr8legacy',
+	'nrip_save' => 'nrip-save',
+	'amx_bf16' => 'amx-bf16',
+	'avx_vnni' => 'avx-vnni',
+	'amx_int8' => 'amx-int8',
+	'amx_tile' => 'amx-tile',
+	'bus_lock_detect' => 'bus-lock-detect',
+	'tsxldtrk' => 'tsx-ldtrk',
+    };
+
+    if ($cpuinfo->{vendor} eq 'AuthenticAMD') {
+	$map->{ssbd} = 'amd-ssbd';
+	$map->{stibp} = 'amd-stibp';
+    }
+
+    my @flags = split ' ', $cpuinfo->{'flags'};
+    foreach my $flag (@flags) {
+	$flag = $map->{$flag} if $map->{$flag};
+	next if !$qemu_supported_flags->{$flag};
+        $res->{$flag} = 1;
+    }
+
+    #spec-ctrl msr
+    my $spectre_msr = $cpuinfo->{msr}->{'spec-ctrl'};
+    $res->{'spec-ctrl'} = 1 if defined($spectre_msr) && $spectre_msr == 0;
+
+    #arch-capabilities msr
+    my $capabilities = {
+	'rdctl-no' => 0,
+	'ibrs-all' => 1,
+	'rsba' => 2,
+	'skip-l1dfl-vmentry' => 3,
+	'ssb-no' => 4,
+	'mds-no' => 5,
+	'pschange-mc-no' => 6,
+	'tsx-ctrl' => 7,
+	'taa-no' => 8,
+	'fb-clear' => 17
+    };
+
+    if ($cpuinfo->{msr}->{'arch-capabilities'}) {
+	foreach my $capabilitie (keys %$capabilities) {
+	    my $value = $capabilities->{$capabilitie};
+	    $res->{$capabilitie} = 1 if $cpuinfo->{msr}->{'arch-capabilities'} >> $value & 1;
+	}
+    }
+
+    return $res;
+}
+
 __PACKAGE__->register();
 __PACKAGE__->init();
 
-- 
2.30.2





More information about the pve-devel mailing list