Statistics
| Branch: | Revision:

root / target-i386 / cpu.c @ 35112e41

History | View | Annotate | Download (53.8 kB)

1
/*
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"
25
#include "kvm.h"
26

    
27
#include "qemu-option.h"
28
#include "qemu-config.h"
29

    
30
#include "qapi/qapi-visit-core.h"
31

    
32
#include "hyperv.h"
33

    
34
/* feature flags taken from "Intel Processor Identification and the CPUID
35
 * Instruction" and AMD's "CPUID Specification".  In cases of disagreement
36
 * between feature naming conventions, aliases may be added.
37
 */
38
static const char *feature_name[] = {
39
    "fpu", "vme", "de", "pse",
40
    "tsc", "msr", "pae", "mce",
41
    "cx8", "apic", NULL, "sep",
42
    "mtrr", "pge", "mca", "cmov",
43
    "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */,
44
    NULL, "ds" /* Intel dts */, "acpi", "mmx",
45
    "fxsr", "sse", "sse2", "ss",
46
    "ht" /* Intel htt */, "tm", "ia64", "pbe",
47
};
48
static const char *ext_feature_name[] = {
49
    "pni|sse3" /* Intel,AMD sse3 */, "pclmulqdq|pclmuldq", "dtes64", "monitor",
50
    "ds_cpl", "vmx", "smx", "est",
51
    "tm2", "ssse3", "cid", NULL,
52
    "fma", "cx16", "xtpr", "pdcm",
53
    NULL, NULL, "dca", "sse4.1|sse4_1",
54
    "sse4.2|sse4_2", "x2apic", "movbe", "popcnt",
55
    "tsc-deadline", "aes", "xsave", "osxsave",
56
    "avx", NULL, NULL, "hypervisor",
57
};
58
static const char *ext2_feature_name[] = {
59
    "fpu", "vme", "de", "pse",
60
    "tsc", "msr", "pae", "mce",
61
    "cx8" /* AMD CMPXCHG8B */, "apic", NULL, "syscall",
62
    "mtrr", "pge", "mca", "cmov",
63
    "pat", "pse36", NULL, NULL /* Linux mp */,
64
    "nx|xd", NULL, "mmxext", "mmx",
65
    "fxsr", "fxsr_opt|ffxsr", "pdpe1gb" /* AMD Page1GB */, "rdtscp",
66
    NULL, "lm|i64", "3dnowext", "3dnow",
67
};
68
static const char *ext3_feature_name[] = {
69
    "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */,
70
    "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse",
71
    "3dnowprefetch", "osvw", "ibs", "xop",
72
    "skinit", "wdt", NULL, NULL,
73
    "fma4", NULL, "cvt16", "nodeid_msr",
74
    NULL, NULL, NULL, NULL,
75
    NULL, NULL, NULL, NULL,
76
    NULL, NULL, NULL, NULL,
77
};
78

    
79
static const char *kvm_feature_name[] = {
80
    "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock", "kvm_asyncpf", NULL, NULL, NULL,
81
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
82
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
83
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
84
};
85

    
86
static const char *svm_feature_name[] = {
87
    "npt", "lbrv", "svm_lock", "nrip_save",
88
    "tsc_scale", "vmcb_clean",  "flushbyasid", "decodeassists",
89
    NULL, NULL, "pause_filter", NULL,
90
    "pfthreshold", NULL, NULL, NULL,
91
    NULL, NULL, NULL, NULL,
92
    NULL, NULL, NULL, NULL,
93
    NULL, NULL, NULL, NULL,
94
    NULL, NULL, NULL, NULL,
95
};
96

    
97
/* collects per-function cpuid data
98
 */
99
typedef struct model_features_t {
100
    uint32_t *guest_feat;
101
    uint32_t *host_feat;
102
    uint32_t check_feat;
103
    const char **flag_names;
104
    uint32_t cpuid;
105
    } model_features_t;
106

    
107
int check_cpuid = 0;
108
int enforce_cpuid = 0;
109

    
110
void host_cpuid(uint32_t function, uint32_t count,
111
                uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
112
{
113
#if defined(CONFIG_KVM)
114
    uint32_t vec[4];
115

    
116
#ifdef __x86_64__
117
    asm volatile("cpuid"
118
                 : "=a"(vec[0]), "=b"(vec[1]),
119
                   "=c"(vec[2]), "=d"(vec[3])
120
                 : "0"(function), "c"(count) : "cc");
121
#else
122
    asm volatile("pusha \n\t"
123
                 "cpuid \n\t"
124
                 "mov %%eax, 0(%2) \n\t"
125
                 "mov %%ebx, 4(%2) \n\t"
126
                 "mov %%ecx, 8(%2) \n\t"
127
                 "mov %%edx, 12(%2) \n\t"
128
                 "popa"
129
                 : : "a"(function), "c"(count), "S"(vec)
130
                 : "memory", "cc");
131
#endif
132

    
133
    if (eax)
134
        *eax = vec[0];
135
    if (ebx)
136
        *ebx = vec[1];
137
    if (ecx)
138
        *ecx = vec[2];
139
    if (edx)
140
        *edx = vec[3];
141
#endif
142
}
143

    
144
#define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c)))
145

    
146
/* general substring compare of *[s1..e1) and *[s2..e2).  sx is start of
147
 * a substring.  ex if !NULL points to the first char after a substring,
148
 * otherwise the string is assumed to sized by a terminating nul.
149
 * Return lexical ordering of *s1:*s2.
150
 */
151
static int sstrcmp(const char *s1, const char *e1, const char *s2,
152
    const char *e2)
153
{
154
    for (;;) {
155
        if (!*s1 || !*s2 || *s1 != *s2)
156
            return (*s1 - *s2);
157
        ++s1, ++s2;
158
        if (s1 == e1 && s2 == e2)
159
            return (0);
160
        else if (s1 == e1)
161
            return (*s2);
162
        else if (s2 == e2)
163
            return (*s1);
164
    }
165
}
166

    
167
/* compare *[s..e) to *altstr.  *altstr may be a simple string or multiple
168
 * '|' delimited (possibly empty) strings in which case search for a match
169
 * within the alternatives proceeds left to right.  Return 0 for success,
170
 * non-zero otherwise.
171
 */
172
static int altcmp(const char *s, const char *e, const char *altstr)
173
{
174
    const char *p, *q;
175

    
176
    for (q = p = altstr; ; ) {
177
        while (*p && *p != '|')
178
            ++p;
179
        if ((q == p && !*s) || (q != p && !sstrcmp(s, e, q, p)))
180
            return (0);
181
        if (!*p)
182
            return (1);
183
        else
184
            q = ++p;
185
    }
186
}
187

    
188
/* search featureset for flag *[s..e), if found set corresponding bit in
189
 * *pval and return true, otherwise return false
190
 */
191
static bool lookup_feature(uint32_t *pval, const char *s, const char *e,
192
                           const char **featureset)
193
{
194
    uint32_t mask;
195
    const char **ppc;
196
    bool found = false;
197

    
198
    for (mask = 1, ppc = featureset; mask; mask <<= 1, ++ppc) {
199
        if (*ppc && !altcmp(s, e, *ppc)) {
200
            *pval |= mask;
201
            found = true;
202
        }
203
    }
204
    return found;
205
}
206

    
207
static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features,
208
                                    uint32_t *ext_features,
209
                                    uint32_t *ext2_features,
210
                                    uint32_t *ext3_features,
211
                                    uint32_t *kvm_features,
212
                                    uint32_t *svm_features)
213
{
214
    if (!lookup_feature(features, flagname, NULL, feature_name) &&
215
        !lookup_feature(ext_features, flagname, NULL, ext_feature_name) &&
216
        !lookup_feature(ext2_features, flagname, NULL, ext2_feature_name) &&
217
        !lookup_feature(ext3_features, flagname, NULL, ext3_feature_name) &&
218
        !lookup_feature(kvm_features, flagname, NULL, kvm_feature_name) &&
219
        !lookup_feature(svm_features, flagname, NULL, svm_feature_name))
220
            fprintf(stderr, "CPU feature %s not found\n", flagname);
221
}
222

    
223
typedef struct x86_def_t {
224
    struct x86_def_t *next;
225
    const char *name;
226
    uint32_t level;
227
    uint32_t vendor1, vendor2, vendor3;
228
    int family;
229
    int model;
230
    int stepping;
231
    int tsc_khz;
232
    uint32_t features, ext_features, ext2_features, ext3_features;
233
    uint32_t kvm_features, svm_features;
234
    uint32_t xlevel;
235
    char model_id[48];
236
    int vendor_override;
237
    uint32_t flags;
238
    /* Store the results of Centaur's CPUID instructions */
239
    uint32_t ext4_features;
240
    uint32_t xlevel2;
241
} x86_def_t;
242

    
243
#define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE)
244
#define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \
245
          CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC)
246
#define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \
247
          CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
248
          CPUID_PSE36 | CPUID_FXSR)
249
#define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE)
250
#define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \
251
          CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \
252
          CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \
253
          CPUID_PAE | CPUID_SEP | CPUID_APIC)
254
#define EXT2_FEATURE_MASK 0x0183F3FF
255

    
256
#define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \
257
          CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \
258
          CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \
259
          CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \
260
          CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS)
261
          /* partly implemented:
262
          CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64)
263
          CPUID_PSE36 (needed for Solaris) */
264
          /* missing:
265
          CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */
266
#define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \
267
          CPUID_EXT_CX16 | CPUID_EXT_POPCNT | \
268
          CPUID_EXT_HYPERVISOR)
269
          /* missing:
270
          CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST,
271
          CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_XSAVE */
272
#define TCG_EXT2_FEATURES ((TCG_FEATURES & EXT2_FEATURE_MASK) | \
273
          CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
274
          CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT)
275
          /* missing:
276
          CPUID_EXT2_PDPE1GB */
277
#define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
278
          CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A)
279
#define TCG_SVM_FEATURES 0
280

    
281
/* maintains list of cpu model definitions
282
 */
283
static x86_def_t *x86_defs = {NULL};
284

    
285
/* built-in cpu model definitions (deprecated)
286
 */
287
static x86_def_t builtin_x86_defs[] = {
288
    {
289
        .name = "qemu64",
290
        .level = 4,
291
        .vendor1 = CPUID_VENDOR_AMD_1,
292
        .vendor2 = CPUID_VENDOR_AMD_2,
293
        .vendor3 = CPUID_VENDOR_AMD_3,
294
        .family = 6,
295
        .model = 2,
296
        .stepping = 3,
297
        .features = PPRO_FEATURES |
298
            CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
299
            CPUID_PSE36,
300
        .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT,
301
        .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) |
302
            CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
303
        .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
304
            CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
305
        .xlevel = 0x8000000A,
306
        .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
307
    },
308
    {
309
        .name = "phenom",
310
        .level = 5,
311
        .vendor1 = CPUID_VENDOR_AMD_1,
312
        .vendor2 = CPUID_VENDOR_AMD_2,
313
        .vendor3 = CPUID_VENDOR_AMD_3,
314
        .family = 16,
315
        .model = 2,
316
        .stepping = 3,
317
        .features = PPRO_FEATURES |
318
            CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
319
            CPUID_PSE36 | CPUID_VME | CPUID_HT,
320
        .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_CX16 |
321
            CPUID_EXT_POPCNT,
322
        .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) |
323
            CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX |
324
            CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT |
325
            CPUID_EXT2_FFXSR | CPUID_EXT2_PDPE1GB | CPUID_EXT2_RDTSCP,
326
        /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
327
                    CPUID_EXT3_CR8LEG,
328
                    CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
329
                    CPUID_EXT3_OSVW, CPUID_EXT3_IBS */
330
        .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM |
331
            CPUID_EXT3_ABM | CPUID_EXT3_SSE4A,
332
        .svm_features = CPUID_SVM_NPT | CPUID_SVM_LBRV,
333
        .xlevel = 0x8000001A,
334
        .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor"
335
    },
336
    {
337
        .name = "core2duo",
338
        .level = 10,
339
        .family = 6,
340
        .model = 15,
341
        .stepping = 11,
342
        .features = PPRO_FEATURES |
343
            CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
344
            CPUID_PSE36 | CPUID_VME | CPUID_DTS | CPUID_ACPI | CPUID_SS |
345
            CPUID_HT | CPUID_TM | CPUID_PBE,
346
        .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
347
            CPUID_EXT_DTES64 | CPUID_EXT_DSCPL | CPUID_EXT_VMX | CPUID_EXT_EST |
348
            CPUID_EXT_TM2 | CPUID_EXT_CX16 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
349
        .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
350
        .ext3_features = CPUID_EXT3_LAHF_LM,
351
        .xlevel = 0x80000008,
352
        .model_id = "Intel(R) Core(TM)2 Duo CPU     T7700  @ 2.40GHz",
353
    },
354
    {
355
        .name = "kvm64",
356
        .level = 5,
357
        .vendor1 = CPUID_VENDOR_INTEL_1,
358
        .vendor2 = CPUID_VENDOR_INTEL_2,
359
        .vendor3 = CPUID_VENDOR_INTEL_3,
360
        .family = 15,
361
        .model = 6,
362
        .stepping = 1,
363
        /* Missing: CPUID_VME, CPUID_HT */
364
        .features = PPRO_FEATURES |
365
            CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA |
366
            CPUID_PSE36,
367
        /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */
368
        .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16,
369
        /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */
370
        .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) |
371
            CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX,
372
        /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC,
373
                    CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A,
374
                    CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH,
375
                    CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */
376
        .ext3_features = 0,
377
        .xlevel = 0x80000008,
378
        .model_id = "Common KVM processor"
379
    },
380
    {
381
        .name = "qemu32",
382
        .level = 4,
383
        .family = 6,
384
        .model = 3,
385
        .stepping = 3,
386
        .features = PPRO_FEATURES,
387
        .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_POPCNT,
388
        .xlevel = 0x80000004,
389
        .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
390
    },
391
    {
392
        .name = "kvm32",
393
        .level = 5,
394
        .family = 15,
395
        .model = 6,
396
        .stepping = 1,
397
        .features = PPRO_FEATURES |
398
            CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_PSE36,
399
        .ext_features = CPUID_EXT_SSE3,
400
        .ext2_features = PPRO_FEATURES & EXT2_FEATURE_MASK,
401
        .ext3_features = 0,
402
        .xlevel = 0x80000008,
403
        .model_id = "Common 32-bit KVM processor"
404
    },
405
    {
406
        .name = "coreduo",
407
        .level = 10,
408
        .family = 6,
409
        .model = 14,
410
        .stepping = 8,
411
        .features = PPRO_FEATURES | CPUID_VME |
412
            CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_DTS | CPUID_ACPI |
413
            CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
414
        .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_VMX |
415
            CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR | CPUID_EXT_PDCM,
416
        .ext2_features = CPUID_EXT2_NX,
417
        .xlevel = 0x80000008,
418
        .model_id = "Genuine Intel(R) CPU           T2600  @ 2.16GHz",
419
    },
420
    {
421
        .name = "486",
422
        .level = 1,
423
        .family = 4,
424
        .model = 0,
425
        .stepping = 0,
426
        .features = I486_FEATURES,
427
        .xlevel = 0,
428
    },
429
    {
430
        .name = "pentium",
431
        .level = 1,
432
        .family = 5,
433
        .model = 4,
434
        .stepping = 3,
435
        .features = PENTIUM_FEATURES,
436
        .xlevel = 0,
437
    },
438
    {
439
        .name = "pentium2",
440
        .level = 2,
441
        .family = 6,
442
        .model = 5,
443
        .stepping = 2,
444
        .features = PENTIUM2_FEATURES,
445
        .xlevel = 0,
446
    },
447
    {
448
        .name = "pentium3",
449
        .level = 2,
450
        .family = 6,
451
        .model = 7,
452
        .stepping = 3,
453
        .features = PENTIUM3_FEATURES,
454
        .xlevel = 0,
455
    },
456
    {
457
        .name = "athlon",
458
        .level = 2,
459
        .vendor1 = CPUID_VENDOR_AMD_1,
460
        .vendor2 = CPUID_VENDOR_AMD_2,
461
        .vendor3 = CPUID_VENDOR_AMD_3,
462
        .family = 6,
463
        .model = 2,
464
        .stepping = 3,
465
        .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR | CPUID_MCA,
466
        .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT,
467
        .xlevel = 0x80000008,
468
        /* XXX: put another string ? */
469
        .model_id = "QEMU Virtual CPU version " QEMU_VERSION,
470
    },
471
    {
472
        .name = "n270",
473
        /* original is on level 10 */
474
        .level = 5,
475
        .family = 6,
476
        .model = 28,
477
        .stepping = 2,
478
        .features = PPRO_FEATURES |
479
            CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME | CPUID_DTS |
480
            CPUID_ACPI | CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE,
481
            /* Some CPUs got no CPUID_SEP */
482
        .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 |
483
            CPUID_EXT_DSCPL | CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR,
484
        .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | CPUID_EXT2_NX,
485
        .ext3_features = CPUID_EXT3_LAHF_LM,
486
        .xlevel = 0x8000000A,
487
        .model_id = "Intel(R) Atom(TM) CPU N270   @ 1.60GHz",
488
    },
489
};
490

    
491
static int cpu_x86_fill_model_id(char *str)
492
{
493
    uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
494
    int i;
495

    
496
    for (i = 0; i < 3; i++) {
497
        host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx);
498
        memcpy(str + i * 16 +  0, &eax, 4);
499
        memcpy(str + i * 16 +  4, &ebx, 4);
500
        memcpy(str + i * 16 +  8, &ecx, 4);
501
        memcpy(str + i * 16 + 12, &edx, 4);
502
    }
503
    return 0;
504
}
505

    
506
static int cpu_x86_fill_host(x86_def_t *x86_cpu_def)
507
{
508
    uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
509

    
510
    x86_cpu_def->name = "host";
511
    host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
512
    x86_cpu_def->level = eax;
513
    x86_cpu_def->vendor1 = ebx;
514
    x86_cpu_def->vendor2 = edx;
515
    x86_cpu_def->vendor3 = ecx;
516

    
517
    host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
518
    x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
519
    x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
520
    x86_cpu_def->stepping = eax & 0x0F;
521
    x86_cpu_def->ext_features = ecx;
522
    x86_cpu_def->features = edx;
523

    
524
    host_cpuid(0x80000000, 0, &eax, &ebx, &ecx, &edx);
525
    x86_cpu_def->xlevel = eax;
526

    
527
    host_cpuid(0x80000001, 0, &eax, &ebx, &ecx, &edx);
528
    x86_cpu_def->ext2_features = edx;
529
    x86_cpu_def->ext3_features = ecx;
530
    cpu_x86_fill_model_id(x86_cpu_def->model_id);
531
    x86_cpu_def->vendor_override = 0;
532

    
533
    /* Call Centaur's CPUID instruction. */
534
    if (x86_cpu_def->vendor1 == CPUID_VENDOR_VIA_1 &&
535
        x86_cpu_def->vendor2 == CPUID_VENDOR_VIA_2 &&
536
        x86_cpu_def->vendor3 == CPUID_VENDOR_VIA_3) {
537
        host_cpuid(0xC0000000, 0, &eax, &ebx, &ecx, &edx);
538
        if (eax >= 0xC0000001) {
539
            /* Support VIA max extended level */
540
            x86_cpu_def->xlevel2 = eax;
541
            host_cpuid(0xC0000001, 0, &eax, &ebx, &ecx, &edx);
542
            x86_cpu_def->ext4_features = edx;
543
        }
544
    }
545

    
546
    /*
547
     * Every SVM feature requires emulation support in KVM - so we can't just
548
     * read the host features here. KVM might even support SVM features not
549
     * available on the host hardware. Just set all bits and mask out the
550
     * unsupported ones later.
551
     */
552
    x86_cpu_def->svm_features = -1;
553

    
554
    return 0;
555
}
556

    
557
static int unavailable_host_feature(struct model_features_t *f, uint32_t mask)
558
{
559
    int i;
560

    
561
    for (i = 0; i < 32; ++i)
562
        if (1 << i & mask) {
563
            fprintf(stderr, "warning: host cpuid %04x_%04x lacks requested"
564
                " flag '%s' [0x%08x]\n",
565
                f->cpuid >> 16, f->cpuid & 0xffff,
566
                f->flag_names[i] ? f->flag_names[i] : "[reserved]", mask);
567
            break;
568
        }
569
    return 0;
570
}
571

    
572
/* best effort attempt to inform user requested cpu flags aren't making
573
 * their way to the guest.  Note: ft[].check_feat ideally should be
574
 * specified via a guest_def field to suppress report of extraneous flags.
575
 */
576
static int check_features_against_host(x86_def_t *guest_def)
577
{
578
    x86_def_t host_def;
579
    uint32_t mask;
580
    int rv, i;
581
    struct model_features_t ft[] = {
582
        {&guest_def->features, &host_def.features,
583
            ~0, feature_name, 0x00000000},
584
        {&guest_def->ext_features, &host_def.ext_features,
585
            ~CPUID_EXT_HYPERVISOR, ext_feature_name, 0x00000001},
586
        {&guest_def->ext2_features, &host_def.ext2_features,
587
            ~PPRO_FEATURES, ext2_feature_name, 0x80000000},
588
        {&guest_def->ext3_features, &host_def.ext3_features,
589
            ~CPUID_EXT3_SVM, ext3_feature_name, 0x80000001}};
590

    
591
    cpu_x86_fill_host(&host_def);
592
    for (rv = 0, i = 0; i < ARRAY_SIZE(ft); ++i)
593
        for (mask = 1; mask; mask <<= 1)
594
            if (ft[i].check_feat & mask && *ft[i].guest_feat & mask &&
595
                !(*ft[i].host_feat & mask)) {
596
                    unavailable_host_feature(&ft[i], mask);
597
                    rv = 1;
598
                }
599
    return rv;
600
}
601

    
602
static void x86_cpuid_version_get_family(Object *obj, Visitor *v, void *opaque,
603
                                         const char *name, Error **errp)
604
{
605
    X86CPU *cpu = X86_CPU(obj);
606
    CPUX86State *env = &cpu->env;
607
    int64_t value;
608

    
609
    value = (env->cpuid_version >> 8) & 0xf;
610
    if (value == 0xf) {
611
        value += (env->cpuid_version >> 20) & 0xff;
612
    }
613
    visit_type_int(v, &value, name, errp);
614
}
615

    
616
static void x86_cpuid_version_set_family(Object *obj, Visitor *v, void *opaque,
617
                                         const char *name, Error **errp)
618
{
619
    X86CPU *cpu = X86_CPU(obj);
620
    CPUX86State *env = &cpu->env;
621
    const int64_t min = 0;
622
    const int64_t max = 0xff + 0xf;
623
    int64_t value;
624

    
625
    visit_type_int(v, &value, name, errp);
626
    if (error_is_set(errp)) {
627
        return;
628
    }
629
    if (value < min || value > max) {
630
        error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
631
                  name ? name : "null", value, min, max);
632
        return;
633
    }
634

    
635
    env->cpuid_version &= ~0xff00f00;
636
    if (value > 0x0f) {
637
        env->cpuid_version |= 0xf00 | ((value - 0x0f) << 20);
638
    } else {
639
        env->cpuid_version |= value << 8;
640
    }
641
}
642

    
643
static void x86_cpuid_version_get_model(Object *obj, Visitor *v, void *opaque,
644
                                        const char *name, Error **errp)
645
{
646
    X86CPU *cpu = X86_CPU(obj);
647
    CPUX86State *env = &cpu->env;
648
    int64_t value;
649

    
650
    value = (env->cpuid_version >> 4) & 0xf;
651
    value |= ((env->cpuid_version >> 16) & 0xf) << 4;
652
    visit_type_int(v, &value, name, errp);
653
}
654

    
655
static void x86_cpuid_version_set_model(Object *obj, Visitor *v, void *opaque,
656
                                        const char *name, Error **errp)
657
{
658
    X86CPU *cpu = X86_CPU(obj);
659
    CPUX86State *env = &cpu->env;
660
    const int64_t min = 0;
661
    const int64_t max = 0xff;
662
    int64_t value;
663

    
664
    visit_type_int(v, &value, name, errp);
665
    if (error_is_set(errp)) {
666
        return;
667
    }
668
    if (value < min || value > max) {
669
        error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
670
                  name ? name : "null", value, min, max);
671
        return;
672
    }
673

    
674
    env->cpuid_version &= ~0xf00f0;
675
    env->cpuid_version |= ((value & 0xf) << 4) | ((value >> 4) << 16);
676
}
677

    
678
static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v,
679
                                           void *opaque, const char *name,
680
                                           Error **errp)
681
{
682
    X86CPU *cpu = X86_CPU(obj);
683
    CPUX86State *env = &cpu->env;
684
    int64_t value;
685

    
686
    value = env->cpuid_version & 0xf;
687
    visit_type_int(v, &value, name, errp);
688
}
689

    
690
static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
691
                                           void *opaque, const char *name,
692
                                           Error **errp)
693
{
694
    X86CPU *cpu = X86_CPU(obj);
695
    CPUX86State *env = &cpu->env;
696
    const int64_t min = 0;
697
    const int64_t max = 0xf;
698
    int64_t value;
699

    
700
    visit_type_int(v, &value, name, errp);
701
    if (error_is_set(errp)) {
702
        return;
703
    }
704
    if (value < min || value > max) {
705
        error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
706
                  name ? name : "null", value, min, max);
707
        return;
708
    }
709

    
710
    env->cpuid_version &= ~0xf;
711
    env->cpuid_version |= value & 0xf;
712
}
713

    
714
static void x86_cpuid_set_model_id(Object *obj, const char *model_id,
715
                                   Error **errp)
716
{
717
    X86CPU *cpu = X86_CPU(obj);
718
    CPUX86State *env = &cpu->env;
719
    int c, len, i;
720

    
721
    if (model_id == NULL) {
722
        model_id = "";
723
    }
724
    len = strlen(model_id);
725
    memset(env->cpuid_model, 0, 48);
726
    for (i = 0; i < 48; i++) {
727
        if (i >= len) {
728
            c = '\0';
729
        } else {
730
            c = (uint8_t)model_id[i];
731
        }
732
        env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
733
    }
734
}
735

    
736
static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
737
{
738
    unsigned int i;
739
    x86_def_t *def;
740

    
741
    char *s = g_strdup(cpu_model);
742
    char *featurestr, *name = strtok(s, ",");
743
    /* Features to be added*/
744
    uint32_t plus_features = 0, plus_ext_features = 0;
745
    uint32_t plus_ext2_features = 0, plus_ext3_features = 0;
746
    uint32_t plus_kvm_features = 0, plus_svm_features = 0;
747
    /* Features to be removed */
748
    uint32_t minus_features = 0, minus_ext_features = 0;
749
    uint32_t minus_ext2_features = 0, minus_ext3_features = 0;
750
    uint32_t minus_kvm_features = 0, minus_svm_features = 0;
751
    uint32_t numvalue;
752

    
753
    for (def = x86_defs; def; def = def->next)
754
        if (name && !strcmp(name, def->name))
755
            break;
756
    if (kvm_enabled() && name && strcmp(name, "host") == 0) {
757
        cpu_x86_fill_host(x86_cpu_def);
758
    } else if (!def) {
759
        goto error;
760
    } else {
761
        memcpy(x86_cpu_def, def, sizeof(*def));
762
    }
763

    
764
    plus_kvm_features = ~0; /* not supported bits will be filtered out later */
765

    
766
    add_flagname_to_bitmaps("hypervisor", &plus_features,
767
        &plus_ext_features, &plus_ext2_features, &plus_ext3_features,
768
        &plus_kvm_features, &plus_svm_features);
769

    
770
    featurestr = strtok(NULL, ",");
771

    
772
    while (featurestr) {
773
        char *val;
774
        if (featurestr[0] == '+') {
775
            add_flagname_to_bitmaps(featurestr + 1, &plus_features,
776
                            &plus_ext_features, &plus_ext2_features,
777
                            &plus_ext3_features, &plus_kvm_features,
778
                            &plus_svm_features);
779
        } else if (featurestr[0] == '-') {
780
            add_flagname_to_bitmaps(featurestr + 1, &minus_features,
781
                            &minus_ext_features, &minus_ext2_features,
782
                            &minus_ext3_features, &minus_kvm_features,
783
                            &minus_svm_features);
784
        } else if ((val = strchr(featurestr, '='))) {
785
            *val = 0; val++;
786
            if (!strcmp(featurestr, "family")) {
787
                char *err;
788
                numvalue = strtoul(val, &err, 0);
789
                if (!*val || *err || numvalue > 0xff + 0xf) {
790
                    fprintf(stderr, "bad numerical value %s\n", val);
791
                    goto error;
792
                }
793
                x86_cpu_def->family = numvalue;
794
            } else if (!strcmp(featurestr, "model")) {
795
                char *err;
796
                numvalue = strtoul(val, &err, 0);
797
                if (!*val || *err || numvalue > 0xff) {
798
                    fprintf(stderr, "bad numerical value %s\n", val);
799
                    goto error;
800
                }
801
                x86_cpu_def->model = numvalue;
802
            } else if (!strcmp(featurestr, "stepping")) {
803
                char *err;
804
                numvalue = strtoul(val, &err, 0);
805
                if (!*val || *err || numvalue > 0xf) {
806
                    fprintf(stderr, "bad numerical value %s\n", val);
807
                    goto error;
808
                }
809
                x86_cpu_def->stepping = numvalue ;
810
            } else if (!strcmp(featurestr, "level")) {
811
                char *err;
812
                numvalue = strtoul(val, &err, 0);
813
                if (!*val || *err) {
814
                    fprintf(stderr, "bad numerical value %s\n", val);
815
                    goto error;
816
                }
817
                x86_cpu_def->level = numvalue;
818
            } else if (!strcmp(featurestr, "xlevel")) {
819
                char *err;
820
                numvalue = strtoul(val, &err, 0);
821
                if (!*val || *err) {
822
                    fprintf(stderr, "bad numerical value %s\n", val);
823
                    goto error;
824
                }
825
                if (numvalue < 0x80000000) {
826
                    numvalue += 0x80000000;
827
                }
828
                x86_cpu_def->xlevel = numvalue;
829
            } else if (!strcmp(featurestr, "vendor")) {
830
                if (strlen(val) != 12) {
831
                    fprintf(stderr, "vendor string must be 12 chars long\n");
832
                    goto error;
833
                }
834
                x86_cpu_def->vendor1 = 0;
835
                x86_cpu_def->vendor2 = 0;
836
                x86_cpu_def->vendor3 = 0;
837
                for(i = 0; i < 4; i++) {
838
                    x86_cpu_def->vendor1 |= ((uint8_t)val[i    ]) << (8 * i);
839
                    x86_cpu_def->vendor2 |= ((uint8_t)val[i + 4]) << (8 * i);
840
                    x86_cpu_def->vendor3 |= ((uint8_t)val[i + 8]) << (8 * i);
841
                }
842
                x86_cpu_def->vendor_override = 1;
843
            } else if (!strcmp(featurestr, "model_id")) {
844
                pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
845
                        val);
846
            } else if (!strcmp(featurestr, "tsc_freq")) {
847
                int64_t tsc_freq;
848
                char *err;
849

    
850
                tsc_freq = strtosz_suffix_unit(val, &err,
851
                                               STRTOSZ_DEFSUFFIX_B, 1000);
852
                if (tsc_freq < 0 || *err) {
853
                    fprintf(stderr, "bad numerical value %s\n", val);
854
                    goto error;
855
                }
856
                x86_cpu_def->tsc_khz = tsc_freq / 1000;
857
            } else if (!strcmp(featurestr, "hv_spinlocks")) {
858
                char *err;
859
                numvalue = strtoul(val, &err, 0);
860
                if (!*val || *err) {
861
                    fprintf(stderr, "bad numerical value %s\n", val);
862
                    goto error;
863
                }
864
                hyperv_set_spinlock_retries(numvalue);
865
            } else {
866
                fprintf(stderr, "unrecognized feature %s\n", featurestr);
867
                goto error;
868
            }
869
        } else if (!strcmp(featurestr, "check")) {
870
            check_cpuid = 1;
871
        } else if (!strcmp(featurestr, "enforce")) {
872
            check_cpuid = enforce_cpuid = 1;
873
        } else if (!strcmp(featurestr, "hv_relaxed")) {
874
            hyperv_enable_relaxed_timing(true);
875
        } else if (!strcmp(featurestr, "hv_vapic")) {
876
            hyperv_enable_vapic_recommended(true);
877
        } else {
878
            fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
879
            goto error;
880
        }
881
        featurestr = strtok(NULL, ",");
882
    }
883
    x86_cpu_def->features |= plus_features;
884
    x86_cpu_def->ext_features |= plus_ext_features;
885
    x86_cpu_def->ext2_features |= plus_ext2_features;
886
    x86_cpu_def->ext3_features |= plus_ext3_features;
887
    x86_cpu_def->kvm_features |= plus_kvm_features;
888
    x86_cpu_def->svm_features |= plus_svm_features;
889
    x86_cpu_def->features &= ~minus_features;
890
    x86_cpu_def->ext_features &= ~minus_ext_features;
891
    x86_cpu_def->ext2_features &= ~minus_ext2_features;
892
    x86_cpu_def->ext3_features &= ~minus_ext3_features;
893
    x86_cpu_def->kvm_features &= ~minus_kvm_features;
894
    x86_cpu_def->svm_features &= ~minus_svm_features;
895
    if (check_cpuid) {
896
        if (check_features_against_host(x86_cpu_def) && enforce_cpuid)
897
            goto error;
898
    }
899
    g_free(s);
900
    return 0;
901

    
902
error:
903
    g_free(s);
904
    return -1;
905
}
906

    
907
/* generate a composite string into buf of all cpuid names in featureset
908
 * selected by fbits.  indicate truncation at bufsize in the event of overflow.
909
 * if flags, suppress names undefined in featureset.
910
 */
911
static void listflags(char *buf, int bufsize, uint32_t fbits,
912
    const char **featureset, uint32_t flags)
913
{
914
    const char **p = &featureset[31];
915
    char *q, *b, bit;
916
    int nc;
917

    
918
    b = 4 <= bufsize ? buf + (bufsize -= 3) - 1 : NULL;
919
    *buf = '\0';
920
    for (q = buf, bit = 31; fbits && bufsize; --p, fbits &= ~(1 << bit), --bit)
921
        if (fbits & 1 << bit && (*p || !flags)) {
922
            if (*p)
923
                nc = snprintf(q, bufsize, "%s%s", q == buf ? "" : " ", *p);
924
            else
925
                nc = snprintf(q, bufsize, "%s[%d]", q == buf ? "" : " ", bit);
926
            if (bufsize <= nc) {
927
                if (b) {
928
                    memcpy(b, "...", sizeof("..."));
929
                }
930
                return;
931
            }
932
            q += nc;
933
            bufsize -= nc;
934
        }
935
}
936

    
937
/* generate CPU information:
938
 * -?        list model names
939
 * -?model   list model names/IDs
940
 * -?dump    output all model (x86_def_t) data
941
 * -?cpuid   list all recognized cpuid flag names
942
 */
943
void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
944
{
945
    unsigned char model = !strcmp("?model", optarg);
946
    unsigned char dump = !strcmp("?dump", optarg);
947
    unsigned char cpuid = !strcmp("?cpuid", optarg);
948
    x86_def_t *def;
949
    char buf[256];
950

    
951
    if (cpuid) {
952
        (*cpu_fprintf)(f, "Recognized CPUID flags:\n");
953
        listflags(buf, sizeof (buf), (uint32_t)~0, feature_name, 1);
954
        (*cpu_fprintf)(f, "  f_edx: %s\n", buf);
955
        listflags(buf, sizeof (buf), (uint32_t)~0, ext_feature_name, 1);
956
        (*cpu_fprintf)(f, "  f_ecx: %s\n", buf);
957
        listflags(buf, sizeof (buf), (uint32_t)~0, ext2_feature_name, 1);
958
        (*cpu_fprintf)(f, "  extf_edx: %s\n", buf);
959
        listflags(buf, sizeof (buf), (uint32_t)~0, ext3_feature_name, 1);
960
        (*cpu_fprintf)(f, "  extf_ecx: %s\n", buf);
961
        return;
962
    }
963
    for (def = x86_defs; def; def = def->next) {
964
        snprintf(buf, sizeof (buf), def->flags ? "[%s]": "%s", def->name);
965
        if (model || dump) {
966
            (*cpu_fprintf)(f, "x86 %16s  %-48s\n", buf, def->model_id);
967
        } else {
968
            (*cpu_fprintf)(f, "x86 %16s\n", buf);
969
        }
970
        if (dump) {
971
            memcpy(buf, &def->vendor1, sizeof (def->vendor1));
972
            memcpy(buf + 4, &def->vendor2, sizeof (def->vendor2));
973
            memcpy(buf + 8, &def->vendor3, sizeof (def->vendor3));
974
            buf[12] = '\0';
975
            (*cpu_fprintf)(f,
976
                "  family %d model %d stepping %d level %d xlevel 0x%x"
977
                " vendor \"%s\"\n",
978
                def->family, def->model, def->stepping, def->level,
979
                def->xlevel, buf);
980
            listflags(buf, sizeof (buf), def->features, feature_name, 0);
981
            (*cpu_fprintf)(f, "  feature_edx %08x (%s)\n", def->features,
982
                buf);
983
            listflags(buf, sizeof (buf), def->ext_features, ext_feature_name,
984
                0);
985
            (*cpu_fprintf)(f, "  feature_ecx %08x (%s)\n", def->ext_features,
986
                buf);
987
            listflags(buf, sizeof (buf), def->ext2_features, ext2_feature_name,
988
                0);
989
            (*cpu_fprintf)(f, "  extfeature_edx %08x (%s)\n",
990
                def->ext2_features, buf);
991
            listflags(buf, sizeof (buf), def->ext3_features, ext3_feature_name,
992
                0);
993
            (*cpu_fprintf)(f, "  extfeature_ecx %08x (%s)\n",
994
                def->ext3_features, buf);
995
            (*cpu_fprintf)(f, "\n");
996
        }
997
    }
998
    if (kvm_enabled()) {
999
        (*cpu_fprintf)(f, "x86 %16s\n", "[host]");
1000
    }
1001
}
1002

    
1003
int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
1004
{
1005
    CPUX86State *env = &cpu->env;
1006
    x86_def_t def1, *def = &def1;
1007
    Error *error = NULL;
1008

    
1009
    memset(def, 0, sizeof(*def));
1010

    
1011
    if (cpu_x86_find_by_name(def, cpu_model) < 0)
1012
        return -1;
1013
    if (def->vendor1) {
1014
        env->cpuid_vendor1 = def->vendor1;
1015
        env->cpuid_vendor2 = def->vendor2;
1016
        env->cpuid_vendor3 = def->vendor3;
1017
    } else {
1018
        env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1;
1019
        env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2;
1020
        env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3;
1021
    }
1022
    env->cpuid_vendor_override = def->vendor_override;
1023
    env->cpuid_level = def->level;
1024
    object_property_set_int(OBJECT(cpu), def->family, "family", &error);
1025
    object_property_set_int(OBJECT(cpu), def->model, "model", &error);
1026
    object_property_set_int(OBJECT(cpu), def->stepping, "stepping", &error);
1027
    env->cpuid_features = def->features;
1028
    env->cpuid_ext_features = def->ext_features;
1029
    env->cpuid_ext2_features = def->ext2_features;
1030
    env->cpuid_ext3_features = def->ext3_features;
1031
    env->cpuid_xlevel = def->xlevel;
1032
    env->cpuid_kvm_features = def->kvm_features;
1033
    env->cpuid_svm_features = def->svm_features;
1034
    env->cpuid_ext4_features = def->ext4_features;
1035
    env->cpuid_xlevel2 = def->xlevel2;
1036
    env->tsc_khz = def->tsc_khz;
1037
    if (!kvm_enabled()) {
1038
        env->cpuid_features &= TCG_FEATURES;
1039
        env->cpuid_ext_features &= TCG_EXT_FEATURES;
1040
        env->cpuid_ext2_features &= (TCG_EXT2_FEATURES
1041
#ifdef TARGET_X86_64
1042
            | CPUID_EXT2_SYSCALL | CPUID_EXT2_LM
1043
#endif
1044
            );
1045
        env->cpuid_ext3_features &= TCG_EXT3_FEATURES;
1046
        env->cpuid_svm_features &= TCG_SVM_FEATURES;
1047
    }
1048
    object_property_set_str(OBJECT(cpu), def->model_id, "model-id", &error);
1049
    if (error_is_set(&error)) {
1050
        error_free(error);
1051
        return -1;
1052
    }
1053
    return 0;
1054
}
1055

    
1056
#if !defined(CONFIG_USER_ONLY)
1057
/* copy vendor id string to 32 bit register, nul pad as needed
1058
 */
1059
static void cpyid(const char *s, uint32_t *id)
1060
{
1061
    char *d = (char *)id;
1062
    char i;
1063

    
1064
    for (i = sizeof (*id); i--; )
1065
        *d++ = *s ? *s++ : '\0';
1066
}
1067

    
1068
/* interpret radix and convert from string to arbitrary scalar,
1069
 * otherwise flag failure
1070
 */
1071
#define setscalar(pval, str, perr)                      \
1072
{                                                       \
1073
    char *pend;                                         \
1074
    unsigned long ul;                                   \
1075
                                                        \
1076
    ul = strtoul(str, &pend, 0);                        \
1077
    *str && !*pend ? (*pval = ul) : (*perr = 1);        \
1078
}
1079

    
1080
/* map cpuid options to feature bits, otherwise return failure
1081
 * (option tags in *str are delimited by whitespace)
1082
 */
1083
static void setfeatures(uint32_t *pval, const char *str,
1084
    const char **featureset, int *perr)
1085
{
1086
    const char *p, *q;
1087

    
1088
    for (q = p = str; *p || *q; q = p) {
1089
        while (iswhite(*p))
1090
            q = ++p;
1091
        while (*p && !iswhite(*p))
1092
            ++p;
1093
        if (!*q && !*p)
1094
            return;
1095
        if (!lookup_feature(pval, q, p, featureset)) {
1096
            fprintf(stderr, "error: feature \"%.*s\" not available in set\n",
1097
                (int)(p - q), q);
1098
            *perr = 1;
1099
            return;
1100
        }
1101
    }
1102
}
1103

    
1104
/* map config file options to x86_def_t form
1105
 */
1106
static int cpudef_setfield(const char *name, const char *str, void *opaque)
1107
{
1108
    x86_def_t *def = opaque;
1109
    int err = 0;
1110

    
1111
    if (!strcmp(name, "name")) {
1112
        g_free((void *)def->name);
1113
        def->name = g_strdup(str);
1114
    } else if (!strcmp(name, "model_id")) {
1115
        strncpy(def->model_id, str, sizeof (def->model_id));
1116
    } else if (!strcmp(name, "level")) {
1117
        setscalar(&def->level, str, &err)
1118
    } else if (!strcmp(name, "vendor")) {
1119
        cpyid(&str[0], &def->vendor1);
1120
        cpyid(&str[4], &def->vendor2);
1121
        cpyid(&str[8], &def->vendor3);
1122
    } else if (!strcmp(name, "family")) {
1123
        setscalar(&def->family, str, &err)
1124
    } else if (!strcmp(name, "model")) {
1125
        setscalar(&def->model, str, &err)
1126
    } else if (!strcmp(name, "stepping")) {
1127
        setscalar(&def->stepping, str, &err)
1128
    } else if (!strcmp(name, "feature_edx")) {
1129
        setfeatures(&def->features, str, feature_name, &err);
1130
    } else if (!strcmp(name, "feature_ecx")) {
1131
        setfeatures(&def->ext_features, str, ext_feature_name, &err);
1132
    } else if (!strcmp(name, "extfeature_edx")) {
1133
        setfeatures(&def->ext2_features, str, ext2_feature_name, &err);
1134
    } else if (!strcmp(name, "extfeature_ecx")) {
1135
        setfeatures(&def->ext3_features, str, ext3_feature_name, &err);
1136
    } else if (!strcmp(name, "xlevel")) {
1137
        setscalar(&def->xlevel, str, &err)
1138
    } else {
1139
        fprintf(stderr, "error: unknown option [%s = %s]\n", name, str);
1140
        return (1);
1141
    }
1142
    if (err) {
1143
        fprintf(stderr, "error: bad option value [%s = %s]\n", name, str);
1144
        return (1);
1145
    }
1146
    return (0);
1147
}
1148

    
1149
/* register config file entry as x86_def_t
1150
 */
1151
static int cpudef_register(QemuOpts *opts, void *opaque)
1152
{
1153
    x86_def_t *def = g_malloc0(sizeof (x86_def_t));
1154

    
1155
    qemu_opt_foreach(opts, cpudef_setfield, def, 1);
1156
    def->next = x86_defs;
1157
    x86_defs = def;
1158
    return (0);
1159
}
1160

    
1161
void cpu_clear_apic_feature(CPUX86State *env)
1162
{
1163
    env->cpuid_features &= ~CPUID_APIC;
1164
}
1165

    
1166
#endif /* !CONFIG_USER_ONLY */
1167

    
1168
/* register "cpudef" models defined in configuration file.  Here we first
1169
 * preload any built-in definitions
1170
 */
1171
void x86_cpudef_setup(void)
1172
{
1173
    int i;
1174

    
1175
    for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); ++i) {
1176
        builtin_x86_defs[i].next = x86_defs;
1177
        builtin_x86_defs[i].flags = 1;
1178
        x86_defs = &builtin_x86_defs[i];
1179
    }
1180
#if !defined(CONFIG_USER_ONLY)
1181
    qemu_opts_foreach(qemu_find_opts("cpudef"), cpudef_register, NULL, 0);
1182
#endif
1183
}
1184

    
1185
static void get_cpuid_vendor(CPUX86State *env, uint32_t *ebx,
1186
                             uint32_t *ecx, uint32_t *edx)
1187
{
1188
    *ebx = env->cpuid_vendor1;
1189
    *edx = env->cpuid_vendor2;
1190
    *ecx = env->cpuid_vendor3;
1191

    
1192
    /* sysenter isn't supported on compatibility mode on AMD, syscall
1193
     * isn't supported in compatibility mode on Intel.
1194
     * Normally we advertise the actual cpu vendor, but you can override
1195
     * this if you want to use KVM's sysenter/syscall emulation
1196
     * in compatibility mode and when doing cross vendor migration
1197
     */
1198
    if (kvm_enabled() && ! env->cpuid_vendor_override) {
1199
        host_cpuid(0, 0, NULL, ebx, ecx, edx);
1200
    }
1201
}
1202

    
1203
void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
1204
                   uint32_t *eax, uint32_t *ebx,
1205
                   uint32_t *ecx, uint32_t *edx)
1206
{
1207
    /* test if maximum index reached */
1208
    if (index & 0x80000000) {
1209
        if (index > env->cpuid_xlevel) {
1210
            if (env->cpuid_xlevel2 > 0) {
1211
                /* Handle the Centaur's CPUID instruction. */
1212
                if (index > env->cpuid_xlevel2) {
1213
                    index = env->cpuid_xlevel2;
1214
                } else if (index < 0xC0000000) {
1215
                    index = env->cpuid_xlevel;
1216
                }
1217
            } else {
1218
                index =  env->cpuid_xlevel;
1219
            }
1220
        }
1221
    } else {
1222
        if (index > env->cpuid_level)
1223
            index = env->cpuid_level;
1224
    }
1225

    
1226
    switch(index) {
1227
    case 0:
1228
        *eax = env->cpuid_level;
1229
        get_cpuid_vendor(env, ebx, ecx, edx);
1230
        break;
1231
    case 1:
1232
        *eax = env->cpuid_version;
1233
        *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
1234
        *ecx = env->cpuid_ext_features;
1235
        *edx = env->cpuid_features;
1236
        if (env->nr_cores * env->nr_threads > 1) {
1237
            *ebx |= (env->nr_cores * env->nr_threads) << 16;
1238
            *edx |= 1 << 28;    /* HTT bit */
1239
        }
1240
        break;
1241
    case 2:
1242
        /* cache info: needed for Pentium Pro compatibility */
1243
        *eax = 1;
1244
        *ebx = 0;
1245
        *ecx = 0;
1246
        *edx = 0x2c307d;
1247
        break;
1248
    case 4:
1249
        /* cache info: needed for Core compatibility */
1250
        if (env->nr_cores > 1) {
1251
            *eax = (env->nr_cores - 1) << 26;
1252
        } else {
1253
            *eax = 0;
1254
        }
1255
        switch (count) {
1256
            case 0: /* L1 dcache info */
1257
                *eax |= 0x0000121;
1258
                *ebx = 0x1c0003f;
1259
                *ecx = 0x000003f;
1260
                *edx = 0x0000001;
1261
                break;
1262
            case 1: /* L1 icache info */
1263
                *eax |= 0x0000122;
1264
                *ebx = 0x1c0003f;
1265
                *ecx = 0x000003f;
1266
                *edx = 0x0000001;
1267
                break;
1268
            case 2: /* L2 cache info */
1269
                *eax |= 0x0000143;
1270
                if (env->nr_threads > 1) {
1271
                    *eax |= (env->nr_threads - 1) << 14;
1272
                }
1273
                *ebx = 0x3c0003f;
1274
                *ecx = 0x0000fff;
1275
                *edx = 0x0000001;
1276
                break;
1277
            default: /* end of info */
1278
                *eax = 0;
1279
                *ebx = 0;
1280
                *ecx = 0;
1281
                *edx = 0;
1282
                break;
1283
        }
1284
        break;
1285
    case 5:
1286
        /* mwait info: needed for Core compatibility */
1287
        *eax = 0; /* Smallest monitor-line size in bytes */
1288
        *ebx = 0; /* Largest monitor-line size in bytes */
1289
        *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
1290
        *edx = 0;
1291
        break;
1292
    case 6:
1293
        /* Thermal and Power Leaf */
1294
        *eax = 0;
1295
        *ebx = 0;
1296
        *ecx = 0;
1297
        *edx = 0;
1298
        break;
1299
    case 7:
1300
        if (kvm_enabled()) {
1301
            KVMState *s = env->kvm_state;
1302

    
1303
            *eax = kvm_arch_get_supported_cpuid(s, 0x7, count, R_EAX);
1304
            *ebx = kvm_arch_get_supported_cpuid(s, 0x7, count, R_EBX);
1305
            *ecx = kvm_arch_get_supported_cpuid(s, 0x7, count, R_ECX);
1306
            *edx = kvm_arch_get_supported_cpuid(s, 0x7, count, R_EDX);
1307
        } else {
1308
            *eax = 0;
1309
            *ebx = 0;
1310
            *ecx = 0;
1311
            *edx = 0;
1312
        }
1313
        break;
1314
    case 9:
1315
        /* Direct Cache Access Information Leaf */
1316
        *eax = 0; /* Bits 0-31 in DCA_CAP MSR */
1317
        *ebx = 0;
1318
        *ecx = 0;
1319
        *edx = 0;
1320
        break;
1321
    case 0xA:
1322
        /* Architectural Performance Monitoring Leaf */
1323
        if (kvm_enabled()) {
1324
            KVMState *s = env->kvm_state;
1325

    
1326
            *eax = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EAX);
1327
            *ebx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EBX);
1328
            *ecx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_ECX);
1329
            *edx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EDX);
1330
        } else {
1331
            *eax = 0;
1332
            *ebx = 0;
1333
            *ecx = 0;
1334
            *edx = 0;
1335
        }
1336
        break;
1337
    case 0xD:
1338
        /* Processor Extended State */
1339
        if (!(env->cpuid_ext_features & CPUID_EXT_XSAVE)) {
1340
            *eax = 0;
1341
            *ebx = 0;
1342
            *ecx = 0;
1343
            *edx = 0;
1344
            break;
1345
        }
1346
        if (kvm_enabled()) {
1347
            KVMState *s = env->kvm_state;
1348

    
1349
            *eax = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EAX);
1350
            *ebx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EBX);
1351
            *ecx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_ECX);
1352
            *edx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EDX);
1353
        } else {
1354
            *eax = 0;
1355
            *ebx = 0;
1356
            *ecx = 0;
1357
            *edx = 0;
1358
        }
1359
        break;
1360
    case 0x80000000:
1361
        *eax = env->cpuid_xlevel;
1362
        *ebx = env->cpuid_vendor1;
1363
        *edx = env->cpuid_vendor2;
1364
        *ecx = env->cpuid_vendor3;
1365
        break;
1366
    case 0x80000001:
1367
        *eax = env->cpuid_version;
1368
        *ebx = 0;
1369
        *ecx = env->cpuid_ext3_features;
1370
        *edx = env->cpuid_ext2_features;
1371

    
1372
        /* The Linux kernel checks for the CMPLegacy bit and
1373
         * discards multiple thread information if it is set.
1374
         * So dont set it here for Intel to make Linux guests happy.
1375
         */
1376
        if (env->nr_cores * env->nr_threads > 1) {
1377
            uint32_t tebx, tecx, tedx;
1378
            get_cpuid_vendor(env, &tebx, &tecx, &tedx);
1379
            if (tebx != CPUID_VENDOR_INTEL_1 ||
1380
                tedx != CPUID_VENDOR_INTEL_2 ||
1381
                tecx != CPUID_VENDOR_INTEL_3) {
1382
                *ecx |= 1 << 1;    /* CmpLegacy bit */
1383
            }
1384
        }
1385
        break;
1386
    case 0x80000002:
1387
    case 0x80000003:
1388
    case 0x80000004:
1389
        *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0];
1390
        *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1];
1391
        *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2];
1392
        *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3];
1393
        break;
1394
    case 0x80000005:
1395
        /* cache info (L1 cache) */
1396
        *eax = 0x01ff01ff;
1397
        *ebx = 0x01ff01ff;
1398
        *ecx = 0x40020140;
1399
        *edx = 0x40020140;
1400
        break;
1401
    case 0x80000006:
1402
        /* cache info (L2 cache) */
1403
        *eax = 0;
1404
        *ebx = 0x42004200;
1405
        *ecx = 0x02008140;
1406
        *edx = 0;
1407
        break;
1408
    case 0x80000008:
1409
        /* virtual & phys address size in low 2 bytes. */
1410
/* XXX: This value must match the one used in the MMU code. */
1411
        if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
1412
            /* 64 bit processor */
1413
/* XXX: The physical address space is limited to 42 bits in exec.c. */
1414
            *eax = 0x00003028;        /* 48 bits virtual, 40 bits physical */
1415
        } else {
1416
            if (env->cpuid_features & CPUID_PSE36)
1417
                *eax = 0x00000024; /* 36 bits physical */
1418
            else
1419
                *eax = 0x00000020; /* 32 bits physical */
1420
        }
1421
        *ebx = 0;
1422
        *ecx = 0;
1423
        *edx = 0;
1424
        if (env->nr_cores * env->nr_threads > 1) {
1425
            *ecx |= (env->nr_cores * env->nr_threads) - 1;
1426
        }
1427
        break;
1428
    case 0x8000000A:
1429
        if (env->cpuid_ext3_features & CPUID_EXT3_SVM) {
1430
                *eax = 0x00000001; /* SVM Revision */
1431
                *ebx = 0x00000010; /* nr of ASIDs */
1432
                *ecx = 0;
1433
                *edx = env->cpuid_svm_features; /* optional features */
1434
        } else {
1435
                *eax = 0;
1436
                *ebx = 0;
1437
                *ecx = 0;
1438
                *edx = 0;
1439
        }
1440
        break;
1441
    case 0xC0000000:
1442
        *eax = env->cpuid_xlevel2;
1443
        *ebx = 0;
1444
        *ecx = 0;
1445
        *edx = 0;
1446
        break;
1447
    case 0xC0000001:
1448
        /* Support for VIA CPU's CPUID instruction */
1449
        *eax = env->cpuid_version;
1450
        *ebx = 0;
1451
        *ecx = 0;
1452
        *edx = env->cpuid_ext4_features;
1453
        break;
1454
    case 0xC0000002:
1455
    case 0xC0000003:
1456
    case 0xC0000004:
1457
        /* Reserved for the future, and now filled with zero */
1458
        *eax = 0;
1459
        *ebx = 0;
1460
        *ecx = 0;
1461
        *edx = 0;
1462
        break;
1463
    default:
1464
        /* reserved values: zero */
1465
        *eax = 0;
1466
        *ebx = 0;
1467
        *ecx = 0;
1468
        *edx = 0;
1469
        break;
1470
    }
1471
}
1472

    
1473
/* CPUClass::reset() */
1474
static void x86_cpu_reset(CPUState *s)
1475
{
1476
    X86CPU *cpu = X86_CPU(s);
1477
    X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
1478
    CPUX86State *env = &cpu->env;
1479
    int i;
1480

    
1481
    if (qemu_loglevel_mask(CPU_LOG_RESET)) {
1482
        qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
1483
        log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
1484
    }
1485

    
1486
    xcc->parent_reset(s);
1487

    
1488

    
1489
    memset(env, 0, offsetof(CPUX86State, breakpoints));
1490

    
1491
    tlb_flush(env, 1);
1492

    
1493
    env->old_exception = -1;
1494

    
1495
    /* init to reset state */
1496

    
1497
#ifdef CONFIG_SOFTMMU
1498
    env->hflags |= HF_SOFTMMU_MASK;
1499
#endif
1500
    env->hflags2 |= HF2_GIF_MASK;
1501

    
1502
    cpu_x86_update_cr0(env, 0x60000010);
1503
    env->a20_mask = ~0x0;
1504
    env->smbase = 0x30000;
1505

    
1506
    env->idt.limit = 0xffff;
1507
    env->gdt.limit = 0xffff;
1508
    env->ldt.limit = 0xffff;
1509
    env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
1510
    env->tr.limit = 0xffff;
1511
    env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
1512

    
1513
    cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
1514
                           DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK |
1515
                           DESC_R_MASK | DESC_A_MASK);
1516
    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
1517
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1518
                           DESC_A_MASK);
1519
    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
1520
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1521
                           DESC_A_MASK);
1522
    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
1523
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1524
                           DESC_A_MASK);
1525
    cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
1526
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1527
                           DESC_A_MASK);
1528
    cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
1529
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
1530
                           DESC_A_MASK);
1531

    
1532
    env->eip = 0xfff0;
1533
    env->regs[R_EDX] = env->cpuid_version;
1534

    
1535
    env->eflags = 0x2;
1536

    
1537
    /* FPU init */
1538
    for (i = 0; i < 8; i++) {
1539
        env->fptags[i] = 1;
1540
    }
1541
    env->fpuc = 0x37f;
1542

    
1543
    env->mxcsr = 0x1f80;
1544

    
1545
    env->pat = 0x0007040600070406ULL;
1546
    env->msr_ia32_misc_enable = MSR_IA32_MISC_ENABLE_DEFAULT;
1547

    
1548
    memset(env->dr, 0, sizeof(env->dr));
1549
    env->dr[6] = DR6_FIXED_1;
1550
    env->dr[7] = DR7_FIXED_1;
1551
    cpu_breakpoint_remove_all(env, BP_CPU);
1552
    cpu_watchpoint_remove_all(env, BP_CPU);
1553
}
1554

    
1555
static void mce_init(X86CPU *cpu)
1556
{
1557
    CPUX86State *cenv = &cpu->env;
1558
    unsigned int bank;
1559

    
1560
    if (((cenv->cpuid_version >> 8) & 0xf) >= 6
1561
        && (cenv->cpuid_features & (CPUID_MCE | CPUID_MCA)) ==
1562
            (CPUID_MCE | CPUID_MCA)) {
1563
        cenv->mcg_cap = MCE_CAP_DEF | MCE_BANKS_DEF;
1564
        cenv->mcg_ctl = ~(uint64_t)0;
1565
        for (bank = 0; bank < MCE_BANKS_DEF; bank++) {
1566
            cenv->mce_banks[bank * 4] = ~(uint64_t)0;
1567
        }
1568
    }
1569
}
1570

    
1571
static void x86_cpu_initfn(Object *obj)
1572
{
1573
    X86CPU *cpu = X86_CPU(obj);
1574
    CPUX86State *env = &cpu->env;
1575

    
1576
    cpu_exec_init(env);
1577

    
1578
    object_property_add(obj, "family", "int",
1579
                        x86_cpuid_version_get_family,
1580
                        x86_cpuid_version_set_family, NULL, NULL, NULL);
1581
    object_property_add(obj, "model", "int",
1582
                        x86_cpuid_version_get_model,
1583
                        x86_cpuid_version_set_model, NULL, NULL, NULL);
1584
    object_property_add(obj, "stepping", "int",
1585
                        x86_cpuid_version_get_stepping,
1586
                        x86_cpuid_version_set_stepping, NULL, NULL, NULL);
1587
    object_property_add_str(obj, "model-id",
1588
                            NULL,
1589
                            x86_cpuid_set_model_id, NULL);
1590

    
1591
    env->cpuid_apic_id = env->cpu_index;
1592
    mce_init(cpu);
1593
}
1594

    
1595
static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
1596
{
1597
    X86CPUClass *xcc = X86_CPU_CLASS(oc);
1598
    CPUClass *cc = CPU_CLASS(oc);
1599

    
1600
    xcc->parent_reset = cc->reset;
1601
    cc->reset = x86_cpu_reset;
1602
}
1603

    
1604
static const TypeInfo x86_cpu_type_info = {
1605
    .name = TYPE_X86_CPU,
1606
    .parent = TYPE_CPU,
1607
    .instance_size = sizeof(X86CPU),
1608
    .instance_init = x86_cpu_initfn,
1609
    .abstract = false,
1610
    .class_size = sizeof(X86CPUClass),
1611
    .class_init = x86_cpu_common_class_init,
1612
};
1613

    
1614
static void x86_cpu_register_types(void)
1615
{
1616
    type_register_static(&x86_cpu_type_info);
1617
}
1618

    
1619
type_init(x86_cpu_register_types)