Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 1 | /* |
Kevin Peng | 67a8754 | 2019-01-28 09:53:58 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, Arm Limited. All rights reserved. |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | #include <stdint.h> |
| 8 | #include <stdbool.h> |
| 9 | |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 10 | #include "cmsis_os2.h" |
| 11 | |
| 12 | #include "tfm_api.h" |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 13 | #include "tfm_ns_lock.h" |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * \brief struct ns_lock_state type |
| 17 | */ |
| 18 | struct ns_lock_state |
| 19 | { |
| 20 | bool init; |
| 21 | osMutexId_t id; |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * \brief ns_lock status |
| 26 | */ |
| 27 | static struct ns_lock_state ns_lock = {.init=false, .id=NULL}; |
| 28 | |
| 29 | /** |
| 30 | * \brief Mutex properties, NS lock |
| 31 | */ |
| 32 | static const osMutexAttr_t ns_lock_attrib = { |
| 33 | .name = "ns_lock", |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame^] | 34 | .attr_bits = osMutexPrioInherit, |
| 35 | .cb_mem = NULL, |
| 36 | .cb_size = 0U |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /** |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 40 | * \brief NS world, NS lock based dispatcher |
| 41 | */ |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 42 | uint32_t tfm_ns_lock_dispatch(veneer_fn fn, |
| 43 | uint32_t arg0, uint32_t arg1, |
| 44 | uint32_t arg2, uint32_t arg3) |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 45 | { |
| 46 | uint32_t result; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 47 | |
| 48 | /* Check the NS lock has been initialized */ |
| 49 | if (ns_lock.init == false) { |
| 50 | return TFM_ERROR_GENERIC; |
| 51 | } |
| 52 | |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 53 | /* TFM request protected by NS lock */ |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame^] | 54 | if (osMutexAcquire(ns_lock.id,osWaitForever) != osOK) { |
| 55 | return TFM_ERROR_GENERIC; |
| 56 | } |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 57 | |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 58 | result = fn(arg0, arg1, arg2, arg3); |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 59 | |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame^] | 60 | if (osMutexRelease(ns_lock.id) != osOK) { |
| 61 | return TFM_ERROR_GENERIC; |
| 62 | } |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 63 | |
| 64 | return result; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * \brief NS world, Init NS lock |
| 69 | */ |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame^] | 70 | enum tfm_status_e tfm_ns_lock_init() |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 71 | { |
| 72 | if (ns_lock.init == false) { |
| 73 | ns_lock.id = osMutexNew(&ns_lock_attrib); |
| 74 | ns_lock.init = true; |
| 75 | return TFM_SUCCESS; |
| 76 | } |
| 77 | else { |
| 78 | return TFM_ERROR_GENERIC; |
| 79 | } |
| 80 | } |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 81 | |
| 82 | bool tfm_ns_lock_get_init_state() |
| 83 | { |
| 84 | return ns_lock.init; |
| 85 | } |
| 86 | |