target/loongarch: Fix the meaning of ECFG reg's VS field
[qemu/rayw.git] / tests / qtest / arm-cpu-features.c
blob5a145273860c844acc5d957561cec36635b27a6a
1 /*
2 * Arm CPU feature test cases
4 * Copyright (c) 2019 Red Hat Inc.
5 * Authors:
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"
13 #include "libqtest.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.
21 #define SVE_MAX_VQ 16
23 #define MACHINE "-machine virt,gic-version=max -accel tcg "
24 #define MACHINE_KVM "-machine virt,gic-version=max -accel kvm -accel tcg "
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);
35 static QDict *do_query(QTestState *qts, const char *cpu_type,
36 const char *fmt, ...)
38 QDict *resp;
40 if (fmt) {
41 QDict *args;
42 va_list ap;
44 va_start(ap, fmt);
45 args = qdict_from_vjsonf_nofail(fmt, ap);
46 va_end(ap);
48 resp = qtest_qmp(qts, QUERY_HEAD "'model': { 'name': %s, "
49 "'props': %p }"
50 QUERY_TAIL, cpu_type, args);
51 } else {
52 resp = do_query_no_props(qts, cpu_type);
55 return resp;
58 static const char *resp_get_error(QDict *resp)
60 QDict *qdict;
62 g_assert(resp);
64 qdict = qdict_get_qdict(resp, "error");
65 if (qdict) {
66 return qdict_get_str(qdict, "desc");
69 return NULL;
72 #define assert_error(qts, cpu_type, expected_error, fmt, ...) \
73 ({ \
74 QDict *_resp; \
75 const char *_error; \
77 _resp = do_query(qts, cpu_type, fmt, ##__VA_ARGS__); \
78 g_assert(_resp); \
79 _error = resp_get_error(_resp); \
80 g_assert(_error); \
81 g_assert(g_str_equal(_error, expected_error)); \
82 qobject_unref(_resp); \
85 static bool resp_has_props(QDict *resp)
87 QDict *qdict;
89 g_assert(resp);
91 if (!qdict_haskey(resp, "return")) {
92 return false;
94 qdict = qdict_get_qdict(resp, "return");
96 if (!qdict_haskey(qdict, "model")) {
97 return false;
99 qdict = qdict_get_qdict(qdict, "model");
101 return qdict_haskey(qdict, "props");
104 static QDict *resp_get_props(QDict *resp)
106 QDict *qdict;
108 g_assert(resp);
109 g_assert(resp_has_props(resp));
111 qdict = qdict_get_qdict(resp, "return");
112 qdict = qdict_get_qdict(qdict, "model");
113 qdict = qdict_get_qdict(qdict, "props");
115 return qdict;
118 static bool resp_get_feature(QDict *resp, const char *feature)
120 QDict *props;
122 g_assert(resp);
123 g_assert(resp_has_props(resp));
124 props = resp_get_props(resp);
125 g_assert(qdict_get(props, feature));
126 return qdict_get_bool(props, feature);
129 #define assert_has_feature(qts, cpu_type, feature) \
130 ({ \
131 QDict *_resp = do_query_no_props(qts, cpu_type); \
132 g_assert(_resp); \
133 g_assert(resp_has_props(_resp)); \
134 g_assert(qdict_get(resp_get_props(_resp), feature)); \
135 qobject_unref(_resp); \
138 #define assert_has_not_feature(qts, cpu_type, feature) \
139 ({ \
140 QDict *_resp = do_query_no_props(qts, cpu_type); \
141 g_assert(_resp); \
142 g_assert(!resp_has_props(_resp) || \
143 !qdict_get(resp_get_props(_resp), feature)); \
144 qobject_unref(_resp); \
147 #define resp_assert_feature(resp, feature, expected_value) \
148 ({ \
149 QDict *_props; \
151 g_assert(_resp); \
152 g_assert(resp_has_props(_resp)); \
153 _props = resp_get_props(_resp); \
154 g_assert(qdict_get(_props, feature)); \
155 g_assert(qdict_get_bool(_props, feature) == (expected_value)); \
158 #define assert_feature(qts, cpu_type, feature, expected_value) \
159 ({ \
160 QDict *_resp; \
162 _resp = do_query_no_props(qts, cpu_type); \
163 g_assert(_resp); \
164 resp_assert_feature(_resp, feature, expected_value); \
165 qobject_unref(_resp); \
168 #define assert_set_feature(qts, cpu_type, feature, value) \
169 ({ \
170 const char *_fmt = (value) ? "{ %s: true }" : "{ %s: false }"; \
171 QDict *_resp; \
173 _resp = do_query(qts, cpu_type, _fmt, feature); \
174 g_assert(_resp); \
175 resp_assert_feature(_resp, feature, value); \
176 qobject_unref(_resp); \
179 #define assert_has_feature_enabled(qts, cpu_type, feature) \
180 assert_feature(qts, cpu_type, feature, true)
182 #define assert_has_feature_disabled(qts, cpu_type, feature) \
183 assert_feature(qts, cpu_type, feature, false)
185 static void assert_type_full(QTestState *qts)
187 const char *error;
188 QDict *resp;
190 resp = qtest_qmp(qts, "{ 'execute': 'query-cpu-model-expansion', "
191 "'arguments': { 'type': 'static', "
192 "'model': { 'name': 'foo' }}}");
193 g_assert(resp);
194 error = resp_get_error(resp);
195 g_assert(error);
196 g_assert(g_str_equal(error,
197 "The requested expansion type is not supported"));
198 qobject_unref(resp);
201 static void assert_bad_props(QTestState *qts, const char *cpu_type)
203 const char *error;
204 QDict *resp;
206 resp = qtest_qmp(qts, "{ 'execute': 'query-cpu-model-expansion', "
207 "'arguments': { 'type': 'full', "
208 "'model': { 'name': %s, "
209 "'props': false }}}",
210 cpu_type);
211 g_assert(resp);
212 error = resp_get_error(resp);
213 g_assert(error);
214 g_assert(g_str_equal(error,
215 "Invalid parameter type for 'props', expected: dict"));
216 qobject_unref(resp);
219 static uint64_t resp_get_sve_vls(QDict *resp)
221 QDict *props;
222 const QDictEntry *e;
223 uint64_t vls = 0;
224 int n = 0;
226 g_assert(resp);
227 g_assert(resp_has_props(resp));
229 props = resp_get_props(resp);
231 for (e = qdict_first(props); e; e = qdict_next(props, e)) {
232 if (strlen(e->key) > 3 && !strncmp(e->key, "sve", 3) &&
233 g_ascii_isdigit(e->key[3])) {
234 char *endptr;
235 int bits;
237 bits = g_ascii_strtoll(&e->key[3], &endptr, 10);
238 if (!bits || *endptr != '\0') {
239 continue;
242 if (qdict_get_bool(props, e->key)) {
243 vls |= BIT_ULL((bits / 128) - 1);
245 ++n;
249 g_assert(n == SVE_MAX_VQ);
251 return vls;
254 #define assert_sve_vls(qts, cpu_type, expected_vls, fmt, ...) \
255 ({ \
256 QDict *_resp = do_query(qts, cpu_type, fmt, ##__VA_ARGS__); \
257 g_assert(_resp); \
258 g_assert(resp_has_props(_resp)); \
259 g_assert(resp_get_sve_vls(_resp) == expected_vls); \
260 qobject_unref(_resp); \
263 static void sve_tests_default(QTestState *qts, const char *cpu_type)
266 * With no sve-max-vq or sve<N> properties on the command line
267 * the default is to have all vector lengths enabled. This also
268 * tests that 'sve' is 'on' by default.
270 assert_sve_vls(qts, cpu_type, BIT_ULL(SVE_MAX_VQ) - 1, NULL);
272 /* With SVE off, all vector lengths should also be off. */
273 assert_sve_vls(qts, cpu_type, 0, "{ 'sve': false }");
275 /* With SVE on, we must have at least one vector length enabled. */
276 assert_error(qts, cpu_type, "cannot disable sve128", "{ 'sve128': false }");
278 /* Basic enable/disable tests. */
279 assert_sve_vls(qts, cpu_type, 0x7, "{ 'sve384': true }");
280 assert_sve_vls(qts, cpu_type, ((BIT_ULL(SVE_MAX_VQ) - 1) & ~BIT_ULL(2)),
281 "{ 'sve384': false }");
284 * ---------------------------------------------------------------------
285 * power-of-two(vq) all-power- can can
286 * of-two(< vq) enable disable
287 * ---------------------------------------------------------------------
288 * vq < max_vq no MUST* yes yes
289 * vq < max_vq yes MUST* yes no
290 * ---------------------------------------------------------------------
291 * vq == max_vq n/a MUST* yes** yes**
292 * ---------------------------------------------------------------------
293 * vq > max_vq n/a no no yes
294 * vq > max_vq n/a yes yes yes
295 * ---------------------------------------------------------------------
297 * [*] "MUST" means this requirement must already be satisfied,
298 * otherwise 'max_vq' couldn't itself be enabled.
300 * [**] Not testable with the QMP interface, only with the command line.
303 /* max_vq := 8 */
304 assert_sve_vls(qts, cpu_type, 0x8b, "{ 'sve1024': true }");
306 /* max_vq := 8, vq < max_vq, !power-of-two(vq) */
307 assert_sve_vls(qts, cpu_type, 0x8f,
308 "{ 'sve1024': true, 'sve384': true }");
309 assert_sve_vls(qts, cpu_type, 0x8b,
310 "{ 'sve1024': true, 'sve384': false }");
312 /* max_vq := 8, vq < max_vq, power-of-two(vq) */
313 assert_sve_vls(qts, cpu_type, 0x8b,
314 "{ 'sve1024': true, 'sve256': true }");
315 assert_error(qts, cpu_type, "cannot disable sve256",
316 "{ 'sve1024': true, 'sve256': false }");
318 /* max_vq := 3, vq > max_vq, !all-power-of-two(< vq) */
319 assert_error(qts, cpu_type, "cannot disable sve512",
320 "{ 'sve384': true, 'sve512': false, 'sve640': true }");
323 * We can disable power-of-two vector lengths when all larger lengths
324 * are also disabled. We only need to disable the power-of-two length,
325 * as all non-enabled larger lengths will then be auto-disabled.
327 assert_sve_vls(qts, cpu_type, 0x7, "{ 'sve512': false }");
329 /* max_vq := 3, vq > max_vq, all-power-of-two(< vq) */
330 assert_sve_vls(qts, cpu_type, 0x1f,
331 "{ 'sve384': true, 'sve512': true, 'sve640': true }");
332 assert_sve_vls(qts, cpu_type, 0xf,
333 "{ 'sve384': true, 'sve512': true, 'sve640': false }");
336 static void sve_tests_sve_max_vq_8(const void *data)
338 QTestState *qts;
340 qts = qtest_init(MACHINE "-cpu max,sve-max-vq=8");
342 assert_sve_vls(qts, "max", BIT_ULL(8) - 1, NULL);
345 * Disabling the max-vq set by sve-max-vq is not allowed, but
346 * of course enabling it is OK.
348 assert_error(qts, "max", "cannot disable sve1024", "{ 'sve1024': false }");
349 assert_sve_vls(qts, "max", 0xff, "{ 'sve1024': true }");
352 * Enabling anything larger than max-vq set by sve-max-vq is not
353 * allowed, but of course disabling everything larger is OK.
355 assert_error(qts, "max", "cannot enable sve1152", "{ 'sve1152': true }");
356 assert_sve_vls(qts, "max", 0xff, "{ 'sve1152': false }");
359 * We can enable/disable non power-of-two lengths smaller than the
360 * max-vq set by sve-max-vq, but, while we can enable power-of-two
361 * lengths, we can't disable them.
363 assert_sve_vls(qts, "max", 0xff, "{ 'sve384': true }");
364 assert_sve_vls(qts, "max", 0xfb, "{ 'sve384': false }");
365 assert_sve_vls(qts, "max", 0xff, "{ 'sve256': true }");
366 assert_error(qts, "max", "cannot disable sve256", "{ 'sve256': false }");
368 qtest_quit(qts);
371 static void sve_tests_sve_off(const void *data)
373 QTestState *qts;
375 qts = qtest_init(MACHINE "-cpu max,sve=off");
377 /* SVE is off, so the map should be empty. */
378 assert_sve_vls(qts, "max", 0, NULL);
380 /* The map stays empty even if we turn lengths off. */
381 assert_sve_vls(qts, "max", 0, "{ 'sve128': false }");
383 /* It's an error to enable lengths when SVE is off. */
384 assert_error(qts, "max", "cannot enable sve128", "{ 'sve128': true }");
386 /* With SVE re-enabled we should get all vector lengths enabled. */
387 assert_sve_vls(qts, "max", BIT_ULL(SVE_MAX_VQ) - 1, "{ 'sve': true }");
389 /* Or enable SVE with just specific vector lengths. */
390 assert_sve_vls(qts, "max", 0x3,
391 "{ 'sve': true, 'sve128': true, 'sve256': true }");
393 qtest_quit(qts);
396 static void sve_tests_sve_off_kvm(const void *data)
398 QTestState *qts;
400 qts = qtest_init(MACHINE_KVM "-cpu max,sve=off");
403 * We don't know if this host supports SVE so we don't
404 * attempt to test enabling anything. We only test that
405 * everything is disabled (as it should be with sve=off)
406 * and that using sve<N>=off to explicitly disable vector
407 * lengths is OK too.
409 assert_sve_vls(qts, "max", 0, NULL);
410 assert_sve_vls(qts, "max", 0, "{ 'sve128': false }");
412 qtest_quit(qts);
415 static void pauth_tests_default(QTestState *qts, const char *cpu_type)
417 assert_has_feature_enabled(qts, cpu_type, "pauth");
418 assert_has_feature_disabled(qts, cpu_type, "pauth-impdef");
419 assert_set_feature(qts, cpu_type, "pauth", false);
420 assert_set_feature(qts, cpu_type, "pauth", true);
421 assert_set_feature(qts, cpu_type, "pauth-impdef", true);
422 assert_set_feature(qts, cpu_type, "pauth-impdef", false);
423 assert_error(qts, cpu_type, "cannot enable pauth-impdef without pauth",
424 "{ 'pauth': false, 'pauth-impdef': true }");
427 static void test_query_cpu_model_expansion(const void *data)
429 QTestState *qts;
431 qts = qtest_init(MACHINE "-cpu max");
433 /* Test common query-cpu-model-expansion input validation */
434 assert_type_full(qts);
435 assert_bad_props(qts, "max");
436 assert_error(qts, "foo", "The CPU type 'foo' is not a recognized "
437 "ARM CPU type", NULL);
438 assert_error(qts, "max", "Parameter 'not-a-prop' is unexpected",
439 "{ 'not-a-prop': false }");
440 assert_error(qts, "host", "The CPU type 'host' requires KVM", NULL);
442 /* Test expected feature presence/absence for some cpu types */
443 assert_has_feature_enabled(qts, "cortex-a15", "pmu");
444 assert_has_not_feature(qts, "cortex-a15", "aarch64");
446 /* Enabling and disabling pmu should always work. */
447 assert_has_feature_enabled(qts, "max", "pmu");
448 assert_set_feature(qts, "max", "pmu", false);
449 assert_set_feature(qts, "max", "pmu", true);
451 assert_has_not_feature(qts, "max", "kvm-no-adjvtime");
452 assert_has_not_feature(qts, "max", "kvm-steal-time");
454 if (g_str_equal(qtest_get_arch(), "aarch64")) {
455 assert_has_feature_enabled(qts, "max", "aarch64");
456 assert_has_feature_enabled(qts, "max", "sve");
457 assert_has_feature_enabled(qts, "max", "sve128");
458 assert_has_feature_enabled(qts, "cortex-a57", "pmu");
459 assert_has_feature_enabled(qts, "cortex-a57", "aarch64");
461 assert_has_feature_enabled(qts, "a64fx", "pmu");
462 assert_has_feature_enabled(qts, "a64fx", "aarch64");
464 * A64FX does not support any other vector lengths besides those
465 * that are enabled by default(128bit, 256bits, 512bit).
467 assert_has_feature_enabled(qts, "a64fx", "sve");
468 assert_sve_vls(qts, "a64fx", 0xb, NULL);
469 assert_error(qts, "a64fx", "cannot enable sve384",
470 "{ 'sve384': true }");
471 assert_error(qts, "a64fx", "cannot enable sve640",
472 "{ 'sve640': true }");
474 sve_tests_default(qts, "max");
475 pauth_tests_default(qts, "max");
477 /* Test that features that depend on KVM generate errors without. */
478 assert_error(qts, "max",
479 "'aarch64' feature cannot be disabled "
480 "unless KVM is enabled and 32-bit EL1 "
481 "is supported",
482 "{ 'aarch64': false }");
485 qtest_quit(qts);
488 static void test_query_cpu_model_expansion_kvm(const void *data)
490 QTestState *qts;
492 qts = qtest_init(MACHINE_KVM "-cpu max");
494 /* Enabling and disabling kvm-no-adjvtime should always work. */
495 assert_has_feature_disabled(qts, "host", "kvm-no-adjvtime");
496 assert_set_feature(qts, "host", "kvm-no-adjvtime", true);
497 assert_set_feature(qts, "host", "kvm-no-adjvtime", false);
499 if (g_str_equal(qtest_get_arch(), "aarch64")) {
500 bool kvm_supports_steal_time;
501 bool kvm_supports_sve;
502 char max_name[8], name[8];
503 uint32_t max_vq, vq;
504 uint64_t vls;
505 QDict *resp;
506 char *error;
508 assert_error(qts, "cortex-a15",
509 "We cannot guarantee the CPU type 'cortex-a15' works "
510 "with KVM on this host", NULL);
512 assert_has_feature_enabled(qts, "host", "aarch64");
514 /* Enabling and disabling pmu should always work. */
515 assert_has_feature_enabled(qts, "host", "pmu");
516 assert_set_feature(qts, "host", "pmu", false);
517 assert_set_feature(qts, "host", "pmu", true);
520 * Some features would be enabled by default, but they're disabled
521 * because this instance of KVM doesn't support them. Test that the
522 * features are present, and, when enabled, issue further tests.
524 assert_has_feature(qts, "host", "kvm-steal-time");
525 assert_has_feature(qts, "host", "sve");
527 resp = do_query_no_props(qts, "host");
528 kvm_supports_steal_time = resp_get_feature(resp, "kvm-steal-time");
529 kvm_supports_sve = resp_get_feature(resp, "sve");
530 vls = resp_get_sve_vls(resp);
531 qobject_unref(resp);
533 if (kvm_supports_steal_time) {
534 /* If we have steal-time then we should be able to toggle it. */
535 assert_set_feature(qts, "host", "kvm-steal-time", false);
536 assert_set_feature(qts, "host", "kvm-steal-time", true);
539 if (kvm_supports_sve) {
540 g_assert(vls != 0);
541 max_vq = 64 - __builtin_clzll(vls);
542 sprintf(max_name, "sve%u", max_vq * 128);
544 /* Enabling a supported length is of course fine. */
545 assert_sve_vls(qts, "host", vls, "{ %s: true }", max_name);
547 /* Get the next supported length smaller than max-vq. */
548 vq = 64 - __builtin_clzll(vls & ~BIT_ULL(max_vq - 1));
549 if (vq) {
551 * We have at least one length smaller than max-vq,
552 * so we can disable max-vq.
554 assert_sve_vls(qts, "host", (vls & ~BIT_ULL(max_vq - 1)),
555 "{ %s: false }", max_name);
558 * Smaller, supported vector lengths cannot be disabled
559 * unless all larger, supported vector lengths are also
560 * disabled.
562 sprintf(name, "sve%u", vq * 128);
563 error = g_strdup_printf("cannot disable %s", name);
564 assert_error(qts, "host", error,
565 "{ %s: true, %s: false }",
566 max_name, name);
567 g_free(error);
571 * The smallest, supported vector length is required, because
572 * we need at least one vector length enabled.
574 vq = __builtin_ffsll(vls);
575 sprintf(name, "sve%u", vq * 128);
576 error = g_strdup_printf("cannot disable %s", name);
577 assert_error(qts, "host", error, "{ %s: false }", name);
578 g_free(error);
580 /* Get an unsupported length. */
581 for (vq = 1; vq <= max_vq; ++vq) {
582 if (!(vls & BIT_ULL(vq - 1))) {
583 break;
586 if (vq <= SVE_MAX_VQ) {
587 sprintf(name, "sve%u", vq * 128);
588 error = g_strdup_printf("cannot enable %s", name);
589 assert_error(qts, "host", error, "{ %s: true }", name);
590 g_free(error);
592 } else {
593 g_assert(vls == 0);
595 } else {
596 assert_has_not_feature(qts, "host", "aarch64");
597 assert_has_not_feature(qts, "host", "pmu");
598 assert_has_not_feature(qts, "host", "sve");
599 assert_has_not_feature(qts, "host", "kvm-steal-time");
602 qtest_quit(qts);
605 int main(int argc, char **argv)
607 g_test_init(&argc, &argv, NULL);
609 qtest_add_data_func("/arm/query-cpu-model-expansion",
610 NULL, test_query_cpu_model_expansion);
613 * For now we only run KVM specific tests with AArch64 QEMU in
614 * order avoid attempting to run an AArch32 QEMU with KVM on
615 * AArch64 hosts. That won't work and isn't easy to detect.
617 if (g_str_equal(qtest_get_arch(), "aarch64") && qtest_has_accel("kvm")) {
619 * This tests target the 'host' CPU type, so register it only if
620 * KVM is available.
622 qtest_add_data_func("/arm/kvm/query-cpu-model-expansion",
623 NULL, test_query_cpu_model_expansion_kvm);
626 if (g_str_equal(qtest_get_arch(), "aarch64")) {
627 qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-max-vq-8",
628 NULL, sve_tests_sve_max_vq_8);
629 qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-off",
630 NULL, sve_tests_sve_off);
631 qtest_add_data_func("/arm/kvm/query-cpu-model-expansion/sve-off",
632 NULL, sve_tests_sve_off_kvm);
635 return g_test_run();