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