blob: d3c57c6ba56179d9251270c73efffc67f2907ccc [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * 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 Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scullf0551c82018-12-15 20:38:47 +00009#include "hf/arch/vm/power_mgmt.h"
10
Andrew Walbranafabe852019-03-20 17:55:11 +000011#include "hf/boot_params.h"
12#include "hf/fdt_handler.h"
Andrew Scullbc7189d2018-08-14 09:35:13 +010013#include "hf/memiter.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010014#include "hf/std.h"
Andrew Scullbc7189d2018-08-14 09:35:13 +010015
David Brazdil3ad6e542019-09-13 17:17:09 +010016#include "hftest_common.h"
Andrew Walbran1e7c7742019-12-13 17:10:02 +000017#include "test/hftest.h"
Andrew Scullbc7189d2018-08-14 09:35:13 +010018
19HFTEST_ENABLE();
20
Andrew Walbranbc342d42019-02-05 16:56:02 +000021static struct hftest_test hftest_constructed[HFTEST_MAX_TESTS];
22static size_t hftest_count;
23static struct hftest_test *hftest_list;
Andrew Scullbc7189d2018-08-14 09:35:13 +010024
Andrew Scullf0551c82018-12-15 20:38:47 +000025static struct hftest_context global_context;
26
27struct hftest_context *hftest_get_context(void)
28{
29 return &global_context;
30}
31
Andrew Walbranbc342d42019-02-05 16:56:02 +000032/**
33 * Adds the given test information to the global list, to be used by
34 * `hftest_use_registered_list`.
35 */
36void hftest_register(struct hftest_test test)
Andrew Scullbc7189d2018-08-14 09:35:13 +010037{
Andrew Walbranbc342d42019-02-05 16:56:02 +000038 if (hftest_count < HFTEST_MAX_TESTS) {
39 hftest_constructed[hftest_count++] = test;
40 } else {
Andrew Walbran78a63b72019-03-18 17:28:22 +000041 HFTEST_FAIL(true, "Too many tests");
Andrew Walbranbc342d42019-02-05 16:56:02 +000042 }
43}
44
45/**
46 * Uses the list of tests registered by `hftest_register(...)` as the ones to
47 * run.
48 */
49void 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 */
57void 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 */
66void hftest_json(void)
67{
Andrew Scullbc7189d2018-08-14 09:35:13 +010068 const char *suite = NULL;
Andrew Walbranbc342d42019-02-05 16:56:02 +000069 size_t i;
Andrew Scullbc7189d2018-08-14 09:35:13 +010070 size_t tests_in_suite = 0;
71
72 HFTEST_LOG("{");
73 HFTEST_LOG(" \"suites\": [");
Andrew Walbranbc342d42019-02-05 16:56:02 +000074 for (i = 0; i < hftest_count; ++i) {
75 struct hftest_test *test = &hftest_list[i];
Andrew Scullbc7189d2018-08-14 09:35:13 +010076 if (test->suite != suite) {
77 /* Close out previously open suite. */
78 if (tests_in_suite) {
79 HFTEST_LOG(" ]");
80 HFTEST_LOG(" },");
81 }
82 /* Move onto new suite. */
Andrew Scullbc7189d2018-08-14 09:35:13 +010083 suite = test->suite;
84 tests_in_suite = 0;
85 HFTEST_LOG(" {");
86 HFTEST_LOG(" \"name\": \"%s\",", test->suite);
87 }
88 if (test->kind == HFTEST_KIND_SET_UP) {
89 HFTEST_LOG(" \"setup\": true,");
90 }
91 if (test->kind == HFTEST_KIND_TEAR_DOWN) {
92 HFTEST_LOG(" \"teardown\": true,");
93 }
94 if (test->kind == HFTEST_KIND_TEST) {
J-Alvesd459b562022-12-05 14:56:33 +000095 /*
96 * If test has a precondition, run respective function.
97 * If it returns false, then the current setup is not
98 * meant to run the test. Hence, we must skip it.
99 */
100 bool skip_test = test->precondition != NULL &&
101 !test->precondition();
102
Andrew Scullbc7189d2018-08-14 09:35:13 +0100103 if (!tests_in_suite) {
104 HFTEST_LOG(" \"tests\": [");
105 }
Andrew Scullf0551c82018-12-15 20:38:47 +0000106 /*
107 * It's easier to put the comma at the start of the line
Andrew Scull2b5fbad2019-04-05 13:55:56 +0100108 * than the end even though the JSON looks a bit funky.
Andrew Scullf0551c82018-12-15 20:38:47 +0000109 */
David Brazdil3cc24aa2019-09-27 10:24:41 +0100110 HFTEST_LOG(" %c{", tests_in_suite ? ',' : ' ');
111 HFTEST_LOG(" \"name\": \"%s\",", test->name);
J-Alvesd459b562022-12-05 14:56:33 +0000112 HFTEST_LOG(" \"is_long_running\": %s,",
David Brazdil3cc24aa2019-09-27 10:24:41 +0100113 test->is_long_running ? "true" : "false");
J-Alvesd459b562022-12-05 14:56:33 +0000114 HFTEST_LOG(" \"skip_test\": %s",
115 skip_test ? "true" : "false");
David Brazdil3cc24aa2019-09-27 10:24:41 +0100116 HFTEST_LOG(" }");
Andrew Scullbc7189d2018-08-14 09:35:13 +0100117 ++tests_in_suite;
118 }
119 }
120 if (tests_in_suite) {
121 HFTEST_LOG(" ]");
122 HFTEST_LOG(" }");
123 }
124 HFTEST_LOG(" ]");
125 HFTEST_LOG("}");
126}
127
Andrew Walbranbc342d42019-02-05 16:56:02 +0000128/**
129 * Logs a failure message and shut down.
130 */
Andrew Sculla59f9bc2019-04-03 15:24:35 +0100131noreturn void abort(void)
Andrew Scullf0551c82018-12-15 20:38:47 +0000132{
133 HFTEST_LOG("FAIL");
Andrew Walbranc6903d12019-03-05 18:28:20 +0000134 arch_power_off();
Andrew Scullf0551c82018-12-15 20:38:47 +0000135}
136
Andrew Scullbc7189d2018-08-14 09:35:13 +0100137static void run_test(hftest_test_fn set_up, hftest_test_fn test,
David Brazdilb856be62020-03-25 10:14:55 +0000138 hftest_test_fn tear_down, const struct fdt *fdt)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100139{
Andrew Scullf0551c82018-12-15 20:38:47 +0000140 /* Prepare the context. */
141 struct hftest_context *ctx = hftest_get_context();
Andrew Scull2b5fbad2019-04-05 13:55:56 +0100142 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
Andrew Scullf0551c82018-12-15 20:38:47 +0000143 ctx->abort = abort;
Andrew Walbranafabe852019-03-20 17:55:11 +0000144 ctx->fdt = fdt;
Andrew Scullbc7189d2018-08-14 09:35:13 +0100145
Andrew Scullf0551c82018-12-15 20:38:47 +0000146 /* Run any set up functions. */
Andrew Scullbc7189d2018-08-14 09:35:13 +0100147 if (set_up) {
Andrew Scullf0551c82018-12-15 20:38:47 +0000148 set_up();
149 if (ctx->failures) {
150 abort();
Andrew Scullbc7189d2018-08-14 09:35:13 +0100151 }
152 }
153
Andrew Scullf0551c82018-12-15 20:38:47 +0000154 /* Run the test. */
155 test();
156 if (ctx->failures) {
157 abort();
Andrew Scullbc7189d2018-08-14 09:35:13 +0100158 }
159
Andrew Scullf0551c82018-12-15 20:38:47 +0000160 /* Run any tear down functions. */
Andrew Scullbc7189d2018-08-14 09:35:13 +0100161 if (tear_down) {
Andrew Scullf0551c82018-12-15 20:38:47 +0000162 tear_down();
163 if (ctx->failures) {
164 abort();
Andrew Scullbc7189d2018-08-14 09:35:13 +0100165 }
166 }
167
Andrew Scullf0551c82018-12-15 20:38:47 +0000168 HFTEST_LOG("FINISHED");
Andrew Scullbc7189d2018-08-14 09:35:13 +0100169}
170
Andrew Walbranbc342d42019-02-05 16:56:02 +0000171/**
172 * Runs the given test case.
173 */
Andrew Walbranafabe852019-03-20 17:55:11 +0000174void hftest_run(struct memiter suite_name, struct memiter test_name,
David Brazdilb856be62020-03-25 10:14:55 +0000175 const struct fdt *fdt)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100176{
Andrew Walbranbc342d42019-02-05 16:56:02 +0000177 size_t i;
Andrew Scullbc7189d2018-08-14 09:35:13 +0100178 hftest_test_fn suite_set_up = NULL;
179 hftest_test_fn suite_tear_down = NULL;
180
Andrew Walbranbc342d42019-02-05 16:56:02 +0000181 for (i = 0; i < hftest_count; ++i) {
182 struct hftest_test *test = &hftest_list[i];
Andrew Walbran320c84e2019-11-12 12:48:24 +0000183
184 /* Check if this test is part of the suite we want. */
185 if (memiter_iseq(&suite_name, test->suite)) {
186 switch (test->kind) {
187 /*
188 * The first entries in the suite are the set up and
189 * tear down functions.
190 */
191 case HFTEST_KIND_SET_UP:
192 suite_set_up = test->fn;
193 break;
194 case HFTEST_KIND_TEAR_DOWN:
195 suite_tear_down = test->fn;
196 break;
197 /* Find the test. */
198 case HFTEST_KIND_TEST:
199 if (memiter_iseq(&test_name, test->name)) {
200 run_test(suite_set_up, test->fn,
201 suite_tear_down, fdt);
202 return;
203 }
204 break;
205 default:
206 /* Ignore other kinds. */
Andrew Scullbc7189d2018-08-14 09:35:13 +0100207 break;
208 }
Andrew Scullbc7189d2018-08-14 09:35:13 +0100209 }
210 }
211
212 HFTEST_LOG("Unable to find requested tests.");
213}
214
Andrew Walbranbc342d42019-02-05 16:56:02 +0000215/**
216 * Writes out usage information.
217 */
218void hftest_help(void)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100219{
220 HFTEST_LOG("usage:");
221 HFTEST_LOG("");
222 HFTEST_LOG(" help");
223 HFTEST_LOG("");
224 HFTEST_LOG(" Show this help.");
225 HFTEST_LOG("");
226 HFTEST_LOG(" json");
227 HFTEST_LOG("");
228 HFTEST_LOG(
229 " Print a directory of test suites and tests in "
230 "JSON "
231 "format.");
232 HFTEST_LOG("");
233 HFTEST_LOG(" run <suite> <test>");
234 HFTEST_LOG("");
235 HFTEST_LOG(" Run the named test from the named test suite.");
236}
Andrew Walbranafabe852019-03-20 17:55:11 +0000237
J-Alves070a40d2021-01-21 14:35:12 +0000238void hftest_command(struct fdt *fdt)
239{
240 struct memiter command_line;
241 struct memiter command;
242
243 if (!hftest_ctrl_start(fdt, &command_line)) {
244 HFTEST_LOG("Unable to read the command line.");
245 return;
246 }
247
248 if (!memiter_parse_str(&command_line, &command)) {
249 HFTEST_LOG("Unable to parse command.");
250 return;
251 }
252
253 if (memiter_iseq(&command, "exit")) {
254 hftest_device_exit_test_environment();
255 return;
256 }
257
258 if (memiter_iseq(&command, "json")) {
259 hftest_json();
260 return;
261 }
262
263 if (memiter_iseq(&command, "run")) {
264 struct memiter suite_name;
265 struct memiter test_name;
266
267 if (!memiter_parse_str(&command_line, &suite_name)) {
268 HFTEST_LOG("Unable to parse test suite.");
269 return;
270 }
271
272 if (!memiter_parse_str(&command_line, &test_name)) {
273 HFTEST_LOG("Unable to parse test.");
274 return;
275 }
276 hftest_run(suite_name, test_name, fdt);
277 return;
278 }
279
280 hftest_help();
281}
282
Andrew Walbranafabe852019-03-20 17:55:11 +0000283static uintptr_t vcpu_index_to_id(size_t index)
284{
285 /* For now we use indices as IDs for vCPUs. */
286 return index;
287}
288
289/**
290 * Get the ID of the CPU with the given index.
291 */
292uintptr_t hftest_get_cpu_id(size_t index)
293{
294 struct boot_params params;
David Brazdilb856be62020-03-25 10:14:55 +0000295 const struct fdt *fdt = hftest_get_context()->fdt;
Andrew Walbranafabe852019-03-20 17:55:11 +0000296
297 if (fdt == NULL) {
298 /*
299 * We must be in a service VM, so apply the mapping that Hafnium
300 * uses for vCPU IDs.
301 */
302 return vcpu_index_to_id(index);
303 }
304
305 /* Find physical CPU ID from FDT. */
David Brazdilb856be62020-03-25 10:14:55 +0000306 fdt_find_cpus(fdt, params.cpu_ids, &params.cpu_count);
Andrew Walbranafabe852019-03-20 17:55:11 +0000307
308 return params.cpu_ids[index];
309}