1 package PVE
::QemuServer
::CPUConfig
;
7 use PVE
::QemuServer
::Helpers
qw(min_version);
16 my $cpu_vendor_list = {
18 486 => 'GenuineIntel',
19 pentium
=> 'GenuineIntel',
20 pentium2
=> 'GenuineIntel',
21 pentium3
=> 'GenuineIntel',
22 coreduo
=> 'GenuineIntel',
23 core2duo
=> 'GenuineIntel',
24 Conroe
=> 'GenuineIntel',
25 Penryn
=> 'GenuineIntel',
26 Nehalem
=> 'GenuineIntel',
27 'Nehalem-IBRS' => 'GenuineIntel',
28 Westmere
=> 'GenuineIntel',
29 'Westmere-IBRS' => 'GenuineIntel',
30 SandyBridge
=> 'GenuineIntel',
31 'SandyBridge-IBRS' => 'GenuineIntel',
32 IvyBridge
=> 'GenuineIntel',
33 'IvyBridge-IBRS' => 'GenuineIntel',
34 Haswell
=> 'GenuineIntel',
35 'Haswell-IBRS' => 'GenuineIntel',
36 'Haswell-noTSX' => 'GenuineIntel',
37 'Haswell-noTSX-IBRS' => 'GenuineIntel',
38 Broadwell
=> 'GenuineIntel',
39 'Broadwell-IBRS' => 'GenuineIntel',
40 'Broadwell-noTSX' => 'GenuineIntel',
41 'Broadwell-noTSX-IBRS' => 'GenuineIntel',
42 'Skylake-Client' => 'GenuineIntel',
43 'Skylake-Client-IBRS' => 'GenuineIntel',
44 'Skylake-Client-noTSX-IBRS' => 'GenuineIntel',
45 'Skylake-Server' => 'GenuineIntel',
46 'Skylake-Server-IBRS' => 'GenuineIntel',
47 'Skylake-Server-noTSX-IBRS' => 'GenuineIntel',
48 'Cascadelake-Server' => 'GenuineIntel',
49 'Cascadelake-Server-noTSX' => 'GenuineIntel',
50 KnightsMill
=> 'GenuineIntel',
51 'Icelake-Client' => 'GenuineIntel',
52 'Icelake-Client-noTSX' => 'GenuineIntel',
53 'Icelake-Server' => 'GenuineIntel',
54 'Icelake-Server-noTSX' => 'GenuineIntel',
57 athlon
=> 'AuthenticAMD',
58 phenom
=> 'AuthenticAMD',
59 Opteron_G1
=> 'AuthenticAMD',
60 Opteron_G2
=> 'AuthenticAMD',
61 Opteron_G3
=> 'AuthenticAMD',
62 Opteron_G4
=> 'AuthenticAMD',
63 Opteron_G5
=> 'AuthenticAMD',
64 EPYC
=> 'AuthenticAMD',
65 'EPYC-IBPB' => 'AuthenticAMD',
67 # generic types, use vendor from host node
76 my @supported_cpu_flags = (
90 my $cpu_flag = qr/[+-](@{[join('|', @supported_cpu_flags)]})/;
94 description
=> "Emulated CPU type.",
96 enum
=> [ sort { "\L$a" cmp "\L$b" } keys %$cpu_vendor_list ],
101 description
=> "Do not identify as a KVM virtual machine.",
108 pattern
=> qr/[a-zA-Z0-9]{1,12}/,
109 format_description
=> 'vendor-id',
110 description
=> 'The Hyper-V vendor ID. Some drivers or programs inside Windows guests need a specific ID.',
114 description
=> "List of additional CPU flags separated by ';'."
115 . " Use '+FLAG' to enable, '-FLAG' to disable a flag."
116 . " Currently supported flags: @{[join(', ', @supported_cpu_flags)]}.",
117 format_description
=> '+FLAG[;-FLAG...]',
119 pattern
=> qr/$cpu_flag(;$cpu_flag)*/,
124 # Print a QEMU device node for a given VM configuration for hotplugging CPUs
125 sub print_cpu_device
{
126 my ($conf, $id) = @_;
128 my $kvm = $conf->{kvm
} // 1;
129 my $cpu = $kvm ?
"kvm64" : "qemu64";
130 if (my $cputype = $conf->{cpu
}) {
131 my $cpuconf = PVE
::JSONSchema
::parse_property_string
($cpu_fmt, $cputype)
132 or die "Cannot parse cpu description: $cputype\n";
133 $cpu = $cpuconf->{cputype
};
136 my $cores = $conf->{cores
} || 1;
138 my $current_core = ($id - 1) % $cores;
139 my $current_socket = int(($id - 1 - $current_core)/$cores);
141 return "$cpu-x86_64-cpu,id=cpu$id,socket-id=$current_socket,core-id=$current_core,thread-id=0";
144 # Calculate QEMU's '-cpu' argument from a given VM configuration
145 sub get_cpu_options
{
146 my ($conf, $arch, $kvm, $kvm_off, $machine_version, $winversion, $gpu_passthrough) = @_;
149 my $ostype = $conf->{ostype
};
151 my $cpu = $kvm ?
"kvm64" : "qemu64";
152 if ($arch eq 'aarch64') {
156 if (my $cputype = $conf->{cpu
}) {
157 my $cpuconf = PVE
::JSONSchema
::parse_property_string
($cpu_fmt, $cputype)
158 or die "Cannot parse cpu description: $cputype\n";
159 $cpu = $cpuconf->{cputype
};
160 $kvm_off = 1 if $cpuconf->{hidden
};
161 $hv_vendor_id = $cpuconf->{'hv-vendor-id'};
163 if (defined(my $flags = $cpuconf->{flags
})) {
164 push @$cpuFlags, split(";", $flags);
168 push @$cpuFlags , '+lahf_lm' if $cpu eq 'kvm64' && $arch eq 'x86_64';
170 push @$cpuFlags , '-x2apic' if $ostype && $ostype eq 'solaris';
172 push @$cpuFlags, '+sep' if $cpu eq 'kvm64' || $cpu eq 'kvm32';
174 push @$cpuFlags, '-rdtscp' if $cpu =~ m/^Opteron/;
176 if (min_version
($machine_version, 2, 3) && $arch eq 'x86_64') {
178 push @$cpuFlags , '+kvm_pv_unhalt' if $kvm;
179 push @$cpuFlags , '+kvm_pv_eoi' if $kvm;
182 add_hyperv_enlightenments
($cpuFlags, $winversion, $machine_version, $conf->{bios
}, $gpu_passthrough, $hv_vendor_id) if $kvm;
184 push @$cpuFlags, 'enforce' if $cpu ne 'host' && $kvm && $arch eq 'x86_64';
186 push @$cpuFlags, 'kvm=off' if $kvm_off;
188 if (my $cpu_vendor = $cpu_vendor_list->{$cpu}) {
189 push @$cpuFlags, "vendor=${cpu_vendor}"
190 if $cpu_vendor ne 'default';
191 } elsif ($arch ne 'aarch64') {
192 die "internal error"; # should not happen
195 $cpu .= "," . join(',', @$cpuFlags) if scalar(@$cpuFlags);
197 return ('-cpu', $cpu);
200 sub add_hyperv_enlightenments
{
201 my ($cpuFlags, $winversion, $machine_version, $bios, $gpu_passthrough, $hv_vendor_id) = @_;
203 return if $winversion < 6;
204 return if $bios && $bios eq 'ovmf' && $winversion < 8;
206 if ($gpu_passthrough || defined($hv_vendor_id)) {
207 $hv_vendor_id //= 'proxmox';
208 push @$cpuFlags , "hv_vendor_id=$hv_vendor_id";
211 if (min_version
($machine_version, 2, 3)) {
212 push @$cpuFlags , 'hv_spinlocks=0x1fff';
213 push @$cpuFlags , 'hv_vapic';
214 push @$cpuFlags , 'hv_time';
216 push @$cpuFlags , 'hv_spinlocks=0xffff';
219 if (min_version
($machine_version, 2, 6)) {
220 push @$cpuFlags , 'hv_reset';
221 push @$cpuFlags , 'hv_vpindex';
222 push @$cpuFlags , 'hv_runtime';
225 if ($winversion >= 7) {
226 push @$cpuFlags , 'hv_relaxed';
228 if (min_version
($machine_version, 2, 12)) {
229 push @$cpuFlags , 'hv_synic';
230 push @$cpuFlags , 'hv_stimer';
233 if (min_version
($machine_version, 3, 1)) {
234 push @$cpuFlags , 'hv_ipi';