]>
Commit | Line | Data |
---|---|---|
d786a274 SR |
1 | package PVE::QemuServer::CPUConfig; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::JSONSchema; | |
b3e89488 | 7 | use PVE::Cluster qw(cfs_register_file cfs_read_file); |
d786a274 SR |
8 | use PVE::QemuServer::Helpers qw(min_version); |
9 | ||
b3e89488 | 10 | use base qw(PVE::SectionConfig Exporter); |
d786a274 SR |
11 | |
12 | our @EXPORT_OK = qw( | |
13 | print_cpu_device | |
14 | get_cpu_options | |
15 | ); | |
16 | ||
b3e89488 SR |
17 | # under certain race-conditions, this module might be loaded before pve-cluster |
18 | # has started completely, so ensure we don't prevent the FUSE mount with our dir | |
1dbe979c | 19 | if (PVE::Cluster::check_cfs_is_mounted(1)) { |
b3e89488 SR |
20 | mkdir "/etc/pve/virtual-guest"; |
21 | } | |
22 | ||
23 | my $default_filename = "virtual-guest/cpu-models.conf"; | |
24 | cfs_register_file($default_filename, | |
25 | sub { PVE::QemuServer::CPUConfig->parse_config(@_); }, | |
26 | sub { PVE::QemuServer::CPUConfig->write_config(@_); }); | |
27 | ||
28 | sub load_custom_model_conf { | |
29 | return cfs_read_file($default_filename); | |
30 | } | |
31 | ||
d786a274 SR |
32 | my $cpu_vendor_list = { |
33 | # Intel CPUs | |
34 | 486 => 'GenuineIntel', | |
35 | pentium => 'GenuineIntel', | |
36 | pentium2 => 'GenuineIntel', | |
37 | pentium3 => 'GenuineIntel', | |
38 | coreduo => 'GenuineIntel', | |
39 | core2duo => 'GenuineIntel', | |
40 | Conroe => 'GenuineIntel', | |
41 | Penryn => 'GenuineIntel', | |
42 | Nehalem => 'GenuineIntel', | |
43 | 'Nehalem-IBRS' => 'GenuineIntel', | |
44 | Westmere => 'GenuineIntel', | |
45 | 'Westmere-IBRS' => 'GenuineIntel', | |
46 | SandyBridge => 'GenuineIntel', | |
47 | 'SandyBridge-IBRS' => 'GenuineIntel', | |
48 | IvyBridge => 'GenuineIntel', | |
49 | 'IvyBridge-IBRS' => 'GenuineIntel', | |
50 | Haswell => 'GenuineIntel', | |
51 | 'Haswell-IBRS' => 'GenuineIntel', | |
52 | 'Haswell-noTSX' => 'GenuineIntel', | |
53 | 'Haswell-noTSX-IBRS' => 'GenuineIntel', | |
54 | Broadwell => 'GenuineIntel', | |
55 | 'Broadwell-IBRS' => 'GenuineIntel', | |
56 | 'Broadwell-noTSX' => 'GenuineIntel', | |
57 | 'Broadwell-noTSX-IBRS' => 'GenuineIntel', | |
58 | 'Skylake-Client' => 'GenuineIntel', | |
59 | 'Skylake-Client-IBRS' => 'GenuineIntel', | |
bb84db9d | 60 | 'Skylake-Client-noTSX-IBRS' => 'GenuineIntel', |
d786a274 SR |
61 | 'Skylake-Server' => 'GenuineIntel', |
62 | 'Skylake-Server-IBRS' => 'GenuineIntel', | |
bb84db9d | 63 | 'Skylake-Server-noTSX-IBRS' => 'GenuineIntel', |
d786a274 | 64 | 'Cascadelake-Server' => 'GenuineIntel', |
bb84db9d | 65 | 'Cascadelake-Server-noTSX' => 'GenuineIntel', |
d786a274 | 66 | KnightsMill => 'GenuineIntel', |
257ae687 | 67 | 'Icelake-Client' => 'GenuineIntel', |
bb84db9d AD |
68 | 'Icelake-Client-noTSX' => 'GenuineIntel', |
69 | 'Icelake-Server' => 'GenuineIntel', | |
d0cdb1de | 70 | 'Icelake-Server-noTSX' => 'GenuineIntel', |
d786a274 SR |
71 | |
72 | # AMD CPUs | |
73 | athlon => 'AuthenticAMD', | |
74 | phenom => 'AuthenticAMD', | |
75 | Opteron_G1 => 'AuthenticAMD', | |
76 | Opteron_G2 => 'AuthenticAMD', | |
77 | Opteron_G3 => 'AuthenticAMD', | |
78 | Opteron_G4 => 'AuthenticAMD', | |
79 | Opteron_G5 => 'AuthenticAMD', | |
80 | EPYC => 'AuthenticAMD', | |
81 | 'EPYC-IBPB' => 'AuthenticAMD', | |
82 | ||
83 | # generic types, use vendor from host node | |
84 | host => 'default', | |
85 | kvm32 => 'default', | |
86 | kvm64 => 'default', | |
87 | qemu32 => 'default', | |
88 | qemu64 => 'default', | |
89 | max => 'default', | |
90 | }; | |
91 | ||
92 | my @supported_cpu_flags = ( | |
93 | 'pcid', | |
94 | 'spec-ctrl', | |
95 | 'ibpb', | |
96 | 'ssbd', | |
97 | 'virt-ssbd', | |
98 | 'amd-ssbd', | |
99 | 'amd-no-ssb', | |
100 | 'pdpe1gb', | |
101 | 'md-clear', | |
102 | 'hv-tlbflush', | |
103 | 'hv-evmcs', | |
104 | 'aes' | |
105 | ); | |
5d008ad3 SR |
106 | my $cpu_flag_supported_re = qr/([+-])(@{[join('|', @supported_cpu_flags)]})/; |
107 | my $cpu_flag_any_re = qr/([+-])([a-zA-Z0-9\-_\.]+)/; | |
d786a274 | 108 | |
58c64ad5 SR |
109 | our $qemu_cmdline_cpu_re = qr/^((?>[+-]?[\w\-_=]+,?)+)$/; |
110 | ||
5d008ad3 | 111 | my $cpu_fmt = { |
d786a274 | 112 | cputype => { |
b3e89488 | 113 | description => "Emulated CPU type. Can be default or custom name (custom model names must be prefixed with 'custom-').", |
d786a274 | 114 | type => 'string', |
b3e89488 | 115 | format_description => 'string', |
d786a274 SR |
116 | default => 'kvm64', |
117 | default_key => 1, | |
b3e89488 SR |
118 | optional => 1, |
119 | }, | |
120 | 'reported-model' => { | |
121 | description => "CPU model and vendor to report to the guest. Must be a QEMU/KVM supported model." | |
122 | . " Only valid for custom CPU model definitions, default models will always report themselves to the guest OS.", | |
123 | type => 'string', | |
124 | enum => [ sort { lc("$a") cmp lc("$b") } keys %$cpu_vendor_list ], | |
125 | default => 'kvm64', | |
126 | optional => 1, | |
d786a274 SR |
127 | }, |
128 | hidden => { | |
129 | description => "Do not identify as a KVM virtual machine.", | |
130 | type => 'boolean', | |
131 | optional => 1, | |
132 | default => 0 | |
133 | }, | |
134 | 'hv-vendor-id' => { | |
135 | type => 'string', | |
136 | pattern => qr/[a-zA-Z0-9]{1,12}/, | |
137 | format_description => 'vendor-id', | |
138 | description => 'The Hyper-V vendor ID. Some drivers or programs inside Windows guests need a specific ID.', | |
139 | optional => 1, | |
140 | }, | |
141 | flags => { | |
142 | description => "List of additional CPU flags separated by ';'." | |
143 | . " Use '+FLAG' to enable, '-FLAG' to disable a flag." | |
5d008ad3 SR |
144 | . " Custom CPU models can specify any flag supported by" |
145 | . " QEMU/KVM, VM-specific flags must be from the following" | |
146 | . " set for security reasons: @{[join(', ', @supported_cpu_flags)]}.", | |
d786a274 SR |
147 | format_description => '+FLAG[;-FLAG...]', |
148 | type => 'string', | |
5d008ad3 | 149 | pattern => qr/$cpu_flag_any_re(;$cpu_flag_any_re)*/, |
d786a274 SR |
150 | optional => 1, |
151 | }, | |
9f9792d3 SR |
152 | 'phys-bits' => { |
153 | type => 'string', | |
154 | format => 'pve-phys-bits', | |
155 | description => "The physical memory address bits that are reported to" | |
156 | . " the guest OS. Should be smaller or equal to the host's." | |
157 | . " Set to 'host' to use value from host CPU, but note that" | |
158 | . " doing so will break live migration to CPUs with other values.", | |
159 | optional => 1, | |
160 | }, | |
d786a274 SR |
161 | }; |
162 | ||
9f9792d3 SR |
163 | PVE::JSONSchema::register_format('pve-phys-bits', \&parse_phys_bits); |
164 | sub parse_phys_bits { | |
165 | my ($str, $noerr) = @_; | |
166 | ||
167 | my $err_msg = "value must be an integer between 8 and 64 or 'host'\n"; | |
168 | ||
169 | if ($str !~ m/^(host|\d{1,2})$/) { | |
170 | die $err_msg if !$noerr; | |
171 | return undef; | |
172 | } | |
173 | ||
174 | if ($str =~ m/^\d+$/ && (int($str) < 8 || int($str) > 64)) { | |
175 | die $err_msg if !$noerr; | |
176 | return undef; | |
177 | } | |
178 | ||
179 | return $str; | |
180 | } | |
181 | ||
5d008ad3 SR |
182 | # $cpu_fmt describes both the CPU config passed as part of a VM config, as well |
183 | # as the definition of a custom CPU model. There are some slight differences | |
184 | # though, which we catch in the custom verification function below. | |
185 | PVE::JSONSchema::register_format('pve-cpu-conf', \&parse_cpu_conf_basic); | |
186 | sub parse_cpu_conf_basic { | |
187 | my ($cpu_str, $noerr) = @_; | |
188 | ||
189 | my $cpu = eval { PVE::JSONSchema::parse_property_string($cpu_fmt, $cpu_str) }; | |
190 | if ($@) { | |
191 | die $@ if !$noerr; | |
192 | return undef; | |
193 | } | |
194 | ||
195 | # required, but can't be forced in schema since it's encoded in section | |
196 | # header for custom models | |
197 | if (!$cpu->{cputype}) { | |
198 | die "CPU is missing cputype\n" if !$noerr; | |
199 | return undef; | |
200 | } | |
201 | ||
202 | return $cpu; | |
203 | } | |
204 | ||
205 | PVE::JSONSchema::register_format('pve-vm-cpu-conf', \&parse_vm_cpu_conf); | |
206 | sub parse_vm_cpu_conf { | |
207 | my ($cpu_str, $noerr) = @_; | |
208 | ||
209 | my $cpu = parse_cpu_conf_basic($cpu_str, $noerr); | |
210 | return undef if !$cpu; | |
211 | ||
212 | my $cputype = $cpu->{cputype}; | |
213 | ||
214 | # a VM-specific config is only valid if the cputype exists | |
215 | if (is_custom_model($cputype)) { | |
216 | eval { get_custom_model($cputype); }; | |
217 | if ($@) { | |
218 | die $@ if !$noerr; | |
219 | return undef; | |
220 | } | |
221 | } else { | |
222 | if (!defined($cpu_vendor_list->{$cputype})) { | |
223 | die "Built-in cputype '$cputype' is not defined (missing 'custom-' prefix?)\n" if !$noerr; | |
224 | return undef; | |
225 | } | |
226 | } | |
227 | ||
228 | # in a VM-specific config, certain properties are limited/forbidden | |
229 | ||
230 | if ($cpu->{flags} && $cpu->{flags} !~ m/$cpu_flag_supported_re(;$cpu_flag_supported_re)*/) { | |
231 | die "VM-specific CPU flags must be a subset of: @{[join(', ', @supported_cpu_flags)]}\n" | |
232 | if !$noerr; | |
233 | return undef; | |
234 | } | |
235 | ||
236 | die "Property 'reported-model' not allowed in VM-specific CPU config.\n" | |
237 | if defined($cpu->{'reported-model'}); | |
238 | ||
239 | return $cpu; | |
240 | } | |
241 | ||
b3e89488 SR |
242 | # Section config settings |
243 | my $defaultData = { | |
244 | # shallow copy, since SectionConfig modifies propertyList internally | |
245 | propertyList => { %$cpu_fmt }, | |
246 | }; | |
247 | ||
248 | sub private { | |
249 | return $defaultData; | |
250 | } | |
251 | ||
252 | sub options { | |
253 | return { %$cpu_fmt }; | |
254 | } | |
255 | ||
256 | sub type { | |
257 | return 'cpu-model'; | |
258 | } | |
259 | ||
260 | sub parse_section_header { | |
261 | my ($class, $line) = @_; | |
262 | ||
263 | my ($type, $sectionId, $errmsg, $config) = | |
264 | $class->SUPER::parse_section_header($line); | |
265 | ||
266 | return undef if !$type; | |
267 | return ($type, $sectionId, $errmsg, { | |
268 | # name is given by section header, and we can always prepend 'custom-' | |
269 | # since we're reading the custom CPU file | |
270 | cputype => "custom-$sectionId", | |
271 | }); | |
272 | } | |
273 | ||
274 | sub write_config { | |
275 | my ($class, $filename, $cfg) = @_; | |
276 | ||
277 | mkdir "/etc/pve/virtual-guest"; | |
278 | ||
279 | for my $model (keys %{$cfg->{ids}}) { | |
280 | my $model_conf = $cfg->{ids}->{$model}; | |
281 | ||
282 | die "internal error: tried saving built-in CPU model (or missing prefix): $model_conf->{cputype}\n" | |
283 | if !is_custom_model($model_conf->{cputype}); | |
284 | ||
285 | die "internal error: tried saving custom cpumodel with cputype (ignoring prefix: $model_conf->{cputype}) not equal to \$cfg->ids entry ($model)\n" | |
286 | if "custom-$model" ne $model_conf->{cputype}; | |
287 | ||
288 | # saved in section header | |
289 | delete $model_conf->{cputype}; | |
290 | } | |
291 | ||
292 | $class->SUPER::write_config($filename, $cfg); | |
293 | } | |
294 | ||
295 | sub is_custom_model { | |
296 | my ($cputype) = @_; | |
297 | return $cputype =~ m/^custom-/; | |
298 | } | |
299 | ||
300 | # Use this to get a single model in the format described by $cpu_fmt. | |
301 | # Allows names with and without custom- prefix. | |
302 | sub get_custom_model { | |
303 | my ($name, $noerr) = @_; | |
304 | ||
305 | $name =~ s/^custom-//; | |
306 | my $conf = load_custom_model_conf(); | |
307 | ||
308 | my $entry = $conf->{ids}->{$name}; | |
309 | if (!defined($entry)) { | |
310 | die "Custom cputype '$name' not found\n" if !$noerr; | |
311 | return undef; | |
312 | } | |
313 | ||
314 | my $model = {}; | |
315 | for my $property (keys %$cpu_fmt) { | |
316 | if (my $value = $entry->{$property}) { | |
317 | $model->{$property} = $value; | |
318 | } | |
319 | } | |
320 | ||
321 | return $model; | |
322 | } | |
323 | ||
d786a274 SR |
324 | # Print a QEMU device node for a given VM configuration for hotplugging CPUs |
325 | sub print_cpu_device { | |
326 | my ($conf, $id) = @_; | |
327 | ||
328 | my $kvm = $conf->{kvm} // 1; | |
329 | my $cpu = $kvm ? "kvm64" : "qemu64"; | |
330 | if (my $cputype = $conf->{cpu}) { | |
5d008ad3 | 331 | my $cpuconf = parse_cpu_conf_basic($cputype) |
d786a274 SR |
332 | or die "Cannot parse cpu description: $cputype\n"; |
333 | $cpu = $cpuconf->{cputype}; | |
b3e89488 SR |
334 | |
335 | if (is_custom_model($cpu)) { | |
336 | my $custom_cpu = get_custom_model($cpu); | |
337 | ||
338 | $cpu = $custom_cpu->{'reported-model'} // | |
339 | $cpu_fmt->{'reported-model'}->{default}; | |
340 | } | |
d786a274 SR |
341 | } |
342 | ||
343 | my $cores = $conf->{cores} || 1; | |
344 | ||
345 | my $current_core = ($id - 1) % $cores; | |
346 | my $current_socket = int(($id - 1 - $current_core)/$cores); | |
347 | ||
348 | return "$cpu-x86_64-cpu,id=cpu$id,socket-id=$current_socket,core-id=$current_core,thread-id=0"; | |
349 | } | |
350 | ||
45619185 SR |
351 | # Resolves multiple arrays of hashes representing CPU flags with metadata to a |
352 | # single string in QEMU "-cpu" compatible format. Later arrays have higher | |
353 | # priority. | |
354 | # | |
355 | # Hashes take the following format: | |
356 | # { | |
357 | # aes => { | |
358 | # op => "+", # defaults to "" if undefined | |
359 | # reason => "to support AES acceleration", # for override warnings | |
360 | # value => "" # needed for kvm=off (value: off) etc... | |
361 | # }, | |
362 | # ... | |
363 | # } | |
364 | sub resolve_cpu_flags { | |
365 | my $flags = {}; | |
366 | ||
367 | for my $hash (@_) { | |
368 | for my $flag_name (keys %$hash) { | |
369 | my $flag = $hash->{$flag_name}; | |
370 | my $old_flag = $flags->{$flag_name}; | |
371 | ||
372 | $flag->{op} //= ""; | |
373 | $flag->{reason} //= "unknown origin"; | |
374 | ||
375 | if ($old_flag) { | |
376 | my $value_changed = (defined($flag->{value}) != defined($old_flag->{value})) || | |
377 | (defined($flag->{value}) && $flag->{value} ne $old_flag->{value}); | |
378 | ||
379 | if ($old_flag->{op} eq $flag->{op} && !$value_changed) { | |
380 | $flags->{$flag_name}->{reason} .= " & $flag->{reason}"; | |
381 | next; | |
382 | } | |
383 | ||
384 | my $old = print_cpuflag_hash($flag_name, $flags->{$flag_name}); | |
385 | my $new = print_cpuflag_hash($flag_name, $flag); | |
386 | warn "warning: CPU flag/setting $new overwrites $old\n"; | |
387 | } | |
388 | ||
389 | $flags->{$flag_name} = $flag; | |
390 | } | |
391 | } | |
392 | ||
393 | my $flag_str = ''; | |
394 | # sort for command line stability | |
395 | for my $flag_name (sort keys %$flags) { | |
396 | $flag_str .= ','; | |
397 | $flag_str .= $flags->{$flag_name}->{op}; | |
398 | $flag_str .= $flag_name; | |
399 | $flag_str .= "=$flags->{$flag_name}->{value}" | |
400 | if $flags->{$flag_name}->{value}; | |
401 | } | |
402 | ||
403 | return $flag_str; | |
404 | } | |
405 | ||
406 | sub print_cpuflag_hash { | |
407 | my ($flag_name, $flag) = @_; | |
408 | my $formatted = "'$flag->{op}$flag_name"; | |
409 | $formatted .= "=$flag->{value}" if defined($flag->{value}); | |
410 | $formatted .= "'"; | |
411 | $formatted .= " ($flag->{reason})" if defined($flag->{reason}); | |
412 | return $formatted; | |
413 | } | |
414 | ||
c4581b9c SR |
415 | sub parse_cpuflag_list { |
416 | my ($re, $reason, $flaglist) = @_; | |
417 | ||
418 | my $res = {}; | |
419 | return $res if !$flaglist; | |
420 | ||
421 | foreach my $flag (split(";", $flaglist)) { | |
422 | if ($flag =~ $re) { | |
423 | $res->{$2} = { op => $1, reason => $reason }; | |
424 | } | |
425 | } | |
426 | ||
427 | return $res; | |
428 | } | |
429 | ||
d786a274 SR |
430 | # Calculate QEMU's '-cpu' argument from a given VM configuration |
431 | sub get_cpu_options { | |
432 | my ($conf, $arch, $kvm, $kvm_off, $machine_version, $winversion, $gpu_passthrough) = @_; | |
433 | ||
c4581b9c | 434 | my $cputype = $kvm ? "kvm64" : "qemu64"; |
d786a274 | 435 | if ($arch eq 'aarch64') { |
c4581b9c | 436 | $cputype = 'cortex-a57'; |
d786a274 SR |
437 | } |
438 | ||
c4581b9c SR |
439 | my $cpu = {}; |
440 | my $custom_cpu; | |
441 | my $hv_vendor_id; | |
442 | if (my $cpu_prop_str = $conf->{cpu}) { | |
443 | $cpu = parse_vm_cpu_conf($cpu_prop_str) | |
444 | or die "Cannot parse cpu description: $cpu_prop_str\n"; | |
d786a274 | 445 | |
c4581b9c | 446 | $cputype = $cpu->{cputype}; |
d786a274 | 447 | |
c4581b9c SR |
448 | if (is_custom_model($cputype)) { |
449 | $custom_cpu = get_custom_model($cputype); | |
d786a274 | 450 | |
c4581b9c SR |
451 | $cputype = $custom_cpu->{'reported-model'} // |
452 | $cpu_fmt->{'reported-model'}->{default}; | |
453 | $kvm_off = $custom_cpu->{hidden} | |
454 | if defined($custom_cpu->{hidden}); | |
455 | $hv_vendor_id = $custom_cpu->{'hv-vendor-id'}; | |
456 | } | |
d786a274 | 457 | |
c4581b9c SR |
458 | # VM-specific settings override custom CPU config |
459 | $kvm_off = $cpu->{hidden} | |
460 | if defined($cpu->{hidden}); | |
461 | $hv_vendor_id = $cpu->{'hv-vendor-id'} | |
462 | if defined($cpu->{'hv-vendor-id'}); | |
463 | } | |
d786a274 | 464 | |
c4581b9c SR |
465 | my $pve_flags = get_pve_cpu_flags($conf, $kvm, $cputype, $arch, |
466 | $machine_version); | |
467 | ||
468 | my $hv_flags = get_hyperv_enlightenments($winversion, $machine_version, | |
469 | $conf->{bios}, $gpu_passthrough, $hv_vendor_id) if $kvm; | |
470 | ||
471 | my $custom_cputype_flags = parse_cpuflag_list($cpu_flag_any_re, | |
472 | "set by custom CPU model", $custom_cpu->{flags}); | |
473 | ||
474 | my $vm_flags = parse_cpuflag_list($cpu_flag_supported_re, | |
475 | "manually set for VM", $cpu->{flags}); | |
476 | ||
477 | my $pve_forced_flags = {}; | |
478 | $pve_forced_flags->{'enforce'} = { | |
479 | reason => "error if requested CPU settings not available", | |
480 | } if $cputype ne 'host' && $kvm && $arch eq 'x86_64'; | |
481 | $pve_forced_flags->{'kvm'} = { | |
482 | value => "off", | |
483 | reason => "hide KVM virtualization from guest", | |
484 | } if $kvm_off; | |
485 | ||
486 | # $cputype is the "reported-model" for custom types, so we can just look up | |
487 | # the vendor in the default list | |
488 | my $cpu_vendor = $cpu_vendor_list->{$cputype}; | |
489 | if ($cpu_vendor) { | |
490 | $pve_forced_flags->{'vendor'} = { | |
491 | value => $cpu_vendor, | |
492 | } if $cpu_vendor ne 'default'; | |
493 | } elsif ($arch ne 'aarch64') { | |
494 | die "internal error"; # should not happen | |
d786a274 SR |
495 | } |
496 | ||
c4581b9c | 497 | my $cpu_str = $cputype; |
d786a274 | 498 | |
c4581b9c SR |
499 | # will be resolved in parameter order |
500 | $cpu_str .= resolve_cpu_flags($pve_flags, $hv_flags, $custom_cputype_flags, | |
501 | $vm_flags, $pve_forced_flags); | |
d786a274 | 502 | |
9f9792d3 SR |
503 | my $phys_bits = ''; |
504 | foreach my $conf ($custom_cpu, $cpu) { | |
505 | next if !defined($conf); | |
506 | my $conf_val = $conf->{'phys-bits'}; | |
507 | next if !$conf_val; | |
508 | if ($conf_val eq 'host') { | |
509 | $phys_bits = ",host-phys-bits=true"; | |
510 | } else { | |
511 | $phys_bits = ",phys-bits=$conf_val"; | |
512 | } | |
513 | } | |
514 | $cpu_str .= $phys_bits; | |
515 | ||
c4581b9c SR |
516 | return ('-cpu', $cpu_str); |
517 | } | |
d786a274 | 518 | |
c4581b9c SR |
519 | # Some hardcoded flags required by certain configurations |
520 | sub get_pve_cpu_flags { | |
521 | my ($conf, $kvm, $cputype, $arch, $machine_version) = @_; | |
522 | ||
523 | my $pve_flags = {}; | |
524 | my $pve_msg = "set by PVE;"; | |
525 | ||
526 | $pve_flags->{'lahf_lm'} = { | |
527 | op => '+', | |
528 | reason => "$pve_msg to support Windows 8.1+", | |
529 | } if $cputype eq 'kvm64' && $arch eq 'x86_64'; | |
530 | ||
531 | $pve_flags->{'x2apic'} = { | |
532 | op => '-', | |
533 | reason => "$pve_msg incompatible with Solaris", | |
534 | } if $conf->{ostype} && $conf->{ostype} eq 'solaris'; | |
535 | ||
536 | $pve_flags->{'sep'} = { | |
537 | op => '+', | |
538 | reason => "$pve_msg to support Windows 8+ and improve Windows XP+", | |
539 | } if $cputype eq 'kvm64' || $cputype eq 'kvm32'; | |
540 | ||
541 | $pve_flags->{'rdtscp'} = { | |
542 | op => '-', | |
543 | reason => "$pve_msg broken on AMD Opteron", | |
544 | } if $cputype =~ m/^Opteron/; | |
545 | ||
546 | if (min_version($machine_version, 2, 3) && $kvm && $arch eq 'x86_64') { | |
547 | $pve_flags->{'kvm_pv_unhalt'} = { | |
548 | op => '+', | |
549 | reason => "$pve_msg to improve Linux guest spinlock performance", | |
550 | }; | |
551 | $pve_flags->{'kvm_pv_eoi'} = { | |
552 | op => '+', | |
553 | reason => "$pve_msg to improve Linux guest interrupt performance", | |
554 | }; | |
d786a274 SR |
555 | } |
556 | ||
c4581b9c | 557 | return $pve_flags; |
d786a274 SR |
558 | } |
559 | ||
c4581b9c SR |
560 | sub get_hyperv_enlightenments { |
561 | my ($winversion, $machine_version, $bios, $gpu_passthrough, $hv_vendor_id) = @_; | |
d786a274 SR |
562 | |
563 | return if $winversion < 6; | |
564 | return if $bios && $bios eq 'ovmf' && $winversion < 8; | |
565 | ||
c4581b9c SR |
566 | my $flags = {}; |
567 | my $default_reason = "automatic Hyper-V enlightenment for Windows"; | |
568 | my $flagfn = sub { | |
569 | my ($flag, $value, $reason) = @_; | |
570 | $flags->{$flag} = { | |
571 | reason => $reason // $default_reason, | |
572 | value => $value, | |
573 | } | |
574 | }; | |
575 | ||
576 | my $hv_vendor_set = defined($hv_vendor_id); | |
577 | if ($gpu_passthrough || $hv_vendor_set) { | |
d786a274 | 578 | $hv_vendor_id //= 'proxmox'; |
c4581b9c SR |
579 | $flagfn->('hv_vendor_id', $hv_vendor_id, $hv_vendor_set ? |
580 | "custom hv_vendor_id set" : "NVIDIA workaround for GPU passthrough"); | |
d786a274 SR |
581 | } |
582 | ||
583 | if (min_version($machine_version, 2, 3)) { | |
c4581b9c SR |
584 | $flagfn->('hv_spinlocks', '0x1fff'); |
585 | $flagfn->('hv_vapic'); | |
586 | $flagfn->('hv_time'); | |
d786a274 | 587 | } else { |
c4581b9c | 588 | $flagfn->('hv_spinlocks', '0xffff'); |
d786a274 SR |
589 | } |
590 | ||
591 | if (min_version($machine_version, 2, 6)) { | |
c4581b9c SR |
592 | $flagfn->('hv_reset'); |
593 | $flagfn->('hv_vpindex'); | |
594 | $flagfn->('hv_runtime'); | |
d786a274 SR |
595 | } |
596 | ||
597 | if ($winversion >= 7) { | |
c4581b9c SR |
598 | my $win7_reason = $default_reason . " 7 and higher"; |
599 | $flagfn->('hv_relaxed', undef, $win7_reason); | |
d786a274 SR |
600 | |
601 | if (min_version($machine_version, 2, 12)) { | |
c4581b9c SR |
602 | $flagfn->('hv_synic', undef, $win7_reason); |
603 | $flagfn->('hv_stimer', undef, $win7_reason); | |
d786a274 SR |
604 | } |
605 | ||
606 | if (min_version($machine_version, 3, 1)) { | |
c4581b9c | 607 | $flagfn->('hv_ipi', undef, $win7_reason); |
d786a274 SR |
608 | } |
609 | } | |
c4581b9c SR |
610 | |
611 | return $flags; | |
d786a274 SR |
612 | } |
613 | ||
58c64ad5 SR |
614 | sub get_cpu_from_running_vm { |
615 | my ($pid) = @_; | |
616 | ||
617 | my $cmdline = PVE::QemuServer::Helpers::parse_cmdline($pid); | |
618 | die "could not read commandline of running machine\n" | |
619 | if !$cmdline->{cpu}->{value}; | |
620 | ||
621 | # sanitize and untaint value | |
622 | $cmdline->{cpu}->{value} =~ $qemu_cmdline_cpu_re; | |
623 | return $1; | |
624 | } | |
625 | ||
b3e89488 SR |
626 | __PACKAGE__->register(); |
627 | __PACKAGE__->init(); | |
628 | ||
d786a274 | 629 | 1; |