2 * Arm CPU feature test cases
4 * Copyright (c) 2019 Red Hat Inc.
6 * Andrew Jones <drjones@redhat.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
12 #include "qemu/bitops.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qjson.h"
18 * We expect the SVE max-vq to be 16. Also it must be <= 64
19 * for our test code, otherwise 'vls' can't just be a uint64_t.
23 #define MACHINE "-machine virt,gic-version=max -accel tcg "
24 #define MACHINE_KVM "-machine virt,gic-version=max -accel kvm "
25 #define QUERY_HEAD "{ 'execute': 'query-cpu-model-expansion', " \
26 " 'arguments': { 'type': 'full', "
27 #define QUERY_TAIL "}}"
29 static QDict
*do_query_no_props(QTestState
*qts
, const char *cpu_type
)
31 return qtest_qmp(qts
, QUERY_HEAD
"'model': { 'name': %s }"
32 QUERY_TAIL
, cpu_type
);
36 static QDict
*do_query(QTestState
*qts
, const char *cpu_type
,
46 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
49 resp
= qtest_qmp(qts
, QUERY_HEAD
"'model': { 'name': %s, "
51 QUERY_TAIL
, cpu_type
, args
);
53 resp
= do_query_no_props(qts
, cpu_type
);
59 static const char *resp_get_error(QDict
*resp
)
65 qdict
= qdict_get_qdict(resp
, "error");
67 return qdict_get_str(qdict
, "desc");
73 #define assert_error(qts, cpu_type, expected_error, fmt, ...) \
78 _resp = do_query(qts, cpu_type, fmt, ##__VA_ARGS__); \
80 _error = resp_get_error(_resp); \
82 g_assert(g_str_equal(_error, expected_error)); \
83 qobject_unref(_resp); \
86 static bool resp_has_props(QDict
*resp
)
92 if (!qdict_haskey(resp
, "return")) {
95 qdict
= qdict_get_qdict(resp
, "return");
97 if (!qdict_haskey(qdict
, "model")) {
100 qdict
= qdict_get_qdict(qdict
, "model");
102 return qdict_haskey(qdict
, "props");
105 static QDict
*resp_get_props(QDict
*resp
)
110 g_assert(resp_has_props(resp
));
112 qdict
= qdict_get_qdict(resp
, "return");
113 qdict
= qdict_get_qdict(qdict
, "model");
114 qdict
= qdict_get_qdict(qdict
, "props");
119 static bool resp_get_feature(QDict
*resp
, const char *feature
)
124 g_assert(resp_has_props(resp
));
125 props
= resp_get_props(resp
);
126 g_assert(qdict_get(props
, feature
));
127 return qdict_get_bool(props
, feature
);
130 #define assert_has_feature(qts, cpu_type, feature) \
132 QDict *_resp = do_query_no_props(qts, cpu_type); \
134 g_assert(resp_has_props(_resp)); \
135 g_assert(qdict_get(resp_get_props(_resp), feature)); \
136 qobject_unref(_resp); \
139 #define assert_has_not_feature(qts, cpu_type, feature) \
141 QDict *_resp = do_query_no_props(qts, cpu_type); \
143 g_assert(!resp_has_props(_resp) || \
144 !qdict_get(resp_get_props(_resp), feature)); \
145 qobject_unref(_resp); \
148 #define resp_assert_feature(resp, feature, expected_value) \
153 g_assert(resp_has_props(_resp)); \
154 _props = resp_get_props(_resp); \
155 g_assert(qdict_get(_props, feature)); \
156 g_assert(qdict_get_bool(_props, feature) == (expected_value)); \
159 #define assert_feature(qts, cpu_type, feature, expected_value) \
163 _resp = do_query_no_props(qts, cpu_type); \
165 resp_assert_feature(_resp, feature, expected_value); \
166 qobject_unref(_resp); \
169 #define assert_set_feature(qts, cpu_type, feature, value) \
171 const char *_fmt = (value) ? "{ %s: true }" : "{ %s: false }"; \
174 _resp = do_query(qts, cpu_type, _fmt, feature); \
176 resp_assert_feature(_resp, feature, value); \
177 qobject_unref(_resp); \
180 #define assert_has_feature_enabled(qts, cpu_type, feature) \
181 assert_feature(qts, cpu_type, feature, true)
183 #define assert_has_feature_disabled(qts, cpu_type, feature) \
184 assert_feature(qts, cpu_type, feature, false)
186 static void assert_type_full(QTestState
*qts
)
191 resp
= qtest_qmp(qts
, "{ 'execute': 'query-cpu-model-expansion', "
192 "'arguments': { 'type': 'static', "
193 "'model': { 'name': 'foo' }}}");
195 error
= resp_get_error(resp
);
197 g_assert(g_str_equal(error
,
198 "The requested expansion type is not supported"));
202 static void assert_bad_props(QTestState
*qts
, const char *cpu_type
)
207 resp
= qtest_qmp(qts
, "{ 'execute': 'query-cpu-model-expansion', "
208 "'arguments': { 'type': 'full', "
209 "'model': { 'name': %s, "
210 "'props': false }}}",
213 error
= resp_get_error(resp
);
215 g_assert(g_str_equal(error
,
216 "Invalid parameter type for 'props', expected: dict"));
220 static uint64_t resp_get_sve_vls(QDict
*resp
)
228 g_assert(resp_has_props(resp
));
230 props
= resp_get_props(resp
);
232 for (e
= qdict_first(props
); e
; e
= qdict_next(props
, e
)) {
233 if (strlen(e
->key
) > 3 && !strncmp(e
->key
, "sve", 3) &&
234 g_ascii_isdigit(e
->key
[3])) {
238 bits
= g_ascii_strtoll(&e
->key
[3], &endptr
, 10);
239 if (!bits
|| *endptr
!= '\0') {
243 if (qdict_get_bool(props
, e
->key
)) {
244 vls
|= BIT_ULL((bits
/ 128) - 1);
250 g_assert(n
== SVE_MAX_VQ
);
255 #define assert_sve_vls(qts, cpu_type, expected_vls, fmt, ...) \
257 QDict *_resp = do_query(qts, cpu_type, fmt, ##__VA_ARGS__); \
259 g_assert(resp_has_props(_resp)); \
260 g_assert(resp_get_sve_vls(_resp) == expected_vls); \
261 qobject_unref(_resp); \
264 static void sve_tests_default(QTestState
*qts
, const char *cpu_type
)
267 * With no sve-max-vq or sve<N> properties on the command line
268 * the default is to have all vector lengths enabled. This also
269 * tests that 'sve' is 'on' by default.
271 assert_sve_vls(qts
, cpu_type
, BIT_ULL(SVE_MAX_VQ
) - 1, NULL
);
273 /* With SVE off, all vector lengths should also be off. */
274 assert_sve_vls(qts
, cpu_type
, 0, "{ 'sve': false }");
276 /* With SVE on, we must have at least one vector length enabled. */
277 assert_error(qts
, cpu_type
, "cannot disable sve128", "{ 'sve128': false }");
279 /* Basic enable/disable tests. */
280 assert_sve_vls(qts
, cpu_type
, 0x7, "{ 'sve384': true }");
281 assert_sve_vls(qts
, cpu_type
, ((BIT_ULL(SVE_MAX_VQ
) - 1) & ~BIT_ULL(2)),
282 "{ 'sve384': false }");
285 * ---------------------------------------------------------------------
286 * power-of-two(vq) all-power- can can
287 * of-two(< vq) enable disable
288 * ---------------------------------------------------------------------
289 * vq < max_vq no MUST* yes yes
290 * vq < max_vq yes MUST* yes no
291 * ---------------------------------------------------------------------
292 * vq == max_vq n/a MUST* yes** yes**
293 * ---------------------------------------------------------------------
294 * vq > max_vq n/a no no yes
295 * vq > max_vq n/a yes yes yes
296 * ---------------------------------------------------------------------
298 * [*] "MUST" means this requirement must already be satisfied,
299 * otherwise 'max_vq' couldn't itself be enabled.
301 * [**] Not testable with the QMP interface, only with the command line.
305 assert_sve_vls(qts
, cpu_type
, 0x8b, "{ 'sve1024': true }");
307 /* max_vq := 8, vq < max_vq, !power-of-two(vq) */
308 assert_sve_vls(qts
, cpu_type
, 0x8f,
309 "{ 'sve1024': true, 'sve384': true }");
310 assert_sve_vls(qts
, cpu_type
, 0x8b,
311 "{ 'sve1024': true, 'sve384': false }");
313 /* max_vq := 8, vq < max_vq, power-of-two(vq) */
314 assert_sve_vls(qts
, cpu_type
, 0x8b,
315 "{ 'sve1024': true, 'sve256': true }");
316 assert_error(qts
, cpu_type
, "cannot disable sve256",
317 "{ 'sve1024': true, 'sve256': false }");
319 /* max_vq := 3, vq > max_vq, !all-power-of-two(< vq) */
320 assert_error(qts
, cpu_type
, "cannot disable sve512",
321 "{ 'sve384': true, 'sve512': false, 'sve640': true }");
324 * We can disable power-of-two vector lengths when all larger lengths
325 * are also disabled. We only need to disable the power-of-two length,
326 * as all non-enabled larger lengths will then be auto-disabled.
328 assert_sve_vls(qts
, cpu_type
, 0x7, "{ 'sve512': false }");
330 /* max_vq := 3, vq > max_vq, all-power-of-two(< vq) */
331 assert_sve_vls(qts
, cpu_type
, 0x1f,
332 "{ 'sve384': true, 'sve512': true, 'sve640': true }");
333 assert_sve_vls(qts
, cpu_type
, 0xf,
334 "{ 'sve384': true, 'sve512': true, 'sve640': false }");
337 static void sve_tests_sve_max_vq_8(const void *data
)
341 qts
= qtest_init(MACHINE
"-cpu max,sve-max-vq=8");
343 assert_sve_vls(qts
, "max", BIT_ULL(8) - 1, NULL
);
346 * Disabling the max-vq set by sve-max-vq is not allowed, but
347 * of course enabling it is OK.
349 assert_error(qts
, "max", "cannot disable sve1024", "{ 'sve1024': false }");
350 assert_sve_vls(qts
, "max", 0xff, "{ 'sve1024': true }");
353 * Enabling anything larger than max-vq set by sve-max-vq is not
354 * allowed, but of course disabling everything larger is OK.
356 assert_error(qts
, "max", "cannot enable sve1152", "{ 'sve1152': true }");
357 assert_sve_vls(qts
, "max", 0xff, "{ 'sve1152': false }");
360 * We can enable/disable non power-of-two lengths smaller than the
361 * max-vq set by sve-max-vq, but, while we can enable power-of-two
362 * lengths, we can't disable them.
364 assert_sve_vls(qts
, "max", 0xff, "{ 'sve384': true }");
365 assert_sve_vls(qts
, "max", 0xfb, "{ 'sve384': false }");
366 assert_sve_vls(qts
, "max", 0xff, "{ 'sve256': true }");
367 assert_error(qts
, "max", "cannot disable sve256", "{ 'sve256': false }");
372 static void sve_tests_sve_off(const void *data
)
376 qts
= qtest_init(MACHINE
"-cpu max,sve=off");
378 /* SVE is off, so the map should be empty. */
379 assert_sve_vls(qts
, "max", 0, NULL
);
381 /* The map stays empty even if we turn lengths off. */
382 assert_sve_vls(qts
, "max", 0, "{ 'sve128': false }");
384 /* It's an error to enable lengths when SVE is off. */
385 assert_error(qts
, "max", "cannot enable sve128", "{ 'sve128': true }");
387 /* With SVE re-enabled we should get all vector lengths enabled. */
388 assert_sve_vls(qts
, "max", BIT_ULL(SVE_MAX_VQ
) - 1, "{ 'sve': true }");
390 /* Or enable SVE with just specific vector lengths. */
391 assert_sve_vls(qts
, "max", 0x3,
392 "{ 'sve': true, 'sve128': true, 'sve256': true }");
397 static void sve_tests_sve_off_kvm(const void *data
)
401 qts
= qtest_init(MACHINE_KVM
"-cpu max,sve=off");
404 * We don't know if this host supports SVE so we don't
405 * attempt to test enabling anything. We only test that
406 * everything is disabled (as it should be with sve=off)
407 * and that using sve<N>=off to explicitly disable vector
410 assert_sve_vls(qts
, "max", 0, NULL
);
411 assert_sve_vls(qts
, "max", 0, "{ 'sve128': false }");
416 static void pauth_tests_default(QTestState
*qts
, const char *cpu_type
)
418 assert_has_feature_enabled(qts
, cpu_type
, "pauth");
419 assert_has_feature_disabled(qts
, cpu_type
, "pauth-impdef");
420 assert_has_feature_disabled(qts
, cpu_type
, "pauth-qarma3");
421 assert_set_feature(qts
, cpu_type
, "pauth", false);
422 assert_set_feature(qts
, cpu_type
, "pauth", true);
423 assert_set_feature(qts
, cpu_type
, "pauth-impdef", true);
424 assert_set_feature(qts
, cpu_type
, "pauth-impdef", false);
425 assert_set_feature(qts
, cpu_type
, "pauth-qarma3", true);
426 assert_set_feature(qts
, cpu_type
, "pauth-qarma3", false);
427 assert_error(qts
, cpu_type
,
428 "cannot enable pauth-impdef or pauth-qarma3 without pauth",
429 "{ 'pauth': false, 'pauth-impdef': true }");
430 assert_error(qts
, cpu_type
,
431 "cannot enable pauth-impdef or pauth-qarma3 without pauth",
432 "{ 'pauth': false, 'pauth-qarma3': true }");
433 assert_error(qts
, cpu_type
,
434 "cannot enable both pauth-impdef and pauth-qarma3",
435 "{ 'pauth': true, 'pauth-impdef': true, 'pauth-qarma3': true }");
438 static void test_query_cpu_model_expansion(const void *data
)
442 qts
= qtest_init(MACHINE
"-cpu max");
444 /* Test common query-cpu-model-expansion input validation */
445 assert_type_full(qts
);
446 assert_bad_props(qts
, "max");
447 assert_error(qts
, "foo", "The CPU type 'foo' is not a recognized "
448 "ARM CPU type", NULL
);
449 assert_error(qts
, "max", "Parameter 'not-a-prop' is unexpected",
450 "{ 'not-a-prop': false }");
451 assert_error(qts
, "host", "The CPU type 'host' requires KVM", NULL
);
453 /* Test expected feature presence/absence for some cpu types */
454 assert_has_feature_enabled(qts
, "cortex-a15", "pmu");
455 assert_has_not_feature(qts
, "cortex-a15", "aarch64");
457 /* Enabling and disabling pmu should always work. */
458 assert_has_feature_enabled(qts
, "max", "pmu");
459 assert_set_feature(qts
, "max", "pmu", false);
460 assert_set_feature(qts
, "max", "pmu", true);
462 assert_has_not_feature(qts
, "max", "kvm-no-adjvtime");
463 assert_has_not_feature(qts
, "max", "kvm-steal-time");
465 if (g_str_equal(qtest_get_arch(), "aarch64")) {
466 assert_has_feature_enabled(qts
, "max", "aarch64");
467 assert_has_feature_enabled(qts
, "max", "sve");
468 assert_has_feature_enabled(qts
, "max", "sve128");
469 assert_has_feature_enabled(qts
, "cortex-a57", "pmu");
470 assert_has_feature_enabled(qts
, "cortex-a57", "aarch64");
472 assert_has_feature_enabled(qts
, "a64fx", "pmu");
473 assert_has_feature_enabled(qts
, "a64fx", "aarch64");
475 * A64FX does not support any other vector lengths besides those
476 * that are enabled by default(128bit, 256bits, 512bit).
478 assert_has_feature_enabled(qts
, "a64fx", "sve");
479 assert_sve_vls(qts
, "a64fx", 0xb, NULL
);
480 assert_error(qts
, "a64fx", "cannot enable sve384",
481 "{ 'sve384': true }");
482 assert_error(qts
, "a64fx", "cannot enable sve640",
483 "{ 'sve640': true }");
485 sve_tests_default(qts
, "max");
486 pauth_tests_default(qts
, "max");
488 /* Test that features that depend on KVM generate errors without. */
489 assert_error(qts
, "max",
490 "'aarch64' feature cannot be disabled "
491 "unless KVM is enabled and 32-bit EL1 "
493 "{ 'aarch64': false }");
499 static void test_query_cpu_model_expansion_kvm(const void *data
)
503 qts
= qtest_init(MACHINE_KVM
"-cpu max");
505 /* Enabling and disabling kvm-no-adjvtime should always work. */
506 assert_has_feature_disabled(qts
, "host", "kvm-no-adjvtime");
507 assert_set_feature(qts
, "host", "kvm-no-adjvtime", true);
508 assert_set_feature(qts
, "host", "kvm-no-adjvtime", false);
510 if (g_str_equal(qtest_get_arch(), "aarch64")) {
511 bool kvm_supports_steal_time
;
512 bool kvm_supports_sve
;
513 char max_name
[8], name
[8];
520 * When using KVM, only the 'host' and 'max' CPU models are
521 * supported. Test that we're emitting a suitable error for
522 * unsupported CPU models.
524 if (qtest_has_accel("tcg")) {
525 assert_error(qts
, "cortex-a7",
526 "We cannot guarantee the CPU type 'cortex-a7' works "
527 "with KVM on this host", NULL
);
530 * With a KVM-only build the 32-bit CPUs are not present.
532 assert_error(qts
, "cortex-a7",
533 "The CPU type 'cortex-a7' is not a "
534 "recognized ARM CPU type", NULL
);
537 assert_has_feature_enabled(qts
, "host", "aarch64");
539 /* Enabling and disabling pmu should always work. */
540 assert_has_feature_enabled(qts
, "host", "pmu");
541 assert_set_feature(qts
, "host", "pmu", false);
542 assert_set_feature(qts
, "host", "pmu", true);
545 * Some features would be enabled by default, but they're disabled
546 * because this instance of KVM doesn't support them. Test that the
547 * features are present, and, when enabled, issue further tests.
549 assert_has_feature(qts
, "host", "kvm-steal-time");
550 assert_has_feature(qts
, "host", "sve");
552 resp
= do_query_no_props(qts
, "host");
553 kvm_supports_steal_time
= resp_get_feature(resp
, "kvm-steal-time");
554 kvm_supports_sve
= resp_get_feature(resp
, "sve");
555 vls
= resp_get_sve_vls(resp
);
558 if (kvm_supports_steal_time
) {
559 /* If we have steal-time then we should be able to toggle it. */
560 assert_set_feature(qts
, "host", "kvm-steal-time", false);
561 assert_set_feature(qts
, "host", "kvm-steal-time", true);
564 if (kvm_supports_sve
) {
566 max_vq
= 64 - __builtin_clzll(vls
);
567 sprintf(max_name
, "sve%u", max_vq
* 128);
569 /* Enabling a supported length is of course fine. */
570 assert_sve_vls(qts
, "host", vls
, "{ %s: true }", max_name
);
572 /* Get the next supported length smaller than max-vq. */
573 vq
= 64 - __builtin_clzll(vls
& ~BIT_ULL(max_vq
- 1));
576 * We have at least one length smaller than max-vq,
577 * so we can disable max-vq.
579 assert_sve_vls(qts
, "host", (vls
& ~BIT_ULL(max_vq
- 1)),
580 "{ %s: false }", max_name
);
583 * Smaller, supported vector lengths cannot be disabled
584 * unless all larger, supported vector lengths are also
587 sprintf(name
, "sve%u", vq
* 128);
588 error
= g_strdup_printf("cannot disable %s", name
);
589 assert_error(qts
, "host", error
,
590 "{ %s: true, %s: false }",
596 * The smallest, supported vector length is required, because
597 * we need at least one vector length enabled.
599 vq
= __builtin_ffsll(vls
);
600 sprintf(name
, "sve%u", vq
* 128);
601 error
= g_strdup_printf("cannot disable %s", name
);
602 assert_error(qts
, "host", error
, "{ %s: false }", name
);
605 /* Get an unsupported length. */
606 for (vq
= 1; vq
<= max_vq
; ++vq
) {
607 if (!(vls
& BIT_ULL(vq
- 1))) {
611 if (vq
<= SVE_MAX_VQ
) {
612 sprintf(name
, "sve%u", vq
* 128);
613 error
= g_strdup_printf("cannot enable %s", name
);
614 assert_error(qts
, "host", error
, "{ %s: true }", name
);
621 assert_has_not_feature(qts
, "host", "aarch64");
622 assert_has_not_feature(qts
, "host", "pmu");
623 assert_has_not_feature(qts
, "host", "sve");
624 assert_has_not_feature(qts
, "host", "kvm-steal-time");
630 int main(int argc
, char **argv
)
632 g_test_init(&argc
, &argv
, NULL
);
634 if (qtest_has_accel("tcg")) {
635 qtest_add_data_func("/arm/query-cpu-model-expansion",
636 NULL
, test_query_cpu_model_expansion
);
639 if (!g_str_equal(qtest_get_arch(), "aarch64")) {
644 * For now we only run KVM specific tests with AArch64 QEMU in
645 * order avoid attempting to run an AArch32 QEMU with KVM on
646 * AArch64 hosts. That won't work and isn't easy to detect.
648 if (qtest_has_accel("kvm")) {
650 * This tests target the 'host' CPU type, so register it only if
653 qtest_add_data_func("/arm/kvm/query-cpu-model-expansion",
654 NULL
, test_query_cpu_model_expansion_kvm
);
656 qtest_add_data_func("/arm/kvm/query-cpu-model-expansion/sve-off",
657 NULL
, sve_tests_sve_off_kvm
);
660 if (qtest_has_accel("tcg")) {
661 qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-max-vq-8",
662 NULL
, sve_tests_sve_max_vq_8
);
663 qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-off",
664 NULL
, sve_tests_sve_off
);