Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Gyorgy Szing | 40a7af0 | 2019-02-06 14:19:47 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, Arm Limited. All rights reserved. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef __SPM_API_H__ |
| 9 | #define __SPM_API_H__ |
| 10 | |
| 11 | /* This file contains the apis exported by the SPM to tfm core */ |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 12 | #include "tfm_api.h" |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 13 | #include "spm_partition_defs.h" |
David Hu | 49a28eb | 2019-08-14 18:18:15 +0800 | [diff] [blame] | 14 | #include "tfm_secure_api.h" |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 15 | #include <stdbool.h> |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 16 | #ifdef TFM_PSA_API |
| 17 | #include "tfm_list.h" |
| 18 | #include "tfm_wait.h" |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 19 | #include "tfm_message_queue.h" |
| 20 | #include "tfm_secure_api.h" |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 21 | #include "tfm_thread.h" |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 22 | #endif |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 23 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 24 | #define SPM_INVALID_PARTITION_IDX (~0U) |
| 25 | |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 26 | /* Privileged definitions for partition thread mode */ |
| 27 | #define TFM_PARTITION_PRIVILEGED_MODE 1 |
| 28 | #define TFM_PARTITION_UNPRIVILEGED_MODE 0 |
| 29 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 30 | enum spm_err_t { |
| 31 | SPM_ERR_OK = 0, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 32 | SPM_ERR_PARTITION_DB_NOT_INIT, |
| 33 | SPM_ERR_PARTITION_ALREADY_ACTIVE, |
| 34 | SPM_ERR_PARTITION_NOT_AVAILABLE, |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 35 | SPM_ERR_INVALID_PARAMETER, |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 36 | SPM_ERR_INVALID_CONFIG, |
| 37 | }; |
| 38 | |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 39 | #define SPM_PARTITION_STATE_UNINIT 0 |
| 40 | #define SPM_PARTITION_STATE_IDLE 1 |
| 41 | #define SPM_PARTITION_STATE_RUNNING 2 |
| 42 | #define SPM_PARTITION_STATE_HANDLING_IRQ 3 |
| 43 | #define SPM_PARTITION_STATE_SUSPENDED 4 |
| 44 | #define SPM_PARTITION_STATE_BLOCKED 5 |
| 45 | #define SPM_PARTITION_STATE_CLOSED 6 |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 46 | |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 47 | #define SPM_PART_FLAG_APP_ROT 0x01 |
| 48 | #define SPM_PART_FLAG_PSA_ROT 0x02 |
| 49 | #define SPM_PART_FLAG_IPC 0x04 |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 50 | |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 51 | #ifndef TFM_PSA_API |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 52 | /** |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 53 | * \brief Holds the iovec parameters that are passed to a service |
| 54 | * |
| 55 | * \note The size of the structure is (and have to be) multiple of 8 bytes |
| 56 | */ |
| 57 | struct iovec_args_t { |
| 58 | psa_invec in_vec[PSA_MAX_IOVEC]; /*!< Array of psa_invec objects */ |
| 59 | size_t in_len; /*!< Number psa_invec objects in in_vec |
| 60 | */ |
| 61 | psa_outvec out_vec[PSA_MAX_IOVEC]; /*!< Array of psa_outvec objects */ |
| 62 | size_t out_len; /*!< Number psa_outvec objects in out_vec |
| 63 | */ |
| 64 | }; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 65 | |
| 66 | /* The size of this struct must be multiple of 4 bytes as it is stacked to an |
| 67 | * uint32_t[] array |
| 68 | */ |
| 69 | struct interrupted_ctx_stack_frame_t { |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 70 | uint32_t partition_state; |
| 71 | }; |
| 72 | |
| 73 | /* The size of this struct must be multiple of 4 bytes as it is stacked to an |
| 74 | * uint32_t[] array |
| 75 | */ |
| 76 | struct handler_ctx_stack_frame_t { |
| 77 | uint32_t partition_state; |
| 78 | uint32_t caller_partition_idx; |
| 79 | }; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 80 | #endif /* !define(TFM_PSA_API) */ |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 81 | |
| 82 | /** |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 83 | * \brief Runtime context information of a partition |
| 84 | */ |
| 85 | struct spm_partition_runtime_data_t { |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 86 | #ifdef TFM_PSA_API |
| 87 | struct tfm_event_t signal_evnt; /* Event signal */ |
| 88 | uint32_t signals; /* Service signals had been triggered*/ |
| 89 | struct tfm_list_node_t service_list;/* Service list */ |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 90 | struct tfm_thrd_ctx sp_thrd; /* Thread context */ |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 91 | #else /* TFM_PSA_API */ |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 92 | uint32_t partition_state; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 93 | uint32_t caller_partition_idx; |
Mate Toth-Pal | 21a74c9 | 2018-04-13 14:05:41 +0200 | [diff] [blame] | 94 | int32_t caller_client_id; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 95 | uint32_t stack_ptr; |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 96 | uint32_t lr; |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 97 | struct iovec_args_t iovec_args; |
| 98 | psa_outvec *orig_outvec; |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 99 | uint32_t *ctx_stack_ptr; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 100 | #endif /* TFM_PSA_API */ |
| 101 | uint32_t signal_mask; /* |
| 102 | * Service signal mask passed by |
| 103 | * psa_wait() |
| 104 | */ |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 105 | }; |
| 106 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 107 | #ifdef TFM_PSA_API |
| 108 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 109 | #define TFM_VERSION_POLICY_RELAXED 0 |
| 110 | #define TFM_VERSION_POLICY_STRICT 1 |
| 111 | |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 112 | #define TFM_CONN_HANDLE_MAX_NUM 16 |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 113 | |
| 114 | /* RoT connection handle list */ |
| 115 | struct tfm_conn_handle_t { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 116 | void *rhandle; /* Reverse handle value */ |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 117 | struct tfm_msg_body_t internal_msg; /* Internal message for message queue */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 118 | struct tfm_spm_service_t *service; /* RoT service pointer */ |
| 119 | struct tfm_list_node_t list; /* list node */ |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /* Service database defined by manifest */ |
| 123 | struct tfm_spm_service_db_t { |
| 124 | char *name; /* Service name */ |
| 125 | uint32_t partition_id; /* Partition ID which service belong to */ |
| 126 | psa_signal_t signal; /* Service signal */ |
| 127 | uint32_t sid; /* Service identifier */ |
| 128 | bool non_secure_client; /* If can be called by non secure client */ |
| 129 | uint32_t minor_version; /* Minor version */ |
| 130 | uint32_t minor_policy; /* Minor version policy */ |
| 131 | }; |
| 132 | |
| 133 | /* RoT Service data */ |
| 134 | struct tfm_spm_service_t { |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 135 | const struct tfm_spm_service_db_t *service_db;/* Service database pointer */ |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 136 | struct spm_partition_desc_t *partition; /* |
| 137 | * Point to secure partition |
| 138 | * data |
| 139 | */ |
| 140 | struct tfm_list_node_t handle_list; /* Service handle list */ |
| 141 | struct tfm_msg_queue_t msg_queue; /* Message queue */ |
| 142 | struct tfm_list_node_t list; /* For list operation */ |
| 143 | }; |
| 144 | #endif /* ifdef(TFM_PSA_API) */ |
| 145 | |
| 146 | /*********************** common definitions ***********************/ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 147 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 148 | /** |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 149 | * \brief Returns the index of the partition with the given partition ID. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 150 | * |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 151 | * \param[in] partition_id Partition id |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 152 | * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 153 | * \return the partition idx if partition_id is valid, |
| 154 | * \ref SPM_INVALID_PARTITION_IDX othervise |
| 155 | */ |
| 156 | uint32_t get_partition_idx(uint32_t partition_id); |
| 157 | |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 158 | /** |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 159 | * \brief Get the id of the partition for its index from the db |
| 160 | * |
| 161 | * \param[in] partition_idx Partition index |
| 162 | * |
| 163 | * \return Partition ID for that partition |
| 164 | * |
| 165 | * \note This function doesn't check if partition_idx is valid. |
| 166 | */ |
| 167 | uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx); |
| 168 | |
| 169 | /** |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 170 | * \brief Get the flags associated with a partition |
| 171 | * |
| 172 | * \param[in] partition_idx Partition index |
| 173 | * |
| 174 | * \return Flags associated with the partition |
| 175 | * |
| 176 | * \note This function doesn't check if partition_idx is valid. |
| 177 | */ |
| 178 | uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx); |
| 179 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 180 | /** |
| 181 | * \brief Initialize partition database |
| 182 | * |
| 183 | * \return Error code \ref spm_err_t |
| 184 | */ |
| 185 | enum spm_err_t tfm_spm_db_init(void); |
| 186 | |
| 187 | /** |
| 188 | * \brief Change the privilege mode for partition thread mode. |
| 189 | * |
| 190 | * \param[in] privileged Privileged mode, |
| 191 | * \ref TFM_PARTITION_PRIVILEGED_MODE |
| 192 | * and \ref TFM_PARTITION_UNPRIVILEGED_MODE |
| 193 | * |
| 194 | * \note Barrier instructions are not called by this function, and if |
| 195 | * it is called in thread mode, it might be necessary to call |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 196 | * them after this function returns. |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 197 | */ |
| 198 | void tfm_spm_partition_change_privilege(uint32_t privileged); |
| 199 | |
| 200 | /*********************** library definitions ***********************/ |
| 201 | |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 202 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 203 | /** |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 204 | * \brief Save interrupted partition context on ctx stack |
| 205 | * |
| 206 | * \param[in] partition_idx Partition index |
| 207 | * |
| 208 | * \note This function doesn't check if partition_idx is valid. |
| 209 | * \note This function doesn't whether the ctx stack overflows. |
| 210 | */ |
| 211 | void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx); |
| 212 | |
| 213 | /** |
| 214 | * \brief Restores interrupted partition context on ctx stack |
| 215 | * |
| 216 | * \param[in] partition_idx Partition index |
| 217 | * |
| 218 | * \note This function doesn't check if partition_idx is valid. |
| 219 | * \note This function doesn't whether the ctx stack underflows. |
| 220 | */ |
| 221 | void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx); |
| 222 | |
| 223 | /** |
| 224 | * \brief Save handler partition context on ctx stack |
| 225 | * |
| 226 | * \param[in] partition_idx Partition index |
| 227 | * |
| 228 | * \note This function doesn't check if partition_idx is valid. |
| 229 | * \note This function doesn't whether the ctx stack overflows. |
| 230 | */ |
| 231 | void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx); |
| 232 | |
| 233 | /** |
| 234 | * \brief Restores handler partition context on ctx stack |
| 235 | * |
| 236 | * \param[in] partition_idx Partition index |
| 237 | * |
| 238 | * \note This function doesn't check if partition_idx is valid. |
| 239 | * \note This function doesn't whether the ctx stack underflows. |
| 240 | */ |
| 241 | void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx); |
| 242 | |
| 243 | /** |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 244 | * \brief Get the current runtime data of a partition |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 245 | * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 246 | * \param[in] partition_idx Partition index |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 247 | * |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 248 | * \return The runtime data of the specified partition |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 249 | * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 250 | * \note This function doesn't check if partition_idx is valid. |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 251 | */ |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 252 | const struct spm_partition_runtime_data_t * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 253 | tfm_spm_partition_get_runtime_data(uint32_t partition_idx); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 254 | |
| 255 | /** |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 256 | * \brief Returns the index of the partition that has running state |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 257 | * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 258 | * \return The index of the partition with the running state, if there is any |
| 259 | * set. 0 otherwise. |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 260 | */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 261 | uint32_t tfm_spm_partition_get_running_partition_idx(void); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 262 | |
| 263 | /** |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 264 | * \brief Save stack pointer and link register for partition in database |
| 265 | * |
| 266 | * \param[in] partition_idx Partition index |
| 267 | * \param[in] stack_ptr Stack pointer to be stored |
| 268 | * \param[in] lr Link register to be stored |
| 269 | * |
| 270 | * \note This function doesn't check if partition_idx is valid. |
| 271 | */ |
| 272 | void tfm_spm_partition_store_context(uint32_t partition_idx, |
| 273 | uint32_t stack_ptr, uint32_t lr); |
| 274 | |
| 275 | /** |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 276 | * \brief Set the current state of a partition |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 277 | * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 278 | * \param[in] partition_idx Partition index |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 279 | * \param[in] state The state to be set |
| 280 | * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 281 | * \note This function doesn't check if partition_idx is valid. |
Gyorgy Szing | 40a7af0 | 2019-02-06 14:19:47 +0100 | [diff] [blame] | 282 | * \note The state has to have the value set of \ref spm_part_state_t. |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 283 | */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 284 | void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 285 | |
| 286 | /** |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 287 | * \brief Set the caller partition index for a given partition |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 288 | * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 289 | * \param[in] partition_idx Partition index |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 290 | * \param[in] caller_partition_idx The index of the caller partition |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 291 | * |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 292 | * \note This function doesn't check if any of the partition_idxs are valid. |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 293 | */ |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 294 | void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx, |
| 295 | uint32_t caller_partition_idx); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 296 | |
| 297 | /** |
Mate Toth-Pal | 21a74c9 | 2018-04-13 14:05:41 +0200 | [diff] [blame] | 298 | * \brief Set the caller client ID for a given partition |
| 299 | * |
| 300 | * \param[in] partition_idx Partition index |
| 301 | * \param[in] caller_client_id The ID of the calling client |
| 302 | * |
| 303 | * \note This function doesn't check if any of the partition_idxs are valid. |
| 304 | */ |
| 305 | void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx, |
| 306 | int32_t caller_client_id); |
| 307 | |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 308 | |
| 309 | /** |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 310 | * \brief Set the iovec parameters for the partition |
| 311 | * |
| 312 | * \param[in] partition_idx Partition index |
| 313 | * \param[in] args The arguments of the secure function |
| 314 | * |
| 315 | * args is expected to be of type int32_t[4] where: |
| 316 | * args[0] is in_vec |
| 317 | * args[1] is in_len |
| 318 | * args[2] is out_vec |
| 319 | * args[3] is out_len |
| 320 | * |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 321 | * \return Error code \ref spm_err_t |
| 322 | * |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 323 | * \note This function doesn't check if partition_idx is valid. |
| 324 | * \note This function assumes that the iovecs that are passed in args are |
| 325 | * valid, and does no sanity check on them at all. |
| 326 | */ |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 327 | enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx, |
| 328 | const int32_t *args); |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 329 | |
| 330 | /** |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 331 | * \brief Execute partition init function |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 332 | * |
| 333 | * \return Error code \ref spm_err_t |
| 334 | */ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 335 | enum spm_err_t tfm_spm_partition_init(void); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 336 | |
| 337 | /** |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 338 | * \brief Clears the context info from the database for a partition. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 339 | * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 340 | * \param[in] partition_idx Partition index |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 341 | * |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 342 | * \note This function doesn't check if partition_idx is valid. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 343 | */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 344 | void tfm_spm_partition_cleanup_context(uint32_t partition_idx); |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 345 | |
| 346 | /** |
| 347 | * \brief Set the signal mask for a given partition |
| 348 | * |
| 349 | * \param[in] partition_idx Partition index |
| 350 | * \param[in] signal_mask The signal mask to be set for the partition |
| 351 | * |
| 352 | * \note This function doesn't check if any of the partition_idxs are valid. |
| 353 | */ |
| 354 | void tfm_spm_partition_set_signal_mask(uint32_t partition_idx, |
| 355 | uint32_t signal_mask); |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 356 | #endif /* !defined(TFM_PSA_API) */ |
| 357 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 358 | #ifdef TFM_PSA_API |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 359 | /*************************** IPC definitions **************************/ |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * \brief Get bottom of stack region for a partition |
| 363 | * |
| 364 | * \param[in] partition_idx Partition index |
| 365 | * |
| 366 | * \return Stack region bottom value |
| 367 | * |
| 368 | * \note This function doesn't check if partition_idx is valid. |
| 369 | */ |
| 370 | uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx); |
| 371 | |
| 372 | /** |
| 373 | * \brief Get top of stack region for a partition |
| 374 | * |
| 375 | * \param[in] partition_idx Partition index |
| 376 | * |
| 377 | * \return Stack region top value |
| 378 | * |
| 379 | * \note This function doesn't check if partition_idx is valid. |
| 380 | */ |
| 381 | uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx); |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 382 | |
| 383 | /** |
| 384 | * \brief Get the running partition ID. |
| 385 | * |
| 386 | * \return Returns the partition ID |
| 387 | */ |
| 388 | uint32_t tfm_spm_partition_get_running_partition_id(void); |
| 389 | |
| 390 | /** |
| 391 | * \brief Get the current partition mode. |
| 392 | * |
Summer Qin | 75f0d75 | 2019-08-27 14:38:20 +0800 | [diff] [blame] | 393 | * \param[in] partition_flags Flags of current partition |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 394 | * |
| 395 | * \retval TFM_PARTITION_PRIVILEGED_MODE Privileged mode |
| 396 | * \retval TFM_PARTITION_UNPRIVILEGED_MODE Unprivileged mode |
| 397 | */ |
Summer Qin | 75f0d75 | 2019-08-27 14:38:20 +0800 | [diff] [blame] | 398 | uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags); |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 399 | |
| 400 | /******************** Service handle management functions ********************/ |
| 401 | |
| 402 | /** |
| 403 | * \brief Create connection handle for client connect |
| 404 | * |
| 405 | * \param[in] service Target service context pointer |
| 406 | * |
| 407 | * \retval PSA_NULL_HANDLE Create failed \ref PSA_NULL_HANDLE |
| 408 | * \retval >0 Service handle created, \ref psa_handle_t |
| 409 | */ |
| 410 | psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service); |
| 411 | |
| 412 | /** |
| 413 | * \brief Free connection handle which not used anymore. |
| 414 | * |
| 415 | * \param[in] service Target service context pointer |
| 416 | * \param[in] conn_handle Connection handle created by |
| 417 | * tfm_spm_create_conn_handle(), \ref psa_handle_t |
| 418 | * |
| 419 | * \retval IPC_SUCCESS Success |
| 420 | * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input |
| 421 | * \retval "Does not return" Panic for not find service by handle |
| 422 | */ |
| 423 | int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service, |
| 424 | psa_handle_t conn_handle); |
| 425 | |
| 426 | /** |
| 427 | * \brief Set reverse handle value for connection. |
| 428 | * |
| 429 | * \param[in] service Target service context pointer |
| 430 | * \param[in] conn_handle Connection handle created by |
| 431 | * tfm_spm_create_conn_handle(), \ref psa_handle_t |
| 432 | * \param[in] rhandle rhandle need to save |
| 433 | * |
| 434 | * \retval IPC_SUCCESS Success |
| 435 | * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input |
| 436 | * \retval "Does not return" Panic for not find handle node |
| 437 | */ |
| 438 | int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service, |
| 439 | psa_handle_t conn_handle, |
| 440 | void *rhandle); |
| 441 | |
| 442 | /** |
| 443 | * \brief Get reverse handle value from connection hanlde. |
| 444 | * |
| 445 | * \param[in] service Target service context pointer |
| 446 | * \param[in] conn_handle Connection handle created by |
| 447 | * tfm_spm_create_conn_handle(), \ref psa_handle_t |
| 448 | * |
| 449 | * \retval void * Success |
| 450 | * \retval "Does not return" Panic for those: |
| 451 | * service pointer are NULL |
| 452 | * hanlde is \ref PSA_NULL_HANDLE |
| 453 | * handle node does not be found |
| 454 | */ |
| 455 | void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service, |
| 456 | psa_handle_t conn_handle); |
| 457 | |
| 458 | /******************** Partition management functions *************************/ |
| 459 | |
| 460 | /** |
| 461 | * \brief Get current running partition context. |
| 462 | * |
| 463 | * \retval NULL Failed |
| 464 | * \retval "Not NULL" Return the parttion context pointer |
| 465 | * \ref spm_partition_desc_t structures |
| 466 | */ |
| 467 | struct spm_partition_desc_t *tfm_spm_get_running_partition(void); |
| 468 | |
| 469 | /** |
| 470 | * \brief Get the service context by signal. |
| 471 | * |
| 472 | * \param[in] partition Partition context pointer |
| 473 | * \ref spm_partition_desc_t structures |
| 474 | * \param[in] signal Signal associated with inputs to the Secure |
| 475 | * Partition, \ref psa_signal_t |
| 476 | * |
| 477 | * \retval NULL Failed |
| 478 | * \retval "Not NULL" Target service context pointer, |
| 479 | * \ref tfm_spm_service_t structures |
| 480 | */ |
| 481 | struct tfm_spm_service_t * |
| 482 | tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition, |
| 483 | psa_signal_t signal); |
| 484 | |
| 485 | /** |
| 486 | * \brief Get the service context by service ID. |
| 487 | * |
| 488 | * \param[in] sid RoT Service identity |
| 489 | * |
| 490 | * \retval NULL Failed |
| 491 | * \retval "Not NULL" Target service context pointer, |
| 492 | * \ref tfm_spm_service_t structures |
| 493 | */ |
| 494 | struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid); |
| 495 | |
| 496 | /** |
| 497 | * \brief Get the service context by connection handle. |
| 498 | * |
| 499 | * \param[in] conn_handle Connection handle created by |
| 500 | * tfm_spm_create_conn_handle() |
| 501 | * |
| 502 | * \retval NULL Failed |
| 503 | * \retval "Not NULL" Target service context pointer, |
| 504 | * \ref tfm_spm_service_t structures |
| 505 | */ |
| 506 | struct tfm_spm_service_t * |
| 507 | tfm_spm_get_service_by_handle(psa_handle_t conn_handle); |
| 508 | |
| 509 | /** |
| 510 | * \brief Get the partition context by partition ID. |
| 511 | * |
| 512 | * \param[in] partition_id Partition identity |
| 513 | * |
| 514 | * \retval NULL Failed |
| 515 | * \retval "Not NULL" Target partition context pointer, |
| 516 | * \ref spm_partition_desc_t structures |
| 517 | */ |
| 518 | struct spm_partition_desc_t * |
| 519 | tfm_spm_get_partition_by_id(int32_t partition_id); |
| 520 | |
| 521 | /************************ Message functions **********************************/ |
| 522 | |
| 523 | /** |
| 524 | * \brief Get message context by message handle. |
| 525 | * |
| 526 | * \param[in] msg_handle Message handle which is a reference generated |
| 527 | * by the SPM to a specific message. |
| 528 | * |
| 529 | * \return The message body context pointer |
| 530 | * \ref tfm_msg_body_t structures |
| 531 | */ |
| 532 | struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle); |
| 533 | |
| 534 | /** |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 535 | * \brief Get message context by connect handle. |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 536 | * |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 537 | * \param[in] conn_handle Service connect handle. |
| 538 | * |
| 539 | * \return The message body context pointer |
| 540 | * \ref msg_body_t structures |
| 541 | */ |
| 542 | struct tfm_msg_body_t * |
| 543 | tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle); |
| 544 | |
| 545 | /** |
| 546 | * \brief Fill the message for PSA client call. |
| 547 | * |
| 548 | * \param[in] msg Service Message Queue buffer pointer |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 549 | * \param[in] service Target service context pointer, which can be |
| 550 | * obtained by partition management functions |
| 551 | * \prarm[in] handle Connect handle return by psa_connect(). |
| 552 | * \param[in] type Message type, PSA_IPC_CONNECT, PSA_IPC_CALL or |
| 553 | * PSA_IPC_DISCONNECT |
| 554 | * \param[in] ns_caller Whether from NS caller |
| 555 | * \param[in] invec Array of input \ref psa_invec structures |
| 556 | * \param[in] in_len Number of input \ref psa_invec structures |
| 557 | * \param[in] outvec Array of output \ref psa_outvec structures |
| 558 | * \param[in] out_len Number of output \ref psa_outvec structures |
| 559 | * \param[in] caller_outvec Array of caller output \ref psa_outvec structures |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 560 | */ |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 561 | void tfm_spm_fill_msg(struct tfm_msg_body_t *msg, |
| 562 | struct tfm_spm_service_t *service, |
| 563 | psa_handle_t handle, |
| 564 | int32_t type, int32_t ns_caller, |
| 565 | psa_invec *invec, size_t in_len, |
| 566 | psa_outvec *outvec, size_t out_len, |
| 567 | psa_outvec *caller_outvec); |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 568 | |
| 569 | /** |
| 570 | * \brief Send message and wake up the SP who is waiting on |
| 571 | * message queue, block the current thread and |
| 572 | * scheduler triggered |
| 573 | * |
| 574 | * \param[in] service Target service context pointer, which can be |
| 575 | * obtained by partition management functions |
| 576 | * \param[in] msg message created by tfm_spm_create_msg() |
| 577 | * \ref tfm_msg_body_t structures |
| 578 | * |
| 579 | * \retval IPC_SUCCESS Success |
| 580 | * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input |
| 581 | * \retval IPC_ERROR_GENERIC Failed to enqueue message to service message queue |
| 582 | */ |
| 583 | int32_t tfm_spm_send_event(struct tfm_spm_service_t *service, |
| 584 | struct tfm_msg_body_t *msg); |
| 585 | |
| 586 | /** |
| 587 | * \brief Check the client minor version according to |
| 588 | * version policy |
| 589 | * |
| 590 | * \param[in] service Target service context pointer, which can be get |
| 591 | * by partition management functions |
| 592 | * \param[in] minor_version Client support minor version |
| 593 | * |
| 594 | * \retval IPC_SUCCESS Success |
| 595 | * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input |
| 596 | * \retval IPC_ERROR_VERSION Check failed |
| 597 | */ |
| 598 | int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service, |
| 599 | uint32_t minor_version); |
| 600 | |
| 601 | /** |
| 602 | * \brief Check the memory reference is valid. |
| 603 | * |
| 604 | * \param[in] buffer Pointer of memory reference |
| 605 | * \param[in] len Length of memory reference in bytes |
| 606 | * \param[in] ns_caller From non-secure caller |
| 607 | * \param[in] access Type of access specified by the |
| 608 | * \ref tfm_memory_access_e |
| 609 | * \param[in] privileged Privileged mode or unprivileged mode: |
| 610 | * \ref TFM_PARTITION_UNPRIVILEGED_MODE |
| 611 | * \ref TFM_PARTITION_PRIVILEGED_MODE |
| 612 | * |
| 613 | * \retval IPC_SUCCESS Success |
| 614 | * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input |
| 615 | * \retval IPC_ERROR_MEMORY_CHECK Check failed |
| 616 | */ |
David Hu | cb05d97 | 2019-08-06 18:10:11 +0800 | [diff] [blame] | 617 | int32_t tfm_memory_check(const void *buffer, size_t len, int32_t ns_caller, |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 618 | enum tfm_memory_access_e access, |
| 619 | uint32_t privileged); |
| 620 | |
| 621 | /* This function should be called before schedule function */ |
| 622 | void tfm_spm_init(void); |
| 623 | |
| 624 | /* |
| 625 | * PendSV specified function. |
| 626 | * |
| 627 | * Parameters : |
| 628 | * ctxb - State context storage pointer |
| 629 | * |
| 630 | * Notes: |
| 631 | * This is a staging API. Scheduler should be called in SPM finally and |
| 632 | * this function will be obsoleted later. |
| 633 | */ |
| 634 | void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb); |
| 635 | |
| 636 | #endif /* ifdef(TFM_PSA_API) */ |
| 637 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 638 | #endif /*__SPM_API_H__ */ |