Xinyu Zhang | 3ea91b9 | 2021-09-22 14:54:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * This is a simple implementaton for reference to assign NSID from NS side. |
| 10 | * NSIDs of specific threads are statically pre-assigned. |
| 11 | * Other threads use default NSID value -1. |
| 12 | * |
| 13 | * Developers can design the assignment according to RTOS and usage scenarios. |
| 14 | * The assignment can be static or dynamic. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include "tfm_nsid_manager.h" |
| 19 | #include "tfm_nsid_map_table.h" |
| 20 | |
| 21 | /* Translation table pair between OS threads and NSIDs */ |
| 22 | struct thread_test_nsid_pair { |
| 23 | const char *t_name; /* Task/Thread name */ |
| 24 | int32_t nsid; /* NSID */ |
| 25 | }; |
| 26 | |
| 27 | /* -1 is reserved for NSID as a default value */ |
| 28 | static const struct thread_test_nsid_pair test_ns_nsid_table[] = |
| 29 | { |
| 30 | {"Thread_A", -2}, |
| 31 | {"Thread_B", -3}, |
| 32 | {"Thread_C", -4}, |
| 33 | {"Thread_D", -5}, |
| 34 | {"seq_task", -6}, |
| 35 | {"mid_task", -7}, |
| 36 | {"pri_task", -8}, |
| 37 | #ifdef PSA_API_TEST_NS |
| 38 | {"psa_api_test", -9} |
| 39 | #endif |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * Workaround: strcmp func in string.h would come into a runtime error |
| 44 | * on AN521 with ARMCLANG compiler. |
| 45 | */ |
| 46 | static int str_cmp(const char* str_a, const char* str_b) |
| 47 | { |
| 48 | int result = 0; |
| 49 | uint32_t i = 0; |
| 50 | |
| 51 | while ((str_a[i] != '\0') || (str_b[i] != '\0')) { |
| 52 | if (str_a[i] != str_b[i]) { |
| 53 | result = 1; |
| 54 | break; |
| 55 | } |
| 56 | i++; |
| 57 | } |
| 58 | |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | int32_t nsid_mgr_get_thread_nsid(const char* t_name) |
| 63 | { |
| 64 | uint32_t i; |
| 65 | |
| 66 | if (t_name == NULL) { |
| 67 | return TFM_DEFAULT_NSID; |
| 68 | } |
| 69 | |
| 70 | for (i = 0; i < ARRAY_SIZE(test_ns_nsid_table); i++) { |
| 71 | if (str_cmp(test_ns_nsid_table[i].t_name, t_name) == 0) { |
| 72 | return test_ns_nsid_table[i].nsid; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* Thread name not specified in the table, return default NSID */ |
| 77 | return TFM_DEFAULT_NSID; |
| 78 | } |