David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 1 | /* |
Xinyu Zhang | 669744c | 2023-02-06 15:24:26 +0800 | [diff] [blame^] | 2 | * Copyright (c) 2019-2023, Arm Limited. All rights reserved. |
BohdanHunko | 628109f | 2023-01-31 17:23:35 +0200 | [diff] [blame] | 3 | * Copyright (c) 2022-2023 Cypress Semiconductor Corporation (an Infineon |
Chris Brand | 10a2acb | 2022-10-18 17:12:27 -0700 | [diff] [blame] | 4 | * company) or an affiliate of Cypress Semiconductor Corporation. All rights |
| 5 | * reserved. |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 6 | * |
| 7 | * SPDX-License-Identifier: BSD-3-Clause |
| 8 | * |
| 9 | */ |
| 10 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 11 | #include <stdint.h> |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 12 | #include "bitops.h" |
Mingyang Sun | 4609ac7 | 2022-02-08 18:56:35 +0800 | [diff] [blame] | 13 | #include "config_impl.h" |
Xinyu Zhang | cdbe362 | 2022-10-31 14:34:25 +0800 | [diff] [blame] | 14 | #include "config_spm.h" |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 15 | #include "critical_section.h" |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 16 | #include "psa/lifecycle.h" |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 17 | #include "psa/service.h" |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 18 | #include "interrupt.h" |
Sherry Zhang | c714702 | 2023-02-03 11:21:10 +0800 | [diff] [blame] | 19 | #include "spm.h" |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 20 | #include "tfm_arch.h" |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 21 | #include "load/partition_defs.h" |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 22 | #include "load/service_defs.h" |
Ken Liu | 3dd9256 | 2021-08-17 16:22:54 +0800 | [diff] [blame] | 23 | #include "load/interrupt_defs.h" |
Summer Qin | 5fdcf63 | 2020-06-22 16:49:24 +0800 | [diff] [blame] | 24 | #include "utilities.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 25 | #include "ffm/backend.h" |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 26 | #include "ffm/psa_api.h" |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 27 | #include "tfm_rpc.h" |
Kevin Peng | b42ed86 | 2022-08-08 14:44:02 +0800 | [diff] [blame] | 28 | #include "tfm_api.h" |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 29 | #include "tfm_hal_interrupt.h" |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 30 | #include "tfm_hal_platform.h" |
Ken Liu | 82e3eac | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 31 | #include "tfm_psa_call_pack.h" |
Summer Qin | 56725eb | 2022-05-06 15:23:40 +0800 | [diff] [blame] | 32 | #include "tfm_hal_isolation.h" |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 33 | |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 34 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 35 | |
| 36 | /* |
| 37 | * The MM-IOVEC status |
| 38 | * The max total number of invec and outvec is 8. |
| 39 | * Each invec/outvec takes 4 bit, 32 bits in total. |
| 40 | * |
| 41 | * The encoding format of the MM-IOVEC status: |
| 42 | *-------------------------------------------------------------- |
| 43 | *| Bit | 31 - 28 | 27 - 24 | ... | 7 - 4 | 3 - 0 | |
| 44 | *-------------------------------------------------------------- |
| 45 | *| Vector | outvec[3] | outvec[2] | ... | invec[1] | invec[0] | |
| 46 | *-------------------------------------------------------------- |
| 47 | * |
| 48 | * Take invec[0] as an example: |
| 49 | * |
| 50 | * bit 0: whether invec[0] has been mapped. |
| 51 | * bit 1: whether invec[0] has been unmapped. |
| 52 | * bit 2: whether invec[0] has been accessed using psa_read(), psa_skip() or |
| 53 | * psa_write(). |
| 54 | * bit 3: reserved for invec[0]. |
| 55 | */ |
| 56 | |
| 57 | #define IOVEC_STATUS_BITS 4 /* Each vector occupies 4 bits. */ |
| 58 | #define OUTVEC_IDX_BASE 4 /* |
| 59 | * Base index of outvec. |
| 60 | * There are four invecs in front of |
| 61 | * outvec. |
| 62 | */ |
| 63 | #define INVEC_IDX_BASE 0 /* Base index of invec. */ |
| 64 | |
| 65 | #define IOVEC_MAPPED_BIT (1U << 0) |
| 66 | #define IOVEC_UNMAPPED_BIT (1U << 1) |
| 67 | #define IOVEC_ACCESSED_BIT (1U << 2) |
| 68 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 69 | #define IOVEC_IS_MAPPED(handle, iovec_idx) \ |
| 70 | ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \ |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 71 | IOVEC_MAPPED_BIT) |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 72 | #define IOVEC_IS_UNMAPPED(handle, iovec_idx) \ |
| 73 | ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \ |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 74 | IOVEC_UNMAPPED_BIT) |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 75 | #define IOVEC_IS_ACCESSED(handle, iovec_idx) \ |
| 76 | ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \ |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 77 | IOVEC_ACCESSED_BIT) |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 78 | #define SET_IOVEC_MAPPED(handle, iovec_idx) \ |
| 79 | (((handle)->iovec_status) |= (IOVEC_MAPPED_BIT << \ |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 80 | ((iovec_idx) * IOVEC_STATUS_BITS))) |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 81 | #define SET_IOVEC_UNMAPPED(handle, iovec_idx) \ |
| 82 | (((handle)->iovec_status) |= (IOVEC_UNMAPPED_BIT << \ |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 83 | ((iovec_idx) * IOVEC_STATUS_BITS))) |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 84 | #define SET_IOVEC_ACCESSED(handle, iovec_idx) \ |
| 85 | (((handle)->iovec_status) |= (IOVEC_ACCESSED_BIT << \ |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 86 | ((iovec_idx) * IOVEC_STATUS_BITS))) |
| 87 | |
| 88 | #endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */ |
| 89 | |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 90 | void spm_handle_programmer_errors(psa_status_t status) |
| 91 | { |
Summer Qin | d9d497b | 2022-04-14 14:39:26 +0800 | [diff] [blame] | 92 | if (status == PSA_ERROR_PROGRAMMER_ERROR || |
| 93 | status == PSA_ERROR_CONNECTION_REFUSED) { |
| 94 | if (!tfm_spm_is_ns_caller()) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 95 | tfm_core_panic(); |
Summer Qin | d9d497b | 2022-04-14 14:39:26 +0800 | [diff] [blame] | 96 | } |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 100 | uint32_t tfm_spm_get_lifecycle_state(void) |
| 101 | { |
| 102 | /* |
| 103 | * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be |
| 104 | * implemented in the future. |
| 105 | */ |
| 106 | return PSA_LIFECYCLE_UNKNOWN; |
| 107 | } |
| 108 | |
| 109 | /* PSA Client API function body */ |
| 110 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 111 | uint32_t tfm_spm_client_psa_framework_version(void) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 112 | { |
| 113 | return PSA_FRAMEWORK_VERSION; |
| 114 | } |
| 115 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 116 | uint32_t tfm_spm_client_psa_version(uint32_t sid) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 117 | { |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 118 | struct service_t *service; |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 119 | bool ns_caller = tfm_spm_is_ns_caller(); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * It should return PSA_VERSION_NONE if the RoT Service is not |
| 123 | * implemented. |
| 124 | */ |
| 125 | service = tfm_spm_get_service_by_sid(sid); |
| 126 | if (!service) { |
| 127 | return PSA_VERSION_NONE; |
| 128 | } |
| 129 | |
| 130 | /* |
Shawn Shan | 2365c90 | 2019-12-19 18:35:36 +0800 | [diff] [blame] | 131 | * It should return PSA_VERSION_NONE if the caller is not authorized |
| 132 | * to access the RoT Service. |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 133 | */ |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 134 | if (tfm_spm_check_authorization(sid, service, ns_caller) != PSA_SUCCESS) { |
Shawn Shan | 2365c90 | 2019-12-19 18:35:36 +0800 | [diff] [blame] | 135 | return PSA_VERSION_NONE; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 138 | return service->p_ldinf->version; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 141 | /* Following PSA APIs are only needed by connection-based services */ |
| 142 | #if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1 |
| 143 | |
| 144 | psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version) |
| 145 | { |
| 146 | struct service_t *service; |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 147 | struct conn_handle_t *conn_handle; |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 148 | int32_t client_id; |
| 149 | psa_handle_t handle; |
| 150 | bool ns_caller = tfm_spm_is_ns_caller(); |
| 151 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
| 152 | |
| 153 | /* |
| 154 | * It is a PROGRAMMER ERROR if the RoT Service does not exist on the |
| 155 | * platform. |
| 156 | */ |
| 157 | service = tfm_spm_get_service_by_sid(sid); |
| 158 | if (!service) { |
| 159 | return PSA_ERROR_CONNECTION_REFUSED; |
| 160 | } |
| 161 | |
| 162 | /* It is a PROGRAMMER ERROR if connecting to a stateless service. */ |
| 163 | if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) { |
| 164 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * It is a PROGRAMMER ERROR if the caller is not authorized to access the |
| 169 | * RoT Service. |
| 170 | */ |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 171 | if (tfm_spm_check_authorization(sid, service, ns_caller) != PSA_SUCCESS) { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 172 | return PSA_ERROR_CONNECTION_REFUSED; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * It is a PROGRAMMER ERROR if the version of the RoT Service requested is |
| 177 | * not supported on the platform. |
| 178 | */ |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 179 | if (tfm_spm_check_client_version(service, version) != PSA_SUCCESS) { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 180 | return PSA_ERROR_CONNECTION_REFUSED; |
| 181 | } |
| 182 | |
| 183 | client_id = tfm_spm_get_client_id(ns_caller); |
| 184 | |
| 185 | /* |
| 186 | * Create connection handle here since it is possible to return the error |
| 187 | * code to client when creation fails. |
| 188 | */ |
| 189 | CRITICAL_SECTION_ENTER(cs_assert); |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 190 | conn_handle = tfm_spm_create_conn_handle(); |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 191 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 192 | if (!conn_handle) { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 193 | return PSA_ERROR_CONNECTION_BUSY; |
| 194 | } |
| 195 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 196 | handle = tfm_spm_to_user_handle(conn_handle); |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 197 | /* No input or output needed for connect message */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 198 | spm_fill_message(conn_handle, service, handle, PSA_IPC_CONNECT, |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 199 | client_id, NULL, 0, NULL, 0, NULL); |
| 200 | |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 201 | return backend_messaging(service, conn_handle); |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 204 | psa_status_t tfm_spm_client_psa_close(psa_handle_t handle) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 205 | { |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 206 | struct service_t *service; |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 207 | struct conn_handle_t *conn_handle; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 208 | int32_t client_id; |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 209 | bool ns_caller = tfm_spm_is_ns_caller(); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 210 | |
| 211 | /* It will have no effect if called with the NULL handle */ |
| 212 | if (handle == PSA_NULL_HANDLE) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 213 | return PSA_SUCCESS; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Mingyang Sun | 00cef5e | 2021-03-04 13:41:56 +0800 | [diff] [blame] | 216 | /* It is a PROGRAMMER ERROR if called with a stateless handle. */ |
Mingyang Sun | e8d3808 | 2021-03-30 18:34:40 +0800 | [diff] [blame] | 217 | if (IS_STATIC_HANDLE(handle)) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 218 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | 00cef5e | 2021-03-04 13:41:56 +0800 | [diff] [blame] | 219 | } |
| 220 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 221 | client_id = tfm_spm_get_client_id(ns_caller); |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 222 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 223 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 224 | * It is a PROGRAMMER ERROR if an invalid handle was provided that is not |
| 225 | * the null handle. |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 226 | */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 227 | conn_handle = spm_get_handle_by_client_handle(handle, client_id); |
| 228 | if (!conn_handle) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 229 | return PSA_ERROR_PROGRAMMER_ERROR; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 230 | } |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 231 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 232 | service = conn_handle->service; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 233 | if (!service) { |
| 234 | /* FixMe: Need to implement one mechanism to resolve this failure. */ |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 235 | return PSA_ERROR_PROGRAMMER_ERROR; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 236 | } |
| 237 | |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 238 | /* |
| 239 | * It is a PROGRAMMER ERROR if the connection is currently handling a |
| 240 | * request. |
| 241 | */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 242 | if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 243 | return PSA_ERROR_PROGRAMMER_ERROR; |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 244 | } |
| 245 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 246 | /* No input or output needed for close message */ |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 247 | spm_fill_message(conn_handle, service, handle, PSA_IPC_DISCONNECT, |
| 248 | client_id, NULL, 0, NULL, 0, NULL); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 249 | |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 250 | return backend_messaging(service, conn_handle); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 251 | } |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 252 | |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 253 | #endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */ |
| 254 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 255 | /* PSA Partition API function body */ |
| 256 | |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 257 | #if CONFIG_TFM_SPM_BACKEND_IPC == 1 \ |
| 258 | || CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 259 | psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask, |
| 260 | uint32_t timeout) |
| 261 | { |
| 262 | struct partition_t *partition = NULL; |
| 263 | |
| 264 | /* |
| 265 | * Timeout[30:0] are reserved for future use. |
| 266 | * SPM must ignore the value of RES. |
| 267 | */ |
| 268 | timeout &= PSA_TIMEOUT_MASK; |
| 269 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 270 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 271 | |
| 272 | /* |
Ruchika Gupta | d183417 | 2022-06-15 12:03:08 +0530 | [diff] [blame] | 273 | * signals_allowed can be 0 for TF-M internal partitions for special usages. |
| 274 | * Regular Secure Partitions should have at least one signal. |
| 275 | * This is gauranteed by the manifest tool. |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 276 | * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned |
| 277 | * signals. |
| 278 | */ |
Ruchika Gupta | d183417 | 2022-06-15 12:03:08 +0530 | [diff] [blame] | 279 | if ((partition->signals_allowed) && |
| 280 | (partition->signals_allowed & signal_mask) == 0) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 281 | tfm_core_panic(); |
| 282 | } |
| 283 | |
| 284 | /* |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 285 | * backend_wake_up() blocks the caller thread if no signals are |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 286 | * available. In this case, the return value of this function is temporary |
| 287 | * set into runtime context. After new signal(s) are available, the return |
| 288 | * value is updated with the available signal(s) and blocked thread gets |
| 289 | * to run. |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 290 | */ |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 291 | if (timeout == PSA_BLOCK) { |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 292 | return backend_wait(partition, signal_mask); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return partition->signals_asserted & signal_mask; |
| 296 | } |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 297 | #endif |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 298 | |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 299 | #if CONFIG_TFM_SPM_BACKEND_IPC == 1 |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 300 | psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg) |
| 301 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 302 | struct conn_handle_t *handle = NULL; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 303 | struct partition_t *partition = NULL; |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 304 | fih_int fih_rc = FIH_FAILURE; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * Only one message could be retrieved every time for psa_get(). It is a |
| 308 | * fatal error if the input signal has more than a signal bit set. |
| 309 | */ |
| 310 | if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) { |
| 311 | tfm_core_panic(); |
| 312 | } |
| 313 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 314 | partition = GET_CURRENT_COMPONENT(); |
| 315 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 316 | /* |
| 317 | * Write the message to the service buffer. It is a fatal error if the |
| 318 | * input msg pointer is not a valid memory reference or not read-write. |
| 319 | */ |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 320 | FIH_CALL(tfm_hal_memory_check, fih_rc, |
| 321 | partition->boundary, (uintptr_t)msg, |
| 322 | sizeof(psa_msg_t), TFM_HAL_ACCESS_READWRITE); |
| 323 | if (fih_not_eq(fih_rc, fih_int_encode(PSA_SUCCESS))) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 324 | tfm_core_panic(); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * It is a fatal error if the caller call psa_get() when no message has |
| 329 | * been set. The caller must call this function after an RoT Service signal |
| 330 | * is returned by psa_wait(). |
| 331 | */ |
| 332 | if (partition->signals_asserted == 0) { |
| 333 | tfm_core_panic(); |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * It is a fatal error if the RoT Service signal is not currently asserted. |
| 338 | */ |
| 339 | if ((partition->signals_asserted & signal) == 0) { |
| 340 | tfm_core_panic(); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Get message by signal from partition. It is a fatal error if getting |
| 345 | * failed, which means the input signal is not correspond to an RoT service. |
| 346 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 347 | handle = spm_get_handle_by_signal(partition, signal); |
| 348 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 349 | return PSA_ERROR_DOES_NOT_EXIST; |
| 350 | } |
| 351 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 352 | spm_memcpy(msg, &handle->msg, sizeof(psa_msg_t)); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 353 | |
| 354 | return PSA_SUCCESS; |
| 355 | } |
Mingyang Sun | 4609ac7 | 2022-02-08 18:56:35 +0800 | [diff] [blame] | 356 | #endif |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 357 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 358 | size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx, |
| 359 | void *buffer, size_t num_bytes) |
| 360 | { |
| 361 | size_t bytes; |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 362 | struct conn_handle_t *handle = NULL; |
Summer Qin | 56725eb | 2022-05-06 15:23:40 +0800 | [diff] [blame] | 363 | struct partition_t *curr_partition = GET_CURRENT_COMPONENT(); |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 364 | fih_int fih_rc = FIH_FAILURE; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 365 | |
| 366 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 367 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 368 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 369 | tfm_core_panic(); |
| 370 | } |
| 371 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 372 | /* |
| 373 | * It is a fatal error if message handle does not refer to a request |
| 374 | * message |
| 375 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 376 | if (handle->msg.type < PSA_IPC_CALL) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 377 | tfm_core_panic(); |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * It is a fatal error if invec_idx is equal to or greater than |
| 382 | * PSA_MAX_IOVEC |
| 383 | */ |
| 384 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 385 | tfm_core_panic(); |
| 386 | } |
| 387 | |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 388 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 389 | /* |
| 390 | * It is a fatal error if the input vector has already been mapped using |
| 391 | * psa_map_invec(). |
| 392 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 393 | if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 394 | tfm_core_panic(); |
| 395 | } |
| 396 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 397 | SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 398 | #endif |
| 399 | |
Kevin Peng | 016b0e4 | 2022-04-15 11:28:48 +0800 | [diff] [blame] | 400 | /* There was no remaining data in this input vector */ |
| 401 | if (handle->msg.in_size[invec_idx] == 0) { |
| 402 | return 0; |
| 403 | } |
| 404 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 405 | /* |
| 406 | * Copy the client data to the service buffer. It is a fatal error |
| 407 | * if the memory reference for buffer is invalid or not read-write. |
| 408 | */ |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 409 | FIH_CALL(tfm_hal_memory_check, fih_rc, |
| 410 | curr_partition->boundary, (uintptr_t)buffer, |
| 411 | num_bytes, TFM_HAL_ACCESS_READWRITE); |
| 412 | if (fih_not_eq(fih_rc, fih_int_encode(PSA_SUCCESS))) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 413 | tfm_core_panic(); |
| 414 | } |
| 415 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 416 | bytes = num_bytes > handle->msg.in_size[invec_idx] ? |
| 417 | handle->msg.in_size[invec_idx] : num_bytes; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 418 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 419 | spm_memcpy(buffer, handle->invec[invec_idx].base, bytes); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 420 | |
| 421 | /* There maybe some remaining data */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 422 | handle->invec[invec_idx].base = |
| 423 | (char *)handle->invec[invec_idx].base + bytes; |
| 424 | handle->msg.in_size[invec_idx] -= bytes; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 425 | |
| 426 | return bytes; |
| 427 | } |
| 428 | |
| 429 | size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx, |
| 430 | size_t num_bytes) |
| 431 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 432 | struct conn_handle_t *handle = NULL; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 433 | |
| 434 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 435 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 436 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 437 | tfm_core_panic(); |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * It is a fatal error if message handle does not refer to a request |
| 442 | * message |
| 443 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 444 | if (handle->msg.type < PSA_IPC_CALL) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 445 | tfm_core_panic(); |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * It is a fatal error if invec_idx is equal to or greater than |
| 450 | * PSA_MAX_IOVEC |
| 451 | */ |
| 452 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 453 | tfm_core_panic(); |
| 454 | } |
| 455 | |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 456 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 457 | /* |
| 458 | * It is a fatal error if the input vector has already been mapped using |
| 459 | * psa_map_invec(). |
| 460 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 461 | if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 462 | tfm_core_panic(); |
| 463 | } |
| 464 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 465 | SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 466 | #endif |
| 467 | |
Kevin Peng | 016b0e4 | 2022-04-15 11:28:48 +0800 | [diff] [blame] | 468 | /* There was no remaining data in this input vector */ |
| 469 | if (handle->msg.in_size[invec_idx] == 0) { |
| 470 | return 0; |
| 471 | } |
| 472 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 473 | /* |
| 474 | * If num_bytes is greater than the remaining size of the input vector then |
| 475 | * the remaining size of the input vector is used. |
| 476 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 477 | if (num_bytes > handle->msg.in_size[invec_idx]) { |
| 478 | num_bytes = handle->msg.in_size[invec_idx]; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* There maybe some remaining data */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 482 | handle->invec[invec_idx].base = |
| 483 | (char *)handle->invec[invec_idx].base + num_bytes; |
| 484 | handle->msg.in_size[invec_idx] -= num_bytes; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 485 | |
| 486 | return num_bytes; |
| 487 | } |
| 488 | |
| 489 | void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx, |
| 490 | const void *buffer, size_t num_bytes) |
| 491 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 492 | struct conn_handle_t *handle = NULL; |
Summer Qin | 56725eb | 2022-05-06 15:23:40 +0800 | [diff] [blame] | 493 | struct partition_t *curr_partition = GET_CURRENT_COMPONENT(); |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 494 | fih_int fih_rc = FIH_FAILURE; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 495 | |
| 496 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 497 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 498 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 499 | tfm_core_panic(); |
| 500 | } |
| 501 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 502 | /* |
| 503 | * It is a fatal error if message handle does not refer to a request |
| 504 | * message |
| 505 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 506 | if (handle->msg.type < PSA_IPC_CALL) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 507 | tfm_core_panic(); |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * It is a fatal error if outvec_idx is equal to or greater than |
| 512 | * PSA_MAX_IOVEC |
| 513 | */ |
| 514 | if (outvec_idx >= PSA_MAX_IOVEC) { |
| 515 | tfm_core_panic(); |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * It is a fatal error if the call attempts to write data past the end of |
| 520 | * the client output vector |
| 521 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 522 | if (num_bytes > handle->msg.out_size[outvec_idx] - |
| 523 | handle->outvec[outvec_idx].len) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 524 | tfm_core_panic(); |
| 525 | } |
| 526 | |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 527 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 528 | /* |
| 529 | * It is a fatal error if the output vector has already been mapped using |
| 530 | * psa_map_outvec(). |
| 531 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 532 | if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 533 | tfm_core_panic(); |
| 534 | } |
| 535 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 536 | SET_IOVEC_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 537 | #endif |
| 538 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 539 | /* |
| 540 | * Copy the service buffer to client outvecs. It is a fatal error |
| 541 | * if the memory reference for buffer is invalid or not readable. |
| 542 | */ |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 543 | FIH_CALL(tfm_hal_memory_check, fih_rc, |
| 544 | curr_partition->boundary, (uintptr_t)buffer, |
| 545 | num_bytes, TFM_HAL_ACCESS_READABLE); |
| 546 | if (fih_not_eq(fih_rc, fih_int_encode(PSA_SUCCESS))) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 547 | tfm_core_panic(); |
| 548 | } |
| 549 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 550 | spm_memcpy((char *)handle->outvec[outvec_idx].base + |
| 551 | handle->outvec[outvec_idx].len, buffer, num_bytes); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 552 | |
| 553 | /* Update the write number */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 554 | handle->outvec[outvec_idx].len += num_bytes; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 555 | } |
| 556 | |
Ken Liu | f8c7e53 | 2022-02-10 15:03:04 +0800 | [diff] [blame] | 557 | psa_status_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle, |
| 558 | psa_status_t status) |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 559 | { |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 560 | struct service_t *service; |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 561 | struct conn_handle_t *handle; |
Ken Liu | f8c7e53 | 2022-02-10 15:03:04 +0800 | [diff] [blame] | 562 | psa_status_t ret = PSA_SUCCESS; |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 563 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 564 | |
| 565 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 566 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 567 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 568 | tfm_core_panic(); |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * RoT Service information is needed in this function, stored it in message |
| 573 | * body structure. Only two parameters are passed in this function: handle |
| 574 | * and status, so it is useful and simply to do like this. |
| 575 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 576 | service = handle->service; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 577 | if (!service) { |
| 578 | tfm_core_panic(); |
| 579 | } |
| 580 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 581 | switch (handle->msg.type) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 582 | case PSA_IPC_CONNECT: |
| 583 | /* |
| 584 | * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the |
| 585 | * input status is PSA_SUCCESS. Others return values are based on the |
| 586 | * input status. |
| 587 | */ |
| 588 | if (status == PSA_SUCCESS) { |
| 589 | ret = msg_handle; |
| 590 | } else if (status == PSA_ERROR_CONNECTION_REFUSED) { |
| 591 | /* Refuse the client connection, indicating a permanent error. */ |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 592 | ret = PSA_ERROR_CONNECTION_REFUSED; |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 593 | handle->status = TFM_HANDLE_STATUS_TO_FREE; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 594 | } else if (status == PSA_ERROR_CONNECTION_BUSY) { |
| 595 | /* Fail the client connection, indicating a transient error. */ |
| 596 | ret = PSA_ERROR_CONNECTION_BUSY; |
| 597 | } else { |
| 598 | tfm_core_panic(); |
| 599 | } |
| 600 | break; |
| 601 | case PSA_IPC_DISCONNECT: |
| 602 | /* Service handle is not used anymore */ |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 603 | handle->status = TFM_HANDLE_STATUS_TO_FREE; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | * If the message type is PSA_IPC_DISCONNECT, then the status code is |
| 607 | * ignored |
| 608 | */ |
| 609 | break; |
| 610 | default: |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 611 | if (handle->msg.type >= PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 612 | |
| 613 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 614 | |
| 615 | /* |
| 616 | * If the unmapped function is not called for an input/output vector |
| 617 | * that has been mapped, the framework will remove the mapping. |
| 618 | */ |
| 619 | int i; |
| 620 | |
| 621 | for (i = 0; i < PSA_MAX_IOVEC * 2; i++) { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 622 | if (IOVEC_IS_MAPPED(handle, i) && |
| 623 | (!IOVEC_IS_UNMAPPED(handle, i))) { |
| 624 | SET_IOVEC_UNMAPPED(handle, i); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 625 | /* |
| 626 | * Any output vectors that are still mapped will report that |
| 627 | * zero bytes have been written. |
| 628 | */ |
| 629 | if (i >= OUTVEC_IDX_BASE) { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 630 | handle->outvec[i - OUTVEC_IDX_BASE].len = 0; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | #endif |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 636 | /* Reply to a request message. Return values are based on status */ |
| 637 | ret = status; |
| 638 | /* |
| 639 | * The total number of bytes written to a single parameter must be |
| 640 | * reported to the client by updating the len member of the |
| 641 | * psa_outvec structure for the parameter before returning from |
| 642 | * psa_call(). |
| 643 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 644 | update_caller_outvec_len(handle); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 645 | if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) { |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 646 | handle->status = TFM_HANDLE_STATUS_TO_FREE; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 647 | } |
| 648 | } else { |
| 649 | tfm_core_panic(); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | if (ret == PSA_ERROR_PROGRAMMER_ERROR) { |
| 654 | /* |
| 655 | * If the source of the programmer error is a Secure Partition, the SPM |
| 656 | * must panic the Secure Partition in response to a PROGRAMMER ERROR. |
| 657 | */ |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 658 | if (!TFM_CLIENT_ID_IS_NS(handle->msg.client_id)) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 659 | tfm_core_panic(); |
| 660 | } |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 661 | } |
| 662 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 663 | /* |
| 664 | * TODO: It can be optimized further by moving critical section protection |
| 665 | * to mailbox. Also need to check implementation when secure context is |
| 666 | * involved. |
| 667 | */ |
| 668 | CRITICAL_SECTION_ENTER(cs_assert); |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 669 | ret = backend_replying(handle, ret); |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 670 | CRITICAL_SECTION_LEAVE(cs_assert); |
| 671 | |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 672 | if (handle->status == TFM_HANDLE_STATUS_TO_FREE) { |
| 673 | tfm_spm_free_conn_handle(handle); |
| 674 | } else { |
| 675 | handle->status = TFM_HANDLE_STATUS_IDLE; |
| 676 | } |
| 677 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 678 | return ret; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 679 | } |
| 680 | |
Kevin Peng | 613b417 | 2022-02-15 14:41:44 +0800 | [diff] [blame] | 681 | #if CONFIG_TFM_DOORBELL_API == 1 |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 682 | void tfm_spm_partition_psa_notify(int32_t partition_id) |
| 683 | { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 684 | struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id); |
| 685 | |
| 686 | spm_assert_signal(p_pt, PSA_DOORBELL); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | void tfm_spm_partition_psa_clear(void) |
| 690 | { |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 691 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 692 | struct partition_t *partition = NULL; |
| 693 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 694 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 695 | |
| 696 | /* |
| 697 | * It is a fatal error if the Secure Partition's doorbell signal is not |
| 698 | * currently asserted. |
| 699 | */ |
| 700 | if ((partition->signals_asserted & PSA_DOORBELL) == 0) { |
| 701 | tfm_core_panic(); |
| 702 | } |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 703 | |
| 704 | CRITICAL_SECTION_ENTER(cs_assert); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 705 | partition->signals_asserted &= ~PSA_DOORBELL; |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 706 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 707 | } |
Kevin Peng | 613b417 | 2022-02-15 14:41:44 +0800 | [diff] [blame] | 708 | #endif /* CONFIG_TFM_DOORBELL_API == 1 */ |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 709 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 710 | void tfm_spm_partition_psa_panic(void) |
| 711 | { |
Joakim Andersson | 97cef7e | 2022-03-18 10:22:00 +0100 | [diff] [blame] | 712 | #ifdef CONFIG_TFM_HALT_ON_CORE_PANIC |
| 713 | tfm_hal_system_halt(); |
| 714 | #else |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 715 | /* |
| 716 | * PSA FF recommends that the SPM causes the system to restart when a secure |
| 717 | * partition panics. |
| 718 | */ |
| 719 | tfm_hal_system_reset(); |
Joakim Andersson | 97cef7e | 2022-03-18 10:22:00 +0100 | [diff] [blame] | 720 | #endif |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 721 | } |
| 722 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 723 | /* psa_set_rhandle is only needed by connection-based services */ |
| 724 | #if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1 |
| 725 | |
| 726 | void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle) |
| 727 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 728 | struct conn_handle_t *handle; |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 729 | |
| 730 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 731 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 732 | if (!handle) { |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 733 | tfm_core_panic(); |
| 734 | } |
| 735 | |
| 736 | /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 737 | if (SERVICE_IS_STATELESS(handle->service->p_ldinf->flags)) { |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 738 | tfm_core_panic(); |
| 739 | } |
| 740 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 741 | handle->msg.rhandle = rhandle; |
| 742 | handle->rhandle = rhandle; |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | #endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */ |
| 746 | |
| 747 | #if CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 |
Kevin Peng | 67a89fd | 2021-11-25 11:22:02 +0800 | [diff] [blame] | 748 | void tfm_spm_partition_psa_irq_enable(psa_signal_t irq_signal) |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 749 | { |
| 750 | struct partition_t *partition; |
Chris Brand | 10a2acb | 2022-10-18 17:12:27 -0700 | [diff] [blame] | 751 | const struct irq_load_info_t *irq_info; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 752 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 753 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 754 | |
| 755 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 756 | if (!irq_info) { |
| 757 | tfm_core_panic(); |
| 758 | } |
| 759 | |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 760 | tfm_hal_irq_enable(irq_info->source); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 761 | } |
| 762 | |
Kevin Peng | 67a89fd | 2021-11-25 11:22:02 +0800 | [diff] [blame] | 763 | psa_irq_status_t tfm_spm_partition_psa_irq_disable(psa_signal_t irq_signal) |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 764 | { |
| 765 | struct partition_t *partition; |
Chris Brand | 10a2acb | 2022-10-18 17:12:27 -0700 | [diff] [blame] | 766 | const struct irq_load_info_t *irq_info; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 767 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 768 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 769 | |
| 770 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 771 | if (!irq_info) { |
| 772 | tfm_core_panic(); |
| 773 | } |
| 774 | |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 775 | tfm_hal_irq_disable(irq_info->source); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 776 | |
| 777 | return 1; |
| 778 | } |
| 779 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 780 | /* This API is only used for FLIH. */ |
| 781 | #if CONFIG_TFM_FLIH_API == 1 |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 782 | void tfm_spm_partition_psa_reset_signal(psa_signal_t irq_signal) |
| 783 | { |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 784 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Chris Brand | 10a2acb | 2022-10-18 17:12:27 -0700 | [diff] [blame] | 785 | const struct irq_load_info_t *irq_info; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 786 | struct partition_t *partition; |
| 787 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 788 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 789 | |
| 790 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 791 | if (!irq_info) { |
| 792 | tfm_core_panic(); |
| 793 | } |
| 794 | |
| 795 | if (!irq_info->flih_func) { |
| 796 | /* This API is for FLIH IRQs only */ |
| 797 | tfm_core_panic(); |
| 798 | } |
| 799 | |
| 800 | if ((partition->signals_asserted & irq_signal) == 0) { |
| 801 | /* The signal is not asserted */ |
| 802 | tfm_core_panic(); |
| 803 | } |
| 804 | |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 805 | CRITICAL_SECTION_ENTER(cs_assert); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 806 | partition->signals_asserted &= ~irq_signal; |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 807 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 808 | } |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 809 | #endif |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 810 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 811 | /* This API is only used for SLIH. */ |
| 812 | #if CONFIG_TFM_SLIH_API == 1 |
| 813 | void tfm_spm_partition_psa_eoi(psa_signal_t irq_signal) |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 814 | { |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 815 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Chris Brand | 10a2acb | 2022-10-18 17:12:27 -0700 | [diff] [blame] | 816 | const struct irq_load_info_t *irq_info = NULL; |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 817 | struct partition_t *partition = NULL; |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 818 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 819 | partition = GET_CURRENT_COMPONENT(); |
| 820 | |
| 821 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 822 | /* It is a fatal error if passed signal is not an interrupt signal. */ |
| 823 | if (!irq_info) { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 824 | tfm_core_panic(); |
| 825 | } |
| 826 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 827 | if (irq_info->flih_func) { |
| 828 | /* This API is for SLIH IRQs only */ |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 829 | tfm_core_panic(); |
| 830 | } |
| 831 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 832 | /* It is a fatal error if passed signal is not currently asserted */ |
| 833 | if ((partition->signals_asserted & irq_signal) == 0) { |
| 834 | tfm_core_panic(); |
| 835 | } |
| 836 | |
| 837 | CRITICAL_SECTION_ENTER(cs_assert); |
| 838 | partition->signals_asserted &= ~irq_signal; |
| 839 | CRITICAL_SECTION_LEAVE(cs_assert); |
| 840 | |
| 841 | tfm_hal_irq_clear_pending(irq_info->source); |
| 842 | tfm_hal_irq_enable(irq_info->source); |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 843 | } |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 844 | #endif |
| 845 | #endif /* CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 */ |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 846 | |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 847 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 848 | |
| 849 | const void *tfm_spm_partition_psa_map_invec(psa_handle_t msg_handle, |
| 850 | uint32_t invec_idx) |
| 851 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 852 | struct conn_handle_t *handle; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 853 | struct partition_t *partition = NULL; |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 854 | fih_int fih_rc = FIH_FAILURE; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 855 | |
| 856 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 857 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 858 | if (!handle) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 859 | tfm_core_panic(); |
| 860 | } |
| 861 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 862 | partition = handle->service->partition; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 863 | |
| 864 | /* |
| 865 | * It is a fatal error if MM-IOVEC has not been enabled for the RoT |
| 866 | * Service that received the message. |
| 867 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 868 | if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 869 | tfm_core_panic(); |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * It is a fatal error if message handle does not refer to a request |
| 874 | * message. |
| 875 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 876 | if (handle->msg.type < PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 877 | tfm_core_panic(); |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | * It is a fatal error if invec_idx is equal to or greater than |
| 882 | * PSA_MAX_IOVEC. |
| 883 | */ |
| 884 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 885 | tfm_core_panic(); |
| 886 | } |
| 887 | |
| 888 | /* It is a fatal error if the input vector has length zero. */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 889 | if (handle->msg.in_size[invec_idx] == 0) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 890 | tfm_core_panic(); |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * It is a fatal error if the input vector has already been mapped using |
| 895 | * psa_map_invec(). |
| 896 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 897 | if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 898 | tfm_core_panic(); |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * It is a fatal error if the input vector has already been accessed |
| 903 | * using psa_read() or psa_skip(). |
| 904 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 905 | if (IOVEC_IS_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 906 | tfm_core_panic(); |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * It is a fatal error if the memory reference for the wrap input vector is |
| 911 | * invalid or not readable. |
| 912 | */ |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 913 | FIH_CALL(tfm_hal_memory_check, fih_rc, |
| 914 | partition->boundary, (uintptr_t)handle->invec[invec_idx].base, |
| 915 | handle->invec[invec_idx].len, TFM_HAL_ACCESS_READABLE); |
| 916 | if (fih_not_eq(fih_rc, fih_int_encode(PSA_SUCCESS))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 917 | tfm_core_panic(); |
| 918 | } |
| 919 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 920 | SET_IOVEC_MAPPED(handle, (invec_idx + INVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 921 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 922 | return handle->invec[invec_idx].base; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle, |
| 926 | uint32_t invec_idx) |
| 927 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 928 | struct conn_handle_t *handle; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 929 | |
| 930 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 931 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 932 | if (!handle) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 933 | tfm_core_panic(); |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * It is a fatal error if MM-IOVEC has not been enabled for the RoT |
| 938 | * Service that received the message. |
| 939 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 940 | if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 941 | tfm_core_panic(); |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | * It is a fatal error if message handle does not refer to a request |
| 946 | * message. |
| 947 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 948 | if (handle->msg.type < PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 949 | tfm_core_panic(); |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * It is a fatal error if invec_idx is equal to or greater than |
| 954 | * PSA_MAX_IOVEC. |
| 955 | */ |
| 956 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 957 | tfm_core_panic(); |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * It is a fatal error if The input vector has not been mapped by a call to |
| 962 | * psa_map_invec(). |
| 963 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 964 | if (!IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 965 | tfm_core_panic(); |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * It is a fatal error if the input vector has already been unmapped by a |
| 970 | * call to psa_unmap_invec(). |
| 971 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 972 | if (IOVEC_IS_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 973 | tfm_core_panic(); |
| 974 | } |
| 975 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 976 | SET_IOVEC_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle, |
| 980 | uint32_t outvec_idx) |
| 981 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 982 | struct conn_handle_t *handle; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 983 | struct partition_t *partition = NULL; |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 984 | fih_int fih_rc = FIH_FAILURE; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 985 | |
| 986 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 987 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 988 | if (!handle) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 989 | tfm_core_panic(); |
| 990 | } |
| 991 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 992 | partition = handle->service->partition; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 993 | |
| 994 | /* |
| 995 | * It is a fatal error if MM-IOVEC has not been enabled for the RoT |
| 996 | * Service that received the message. |
| 997 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 998 | if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 999 | tfm_core_panic(); |
| 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * It is a fatal error if message handle does not refer to a request |
| 1004 | * message. |
| 1005 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1006 | if (handle->msg.type < PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1007 | tfm_core_panic(); |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * It is a fatal error if outvec_idx is equal to or greater than |
| 1012 | * PSA_MAX_IOVEC. |
| 1013 | */ |
| 1014 | if (outvec_idx >= PSA_MAX_IOVEC) { |
| 1015 | tfm_core_panic(); |
| 1016 | } |
| 1017 | |
| 1018 | /* It is a fatal error if the output vector has length zero. */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1019 | if (handle->msg.out_size[outvec_idx] == 0) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1020 | tfm_core_panic(); |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * It is a fatal error if the output vector has already been mapped using |
| 1025 | * psa_map_outvec(). |
| 1026 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1027 | if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1028 | tfm_core_panic(); |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * It is a fatal error if the output vector has already been accessed |
| 1033 | * using psa_write(). |
| 1034 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1035 | if (IOVEC_IS_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1036 | tfm_core_panic(); |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * It is a fatal error if the output vector is invalid or not read-write. |
| 1041 | */ |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 1042 | FIH_CALL(tfm_hal_memory_check, fih_rc, |
| 1043 | partition->boundary, (uintptr_t)handle->outvec[outvec_idx].base, |
| 1044 | handle->outvec[outvec_idx].len, TFM_HAL_ACCESS_READWRITE); |
| 1045 | if (fih_not_eq(fih_rc, fih_int_encode(PSA_SUCCESS))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1046 | tfm_core_panic(); |
| 1047 | } |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1048 | SET_IOVEC_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1049 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1050 | return handle->outvec[outvec_idx].base; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle, |
| 1054 | uint32_t outvec_idx, size_t len) |
| 1055 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1056 | struct conn_handle_t *handle; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1057 | |
| 1058 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 1059 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1060 | if (!handle) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1061 | tfm_core_panic(); |
| 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | * It is a fatal error if MM-IOVEC has not been enabled for the RoT |
| 1066 | * Service that received the message. |
| 1067 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1068 | if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1069 | tfm_core_panic(); |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * It is a fatal error if message handle does not refer to a request |
| 1074 | * message. |
| 1075 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1076 | if (handle->msg.type < PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1077 | tfm_core_panic(); |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * It is a fatal error if outvec_idx is equal to or greater than |
| 1082 | * PSA_MAX_IOVEC. |
| 1083 | */ |
| 1084 | if (outvec_idx >= PSA_MAX_IOVEC) { |
| 1085 | tfm_core_panic(); |
| 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * It is a fatal error if len is greater than the output vector size. |
| 1090 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1091 | if (len > handle->msg.out_size[outvec_idx]) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1092 | tfm_core_panic(); |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * It is a fatal error if The output vector has not been mapped by a call to |
| 1097 | * psa_map_outvec(). |
| 1098 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1099 | if (!IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1100 | tfm_core_panic(); |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * It is a fatal error if the output vector has already been unmapped by a |
| 1105 | * call to psa_unmap_outvec(). |
| 1106 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1107 | if (IOVEC_IS_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1108 | tfm_core_panic(); |
| 1109 | } |
| 1110 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1111 | SET_IOVEC_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1112 | |
| 1113 | /* Update the write number */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1114 | handle->outvec[outvec_idx].len = len; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | #endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */ |