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