David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 1 | /* |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 2 | * Copyright (c) 2019-2022, 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 | 4609ac7 | 2022-02-08 18:56:35 +0800 | [diff] [blame] | 10 | #include "config_impl.h" |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 11 | #include "critical_section.h" |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 12 | #include "psa/lifecycle.h" |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 13 | #include "psa/service.h" |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 14 | #include "interrupt.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 15 | #include "spm_ipc.h" |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 16 | #include "tfm_arch.h" |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 17 | #include "tfm_core_utils.h" |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 18 | #include "load/partition_defs.h" |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 19 | #include "load/service_defs.h" |
Ken Liu | 3dd9256 | 2021-08-17 16:22:54 +0800 | [diff] [blame] | 20 | #include "load/interrupt_defs.h" |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 21 | #include "ffm/psa_api.h" |
Summer Qin | 5fdcf63 | 2020-06-22 16:49:24 +0800 | [diff] [blame] | 22 | #include "utilities.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 23 | #include "ffm/backend.h" |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 24 | #include "ffm/psa_api.h" |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 25 | #include "tfm_rpc.h" |
| 26 | #include "tfm_spm_hal.h" |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 27 | #include "tfm_hal_interrupt.h" |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 28 | #include "tfm_hal_platform.h" |
Ken Liu | 82e3eac | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 29 | #include "tfm_psa_call_pack.h" |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 30 | |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 31 | #define GET_STATELESS_SERVICE(index) (stateless_services_ref_tbl[index]) |
Xinyu Zhang | a38e9b5 | 2021-06-02 17:48:01 +0800 | [diff] [blame] | 32 | extern struct service_t *stateless_services_ref_tbl[]; |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +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 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 141 | psa_status_t tfm_spm_client_psa_call(psa_handle_t handle, |
| 142 | uint32_t ctrl_param, |
| 143 | const psa_invec *inptr, |
| 144 | psa_outvec *outptr) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 145 | { |
| 146 | psa_invec invecs[PSA_MAX_IOVEC]; |
| 147 | psa_outvec outvecs[PSA_MAX_IOVEC]; |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 148 | struct conn_handle_t *conn_handle; |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 149 | struct service_t *service; |
Summer Qin | ba2346e | 2019-11-12 16:26:31 +0800 | [diff] [blame] | 150 | int i, j; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 151 | int32_t client_id; |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 152 | uint32_t sid, version, index; |
Mingyang Sun | e529e3b | 2021-07-12 14:46:30 +0800 | [diff] [blame] | 153 | uint32_t privileged; |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 154 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 155 | bool ns_caller = tfm_spm_is_ns_caller(); |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 156 | int32_t type = (int32_t)(int16_t)((ctrl_param & TYPE_MASK) >> TYPE_OFFSET); |
| 157 | size_t in_num = (size_t)((ctrl_param & IN_LEN_MASK) >> IN_LEN_OFFSET); |
| 158 | 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] | 159 | |
| 160 | /* The request type must be zero or positive. */ |
| 161 | if (type < 0) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 162 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 163 | } |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 164 | |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 165 | /* It is a PROGRAMMER ERROR if in_len + out_len > PSA_MAX_IOVEC. */ |
Summer Qin | 22c301a | 2022-04-27 17:36:00 +0800 | [diff] [blame^] | 166 | if ((in_num > SIZE_MAX - out_num) || |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 167 | (in_num + out_num > PSA_MAX_IOVEC)) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 168 | return PSA_ERROR_PROGRAMMER_ERROR; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Mingyang Sun | cf0b1a5 | 2022-03-28 17:29:03 +0800 | [diff] [blame] | 171 | /* It is a PROGRAMMER ERROR if the handle is a null handle. */ |
| 172 | if (handle == PSA_NULL_HANDLE) { |
| 173 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 174 | } |
| 175 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 176 | client_id = tfm_spm_get_client_id(ns_caller); |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 177 | |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 178 | /* Allocate space from handle pool for static handle. */ |
Mingyang Sun | e8d3808 | 2021-03-30 18:34:40 +0800 | [diff] [blame] | 179 | if (IS_STATIC_HANDLE(handle)) { |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 180 | index = GET_INDEX_FROM_STATIC_HANDLE(handle); |
Mingyang Sun | e8d3808 | 2021-03-30 18:34:40 +0800 | [diff] [blame] | 181 | |
| 182 | if (!IS_VALID_STATIC_HANDLE_IDX(index)) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 183 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | e8d3808 | 2021-03-30 18:34:40 +0800 | [diff] [blame] | 184 | } |
| 185 | |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 186 | service = GET_STATELESS_SERVICE(index); |
Mingyang Sun | 8621324 | 2021-07-14 10:26:43 +0800 | [diff] [blame] | 187 | if (!service) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 188 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | 8621324 | 2021-07-14 10:26:43 +0800 | [diff] [blame] | 189 | } |
| 190 | |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 191 | sid = service->p_ldinf->sid; |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 192 | |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 193 | /* |
| 194 | * It is a PROGRAMMER ERROR if the caller is not authorized to access |
| 195 | * the RoT Service. |
| 196 | */ |
| 197 | if (tfm_spm_check_authorization(sid, service, ns_caller) |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 198 | != PSA_SUCCESS) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 199 | return PSA_ERROR_CONNECTION_REFUSED; |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 200 | } |
| 201 | |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 202 | version = GET_VERSION_FROM_STATIC_HANDLE(handle); |
| 203 | |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 204 | if (tfm_spm_check_client_version(service, version) != PSA_SUCCESS) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 205 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 206 | } |
| 207 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 208 | CRITICAL_SECTION_ENTER(cs_assert); |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 209 | conn_handle = tfm_spm_create_conn_handle(); |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 210 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 211 | |
| 212 | if (!conn_handle) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 213 | return PSA_ERROR_CONNECTION_BUSY; |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Mingyang Sun | 6d5dc3d | 2021-03-15 15:34:44 +0800 | [diff] [blame] | 216 | conn_handle->rhandle = NULL; |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 217 | handle = tfm_spm_to_user_handle(conn_handle); |
| 218 | } else { |
Sherry Zhang | a4575f3 | 2022-02-23 15:45:51 +0800 | [diff] [blame] | 219 | #if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1 |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 220 | /* It is a PROGRAMMER ERROR if an invalid handle was passed. */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 221 | conn_handle = spm_get_handle_by_client_handle(handle, client_id); |
| 222 | if (!conn_handle) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 223 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
| 227 | * It is a PROGRAMMER ERROR if the connection is currently |
| 228 | * handling a request. |
| 229 | */ |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 230 | if (conn_handle->status != TFM_HANDLE_STATUS_IDLE) { |
Mingyang Sun | cb6f70e | 2021-03-05 23:30:25 +0800 | [diff] [blame] | 231 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 232 | } |
| 233 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 234 | service = conn_handle->service; |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 235 | |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 236 | if (!service) { |
| 237 | /* FixMe: Need to implement a mechanism to resolve this failure. */ |
| 238 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 239 | } |
Sherry Zhang | a4575f3 | 2022-02-23 15:45:51 +0800 | [diff] [blame] | 240 | #else |
| 241 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 242 | #endif |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 243 | } |
| 244 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 245 | privileged = GET_CURRENT_PARTITION_PRIVILEGED_MODE(); |
Mingyang Sun | e529e3b | 2021-07-12 14:46:30 +0800 | [diff] [blame] | 246 | |
Kevin Peng | edb8ee4 | 2021-03-09 16:50:11 +0800 | [diff] [blame] | 247 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 248 | * 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] | 249 | * if the memory reference for the wrap input vector is invalid or not |
| 250 | * readable. |
| 251 | */ |
| 252 | if (tfm_memory_check(inptr, in_num * sizeof(psa_invec), ns_caller, |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 253 | TFM_MEMORY_ACCESS_RO, privileged) != PSA_SUCCESS) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 254 | return PSA_ERROR_PROGRAMMER_ERROR; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 255 | } |
Summer Qin | ba2346e | 2019-11-12 16:26:31 +0800 | [diff] [blame] | 256 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 257 | /* |
| 258 | * Read client outvecs from the wrap output vector and will update the |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 259 | * 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] | 260 | * the wrap output vector is invalid or not read-write. |
| 261 | */ |
| 262 | if (tfm_memory_check(outptr, out_num * sizeof(psa_outvec), ns_caller, |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 263 | TFM_MEMORY_ACCESS_RW, privileged) != PSA_SUCCESS) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 264 | return PSA_ERROR_PROGRAMMER_ERROR; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 265 | } |
| 266 | |
Summer Qin | f24dbb5 | 2020-07-23 14:53:54 +0800 | [diff] [blame] | 267 | spm_memset(invecs, 0, sizeof(invecs)); |
| 268 | spm_memset(outvecs, 0, sizeof(outvecs)); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 269 | |
| 270 | /* Copy the address out to avoid TOCTOU attacks. */ |
Summer Qin | f24dbb5 | 2020-07-23 14:53:54 +0800 | [diff] [blame] | 271 | spm_memcpy(invecs, inptr, in_num * sizeof(psa_invec)); |
| 272 | spm_memcpy(outvecs, outptr, out_num * sizeof(psa_outvec)); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 273 | |
| 274 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 275 | * 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] | 276 | * memory reference was invalid or not readable. |
| 277 | */ |
| 278 | for (i = 0; i < in_num; i++) { |
| 279 | if (tfm_memory_check(invecs[i].base, invecs[i].len, ns_caller, |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 280 | TFM_MEMORY_ACCESS_RO, privileged) != PSA_SUCCESS) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 281 | return PSA_ERROR_PROGRAMMER_ERROR; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 282 | } |
| 283 | } |
Summer Qin | ba2346e | 2019-11-12 16:26:31 +0800 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * Clients must never overlap input parameters because of the risk of a |
| 287 | * double-fetch inconsistency. |
| 288 | * Overflow is checked in tfm_memory_check functions. |
| 289 | */ |
| 290 | for (i = 0; i + 1 < in_num; i++) { |
| 291 | for (j = i+1; j < in_num; j++) { |
TTornblom | 83d9637 | 2019-11-19 12:53:16 +0100 | [diff] [blame] | 292 | if (!((char *) invecs[j].base + invecs[j].len <= |
| 293 | (char *) invecs[i].base || |
| 294 | (char *) invecs[j].base >= |
| 295 | (char *) invecs[i].base + invecs[i].len)) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 296 | return PSA_ERROR_PROGRAMMER_ERROR; |
Summer Qin | ba2346e | 2019-11-12 16:26:31 +0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 301 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 302 | * For client output vector, it is a PROGRAMMER ERROR if the provided |
| 303 | * payload memory reference was invalid or not read-write. |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 304 | */ |
| 305 | for (i = 0; i < out_num; i++) { |
| 306 | if (tfm_memory_check(outvecs[i].base, outvecs[i].len, |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 307 | ns_caller, TFM_MEMORY_ACCESS_RW, privileged) != PSA_SUCCESS) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 308 | return PSA_ERROR_PROGRAMMER_ERROR; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 312 | spm_fill_message(conn_handle, service, handle, type, client_id, |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 313 | invecs, in_num, outvecs, out_num, outptr); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 314 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 315 | return backend_instance.messaging(service, conn_handle); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 316 | } |
| 317 | |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 318 | /* Following PSA APIs are only needed by connection-based services */ |
| 319 | #if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1 |
| 320 | |
| 321 | psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version) |
| 322 | { |
| 323 | struct service_t *service; |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 324 | struct conn_handle_t *conn_handle; |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 325 | int32_t client_id; |
| 326 | psa_handle_t handle; |
| 327 | bool ns_caller = tfm_spm_is_ns_caller(); |
| 328 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
| 329 | |
| 330 | /* |
| 331 | * It is a PROGRAMMER ERROR if the RoT Service does not exist on the |
| 332 | * platform. |
| 333 | */ |
| 334 | service = tfm_spm_get_service_by_sid(sid); |
| 335 | if (!service) { |
| 336 | return PSA_ERROR_CONNECTION_REFUSED; |
| 337 | } |
| 338 | |
| 339 | /* It is a PROGRAMMER ERROR if connecting to a stateless service. */ |
| 340 | if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) { |
| 341 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * It is a PROGRAMMER ERROR if the caller is not authorized to access the |
| 346 | * RoT Service. |
| 347 | */ |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 348 | if (tfm_spm_check_authorization(sid, service, ns_caller) != PSA_SUCCESS) { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 349 | return PSA_ERROR_CONNECTION_REFUSED; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * It is a PROGRAMMER ERROR if the version of the RoT Service requested is |
| 354 | * not supported on the platform. |
| 355 | */ |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 356 | if (tfm_spm_check_client_version(service, version) != PSA_SUCCESS) { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 357 | return PSA_ERROR_CONNECTION_REFUSED; |
| 358 | } |
| 359 | |
| 360 | client_id = tfm_spm_get_client_id(ns_caller); |
| 361 | |
| 362 | /* |
| 363 | * Create connection handle here since it is possible to return the error |
| 364 | * code to client when creation fails. |
| 365 | */ |
| 366 | CRITICAL_SECTION_ENTER(cs_assert); |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 367 | conn_handle = tfm_spm_create_conn_handle(); |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 368 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 369 | if (!conn_handle) { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 370 | return PSA_ERROR_CONNECTION_BUSY; |
| 371 | } |
| 372 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 373 | handle = tfm_spm_to_user_handle(conn_handle); |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 374 | /* No input or output needed for connect message */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 375 | spm_fill_message(conn_handle, service, handle, PSA_IPC_CONNECT, |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 376 | client_id, NULL, 0, NULL, 0, NULL); |
| 377 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 378 | return backend_instance.messaging(service, conn_handle); |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 379 | } |
| 380 | |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 381 | psa_status_t tfm_spm_client_psa_close(psa_handle_t handle) |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 382 | { |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 383 | struct service_t *service; |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 384 | struct conn_handle_t *conn_handle; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 385 | int32_t client_id; |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 386 | bool ns_caller = tfm_spm_is_ns_caller(); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 387 | |
| 388 | /* It will have no effect if called with the NULL handle */ |
| 389 | if (handle == PSA_NULL_HANDLE) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 390 | return PSA_SUCCESS; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 391 | } |
| 392 | |
Mingyang Sun | 00cef5e | 2021-03-04 13:41:56 +0800 | [diff] [blame] | 393 | /* It is a PROGRAMMER ERROR if called with a stateless handle. */ |
Mingyang Sun | e8d3808 | 2021-03-30 18:34:40 +0800 | [diff] [blame] | 394 | if (IS_STATIC_HANDLE(handle)) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 395 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | 00cef5e | 2021-03-04 13:41:56 +0800 | [diff] [blame] | 396 | } |
| 397 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 398 | client_id = tfm_spm_get_client_id(ns_caller); |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 399 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 400 | /* |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 401 | * It is a PROGRAMMER ERROR if an invalid handle was provided that is not |
| 402 | * the null handle. |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 403 | */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 404 | conn_handle = spm_get_handle_by_client_handle(handle, client_id); |
| 405 | if (!conn_handle) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 406 | return PSA_ERROR_PROGRAMMER_ERROR; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 407 | } |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 408 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 409 | service = conn_handle->service; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 410 | if (!service) { |
| 411 | /* FixMe: Need to implement one mechanism to resolve this failure. */ |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 412 | return PSA_ERROR_PROGRAMMER_ERROR; |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 413 | } |
| 414 | |
Shawn Shan | b222d89 | 2021-01-04 17:41:48 +0800 | [diff] [blame] | 415 | /* |
| 416 | * It is a PROGRAMMER ERROR if the connection is currently handling a |
| 417 | * request. |
| 418 | */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 419 | if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) { |
Xinyu Zhang | b287ef8 | 2021-11-03 18:38:50 +0800 | [diff] [blame] | 420 | return PSA_ERROR_PROGRAMMER_ERROR; |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 421 | } |
| 422 | |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 423 | /* No input or output needed for close message */ |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 424 | spm_fill_message(conn_handle, service, handle, PSA_IPC_DISCONNECT, |
| 425 | client_id, NULL, 0, NULL, 0, NULL); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 426 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 427 | return backend_instance.messaging(service, conn_handle); |
David Hu | 733d8f9 | 2019-09-23 15:32:40 +0800 | [diff] [blame] | 428 | } |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 429 | |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 430 | #endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */ |
| 431 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame] | 432 | /* PSA Partition API function body */ |
| 433 | |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 434 | #if CONFIG_TFM_SPM_BACKEND_IPC == 1 \ |
| 435 | || CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 436 | psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask, |
| 437 | uint32_t timeout) |
| 438 | { |
| 439 | struct partition_t *partition = NULL; |
| 440 | |
| 441 | /* |
| 442 | * Timeout[30:0] are reserved for future use. |
| 443 | * SPM must ignore the value of RES. |
| 444 | */ |
| 445 | timeout &= PSA_TIMEOUT_MASK; |
| 446 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 447 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned |
| 451 | * signals. |
| 452 | */ |
| 453 | if ((partition->signals_allowed & signal_mask) == 0) { |
| 454 | tfm_core_panic(); |
| 455 | } |
| 456 | |
| 457 | /* |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 458 | * backend_instance.wait() blocks the caller thread if no signals are |
| 459 | * available. In this case, the return value of this function is temporary |
| 460 | * set into runtime context. After new signal(s) are available, the return |
| 461 | * value is updated with the available signal(s) and blocked thread gets |
| 462 | * to run. |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 463 | */ |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 464 | if (timeout == PSA_BLOCK) { |
| 465 | return backend_instance.wait(partition, signal_mask); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | return partition->signals_asserted & signal_mask; |
| 469 | } |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 470 | #endif |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 471 | |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 472 | #if CONFIG_TFM_SPM_BACKEND_IPC == 1 |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 473 | psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg) |
| 474 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 475 | struct conn_handle_t *handle = NULL; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 476 | struct partition_t *partition = NULL; |
| 477 | uint32_t privileged; |
| 478 | |
| 479 | /* |
| 480 | * Only one message could be retrieved every time for psa_get(). It is a |
| 481 | * fatal error if the input signal has more than a signal bit set. |
| 482 | */ |
| 483 | if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) { |
| 484 | tfm_core_panic(); |
| 485 | } |
| 486 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 487 | partition = GET_CURRENT_COMPONENT(); |
| 488 | |
Kevin Peng | a40d29f | 2022-01-19 14:44:34 +0800 | [diff] [blame] | 489 | privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 490 | |
| 491 | /* |
| 492 | * Write the message to the service buffer. It is a fatal error if the |
| 493 | * input msg pointer is not a valid memory reference or not read-write. |
| 494 | */ |
| 495 | if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW, |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 496 | privileged) != PSA_SUCCESS) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 497 | tfm_core_panic(); |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * It is a fatal error if the caller call psa_get() when no message has |
| 502 | * been set. The caller must call this function after an RoT Service signal |
| 503 | * is returned by psa_wait(). |
| 504 | */ |
| 505 | if (partition->signals_asserted == 0) { |
| 506 | tfm_core_panic(); |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * It is a fatal error if the RoT Service signal is not currently asserted. |
| 511 | */ |
| 512 | if ((partition->signals_asserted & signal) == 0) { |
| 513 | tfm_core_panic(); |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Get message by signal from partition. It is a fatal error if getting |
| 518 | * failed, which means the input signal is not correspond to an RoT service. |
| 519 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 520 | handle = spm_get_handle_by_signal(partition, signal); |
| 521 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 522 | return PSA_ERROR_DOES_NOT_EXIST; |
| 523 | } |
| 524 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 525 | spm_memcpy(msg, &handle->msg, sizeof(psa_msg_t)); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 526 | |
| 527 | return PSA_SUCCESS; |
| 528 | } |
Mingyang Sun | 4609ac7 | 2022-02-08 18:56:35 +0800 | [diff] [blame] | 529 | #endif |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 530 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 531 | size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx, |
| 532 | void *buffer, size_t num_bytes) |
| 533 | { |
| 534 | size_t bytes; |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 535 | struct conn_handle_t *handle = NULL; |
Kevin Peng | a40d29f | 2022-01-19 14:44:34 +0800 | [diff] [blame] | 536 | uint32_t priv_mode; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 537 | |
| 538 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 539 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 540 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 541 | tfm_core_panic(); |
| 542 | } |
| 543 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 544 | priv_mode = GET_PARTITION_PRIVILEGED_MODE( |
| 545 | handle->service->partition->p_ldinf); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * It is a fatal error if message handle does not refer to a request |
| 549 | * message |
| 550 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 551 | if (handle->msg.type < PSA_IPC_CALL) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 552 | tfm_core_panic(); |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * It is a fatal error if invec_idx is equal to or greater than |
| 557 | * PSA_MAX_IOVEC |
| 558 | */ |
| 559 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 560 | tfm_core_panic(); |
| 561 | } |
| 562 | |
| 563 | /* There was no remaining data in this input vector */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 564 | if (handle->msg.in_size[invec_idx] == 0) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 568 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 569 | /* |
| 570 | * It is a fatal error if the input vector has already been mapped using |
| 571 | * psa_map_invec(). |
| 572 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 573 | if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 574 | tfm_core_panic(); |
| 575 | } |
| 576 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 577 | SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 578 | #endif |
| 579 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 580 | /* |
| 581 | * Copy the client data to the service buffer. It is a fatal error |
| 582 | * if the memory reference for buffer is invalid or not read-write. |
| 583 | */ |
| 584 | if (tfm_memory_check(buffer, num_bytes, false, |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 585 | TFM_MEMORY_ACCESS_RW, priv_mode) != PSA_SUCCESS) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 586 | tfm_core_panic(); |
| 587 | } |
| 588 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 589 | bytes = num_bytes > handle->msg.in_size[invec_idx] ? |
| 590 | handle->msg.in_size[invec_idx] : num_bytes; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 591 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 592 | spm_memcpy(buffer, handle->invec[invec_idx].base, bytes); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 593 | |
| 594 | /* There maybe some remaining data */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 595 | handle->invec[invec_idx].base = |
| 596 | (char *)handle->invec[invec_idx].base + bytes; |
| 597 | handle->msg.in_size[invec_idx] -= bytes; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 598 | |
| 599 | return bytes; |
| 600 | } |
| 601 | |
| 602 | size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx, |
| 603 | size_t num_bytes) |
| 604 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 605 | struct conn_handle_t *handle = NULL; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 606 | |
| 607 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 608 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 609 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 610 | tfm_core_panic(); |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * It is a fatal error if message handle does not refer to a request |
| 615 | * message |
| 616 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 617 | if (handle->msg.type < PSA_IPC_CALL) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 618 | tfm_core_panic(); |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * It is a fatal error if invec_idx is equal to or greater than |
| 623 | * PSA_MAX_IOVEC |
| 624 | */ |
| 625 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 626 | tfm_core_panic(); |
| 627 | } |
| 628 | |
| 629 | /* There was no remaining data in this input vector */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 630 | if (handle->msg.in_size[invec_idx] == 0) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 631 | return 0; |
| 632 | } |
| 633 | |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 634 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 635 | /* |
| 636 | * It is a fatal error if the input vector has already been mapped using |
| 637 | * psa_map_invec(). |
| 638 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 639 | if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 640 | tfm_core_panic(); |
| 641 | } |
| 642 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 643 | SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 644 | #endif |
| 645 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 646 | /* |
| 647 | * If num_bytes is greater than the remaining size of the input vector then |
| 648 | * the remaining size of the input vector is used. |
| 649 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 650 | if (num_bytes > handle->msg.in_size[invec_idx]) { |
| 651 | num_bytes = handle->msg.in_size[invec_idx]; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /* There maybe some remaining data */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 655 | handle->invec[invec_idx].base = |
| 656 | (char *)handle->invec[invec_idx].base + num_bytes; |
| 657 | handle->msg.in_size[invec_idx] -= num_bytes; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 658 | |
| 659 | return num_bytes; |
| 660 | } |
| 661 | |
| 662 | void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx, |
| 663 | const void *buffer, size_t num_bytes) |
| 664 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 665 | struct conn_handle_t *handle = NULL; |
Kevin Peng | a40d29f | 2022-01-19 14:44:34 +0800 | [diff] [blame] | 666 | uint32_t priv_mode; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 667 | |
| 668 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 669 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 670 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 671 | tfm_core_panic(); |
| 672 | } |
| 673 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 674 | priv_mode = GET_PARTITION_PRIVILEGED_MODE( |
| 675 | handle->service->partition->p_ldinf); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 676 | |
| 677 | /* |
| 678 | * It is a fatal error if message handle does not refer to a request |
| 679 | * message |
| 680 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 681 | if (handle->msg.type < PSA_IPC_CALL) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 682 | tfm_core_panic(); |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * It is a fatal error if outvec_idx is equal to or greater than |
| 687 | * PSA_MAX_IOVEC |
| 688 | */ |
| 689 | if (outvec_idx >= PSA_MAX_IOVEC) { |
| 690 | tfm_core_panic(); |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * It is a fatal error if the call attempts to write data past the end of |
| 695 | * the client output vector |
| 696 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 697 | if (num_bytes > handle->msg.out_size[outvec_idx] - |
| 698 | handle->outvec[outvec_idx].len) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 699 | tfm_core_panic(); |
| 700 | } |
| 701 | |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 702 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 703 | /* |
| 704 | * It is a fatal error if the output vector has already been mapped using |
| 705 | * psa_map_outvec(). |
| 706 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 707 | if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 708 | tfm_core_panic(); |
| 709 | } |
| 710 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 711 | SET_IOVEC_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 712 | #endif |
| 713 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 714 | /* |
| 715 | * Copy the service buffer to client outvecs. It is a fatal error |
| 716 | * if the memory reference for buffer is invalid or not readable. |
| 717 | */ |
| 718 | if (tfm_memory_check(buffer, num_bytes, false, |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 719 | TFM_MEMORY_ACCESS_RO, priv_mode) != PSA_SUCCESS) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 720 | tfm_core_panic(); |
| 721 | } |
| 722 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 723 | spm_memcpy((char *)handle->outvec[outvec_idx].base + |
| 724 | handle->outvec[outvec_idx].len, buffer, num_bytes); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 725 | |
| 726 | /* Update the write number */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 727 | handle->outvec[outvec_idx].len += num_bytes; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 728 | } |
| 729 | |
Ken Liu | f8c7e53 | 2022-02-10 15:03:04 +0800 | [diff] [blame] | 730 | psa_status_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle, |
| 731 | psa_status_t status) |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 732 | { |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 733 | struct service_t *service; |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 734 | struct conn_handle_t *handle; |
Ken Liu | f8c7e53 | 2022-02-10 15:03:04 +0800 | [diff] [blame] | 735 | psa_status_t ret = PSA_SUCCESS; |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 736 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 737 | |
| 738 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 739 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 740 | if (!handle) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 741 | tfm_core_panic(); |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * RoT Service information is needed in this function, stored it in message |
| 746 | * body structure. Only two parameters are passed in this function: handle |
| 747 | * and status, so it is useful and simply to do like this. |
| 748 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 749 | service = handle->service; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 750 | if (!service) { |
| 751 | tfm_core_panic(); |
| 752 | } |
| 753 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 754 | switch (handle->msg.type) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 755 | case PSA_IPC_CONNECT: |
| 756 | /* |
| 757 | * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the |
| 758 | * input status is PSA_SUCCESS. Others return values are based on the |
| 759 | * input status. |
| 760 | */ |
| 761 | if (status == PSA_SUCCESS) { |
| 762 | ret = msg_handle; |
| 763 | } else if (status == PSA_ERROR_CONNECTION_REFUSED) { |
| 764 | /* Refuse the client connection, indicating a permanent error. */ |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 765 | ret = PSA_ERROR_CONNECTION_REFUSED; |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 766 | handle->status = TFM_HANDLE_STATUS_TO_FREE; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 767 | } else if (status == PSA_ERROR_CONNECTION_BUSY) { |
| 768 | /* Fail the client connection, indicating a transient error. */ |
| 769 | ret = PSA_ERROR_CONNECTION_BUSY; |
| 770 | } else { |
| 771 | tfm_core_panic(); |
| 772 | } |
| 773 | break; |
| 774 | case PSA_IPC_DISCONNECT: |
| 775 | /* Service handle is not used anymore */ |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 776 | handle->status = TFM_HANDLE_STATUS_TO_FREE; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 777 | |
| 778 | /* |
| 779 | * If the message type is PSA_IPC_DISCONNECT, then the status code is |
| 780 | * ignored |
| 781 | */ |
| 782 | break; |
| 783 | default: |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 784 | if (handle->msg.type >= PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 785 | |
| 786 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 787 | |
| 788 | /* |
| 789 | * If the unmapped function is not called for an input/output vector |
| 790 | * that has been mapped, the framework will remove the mapping. |
| 791 | */ |
| 792 | int i; |
| 793 | |
| 794 | for (i = 0; i < PSA_MAX_IOVEC * 2; i++) { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 795 | if (IOVEC_IS_MAPPED(handle, i) && |
| 796 | (!IOVEC_IS_UNMAPPED(handle, i))) { |
| 797 | SET_IOVEC_UNMAPPED(handle, i); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 798 | /* |
| 799 | * Any output vectors that are still mapped will report that |
| 800 | * zero bytes have been written. |
| 801 | */ |
| 802 | if (i >= OUTVEC_IDX_BASE) { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 803 | handle->outvec[i - OUTVEC_IDX_BASE].len = 0; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | #endif |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 809 | /* Reply to a request message. Return values are based on status */ |
| 810 | ret = status; |
| 811 | /* |
| 812 | * The total number of bytes written to a single parameter must be |
| 813 | * reported to the client by updating the len member of the |
| 814 | * psa_outvec structure for the parameter before returning from |
| 815 | * psa_call(). |
| 816 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 817 | update_caller_outvec_len(handle); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 818 | if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) { |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 819 | handle->status = TFM_HANDLE_STATUS_TO_FREE; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 820 | } |
| 821 | } else { |
| 822 | tfm_core_panic(); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | if (ret == PSA_ERROR_PROGRAMMER_ERROR) { |
| 827 | /* |
| 828 | * If the source of the programmer error is a Secure Partition, the SPM |
| 829 | * must panic the Secure Partition in response to a PROGRAMMER ERROR. |
| 830 | */ |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 831 | if (!TFM_CLIENT_ID_IS_NS(handle->msg.client_id)) { |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 832 | tfm_core_panic(); |
| 833 | } |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 834 | } |
| 835 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 836 | /* |
| 837 | * TODO: It can be optimized further by moving critical section protection |
| 838 | * to mailbox. Also need to check implementation when secure context is |
| 839 | * involved. |
| 840 | */ |
| 841 | CRITICAL_SECTION_ENTER(cs_assert); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 842 | ret = backend_instance.replying(handle, ret); |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 843 | CRITICAL_SECTION_LEAVE(cs_assert); |
| 844 | |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 845 | if (handle->status == TFM_HANDLE_STATUS_TO_FREE) { |
| 846 | tfm_spm_free_conn_handle(handle); |
| 847 | } else { |
| 848 | handle->status = TFM_HANDLE_STATUS_IDLE; |
| 849 | } |
| 850 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 851 | return ret; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 852 | } |
| 853 | |
Kevin Peng | 613b417 | 2022-02-15 14:41:44 +0800 | [diff] [blame] | 854 | #if CONFIG_TFM_DOORBELL_API == 1 |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 855 | void tfm_spm_partition_psa_notify(int32_t partition_id) |
| 856 | { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 857 | struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id); |
| 858 | |
| 859 | spm_assert_signal(p_pt, PSA_DOORBELL); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | void tfm_spm_partition_psa_clear(void) |
| 863 | { |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 864 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 865 | struct partition_t *partition = NULL; |
| 866 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 867 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 868 | |
| 869 | /* |
| 870 | * It is a fatal error if the Secure Partition's doorbell signal is not |
| 871 | * currently asserted. |
| 872 | */ |
| 873 | if ((partition->signals_asserted & PSA_DOORBELL) == 0) { |
| 874 | tfm_core_panic(); |
| 875 | } |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 876 | |
| 877 | CRITICAL_SECTION_ENTER(cs_assert); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 878 | partition->signals_asserted &= ~PSA_DOORBELL; |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 879 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 880 | } |
Kevin Peng | 613b417 | 2022-02-15 14:41:44 +0800 | [diff] [blame] | 881 | #endif /* CONFIG_TFM_DOORBELL_API == 1 */ |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 882 | |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 883 | void tfm_spm_partition_psa_panic(void) |
| 884 | { |
Joakim Andersson | 97cef7e | 2022-03-18 10:22:00 +0100 | [diff] [blame] | 885 | #ifdef CONFIG_TFM_HALT_ON_CORE_PANIC |
| 886 | tfm_hal_system_halt(); |
| 887 | #else |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 888 | /* |
| 889 | * PSA FF recommends that the SPM causes the system to restart when a secure |
| 890 | * partition panics. |
| 891 | */ |
| 892 | tfm_hal_system_reset(); |
Joakim Andersson | 97cef7e | 2022-03-18 10:22:00 +0100 | [diff] [blame] | 893 | #endif |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 894 | } |
| 895 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 896 | /* psa_set_rhandle is only needed by connection-based services */ |
| 897 | #if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1 |
| 898 | |
| 899 | void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle) |
| 900 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 901 | struct conn_handle_t *handle; |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 902 | |
| 903 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 904 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 905 | if (!handle) { |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 906 | tfm_core_panic(); |
| 907 | } |
| 908 | |
| 909 | /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 910 | if (SERVICE_IS_STATELESS(handle->service->p_ldinf->flags)) { |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 911 | tfm_core_panic(); |
| 912 | } |
| 913 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 914 | handle->msg.rhandle = rhandle; |
| 915 | handle->rhandle = rhandle; |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | #endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */ |
| 919 | |
| 920 | #if CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 |
Kevin Peng | 67a89fd | 2021-11-25 11:22:02 +0800 | [diff] [blame] | 921 | void tfm_spm_partition_psa_irq_enable(psa_signal_t irq_signal) |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 922 | { |
| 923 | struct partition_t *partition; |
| 924 | struct irq_load_info_t *irq_info; |
| 925 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 926 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 927 | |
| 928 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 929 | if (!irq_info) { |
| 930 | tfm_core_panic(); |
| 931 | } |
| 932 | |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 933 | tfm_hal_irq_enable(irq_info->source); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 934 | } |
| 935 | |
Kevin Peng | 67a89fd | 2021-11-25 11:22:02 +0800 | [diff] [blame] | 936 | 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] | 937 | { |
| 938 | struct partition_t *partition; |
| 939 | struct irq_load_info_t *irq_info; |
| 940 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 941 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 942 | |
| 943 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 944 | if (!irq_info) { |
| 945 | tfm_core_panic(); |
| 946 | } |
| 947 | |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 948 | tfm_hal_irq_disable(irq_info->source); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 949 | |
| 950 | return 1; |
| 951 | } |
| 952 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 953 | /* This API is only used for FLIH. */ |
| 954 | #if CONFIG_TFM_FLIH_API == 1 |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 955 | void tfm_spm_partition_psa_reset_signal(psa_signal_t irq_signal) |
| 956 | { |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 957 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 958 | struct irq_load_info_t *irq_info; |
| 959 | struct partition_t *partition; |
| 960 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 961 | partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 962 | |
| 963 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 964 | if (!irq_info) { |
| 965 | tfm_core_panic(); |
| 966 | } |
| 967 | |
| 968 | if (!irq_info->flih_func) { |
| 969 | /* This API is for FLIH IRQs only */ |
| 970 | tfm_core_panic(); |
| 971 | } |
| 972 | |
| 973 | if ((partition->signals_asserted & irq_signal) == 0) { |
| 974 | /* The signal is not asserted */ |
| 975 | tfm_core_panic(); |
| 976 | } |
| 977 | |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 978 | CRITICAL_SECTION_ENTER(cs_assert); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 979 | partition->signals_asserted &= ~irq_signal; |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 980 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | b26b280 | 2021-07-07 11:25:00 +0800 | [diff] [blame] | 981 | } |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 982 | #endif |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 983 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 984 | /* This API is only used for SLIH. */ |
| 985 | #if CONFIG_TFM_SLIH_API == 1 |
| 986 | void tfm_spm_partition_psa_eoi(psa_signal_t irq_signal) |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 987 | { |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 988 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
| 989 | struct irq_load_info_t *irq_info = NULL; |
| 990 | struct partition_t *partition = NULL; |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 991 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 992 | partition = GET_CURRENT_COMPONENT(); |
| 993 | |
| 994 | irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal); |
| 995 | /* It is a fatal error if passed signal is not an interrupt signal. */ |
| 996 | if (!irq_info) { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 997 | tfm_core_panic(); |
| 998 | } |
| 999 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 1000 | if (irq_info->flih_func) { |
| 1001 | /* This API is for SLIH IRQs only */ |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 1002 | tfm_core_panic(); |
| 1003 | } |
| 1004 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 1005 | /* It is a fatal error if passed signal is not currently asserted */ |
| 1006 | if ((partition->signals_asserted & irq_signal) == 0) { |
| 1007 | tfm_core_panic(); |
| 1008 | } |
| 1009 | |
| 1010 | CRITICAL_SECTION_ENTER(cs_assert); |
| 1011 | partition->signals_asserted &= ~irq_signal; |
| 1012 | CRITICAL_SECTION_LEAVE(cs_assert); |
| 1013 | |
| 1014 | tfm_hal_irq_clear_pending(irq_info->source); |
| 1015 | tfm_hal_irq_enable(irq_info->source); |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 1016 | } |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 1017 | #endif |
| 1018 | #endif /* CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 */ |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 1019 | |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1020 | #if PSA_FRAMEWORK_HAS_MM_IOVEC |
| 1021 | |
| 1022 | const void *tfm_spm_partition_psa_map_invec(psa_handle_t msg_handle, |
| 1023 | uint32_t invec_idx) |
| 1024 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1025 | struct conn_handle_t *handle; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1026 | uint32_t privileged; |
| 1027 | struct partition_t *partition = NULL; |
| 1028 | |
| 1029 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 1030 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1031 | if (!handle) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1032 | tfm_core_panic(); |
| 1033 | } |
| 1034 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1035 | partition = handle->service->partition; |
Kevin Peng | a40d29f | 2022-01-19 14:44:34 +0800 | [diff] [blame] | 1036 | privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1037 | |
| 1038 | /* |
| 1039 | * It is a fatal error if MM-IOVEC has not been enabled for the RoT |
| 1040 | * Service that received the message. |
| 1041 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1042 | if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1043 | tfm_core_panic(); |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * It is a fatal error if message handle does not refer to a request |
| 1048 | * message. |
| 1049 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1050 | if (handle->msg.type < PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1051 | tfm_core_panic(); |
| 1052 | } |
| 1053 | |
| 1054 | /* |
| 1055 | * It is a fatal error if invec_idx is equal to or greater than |
| 1056 | * PSA_MAX_IOVEC. |
| 1057 | */ |
| 1058 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 1059 | tfm_core_panic(); |
| 1060 | } |
| 1061 | |
| 1062 | /* It is a fatal error if the input vector has length zero. */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1063 | if (handle->msg.in_size[invec_idx] == 0) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1064 | tfm_core_panic(); |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | * It is a fatal error if the input vector has already been mapped using |
| 1069 | * psa_map_invec(). |
| 1070 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1071 | if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1072 | tfm_core_panic(); |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * It is a fatal error if the input vector has already been accessed |
| 1077 | * using psa_read() or psa_skip(). |
| 1078 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1079 | if (IOVEC_IS_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1080 | tfm_core_panic(); |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * It is a fatal error if the memory reference for the wrap input vector is |
| 1085 | * invalid or not readable. |
| 1086 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1087 | if (tfm_memory_check(handle->invec[invec_idx].base, |
| 1088 | handle->invec[invec_idx].len, |
| 1089 | false, TFM_MEMORY_ACCESS_RO, |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 1090 | privileged) != PSA_SUCCESS) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1091 | tfm_core_panic(); |
| 1092 | } |
| 1093 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1094 | SET_IOVEC_MAPPED(handle, (invec_idx + INVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1095 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1096 | return handle->invec[invec_idx].base; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle, |
| 1100 | uint32_t invec_idx) |
| 1101 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1102 | struct conn_handle_t *handle; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1103 | |
| 1104 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 1105 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1106 | if (!handle) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1107 | tfm_core_panic(); |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * It is a fatal error if MM-IOVEC has not been enabled for the RoT |
| 1112 | * Service that received the message. |
| 1113 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1114 | if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1115 | tfm_core_panic(); |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * It is a fatal error if message handle does not refer to a request |
| 1120 | * message. |
| 1121 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1122 | if (handle->msg.type < PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1123 | tfm_core_panic(); |
| 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * It is a fatal error if invec_idx is equal to or greater than |
| 1128 | * PSA_MAX_IOVEC. |
| 1129 | */ |
| 1130 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 1131 | tfm_core_panic(); |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * It is a fatal error if The input vector has not been mapped by a call to |
| 1136 | * psa_map_invec(). |
| 1137 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1138 | if (!IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1139 | tfm_core_panic(); |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * It is a fatal error if the input vector has already been unmapped by a |
| 1144 | * call to psa_unmap_invec(). |
| 1145 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1146 | if (IOVEC_IS_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1147 | tfm_core_panic(); |
| 1148 | } |
| 1149 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1150 | SET_IOVEC_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle, |
| 1154 | uint32_t outvec_idx) |
| 1155 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1156 | struct conn_handle_t *handle; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1157 | uint32_t privileged; |
| 1158 | struct partition_t *partition = NULL; |
| 1159 | |
| 1160 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 1161 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1162 | if (!handle) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1163 | tfm_core_panic(); |
| 1164 | } |
| 1165 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1166 | partition = handle->service->partition; |
Kevin Peng | a40d29f | 2022-01-19 14:44:34 +0800 | [diff] [blame] | 1167 | privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1168 | |
| 1169 | /* |
| 1170 | * It is a fatal error if MM-IOVEC has not been enabled for the RoT |
| 1171 | * Service that received the message. |
| 1172 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1173 | if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1174 | tfm_core_panic(); |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * It is a fatal error if message handle does not refer to a request |
| 1179 | * message. |
| 1180 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1181 | if (handle->msg.type < PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1182 | tfm_core_panic(); |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * It is a fatal error if outvec_idx is equal to or greater than |
| 1187 | * PSA_MAX_IOVEC. |
| 1188 | */ |
| 1189 | if (outvec_idx >= PSA_MAX_IOVEC) { |
| 1190 | tfm_core_panic(); |
| 1191 | } |
| 1192 | |
| 1193 | /* It is a fatal error if the output vector has length zero. */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1194 | if (handle->msg.out_size[outvec_idx] == 0) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1195 | tfm_core_panic(); |
| 1196 | } |
| 1197 | |
| 1198 | /* |
| 1199 | * It is a fatal error if the output vector has already been mapped using |
| 1200 | * psa_map_outvec(). |
| 1201 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1202 | if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1203 | tfm_core_panic(); |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * It is a fatal error if the output vector has already been accessed |
| 1208 | * using psa_write(). |
| 1209 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1210 | if (IOVEC_IS_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1211 | tfm_core_panic(); |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * It is a fatal error if the output vector is invalid or not read-write. |
| 1216 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1217 | if (tfm_memory_check(handle->outvec[outvec_idx].base, |
| 1218 | handle->outvec[outvec_idx].len, false, |
Ken Liu | ad3f3e6 | 2022-03-04 22:23:52 +0800 | [diff] [blame] | 1219 | TFM_MEMORY_ACCESS_RW, privileged) != PSA_SUCCESS) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1220 | tfm_core_panic(); |
| 1221 | } |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1222 | SET_IOVEC_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1223 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1224 | return handle->outvec[outvec_idx].base; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle, |
| 1228 | uint32_t outvec_idx, size_t len) |
| 1229 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1230 | struct conn_handle_t *handle; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1231 | |
| 1232 | /* It is a fatal error if message handle is invalid */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 1233 | handle = spm_get_handle_by_msg_handle(msg_handle); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1234 | if (!handle) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1235 | tfm_core_panic(); |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * It is a fatal error if MM-IOVEC has not been enabled for the RoT |
| 1240 | * Service that received the message. |
| 1241 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1242 | if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1243 | tfm_core_panic(); |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * It is a fatal error if message handle does not refer to a request |
| 1248 | * message. |
| 1249 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1250 | if (handle->msg.type < PSA_IPC_CALL) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1251 | tfm_core_panic(); |
| 1252 | } |
| 1253 | |
| 1254 | /* |
| 1255 | * It is a fatal error if outvec_idx is equal to or greater than |
| 1256 | * PSA_MAX_IOVEC. |
| 1257 | */ |
| 1258 | if (outvec_idx >= PSA_MAX_IOVEC) { |
| 1259 | tfm_core_panic(); |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * It is a fatal error if len is greater than the output vector size. |
| 1264 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1265 | if (len > handle->msg.out_size[outvec_idx]) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1266 | tfm_core_panic(); |
| 1267 | } |
| 1268 | |
| 1269 | /* |
| 1270 | * It is a fatal error if The output vector has not been mapped by a call to |
| 1271 | * psa_map_outvec(). |
| 1272 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1273 | if (!IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1274 | tfm_core_panic(); |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * It is a fatal error if the output vector has already been unmapped by a |
| 1279 | * call to psa_unmap_outvec(). |
| 1280 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1281 | if (IOVEC_IS_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) { |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1282 | tfm_core_panic(); |
| 1283 | } |
| 1284 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1285 | SET_IOVEC_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE)); |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1286 | |
| 1287 | /* Update the write number */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 1288 | handle->outvec[outvec_idx].len = len; |
Shawn Shan | 038348e | 2021-09-08 17:11:04 +0800 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | #endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */ |