1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
3 #include "qapi/qmp/qlist.h"
4 #include "qapi/qmp/qdict.h"
5 #include "qapi/qmp/qint.h"
6 #include "qapi/qmp/qbool.h"
9 static char *get_cpu0_qom_path(void)
16 resp
= qmp("{'execute': 'query-cpus', 'arguments': {}}");
17 g_assert(qdict_haskey(resp
, "return"));
18 ret
= qdict_get_qlist(resp
, "return");
20 cpu0
= qobject_to_qdict(qlist_peek(ret
));
21 path
= g_strdup(qdict_get_str(cpu0
, "qom_path"));
26 static QObject
*qom_get(const char *path
, const char *prop
)
28 QDict
*resp
= qmp("{ 'execute': 'qom-get',"
29 " 'arguments': { 'path': %s,"
30 " 'property': %s } }",
32 QObject
*ret
= qdict_get(resp
, "return");
38 static bool qom_get_bool(const char *path
, const char *prop
)
40 QBool
*value
= qobject_to_qbool(qom_get(path
, prop
));
41 bool b
= qbool_get_bool(value
);
47 typedef struct CpuidTestArgs
{
50 int64_t expected_value
;
53 static void test_cpuid_prop(const void *data
)
55 const CpuidTestArgs
*args
= data
;
59 qtest_start(args
->cmdline
);
60 path
= get_cpu0_qom_path();
61 value
= qobject_to_qint(qom_get(path
, args
->property
));
62 g_assert_cmpint(qint_get_int(value
), ==, args
->expected_value
);
69 static void add_cpuid_test(const char *name
, const char *cmdline
,
70 const char *property
, int64_t expected_value
)
72 CpuidTestArgs
*args
= g_new0(CpuidTestArgs
, 1);
73 args
->cmdline
= cmdline
;
74 args
->property
= property
;
75 args
->expected_value
= expected_value
;
76 qtest_add_data_func(name
, args
, test_cpuid_prop
);
79 static void test_plus_minus(void)
84 * 1)"-foo" overrides "+foo"
85 * 2) "[+-]foo" overrides "foo=..."
86 * 3) Old feature names with underscores (e.g. "sse4_2")
89 * Note: rules 1 and 2 are planned to be removed soon, but we
90 * need to keep compatibility for a while until we start
91 * warning users about it.
93 qtest_start("-cpu pentium,-fpu,+fpu,-mce,mce=on,+cx8,cx8=off,+sse4_1,sse4_2=on");
94 path
= get_cpu0_qom_path();
96 g_assert_false(qom_get_bool(path
, "fpu"));
97 g_assert_false(qom_get_bool(path
, "mce"));
98 g_assert_true(qom_get_bool(path
, "cx8"));
100 /* Test both the original and the alias feature names: */
101 g_assert_true(qom_get_bool(path
, "sse4-1"));
102 g_assert_true(qom_get_bool(path
, "sse4.1"));
104 g_assert_true(qom_get_bool(path
, "sse4-2"));
105 g_assert_true(qom_get_bool(path
, "sse4.2"));
111 int main(int argc
, char **argv
)
113 g_test_init(&argc
, &argv
, NULL
);
115 qtest_add_func("x86/cpuid/parsing-plus-minus", test_plus_minus
);
117 /* Original level values for CPU models: */
118 add_cpuid_test("x86/cpuid/phenom/level",
119 "-cpu phenom", "level", 5);
120 add_cpuid_test("x86/cpuid/Conroe/level",
121 "-cpu Conroe", "level", 10);
122 add_cpuid_test("x86/cpuid/SandyBridge/level",
123 "-cpu SandyBridge", "level", 0xd);
124 add_cpuid_test("x86/cpuid/486/xlevel",
125 "-cpu 486", "xlevel", 0);
126 add_cpuid_test("x86/cpuid/core2duo/xlevel",
127 "-cpu core2duo", "xlevel", 0x80000008);
128 add_cpuid_test("x86/cpuid/phenom/xlevel",
129 "-cpu phenom", "xlevel", 0x8000001A);
130 add_cpuid_test("x86/cpuid/athlon/xlevel",
131 "-cpu athlon", "xlevel", 0x80000008);
133 /* If level is not large enough, it should increase automatically: */
135 add_cpuid_test("x86/cpuid/auto-level/phenom/arat",
136 "-cpu 486,+arat", "level", 6);
137 /* CPUID[EAX=7,ECX=0].EBX: */
138 add_cpuid_test("x86/cpuid/auto-level/phenom/fsgsbase",
139 "-cpu phenom,+fsgsbase", "level", 7);
140 /* CPUID[EAX=7,ECX=0].ECX: */
141 add_cpuid_test("x86/cpuid/auto-level/phenom/avx512vbmi",
142 "-cpu phenom,+avx512vbmi", "level", 7);
143 /* CPUID[EAX=0xd,ECX=1].EAX: */
144 add_cpuid_test("x86/cpuid/auto-level/phenom/xsaveopt",
145 "-cpu phenom,+xsaveopt", "level", 0xd);
146 /* CPUID[8000_0001].EDX: */
147 add_cpuid_test("x86/cpuid/auto-xlevel/486/3dnow",
148 "-cpu 486,+3dnow", "xlevel", 0x80000001);
149 /* CPUID[8000_0001].ECX: */
150 add_cpuid_test("x86/cpuid/auto-xlevel/486/sse4a",
151 "-cpu 486,+sse4a", "xlevel", 0x80000001);
152 /* CPUID[8000_0007].EDX: */
153 add_cpuid_test("x86/cpuid/auto-xlevel/486/invtsc",
154 "-cpu 486,+invtsc", "xlevel", 0x80000007);
155 /* CPUID[8000_000A].EDX: */
156 add_cpuid_test("x86/cpuid/auto-xlevel/486/npt",
157 "-cpu 486,+npt", "xlevel", 0x8000000A);
158 /* CPUID[C000_0001].EDX: */
159 add_cpuid_test("x86/cpuid/auto-xlevel2/phenom/xstore",
160 "-cpu phenom,+xstore", "xlevel2", 0xC0000001);
161 /* SVM needs CPUID[0x8000000A] */
162 add_cpuid_test("x86/cpuid/auto-xlevel/athlon/svm",
163 "-cpu athlon,+svm", "xlevel", 0x8000000A);
166 /* If level is already large enough, it shouldn't change: */
167 add_cpuid_test("x86/cpuid/auto-level/SandyBridge/multiple",
168 "-cpu SandyBridge,+arat,+fsgsbase,+avx512vbmi",
170 /* If level is explicitly set, it shouldn't change: */
171 add_cpuid_test("x86/cpuid/auto-level/486/fixed/0xF",
172 "-cpu 486,level=0xF,+arat,+fsgsbase,+avx512vbmi,+xsaveopt",
174 add_cpuid_test("x86/cpuid/auto-level/486/fixed/2",
175 "-cpu 486,level=2,+arat,+fsgsbase,+avx512vbmi,+xsaveopt",
177 add_cpuid_test("x86/cpuid/auto-level/486/fixed/0",
178 "-cpu 486,level=0,+arat,+fsgsbase,+avx512vbmi,+xsaveopt",
181 /* if xlevel is already large enough, it shouldn't change: */
182 add_cpuid_test("x86/cpuid/auto-xlevel/phenom/3dnow",
183 "-cpu phenom,+3dnow,+sse4a,+invtsc,+npt,+svm",
184 "xlevel", 0x8000001A);
185 /* If xlevel is explicitly set, it shouldn't change: */
186 add_cpuid_test("x86/cpuid/auto-xlevel/486/fixed/80000002",
187 "-cpu 486,xlevel=0x80000002,+3dnow,+sse4a,+invtsc,+npt,+svm",
188 "xlevel", 0x80000002);
189 add_cpuid_test("x86/cpuid/auto-xlevel/486/fixed/8000001A",
190 "-cpu 486,xlevel=0x8000001A,+3dnow,+sse4a,+invtsc,+npt,+svm",
191 "xlevel", 0x8000001A);
192 add_cpuid_test("x86/cpuid/auto-xlevel/phenom/fixed/0",
193 "-cpu 486,xlevel=0,+3dnow,+sse4a,+invtsc,+npt,+svm",
196 /* if xlevel2 is already large enough, it shouldn't change: */
197 add_cpuid_test("x86/cpuid/auto-xlevel2/486/fixed",
198 "-cpu 486,xlevel2=0xC0000002,+xstore",
199 "xlevel2", 0xC0000002);
201 /* Check compatibility of old machine-types that didn't
202 * auto-increase level/xlevel/xlevel2: */
204 add_cpuid_test("x86/cpuid/auto-level/pc-2.7",
205 "-machine pc-i440fx-2.7 -cpu 486,+arat,+avx512vbmi,+xsaveopt",
207 add_cpuid_test("x86/cpuid/auto-xlevel/pc-2.7",
208 "-machine pc-i440fx-2.7 -cpu 486,+3dnow,+sse4a,+invtsc,+npt,+svm",
210 add_cpuid_test("x86/cpuid/auto-xlevel2/pc-2.7",
211 "-machine pc-i440fx-2.7 -cpu 486,+xstore",