Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 9 | #include "hf/arch/vm/power_mgmt.h" |
| 10 | |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame] | 11 | #include "hf/boot_params.h" |
| 12 | #include "hf/fdt_handler.h" |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 13 | #include "hf/memiter.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 14 | #include "hf/std.h" |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 15 | |
David Brazdil | 3ad6e54 | 2019-09-13 17:17:09 +0100 | [diff] [blame] | 16 | #include "hftest_common.h" |
Andrew Walbran | 1e7c774 | 2019-12-13 17:10:02 +0000 | [diff] [blame] | 17 | #include "test/hftest.h" |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 18 | |
Daniel Boulby | 61049dc | 2023-06-16 14:15:21 +0100 | [diff] [blame] | 19 | #define HFTEST_CTRL_JSON_START "[hftest_ctrl:json_start]" |
| 20 | #define HFTEST_CTRL_JSON_END "[hftest_ctrl:json_end]" |
| 21 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 22 | HFTEST_ENABLE(); |
| 23 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 24 | static struct hftest_test hftest_constructed[HFTEST_MAX_TESTS]; |
| 25 | static size_t hftest_count; |
| 26 | static struct hftest_test *hftest_list; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 27 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 28 | static struct hftest_context global_context; |
| 29 | |
| 30 | struct hftest_context *hftest_get_context(void) |
| 31 | { |
| 32 | return &global_context; |
| 33 | } |
| 34 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 35 | /** |
| 36 | * Adds the given test information to the global list, to be used by |
| 37 | * `hftest_use_registered_list`. |
| 38 | */ |
| 39 | void hftest_register(struct hftest_test test) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 40 | { |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 41 | if (hftest_count < HFTEST_MAX_TESTS) { |
| 42 | hftest_constructed[hftest_count++] = test; |
| 43 | } else { |
Andrew Walbran | 78a63b7 | 2019-03-18 17:28:22 +0000 | [diff] [blame] | 44 | HFTEST_FAIL(true, "Too many tests"); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Uses the list of tests registered by `hftest_register(...)` as the ones to |
| 50 | * run. |
| 51 | */ |
| 52 | void hftest_use_registered_list(void) |
| 53 | { |
| 54 | hftest_list = hftest_constructed; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Uses the given list of tests as the ones to run. |
| 59 | */ |
| 60 | void hftest_use_list(struct hftest_test list[], size_t count) |
| 61 | { |
| 62 | hftest_list = list; |
| 63 | hftest_count = count; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Writes out a JSON structure describing the available tests. |
| 68 | */ |
| 69 | void hftest_json(void) |
| 70 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 71 | const char *suite = NULL; |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 72 | size_t i; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 73 | size_t tests_in_suite = 0; |
| 74 | |
Daniel Boulby | 61049dc | 2023-06-16 14:15:21 +0100 | [diff] [blame] | 75 | /* Wrap the JSON in tags for the hftest script to use. */ |
| 76 | HFTEST_LOG(HFTEST_CTRL_JSON_START); |
| 77 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 78 | HFTEST_LOG("{"); |
| 79 | HFTEST_LOG(" \"suites\": ["); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 80 | for (i = 0; i < hftest_count; ++i) { |
| 81 | struct hftest_test *test = &hftest_list[i]; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 82 | if (test->suite != suite) { |
| 83 | /* Close out previously open suite. */ |
| 84 | if (tests_in_suite) { |
| 85 | HFTEST_LOG(" ]"); |
| 86 | HFTEST_LOG(" },"); |
| 87 | } |
| 88 | /* Move onto new suite. */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 89 | suite = test->suite; |
| 90 | tests_in_suite = 0; |
| 91 | HFTEST_LOG(" {"); |
| 92 | HFTEST_LOG(" \"name\": \"%s\",", test->suite); |
| 93 | } |
| 94 | if (test->kind == HFTEST_KIND_SET_UP) { |
| 95 | HFTEST_LOG(" \"setup\": true,"); |
| 96 | } |
| 97 | if (test->kind == HFTEST_KIND_TEAR_DOWN) { |
| 98 | HFTEST_LOG(" \"teardown\": true,"); |
| 99 | } |
| 100 | if (test->kind == HFTEST_KIND_TEST) { |
J-Alves | d459b56 | 2022-12-05 14:56:33 +0000 | [diff] [blame] | 101 | /* |
| 102 | * If test has a precondition, run respective function. |
| 103 | * If it returns false, then the current setup is not |
| 104 | * meant to run the test. Hence, we must skip it. |
| 105 | */ |
| 106 | bool skip_test = test->precondition != NULL && |
| 107 | !test->precondition(); |
| 108 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 109 | if (!tests_in_suite) { |
| 110 | HFTEST_LOG(" \"tests\": ["); |
| 111 | } |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 112 | /* |
| 113 | * It's easier to put the comma at the start of the line |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 114 | * than the end even though the JSON looks a bit funky. |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 115 | */ |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 116 | HFTEST_LOG(" %c{", tests_in_suite ? ',' : ' '); |
| 117 | HFTEST_LOG(" \"name\": \"%s\",", test->name); |
J-Alves | d459b56 | 2022-12-05 14:56:33 +0000 | [diff] [blame] | 118 | HFTEST_LOG(" \"is_long_running\": %s,", |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 119 | test->is_long_running ? "true" : "false"); |
J-Alves | d459b56 | 2022-12-05 14:56:33 +0000 | [diff] [blame] | 120 | HFTEST_LOG(" \"skip_test\": %s", |
| 121 | skip_test ? "true" : "false"); |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 122 | HFTEST_LOG(" }"); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 123 | ++tests_in_suite; |
| 124 | } |
| 125 | } |
| 126 | if (tests_in_suite) { |
| 127 | HFTEST_LOG(" ]"); |
| 128 | HFTEST_LOG(" }"); |
| 129 | } |
| 130 | HFTEST_LOG(" ]"); |
| 131 | HFTEST_LOG("}"); |
Daniel Boulby | 61049dc | 2023-06-16 14:15:21 +0100 | [diff] [blame] | 132 | |
| 133 | /* Wrap the JSON in tags for the hftest script to use. */ |
| 134 | HFTEST_LOG(HFTEST_CTRL_JSON_END); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 137 | /** |
| 138 | * Logs a failure message and shut down. |
| 139 | */ |
Andrew Scull | a59f9bc | 2019-04-03 15:24:35 +0100 | [diff] [blame] | 140 | noreturn void abort(void) |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 141 | { |
| 142 | HFTEST_LOG("FAIL"); |
Andrew Walbran | c6903d1 | 2019-03-05 18:28:20 +0000 | [diff] [blame] | 143 | arch_power_off(); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 146 | static void run_test(hftest_test_fn set_up, hftest_test_fn test, |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 147 | hftest_test_fn tear_down, const struct fdt *fdt) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 148 | { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 149 | /* Prepare the context. */ |
| 150 | struct hftest_context *ctx = hftest_get_context(); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 151 | memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx)); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 152 | ctx->abort = abort; |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame] | 153 | ctx->fdt = fdt; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 154 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 155 | /* Run any set up functions. */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 156 | if (set_up) { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 157 | set_up(); |
| 158 | if (ctx->failures) { |
| 159 | abort(); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 163 | /* Run the test. */ |
| 164 | test(); |
| 165 | if (ctx->failures) { |
| 166 | abort(); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 169 | /* Run any tear down functions. */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 170 | if (tear_down) { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 171 | tear_down(); |
| 172 | if (ctx->failures) { |
| 173 | abort(); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 177 | HFTEST_LOG("FINISHED"); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 180 | /** |
| 181 | * Runs the given test case. |
| 182 | */ |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame] | 183 | void hftest_run(struct memiter suite_name, struct memiter test_name, |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 184 | const struct fdt *fdt) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 185 | { |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 186 | size_t i; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 187 | hftest_test_fn suite_set_up = NULL; |
| 188 | hftest_test_fn suite_tear_down = NULL; |
| 189 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 190 | for (i = 0; i < hftest_count; ++i) { |
| 191 | struct hftest_test *test = &hftest_list[i]; |
Andrew Walbran | 320c84e | 2019-11-12 12:48:24 +0000 | [diff] [blame] | 192 | |
| 193 | /* Check if this test is part of the suite we want. */ |
| 194 | if (memiter_iseq(&suite_name, test->suite)) { |
| 195 | switch (test->kind) { |
| 196 | /* |
| 197 | * The first entries in the suite are the set up and |
| 198 | * tear down functions. |
| 199 | */ |
| 200 | case HFTEST_KIND_SET_UP: |
| 201 | suite_set_up = test->fn; |
| 202 | break; |
| 203 | case HFTEST_KIND_TEAR_DOWN: |
| 204 | suite_tear_down = test->fn; |
| 205 | break; |
| 206 | /* Find the test. */ |
| 207 | case HFTEST_KIND_TEST: |
| 208 | if (memiter_iseq(&test_name, test->name)) { |
| 209 | run_test(suite_set_up, test->fn, |
| 210 | suite_tear_down, fdt); |
| 211 | return; |
| 212 | } |
| 213 | break; |
| 214 | default: |
| 215 | /* Ignore other kinds. */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 216 | break; |
| 217 | } |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | HFTEST_LOG("Unable to find requested tests."); |
| 222 | } |
| 223 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 224 | /** |
| 225 | * Writes out usage information. |
| 226 | */ |
| 227 | void hftest_help(void) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 228 | { |
| 229 | HFTEST_LOG("usage:"); |
| 230 | HFTEST_LOG(""); |
| 231 | HFTEST_LOG(" help"); |
| 232 | HFTEST_LOG(""); |
| 233 | HFTEST_LOG(" Show this help."); |
| 234 | HFTEST_LOG(""); |
| 235 | HFTEST_LOG(" json"); |
| 236 | HFTEST_LOG(""); |
| 237 | HFTEST_LOG( |
| 238 | " Print a directory of test suites and tests in " |
| 239 | "JSON " |
| 240 | "format."); |
| 241 | HFTEST_LOG(""); |
| 242 | HFTEST_LOG(" run <suite> <test>"); |
| 243 | HFTEST_LOG(""); |
| 244 | HFTEST_LOG(" Run the named test from the named test suite."); |
| 245 | } |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame] | 246 | |
J-Alves | 070a40d | 2021-01-21 14:35:12 +0000 | [diff] [blame] | 247 | void hftest_command(struct fdt *fdt) |
| 248 | { |
| 249 | struct memiter command_line; |
| 250 | struct memiter command; |
| 251 | |
| 252 | if (!hftest_ctrl_start(fdt, &command_line)) { |
| 253 | HFTEST_LOG("Unable to read the command line."); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (!memiter_parse_str(&command_line, &command)) { |
| 258 | HFTEST_LOG("Unable to parse command."); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | if (memiter_iseq(&command, "exit")) { |
| 263 | hftest_device_exit_test_environment(); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | if (memiter_iseq(&command, "json")) { |
| 268 | hftest_json(); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | if (memiter_iseq(&command, "run")) { |
| 273 | struct memiter suite_name; |
| 274 | struct memiter test_name; |
| 275 | |
| 276 | if (!memiter_parse_str(&command_line, &suite_name)) { |
| 277 | HFTEST_LOG("Unable to parse test suite."); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | if (!memiter_parse_str(&command_line, &test_name)) { |
| 282 | HFTEST_LOG("Unable to parse test."); |
| 283 | return; |
| 284 | } |
| 285 | hftest_run(suite_name, test_name, fdt); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | hftest_help(); |
| 290 | } |
| 291 | |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame] | 292 | static uintptr_t vcpu_index_to_id(size_t index) |
| 293 | { |
| 294 | /* For now we use indices as IDs for vCPUs. */ |
| 295 | return index; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Get the ID of the CPU with the given index. |
| 300 | */ |
Karl Meakin | 2b56fc1 | 2024-07-02 15:24:27 +0100 | [diff] [blame] | 301 | cpu_id_t hftest_get_cpu_id(size_t index) |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame] | 302 | { |
| 303 | struct boot_params params; |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 304 | const struct fdt *fdt = hftest_get_context()->fdt; |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame] | 305 | |
| 306 | if (fdt == NULL) { |
| 307 | /* |
| 308 | * We must be in a service VM, so apply the mapping that Hafnium |
| 309 | * uses for vCPU IDs. |
| 310 | */ |
| 311 | return vcpu_index_to_id(index); |
| 312 | } |
| 313 | |
J-Alves | 0c029c2 | 2023-06-21 10:32:57 +0100 | [diff] [blame] | 314 | /* |
| 315 | * VM is primary VM. Convert vCPU ids to the linear cpu id as passed to |
| 316 | * the primary VM in the FDT structure. |
| 317 | */ |
| 318 | index = MAX_CPUS - index; |
| 319 | |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame] | 320 | /* Find physical CPU ID from FDT. */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 321 | fdt_find_cpus(fdt, params.cpu_ids, ¶ms.cpu_count); |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame] | 322 | |
| 323 | return params.cpu_ids[index]; |
| 324 | } |