]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuServer/CPUConfig.pm
Add CPUConfig file and migrate some helpers
[qemu-server.git] / PVE / QemuServer / CPUConfig.pm
1 package PVE::QemuServer::CPUConfig;
2
3 use strict;
4 use warnings;
5
6 use PVE::JSONSchema;
7 use PVE::QemuServer::Helpers qw(min_version);
8
9 use base qw(Exporter);
10
11 our @EXPORT_OK = qw(
12 print_cpu_device
13 get_cpu_options
14 );
15
16 my $cpu_vendor_list = {
17 # Intel CPUs
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-Server' => 'GenuineIntel',
45 'Skylake-Server-IBRS' => 'GenuineIntel',
46 'Cascadelake-Server' => 'GenuineIntel',
47 KnightsMill => 'GenuineIntel',
48
49 # AMD CPUs
50 athlon => 'AuthenticAMD',
51 phenom => 'AuthenticAMD',
52 Opteron_G1 => 'AuthenticAMD',
53 Opteron_G2 => 'AuthenticAMD',
54 Opteron_G3 => 'AuthenticAMD',
55 Opteron_G4 => 'AuthenticAMD',
56 Opteron_G5 => 'AuthenticAMD',
57 EPYC => 'AuthenticAMD',
58 'EPYC-IBPB' => 'AuthenticAMD',
59
60 # generic types, use vendor from host node
61 host => 'default',
62 kvm32 => 'default',
63 kvm64 => 'default',
64 qemu32 => 'default',
65 qemu64 => 'default',
66 max => 'default',
67 };
68
69 my @supported_cpu_flags = (
70 'pcid',
71 'spec-ctrl',
72 'ibpb',
73 'ssbd',
74 'virt-ssbd',
75 'amd-ssbd',
76 'amd-no-ssb',
77 'pdpe1gb',
78 'md-clear',
79 'hv-tlbflush',
80 'hv-evmcs',
81 'aes'
82 );
83 my $cpu_flag = qr/[+-](@{[join('|', @supported_cpu_flags)]})/;
84
85 our $cpu_fmt = {
86 cputype => {
87 description => "Emulated CPU type.",
88 type => 'string',
89 enum => [ sort { "\L$a" cmp "\L$b" } keys %$cpu_vendor_list ],
90 default => 'kvm64',
91 default_key => 1,
92 },
93 hidden => {
94 description => "Do not identify as a KVM virtual machine.",
95 type => 'boolean',
96 optional => 1,
97 default => 0
98 },
99 'hv-vendor-id' => {
100 type => 'string',
101 pattern => qr/[a-zA-Z0-9]{1,12}/,
102 format_description => 'vendor-id',
103 description => 'The Hyper-V vendor ID. Some drivers or programs inside Windows guests need a specific ID.',
104 optional => 1,
105 },
106 flags => {
107 description => "List of additional CPU flags separated by ';'."
108 . " Use '+FLAG' to enable, '-FLAG' to disable a flag."
109 . " Currently supported flags: @{[join(', ', @supported_cpu_flags)]}.",
110 format_description => '+FLAG[;-FLAG...]',
111 type => 'string',
112 pattern => qr/$cpu_flag(;$cpu_flag)*/,
113 optional => 1,
114 },
115 };
116
117 # Print a QEMU device node for a given VM configuration for hotplugging CPUs
118 sub print_cpu_device {
119 my ($conf, $id) = @_;
120
121 my $kvm = $conf->{kvm} // 1;
122 my $cpu = $kvm ? "kvm64" : "qemu64";
123 if (my $cputype = $conf->{cpu}) {
124 my $cpuconf = PVE::JSONSchema::parse_property_string($cpu_fmt, $cputype)
125 or die "Cannot parse cpu description: $cputype\n";
126 $cpu = $cpuconf->{cputype};
127 }
128
129 my $cores = $conf->{cores} || 1;
130
131 my $current_core = ($id - 1) % $cores;
132 my $current_socket = int(($id - 1 - $current_core)/$cores);
133
134 return "$cpu-x86_64-cpu,id=cpu$id,socket-id=$current_socket,core-id=$current_core,thread-id=0";
135 }
136
137 # Calculate QEMU's '-cpu' argument from a given VM configuration
138 sub get_cpu_options {
139 my ($conf, $arch, $kvm, $kvm_off, $machine_version, $winversion, $gpu_passthrough) = @_;
140
141 my $cpuFlags = [];
142 my $ostype = $conf->{ostype};
143
144 my $cpu = $kvm ? "kvm64" : "qemu64";
145 if ($arch eq 'aarch64') {
146 $cpu = 'cortex-a57';
147 }
148 my $hv_vendor_id;
149 if (my $cputype = $conf->{cpu}) {
150 my $cpuconf = PVE::JSONSchema::parse_property_string($cpu_fmt, $cputype)
151 or die "Cannot parse cpu description: $cputype\n";
152 $cpu = $cpuconf->{cputype};
153 $kvm_off = 1 if $cpuconf->{hidden};
154 $hv_vendor_id = $cpuconf->{'hv-vendor-id'};
155
156 if (defined(my $flags = $cpuconf->{flags})) {
157 push @$cpuFlags, split(";", $flags);
158 }
159 }
160
161 push @$cpuFlags , '+lahf_lm' if $cpu eq 'kvm64' && $arch eq 'x86_64';
162
163 push @$cpuFlags , '-x2apic' if $ostype && $ostype eq 'solaris';
164
165 push @$cpuFlags, '+sep' if $cpu eq 'kvm64' || $cpu eq 'kvm32';
166
167 push @$cpuFlags, '-rdtscp' if $cpu =~ m/^Opteron/;
168
169 if (min_version($machine_version, 2, 3) && $arch eq 'x86_64') {
170
171 push @$cpuFlags , '+kvm_pv_unhalt' if $kvm;
172 push @$cpuFlags , '+kvm_pv_eoi' if $kvm;
173 }
174
175 add_hyperv_enlightenments($cpuFlags, $winversion, $machine_version, $conf->{bios}, $gpu_passthrough, $hv_vendor_id) if $kvm;
176
177 push @$cpuFlags, 'enforce' if $cpu ne 'host' && $kvm && $arch eq 'x86_64';
178
179 push @$cpuFlags, 'kvm=off' if $kvm_off;
180
181 if (my $cpu_vendor = $cpu_vendor_list->{$cpu}) {
182 push @$cpuFlags, "vendor=${cpu_vendor}"
183 if $cpu_vendor ne 'default';
184 } elsif ($arch ne 'aarch64') {
185 die "internal error"; # should not happen
186 }
187
188 $cpu .= "," . join(',', @$cpuFlags) if scalar(@$cpuFlags);
189
190 return ('-cpu', $cpu);
191 }
192
193 sub add_hyperv_enlightenments {
194 my ($cpuFlags, $winversion, $machine_version, $bios, $gpu_passthrough, $hv_vendor_id) = @_;
195
196 return if $winversion < 6;
197 return if $bios && $bios eq 'ovmf' && $winversion < 8;
198
199 if ($gpu_passthrough || defined($hv_vendor_id)) {
200 $hv_vendor_id //= 'proxmox';
201 push @$cpuFlags , "hv_vendor_id=$hv_vendor_id";
202 }
203
204 if (min_version($machine_version, 2, 3)) {
205 push @$cpuFlags , 'hv_spinlocks=0x1fff';
206 push @$cpuFlags , 'hv_vapic';
207 push @$cpuFlags , 'hv_time';
208 } else {
209 push @$cpuFlags , 'hv_spinlocks=0xffff';
210 }
211
212 if (min_version($machine_version, 2, 6)) {
213 push @$cpuFlags , 'hv_reset';
214 push @$cpuFlags , 'hv_vpindex';
215 push @$cpuFlags , 'hv_runtime';
216 }
217
218 if ($winversion >= 7) {
219 push @$cpuFlags , 'hv_relaxed';
220
221 if (min_version($machine_version, 2, 12)) {
222 push @$cpuFlags , 'hv_synic';
223 push @$cpuFlags , 'hv_stimer';
224 }
225
226 if (min_version($machine_version, 3, 1)) {
227 push @$cpuFlags , 'hv_ipi';
228 }
229 }
230 }
231
232 1;