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