blob: 37421fb2b4247f9b0eb07731061a77db71ec9a53 [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"
16
17#include "tfm_integ_test.h"
18#include "test/framework/integ_test.h"
19
Ben Davis6d7256b2018-04-18 14:16:53 +010020#ifdef TEST_FRAMEWORK_S
21#include \
22 "test/test_services/tfm_secure_client_service/tfm_secure_client_service_api.h"
23#endif
24
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000025#ifdef CORE_TEST_INTERACTIVE
26#include "test/test_services/tfm_core_test/core_test_defs.h"
27#include "test/test_services/tfm_core_test/tfm_ss_core_test_veneers.h"
28#include "tfm_ns_svc.h"
29
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/**
58 * \brief SVC_SECURE_DECREMENT_NS_LOCK_1
59 *
60 */
61void svc_secure_decrement_ns_lock_1(void)
62{
63 TRY_SFN(tfm_core_test_sfn, CORE_TEST_ID_BLOCK, 0x1, 0x1, 0x1);
64}
65
66/**
67 * \brief SVC_SECURE_DECREMENT_NS_LOCK_2
68 *
69 */
70void svc_secure_decrement_ns_lock_2(void)
71{
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/**
133 * \brief SVC dispatcher
134 */
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000135__attribute__((always_inline)) __STATIC_INLINE
136void svc_dispatch(enum tfm_svc_num svc_num)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000137{
138 switch (svc_num) {
139 case SVC_SECURE_DECREMENT_NS_LOCK_1:
140 SVC(SVC_SECURE_DECREMENT_NS_LOCK_1);
141 break;
142 case SVC_SECURE_DECREMENT_NS_LOCK_2:
143 SVC(SVC_SECURE_DECREMENT_NS_LOCK_2);
144 break;
145 default:
146 break;
147 }
148}
149
150/**
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000151 * \brief TFM NS lock options
152 *
153 * \details Options used while acquiring the NS lock
154 */
155struct tfm_ns_lock_options
156{
157 bool use_ns_lock;
158 uint32_t timeout;
159};
160
161/**
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000162 * \brief tfm_service_request
163 *
164 * \details This function is used to request a TFM
165 * service in handler mode, using SVC.
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000166 * Optionally uses the NS lock and specifies
167 * a timeout for obtaining the NS lock
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000168 */
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000169static void tfm_service_request(enum tfm_svc_num svc_num,
170 struct tfm_ns_lock_options *ns_lock_options_p)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000171{
172 osStatus_t result;
173
174 char buffer[80];
175
176#define LOG_MSG_THREAD(MSG_THREAD) \
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000177 do { \
178 sprintf(buffer,"%s [%s]", MSG_THREAD, osThreadGetName(osThreadGetId())); \
179 LOG_MSG(buffer); \
180 } \
181 while(0)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000182
183 LOG_MSG_THREAD("Trying to acquire the TFM core from NS");
184
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000185 if (ns_lock_options_p->use_ns_lock) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000186 result = osMutexAcquire(mutex_id,0);
187 if (result == osOK) {
188 LOG_MSG_THREAD("NS Lock: acquired");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000189 /* Add a delay here just to let the pri_task try to
190 * acquire the NS lock before seq_task enters secure world
191 */
192 if (!strcmp(osThreadGetName(osThreadGetId()),"seq_task")) {
193 osDelay(100U);
194 }
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000195 svc_dispatch(svc_num);
196 LOG_MSG_THREAD("NS Lock: releasing...");
197 osMutexRelease(mutex_id);
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000198 } else {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000199
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000200 if (ns_lock_options_p->timeout == osWaitForever) {
201 LOG_MSG_THREAD("Failed to acquire NS lock, keep waiting");
202 } else {
203 LOG_MSG_THREAD("Failed to acquire NS lock, wait with timeout");
204 }
205
206 result = osMutexAcquire(mutex_id,ns_lock_options_p->timeout);
207 if (result == osOK) {
208 LOG_MSG_THREAD("NS Lock: acquired");
209 svc_dispatch(svc_num);
210 LOG_MSG_THREAD("NS Lock: releasing...");
211 osMutexRelease(mutex_id);
212 } else if (result == osErrorTimeout) {
213 LOG_MSG_THREAD("NS Lock: failed to acquire, timeout expired");
214 } else {
215 LOG_MSG_THREAD("NS Lock: unexpected failure trying to acquire");
216 }
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000217 }
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000218 } else {
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000219 /* Add a delay here to let the seq_task (which always uses the NS lock)
220 * enter secure world before the pri_task (which can try to overcome the
221 * NS lock in test scenario 5)
222 */
223 if (!strcmp(osThreadGetName(osThreadGetId()),"pri_task")) {
224 osDelay(100U);
225 }
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000226 svc_dispatch(svc_num);
227 }
228}
229
230/**
231 * \brief Non-blocking test thread
232 *
233 */
234__attribute__((noreturn))
235static void mid_task(void *argument)
236{
237 osThreadId_t thread_id_pri;
238 osThreadState_t thread_pri_state;
239 uint32_t idx;
240
241 thread_id_pri = *((osThreadId_t *)argument);
242
243 /* go to sleep */
244 osDelay(100U);
245
246 thread_pri_state = osThreadGetState(thread_id_pri);
247
248 if (thread_pri_state == osThreadBlocked) {
249 LOG_MSG("Running [mid_task] while [pri_task] is blocked");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000250 } else if (thread_pri_state == osThreadTerminated) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000251 LOG_MSG("Running [mid_task] while [pri_task] is terminated");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000252 } else {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000253 LOG_MSG("Running [mid_task]");
254 }
255
256 /* Do non TFM related, non blocking, operations */
257 for (idx=0; idx<0x3ffffff; idx++) {
258 }
259
260 LOG_MSG("Exiting [mid_task]");
261
262 osThreadExit();
263}
264
265/**
266 * \brief Priority test thread
267 *
268 */
269__attribute__((noreturn))
270static void pri_task(void *argument)
271{
272 /* go to sleep */
273 osDelay(100U);
274
275 /* After wake up, try to get hold of the NS lock */
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000276 tfm_service_request(SVC_SECURE_DECREMENT_NS_LOCK_2,
277 (struct tfm_ns_lock_options *)argument);
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000278
279 osThreadExit();
280}
281
282/**
283 * \brief Sequential test thread
284 *
285 */
286__attribute__((noreturn))
287static void seq_task(void *argument)
288{
289 osThreadId_t thread_id, thread_id_mid;
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000290 enum test_type test_type;
291
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000292 /* By default, use NS lock and wait forever if busy, i.e. until unblocked */
293 struct tfm_ns_lock_options ns_lock_opt =
294 {.use_ns_lock=true, .timeout=osWaitForever};
295 struct tfm_ns_lock_options ns_lock_opt_pri =
296 {.use_ns_lock=true, .timeout=osWaitForever};
297
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000298 test_type = *((enum test_type *)argument);
299
300 if (test_type == TEST_TYPE_1) {
301 LOG_MSG("Scenario 1 - Sequential");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000302 } else if (test_type == TEST_TYPE_2) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000303 LOG_MSG("Scenario 2 - Priority");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000304 thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000305 } else if (test_type == TEST_TYPE_3) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000306 LOG_MSG("Scenario 3 - Priority inversion");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000307 thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000308 thread_id_mid = osThreadNew(mid_task, &thread_id, &tattr_mid);
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000309 } else if (test_type == TEST_TYPE_4) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000310 LOG_MSG("Scenario 4 - non-NS lock");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000311 ns_lock_opt.use_ns_lock = false;
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000312 } else if (test_type == TEST_TYPE_5) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000313 LOG_MSG("Scenario 5 - non-NS lock, core locked");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000314 ns_lock_opt_pri.use_ns_lock = false;
315 thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
316 } else if (test_type == TEST_TYPE_6) {
317 LOG_MSG("Scenario 6 - Core prioritization effects on NS world");
318 ns_lock_opt_pri.timeout = 0x10000; /* timed_wait for NS lock */
319 thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000320 } else {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000321 LOG_MSG("Scenario not supported");
322 osThreadExit();
323 }
324
325 /* Try to acquire the NS lock */
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000326 tfm_service_request(SVC_SECURE_DECREMENT_NS_LOCK_1, &ns_lock_opt);
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000327
328 if (test_type == TEST_TYPE_1) {
329 LOG_MSG("Scenario 1 - test finished\n");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000330 } else if (test_type == TEST_TYPE_2) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000331 osThreadJoin(thread_id);
332 LOG_MSG("Scenario 2 - test finished\n");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000333 } else if (test_type == TEST_TYPE_3) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000334 osThreadJoin(thread_id);
335 osThreadJoin(thread_id_mid);
336 LOG_MSG("Scenario 3 - test finished\n");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000337 } else if (test_type == TEST_TYPE_4) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000338 LOG_MSG("Scenario 4 - test finished\n");
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000339 } else if (test_type == TEST_TYPE_5) {
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000340 osThreadJoin(thread_id);
341 LOG_MSG("Scenario 5 - test finished\n");
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000342 } else if (test_type == TEST_TYPE_6) {
343 osThreadJoin(thread_id);
344 LOG_MSG("Scenario 6 - test finished\n");
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000345 }
346
347 osThreadExit();
348}
349
350/**
351 * \brief Execute the interactive tets cases
352 *
353 */
354void execute_ns_interactive_tests(void)
355{
356 uint8_t idx;
357
358 osThreadId_t thread_id;
359
360 /* Test type list */
Antonio de Angelis04bf6592018-02-26 11:57:36 +0000361 enum test_type test_type[] = {TEST_TYPE_1, TEST_TYPE_2, TEST_TYPE_3,
Antonio de Angelis1ea2a132017-12-06 14:36:05 +0000362 TEST_TYPE_4, TEST_TYPE_5, TEST_TYPE_6};
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000363
364 /* Create the NS lock -- shared among testing scenarios */
365 mutex_id = osMutexNew(&mattr_ns_lock);
366
367 /* Loop in the test list */
368 for (idx=0; idx<sizeof(test_type); idx++) {
369 /* Spawn the main thread */
370 thread_id = osThreadNew(seq_task, &test_type[idx], &tattr_seq);
371
372 /* Wait for it to finish before moving to the next scenario */
373 osThreadJoin(thread_id);
374 }
375}
376#endif /* CORE_TEST_INTERACTIVE */
377
Ben Davis6d7256b2018-04-18 14:16:53 +0100378#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000379/**
380 * \brief Services test thread
381 *
382 */
383__attribute__((noreturn))
384void test_app(void *argument)
385{
386 UNUSED_VARIABLE(argument);
Ben Davis6d7256b2018-04-18 14:16:53 +0100387
388#ifdef TEST_FRAMEWORK_S
389 /* FIXME: The non-secure audit log test currently relies on the fact that
390 * the audit log secure test is run first. However the Non-secure tests
391 * represent simpler and more common test cases which would make more sense
392 * to be run first. Therefore if this dependency is removed the execution
393 * order of these test classes should be reversed. */
394 tfm_secure_client_run_tests();
395#endif
396#ifdef TEST_FRAMEWORK_NS
397 tfm_non_secure_client_run_tests();
398#endif
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000399 /* End of test */
400 for (;;) {
401 }
402}
Ben Davis6d7256b2018-04-18 14:16:53 +0100403#endif /* TEST_FRAMEWORK_NS OR TEST_FRAMEWORK_S */