blob: 24420f988e731d49602d7a2d09bf1458c553b740 [file] [log] [blame]
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001/*
2 * i386 CPUID helper functions
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <inttypes.h>
23
24#include "cpu.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/kvm.h"
Eduardo Habkost8932cfd2013-01-22 18:25:09 -020026#include "sysemu/cpus.h"
27#include "topology.h"
Andre Przywarac6dc6f62010-03-11 14:38:55 +010028
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/option.h"
30#include "qemu/config-file.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010031#include "qapi/qmp/qerror.h"
Andre Przywarac6dc6f62010-03-11 14:38:55 +010032
Eduardo Habkost8e8aba52013-05-06 13:20:07 -030033#include "qapi-types.h"
34#include "qapi-visit.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010035#include "qapi/visitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/arch_init.h"
Andreas Färber71ad61d2012-04-17 12:10:29 +020037
Igor Mammedov65dee382012-07-23 15:22:28 +020038#include "hw/hw.h"
Stefan Weilb834b502012-08-30 22:28:31 +020039#if defined(CONFIG_KVM)
Anthony Liguorief8621b2012-08-29 09:32:41 -050040#include <linux/kvm_para.h>
Stefan Weilb834b502012-08-30 22:28:31 +020041#endif
Igor Mammedov65dee382012-07-23 15:22:28 +020042
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/sysemu.h"
Igor Mammedov53a89e22013-04-29 19:03:01 +020044#include "hw/qdev-properties.h"
Igor Mammedov62fc4032013-04-29 18:54:13 +020045#include "hw/cpu/icc_bus.h"
Igor Mammedovbdeec802012-10-13 22:35:39 +020046#ifndef CONFIG_USER_ONLY
Paolo Bonzini0d09e412013-02-05 17:06:20 +010047#include "hw/xen/xen.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010048#include "hw/i386/apic_internal.h"
Igor Mammedovbdeec802012-10-13 22:35:39 +020049#endif
50
Eduardo Habkost5e891bf2013-08-27 12:24:37 -030051
52/* Cache topology CPUID constants: */
53
54/* CPUID Leaf 2 Descriptors */
55
56#define CPUID_2_L1D_32KB_8WAY_64B 0x2c
57#define CPUID_2_L1I_32KB_8WAY_64B 0x30
58#define CPUID_2_L2_2MB_8WAY_64B 0x7d
59
60
61/* CPUID Leaf 4 constants: */
62
63/* EAX: */
64#define CPUID_4_TYPE_DCACHE 1
65#define CPUID_4_TYPE_ICACHE 2
66#define CPUID_4_TYPE_UNIFIED 3
67
68#define CPUID_4_LEVEL(l) ((l) << 5)
69
70#define CPUID_4_SELF_INIT_LEVEL (1 << 8)
71#define CPUID_4_FULLY_ASSOC (1 << 9)
72
73/* EDX: */
74#define CPUID_4_NO_INVD_SHARING (1 << 0)
75#define CPUID_4_INCLUSIVE (1 << 1)
76#define CPUID_4_COMPLEX_IDX (1 << 2)
77
78#define ASSOC_FULL 0xFF
79
80/* AMD associativity encoding used on CPUID Leaf 0x80000006: */
81#define AMD_ENC_ASSOC(a) (a <= 1 ? a : \
82 a == 2 ? 0x2 : \
83 a == 4 ? 0x4 : \
84 a == 8 ? 0x6 : \
85 a == 16 ? 0x8 : \
86 a == 32 ? 0xA : \
87 a == 48 ? 0xB : \
88 a == 64 ? 0xC : \
89 a == 96 ? 0xD : \
90 a == 128 ? 0xE : \
91 a == ASSOC_FULL ? 0xF : \
92 0 /* invalid value */)
93
94
95/* Definitions of the hardcoded cache entries we expose: */
96
97/* L1 data cache: */
98#define L1D_LINE_SIZE 64
99#define L1D_ASSOCIATIVITY 8
100#define L1D_SETS 64
101#define L1D_PARTITIONS 1
102/* Size = LINE_SIZE*ASSOCIATIVITY*SETS*PARTITIONS = 32KiB */
103#define L1D_DESCRIPTOR CPUID_2_L1D_32KB_8WAY_64B
104/*FIXME: CPUID leaf 0x80000005 is inconsistent with leaves 2 & 4 */
105#define L1D_LINES_PER_TAG 1
106#define L1D_SIZE_KB_AMD 64
107#define L1D_ASSOCIATIVITY_AMD 2
108
109/* L1 instruction cache: */
110#define L1I_LINE_SIZE 64
111#define L1I_ASSOCIATIVITY 8
112#define L1I_SETS 64
113#define L1I_PARTITIONS 1
114/* Size = LINE_SIZE*ASSOCIATIVITY*SETS*PARTITIONS = 32KiB */
115#define L1I_DESCRIPTOR CPUID_2_L1I_32KB_8WAY_64B
116/*FIXME: CPUID leaf 0x80000005 is inconsistent with leaves 2 & 4 */
117#define L1I_LINES_PER_TAG 1
118#define L1I_SIZE_KB_AMD 64
119#define L1I_ASSOCIATIVITY_AMD 2
120
121/* Level 2 unified cache: */
122#define L2_LINE_SIZE 64
123#define L2_ASSOCIATIVITY 16
124#define L2_SETS 4096
125#define L2_PARTITIONS 1
126/* Size = LINE_SIZE*ASSOCIATIVITY*SETS*PARTITIONS = 4MiB */
127/*FIXME: CPUID leaf 2 descriptor is inconsistent with CPUID leaf 4 */
128#define L2_DESCRIPTOR CPUID_2_L2_2MB_8WAY_64B
129/*FIXME: CPUID leaf 0x80000006 is inconsistent with leaves 2 & 4 */
130#define L2_LINES_PER_TAG 1
131#define L2_SIZE_KB_AMD 512
132
133/* No L3 cache: */
134#define L3_SIZE_KB 0 /* disabled */
135#define L3_ASSOCIATIVITY 0 /* disabled */
136#define L3_LINES_PER_TAG 0 /* disabled */
137#define L3_LINE_SIZE 0 /* disabled */
138
139/* TLB definitions: */
140
141#define L1_DTLB_2M_ASSOC 1
142#define L1_DTLB_2M_ENTRIES 255
143#define L1_DTLB_4K_ASSOC 1
144#define L1_DTLB_4K_ENTRIES 255
145
146#define L1_ITLB_2M_ASSOC 1
147#define L1_ITLB_2M_ENTRIES 255
148#define L1_ITLB_4K_ASSOC 1
149#define L1_ITLB_4K_ENTRIES 255
150
151#define L2_DTLB_2M_ASSOC 0 /* disabled */
152#define L2_DTLB_2M_ENTRIES 0 /* disabled */
153#define L2_DTLB_4K_ASSOC 4
154#define L2_DTLB_4K_ENTRIES 512
155
156#define L2_ITLB_2M_ASSOC 0 /* disabled */
157#define L2_ITLB_2M_ENTRIES 0 /* disabled */
158#define L2_ITLB_4K_ASSOC 4
159#define L2_ITLB_4K_ENTRIES 512
160
161
162
Igor Mammedov99b88a12013-01-21 15:06:36 +0100163static void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
164 uint32_t vendor2, uint32_t vendor3)
165{
166 int i;
167 for (i = 0; i < 4; i++) {
168 dst[i] = vendor1 >> (8 * i);
169 dst[i + 4] = vendor2 >> (8 * i);
170 dst[i + 8] = vendor3 >> (8 * i);
171 }
172 dst[CPUID_VENDOR_SZ] = '\0';
173}
174
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100175/* feature flags taken from "Intel Processor Identification and the CPUID
176 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
177 * between feature naming conventions, aliases may be added.
178 */
179static const char *feature_name[] = {
180 "fpu", "vme", "de", "pse",
181 "tsc", "msr", "pae", "mce",
182 "cx8", "apic", NULL, "sep",
183 "mtrr", "pge", "mca", "cmov",
184 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
185 NULL, "ds" /* Intel dts */, "acpi", "mmx",
186 "fxsr", "sse", "sse2", "ss",
187 "ht" /* Intel htt */, "tm", "ia64", "pbe",
188};
189static const char *ext_feature_name[] = {
Eduardo Habkostf370be32012-02-17 14:41:20 -0200190 "pni|sse3" /* Intel,AMD sse3 */, "pclmulqdq|pclmuldq", "dtes64", "monitor",
Andre Przywarae117f772010-03-11 14:38:59 +0100191 "ds_cpl", "vmx", "smx", "est",
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100192 "tm2", "ssse3", "cid", NULL,
Andre Przywarae117f772010-03-11 14:38:59 +0100193 "fma", "cx16", "xtpr", "pdcm",
Mao, Junjie434acb82012-07-20 07:08:21 +0000194 NULL, "pcid", "dca", "sse4.1|sse4_1",
Andre Przywarae117f772010-03-11 14:38:59 +0100195 "sse4.2|sse4_2", "x2apic", "movbe", "popcnt",
Eduardo Habkosteaf3f092012-03-06 15:11:30 -0300196 "tsc-deadline", "aes", "xsave", "osxsave",
Andre Przywarac8acc382012-11-14 16:28:52 -0200197 "avx", "f16c", "rdrand", "hypervisor",
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100198};
Eduardo Habkost3b671a42012-09-06 10:05:38 +0000199/* Feature names that are already defined on feature_name[] but are set on
200 * CPUID[8000_0001].EDX on AMD CPUs don't have their names on
201 * ext2_feature_name[]. They are copied automatically to cpuid_ext2_features
202 * if and only if CPU vendor is AMD.
203 */
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100204static const char *ext2_feature_name[] = {
Eduardo Habkost3b671a42012-09-06 10:05:38 +0000205 NULL /* fpu */, NULL /* vme */, NULL /* de */, NULL /* pse */,
206 NULL /* tsc */, NULL /* msr */, NULL /* pae */, NULL /* mce */,
207 NULL /* cx8 */ /* AMD CMPXCHG8B */, NULL /* apic */, NULL, "syscall",
208 NULL /* mtrr */, NULL /* pge */, NULL /* mca */, NULL /* cmov */,
209 NULL /* pat */, NULL /* pse36 */, NULL, NULL /* Linux mp */,
210 "nx|xd", NULL, "mmxext", NULL /* mmx */,
211 NULL /* fxsr */, "fxsr_opt|ffxsr", "pdpe1gb" /* AMD Page1GB */, "rdtscp",
Eduardo Habkost01f590d2012-10-24 12:10:33 -0200212 NULL, "lm|i64", "3dnowext", "3dnow",
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100213};
214static const char *ext3_feature_name[] = {
215 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
216 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
Andre Przywarae117f772010-03-11 14:38:59 +0100217 "3dnowprefetch", "osvw", "ibs", "xop",
Andre Przywarac8acc382012-11-14 16:28:52 -0200218 "skinit", "wdt", NULL, "lwp",
219 "fma4", "tce", NULL, "nodeid_msr",
220 NULL, "tbm", "topoext", "perfctr_core",
221 "perfctr_nb", NULL, NULL, NULL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100222 NULL, NULL, NULL, NULL,
223};
224
Eduardo Habkost89e49c82013-01-07 16:20:47 -0200225static const char *ext4_feature_name[] = {
226 NULL, NULL, "xstore", "xstore-en",
227 NULL, NULL, "xcrypt", "xcrypt-en",
228 "ace2", "ace2-en", "phe", "phe-en",
229 "pmm", "pmm-en", NULL, NULL,
230 NULL, NULL, NULL, NULL,
231 NULL, NULL, NULL, NULL,
232 NULL, NULL, NULL, NULL,
233 NULL, NULL, NULL, NULL,
234};
235
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100236static const char *kvm_feature_name[] = {
Don Slutzc3d39802012-10-12 15:43:23 -0400237 "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock",
Andrew Jonesf010bc62013-09-18 16:41:45 +0200238 "kvm_asyncpf", "kvm_steal_time", "kvm_pv_eoi", "kvm_pv_unhalt",
Don Slutzc3d39802012-10-12 15:43:23 -0400239 NULL, NULL, NULL, NULL,
240 NULL, NULL, NULL, NULL,
241 NULL, NULL, NULL, NULL,
242 NULL, NULL, NULL, NULL,
243 NULL, NULL, NULL, NULL,
244 NULL, NULL, NULL, NULL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100245};
246
Joerg Roedel296acb62010-09-27 15:16:17 +0200247static const char *svm_feature_name[] = {
248 "npt", "lbrv", "svm_lock", "nrip_save",
249 "tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists",
250 NULL, NULL, "pause_filter", NULL,
251 "pfthreshold", NULL, NULL, NULL,
252 NULL, NULL, NULL, NULL,
253 NULL, NULL, NULL, NULL,
254 NULL, NULL, NULL, NULL,
255 NULL, NULL, NULL, NULL,
256};
257
H. Peter Anvina9321a42012-09-26 13:18:43 -0700258static const char *cpuid_7_0_ebx_feature_name[] = {
Eduardo Habkost811a8ae2012-10-09 11:03:59 -0300259 "fsgsbase", NULL, NULL, "bmi1", "hle", "avx2", NULL, "smep",
260 "bmi2", "erms", "invpcid", "rtm", NULL, NULL, NULL, NULL,
Andre Przywarac8acc382012-11-14 16:28:52 -0200261 NULL, NULL, "rdseed", "adx", "smap", NULL, NULL, NULL,
H. Peter Anvina9321a42012-09-26 13:18:43 -0700262 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
263};
264
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200265typedef struct FeatureWordInfo {
266 const char **feat_names;
Eduardo Habkost04d104b2013-04-22 16:00:16 -0300267 uint32_t cpuid_eax; /* Input EAX for CPUID */
268 bool cpuid_needs_ecx; /* CPUID instruction uses ECX as input */
269 uint32_t cpuid_ecx; /* Input ECX value for CPUID */
270 int cpuid_reg; /* output register (R_* constant) */
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200271} FeatureWordInfo;
272
273static FeatureWordInfo feature_word_info[FEATURE_WORDS] = {
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200274 [FEAT_1_EDX] = {
275 .feat_names = feature_name,
276 .cpuid_eax = 1, .cpuid_reg = R_EDX,
277 },
278 [FEAT_1_ECX] = {
279 .feat_names = ext_feature_name,
280 .cpuid_eax = 1, .cpuid_reg = R_ECX,
281 },
282 [FEAT_8000_0001_EDX] = {
283 .feat_names = ext2_feature_name,
284 .cpuid_eax = 0x80000001, .cpuid_reg = R_EDX,
285 },
286 [FEAT_8000_0001_ECX] = {
287 .feat_names = ext3_feature_name,
288 .cpuid_eax = 0x80000001, .cpuid_reg = R_ECX,
289 },
Eduardo Habkost89e49c82013-01-07 16:20:47 -0200290 [FEAT_C000_0001_EDX] = {
291 .feat_names = ext4_feature_name,
292 .cpuid_eax = 0xC0000001, .cpuid_reg = R_EDX,
293 },
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200294 [FEAT_KVM] = {
295 .feat_names = kvm_feature_name,
296 .cpuid_eax = KVM_CPUID_FEATURES, .cpuid_reg = R_EAX,
297 },
298 [FEAT_SVM] = {
299 .feat_names = svm_feature_name,
300 .cpuid_eax = 0x8000000A, .cpuid_reg = R_EDX,
301 },
302 [FEAT_7_0_EBX] = {
303 .feat_names = cpuid_7_0_ebx_feature_name,
Eduardo Habkost04d104b2013-04-22 16:00:16 -0300304 .cpuid_eax = 7,
305 .cpuid_needs_ecx = true, .cpuid_ecx = 0,
306 .cpuid_reg = R_EBX,
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200307 },
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200308};
309
Eduardo Habkost8e8aba52013-05-06 13:20:07 -0300310typedef struct X86RegisterInfo32 {
311 /* Name of register */
312 const char *name;
313 /* QAPI enum value register */
314 X86CPURegister32 qapi_enum;
315} X86RegisterInfo32;
316
317#define REGISTER(reg) \
Wenchao Xia5d371f42014-03-04 18:44:40 -0800318 [R_##reg] = { .name = #reg, .qapi_enum = X86_CPU_REGISTER32_##reg }
Eduardo Habkost8e8aba52013-05-06 13:20:07 -0300319X86RegisterInfo32 x86_reg_info_32[CPU_NB_REGS32] = {
320 REGISTER(EAX),
321 REGISTER(ECX),
322 REGISTER(EDX),
323 REGISTER(EBX),
324 REGISTER(ESP),
325 REGISTER(EBP),
326 REGISTER(ESI),
327 REGISTER(EDI),
328};
329#undef REGISTER
330
Paolo Bonzini2560f192013-10-02 17:54:57 +0200331typedef struct ExtSaveArea {
332 uint32_t feature, bits;
333 uint32_t offset, size;
334} ExtSaveArea;
335
336static const ExtSaveArea ext_save_areas[] = {
337 [2] = { .feature = FEAT_1_ECX, .bits = CPUID_EXT_AVX,
Liu Jinsong33f373d2013-12-03 04:17:50 +0800338 .offset = 0x240, .size = 0x100 },
Liu Jinsong79e9ebe2013-12-05 08:32:12 +0800339 [3] = { .feature = FEAT_7_0_EBX, .bits = CPUID_7_0_EBX_MPX,
340 .offset = 0x3c0, .size = 0x40 },
341 [4] = { .feature = FEAT_7_0_EBX, .bits = CPUID_7_0_EBX_MPX,
Liu, Jinsongb0f15a52014-03-03 05:24:14 +0000342 .offset = 0x400, .size = 0x40 },
Paolo Bonzini2560f192013-10-02 17:54:57 +0200343};
Eduardo Habkost8e8aba52013-05-06 13:20:07 -0300344
Eduardo Habkost8b4bedd2013-01-04 20:01:06 -0200345const char *get_register_name_32(unsigned int reg)
346{
Igor Mammedov31ccdde2013-06-03 18:23:27 +0200347 if (reg >= CPU_NB_REGS32) {
Eduardo Habkost8b4bedd2013-01-04 20:01:06 -0200348 return NULL;
349 }
Eduardo Habkost8e8aba52013-05-06 13:20:07 -0300350 return x86_reg_info_32[reg].name;
Eduardo Habkost8b4bedd2013-01-04 20:01:06 -0200351}
352
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100353/* collects per-function cpuid data
354 */
355typedef struct model_features_t {
356 uint32_t *guest_feat;
357 uint32_t *host_feat;
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200358 FeatureWord feat_word;
Eduardo Habkost8b4bedd2013-01-04 20:01:06 -0200359} model_features_t;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100360
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200361static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
362 (1 << KVM_FEATURE_NOP_IO_DELAY) |
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200363 (1 << KVM_FEATURE_CLOCKSOURCE2) |
364 (1 << KVM_FEATURE_ASYNC_PF) |
365 (1 << KVM_FEATURE_STEAL_TIME) |
Eduardo Habkost29694752013-01-17 18:59:29 -0200366 (1 << KVM_FEATURE_PV_EOI) |
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200367 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200368
Eduardo Habkost29694752013-01-17 18:59:29 -0200369void disable_kvm_pv_eoi(void)
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200370{
Eduardo Habkost29694752013-01-17 18:59:29 -0200371 kvm_default_features &= ~(1UL << KVM_FEATURE_PV_EOI);
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200372}
373
Jan Kiszkabb44e0d2011-01-21 21:48:07 +0100374void host_cpuid(uint32_t function, uint32_t count,
375 uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
Andre Przywarabdde4762010-03-11 14:38:58 +0100376{
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600377 uint32_t vec[4];
378
379#ifdef __x86_64__
380 asm volatile("cpuid"
381 : "=a"(vec[0]), "=b"(vec[1]),
382 "=c"(vec[2]), "=d"(vec[3])
383 : "0"(function), "c"(count) : "cc");
Eduardo Habkostc1f41222014-01-30 17:48:53 -0200384#elif defined(__i386__)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600385 asm volatile("pusha \n\t"
386 "cpuid \n\t"
387 "mov %%eax, 0(%2) \n\t"
388 "mov %%ebx, 4(%2) \n\t"
389 "mov %%ecx, 8(%2) \n\t"
390 "mov %%edx, 12(%2) \n\t"
391 "popa"
392 : : "a"(function), "c"(count), "S"(vec)
393 : "memory", "cc");
Eduardo Habkostc1f41222014-01-30 17:48:53 -0200394#else
395 abort();
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600396#endif
397
Andre Przywarabdde4762010-03-11 14:38:58 +0100398 if (eax)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600399 *eax = vec[0];
Andre Przywarabdde4762010-03-11 14:38:58 +0100400 if (ebx)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600401 *ebx = vec[1];
Andre Przywarabdde4762010-03-11 14:38:58 +0100402 if (ecx)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600403 *ecx = vec[2];
Andre Przywarabdde4762010-03-11 14:38:58 +0100404 if (edx)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600405 *edx = vec[3];
Andre Przywarabdde4762010-03-11 14:38:58 +0100406}
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100407
408#define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
409
410/* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
411 * a substring. ex if !NULL points to the first char after a substring,
412 * otherwise the string is assumed to sized by a terminating nul.
413 * Return lexical ordering of *s1:*s2.
414 */
415static int sstrcmp(const char *s1, const char *e1, const char *s2,
416 const char *e2)
417{
418 for (;;) {
419 if (!*s1 || !*s2 || *s1 != *s2)
420 return (*s1 - *s2);
421 ++s1, ++s2;
422 if (s1 == e1 && s2 == e2)
423 return (0);
424 else if (s1 == e1)
425 return (*s2);
426 else if (s2 == e2)
427 return (*s1);
428 }
429}
430
431/* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
432 * '|' delimited (possibly empty) strings in which case search for a match
433 * within the alternatives proceeds left to right. Return 0 for success,
434 * non-zero otherwise.
435 */
436static int altcmp(const char *s, const char *e, const char *altstr)
437{
438 const char *p, *q;
439
440 for (q = p = altstr; ; ) {
441 while (*p && *p != '|')
442 ++p;
443 if ((q == p && !*s) || (q != p && !sstrcmp(s, e, q, p)))
444 return (0);
445 if (!*p)
446 return (1);
447 else
448 q = ++p;
449 }
450}
451
452/* search featureset for flag *[s..e), if found set corresponding bit in
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200453 * *pval and return true, otherwise return false
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100454 */
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200455static bool lookup_feature(uint32_t *pval, const char *s, const char *e,
456 const char **featureset)
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100457{
458 uint32_t mask;
459 const char **ppc;
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200460 bool found = false;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100461
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200462 for (mask = 1, ppc = featureset; mask; mask <<= 1, ++ppc) {
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100463 if (*ppc && !altcmp(s, e, *ppc)) {
464 *pval |= mask;
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200465 found = true;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100466 }
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200467 }
468 return found;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100469}
470
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200471static void add_flagname_to_bitmaps(const char *flagname,
472 FeatureWordArray words)
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100473{
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200474 FeatureWord w;
475 for (w = 0; w < FEATURE_WORDS; w++) {
476 FeatureWordInfo *wi = &feature_word_info[w];
477 if (wi->feat_names &&
478 lookup_feature(&words[w], flagname, NULL, wi->feat_names)) {
479 break;
480 }
481 }
482 if (w == FEATURE_WORDS) {
483 fprintf(stderr, "CPU feature %s not found\n", flagname);
484 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100485}
486
Eduardo Habkost9576de72014-01-30 17:48:58 -0200487typedef struct X86CPUDefinition {
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100488 const char *name;
489 uint32_t level;
Eduardo Habkost90e4b0c2013-04-22 16:00:12 -0300490 uint32_t xlevel;
491 uint32_t xlevel2;
Igor Mammedov99b88a12013-01-21 15:06:36 +0100492 /* vendor is zero-terminated, 12 character ASCII string */
493 char vendor[CPUID_VENDOR_SZ + 1];
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100494 int family;
495 int model;
496 int stepping;
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300497 FeatureWordArray features;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100498 char model_id[48];
Benoît Canet787aaf52013-09-02 17:06:37 +0200499 bool cache_info_passthrough;
Eduardo Habkost9576de72014-01-30 17:48:58 -0200500} X86CPUDefinition;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100501
502#define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
503#define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
504 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
505#define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
506 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
507 CPUID_PSE36 | CPUID_FXSR)
508#define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
509#define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
510 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
511 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
512 CPUID_PAE | CPUID_SEP | CPUID_APIC)
513
Andre Przywara551a2de2010-03-11 14:39:03 +0100514#define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
515 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
516 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
517 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
518 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100519 /* partly implemented:
520 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
521 CPUID_PSE36 (needed for Solaris) */
522 /* missing:
523 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
Aurelien Jarnoe71827b2013-03-31 12:58:30 +0200524#define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_PCLMULQDQ | \
525 CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | \
526 CPUID_EXT_SSE41 | CPUID_EXT_SSE42 | CPUID_EXT_POPCNT | \
Aurelien Jarnod6400452013-03-31 12:58:31 +0200527 CPUID_EXT_MOVBE | CPUID_EXT_AES | CPUID_EXT_HYPERVISOR)
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100528 /* missing:
Aurelien Jarnoe71827b2013-03-31 12:58:30 +0200529 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_SMX,
530 CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_CID, CPUID_EXT_FMA,
531 CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_PCID, CPUID_EXT_DCA,
Aurelien Jarnod6400452013-03-31 12:58:31 +0200532 CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER, CPUID_EXT_XSAVE,
533 CPUID_EXT_OSXSAVE, CPUID_EXT_AVX, CPUID_EXT_F16C,
Aurelien Jarno83f7dc22013-03-26 19:56:02 +0100534 CPUID_EXT_RDRAND */
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000535#define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
Andre Przywara551a2de2010-03-11 14:39:03 +0100536 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
537 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100538 /* missing:
539 CPUID_EXT2_PDPE1GB */
Andre Przywara551a2de2010-03-11 14:39:03 +0100540#define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
541 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
Joerg Roedel296acb62010-09-27 15:16:17 +0200542#define TCG_SVM_FEATURES 0
Richard Henderson7073fba2013-01-23 16:17:10 -0800543#define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP \
Richard Hendersoncd7f97c2013-01-23 18:17:33 -0800544 CPUID_7_0_EBX_BMI1 | CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ADX)
Richard Henderson111994e2013-01-10 12:06:59 -0800545 /* missing:
Richard Henderson7073fba2013-01-23 16:17:10 -0800546 CPUID_7_0_EBX_FSGSBASE, CPUID_7_0_EBX_HLE, CPUID_7_0_EBX_AVX2,
547 CPUID_7_0_EBX_ERMS, CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM,
Richard Hendersoncd7f97c2013-01-23 18:17:33 -0800548 CPUID_7_0_EBX_RDSEED */
Andre Przywara551a2de2010-03-11 14:39:03 +0100549
Andreas Färber7fc9b712013-01-21 01:02:28 +0100550/* built-in CPU model definitions
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100551 */
Eduardo Habkost9576de72014-01-30 17:48:58 -0200552static X86CPUDefinition builtin_x86_defs[] = {
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100553 {
554 .name = "qemu64",
555 .level = 4,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100556 .vendor = CPUID_VENDOR_AMD,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100557 .family = 6,
Eduardo Habkostf8e6a112013-09-10 17:48:59 -0300558 .model = 6,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100559 .stepping = 3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300560 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300561 PPRO_FEATURES |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100562 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100563 CPUID_PSE36,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300564 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300565 CPUID_EXT_SSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300566 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300567 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100568 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300569 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300570 CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100571 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
572 .xlevel = 0x8000000A,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100573 },
574 {
575 .name = "phenom",
576 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100577 .vendor = CPUID_VENDOR_AMD,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100578 .family = 16,
579 .model = 2,
580 .stepping = 3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300581 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300582 PPRO_FEATURES |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100583 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100584 CPUID_PSE36 | CPUID_VME | CPUID_HT,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300585 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300586 CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_CX16 |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100587 CPUID_EXT_POPCNT,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300588 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300589 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100590 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
591 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100592 CPUID_EXT2_FFXSR | CPUID_EXT2_PDPE1GB | CPUID_EXT2_RDTSCP,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100593 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
594 CPUID_EXT3_CR8LEG,
595 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
596 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300597 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300598 CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100599 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300600 .features[FEAT_SVM] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300601 CPUID_SVM_NPT | CPUID_SVM_LBRV,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100602 .xlevel = 0x8000001A,
603 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
604 },
605 {
606 .name = "core2duo",
607 .level = 10,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100608 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100609 .family = 6,
610 .model = 15,
611 .stepping = 11,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300612 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300613 PPRO_FEATURES |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100614 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100615 CPUID_PSE36 | CPUID_VME | CPUID_DTS | CPUID_ACPI | CPUID_SS |
616 CPUID_HT | CPUID_TM | CPUID_PBE,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300617 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300618 CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100619 CPUID_EXT_DTES64 | CPUID_EXT_DSCPL | CPUID_EXT_VMX | CPUID_EXT_EST |
620 CPUID_EXT_TM2 | CPUID_EXT_CX16 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300621 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300622 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300623 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300624 CPUID_EXT3_LAHF_LM,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100625 .xlevel = 0x80000008,
626 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
627 },
628 {
629 .name = "kvm64",
630 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100631 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100632 .family = 15,
633 .model = 6,
634 .stepping = 1,
635 /* Missing: CPUID_VME, CPUID_HT */
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300636 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300637 PPRO_FEATURES |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100638 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
639 CPUID_PSE36,
640 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300641 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300642 CPUID_EXT_SSE3 | CPUID_EXT_CX16,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100643 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300644 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300645 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100646 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
647 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
648 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
649 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
650 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300651 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300652 0,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100653 .xlevel = 0x80000008,
654 .model_id = "Common KVM processor"
655 },
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100656 {
657 .name = "qemu32",
658 .level = 4,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100659 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100660 .family = 6,
Eduardo Habkostf8e6a112013-09-10 17:48:59 -0300661 .model = 6,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100662 .stepping = 3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300663 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300664 PPRO_FEATURES,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300665 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300666 CPUID_EXT_SSE3 | CPUID_EXT_POPCNT,
Andre Przywara58012d62010-03-11 14:39:06 +0100667 .xlevel = 0x80000004,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100668 },
669 {
Andre Przywaraeafaf1e2010-05-21 09:50:51 +0200670 .name = "kvm32",
671 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100672 .vendor = CPUID_VENDOR_INTEL,
Andre Przywaraeafaf1e2010-05-21 09:50:51 +0200673 .family = 15,
674 .model = 6,
675 .stepping = 1,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300676 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300677 PPRO_FEATURES |
Andre Przywaraeafaf1e2010-05-21 09:50:51 +0200678 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_PSE36,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300679 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300680 CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300681 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300682 PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300683 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300684 0,
Andre Przywaraeafaf1e2010-05-21 09:50:51 +0200685 .xlevel = 0x80000008,
686 .model_id = "Common 32-bit KVM processor"
687 },
688 {
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100689 .name = "coreduo",
690 .level = 10,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100691 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100692 .family = 6,
693 .model = 14,
694 .stepping = 8,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300695 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300696 PPRO_FEATURES | CPUID_VME |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100697 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_DTS | CPUID_ACPI |
698 CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300699 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300700 CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_VMX |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100701 CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300702 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300703 CPUID_EXT2_NX,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100704 .xlevel = 0x80000008,
705 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
706 },
707 {
708 .name = "486",
Andre Przywara58012d62010-03-11 14:39:06 +0100709 .level = 1,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100710 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100711 .family = 4,
Andreas Färberb2a856d2013-05-01 17:30:51 +0200712 .model = 8,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100713 .stepping = 0,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300714 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300715 I486_FEATURES,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100716 .xlevel = 0,
717 },
718 {
719 .name = "pentium",
720 .level = 1,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100721 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100722 .family = 5,
723 .model = 4,
724 .stepping = 3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300725 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300726 PENTIUM_FEATURES,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100727 .xlevel = 0,
728 },
729 {
730 .name = "pentium2",
731 .level = 2,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100732 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100733 .family = 6,
734 .model = 5,
735 .stepping = 2,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300736 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300737 PENTIUM2_FEATURES,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100738 .xlevel = 0,
739 },
740 {
741 .name = "pentium3",
742 .level = 2,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100743 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100744 .family = 6,
745 .model = 7,
746 .stepping = 3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300747 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300748 PENTIUM3_FEATURES,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100749 .xlevel = 0,
750 },
751 {
752 .name = "athlon",
753 .level = 2,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100754 .vendor = CPUID_VENDOR_AMD,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100755 .family = 6,
756 .model = 2,
757 .stepping = 3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300758 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300759 PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR |
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000760 CPUID_MCA,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300761 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300762 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000763 CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100764 .xlevel = 0x80000008,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100765 },
766 {
767 .name = "n270",
768 /* original is on level 10 */
769 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100770 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100771 .family = 6,
772 .model = 28,
773 .stepping = 2,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300774 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300775 PPRO_FEATURES |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100776 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME | CPUID_DTS |
777 CPUID_ACPI | CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100778 /* Some CPUs got no CPUID_SEP */
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300779 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300780 CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
Borislav Petkov4458c232013-04-25 15:43:04 -0300781 CPUID_EXT_DSCPL | CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR |
782 CPUID_EXT_MOVBE,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300783 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300784 (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000785 CPUID_EXT2_NX,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300786 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300787 CPUID_EXT3_LAHF_LM,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100788 .xlevel = 0x8000000A,
789 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
790 },
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300791 {
792 .name = "Conroe",
Eduardo Habkost6b113222013-05-27 17:23:55 -0300793 .level = 4,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100794 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300795 .family = 6,
Eduardo Habkostffce9eb2013-05-27 17:23:54 -0300796 .model = 15,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300797 .stepping = 3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300798 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300799 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300800 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
801 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
802 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
803 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300804 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300805 CPUID_EXT_SSSE3 | CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300806 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300807 CPUID_EXT2_LM | CPUID_EXT2_NX | CPUID_EXT2_SYSCALL,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300808 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300809 CPUID_EXT3_LAHF_LM,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300810 .xlevel = 0x8000000A,
811 .model_id = "Intel Celeron_4x0 (Conroe/Merom Class Core 2)",
812 },
813 {
814 .name = "Penryn",
Eduardo Habkost6b113222013-05-27 17:23:55 -0300815 .level = 4,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100816 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300817 .family = 6,
Eduardo Habkostffce9eb2013-05-27 17:23:54 -0300818 .model = 23,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300819 .stepping = 3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300820 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300821 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300822 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
823 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
824 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
825 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300826 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300827 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300828 CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300829 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300830 CPUID_EXT2_LM | CPUID_EXT2_NX | CPUID_EXT2_SYSCALL,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300831 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300832 CPUID_EXT3_LAHF_LM,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300833 .xlevel = 0x8000000A,
834 .model_id = "Intel Core 2 Duo P9xxx (Penryn Class Core 2)",
835 },
836 {
837 .name = "Nehalem",
Eduardo Habkost6b113222013-05-27 17:23:55 -0300838 .level = 4,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100839 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300840 .family = 6,
Eduardo Habkostffce9eb2013-05-27 17:23:54 -0300841 .model = 26,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300842 .stepping = 3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300843 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300844 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300845 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
846 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
847 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
848 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300849 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300850 CPUID_EXT_POPCNT | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300851 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300852 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300853 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300854 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300855 CPUID_EXT3_LAHF_LM,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300856 .xlevel = 0x8000000A,
857 .model_id = "Intel Core i7 9xx (Nehalem Class Core i7)",
858 },
859 {
860 .name = "Westmere",
861 .level = 11,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100862 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300863 .family = 6,
864 .model = 44,
865 .stepping = 1,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300866 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300867 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300868 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
869 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
870 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
871 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300872 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300873 CPUID_EXT_AES | CPUID_EXT_POPCNT | CPUID_EXT_SSE42 |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300874 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
Aurelien Jarno41cb3832013-03-31 12:58:30 +0200875 CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300876 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300877 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300878 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300879 CPUID_EXT3_LAHF_LM,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300880 .xlevel = 0x8000000A,
881 .model_id = "Westmere E56xx/L56xx/X56xx (Nehalem-C)",
882 },
883 {
884 .name = "SandyBridge",
885 .level = 0xd,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100886 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300887 .family = 6,
888 .model = 42,
889 .stepping = 1,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300890 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300891 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300892 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
893 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
894 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
895 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300896 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300897 CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300898 CPUID_EXT_TSC_DEADLINE_TIMER | CPUID_EXT_POPCNT |
899 CPUID_EXT_X2APIC | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
900 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ |
901 CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300902 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300903 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_NX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300904 CPUID_EXT2_SYSCALL,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300905 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300906 CPUID_EXT3_LAHF_LM,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300907 .xlevel = 0x8000000A,
908 .model_id = "Intel Xeon E312xx (Sandy Bridge)",
909 },
910 {
Eduardo Habkost37507092012-11-14 16:28:54 -0200911 .name = "Haswell",
912 .level = 0xd,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100913 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost37507092012-11-14 16:28:54 -0200914 .family = 6,
915 .model = 60,
916 .stepping = 1,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300917 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300918 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost37507092012-11-14 16:28:54 -0200919 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
Eduardo Habkost80ae4162012-11-22 13:31:03 -0200920 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
Eduardo Habkost37507092012-11-14 16:28:54 -0200921 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
922 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300923 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300924 CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
Eduardo Habkost37507092012-11-14 16:28:54 -0200925 CPUID_EXT_POPCNT | CPUID_EXT_X2APIC | CPUID_EXT_SSE42 |
926 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
927 CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3 |
928 CPUID_EXT_TSC_DEADLINE_TIMER | CPUID_EXT_FMA | CPUID_EXT_MOVBE |
929 CPUID_EXT_PCID,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300930 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300931 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_NX |
Eduardo Habkost80ae4162012-11-22 13:31:03 -0200932 CPUID_EXT2_SYSCALL,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300933 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300934 CPUID_EXT3_LAHF_LM,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300935 .features[FEAT_7_0_EBX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300936 CPUID_7_0_EBX_FSGSBASE | CPUID_7_0_EBX_BMI1 |
Eduardo Habkost37507092012-11-14 16:28:54 -0200937 CPUID_7_0_EBX_HLE | CPUID_7_0_EBX_AVX2 | CPUID_7_0_EBX_SMEP |
938 CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_INVPCID |
939 CPUID_7_0_EBX_RTM,
940 .xlevel = 0x8000000A,
941 .model_id = "Intel Core Processor (Haswell)",
942 },
943 {
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300944 .name = "Opteron_G1",
945 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100946 .vendor = CPUID_VENDOR_AMD,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300947 .family = 15,
948 .model = 6,
949 .stepping = 1,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300950 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300951 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300952 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
953 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
954 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
955 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300956 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300957 CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300958 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300959 CPUID_EXT2_LM | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300960 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
961 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
962 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
963 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
964 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
965 .xlevel = 0x80000008,
966 .model_id = "AMD Opteron 240 (Gen 1 Class Opteron)",
967 },
968 {
969 .name = "Opteron_G2",
970 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100971 .vendor = CPUID_VENDOR_AMD,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300972 .family = 15,
973 .model = 6,
974 .stepping = 1,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300975 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300976 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300977 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
978 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
979 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
980 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300981 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300982 CPUID_EXT_CX16 | CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300983 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300984 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_FXSR |
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300985 CPUID_EXT2_MMX | CPUID_EXT2_NX | CPUID_EXT2_PSE36 |
986 CPUID_EXT2_PAT | CPUID_EXT2_CMOV | CPUID_EXT2_MCA |
987 CPUID_EXT2_PGE | CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL |
988 CPUID_EXT2_APIC | CPUID_EXT2_CX8 | CPUID_EXT2_MCE |
989 CPUID_EXT2_PAE | CPUID_EXT2_MSR | CPUID_EXT2_TSC | CPUID_EXT2_PSE |
990 CPUID_EXT2_DE | CPUID_EXT2_FPU,
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300991 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -0300992 CPUID_EXT3_SVM | CPUID_EXT3_LAHF_LM,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300993 .xlevel = 0x80000008,
994 .model_id = "AMD Opteron 22xx (Gen 2 Class Opteron)",
995 },
996 {
997 .name = "Opteron_G3",
998 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100999 .vendor = CPUID_VENDOR_AMD,
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001000 .family = 15,
1001 .model = 6,
1002 .stepping = 1,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001003 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001004 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001005 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
1006 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
1007 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
1008 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001009 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001010 CPUID_EXT_POPCNT | CPUID_EXT_CX16 | CPUID_EXT_MONITOR |
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001011 CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001012 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001013 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_FXSR |
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001014 CPUID_EXT2_MMX | CPUID_EXT2_NX | CPUID_EXT2_PSE36 |
1015 CPUID_EXT2_PAT | CPUID_EXT2_CMOV | CPUID_EXT2_MCA |
1016 CPUID_EXT2_PGE | CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL |
1017 CPUID_EXT2_APIC | CPUID_EXT2_CX8 | CPUID_EXT2_MCE |
1018 CPUID_EXT2_PAE | CPUID_EXT2_MSR | CPUID_EXT2_TSC | CPUID_EXT2_PSE |
1019 CPUID_EXT2_DE | CPUID_EXT2_FPU,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001020 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001021 CPUID_EXT3_MISALIGNSSE | CPUID_EXT3_SSE4A |
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001022 CPUID_EXT3_ABM | CPUID_EXT3_SVM | CPUID_EXT3_LAHF_LM,
1023 .xlevel = 0x80000008,
1024 .model_id = "AMD Opteron 23xx (Gen 3 Class Opteron)",
1025 },
1026 {
1027 .name = "Opteron_G4",
1028 .level = 0xd,
Igor Mammedov99b88a12013-01-21 15:06:36 +01001029 .vendor = CPUID_VENDOR_AMD,
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001030 .family = 21,
1031 .model = 1,
1032 .stepping = 2,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001033 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001034 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001035 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
1036 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
1037 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
1038 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001039 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001040 CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001041 CPUID_EXT_POPCNT | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
1042 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ |
1043 CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001044 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001045 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP |
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001046 CPUID_EXT2_PDPE1GB | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
1047 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
1048 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
1049 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
1050 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
1051 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001052 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001053 CPUID_EXT3_FMA4 | CPUID_EXT3_XOP |
Eduardo Habkost3eca4642012-09-05 17:41:10 -03001054 CPUID_EXT3_3DNOWPREFETCH | CPUID_EXT3_MISALIGNSSE |
1055 CPUID_EXT3_SSE4A | CPUID_EXT3_ABM | CPUID_EXT3_SVM |
1056 CPUID_EXT3_LAHF_LM,
1057 .xlevel = 0x8000001A,
1058 .model_id = "AMD Opteron 62xx class CPU",
1059 },
Andre Przywara021941b2012-11-14 16:28:53 -02001060 {
1061 .name = "Opteron_G5",
1062 .level = 0xd,
Igor Mammedov99b88a12013-01-21 15:06:36 +01001063 .vendor = CPUID_VENDOR_AMD,
Andre Przywara021941b2012-11-14 16:28:53 -02001064 .family = 21,
1065 .model = 2,
1066 .stepping = 0,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001067 .features[FEAT_1_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001068 CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
Andre Przywara021941b2012-11-14 16:28:53 -02001069 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
1070 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
1071 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
1072 CPUID_DE | CPUID_FP87,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001073 .features[FEAT_1_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001074 CPUID_EXT_F16C | CPUID_EXT_AVX | CPUID_EXT_XSAVE |
Andre Przywara021941b2012-11-14 16:28:53 -02001075 CPUID_EXT_AES | CPUID_EXT_POPCNT | CPUID_EXT_SSE42 |
1076 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_FMA |
1077 CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001078 .features[FEAT_8000_0001_EDX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001079 CPUID_EXT2_LM | CPUID_EXT2_RDTSCP |
Andre Przywara021941b2012-11-14 16:28:53 -02001080 CPUID_EXT2_PDPE1GB | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
1081 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
1082 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
1083 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
1084 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
1085 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001086 .features[FEAT_8000_0001_ECX] =
Eduardo Habkost27861ec2013-04-22 16:00:14 -03001087 CPUID_EXT3_TBM | CPUID_EXT3_FMA4 | CPUID_EXT3_XOP |
Andre Przywara021941b2012-11-14 16:28:53 -02001088 CPUID_EXT3_3DNOWPREFETCH | CPUID_EXT3_MISALIGNSSE |
1089 CPUID_EXT3_SSE4A | CPUID_EXT3_ABM | CPUID_EXT3_SVM |
1090 CPUID_EXT3_LAHF_LM,
1091 .xlevel = 0x8000001A,
1092 .model_id = "AMD Opteron 63xx class CPU",
1093 },
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001094};
1095
Eduardo Habkost0668af52013-04-25 15:43:00 -03001096/**
1097 * x86_cpu_compat_set_features:
1098 * @cpu_model: CPU model name to be changed. If NULL, all CPU models are changed
1099 * @w: Identifies the feature word to be changed.
1100 * @feat_add: Feature bits to be added to feature word
1101 * @feat_remove: Feature bits to be removed from feature word
1102 *
1103 * Change CPU model feature bits for compatibility.
1104 *
1105 * This function may be used by machine-type compatibility functions
1106 * to enable or disable feature bits on specific CPU models.
1107 */
1108void x86_cpu_compat_set_features(const char *cpu_model, FeatureWord w,
1109 uint32_t feat_add, uint32_t feat_remove)
1110{
Eduardo Habkost9576de72014-01-30 17:48:58 -02001111 X86CPUDefinition *def;
Eduardo Habkost0668af52013-04-25 15:43:00 -03001112 int i;
1113 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
1114 def = &builtin_x86_defs[i];
1115 if (!cpu_model || !strcmp(cpu_model, def->name)) {
1116 def->features[w] |= feat_add;
1117 def->features[w] &= ~feat_remove;
1118 }
1119 }
1120}
1121
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001122static int cpu_x86_fill_model_id(char *str)
1123{
1124 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
1125 int i;
1126
1127 for (i = 0; i < 3; i++) {
1128 host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx);
1129 memcpy(str + i * 16 + 0, &eax, 4);
1130 memcpy(str + i * 16 + 4, &ebx, 4);
1131 memcpy(str + i * 16 + 8, &ecx, 4);
1132 memcpy(str + i * 16 + 12, &edx, 4);
1133 }
1134 return 0;
1135}
1136
Eduardo Habkost9576de72014-01-30 17:48:58 -02001137/* Fill a X86CPUDefinition struct with information about the host CPU, and
Eduardo Habkost6e746f32012-10-24 19:44:06 -02001138 * the CPU features supported by the host hardware + host kernel
1139 *
1140 * This function may be called only if KVM is enabled.
1141 */
Eduardo Habkost9576de72014-01-30 17:48:58 -02001142static void kvm_cpu_fill_host(X86CPUDefinition *x86_cpu_def)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001143{
Eduardo Habkost12869992012-10-24 19:44:07 -02001144 KVMState *s = kvm_state;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001145 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
1146
Eduardo Habkost6e746f32012-10-24 19:44:06 -02001147 assert(kvm_enabled());
1148
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001149 x86_cpu_def->name = "host";
Benoît Canet787aaf52013-09-02 17:06:37 +02001150 x86_cpu_def->cache_info_passthrough = true;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001151 host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
Igor Mammedov99b88a12013-01-21 15:06:36 +01001152 x86_cpu_vendor_words2str(x86_cpu_def->vendor, ebx, edx, ecx);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001153
1154 host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
1155 x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
1156 x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
1157 x86_cpu_def->stepping = eax & 0x0F;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001158
Eduardo Habkost12869992012-10-24 19:44:07 -02001159 x86_cpu_def->level = kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX);
Eduardo Habkost2a573252014-01-20 14:41:12 -02001160 x86_cpu_def->xlevel = kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX);
1161 x86_cpu_def->xlevel2 =
1162 kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
1163
1164 cpu_x86_fill_model_id(x86_cpu_def->model_id);
1165
Eduardo Habkost2bc65d22014-01-20 14:41:13 -02001166 FeatureWord w;
1167 for (w = 0; w < FEATURE_WORDS; w++) {
1168 FeatureWordInfo *wi = &feature_word_info[w];
1169 x86_cpu_def->features[w] =
1170 kvm_arch_get_supported_cpuid(s, wi->cpuid_eax, wi->cpuid_ecx,
1171 wi->cpuid_reg);
1172 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001173}
1174
Eduardo Habkostbffd67b2013-01-07 16:20:46 -02001175static int unavailable_host_feature(FeatureWordInfo *f, uint32_t mask)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001176{
1177 int i;
1178
1179 for (i = 0; i < 32; ++i)
1180 if (1 << i & mask) {
Eduardo Habkostbffd67b2013-01-07 16:20:46 -02001181 const char *reg = get_register_name_32(f->cpuid_reg);
Eduardo Habkost8b4bedd2013-01-04 20:01:06 -02001182 assert(reg);
1183 fprintf(stderr, "warning: host doesn't support requested feature: "
1184 "CPUID.%02XH:%s%s%s [bit %d]\n",
Eduardo Habkostbffd67b2013-01-07 16:20:46 -02001185 f->cpuid_eax, reg,
1186 f->feat_names[i] ? "." : "",
1187 f->feat_names[i] ? f->feat_names[i] : "", i);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001188 break;
1189 }
1190 return 0;
1191}
1192
Eduardo Habkost07ca5942013-01-07 16:20:48 -02001193/* Check if all requested cpu flags are making their way to the guest
1194 *
1195 * Returns 0 if all flags are supported by the host, non-zero otherwise.
Eduardo Habkost6e746f32012-10-24 19:44:06 -02001196 *
1197 * This function may be called only if KVM is enabled.
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001198 */
Eduardo Habkostf0b9b112014-01-20 14:41:14 -02001199static int kvm_check_features_against_host(KVMState *s, X86CPU *cpu)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001200{
Igor Mammedov5ec01c22013-01-11 03:10:17 +01001201 CPUX86State *env = &cpu->env;
Eduardo Habkostf0b9b112014-01-20 14:41:14 -02001202 int rv = 0;
1203 FeatureWord w;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001204
Eduardo Habkost6e746f32012-10-24 19:44:06 -02001205 assert(kvm_enabled());
1206
Eduardo Habkostf0b9b112014-01-20 14:41:14 -02001207 for (w = 0; w < FEATURE_WORDS; w++) {
Eduardo Habkostbffd67b2013-01-07 16:20:46 -02001208 FeatureWordInfo *wi = &feature_word_info[w];
Eduardo Habkostf0b9b112014-01-20 14:41:14 -02001209 uint32_t guest_feat = env->features[w];
1210 uint32_t host_feat = kvm_arch_get_supported_cpuid(s, wi->cpuid_eax,
1211 wi->cpuid_ecx,
1212 wi->cpuid_reg);
1213 uint32_t mask;
Eduardo Habkostbffd67b2013-01-07 16:20:46 -02001214 for (mask = 1; mask; mask <<= 1) {
Eduardo Habkostf0b9b112014-01-20 14:41:14 -02001215 if (guest_feat & mask && !(host_feat & mask)) {
Eduardo Habkostbffd67b2013-01-07 16:20:46 -02001216 unavailable_host_feature(wi, mask);
1217 rv = 1;
1218 }
1219 }
1220 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001221 return rv;
1222}
1223
Andreas Färber95b85192012-04-17 14:42:22 +02001224static void x86_cpuid_version_get_family(Object *obj, Visitor *v, void *opaque,
1225 const char *name, Error **errp)
1226{
1227 X86CPU *cpu = X86_CPU(obj);
1228 CPUX86State *env = &cpu->env;
1229 int64_t value;
1230
1231 value = (env->cpuid_version >> 8) & 0xf;
1232 if (value == 0xf) {
1233 value += (env->cpuid_version >> 20) & 0xff;
1234 }
1235 visit_type_int(v, &value, name, errp);
1236}
1237
Andreas Färber71ad61d2012-04-17 12:10:29 +02001238static void x86_cpuid_version_set_family(Object *obj, Visitor *v, void *opaque,
1239 const char *name, Error **errp)
Andreas Färbered5e1ec2012-02-17 17:46:01 +01001240{
Andreas Färber71ad61d2012-04-17 12:10:29 +02001241 X86CPU *cpu = X86_CPU(obj);
1242 CPUX86State *env = &cpu->env;
1243 const int64_t min = 0;
1244 const int64_t max = 0xff + 0xf;
1245 int64_t value;
1246
1247 visit_type_int(v, &value, name, errp);
1248 if (error_is_set(errp)) {
1249 return;
1250 }
1251 if (value < min || value > max) {
1252 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1253 name ? name : "null", value, min, max);
1254 return;
1255 }
1256
Andreas Färbered5e1ec2012-02-17 17:46:01 +01001257 env->cpuid_version &= ~0xff00f00;
Andreas Färber71ad61d2012-04-17 12:10:29 +02001258 if (value > 0x0f) {
1259 env->cpuid_version |= 0xf00 | ((value - 0x0f) << 20);
Andreas Färbered5e1ec2012-02-17 17:46:01 +01001260 } else {
Andreas Färber71ad61d2012-04-17 12:10:29 +02001261 env->cpuid_version |= value << 8;
Andreas Färbered5e1ec2012-02-17 17:46:01 +01001262 }
1263}
1264
Andreas Färber67e30c82012-04-17 14:48:14 +02001265static void x86_cpuid_version_get_model(Object *obj, Visitor *v, void *opaque,
1266 const char *name, Error **errp)
1267{
1268 X86CPU *cpu = X86_CPU(obj);
1269 CPUX86State *env = &cpu->env;
1270 int64_t value;
1271
1272 value = (env->cpuid_version >> 4) & 0xf;
1273 value |= ((env->cpuid_version >> 16) & 0xf) << 4;
1274 visit_type_int(v, &value, name, errp);
1275}
1276
Andreas Färberc5291a42012-04-17 12:16:39 +02001277static void x86_cpuid_version_set_model(Object *obj, Visitor *v, void *opaque,
1278 const char *name, Error **errp)
Andreas Färberb0704cb2012-02-17 17:46:02 +01001279{
Andreas Färberc5291a42012-04-17 12:16:39 +02001280 X86CPU *cpu = X86_CPU(obj);
1281 CPUX86State *env = &cpu->env;
1282 const int64_t min = 0;
1283 const int64_t max = 0xff;
1284 int64_t value;
1285
1286 visit_type_int(v, &value, name, errp);
1287 if (error_is_set(errp)) {
1288 return;
1289 }
1290 if (value < min || value > max) {
1291 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1292 name ? name : "null", value, min, max);
1293 return;
1294 }
1295
Andreas Färberb0704cb2012-02-17 17:46:02 +01001296 env->cpuid_version &= ~0xf00f0;
Andreas Färberc5291a42012-04-17 12:16:39 +02001297 env->cpuid_version |= ((value & 0xf) << 4) | ((value >> 4) << 16);
Andreas Färberb0704cb2012-02-17 17:46:02 +01001298}
1299
Andreas Färber35112e42012-04-17 14:50:53 +02001300static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v,
1301 void *opaque, const char *name,
1302 Error **errp)
1303{
1304 X86CPU *cpu = X86_CPU(obj);
1305 CPUX86State *env = &cpu->env;
1306 int64_t value;
1307
1308 value = env->cpuid_version & 0xf;
1309 visit_type_int(v, &value, name, errp);
1310}
1311
Andreas Färber036e2222012-04-17 14:14:18 +02001312static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
1313 void *opaque, const char *name,
1314 Error **errp)
Andreas Färber38c3dc42012-02-17 17:46:03 +01001315{
Andreas Färber036e2222012-04-17 14:14:18 +02001316 X86CPU *cpu = X86_CPU(obj);
1317 CPUX86State *env = &cpu->env;
1318 const int64_t min = 0;
1319 const int64_t max = 0xf;
1320 int64_t value;
1321
1322 visit_type_int(v, &value, name, errp);
1323 if (error_is_set(errp)) {
1324 return;
1325 }
1326 if (value < min || value > max) {
1327 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1328 name ? name : "null", value, min, max);
1329 return;
1330 }
1331
Andreas Färber38c3dc42012-02-17 17:46:03 +01001332 env->cpuid_version &= ~0xf;
Andreas Färber036e2222012-04-17 14:14:18 +02001333 env->cpuid_version |= value & 0xf;
Andreas Färber38c3dc42012-02-17 17:46:03 +01001334}
1335
Andreas Färber8e1898b2012-04-17 18:41:40 +02001336static void x86_cpuid_get_level(Object *obj, Visitor *v, void *opaque,
1337 const char *name, Error **errp)
1338{
1339 X86CPU *cpu = X86_CPU(obj);
Andreas Färber8e1898b2012-04-17 18:41:40 +02001340
Andreas Färberfa029882012-05-01 23:33:13 +02001341 visit_type_uint32(v, &cpu->env.cpuid_level, name, errp);
Andreas Färber8e1898b2012-04-17 18:41:40 +02001342}
1343
1344static void x86_cpuid_set_level(Object *obj, Visitor *v, void *opaque,
1345 const char *name, Error **errp)
1346{
1347 X86CPU *cpu = X86_CPU(obj);
Andreas Färber8e1898b2012-04-17 18:41:40 +02001348
Andreas Färberfa029882012-05-01 23:33:13 +02001349 visit_type_uint32(v, &cpu->env.cpuid_level, name, errp);
Andreas Färber8e1898b2012-04-17 18:41:40 +02001350}
1351
Andreas Färber16b93aa2012-04-17 18:44:07 +02001352static void x86_cpuid_get_xlevel(Object *obj, Visitor *v, void *opaque,
1353 const char *name, Error **errp)
1354{
1355 X86CPU *cpu = X86_CPU(obj);
Andreas Färber16b93aa2012-04-17 18:44:07 +02001356
Andreas Färberfa029882012-05-01 23:33:13 +02001357 visit_type_uint32(v, &cpu->env.cpuid_xlevel, name, errp);
Andreas Färber16b93aa2012-04-17 18:44:07 +02001358}
1359
1360static void x86_cpuid_set_xlevel(Object *obj, Visitor *v, void *opaque,
1361 const char *name, Error **errp)
1362{
1363 X86CPU *cpu = X86_CPU(obj);
Andreas Färber16b93aa2012-04-17 18:44:07 +02001364
Andreas Färberfa029882012-05-01 23:33:13 +02001365 visit_type_uint32(v, &cpu->env.cpuid_xlevel, name, errp);
Andreas Färber16b93aa2012-04-17 18:44:07 +02001366}
1367
Andreas Färberd480e1a2012-04-17 19:22:58 +02001368static char *x86_cpuid_get_vendor(Object *obj, Error **errp)
1369{
1370 X86CPU *cpu = X86_CPU(obj);
1371 CPUX86State *env = &cpu->env;
1372 char *value;
Andreas Färberd480e1a2012-04-17 19:22:58 +02001373
Igor Mammedov9df694e2012-10-22 17:03:10 +02001374 value = (char *)g_malloc(CPUID_VENDOR_SZ + 1);
Igor Mammedov99b88a12013-01-21 15:06:36 +01001375 x86_cpu_vendor_words2str(value, env->cpuid_vendor1, env->cpuid_vendor2,
1376 env->cpuid_vendor3);
Andreas Färberd480e1a2012-04-17 19:22:58 +02001377 return value;
1378}
1379
1380static void x86_cpuid_set_vendor(Object *obj, const char *value,
1381 Error **errp)
1382{
1383 X86CPU *cpu = X86_CPU(obj);
1384 CPUX86State *env = &cpu->env;
1385 int i;
1386
Igor Mammedov9df694e2012-10-22 17:03:10 +02001387 if (strlen(value) != CPUID_VENDOR_SZ) {
Andreas Färberd480e1a2012-04-17 19:22:58 +02001388 error_set(errp, QERR_PROPERTY_VALUE_BAD, "",
1389 "vendor", value);
1390 return;
1391 }
1392
1393 env->cpuid_vendor1 = 0;
1394 env->cpuid_vendor2 = 0;
1395 env->cpuid_vendor3 = 0;
1396 for (i = 0; i < 4; i++) {
1397 env->cpuid_vendor1 |= ((uint8_t)value[i ]) << (8 * i);
1398 env->cpuid_vendor2 |= ((uint8_t)value[i + 4]) << (8 * i);
1399 env->cpuid_vendor3 |= ((uint8_t)value[i + 8]) << (8 * i);
1400 }
Andreas Färberd480e1a2012-04-17 19:22:58 +02001401}
1402
Andreas Färber63e886e2012-04-17 23:02:26 +02001403static char *x86_cpuid_get_model_id(Object *obj, Error **errp)
1404{
1405 X86CPU *cpu = X86_CPU(obj);
1406 CPUX86State *env = &cpu->env;
1407 char *value;
1408 int i;
1409
1410 value = g_malloc(48 + 1);
1411 for (i = 0; i < 48; i++) {
1412 value[i] = env->cpuid_model[i >> 2] >> (8 * (i & 3));
1413 }
1414 value[48] = '\0';
1415 return value;
1416}
1417
Andreas Färber938d4c22012-04-17 15:17:27 +02001418static void x86_cpuid_set_model_id(Object *obj, const char *model_id,
1419 Error **errp)
Andreas Färberdcce6672012-02-17 17:46:04 +01001420{
Andreas Färber938d4c22012-04-17 15:17:27 +02001421 X86CPU *cpu = X86_CPU(obj);
1422 CPUX86State *env = &cpu->env;
Andreas Färberdcce6672012-02-17 17:46:04 +01001423 int c, len, i;
1424
1425 if (model_id == NULL) {
1426 model_id = "";
1427 }
1428 len = strlen(model_id);
Andreas Färberd0a6acf2012-04-17 18:21:52 +02001429 memset(env->cpuid_model, 0, 48);
Andreas Färberdcce6672012-02-17 17:46:04 +01001430 for (i = 0; i < 48; i++) {
1431 if (i >= len) {
1432 c = '\0';
1433 } else {
1434 c = (uint8_t)model_id[i];
1435 }
1436 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
1437 }
1438}
1439
Andreas Färber89e48962012-04-18 00:12:23 +02001440static void x86_cpuid_get_tsc_freq(Object *obj, Visitor *v, void *opaque,
1441 const char *name, Error **errp)
1442{
1443 X86CPU *cpu = X86_CPU(obj);
1444 int64_t value;
1445
1446 value = cpu->env.tsc_khz * 1000;
1447 visit_type_int(v, &value, name, errp);
1448}
1449
1450static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,
1451 const char *name, Error **errp)
1452{
1453 X86CPU *cpu = X86_CPU(obj);
1454 const int64_t min = 0;
Don Slutz2e848492012-09-21 20:13:13 -04001455 const int64_t max = INT64_MAX;
Andreas Färber89e48962012-04-18 00:12:23 +02001456 int64_t value;
1457
1458 visit_type_int(v, &value, name, errp);
1459 if (error_is_set(errp)) {
1460 return;
1461 }
1462 if (value < min || value > max) {
1463 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1464 name ? name : "null", value, min, max);
1465 return;
1466 }
1467
1468 cpu->env.tsc_khz = value / 1000;
1469}
1470
Igor Mammedov31050932013-04-25 16:05:26 +02001471static void x86_cpuid_get_apic_id(Object *obj, Visitor *v, void *opaque,
1472 const char *name, Error **errp)
1473{
1474 X86CPU *cpu = X86_CPU(obj);
1475 int64_t value = cpu->env.cpuid_apic_id;
1476
1477 visit_type_int(v, &value, name, errp);
1478}
1479
1480static void x86_cpuid_set_apic_id(Object *obj, Visitor *v, void *opaque,
1481 const char *name, Error **errp)
1482{
1483 X86CPU *cpu = X86_CPU(obj);
Igor Mammedov8d6d4982013-04-26 19:51:52 +02001484 DeviceState *dev = DEVICE(obj);
Igor Mammedov31050932013-04-25 16:05:26 +02001485 const int64_t min = 0;
1486 const int64_t max = UINT32_MAX;
1487 Error *error = NULL;
1488 int64_t value;
1489
Igor Mammedov8d6d4982013-04-26 19:51:52 +02001490 if (dev->realized) {
1491 error_setg(errp, "Attempt to set property '%s' on '%s' after "
1492 "it was realized", name, object_get_typename(obj));
1493 return;
1494 }
1495
Igor Mammedov31050932013-04-25 16:05:26 +02001496 visit_type_int(v, &value, name, &error);
1497 if (error) {
1498 error_propagate(errp, error);
1499 return;
1500 }
1501 if (value < min || value > max) {
1502 error_setg(errp, "Property %s.%s doesn't take value %" PRId64
1503 " (minimum: %" PRId64 ", maximum: %" PRId64 ")" ,
1504 object_get_typename(obj), name, value, min, max);
1505 return;
1506 }
1507
1508 if ((value != cpu->env.cpuid_apic_id) && cpu_exists(value)) {
1509 error_setg(errp, "CPU with APIC ID %" PRIi64 " exists", value);
1510 return;
1511 }
1512 cpu->env.cpuid_apic_id = value;
1513}
1514
Eduardo Habkost7e5292b2013-05-06 13:20:09 -03001515/* Generic getter for "feature-words" and "filtered-features" properties */
Eduardo Habkost8e8aba52013-05-06 13:20:07 -03001516static void x86_cpu_get_feature_words(Object *obj, Visitor *v, void *opaque,
1517 const char *name, Error **errp)
1518{
Eduardo Habkost7e5292b2013-05-06 13:20:09 -03001519 uint32_t *array = (uint32_t *)opaque;
Eduardo Habkost8e8aba52013-05-06 13:20:07 -03001520 FeatureWord w;
1521 Error *err = NULL;
1522 X86CPUFeatureWordInfo word_infos[FEATURE_WORDS] = { };
1523 X86CPUFeatureWordInfoList list_entries[FEATURE_WORDS] = { };
1524 X86CPUFeatureWordInfoList *list = NULL;
1525
1526 for (w = 0; w < FEATURE_WORDS; w++) {
1527 FeatureWordInfo *wi = &feature_word_info[w];
1528 X86CPUFeatureWordInfo *qwi = &word_infos[w];
1529 qwi->cpuid_input_eax = wi->cpuid_eax;
1530 qwi->has_cpuid_input_ecx = wi->cpuid_needs_ecx;
1531 qwi->cpuid_input_ecx = wi->cpuid_ecx;
1532 qwi->cpuid_register = x86_reg_info_32[wi->cpuid_reg].qapi_enum;
Eduardo Habkost7e5292b2013-05-06 13:20:09 -03001533 qwi->features = array[w];
Eduardo Habkost8e8aba52013-05-06 13:20:07 -03001534
1535 /* List will be in reverse order, but order shouldn't matter */
1536 list_entries[w].next = list;
1537 list_entries[w].value = &word_infos[w];
1538 list = &list_entries[w];
1539 }
1540
1541 visit_type_X86CPUFeatureWordInfoList(v, &list, "feature-words", &err);
1542 error_propagate(errp, err);
1543}
1544
Igor Mammedovc8f0f882013-06-04 15:05:25 +02001545static void x86_get_hv_spinlocks(Object *obj, Visitor *v, void *opaque,
1546 const char *name, Error **errp)
1547{
1548 X86CPU *cpu = X86_CPU(obj);
1549 int64_t value = cpu->hyperv_spinlock_attempts;
1550
1551 visit_type_int(v, &value, name, errp);
1552}
1553
1554static void x86_set_hv_spinlocks(Object *obj, Visitor *v, void *opaque,
1555 const char *name, Error **errp)
1556{
1557 const int64_t min = 0xFFF;
1558 const int64_t max = UINT_MAX;
1559 X86CPU *cpu = X86_CPU(obj);
1560 Error *err = NULL;
1561 int64_t value;
1562
1563 visit_type_int(v, &value, name, &err);
1564 if (err) {
1565 error_propagate(errp, err);
1566 return;
1567 }
1568
1569 if (value < min || value > max) {
1570 error_setg(errp, "Property %s.%s doesn't take value %" PRId64
1571 " (minimum: %" PRId64 ", maximum: %" PRId64 ")",
1572 object_get_typename(obj), name ? name : "null",
1573 value, min, max);
1574 return;
1575 }
1576 cpu->hyperv_spinlock_attempts = value;
1577}
1578
1579static PropertyInfo qdev_prop_spinlocks = {
1580 .name = "int",
1581 .get = x86_get_hv_spinlocks,
1582 .set = x86_set_hv_spinlocks,
1583};
1584
Eduardo Habkost9576de72014-01-30 17:48:58 -02001585static int cpu_x86_find_by_name(X86CPU *cpu, X86CPUDefinition *x86_cpu_def,
Eduardo Habkostc1399112013-07-26 17:09:35 -03001586 const char *name)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001587{
Eduardo Habkost9576de72014-01-30 17:48:58 -02001588 X86CPUDefinition *def;
Andreas Färber7fc9b712013-01-21 01:02:28 +01001589 int i;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001590
Andreas Färber4bfe9102013-01-21 01:00:24 +01001591 if (name == NULL) {
Eduardo Habkost8f961352012-12-04 17:34:39 -02001592 return -1;
Andreas Färber4bfe9102013-01-21 01:00:24 +01001593 }
1594 if (kvm_enabled() && strcmp(name, "host") == 0) {
1595 kvm_cpu_fill_host(x86_cpu_def);
Peter Crosthwaite00b81052014-01-01 18:48:43 -08001596 object_property_set_bool(OBJECT(cpu), true, "pmu", &error_abort);
Andreas Färber4bfe9102013-01-21 01:00:24 +01001597 return 0;
Eduardo Habkost8f961352012-12-04 17:34:39 -02001598 }
1599
Andreas Färber7fc9b712013-01-21 01:02:28 +01001600 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
1601 def = &builtin_x86_defs[i];
Andreas Färber4bfe9102013-01-21 01:00:24 +01001602 if (strcmp(name, def->name) == 0) {
1603 memcpy(x86_cpu_def, def, sizeof(*def));
1604 return 0;
1605 }
1606 }
1607
1608 return -1;
Eduardo Habkost8f961352012-12-04 17:34:39 -02001609}
1610
Igor Mammedov72ac2e82013-04-26 18:04:32 +02001611/* Convert all '_' in a feature string option name to '-', to make feature
1612 * name conform to QOM property naming rule, which uses '-' instead of '_'.
1613 */
1614static inline void feat2prop(char *s)
1615{
1616 while ((s = strchr(s, '_'))) {
1617 *s = '-';
1618 }
1619}
1620
Eduardo Habkost8f961352012-12-04 17:34:39 -02001621/* Parse "+feature,-feature,feature=foo" CPU feature string
1622 */
Igor Mammedova91987c2013-01-21 15:06:38 +01001623static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
Eduardo Habkost8f961352012-12-04 17:34:39 -02001624{
Eduardo Habkost8f961352012-12-04 17:34:39 -02001625 char *featurestr; /* Single 'key=value" string being parsed */
Eduardo Habkost9f3fb562012-12-04 17:34:38 -02001626 /* Features to be added */
Igor Mammedov077c68c2013-01-11 03:10:15 +01001627 FeatureWordArray plus_features = { 0 };
Joerg Roedel296acb62010-09-27 15:16:17 +02001628 /* Features to be removed */
Eduardo Habkost5ef57872013-01-07 16:20:45 -02001629 FeatureWordArray minus_features = { 0 };
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001630 uint32_t numvalue;
Igor Mammedova91987c2013-01-21 15:06:38 +01001631 CPUX86State *env = &cpu->env;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001632
Eduardo Habkost8f961352012-12-04 17:34:39 -02001633 featurestr = features ? strtok(features, ",") : NULL;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001634
1635 while (featurestr) {
1636 char *val;
1637 if (featurestr[0] == '+') {
Eduardo Habkost5ef57872013-01-07 16:20:45 -02001638 add_flagname_to_bitmaps(featurestr + 1, plus_features);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001639 } else if (featurestr[0] == '-') {
Eduardo Habkost5ef57872013-01-07 16:20:45 -02001640 add_flagname_to_bitmaps(featurestr + 1, minus_features);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001641 } else if ((val = strchr(featurestr, '='))) {
1642 *val = 0; val++;
Igor Mammedov72ac2e82013-04-26 18:04:32 +02001643 feat2prop(featurestr);
Igor Mammedovd024d202012-12-14 01:29:56 +01001644 if (!strcmp(featurestr, "xlevel")) {
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001645 char *err;
Igor Mammedova91987c2013-01-21 15:06:38 +01001646 char num[32];
1647
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001648 numvalue = strtoul(val, &err, 0);
1649 if (!*val || *err) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001650 error_setg(errp, "bad numerical value %s", val);
Igor Mammedova91987c2013-01-21 15:06:38 +01001651 goto out;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001652 }
1653 if (numvalue < 0x80000000) {
Igor Mammedov8ba8a692013-01-21 15:06:35 +01001654 fprintf(stderr, "xlevel value shall always be >= 0x80000000"
1655 ", fixup will be removed in future versions\n");
Aurelien Jarno2f7a21c2010-03-13 16:46:33 +01001656 numvalue += 0x80000000;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001657 }
Igor Mammedova91987c2013-01-21 15:06:38 +01001658 snprintf(num, sizeof(num), "%" PRIu32, numvalue);
1659 object_property_parse(OBJECT(cpu), num, featurestr, errp);
Igor Mammedov72ac2e82013-04-26 18:04:32 +02001660 } else if (!strcmp(featurestr, "tsc-freq")) {
Joerg Roedelb862d1f2011-07-07 16:13:12 +02001661 int64_t tsc_freq;
1662 char *err;
Igor Mammedova91987c2013-01-21 15:06:38 +01001663 char num[32];
Joerg Roedelb862d1f2011-07-07 16:13:12 +02001664
1665 tsc_freq = strtosz_suffix_unit(val, &err,
1666 STRTOSZ_DEFSUFFIX_B, 1000);
Markus Armbruster45009a32011-11-22 09:46:04 +01001667 if (tsc_freq < 0 || *err) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001668 error_setg(errp, "bad numerical value %s", val);
Igor Mammedova91987c2013-01-21 15:06:38 +01001669 goto out;
Joerg Roedelb862d1f2011-07-07 16:13:12 +02001670 }
Igor Mammedova91987c2013-01-21 15:06:38 +01001671 snprintf(num, sizeof(num), "%" PRId64, tsc_freq);
1672 object_property_parse(OBJECT(cpu), num, "tsc-frequency", errp);
Igor Mammedov72ac2e82013-04-26 18:04:32 +02001673 } else if (!strcmp(featurestr, "hv-spinlocks")) {
Vadim Rozenfeld28f52cc2011-12-18 22:48:13 +02001674 char *err;
Igor Mammedov92067bf2013-06-05 15:18:40 +02001675 const int min = 0xFFF;
Igor Mammedovc8f0f882013-06-04 15:05:25 +02001676 char num[32];
Vadim Rozenfeld28f52cc2011-12-18 22:48:13 +02001677 numvalue = strtoul(val, &err, 0);
1678 if (!*val || *err) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001679 error_setg(errp, "bad numerical value %s", val);
Igor Mammedova91987c2013-01-21 15:06:38 +01001680 goto out;
Vadim Rozenfeld28f52cc2011-12-18 22:48:13 +02001681 }
Igor Mammedov92067bf2013-06-05 15:18:40 +02001682 if (numvalue < min) {
1683 fprintf(stderr, "hv-spinlocks value shall always be >= 0x%x"
1684 ", fixup will be removed in future versions\n",
1685 min);
1686 numvalue = min;
1687 }
Igor Mammedovc8f0f882013-06-04 15:05:25 +02001688 snprintf(num, sizeof(num), "%" PRId32, numvalue);
1689 object_property_parse(OBJECT(cpu), num, featurestr, errp);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001690 } else {
Igor Mammedovd024d202012-12-14 01:29:56 +01001691 object_property_parse(OBJECT(cpu), val, featurestr, errp);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001692 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001693 } else {
Igor Mammedov258f5ab2012-12-14 01:16:00 +01001694 feat2prop(featurestr);
1695 object_property_parse(OBJECT(cpu), "on", featurestr, errp);
Igor Mammedova91987c2013-01-21 15:06:38 +01001696 }
1697 if (error_is_set(errp)) {
1698 goto out;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001699 }
1700 featurestr = strtok(NULL, ",");
1701 }
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001702 env->features[FEAT_1_EDX] |= plus_features[FEAT_1_EDX];
1703 env->features[FEAT_1_ECX] |= plus_features[FEAT_1_ECX];
1704 env->features[FEAT_8000_0001_EDX] |= plus_features[FEAT_8000_0001_EDX];
1705 env->features[FEAT_8000_0001_ECX] |= plus_features[FEAT_8000_0001_ECX];
1706 env->features[FEAT_C000_0001_EDX] |= plus_features[FEAT_C000_0001_EDX];
1707 env->features[FEAT_KVM] |= plus_features[FEAT_KVM];
1708 env->features[FEAT_SVM] |= plus_features[FEAT_SVM];
1709 env->features[FEAT_7_0_EBX] |= plus_features[FEAT_7_0_EBX];
1710 env->features[FEAT_1_EDX] &= ~minus_features[FEAT_1_EDX];
1711 env->features[FEAT_1_ECX] &= ~minus_features[FEAT_1_ECX];
1712 env->features[FEAT_8000_0001_EDX] &= ~minus_features[FEAT_8000_0001_EDX];
1713 env->features[FEAT_8000_0001_ECX] &= ~minus_features[FEAT_8000_0001_ECX];
1714 env->features[FEAT_C000_0001_EDX] &= ~minus_features[FEAT_C000_0001_EDX];
1715 env->features[FEAT_KVM] &= ~minus_features[FEAT_KVM];
1716 env->features[FEAT_SVM] &= ~minus_features[FEAT_SVM];
1717 env->features[FEAT_7_0_EBX] &= ~minus_features[FEAT_7_0_EBX];
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001718
Igor Mammedova91987c2013-01-21 15:06:38 +01001719out:
1720 return;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001721}
1722
1723/* generate a composite string into buf of all cpuid names in featureset
1724 * selected by fbits. indicate truncation at bufsize in the event of overflow.
1725 * if flags, suppress names undefined in featureset.
1726 */
1727static void listflags(char *buf, int bufsize, uint32_t fbits,
1728 const char **featureset, uint32_t flags)
1729{
1730 const char **p = &featureset[31];
1731 char *q, *b, bit;
1732 int nc;
1733
1734 b = 4 <= bufsize ? buf + (bufsize -= 3) - 1 : NULL;
1735 *buf = '\0';
1736 for (q = buf, bit = 31; fbits && bufsize; --p, fbits &= ~(1 << bit), --bit)
1737 if (fbits & 1 << bit && (*p || !flags)) {
1738 if (*p)
1739 nc = snprintf(q, bufsize, "%s%s", q == buf ? "" : " ", *p);
1740 else
1741 nc = snprintf(q, bufsize, "%s[%d]", q == buf ? "" : " ", bit);
1742 if (bufsize <= nc) {
1743 if (b) {
1744 memcpy(b, "...", sizeof("..."));
1745 }
1746 return;
1747 }
1748 q += nc;
1749 bufsize -= nc;
1750 }
1751}
1752
Peter Maydelle916cbf2012-09-05 17:41:08 -03001753/* generate CPU information. */
1754void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001755{
Eduardo Habkost9576de72014-01-30 17:48:58 -02001756 X86CPUDefinition *def;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001757 char buf[256];
Andreas Färber7fc9b712013-01-21 01:02:28 +01001758 int i;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001759
Andreas Färber7fc9b712013-01-21 01:02:28 +01001760 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
1761 def = &builtin_x86_defs[i];
Eduardo Habkostc04321b2012-09-05 17:41:13 -03001762 snprintf(buf, sizeof(buf), "%s", def->name);
Peter Maydell6cdf8852012-09-05 17:41:07 -03001763 (*cpu_fprintf)(f, "x86 %16s %-48s\n", buf, def->model_id);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001764 }
Jan Kiszka21ad7782013-03-24 17:01:02 +01001765#ifdef CONFIG_KVM
1766 (*cpu_fprintf)(f, "x86 %16s %-48s\n", "host",
1767 "KVM processor with all supported host features "
1768 "(only available in KVM mode)");
1769#endif
1770
Peter Maydell6cdf8852012-09-05 17:41:07 -03001771 (*cpu_fprintf)(f, "\nRecognized CPUID flags:\n");
Jan Kiszka3af60be2013-02-27 10:15:51 +01001772 for (i = 0; i < ARRAY_SIZE(feature_word_info); i++) {
1773 FeatureWordInfo *fw = &feature_word_info[i];
1774
1775 listflags(buf, sizeof(buf), (uint32_t)~0, fw->feat_names, 1);
1776 (*cpu_fprintf)(f, " %s\n", buf);
1777 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001778}
1779
Anthony Liguori76b64a72012-08-14 22:17:36 -05001780CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
Anthony Liguorie3966122012-08-10 11:04:14 -05001781{
1782 CpuDefinitionInfoList *cpu_list = NULL;
Eduardo Habkost9576de72014-01-30 17:48:58 -02001783 X86CPUDefinition *def;
Andreas Färber7fc9b712013-01-21 01:02:28 +01001784 int i;
Anthony Liguorie3966122012-08-10 11:04:14 -05001785
Andreas Färber7fc9b712013-01-21 01:02:28 +01001786 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
Anthony Liguorie3966122012-08-10 11:04:14 -05001787 CpuDefinitionInfoList *entry;
1788 CpuDefinitionInfo *info;
1789
Andreas Färber7fc9b712013-01-21 01:02:28 +01001790 def = &builtin_x86_defs[i];
Anthony Liguorie3966122012-08-10 11:04:14 -05001791 info = g_malloc0(sizeof(*info));
1792 info->name = g_strdup(def->name);
1793
1794 entry = g_malloc0(sizeof(*entry));
1795 entry->value = info;
1796 entry->next = cpu_list;
1797 cpu_list = entry;
1798 }
1799
1800 return cpu_list;
1801}
1802
Eduardo Habkostbc74b7d2012-10-04 17:49:05 -03001803static void filter_features_for_kvm(X86CPU *cpu)
1804{
1805 CPUX86State *env = &cpu->env;
1806 KVMState *s = kvm_state;
Eduardo Habkostbd87d2a2013-04-22 16:00:18 -03001807 FeatureWord w;
Eduardo Habkostbc74b7d2012-10-04 17:49:05 -03001808
Eduardo Habkostbd87d2a2013-04-22 16:00:18 -03001809 for (w = 0; w < FEATURE_WORDS; w++) {
1810 FeatureWordInfo *wi = &feature_word_info[w];
Eduardo Habkost034acf42013-05-06 13:20:08 -03001811 uint32_t host_feat = kvm_arch_get_supported_cpuid(s, wi->cpuid_eax,
1812 wi->cpuid_ecx,
1813 wi->cpuid_reg);
1814 uint32_t requested_features = env->features[w];
1815 env->features[w] &= host_feat;
1816 cpu->filtered_features[w] = requested_features & ~env->features[w];
Eduardo Habkostbd87d2a2013-04-22 16:00:18 -03001817 }
Eduardo Habkostbc74b7d2012-10-04 17:49:05 -03001818}
Eduardo Habkostbc74b7d2012-10-04 17:49:05 -03001819
Eduardo Habkostc080e302014-01-30 17:48:56 -02001820/* Load CPU definition for a given CPU model name
1821 */
1822static void x86_cpu_load_def(X86CPU *cpu, const char *name, Error **errp)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001823{
Andreas Färber61dcd772012-04-17 12:00:51 +02001824 CPUX86State *env = &cpu->env;
Eduardo Habkost9576de72014-01-30 17:48:58 -02001825 X86CPUDefinition def1, *def = &def1;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001826
Joerg Roedeldb0ad1b2010-09-27 15:16:16 +02001827 memset(def, 0, sizeof(*def));
1828
Eduardo Habkostc1399112013-07-26 17:09:35 -03001829 if (cpu_x86_find_by_name(cpu, def, name) < 0) {
Andreas Färber2d642552013-02-15 14:06:56 +01001830 error_setg(errp, "Unable to find CPU definition: %s", name);
1831 return;
1832 }
1833
Andreas Färber2d642552013-02-15 14:06:56 +01001834 object_property_set_int(OBJECT(cpu), def->level, "level", errp);
1835 object_property_set_int(OBJECT(cpu), def->family, "family", errp);
1836 object_property_set_int(OBJECT(cpu), def->model, "model", errp);
1837 object_property_set_int(OBJECT(cpu), def->stepping, "stepping", errp);
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001838 env->features[FEAT_1_EDX] = def->features[FEAT_1_EDX];
1839 env->features[FEAT_1_ECX] = def->features[FEAT_1_ECX];
1840 env->features[FEAT_8000_0001_EDX] = def->features[FEAT_8000_0001_EDX];
1841 env->features[FEAT_8000_0001_ECX] = def->features[FEAT_8000_0001_ECX];
Andreas Färber2d642552013-02-15 14:06:56 +01001842 object_property_set_int(OBJECT(cpu), def->xlevel, "xlevel", errp);
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001843 env->features[FEAT_KVM] = def->features[FEAT_KVM];
1844 env->features[FEAT_SVM] = def->features[FEAT_SVM];
1845 env->features[FEAT_C000_0001_EDX] = def->features[FEAT_C000_0001_EDX];
1846 env->features[FEAT_7_0_EBX] = def->features[FEAT_7_0_EBX];
Andreas Färber2d642552013-02-15 14:06:56 +01001847 env->cpuid_xlevel2 = def->xlevel2;
Benoît Canet787aaf52013-09-02 17:06:37 +02001848 cpu->cache_info_passthrough = def->cache_info_passthrough;
Andreas Färber2d642552013-02-15 14:06:56 +01001849
1850 object_property_set_str(OBJECT(cpu), def->model_id, "model-id", errp);
Eduardo Habkost82beb532014-01-30 17:48:54 -02001851
Eduardo Habkost9576de72014-01-30 17:48:58 -02001852 /* Special cases not set in the X86CPUDefinition structs: */
Eduardo Habkost82beb532014-01-30 17:48:54 -02001853 if (kvm_enabled()) {
1854 env->features[FEAT_KVM] |= kvm_default_features;
1855 }
1856 env->features[FEAT_1_ECX] |= CPUID_EXT_HYPERVISOR;
Eduardo Habkost7c08db32014-01-30 17:48:55 -02001857
1858 /* sysenter isn't supported in compatibility mode on AMD,
1859 * syscall isn't supported in compatibility mode on Intel.
1860 * Normally we advertise the actual CPU vendor, but you can
1861 * override this using the 'vendor' property if you want to use
1862 * KVM's sysenter/syscall emulation in compatibility mode and
1863 * when doing cross vendor migration
1864 */
1865 const char *vendor = def->vendor;
1866 char host_vendor[CPUID_VENDOR_SZ + 1];
1867 if (kvm_enabled()) {
1868 uint32_t ebx = 0, ecx = 0, edx = 0;
1869 host_cpuid(0, 0, NULL, &ebx, &ecx, &edx);
1870 x86_cpu_vendor_words2str(host_vendor, ebx, edx, ecx);
1871 vendor = host_vendor;
1872 }
1873
1874 object_property_set_str(OBJECT(cpu), vendor, "vendor", errp);
1875
Andreas Färber2d642552013-02-15 14:06:56 +01001876}
1877
Igor Mammedov62fc4032013-04-29 18:54:13 +02001878X86CPU *cpu_x86_create(const char *cpu_model, DeviceState *icc_bridge,
1879 Error **errp)
Andreas Färber2d642552013-02-15 14:06:56 +01001880{
1881 X86CPU *cpu = NULL;
Andreas Färber2d642552013-02-15 14:06:56 +01001882 gchar **model_pieces;
1883 char *name, *features;
Andreas Färberba2bc7a2013-05-01 17:05:47 +02001884 char *typename;
Andreas Färber2d642552013-02-15 14:06:56 +01001885 Error *error = NULL;
1886
Eduardo Habkost8f961352012-12-04 17:34:39 -02001887 model_pieces = g_strsplit(cpu_model, ",", 2);
1888 if (!model_pieces[0]) {
Igor Mammedovfa2db3c2013-01-11 03:10:16 +01001889 error_setg(&error, "Invalid/empty CPU model name");
1890 goto out;
Eduardo Habkost8f961352012-12-04 17:34:39 -02001891 }
1892 name = model_pieces[0];
1893 features = model_pieces[1];
1894
Andreas Färber2d642552013-02-15 14:06:56 +01001895 cpu = X86_CPU(object_new(TYPE_X86_CPU));
Eduardo Habkost285f0252014-01-30 17:48:57 -02001896 x86_cpu_load_def(cpu, name, &error);
1897 if (error) {
1898 goto out;
1899 }
1900
Igor Mammedov62fc4032013-04-29 18:54:13 +02001901#ifndef CONFIG_USER_ONLY
1902 if (icc_bridge == NULL) {
1903 error_setg(&error, "Invalid icc-bridge value");
1904 goto out;
1905 }
1906 qdev_set_parent_bus(DEVICE(cpu), qdev_get_child_bus(icc_bridge, "icc"));
1907 object_unref(OBJECT(cpu));
1908#endif
Eduardo Habkost8f961352012-12-04 17:34:39 -02001909
Andreas Färberba2bc7a2013-05-01 17:05:47 +02001910 /* Emulate per-model subclasses for global properties */
1911 typename = g_strdup_printf("%s-" TYPE_X86_CPU, name);
1912 qdev_prop_set_globals_for_type(DEVICE(cpu), typename, &error);
1913 g_free(typename);
1914 if (error) {
1915 goto out;
1916 }
1917
Igor Mammedova91987c2013-01-21 15:06:38 +01001918 cpu_x86_parse_featurestr(cpu, features, &error);
Andreas Färber2d642552013-02-15 14:06:56 +01001919 if (error) {
1920 goto out;
1921 }
1922
Igor Mammedov7f833242013-04-11 16:51:40 +02001923out:
Andreas Färbercd7b87f2013-08-02 18:56:05 +02001924 if (error != NULL) {
1925 error_propagate(errp, error);
1926 object_unref(OBJECT(cpu));
1927 cpu = NULL;
1928 }
Igor Mammedov7f833242013-04-11 16:51:40 +02001929 g_strfreev(model_pieces);
1930 return cpu;
1931}
1932
1933X86CPU *cpu_x86_init(const char *cpu_model)
1934{
1935 Error *error = NULL;
1936 X86CPU *cpu;
1937
Igor Mammedov62fc4032013-04-29 18:54:13 +02001938 cpu = cpu_x86_create(cpu_model, NULL, &error);
Andreas Färber2d642552013-02-15 14:06:56 +01001939 if (error) {
1940 goto out;
1941 }
1942
Igor Mammedov7f833242013-04-11 16:51:40 +02001943 object_property_set_bool(OBJECT(cpu), true, "realized", &error);
1944
Igor Mammedovfa2db3c2013-01-11 03:10:16 +01001945out:
Igor Mammedov18eb4732012-10-02 17:36:54 +02001946 if (error) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04001947 error_report("%s", error_get_pretty(error));
Andreas Färber71ad61d2012-04-17 12:10:29 +02001948 error_free(error);
Andreas Färber2d642552013-02-15 14:06:56 +01001949 if (cpu != NULL) {
1950 object_unref(OBJECT(cpu));
1951 cpu = NULL;
1952 }
Andreas Färber5c3c6a62013-02-01 15:12:13 +01001953 }
1954 return cpu;
1955}
1956
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001957#if !defined(CONFIG_USER_ONLY)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001958
Blue Swirl0e26b7b2010-06-19 10:42:34 +03001959void cpu_clear_apic_feature(CPUX86State *env)
1960{
Eduardo Habkost0514ef22013-04-22 16:00:15 -03001961 env->features[FEAT_1_EDX] &= ~CPUID_APIC;
Blue Swirl0e26b7b2010-06-19 10:42:34 +03001962}
1963
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001964#endif /* !CONFIG_USER_ONLY */
1965
Eduardo Habkostc04321b2012-09-05 17:41:13 -03001966/* Initialize list of CPU models, filling some non-static fields if necessary
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001967 */
1968void x86_cpudef_setup(void)
1969{
Crístian Viana93bfef42012-05-30 00:35:51 -03001970 int i, j;
1971 static const char *model_with_versions[] = { "qemu32", "qemu64", "athlon" };
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001972
1973 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); ++i) {
Eduardo Habkost9576de72014-01-30 17:48:58 -02001974 X86CPUDefinition *def = &builtin_x86_defs[i];
Crístian Viana93bfef42012-05-30 00:35:51 -03001975
1976 /* Look for specific "cpudef" models that */
Stefan Weil09faecf2012-06-20 06:05:51 +02001977 /* have the QEMU version in .model_id */
Crístian Viana93bfef42012-05-30 00:35:51 -03001978 for (j = 0; j < ARRAY_SIZE(model_with_versions); j++) {
Eduardo Habkostbc3e1292012-09-05 17:41:12 -03001979 if (strcmp(model_with_versions[j], def->name) == 0) {
1980 pstrcpy(def->model_id, sizeof(def->model_id),
1981 "QEMU Virtual CPU version ");
1982 pstrcat(def->model_id, sizeof(def->model_id),
1983 qemu_get_version());
Crístian Viana93bfef42012-05-30 00:35:51 -03001984 break;
1985 }
1986 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001987 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001988}
1989
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001990static void get_cpuid_vendor(CPUX86State *env, uint32_t *ebx,
1991 uint32_t *ecx, uint32_t *edx)
1992{
1993 *ebx = env->cpuid_vendor1;
1994 *edx = env->cpuid_vendor2;
1995 *ecx = env->cpuid_vendor3;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001996}
1997
1998void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
1999 uint32_t *eax, uint32_t *ebx,
2000 uint32_t *ecx, uint32_t *edx)
2001{
Andreas Färbera60f24b2012-12-01 05:35:08 +01002002 X86CPU *cpu = x86_env_get_cpu(env);
2003 CPUState *cs = CPU(cpu);
2004
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002005 /* test if maximum index reached */
2006 if (index & 0x80000000) {
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +08002007 if (index > env->cpuid_xlevel) {
2008 if (env->cpuid_xlevel2 > 0) {
2009 /* Handle the Centaur's CPUID instruction. */
2010 if (index > env->cpuid_xlevel2) {
2011 index = env->cpuid_xlevel2;
2012 } else if (index < 0xC0000000) {
2013 index = env->cpuid_xlevel;
2014 }
2015 } else {
Eduardo Habkost57f26ae2012-12-20 16:43:48 -02002016 /* Intel documentation states that invalid EAX input will
2017 * return the same information as EAX=cpuid_level
2018 * (Intel SDM Vol. 2A - Instruction Set Reference - CPUID)
2019 */
2020 index = env->cpuid_level;
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +08002021 }
2022 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002023 } else {
2024 if (index > env->cpuid_level)
2025 index = env->cpuid_level;
2026 }
2027
2028 switch(index) {
2029 case 0:
2030 *eax = env->cpuid_level;
2031 get_cpuid_vendor(env, ebx, ecx, edx);
2032 break;
2033 case 1:
2034 *eax = env->cpuid_version;
2035 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002036 *ecx = env->features[FEAT_1_ECX];
2037 *edx = env->features[FEAT_1_EDX];
Andreas Färberce3960e2012-12-17 03:27:07 +01002038 if (cs->nr_cores * cs->nr_threads > 1) {
2039 *ebx |= (cs->nr_cores * cs->nr_threads) << 16;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002040 *edx |= 1 << 28; /* HTT bit */
2041 }
2042 break;
2043 case 2:
2044 /* cache info: needed for Pentium Pro compatibility */
Benoît Canet787aaf52013-09-02 17:06:37 +02002045 if (cpu->cache_info_passthrough) {
2046 host_cpuid(index, 0, eax, ebx, ecx, edx);
2047 break;
2048 }
Eduardo Habkost5e891bf2013-08-27 12:24:37 -03002049 *eax = 1; /* Number of CPUID[EAX=2] calls required */
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002050 *ebx = 0;
2051 *ecx = 0;
Eduardo Habkost5e891bf2013-08-27 12:24:37 -03002052 *edx = (L1D_DESCRIPTOR << 16) | \
2053 (L1I_DESCRIPTOR << 8) | \
2054 (L2_DESCRIPTOR);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002055 break;
2056 case 4:
2057 /* cache info: needed for Core compatibility */
Benoît Canet787aaf52013-09-02 17:06:37 +02002058 if (cpu->cache_info_passthrough) {
2059 host_cpuid(index, count, eax, ebx, ecx, edx);
Paolo Bonzini76c29752013-11-19 17:49:46 +01002060 *eax &= ~0xFC000000;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002061 } else {
Aurelien Jarno2f7a21c2010-03-13 16:46:33 +01002062 *eax = 0;
Paolo Bonzini76c29752013-11-19 17:49:46 +01002063 switch (count) {
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002064 case 0: /* L1 dcache info */
Eduardo Habkost5e891bf2013-08-27 12:24:37 -03002065 *eax |= CPUID_4_TYPE_DCACHE | \
2066 CPUID_4_LEVEL(1) | \
2067 CPUID_4_SELF_INIT_LEVEL;
2068 *ebx = (L1D_LINE_SIZE - 1) | \
2069 ((L1D_PARTITIONS - 1) << 12) | \
2070 ((L1D_ASSOCIATIVITY - 1) << 22);
2071 *ecx = L1D_SETS - 1;
2072 *edx = CPUID_4_NO_INVD_SHARING;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002073 break;
2074 case 1: /* L1 icache info */
Eduardo Habkost5e891bf2013-08-27 12:24:37 -03002075 *eax |= CPUID_4_TYPE_ICACHE | \
2076 CPUID_4_LEVEL(1) | \
2077 CPUID_4_SELF_INIT_LEVEL;
2078 *ebx = (L1I_LINE_SIZE - 1) | \
2079 ((L1I_PARTITIONS - 1) << 12) | \
2080 ((L1I_ASSOCIATIVITY - 1) << 22);
2081 *ecx = L1I_SETS - 1;
2082 *edx = CPUID_4_NO_INVD_SHARING;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002083 break;
2084 case 2: /* L2 cache info */
Eduardo Habkost5e891bf2013-08-27 12:24:37 -03002085 *eax |= CPUID_4_TYPE_UNIFIED | \
2086 CPUID_4_LEVEL(2) | \
2087 CPUID_4_SELF_INIT_LEVEL;
Andreas Färberce3960e2012-12-17 03:27:07 +01002088 if (cs->nr_threads > 1) {
2089 *eax |= (cs->nr_threads - 1) << 14;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002090 }
Eduardo Habkost5e891bf2013-08-27 12:24:37 -03002091 *ebx = (L2_LINE_SIZE - 1) | \
2092 ((L2_PARTITIONS - 1) << 12) | \
2093 ((L2_ASSOCIATIVITY - 1) << 22);
2094 *ecx = L2_SETS - 1;
2095 *edx = CPUID_4_NO_INVD_SHARING;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002096 break;
2097 default: /* end of info */
2098 *eax = 0;
2099 *ebx = 0;
2100 *ecx = 0;
2101 *edx = 0;
2102 break;
Paolo Bonzini76c29752013-11-19 17:49:46 +01002103 }
2104 }
2105
2106 /* QEMU gives out its own APIC IDs, never pass down bits 31..26. */
2107 if ((*eax & 31) && cs->nr_cores > 1) {
2108 *eax |= (cs->nr_cores - 1) << 26;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002109 }
2110 break;
2111 case 5:
2112 /* mwait info: needed for Core compatibility */
2113 *eax = 0; /* Smallest monitor-line size in bytes */
2114 *ebx = 0; /* Largest monitor-line size in bytes */
2115 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
2116 *edx = 0;
2117 break;
2118 case 6:
2119 /* Thermal and Power Leaf */
2120 *eax = 0;
2121 *ebx = 0;
2122 *ecx = 0;
2123 *edx = 0;
2124 break;
Yang, Wei Yf7911682011-05-30 23:17:42 +08002125 case 7:
Eduardo Habkost13526722012-05-21 11:27:02 -03002126 /* Structured Extended Feature Flags Enumeration Leaf */
2127 if (count == 0) {
2128 *eax = 0; /* Maximum ECX value for sub-leaves */
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002129 *ebx = env->features[FEAT_7_0_EBX]; /* Feature flags */
Eduardo Habkost13526722012-05-21 11:27:02 -03002130 *ecx = 0; /* Reserved */
2131 *edx = 0; /* Reserved */
Yang, Wei Yf7911682011-05-30 23:17:42 +08002132 } else {
2133 *eax = 0;
2134 *ebx = 0;
2135 *ecx = 0;
2136 *edx = 0;
2137 }
2138 break;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002139 case 9:
2140 /* Direct Cache Access Information Leaf */
2141 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
2142 *ebx = 0;
2143 *ecx = 0;
2144 *edx = 0;
2145 break;
2146 case 0xA:
2147 /* Architectural Performance Monitoring Leaf */
Eduardo Habkost9337e3b2013-07-26 17:09:36 -03002148 if (kvm_enabled() && cpu->enable_pmu) {
Andreas Färbera60f24b2012-12-01 05:35:08 +01002149 KVMState *s = cs->kvm_state;
Gleb Natapova0fa8202011-12-15 12:44:05 +02002150
2151 *eax = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EAX);
2152 *ebx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EBX);
2153 *ecx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_ECX);
2154 *edx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EDX);
2155 } else {
2156 *eax = 0;
2157 *ebx = 0;
2158 *ecx = 0;
2159 *edx = 0;
2160 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002161 break;
Paolo Bonzini2560f192013-10-02 17:54:57 +02002162 case 0xD: {
2163 KVMState *s = cs->kvm_state;
2164 uint64_t kvm_mask;
2165 int i;
2166
Sheng Yang51e49432010-06-17 15:18:14 +08002167 /* Processor Extended State */
Paolo Bonzini2560f192013-10-02 17:54:57 +02002168 *eax = 0;
2169 *ebx = 0;
2170 *ecx = 0;
2171 *edx = 0;
2172 if (!(env->features[FEAT_1_ECX] & CPUID_EXT_XSAVE) || !kvm_enabled()) {
Sheng Yang51e49432010-06-17 15:18:14 +08002173 break;
2174 }
Paolo Bonzini2560f192013-10-02 17:54:57 +02002175 kvm_mask =
2176 kvm_arch_get_supported_cpuid(s, 0xd, 0, R_EAX) |
2177 ((uint64_t)kvm_arch_get_supported_cpuid(s, 0xd, 0, R_EDX) << 32);
Jan Kiszkaba9bc592011-06-08 16:11:05 +02002178
Paolo Bonzini2560f192013-10-02 17:54:57 +02002179 if (count == 0) {
2180 *ecx = 0x240;
2181 for (i = 2; i < ARRAY_SIZE(ext_save_areas); i++) {
2182 const ExtSaveArea *esa = &ext_save_areas[i];
2183 if ((env->features[esa->feature] & esa->bits) == esa->bits &&
2184 (kvm_mask & (1 << i)) != 0) {
2185 if (i < 32) {
2186 *eax |= 1 << i;
2187 } else {
2188 *edx |= 1 << (i - 32);
2189 }
2190 *ecx = MAX(*ecx, esa->offset + esa->size);
2191 }
2192 }
2193 *eax |= kvm_mask & (XSTATE_FP | XSTATE_SSE);
2194 *ebx = *ecx;
2195 } else if (count == 1) {
2196 *eax = kvm_arch_get_supported_cpuid(s, 0xd, 1, R_EAX);
2197 } else if (count < ARRAY_SIZE(ext_save_areas)) {
2198 const ExtSaveArea *esa = &ext_save_areas[count];
2199 if ((env->features[esa->feature] & esa->bits) == esa->bits &&
2200 (kvm_mask & (1 << count)) != 0) {
Liu Jinsong33f373d2013-12-03 04:17:50 +08002201 *eax = esa->size;
2202 *ebx = esa->offset;
Paolo Bonzini2560f192013-10-02 17:54:57 +02002203 }
Sheng Yang51e49432010-06-17 15:18:14 +08002204 }
2205 break;
Paolo Bonzini2560f192013-10-02 17:54:57 +02002206 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002207 case 0x80000000:
2208 *eax = env->cpuid_xlevel;
2209 *ebx = env->cpuid_vendor1;
2210 *edx = env->cpuid_vendor2;
2211 *ecx = env->cpuid_vendor3;
2212 break;
2213 case 0x80000001:
2214 *eax = env->cpuid_version;
2215 *ebx = 0;
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002216 *ecx = env->features[FEAT_8000_0001_ECX];
2217 *edx = env->features[FEAT_8000_0001_EDX];
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002218
2219 /* The Linux kernel checks for the CMPLegacy bit and
2220 * discards multiple thread information if it is set.
2221 * So dont set it here for Intel to make Linux guests happy.
2222 */
Andreas Färberce3960e2012-12-17 03:27:07 +01002223 if (cs->nr_cores * cs->nr_threads > 1) {
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002224 uint32_t tebx, tecx, tedx;
2225 get_cpuid_vendor(env, &tebx, &tecx, &tedx);
2226 if (tebx != CPUID_VENDOR_INTEL_1 ||
2227 tedx != CPUID_VENDOR_INTEL_2 ||
2228 tecx != CPUID_VENDOR_INTEL_3) {
2229 *ecx |= 1 << 1; /* CmpLegacy bit */
2230 }
2231 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002232 break;
2233 case 0x80000002:
2234 case 0x80000003:
2235 case 0x80000004:
2236 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
2237 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
2238 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
2239 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
2240 break;
2241 case 0x80000005:
2242 /* cache info (L1 cache) */
Benoît Canet787aaf52013-09-02 17:06:37 +02002243 if (cpu->cache_info_passthrough) {
2244 host_cpuid(index, 0, eax, ebx, ecx, edx);
2245 break;
2246 }
Eduardo Habkost5e891bf2013-08-27 12:24:37 -03002247 *eax = (L1_DTLB_2M_ASSOC << 24) | (L1_DTLB_2M_ENTRIES << 16) | \
2248 (L1_ITLB_2M_ASSOC << 8) | (L1_ITLB_2M_ENTRIES);
2249 *ebx = (L1_DTLB_4K_ASSOC << 24) | (L1_DTLB_4K_ENTRIES << 16) | \
2250 (L1_ITLB_4K_ASSOC << 8) | (L1_ITLB_4K_ENTRIES);
2251 *ecx = (L1D_SIZE_KB_AMD << 24) | (L1D_ASSOCIATIVITY_AMD << 16) | \
2252 (L1D_LINES_PER_TAG << 8) | (L1D_LINE_SIZE);
2253 *edx = (L1I_SIZE_KB_AMD << 24) | (L1I_ASSOCIATIVITY_AMD << 16) | \
2254 (L1I_LINES_PER_TAG << 8) | (L1I_LINE_SIZE);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002255 break;
2256 case 0x80000006:
2257 /* cache info (L2 cache) */
Benoît Canet787aaf52013-09-02 17:06:37 +02002258 if (cpu->cache_info_passthrough) {
2259 host_cpuid(index, 0, eax, ebx, ecx, edx);
2260 break;
2261 }
Eduardo Habkost5e891bf2013-08-27 12:24:37 -03002262 *eax = (AMD_ENC_ASSOC(L2_DTLB_2M_ASSOC) << 28) | \
2263 (L2_DTLB_2M_ENTRIES << 16) | \
2264 (AMD_ENC_ASSOC(L2_ITLB_2M_ASSOC) << 12) | \
2265 (L2_ITLB_2M_ENTRIES);
2266 *ebx = (AMD_ENC_ASSOC(L2_DTLB_4K_ASSOC) << 28) | \
2267 (L2_DTLB_4K_ENTRIES << 16) | \
2268 (AMD_ENC_ASSOC(L2_ITLB_4K_ASSOC) << 12) | \
2269 (L2_ITLB_4K_ENTRIES);
2270 *ecx = (L2_SIZE_KB_AMD << 16) | \
2271 (AMD_ENC_ASSOC(L2_ASSOCIATIVITY) << 12) | \
2272 (L2_LINES_PER_TAG << 8) | (L2_LINE_SIZE);
2273 *edx = ((L3_SIZE_KB/512) << 18) | \
2274 (AMD_ENC_ASSOC(L3_ASSOCIATIVITY) << 12) | \
2275 (L3_LINES_PER_TAG << 8) | (L3_LINE_SIZE);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002276 break;
2277 case 0x80000008:
2278 /* virtual & phys address size in low 2 bytes. */
2279/* XXX: This value must match the one used in the MMU code. */
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002280 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM) {
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002281 /* 64 bit processor */
2282/* XXX: The physical address space is limited to 42 bits in exec.c. */
Eduardo Habkostdd13e082013-04-11 17:07:23 -03002283 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002284 } else {
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002285 if (env->features[FEAT_1_EDX] & CPUID_PSE36) {
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002286 *eax = 0x00000024; /* 36 bits physical */
Eduardo Habkostdd13e082013-04-11 17:07:23 -03002287 } else {
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002288 *eax = 0x00000020; /* 32 bits physical */
Eduardo Habkostdd13e082013-04-11 17:07:23 -03002289 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002290 }
2291 *ebx = 0;
2292 *ecx = 0;
2293 *edx = 0;
Andreas Färberce3960e2012-12-17 03:27:07 +01002294 if (cs->nr_cores * cs->nr_threads > 1) {
2295 *ecx |= (cs->nr_cores * cs->nr_threads) - 1;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002296 }
2297 break;
2298 case 0x8000000A:
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002299 if (env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM) {
Eduardo Habkost9f3fb562012-12-04 17:34:38 -02002300 *eax = 0x00000001; /* SVM Revision */
2301 *ebx = 0x00000010; /* nr of ASIDs */
2302 *ecx = 0;
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002303 *edx = env->features[FEAT_SVM]; /* optional features */
Eduardo Habkost9f3fb562012-12-04 17:34:38 -02002304 } else {
2305 *eax = 0;
2306 *ebx = 0;
2307 *ecx = 0;
2308 *edx = 0;
2309 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002310 break;
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +08002311 case 0xC0000000:
2312 *eax = env->cpuid_xlevel2;
2313 *ebx = 0;
2314 *ecx = 0;
2315 *edx = 0;
2316 break;
2317 case 0xC0000001:
2318 /* Support for VIA CPU's CPUID instruction */
2319 *eax = env->cpuid_version;
2320 *ebx = 0;
2321 *ecx = 0;
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002322 *edx = env->features[FEAT_C000_0001_EDX];
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +08002323 break;
2324 case 0xC0000002:
2325 case 0xC0000003:
2326 case 0xC0000004:
2327 /* Reserved for the future, and now filled with zero */
2328 *eax = 0;
2329 *ebx = 0;
2330 *ecx = 0;
2331 *edx = 0;
2332 break;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01002333 default:
2334 /* reserved values: zero */
2335 *eax = 0;
2336 *ebx = 0;
2337 *ecx = 0;
2338 *edx = 0;
2339 break;
2340 }
2341}
Andreas Färber5fd20872012-04-02 23:20:08 +02002342
2343/* CPUClass::reset() */
2344static void x86_cpu_reset(CPUState *s)
2345{
2346 X86CPU *cpu = X86_CPU(s);
2347 X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
2348 CPUX86State *env = &cpu->env;
Andreas Färberc1958ae2012-04-03 00:16:24 +02002349 int i;
2350
Andreas Färber5fd20872012-04-02 23:20:08 +02002351 xcc->parent_reset(s);
2352
Andreas Färberc1958ae2012-04-03 00:16:24 +02002353
2354 memset(env, 0, offsetof(CPUX86State, breakpoints));
2355
2356 tlb_flush(env, 1);
2357
2358 env->old_exception = -1;
2359
2360 /* init to reset state */
2361
2362#ifdef CONFIG_SOFTMMU
2363 env->hflags |= HF_SOFTMMU_MASK;
2364#endif
2365 env->hflags2 |= HF2_GIF_MASK;
2366
2367 cpu_x86_update_cr0(env, 0x60000010);
2368 env->a20_mask = ~0x0;
2369 env->smbase = 0x30000;
2370
2371 env->idt.limit = 0xffff;
2372 env->gdt.limit = 0xffff;
2373 env->ldt.limit = 0xffff;
2374 env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
2375 env->tr.limit = 0xffff;
2376 env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
2377
2378 cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
2379 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK |
2380 DESC_R_MASK | DESC_A_MASK);
2381 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
2382 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2383 DESC_A_MASK);
2384 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
2385 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2386 DESC_A_MASK);
2387 cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
2388 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2389 DESC_A_MASK);
2390 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
2391 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2392 DESC_A_MASK);
2393 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
2394 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2395 DESC_A_MASK);
2396
2397 env->eip = 0xfff0;
2398 env->regs[R_EDX] = env->cpuid_version;
2399
2400 env->eflags = 0x2;
2401
2402 /* FPU init */
2403 for (i = 0; i < 8; i++) {
2404 env->fptags[i] = 1;
2405 }
2406 env->fpuc = 0x37f;
2407
2408 env->mxcsr = 0x1f80;
Paolo Bonzinic74f41b2013-09-13 15:55:57 +02002409 env->xstate_bv = XSTATE_FP | XSTATE_SSE;
Andreas Färberc1958ae2012-04-03 00:16:24 +02002410
2411 env->pat = 0x0007040600070406ULL;
2412 env->msr_ia32_misc_enable = MSR_IA32_MISC_ENABLE_DEFAULT;
2413
2414 memset(env->dr, 0, sizeof(env->dr));
2415 env->dr[6] = DR6_FIXED_1;
2416 env->dr[7] = DR7_FIXED_1;
2417 cpu_breakpoint_remove_all(env, BP_CPU);
2418 cpu_watchpoint_remove_all(env, BP_CPU);
Igor Mammedovdd673282012-07-23 15:22:27 +02002419
Fernando Luis Vázquez Cao05226042013-12-06 17:33:01 +09002420 env->tsc_adjust = 0;
2421 env->tsc = 0;
2422
Igor Mammedovdd673282012-07-23 15:22:27 +02002423#if !defined(CONFIG_USER_ONLY)
2424 /* We hard-wire the BSP to the first CPU. */
Andreas Färber55e5c282012-12-17 06:18:02 +01002425 if (s->cpu_index == 0) {
Chen Fan02e51482013-12-23 17:04:02 +08002426 apic_designate_bsp(cpu->apic_state);
Igor Mammedovdd673282012-07-23 15:22:27 +02002427 }
2428
Andreas Färber259186a2013-01-17 18:51:17 +01002429 s->halted = !cpu_is_bsp(cpu);
Igor Mammedovdd673282012-07-23 15:22:27 +02002430#endif
Andreas Färber5fd20872012-04-02 23:20:08 +02002431}
2432
Igor Mammedovdd673282012-07-23 15:22:27 +02002433#ifndef CONFIG_USER_ONLY
2434bool cpu_is_bsp(X86CPU *cpu)
2435{
Chen Fan02e51482013-12-23 17:04:02 +08002436 return cpu_get_apic_base(cpu->apic_state) & MSR_IA32_APICBASE_BSP;
Igor Mammedovdd673282012-07-23 15:22:27 +02002437}
Igor Mammedov65dee382012-07-23 15:22:28 +02002438
2439/* TODO: remove me, when reset over QOM tree is implemented */
2440static void x86_cpu_machine_reset_cb(void *opaque)
2441{
2442 X86CPU *cpu = opaque;
2443 cpu_reset(CPU(cpu));
2444}
Igor Mammedovdd673282012-07-23 15:22:27 +02002445#endif
2446
Andreas Färberde024812012-04-03 00:00:17 +02002447static void mce_init(X86CPU *cpu)
2448{
2449 CPUX86State *cenv = &cpu->env;
2450 unsigned int bank;
2451
2452 if (((cenv->cpuid_version >> 8) & 0xf) >= 6
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002453 && (cenv->features[FEAT_1_EDX] & (CPUID_MCE | CPUID_MCA)) ==
Andreas Färberde024812012-04-03 00:00:17 +02002454 (CPUID_MCE | CPUID_MCA)) {
2455 cenv->mcg_cap = MCE_CAP_DEF | MCE_BANKS_DEF;
2456 cenv->mcg_ctl = ~(uint64_t)0;
2457 for (bank = 0; bank < MCE_BANKS_DEF; bank++) {
2458 cenv->mce_banks[bank * 4] = ~(uint64_t)0;
2459 }
2460 }
2461}
2462
Igor Mammedovbdeec802012-10-13 22:35:39 +02002463#ifndef CONFIG_USER_ONLY
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002464static void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
Igor Mammedovbdeec802012-10-13 22:35:39 +02002465{
Igor Mammedovbdeec802012-10-13 22:35:39 +02002466 CPUX86State *env = &cpu->env;
Igor Mammedov53a89e22013-04-29 19:03:01 +02002467 DeviceState *dev = DEVICE(cpu);
Andreas Färber449994e2012-10-10 12:18:02 +02002468 APICCommonState *apic;
Igor Mammedovbdeec802012-10-13 22:35:39 +02002469 const char *apic_type = "apic";
2470
2471 if (kvm_irqchip_in_kernel()) {
2472 apic_type = "kvm-apic";
2473 } else if (xen_enabled()) {
2474 apic_type = "xen-apic";
2475 }
2476
Chen Fan02e51482013-12-23 17:04:02 +08002477 cpu->apic_state = qdev_try_create(qdev_get_parent_bus(dev), apic_type);
2478 if (cpu->apic_state == NULL) {
Igor Mammedovbdeec802012-10-13 22:35:39 +02002479 error_setg(errp, "APIC device '%s' could not be created", apic_type);
2480 return;
2481 }
2482
2483 object_property_add_child(OBJECT(cpu), "apic",
Chen Fan02e51482013-12-23 17:04:02 +08002484 OBJECT(cpu->apic_state), NULL);
2485 qdev_prop_set_uint8(cpu->apic_state, "id", env->cpuid_apic_id);
Igor Mammedovbdeec802012-10-13 22:35:39 +02002486 /* TODO: convert to link<> */
Chen Fan02e51482013-12-23 17:04:02 +08002487 apic = APIC_COMMON(cpu->apic_state);
Andreas Färber60671e52012-10-10 14:10:07 +02002488 apic->cpu = cpu;
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002489}
2490
2491static void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
2492{
Chen Fan02e51482013-12-23 17:04:02 +08002493 if (cpu->apic_state == NULL) {
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002494 return;
2495 }
Igor Mammedovbdeec802012-10-13 22:35:39 +02002496
Chen Fan02e51482013-12-23 17:04:02 +08002497 if (qdev_init(cpu->apic_state)) {
Igor Mammedovbdeec802012-10-13 22:35:39 +02002498 error_setg(errp, "APIC device '%s' could not be initialized",
Chen Fan02e51482013-12-23 17:04:02 +08002499 object_get_typename(OBJECT(cpu->apic_state)));
Igor Mammedovbdeec802012-10-13 22:35:39 +02002500 return;
2501 }
Igor Mammedovbdeec802012-10-13 22:35:39 +02002502}
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002503#else
2504static void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
2505{
2506}
Igor Mammedovbdeec802012-10-13 22:35:39 +02002507#endif
2508
Andreas Färber2b6f2942013-01-16 03:41:47 +01002509static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
Andreas Färber7a059952012-05-09 23:15:32 +02002510{
Andreas Färber14a10fc2013-07-27 02:53:25 +02002511 CPUState *cs = CPU(dev);
Andreas Färber2b6f2942013-01-16 03:41:47 +01002512 X86CPU *cpu = X86_CPU(dev);
2513 X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
Igor Mammedovb34d12d2012-10-22 17:03:00 +02002514 CPUX86State *env = &cpu->env;
Andreas Färber2b6f2942013-01-16 03:41:47 +01002515 Error *local_err = NULL;
Igor Mammedovb34d12d2012-10-22 17:03:00 +02002516
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002517 if (env->features[FEAT_7_0_EBX] && env->cpuid_level < 7) {
Igor Mammedovb34d12d2012-10-22 17:03:00 +02002518 env->cpuid_level = 7;
2519 }
Andreas Färber7a059952012-05-09 23:15:32 +02002520
Igor Mammedov9b15cd92012-12-28 21:01:17 +01002521 /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on
2522 * CPUID[1].EDX.
2523 */
2524 if (env->cpuid_vendor1 == CPUID_VENDOR_AMD_1 &&
2525 env->cpuid_vendor2 == CPUID_VENDOR_AMD_2 &&
2526 env->cpuid_vendor3 == CPUID_VENDOR_AMD_3) {
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002527 env->features[FEAT_8000_0001_EDX] &= ~CPUID_EXT2_AMD_ALIASES;
2528 env->features[FEAT_8000_0001_EDX] |= (env->features[FEAT_1_EDX]
Igor Mammedov9b15cd92012-12-28 21:01:17 +01002529 & CPUID_EXT2_AMD_ALIASES);
2530 }
2531
Igor Mammedov4586f152012-12-28 21:01:16 +01002532 if (!kvm_enabled()) {
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002533 env->features[FEAT_1_EDX] &= TCG_FEATURES;
2534 env->features[FEAT_1_ECX] &= TCG_EXT_FEATURES;
2535 env->features[FEAT_8000_0001_EDX] &= (TCG_EXT2_FEATURES
Igor Mammedov4586f152012-12-28 21:01:16 +01002536#ifdef TARGET_X86_64
2537 | CPUID_EXT2_SYSCALL | CPUID_EXT2_LM
2538#endif
2539 );
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002540 env->features[FEAT_8000_0001_ECX] &= TCG_EXT3_FEATURES;
2541 env->features[FEAT_SVM] &= TCG_SVM_FEATURES;
Igor Mammedov4586f152012-12-28 21:01:16 +01002542 } else {
Eduardo Habkostf0b9b112014-01-20 14:41:14 -02002543 KVMState *s = kvm_state;
Igor Mammedov912ffc42013-06-04 15:13:14 +02002544 if ((cpu->check_cpuid || cpu->enforce_cpuid)
Eduardo Habkostf0b9b112014-01-20 14:41:14 -02002545 && kvm_check_features_against_host(s, cpu) && cpu->enforce_cpuid) {
Igor Mammedov4dc1f442013-04-05 16:36:53 +02002546 error_setg(&local_err,
2547 "Host's CPU doesn't support requested features");
2548 goto out;
Igor Mammedov5ec01c22013-01-11 03:10:17 +01002549 }
Eduardo Habkosta509d632013-04-02 17:48:12 -03002550 filter_features_for_kvm(cpu);
Igor Mammedov4586f152012-12-28 21:01:16 +01002551 }
2552
Igor Mammedov65dee382012-07-23 15:22:28 +02002553#ifndef CONFIG_USER_ONLY
2554 qemu_register_reset(x86_cpu_machine_reset_cb, cpu);
Igor Mammedovbdeec802012-10-13 22:35:39 +02002555
Eduardo Habkost0514ef22013-04-22 16:00:15 -03002556 if (cpu->env.features[FEAT_1_EDX] & CPUID_APIC || smp_cpus > 1) {
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002557 x86_cpu_apic_create(cpu, &local_err);
Andreas Färber2b6f2942013-01-16 03:41:47 +01002558 if (local_err != NULL) {
Igor Mammedov4dc1f442013-04-05 16:36:53 +02002559 goto out;
Igor Mammedovbdeec802012-10-13 22:35:39 +02002560 }
2561 }
Igor Mammedov65dee382012-07-23 15:22:28 +02002562#endif
2563
Andreas Färber7a059952012-05-09 23:15:32 +02002564 mce_init(cpu);
Andreas Färber14a10fc2013-07-27 02:53:25 +02002565 qemu_init_vcpu(cs);
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002566
2567 x86_cpu_apic_realize(cpu, &local_err);
2568 if (local_err != NULL) {
2569 goto out;
2570 }
Andreas Färber14a10fc2013-07-27 02:53:25 +02002571 cpu_reset(cs);
Andreas Färber2b6f2942013-01-16 03:41:47 +01002572
Igor Mammedov4dc1f442013-04-05 16:36:53 +02002573 xcc->parent_realize(dev, &local_err);
2574out:
2575 if (local_err != NULL) {
2576 error_propagate(errp, local_err);
2577 return;
2578 }
Andreas Färber7a059952012-05-09 23:15:32 +02002579}
2580
Eduardo Habkost8932cfd2013-01-22 18:25:09 -02002581/* Enables contiguous-apic-ID mode, for compatibility */
2582static bool compat_apic_id_mode;
2583
2584void enable_compat_apic_id_mode(void)
2585{
2586 compat_apic_id_mode = true;
2587}
2588
Eduardo Habkostcb41bad2013-01-22 18:25:04 -02002589/* Calculates initial APIC ID for a specific CPU index
2590 *
2591 * Currently we need to be able to calculate the APIC ID from the CPU index
2592 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
2593 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
2594 * all CPUs up to max_cpus.
2595 */
2596uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index)
2597{
Eduardo Habkost8932cfd2013-01-22 18:25:09 -02002598 uint32_t correct_id;
2599 static bool warned;
2600
2601 correct_id = x86_apicid_from_cpu_idx(smp_cores, smp_threads, cpu_index);
2602 if (compat_apic_id_mode) {
2603 if (cpu_index != correct_id && !warned) {
2604 error_report("APIC IDs set in compatibility mode, "
2605 "CPU topology won't match the configuration");
2606 warned = true;
2607 }
2608 return cpu_index;
2609 } else {
2610 return correct_id;
2611 }
Eduardo Habkostcb41bad2013-01-22 18:25:04 -02002612}
2613
Andreas Färberde024812012-04-03 00:00:17 +02002614static void x86_cpu_initfn(Object *obj)
2615{
Andreas Färber55e5c282012-12-17 06:18:02 +01002616 CPUState *cs = CPU(obj);
Andreas Färberde024812012-04-03 00:00:17 +02002617 X86CPU *cpu = X86_CPU(obj);
2618 CPUX86State *env = &cpu->env;
Igor Mammedovd65e9812012-06-19 15:39:46 +02002619 static int inited;
Andreas Färberde024812012-04-03 00:00:17 +02002620
Andreas Färberc05efcb2013-01-17 12:13:41 +01002621 cs->env_ptr = env;
Andreas Färberde024812012-04-03 00:00:17 +02002622 cpu_exec_init(env);
Andreas Färber71ad61d2012-04-17 12:10:29 +02002623
2624 object_property_add(obj, "family", "int",
Andreas Färber95b85192012-04-17 14:42:22 +02002625 x86_cpuid_version_get_family,
Andreas Färber71ad61d2012-04-17 12:10:29 +02002626 x86_cpuid_version_set_family, NULL, NULL, NULL);
Andreas Färberc5291a42012-04-17 12:16:39 +02002627 object_property_add(obj, "model", "int",
Andreas Färber67e30c82012-04-17 14:48:14 +02002628 x86_cpuid_version_get_model,
Andreas Färberc5291a42012-04-17 12:16:39 +02002629 x86_cpuid_version_set_model, NULL, NULL, NULL);
Andreas Färber036e2222012-04-17 14:14:18 +02002630 object_property_add(obj, "stepping", "int",
Andreas Färber35112e42012-04-17 14:50:53 +02002631 x86_cpuid_version_get_stepping,
Andreas Färber036e2222012-04-17 14:14:18 +02002632 x86_cpuid_version_set_stepping, NULL, NULL, NULL);
Andreas Färber8e1898b2012-04-17 18:41:40 +02002633 object_property_add(obj, "level", "int",
2634 x86_cpuid_get_level,
2635 x86_cpuid_set_level, NULL, NULL, NULL);
Andreas Färber16b93aa2012-04-17 18:44:07 +02002636 object_property_add(obj, "xlevel", "int",
2637 x86_cpuid_get_xlevel,
2638 x86_cpuid_set_xlevel, NULL, NULL, NULL);
Andreas Färberd480e1a2012-04-17 19:22:58 +02002639 object_property_add_str(obj, "vendor",
2640 x86_cpuid_get_vendor,
2641 x86_cpuid_set_vendor, NULL);
Andreas Färber938d4c22012-04-17 15:17:27 +02002642 object_property_add_str(obj, "model-id",
Andreas Färber63e886e2012-04-17 23:02:26 +02002643 x86_cpuid_get_model_id,
Andreas Färber938d4c22012-04-17 15:17:27 +02002644 x86_cpuid_set_model_id, NULL);
Andreas Färber89e48962012-04-18 00:12:23 +02002645 object_property_add(obj, "tsc-frequency", "int",
2646 x86_cpuid_get_tsc_freq,
2647 x86_cpuid_set_tsc_freq, NULL, NULL, NULL);
Igor Mammedov31050932013-04-25 16:05:26 +02002648 object_property_add(obj, "apic-id", "int",
2649 x86_cpuid_get_apic_id,
2650 x86_cpuid_set_apic_id, NULL, NULL, NULL);
Eduardo Habkost8e8aba52013-05-06 13:20:07 -03002651 object_property_add(obj, "feature-words", "X86CPUFeatureWordInfo",
2652 x86_cpu_get_feature_words,
Eduardo Habkost7e5292b2013-05-06 13:20:09 -03002653 NULL, NULL, (void *)env->features, NULL);
2654 object_property_add(obj, "filtered-features", "X86CPUFeatureWordInfo",
2655 x86_cpu_get_feature_words,
2656 NULL, NULL, (void *)cpu->filtered_features, NULL);
Andreas Färber71ad61d2012-04-17 12:10:29 +02002657
Igor Mammedov92067bf2013-06-05 15:18:40 +02002658 cpu->hyperv_spinlock_attempts = HYPERV_SPINLOCK_NEVER_RETRY;
Eduardo Habkostcb41bad2013-01-22 18:25:04 -02002659 env->cpuid_apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);
Igor Mammedovd65e9812012-06-19 15:39:46 +02002660
2661 /* init various static tables used in TCG mode */
2662 if (tcg_enabled() && !inited) {
2663 inited = 1;
2664 optimize_flags_init();
2665#ifndef CONFIG_USER_ONLY
2666 cpu_set_debug_excp_handler(breakpoint_handler);
2667#endif
2668 }
Andreas Färberde024812012-04-03 00:00:17 +02002669}
2670
Igor Mammedov997395d2013-04-23 10:29:41 +02002671static int64_t x86_cpu_get_arch_id(CPUState *cs)
2672{
2673 X86CPU *cpu = X86_CPU(cs);
2674 CPUX86State *env = &cpu->env;
2675
2676 return env->cpuid_apic_id;
2677}
2678
Andreas Färber444d5592013-05-28 13:28:38 +02002679static bool x86_cpu_get_paging_enabled(const CPUState *cs)
2680{
2681 X86CPU *cpu = X86_CPU(cs);
2682
2683 return cpu->env.cr[0] & CR0_PG_MASK;
2684}
2685
Andreas Färberf45748f2013-06-21 19:09:18 +02002686static void x86_cpu_set_pc(CPUState *cs, vaddr value)
2687{
2688 X86CPU *cpu = X86_CPU(cs);
2689
2690 cpu->env.eip = value;
2691}
2692
Andreas Färberbdf7ae52013-06-28 19:31:32 +02002693static void x86_cpu_synchronize_from_tb(CPUState *cs, TranslationBlock *tb)
2694{
2695 X86CPU *cpu = X86_CPU(cs);
2696
2697 cpu->env.eip = tb->pc - tb->cs_base;
2698}
2699
Andreas Färber8c2e1b02013-08-25 18:53:55 +02002700static bool x86_cpu_has_work(CPUState *cs)
2701{
2702 X86CPU *cpu = X86_CPU(cs);
2703 CPUX86State *env = &cpu->env;
2704
2705 return ((cs->interrupt_request & (CPU_INTERRUPT_HARD |
2706 CPU_INTERRUPT_POLL)) &&
2707 (env->eflags & IF_MASK)) ||
2708 (cs->interrupt_request & (CPU_INTERRUPT_NMI |
2709 CPU_INTERRUPT_INIT |
2710 CPU_INTERRUPT_SIPI |
2711 CPU_INTERRUPT_MCE));
2712}
2713
Eduardo Habkost9337e3b2013-07-26 17:09:36 -03002714static Property x86_cpu_properties[] = {
2715 DEFINE_PROP_BOOL("pmu", X86CPU, enable_pmu, false),
Igor Mammedovc8f0f882013-06-04 15:05:25 +02002716 { .name = "hv-spinlocks", .info = &qdev_prop_spinlocks },
Igor Mammedov89314502012-08-08 13:52:51 +02002717 DEFINE_PROP_BOOL("hv-relaxed", X86CPU, hyperv_relaxed_timing, false),
Igor Mammedov0f466852012-08-08 13:54:27 +02002718 DEFINE_PROP_BOOL("hv-vapic", X86CPU, hyperv_vapic, false),
Vadim Rozenfeld48a5f3b2014-01-24 00:40:49 +11002719 DEFINE_PROP_BOOL("hv-time", X86CPU, hyperv_time, false),
Igor Mammedov912ffc42013-06-04 15:13:14 +02002720 DEFINE_PROP_BOOL("check", X86CPU, check_cpuid, false),
2721 DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false),
Eduardo Habkost9337e3b2013-07-26 17:09:36 -03002722 DEFINE_PROP_END_OF_LIST()
2723};
2724
Andreas Färber5fd20872012-04-02 23:20:08 +02002725static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
2726{
2727 X86CPUClass *xcc = X86_CPU_CLASS(oc);
2728 CPUClass *cc = CPU_CLASS(oc);
Andreas Färber2b6f2942013-01-16 03:41:47 +01002729 DeviceClass *dc = DEVICE_CLASS(oc);
2730
2731 xcc->parent_realize = dc->realize;
2732 dc->realize = x86_cpu_realizefn;
Igor Mammedov62fc4032013-04-29 18:54:13 +02002733 dc->bus_type = TYPE_ICC_BUS;
Eduardo Habkost9337e3b2013-07-26 17:09:36 -03002734 dc->props = x86_cpu_properties;
Andreas Färber5fd20872012-04-02 23:20:08 +02002735
2736 xcc->parent_reset = cc->reset;
2737 cc->reset = x86_cpu_reset;
Andreas Färber91b1df82013-06-16 07:49:48 +02002738 cc->reset_dump_flags = CPU_DUMP_FPU | CPU_DUMP_CCOP;
Andreas Färberf56e3a12013-02-02 13:38:08 +01002739
Andreas Färber8c2e1b02013-08-25 18:53:55 +02002740 cc->has_work = x86_cpu_has_work;
Andreas Färber97a8ea52013-02-02 10:57:51 +01002741 cc->do_interrupt = x86_cpu_do_interrupt;
Andreas Färber878096e2013-05-27 01:33:50 +02002742 cc->dump_state = x86_cpu_dump_state;
Andreas Färberf45748f2013-06-21 19:09:18 +02002743 cc->set_pc = x86_cpu_set_pc;
Andreas Färberbdf7ae52013-06-28 19:31:32 +02002744 cc->synchronize_from_tb = x86_cpu_synchronize_from_tb;
Andreas Färber5b50e792013-06-29 04:18:45 +02002745 cc->gdb_read_register = x86_cpu_gdb_read_register;
2746 cc->gdb_write_register = x86_cpu_gdb_write_register;
Andreas Färber444d5592013-05-28 13:28:38 +02002747 cc->get_arch_id = x86_cpu_get_arch_id;
2748 cc->get_paging_enabled = x86_cpu_get_paging_enabled;
Jens Freimannc72bf462013-04-19 16:45:06 +02002749#ifndef CONFIG_USER_ONLY
Andreas Färbera23bbfd2013-05-28 13:52:01 +02002750 cc->get_memory_mapping = x86_cpu_get_memory_mapping;
Andreas Färber00b941e2013-06-29 18:55:54 +02002751 cc->get_phys_page_debug = x86_cpu_get_phys_page_debug;
Jens Freimannc72bf462013-04-19 16:45:06 +02002752 cc->write_elf64_note = x86_cpu_write_elf64_note;
2753 cc->write_elf64_qemunote = x86_cpu_write_elf64_qemunote;
2754 cc->write_elf32_note = x86_cpu_write_elf32_note;
2755 cc->write_elf32_qemunote = x86_cpu_write_elf32_qemunote;
Andreas Färber00b941e2013-06-29 18:55:54 +02002756 cc->vmsd = &vmstate_x86_cpu;
Jens Freimannc72bf462013-04-19 16:45:06 +02002757#endif
Andreas Färbera0e372f2013-06-28 23:18:47 +02002758 cc->gdb_num_core_regs = CPU_NB_REGS * 2 + 25;
Andreas Färber5fd20872012-04-02 23:20:08 +02002759}
2760
2761static const TypeInfo x86_cpu_type_info = {
2762 .name = TYPE_X86_CPU,
2763 .parent = TYPE_CPU,
2764 .instance_size = sizeof(X86CPU),
Andreas Färberde024812012-04-03 00:00:17 +02002765 .instance_init = x86_cpu_initfn,
Andreas Färber5fd20872012-04-02 23:20:08 +02002766 .abstract = false,
2767 .class_size = sizeof(X86CPUClass),
2768 .class_init = x86_cpu_common_class_init,
2769};
2770
2771static void x86_cpu_register_types(void)
2772{
2773 type_register_static(&x86_cpu_type_info);
2774}
2775
2776type_init(x86_cpu_register_types)