| 1 | /* |
| 2 | * Arm CPU feature test cases |
| 3 | * |
| 4 | * Copyright (c) 2019 Red Hat Inc. |
| 5 | * Authors: |
| 6 | * Andrew Jones <drjones@redhat.com> |
| 7 | * |
| 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. |
| 10 | */ |
| 11 | #include "qemu/osdep.h" |
| 12 | #include "qemu/bitops.h" |
| 13 | #include "libqtest.h" |
| 14 | #include "qobject/qdict.h" |
| 15 | #include "qobject/qjson.h" |
| 16 | |
| 17 | /* |
| 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. |
| 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 " |
| 25 | #define QUERY_HEAD "{ 'execute': 'query-cpu-model-expansion', " \ |
| 26 | " 'arguments': { 'type': 'full', " |
| 27 | #define QUERY_TAIL "}}" |
| 28 | |
| 29 | static 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 | |
| 35 | G_GNUC_PRINTF(3, 4) |
| 36 | static QDict *do_query(QTestState *qts, const char *cpu_type, |
| 37 | const char *fmt, ...) |
| 38 | { |
| 39 | QDict *resp; |
| 40 | |
| 41 | if (fmt) { |
| 42 | QDict *args; |
| 43 | va_list ap; |
| 44 | |
| 45 | va_start(ap, fmt); |
| 46 | args = qdict_from_vjsonf_nofail(fmt, ap); |
| 47 | va_end(ap); |
| 48 | |
| 49 | resp = qtest_qmp(qts, QUERY_HEAD "'model': { 'name': %s, " |
| 50 | "'props': %p }" |
| 51 | QUERY_TAIL, cpu_type, args); |
| 52 | } else { |
| 53 | resp = do_query_no_props(qts, cpu_type); |
| 54 | } |
| 55 | |
| 56 | return resp; |
| 57 | } |
| 58 | |
| 59 | static const char *resp_get_error(QDict *resp) |
| 60 | { |
| 61 | QDict *qdict; |
| 62 | |
| 63 | g_assert(resp); |
| 64 | |
| 65 | qdict = qdict_get_qdict(resp, "error"); |
| 66 | if (qdict) { |
| 67 | return qdict_get_str(qdict, "desc"); |
| 68 | } |
| 69 | |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | #define assert_error(qts, cpu_type, expected_error, fmt, ...) \ |
| 74 | ({ \ |
| 75 | QDict *_resp; \ |
| 76 | const char *_error; \ |
| 77 | \ |
| 78 | _resp = do_query(qts, cpu_type, fmt, ##__VA_ARGS__); \ |
| 79 | g_assert(_resp); \ |
| 80 | _error = resp_get_error(_resp); \ |
| 81 | g_assert(_error); \ |
| 82 | g_assert_cmpstr(_error, ==, expected_error); \ |
| 83 | qobject_unref(_resp); \ |
| 84 | }) |
| 85 | |
| 86 | static bool resp_has_props(QDict *resp) |
| 87 | { |
| 88 | QDict *qdict; |
| 89 | |
| 90 | g_assert(resp); |
| 91 | |
| 92 | if (!qdict_haskey(resp, "return")) { |
| 93 | return false; |
| 94 | } |
| 95 | qdict = qdict_get_qdict(resp, "return"); |
| 96 | |
| 97 | if (!qdict_haskey(qdict, "model")) { |
| 98 | return false; |
| 99 | } |
| 100 | qdict = qdict_get_qdict(qdict, "model"); |
| 101 | |
| 102 | return qdict_haskey(qdict, "props"); |
| 103 | } |
| 104 | |
| 105 | static QDict *resp_get_props(QDict *resp) |
| 106 | { |
| 107 | QDict *qdict; |
| 108 | |
| 109 | g_assert(resp); |
| 110 | g_assert(resp_has_props(resp)); |
| 111 | |
| 112 | qdict = qdict_get_qdict(resp, "return"); |
| 113 | qdict = qdict_get_qdict(qdict, "model"); |
| 114 | qdict = qdict_get_qdict(qdict, "props"); |
| 115 | |
| 116 | return qdict; |
| 117 | } |
| 118 | |
| 119 | static bool resp_get_feature(QDict *resp, const char *feature) |
| 120 | { |
| 121 | QDict *props; |
| 122 | |
| 123 | g_assert(resp); |
| 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); |
| 128 | } |
| 129 | |
| 130 | #define assert_has_feature(qts, cpu_type, feature) \ |
| 131 | ({ \ |
| 132 | QDict *_resp = do_query_no_props(qts, cpu_type); \ |
| 133 | g_assert(_resp); \ |
| 134 | g_assert(resp_has_props(_resp)); \ |
| 135 | g_assert(qdict_get(resp_get_props(_resp), feature)); \ |
| 136 | qobject_unref(_resp); \ |
| 137 | }) |
| 138 | |
| 139 | #define assert_has_not_feature(qts, cpu_type, feature) \ |
| 140 | ({ \ |
| 141 | QDict *_resp = do_query_no_props(qts, cpu_type); \ |
| 142 | g_assert(_resp); \ |
| 143 | g_assert(!resp_has_props(_resp) || \ |
| 144 | !qdict_get(resp_get_props(_resp), feature)); \ |
| 145 | qobject_unref(_resp); \ |
| 146 | }) |
| 147 | |
| 148 | #define resp_assert_feature(resp, feature, expected_value) \ |
| 149 | ({ \ |
| 150 | QDict *_props; \ |
| 151 | \ |
| 152 | g_assert(_resp); \ |
| 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)); \ |
| 157 | }) |
| 158 | |
| 159 | #define assert_feature(qts, cpu_type, feature, expected_value) \ |
| 160 | ({ \ |
| 161 | QDict *_resp; \ |
| 162 | \ |
| 163 | _resp = do_query_no_props(qts, cpu_type); \ |
| 164 | g_assert(_resp); \ |
| 165 | resp_assert_feature(_resp, feature, expected_value); \ |
| 166 | qobject_unref(_resp); \ |
| 167 | }) |
| 168 | |
| 169 | #define assert_set_feature(qts, cpu_type, feature, value) \ |
| 170 | ({ \ |
| 171 | const char *_fmt = (value) ? "{ %s: true }" : "{ %s: false }"; \ |
| 172 | QDict *_resp; \ |
| 173 | \ |
| 174 | _resp = do_query(qts, cpu_type, _fmt, feature); \ |
| 175 | g_assert(_resp); \ |
| 176 | resp_assert_feature(_resp, feature, value); \ |
| 177 | qobject_unref(_resp); \ |
| 178 | }) |
| 179 | |
| 180 | #define assert_has_feature_enabled(qts, cpu_type, feature) \ |
| 181 | assert_feature(qts, cpu_type, feature, true) |
| 182 | |
| 183 | #define assert_has_feature_disabled(qts, cpu_type, feature) \ |
| 184 | assert_feature(qts, cpu_type, feature, false) |
| 185 | |
| 186 | static void assert_type_full(QTestState *qts) |
| 187 | { |
| 188 | const char *error; |
| 189 | QDict *resp; |
| 190 | |
| 191 | resp = qtest_qmp(qts, "{ 'execute': 'query-cpu-model-expansion', " |
| 192 | "'arguments': { 'type': 'static', " |
| 193 | "'model': { 'name': 'foo' }}}"); |
| 194 | g_assert(resp); |
| 195 | error = resp_get_error(resp); |
| 196 | g_assert(error); |
| 197 | g_assert_cmpstr(error, ==, |
| 198 | "The requested expansion type is not supported"); |
| 199 | qobject_unref(resp); |
| 200 | } |
| 201 | |
| 202 | static void assert_bad_props(QTestState *qts, const char *cpu_type) |
| 203 | { |
| 204 | const char *error; |
| 205 | QDict *resp; |
| 206 | |
| 207 | resp = qtest_qmp(qts, "{ 'execute': 'query-cpu-model-expansion', " |
| 208 | "'arguments': { 'type': 'full', " |
| 209 | "'model': { 'name': %s, " |
| 210 | "'props': false }}}", |
| 211 | cpu_type); |
| 212 | g_assert(resp); |
| 213 | error = resp_get_error(resp); |
| 214 | g_assert(error); |
| 215 | g_assert_cmpstr(error, ==, |
| 216 | "Invalid parameter type for 'model.props'," |
| 217 | " expected: object"); |
| 218 | qobject_unref(resp); |
| 219 | } |
| 220 | |
| 221 | static uint64_t resp_get_sve_vls(QDict *resp) |
| 222 | { |
| 223 | QDict *props; |
| 224 | const QDictEntry *e; |
| 225 | uint64_t vls = 0; |
| 226 | int n = 0; |
| 227 | |
| 228 | g_assert(resp); |
| 229 | g_assert(resp_has_props(resp)); |
| 230 | |
| 231 | props = resp_get_props(resp); |
| 232 | |
| 233 | for (e = qdict_first(props); e; e = qdict_next(props, e)) { |
| 234 | if (strlen(e->key) > 3 && !strncmp(e->key, "sve", 3) && |
| 235 | g_ascii_isdigit(e->key[3])) { |
| 236 | char *endptr; |
| 237 | int bits; |
| 238 | |
| 239 | bits = g_ascii_strtoll(&e->key[3], &endptr, 10); |
| 240 | if (!bits || *endptr != '\0') { |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | if (qdict_get_bool(props, e->key)) { |
| 245 | vls |= BIT_ULL((bits / 128) - 1); |
| 246 | } |
| 247 | ++n; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | g_assert(n == SVE_MAX_VQ); |
| 252 | |
| 253 | return vls; |
| 254 | } |
| 255 | |
| 256 | #define assert_sve_vls(qts, cpu_type, expected_vls, fmt, ...) \ |
| 257 | ({ \ |
| 258 | QDict *_resp = do_query(qts, cpu_type, fmt, ##__VA_ARGS__); \ |
| 259 | g_assert(_resp); \ |
| 260 | g_assert(resp_has_props(_resp)); \ |
| 261 | g_assert(resp_get_sve_vls(_resp) == expected_vls); \ |
| 262 | qobject_unref(_resp); \ |
| 263 | }) |
| 264 | |
| 265 | static void sve_tests_default(QTestState *qts, const char *cpu_type) |
| 266 | { |
| 267 | /* |
| 268 | * With no sve-max-vq or sve<N> properties on the command line |
| 269 | * the default is to have all vector lengths enabled. This also |
| 270 | * tests that 'sve' is 'on' by default. |
| 271 | */ |
| 272 | assert_sve_vls(qts, cpu_type, BIT_ULL(SVE_MAX_VQ) - 1, NULL); |
| 273 | |
| 274 | /* With SVE off, all vector lengths should also be off. */ |
| 275 | assert_sve_vls(qts, cpu_type, 0, "{ 'sve': false }"); |
| 276 | |
| 277 | /* With SVE on, we must have at least one vector length enabled. */ |
| 278 | assert_error(qts, cpu_type, "cannot disable sve128", "{ 'sve128': false }"); |
| 279 | |
| 280 | /* Basic enable/disable tests. */ |
| 281 | assert_sve_vls(qts, cpu_type, 0x7, "{ 'sve384': true }"); |
| 282 | assert_sve_vls(qts, cpu_type, ((BIT_ULL(SVE_MAX_VQ) - 1) & ~BIT_ULL(2)), |
| 283 | "{ 'sve384': false }"); |
| 284 | |
| 285 | /* |
| 286 | * --------------------------------------------------------------------- |
| 287 | * power-of-two(vq) all-power- can can |
| 288 | * of-two(< vq) enable disable |
| 289 | * --------------------------------------------------------------------- |
| 290 | * vq < max_vq no MUST* yes yes |
| 291 | * vq < max_vq yes MUST* yes no |
| 292 | * --------------------------------------------------------------------- |
| 293 | * vq == max_vq n/a MUST* yes** yes** |
| 294 | * --------------------------------------------------------------------- |
| 295 | * vq > max_vq n/a no no yes |
| 296 | * vq > max_vq n/a yes yes yes |
| 297 | * --------------------------------------------------------------------- |
| 298 | * |
| 299 | * [*] "MUST" means this requirement must already be satisfied, |
| 300 | * otherwise 'max_vq' couldn't itself be enabled. |
| 301 | * |
| 302 | * [**] Not testable with the QMP interface, only with the command line. |
| 303 | */ |
| 304 | |
| 305 | /* max_vq := 8 */ |
| 306 | assert_sve_vls(qts, cpu_type, 0x8b, "{ 'sve1024': true }"); |
| 307 | |
| 308 | /* max_vq := 8, vq < max_vq, !power-of-two(vq) */ |
| 309 | assert_sve_vls(qts, cpu_type, 0x8f, |
| 310 | "{ 'sve1024': true, 'sve384': true }"); |
| 311 | assert_sve_vls(qts, cpu_type, 0x8b, |
| 312 | "{ 'sve1024': true, 'sve384': false }"); |
| 313 | |
| 314 | /* max_vq := 8, vq < max_vq, power-of-two(vq) */ |
| 315 | assert_sve_vls(qts, cpu_type, 0x8b, |
| 316 | "{ 'sve1024': true, 'sve256': true }"); |
| 317 | assert_error(qts, cpu_type, "cannot disable sve256", |
| 318 | "{ 'sve1024': true, 'sve256': false }"); |
| 319 | |
| 320 | /* max_vq := 3, vq > max_vq, !all-power-of-two(< vq) */ |
| 321 | assert_error(qts, cpu_type, "cannot disable sve512", |
| 322 | "{ 'sve384': true, 'sve512': false, 'sve640': true }"); |
| 323 | |
| 324 | /* |
| 325 | * We can disable power-of-two vector lengths when all larger lengths |
| 326 | * are also disabled. We only need to disable the power-of-two length, |
| 327 | * as all non-enabled larger lengths will then be auto-disabled. |
| 328 | */ |
| 329 | assert_sve_vls(qts, cpu_type, 0x7, "{ 'sve512': false }"); |
| 330 | |
| 331 | /* max_vq := 3, vq > max_vq, all-power-of-two(< vq) */ |
| 332 | assert_sve_vls(qts, cpu_type, 0x1f, |
| 333 | "{ 'sve384': true, 'sve512': true, 'sve640': true }"); |
| 334 | assert_sve_vls(qts, cpu_type, 0xf, |
| 335 | "{ 'sve384': true, 'sve512': true, 'sve640': false }"); |
| 336 | } |
| 337 | |
| 338 | static void sve_tests_sve_max_vq_8(const void *data) |
| 339 | { |
| 340 | QTestState *qts; |
| 341 | |
| 342 | qts = qtest_init(MACHINE "-cpu max,sve-max-vq=8"); |
| 343 | |
| 344 | assert_sve_vls(qts, "max", BIT_ULL(8) - 1, NULL); |
| 345 | |
| 346 | /* |
| 347 | * Disabling the max-vq set by sve-max-vq is not allowed, but |
| 348 | * of course enabling it is OK. |
| 349 | */ |
| 350 | assert_error(qts, "max", "cannot disable sve1024", "{ 'sve1024': false }"); |
| 351 | assert_sve_vls(qts, "max", 0xff, "{ 'sve1024': true }"); |
| 352 | |
| 353 | /* |
| 354 | * Enabling anything larger than max-vq set by sve-max-vq is not |
| 355 | * allowed, but of course disabling everything larger is OK. |
| 356 | */ |
| 357 | assert_error(qts, "max", "cannot enable sve1152", "{ 'sve1152': true }"); |
| 358 | assert_sve_vls(qts, "max", 0xff, "{ 'sve1152': false }"); |
| 359 | |
| 360 | /* |
| 361 | * We can enable/disable non power-of-two lengths smaller than the |
| 362 | * max-vq set by sve-max-vq, but, while we can enable power-of-two |
| 363 | * lengths, we can't disable them. |
| 364 | */ |
| 365 | assert_sve_vls(qts, "max", 0xff, "{ 'sve384': true }"); |
| 366 | assert_sve_vls(qts, "max", 0xfb, "{ 'sve384': false }"); |
| 367 | assert_sve_vls(qts, "max", 0xff, "{ 'sve256': true }"); |
| 368 | assert_error(qts, "max", "cannot disable sve256", "{ 'sve256': false }"); |
| 369 | |
| 370 | qtest_quit(qts); |
| 371 | } |
| 372 | |
| 373 | static void sve_tests_sve_off(const void *data) |
| 374 | { |
| 375 | QTestState *qts; |
| 376 | |
| 377 | qts = qtest_init(MACHINE "-cpu max,sve=off"); |
| 378 | |
| 379 | /* SVE is off, so the map should be empty. */ |
| 380 | assert_sve_vls(qts, "max", 0, NULL); |
| 381 | |
| 382 | /* The map stays empty even if we turn lengths off. */ |
| 383 | assert_sve_vls(qts, "max", 0, "{ 'sve128': false }"); |
| 384 | |
| 385 | /* It's an error to enable lengths when SVE is off. */ |
| 386 | assert_error(qts, "max", "cannot enable sve128", "{ 'sve128': true }"); |
| 387 | |
| 388 | /* With SVE re-enabled we should get all vector lengths enabled. */ |
| 389 | assert_sve_vls(qts, "max", BIT_ULL(SVE_MAX_VQ) - 1, "{ 'sve': true }"); |
| 390 | |
| 391 | /* Or enable SVE with just specific vector lengths. */ |
| 392 | assert_sve_vls(qts, "max", 0x3, |
| 393 | "{ 'sve': true, 'sve128': true, 'sve256': true }"); |
| 394 | |
| 395 | qtest_quit(qts); |
| 396 | } |
| 397 | |
| 398 | static void sve_tests_sve_off_kvm(const void *data) |
| 399 | { |
| 400 | QTestState *qts; |
| 401 | |
| 402 | qts = qtest_init(MACHINE_KVM "-cpu max,sve=off"); |
| 403 | |
| 404 | /* |
| 405 | * We don't know if this host supports SVE so we don't |
| 406 | * attempt to test enabling anything. We only test that |
| 407 | * everything is disabled (as it should be with sve=off) |
| 408 | * and that using sve<N>=off to explicitly disable vector |
| 409 | * lengths is OK too. |
| 410 | */ |
| 411 | assert_sve_vls(qts, "max", 0, NULL); |
| 412 | assert_sve_vls(qts, "max", 0, "{ 'sve128': false }"); |
| 413 | |
| 414 | qtest_quit(qts); |
| 415 | } |
| 416 | |
| 417 | static void pauth_tests_default(QTestState *qts, const char *cpu_type) |
| 418 | { |
| 419 | assert_has_feature_enabled(qts, cpu_type, "pauth"); |
| 420 | assert_has_feature_disabled(qts, cpu_type, "pauth-impdef"); |
| 421 | assert_has_feature_disabled(qts, cpu_type, "pauth-qarma3"); |
| 422 | assert_has_feature_disabled(qts, cpu_type, "pauth-qarma5"); |
| 423 | assert_set_feature(qts, cpu_type, "pauth", false); |
| 424 | assert_set_feature(qts, cpu_type, "pauth", true); |
| 425 | assert_set_feature(qts, cpu_type, "pauth-impdef", true); |
| 426 | assert_set_feature(qts, cpu_type, "pauth-impdef", false); |
| 427 | assert_set_feature(qts, cpu_type, "pauth-qarma3", true); |
| 428 | assert_set_feature(qts, cpu_type, "pauth-qarma3", false); |
| 429 | assert_set_feature(qts, cpu_type, "pauth-qarma5", true); |
| 430 | assert_set_feature(qts, cpu_type, "pauth-qarma5", false); |
| 431 | assert_error(qts, cpu_type, |
| 432 | "cannot enable pauth-impdef, pauth-qarma3 or pauth-qarma5 without pauth", |
| 433 | "{ 'pauth': false, 'pauth-impdef': true }"); |
| 434 | assert_error(qts, cpu_type, |
| 435 | "cannot enable pauth-impdef, pauth-qarma3 or pauth-qarma5 without pauth", |
| 436 | "{ 'pauth': false, 'pauth-qarma3': true }"); |
| 437 | assert_error(qts, cpu_type, |
| 438 | "cannot enable pauth-impdef, pauth-qarma3 or pauth-qarma5 without pauth", |
| 439 | "{ 'pauth': false, 'pauth-qarma5': true }"); |
| 440 | assert_error(qts, cpu_type, |
| 441 | "cannot enable pauth-impdef, pauth-qarma3 and pauth-qarma5 at the same time", |
| 442 | "{ 'pauth': true, 'pauth-impdef': true, 'pauth-qarma3': true," |
| 443 | " 'pauth-qarma5': true }"); |
| 444 | } |
| 445 | |
| 446 | static void test_query_cpu_model_expansion(const void *data) |
| 447 | { |
| 448 | QTestState *qts; |
| 449 | |
| 450 | qts = qtest_init(MACHINE "-cpu max"); |
| 451 | |
| 452 | /* Test common query-cpu-model-expansion input validation */ |
| 453 | assert_type_full(qts); |
| 454 | assert_bad_props(qts, "max"); |
| 455 | assert_error(qts, "foo", "The CPU type 'foo' is not a recognized " |
| 456 | "ARM CPU type", NULL); |
| 457 | assert_error(qts, "max", "Parameter 'model.props.not-a-prop' is unexpected", |
| 458 | "{ 'not-a-prop': false }"); |
| 459 | assert_error(qts, "host", "The CPU type 'host' requires KVM", NULL); |
| 460 | |
| 461 | /* Test expected feature presence/absence for some cpu types */ |
| 462 | assert_has_feature_enabled(qts, "cortex-a15", "pmu"); |
| 463 | assert_has_not_feature(qts, "cortex-a15", "aarch64"); |
| 464 | |
| 465 | /* Enabling and disabling pmu should always work. */ |
| 466 | assert_has_feature_enabled(qts, "max", "pmu"); |
| 467 | assert_set_feature(qts, "max", "pmu", false); |
| 468 | assert_set_feature(qts, "max", "pmu", true); |
| 469 | |
| 470 | assert_has_not_feature(qts, "max", "kvm-no-adjvtime"); |
| 471 | assert_has_not_feature(qts, "max", "kvm-steal-time"); |
| 472 | |
| 473 | if (g_str_equal(qtest_get_arch(), "aarch64")) { |
| 474 | assert_has_feature_enabled(qts, "max", "aarch64"); |
| 475 | assert_has_feature_enabled(qts, "max", "sve"); |
| 476 | assert_has_feature_enabled(qts, "max", "sve128"); |
| 477 | assert_has_feature_enabled(qts, "cortex-a57", "pmu"); |
| 478 | assert_has_feature_enabled(qts, "cortex-a57", "aarch64"); |
| 479 | |
| 480 | assert_has_feature_enabled(qts, "a64fx", "pmu"); |
| 481 | assert_has_feature_enabled(qts, "a64fx", "aarch64"); |
| 482 | /* |
| 483 | * A64FX does not support any other vector lengths besides those |
| 484 | * that are enabled by default(128bit, 256bits, 512bit). |
| 485 | */ |
| 486 | assert_has_feature_enabled(qts, "a64fx", "sve"); |
| 487 | assert_sve_vls(qts, "a64fx", 0xb, NULL); |
| 488 | assert_error(qts, "a64fx", "cannot enable sve384", |
| 489 | "{ 'sve384': true }"); |
| 490 | assert_error(qts, "a64fx", "cannot enable sve640", |
| 491 | "{ 'sve640': true }"); |
| 492 | |
| 493 | sve_tests_default(qts, "max"); |
| 494 | pauth_tests_default(qts, "max"); |
| 495 | |
| 496 | /* TCG allows us to turn off AArch64 on the 'max-v8' CPU type */ |
| 497 | assert_set_feature(qts, "max-v8", "aarch64", false); |
| 498 | } |
| 499 | |
| 500 | qtest_quit(qts); |
| 501 | } |
| 502 | |
| 503 | static void test_query_cpu_model_expansion_kvm(const void *data) |
| 504 | { |
| 505 | QTestState *qts; |
| 506 | |
| 507 | qts = qtest_init(MACHINE_KVM "-cpu max"); |
| 508 | |
| 509 | /* Enabling and disabling kvm-no-adjvtime should always work. */ |
| 510 | assert_has_feature_disabled(qts, "host", "kvm-no-adjvtime"); |
| 511 | assert_set_feature(qts, "host", "kvm-no-adjvtime", true); |
| 512 | assert_set_feature(qts, "host", "kvm-no-adjvtime", false); |
| 513 | |
| 514 | if (g_str_equal(qtest_get_arch(), "aarch64")) { |
| 515 | bool kvm_supports_pmu; |
| 516 | bool kvm_supports_steal_time; |
| 517 | bool kvm_supports_sve; |
| 518 | uint32_t max_vq, vq; |
| 519 | uint64_t vls; |
| 520 | QDict *resp; |
| 521 | char *error; |
| 522 | |
| 523 | /* |
| 524 | * When using KVM, only the 'host' and 'max' CPU models are |
| 525 | * supported. Test that we're emitting a suitable error for |
| 526 | * unsupported CPU models. |
| 527 | */ |
| 528 | if (qtest_has_accel("tcg")) { |
| 529 | assert_error(qts, "cortex-a7", |
| 530 | "We cannot guarantee the CPU type 'cortex-a7' works " |
| 531 | "with KVM on this host", NULL); |
| 532 | } else { |
| 533 | /* |
| 534 | * With a KVM-only build the 32-bit CPUs are not present. |
| 535 | */ |
| 536 | assert_error(qts, "cortex-a7", |
| 537 | "The CPU type 'cortex-a7' is not a " |
| 538 | "recognized ARM CPU type", NULL); |
| 539 | } |
| 540 | |
| 541 | assert_has_feature_enabled(qts, "host", "aarch64"); |
| 542 | |
| 543 | /* |
| 544 | * Some features would be enabled by default, but they're disabled |
| 545 | * because this instance of KVM doesn't support them. Test that the |
| 546 | * features are present, and, when enabled, issue further tests. |
| 547 | */ |
| 548 | assert_has_feature(qts, "host", "kvm-steal-time"); |
| 549 | assert_has_feature(qts, "host", "sve"); |
| 550 | |
| 551 | resp = do_query_no_props(qts, "host"); |
| 552 | kvm_supports_pmu = resp_get_feature(resp, "pmu"); |
| 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); |
| 556 | qobject_unref(resp); |
| 557 | |
| 558 | if (kvm_supports_pmu) { |
| 559 | /* If we have pmu then we should be able to toggle it. */ |
| 560 | assert_set_feature(qts, "host", "pmu", false); |
| 561 | assert_set_feature(qts, "host", "pmu", true); |
| 562 | } |
| 563 | |
| 564 | if (kvm_supports_steal_time) { |
| 565 | /* If we have steal-time then we should be able to toggle it. */ |
| 566 | assert_set_feature(qts, "host", "kvm-steal-time", false); |
| 567 | assert_set_feature(qts, "host", "kvm-steal-time", true); |
| 568 | } |
| 569 | |
| 570 | if (kvm_supports_sve) { |
| 571 | g_autofree const char *max_name = NULL; |
| 572 | g_autofree const char *name = NULL; |
| 573 | |
| 574 | g_assert(vls != 0); |
| 575 | max_vq = 64 - __builtin_clzll(vls); |
| 576 | max_name = g_strdup_printf("sve%u", max_vq * 128); |
| 577 | |
| 578 | /* Enabling a supported length is of course fine. */ |
| 579 | assert_sve_vls(qts, "host", vls, "{ %s: true }", max_name); |
| 580 | |
| 581 | /* Get the next supported length smaller than max-vq. */ |
| 582 | vq = 64 - __builtin_clzll(vls & ~BIT_ULL(max_vq - 1)); |
| 583 | if (vq) { |
| 584 | g_autofree const char *name2 = |
| 585 | g_strdup_printf("sve%u", vq * 128); |
| 586 | |
| 587 | /* |
| 588 | * We have at least one length smaller than max-vq, |
| 589 | * so we can disable max-vq. |
| 590 | */ |
| 591 | assert_sve_vls(qts, "host", (vls & ~BIT_ULL(max_vq - 1)), |
| 592 | "{ %s: false }", max_name); |
| 593 | |
| 594 | /* |
| 595 | * Smaller, supported vector lengths cannot be disabled |
| 596 | * unless all larger, supported vector lengths are also |
| 597 | * disabled. |
| 598 | */ |
| 599 | error = g_strdup_printf("cannot disable %s", name2); |
| 600 | assert_error(qts, "host", error, |
| 601 | "{ %s: true, %s: false }", |
| 602 | max_name, name2); |
| 603 | g_free(error); |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * The smallest, supported vector length is required, because |
| 608 | * we need at least one vector length enabled. |
| 609 | */ |
| 610 | vq = __builtin_ffsll(vls); |
| 611 | name = g_strdup_printf("sve%u", vq * 128); |
| 612 | error = g_strdup_printf("cannot disable %s", name); |
| 613 | assert_error(qts, "host", error, "{ %s: false }", name); |
| 614 | g_free(error); |
| 615 | |
| 616 | /* Get an unsupported length. */ |
| 617 | for (vq = 1; vq <= max_vq; ++vq) { |
| 618 | if (!(vls & BIT_ULL(vq - 1))) { |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | if (vq <= SVE_MAX_VQ) { |
| 623 | g_autofree const char *name2 = |
| 624 | g_strdup_printf("sve%u", vq * 128); |
| 625 | |
| 626 | error = g_strdup_printf("cannot enable %s", name2); |
| 627 | assert_error(qts, "host", error, "{ %s: true }", name2); |
| 628 | g_free(error); |
| 629 | } |
| 630 | } else { |
| 631 | g_assert(vls == 0); |
| 632 | } |
| 633 | } else { |
| 634 | assert_has_not_feature(qts, "host", "aarch64"); |
| 635 | assert_has_not_feature(qts, "host", "pmu"); |
| 636 | assert_has_not_feature(qts, "host", "sve"); |
| 637 | assert_has_not_feature(qts, "host", "kvm-steal-time"); |
| 638 | } |
| 639 | |
| 640 | qtest_quit(qts); |
| 641 | } |
| 642 | |
| 643 | int main(int argc, char **argv) |
| 644 | { |
| 645 | g_test_init(&argc, &argv, NULL); |
| 646 | |
| 647 | if (!qtest_has_machine("virt")) { |
| 648 | goto out; |
| 649 | } |
| 650 | |
| 651 | if (qtest_has_accel("tcg")) { |
| 652 | qtest_add_data_func("/arm/query-cpu-model-expansion", |
| 653 | NULL, test_query_cpu_model_expansion); |
| 654 | } |
| 655 | |
| 656 | if (!g_str_equal(qtest_get_arch(), "aarch64")) { |
| 657 | goto out; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * For now we only run KVM specific tests with AArch64 QEMU in |
| 662 | * order avoid attempting to run an AArch32 QEMU with KVM on |
| 663 | * AArch64 hosts. That won't work and isn't easy to detect. |
| 664 | */ |
| 665 | if (qtest_has_accel("kvm")) { |
| 666 | /* |
| 667 | * This tests target the 'host' CPU type, so register it only if |
| 668 | * KVM is available. |
| 669 | */ |
| 670 | qtest_add_data_func("/arm/kvm/query-cpu-model-expansion", |
| 671 | NULL, test_query_cpu_model_expansion_kvm); |
| 672 | |
| 673 | qtest_add_data_func("/arm/kvm/query-cpu-model-expansion/sve-off", |
| 674 | NULL, sve_tests_sve_off_kvm); |
| 675 | } |
| 676 | |
| 677 | if (qtest_has_accel("tcg")) { |
| 678 | qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-max-vq-8", |
| 679 | NULL, sve_tests_sve_max_vq_8); |
| 680 | qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-off", |
| 681 | NULL, sve_tests_sve_off); |
| 682 | } |
| 683 | |
| 684 | out: |
| 685 | return g_test_run(); |
| 686 | } |