test-x86-cpuid-compat.c 14 KB

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