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