1
2
3
4
5
6
7
8
9
10
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"
16
17
18
19
20
21#define SVE_MAX_VQ 16
22
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 "}}"
28
29static QDict *do_query_no_props(QTestState *qts, const char *cpu_type)
30{
31 return qtest_qmp(qts, QUERY_HEAD "'model': { 'name': %s }"
32 QUERY_TAIL, cpu_type);
33}
34
35static QDict *do_query(QTestState *qts, const char *cpu_type,
36 const char *fmt, ...)
37{
38 QDict *resp;
39
40 if (fmt) {
41 QDict *args;
42 va_list ap;
43
44 va_start(ap, fmt);
45 args = qdict_from_vjsonf_nofail(fmt, ap);
46 va_end(ap);
47
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);
53 }
54
55 return resp;
56}
57
58static const char *resp_get_error(QDict *resp)
59{
60 QDict *qdict;
61
62 g_assert(resp);
63
64 qdict = qdict_get_qdict(resp, "error");
65 if (qdict) {
66 return qdict_get_str(qdict, "desc");
67 }
68
69 return NULL;
70}
71
72#define assert_error(qts, cpu_type, expected_error, fmt, ...) \
73({ \
74 QDict *_resp; \
75 const char *_error; \
76 \
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); \
83})
84
85static bool resp_has_props(QDict *resp)
86{
87 QDict *qdict;
88
89 g_assert(resp);
90
91 if (!qdict_haskey(resp, "return")) {
92 return false;
93 }
94 qdict = qdict_get_qdict(resp, "return");
95
96 if (!qdict_haskey(qdict, "model")) {
97 return false;
98 }
99 qdict = qdict_get_qdict(qdict, "model");
100
101 return qdict_haskey(qdict, "props");
102}
103
104static QDict *resp_get_props(QDict *resp)
105{
106 QDict *qdict;
107
108 g_assert(resp);
109 g_assert(resp_has_props(resp));
110
111 qdict = qdict_get_qdict(resp, "return");
112 qdict = qdict_get_qdict(qdict, "model");
113 qdict = qdict_get_qdict(qdict, "props");
114
115 return qdict;
116}
117
118static bool resp_get_feature(QDict *resp, const char *feature)
119{
120 QDict *props;
121
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);
127}
128
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); \
136})
137
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); \
145})
146
147#define resp_assert_feature(resp, feature, expected_value) \
148({ \
149 QDict *_props; \
150 \
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)); \
156})
157
158#define assert_feature(qts, cpu_type, feature, expected_value) \
159({ \
160 QDict *_resp; \
161 \
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); \
166})
167
168#define assert_set_feature(qts, cpu_type, feature, value) \
169({ \
170 const char *_fmt = (value) ? "{ %s: true }" : "{ %s: false }"; \
171 QDict *_resp; \
172 \
173 _resp = do_query(qts, cpu_type, _fmt, feature); \
174 g_assert(_resp); \
175 resp_assert_feature(_resp, feature, value); \
176 qobject_unref(_resp); \
177})
178
179#define assert_has_feature_enabled(qts, cpu_type, feature) \
180 assert_feature(qts, cpu_type, feature, true)
181
182#define assert_has_feature_disabled(qts, cpu_type, feature) \
183 assert_feature(qts, cpu_type, feature, false)
184
185static void assert_type_full(QTestState *qts)
186{
187 const char *error;
188 QDict *resp;
189
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);
199}
200
201static void assert_bad_props(QTestState *qts, const char *cpu_type)
202{
203 const char *error;
204 QDict *resp;
205
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);
217}
218
219static uint64_t resp_get_sve_vls(QDict *resp)
220{
221 QDict *props;
222 const QDictEntry *e;
223 uint64_t vls = 0;
224 int n = 0;
225
226 g_assert(resp);
227 g_assert(resp_has_props(resp));
228
229 props = resp_get_props(resp);
230
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;
236
237 bits = g_ascii_strtoll(&e->key[3], &endptr, 10);
238 if (!bits || *endptr != '\0') {
239 continue;
240 }
241
242 if (qdict_get_bool(props, e->key)) {
243 vls |= BIT_ULL((bits / 128) - 1);
244 }
245 ++n;
246 }
247 }
248
249 g_assert(n == SVE_MAX_VQ);
250
251 return vls;
252}
253
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); \
261})
262
263static void sve_tests_default(QTestState *qts, const char *cpu_type)
264{
265
266
267
268
269
270 assert_sve_vls(qts, cpu_type, BIT_ULL(SVE_MAX_VQ) - 1, NULL);
271
272
273 assert_sve_vls(qts, cpu_type, 0, "{ 'sve': false }");
274
275
276 assert_error(qts, cpu_type, "cannot disable sve128", "{ 'sve128': false }");
277
278
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 }");
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304 assert_sve_vls(qts, cpu_type, 0x8b, "{ 'sve1024': true }");
305
306
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 }");
311
312
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 }");
317
318
319 assert_error(qts, cpu_type, "cannot disable sve512",
320 "{ 'sve384': true, 'sve512': false, 'sve640': true }");
321
322
323
324
325
326
327 assert_sve_vls(qts, cpu_type, 0x7, "{ 'sve512': false }");
328
329
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 }");
334}
335
336static void sve_tests_sve_max_vq_8(const void *data)
337{
338 QTestState *qts;
339
340 qts = qtest_init(MACHINE "-cpu max,sve-max-vq=8");
341
342 assert_sve_vls(qts, "max", BIT_ULL(8) - 1, NULL);
343
344
345
346
347
348 assert_error(qts, "max", "cannot disable sve1024", "{ 'sve1024': false }");
349 assert_sve_vls(qts, "max", 0xff, "{ 'sve1024': true }");
350
351
352
353
354
355 assert_error(qts, "max", "cannot enable sve1152", "{ 'sve1152': true }");
356 assert_sve_vls(qts, "max", 0xff, "{ 'sve1152': false }");
357
358
359
360
361
362
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 }");
367
368 qtest_quit(qts);
369}
370
371static void sve_tests_sve_off(const void *data)
372{
373 QTestState *qts;
374
375 qts = qtest_init(MACHINE "-cpu max,sve=off");
376
377
378 assert_sve_vls(qts, "max", 0, NULL);
379
380
381 assert_sve_vls(qts, "max", 0, "{ 'sve128': false }");
382
383
384 assert_error(qts, "max", "cannot enable sve128", "{ 'sve128': true }");
385
386
387 assert_sve_vls(qts, "max", BIT_ULL(SVE_MAX_VQ) - 1, "{ 'sve': true }");
388
389
390 assert_sve_vls(qts, "max", 0x3,
391 "{ 'sve': true, 'sve128': true, 'sve256': true }");
392
393 qtest_quit(qts);
394}
395
396static void sve_tests_sve_off_kvm(const void *data)
397{
398 QTestState *qts;
399
400 qts = qtest_init(MACHINE_KVM "-cpu max,sve=off");
401
402
403
404
405
406
407
408
409 assert_sve_vls(qts, "max", 0, NULL);
410 assert_sve_vls(qts, "max", 0, "{ 'sve128': false }");
411
412 qtest_quit(qts);
413}
414
415static void pauth_tests_default(QTestState *qts, const char *cpu_type)
416{
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 }");
425}
426
427static void test_query_cpu_model_expansion(const void *data)
428{
429 QTestState *qts;
430
431 qts = qtest_init(MACHINE "-cpu max");
432
433
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);
441
442
443 assert_has_feature_enabled(qts, "cortex-a15", "pmu");
444 assert_has_not_feature(qts, "cortex-a15", "aarch64");
445
446
447 assert_has_feature_enabled(qts, "max", "pmu");
448 assert_set_feature(qts, "max", "pmu", false);
449 assert_set_feature(qts, "max", "pmu", true);
450
451 assert_has_not_feature(qts, "max", "kvm-no-adjvtime");
452 assert_has_not_feature(qts, "max", "kvm-steal-time");
453
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");
460
461 assert_has_feature_enabled(qts, "a64fx", "pmu");
462 assert_has_feature_enabled(qts, "a64fx", "aarch64");
463
464
465
466
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 }");
473
474 sve_tests_default(qts, "max");
475 pauth_tests_default(qts, "max");
476
477
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 }");
483 }
484
485 qtest_quit(qts);
486}
487
488static void test_query_cpu_model_expansion_kvm(const void *data)
489{
490 QTestState *qts;
491
492 qts = qtest_init(MACHINE_KVM "-cpu max");
493
494
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);
498
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;
507
508 assert_error(qts, "cortex-a15",
509 "We cannot guarantee the CPU type 'cortex-a15' works "
510 "with KVM on this host", NULL);
511
512 assert_has_feature_enabled(qts, "host", "aarch64");
513
514
515 assert_has_feature_enabled(qts, "host", "pmu");
516 assert_set_feature(qts, "host", "pmu", false);
517 assert_set_feature(qts, "host", "pmu", true);
518
519
520
521
522
523
524 assert_has_feature(qts, "host", "kvm-steal-time");
525 assert_has_feature(qts, "host", "sve");
526
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);
532
533 if (kvm_supports_steal_time) {
534
535 assert_set_feature(qts, "host", "kvm-steal-time", false);
536 assert_set_feature(qts, "host", "kvm-steal-time", true);
537 }
538
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);
543
544
545 assert_sve_vls(qts, "host", vls, "{ %s: true }", max_name);
546
547
548 vq = 64 - __builtin_clzll(vls & ~BIT_ULL(max_vq - 1));
549 if (vq) {
550
551
552
553
554 assert_sve_vls(qts, "host", (vls & ~BIT_ULL(max_vq - 1)),
555 "{ %s: false }", max_name);
556
557
558
559
560
561
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);
568 }
569
570
571
572
573
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);
579
580
581 for (vq = 1; vq <= max_vq; ++vq) {
582 if (!(vls & BIT_ULL(vq - 1))) {
583 break;
584 }
585 }
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);
591 }
592 } else {
593 g_assert(vls == 0);
594 }
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");
600 }
601
602 qtest_quit(qts);
603}
604
605int main(int argc, char **argv)
606{
607 g_test_init(&argc, &argv, NULL);
608
609 qtest_add_data_func("/arm/query-cpu-model-expansion",
610 NULL, test_query_cpu_model_expansion);
611
612
613
614
615
616
617 if (g_str_equal(qtest_get_arch(), "aarch64") && qtest_has_accel("kvm")) {
618
619
620
621
622 qtest_add_data_func("/arm/kvm/query-cpu-model-expansion",
623 NULL, test_query_cpu_model_expansion_kvm);
624 }
625
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);
633 }
634
635 return g_test_run();
636}
637