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