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