blob: 64eeac40480f78e1ba1d6cb965e6b1d20847bfc5 [file] [log] [blame]
Andrew Scullf0551c82018-12-15 20:38:47 +00001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scullf0551c82018-12-15 20:38:47 +00003 *
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
17#pragma once
18
19#include <stdnoreturn.h>
20
Andrew Walbranafabe852019-03-20 17:55:11 +000021#include "hf/fdt.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000022#include "hf/spci.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010023#include "hf/std.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000024
Andrew Walbranbc342d42019-02-05 16:56:02 +000025#define HFTEST_MAX_TESTS 50
26
Andrew Scullf0551c82018-12-15 20:38:47 +000027/*
28 * Log with the HFTEST_LOG_PREFIX and a new line. The zero is added so there is
29 * always at least one variadic argument.
30 */
31#define HFTEST_LOG(...) HFTEST_LOG_IMPL(__VA_ARGS__, 0)
32#define HFTEST_LOG_IMPL(format, ...) \
33 dlog("%s" format "\n", HFTEST_LOG_PREFIX, __VA_ARGS__)
34
35/* Helper to wrap the argument in quotes. */
36#define HFTEST_STR(str) #str
37
38/*
39 * Sections are names such that when the linker sorts them, all entries for the
40 * same test suite are contiguous and the set up and tear down entries come
41 * before the tests. This order simplifies test discovery in the running image.
42 */
43#define HFTEST_SET_UP_SECTION(suite_name) \
44 HFTEST_STR(.hftest.suite.suite_name .1set_up)
45#define HFTEST_TEAR_DOWN_SECTION(suite_name) \
46 HFTEST_STR(.hftest.suite.suite_name .1tear_down)
47#define HFTEST_TEST_SECTION(suite_name, test_name) \
48 HFTEST_STR(.hftest.suite.suite_name .2test.test_name)
49#define HFTEST_SERVICE_SECTION(service_name) \
50 HFTEST_STR(.hftest.service.service_name)
51
52/* Helpers to construct unique identifiers. */
53#define HFTEST_SET_UP_STRUCT(suite_name) hftest_set_up_##suite_name
54#define HFTEST_TEAR_DOWN_STRUCT(suite_name) hftest_tear_down_##suite_name
55#define HFTEST_TEST_STRUCT(suite_name, test_name) \
56 hftest_test_##suite_name##_##test_name
57#define HFTEST_SERVICE_STRUCT(service_name) hftest_service_##service_name
58
59#define HFTEST_SET_UP_FN(suite_name) hftest_set_up_fn_##suite_name
60#define HFTEST_TEAR_DOWN_FN(suite_name) hftest_tear_down_fn_##suite_name
61#define HFTEST_TEST_FN(suite_name, test_name) \
62 hftest_test_fn_##suite_name##_##test_name
63#define HFTEST_SERVICE_FN(service_name) hftest_service_fn_##service_name
64
Andrew Walbranbc342d42019-02-05 16:56:02 +000065#define HFTEST_SET_UP_CONSTRUCTOR(suite_name) hftest_set_up_ctor_##suite_name
66#define HFTEST_TEAR_DOWN_CONSTRUCTOR(suite_name) \
67 hftest_tear_down_ctor_##suite_name
68#define HFTEST_TEST_CONSTRUCTOR(suite_name, test_name) \
69 hftest_test_ctor_##suite_name##_##test_name
70
Andrew Scullf0551c82018-12-15 20:38:47 +000071/* Register test functions. */
72#define HFTEST_SET_UP(suite_name) \
73 static void HFTEST_SET_UP_FN(suite_name)(void); \
74 const struct hftest_test __attribute__((used)) \
75 __attribute__((section(HFTEST_SET_UP_SECTION(suite_name)))) \
76 HFTEST_SET_UP_STRUCT(suite_name) = { \
77 .suite = #suite_name, \
78 .kind = HFTEST_KIND_SET_UP, \
79 .fn = HFTEST_SET_UP_FN(suite_name), \
80 }; \
Andrew Walbranbc342d42019-02-05 16:56:02 +000081 static void __attribute__((constructor)) \
82 HFTEST_SET_UP_CONSTRUCTOR(suite_name)(void) \
83 { \
84 hftest_register(HFTEST_SET_UP_STRUCT(suite_name)); \
85 } \
Andrew Scullf0551c82018-12-15 20:38:47 +000086 static void HFTEST_SET_UP_FN(suite_name)(void)
87
88#define HFTEST_TEAR_DOWN(suite_name) \
89 static void HFTEST_TEAR_DOWN_FN(suite_name)(void); \
90 const struct hftest_test __attribute__((used)) \
91 __attribute__((section(HFTEST_TEAR_DOWN_SECTION(suite_name)))) \
92 HFTEST_TEAR_DOWN_STRUCT(suite_name) = { \
93 .suite = #suite_name, \
94 .kind = HFTEST_KIND_TEAR_DOWN, \
95 .fn = HFTEST_TEAR_DOWN_FN(suite_name), \
96 }; \
Andrew Walbranbc342d42019-02-05 16:56:02 +000097 static void __attribute__((constructor)) \
98 HFTEST_TEAR_DOWN_CONSTRUCTOR(suite_name)(void) \
99 { \
100 hftest_register(HFTEST_TEAR_DOWN_STRUCT(suite_name)); \
101 } \
Andrew Scullf0551c82018-12-15 20:38:47 +0000102 static void HFTEST_TEAR_DOWN_FN(suite_name)(void)
103
Andrew Walbranbc342d42019-02-05 16:56:02 +0000104#define HFTEST_TEST(suite_name, test_name) \
105 static void HFTEST_TEST_FN(suite_name, test_name)(void); \
106 const struct hftest_test __attribute__((used)) __attribute__( \
107 (section(HFTEST_TEST_SECTION(suite_name, test_name)))) \
108 HFTEST_TEST_STRUCT(suite_name, test_name) = { \
109 .suite = #suite_name, \
110 .kind = HFTEST_KIND_TEST, \
111 .name = #test_name, \
112 .fn = HFTEST_TEST_FN(suite_name, test_name), \
113 }; \
114 static void __attribute__((constructor)) \
115 HFTEST_TEST_CONSTRUCTOR(suite_name, test_name)(void) \
116 { \
117 hftest_register(HFTEST_TEST_STRUCT(suite_name, test_name)); \
118 } \
Andrew Scullf0551c82018-12-15 20:38:47 +0000119 static void HFTEST_TEST_FN(suite_name, test_name)(void)
120
121#define HFTEST_TEST_SERVICE(service_name) \
122 static void HFTEST_SERVICE_FN(service_name)(void); \
123 const struct hftest_test __attribute__((used)) \
124 __attribute__((section(HFTEST_SERVICE_SECTION(service_name)))) \
125 HFTEST_SERVICE_STRUCT(service_name) = { \
126 .kind = HFTEST_KIND_SERVICE, \
127 .name = #service_name, \
128 .fn = HFTEST_SERVICE_FN(service_name), \
129 }; \
130 static void HFTEST_SERVICE_FN(service_name)(void)
131
132/* Context for tests. */
133struct hftest_context {
134 uint32_t failures;
135 noreturn void (*abort)(void);
136
Andrew Walbranafabe852019-03-20 17:55:11 +0000137 /* These are used in primary VMs. */
138 const struct fdt_header *fdt;
139
Andrew Scullf0551c82018-12-15 20:38:47 +0000140 /* These are used in services. */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000141 struct spci_message *send;
142 struct spci_message *recv;
Andrew Walbran2b87c702019-04-16 18:16:05 +0100143 size_t memory_size;
Andrew Scullf0551c82018-12-15 20:38:47 +0000144};
145
146struct hftest_context *hftest_get_context(void);
147
148/* A test case. */
149typedef void (*hftest_test_fn)(void);
150
151enum hftest_kind {
152 HFTEST_KIND_SET_UP = 0,
153 HFTEST_KIND_TEST = 1,
154 HFTEST_KIND_TEAR_DOWN = 2,
155 HFTEST_KIND_SERVICE = 3,
156};
157
158/**
159 * The .hftest section contains an array of this struct which describes the test
160 * functions contained in the image allowing the image to inspect the tests it
161 * contains.
162 */
163struct hftest_test {
164 const char *suite;
165 enum hftest_kind kind;
166 const char *name;
167 hftest_test_fn fn;
168};
169
170/*
171 * This union can store any of the primitive types supported by the assertion
172 * macros.
173 *
174 * It does not include pointers as comparison of pointers is not often needed
175 * and could be a mistake for string comparison. If pointer comparison is needed
176 * and explicit assertion such as ASSERT_PTR_EQ() would be more appropriate.
177 */
178union hftest_any {
179 bool b;
180 char c;
181 signed char sc;
182 unsigned char uc;
183 signed short ss;
184 unsigned short us;
185 signed int si;
186 unsigned int ui;
187 signed long int sli;
188 unsigned long int uli;
189 signed long long int slli;
190 unsigned long long int ulli;
191};
192
193/* _Generic formatting doesn't seem to be supported so doing this manually. */
194/* clang-format off */
195
196/* Select the union member to match the type of the expression. */
197#define hftest_any_get(any, x) \
198 _Generic((x), \
199 bool: (any).b, \
200 char: (any).c, \
201 signed char: (any).sc, \
202 unsigned char: (any).uc, \
203 signed short: (any).ss, \
204 unsigned short: (any).us, \
205 signed int: (any).si, \
206 unsigned int: (any).ui, \
207 signed long int: (any).sli, \
208 unsigned long int: (any).uli, \
209 signed long long int: (any).slli, \
210 unsigned long long int: (any).ulli)
211
212/*
213 * dlog format specifier for types. Note, these aren't the standard specifiers
214 * for the types.
215 */
216#define hftest_dlog_format(x) \
217 _Generic((x), \
218 bool: "%u", \
219 char: "%c", \
220 signed char: "%d", \
221 unsigned char: "%u", \
222 signed short: "%d", \
223 unsigned short: "%u", \
224 signed int: "%d", \
225 unsigned int: "%u", \
226 signed long int: "%d", \
227 unsigned long int: "%u", \
228 signed long long int: "%d", \
229 unsigned long long int: "%u")
230
231/* clang-format on */
232
233#define HFTEST_LOG_FAILURE() \
234 dlog(HFTEST_LOG_PREFIX "Failure: %s:%u\n", __FILE__, __LINE__);
235
236#define HFTEST_ASSERT_OP(lhs, rhs, op, fatal) \
237 do { \
238 union hftest_any lhs_value; \
239 union hftest_any rhs_value; \
240 hftest_any_get(lhs_value, lhs) = (lhs); \
241 hftest_any_get(rhs_value, rhs) = (rhs); \
242 if (!(hftest_any_get(lhs_value, lhs) \
243 op hftest_any_get(rhs_value, rhs))) { \
244 struct hftest_context *ctx = hftest_get_context(); \
245 ++ctx->failures; \
246 HFTEST_LOG_FAILURE(); \
247 dlog(HFTEST_LOG_PREFIX HFTEST_LOG_INDENT \
248 "%s %s %s (%s=", \
249 #lhs, #op, #rhs, #lhs); \
250 dlog(hftest_dlog_format(lhs), \
251 hftest_any_get(lhs_value, lhs)); \
252 dlog(", %s=", #rhs); \
253 dlog(hftest_dlog_format(rhs), \
254 hftest_any_get(rhs_value, rhs)); \
255 dlog(")\n"); \
256 if (fatal) { \
257 ctx->abort(); \
258 } \
259 } \
260 } while (0)
261
Andrew Walbran78a63b72019-03-18 17:28:22 +0000262#define HFTEST_FAIL(fatal, ...) \
263 do { \
264 struct hftest_context *ctx = hftest_get_context(); \
265 ++ctx->failures; \
266 HFTEST_LOG_FAILURE(); \
267 dlog(HFTEST_LOG_PREFIX HFTEST_LOG_INDENT __VA_ARGS__); \
268 dlog("\n"); \
269 if (fatal) { \
270 ctx->abort(); \
271 } \
Andrew Walbranb90daf12018-12-11 14:25:54 +0000272 } while (0)
273
Andrew Scullf0551c82018-12-15 20:38:47 +0000274/**
275 * Select the service to run in a service VM.
276 */
277#define HFTEST_SERVICE_SELECT(vm_id, service, send_buffer) \
278 do { \
279 struct hf_vcpu_run_return run_res; \
Andrew Scull4a867bc2019-04-08 10:15:11 +0100280 uint32_t msg_length = \
281 strnlen_s(service, SERVICE_NAME_MAX_LENGTH); \
Andrew Scullf0551c82018-12-15 20:38:47 +0000282 \
283 /* \
284 * Let the service configure its mailbox and wait for a \
285 * message. \
286 */ \
287 run_res = hf_vcpu_run(vm_id, 0); \
Andrew Scullb06d1752019-02-04 10:15:48 +0000288 ASSERT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE); \
289 ASSERT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); \
Andrew Scullf0551c82018-12-15 20:38:47 +0000290 \
291 /* Send the selected service to run and let it be handled. */ \
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100292 memcpy_s(send_buffer->payload, SPCI_MSG_PAYLOAD_MAX, service, \
293 msg_length); \
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000294 spci_message_init(send_buffer, msg_length, vm_id, \
295 hf_vm_get_id()); \
296 \
297 ASSERT_EQ(spci_msg_send(0), 0); \
Andrew Scullf0551c82018-12-15 20:38:47 +0000298 run_res = hf_vcpu_run(vm_id, 0); \
299 ASSERT_EQ(run_res.code, HF_VCPU_RUN_YIELD); \
300 } while (0)
301
302#define HFTEST_SERVICE_SEND_BUFFER() hftest_get_context()->send
303#define HFTEST_SERVICE_RECV_BUFFER() hftest_get_context()->recv
Andrew Walbran2b87c702019-04-16 18:16:05 +0100304#define HFTEST_SERVICE_MEMORY_SIZE() hftest_get_context()->memory_size
Andrew Walbranbc342d42019-02-05 16:56:02 +0000305
306void hftest_register(struct hftest_test test);