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