11e8a1faeSThomas Huth #include "qemu/osdep.h"
21e8a1faeSThomas Huth #include "qapi/qmp/qdict.h"
31e8a1faeSThomas Huth #include "qapi/qmp/qlist.h"
41e8a1faeSThomas Huth #include "qapi/qmp/qnum.h"
51e8a1faeSThomas Huth #include "qapi/qmp/qbool.h"
61e8a1faeSThomas Huth #include "libqtest-single.h"
71e8a1faeSThomas Huth 
get_cpu0_qom_path(void)81e8a1faeSThomas Huth static char *get_cpu0_qom_path(void)
91e8a1faeSThomas Huth {
101e8a1faeSThomas Huth     QDict *resp;
111e8a1faeSThomas Huth     QList *ret;
121e8a1faeSThomas Huth     QDict *cpu0;
131e8a1faeSThomas Huth     char *path;
141e8a1faeSThomas Huth 
158af54b91SDaniel P. Berrangé     resp = qmp("{'execute': 'query-cpus-fast', 'arguments': {}}");
161e8a1faeSThomas Huth     g_assert(qdict_haskey(resp, "return"));
171e8a1faeSThomas Huth     ret = qdict_get_qlist(resp, "return");
181e8a1faeSThomas Huth 
191e8a1faeSThomas Huth     cpu0 = qobject_to(QDict, qlist_peek(ret));
208af54b91SDaniel P. Berrangé     path = g_strdup(qdict_get_str(cpu0, "qom-path"));
211e8a1faeSThomas Huth     qobject_unref(resp);
221e8a1faeSThomas Huth     return path;
231e8a1faeSThomas Huth }
241e8a1faeSThomas Huth 
qom_get(const char * path,const char * prop)251e8a1faeSThomas Huth static QObject *qom_get(const char *path, const char *prop)
261e8a1faeSThomas Huth {
271e8a1faeSThomas Huth     QDict *resp = qmp("{ 'execute': 'qom-get',"
281e8a1faeSThomas Huth                       "  'arguments': { 'path': %s,"
291e8a1faeSThomas Huth                       "                 'property': %s } }",
301e8a1faeSThomas Huth                       path, prop);
311e8a1faeSThomas Huth     QObject *ret = qdict_get(resp, "return");
321e8a1faeSThomas Huth     qobject_ref(ret);
331e8a1faeSThomas Huth     qobject_unref(resp);
341e8a1faeSThomas Huth     return ret;
351e8a1faeSThomas Huth }
361e8a1faeSThomas Huth 
qom_get_bool(const char * path,const char * prop)371e8a1faeSThomas Huth static bool qom_get_bool(const char *path, const char *prop)
381e8a1faeSThomas Huth {
391e8a1faeSThomas Huth     QBool *value = qobject_to(QBool, qom_get(path, prop));
401e8a1faeSThomas Huth     bool b = qbool_get_bool(value);
411e8a1faeSThomas Huth 
421e8a1faeSThomas Huth     qobject_unref(value);
431e8a1faeSThomas Huth     return b;
441e8a1faeSThomas Huth }
451e8a1faeSThomas Huth 
461e8a1faeSThomas Huth typedef struct CpuidTestArgs {
471e8a1faeSThomas Huth     const char *cmdline;
481e8a1faeSThomas Huth     const char *property;
491e8a1faeSThomas Huth     int64_t expected_value;
501e8a1faeSThomas Huth } CpuidTestArgs;
511e8a1faeSThomas Huth 
test_cpuid_prop(const void * data)521e8a1faeSThomas Huth static void test_cpuid_prop(const void *data)
531e8a1faeSThomas Huth {
541e8a1faeSThomas Huth     const CpuidTestArgs *args = data;
551e8a1faeSThomas Huth     char *path;
561e8a1faeSThomas Huth     QNum *value;
571e8a1faeSThomas Huth     int64_t val;
581e8a1faeSThomas Huth 
591e8a1faeSThomas Huth     qtest_start(args->cmdline);
601e8a1faeSThomas Huth     path = get_cpu0_qom_path();
611e8a1faeSThomas Huth     value = qobject_to(QNum, qom_get(path, args->property));
621e8a1faeSThomas Huth     g_assert(qnum_get_try_int(value, &val));
631e8a1faeSThomas Huth     g_assert_cmpint(val, ==, args->expected_value);
641e8a1faeSThomas Huth     qtest_end();
651e8a1faeSThomas Huth 
661e8a1faeSThomas Huth     qobject_unref(value);
671e8a1faeSThomas Huth     g_free(path);
681e8a1faeSThomas Huth }
691e8a1faeSThomas Huth 
add_cpuid_test(const char * name,const char * cmdline,const char * property,int64_t expected_value)701e8a1faeSThomas Huth static void add_cpuid_test(const char *name, const char *cmdline,
711e8a1faeSThomas Huth                            const char *property, int64_t expected_value)
721e8a1faeSThomas Huth {
731e8a1faeSThomas Huth     CpuidTestArgs *args = g_new0(CpuidTestArgs, 1);
741e8a1faeSThomas Huth     args->cmdline = cmdline;
751e8a1faeSThomas Huth     args->property = property;
761e8a1faeSThomas Huth     args->expected_value = expected_value;
771e8a1faeSThomas Huth     qtest_add_data_func(name, args, test_cpuid_prop);
781e8a1faeSThomas Huth }
791e8a1faeSThomas Huth 
801e8a1faeSThomas Huth 
811e8a1faeSThomas Huth /* Parameters to a add_feature_test() test case */
821e8a1faeSThomas Huth typedef struct FeatureTestArgs {
831e8a1faeSThomas Huth     /* cmdline to start QEMU */
841e8a1faeSThomas Huth     const char *cmdline;
851e8a1faeSThomas Huth     /*
861e8a1faeSThomas Huth      * cpuid-input-eax and cpuid-input-ecx values to look for,
871e8a1faeSThomas Huth      * in "feature-words" and "filtered-features" properties.
881e8a1faeSThomas Huth      */
891e8a1faeSThomas Huth     uint32_t in_eax, in_ecx;
901e8a1faeSThomas Huth     /* The register name to look for, in the X86CPUFeatureWordInfo array */
911e8a1faeSThomas Huth     const char *reg;
921e8a1faeSThomas Huth     /* The bit to check in X86CPUFeatureWordInfo.features */
931e8a1faeSThomas Huth     int bitnr;
941e8a1faeSThomas Huth     /* The expected value for the bit in (X86CPUFeatureWordInfo.features) */
951e8a1faeSThomas Huth     bool expected_value;
961e8a1faeSThomas Huth } FeatureTestArgs;
971e8a1faeSThomas Huth 
981e8a1faeSThomas Huth /* Get the value for a feature word in a X86CPUFeatureWordInfo list */
get_feature_word(QList * features,uint32_t eax,uint32_t ecx,const char * reg)991e8a1faeSThomas Huth static uint32_t get_feature_word(QList *features, uint32_t eax, uint32_t ecx,
1001e8a1faeSThomas Huth                                  const char *reg)
1011e8a1faeSThomas Huth {
1021e8a1faeSThomas Huth     const QListEntry *e;
1031e8a1faeSThomas Huth 
1041e8a1faeSThomas Huth     for (e = qlist_first(features); e; e = qlist_next(e)) {
1051e8a1faeSThomas Huth         QDict *w = qobject_to(QDict, qlist_entry_obj(e));
1061e8a1faeSThomas Huth         const char *rreg = qdict_get_str(w, "cpuid-register");
1071e8a1faeSThomas Huth         uint32_t reax = qdict_get_int(w, "cpuid-input-eax");
1081e8a1faeSThomas Huth         bool has_ecx = qdict_haskey(w, "cpuid-input-ecx");
1091e8a1faeSThomas Huth         uint32_t recx = 0;
1101e8a1faeSThomas Huth         int64_t val;
1111e8a1faeSThomas Huth 
1121e8a1faeSThomas Huth         if (has_ecx) {
1131e8a1faeSThomas Huth             recx = qdict_get_int(w, "cpuid-input-ecx");
1141e8a1faeSThomas Huth         }
1151e8a1faeSThomas Huth         if (eax == reax && (!has_ecx || ecx == recx) && !strcmp(rreg, reg)) {
1161e8a1faeSThomas Huth             g_assert(qnum_get_try_int(qobject_to(QNum,
1171e8a1faeSThomas Huth                                                  qdict_get(w, "features")),
1181e8a1faeSThomas Huth                                       &val));
1191e8a1faeSThomas Huth             return val;
1201e8a1faeSThomas Huth         }
1211e8a1faeSThomas Huth     }
1221e8a1faeSThomas Huth     return 0;
1231e8a1faeSThomas Huth }
1241e8a1faeSThomas Huth 
test_feature_flag(const void * data)1251e8a1faeSThomas Huth static void test_feature_flag(const void *data)
1261e8a1faeSThomas Huth {
1271e8a1faeSThomas Huth     const FeatureTestArgs *args = data;
1281e8a1faeSThomas Huth     char *path;
1291e8a1faeSThomas Huth     QList *present, *filtered;
1301e8a1faeSThomas Huth     uint32_t value;
1311e8a1faeSThomas Huth 
1321e8a1faeSThomas Huth     qtest_start(args->cmdline);
1331e8a1faeSThomas Huth     path = get_cpu0_qom_path();
1341e8a1faeSThomas Huth     present = qobject_to(QList, qom_get(path, "feature-words"));
1351e8a1faeSThomas Huth     filtered = qobject_to(QList, qom_get(path, "filtered-features"));
1361e8a1faeSThomas Huth     value = get_feature_word(present, args->in_eax, args->in_ecx, args->reg);
1371e8a1faeSThomas Huth     value |= get_feature_word(filtered, args->in_eax, args->in_ecx, args->reg);
1381e8a1faeSThomas Huth     qtest_end();
1391e8a1faeSThomas Huth 
1401e8a1faeSThomas Huth     g_assert(!!(value & (1U << args->bitnr)) == args->expected_value);
1411e8a1faeSThomas Huth 
1421e8a1faeSThomas Huth     qobject_unref(present);
1431e8a1faeSThomas Huth     qobject_unref(filtered);
1441e8a1faeSThomas Huth     g_free(path);
1451e8a1faeSThomas Huth }
1461e8a1faeSThomas Huth 
1471e8a1faeSThomas Huth /*
1481e8a1faeSThomas Huth  * Add test case to ensure that a given feature flag is set in
1491e8a1faeSThomas Huth  * either "feature-words" or "filtered-features", when running QEMU
1501e8a1faeSThomas Huth  * using cmdline
1511e8a1faeSThomas Huth  */
add_feature_test(const char * name,const char * cmdline,uint32_t eax,uint32_t ecx,const char * reg,int bitnr,bool expected_value)1521e8a1faeSThomas Huth static FeatureTestArgs *add_feature_test(const char *name, const char *cmdline,
1531e8a1faeSThomas Huth                                          uint32_t eax, uint32_t ecx,
1541e8a1faeSThomas Huth                                          const char *reg, int bitnr,
1551e8a1faeSThomas Huth                                          bool expected_value)
1561e8a1faeSThomas Huth {
1571e8a1faeSThomas Huth     FeatureTestArgs *args = g_new0(FeatureTestArgs, 1);
1581e8a1faeSThomas Huth     args->cmdline = cmdline;
1591e8a1faeSThomas Huth     args->in_eax = eax;
1601e8a1faeSThomas Huth     args->in_ecx = ecx;
1611e8a1faeSThomas Huth     args->reg = reg;
1621e8a1faeSThomas Huth     args->bitnr = bitnr;
1631e8a1faeSThomas Huth     args->expected_value = expected_value;
1641e8a1faeSThomas Huth     qtest_add_data_func(name, args, test_feature_flag);
1651e8a1faeSThomas Huth     return args;
1661e8a1faeSThomas Huth }
1671e8a1faeSThomas Huth 
test_plus_minus_subprocess(void)1681e8a1faeSThomas Huth static void test_plus_minus_subprocess(void)
1691e8a1faeSThomas Huth {
1701e8a1faeSThomas Huth     char *path;
1711e8a1faeSThomas Huth 
1721e8a1faeSThomas Huth     /* Rules:
1731e8a1faeSThomas Huth      * 1)"-foo" overrides "+foo"
1741e8a1faeSThomas Huth      * 2) "[+-]foo" overrides "foo=..."
1751e8a1faeSThomas Huth      * 3) Old feature names with underscores (e.g. "sse4_2")
1761e8a1faeSThomas Huth      *    should keep working
1771e8a1faeSThomas Huth      *
1781e8a1faeSThomas Huth      * Note: rules 1 and 2 are planned to be removed soon, and
1791e8a1faeSThomas Huth      * should generate a warning.
1801e8a1faeSThomas Huth      */
1811e8a1faeSThomas Huth     qtest_start("-cpu pentium,-fpu,+fpu,-mce,mce=on,+cx8,cx8=off,+sse4_1,sse4_2=on");
1821e8a1faeSThomas Huth     path = get_cpu0_qom_path();
1831e8a1faeSThomas Huth 
1841e8a1faeSThomas Huth     g_assert_false(qom_get_bool(path, "fpu"));
1851e8a1faeSThomas Huth     g_assert_false(qom_get_bool(path, "mce"));
1861e8a1faeSThomas Huth     g_assert_true(qom_get_bool(path, "cx8"));
1871e8a1faeSThomas Huth 
1881e8a1faeSThomas Huth     /* Test both the original and the alias feature names: */
1891e8a1faeSThomas Huth     g_assert_true(qom_get_bool(path, "sse4-1"));
1901e8a1faeSThomas Huth     g_assert_true(qom_get_bool(path, "sse4.1"));
1911e8a1faeSThomas Huth 
1921e8a1faeSThomas Huth     g_assert_true(qom_get_bool(path, "sse4-2"));
1931e8a1faeSThomas Huth     g_assert_true(qom_get_bool(path, "sse4.2"));
1941e8a1faeSThomas Huth 
1951e8a1faeSThomas Huth     qtest_end();
1961e8a1faeSThomas Huth     g_free(path);
1971e8a1faeSThomas Huth }
1981e8a1faeSThomas Huth 
test_plus_minus(void)1991e8a1faeSThomas Huth static void test_plus_minus(void)
2001e8a1faeSThomas Huth {
2011e8a1faeSThomas Huth     g_test_trap_subprocess("/x86/cpuid/parsing-plus-minus/subprocess", 0, 0);
2021e8a1faeSThomas Huth     g_test_trap_assert_passed();
2031e8a1faeSThomas Huth     g_test_trap_assert_stderr("*Ambiguous CPU model string. "
2041e8a1faeSThomas Huth                               "Don't mix both \"-mce\" and \"mce=on\"*");
2051e8a1faeSThomas Huth     g_test_trap_assert_stderr("*Ambiguous CPU model string. "
2061e8a1faeSThomas Huth                               "Don't mix both \"+cx8\" and \"cx8=off\"*");
2071e8a1faeSThomas Huth     g_test_trap_assert_stdout("");
2081e8a1faeSThomas Huth }
2091e8a1faeSThomas Huth 
main(int argc,char ** argv)2101e8a1faeSThomas Huth int main(int argc, char **argv)
2111e8a1faeSThomas Huth {
2121e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
2131e8a1faeSThomas Huth 
2141e8a1faeSThomas Huth     g_test_add_func("/x86/cpuid/parsing-plus-minus/subprocess",
2151e8a1faeSThomas Huth                     test_plus_minus_subprocess);
2161e8a1faeSThomas Huth     g_test_add_func("/x86/cpuid/parsing-plus-minus", test_plus_minus);
2171e8a1faeSThomas Huth 
2181e8a1faeSThomas Huth     /* Original level values for CPU models: */
2191e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/phenom/level",
2201e8a1faeSThomas Huth                    "-cpu phenom", "level", 5);
2211e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/Conroe/level",
2221e8a1faeSThomas Huth                    "-cpu Conroe", "level", 10);
2231e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/SandyBridge/level",
2241e8a1faeSThomas Huth                    "-cpu SandyBridge", "level", 0xd);
2251e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/486/xlevel",
2261e8a1faeSThomas Huth                    "-cpu 486", "xlevel", 0);
2271e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/core2duo/xlevel",
2281e8a1faeSThomas Huth                    "-cpu core2duo", "xlevel", 0x80000008);
2291e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/phenom/xlevel",
2301e8a1faeSThomas Huth                    "-cpu phenom", "xlevel", 0x8000001A);
2311e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/athlon/xlevel",
2321e8a1faeSThomas Huth                    "-cpu athlon", "xlevel", 0x80000008);
2331e8a1faeSThomas Huth 
2341e8a1faeSThomas Huth     /* If level is not large enough, it should increase automatically: */
2351e8a1faeSThomas Huth     /* CPUID[6].EAX: */
2361e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-level/phenom/arat",
237b7d77f5aSDaniel P. Berrangé                    "-cpu 486,arat=on", "level", 6);
2381e8a1faeSThomas Huth     /* CPUID[EAX=7,ECX=0].EBX: */
2391e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-level/phenom/fsgsbase",
240b7d77f5aSDaniel P. Berrangé                    "-cpu phenom,fsgsbase=on", "level", 7);
2411e8a1faeSThomas Huth     /* CPUID[EAX=7,ECX=0].ECX: */
2421e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-level/phenom/avx512vbmi",
243b7d77f5aSDaniel P. Berrangé                    "-cpu phenom,avx512vbmi=on", "level", 7);
2441e8a1faeSThomas Huth     /* CPUID[EAX=0xd,ECX=1].EAX: */
2451e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-level/phenom/xsaveopt",
246b7d77f5aSDaniel P. Berrangé                    "-cpu phenom,xsaveopt=on", "level", 0xd);
2471e8a1faeSThomas Huth     /* CPUID[8000_0001].EDX: */
2481e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel/486/3dnow",
249b7d77f5aSDaniel P. Berrangé                    "-cpu 486,3dnow=on", "xlevel", 0x80000001);
2501e8a1faeSThomas Huth     /* CPUID[8000_0001].ECX: */
2511e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel/486/sse4a",
252b7d77f5aSDaniel P. Berrangé                    "-cpu 486,sse4a=on", "xlevel", 0x80000001);
2531e8a1faeSThomas Huth     /* CPUID[8000_0007].EDX: */
2541e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel/486/invtsc",
255b7d77f5aSDaniel P. Berrangé                    "-cpu 486,invtsc=on", "xlevel", 0x80000007);
2561e8a1faeSThomas Huth     /* CPUID[8000_000A].EDX: */
2571e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel/486/npt",
258b7d77f5aSDaniel P. Berrangé                    "-cpu 486,svm=on,npt=on", "xlevel", 0x8000000A);
2591e8a1faeSThomas Huth     /* CPUID[C000_0001].EDX: */
2601e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel2/phenom/xstore",
261b7d77f5aSDaniel P. Berrangé                    "-cpu phenom,xstore=on", "xlevel2", 0xC0000001);
2621e8a1faeSThomas Huth     /* SVM needs CPUID[0x8000000A] */
2631e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel/athlon/svm",
264b7d77f5aSDaniel P. Berrangé                    "-cpu athlon,svm=on", "xlevel", 0x8000000A);
2651e8a1faeSThomas Huth 
2661e8a1faeSThomas Huth 
2671e8a1faeSThomas Huth     /* If level is already large enough, it shouldn't change: */
2681e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-level/SandyBridge/multiple",
269b7d77f5aSDaniel P. Berrangé                    "-cpu SandyBridge,arat=on,fsgsbase=on,avx512vbmi=on",
2701e8a1faeSThomas Huth                    "level", 0xd);
2711e8a1faeSThomas Huth     /* If level is explicitly set, it shouldn't change: */
2721e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-level/486/fixed/0xF",
273b7d77f5aSDaniel P. Berrangé                    "-cpu 486,level=0xF,arat=on,fsgsbase=on,avx512vbmi=on,xsaveopt=on",
2741e8a1faeSThomas Huth                    "level", 0xF);
2751e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-level/486/fixed/2",
276b7d77f5aSDaniel P. Berrangé                    "-cpu 486,level=2,arat=on,fsgsbase=on,avx512vbmi=on,xsaveopt=on",
2771e8a1faeSThomas Huth                    "level", 2);
2781e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-level/486/fixed/0",
279b7d77f5aSDaniel P. Berrangé                    "-cpu 486,level=0,arat=on,fsgsbase=on,avx512vbmi=on,xsaveopt=on",
2801e8a1faeSThomas Huth                    "level", 0);
2811e8a1faeSThomas Huth 
2821e8a1faeSThomas Huth     /* if xlevel is already large enough, it shouldn't change: */
2831e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel/phenom/3dnow",
284b7d77f5aSDaniel P. Berrangé                    "-cpu phenom,3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
2851e8a1faeSThomas Huth                    "xlevel", 0x8000001A);
2861e8a1faeSThomas Huth     /* If xlevel is explicitly set, it shouldn't change: */
2871e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel/486/fixed/80000002",
288b7d77f5aSDaniel P. Berrangé                    "-cpu 486,xlevel=0x80000002,3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
2891e8a1faeSThomas Huth                    "xlevel", 0x80000002);
2901e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel/486/fixed/8000001A",
291b7d77f5aSDaniel P. Berrangé                    "-cpu 486,xlevel=0x8000001A,3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
2921e8a1faeSThomas Huth                    "xlevel", 0x8000001A);
2931e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel/phenom/fixed/0",
294b7d77f5aSDaniel P. Berrangé                    "-cpu 486,xlevel=0,3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
2951e8a1faeSThomas Huth                    "xlevel", 0);
2961e8a1faeSThomas Huth 
2971e8a1faeSThomas Huth     /* if xlevel2 is already large enough, it shouldn't change: */
2981e8a1faeSThomas Huth     add_cpuid_test("x86/cpuid/auto-xlevel2/486/fixed",
299b7d77f5aSDaniel P. Berrangé                    "-cpu 486,xlevel2=0xC0000002,xstore=on",
3001e8a1faeSThomas Huth                    "xlevel2", 0xC0000002);
3011e8a1faeSThomas Huth 
3021e8a1faeSThomas Huth     /* Check compatibility of old machine-types that didn't
3031e8a1faeSThomas Huth      * auto-increase level/xlevel/xlevel2: */
3047539fa01SThomas Huth     if (qtest_has_machine("pc-i440fx-2.7")) {
3051e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/auto-level/pc-2.7",
306b7d77f5aSDaniel P. Berrangé                        "-machine pc-i440fx-2.7 -cpu 486,arat=on,avx512vbmi=on,xsaveopt=on",
3071e8a1faeSThomas Huth                        "level", 1);
3081e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/auto-xlevel/pc-2.7",
309b7d77f5aSDaniel P. Berrangé                        "-machine pc-i440fx-2.7 -cpu 486,3dnow=on,sse4a=on,invtsc=on,npt=on,svm=on",
3101e8a1faeSThomas Huth                        "xlevel", 0);
3111e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/auto-xlevel2/pc-2.7",
312b7d77f5aSDaniel P. Berrangé                        "-machine pc-i440fx-2.7 -cpu 486,xstore=on",
3131e8a1faeSThomas Huth                        "xlevel2", 0);
3147539fa01SThomas Huth     }
3151e8a1faeSThomas Huth     /*
316*ea985d23SPaolo Bonzini      * QEMU 2.3.0 had auto-level enabled for CPUID[7], already,
3171e8a1faeSThomas Huth      * and the compat code that sets default level shouldn't
3181e8a1faeSThomas Huth      * disable the auto-level=7 code:
3191e8a1faeSThomas Huth      */
3207539fa01SThomas Huth     if (qtest_has_machine("pc-i440fx-2.3")) {
3211e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/auto-level7/pc-i440fx-2.3/off",
3221e8a1faeSThomas Huth                        "-machine pc-i440fx-2.3 -cpu Penryn",
3231e8a1faeSThomas Huth                        "level", 4);
3241e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/auto-level7/pc-i440fx-2.3/on",
325b7d77f5aSDaniel P. Berrangé                        "-machine pc-i440fx-2.3 -cpu Penryn,erms=on",
3261e8a1faeSThomas Huth                        "level", 7);
3277539fa01SThomas Huth     }
3287539fa01SThomas Huth     if (qtest_has_machine("pc-i440fx-2.9")) {
3291e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/auto-level7/pc-i440fx-2.9/off",
3301e8a1faeSThomas Huth                        "-machine pc-i440fx-2.9 -cpu Conroe",
3311e8a1faeSThomas Huth                        "level", 10);
3321e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/auto-level7/pc-i440fx-2.9/on",
333b7d77f5aSDaniel P. Berrangé                        "-machine pc-i440fx-2.9 -cpu Conroe,erms=on",
3341e8a1faeSThomas Huth                        "level", 10);
3357539fa01SThomas Huth     }
3361e8a1faeSThomas Huth 
3371e8a1faeSThomas Huth     /*
3381e8a1faeSThomas Huth      * xlevel doesn't have any feature that triggers auto-level
3391e8a1faeSThomas Huth      * code on old machine-types.  Just check that the compat code
3401e8a1faeSThomas Huth      * is working correctly:
3411e8a1faeSThomas Huth      */
3427539fa01SThomas Huth     if (qtest_has_machine("pc-i440fx-2.3")) {
3431e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/xlevel-compat/pc-i440fx-2.3",
3441e8a1faeSThomas Huth                        "-machine pc-i440fx-2.3 -cpu SandyBridge",
3451e8a1faeSThomas Huth                        "xlevel", 0x8000000a);
3467539fa01SThomas Huth     }
3477539fa01SThomas Huth     if (qtest_has_machine("pc-i440fx-2.4")) {
3481e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/xlevel-compat/pc-i440fx-2.4/npt-off",
3491e8a1faeSThomas Huth                        "-machine pc-i440fx-2.4 -cpu SandyBridge,",
3501e8a1faeSThomas Huth                        "xlevel", 0x80000008);
3511e8a1faeSThomas Huth         add_cpuid_test("x86/cpuid/xlevel-compat/pc-i440fx-2.4/npt-on",
352b7d77f5aSDaniel P. Berrangé                        "-machine pc-i440fx-2.4 -cpu SandyBridge,svm=on,npt=on",
3531e8a1faeSThomas Huth                        "xlevel", 0x80000008);
3547539fa01SThomas Huth     }
3551e8a1faeSThomas Huth 
3561e8a1faeSThomas Huth     /* Test feature parsing */
3571e8a1faeSThomas Huth     add_feature_test("x86/cpuid/features/plus",
3581e8a1faeSThomas Huth                      "-cpu 486,+arat",
3591e8a1faeSThomas Huth                      6, 0, "EAX", 2, true);
3601e8a1faeSThomas Huth     add_feature_test("x86/cpuid/features/minus",
3611e8a1faeSThomas Huth                      "-cpu pentium,-mmx",
3621e8a1faeSThomas Huth                      1, 0, "EDX", 23, false);
3631e8a1faeSThomas Huth     add_feature_test("x86/cpuid/features/on",
3641e8a1faeSThomas Huth                      "-cpu 486,arat=on",
3651e8a1faeSThomas Huth                      6, 0, "EAX", 2, true);
3661e8a1faeSThomas Huth     add_feature_test("x86/cpuid/features/off",
3671e8a1faeSThomas Huth                      "-cpu pentium,mmx=off",
3681e8a1faeSThomas Huth                      1, 0, "EDX", 23, false);
3691e8a1faeSThomas Huth     add_feature_test("x86/cpuid/features/max-plus-invtsc",
3701e8a1faeSThomas Huth                      "-cpu max,+invtsc",
3711e8a1faeSThomas Huth                      0x80000007, 0, "EDX", 8, true);
3721e8a1faeSThomas Huth     add_feature_test("x86/cpuid/features/max-invtsc-on",
3731e8a1faeSThomas Huth                      "-cpu max,invtsc=on",
3741e8a1faeSThomas Huth                      0x80000007, 0, "EDX", 8, true);
3751e8a1faeSThomas Huth     add_feature_test("x86/cpuid/features/max-minus-mmx",
3761e8a1faeSThomas Huth                      "-cpu max,-mmx",
3771e8a1faeSThomas Huth                      1, 0, "EDX", 23, false);
3781e8a1faeSThomas Huth     add_feature_test("x86/cpuid/features/max-invtsc-on,mmx=off",
3791e8a1faeSThomas Huth                      "-cpu max,mmx=off",
3801e8a1faeSThomas Huth                      1, 0, "EDX", 23, false);
3811e8a1faeSThomas Huth 
3821e8a1faeSThomas Huth     return g_test_run();
3831e8a1faeSThomas Huth }
384