Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | #include <stdint.h> |
| 8 | #include <stdbool.h> |
| 9 | |
| 10 | #include "cmsis.h" |
| 11 | #include "cmsis_os2.h" |
| 12 | |
| 13 | #include "tfm_api.h" |
| 14 | #include "tfm_ns_svc.h" |
| 15 | |
| 16 | /** |
| 17 | * \brief struct ns_lock_state type |
| 18 | */ |
| 19 | struct ns_lock_state |
| 20 | { |
| 21 | bool init; |
| 22 | osMutexId_t id; |
| 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * \brief ns_lock status |
| 27 | */ |
| 28 | static struct ns_lock_state ns_lock = {.init=false, .id=NULL}; |
| 29 | |
| 30 | /** |
| 31 | * \brief Mutex properties, NS lock |
| 32 | */ |
| 33 | static const osMutexAttr_t ns_lock_attrib = { |
| 34 | .name = "ns_lock", |
| 35 | .attr_bits = osMutexPrioInherit |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * \def NUM_SVC_DISPATCHERS |
| 40 | * |
| 41 | */ |
| 42 | #define NUM_SVC_DISPATCHERS (6) |
| 43 | |
| 44 | /** |
| 45 | * \brief Naked functions associated to each |
| 46 | * SVC needed |
| 47 | */ |
| 48 | __attribute__((naked)) |
| 49 | static uint32_t tfm_svc_dispatch_SST_GET_HANDLE(uint32_t arg0, uint32_t arg1, |
| 50 | uint32_t arg2, uint32_t arg3) |
| 51 | { |
| 52 | SVC(SVC_TFM_SST_GET_HANDLE); |
| 53 | __ASM("BX LR"); |
| 54 | } |
| 55 | |
| 56 | __attribute__((naked)) |
| 57 | static uint32_t tfm_svc_dispatch_SST_CREATE(uint32_t arg0, uint32_t arg1, |
| 58 | uint32_t arg2, uint32_t arg3) |
| 59 | { |
| 60 | SVC(SVC_TFM_SST_CREATE); |
| 61 | __ASM("BX LR"); |
| 62 | } |
| 63 | |
| 64 | __attribute__((naked)) |
| 65 | static uint32_t tfm_svc_dispatch_SST_GET_ATTRIBUTES(uint32_t arg0,uint32_t arg1, |
| 66 | uint32_t arg2,uint32_t arg3) |
| 67 | { |
| 68 | SVC(SVC_TFM_SST_GET_ATTRIBUTES); |
| 69 | __ASM("BX LR"); |
| 70 | } |
| 71 | |
| 72 | __attribute__((naked)) |
| 73 | static uint32_t tfm_svc_dispatch_SST_READ(uint32_t arg0, uint32_t arg1, |
| 74 | uint32_t arg2, uint32_t arg3) |
| 75 | { |
| 76 | SVC(SVC_TFM_SST_READ); |
| 77 | __ASM("BX LR"); |
| 78 | } |
| 79 | |
| 80 | __attribute__((naked)) |
| 81 | static uint32_t tfm_svc_dispatch_SST_WRITE(uint32_t arg0, uint32_t arg1, |
| 82 | uint32_t arg2, uint32_t arg3) |
| 83 | { |
| 84 | SVC(SVC_TFM_SST_WRITE); |
| 85 | __ASM("BX LR"); |
| 86 | } |
| 87 | |
| 88 | __attribute__((naked)) |
| 89 | static uint32_t tfm_svc_dispatch_SST_DELETE(uint32_t arg0, uint32_t arg1, |
| 90 | uint32_t arg2, uint32_t arg3) |
| 91 | { |
| 92 | SVC(SVC_TFM_SST_DELETE); |
| 93 | __ASM("BX LR"); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * \brief Array with function pointers to the |
| 98 | * naked functions. Entry 0 is treated |
| 99 | * as invalid |
| 100 | */ |
| 101 | static void *tfm_svc_dispatch_functions[NUM_SVC_DISPATCHERS+1] = { |
| 102 | (void *) NULL, /* SVC_INVALID */ |
| 103 | (void *) tfm_svc_dispatch_SST_GET_HANDLE, |
| 104 | (void *) tfm_svc_dispatch_SST_CREATE, |
| 105 | (void *) tfm_svc_dispatch_SST_GET_ATTRIBUTES, |
| 106 | (void *) tfm_svc_dispatch_SST_READ, |
| 107 | (void *) tfm_svc_dispatch_SST_WRITE, |
| 108 | (void *) tfm_svc_dispatch_SST_DELETE |
| 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * \brief NS world, NS lock based dispatcher |
| 113 | */ |
| 114 | uint32_t tfm_ns_lock_svc_dispatch(enum tfm_svc_num svc_num, |
| 115 | uint32_t arg0, |
| 116 | uint32_t arg1, |
| 117 | uint32_t arg2, |
| 118 | uint32_t arg3) |
| 119 | { |
| 120 | uint32_t result; |
| 121 | uint32_t (*tfm_svc_dispatch_function_p)(uint32_t, uint32_t, |
| 122 | uint32_t, uint32_t); |
| 123 | |
| 124 | /* Check the NS lock has been initialized */ |
| 125 | if (ns_lock.init == false) { |
| 126 | return TFM_ERROR_GENERIC; |
| 127 | } |
| 128 | |
| 129 | /* Validate the SVC number requested */ |
| 130 | if ((svc_num > SVC_INVALID) && (svc_num < (NUM_SVC_DISPATCHERS+1))) { |
| 131 | tfm_svc_dispatch_function_p = tfm_svc_dispatch_functions[svc_num]; |
| 132 | |
| 133 | /* TFM request protected by NS lock */ |
| 134 | osMutexAcquire(ns_lock.id,osWaitForever); |
| 135 | result = (*tfm_svc_dispatch_function_p)(arg0, arg1, arg2, arg3); |
| 136 | osMutexRelease(ns_lock.id); |
| 137 | |
| 138 | return result; |
| 139 | } |
| 140 | else { |
| 141 | return TFM_ERROR_GENERIC; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * \brief NS world, Init NS lock |
| 147 | */ |
| 148 | uint32_t tfm_ns_lock_init() |
| 149 | { |
| 150 | if (ns_lock.init == false) { |
| 151 | ns_lock.id = osMutexNew(&ns_lock_attrib); |
| 152 | ns_lock.init = true; |
| 153 | return TFM_SUCCESS; |
| 154 | } |
| 155 | else { |
| 156 | return TFM_ERROR_GENERIC; |
| 157 | } |
| 158 | } |