blob: a165bcf0a7162f4e3908fe6c84b73ecc878c4732 [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
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010033#include "qapi/visitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/arch_init.h"
Andreas Färber71ad61d2012-04-17 12:10:29 +020035
Vadim Rozenfeld28f52cc2011-12-18 22:48:13 +020036#include "hyperv.h"
37
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 Mammedov62fc4032013-04-29 18:54:13 +020044#include "hw/cpu/icc_bus.h"
Igor Mammedovbdeec802012-10-13 22:35:39 +020045#ifndef CONFIG_USER_ONLY
Paolo Bonzini0d09e412013-02-05 17:06:20 +010046#include "hw/xen/xen.h"
Igor Mammedovbdeec802012-10-13 22:35:39 +020047#include "hw/sysbus.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
Igor Mammedov99b88a12013-01-21 15:06:36 +010051static void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
52 uint32_t vendor2, uint32_t vendor3)
53{
54 int i;
55 for (i = 0; i < 4; i++) {
56 dst[i] = vendor1 >> (8 * i);
57 dst[i + 4] = vendor2 >> (8 * i);
58 dst[i + 8] = vendor3 >> (8 * i);
59 }
60 dst[CPUID_VENDOR_SZ] = '\0';
61}
62
Andre Przywarac6dc6f62010-03-11 14:38:55 +010063/* feature flags taken from "Intel Processor Identification and the CPUID
64 * Instruction" and AMD's "CPUID Specification". In cases of disagreement
65 * between feature naming conventions, aliases may be added.
66 */
67static const char *feature_name[] = {
68 "fpu", "vme", "de", "pse",
69 "tsc", "msr", "pae", "mce",
70 "cx8", "apic", NULL, "sep",
71 "mtrr", "pge", "mca", "cmov",
72 "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
73 NULL, "ds" /* Intel dts */, "acpi", "mmx",
74 "fxsr", "sse", "sse2", "ss",
75 "ht" /* Intel htt */, "tm", "ia64", "pbe",
76};
77static const char *ext_feature_name[] = {
Eduardo Habkostf370be32012-02-17 14:41:20 -020078 "pni|sse3" /* Intel,AMD sse3 */, "pclmulqdq|pclmuldq", "dtes64", "monitor",
Andre Przywarae117f772010-03-11 14:38:59 +010079 "ds_cpl", "vmx", "smx", "est",
Andre Przywarac6dc6f62010-03-11 14:38:55 +010080 "tm2", "ssse3", "cid", NULL,
Andre Przywarae117f772010-03-11 14:38:59 +010081 "fma", "cx16", "xtpr", "pdcm",
Mao, Junjie434acb82012-07-20 07:08:21 +000082 NULL, "pcid", "dca", "sse4.1|sse4_1",
Andre Przywarae117f772010-03-11 14:38:59 +010083 "sse4.2|sse4_2", "x2apic", "movbe", "popcnt",
Eduardo Habkosteaf3f092012-03-06 15:11:30 -030084 "tsc-deadline", "aes", "xsave", "osxsave",
Andre Przywarac8acc382012-11-14 16:28:52 -020085 "avx", "f16c", "rdrand", "hypervisor",
Andre Przywarac6dc6f62010-03-11 14:38:55 +010086};
Eduardo Habkost3b671a42012-09-06 10:05:38 +000087/* Feature names that are already defined on feature_name[] but are set on
88 * CPUID[8000_0001].EDX on AMD CPUs don't have their names on
89 * ext2_feature_name[]. They are copied automatically to cpuid_ext2_features
90 * if and only if CPU vendor is AMD.
91 */
Andre Przywarac6dc6f62010-03-11 14:38:55 +010092static const char *ext2_feature_name[] = {
Eduardo Habkost3b671a42012-09-06 10:05:38 +000093 NULL /* fpu */, NULL /* vme */, NULL /* de */, NULL /* pse */,
94 NULL /* tsc */, NULL /* msr */, NULL /* pae */, NULL /* mce */,
95 NULL /* cx8 */ /* AMD CMPXCHG8B */, NULL /* apic */, NULL, "syscall",
96 NULL /* mtrr */, NULL /* pge */, NULL /* mca */, NULL /* cmov */,
97 NULL /* pat */, NULL /* pse36 */, NULL, NULL /* Linux mp */,
98 "nx|xd", NULL, "mmxext", NULL /* mmx */,
99 NULL /* fxsr */, "fxsr_opt|ffxsr", "pdpe1gb" /* AMD Page1GB */, "rdtscp",
Eduardo Habkost01f590d2012-10-24 12:10:33 -0200100 NULL, "lm|i64", "3dnowext", "3dnow",
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100101};
102static const char *ext3_feature_name[] = {
103 "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
104 "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
Andre Przywarae117f772010-03-11 14:38:59 +0100105 "3dnowprefetch", "osvw", "ibs", "xop",
Andre Przywarac8acc382012-11-14 16:28:52 -0200106 "skinit", "wdt", NULL, "lwp",
107 "fma4", "tce", NULL, "nodeid_msr",
108 NULL, "tbm", "topoext", "perfctr_core",
109 "perfctr_nb", NULL, NULL, NULL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100110 NULL, NULL, NULL, NULL,
111};
112
Eduardo Habkost89e49c82013-01-07 16:20:47 -0200113static const char *ext4_feature_name[] = {
114 NULL, NULL, "xstore", "xstore-en",
115 NULL, NULL, "xcrypt", "xcrypt-en",
116 "ace2", "ace2-en", "phe", "phe-en",
117 "pmm", "pmm-en", NULL, NULL,
118 NULL, NULL, NULL, NULL,
119 NULL, NULL, NULL, NULL,
120 NULL, NULL, NULL, NULL,
121 NULL, NULL, NULL, NULL,
122};
123
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100124static const char *kvm_feature_name[] = {
Don Slutzc3d39802012-10-12 15:43:23 -0400125 "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock",
126 "kvm_asyncpf", "kvm_steal_time", "kvm_pv_eoi", NULL,
127 NULL, NULL, NULL, NULL,
128 NULL, NULL, NULL, NULL,
129 NULL, NULL, NULL, NULL,
130 NULL, NULL, NULL, NULL,
131 NULL, NULL, NULL, NULL,
132 NULL, NULL, NULL, NULL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100133};
134
Joerg Roedel296acb62010-09-27 15:16:17 +0200135static const char *svm_feature_name[] = {
136 "npt", "lbrv", "svm_lock", "nrip_save",
137 "tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists",
138 NULL, NULL, "pause_filter", NULL,
139 "pfthreshold", NULL, NULL, NULL,
140 NULL, NULL, NULL, NULL,
141 NULL, NULL, NULL, NULL,
142 NULL, NULL, NULL, NULL,
143 NULL, NULL, NULL, NULL,
144};
145
H. Peter Anvina9321a42012-09-26 13:18:43 -0700146static const char *cpuid_7_0_ebx_feature_name[] = {
Eduardo Habkost811a8ae2012-10-09 11:03:59 -0300147 "fsgsbase", NULL, NULL, "bmi1", "hle", "avx2", NULL, "smep",
148 "bmi2", "erms", "invpcid", "rtm", NULL, NULL, NULL, NULL,
Andre Przywarac8acc382012-11-14 16:28:52 -0200149 NULL, NULL, "rdseed", "adx", "smap", NULL, NULL, NULL,
H. Peter Anvina9321a42012-09-26 13:18:43 -0700150 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
151};
152
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200153typedef struct FeatureWordInfo {
154 const char **feat_names;
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200155 uint32_t cpuid_eax; /* Input EAX for CPUID */
156 int cpuid_reg; /* R_* register constant */
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200157} FeatureWordInfo;
158
159static FeatureWordInfo feature_word_info[FEATURE_WORDS] = {
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200160 [FEAT_1_EDX] = {
161 .feat_names = feature_name,
162 .cpuid_eax = 1, .cpuid_reg = R_EDX,
163 },
164 [FEAT_1_ECX] = {
165 .feat_names = ext_feature_name,
166 .cpuid_eax = 1, .cpuid_reg = R_ECX,
167 },
168 [FEAT_8000_0001_EDX] = {
169 .feat_names = ext2_feature_name,
170 .cpuid_eax = 0x80000001, .cpuid_reg = R_EDX,
171 },
172 [FEAT_8000_0001_ECX] = {
173 .feat_names = ext3_feature_name,
174 .cpuid_eax = 0x80000001, .cpuid_reg = R_ECX,
175 },
Eduardo Habkost89e49c82013-01-07 16:20:47 -0200176 [FEAT_C000_0001_EDX] = {
177 .feat_names = ext4_feature_name,
178 .cpuid_eax = 0xC0000001, .cpuid_reg = R_EDX,
179 },
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200180 [FEAT_KVM] = {
181 .feat_names = kvm_feature_name,
182 .cpuid_eax = KVM_CPUID_FEATURES, .cpuid_reg = R_EAX,
183 },
184 [FEAT_SVM] = {
185 .feat_names = svm_feature_name,
186 .cpuid_eax = 0x8000000A, .cpuid_reg = R_EDX,
187 },
188 [FEAT_7_0_EBX] = {
189 .feat_names = cpuid_7_0_ebx_feature_name,
190 .cpuid_eax = 7, .cpuid_reg = R_EBX,
191 },
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200192};
193
Eduardo Habkost8b4bedd2013-01-04 20:01:06 -0200194const char *get_register_name_32(unsigned int reg)
195{
196 static const char *reg_names[CPU_NB_REGS32] = {
197 [R_EAX] = "EAX",
198 [R_ECX] = "ECX",
199 [R_EDX] = "EDX",
200 [R_EBX] = "EBX",
201 [R_ESP] = "ESP",
202 [R_EBP] = "EBP",
203 [R_ESI] = "ESI",
204 [R_EDI] = "EDI",
205 };
206
207 if (reg > CPU_NB_REGS32) {
208 return NULL;
209 }
210 return reg_names[reg];
211}
212
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100213/* collects per-function cpuid data
214 */
215typedef struct model_features_t {
216 uint32_t *guest_feat;
217 uint32_t *host_feat;
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200218 FeatureWord feat_word;
Eduardo Habkost8b4bedd2013-01-04 20:01:06 -0200219} model_features_t;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100220
221int check_cpuid = 0;
222int enforce_cpuid = 0;
223
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200224static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
225 (1 << KVM_FEATURE_NOP_IO_DELAY) |
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200226 (1 << KVM_FEATURE_CLOCKSOURCE2) |
227 (1 << KVM_FEATURE_ASYNC_PF) |
228 (1 << KVM_FEATURE_STEAL_TIME) |
Eduardo Habkost29694752013-01-17 18:59:29 -0200229 (1 << KVM_FEATURE_PV_EOI) |
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200230 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200231
Eduardo Habkost29694752013-01-17 18:59:29 -0200232void disable_kvm_pv_eoi(void)
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200233{
Eduardo Habkost29694752013-01-17 18:59:29 -0200234 kvm_default_features &= ~(1UL << KVM_FEATURE_PV_EOI);
Michael S. Tsirkindc599442012-10-18 00:15:48 +0200235}
236
Jan Kiszkabb44e0d2011-01-21 21:48:07 +0100237void host_cpuid(uint32_t function, uint32_t count,
238 uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
Andre Przywarabdde4762010-03-11 14:38:58 +0100239{
240#if defined(CONFIG_KVM)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600241 uint32_t vec[4];
242
243#ifdef __x86_64__
244 asm volatile("cpuid"
245 : "=a"(vec[0]), "=b"(vec[1]),
246 "=c"(vec[2]), "=d"(vec[3])
247 : "0"(function), "c"(count) : "cc");
248#else
249 asm volatile("pusha \n\t"
250 "cpuid \n\t"
251 "mov %%eax, 0(%2) \n\t"
252 "mov %%ebx, 4(%2) \n\t"
253 "mov %%ecx, 8(%2) \n\t"
254 "mov %%edx, 12(%2) \n\t"
255 "popa"
256 : : "a"(function), "c"(count), "S"(vec)
257 : "memory", "cc");
258#endif
259
Andre Przywarabdde4762010-03-11 14:38:58 +0100260 if (eax)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600261 *eax = vec[0];
Andre Przywarabdde4762010-03-11 14:38:58 +0100262 if (ebx)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600263 *ebx = vec[1];
Andre Przywarabdde4762010-03-11 14:38:58 +0100264 if (ecx)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600265 *ecx = vec[2];
Andre Przywarabdde4762010-03-11 14:38:58 +0100266 if (edx)
Anthony Liguoria1fd24a2011-11-27 11:13:01 -0600267 *edx = vec[3];
Andre Przywarabdde4762010-03-11 14:38:58 +0100268#endif
269}
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100270
271#define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
272
273/* general substring compare of *[s1..e1) and *[s2..e2). sx is start of
274 * a substring. ex if !NULL points to the first char after a substring,
275 * otherwise the string is assumed to sized by a terminating nul.
276 * Return lexical ordering of *s1:*s2.
277 */
278static int sstrcmp(const char *s1, const char *e1, const char *s2,
279 const char *e2)
280{
281 for (;;) {
282 if (!*s1 || !*s2 || *s1 != *s2)
283 return (*s1 - *s2);
284 ++s1, ++s2;
285 if (s1 == e1 && s2 == e2)
286 return (0);
287 else if (s1 == e1)
288 return (*s2);
289 else if (s2 == e2)
290 return (*s1);
291 }
292}
293
294/* compare *[s..e) to *altstr. *altstr may be a simple string or multiple
295 * '|' delimited (possibly empty) strings in which case search for a match
296 * within the alternatives proceeds left to right. Return 0 for success,
297 * non-zero otherwise.
298 */
299static int altcmp(const char *s, const char *e, const char *altstr)
300{
301 const char *p, *q;
302
303 for (q = p = altstr; ; ) {
304 while (*p && *p != '|')
305 ++p;
306 if ((q == p && !*s) || (q != p && !sstrcmp(s, e, q, p)))
307 return (0);
308 if (!*p)
309 return (1);
310 else
311 q = ++p;
312 }
313}
314
315/* search featureset for flag *[s..e), if found set corresponding bit in
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200316 * *pval and return true, otherwise return false
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100317 */
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200318static bool lookup_feature(uint32_t *pval, const char *s, const char *e,
319 const char **featureset)
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100320{
321 uint32_t mask;
322 const char **ppc;
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200323 bool found = false;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100324
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200325 for (mask = 1, ppc = featureset; mask; mask <<= 1, ++ppc) {
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100326 if (*ppc && !altcmp(s, e, *ppc)) {
327 *pval |= mask;
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200328 found = true;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100329 }
Jan Kiszkae41e0fc2011-04-19 13:06:06 +0200330 }
331 return found;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100332}
333
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200334static void add_flagname_to_bitmaps(const char *flagname,
335 FeatureWordArray words)
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100336{
Eduardo Habkost5ef57872013-01-07 16:20:45 -0200337 FeatureWord w;
338 for (w = 0; w < FEATURE_WORDS; w++) {
339 FeatureWordInfo *wi = &feature_word_info[w];
340 if (wi->feat_names &&
341 lookup_feature(&words[w], flagname, NULL, wi->feat_names)) {
342 break;
343 }
344 }
345 if (w == FEATURE_WORDS) {
346 fprintf(stderr, "CPU feature %s not found\n", flagname);
347 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100348}
349
350typedef struct x86_def_t {
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100351 const char *name;
352 uint32_t level;
Igor Mammedov99b88a12013-01-21 15:06:36 +0100353 /* vendor is zero-terminated, 12 character ASCII string */
354 char vendor[CPUID_VENDOR_SZ + 1];
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100355 int family;
356 int model;
357 int stepping;
Joerg Roedel296acb62010-09-27 15:16:17 +0200358 uint32_t features, ext_features, ext2_features, ext3_features;
359 uint32_t kvm_features, svm_features;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100360 uint32_t xlevel;
361 char model_id[48];
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +0800362 /* Store the results of Centaur's CPUID instructions */
363 uint32_t ext4_features;
364 uint32_t xlevel2;
Eduardo Habkost13526722012-05-21 11:27:02 -0300365 /* The feature bits on CPUID[EAX=7,ECX=0].EBX */
366 uint32_t cpuid_7_0_ebx_features;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100367} x86_def_t;
368
369#define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
370#define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
371 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
372#define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
373 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
374 CPUID_PSE36 | CPUID_FXSR)
375#define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
376#define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
377 CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
378 CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
379 CPUID_PAE | CPUID_SEP | CPUID_APIC)
380
Andre Przywara551a2de2010-03-11 14:39:03 +0100381#define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
382 CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
383 CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
384 CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
385 CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100386 /* partly implemented:
387 CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
388 CPUID_PSE36 (needed for Solaris) */
389 /* missing:
390 CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
Aurelien Jarnoe71827b2013-03-31 12:58:30 +0200391#define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_PCLMULQDQ | \
392 CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | \
393 CPUID_EXT_SSE41 | CPUID_EXT_SSE42 | CPUID_EXT_POPCNT | \
Aurelien Jarnod6400452013-03-31 12:58:31 +0200394 CPUID_EXT_MOVBE | CPUID_EXT_AES | CPUID_EXT_HYPERVISOR)
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100395 /* missing:
Aurelien Jarnoe71827b2013-03-31 12:58:30 +0200396 CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_SMX,
397 CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_CID, CPUID_EXT_FMA,
398 CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_PCID, CPUID_EXT_DCA,
Aurelien Jarnod6400452013-03-31 12:58:31 +0200399 CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER, CPUID_EXT_XSAVE,
400 CPUID_EXT_OSXSAVE, CPUID_EXT_AVX, CPUID_EXT_F16C,
Aurelien Jarno83f7dc22013-03-26 19:56:02 +0100401 CPUID_EXT_RDRAND */
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000402#define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
Andre Przywara551a2de2010-03-11 14:39:03 +0100403 CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
404 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100405 /* missing:
406 CPUID_EXT2_PDPE1GB */
Andre Przywara551a2de2010-03-11 14:39:03 +0100407#define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
408 CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
Joerg Roedel296acb62010-09-27 15:16:17 +0200409#define TCG_SVM_FEATURES 0
Richard Henderson7073fba2013-01-23 16:17:10 -0800410#define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP \
Richard Hendersoncd7f97c2013-01-23 18:17:33 -0800411 CPUID_7_0_EBX_BMI1 | CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ADX)
Richard Henderson111994e2013-01-10 12:06:59 -0800412 /* missing:
Richard Henderson7073fba2013-01-23 16:17:10 -0800413 CPUID_7_0_EBX_FSGSBASE, CPUID_7_0_EBX_HLE, CPUID_7_0_EBX_AVX2,
414 CPUID_7_0_EBX_ERMS, CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM,
Richard Hendersoncd7f97c2013-01-23 18:17:33 -0800415 CPUID_7_0_EBX_RDSEED */
Andre Przywara551a2de2010-03-11 14:39:03 +0100416
Andreas Färber7fc9b712013-01-21 01:02:28 +0100417/* built-in CPU model definitions
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100418 */
419static x86_def_t builtin_x86_defs[] = {
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100420 {
421 .name = "qemu64",
422 .level = 4,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100423 .vendor = CPUID_VENDOR_AMD,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100424 .family = 6,
425 .model = 2,
426 .stepping = 3,
427 .features = PPRO_FEATURES |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100428 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100429 CPUID_PSE36,
430 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT,
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000431 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100432 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
433 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
434 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
435 .xlevel = 0x8000000A,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100436 },
437 {
438 .name = "phenom",
439 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100440 .vendor = CPUID_VENDOR_AMD,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100441 .family = 16,
442 .model = 2,
443 .stepping = 3,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100444 .features = PPRO_FEATURES |
445 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100446 CPUID_PSE36 | CPUID_VME | CPUID_HT,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100447 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_CX16 |
448 CPUID_EXT_POPCNT,
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000449 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100450 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
451 CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100452 CPUID_EXT2_FFXSR | CPUID_EXT2_PDPE1GB | CPUID_EXT2_RDTSCP,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100453 /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
454 CPUID_EXT3_CR8LEG,
455 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
456 CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
457 .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
458 CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
Joerg Roedel296acb62010-09-27 15:16:17 +0200459 .svm_features = CPUID_SVM_NPT | CPUID_SVM_LBRV,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100460 .xlevel = 0x8000001A,
461 .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
462 },
463 {
464 .name = "core2duo",
465 .level = 10,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100466 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100467 .family = 6,
468 .model = 15,
469 .stepping = 11,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100470 .features = PPRO_FEATURES |
471 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100472 CPUID_PSE36 | CPUID_VME | CPUID_DTS | CPUID_ACPI | CPUID_SS |
473 CPUID_HT | CPUID_TM | CPUID_PBE,
474 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
475 CPUID_EXT_DTES64 | CPUID_EXT_DSCPL | CPUID_EXT_VMX | CPUID_EXT_EST |
476 CPUID_EXT_TM2 | CPUID_EXT_CX16 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100477 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
478 .ext3_features = CPUID_EXT3_LAHF_LM,
479 .xlevel = 0x80000008,
480 .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz",
481 },
482 {
483 .name = "kvm64",
484 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100485 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100486 .family = 15,
487 .model = 6,
488 .stepping = 1,
489 /* Missing: CPUID_VME, CPUID_HT */
490 .features = PPRO_FEATURES |
491 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
492 CPUID_PSE36,
493 /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
494 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16,
495 /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000496 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100497 CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
498 /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
499 CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
500 CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
501 CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
502 .ext3_features = 0,
503 .xlevel = 0x80000008,
504 .model_id = "Common KVM processor"
505 },
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100506 {
507 .name = "qemu32",
508 .level = 4,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100509 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100510 .family = 6,
511 .model = 3,
512 .stepping = 3,
513 .features = PPRO_FEATURES,
514 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_POPCNT,
Andre Przywara58012d62010-03-11 14:39:06 +0100515 .xlevel = 0x80000004,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100516 },
517 {
Andre Przywaraeafaf1e2010-05-21 09:50:51 +0200518 .name = "kvm32",
519 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100520 .vendor = CPUID_VENDOR_INTEL,
Andre Przywaraeafaf1e2010-05-21 09:50:51 +0200521 .family = 15,
522 .model = 6,
523 .stepping = 1,
524 .features = PPRO_FEATURES |
525 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_PSE36,
526 .ext_features = CPUID_EXT_SSE3,
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000527 .ext2_features = PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES,
Andre Przywaraeafaf1e2010-05-21 09:50:51 +0200528 .ext3_features = 0,
529 .xlevel = 0x80000008,
530 .model_id = "Common 32-bit KVM processor"
531 },
532 {
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100533 .name = "coreduo",
534 .level = 10,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100535 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100536 .family = 6,
537 .model = 14,
538 .stepping = 8,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100539 .features = PPRO_FEATURES | CPUID_VME |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100540 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_DTS | CPUID_ACPI |
541 CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
542 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_VMX |
543 CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100544 .ext2_features = CPUID_EXT2_NX,
545 .xlevel = 0x80000008,
546 .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz",
547 },
548 {
549 .name = "486",
Andre Przywara58012d62010-03-11 14:39:06 +0100550 .level = 1,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100551 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100552 .family = 4,
553 .model = 0,
554 .stepping = 0,
555 .features = I486_FEATURES,
556 .xlevel = 0,
557 },
558 {
559 .name = "pentium",
560 .level = 1,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100561 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100562 .family = 5,
563 .model = 4,
564 .stepping = 3,
565 .features = PENTIUM_FEATURES,
566 .xlevel = 0,
567 },
568 {
569 .name = "pentium2",
570 .level = 2,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100571 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100572 .family = 6,
573 .model = 5,
574 .stepping = 2,
575 .features = PENTIUM2_FEATURES,
576 .xlevel = 0,
577 },
578 {
579 .name = "pentium3",
580 .level = 2,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100581 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100582 .family = 6,
583 .model = 7,
584 .stepping = 3,
585 .features = PENTIUM3_FEATURES,
586 .xlevel = 0,
587 },
588 {
589 .name = "athlon",
590 .level = 2,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100591 .vendor = CPUID_VENDOR_AMD,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100592 .family = 6,
593 .model = 2,
594 .stepping = 3,
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000595 .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR |
596 CPUID_MCA,
597 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
598 CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100599 .xlevel = 0x80000008,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100600 },
601 {
602 .name = "n270",
603 /* original is on level 10 */
604 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100605 .vendor = CPUID_VENDOR_INTEL,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100606 .family = 6,
607 .model = 28,
608 .stepping = 2,
609 .features = PPRO_FEATURES |
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100610 CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME | CPUID_DTS |
611 CPUID_ACPI | CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100612 /* Some CPUs got no CPUID_SEP */
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100613 .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
614 CPUID_EXT_DSCPL | CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR,
Eduardo Habkost60032ac2012-09-06 10:05:37 +0000615 .ext2_features = (PPRO_FEATURES & CPUID_EXT2_AMD_ALIASES) |
616 CPUID_EXT2_NX,
Aurelien Jarno8560efe2010-03-13 16:43:15 +0100617 .ext3_features = CPUID_EXT3_LAHF_LM,
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100618 .xlevel = 0x8000000A,
619 .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz",
620 },
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300621 {
622 .name = "Conroe",
623 .level = 2,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100624 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300625 .family = 6,
626 .model = 2,
627 .stepping = 3,
628 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
629 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
630 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
631 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
632 CPUID_DE | CPUID_FP87,
633 .ext_features = CPUID_EXT_SSSE3 | CPUID_EXT_SSE3,
634 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_NX | CPUID_EXT2_SYSCALL,
635 .ext3_features = CPUID_EXT3_LAHF_LM,
636 .xlevel = 0x8000000A,
637 .model_id = "Intel Celeron_4x0 (Conroe/Merom Class Core 2)",
638 },
639 {
640 .name = "Penryn",
641 .level = 2,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100642 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300643 .family = 6,
644 .model = 2,
645 .stepping = 3,
646 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
647 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
648 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
649 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
650 CPUID_DE | CPUID_FP87,
651 .ext_features = CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
652 CPUID_EXT_SSE3,
653 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_NX | CPUID_EXT2_SYSCALL,
654 .ext3_features = CPUID_EXT3_LAHF_LM,
655 .xlevel = 0x8000000A,
656 .model_id = "Intel Core 2 Duo P9xxx (Penryn Class Core 2)",
657 },
658 {
659 .name = "Nehalem",
660 .level = 2,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100661 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300662 .family = 6,
663 .model = 2,
664 .stepping = 3,
665 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
666 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
667 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
668 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
669 CPUID_DE | CPUID_FP87,
670 .ext_features = CPUID_EXT_POPCNT | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
671 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_SSE3,
672 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
673 .ext3_features = CPUID_EXT3_LAHF_LM,
674 .xlevel = 0x8000000A,
675 .model_id = "Intel Core i7 9xx (Nehalem Class Core i7)",
676 },
677 {
678 .name = "Westmere",
679 .level = 11,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100680 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300681 .family = 6,
682 .model = 44,
683 .stepping = 1,
684 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
685 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
686 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
687 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
688 CPUID_DE | CPUID_FP87,
689 .ext_features = CPUID_EXT_AES | CPUID_EXT_POPCNT | CPUID_EXT_SSE42 |
690 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
Aurelien Jarno41cb3832013-03-31 12:58:30 +0200691 CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300692 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
693 .ext3_features = CPUID_EXT3_LAHF_LM,
694 .xlevel = 0x8000000A,
695 .model_id = "Westmere E56xx/L56xx/X56xx (Nehalem-C)",
696 },
697 {
698 .name = "SandyBridge",
699 .level = 0xd,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100700 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300701 .family = 6,
702 .model = 42,
703 .stepping = 1,
704 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
705 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
706 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
707 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
708 CPUID_DE | CPUID_FP87,
709 .ext_features = CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
710 CPUID_EXT_TSC_DEADLINE_TIMER | CPUID_EXT_POPCNT |
711 CPUID_EXT_X2APIC | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
712 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ |
713 CPUID_EXT_SSE3,
714 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_NX |
715 CPUID_EXT2_SYSCALL,
716 .ext3_features = CPUID_EXT3_LAHF_LM,
717 .xlevel = 0x8000000A,
718 .model_id = "Intel Xeon E312xx (Sandy Bridge)",
719 },
720 {
Eduardo Habkost37507092012-11-14 16:28:54 -0200721 .name = "Haswell",
722 .level = 0xd,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100723 .vendor = CPUID_VENDOR_INTEL,
Eduardo Habkost37507092012-11-14 16:28:54 -0200724 .family = 6,
725 .model = 60,
726 .stepping = 1,
727 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
728 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
Eduardo Habkost80ae4162012-11-22 13:31:03 -0200729 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
Eduardo Habkost37507092012-11-14 16:28:54 -0200730 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
731 CPUID_DE | CPUID_FP87,
732 .ext_features = CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
733 CPUID_EXT_POPCNT | CPUID_EXT_X2APIC | CPUID_EXT_SSE42 |
734 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_SSSE3 |
735 CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3 |
736 CPUID_EXT_TSC_DEADLINE_TIMER | CPUID_EXT_FMA | CPUID_EXT_MOVBE |
737 CPUID_EXT_PCID,
Eduardo Habkost80ae4162012-11-22 13:31:03 -0200738 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_NX |
739 CPUID_EXT2_SYSCALL,
Eduardo Habkost37507092012-11-14 16:28:54 -0200740 .ext3_features = CPUID_EXT3_LAHF_LM,
741 .cpuid_7_0_ebx_features = CPUID_7_0_EBX_FSGSBASE | CPUID_7_0_EBX_BMI1 |
742 CPUID_7_0_EBX_HLE | CPUID_7_0_EBX_AVX2 | CPUID_7_0_EBX_SMEP |
743 CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_INVPCID |
744 CPUID_7_0_EBX_RTM,
745 .xlevel = 0x8000000A,
746 .model_id = "Intel Core Processor (Haswell)",
747 },
748 {
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300749 .name = "Opteron_G1",
750 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100751 .vendor = CPUID_VENDOR_AMD,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300752 .family = 15,
753 .model = 6,
754 .stepping = 1,
755 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
756 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
757 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
758 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
759 CPUID_DE | CPUID_FP87,
760 .ext_features = CPUID_EXT_SSE3,
761 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
762 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
763 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
764 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
765 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
766 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
767 .xlevel = 0x80000008,
768 .model_id = "AMD Opteron 240 (Gen 1 Class Opteron)",
769 },
770 {
771 .name = "Opteron_G2",
772 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100773 .vendor = CPUID_VENDOR_AMD,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300774 .family = 15,
775 .model = 6,
776 .stepping = 1,
777 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
778 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
779 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
780 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
781 CPUID_DE | CPUID_FP87,
782 .ext_features = CPUID_EXT_CX16 | CPUID_EXT_SSE3,
783 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_FXSR |
784 CPUID_EXT2_MMX | CPUID_EXT2_NX | CPUID_EXT2_PSE36 |
785 CPUID_EXT2_PAT | CPUID_EXT2_CMOV | CPUID_EXT2_MCA |
786 CPUID_EXT2_PGE | CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL |
787 CPUID_EXT2_APIC | CPUID_EXT2_CX8 | CPUID_EXT2_MCE |
788 CPUID_EXT2_PAE | CPUID_EXT2_MSR | CPUID_EXT2_TSC | CPUID_EXT2_PSE |
789 CPUID_EXT2_DE | CPUID_EXT2_FPU,
790 .ext3_features = CPUID_EXT3_SVM | CPUID_EXT3_LAHF_LM,
791 .xlevel = 0x80000008,
792 .model_id = "AMD Opteron 22xx (Gen 2 Class Opteron)",
793 },
794 {
795 .name = "Opteron_G3",
796 .level = 5,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100797 .vendor = CPUID_VENDOR_AMD,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300798 .family = 15,
799 .model = 6,
800 .stepping = 1,
801 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
802 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
803 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
804 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
805 CPUID_DE | CPUID_FP87,
806 .ext_features = CPUID_EXT_POPCNT | CPUID_EXT_CX16 | CPUID_EXT_MONITOR |
807 CPUID_EXT_SSE3,
808 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP | CPUID_EXT2_FXSR |
809 CPUID_EXT2_MMX | CPUID_EXT2_NX | CPUID_EXT2_PSE36 |
810 CPUID_EXT2_PAT | CPUID_EXT2_CMOV | CPUID_EXT2_MCA |
811 CPUID_EXT2_PGE | CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL |
812 CPUID_EXT2_APIC | CPUID_EXT2_CX8 | CPUID_EXT2_MCE |
813 CPUID_EXT2_PAE | CPUID_EXT2_MSR | CPUID_EXT2_TSC | CPUID_EXT2_PSE |
814 CPUID_EXT2_DE | CPUID_EXT2_FPU,
815 .ext3_features = CPUID_EXT3_MISALIGNSSE | CPUID_EXT3_SSE4A |
816 CPUID_EXT3_ABM | CPUID_EXT3_SVM | CPUID_EXT3_LAHF_LM,
817 .xlevel = 0x80000008,
818 .model_id = "AMD Opteron 23xx (Gen 3 Class Opteron)",
819 },
820 {
821 .name = "Opteron_G4",
822 .level = 0xd,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100823 .vendor = CPUID_VENDOR_AMD,
Eduardo Habkost3eca4642012-09-05 17:41:10 -0300824 .family = 21,
825 .model = 1,
826 .stepping = 2,
827 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
828 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
829 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
830 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
831 CPUID_DE | CPUID_FP87,
832 .ext_features = CPUID_EXT_AVX | CPUID_EXT_XSAVE | CPUID_EXT_AES |
833 CPUID_EXT_POPCNT | CPUID_EXT_SSE42 | CPUID_EXT_SSE41 |
834 CPUID_EXT_CX16 | CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ |
835 CPUID_EXT_SSE3,
836 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP |
837 CPUID_EXT2_PDPE1GB | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
838 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
839 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
840 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
841 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
842 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
843 .ext3_features = CPUID_EXT3_FMA4 | CPUID_EXT3_XOP |
844 CPUID_EXT3_3DNOWPREFETCH | CPUID_EXT3_MISALIGNSSE |
845 CPUID_EXT3_SSE4A | CPUID_EXT3_ABM | CPUID_EXT3_SVM |
846 CPUID_EXT3_LAHF_LM,
847 .xlevel = 0x8000001A,
848 .model_id = "AMD Opteron 62xx class CPU",
849 },
Andre Przywara021941b2012-11-14 16:28:53 -0200850 {
851 .name = "Opteron_G5",
852 .level = 0xd,
Igor Mammedov99b88a12013-01-21 15:06:36 +0100853 .vendor = CPUID_VENDOR_AMD,
Andre Przywara021941b2012-11-14 16:28:53 -0200854 .family = 21,
855 .model = 2,
856 .stepping = 0,
857 .features = CPUID_SSE2 | CPUID_SSE | CPUID_FXSR | CPUID_MMX |
858 CPUID_CLFLUSH | CPUID_PSE36 | CPUID_PAT | CPUID_CMOV | CPUID_MCA |
859 CPUID_PGE | CPUID_MTRR | CPUID_SEP | CPUID_APIC | CPUID_CX8 |
860 CPUID_MCE | CPUID_PAE | CPUID_MSR | CPUID_TSC | CPUID_PSE |
861 CPUID_DE | CPUID_FP87,
862 .ext_features = CPUID_EXT_F16C | CPUID_EXT_AVX | CPUID_EXT_XSAVE |
863 CPUID_EXT_AES | CPUID_EXT_POPCNT | CPUID_EXT_SSE42 |
864 CPUID_EXT_SSE41 | CPUID_EXT_CX16 | CPUID_EXT_FMA |
865 CPUID_EXT_SSSE3 | CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3,
866 .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_RDTSCP |
867 CPUID_EXT2_PDPE1GB | CPUID_EXT2_FXSR | CPUID_EXT2_MMX |
868 CPUID_EXT2_NX | CPUID_EXT2_PSE36 | CPUID_EXT2_PAT |
869 CPUID_EXT2_CMOV | CPUID_EXT2_MCA | CPUID_EXT2_PGE |
870 CPUID_EXT2_MTRR | CPUID_EXT2_SYSCALL | CPUID_EXT2_APIC |
871 CPUID_EXT2_CX8 | CPUID_EXT2_MCE | CPUID_EXT2_PAE | CPUID_EXT2_MSR |
872 CPUID_EXT2_TSC | CPUID_EXT2_PSE | CPUID_EXT2_DE | CPUID_EXT2_FPU,
873 .ext3_features = CPUID_EXT3_TBM | CPUID_EXT3_FMA4 | CPUID_EXT3_XOP |
874 CPUID_EXT3_3DNOWPREFETCH | CPUID_EXT3_MISALIGNSSE |
875 CPUID_EXT3_SSE4A | CPUID_EXT3_ABM | CPUID_EXT3_SVM |
876 CPUID_EXT3_LAHF_LM,
877 .xlevel = 0x8000001A,
878 .model_id = "AMD Opteron 63xx class CPU",
879 },
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100880};
881
Eduardo Habkoste4ab0d62012-11-02 14:25:15 -0200882#ifdef CONFIG_KVM
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100883static int cpu_x86_fill_model_id(char *str)
884{
885 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
886 int i;
887
888 for (i = 0; i < 3; i++) {
889 host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx);
890 memcpy(str + i * 16 + 0, &eax, 4);
891 memcpy(str + i * 16 + 4, &ebx, 4);
892 memcpy(str + i * 16 + 8, &ecx, 4);
893 memcpy(str + i * 16 + 12, &edx, 4);
894 }
895 return 0;
896}
Eduardo Habkoste4ab0d62012-11-02 14:25:15 -0200897#endif
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100898
Eduardo Habkost6e746f32012-10-24 19:44:06 -0200899/* Fill a x86_def_t struct with information about the host CPU, and
900 * the CPU features supported by the host hardware + host kernel
901 *
902 * This function may be called only if KVM is enabled.
903 */
904static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100905{
Eduardo Habkoste4ab0d62012-11-02 14:25:15 -0200906#ifdef CONFIG_KVM
Eduardo Habkost12869992012-10-24 19:44:07 -0200907 KVMState *s = kvm_state;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100908 uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
909
Eduardo Habkost6e746f32012-10-24 19:44:06 -0200910 assert(kvm_enabled());
911
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100912 x86_cpu_def->name = "host";
913 host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
Igor Mammedov99b88a12013-01-21 15:06:36 +0100914 x86_cpu_vendor_words2str(x86_cpu_def->vendor, ebx, edx, ecx);
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100915
916 host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
917 x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
918 x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
919 x86_cpu_def->stepping = eax & 0x0F;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100920
Eduardo Habkost12869992012-10-24 19:44:07 -0200921 x86_cpu_def->level = kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX);
922 x86_cpu_def->features = kvm_arch_get_supported_cpuid(s, 0x1, 0, R_EDX);
923 x86_cpu_def->ext_features = kvm_arch_get_supported_cpuid(s, 0x1, 0, R_ECX);
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100924
Eduardo Habkost6e746f32012-10-24 19:44:06 -0200925 if (x86_cpu_def->level >= 7) {
Eduardo Habkost12869992012-10-24 19:44:07 -0200926 x86_cpu_def->cpuid_7_0_ebx_features =
927 kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX);
Eduardo Habkost13526722012-05-21 11:27:02 -0300928 } else {
929 x86_cpu_def->cpuid_7_0_ebx_features = 0;
930 }
931
Eduardo Habkost12869992012-10-24 19:44:07 -0200932 x86_cpu_def->xlevel = kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX);
933 x86_cpu_def->ext2_features =
934 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX);
935 x86_cpu_def->ext3_features =
936 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX);
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100937
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100938 cpu_x86_fill_model_id(x86_cpu_def->model_id);
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100939
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +0800940 /* Call Centaur's CPUID instruction. */
Igor Mammedov99b88a12013-01-21 15:06:36 +0100941 if (!strcmp(x86_cpu_def->vendor, CPUID_VENDOR_VIA)) {
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +0800942 host_cpuid(0xC0000000, 0, &eax, &ebx, &ecx, &edx);
Eduardo Habkost12869992012-10-24 19:44:07 -0200943 eax = kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +0800944 if (eax >= 0xC0000001) {
945 /* Support VIA max extended level */
946 x86_cpu_def->xlevel2 = eax;
947 host_cpuid(0xC0000001, 0, &eax, &ebx, &ecx, &edx);
Eduardo Habkost12869992012-10-24 19:44:07 -0200948 x86_cpu_def->ext4_features =
949 kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +0800950 }
951 }
Joerg Roedel296acb62010-09-27 15:16:17 +0200952
Eduardo Habkostfcb93c02013-01-04 20:01:04 -0200953 /* Other KVM-specific feature fields: */
954 x86_cpu_def->svm_features =
955 kvm_arch_get_supported_cpuid(s, 0x8000000A, 0, R_EDX);
Eduardo Habkostbd004be2013-01-04 20:01:05 -0200956 x86_cpu_def->kvm_features =
957 kvm_arch_get_supported_cpuid(s, KVM_CPUID_FEATURES, 0, R_EAX);
Eduardo Habkostfcb93c02013-01-04 20:01:04 -0200958
Eduardo Habkoste4ab0d62012-11-02 14:25:15 -0200959#endif /* CONFIG_KVM */
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100960}
961
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200962static int unavailable_host_feature(FeatureWordInfo *f, uint32_t mask)
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100963{
964 int i;
965
966 for (i = 0; i < 32; ++i)
967 if (1 << i & mask) {
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200968 const char *reg = get_register_name_32(f->cpuid_reg);
Eduardo Habkost8b4bedd2013-01-04 20:01:06 -0200969 assert(reg);
970 fprintf(stderr, "warning: host doesn't support requested feature: "
971 "CPUID.%02XH:%s%s%s [bit %d]\n",
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200972 f->cpuid_eax, reg,
973 f->feat_names[i] ? "." : "",
974 f->feat_names[i] ? f->feat_names[i] : "", i);
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100975 break;
976 }
977 return 0;
978}
979
Eduardo Habkost07ca5942013-01-07 16:20:48 -0200980/* Check if all requested cpu flags are making their way to the guest
981 *
982 * Returns 0 if all flags are supported by the host, non-zero otherwise.
Eduardo Habkost6e746f32012-10-24 19:44:06 -0200983 *
984 * This function may be called only if KVM is enabled.
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100985 */
Igor Mammedov5ec01c22013-01-11 03:10:17 +0100986static int kvm_check_features_against_host(X86CPU *cpu)
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100987{
Igor Mammedov5ec01c22013-01-11 03:10:17 +0100988 CPUX86State *env = &cpu->env;
Andre Przywarac6dc6f62010-03-11 14:38:55 +0100989 x86_def_t host_def;
990 uint32_t mask;
991 int rv, i;
992 struct model_features_t ft[] = {
Igor Mammedov5ec01c22013-01-11 03:10:17 +0100993 {&env->cpuid_features, &host_def.features,
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200994 FEAT_1_EDX },
Igor Mammedov5ec01c22013-01-11 03:10:17 +0100995 {&env->cpuid_ext_features, &host_def.ext_features,
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200996 FEAT_1_ECX },
Igor Mammedov5ec01c22013-01-11 03:10:17 +0100997 {&env->cpuid_ext2_features, &host_def.ext2_features,
Eduardo Habkostbffd67b2013-01-07 16:20:46 -0200998 FEAT_8000_0001_EDX },
Igor Mammedov5ec01c22013-01-11 03:10:17 +0100999 {&env->cpuid_ext3_features, &host_def.ext3_features,
Eduardo Habkostbffd67b2013-01-07 16:20:46 -02001000 FEAT_8000_0001_ECX },
Igor Mammedov5ec01c22013-01-11 03:10:17 +01001001 {&env->cpuid_ext4_features, &host_def.ext4_features,
Eduardo Habkost07ca5942013-01-07 16:20:48 -02001002 FEAT_C000_0001_EDX },
Igor Mammedov5ec01c22013-01-11 03:10:17 +01001003 {&env->cpuid_7_0_ebx_features, &host_def.cpuid_7_0_ebx_features,
Eduardo Habkost07ca5942013-01-07 16:20:48 -02001004 FEAT_7_0_EBX },
Igor Mammedov5ec01c22013-01-11 03:10:17 +01001005 {&env->cpuid_svm_features, &host_def.svm_features,
Eduardo Habkost07ca5942013-01-07 16:20:48 -02001006 FEAT_SVM },
Igor Mammedov5ec01c22013-01-11 03:10:17 +01001007 {&env->cpuid_kvm_features, &host_def.kvm_features,
Eduardo Habkost07ca5942013-01-07 16:20:48 -02001008 FEAT_KVM },
Eduardo Habkost8b4bedd2013-01-04 20:01:06 -02001009 };
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001010
Eduardo Habkost6e746f32012-10-24 19:44:06 -02001011 assert(kvm_enabled());
1012
1013 kvm_cpu_fill_host(&host_def);
Eduardo Habkostbffd67b2013-01-07 16:20:46 -02001014 for (rv = 0, i = 0; i < ARRAY_SIZE(ft); ++i) {
1015 FeatureWord w = ft[i].feat_word;
1016 FeatureWordInfo *wi = &feature_word_info[w];
1017 for (mask = 1; mask; mask <<= 1) {
Eduardo Habkoste8beac02013-01-04 20:01:10 -02001018 if (*ft[i].guest_feat & mask &&
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001019 !(*ft[i].host_feat & mask)) {
Eduardo Habkostbffd67b2013-01-07 16:20:46 -02001020 unavailable_host_feature(wi, mask);
1021 rv = 1;
1022 }
1023 }
1024 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001025 return rv;
1026}
1027
Andreas Färber95b85192012-04-17 14:42:22 +02001028static void x86_cpuid_version_get_family(Object *obj, Visitor *v, void *opaque,
1029 const char *name, Error **errp)
1030{
1031 X86CPU *cpu = X86_CPU(obj);
1032 CPUX86State *env = &cpu->env;
1033 int64_t value;
1034
1035 value = (env->cpuid_version >> 8) & 0xf;
1036 if (value == 0xf) {
1037 value += (env->cpuid_version >> 20) & 0xff;
1038 }
1039 visit_type_int(v, &value, name, errp);
1040}
1041
Andreas Färber71ad61d2012-04-17 12:10:29 +02001042static void x86_cpuid_version_set_family(Object *obj, Visitor *v, void *opaque,
1043 const char *name, Error **errp)
Andreas Färbered5e1ec2012-02-17 17:46:01 +01001044{
Andreas Färber71ad61d2012-04-17 12:10:29 +02001045 X86CPU *cpu = X86_CPU(obj);
1046 CPUX86State *env = &cpu->env;
1047 const int64_t min = 0;
1048 const int64_t max = 0xff + 0xf;
1049 int64_t value;
1050
1051 visit_type_int(v, &value, name, errp);
1052 if (error_is_set(errp)) {
1053 return;
1054 }
1055 if (value < min || value > max) {
1056 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1057 name ? name : "null", value, min, max);
1058 return;
1059 }
1060
Andreas Färbered5e1ec2012-02-17 17:46:01 +01001061 env->cpuid_version &= ~0xff00f00;
Andreas Färber71ad61d2012-04-17 12:10:29 +02001062 if (value > 0x0f) {
1063 env->cpuid_version |= 0xf00 | ((value - 0x0f) << 20);
Andreas Färbered5e1ec2012-02-17 17:46:01 +01001064 } else {
Andreas Färber71ad61d2012-04-17 12:10:29 +02001065 env->cpuid_version |= value << 8;
Andreas Färbered5e1ec2012-02-17 17:46:01 +01001066 }
1067}
1068
Andreas Färber67e30c82012-04-17 14:48:14 +02001069static void x86_cpuid_version_get_model(Object *obj, Visitor *v, void *opaque,
1070 const char *name, Error **errp)
1071{
1072 X86CPU *cpu = X86_CPU(obj);
1073 CPUX86State *env = &cpu->env;
1074 int64_t value;
1075
1076 value = (env->cpuid_version >> 4) & 0xf;
1077 value |= ((env->cpuid_version >> 16) & 0xf) << 4;
1078 visit_type_int(v, &value, name, errp);
1079}
1080
Andreas Färberc5291a42012-04-17 12:16:39 +02001081static void x86_cpuid_version_set_model(Object *obj, Visitor *v, void *opaque,
1082 const char *name, Error **errp)
Andreas Färberb0704cb2012-02-17 17:46:02 +01001083{
Andreas Färberc5291a42012-04-17 12:16:39 +02001084 X86CPU *cpu = X86_CPU(obj);
1085 CPUX86State *env = &cpu->env;
1086 const int64_t min = 0;
1087 const int64_t max = 0xff;
1088 int64_t value;
1089
1090 visit_type_int(v, &value, name, errp);
1091 if (error_is_set(errp)) {
1092 return;
1093 }
1094 if (value < min || value > max) {
1095 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1096 name ? name : "null", value, min, max);
1097 return;
1098 }
1099
Andreas Färberb0704cb2012-02-17 17:46:02 +01001100 env->cpuid_version &= ~0xf00f0;
Andreas Färberc5291a42012-04-17 12:16:39 +02001101 env->cpuid_version |= ((value & 0xf) << 4) | ((value >> 4) << 16);
Andreas Färberb0704cb2012-02-17 17:46:02 +01001102}
1103
Andreas Färber35112e42012-04-17 14:50:53 +02001104static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v,
1105 void *opaque, const char *name,
1106 Error **errp)
1107{
1108 X86CPU *cpu = X86_CPU(obj);
1109 CPUX86State *env = &cpu->env;
1110 int64_t value;
1111
1112 value = env->cpuid_version & 0xf;
1113 visit_type_int(v, &value, name, errp);
1114}
1115
Andreas Färber036e2222012-04-17 14:14:18 +02001116static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
1117 void *opaque, const char *name,
1118 Error **errp)
Andreas Färber38c3dc42012-02-17 17:46:03 +01001119{
Andreas Färber036e2222012-04-17 14:14:18 +02001120 X86CPU *cpu = X86_CPU(obj);
1121 CPUX86State *env = &cpu->env;
1122 const int64_t min = 0;
1123 const int64_t max = 0xf;
1124 int64_t value;
1125
1126 visit_type_int(v, &value, name, errp);
1127 if (error_is_set(errp)) {
1128 return;
1129 }
1130 if (value < min || value > max) {
1131 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1132 name ? name : "null", value, min, max);
1133 return;
1134 }
1135
Andreas Färber38c3dc42012-02-17 17:46:03 +01001136 env->cpuid_version &= ~0xf;
Andreas Färber036e2222012-04-17 14:14:18 +02001137 env->cpuid_version |= value & 0xf;
Andreas Färber38c3dc42012-02-17 17:46:03 +01001138}
1139
Andreas Färber8e1898b2012-04-17 18:41:40 +02001140static void x86_cpuid_get_level(Object *obj, Visitor *v, void *opaque,
1141 const char *name, Error **errp)
1142{
1143 X86CPU *cpu = X86_CPU(obj);
Andreas Färber8e1898b2012-04-17 18:41:40 +02001144
Andreas Färberfa029882012-05-01 23:33:13 +02001145 visit_type_uint32(v, &cpu->env.cpuid_level, name, errp);
Andreas Färber8e1898b2012-04-17 18:41:40 +02001146}
1147
1148static void x86_cpuid_set_level(Object *obj, Visitor *v, void *opaque,
1149 const char *name, Error **errp)
1150{
1151 X86CPU *cpu = X86_CPU(obj);
Andreas Färber8e1898b2012-04-17 18:41:40 +02001152
Andreas Färberfa029882012-05-01 23:33:13 +02001153 visit_type_uint32(v, &cpu->env.cpuid_level, name, errp);
Andreas Färber8e1898b2012-04-17 18:41:40 +02001154}
1155
Andreas Färber16b93aa2012-04-17 18:44:07 +02001156static void x86_cpuid_get_xlevel(Object *obj, Visitor *v, void *opaque,
1157 const char *name, Error **errp)
1158{
1159 X86CPU *cpu = X86_CPU(obj);
Andreas Färber16b93aa2012-04-17 18:44:07 +02001160
Andreas Färberfa029882012-05-01 23:33:13 +02001161 visit_type_uint32(v, &cpu->env.cpuid_xlevel, name, errp);
Andreas Färber16b93aa2012-04-17 18:44:07 +02001162}
1163
1164static void x86_cpuid_set_xlevel(Object *obj, Visitor *v, void *opaque,
1165 const char *name, Error **errp)
1166{
1167 X86CPU *cpu = X86_CPU(obj);
Andreas Färber16b93aa2012-04-17 18:44:07 +02001168
Andreas Färberfa029882012-05-01 23:33:13 +02001169 visit_type_uint32(v, &cpu->env.cpuid_xlevel, name, errp);
Andreas Färber16b93aa2012-04-17 18:44:07 +02001170}
1171
Andreas Färberd480e1a2012-04-17 19:22:58 +02001172static char *x86_cpuid_get_vendor(Object *obj, Error **errp)
1173{
1174 X86CPU *cpu = X86_CPU(obj);
1175 CPUX86State *env = &cpu->env;
1176 char *value;
Andreas Färberd480e1a2012-04-17 19:22:58 +02001177
Igor Mammedov9df694e2012-10-22 17:03:10 +02001178 value = (char *)g_malloc(CPUID_VENDOR_SZ + 1);
Igor Mammedov99b88a12013-01-21 15:06:36 +01001179 x86_cpu_vendor_words2str(value, env->cpuid_vendor1, env->cpuid_vendor2,
1180 env->cpuid_vendor3);
Andreas Färberd480e1a2012-04-17 19:22:58 +02001181 return value;
1182}
1183
1184static void x86_cpuid_set_vendor(Object *obj, const char *value,
1185 Error **errp)
1186{
1187 X86CPU *cpu = X86_CPU(obj);
1188 CPUX86State *env = &cpu->env;
1189 int i;
1190
Igor Mammedov9df694e2012-10-22 17:03:10 +02001191 if (strlen(value) != CPUID_VENDOR_SZ) {
Andreas Färberd480e1a2012-04-17 19:22:58 +02001192 error_set(errp, QERR_PROPERTY_VALUE_BAD, "",
1193 "vendor", value);
1194 return;
1195 }
1196
1197 env->cpuid_vendor1 = 0;
1198 env->cpuid_vendor2 = 0;
1199 env->cpuid_vendor3 = 0;
1200 for (i = 0; i < 4; i++) {
1201 env->cpuid_vendor1 |= ((uint8_t)value[i ]) << (8 * i);
1202 env->cpuid_vendor2 |= ((uint8_t)value[i + 4]) << (8 * i);
1203 env->cpuid_vendor3 |= ((uint8_t)value[i + 8]) << (8 * i);
1204 }
Andreas Färberd480e1a2012-04-17 19:22:58 +02001205}
1206
Andreas Färber63e886e2012-04-17 23:02:26 +02001207static char *x86_cpuid_get_model_id(Object *obj, Error **errp)
1208{
1209 X86CPU *cpu = X86_CPU(obj);
1210 CPUX86State *env = &cpu->env;
1211 char *value;
1212 int i;
1213
1214 value = g_malloc(48 + 1);
1215 for (i = 0; i < 48; i++) {
1216 value[i] = env->cpuid_model[i >> 2] >> (8 * (i & 3));
1217 }
1218 value[48] = '\0';
1219 return value;
1220}
1221
Andreas Färber938d4c22012-04-17 15:17:27 +02001222static void x86_cpuid_set_model_id(Object *obj, const char *model_id,
1223 Error **errp)
Andreas Färberdcce6672012-02-17 17:46:04 +01001224{
Andreas Färber938d4c22012-04-17 15:17:27 +02001225 X86CPU *cpu = X86_CPU(obj);
1226 CPUX86State *env = &cpu->env;
Andreas Färberdcce6672012-02-17 17:46:04 +01001227 int c, len, i;
1228
1229 if (model_id == NULL) {
1230 model_id = "";
1231 }
1232 len = strlen(model_id);
Andreas Färberd0a6acf2012-04-17 18:21:52 +02001233 memset(env->cpuid_model, 0, 48);
Andreas Färberdcce6672012-02-17 17:46:04 +01001234 for (i = 0; i < 48; i++) {
1235 if (i >= len) {
1236 c = '\0';
1237 } else {
1238 c = (uint8_t)model_id[i];
1239 }
1240 env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
1241 }
1242}
1243
Andreas Färber89e48962012-04-18 00:12:23 +02001244static void x86_cpuid_get_tsc_freq(Object *obj, Visitor *v, void *opaque,
1245 const char *name, Error **errp)
1246{
1247 X86CPU *cpu = X86_CPU(obj);
1248 int64_t value;
1249
1250 value = cpu->env.tsc_khz * 1000;
1251 visit_type_int(v, &value, name, errp);
1252}
1253
1254static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,
1255 const char *name, Error **errp)
1256{
1257 X86CPU *cpu = X86_CPU(obj);
1258 const int64_t min = 0;
Don Slutz2e848492012-09-21 20:13:13 -04001259 const int64_t max = INT64_MAX;
Andreas Färber89e48962012-04-18 00:12:23 +02001260 int64_t value;
1261
1262 visit_type_int(v, &value, name, errp);
1263 if (error_is_set(errp)) {
1264 return;
1265 }
1266 if (value < min || value > max) {
1267 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
1268 name ? name : "null", value, min, max);
1269 return;
1270 }
1271
1272 cpu->env.tsc_khz = value / 1000;
1273}
1274
Igor Mammedov31050932013-04-25 16:05:26 +02001275static void x86_cpuid_get_apic_id(Object *obj, Visitor *v, void *opaque,
1276 const char *name, Error **errp)
1277{
1278 X86CPU *cpu = X86_CPU(obj);
1279 int64_t value = cpu->env.cpuid_apic_id;
1280
1281 visit_type_int(v, &value, name, errp);
1282}
1283
1284static void x86_cpuid_set_apic_id(Object *obj, Visitor *v, void *opaque,
1285 const char *name, Error **errp)
1286{
1287 X86CPU *cpu = X86_CPU(obj);
Igor Mammedov8d6d4982013-04-26 19:51:52 +02001288 DeviceState *dev = DEVICE(obj);
Igor Mammedov31050932013-04-25 16:05:26 +02001289 const int64_t min = 0;
1290 const int64_t max = UINT32_MAX;
1291 Error *error = NULL;
1292 int64_t value;
1293
Igor Mammedov8d6d4982013-04-26 19:51:52 +02001294 if (dev->realized) {
1295 error_setg(errp, "Attempt to set property '%s' on '%s' after "
1296 "it was realized", name, object_get_typename(obj));
1297 return;
1298 }
1299
Igor Mammedov31050932013-04-25 16:05:26 +02001300 visit_type_int(v, &value, name, &error);
1301 if (error) {
1302 error_propagate(errp, error);
1303 return;
1304 }
1305 if (value < min || value > max) {
1306 error_setg(errp, "Property %s.%s doesn't take value %" PRId64
1307 " (minimum: %" PRId64 ", maximum: %" PRId64 ")" ,
1308 object_get_typename(obj), name, value, min, max);
1309 return;
1310 }
1311
1312 if ((value != cpu->env.cpuid_apic_id) && cpu_exists(value)) {
1313 error_setg(errp, "CPU with APIC ID %" PRIi64 " exists", value);
1314 return;
1315 }
1316 cpu->env.cpuid_apic_id = value;
1317}
1318
Eduardo Habkost8f961352012-12-04 17:34:39 -02001319static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *name)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001320{
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001321 x86_def_t *def;
Andreas Färber7fc9b712013-01-21 01:02:28 +01001322 int i;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001323
Andreas Färber4bfe9102013-01-21 01:00:24 +01001324 if (name == NULL) {
Eduardo Habkost8f961352012-12-04 17:34:39 -02001325 return -1;
Andreas Färber4bfe9102013-01-21 01:00:24 +01001326 }
1327 if (kvm_enabled() && strcmp(name, "host") == 0) {
1328 kvm_cpu_fill_host(x86_cpu_def);
1329 return 0;
Eduardo Habkost8f961352012-12-04 17:34:39 -02001330 }
1331
Andreas Färber7fc9b712013-01-21 01:02:28 +01001332 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
1333 def = &builtin_x86_defs[i];
Andreas Färber4bfe9102013-01-21 01:00:24 +01001334 if (strcmp(name, def->name) == 0) {
1335 memcpy(x86_cpu_def, def, sizeof(*def));
Igor Mammedov11acfdd2013-01-21 15:06:37 +01001336 /* sysenter isn't supported in compatibility mode on AMD,
1337 * syscall isn't supported in compatibility mode on Intel.
1338 * Normally we advertise the actual CPU vendor, but you can
1339 * override this using the 'vendor' property if you want to use
1340 * KVM's sysenter/syscall emulation in compatibility mode and
1341 * when doing cross vendor migration
1342 */
1343 if (kvm_enabled()) {
1344 uint32_t ebx = 0, ecx = 0, edx = 0;
1345 host_cpuid(0, 0, NULL, &ebx, &ecx, &edx);
1346 x86_cpu_vendor_words2str(x86_cpu_def->vendor, ebx, edx, ecx);
1347 }
Andreas Färber4bfe9102013-01-21 01:00:24 +01001348 return 0;
1349 }
1350 }
1351
1352 return -1;
Eduardo Habkost8f961352012-12-04 17:34:39 -02001353}
1354
Igor Mammedov72ac2e82013-04-26 18:04:32 +02001355/* Convert all '_' in a feature string option name to '-', to make feature
1356 * name conform to QOM property naming rule, which uses '-' instead of '_'.
1357 */
1358static inline void feat2prop(char *s)
1359{
1360 while ((s = strchr(s, '_'))) {
1361 *s = '-';
1362 }
1363}
1364
Eduardo Habkost8f961352012-12-04 17:34:39 -02001365/* Parse "+feature,-feature,feature=foo" CPU feature string
1366 */
Igor Mammedova91987c2013-01-21 15:06:38 +01001367static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
Eduardo Habkost8f961352012-12-04 17:34:39 -02001368{
Eduardo Habkost8f961352012-12-04 17:34:39 -02001369 char *featurestr; /* Single 'key=value" string being parsed */
Eduardo Habkost9f3fb562012-12-04 17:34:38 -02001370 /* Features to be added */
Igor Mammedov077c68c2013-01-11 03:10:15 +01001371 FeatureWordArray plus_features = { 0 };
Joerg Roedel296acb62010-09-27 15:16:17 +02001372 /* Features to be removed */
Eduardo Habkost5ef57872013-01-07 16:20:45 -02001373 FeatureWordArray minus_features = { 0 };
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001374 uint32_t numvalue;
Igor Mammedova91987c2013-01-21 15:06:38 +01001375 CPUX86State *env = &cpu->env;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001376
Eduardo Habkost8f961352012-12-04 17:34:39 -02001377 featurestr = features ? strtok(features, ",") : NULL;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001378
1379 while (featurestr) {
1380 char *val;
1381 if (featurestr[0] == '+') {
Eduardo Habkost5ef57872013-01-07 16:20:45 -02001382 add_flagname_to_bitmaps(featurestr + 1, plus_features);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001383 } else if (featurestr[0] == '-') {
Eduardo Habkost5ef57872013-01-07 16:20:45 -02001384 add_flagname_to_bitmaps(featurestr + 1, minus_features);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001385 } else if ((val = strchr(featurestr, '='))) {
1386 *val = 0; val++;
Igor Mammedov72ac2e82013-04-26 18:04:32 +02001387 feat2prop(featurestr);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001388 if (!strcmp(featurestr, "family")) {
Igor Mammedova91987c2013-01-21 15:06:38 +01001389 object_property_parse(OBJECT(cpu), val, featurestr, errp);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001390 } else if (!strcmp(featurestr, "model")) {
Igor Mammedova91987c2013-01-21 15:06:38 +01001391 object_property_parse(OBJECT(cpu), val, featurestr, errp);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001392 } else if (!strcmp(featurestr, "stepping")) {
Igor Mammedova91987c2013-01-21 15:06:38 +01001393 object_property_parse(OBJECT(cpu), val, featurestr, errp);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001394 } else if (!strcmp(featurestr, "level")) {
Igor Mammedova91987c2013-01-21 15:06:38 +01001395 object_property_parse(OBJECT(cpu), val, featurestr, errp);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001396 } else if (!strcmp(featurestr, "xlevel")) {
1397 char *err;
Igor Mammedova91987c2013-01-21 15:06:38 +01001398 char num[32];
1399
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001400 numvalue = strtoul(val, &err, 0);
1401 if (!*val || *err) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001402 error_setg(errp, "bad numerical value %s", val);
Igor Mammedova91987c2013-01-21 15:06:38 +01001403 goto out;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001404 }
1405 if (numvalue < 0x80000000) {
Igor Mammedov8ba8a692013-01-21 15:06:35 +01001406 fprintf(stderr, "xlevel value shall always be >= 0x80000000"
1407 ", fixup will be removed in future versions\n");
Aurelien Jarno2f7a21c2010-03-13 16:46:33 +01001408 numvalue += 0x80000000;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001409 }
Igor Mammedova91987c2013-01-21 15:06:38 +01001410 snprintf(num, sizeof(num), "%" PRIu32, numvalue);
1411 object_property_parse(OBJECT(cpu), num, featurestr, errp);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001412 } else if (!strcmp(featurestr, "vendor")) {
Igor Mammedova91987c2013-01-21 15:06:38 +01001413 object_property_parse(OBJECT(cpu), val, featurestr, errp);
Igor Mammedov72ac2e82013-04-26 18:04:32 +02001414 } else if (!strcmp(featurestr, "model-id")) {
1415 object_property_parse(OBJECT(cpu), val, featurestr, errp);
1416 } else if (!strcmp(featurestr, "tsc-freq")) {
Joerg Roedelb862d1f2011-07-07 16:13:12 +02001417 int64_t tsc_freq;
1418 char *err;
Igor Mammedova91987c2013-01-21 15:06:38 +01001419 char num[32];
Joerg Roedelb862d1f2011-07-07 16:13:12 +02001420
1421 tsc_freq = strtosz_suffix_unit(val, &err,
1422 STRTOSZ_DEFSUFFIX_B, 1000);
Markus Armbruster45009a32011-11-22 09:46:04 +01001423 if (tsc_freq < 0 || *err) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001424 error_setg(errp, "bad numerical value %s", val);
Igor Mammedova91987c2013-01-21 15:06:38 +01001425 goto out;
Joerg Roedelb862d1f2011-07-07 16:13:12 +02001426 }
Igor Mammedova91987c2013-01-21 15:06:38 +01001427 snprintf(num, sizeof(num), "%" PRId64, tsc_freq);
1428 object_property_parse(OBJECT(cpu), num, "tsc-frequency", errp);
Igor Mammedov72ac2e82013-04-26 18:04:32 +02001429 } else if (!strcmp(featurestr, "hv-spinlocks")) {
Vadim Rozenfeld28f52cc2011-12-18 22:48:13 +02001430 char *err;
1431 numvalue = strtoul(val, &err, 0);
1432 if (!*val || *err) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001433 error_setg(errp, "bad numerical value %s", val);
Igor Mammedova91987c2013-01-21 15:06:38 +01001434 goto out;
Vadim Rozenfeld28f52cc2011-12-18 22:48:13 +02001435 }
1436 hyperv_set_spinlock_retries(numvalue);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001437 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001438 error_setg(errp, "unrecognized feature %s", featurestr);
Igor Mammedova91987c2013-01-21 15:06:38 +01001439 goto out;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001440 }
1441 } else if (!strcmp(featurestr, "check")) {
1442 check_cpuid = 1;
1443 } else if (!strcmp(featurestr, "enforce")) {
1444 check_cpuid = enforce_cpuid = 1;
Vadim Rozenfeld28f52cc2011-12-18 22:48:13 +02001445 } else if (!strcmp(featurestr, "hv_relaxed")) {
1446 hyperv_enable_relaxed_timing(true);
1447 } else if (!strcmp(featurestr, "hv_vapic")) {
1448 hyperv_enable_vapic_recommended(true);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001449 } else {
Igor Mammedova91987c2013-01-21 15:06:38 +01001450 error_setg(errp, "feature string `%s' not in format (+feature|"
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001451 "-feature|feature=xyz)", featurestr);
Igor Mammedova91987c2013-01-21 15:06:38 +01001452 goto out;
1453 }
1454 if (error_is_set(errp)) {
1455 goto out;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001456 }
1457 featurestr = strtok(NULL, ",");
1458 }
Igor Mammedova91987c2013-01-21 15:06:38 +01001459 env->cpuid_features |= plus_features[FEAT_1_EDX];
1460 env->cpuid_ext_features |= plus_features[FEAT_1_ECX];
1461 env->cpuid_ext2_features |= plus_features[FEAT_8000_0001_EDX];
1462 env->cpuid_ext3_features |= plus_features[FEAT_8000_0001_ECX];
1463 env->cpuid_ext4_features |= plus_features[FEAT_C000_0001_EDX];
1464 env->cpuid_kvm_features |= plus_features[FEAT_KVM];
1465 env->cpuid_svm_features |= plus_features[FEAT_SVM];
1466 env->cpuid_7_0_ebx_features |= plus_features[FEAT_7_0_EBX];
1467 env->cpuid_features &= ~minus_features[FEAT_1_EDX];
1468 env->cpuid_ext_features &= ~minus_features[FEAT_1_ECX];
1469 env->cpuid_ext2_features &= ~minus_features[FEAT_8000_0001_EDX];
1470 env->cpuid_ext3_features &= ~minus_features[FEAT_8000_0001_ECX];
1471 env->cpuid_ext4_features &= ~minus_features[FEAT_C000_0001_EDX];
1472 env->cpuid_kvm_features &= ~minus_features[FEAT_KVM];
1473 env->cpuid_svm_features &= ~minus_features[FEAT_SVM];
1474 env->cpuid_7_0_ebx_features &= ~minus_features[FEAT_7_0_EBX];
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001475
Igor Mammedova91987c2013-01-21 15:06:38 +01001476out:
1477 return;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001478}
1479
1480/* generate a composite string into buf of all cpuid names in featureset
1481 * selected by fbits. indicate truncation at bufsize in the event of overflow.
1482 * if flags, suppress names undefined in featureset.
1483 */
1484static void listflags(char *buf, int bufsize, uint32_t fbits,
1485 const char **featureset, uint32_t flags)
1486{
1487 const char **p = &featureset[31];
1488 char *q, *b, bit;
1489 int nc;
1490
1491 b = 4 <= bufsize ? buf + (bufsize -= 3) - 1 : NULL;
1492 *buf = '\0';
1493 for (q = buf, bit = 31; fbits && bufsize; --p, fbits &= ~(1 << bit), --bit)
1494 if (fbits & 1 << bit && (*p || !flags)) {
1495 if (*p)
1496 nc = snprintf(q, bufsize, "%s%s", q == buf ? "" : " ", *p);
1497 else
1498 nc = snprintf(q, bufsize, "%s[%d]", q == buf ? "" : " ", bit);
1499 if (bufsize <= nc) {
1500 if (b) {
1501 memcpy(b, "...", sizeof("..."));
1502 }
1503 return;
1504 }
1505 q += nc;
1506 bufsize -= nc;
1507 }
1508}
1509
Peter Maydelle916cbf2012-09-05 17:41:08 -03001510/* generate CPU information. */
1511void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001512{
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001513 x86_def_t *def;
1514 char buf[256];
Andreas Färber7fc9b712013-01-21 01:02:28 +01001515 int i;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001516
Andreas Färber7fc9b712013-01-21 01:02:28 +01001517 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
1518 def = &builtin_x86_defs[i];
Eduardo Habkostc04321b2012-09-05 17:41:13 -03001519 snprintf(buf, sizeof(buf), "%s", def->name);
Peter Maydell6cdf8852012-09-05 17:41:07 -03001520 (*cpu_fprintf)(f, "x86 %16s %-48s\n", buf, def->model_id);
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001521 }
Jan Kiszka21ad7782013-03-24 17:01:02 +01001522#ifdef CONFIG_KVM
1523 (*cpu_fprintf)(f, "x86 %16s %-48s\n", "host",
1524 "KVM processor with all supported host features "
1525 "(only available in KVM mode)");
1526#endif
1527
Peter Maydell6cdf8852012-09-05 17:41:07 -03001528 (*cpu_fprintf)(f, "\nRecognized CPUID flags:\n");
Jan Kiszka3af60be2013-02-27 10:15:51 +01001529 for (i = 0; i < ARRAY_SIZE(feature_word_info); i++) {
1530 FeatureWordInfo *fw = &feature_word_info[i];
1531
1532 listflags(buf, sizeof(buf), (uint32_t)~0, fw->feat_names, 1);
1533 (*cpu_fprintf)(f, " %s\n", buf);
1534 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001535}
1536
Anthony Liguori76b64a72012-08-14 22:17:36 -05001537CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
Anthony Liguorie3966122012-08-10 11:04:14 -05001538{
1539 CpuDefinitionInfoList *cpu_list = NULL;
1540 x86_def_t *def;
Andreas Färber7fc9b712013-01-21 01:02:28 +01001541 int i;
Anthony Liguorie3966122012-08-10 11:04:14 -05001542
Andreas Färber7fc9b712013-01-21 01:02:28 +01001543 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
Anthony Liguorie3966122012-08-10 11:04:14 -05001544 CpuDefinitionInfoList *entry;
1545 CpuDefinitionInfo *info;
1546
Andreas Färber7fc9b712013-01-21 01:02:28 +01001547 def = &builtin_x86_defs[i];
Anthony Liguorie3966122012-08-10 11:04:14 -05001548 info = g_malloc0(sizeof(*info));
1549 info->name = g_strdup(def->name);
1550
1551 entry = g_malloc0(sizeof(*entry));
1552 entry->value = info;
1553 entry->next = cpu_list;
1554 cpu_list = entry;
1555 }
1556
1557 return cpu_list;
1558}
1559
Eduardo Habkostbc74b7d2012-10-04 17:49:05 -03001560#ifdef CONFIG_KVM
1561static void filter_features_for_kvm(X86CPU *cpu)
1562{
1563 CPUX86State *env = &cpu->env;
1564 KVMState *s = kvm_state;
1565
Eduardo Habkostb8091f22012-10-04 17:49:06 -03001566 env->cpuid_features &=
1567 kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
1568 env->cpuid_ext_features &=
1569 kvm_arch_get_supported_cpuid(s, 1, 0, R_ECX);
1570 env->cpuid_ext2_features &=
1571 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX);
1572 env->cpuid_ext3_features &=
1573 kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX);
1574 env->cpuid_svm_features &=
1575 kvm_arch_get_supported_cpuid(s, 0x8000000A, 0, R_EDX);
Eduardo Habkostffa8c112012-10-04 17:49:07 -03001576 env->cpuid_7_0_ebx_features &=
1577 kvm_arch_get_supported_cpuid(s, 7, 0, R_EBX);
Eduardo Habkostbc74b7d2012-10-04 17:49:05 -03001578 env->cpuid_kvm_features &=
Eduardo Habkostb8091f22012-10-04 17:49:06 -03001579 kvm_arch_get_supported_cpuid(s, KVM_CPUID_FEATURES, 0, R_EAX);
1580 env->cpuid_ext4_features &=
1581 kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
Eduardo Habkostbc74b7d2012-10-04 17:49:05 -03001582
1583}
1584#endif
1585
Andreas Färber2d642552013-02-15 14:06:56 +01001586static void cpu_x86_register(X86CPU *cpu, const char *name, Error **errp)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001587{
Andreas Färber61dcd772012-04-17 12:00:51 +02001588 CPUX86State *env = &cpu->env;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001589 x86_def_t def1, *def = &def1;
1590
Joerg Roedeldb0ad1b2010-09-27 15:16:16 +02001591 memset(def, 0, sizeof(*def));
1592
Andreas Färber2d642552013-02-15 14:06:56 +01001593 if (cpu_x86_find_by_name(def, name) < 0) {
1594 error_setg(errp, "Unable to find CPU definition: %s", name);
1595 return;
1596 }
1597
1598 if (kvm_enabled()) {
1599 def->kvm_features |= kvm_default_features;
1600 }
1601 def->ext_features |= CPUID_EXT_HYPERVISOR;
1602
1603 object_property_set_str(OBJECT(cpu), def->vendor, "vendor", errp);
1604 object_property_set_int(OBJECT(cpu), def->level, "level", errp);
1605 object_property_set_int(OBJECT(cpu), def->family, "family", errp);
1606 object_property_set_int(OBJECT(cpu), def->model, "model", errp);
1607 object_property_set_int(OBJECT(cpu), def->stepping, "stepping", errp);
1608 env->cpuid_features = def->features;
1609 env->cpuid_ext_features = def->ext_features;
1610 env->cpuid_ext2_features = def->ext2_features;
1611 env->cpuid_ext3_features = def->ext3_features;
1612 object_property_set_int(OBJECT(cpu), def->xlevel, "xlevel", errp);
1613 env->cpuid_kvm_features = def->kvm_features;
1614 env->cpuid_svm_features = def->svm_features;
1615 env->cpuid_ext4_features = def->ext4_features;
1616 env->cpuid_7_0_ebx_features = def->cpuid_7_0_ebx_features;
1617 env->cpuid_xlevel2 = def->xlevel2;
1618
1619 object_property_set_str(OBJECT(cpu), def->model_id, "model-id", errp);
1620}
1621
Igor Mammedov62fc4032013-04-29 18:54:13 +02001622X86CPU *cpu_x86_create(const char *cpu_model, DeviceState *icc_bridge,
1623 Error **errp)
Andreas Färber2d642552013-02-15 14:06:56 +01001624{
1625 X86CPU *cpu = NULL;
1626 CPUX86State *env;
1627 gchar **model_pieces;
1628 char *name, *features;
1629 Error *error = NULL;
1630
Eduardo Habkost8f961352012-12-04 17:34:39 -02001631 model_pieces = g_strsplit(cpu_model, ",", 2);
1632 if (!model_pieces[0]) {
Igor Mammedovfa2db3c2013-01-11 03:10:16 +01001633 error_setg(&error, "Invalid/empty CPU model name");
1634 goto out;
Eduardo Habkost8f961352012-12-04 17:34:39 -02001635 }
1636 name = model_pieces[0];
1637 features = model_pieces[1];
1638
Andreas Färber2d642552013-02-15 14:06:56 +01001639 cpu = X86_CPU(object_new(TYPE_X86_CPU));
Igor Mammedov62fc4032013-04-29 18:54:13 +02001640#ifndef CONFIG_USER_ONLY
1641 if (icc_bridge == NULL) {
1642 error_setg(&error, "Invalid icc-bridge value");
1643 goto out;
1644 }
1645 qdev_set_parent_bus(DEVICE(cpu), qdev_get_child_bus(icc_bridge, "icc"));
1646 object_unref(OBJECT(cpu));
1647#endif
Andreas Färber2d642552013-02-15 14:06:56 +01001648 env = &cpu->env;
1649 env->cpu_model_str = cpu_model;
Eduardo Habkost8f961352012-12-04 17:34:39 -02001650
Andreas Färber2d642552013-02-15 14:06:56 +01001651 cpu_x86_register(cpu, name, &error);
Igor Mammedova91987c2013-01-21 15:06:38 +01001652 if (error) {
1653 goto out;
1654 }
Igor Mammedovfa2db3c2013-01-11 03:10:16 +01001655
Igor Mammedova91987c2013-01-21 15:06:38 +01001656 cpu_x86_parse_featurestr(cpu, features, &error);
Andreas Färber2d642552013-02-15 14:06:56 +01001657 if (error) {
1658 goto out;
1659 }
1660
Igor Mammedov7f833242013-04-11 16:51:40 +02001661out:
1662 error_propagate(errp, error);
1663 g_strfreev(model_pieces);
1664 return cpu;
1665}
1666
1667X86CPU *cpu_x86_init(const char *cpu_model)
1668{
1669 Error *error = NULL;
1670 X86CPU *cpu;
1671
Igor Mammedov62fc4032013-04-29 18:54:13 +02001672 cpu = cpu_x86_create(cpu_model, NULL, &error);
Andreas Färber2d642552013-02-15 14:06:56 +01001673 if (error) {
1674 goto out;
1675 }
1676
Igor Mammedov7f833242013-04-11 16:51:40 +02001677 object_property_set_bool(OBJECT(cpu), true, "realized", &error);
1678
Igor Mammedovfa2db3c2013-01-11 03:10:16 +01001679out:
Igor Mammedov18eb4732012-10-02 17:36:54 +02001680 if (error) {
1681 fprintf(stderr, "%s\n", error_get_pretty(error));
Andreas Färber71ad61d2012-04-17 12:10:29 +02001682 error_free(error);
Andreas Färber2d642552013-02-15 14:06:56 +01001683 if (cpu != NULL) {
1684 object_unref(OBJECT(cpu));
1685 cpu = NULL;
1686 }
Andreas Färber5c3c6a62013-02-01 15:12:13 +01001687 }
1688 return cpu;
1689}
1690
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001691#if !defined(CONFIG_USER_ONLY)
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001692
Blue Swirl0e26b7b2010-06-19 10:42:34 +03001693void cpu_clear_apic_feature(CPUX86State *env)
1694{
1695 env->cpuid_features &= ~CPUID_APIC;
1696}
1697
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001698#endif /* !CONFIG_USER_ONLY */
1699
Eduardo Habkostc04321b2012-09-05 17:41:13 -03001700/* Initialize list of CPU models, filling some non-static fields if necessary
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001701 */
1702void x86_cpudef_setup(void)
1703{
Crístian Viana93bfef42012-05-30 00:35:51 -03001704 int i, j;
1705 static const char *model_with_versions[] = { "qemu32", "qemu64", "athlon" };
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001706
1707 for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); ++i) {
Eduardo Habkostbc3e1292012-09-05 17:41:12 -03001708 x86_def_t *def = &builtin_x86_defs[i];
Crístian Viana93bfef42012-05-30 00:35:51 -03001709
1710 /* Look for specific "cpudef" models that */
Stefan Weil09faecf2012-06-20 06:05:51 +02001711 /* have the QEMU version in .model_id */
Crístian Viana93bfef42012-05-30 00:35:51 -03001712 for (j = 0; j < ARRAY_SIZE(model_with_versions); j++) {
Eduardo Habkostbc3e1292012-09-05 17:41:12 -03001713 if (strcmp(model_with_versions[j], def->name) == 0) {
1714 pstrcpy(def->model_id, sizeof(def->model_id),
1715 "QEMU Virtual CPU version ");
1716 pstrcat(def->model_id, sizeof(def->model_id),
1717 qemu_get_version());
Crístian Viana93bfef42012-05-30 00:35:51 -03001718 break;
1719 }
1720 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001721 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001722}
1723
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001724static void get_cpuid_vendor(CPUX86State *env, uint32_t *ebx,
1725 uint32_t *ecx, uint32_t *edx)
1726{
1727 *ebx = env->cpuid_vendor1;
1728 *edx = env->cpuid_vendor2;
1729 *ecx = env->cpuid_vendor3;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001730}
1731
1732void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
1733 uint32_t *eax, uint32_t *ebx,
1734 uint32_t *ecx, uint32_t *edx)
1735{
Andreas Färbera60f24b2012-12-01 05:35:08 +01001736 X86CPU *cpu = x86_env_get_cpu(env);
1737 CPUState *cs = CPU(cpu);
1738
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001739 /* test if maximum index reached */
1740 if (index & 0x80000000) {
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +08001741 if (index > env->cpuid_xlevel) {
1742 if (env->cpuid_xlevel2 > 0) {
1743 /* Handle the Centaur's CPUID instruction. */
1744 if (index > env->cpuid_xlevel2) {
1745 index = env->cpuid_xlevel2;
1746 } else if (index < 0xC0000000) {
1747 index = env->cpuid_xlevel;
1748 }
1749 } else {
Eduardo Habkost57f26ae2012-12-20 16:43:48 -02001750 /* Intel documentation states that invalid EAX input will
1751 * return the same information as EAX=cpuid_level
1752 * (Intel SDM Vol. 2A - Instruction Set Reference - CPUID)
1753 */
1754 index = env->cpuid_level;
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +08001755 }
1756 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001757 } else {
1758 if (index > env->cpuid_level)
1759 index = env->cpuid_level;
1760 }
1761
1762 switch(index) {
1763 case 0:
1764 *eax = env->cpuid_level;
1765 get_cpuid_vendor(env, ebx, ecx, edx);
1766 break;
1767 case 1:
1768 *eax = env->cpuid_version;
1769 *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1770 *ecx = env->cpuid_ext_features;
1771 *edx = env->cpuid_features;
Andreas Färberce3960e2012-12-17 03:27:07 +01001772 if (cs->nr_cores * cs->nr_threads > 1) {
1773 *ebx |= (cs->nr_cores * cs->nr_threads) << 16;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001774 *edx |= 1 << 28; /* HTT bit */
1775 }
1776 break;
1777 case 2:
1778 /* cache info: needed for Pentium Pro compatibility */
1779 *eax = 1;
1780 *ebx = 0;
1781 *ecx = 0;
1782 *edx = 0x2c307d;
1783 break;
1784 case 4:
1785 /* cache info: needed for Core compatibility */
Andreas Färberce3960e2012-12-17 03:27:07 +01001786 if (cs->nr_cores > 1) {
1787 *eax = (cs->nr_cores - 1) << 26;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001788 } else {
Aurelien Jarno2f7a21c2010-03-13 16:46:33 +01001789 *eax = 0;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001790 }
1791 switch (count) {
1792 case 0: /* L1 dcache info */
1793 *eax |= 0x0000121;
1794 *ebx = 0x1c0003f;
1795 *ecx = 0x000003f;
1796 *edx = 0x0000001;
1797 break;
1798 case 1: /* L1 icache info */
1799 *eax |= 0x0000122;
1800 *ebx = 0x1c0003f;
1801 *ecx = 0x000003f;
1802 *edx = 0x0000001;
1803 break;
1804 case 2: /* L2 cache info */
1805 *eax |= 0x0000143;
Andreas Färberce3960e2012-12-17 03:27:07 +01001806 if (cs->nr_threads > 1) {
1807 *eax |= (cs->nr_threads - 1) << 14;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001808 }
1809 *ebx = 0x3c0003f;
1810 *ecx = 0x0000fff;
1811 *edx = 0x0000001;
1812 break;
1813 default: /* end of info */
1814 *eax = 0;
1815 *ebx = 0;
1816 *ecx = 0;
1817 *edx = 0;
1818 break;
1819 }
1820 break;
1821 case 5:
1822 /* mwait info: needed for Core compatibility */
1823 *eax = 0; /* Smallest monitor-line size in bytes */
1824 *ebx = 0; /* Largest monitor-line size in bytes */
1825 *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
1826 *edx = 0;
1827 break;
1828 case 6:
1829 /* Thermal and Power Leaf */
1830 *eax = 0;
1831 *ebx = 0;
1832 *ecx = 0;
1833 *edx = 0;
1834 break;
Yang, Wei Yf7911682011-05-30 23:17:42 +08001835 case 7:
Eduardo Habkost13526722012-05-21 11:27:02 -03001836 /* Structured Extended Feature Flags Enumeration Leaf */
1837 if (count == 0) {
1838 *eax = 0; /* Maximum ECX value for sub-leaves */
H. Peter Anvina9321a42012-09-26 13:18:43 -07001839 *ebx = env->cpuid_7_0_ebx_features; /* Feature flags */
Eduardo Habkost13526722012-05-21 11:27:02 -03001840 *ecx = 0; /* Reserved */
1841 *edx = 0; /* Reserved */
Yang, Wei Yf7911682011-05-30 23:17:42 +08001842 } else {
1843 *eax = 0;
1844 *ebx = 0;
1845 *ecx = 0;
1846 *edx = 0;
1847 }
1848 break;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001849 case 9:
1850 /* Direct Cache Access Information Leaf */
1851 *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1852 *ebx = 0;
1853 *ecx = 0;
1854 *edx = 0;
1855 break;
1856 case 0xA:
1857 /* Architectural Performance Monitoring Leaf */
Gleb Natapova0fa8202011-12-15 12:44:05 +02001858 if (kvm_enabled()) {
Andreas Färbera60f24b2012-12-01 05:35:08 +01001859 KVMState *s = cs->kvm_state;
Gleb Natapova0fa8202011-12-15 12:44:05 +02001860
1861 *eax = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EAX);
1862 *ebx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EBX);
1863 *ecx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_ECX);
1864 *edx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EDX);
1865 } else {
1866 *eax = 0;
1867 *ebx = 0;
1868 *ecx = 0;
1869 *edx = 0;
1870 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001871 break;
Sheng Yang51e49432010-06-17 15:18:14 +08001872 case 0xD:
1873 /* Processor Extended State */
1874 if (!(env->cpuid_ext_features & CPUID_EXT_XSAVE)) {
1875 *eax = 0;
1876 *ebx = 0;
1877 *ecx = 0;
1878 *edx = 0;
1879 break;
1880 }
1881 if (kvm_enabled()) {
Andreas Färbera60f24b2012-12-01 05:35:08 +01001882 KVMState *s = cs->kvm_state;
Jan Kiszkaba9bc592011-06-08 16:11:05 +02001883
1884 *eax = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EAX);
1885 *ebx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EBX);
1886 *ecx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_ECX);
1887 *edx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EDX);
Sheng Yang51e49432010-06-17 15:18:14 +08001888 } else {
1889 *eax = 0;
1890 *ebx = 0;
1891 *ecx = 0;
1892 *edx = 0;
1893 }
1894 break;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001895 case 0x80000000:
1896 *eax = env->cpuid_xlevel;
1897 *ebx = env->cpuid_vendor1;
1898 *edx = env->cpuid_vendor2;
1899 *ecx = env->cpuid_vendor3;
1900 break;
1901 case 0x80000001:
1902 *eax = env->cpuid_version;
1903 *ebx = 0;
1904 *ecx = env->cpuid_ext3_features;
1905 *edx = env->cpuid_ext2_features;
1906
1907 /* The Linux kernel checks for the CMPLegacy bit and
1908 * discards multiple thread information if it is set.
1909 * So dont set it here for Intel to make Linux guests happy.
1910 */
Andreas Färberce3960e2012-12-17 03:27:07 +01001911 if (cs->nr_cores * cs->nr_threads > 1) {
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001912 uint32_t tebx, tecx, tedx;
1913 get_cpuid_vendor(env, &tebx, &tecx, &tedx);
1914 if (tebx != CPUID_VENDOR_INTEL_1 ||
1915 tedx != CPUID_VENDOR_INTEL_2 ||
1916 tecx != CPUID_VENDOR_INTEL_3) {
1917 *ecx |= 1 << 1; /* CmpLegacy bit */
1918 }
1919 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001920 break;
1921 case 0x80000002:
1922 case 0x80000003:
1923 case 0x80000004:
1924 *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
1925 *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
1926 *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
1927 *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
1928 break;
1929 case 0x80000005:
1930 /* cache info (L1 cache) */
1931 *eax = 0x01ff01ff;
1932 *ebx = 0x01ff01ff;
1933 *ecx = 0x40020140;
1934 *edx = 0x40020140;
1935 break;
1936 case 0x80000006:
1937 /* cache info (L2 cache) */
1938 *eax = 0;
1939 *ebx = 0x42004200;
1940 *ecx = 0x02008140;
1941 *edx = 0;
1942 break;
1943 case 0x80000008:
1944 /* virtual & phys address size in low 2 bytes. */
1945/* XXX: This value must match the one used in the MMU code. */
1946 if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
1947 /* 64 bit processor */
1948/* XXX: The physical address space is limited to 42 bits in exec.c. */
Eduardo Habkostdd13e082013-04-11 17:07:23 -03001949 *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001950 } else {
Eduardo Habkostdd13e082013-04-11 17:07:23 -03001951 if (env->cpuid_features & CPUID_PSE36) {
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001952 *eax = 0x00000024; /* 36 bits physical */
Eduardo Habkostdd13e082013-04-11 17:07:23 -03001953 } else {
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001954 *eax = 0x00000020; /* 32 bits physical */
Eduardo Habkostdd13e082013-04-11 17:07:23 -03001955 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001956 }
1957 *ebx = 0;
1958 *ecx = 0;
1959 *edx = 0;
Andreas Färberce3960e2012-12-17 03:27:07 +01001960 if (cs->nr_cores * cs->nr_threads > 1) {
1961 *ecx |= (cs->nr_cores * cs->nr_threads) - 1;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001962 }
1963 break;
1964 case 0x8000000A:
Eduardo Habkost9f3fb562012-12-04 17:34:38 -02001965 if (env->cpuid_ext3_features & CPUID_EXT3_SVM) {
1966 *eax = 0x00000001; /* SVM Revision */
1967 *ebx = 0x00000010; /* nr of ASIDs */
1968 *ecx = 0;
1969 *edx = env->cpuid_svm_features; /* optional features */
1970 } else {
1971 *eax = 0;
1972 *ebx = 0;
1973 *ecx = 0;
1974 *edx = 0;
1975 }
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001976 break;
brillywu@viatech.com.cnb3baa152011-06-01 09:59:52 +08001977 case 0xC0000000:
1978 *eax = env->cpuid_xlevel2;
1979 *ebx = 0;
1980 *ecx = 0;
1981 *edx = 0;
1982 break;
1983 case 0xC0000001:
1984 /* Support for VIA CPU's CPUID instruction */
1985 *eax = env->cpuid_version;
1986 *ebx = 0;
1987 *ecx = 0;
1988 *edx = env->cpuid_ext4_features;
1989 break;
1990 case 0xC0000002:
1991 case 0xC0000003:
1992 case 0xC0000004:
1993 /* Reserved for the future, and now filled with zero */
1994 *eax = 0;
1995 *ebx = 0;
1996 *ecx = 0;
1997 *edx = 0;
1998 break;
Andre Przywarac6dc6f62010-03-11 14:38:55 +01001999 default:
2000 /* reserved values: zero */
2001 *eax = 0;
2002 *ebx = 0;
2003 *ecx = 0;
2004 *edx = 0;
2005 break;
2006 }
2007}
Andreas Färber5fd20872012-04-02 23:20:08 +02002008
2009/* CPUClass::reset() */
2010static void x86_cpu_reset(CPUState *s)
2011{
2012 X86CPU *cpu = X86_CPU(s);
2013 X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
2014 CPUX86State *env = &cpu->env;
Andreas Färberc1958ae2012-04-03 00:16:24 +02002015 int i;
2016
2017 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
Andreas Färber55e5c282012-12-17 06:18:02 +01002018 qemu_log("CPU Reset (CPU %d)\n", s->cpu_index);
Peter Maydell6fd2a022012-10-05 15:04:43 +01002019 log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
Andreas Färberc1958ae2012-04-03 00:16:24 +02002020 }
Andreas Färber5fd20872012-04-02 23:20:08 +02002021
2022 xcc->parent_reset(s);
2023
Andreas Färberc1958ae2012-04-03 00:16:24 +02002024
2025 memset(env, 0, offsetof(CPUX86State, breakpoints));
2026
2027 tlb_flush(env, 1);
2028
2029 env->old_exception = -1;
2030
2031 /* init to reset state */
2032
2033#ifdef CONFIG_SOFTMMU
2034 env->hflags |= HF_SOFTMMU_MASK;
2035#endif
2036 env->hflags2 |= HF2_GIF_MASK;
2037
2038 cpu_x86_update_cr0(env, 0x60000010);
2039 env->a20_mask = ~0x0;
2040 env->smbase = 0x30000;
2041
2042 env->idt.limit = 0xffff;
2043 env->gdt.limit = 0xffff;
2044 env->ldt.limit = 0xffff;
2045 env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
2046 env->tr.limit = 0xffff;
2047 env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
2048
2049 cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
2050 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK |
2051 DESC_R_MASK | DESC_A_MASK);
2052 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
2053 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2054 DESC_A_MASK);
2055 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
2056 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2057 DESC_A_MASK);
2058 cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
2059 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2060 DESC_A_MASK);
2061 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
2062 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2063 DESC_A_MASK);
2064 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
2065 DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
2066 DESC_A_MASK);
2067
2068 env->eip = 0xfff0;
2069 env->regs[R_EDX] = env->cpuid_version;
2070
2071 env->eflags = 0x2;
2072
2073 /* FPU init */
2074 for (i = 0; i < 8; i++) {
2075 env->fptags[i] = 1;
2076 }
2077 env->fpuc = 0x37f;
2078
2079 env->mxcsr = 0x1f80;
2080
2081 env->pat = 0x0007040600070406ULL;
2082 env->msr_ia32_misc_enable = MSR_IA32_MISC_ENABLE_DEFAULT;
2083
2084 memset(env->dr, 0, sizeof(env->dr));
2085 env->dr[6] = DR6_FIXED_1;
2086 env->dr[7] = DR7_FIXED_1;
2087 cpu_breakpoint_remove_all(env, BP_CPU);
2088 cpu_watchpoint_remove_all(env, BP_CPU);
Igor Mammedovdd673282012-07-23 15:22:27 +02002089
2090#if !defined(CONFIG_USER_ONLY)
2091 /* We hard-wire the BSP to the first CPU. */
Andreas Färber55e5c282012-12-17 06:18:02 +01002092 if (s->cpu_index == 0) {
Igor Mammedovdd673282012-07-23 15:22:27 +02002093 apic_designate_bsp(env->apic_state);
2094 }
2095
Andreas Färber259186a2013-01-17 18:51:17 +01002096 s->halted = !cpu_is_bsp(cpu);
Igor Mammedovdd673282012-07-23 15:22:27 +02002097#endif
Andreas Färber5fd20872012-04-02 23:20:08 +02002098}
2099
Igor Mammedovdd673282012-07-23 15:22:27 +02002100#ifndef CONFIG_USER_ONLY
2101bool cpu_is_bsp(X86CPU *cpu)
2102{
2103 return cpu_get_apic_base(cpu->env.apic_state) & MSR_IA32_APICBASE_BSP;
2104}
Igor Mammedov65dee382012-07-23 15:22:28 +02002105
2106/* TODO: remove me, when reset over QOM tree is implemented */
2107static void x86_cpu_machine_reset_cb(void *opaque)
2108{
2109 X86CPU *cpu = opaque;
2110 cpu_reset(CPU(cpu));
2111}
Igor Mammedovdd673282012-07-23 15:22:27 +02002112#endif
2113
Andreas Färberde024812012-04-03 00:00:17 +02002114static void mce_init(X86CPU *cpu)
2115{
2116 CPUX86State *cenv = &cpu->env;
2117 unsigned int bank;
2118
2119 if (((cenv->cpuid_version >> 8) & 0xf) >= 6
2120 && (cenv->cpuid_features & (CPUID_MCE | CPUID_MCA)) ==
2121 (CPUID_MCE | CPUID_MCA)) {
2122 cenv->mcg_cap = MCE_CAP_DEF | MCE_BANKS_DEF;
2123 cenv->mcg_ctl = ~(uint64_t)0;
2124 for (bank = 0; bank < MCE_BANKS_DEF; bank++) {
2125 cenv->mce_banks[bank * 4] = ~(uint64_t)0;
2126 }
2127 }
2128}
2129
Igor Mammedovbdeec802012-10-13 22:35:39 +02002130#ifndef CONFIG_USER_ONLY
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002131static void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
Igor Mammedovbdeec802012-10-13 22:35:39 +02002132{
Igor Mammedovbdeec802012-10-13 22:35:39 +02002133 CPUX86State *env = &cpu->env;
Andreas Färber449994e2012-10-10 12:18:02 +02002134 APICCommonState *apic;
Igor Mammedovbdeec802012-10-13 22:35:39 +02002135 const char *apic_type = "apic";
2136
2137 if (kvm_irqchip_in_kernel()) {
2138 apic_type = "kvm-apic";
2139 } else if (xen_enabled()) {
2140 apic_type = "xen-apic";
2141 }
2142
2143 env->apic_state = qdev_try_create(NULL, apic_type);
2144 if (env->apic_state == NULL) {
2145 error_setg(errp, "APIC device '%s' could not be created", apic_type);
2146 return;
2147 }
2148
2149 object_property_add_child(OBJECT(cpu), "apic",
2150 OBJECT(env->apic_state), NULL);
2151 qdev_prop_set_uint8(env->apic_state, "id", env->cpuid_apic_id);
2152 /* TODO: convert to link<> */
Andreas Färber449994e2012-10-10 12:18:02 +02002153 apic = APIC_COMMON(env->apic_state);
Andreas Färber60671e52012-10-10 14:10:07 +02002154 apic->cpu = cpu;
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002155}
2156
2157static void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
2158{
2159 CPUX86State *env = &cpu->env;
2160 static int apic_mapped;
2161
2162 if (env->apic_state == NULL) {
2163 return;
2164 }
Igor Mammedovbdeec802012-10-13 22:35:39 +02002165
2166 if (qdev_init(env->apic_state)) {
2167 error_setg(errp, "APIC device '%s' could not be initialized",
2168 object_get_typename(OBJECT(env->apic_state)));
2169 return;
2170 }
2171
2172 /* XXX: mapping more APICs at the same memory location */
2173 if (apic_mapped == 0) {
2174 /* NOTE: the APIC is directly connected to the CPU - it is not
2175 on the global memory bus. */
2176 /* XXX: what if the base changes? */
Alexey Korolev7feb6402013-02-22 16:58:44 +13002177 sysbus_mmio_map_overlap(SYS_BUS_DEVICE(env->apic_state), 0,
Laszlo Ersekdab86232013-03-21 00:23:20 +01002178 APIC_DEFAULT_ADDRESS, 0x1000);
Igor Mammedovbdeec802012-10-13 22:35:39 +02002179 apic_mapped = 1;
2180 }
2181}
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002182#else
2183static void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
2184{
2185}
Igor Mammedovbdeec802012-10-13 22:35:39 +02002186#endif
2187
Andreas Färber2b6f2942013-01-16 03:41:47 +01002188static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
Andreas Färber7a059952012-05-09 23:15:32 +02002189{
Andreas Färber2b6f2942013-01-16 03:41:47 +01002190 X86CPU *cpu = X86_CPU(dev);
2191 X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
Igor Mammedovb34d12d2012-10-22 17:03:00 +02002192 CPUX86State *env = &cpu->env;
Andreas Färber2b6f2942013-01-16 03:41:47 +01002193 Error *local_err = NULL;
Igor Mammedovb34d12d2012-10-22 17:03:00 +02002194
2195 if (env->cpuid_7_0_ebx_features && env->cpuid_level < 7) {
2196 env->cpuid_level = 7;
2197 }
Andreas Färber7a059952012-05-09 23:15:32 +02002198
Igor Mammedov9b15cd92012-12-28 21:01:17 +01002199 /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on
2200 * CPUID[1].EDX.
2201 */
2202 if (env->cpuid_vendor1 == CPUID_VENDOR_AMD_1 &&
2203 env->cpuid_vendor2 == CPUID_VENDOR_AMD_2 &&
2204 env->cpuid_vendor3 == CPUID_VENDOR_AMD_3) {
2205 env->cpuid_ext2_features &= ~CPUID_EXT2_AMD_ALIASES;
2206 env->cpuid_ext2_features |= (env->cpuid_features
2207 & CPUID_EXT2_AMD_ALIASES);
2208 }
2209
Igor Mammedov4586f152012-12-28 21:01:16 +01002210 if (!kvm_enabled()) {
2211 env->cpuid_features &= TCG_FEATURES;
2212 env->cpuid_ext_features &= TCG_EXT_FEATURES;
2213 env->cpuid_ext2_features &= (TCG_EXT2_FEATURES
2214#ifdef TARGET_X86_64
2215 | CPUID_EXT2_SYSCALL | CPUID_EXT2_LM
2216#endif
2217 );
2218 env->cpuid_ext3_features &= TCG_EXT3_FEATURES;
2219 env->cpuid_svm_features &= TCG_SVM_FEATURES;
2220 } else {
Igor Mammedov5ec01c22013-01-11 03:10:17 +01002221 if (check_cpuid && kvm_check_features_against_host(cpu)
2222 && enforce_cpuid) {
Igor Mammedov4dc1f442013-04-05 16:36:53 +02002223 error_setg(&local_err,
2224 "Host's CPU doesn't support requested features");
2225 goto out;
Igor Mammedov5ec01c22013-01-11 03:10:17 +01002226 }
Eduardo Habkosta509d632013-04-02 17:48:12 -03002227#ifdef CONFIG_KVM
2228 filter_features_for_kvm(cpu);
2229#endif
Igor Mammedov4586f152012-12-28 21:01:16 +01002230 }
2231
Igor Mammedov65dee382012-07-23 15:22:28 +02002232#ifndef CONFIG_USER_ONLY
2233 qemu_register_reset(x86_cpu_machine_reset_cb, cpu);
Igor Mammedovbdeec802012-10-13 22:35:39 +02002234
2235 if (cpu->env.cpuid_features & CPUID_APIC || smp_cpus > 1) {
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002236 x86_cpu_apic_create(cpu, &local_err);
Andreas Färber2b6f2942013-01-16 03:41:47 +01002237 if (local_err != NULL) {
Igor Mammedov4dc1f442013-04-05 16:36:53 +02002238 goto out;
Igor Mammedovbdeec802012-10-13 22:35:39 +02002239 }
2240 }
Igor Mammedov65dee382012-07-23 15:22:28 +02002241#endif
2242
Andreas Färber7a059952012-05-09 23:15:32 +02002243 mce_init(cpu);
2244 qemu_init_vcpu(&cpu->env);
Igor Mammedovd3c64d62013-04-05 16:36:54 +02002245
2246 x86_cpu_apic_realize(cpu, &local_err);
2247 if (local_err != NULL) {
2248 goto out;
2249 }
Igor Mammedov65dee382012-07-23 15:22:28 +02002250 cpu_reset(CPU(cpu));
Andreas Färber2b6f2942013-01-16 03:41:47 +01002251
Igor Mammedov4dc1f442013-04-05 16:36:53 +02002252 xcc->parent_realize(dev, &local_err);
2253out:
2254 if (local_err != NULL) {
2255 error_propagate(errp, local_err);
2256 return;
2257 }
Andreas Färber7a059952012-05-09 23:15:32 +02002258}
2259
Eduardo Habkost8932cfd2013-01-22 18:25:09 -02002260/* Enables contiguous-apic-ID mode, for compatibility */
2261static bool compat_apic_id_mode;
2262
2263void enable_compat_apic_id_mode(void)
2264{
2265 compat_apic_id_mode = true;
2266}
2267
Eduardo Habkostcb41bad2013-01-22 18:25:04 -02002268/* Calculates initial APIC ID for a specific CPU index
2269 *
2270 * Currently we need to be able to calculate the APIC ID from the CPU index
2271 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
2272 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
2273 * all CPUs up to max_cpus.
2274 */
2275uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index)
2276{
Eduardo Habkost8932cfd2013-01-22 18:25:09 -02002277 uint32_t correct_id;
2278 static bool warned;
2279
2280 correct_id = x86_apicid_from_cpu_idx(smp_cores, smp_threads, cpu_index);
2281 if (compat_apic_id_mode) {
2282 if (cpu_index != correct_id && !warned) {
2283 error_report("APIC IDs set in compatibility mode, "
2284 "CPU topology won't match the configuration");
2285 warned = true;
2286 }
2287 return cpu_index;
2288 } else {
2289 return correct_id;
2290 }
Eduardo Habkostcb41bad2013-01-22 18:25:04 -02002291}
2292
Andreas Färberde024812012-04-03 00:00:17 +02002293static void x86_cpu_initfn(Object *obj)
2294{
Andreas Färber55e5c282012-12-17 06:18:02 +01002295 CPUState *cs = CPU(obj);
Andreas Färberde024812012-04-03 00:00:17 +02002296 X86CPU *cpu = X86_CPU(obj);
2297 CPUX86State *env = &cpu->env;
Igor Mammedovd65e9812012-06-19 15:39:46 +02002298 static int inited;
Andreas Färberde024812012-04-03 00:00:17 +02002299
Andreas Färberc05efcb2013-01-17 12:13:41 +01002300 cs->env_ptr = env;
Andreas Färberde024812012-04-03 00:00:17 +02002301 cpu_exec_init(env);
Andreas Färber71ad61d2012-04-17 12:10:29 +02002302
2303 object_property_add(obj, "family", "int",
Andreas Färber95b85192012-04-17 14:42:22 +02002304 x86_cpuid_version_get_family,
Andreas Färber71ad61d2012-04-17 12:10:29 +02002305 x86_cpuid_version_set_family, NULL, NULL, NULL);
Andreas Färberc5291a42012-04-17 12:16:39 +02002306 object_property_add(obj, "model", "int",
Andreas Färber67e30c82012-04-17 14:48:14 +02002307 x86_cpuid_version_get_model,
Andreas Färberc5291a42012-04-17 12:16:39 +02002308 x86_cpuid_version_set_model, NULL, NULL, NULL);
Andreas Färber036e2222012-04-17 14:14:18 +02002309 object_property_add(obj, "stepping", "int",
Andreas Färber35112e42012-04-17 14:50:53 +02002310 x86_cpuid_version_get_stepping,
Andreas Färber036e2222012-04-17 14:14:18 +02002311 x86_cpuid_version_set_stepping, NULL, NULL, NULL);
Andreas Färber8e1898b2012-04-17 18:41:40 +02002312 object_property_add(obj, "level", "int",
2313 x86_cpuid_get_level,
2314 x86_cpuid_set_level, NULL, NULL, NULL);
Andreas Färber16b93aa2012-04-17 18:44:07 +02002315 object_property_add(obj, "xlevel", "int",
2316 x86_cpuid_get_xlevel,
2317 x86_cpuid_set_xlevel, NULL, NULL, NULL);
Andreas Färberd480e1a2012-04-17 19:22:58 +02002318 object_property_add_str(obj, "vendor",
2319 x86_cpuid_get_vendor,
2320 x86_cpuid_set_vendor, NULL);
Andreas Färber938d4c22012-04-17 15:17:27 +02002321 object_property_add_str(obj, "model-id",
Andreas Färber63e886e2012-04-17 23:02:26 +02002322 x86_cpuid_get_model_id,
Andreas Färber938d4c22012-04-17 15:17:27 +02002323 x86_cpuid_set_model_id, NULL);
Andreas Färber89e48962012-04-18 00:12:23 +02002324 object_property_add(obj, "tsc-frequency", "int",
2325 x86_cpuid_get_tsc_freq,
2326 x86_cpuid_set_tsc_freq, NULL, NULL, NULL);
Igor Mammedov31050932013-04-25 16:05:26 +02002327 object_property_add(obj, "apic-id", "int",
2328 x86_cpuid_get_apic_id,
2329 x86_cpuid_set_apic_id, NULL, NULL, NULL);
Andreas Färber71ad61d2012-04-17 12:10:29 +02002330
Eduardo Habkostcb41bad2013-01-22 18:25:04 -02002331 env->cpuid_apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);
Igor Mammedovd65e9812012-06-19 15:39:46 +02002332
2333 /* init various static tables used in TCG mode */
2334 if (tcg_enabled() && !inited) {
2335 inited = 1;
2336 optimize_flags_init();
2337#ifndef CONFIG_USER_ONLY
2338 cpu_set_debug_excp_handler(breakpoint_handler);
2339#endif
2340 }
Andreas Färberde024812012-04-03 00:00:17 +02002341}
2342
Igor Mammedov997395d2013-04-23 10:29:41 +02002343static int64_t x86_cpu_get_arch_id(CPUState *cs)
2344{
2345 X86CPU *cpu = X86_CPU(cs);
2346 CPUX86State *env = &cpu->env;
2347
2348 return env->cpuid_apic_id;
2349}
2350
Andreas Färber5fd20872012-04-02 23:20:08 +02002351static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
2352{
2353 X86CPUClass *xcc = X86_CPU_CLASS(oc);
2354 CPUClass *cc = CPU_CLASS(oc);
Andreas Färber2b6f2942013-01-16 03:41:47 +01002355 DeviceClass *dc = DEVICE_CLASS(oc);
2356
2357 xcc->parent_realize = dc->realize;
2358 dc->realize = x86_cpu_realizefn;
Igor Mammedov62fc4032013-04-29 18:54:13 +02002359 dc->bus_type = TYPE_ICC_BUS;
Andreas Färber5fd20872012-04-02 23:20:08 +02002360
2361 xcc->parent_reset = cc->reset;
2362 cc->reset = x86_cpu_reset;
Andreas Färberf56e3a12013-02-02 13:38:08 +01002363
Andreas Färber97a8ea52013-02-02 10:57:51 +01002364 cc->do_interrupt = x86_cpu_do_interrupt;
Jens Freimannc72bf462013-04-19 16:45:06 +02002365#ifndef CONFIG_USER_ONLY
2366 cc->write_elf64_note = x86_cpu_write_elf64_note;
2367 cc->write_elf64_qemunote = x86_cpu_write_elf64_qemunote;
2368 cc->write_elf32_note = x86_cpu_write_elf32_note;
2369 cc->write_elf32_qemunote = x86_cpu_write_elf32_qemunote;
2370#endif
Andreas Färberf56e3a12013-02-02 13:38:08 +01002371 cpu_class_set_vmsd(cc, &vmstate_x86_cpu);
Igor Mammedov997395d2013-04-23 10:29:41 +02002372
2373 cc->get_arch_id = x86_cpu_get_arch_id;
Andreas Färber5fd20872012-04-02 23:20:08 +02002374}
2375
2376static const TypeInfo x86_cpu_type_info = {
2377 .name = TYPE_X86_CPU,
2378 .parent = TYPE_CPU,
2379 .instance_size = sizeof(X86CPU),
Andreas Färberde024812012-04-03 00:00:17 +02002380 .instance_init = x86_cpu_initfn,
Andreas Färber5fd20872012-04-02 23:20:08 +02002381 .abstract = false,
2382 .class_size = sizeof(X86CPUClass),
2383 .class_init = x86_cpu_common_class_init,
2384};
2385
2386static void x86_cpu_register_types(void)
2387{
2388 type_register_static(&x86_cpu_type_info);
2389}
2390
2391type_init(x86_cpu_register_types)