blob: 07077372c6369e9f2df70f6022296fef65309195 [file] [log] [blame]
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +00001/*
Antonio de Angelis04bf6592018-02-26 11:57:36 +00002 * Copyright (c) 2017 - 2018, Arm Limited. All rights reserved.
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <stdio.h>
9#include <stdint.h>
10#include <string.h>
11#include <stdbool.h>
12
13#include "cmsis.h"
14#include "tfm_api.h"
15#include "cmsis_os2.h"
Mate Toth-Pal3956a8a2018-08-03 17:18:47 +020016#include "tfm_nspm_api.h"
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000017
18#include "tfm_integ_test.h"
Tamas Banc2074a72018-08-14 10:23:12 +010019#include "test/framework/test_framework_integ_test.h"
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000020
Ben Davis6d7256b2018-04-18 14:16:53 +010021#ifdef TEST_FRAMEWORK_S
22#include \
23 "test/test_services/tfm_secure_client_service/tfm_secure_client_service_api.h"
24#endif
25
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000026#ifdef CORE_TEST_INTERACTIVE
27#include "test/test_services/tfm_core_test/core_test_defs.h"
28#include "test/test_services/tfm_core_test/tfm_ss_core_test_veneers.h"
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000029
30#define TRY_SFN(fn, ...) \
31 do { \
32 enum tfm_status_e res = (enum tfm_status_e) fn(__VA_ARGS__); \
33 switch(res) { \
34 case TFM_SUCCESS: \
Antonio de Angelis04bf6592018-02-26 11:57:36 +000035 LOG_MSG("Secure call to " #fn "(" #__VA_ARGS__") successful!");\
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000036 break; \
Mate Toth-Pal7de74b52018-02-23 15:46:47 +010037 case TFM_PARTITION_PENDED: \
Antonio de Angelis04bf6592018-02-26 11:57:36 +000038 LOG_MSG("Secure call to " #fn "(" #__VA_ARGS__") pended!"); \
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000039 break; \
Mate Toth-Pal7de74b52018-02-23 15:46:47 +010040 case TFM_ERROR_PARTITION_ALREADY_PENDED: \
Antonio de Angelis04bf6592018-02-26 11:57:36 +000041 LOG_MSG("Secure call to " #fn "(" #__VA_ARGS__") failed, " \
42 "already pended!");\
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000043 break; \
44 case TFM_ERROR_SECURE_DOMAIN_LOCKED: \
Antonio de Angelis04bf6592018-02-26 11:57:36 +000045 LOG_MSG("Secure call to " #fn "(" #__VA_ARGS__") failed, " \
46 "S domain locked!");\
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000047 break; \
48 case TFM_ERROR_NS_THREAD_MODE_CALL: \
Antonio de Angelis04bf6592018-02-26 11:57:36 +000049 LOG_MSG("Secure call to " #fn "(" #__VA_ARGS__") failed, " \
50 "NS thread mode!");\
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000051 break; \
52 default: \
Antonio de Angelis04bf6592018-02-26 11:57:36 +000053 LOG_MSG("Secure call to " #fn "(" #__VA_ARGS__") failed, " \
54 "generic!");\
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000055 } \
56 } while(0)
57/**
Mate Toth-Pal1379e152018-07-30 17:38:29 +020058 * \brief secure_decrement_ns_lock_1
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000059 *
60 */
Mate Toth-Pal1379e152018-07-30 17:38:29 +020061void secure_decrement_ns_lock_1(void)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000062{
63 TRY_SFN(tfm_core_test_sfn, CORE_TEST_ID_BLOCK, 0x1, 0x1, 0x1);
64}
65
66/**
Mate Toth-Pal1379e152018-07-30 17:38:29 +020067 * \brief secure_decrement_ns_lock_2
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000068 *
69 */
Mate Toth-Pal1379e152018-07-30 17:38:29 +020070void secure_decrement_ns_lock_2(void)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000071{
72 TRY_SFN(tfm_core_test_sfn, CORE_TEST_ID_BLOCK, 0x2, 0x2, 0x2);
73}
74/**
75 * \brief Test definition for the RTX - TFM integration tests
76 * scenarios
77 */
78enum test_type {
Antonio de Angelis04bf6592018-02-26 11:57:36 +000079 TEST_TYPE_1 = 1, /*!< Sequential test: single task using the NS lock to
80 access TFM */
81 TEST_TYPE_2, /*!< Priority test: high priority tries to preempt TFM,
82 gets delayed */
83 TEST_TYPE_3, /*!< Priority inversion: classical scenario with high
84 priority task waiting on lower priority task
85 undefinitely if NS lock is configured without priority
86 inheritance */
87 TEST_TYPE_4, /*!< non-NS lock: like sequential, but doesn't use any NS
88 lock mechanism */
89 TEST_TYPE_5, /*!< non-NS lock, core locked: high priority tries to
90 overcome the NS lock but finds TFM core locked by
91 lower priority task and fails */
Antonio de Angelis1ea2a132017-12-06 14:36:05 +000092 TEST_TYPE_6 /*!< Like TEST_TYPE_2, but the high priority task has now a
93 timeout to acquire the NS lock. The timeout will
94 expire only if TFM Core is built with the
Miklos Balintace4c3f2018-07-30 12:31:15 +020095 de-prioritization disabled */
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000096};
97
98static const osThreadAttr_t tattr_seq = {
99 .name = "seq_task",
100 .stack_size = 1024U,
101 .attr_bits = osThreadJoinable,
102 .tz_module = 1,
103};
104static const osThreadAttr_t tattr_mid = {
105 .name = "mid_task",
106 .stack_size = 512U,
107 .attr_bits = osThreadJoinable,
108 .tz_module = 0,
109 .priority = osPriorityAboveNormal
110};
111static const osThreadAttr_t tattr_pri = {
112 .name = "pri_task",
113 .stack_size = 1024U,
114 .attr_bits = osThreadJoinable,
115 .tz_module = 1,
116 .priority = osPriorityHigh
117};
118
119/**
120 * \brief Mutex id, NS lock
121 */
122static osMutexId_t mutex_id;
123
124/**
125 * \brief Mutex properties, NS lock
126 */
127static const osMutexAttr_t mattr_ns_lock = {
128 .name = "ns_lock",
129 //.attr_bits = osMutexPrioInherit
130};
131
132/**
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000133 * \brief TFM NS lock options
134 *
135 * \details Options used while acquiring the NS lock
136 */
137struct tfm_ns_lock_options
138{
139 bool use_ns_lock;
140 uint32_t timeout;
141};
142
143/**
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000144 * \brief tfm_service_request
145 *
Mate Toth-Pal1379e152018-07-30 17:38:29 +0200146 * \details This function is used to request a TFM service in thread mode.
147 * Optionally uses the NS lock and specifies a timeout for obtaining
148 * the NS lock.
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000149 */
Mate Toth-Pal1379e152018-07-30 17:38:29 +0200150static void tfm_service_request(void(*fn)(void),
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000151 struct tfm_ns_lock_options *ns_lock_options_p)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000152{
153 osStatus_t result;
154
155 char buffer[80];
156
157#define LOG_MSG_THREAD(MSG_THREAD) \
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000158 do { \
159 sprintf(buffer,"%s [%s]", MSG_THREAD, osThreadGetName(osThreadGetId())); \
160 LOG_MSG(buffer); \
161 } \
162 while(0)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000163
164 LOG_MSG_THREAD("Trying to acquire the TFM core from NS");
165
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000166 if (ns_lock_options_p->use_ns_lock) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000167 result = osMutexAcquire(mutex_id,0);
168 if (result == osOK) {
169 LOG_MSG_THREAD("NS Lock: acquired");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000170 /* Add a delay here just to let the pri_task try to
171 * acquire the NS lock before seq_task enters secure world
172 */
173 if (!strcmp(osThreadGetName(osThreadGetId()),"seq_task")) {
174 osDelay(100U);
175 }
Mate Toth-Pal1379e152018-07-30 17:38:29 +0200176 fn();
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000177 LOG_MSG_THREAD("NS Lock: releasing...");
178 osMutexRelease(mutex_id);
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000179 } else {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000180
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000181 if (ns_lock_options_p->timeout == osWaitForever) {
182 LOG_MSG_THREAD("Failed to acquire NS lock, keep waiting");
183 } else {
184 LOG_MSG_THREAD("Failed to acquire NS lock, wait with timeout");
185 }
186
187 result = osMutexAcquire(mutex_id,ns_lock_options_p->timeout);
188 if (result == osOK) {
189 LOG_MSG_THREAD("NS Lock: acquired");
Mate Toth-Pal1379e152018-07-30 17:38:29 +0200190 fn();
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000191 LOG_MSG_THREAD("NS Lock: releasing...");
192 osMutexRelease(mutex_id);
193 } else if (result == osErrorTimeout) {
194 LOG_MSG_THREAD("NS Lock: failed to acquire, timeout expired");
195 } else {
196 LOG_MSG_THREAD("NS Lock: unexpected failure trying to acquire");
197 }
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000198 }
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000199 } else {
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000200 /* Add a delay here to let the seq_task (which always uses the NS lock)
201 * enter secure world before the pri_task (which can try to overcome the
202 * NS lock in test scenario 5)
203 */
204 if (!strcmp(osThreadGetName(osThreadGetId()),"pri_task")) {
205 osDelay(100U);
206 }
Mate Toth-Pal1379e152018-07-30 17:38:29 +0200207 fn();
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000208 }
209}
210
211/**
212 * \brief Non-blocking test thread
213 *
214 */
215__attribute__((noreturn))
216static void mid_task(void *argument)
217{
218 osThreadId_t thread_id_pri;
219 osThreadState_t thread_pri_state;
220 uint32_t idx;
221
Mate Toth-Pal3956a8a2018-08-03 17:18:47 +0200222 tfm_nspm_register_client_id();
223
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000224 thread_id_pri = *((osThreadId_t *)argument);
225
226 /* go to sleep */
227 osDelay(100U);
228
229 thread_pri_state = osThreadGetState(thread_id_pri);
230
231 if (thread_pri_state == osThreadBlocked) {
232 LOG_MSG("Running [mid_task] while [pri_task] is blocked");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000233 } else if (thread_pri_state == osThreadTerminated) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000234 LOG_MSG("Running [mid_task] while [pri_task] is terminated");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000235 } else {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000236 LOG_MSG("Running [mid_task]");
237 }
238
239 /* Do non TFM related, non blocking, operations */
240 for (idx=0; idx<0x3ffffff; idx++) {
241 }
242
243 LOG_MSG("Exiting [mid_task]");
244
245 osThreadExit();
246}
247
248/**
249 * \brief Priority test thread
250 *
251 */
252__attribute__((noreturn))
253static void pri_task(void *argument)
254{
Mate Toth-Pal3956a8a2018-08-03 17:18:47 +0200255 tfm_nspm_register_client_id();
256
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000257 /* go to sleep */
258 osDelay(100U);
259
260 /* After wake up, try to get hold of the NS lock */
Mate Toth-Pal1379e152018-07-30 17:38:29 +0200261 tfm_service_request(secure_decrement_ns_lock_2,
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000262 (struct tfm_ns_lock_options *)argument);
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000263
264 osThreadExit();
265}
266
267/**
268 * \brief Sequential test thread
269 *
270 */
271__attribute__((noreturn))
272static void seq_task(void *argument)
273{
274 osThreadId_t thread_id, thread_id_mid;
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000275 enum test_type test_type;
276
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000277 /* By default, use NS lock and wait forever if busy, i.e. until unblocked */
278 struct tfm_ns_lock_options ns_lock_opt =
279 {.use_ns_lock=true, .timeout=osWaitForever};
280 struct tfm_ns_lock_options ns_lock_opt_pri =
281 {.use_ns_lock=true, .timeout=osWaitForever};
282
Mate Toth-Pal3956a8a2018-08-03 17:18:47 +0200283 tfm_nspm_register_client_id();
284
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000285 test_type = *((enum test_type *)argument);
286
287 if (test_type == TEST_TYPE_1) {
288 LOG_MSG("Scenario 1 - Sequential");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000289 } else if (test_type == TEST_TYPE_2) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000290 LOG_MSG("Scenario 2 - Priority");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000291 thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000292 } else if (test_type == TEST_TYPE_3) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000293 LOG_MSG("Scenario 3 - Priority inversion");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000294 thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000295 thread_id_mid = osThreadNew(mid_task, &thread_id, &tattr_mid);
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000296 } else if (test_type == TEST_TYPE_4) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000297 LOG_MSG("Scenario 4 - non-NS lock");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000298 ns_lock_opt.use_ns_lock = false;
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000299 } else if (test_type == TEST_TYPE_5) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000300 LOG_MSG("Scenario 5 - non-NS lock, core locked");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000301 ns_lock_opt_pri.use_ns_lock = false;
302 thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
303 } else if (test_type == TEST_TYPE_6) {
304 LOG_MSG("Scenario 6 - Core prioritization effects on NS world");
305 ns_lock_opt_pri.timeout = 0x10000; /* timed_wait for NS lock */
306 thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000307 } else {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000308 LOG_MSG("Scenario not supported");
309 osThreadExit();
310 }
311
312 /* Try to acquire the NS lock */
Mate Toth-Pal1379e152018-07-30 17:38:29 +0200313 tfm_service_request(secure_decrement_ns_lock_1, &ns_lock_opt);
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000314
315 if (test_type == TEST_TYPE_1) {
316 LOG_MSG("Scenario 1 - test finished\n");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000317 } else if (test_type == TEST_TYPE_2) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000318 osThreadJoin(thread_id);
319 LOG_MSG("Scenario 2 - test finished\n");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000320 } else if (test_type == TEST_TYPE_3) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000321 osThreadJoin(thread_id);
322 osThreadJoin(thread_id_mid);
323 LOG_MSG("Scenario 3 - test finished\n");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000324 } else if (test_type == TEST_TYPE_4) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000325 LOG_MSG("Scenario 4 - test finished\n");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000326 } else if (test_type == TEST_TYPE_5) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000327 osThreadJoin(thread_id);
328 LOG_MSG("Scenario 5 - test finished\n");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000329 } else if (test_type == TEST_TYPE_6) {
330 osThreadJoin(thread_id);
331 LOG_MSG("Scenario 6 - test finished\n");
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000332 }
333
334 osThreadExit();
335}
336
337/**
338 * \brief Execute the interactive tets cases
339 *
340 */
341void execute_ns_interactive_tests(void)
342{
343 uint8_t idx;
344
345 osThreadId_t thread_id;
346
347 /* Test type list */
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000348 enum test_type test_type[] = {TEST_TYPE_1, TEST_TYPE_2, TEST_TYPE_3,
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000349 TEST_TYPE_4, TEST_TYPE_5, TEST_TYPE_6};
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000350
351 /* Create the NS lock -- shared among testing scenarios */
352 mutex_id = osMutexNew(&mattr_ns_lock);
353
354 /* Loop in the test list */
355 for (idx=0; idx<sizeof(test_type); idx++) {
356 /* Spawn the main thread */
357 thread_id = osThreadNew(seq_task, &test_type[idx], &tattr_seq);
358
359 /* Wait for it to finish before moving to the next scenario */
360 osThreadJoin(thread_id);
361 }
362}
363#endif /* CORE_TEST_INTERACTIVE */
364
Ben Davis6d7256b2018-04-18 14:16:53 +0100365#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000366/**
367 * \brief Services test thread
368 *
369 */
370__attribute__((noreturn))
371void test_app(void *argument)
372{
373 UNUSED_VARIABLE(argument);
Ben Davis6d7256b2018-04-18 14:16:53 +0100374
375#ifdef TEST_FRAMEWORK_S
376 /* FIXME: The non-secure audit log test currently relies on the fact that
377 * the audit log secure test is run first. However the Non-secure tests
378 * represent simpler and more common test cases which would make more sense
379 * to be run first. Therefore if this dependency is removed the execution
380 * order of these test classes should be reversed. */
381 tfm_secure_client_run_tests();
382#endif
383#ifdef TEST_FRAMEWORK_NS
384 tfm_non_secure_client_run_tests();
385#endif
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000386 /* End of test */
387 for (;;) {
388 }
389}
Ben Davis6d7256b2018-04-18 14:16:53 +0100390#endif /* TEST_FRAMEWORK_NS OR TEST_FRAMEWORK_S */