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