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