1 package PVE
::QemuServer
::CPUConfig
;
7 use PVE
::Cluster
qw(cfs_register_file cfs_read_file);
8 use PVE
::QemuServer
::Helpers
qw(min_version);
10 use base
qw(PVE::SectionConfig Exporter);
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
19 if (PVE
::Cluster
::check_cfs_is_mounted
(1)) {
20 mkdir "/etc/pve/virtual-guest";
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(@_); });
28 sub load_custom_model_conf
{
29 return cfs_read_file
($default_filename);
32 my $cpu_vendor_list = {
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',
60 'Skylake-Client-noTSX-IBRS' => 'GenuineIntel',
61 'Skylake-Server' => 'GenuineIntel',
62 'Skylake-Server-IBRS' => 'GenuineIntel',
63 'Skylake-Server-noTSX-IBRS' => 'GenuineIntel',
64 'Cascadelake-Server' => 'GenuineIntel',
65 'Cascadelake-Server-noTSX' => 'GenuineIntel',
66 KnightsMill
=> 'GenuineIntel',
67 'Icelake-Client' => 'GenuineIntel',
68 'Icelake-Client-noTSX' => 'GenuineIntel',
69 'Icelake-Server' => 'GenuineIntel',
70 'Icelake-Server-noTSX' => 'GenuineIntel',
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 'EPYC-Rome' => 'AuthenticAMD',
84 # generic types, use vendor from host node
93 my @supported_cpu_flags = (
107 my $cpu_flag_supported_re = qr/([+-])(@{[join('|', @supported_cpu_flags)]})/;
108 my $cpu_flag_any_re = qr/([+-])([a-zA-Z0-9\-_\.]+)/;
110 our $qemu_cmdline_cpu_re = qr/^((?>[+-]?[\w\-_=]+,?)+)$/;
114 description
=> "Emulated CPU type. Can be default or custom name (custom model names must be prefixed with 'custom-').",
116 format_description
=> 'string',
121 'reported-model' => {
122 description
=> "CPU model and vendor to report to the guest. Must be a QEMU/KVM supported model."
123 . " Only valid for custom CPU model definitions, default models will always report themselves to the guest OS.",
125 enum
=> [ sort { lc("$a") cmp lc("$b") } keys %$cpu_vendor_list ],
130 description
=> "Do not identify as a KVM virtual machine.",
137 pattern
=> qr/[a-zA-Z0-9]{1,12}/,
138 format_description
=> 'vendor-id',
139 description
=> 'The Hyper-V vendor ID. Some drivers or programs inside Windows guests need a specific ID.',
143 description
=> "List of additional CPU flags separated by ';'."
144 . " Use '+FLAG' to enable, '-FLAG' to disable a flag."
145 . " Custom CPU models can specify any flag supported by"
146 . " QEMU/KVM, VM-specific flags must be from the following"
147 . " set for security reasons: @{[join(', ', @supported_cpu_flags)]}.",
148 format_description
=> '+FLAG[;-FLAG...]',
150 pattern
=> qr/$cpu_flag_any_re(;$cpu_flag_any_re)*/,
155 format
=> 'pve-phys-bits',
156 description
=> "The physical memory address bits that are reported to"
157 . " the guest OS. Should be smaller or equal to the host's."
158 . " Set to 'host' to use value from host CPU, but note that"
159 . " doing so will break live migration to CPUs with other values.",
164 PVE
::JSONSchema
::register_format
('pve-phys-bits', \
&parse_phys_bits
);
165 sub parse_phys_bits
{
166 my ($str, $noerr) = @_;
168 my $err_msg = "value must be an integer between 8 and 64 or 'host'\n";
170 if ($str !~ m/^(host|\d{1,2})$/) {
171 die $err_msg if !$noerr;
175 if ($str =~ m/^\d+$/ && (int($str) < 8 || int($str) > 64)) {
176 die $err_msg if !$noerr;
183 # $cpu_fmt describes both the CPU config passed as part of a VM config, as well
184 # as the definition of a custom CPU model. There are some slight differences
185 # though, which we catch in the custom verification function below.
186 PVE
::JSONSchema
::register_format
('pve-cpu-conf', \
&parse_cpu_conf_basic
);
187 sub parse_cpu_conf_basic
{
188 my ($cpu_str, $noerr) = @_;
190 my $cpu = eval { PVE
::JSONSchema
::parse_property_string
($cpu_fmt, $cpu_str) };
196 # required, but can't be forced in schema since it's encoded in section
197 # header for custom models
198 if (!$cpu->{cputype
}) {
199 die "CPU is missing cputype\n" if !$noerr;
206 PVE
::JSONSchema
::register_format
('pve-vm-cpu-conf', \
&parse_vm_cpu_conf
);
207 sub parse_vm_cpu_conf
{
208 my ($cpu_str, $noerr) = @_;
210 my $cpu = parse_cpu_conf_basic
($cpu_str, $noerr);
211 return undef if !$cpu;
213 my $cputype = $cpu->{cputype
};
215 # a VM-specific config is only valid if the cputype exists
216 if (is_custom_model
($cputype)) {
217 eval { get_custom_model
($cputype); };
223 if (!defined($cpu_vendor_list->{$cputype})) {
224 die "Built-in cputype '$cputype' is not defined (missing 'custom-' prefix?)\n" if !$noerr;
229 # in a VM-specific config, certain properties are limited/forbidden
231 if ($cpu->{flags
} && $cpu->{flags
} !~ m/$cpu_flag_supported_re(;$cpu_flag_supported_re)*/) {
232 die "VM-specific CPU flags must be a subset of: @{[join(', ', @supported_cpu_flags)]}\n"
237 die "Property 'reported-model' not allowed in VM-specific CPU config.\n"
238 if defined($cpu->{'reported-model'});
243 # Section config settings
245 # shallow copy, since SectionConfig modifies propertyList internally
246 propertyList
=> { %$cpu_fmt },
254 return { %$cpu_fmt };
261 sub parse_section_header
{
262 my ($class, $line) = @_;
264 my ($type, $sectionId, $errmsg, $config) =
265 $class->SUPER::parse_section_header
($line);
267 return undef if !$type;
268 return ($type, $sectionId, $errmsg, {
269 # name is given by section header, and we can always prepend 'custom-'
270 # since we're reading the custom CPU file
271 cputype
=> "custom-$sectionId",
276 my ($class, $filename, $cfg) = @_;
278 mkdir "/etc/pve/virtual-guest";
280 for my $model (keys %{$cfg->{ids
}}) {
281 my $model_conf = $cfg->{ids
}->{$model};
283 die "internal error: tried saving built-in CPU model (or missing prefix): $model_conf->{cputype}\n"
284 if !is_custom_model
($model_conf->{cputype
});
286 die "internal error: tried saving custom cpumodel with cputype (ignoring prefix: $model_conf->{cputype}) not equal to \$cfg->ids entry ($model)\n"
287 if "custom-$model" ne $model_conf->{cputype
};
289 # saved in section header
290 delete $model_conf->{cputype
};
293 $class->SUPER::write_config
($filename, $cfg);
296 sub add_cpu_json_properties
{
299 foreach my $opt (keys %$cpu_fmt) {
300 $prop->{$opt} = $cpu_fmt->{$opt};
307 my ($include_custom) = @_;
311 for my $default_model (keys %{$cpu_vendor_list}) {
313 name
=> $default_model,
315 vendor
=> $cpu_vendor_list->{$default_model},
319 return $models if !$include_custom;
321 my $conf = load_custom_model_conf
();
322 for my $custom_model (keys %{$conf->{ids
}}) {
323 my $reported_model = $conf->{ids
}->{$custom_model}->{'reported-model'};
324 $reported_model //= $cpu_fmt->{'reported-model'}->{default};
325 my $vendor = $cpu_vendor_list->{$reported_model};
327 name
=> "custom-$custom_model",
336 sub is_custom_model
{
338 return $cputype =~ m/^custom-/;
341 # Use this to get a single model in the format described by $cpu_fmt.
342 # Allows names with and without custom- prefix.
343 sub get_custom_model
{
344 my ($name, $noerr) = @_;
346 $name =~ s/^custom-//;
347 my $conf = load_custom_model_conf
();
349 my $entry = $conf->{ids
}->{$name};
350 if (!defined($entry)) {
351 die "Custom cputype '$name' not found\n" if !$noerr;
356 for my $property (keys %$cpu_fmt) {
357 if (my $value = $entry->{$property}) {
358 $model->{$property} = $value;
365 # Print a QEMU device node for a given VM configuration for hotplugging CPUs
366 sub print_cpu_device
{
367 my ($conf, $id) = @_;
369 my $kvm = $conf->{kvm
} // 1;
370 my $cpu = $kvm ?
"kvm64" : "qemu64";
371 if (my $cputype = $conf->{cpu
}) {
372 my $cpuconf = parse_cpu_conf_basic
($cputype)
373 or die "Cannot parse cpu description: $cputype\n";
374 $cpu = $cpuconf->{cputype
};
376 if (is_custom_model
($cpu)) {
377 my $custom_cpu = get_custom_model
($cpu);
379 $cpu = $custom_cpu->{'reported-model'} //
380 $cpu_fmt->{'reported-model'}->{default};
384 my $cores = $conf->{cores
} || 1;
386 my $current_core = ($id - 1) % $cores;
387 my $current_socket = int(($id - 1 - $current_core)/$cores);
389 return "$cpu-x86_64-cpu,id=cpu$id,socket-id=$current_socket,core-id=$current_core,thread-id=0";
392 # Resolves multiple arrays of hashes representing CPU flags with metadata to a
393 # single string in QEMU "-cpu" compatible format. Later arrays have higher
396 # Hashes take the following format:
399 # op => "+", # defaults to "" if undefined
400 # reason => "to support AES acceleration", # for override warnings
401 # value => "" # needed for kvm=off (value: off) etc...
405 sub resolve_cpu_flags
{
409 for my $flag_name (keys %$hash) {
410 my $flag = $hash->{$flag_name};
411 my $old_flag = $flags->{$flag_name};
414 $flag->{reason
} //= "unknown origin";
417 my $value_changed = (defined($flag->{value
}) != defined($old_flag->{value
})) ||
418 (defined($flag->{value
}) && $flag->{value
} ne $old_flag->{value
});
420 if ($old_flag->{op
} eq $flag->{op
} && !$value_changed) {
421 $flags->{$flag_name}->{reason
} .= " & $flag->{reason}";
425 my $old = print_cpuflag_hash
($flag_name, $flags->{$flag_name});
426 my $new = print_cpuflag_hash
($flag_name, $flag);
427 warn "warning: CPU flag/setting $new overwrites $old\n";
430 $flags->{$flag_name} = $flag;
435 # sort for command line stability
436 for my $flag_name (sort keys %$flags) {
438 $flag_str .= $flags->{$flag_name}->{op
};
439 $flag_str .= $flag_name;
440 $flag_str .= "=$flags->{$flag_name}->{value}"
441 if $flags->{$flag_name}->{value
};
447 sub print_cpuflag_hash
{
448 my ($flag_name, $flag) = @_;
449 my $formatted = "'$flag->{op}$flag_name";
450 $formatted .= "=$flag->{value}" if defined($flag->{value
});
452 $formatted .= " ($flag->{reason})" if defined($flag->{reason
});
456 sub parse_cpuflag_list
{
457 my ($re, $reason, $flaglist) = @_;
460 return $res if !$flaglist;
462 foreach my $flag (split(";", $flaglist)) {
464 $res->{$2} = { op
=> $1, reason
=> $reason };
471 # Calculate QEMU's '-cpu' argument from a given VM configuration
472 sub get_cpu_options
{
473 my ($conf, $arch, $kvm, $kvm_off, $machine_version, $winversion, $gpu_passthrough) = @_;
475 my $cputype = $kvm ?
"kvm64" : "qemu64";
476 if ($arch eq 'aarch64') {
477 $cputype = 'cortex-a57';
483 if (my $cpu_prop_str = $conf->{cpu
}) {
484 $cpu = parse_vm_cpu_conf
($cpu_prop_str)
485 or die "Cannot parse cpu description: $cpu_prop_str\n";
487 $cputype = $cpu->{cputype
};
489 if (is_custom_model
($cputype)) {
490 $custom_cpu = get_custom_model
($cputype);
492 $cputype = $custom_cpu->{'reported-model'} //
493 $cpu_fmt->{'reported-model'}->{default};
494 $kvm_off = $custom_cpu->{hidden
}
495 if defined($custom_cpu->{hidden
});
496 $hv_vendor_id = $custom_cpu->{'hv-vendor-id'};
499 # VM-specific settings override custom CPU config
500 $kvm_off = $cpu->{hidden
}
501 if defined($cpu->{hidden
});
502 $hv_vendor_id = $cpu->{'hv-vendor-id'}
503 if defined($cpu->{'hv-vendor-id'});
506 my $pve_flags = get_pve_cpu_flags
($conf, $kvm, $cputype, $arch,
509 my $hv_flags = get_hyperv_enlightenments
($winversion, $machine_version,
510 $conf->{bios
}, $gpu_passthrough, $hv_vendor_id) if $kvm;
512 my $custom_cputype_flags = parse_cpuflag_list
($cpu_flag_any_re,
513 "set by custom CPU model", $custom_cpu->{flags
});
515 my $vm_flags = parse_cpuflag_list
($cpu_flag_supported_re,
516 "manually set for VM", $cpu->{flags
});
518 my $pve_forced_flags = {};
519 $pve_forced_flags->{'enforce'} = {
520 reason
=> "error if requested CPU settings not available",
521 } if $cputype ne 'host' && $kvm && $arch eq 'x86_64';
522 $pve_forced_flags->{'kvm'} = {
524 reason
=> "hide KVM virtualization from guest",
527 # $cputype is the "reported-model" for custom types, so we can just look up
528 # the vendor in the default list
529 my $cpu_vendor = $cpu_vendor_list->{$cputype};
531 $pve_forced_flags->{'vendor'} = {
532 value
=> $cpu_vendor,
533 } if $cpu_vendor ne 'default';
534 } elsif ($arch ne 'aarch64') {
535 die "internal error"; # should not happen
538 my $cpu_str = $cputype;
540 # will be resolved in parameter order
541 $cpu_str .= resolve_cpu_flags
($pve_flags, $hv_flags, $custom_cputype_flags,
542 $vm_flags, $pve_forced_flags);
545 foreach my $conf ($custom_cpu, $cpu) {
546 next if !defined($conf);
547 my $conf_val = $conf->{'phys-bits'};
549 if ($conf_val eq 'host') {
550 $phys_bits = ",host-phys-bits=true";
552 $phys_bits = ",phys-bits=$conf_val";
555 $cpu_str .= $phys_bits;
557 return ('-cpu', $cpu_str);
560 # Some hardcoded flags required by certain configurations
561 sub get_pve_cpu_flags
{
562 my ($conf, $kvm, $cputype, $arch, $machine_version) = @_;
565 my $pve_msg = "set by PVE;";
567 $pve_flags->{'lahf_lm'} = {
569 reason
=> "$pve_msg to support Windows 8.1+",
570 } if $cputype eq 'kvm64' && $arch eq 'x86_64';
572 $pve_flags->{'x2apic'} = {
574 reason
=> "$pve_msg incompatible with Solaris",
575 } if $conf->{ostype
} && $conf->{ostype
} eq 'solaris';
577 $pve_flags->{'sep'} = {
579 reason
=> "$pve_msg to support Windows 8+ and improve Windows XP+",
580 } if $cputype eq 'kvm64' || $cputype eq 'kvm32';
582 $pve_flags->{'rdtscp'} = {
584 reason
=> "$pve_msg broken on AMD Opteron",
585 } if $cputype =~ m/^Opteron/;
587 if (min_version
($machine_version, 2, 3) && $kvm && $arch eq 'x86_64') {
588 $pve_flags->{'kvm_pv_unhalt'} = {
590 reason
=> "$pve_msg to improve Linux guest spinlock performance",
592 $pve_flags->{'kvm_pv_eoi'} = {
594 reason
=> "$pve_msg to improve Linux guest interrupt performance",
601 sub get_hyperv_enlightenments
{
602 my ($winversion, $machine_version, $bios, $gpu_passthrough, $hv_vendor_id) = @_;
604 return if $winversion < 6;
605 return if $bios && $bios eq 'ovmf' && $winversion < 8;
608 my $default_reason = "automatic Hyper-V enlightenment for Windows";
610 my ($flag, $value, $reason) = @_;
612 reason
=> $reason // $default_reason,
617 my $hv_vendor_set = defined($hv_vendor_id);
618 if ($gpu_passthrough || $hv_vendor_set) {
619 $hv_vendor_id //= 'proxmox';
620 $flagfn->('hv_vendor_id', $hv_vendor_id, $hv_vendor_set ?
621 "custom hv_vendor_id set" : "NVIDIA workaround for GPU passthrough");
624 if (min_version
($machine_version, 2, 3)) {
625 $flagfn->('hv_spinlocks', '0x1fff');
626 $flagfn->('hv_vapic');
627 $flagfn->('hv_time');
629 $flagfn->('hv_spinlocks', '0xffff');
632 if (min_version
($machine_version, 2, 6)) {
633 $flagfn->('hv_reset');
634 $flagfn->('hv_vpindex');
635 $flagfn->('hv_runtime');
638 if ($winversion >= 7) {
639 my $win7_reason = $default_reason . " 7 and higher";
640 $flagfn->('hv_relaxed', undef, $win7_reason);
642 if (min_version
($machine_version, 2, 12)) {
643 $flagfn->('hv_synic', undef, $win7_reason);
644 $flagfn->('hv_stimer', undef, $win7_reason);
647 if (min_version
($machine_version, 3, 1)) {
648 $flagfn->('hv_ipi', undef, $win7_reason);
655 sub get_cpu_from_running_vm
{
658 my $cmdline = PVE
::QemuServer
::Helpers
::parse_cmdline
($pid);
659 die "could not read commandline of running machine\n"
660 if !$cmdline->{cpu
}->{value
};
662 # sanitize and untaint value
663 $cmdline->{cpu
}->{value
} =~ $qemu_cmdline_cpu_re;
667 __PACKAGE__-
>register();