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