aboutsummaryrefslogtreecommitdiff
path: root/app/tfm_integ_test.c
blob: 19b079d6b4cce8719053a630842db714e8258b55 (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

#include "tfm_api.h"
#include "cmsis_os2.h"
#include "tfm_nspm_api.h"

#include "tfm_integ_test.h"
#include "test/framework/test_framework_integ_test.h"
#ifdef TFM_PSA_API
#include "psa_manifest/sid.h"
#endif

#ifdef TEST_FRAMEWORK_S
#include \
  "test/test_services/tfm_secure_client_service/tfm_secure_client_service_api.h"
#endif

#ifdef CORE_TEST_INTERACTIVE
#include "test/test_services/tfm_core_test/core_test_defs.h"
#include "tfm_veneers.h"

#define TRY_SFN(fn, ...) \
    do { \
        enum tfm_status_e res = (enum tfm_status_e) fn(__VA_ARGS__); \
        switch(res) { \
            case TFM_SUCCESS: \
                LOG_MSG("Secure call to " #fn "(" #__VA_ARGS__") successful!");\
                break; \
            case TFM_ERROR_SECURE_DOMAIN_LOCKED: \
                LOG_MSG("Secure call to " #fn "(" #__VA_ARGS__") failed, " \
                                                           "S domain locked!");\
                break; \
            default: \
                LOG_MSG("Secure call to " #fn "(" #__VA_ARGS__") failed, " \
                                                                   "generic!");\
        } \
    } while(0)

#ifdef TFM_PSA_API
static psa_status_t psa_test_common(uint32_t sid, uint32_t version,
                                    const psa_invec *in_vecs, size_t in_len,
                                    psa_outvec *out_vecs, size_t out_len)
{
    psa_handle_t handle;
    psa_status_t status;

    handle = psa_connect(sid, version);
    if (handle <= 0) {
        return CORE_TEST_ERRNO_INVALID_PARAMETER;
    }

    status = psa_call(handle, PSA_IPC_CALL, in_vecs, in_len, out_vecs, out_len);
    if (status < 0) {
        status = CORE_TEST_ERRNO_UNEXPECTED_CORE_BEHAVIOUR;
    }

    psa_close(handle);
    return status;
}
#endif /* TFM_PSA_API */

/**
 * \brief secure_decrement_ns_lock_1
 *
 */
void secure_decrement_ns_lock_1(void)
{
#ifndef TFM_PSA_API
    uint32_t testcase_id = CORE_TEST_ID_BLOCK;
    psa_invec in_vec = {&testcase_id, sizeof(testcase_id)};

    TRY_SFN(tfm_spm_core_test_sfn_veneer, &in_vec, 1, NULL, 0);
#else
    psa_status_t err;

    err = psa_test_common(SPM_CORE_TEST_BLOCK_SID,
                          SPM_CORE_TEST_BLOCK_VERSION,
                          NULL, 0, NULL, 0);
    if (err != PSA_SUCCESS) {
        LOG_MSG("Secure call to sfn block failed, generic!");
    }
#endif
}

/**
 * \brief secure_decrement_ns_lock_2
 *
 */
void secure_decrement_ns_lock_2(void)
{
#ifndef TFM_PSA_API
    uint32_t testcase_id = CORE_TEST_ID_BLOCK;
    psa_invec in_vec = {&testcase_id, sizeof(testcase_id)};

    TRY_SFN(tfm_spm_core_test_sfn_veneer, &in_vec, 1, NULL, 0);
#else
    psa_status_t err;

    err = psa_test_common(SPM_CORE_TEST_BLOCK_SID,
                          SPM_CORE_TEST_BLOCK_VERSION,
                          NULL, 0, NULL, 0);
    if (err != PSA_SUCCESS) {
        LOG_MSG("Secure call to sfn block failed, generic!");
    }
#endif
}
/**
 * \brief Test definition for the RTX - TFM integration tests
 *        scenarios
 */
enum test_type {
    TEST_TYPE_1 = 1, /*!< Sequential test: single task using the NS lock to
                          access TFM */
    TEST_TYPE_2,     /*!< Priority test: high priority tries to preempt TFM,
                          gets delayed */
    TEST_TYPE_3,     /*!< Priority inversion: classical scenario with high
                          priority task waiting on lower priority task
                          undefinitely if NS lock is configured without priority
                          inheritance */
    TEST_TYPE_4,     /*!< non-NS lock: like sequential, but doesn't use any NS
                          lock mechanism */
    TEST_TYPE_5,     /*!< non-NS lock, core locked: high priority tries to
                          overcome the NS lock but finds TFM core locked by
                          lower priority task and fails */
    TEST_TYPE_6      /*!< Like TEST_TYPE_2, but the high priority task has now a
                          timeout to acquire the NS lock. The timeout will
                          expire only if TFM Core is built with the
                          de-prioritization disabled */
};

static const osThreadAttr_t tattr_seq = {
    .name = "seq_task",
    .stack_size = 1024U,
    .attr_bits = osThreadJoinable,
    .tz_module = 1,
};
static const osThreadAttr_t tattr_mid = {
    .name = "mid_task",
    .stack_size = 512U,
    .attr_bits = osThreadJoinable,
    .tz_module = 0,
    .priority = osPriorityAboveNormal
};
static const osThreadAttr_t tattr_pri = {
    .name = "pri_task",
    .stack_size = 1024U,
    .attr_bits = osThreadJoinable,
    .tz_module = 1,
    .priority = osPriorityHigh
};

/**
 * \brief Mutex id, NS lock
 */
static osMutexId_t  mutex_id;

/**
 * \brief Mutex properties, NS lock
 */
static const osMutexAttr_t mattr_ns_lock = {
    .name = "ns_lock",
    //.attr_bits = osMutexPrioInherit
};

/**
 * \brief TFM NS lock options
 *
 * \details Options used while acquiring the NS lock
 */
struct tfm_ns_lock_options
{
    bool use_ns_lock;
    uint32_t timeout;
};

/**
 * \brief tfm_service_request
 *
 * \details This function is used to request a TFM service in thread mode.
 *          Optionally uses the NS lock and specifies a timeout for obtaining
 *          the NS lock.
 */
static void tfm_service_request(void(*fn)(void),
                                struct tfm_ns_lock_options *ns_lock_options_p)
{
    osStatus_t result;

    char buffer[80];

#define LOG_MSG_THREAD(MSG_THREAD) \
  do { \
      sprintf(buffer,"%s [%s]", MSG_THREAD, osThreadGetName(osThreadGetId())); \
      LOG_MSG(buffer); \
  } \
  while(0)

    LOG_MSG_THREAD("Trying to acquire the TFM core from NS");

    if (ns_lock_options_p->use_ns_lock) {
        result = osMutexAcquire(mutex_id,0);
        if (result == osOK) {
            LOG_MSG_THREAD("NS Lock: acquired");
            /* Add a delay here just to let the pri_task try to
             * acquire the NS lock before seq_task enters secure world
             */
            if (!strcmp(osThreadGetName(osThreadGetId()),"seq_task")) {
                osDelay(100U);
            }
            fn();
            LOG_MSG_THREAD("NS Lock: releasing...");
            osMutexRelease(mutex_id);
        } else {

            if (ns_lock_options_p->timeout == osWaitForever) {
                LOG_MSG_THREAD("Failed to acquire NS lock, keep waiting");
            } else {
                LOG_MSG_THREAD("Failed to acquire NS lock, wait with timeout");
            }

            result = osMutexAcquire(mutex_id,ns_lock_options_p->timeout);
            if (result == osOK) {
                LOG_MSG_THREAD("NS Lock: acquired");
                fn();
                LOG_MSG_THREAD("NS Lock: releasing...");
                osMutexRelease(mutex_id);
            } else if (result == osErrorTimeout) {
                LOG_MSG_THREAD("NS Lock: failed to acquire, timeout expired");
            } else {
                LOG_MSG_THREAD("NS Lock: unexpected failure trying to acquire");
            }
        }
    } else {
        /* Add a delay here to let the seq_task (which always uses the NS lock)
         * enter secure world before the pri_task (which can try to overcome the
         * NS lock in test scenario 5)
         */
        if (!strcmp(osThreadGetName(osThreadGetId()),"pri_task")) {
            osDelay(100U);
        }
        fn();
    }
}

/**
 * \brief Non-blocking test thread
 *
 */
__attribute__((noreturn))
static void mid_task(void *argument)
{
    osThreadId_t thread_id_pri;
    osThreadState_t thread_pri_state;
    uint32_t idx;

#ifdef TFM_NS_CLIENT_IDENTIFICATION
    tfm_nspm_register_client_id();
#endif /* TFM_NS_CLIENT_IDENTIFICATION */

    thread_id_pri = *((osThreadId_t *)argument);

    /* go to sleep */
    osDelay(100U);

    thread_pri_state = osThreadGetState(thread_id_pri);

    if (thread_pri_state == osThreadBlocked) {
        LOG_MSG("Running [mid_task] while [pri_task] is blocked");
    } else if (thread_pri_state == osThreadTerminated) {
        LOG_MSG("Running [mid_task] while [pri_task] is terminated");
    } else {
        LOG_MSG("Running [mid_task]");
    }

    /* Do non TFM related, non blocking, operations */
    for (idx=0; idx<0x3ffffff; idx++) {
    }

    LOG_MSG("Exiting [mid_task]");

    osThreadExit();
}

/**
 * \brief Priority test thread
 *
 */
__attribute__((noreturn))
static void pri_task(void *argument)
{
#ifdef TFM_NS_CLIENT_IDENTIFICATION
    tfm_nspm_register_client_id();
#endif /* TFM_NS_CLIENT_IDENTIFICATION */

    /* go to sleep */
    osDelay(100U);

    /* After wake up, try to get hold of the NS lock */
    tfm_service_request(secure_decrement_ns_lock_2,
                            (struct tfm_ns_lock_options *)argument);

    osThreadExit();
}

/**
 * \brief Sequential test thread
 *
 */
__attribute__((noreturn))
static void seq_task(void *argument)
{
    osThreadId_t thread_id, thread_id_mid;
    enum test_type test_type;

    /* By default, use NS lock and wait forever if busy, i.e. until unblocked */
    struct tfm_ns_lock_options ns_lock_opt =
                                  {.use_ns_lock=true, .timeout=osWaitForever};
    struct tfm_ns_lock_options ns_lock_opt_pri =
                                  {.use_ns_lock=true, .timeout=osWaitForever};

#ifdef TFM_NS_CLIENT_IDENTIFICATION
    tfm_nspm_register_client_id();
#endif /* TFM_NS_CLIENT_IDENTIFICATION */

    test_type = *((enum test_type *)argument);

    if (test_type == TEST_TYPE_1) {
        LOG_MSG("Scenario 1 - Sequential");
    } else if (test_type == TEST_TYPE_2) {
        LOG_MSG("Scenario 2 - Priority");
        thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
    } else if (test_type == TEST_TYPE_3) {
        LOG_MSG("Scenario 3 - Priority inversion");
        thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
        thread_id_mid = osThreadNew(mid_task, &thread_id, &tattr_mid);
    } else if (test_type == TEST_TYPE_4) {
        LOG_MSG("Scenario 4 - non-NS lock");
        ns_lock_opt.use_ns_lock = false;
    } else if (test_type == TEST_TYPE_5) {
        LOG_MSG("Scenario 5 - non-NS lock, core locked");
        ns_lock_opt_pri.use_ns_lock = false;
        thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
    } else if (test_type == TEST_TYPE_6) {
        LOG_MSG("Scenario 6 - Core prioritization effects on NS world");
        ns_lock_opt_pri.timeout = 0x10000; /* timed_wait for NS lock */
        thread_id = osThreadNew(pri_task, &ns_lock_opt_pri, &tattr_pri);
    } else {
        LOG_MSG("Scenario not supported");
        osThreadExit();
    }

    /* Try to acquire the NS lock */
    tfm_service_request(secure_decrement_ns_lock_1, &ns_lock_opt);

    if (test_type == TEST_TYPE_1) {
        LOG_MSG("Scenario 1 - test finished\n");
    } else if (test_type == TEST_TYPE_2) {
        osThreadJoin(thread_id);
        LOG_MSG("Scenario 2 - test finished\n");
    } else if (test_type == TEST_TYPE_3) {
        osThreadJoin(thread_id);
        osThreadJoin(thread_id_mid);
        LOG_MSG("Scenario 3 - test finished\n");
    } else if (test_type == TEST_TYPE_4) {
        LOG_MSG("Scenario 4 - test finished\n");
    } else if (test_type == TEST_TYPE_5) {
        osThreadJoin(thread_id);
        LOG_MSG("Scenario 5 - test finished\n");
    } else if (test_type == TEST_TYPE_6) {
        osThreadJoin(thread_id);
        LOG_MSG("Scenario 6 - test finished\n");
    }

    osThreadExit();
}

/**
 * \brief Execute the interactive tets cases
 *
 */
void execute_ns_interactive_tests(void)
{
    uint8_t idx;

    osThreadId_t thread_id;

    /* Test type list */
    enum test_type test_type[] = {TEST_TYPE_1, TEST_TYPE_2, TEST_TYPE_3,
                                  TEST_TYPE_4, TEST_TYPE_5, TEST_TYPE_6};

    /* Create the NS lock -- shared among testing scenarios */
    mutex_id = osMutexNew(&mattr_ns_lock);

    /* Loop in the test list */
    for (idx=0; idx<sizeof(test_type); idx++) {
        /* Spawn the main thread */
        thread_id = osThreadNew(seq_task, &test_type[idx], &tattr_seq);

        /* Wait for it to finish before moving to the next scenario */
        osThreadJoin(thread_id);
    }
}
#endif /* CORE_TEST_INTERACTIVE */

#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
/**
 * \brief Services test thread
 *
 */
__attribute__((noreturn))
void test_app(void *argument)
{
    UNUSED_VARIABLE(argument);

#ifdef TEST_FRAMEWORK_S
    /* FIXME: The non-secure audit log test currently relies on the fact that
     * the audit log secure test is run first. However the Non-secure tests
     * represent simpler and more common test cases which would make more sense
     * to be run first. Therefore if this dependency is removed the execution
     * order of these test classes should be reversed. */
    tfm_secure_client_run_tests();
#endif
#ifdef TEST_FRAMEWORK_NS
    tfm_non_secure_client_run_tests();
#endif
    /* End of test */
    for (;;) {
    }
}
#endif /* TEST_FRAMEWORK_NS OR TEST_FRAMEWORK_S */