David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 1 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, Arm Limited. All rights reserved. |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 8 | #include <stdint.h> |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 9 | #include "bitops.h" |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 10 | #include "psa/lifecycle.h" |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 11 | #include "psa/service.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 12 | #include "spm_ipc.h" |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 13 | #include "tfm_arch.h" |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 14 | #include "tfm_core_utils.h" |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 15 | #include "load/partition_defs.h" |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 16 | #include "load/service_defs.h" |
Ken Liu | 3dd9256 | 2021-08-17 16:22:54 +0800 | [diff] [blame] | 17 | #include "load/interrupt_defs.h" |
Mingyang Sun | 133a792 | 2021-07-08 16:01:26 +0800 | [diff] [blame] | 18 | #include "psa_api.h" |
Summer Qin | 5fdcf63 | 2020-06-22 16:49:24 +0800 | [diff] [blame] | 19 | #include "utilities.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 20 | #include "ffm/backend.h" |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 21 | #include "ffm/spm_error_base.h" |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 22 | #include "tfm_rpc.h" |
| 23 | #include "tfm_spm_hal.h" |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 24 | #include "tfm_hal_interrupt.h" |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 25 | #include "tfm_hal_platform.h" |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 26 | #include "tfm_psa_call_param.h" |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 27 | |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 28 | #define GET_STATELESS_SERVICE(index) (stateless_services_ref_tbl[index]) |
Xinyu Zhang | a38e9b5 | 2021-06-02 17:48:01 +0800 | [diff] [blame] | 29 | extern struct service_t *stateless_services_ref_tbl[]; |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 30 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 31 | uint32_t tfm_spm_get_lifecycle_state(void) |
| 32 | { |
| 33 | /* |
| 34 | * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be |
| 35 | * implemented in the future. |
| 36 | */ |
| 37 | return PSA_LIFECYCLE_UNKNOWN; |
| 38 | } |
| 39 | |
| 40 | /* PSA Client API function body */ |
| 41 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 42 | uint32_t tfm_spm_client_psa_framework_version(void) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 43 | { |
| 44 | return PSA_FRAMEWORK_VERSION; |
| 45 | } |
| 46 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 47 | uint32_t tfm_spm_client_psa_version(uint32_t sid) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 48 | { |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 49 | struct service_t *service; |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 50 | bool ns_caller = tfm_spm_is_ns_caller(); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * It should return PSA_VERSION_NONE if the RoT Service is not |
| 54 | * implemented. |
| 55 | */ |
| 56 | service = tfm_spm_get_service_by_sid(sid); |
| 57 | if (!service) { |
| 58 | return PSA_VERSION_NONE; |
| 59 | } |
| 60 | |
| 61 | /* |
Shawn Shan | 2365c90 | 2019-12-19 18:35:36 +0800 | [diff] [blame] | 62 | * It should return PSA_VERSION_NONE if the caller is not authorized |
| 63 | * to access the RoT Service. |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 64 | */ |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 65 | if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) { |
Shawn Shan | 2365c90 | 2019-12-19 18:35:36 +0800 | [diff] [blame] | 66 | return PSA_VERSION_NONE; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 69 | return service->p_ldinf->version; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 70 | } |
| 71 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 72 | psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 73 | { |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 74 | struct service_t *service; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 75 | struct tfm_msg_body_t *msg; |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 76 | struct tfm_conn_handle_t *connect_handle; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 77 | int32_t client_id; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 78 | psa_handle_t handle; |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 79 | bool ns_caller = tfm_spm_is_ns_caller(); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 80 | |
Kevin Peng | edb8ee4 | 2021-03-09 16:50:11 +0800 | [diff] [blame] | 81 | /* |
| 82 | * It is a PROGRAMMER ERROR if the RoT Service does not exist on the |
| 83 | * platform. |
| 84 | */ |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 85 | service = tfm_spm_get_service_by_sid(sid); |
| 86 | if (!service) { |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 87 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_CONNECTION_REFUSED); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Mingyang Sun | ef42f44 | 2021-06-11 15:07:58 +0800 | [diff] [blame] | 90 | /* It is a PROGRAMMER ERROR if connecting to a stateless service. */ |
| 91 | if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) { |
| 92 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
| 93 | } |
| 94 | |
Kevin Peng | edb8ee4 | 2021-03-09 16:50:11 +0800 | [diff] [blame] | 95 | /* |
| 96 | * It is a PROGRAMMER ERROR if the caller is not authorized to access the |
| 97 | * RoT Service. |
| 98 | */ |
| 99 | if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) { |
| 100 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_CONNECTION_REFUSED); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * It is a PROGRAMMER ERROR if the version of the RoT Service requested is |
| 105 | * not supported on the platform. |
| 106 | */ |
| 107 | if (tfm_spm_check_client_version(service, version) != SPM_SUCCESS) { |
| 108 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_CONNECTION_REFUSED); |
| 109 | } |
| 110 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 111 | client_id = tfm_spm_get_client_id(ns_caller); |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 112 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 113 | /* |
| 114 | * Create connection handle here since it is possible to return the error |
| 115 | * code to client when creation fails. |
| 116 | */ |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 117 | connect_handle = tfm_spm_create_conn_handle(service, client_id); |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 118 | if (!connect_handle) { |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 119 | return PSA_ERROR_CONNECTION_BUSY; |
| 120 | } |
| 121 | |
Kevin Peng | df6aa29 | 2021-03-11 17:58:50 +0800 | [diff] [blame] | 122 | msg = tfm_spm_get_msg_buffer_from_conn_handle(connect_handle); |
| 123 | if (!msg) { |
| 124 | /* Have no enough resource to create message */ |
| 125 | return PSA_ERROR_CONNECTION_BUSY; |
| 126 | } |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 127 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 128 | handle = tfm_spm_to_user_handle(connect_handle); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 129 | /* No input or output needed for connect message */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 130 | tfm_spm_fill_msg(msg, service, handle, PSA_IPC_CONNECT, |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 131 | client_id, NULL, 0, NULL, 0, NULL); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 132 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 133 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 134 | return backend_instance.messaging(service, msg); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 135 | } |
| 136 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 137 | psa_status_t tfm_spm_client_psa_call(psa_handle_t handle, |
| 138 | uint32_t ctrl_param, |
| 139 | const psa_invec *inptr, |
| 140 | psa_outvec *outptr) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 141 | { |
| 142 | psa_invec invecs[PSA_MAX_IOVEC]; |
| 143 | psa_outvec outvecs[PSA_MAX_IOVEC]; |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 144 | struct tfm_conn_handle_t *conn_handle; |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 145 | struct service_t *service; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 146 | struct tfm_msg_body_t *msg; |
Summer Qin | ba2346e | 2019-11-12 16:26:31 +0800 | [diff] [blame] | 147 | int i, j; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 148 | int32_t client_id; |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 149 | uint32_t sid, version, index; |
Mingyang Sun | e529e3b | 2021-07-12 14:46:30 +0800 | [diff] [blame] | 150 | uint32_t privileged; |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 151 | bool ns_caller = tfm_spm_is_ns_caller(); |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 152 | int32_t type = (int32_t)(int16_t)((ctrl_param & TYPE_MASK) >> TYPE_OFFSET); |
| 153 | size_t in_num = (size_t)((ctrl_param & IN_LEN_MASK) >> IN_LEN_OFFSET); |
| 154 | size_t out_num = (size_t)((ctrl_param & OUT_LEN_MASK) >> OUT_LEN_OFFSET); |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 155 | |
| 156 | /* The request type must be zero or positive. */ |
| 157 | if (type < 0) { |
| 158 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
| 159 | } |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 160 | |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 161 | /* It is a PROGRAMMER ERROR if in_len + out_len > PSA_MAX_IOVEC. */ |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 162 | if ((in_num > PSA_MAX_IOVEC) || |
| 163 | (out_num > PSA_MAX_IOVEC) || |
| 164 | (in_num + out_num > PSA_MAX_IOVEC)) { |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 165 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 168 | client_id = tfm_spm_get_client_id(ns_caller); |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 169 | |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 170 | /* Allocate space from handle pool for static handle. */ |
Mingyang Sun | e8d3808 | 2021-03-30 18:34:40 +0800 | [diff] [blame] | 171 | if (IS_STATIC_HANDLE(handle)) { |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 172 | index = GET_INDEX_FROM_STATIC_HANDLE(handle); |
Mingyang Sun | e8d3808 | 2021-03-30 18:34:40 +0800 | [diff] [blame] | 173 | |
| 174 | if (!IS_VALID_STATIC_HANDLE_IDX(index)) { |
| 175 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
| 176 | } |
| 177 | |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 178 | service = GET_STATELESS_SERVICE(index); |
Mingyang Sun | 8621324 | 2021-07-14 10:26:43 +0800 | [diff] [blame] | 179 | if (!service) { |
| 180 | tfm_core_panic(); |
| 181 | } |
| 182 | |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 183 | sid = service->p_ldinf->sid; |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 184 | |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 185 | /* |
| 186 | * It is a PROGRAMMER ERROR if the caller is not authorized to access |
| 187 | * the RoT Service. |
| 188 | */ |
| 189 | if (tfm_spm_check_authorization(sid, service, ns_caller) |
| 190 | != SPM_SUCCESS) { |
| 191 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_CONNECTION_REFUSED); |
| 192 | } |
| 193 | |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 194 | version = GET_VERSION_FROM_STATIC_HANDLE(handle); |
| 195 | |
| 196 | if (tfm_spm_check_client_version(service, version) != SPM_SUCCESS) { |
| 197 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
| 198 | } |
| 199 | |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 200 | conn_handle = tfm_spm_create_conn_handle(service, client_id); |
| 201 | |
| 202 | if (!conn_handle) { |
| 203 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_CONNECTION_BUSY); |
| 204 | } |
| 205 | |
Mingyang Sun | 6d5dc3d | 2021-03-15 15:34:44 +0800 | [diff] [blame] | 206 | conn_handle->rhandle = NULL; |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 207 | handle = tfm_spm_to_user_handle(conn_handle); |
| 208 | } else { |
| 209 | conn_handle = tfm_spm_to_handle_instance(handle); |
| 210 | |
| 211 | /* It is a PROGRAMMER ERROR if an invalid handle was passed. */ |
| 212 | if (tfm_spm_validate_conn_handle(conn_handle, client_id) |
| 213 | != SPM_SUCCESS) { |
| 214 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * It is a PROGRAMMER ERROR if the connection is currently |
| 219 | * handling a request. |
| 220 | */ |
| 221 | if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) { |
| 222 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Return PSA_ERROR_PROGRAMMER_ERROR immediately for the connection |
| 227 | * has been terminated by the RoT Service. |
| 228 | */ |
| 229 | if (conn_handle->status == TFM_HANDLE_STATUS_CONNECT_ERROR) { |
| 230 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 231 | } |
| 232 | |
| 233 | service = conn_handle->service; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 234 | } |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 235 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 236 | if (!service) { |
| 237 | /* FixMe: Need to implement one mechanism to resolve this failure. */ |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 238 | tfm_core_panic(); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 239 | } |
| 240 | |
Mingyang Sun | e529e3b | 2021-07-12 14:46:30 +0800 | [diff] [blame] | 241 | privileged = tfm_spm_get_caller_privilege_mode(); |
| 242 | |
Kevin Peng | edb8ee4 | 2021-03-09 16:50:11 +0800 | [diff] [blame] | 243 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 244 | * Read client invecs from the wrap input vector. It is a PROGRAMMER ERROR |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 245 | * if the memory reference for the wrap input vector is invalid or not |
| 246 | * readable. |
| 247 | */ |
| 248 | if (tfm_memory_check(inptr, in_num * sizeof(psa_invec), ns_caller, |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 249 | TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) { |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 250 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 251 | } |
Summer Qin | ba2346e | 2019-11-12 16:26:31 +0800 | [diff] [blame] | 252 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 253 | /* |
| 254 | * Read client outvecs from the wrap output vector and will update the |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 255 | * actual length later. It is a PROGRAMMER ERROR if the memory reference for |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 256 | * the wrap output vector is invalid or not read-write. |
| 257 | */ |
| 258 | if (tfm_memory_check(outptr, out_num * sizeof(psa_outvec), ns_caller, |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 259 | TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) { |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 260 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 261 | } |
| 262 | |
Summer Qin | f24dbb5 | 2020-07-23 14:53:54 +0800 | [diff] [blame] | 263 | spm_memset(invecs, 0, sizeof(invecs)); |
| 264 | spm_memset(outvecs, 0, sizeof(outvecs)); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 265 | |
| 266 | /* Copy the address out to avoid TOCTOU attacks. */ |
Summer Qin | f24dbb5 | 2020-07-23 14:53:54 +0800 | [diff] [blame] | 267 | spm_memcpy(invecs, inptr, in_num * sizeof(psa_invec)); |
| 268 | spm_memcpy(outvecs, outptr, out_num * sizeof(psa_outvec)); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 269 | |
| 270 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 271 | * For client input vector, it is a PROGRAMMER ERROR if the provided payload |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 272 | * memory reference was invalid or not readable. |
| 273 | */ |
| 274 | for (i = 0; i < in_num; i++) { |
| 275 | if (tfm_memory_check(invecs[i].base, invecs[i].len, ns_caller, |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 276 | TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) { |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 277 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 278 | } |
| 279 | } |
Summer Qin | ba2346e | 2019-11-12 16:26:31 +0800 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * Clients must never overlap input parameters because of the risk of a |
| 283 | * double-fetch inconsistency. |
| 284 | * Overflow is checked in tfm_memory_check functions. |
| 285 | */ |
| 286 | for (i = 0; i + 1 < in_num; i++) { |
| 287 | for (j = i+1; j < in_num; j++) { |
TTornblom | 83d9637 | 2019-11-19 12:53:16 +0100 | [diff] [blame] | 288 | if (!((char *) invecs[j].base + invecs[j].len <= |
| 289 | (char *) invecs[i].base || |
| 290 | (char *) invecs[j].base >= |
| 291 | (char *) invecs[i].base + invecs[i].len)) { |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 292 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
Summer Qin | ba2346e | 2019-11-12 16:26:31 +0800 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 297 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 298 | * For client output vector, it is a PROGRAMMER ERROR if the provided |
| 299 | * payload memory reference was invalid or not read-write. |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 300 | */ |
| 301 | for (i = 0; i < out_num; i++) { |
| 302 | if (tfm_memory_check(outvecs[i].base, outvecs[i].len, |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 303 | ns_caller, TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) { |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 304 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * FixMe: Need to check if the message is unrecognized by the RoT |
| 310 | * Service or incorrectly formatted. |
| 311 | */ |
Kevin Peng | df6aa29 | 2021-03-11 17:58:50 +0800 | [diff] [blame] | 312 | msg = tfm_spm_get_msg_buffer_from_conn_handle(conn_handle); |
| 313 | if (!msg) { |
| 314 | /* FixMe: Need to implement one mechanism to resolve this failure. */ |
| 315 | TFM_PROGRAMMER_ERROR(ns_caller, PSA_ERROR_PROGRAMMER_ERROR); |
| 316 | } |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 317 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 318 | tfm_spm_fill_msg(msg, service, handle, type, client_id, |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 319 | invecs, in_num, outvecs, out_num, outptr); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 320 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 321 | return backend_instance.messaging(service, msg); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 322 | } |
| 323 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 324 | void tfm_spm_client_psa_close(psa_handle_t handle) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 325 | { |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 326 | struct service_t *service; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 327 | struct tfm_msg_body_t *msg; |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 328 | struct tfm_conn_handle_t *conn_handle; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 329 | int32_t client_id; |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 330 | bool ns_caller = tfm_spm_is_ns_caller(); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 331 | |
| 332 | /* It will have no effect if called with the NULL handle */ |
| 333 | if (handle == PSA_NULL_HANDLE) { |
| 334 | return; |
| 335 | } |
| 336 | |
Mingyang Sun | 00cef5e | 2021-03-04 13:41:56 +0800 | [diff] [blame] | 337 | /* It is a PROGRAMMER ERROR if called with a stateless handle. */ |
Mingyang Sun | e8d3808 | 2021-03-30 18:34:40 +0800 | [diff] [blame] | 338 | if (IS_STATIC_HANDLE(handle)) { |
Mingyang Sun | 00cef5e | 2021-03-04 13:41:56 +0800 | [diff] [blame] | 339 | TFM_PROGRAMMER_ERROR(ns_caller, PROGRAMMER_ERROR_NULL); |
| 340 | } |
| 341 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 342 | client_id = tfm_spm_get_client_id(ns_caller); |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 343 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 344 | conn_handle = tfm_spm_to_handle_instance(handle); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 345 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 346 | * It is a PROGRAMMER ERROR if an invalid handle was provided that is not |
| 347 | * the null handle. |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 348 | */ |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 349 | if (tfm_spm_validate_conn_handle(conn_handle, client_id) != SPM_SUCCESS) { |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 350 | TFM_PROGRAMMER_ERROR(ns_caller, PROGRAMMER_ERROR_NULL); |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 351 | } |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 352 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 353 | service = conn_handle->service; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 354 | if (!service) { |
| 355 | /* FixMe: Need to implement one mechanism to resolve this failure. */ |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 356 | tfm_core_panic(); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 357 | } |
| 358 | |
Kevin Peng | df6aa29 | 2021-03-11 17:58:50 +0800 | [diff] [blame] | 359 | msg = tfm_spm_get_msg_buffer_from_conn_handle(conn_handle); |
| 360 | if (!msg) { |
| 361 | /* FixMe: Need to implement one mechanism to resolve this failure. */ |
| 362 | tfm_core_panic(); |
| 363 | } |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 364 | |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 365 | /* |
| 366 | * It is a PROGRAMMER ERROR if the connection is currently handling a |
| 367 | * request. |
| 368 | */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 369 | if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) { |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 370 | TFM_PROGRAMMER_ERROR(ns_caller, PROGRAMMER_ERROR_NULL); |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 371 | } |
| 372 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 373 | /* No input or output needed for close message */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 374 | tfm_spm_fill_msg(msg, service, handle, PSA_IPC_DISCONNECT, client_id, |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 375 | NULL, 0, NULL, 0, NULL); |
| 376 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 377 | (void)backend_instance.messaging(service, msg); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 378 | } |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 379 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 380 | /* PSA Partition API function body */ |
| 381 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 382 | psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask, |
| 383 | uint32_t timeout) |
| 384 | { |
| 385 | struct partition_t *partition = NULL; |
| 386 | |
| 387 | /* |
| 388 | * Timeout[30:0] are reserved for future use. |
| 389 | * SPM must ignore the value of RES. |
| 390 | */ |
| 391 | timeout &= PSA_TIMEOUT_MASK; |
| 392 | |
| 393 | partition = tfm_spm_get_running_partition(); |
| 394 | if (!partition) { |
| 395 | tfm_core_panic(); |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned |
| 400 | * signals. |
| 401 | */ |
| 402 | if ((partition->signals_allowed & signal_mask) == 0) { |
| 403 | tfm_core_panic(); |
| 404 | } |
| 405 | |
| 406 | /* |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 407 | * thrd_wait_on() blocks the caller thread if no signals are available. |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 408 | * In this case, the return value of this function is temporary set into |
| 409 | * runtime context. After new signal(s) are available, the return value |
| 410 | * is updated with the available signal(s) and blocked thread gets to run. |
| 411 | */ |
| 412 | if (timeout == PSA_BLOCK && |
| 413 | (partition->signals_asserted & signal_mask) == 0) { |
| 414 | partition->signals_waiting = signal_mask; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 415 | thrd_wait_on(&partition->waitobj, |
| 416 | &(tfm_spm_get_running_partition()->thrd)); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | return partition->signals_asserted & signal_mask; |
| 420 | } |
| 421 | |
| 422 | psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg) |
| 423 | { |
| 424 | struct tfm_msg_body_t *tmp_msg = NULL; |
| 425 | struct partition_t *partition = NULL; |
| 426 | uint32_t privileged; |
| 427 | |
| 428 | /* |
| 429 | * Only one message could be retrieved every time for psa_get(). It is a |
| 430 | * fatal error if the input signal has more than a signal bit set. |
| 431 | */ |
| 432 | if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) { |
| 433 | tfm_core_panic(); |
| 434 | } |
| 435 | |
| 436 | partition = tfm_spm_get_running_partition(); |
| 437 | if (!partition) { |
| 438 | tfm_core_panic(); |
| 439 | } |
| 440 | privileged = tfm_spm_partition_get_privileged_mode( |
| 441 | partition->p_ldinf->flags); |
| 442 | |
| 443 | /* |
| 444 | * Write the message to the service buffer. It is a fatal error if the |
| 445 | * input msg pointer is not a valid memory reference or not read-write. |
| 446 | */ |
| 447 | if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW, |
| 448 | privileged) != SPM_SUCCESS) { |
| 449 | tfm_core_panic(); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * It is a fatal error if the caller call psa_get() when no message has |
| 454 | * been set. The caller must call this function after an RoT Service signal |
| 455 | * is returned by psa_wait(). |
| 456 | */ |
| 457 | if (partition->signals_asserted == 0) { |
| 458 | tfm_core_panic(); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * It is a fatal error if the RoT Service signal is not currently asserted. |
| 463 | */ |
| 464 | if ((partition->signals_asserted & signal) == 0) { |
| 465 | tfm_core_panic(); |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Get message by signal from partition. It is a fatal error if getting |
| 470 | * failed, which means the input signal is not correspond to an RoT service. |
| 471 | */ |
| 472 | tmp_msg = tfm_spm_get_msg_by_signal(partition, signal); |
| 473 | if (!tmp_msg) { |
| 474 | return PSA_ERROR_DOES_NOT_EXIST; |
| 475 | } |
| 476 | |
| 477 | (TO_CONTAINER(tmp_msg, |
| 478 | struct tfm_conn_handle_t, |
| 479 | internal_msg))->status = TFM_HANDLE_STATUS_ACTIVE; |
| 480 | |
| 481 | spm_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t)); |
| 482 | |
| 483 | return PSA_SUCCESS; |
| 484 | } |
| 485 | |
| 486 | void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle) |
| 487 | { |
| 488 | struct tfm_msg_body_t *msg = NULL; |
| 489 | struct tfm_conn_handle_t *conn_handle; |
| 490 | |
| 491 | /* It is a fatal error if message handle is invalid */ |
| 492 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 493 | if (!msg) { |
| 494 | tfm_core_panic(); |
| 495 | } |
| 496 | |
| 497 | /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */ |
| 498 | if (SERVICE_IS_STATELESS(msg->service->p_ldinf->flags)) { |
| 499 | tfm_core_panic(); |
| 500 | } |
| 501 | |
| 502 | msg->msg.rhandle = rhandle; |
| 503 | conn_handle = tfm_spm_to_handle_instance(msg_handle); |
| 504 | |
| 505 | /* Store reverse handle for following client calls. */ |
| 506 | tfm_spm_set_rhandle(msg->service, conn_handle, rhandle); |
| 507 | } |
| 508 | |
| 509 | size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx, |
| 510 | void *buffer, size_t num_bytes) |
| 511 | { |
| 512 | size_t bytes; |
| 513 | struct tfm_msg_body_t *msg = NULL; |
| 514 | uint32_t privileged; |
| 515 | struct partition_t *partition = NULL; |
| 516 | |
| 517 | /* It is a fatal error if message handle is invalid */ |
| 518 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 519 | if (!msg) { |
| 520 | tfm_core_panic(); |
| 521 | } |
| 522 | |
| 523 | partition = msg->service->partition; |
| 524 | privileged = tfm_spm_partition_get_privileged_mode( |
| 525 | partition->p_ldinf->flags); |
| 526 | |
| 527 | /* |
| 528 | * It is a fatal error if message handle does not refer to a request |
| 529 | * message |
| 530 | */ |
| 531 | if (msg->msg.type < PSA_IPC_CALL) { |
| 532 | tfm_core_panic(); |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * It is a fatal error if invec_idx is equal to or greater than |
| 537 | * PSA_MAX_IOVEC |
| 538 | */ |
| 539 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 540 | tfm_core_panic(); |
| 541 | } |
| 542 | |
| 543 | /* There was no remaining data in this input vector */ |
| 544 | if (msg->msg.in_size[invec_idx] == 0) { |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Copy the client data to the service buffer. It is a fatal error |
| 550 | * if the memory reference for buffer is invalid or not read-write. |
| 551 | */ |
| 552 | if (tfm_memory_check(buffer, num_bytes, false, |
| 553 | TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) { |
| 554 | tfm_core_panic(); |
| 555 | } |
| 556 | |
| 557 | bytes = num_bytes > msg->msg.in_size[invec_idx] ? |
| 558 | msg->msg.in_size[invec_idx] : num_bytes; |
| 559 | |
| 560 | spm_memcpy(buffer, msg->invec[invec_idx].base, bytes); |
| 561 | |
| 562 | /* There maybe some remaining data */ |
| 563 | msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes; |
| 564 | msg->msg.in_size[invec_idx] -= bytes; |
| 565 | |
| 566 | return bytes; |
| 567 | } |
| 568 | |
| 569 | size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx, |
| 570 | size_t num_bytes) |
| 571 | { |
| 572 | struct tfm_msg_body_t *msg = NULL; |
| 573 | |
| 574 | /* It is a fatal error if message handle is invalid */ |
| 575 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 576 | if (!msg) { |
| 577 | tfm_core_panic(); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * It is a fatal error if message handle does not refer to a request |
| 582 | * message |
| 583 | */ |
| 584 | if (msg->msg.type < PSA_IPC_CALL) { |
| 585 | tfm_core_panic(); |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | * It is a fatal error if invec_idx is equal to or greater than |
| 590 | * PSA_MAX_IOVEC |
| 591 | */ |
| 592 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 593 | tfm_core_panic(); |
| 594 | } |
| 595 | |
| 596 | /* There was no remaining data in this input vector */ |
| 597 | if (msg->msg.in_size[invec_idx] == 0) { |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * If num_bytes is greater than the remaining size of the input vector then |
| 603 | * the remaining size of the input vector is used. |
| 604 | */ |
| 605 | if (num_bytes > msg->msg.in_size[invec_idx]) { |
| 606 | num_bytes = msg->msg.in_size[invec_idx]; |
| 607 | } |
| 608 | |
| 609 | /* There maybe some remaining data */ |
| 610 | msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + |
| 611 | num_bytes; |
| 612 | msg->msg.in_size[invec_idx] -= num_bytes; |
| 613 | |
| 614 | return num_bytes; |
| 615 | } |
| 616 | |
| 617 | void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx, |
| 618 | const void *buffer, size_t num_bytes) |
| 619 | { |
| 620 | struct tfm_msg_body_t *msg = NULL; |
| 621 | uint32_t privileged; |
| 622 | struct partition_t *partition = NULL; |
| 623 | |
| 624 | /* It is a fatal error if message handle is invalid */ |
| 625 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 626 | if (!msg) { |
| 627 | tfm_core_panic(); |
| 628 | } |
| 629 | |
| 630 | partition = msg->service->partition; |
| 631 | privileged = tfm_spm_partition_get_privileged_mode( |
| 632 | partition->p_ldinf->flags); |
| 633 | |
| 634 | /* |
| 635 | * It is a fatal error if message handle does not refer to a request |
| 636 | * message |
| 637 | */ |
| 638 | if (msg->msg.type < PSA_IPC_CALL) { |
| 639 | tfm_core_panic(); |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * It is a fatal error if outvec_idx is equal to or greater than |
| 644 | * PSA_MAX_IOVEC |
| 645 | */ |
| 646 | if (outvec_idx >= PSA_MAX_IOVEC) { |
| 647 | tfm_core_panic(); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * It is a fatal error if the call attempts to write data past the end of |
| 652 | * the client output vector |
| 653 | */ |
| 654 | if (num_bytes > msg->msg.out_size[outvec_idx] - |
| 655 | msg->outvec[outvec_idx].len) { |
| 656 | tfm_core_panic(); |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Copy the service buffer to client outvecs. It is a fatal error |
| 661 | * if the memory reference for buffer is invalid or not readable. |
| 662 | */ |
| 663 | if (tfm_memory_check(buffer, num_bytes, false, |
| 664 | TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) { |
| 665 | tfm_core_panic(); |
| 666 | } |
| 667 | |
| 668 | spm_memcpy((char *)msg->outvec[outvec_idx].base + |
| 669 | msg->outvec[outvec_idx].len, buffer, num_bytes); |
| 670 | |
| 671 | /* Update the write number */ |
| 672 | msg->outvec[outvec_idx].len += num_bytes; |
| 673 | } |
| 674 | |
| 675 | void tfm_spm_partition_psa_reply(psa_handle_t msg_handle, psa_status_t status) |
| 676 | { |
| 677 | struct service_t *service = NULL; |
| 678 | struct tfm_msg_body_t *msg = NULL; |
| 679 | int32_t ret = PSA_SUCCESS; |
| 680 | struct tfm_conn_handle_t *conn_handle; |
| 681 | |
| 682 | /* It is a fatal error if message handle is invalid */ |
| 683 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 684 | if (!msg) { |
| 685 | tfm_core_panic(); |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * RoT Service information is needed in this function, stored it in message |
| 690 | * body structure. Only two parameters are passed in this function: handle |
| 691 | * and status, so it is useful and simply to do like this. |
| 692 | */ |
| 693 | service = msg->service; |
| 694 | if (!service) { |
| 695 | tfm_core_panic(); |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Three type of message are passed in this function: CONNECTION, REQUEST, |
| 700 | * DISCONNECTION. It needs to process differently for each type. |
| 701 | */ |
| 702 | conn_handle = tfm_spm_to_handle_instance(msg_handle); |
| 703 | switch (msg->msg.type) { |
| 704 | case PSA_IPC_CONNECT: |
| 705 | /* |
| 706 | * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the |
| 707 | * input status is PSA_SUCCESS. Others return values are based on the |
| 708 | * input status. |
| 709 | */ |
| 710 | if (status == PSA_SUCCESS) { |
| 711 | ret = msg_handle; |
| 712 | } else if (status == PSA_ERROR_CONNECTION_REFUSED) { |
| 713 | /* Refuse the client connection, indicating a permanent error. */ |
| 714 | tfm_spm_free_conn_handle(service, conn_handle); |
| 715 | ret = PSA_ERROR_CONNECTION_REFUSED; |
| 716 | } else if (status == PSA_ERROR_CONNECTION_BUSY) { |
| 717 | /* Fail the client connection, indicating a transient error. */ |
| 718 | ret = PSA_ERROR_CONNECTION_BUSY; |
| 719 | } else { |
| 720 | tfm_core_panic(); |
| 721 | } |
| 722 | break; |
| 723 | case PSA_IPC_DISCONNECT: |
| 724 | /* Service handle is not used anymore */ |
| 725 | tfm_spm_free_conn_handle(service, conn_handle); |
| 726 | |
| 727 | /* |
| 728 | * If the message type is PSA_IPC_DISCONNECT, then the status code is |
| 729 | * ignored |
| 730 | */ |
| 731 | break; |
| 732 | default: |
| 733 | if (msg->msg.type >= PSA_IPC_CALL) { |
| 734 | /* Reply to a request message. Return values are based on status */ |
| 735 | ret = status; |
| 736 | /* |
| 737 | * The total number of bytes written to a single parameter must be |
| 738 | * reported to the client by updating the len member of the |
| 739 | * psa_outvec structure for the parameter before returning from |
| 740 | * psa_call(). |
| 741 | */ |
| 742 | update_caller_outvec_len(msg); |
| 743 | if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) { |
| 744 | tfm_spm_free_conn_handle(service, conn_handle); |
| 745 | } |
| 746 | } else { |
| 747 | tfm_core_panic(); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | if (ret == PSA_ERROR_PROGRAMMER_ERROR) { |
| 752 | /* |
| 753 | * If the source of the programmer error is a Secure Partition, the SPM |
| 754 | * must panic the Secure Partition in response to a PROGRAMMER ERROR. |
| 755 | */ |
| 756 | if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) { |
| 757 | conn_handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR; |
| 758 | } else { |
| 759 | tfm_core_panic(); |
| 760 | } |
| 761 | } else { |
| 762 | conn_handle->status = TFM_HANDLE_STATUS_IDLE; |
| 763 | } |
| 764 | |
| 765 | if (is_tfm_rpc_msg(msg)) { |
| 766 | tfm_rpc_client_call_reply(msg, ret); |
| 767 | } else { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 768 | thrd_wake_up(&msg->ack_evnt, ret); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
| 772 | void tfm_spm_partition_psa_notify(int32_t partition_id) |
| 773 | { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 774 | struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id); |
| 775 | |
| 776 | spm_assert_signal(p_pt, PSA_DOORBELL); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | void tfm_spm_partition_psa_clear(void) |
| 780 | { |
| 781 | struct partition_t *partition = NULL; |
| 782 | |
| 783 | partition = tfm_spm_get_running_partition(); |
| 784 | if (!partition) { |
| 785 | tfm_core_panic(); |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * It is a fatal error if the Secure Partition's doorbell signal is not |
| 790 | * currently asserted. |
| 791 | */ |
| 792 | if ((partition->signals_asserted & PSA_DOORBELL) == 0) { |
| 793 | tfm_core_panic(); |
| 794 | } |
| 795 | partition->signals_asserted &= ~PSA_DOORBELL; |
| 796 | } |
| 797 | |
| 798 | void tfm_spm_partition_psa_eoi(psa_signal_t irq_signal) |
| 799 | { |
| 800 | struct irq_load_info_t *irq_info = NULL; |
| 801 | struct partition_t *partition = NULL; |
| 802 | |
| 803 | partition = tfm_spm_get_running_partition(); |
| 804 | if (!partition) { |
| 805 | tfm_core_panic(); |
| 806 | } |
| 807 | |
| 808 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 809 | /* It is a fatal error if passed signal is not an interrupt signal. */ |
| 810 | if (!irq_info) { |
| 811 | tfm_core_panic(); |
| 812 | } |
| 813 | |
| 814 | if (irq_info->flih_func) { |
| 815 | /* This API is for SLIH IRQs only */ |
| 816 | psa_panic(); |
| 817 | } |
| 818 | |
| 819 | /* It is a fatal error if passed signal is not currently asserted */ |
| 820 | if ((partition->signals_asserted & irq_signal) == 0) { |
| 821 | tfm_core_panic(); |
| 822 | } |
| 823 | |
| 824 | partition->signals_asserted &= ~irq_signal; |
| 825 | |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 826 | tfm_hal_irq_clear_pending(irq_info->source); |
| 827 | tfm_hal_irq_enable(irq_info->source); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | void tfm_spm_partition_psa_panic(void) |
| 831 | { |
| 832 | /* |
| 833 | * PSA FF recommends that the SPM causes the system to restart when a secure |
| 834 | * partition panics. |
| 835 | */ |
| 836 | tfm_hal_system_reset(); |
| 837 | } |
| 838 | |
| 839 | void tfm_spm_partition_irq_enable(psa_signal_t irq_signal) |
| 840 | { |
| 841 | struct partition_t *partition; |
| 842 | struct irq_load_info_t *irq_info; |
| 843 | |
| 844 | partition = tfm_spm_get_running_partition(); |
| 845 | if (!partition) { |
| 846 | tfm_core_panic(); |
| 847 | } |
| 848 | |
| 849 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 850 | if (!irq_info) { |
| 851 | tfm_core_panic(); |
| 852 | } |
| 853 | |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 854 | tfm_hal_irq_enable(irq_info->source); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | psa_irq_status_t tfm_spm_partition_irq_disable(psa_signal_t irq_signal) |
| 858 | { |
| 859 | struct partition_t *partition; |
| 860 | struct irq_load_info_t *irq_info; |
| 861 | |
| 862 | partition = tfm_spm_get_running_partition(); |
| 863 | if (!partition) { |
| 864 | tfm_core_panic(); |
| 865 | } |
| 866 | |
| 867 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 868 | if (!irq_info) { |
| 869 | tfm_core_panic(); |
| 870 | } |
| 871 | |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 872 | tfm_hal_irq_disable(irq_info->source); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 873 | |
| 874 | return 1; |
| 875 | } |
| 876 | |
| 877 | void tfm_spm_partition_psa_reset_signal(psa_signal_t irq_signal) |
| 878 | { |
| 879 | struct irq_load_info_t *irq_info; |
| 880 | struct partition_t *partition; |
| 881 | |
| 882 | partition = tfm_spm_get_running_partition(); |
| 883 | if (!partition) { |
| 884 | tfm_core_panic(); |
| 885 | } |
| 886 | |
| 887 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 888 | if (!irq_info) { |
| 889 | tfm_core_panic(); |
| 890 | } |
| 891 | |
| 892 | if (!irq_info->flih_func) { |
| 893 | /* This API is for FLIH IRQs only */ |
| 894 | tfm_core_panic(); |
| 895 | } |
| 896 | |
| 897 | if ((partition->signals_asserted & irq_signal) == 0) { |
| 898 | /* The signal is not asserted */ |
| 899 | tfm_core_panic(); |
| 900 | } |
| 901 | |
| 902 | partition->signals_asserted &= ~irq_signal; |
| 903 | } |