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 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 17 | #include "hftest_common.h" |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 18 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 19 | #include "hf/arch/vm/power_mgmt.h" |
| 20 | |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame^] | 21 | #include "hf/boot_params.h" |
| 22 | #include "hf/fdt_handler.h" |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 23 | #include "hf/memiter.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 24 | #include "hf/std.h" |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 25 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 26 | #include "hftest.h" |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 27 | |
| 28 | HFTEST_ENABLE(); |
| 29 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 30 | static struct hftest_test hftest_constructed[HFTEST_MAX_TESTS]; |
| 31 | static size_t hftest_count; |
| 32 | static struct hftest_test *hftest_list; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 33 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 34 | static struct hftest_context global_context; |
| 35 | |
| 36 | struct hftest_context *hftest_get_context(void) |
| 37 | { |
| 38 | return &global_context; |
| 39 | } |
| 40 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Adds the given test information to the global list, to be used by |
| 43 | * `hftest_use_registered_list`. |
| 44 | */ |
| 45 | void hftest_register(struct hftest_test test) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 46 | { |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 47 | if (hftest_count < HFTEST_MAX_TESTS) { |
| 48 | hftest_constructed[hftest_count++] = test; |
| 49 | } else { |
Andrew Walbran | 78a63b7 | 2019-03-18 17:28:22 +0000 | [diff] [blame] | 50 | HFTEST_FAIL(true, "Too many tests"); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Uses the list of tests registered by `hftest_register(...)` as the ones to |
| 56 | * run. |
| 57 | */ |
| 58 | void hftest_use_registered_list(void) |
| 59 | { |
| 60 | hftest_list = hftest_constructed; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Uses the given list of tests as the ones to run. |
| 65 | */ |
| 66 | void hftest_use_list(struct hftest_test list[], size_t count) |
| 67 | { |
| 68 | hftest_list = list; |
| 69 | hftest_count = count; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Writes out a JSON structure describing the available tests. |
| 74 | */ |
| 75 | void hftest_json(void) |
| 76 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 77 | const char *suite = NULL; |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 78 | size_t i; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 79 | size_t suites_in_image = 0; |
| 80 | size_t tests_in_suite = 0; |
| 81 | |
| 82 | HFTEST_LOG("{"); |
| 83 | HFTEST_LOG(" \"suites\": ["); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 84 | for (i = 0; i < hftest_count; ++i) { |
| 85 | struct hftest_test *test = &hftest_list[i]; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 86 | if (test->suite != suite) { |
| 87 | /* Close out previously open suite. */ |
| 88 | if (tests_in_suite) { |
| 89 | HFTEST_LOG(" ]"); |
| 90 | HFTEST_LOG(" },"); |
| 91 | } |
| 92 | /* Move onto new suite. */ |
| 93 | ++suites_in_image; |
| 94 | suite = test->suite; |
| 95 | tests_in_suite = 0; |
| 96 | HFTEST_LOG(" {"); |
| 97 | HFTEST_LOG(" \"name\": \"%s\",", test->suite); |
| 98 | } |
| 99 | if (test->kind == HFTEST_KIND_SET_UP) { |
| 100 | HFTEST_LOG(" \"setup\": true,"); |
| 101 | } |
| 102 | if (test->kind == HFTEST_KIND_TEAR_DOWN) { |
| 103 | HFTEST_LOG(" \"teardown\": true,"); |
| 104 | } |
| 105 | if (test->kind == HFTEST_KIND_TEST) { |
| 106 | if (!tests_in_suite) { |
| 107 | HFTEST_LOG(" \"tests\": ["); |
| 108 | } |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 109 | /* |
| 110 | * 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] | 111 | * than the end even though the JSON looks a bit funky. |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 112 | */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 113 | HFTEST_LOG(" %c\"%s\"", |
| 114 | tests_in_suite ? ',' : ' ', test->name); |
| 115 | ++tests_in_suite; |
| 116 | } |
| 117 | } |
| 118 | if (tests_in_suite) { |
| 119 | HFTEST_LOG(" ]"); |
| 120 | HFTEST_LOG(" }"); |
| 121 | } |
| 122 | HFTEST_LOG(" ]"); |
| 123 | HFTEST_LOG("}"); |
| 124 | } |
| 125 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 126 | /** |
| 127 | * Logs a failure message and shut down. |
| 128 | */ |
Andrew Scull | a59f9bc | 2019-04-03 15:24:35 +0100 | [diff] [blame] | 129 | noreturn void abort(void) |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 130 | { |
| 131 | HFTEST_LOG("FAIL"); |
Andrew Walbran | c6903d1 | 2019-03-05 18:28:20 +0000 | [diff] [blame] | 132 | arch_power_off(); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 135 | static void run_test(hftest_test_fn set_up, hftest_test_fn test, |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame^] | 136 | hftest_test_fn tear_down, const struct fdt_header *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 | /* Prepare the context. */ |
| 139 | struct hftest_context *ctx = hftest_get_context(); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 140 | memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx)); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 141 | ctx->abort = abort; |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame^] | 142 | ctx->fdt = fdt; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 143 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 144 | /* Run any set up functions. */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 145 | if (set_up) { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 146 | set_up(); |
| 147 | if (ctx->failures) { |
| 148 | abort(); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 152 | /* Run the test. */ |
| 153 | test(); |
| 154 | if (ctx->failures) { |
| 155 | abort(); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 158 | /* Run any tear down functions. */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 159 | if (tear_down) { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 160 | tear_down(); |
| 161 | if (ctx->failures) { |
| 162 | abort(); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 166 | HFTEST_LOG("FINISHED"); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 169 | /** |
| 170 | * Runs the given test case. |
| 171 | */ |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame^] | 172 | void hftest_run(struct memiter suite_name, struct memiter test_name, |
| 173 | const struct fdt_header *fdt) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 174 | { |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 175 | size_t i; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 176 | bool found_suite = false; |
| 177 | const char *suite = NULL; |
| 178 | hftest_test_fn suite_set_up = NULL; |
| 179 | hftest_test_fn suite_tear_down = NULL; |
| 180 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 181 | for (i = 0; i < hftest_count; ++i) { |
| 182 | struct hftest_test *test = &hftest_list[i]; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 183 | /* Find the test suite. */ |
| 184 | if (found_suite) { |
| 185 | if (test->suite != suite) { |
| 186 | /* Test wasn't in the suite. */ |
| 187 | break; |
| 188 | } |
| 189 | } else { |
| 190 | if (test->suite == suite) { |
| 191 | /* This isn't the right suite so keep going. */ |
| 192 | continue; |
| 193 | } |
| 194 | /* Examine a new suite. */ |
| 195 | suite = test->suite; |
| 196 | if (memiter_iseq(&suite_name, test->suite)) { |
| 197 | found_suite = true; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | switch (test->kind) { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 202 | /* |
| 203 | * The first entries in the suite are the set up and tear down |
| 204 | * functions. |
| 205 | */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 206 | case HFTEST_KIND_SET_UP: |
| 207 | suite_set_up = test->fn; |
| 208 | break; |
| 209 | case HFTEST_KIND_TEAR_DOWN: |
| 210 | suite_tear_down = test->fn; |
| 211 | break; |
| 212 | /* Find the test. */ |
| 213 | case HFTEST_KIND_TEST: |
| 214 | if (memiter_iseq(&test_name, test->name)) { |
| 215 | run_test(suite_set_up, test->fn, |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame^] | 216 | suite_tear_down, fdt); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | break; |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 220 | default: |
| 221 | /* Ignore other kinds. */ |
| 222 | break; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | HFTEST_LOG("Unable to find requested tests."); |
| 227 | } |
| 228 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 229 | /** |
| 230 | * Writes out usage information. |
| 231 | */ |
| 232 | void hftest_help(void) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 233 | { |
| 234 | HFTEST_LOG("usage:"); |
| 235 | HFTEST_LOG(""); |
| 236 | HFTEST_LOG(" help"); |
| 237 | HFTEST_LOG(""); |
| 238 | HFTEST_LOG(" Show this help."); |
| 239 | HFTEST_LOG(""); |
| 240 | HFTEST_LOG(" json"); |
| 241 | HFTEST_LOG(""); |
| 242 | HFTEST_LOG( |
| 243 | " Print a directory of test suites and tests in " |
| 244 | "JSON " |
| 245 | "format."); |
| 246 | HFTEST_LOG(""); |
| 247 | HFTEST_LOG(" run <suite> <test>"); |
| 248 | HFTEST_LOG(""); |
| 249 | HFTEST_LOG(" Run the named test from the named test suite."); |
| 250 | } |
Andrew Walbran | afabe85 | 2019-03-20 17:55:11 +0000 | [diff] [blame^] | 251 | |
| 252 | static uintptr_t vcpu_index_to_id(size_t index) |
| 253 | { |
| 254 | /* For now we use indices as IDs for vCPUs. */ |
| 255 | return index; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Get the ID of the CPU with the given index. |
| 260 | */ |
| 261 | uintptr_t hftest_get_cpu_id(size_t index) |
| 262 | { |
| 263 | struct boot_params params; |
| 264 | struct fdt_node n; |
| 265 | const struct fdt_header *fdt = hftest_get_context()->fdt; |
| 266 | |
| 267 | if (fdt == NULL) { |
| 268 | /* |
| 269 | * We must be in a service VM, so apply the mapping that Hafnium |
| 270 | * uses for vCPU IDs. |
| 271 | */ |
| 272 | return vcpu_index_to_id(index); |
| 273 | } |
| 274 | |
| 275 | /* Find physical CPU ID from FDT. */ |
| 276 | if (!fdt_root_node(&n, fdt)) { |
| 277 | FAIL("FDT failed validation."); |
| 278 | } |
| 279 | if (!fdt_find_child(&n, "")) { |
| 280 | FAIL("Unable to find FDT root node."); |
| 281 | } |
| 282 | fdt_find_cpus(&n, params.cpu_ids, ¶ms.cpu_count); |
| 283 | |
| 284 | return params.cpu_ids[index]; |
| 285 | } |