summaryrefslogtreecommitdiff
path: root/tftf/tests/runtime_services/generic/generic_smc.c
blob: 6d057a6a87a7081b71f90e571d9c2dc8a050fcad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <psci.h>
#include <smccc.h>
#include <std_svc.h>
#include <stdio.h>
#include <string.h>
#include <tftf_lib.h>
#include <trusted_os.h>
#include <tsp.h>
#include <utils_def.h>

/* An invalid SMC function number. */
#define INVALID_FN 0x666

/* PSCI version returned by TF-A. */
static const uint32_t psci_version = PSCI_VERSION(PSCI_MAJOR_VER,
						  PSCI_MINOR_VER);

/* UUID of the standard service in TF-A. */
static const smc_ret_values std_svc_uuid = {
	0x108d905b, 0x47e8f863, 0xfbc02dae, 0xe2f64156
};

/*
 * Build an SMC function ID given its type (fast/yielding), calling convention,
 * owning entity number and function number.
 */
static inline uint32_t make_smc_fid(unsigned int type, unsigned int cc,
				    unsigned int oen, unsigned int func_num)
{
	return (type << FUNCID_TYPE_SHIFT) | (cc << FUNCID_CC_SHIFT)
		| (oen << FUNCID_OEN_SHIFT) | (func_num << FUNCID_NUM_SHIFT);
}

/* Exit the test if the specified condition holds true. */
#define FAIL_IF(_cond)						\
	do {							\
		if ((_cond)) {					\
			return TEST_RESULT_FAIL;		\
		}						\
	} while (0)

/*
 * Send an SMC with the specified arguments.
 * Check that the values it returns match the expected ones. If not, write an
 * error message in the test report.
 */
static bool smc_check_eq(const smc_args *args, const smc_ret_values *expect)
{
	smc_ret_values ret = tftf_smc(args);

	if (memcmp(&ret, expect, sizeof(smc_ret_values)) == 0) {
		return true;
	} else {
		tftf_testcase_printf(
			"Got {0x%lx,0x%lx,0x%lx,0x%lx,0x%lx,0x%lx,0x%lx,0x%lx}, \
			expected {0x%lx,0x%lx,0x%lx,0x%lx,0x%lx,0x%lx,0x%lx,0x%lx}.\n",
			ret.ret0, ret.ret1, ret.ret2, ret.ret3,
			ret.ret4, ret.ret5, ret.ret6, ret.ret7,
			expect->ret0, expect->ret1, expect->ret2, expect->ret3,
			expect->ret4, expect->ret5, expect->ret6, expect->ret7);
		return false;
	}
}

/*
 * Send an SMC with the specified arguments.
 * Check that the values it returns match the expected ones. The do_check[]
 * array indicates which ones should be checked and provides some flexibility
 * to ignore some of them. Also the allow_zeros[] array lets the values to be
 * zeroes. allow_zeros[] is only evaluated if do_check[] is true for the given
 * value.
 * The two common solutions for preventing data leak from the TEE is to either
 * preserve the register values or zero them out. Having an expected value and
 * also allowing zeroes in this function comes handy in this previous case.
 * If the values do not match, write an error message in the test report.
 */
static bool smc_check_match(const smc_args *args, const smc_ret_values *expect,
			    const bool do_check[8], const bool allow_zeros[8])
{
	smc_ret_values ret = tftf_smc(args);

#define CHK_RET(ret, expect, allow_zeros)				\
	((ret) != (expect) && !((allow_zeros) && (ret) == 0))

	if ((do_check[0] && CHK_RET(ret.ret0, expect->ret0, allow_zeros[0])) ||
	    (do_check[1] && CHK_RET(ret.ret1, expect->ret1, allow_zeros[1])) ||
	    (do_check[2] && CHK_RET(ret.ret2, expect->ret2, allow_zeros[2])) ||
	    (do_check[3] && CHK_RET(ret.ret3, expect->ret3, allow_zeros[3])) ||
	    (do_check[4] && CHK_RET(ret.ret4, expect->ret4, allow_zeros[4])) ||
	    (do_check[5] && CHK_RET(ret.ret5, expect->ret5, allow_zeros[5])) ||
	    (do_check[6] && CHK_RET(ret.ret6, expect->ret6, allow_zeros[6])) ||
	    (do_check[7] && CHK_RET(ret.ret7, expect->ret7, allow_zeros[7]))) {

#undef CHK_RET
		/*
		 * Build an error message where unchecked SMC return values are
		 * displayed as '*'.
		 */
		char expect_str[8][28];
#define BUILD_STR(_buf, _buf_size, _do_check, _allow_zero, _expect)	\
		do {							\
			if (_do_check) {				\
				if (_allow_zero) {			\
					snprintf(_buf, _buf_size,	\
						"0x%lx or zero",	\
						_expect);		\
				} else {				\
					snprintf(_buf, _buf_size,	\
						"0x%lx", _expect);	\
				}					\
			} else {					\
				_buf[0] = '*';				\
				_buf[1] = '\0';				\
			}						\
		} while (0)
		BUILD_STR(expect_str[0], sizeof(expect_str[0]),
			do_check[0], allow_zeros[0], expect->ret0);
		BUILD_STR(expect_str[1], sizeof(expect_str[1]),
			do_check[1], allow_zeros[1], expect->ret1);
		BUILD_STR(expect_str[2], sizeof(expect_str[2]),
			do_check[2], allow_zeros[2], expect->ret2);
		BUILD_STR(expect_str[3], sizeof(expect_str[3]),
			do_check[3], allow_zeros[3], expect->ret3);
		BUILD_STR(expect_str[4], sizeof(expect_str[4]),
			do_check[4], allow_zeros[4], expect->ret4);
		BUILD_STR(expect_str[5], sizeof(expect_str[5]),
			do_check[5], allow_zeros[5], expect->ret5);
		BUILD_STR(expect_str[6], sizeof(expect_str[6]),
			do_check[6], allow_zeros[6], expect->ret6);
		BUILD_STR(expect_str[7], sizeof(expect_str[7]),
			do_check[7], allow_zeros[7], expect->ret7);
#undef BUILD_STR
		tftf_testcase_printf(
		"Got {0x%lx,0x%lx,0x%lx,0x%lx,0x%lx,0x%lx,0x%lx,0x%lx}, \
			expected {%s,%s,%s,%s,%s,%s,%s,%s}.\n",
			ret.ret0, ret.ret1, ret.ret2, ret.ret3,
			ret.ret4, ret.ret5, ret.ret6, ret.ret7,
			expect_str[0], expect_str[1], expect_str[2], expect_str[3],
			expect_str[4], expect_str[5], expect_str[6], expect_str[7]);

		return false;
	} else {
		return true;
	}
}

/* Exercise SMC32 calling convention with fast SMC calls. */
test_result_t smc32_fast(void)
{
	/* Valid Fast SMC32 using all 4 return values. */
	const smc_args args1 = { .fid = SMC_STD_SVC_UID };
	FAIL_IF(!smc_check_eq(&args1, &std_svc_uuid));

	/* Invalid Fast SMC32. */
	const smc_args args2 = {
		make_smc_fid(SMC_TYPE_FAST, SMC_32, OEN_ARM_START, INVALID_FN),
		0x11111111, 0x22222222, 0x33333333, 0x44444444,
		0x55555555, 0x66666666, 0x77777777 };
	const smc_ret_values ret2
		= { SMC_UNKNOWN, 0x11111111, 0x22222222, 0x33333333, 0x44444444,
		0x55555555, 0x66666666, 0x77777777 };
	FAIL_IF(!smc_check_eq(&args2, &ret2));

	/* Valid Fast SMC32 using 1 return value. */
	const smc_args args3
		= { SMC_PSCI_VERSION, 0x44444444, 0x55555555, 0x66666666,
		0x77777777, 0x88888888, 0x99999999, 0xaaaaaaaa };
	const smc_ret_values ret3
		= { psci_version, 0x44444444, 0x55555555, 0x66666666,
		0x77777777, 0x88888888, 0x99999999, 0xaaaaaaaa };
	FAIL_IF(!smc_check_eq(&args3, &ret3));

	return TEST_RESULT_SUCCESS;
}

/* Exercise SMC64 calling convention with yielding SMC calls. */
test_result_t smc64_yielding(void)
{
	/* Valid Fast SMC32 using all 4 return values. */
	const smc_args args1 = { .fid = SMC_STD_SVC_UID };
	FAIL_IF(!smc_check_eq(&args1, &std_svc_uuid));

	/* Invalid function number, SMC64 Yielding. */
	const smc_args args2 = {
		make_smc_fid(SMC_TYPE_STD, SMC_64, OEN_ARM_START, INVALID_FN),
		0x11111111, 0x22222222, 0x33333333, 0x44444444,
		0x55555555, 0x66666666, 0x77777777 };
	const smc_ret_values ret2
		= { SMC_UNKNOWN, 0x11111111, 0x22222222, 0x33333333, 0x44444444,
		0x55555555, 0x66666666, 0x77777777 };
	FAIL_IF(!smc_check_eq(&args2, &ret2));

	/*
	 * Valid[1] yielding SMC64 using 1 return value.
	 *
	 * [1] Valid from the point of view of the generic SMC handler if the
	 * TSPd is present. In this case, the SMC request gets passed to the
	 * TSPd handler code. The fact that it then gets rejected by the TSPd is
	 * irrelevant here, as we are not trying to test the TSPd nor the TSP.
	 *
	 * In other cases (i.e. AArch64 BL31 with no TSPd support or AArch32
	 * SP_MIN) this test should still fail in the same way, although it
	 * doesn't exercise the same code path in TF-A.
	 */
	const smc_args args3 = {
		make_smc_fid(SMC_TYPE_STD, SMC_64, OEN_TOS_START, INVALID_FN),
		0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888,
		0x99999999, 0xaaaaaaaa };

	if (is_trusted_os_present(NULL)) {
		/*
		 * The Trusted OS is free to return any error code in x0 but it
		 * should at least preserve or fill by zeroes the values of
		 * x1-x3.
		 */
		const smc_ret_values ret3 = { 0, 0x44444444, 0x55555555, 0x66666666,
			0x77777777, 0x88888888, 0x99999999, 0xaaaaaaaa };
		const bool check[8] = { false, true, true, true, true, true, true, true };
		const bool allow_zeros[8] = { false, true, true, true,
						true, true, true, true };

		FAIL_IF(!smc_check_match(&args3, &ret3, check, allow_zeros));
	} else {
		const smc_ret_values ret3
			= { SMC_UNKNOWN, 0x44444444, 0x55555555, 0x66666666,
			0x77777777, 0x88888888, 0x99999999, 0xaaaaaaaa };
		FAIL_IF(!smc_check_eq(&args3, &ret3));
	}

	return TEST_RESULT_SUCCESS;
}

#ifndef __aarch64__
static test_result_t smc64_fast_caller32(void)
{
	/* Valid Fast SMC32 using all 4 return values. */
	smc_args args1 = { .fid = SMC_STD_SVC_UID };
	FAIL_IF(!smc_check_eq(&args1, &std_svc_uuid));

	/* Invalid SMC function number, Fast SMC64. */
	const smc_args args2 = {
		make_smc_fid(SMC_TYPE_FAST, SMC_64, OEN_ARM_START, INVALID_FN),
		0x11111111, 0x22222222, 0x33333333, 0x44444444,
		0x55555555, 0x66666666, 0x77777777 };
	const smc_ret_values ret2
		= { SMC_UNKNOWN, 0x11111111, 0x22222222, 0x33333333,
		0x44444444, 0x55555555, 0x66666666, 0x77777777 };
	FAIL_IF(!smc_check_eq(&args2, &ret2));

	/*
	 * Valid SMC function number, Fast SMC64. However, 32-bit callers are
	 * forbidden to use the SMC64 calling convention.
	 */
	const smc_args args3 = { SMC_PSCI_AFFINITY_INFO_AARCH64,
		0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888,
		0x99999999, 0xaaaaaaaa };
	const smc_ret_values ret3
		= { SMC_UNKNOWN, 0x44444444, 0x55555555, 0x66666666,
		0x77777777, 0x88888888, 0x99999999, 0xaaaaaaaa };
	FAIL_IF(!smc_check_eq(&args3, &ret3));

	return TEST_RESULT_SUCCESS;
}
#else
static test_result_t smc64_fast_caller64(void)
{
	/* Valid Fast SMC32 using all 4 return values. */
	smc_args args1 = { .fid = SMC_STD_SVC_UID };
	FAIL_IF(!smc_check_eq(&args1, &std_svc_uuid));

	/* Invalid function number, Fast SMC64. */
	const smc_args args2 = {
		make_smc_fid(SMC_TYPE_FAST, SMC_64, OEN_ARM_START, INVALID_FN),
		0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555,
		0x66666666, 0x77777777 };
	const smc_ret_values ret2
		= { SMC_UNKNOWN, 0x11111111, 0x22222222, 0x33333333, 0x44444444,
		0x55555555, 0x66666666, 0x77777777 };
	FAIL_IF(!smc_check_eq(&args2, &ret2));

	/* Valid Fast SMC64 using 1 return value. */
	const smc_args args3 = { SMC_PSCI_AFFINITY_INFO_AARCH64,
			0x44444444, 0x55555555, 0x66666666, 0x77777777,
			0x88888888, 0x99999999, 0xaaaaaaaa };
	const smc_ret_values ret3
		= { PSCI_E_INVALID_PARAMS, 0x44444444, 0x55555555, 0x66666666,
		0x77777777, 0x88888888, 0x99999999, 0xaaaaaaaa };
	FAIL_IF(!smc_check_eq(&args3, &ret3));

	return TEST_RESULT_SUCCESS;
}
#endif /* !__aarch64__ */

/* Exercise SMC64 calling convention with fast SMC calls. */
test_result_t smc64_fast(void)
{
#ifndef __aarch64__
	return smc64_fast_caller32();
#else
	return smc64_fast_caller64();
#endif
}

/* Exercise SMC32 calling convention with yielding SMC calls. */
test_result_t smc32_yielding(void)
{
	/* Valid Fast SMC32 using all 4 return values. */
	const smc_args args1 = { .fid = SMC_STD_SVC_UID };
	FAIL_IF(!smc_check_eq(&args1, &std_svc_uuid));

	/* Invalid function number, SMC32 Yielding. */
	const smc_args args2 = {
		make_smc_fid(SMC_TYPE_STD, SMC_32, OEN_ARM_START, INVALID_FN),
		0x11111111, 0x22222222, 0x33333333, 0x44444444,
		0x55555555, 0x66666666, 0x77777777 };
	const smc_ret_values ret2
		= { SMC_UNKNOWN, 0x11111111, 0x22222222, 0x33333333, 0x44444444,
		0x55555555, 0x66666666, 0x77777777 };
	FAIL_IF(!smc_check_eq(&args2, &ret2));

	/*
	 * Valid[1] yielding SMC32 using 1 return value.
	 *
	 * [1] Valid from the point of view of the generic SMC handler if a
	 * secure payload dispatcher handling this SMC range is present. In this
	 * case, the SMC request gets passed to the dispatcher handler code. The
	 * fact that it then gets rejected by the dispatcher is irrelevant here,
	 * as we are not trying to test the dispatcher nor the secure payload.
	 *
	 * In BL31 has no SPD support, this test should still fail in the same
	 * way, although it doesn't exercise the same code path in TF-A.
	 */
	const smc_args args3 = {
		make_smc_fid(SMC_TYPE_STD, SMC_32, OEN_TOS_START, INVALID_FN),
		0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888,
		0x99999999, 0xaaaaaaaa };

	if (is_trusted_os_present(NULL)) {
		/*
		 * The Trusted OS is free to return any error code in x0 but it
		 * should at least preserve or fill by zeroes the values of
		 * x1-x3.
		 */
		const smc_ret_values ret3 = { 0, 0x44444444, 0x55555555, 0x66666666,
			0x77777777, 0x88888888, 0x99999999, 0xaaaaaaaa };
		const bool check[8] = { false, true, true, true, true, true, true, true };
		const bool allow_zeros[8] = { false, true, true, true,
						true, true, true, true };

		FAIL_IF(!smc_check_match(&args3, &ret3, check, allow_zeros));
	} else {
		const smc_ret_values ret3
			= { SMC_UNKNOWN, 0x44444444, 0x55555555, 0x66666666,
			0x77777777, 0x88888888, 0x99999999, 0xaaaaaaaa };
		FAIL_IF(!smc_check_eq(&args3, &ret3));
	}

	return TEST_RESULT_SUCCESS;
}