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