1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
#include "qemu/osdep.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qnum.h"
#include "qapi/qmp/qbool.h"
#include "libqtest-single.h"
static char *get_cpu0_qom_path(void)
{
QDict *resp;
QList *ret;
QDict *cpu0;
char *path;
resp = qmp("{'execute': 'query-cpus-fast', 'arguments': {}}");
g_assert(qdict_haskey(resp, "return"));
ret = qdict_get_qlist(resp, "return");
cpu0 = qobject_to(QDict, qlist_peek(ret));
path = g_strdup(qdict_get_str(cpu0, "qom-path"));
qobject_unref(resp);
return path;
}
static QObject *qom_get(const char *path, const char *prop)
{
QDict *resp = qmp("{ 'execute': 'qom-get',"
" 'arguments': { 'path': %s,"
" 'property': %s } }",
path, prop);
QObject *ret = qdict_get(resp, "return");
qobject_ref(ret);
qobject_unref(resp);
return ret;
}
static bool qom_get_bool(const char *path, const char *prop)
{
QBool *value = qobject_to(QBool, qom_get(path, prop));
bool b = qbool_get_bool(value);
qobject_unref(value);
return b;
}
typedef struct CpuidTestArgs {
const char *cmdline;
const char *property;
int64_t expected_value;
} CpuidTestArgs;
static void test_cpuid_prop(const void *data)
{
const CpuidTestArgs *args = data;
char *path;
QNum *value;
int64_t val;
qtest_start(args->cmdline);
path = get_cpu0_qom_path();
value = qobject_to(QNum, qom_get(path, args->property));
g_assert(qnum_get_try_int(value, &val));
g_assert_cmpint(val, ==, args->expected_value);
qtest_end();
qobject_unref(value);
g_free(path);
}
static void add_cpuid_test(const char *name, const char *cpu,
const char *cpufeat, const char *machine,
const char *property, int64_t expected_value)
{
CpuidTestArgs *args = g_new0(CpuidTestArgs, 1);
char *cmdline;
char *save;
if (!qtest_has_cpu_model(cpu)) {
return;
}
cmdline = g_strdup_printf("-cpu %s", cpu);
if (cpufeat) {
save = cmdline;
cmdline = g_strdup_printf("%s,%s", cmdline, cpufeat);
g_free(save);
}
if (machine) {
save = cmdline;
cmdline = g_strdup_printf("-machine %s %s", machine, cmdline);
g_free(save);
}
args->cmdline = cmdline;
args->property = property;
args->expected_value = expected_value;
qtest_add_data_func(name, args, test_cpuid_prop);
}
/* Parameters to a add_feature_test() test case */
typedef struct FeatureTestArgs {
/* cmdline to start QEMU */
const char *cmdline;
/*
* cpuid-input-eax and cpuid-input-ecx values to look for,
* in "feature-words" and "filtered-features" properties.
*/
uint32_t in_eax, in_ecx;
/* The register name to look for, in the X86CPUFeatureWordInfo array */
const char *reg;
/* The bit to check in X86CPUFeatureWordInfo.features */
int bitnr;
/* The expected value for the bit in (X86CPUFeatureWordInfo.features) */
bool expected_value;
} FeatureTestArgs;
/* Get the value for a feature word in a X86CPUFeatureWordInfo list */
static uint32_t get_feature_word(QList *features, uint32_t eax, uint32_t ecx,
const char *reg)
{
const QListEntry *e;
for (e = qlist_first(features); e; e = qlist_next(e)) {
QDict *w = qobject_to(QDict, qlist_entry_obj(e));
const char *rreg = qdict_get_str(w, "cpuid-register");
uint32_t reax = qdict_get_int(w, "cpuid-input-eax");
bool has_ecx = qdict_haskey(w, "cpuid-input-ecx");
uint32_t recx = 0;
int64_t val;
if (has_ecx) {
recx = qdict_get_int(w, "cpuid-input-ecx");
}
if (eax == reax && (!has_ecx || ecx == recx) && !strcmp(rreg, reg)) {
g_assert(qnum_get_try_int(qobject_to(QNum,
qdict_get(w, "features")),
&val));
return val;
}
}
return 0;
}
static void test_feature_flag(const void *data)
{
const FeatureTestArgs *args = data;
char *path;
QList *present, *filtered;
uint32_t value;
qtest_start(args->cmdline);
path = get_cpu0_qom_path();
present = qobject_to(QList, qom_get(path, "feature-words"));
filtered = qobject_to(QList, qom_get(path, "filtered-features"));
value = get_feature_word(present, args->in_eax, args->in_ecx, args->reg);
value |= get_feature_word(filtered, args->in_eax, args->in_ecx, args->reg);
qtest_end();
g_assert(!!(value & (1U << args->bitnr)) == args->expected_value);
qobject_unref(present);
qobject_unref(filtered);
g_free(path);
}
/*
* Add test case to ensure that a given feature flag is set in
* either "feature-words" or "filtered-features", when running QEMU
* using cmdline
*/
static void add_feature_test(const char *name, const char *cpu,
const char *cpufeat, uint32_t eax,
uint32_t ecx, const char *reg,
int bitnr, bool expected_value)
{
FeatureTestArgs *args = g_new0(FeatureTestArgs, 1);
char *cmdline;
if (!qtest_has_cpu_model(cpu)) {
return;
}
if (cpufeat) {
cmdline = g_strdup_printf("-cpu %s,%s", cpu, cpufeat);
} else {
cmdline = g_strdup_printf("-cpu %s", cpu);
}
args->cmdline = cmdline;
args->in_eax = eax;
args->in_ecx = ecx;
args->reg = reg;
args->bitnr = bitnr;
args->expected_value = expected_value;
qtest_add_data_func(name, args, test_feature_flag);
return;
}
static void test_plus_minus_subprocess(void)
{
char *path;
if (!qtest_has_cpu_model("pentium")) {
return;
}
/* Rules:
* 1)"-foo" overrides "+foo"
* 2) "[+-]foo" overrides "foo=..."
* 3) Old feature names with underscores (e.g. "sse4_2")
* should keep working
*
* Note: rules 1 and 2 are planned to be removed soon, and
* should generate a warning.
*/
qtest_start("-cpu pentium,-fpu,+fpu,-mce,mce=on,+cx8,cx8=off,+sse4_1,sse4_2=on");
path = get_cpu0_qom_path();
g_assert_false(qom_get_bool(path, "fpu"));
g_assert_false(qom_get_bool(path, "mce"));
g_assert_true(qom_get_bool(path, "cx8"));
/* Test both the original and the alias feature names: */
g_assert_true(qom_get_bool(path, "sse4-1"));
g_assert_true(qom_get_bool(path, "sse4.1"));
g_assert_true(qom_get_bool(path, "sse4-2"));
g_assert_true(qom_get_bool(path, "sse4.2"));
qtest_end();
g_free(path);
}
static void test_plus_minus(void)
{
if (!qtest_has_cpu_model("pentium")) {
return;
}
g_test_trap_subprocess("/x86/cpuid/parsing-plus-minus/subprocess", 0, 0);
g_test_trap_assert_passed();
g_test_trap_assert_stderr("*Ambiguous CPU model string. "
"Don't mix both \"-mce\" and \"mce=on\"*");
g_test_trap_assert_stderr("*Ambiguous CPU model string. "
"Don't mix both \"+cx8\" and \"cx8=off\"*");
g_test_trap_assert_stdout("");
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/x86/cpuid/parsing-plus-minus/subprocess",
test_plus_minus_subprocess);
g_test_add_func("/x86/cpuid/parsing-plus-minus", test_plus_minus);
/* Original level values for CPU models: */
add_cpuid_test("x86/cpuid/phenom/level",
"phenom", NULL, NULL, "level", 5);
add_cpuid_test("x86/cpuid/Conroe/level",
"Conroe", NULL, NULL, "level", 10);
add_cpuid_test("x86/cpuid/SandyBridge/level",
"SandyBridge", NULL, NULL, "level", 0xd);
add_cpuid_test("x86/cpuid/486/xlevel",
"486", NULL, NULL, "xlevel", 0);
add_cpuid_test("x86/cpuid/core2duo/xlevel",
"core2duo", NULL, NULL, "xlevel", 0x80000008);
add_cpuid_test("x86/cpuid/phenom/xlevel",
"phenom", NULL, NULL, "xlevel", 0x8000001A);
add_cpuid_test("x86/cpuid/athlon/xlevel",
"athlon", NULL, NULL, "xlevel", 0x80000008);
/* If level is not large enough, it should increase automatically: */
/* CPUID[6].EAX: */
add_cpuid_test("x86/cpuid/auto-level/486/arat",
"486", "arat=on", NULL, "level", 6);
/* CPUID[EAX=7,ECX=0].EBX: */
add_cpuid_test("x86/cpuid/auto-level/phenom/fsgsbase",
"phenom", "fsgsbase=on", NULL, "level", 7);
/* CPUID[EAX=7,ECX=0].ECX: */
add_cpuid_test("x86/cpuid/auto-level/phenom/avx512vbmi",
"phenom", "avx512vbmi=on", NULL, "level", 7);
/* CPUID[EAX=0xd,ECX=1].EAX: */
add_cpuid_test("x86/cpuid/auto-level/phenom/xsaveopt",
"phenom", "xsaveopt=on", NULL, "level", 0xd);
/* CPUID[8000_0001].EDX: */
add_cpuid_test("x86/cpuid/auto-xlevel/486/3dnow",
"486", "3dnow=on", NULL, "xlevel", 0x80000001);
/* CPUID[8000_0001].ECX: */
add_cpuid_test("x86/cpuid/auto-xlevel/486/sse4a",
"486", "sse4a=on", NULL, "xlevel", 0x80000001);
/* CPUID[8000_0007].EDX: */
add_cpuid_test("x86/cpuid/auto-xlevel/486/invtsc",
"486", "invtsc=on", NULL, "xlevel", 0x80000007);
/* CPUID[8000_000A].EDX: */
add_cpuid_test("x86/cpuid/auto-xlevel/486/npt",
"486", "svm=on,npt=on", NULL, "xlevel", 0x8000000A);
/* CPUID[C000_0001].EDX: */
add_cpuid_test("x86/cpuid/auto-xlevel2/phenom/xstore",
"phenom", "xstore=on", NULL, "xlevel2", 0xC0000001);
/* SVM needs CPUID[0x8000000A] */
add_cpuid_test("x86/cpuid/auto-xlevel/athlon/svm",
"athlon", "svm=on", NULL, "xlevel", 0x8000000A);
/* If level is already large enough, it shouldn't change: */
add_cpuid_test("x86/cpuid/auto-level/SandyBridge/multiple",
"SandyBridge", "arat=on,fsgsbase=on,avx512vbmi=on",
NULL, "level", 0xd);
/* If level is explicitly set, it shouldn't change: */
add_cpuid_test("x86/cpuid/auto-level/486/fixed/0xF",
"486",
"level=0xF,arat=on,fsgsbase=on,avx512vbmi=on,xsaveopt=on",
NULL, "level", 0xF);
add_cpuid_test("x86/cpuid/auto-level/486/fixed/2",
"486",
"level=2,arat=on,fsgsbase=on,avx512vbmi=on,xsaveopt=on",
NULL, "level", 2);
add_cpuid_test("x86/cpuid/auto-level/486/fixed/0",
"486",
"level=0,arat=on,fsgsbase=on,avx512vbmi=on,xsaveopt=on",
NULL, "level", 0);
/* if xlevel is already large enough, it shouldn't change: */
add_cpuid_test("x86/cpuid/auto-xlevel/phenom/3dnow",
"phenom", "3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
NULL, "xlevel", 0x8000001A);
/* If xlevel is explicitly set, it shouldn't change: */
add_cpuid_test("x86/cpuid/auto-xlevel/486/fixed/80000002",
"486",
"xlevel=0x80000002,3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
NULL, "xlevel", 0x80000002);
add_cpuid_test("x86/cpuid/auto-xlevel/486/fixed/8000001A",
"486",
"xlevel=0x8000001A,3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
NULL, "xlevel", 0x8000001A);
add_cpuid_test("x86/cpuid/auto-xlevel/phenom/fixed/0",
"486",
"xlevel=0,3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
NULL, "xlevel", 0);
/* if xlevel2 is already large enough, it shouldn't change: */
add_cpuid_test("x86/cpuid/auto-xlevel2/486/fixed",
"486", "xlevel2=0xC0000002,xstore=on",
NULL, "xlevel2", 0xC0000002);
/* Check compatibility of old machine-types that didn't
* auto-increase level/xlevel/xlevel2: */
if (qtest_has_machine("pc-i440fx-2.7")) {
add_cpuid_test("x86/cpuid/auto-level/pc-2.7",
"486", "arat=on,avx512vbmi=on,xsaveopt=on",
"pc-i440fx-2.7", "level", 1);
add_cpuid_test("x86/cpuid/auto-xlevel/pc-2.7",
"486", "3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
"pc-i440fx-2.7", "xlevel", 0);
add_cpuid_test("x86/cpuid/auto-xlevel2/pc-2.7",
"486", "xstore=on", "pc-i440fx-2.7",
"xlevel2", 0);
}
/*
* QEMU 2.3.0 had auto-level enabled for CPUID[7], already,
* and the compat code that sets default level shouldn't
* disable the auto-level=7 code:
*/
if (qtest_has_machine("pc-i440fx-2.3")) {
add_cpuid_test("x86/cpuid/auto-level7/pc-i440fx-2.3/off",
"Penryn", NULL, "pc-i440fx-2.3",
"level", 4);
add_cpuid_test("x86/cpuid/auto-level7/pc-i440fx-2.3/on",
"Penryn", "erms=on", "pc-i440fx-2.3",
"level", 7);
}
if (qtest_has_machine("pc-i440fx-2.9")) {
add_cpuid_test("x86/cpuid/auto-level7/pc-i440fx-2.9/off",
"Conroe", NULL, "pc-i440fx-2.9",
"level", 10);
add_cpuid_test("x86/cpuid/auto-level7/pc-i440fx-2.9/on",
"Conroe", "erms=on", "pc-i440fx-2.9",
"level", 10);
}
/*
* xlevel doesn't have any feature that triggers auto-level
* code on old machine-types. Just check that the compat code
* is working correctly:
*/
if (qtest_has_machine("pc-i440fx-2.3")) {
add_cpuid_test("x86/cpuid/xlevel-compat/pc-i440fx-2.3",
"SandyBridge", NULL, "pc-i440fx-2.3",
"xlevel", 0x8000000a);
}
if (qtest_has_machine("pc-i440fx-2.4")) {
add_cpuid_test("x86/cpuid/xlevel-compat/pc-i440fx-2.4/npt-off",
"SandyBridge", NULL, "pc-i440fx-2.4",
"xlevel", 0x80000008);
add_cpuid_test("x86/cpuid/xlevel-compat/pc-i440fx-2.4/npt-on",
"SandyBridge", "svm=on,npt=on", "pc-i440fx-2.4",
"xlevel", 0x80000008);
}
/* Test feature parsing */
add_feature_test("x86/cpuid/features/plus",
"486", "+arat",
6, 0, "EAX", 2, true);
add_feature_test("x86/cpuid/features/minus",
"pentium", "-mmx",
1, 0, "EDX", 23, false);
add_feature_test("x86/cpuid/features/on",
"486", "arat=on",
6, 0, "EAX", 2, true);
add_feature_test("x86/cpuid/features/off",
"pentium", "mmx=off",
1, 0, "EDX", 23, false);
add_feature_test("x86/cpuid/features/max-plus-invtsc",
"max" , "+invtsc",
0x80000007, 0, "EDX", 8, true);
add_feature_test("x86/cpuid/features/max-invtsc-on",
"max", "invtsc=on",
0x80000007, 0, "EDX", 8, true);
add_feature_test("x86/cpuid/features/max-minus-mmx",
"max", "-mmx",
1, 0, "EDX", 23, false);
add_feature_test("x86/cpuid/features/max-invtsc-on,mmx=off",
"max", "mmx=off",
1, 0, "EDX", 23, false);
return g_test_run();
}
|