blob: a93b2c90414510814476a8a42a2a5aabbec75246 [file] [log] [blame]
David Hu733d8f92019-09-23 15:32:40 +08001/*
Mingyang Sunbb4a42a2021-12-14 15:18:52 +08002 * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
David Hu733d8f92019-09-23 15:32:40 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Mingyang Suneeca4652021-07-15 15:19:16 +08008#include <stdint.h>
Mingyang Sunb26b2802021-07-07 11:25:00 +08009#include "bitops.h"
Mingyang Sun4609ac72022-02-08 18:56:35 +080010#include "config_impl.h"
Ken Liu92ede9f2021-10-20 09:35:00 +080011#include "critical_section.h"
Mingyang Suneeca4652021-07-15 15:19:16 +080012#include "psa/lifecycle.h"
David Hu733d8f92019-09-23 15:32:40 +080013#include "psa/service.h"
Kevin Peng3f67b2e2021-10-18 17:47:27 +080014#include "interrupt.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080015#include "spm_ipc.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080016#include "tfm_arch.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080017#include "load/partition_defs.h"
Mingyang Sun2b352662021-04-21 11:35:43 +080018#include "load/service_defs.h"
Ken Liu3dd92562021-08-17 16:22:54 +080019#include "load/interrupt_defs.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080020#include "ffm/psa_api.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080021#include "utilities.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080022#include "ffm/backend.h"
Ken Liue07c3b72021-10-14 16:19:13 +080023#include "ffm/psa_api.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080024#include "tfm_rpc.h"
Kevin Pengb42ed862022-08-08 14:44:02 +080025#include "tfm_api.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080026#include "tfm_hal_interrupt.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080027#include "tfm_hal_platform.h"
Ken Liu82e3eac2021-10-14 16:19:13 +080028#include "tfm_psa_call_pack.h"
Summer Qin56725eb2022-05-06 15:23:40 +080029#include "tfm_hal_isolation.h"
David Hu733d8f92019-09-23 15:32:40 +080030
Ken Liub3b2cb62021-05-22 00:39:28 +080031#define GET_STATELESS_SERVICE(index) (stateless_services_ref_tbl[index])
Xinyu Zhanga38e9b52021-06-02 17:48:01 +080032extern struct service_t *stateless_services_ref_tbl[];
Mingyang Suncb6f70e2021-03-05 23:30:25 +080033
Shawn Shan038348e2021-09-08 17:11:04 +080034#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 Suna09adda2022-02-16 18:11:33 +080069#define IOVEC_IS_MAPPED(handle, iovec_idx) \
70 ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
Shawn Shan038348e2021-09-08 17:11:04 +080071 IOVEC_MAPPED_BIT)
Mingyang Suna09adda2022-02-16 18:11:33 +080072#define IOVEC_IS_UNMAPPED(handle, iovec_idx) \
73 ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
Shawn Shan038348e2021-09-08 17:11:04 +080074 IOVEC_UNMAPPED_BIT)
Mingyang Suna09adda2022-02-16 18:11:33 +080075#define IOVEC_IS_ACCESSED(handle, iovec_idx) \
76 ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
Shawn Shan038348e2021-09-08 17:11:04 +080077 IOVEC_ACCESSED_BIT)
Mingyang Suna09adda2022-02-16 18:11:33 +080078#define SET_IOVEC_MAPPED(handle, iovec_idx) \
79 (((handle)->iovec_status) |= (IOVEC_MAPPED_BIT << \
Shawn Shan038348e2021-09-08 17:11:04 +080080 ((iovec_idx) * IOVEC_STATUS_BITS)))
Mingyang Suna09adda2022-02-16 18:11:33 +080081#define SET_IOVEC_UNMAPPED(handle, iovec_idx) \
82 (((handle)->iovec_status) |= (IOVEC_UNMAPPED_BIT << \
Shawn Shan038348e2021-09-08 17:11:04 +080083 ((iovec_idx) * IOVEC_STATUS_BITS)))
Mingyang Suna09adda2022-02-16 18:11:33 +080084#define SET_IOVEC_ACCESSED(handle, iovec_idx) \
85 (((handle)->iovec_status) |= (IOVEC_ACCESSED_BIT << \
Shawn Shan038348e2021-09-08 17:11:04 +080086 ((iovec_idx) * IOVEC_STATUS_BITS)))
87
88#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */
89
Xinyu Zhangb287ef82021-11-03 18:38:50 +080090void spm_handle_programmer_errors(psa_status_t status)
91{
Summer Qind9d497b2022-04-14 14:39:26 +080092 if (status == PSA_ERROR_PROGRAMMER_ERROR ||
93 status == PSA_ERROR_CONNECTION_REFUSED) {
94 if (!tfm_spm_is_ns_caller()) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +080095 tfm_core_panic();
Summer Qind9d497b2022-04-14 14:39:26 +080096 }
Xinyu Zhangb287ef82021-11-03 18:38:50 +080097 }
98}
99
Mingyang Suneeca4652021-07-15 15:19:16 +0800100uint32_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 Sund44522a2020-01-16 16:48:37 +0800111uint32_t tfm_spm_client_psa_framework_version(void)
David Hu733d8f92019-09-23 15:32:40 +0800112{
113 return PSA_FRAMEWORK_VERSION;
114}
115
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800116uint32_t tfm_spm_client_psa_version(uint32_t sid)
David Hu733d8f92019-09-23 15:32:40 +0800117{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800118 struct service_t *service;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800119 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800120
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 Shan2365c902019-12-19 18:35:36 +0800131 * It should return PSA_VERSION_NONE if the caller is not authorized
132 * to access the RoT Service.
David Hu733d8f92019-09-23 15:32:40 +0800133 */
Ken Liuad3f3e62022-03-04 22:23:52 +0800134 if (tfm_spm_check_authorization(sid, service, ns_caller) != PSA_SUCCESS) {
Shawn Shan2365c902019-12-19 18:35:36 +0800135 return PSA_VERSION_NONE;
David Hu733d8f92019-09-23 15:32:40 +0800136 }
137
Ken Liuacd2a572021-05-12 16:19:04 +0800138 return service->p_ldinf->version;
David Hu733d8f92019-09-23 15:32:40 +0800139}
140
Mingyang Suneeca4652021-07-15 15:19:16 +0800141psa_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 Hu733d8f92019-09-23 15:32:40 +0800145{
146 psa_invec invecs[PSA_MAX_IOVEC];
147 psa_outvec outvecs[PSA_MAX_IOVEC];
Ken Liu0bed7e02022-02-10 12:38:07 +0800148 struct conn_handle_t *conn_handle;
Mingyang Sun783a59b2021-04-20 15:52:18 +0800149 struct service_t *service;
Summer Qinba2346e2019-11-12 16:26:31 +0800150 int i, j;
Summer Qin1ce712a2019-10-14 18:04:05 +0800151 int32_t client_id;
Mingyang Sun453ad402021-03-17 17:58:33 +0800152 uint32_t sid, version, index;
Mingyang Sun620c8562021-11-10 11:44:58 +0800153 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800154 bool ns_caller = tfm_spm_is_ns_caller();
Summer Qin56725eb2022-05-06 15:23:40 +0800155 struct partition_t *curr_partition = GET_CURRENT_COMPONENT();
Mingyang Suneeca4652021-07-15 15:19:16 +0800156 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);
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800159
160 /* The request type must be zero or positive. */
161 if (type < 0) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800162 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800163 }
David Hu733d8f92019-09-23 15:32:40 +0800164
Shawn Shanb222d892021-01-04 17:41:48 +0800165 /* It is a PROGRAMMER ERROR if in_len + out_len > PSA_MAX_IOVEC. */
Summer Qin22c301a2022-04-27 17:36:00 +0800166 if ((in_num > SIZE_MAX - out_num) ||
David Hu733d8f92019-09-23 15:32:40 +0800167 (in_num + out_num > PSA_MAX_IOVEC)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800168 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800169 }
170
Mingyang Suncf0b1a52022-03-28 17:29:03 +0800171 /* It is a PROGRAMMER ERROR if the handle is a null handle. */
172 if (handle == PSA_NULL_HANDLE) {
173 return PSA_ERROR_PROGRAMMER_ERROR;
174 }
175
Kevin Peng385fda82021-08-18 10:41:19 +0800176 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800177
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800178 /* Allocate space from handle pool for static handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800179 if (IS_STATIC_HANDLE(handle)) {
Mingyang Sun453ad402021-03-17 17:58:33 +0800180 index = GET_INDEX_FROM_STATIC_HANDLE(handle);
Mingyang Sune8d38082021-03-30 18:34:40 +0800181
182 if (!IS_VALID_STATIC_HANDLE_IDX(index)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800183 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sune8d38082021-03-30 18:34:40 +0800184 }
185
Mingyang Sun453ad402021-03-17 17:58:33 +0800186 service = GET_STATELESS_SERVICE(index);
Mingyang Sun86213242021-07-14 10:26:43 +0800187 if (!service) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800188 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun86213242021-07-14 10:26:43 +0800189 }
190
Ken Liub3b2cb62021-05-22 00:39:28 +0800191 sid = service->p_ldinf->sid;
Mingyang Sun453ad402021-03-17 17:58:33 +0800192
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800193 /*
194 * It is a PROGRAMMER ERROR if the caller is not authorized to access
195 * the RoT Service.
196 */
197 if (tfm_spm_check_authorization(sid, service, ns_caller)
Ken Liuad3f3e62022-03-04 22:23:52 +0800198 != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800199 return PSA_ERROR_CONNECTION_REFUSED;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800200 }
201
Mingyang Sun453ad402021-03-17 17:58:33 +0800202 version = GET_VERSION_FROM_STATIC_HANDLE(handle);
203
Ken Liuad3f3e62022-03-04 22:23:52 +0800204 if (tfm_spm_check_client_version(service, version) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800205 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun453ad402021-03-17 17:58:33 +0800206 }
207
Mingyang Sun620c8562021-11-10 11:44:58 +0800208 CRITICAL_SECTION_ENTER(cs_assert);
Kevin Penge61e7052022-01-27 14:57:06 +0800209 conn_handle = tfm_spm_create_conn_handle();
Mingyang Sun620c8562021-11-10 11:44:58 +0800210 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800211
212 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800213 return PSA_ERROR_CONNECTION_BUSY;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800214 }
215
Mingyang Sun6d5dc3d2021-03-15 15:34:44 +0800216 conn_handle->rhandle = NULL;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800217 handle = tfm_spm_to_user_handle(conn_handle);
218 } else {
Sherry Zhanga4575f32022-02-23 15:45:51 +0800219#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800220 /* It is a PROGRAMMER ERROR if an invalid handle was passed. */
Kevin Penge61e7052022-01-27 14:57:06 +0800221 conn_handle = spm_get_handle_by_client_handle(handle, client_id);
222 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800223 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800224 }
225
226 /*
227 * It is a PROGRAMMER ERROR if the connection is currently
228 * handling a request.
229 */
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800230 if (conn_handle->status != TFM_HANDLE_STATUS_IDLE) {
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800231 return PSA_ERROR_PROGRAMMER_ERROR;
232 }
233
Ken Liu0bed7e02022-02-10 12:38:07 +0800234 service = conn_handle->service;
Shawn Shanb222d892021-01-04 17:41:48 +0800235
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800236 if (!service) {
237 /* FixMe: Need to implement a mechanism to resolve this failure. */
238 return PSA_ERROR_PROGRAMMER_ERROR;
239 }
Sherry Zhanga4575f32022-02-23 15:45:51 +0800240#else
241 return PSA_ERROR_PROGRAMMER_ERROR;
242#endif
David Hu733d8f92019-09-23 15:32:40 +0800243 }
244
Kevin Pengedb8ee42021-03-09 16:50:11 +0800245 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800246 * Read client invecs from the wrap input vector. It is a PROGRAMMER ERROR
David Hu733d8f92019-09-23 15:32:40 +0800247 * if the memory reference for the wrap input vector is invalid or not
248 * readable.
249 */
Summer Qin56725eb2022-05-06 15:23:40 +0800250 if (tfm_hal_memory_check(curr_partition->boundary,
251 (uintptr_t)inptr, in_num * sizeof(psa_invec),
252 TFM_HAL_ACCESS_READABLE) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800253 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800254 }
Summer Qinba2346e2019-11-12 16:26:31 +0800255
David Hu733d8f92019-09-23 15:32:40 +0800256 /*
257 * Read client outvecs from the wrap output vector and will update the
Shawn Shanb222d892021-01-04 17:41:48 +0800258 * actual length later. It is a PROGRAMMER ERROR if the memory reference for
David Hu733d8f92019-09-23 15:32:40 +0800259 * the wrap output vector is invalid or not read-write.
260 */
Summer Qin56725eb2022-05-06 15:23:40 +0800261 if (tfm_hal_memory_check(curr_partition->boundary, (uintptr_t)outptr,
262 out_num * sizeof(psa_outvec), TFM_HAL_ACCESS_READWRITE) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800263 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800264 }
265
Summer Qinf24dbb52020-07-23 14:53:54 +0800266 spm_memset(invecs, 0, sizeof(invecs));
267 spm_memset(outvecs, 0, sizeof(outvecs));
David Hu733d8f92019-09-23 15:32:40 +0800268
269 /* Copy the address out to avoid TOCTOU attacks. */
Summer Qinf24dbb52020-07-23 14:53:54 +0800270 spm_memcpy(invecs, inptr, in_num * sizeof(psa_invec));
271 spm_memcpy(outvecs, outptr, out_num * sizeof(psa_outvec));
David Hu733d8f92019-09-23 15:32:40 +0800272
273 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800274 * For client input vector, it is a PROGRAMMER ERROR if the provided payload
David Hu733d8f92019-09-23 15:32:40 +0800275 * memory reference was invalid or not readable.
276 */
277 for (i = 0; i < in_num; i++) {
Summer Qin56725eb2022-05-06 15:23:40 +0800278 if (tfm_hal_memory_check(curr_partition->boundary,
279 (uintptr_t)invecs[i].base, invecs[i].len,
280 TFM_HAL_ACCESS_READABLE) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800281 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800282 }
283 }
Summer Qinba2346e2019-11-12 16:26:31 +0800284
285 /*
286 * Clients must never overlap input parameters because of the risk of a
287 * double-fetch inconsistency.
Summer Qin56725eb2022-05-06 15:23:40 +0800288 * Overflow is checked in tfm_hal_memory_check functions.
Summer Qinba2346e2019-11-12 16:26:31 +0800289 */
290 for (i = 0; i + 1 < in_num; i++) {
291 for (j = i+1; j < in_num; j++) {
TTornblom83d96372019-11-19 12:53:16 +0100292 if (!((char *) invecs[j].base + invecs[j].len <=
293 (char *) invecs[i].base ||
294 (char *) invecs[j].base >=
295 (char *) invecs[i].base + invecs[i].len)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800296 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qinba2346e2019-11-12 16:26:31 +0800297 }
298 }
299 }
300
David Hu733d8f92019-09-23 15:32:40 +0800301 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800302 * For client output vector, it is a PROGRAMMER ERROR if the provided
303 * payload memory reference was invalid or not read-write.
David Hu733d8f92019-09-23 15:32:40 +0800304 */
305 for (i = 0; i < out_num; i++) {
Summer Qin56725eb2022-05-06 15:23:40 +0800306 if (tfm_hal_memory_check(curr_partition->boundary,
307 (uintptr_t)outvecs[i].base, outvecs[i].len,
308 TFM_HAL_ACCESS_READWRITE) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800309 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800310 }
311 }
312
Ken Liu0bed7e02022-02-10 12:38:07 +0800313 spm_fill_message(conn_handle, service, handle, type, client_id,
Summer Qin630c76b2020-05-20 10:32:58 +0800314 invecs, in_num, outvecs, out_num, outptr);
David Hu733d8f92019-09-23 15:32:40 +0800315
Ken Liu995a9742022-05-18 19:28:30 +0800316 return backend_messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800317}
318
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800319/* Following PSA APIs are only needed by connection-based services */
320#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
321
322psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version)
323{
324 struct service_t *service;
Mingyang Suna09adda2022-02-16 18:11:33 +0800325 struct conn_handle_t *conn_handle;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800326 int32_t client_id;
327 psa_handle_t handle;
328 bool ns_caller = tfm_spm_is_ns_caller();
329 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
330
331 /*
332 * It is a PROGRAMMER ERROR if the RoT Service does not exist on the
333 * platform.
334 */
335 service = tfm_spm_get_service_by_sid(sid);
336 if (!service) {
337 return PSA_ERROR_CONNECTION_REFUSED;
338 }
339
340 /* It is a PROGRAMMER ERROR if connecting to a stateless service. */
341 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
342 return PSA_ERROR_PROGRAMMER_ERROR;
343 }
344
345 /*
346 * It is a PROGRAMMER ERROR if the caller is not authorized to access the
347 * RoT Service.
348 */
Ken Liuad3f3e62022-03-04 22:23:52 +0800349 if (tfm_spm_check_authorization(sid, service, ns_caller) != PSA_SUCCESS) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800350 return PSA_ERROR_CONNECTION_REFUSED;
351 }
352
353 /*
354 * It is a PROGRAMMER ERROR if the version of the RoT Service requested is
355 * not supported on the platform.
356 */
Ken Liuad3f3e62022-03-04 22:23:52 +0800357 if (tfm_spm_check_client_version(service, version) != PSA_SUCCESS) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800358 return PSA_ERROR_CONNECTION_REFUSED;
359 }
360
361 client_id = tfm_spm_get_client_id(ns_caller);
362
363 /*
364 * Create connection handle here since it is possible to return the error
365 * code to client when creation fails.
366 */
367 CRITICAL_SECTION_ENTER(cs_assert);
Kevin Penge61e7052022-01-27 14:57:06 +0800368 conn_handle = tfm_spm_create_conn_handle();
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800369 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Suna09adda2022-02-16 18:11:33 +0800370 if (!conn_handle) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800371 return PSA_ERROR_CONNECTION_BUSY;
372 }
373
Mingyang Suna09adda2022-02-16 18:11:33 +0800374 handle = tfm_spm_to_user_handle(conn_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800375 /* No input or output needed for connect message */
Mingyang Suna09adda2022-02-16 18:11:33 +0800376 spm_fill_message(conn_handle, service, handle, PSA_IPC_CONNECT,
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800377 client_id, NULL, 0, NULL, 0, NULL);
378
Ken Liu995a9742022-05-18 19:28:30 +0800379 return backend_messaging(service, conn_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800380}
381
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800382psa_status_t tfm_spm_client_psa_close(psa_handle_t handle)
David Hu733d8f92019-09-23 15:32:40 +0800383{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800384 struct service_t *service;
Ken Liu0bed7e02022-02-10 12:38:07 +0800385 struct conn_handle_t *conn_handle;
Summer Qin1ce712a2019-10-14 18:04:05 +0800386 int32_t client_id;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800387 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800388
389 /* It will have no effect if called with the NULL handle */
390 if (handle == PSA_NULL_HANDLE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800391 return PSA_SUCCESS;
David Hu733d8f92019-09-23 15:32:40 +0800392 }
393
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800394 /* It is a PROGRAMMER ERROR if called with a stateless handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800395 if (IS_STATIC_HANDLE(handle)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800396 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800397 }
398
Kevin Peng385fda82021-08-18 10:41:19 +0800399 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800400
David Hu733d8f92019-09-23 15:32:40 +0800401 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800402 * It is a PROGRAMMER ERROR if an invalid handle was provided that is not
403 * the null handle.
David Hu733d8f92019-09-23 15:32:40 +0800404 */
Kevin Penge61e7052022-01-27 14:57:06 +0800405 conn_handle = spm_get_handle_by_client_handle(handle, client_id);
406 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800407 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qin1ce712a2019-10-14 18:04:05 +0800408 }
Shawn Shanb222d892021-01-04 17:41:48 +0800409
Ken Liu0bed7e02022-02-10 12:38:07 +0800410 service = conn_handle->service;
David Hu733d8f92019-09-23 15:32:40 +0800411 if (!service) {
412 /* FixMe: Need to implement one mechanism to resolve this failure. */
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800413 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800414 }
415
Shawn Shanb222d892021-01-04 17:41:48 +0800416 /*
417 * It is a PROGRAMMER ERROR if the connection is currently handling a
418 * request.
419 */
Summer Qin630c76b2020-05-20 10:32:58 +0800420 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800421 return PSA_ERROR_PROGRAMMER_ERROR;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800422 }
423
David Hu733d8f92019-09-23 15:32:40 +0800424 /* No input or output needed for close message */
Ken Liu0bed7e02022-02-10 12:38:07 +0800425 spm_fill_message(conn_handle, service, handle, PSA_IPC_DISCONNECT,
426 client_id, NULL, 0, NULL, 0, NULL);
David Hu733d8f92019-09-23 15:32:40 +0800427
Ken Liu995a9742022-05-18 19:28:30 +0800428 return backend_messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800429}
Mingyang Sunb26b2802021-07-07 11:25:00 +0800430
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800431#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
432
Mingyang Suneeca4652021-07-15 15:19:16 +0800433/* PSA Partition API function body */
434
Kevin Pengdef92de2021-11-10 16:14:48 +0800435#if CONFIG_TFM_SPM_BACKEND_IPC == 1 \
436 || CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800437psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask,
438 uint32_t timeout)
439{
440 struct partition_t *partition = NULL;
441
442 /*
443 * Timeout[30:0] are reserved for future use.
444 * SPM must ignore the value of RES.
445 */
446 timeout &= PSA_TIMEOUT_MASK;
447
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800448 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800449
450 /*
Ruchika Guptad1834172022-06-15 12:03:08 +0530451 * signals_allowed can be 0 for TF-M internal partitions for special usages.
452 * Regular Secure Partitions should have at least one signal.
453 * This is gauranteed by the manifest tool.
Mingyang Sunb26b2802021-07-07 11:25:00 +0800454 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
455 * signals.
456 */
Ruchika Guptad1834172022-06-15 12:03:08 +0530457 if ((partition->signals_allowed) &&
458 (partition->signals_allowed & signal_mask) == 0) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800459 tfm_core_panic();
460 }
461
462 /*
Ken Liu995a9742022-05-18 19:28:30 +0800463 * backend_wake_up() blocks the caller thread if no signals are
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800464 * available. In this case, the return value of this function is temporary
465 * set into runtime context. After new signal(s) are available, the return
466 * value is updated with the available signal(s) and blocked thread gets
467 * to run.
Mingyang Sunb26b2802021-07-07 11:25:00 +0800468 */
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800469 if (timeout == PSA_BLOCK) {
Ken Liu995a9742022-05-18 19:28:30 +0800470 return backend_wait(partition, signal_mask);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800471 }
472
473 return partition->signals_asserted & signal_mask;
474}
Kevin Pengdef92de2021-11-10 16:14:48 +0800475#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800476
Kevin Pengdef92de2021-11-10 16:14:48 +0800477#if CONFIG_TFM_SPM_BACKEND_IPC == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800478psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg)
479{
Mingyang Suna09adda2022-02-16 18:11:33 +0800480 struct conn_handle_t *handle = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800481 struct partition_t *partition = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800482
483 /*
484 * Only one message could be retrieved every time for psa_get(). It is a
485 * fatal error if the input signal has more than a signal bit set.
486 */
487 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
488 tfm_core_panic();
489 }
490
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800491 partition = GET_CURRENT_COMPONENT();
492
Mingyang Sunb26b2802021-07-07 11:25:00 +0800493 /*
494 * Write the message to the service buffer. It is a fatal error if the
495 * input msg pointer is not a valid memory reference or not read-write.
496 */
Summer Qin56725eb2022-05-06 15:23:40 +0800497 if (tfm_hal_memory_check(partition->boundary, (uintptr_t)msg,
498 sizeof(psa_msg_t), TFM_HAL_ACCESS_READWRITE) != PSA_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800499 tfm_core_panic();
500 }
501
502 /*
503 * It is a fatal error if the caller call psa_get() when no message has
504 * been set. The caller must call this function after an RoT Service signal
505 * is returned by psa_wait().
506 */
507 if (partition->signals_asserted == 0) {
508 tfm_core_panic();
509 }
510
511 /*
512 * It is a fatal error if the RoT Service signal is not currently asserted.
513 */
514 if ((partition->signals_asserted & signal) == 0) {
515 tfm_core_panic();
516 }
517
518 /*
519 * Get message by signal from partition. It is a fatal error if getting
520 * failed, which means the input signal is not correspond to an RoT service.
521 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800522 handle = spm_get_handle_by_signal(partition, signal);
523 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800524 return PSA_ERROR_DOES_NOT_EXIST;
525 }
526
Mingyang Suna09adda2022-02-16 18:11:33 +0800527 spm_memcpy(msg, &handle->msg, sizeof(psa_msg_t));
Mingyang Sunb26b2802021-07-07 11:25:00 +0800528
529 return PSA_SUCCESS;
530}
Mingyang Sun4609ac72022-02-08 18:56:35 +0800531#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800532
Mingyang Sunb26b2802021-07-07 11:25:00 +0800533size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
534 void *buffer, size_t num_bytes)
535{
536 size_t bytes;
Mingyang Suna09adda2022-02-16 18:11:33 +0800537 struct conn_handle_t *handle = NULL;
Summer Qin56725eb2022-05-06 15:23:40 +0800538 struct partition_t *curr_partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800539
540 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800541 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800542 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800543 tfm_core_panic();
544 }
545
Mingyang Sunb26b2802021-07-07 11:25:00 +0800546 /*
547 * It is a fatal error if message handle does not refer to a request
548 * message
549 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800550 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800551 tfm_core_panic();
552 }
553
554 /*
555 * It is a fatal error if invec_idx is equal to or greater than
556 * PSA_MAX_IOVEC
557 */
558 if (invec_idx >= PSA_MAX_IOVEC) {
559 tfm_core_panic();
560 }
561
Shawn Shan038348e2021-09-08 17:11:04 +0800562#if PSA_FRAMEWORK_HAS_MM_IOVEC
563 /*
564 * It is a fatal error if the input vector has already been mapped using
565 * psa_map_invec().
566 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800567 if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800568 tfm_core_panic();
569 }
570
Mingyang Suna09adda2022-02-16 18:11:33 +0800571 SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800572#endif
573
Kevin Peng016b0e42022-04-15 11:28:48 +0800574 /* There was no remaining data in this input vector */
575 if (handle->msg.in_size[invec_idx] == 0) {
576 return 0;
577 }
578
Mingyang Sunb26b2802021-07-07 11:25:00 +0800579 /*
580 * Copy the client data to the service buffer. It is a fatal error
581 * if the memory reference for buffer is invalid or not read-write.
582 */
Summer Qin56725eb2022-05-06 15:23:40 +0800583 if (tfm_hal_memory_check(curr_partition->boundary, (uintptr_t)buffer,
584 num_bytes, TFM_HAL_ACCESS_READWRITE) != PSA_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800585 tfm_core_panic();
586 }
587
Mingyang Suna09adda2022-02-16 18:11:33 +0800588 bytes = num_bytes > handle->msg.in_size[invec_idx] ?
589 handle->msg.in_size[invec_idx] : num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800590
Mingyang Suna09adda2022-02-16 18:11:33 +0800591 spm_memcpy(buffer, handle->invec[invec_idx].base, bytes);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800592
593 /* There maybe some remaining data */
Mingyang Suna09adda2022-02-16 18:11:33 +0800594 handle->invec[invec_idx].base =
595 (char *)handle->invec[invec_idx].base + bytes;
596 handle->msg.in_size[invec_idx] -= bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800597
598 return bytes;
599}
600
601size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx,
602 size_t num_bytes)
603{
Mingyang Suna09adda2022-02-16 18:11:33 +0800604 struct conn_handle_t *handle = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800605
606 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800607 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800608 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800609 tfm_core_panic();
610 }
611
612 /*
613 * It is a fatal error if message handle does not refer to a request
614 * message
615 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800616 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800617 tfm_core_panic();
618 }
619
620 /*
621 * It is a fatal error if invec_idx is equal to or greater than
622 * PSA_MAX_IOVEC
623 */
624 if (invec_idx >= PSA_MAX_IOVEC) {
625 tfm_core_panic();
626 }
627
Shawn Shan038348e2021-09-08 17:11:04 +0800628#if PSA_FRAMEWORK_HAS_MM_IOVEC
629 /*
630 * It is a fatal error if the input vector has already been mapped using
631 * psa_map_invec().
632 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800633 if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800634 tfm_core_panic();
635 }
636
Mingyang Suna09adda2022-02-16 18:11:33 +0800637 SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800638#endif
639
Kevin Peng016b0e42022-04-15 11:28:48 +0800640 /* There was no remaining data in this input vector */
641 if (handle->msg.in_size[invec_idx] == 0) {
642 return 0;
643 }
644
Mingyang Sunb26b2802021-07-07 11:25:00 +0800645 /*
646 * If num_bytes is greater than the remaining size of the input vector then
647 * the remaining size of the input vector is used.
648 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800649 if (num_bytes > handle->msg.in_size[invec_idx]) {
650 num_bytes = handle->msg.in_size[invec_idx];
Mingyang Sunb26b2802021-07-07 11:25:00 +0800651 }
652
653 /* There maybe some remaining data */
Mingyang Suna09adda2022-02-16 18:11:33 +0800654 handle->invec[invec_idx].base =
655 (char *)handle->invec[invec_idx].base + num_bytes;
656 handle->msg.in_size[invec_idx] -= num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800657
658 return num_bytes;
659}
660
661void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
662 const void *buffer, size_t num_bytes)
663{
Mingyang Suna09adda2022-02-16 18:11:33 +0800664 struct conn_handle_t *handle = NULL;
Summer Qin56725eb2022-05-06 15:23:40 +0800665 struct partition_t *curr_partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800666
667 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800668 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800669 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800670 tfm_core_panic();
671 }
672
Mingyang Sunb26b2802021-07-07 11:25:00 +0800673 /*
674 * It is a fatal error if message handle does not refer to a request
675 * message
676 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800677 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800678 tfm_core_panic();
679 }
680
681 /*
682 * It is a fatal error if outvec_idx is equal to or greater than
683 * PSA_MAX_IOVEC
684 */
685 if (outvec_idx >= PSA_MAX_IOVEC) {
686 tfm_core_panic();
687 }
688
689 /*
690 * It is a fatal error if the call attempts to write data past the end of
691 * the client output vector
692 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800693 if (num_bytes > handle->msg.out_size[outvec_idx] -
694 handle->outvec[outvec_idx].len) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800695 tfm_core_panic();
696 }
697
Shawn Shan038348e2021-09-08 17:11:04 +0800698#if PSA_FRAMEWORK_HAS_MM_IOVEC
699 /*
700 * It is a fatal error if the output vector has already been mapped using
701 * psa_map_outvec().
702 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800703 if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800704 tfm_core_panic();
705 }
706
Mingyang Suna09adda2022-02-16 18:11:33 +0800707 SET_IOVEC_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800708#endif
709
Mingyang Sunb26b2802021-07-07 11:25:00 +0800710 /*
711 * Copy the service buffer to client outvecs. It is a fatal error
712 * if the memory reference for buffer is invalid or not readable.
713 */
Summer Qin56725eb2022-05-06 15:23:40 +0800714 if (tfm_hal_memory_check(curr_partition->boundary,
715 (uintptr_t)buffer, num_bytes, TFM_HAL_ACCESS_READABLE) != PSA_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800716 tfm_core_panic();
717 }
718
Mingyang Suna09adda2022-02-16 18:11:33 +0800719 spm_memcpy((char *)handle->outvec[outvec_idx].base +
720 handle->outvec[outvec_idx].len, buffer, num_bytes);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800721
722 /* Update the write number */
Mingyang Suna09adda2022-02-16 18:11:33 +0800723 handle->outvec[outvec_idx].len += num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800724}
725
Ken Liuf8c7e532022-02-10 15:03:04 +0800726psa_status_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle,
727 psa_status_t status)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800728{
Ken Liu0bed7e02022-02-10 12:38:07 +0800729 struct service_t *service;
Mingyang Suna09adda2022-02-16 18:11:33 +0800730 struct conn_handle_t *handle;
Ken Liuf8c7e532022-02-10 15:03:04 +0800731 psa_status_t ret = PSA_SUCCESS;
Mingyang Sun620c8562021-11-10 11:44:58 +0800732 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800733
734 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800735 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800736 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800737 tfm_core_panic();
738 }
739
740 /*
741 * RoT Service information is needed in this function, stored it in message
742 * body structure. Only two parameters are passed in this function: handle
743 * and status, so it is useful and simply to do like this.
744 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800745 service = handle->service;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800746 if (!service) {
747 tfm_core_panic();
748 }
749
Mingyang Suna09adda2022-02-16 18:11:33 +0800750 switch (handle->msg.type) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800751 case PSA_IPC_CONNECT:
752 /*
753 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
754 * input status is PSA_SUCCESS. Others return values are based on the
755 * input status.
756 */
757 if (status == PSA_SUCCESS) {
758 ret = msg_handle;
759 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
760 /* Refuse the client connection, indicating a permanent error. */
Mingyang Sunb26b2802021-07-07 11:25:00 +0800761 ret = PSA_ERROR_CONNECTION_REFUSED;
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800762 handle->status = TFM_HANDLE_STATUS_TO_FREE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800763 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
764 /* Fail the client connection, indicating a transient error. */
765 ret = PSA_ERROR_CONNECTION_BUSY;
766 } else {
767 tfm_core_panic();
768 }
769 break;
770 case PSA_IPC_DISCONNECT:
771 /* Service handle is not used anymore */
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800772 handle->status = TFM_HANDLE_STATUS_TO_FREE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800773
774 /*
775 * If the message type is PSA_IPC_DISCONNECT, then the status code is
776 * ignored
777 */
778 break;
779 default:
Mingyang Suna09adda2022-02-16 18:11:33 +0800780 if (handle->msg.type >= PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +0800781
782#if PSA_FRAMEWORK_HAS_MM_IOVEC
783
784 /*
785 * If the unmapped function is not called for an input/output vector
786 * that has been mapped, the framework will remove the mapping.
787 */
788 int i;
789
790 for (i = 0; i < PSA_MAX_IOVEC * 2; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800791 if (IOVEC_IS_MAPPED(handle, i) &&
792 (!IOVEC_IS_UNMAPPED(handle, i))) {
793 SET_IOVEC_UNMAPPED(handle, i);
Shawn Shan038348e2021-09-08 17:11:04 +0800794 /*
795 * Any output vectors that are still mapped will report that
796 * zero bytes have been written.
797 */
798 if (i >= OUTVEC_IDX_BASE) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800799 handle->outvec[i - OUTVEC_IDX_BASE].len = 0;
Shawn Shan038348e2021-09-08 17:11:04 +0800800 }
801 }
802 }
803
804#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800805 /* Reply to a request message. Return values are based on status */
806 ret = status;
807 /*
808 * The total number of bytes written to a single parameter must be
809 * reported to the client by updating the len member of the
810 * psa_outvec structure for the parameter before returning from
811 * psa_call().
812 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800813 update_caller_outvec_len(handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800814 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800815 handle->status = TFM_HANDLE_STATUS_TO_FREE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800816 }
817 } else {
818 tfm_core_panic();
819 }
820 }
821
822 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
823 /*
824 * If the source of the programmer error is a Secure Partition, the SPM
825 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
826 */
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800827 if (!TFM_CLIENT_ID_IS_NS(handle->msg.client_id)) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800828 tfm_core_panic();
829 }
Mingyang Sunb26b2802021-07-07 11:25:00 +0800830 }
831
Mingyang Sun620c8562021-11-10 11:44:58 +0800832 /*
833 * TODO: It can be optimized further by moving critical section protection
834 * to mailbox. Also need to check implementation when secure context is
835 * involved.
836 */
837 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu995a9742022-05-18 19:28:30 +0800838 ret = backend_replying(handle, ret);
Mingyang Sun620c8562021-11-10 11:44:58 +0800839 CRITICAL_SECTION_LEAVE(cs_assert);
840
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800841 if (handle->status == TFM_HANDLE_STATUS_TO_FREE) {
842 tfm_spm_free_conn_handle(handle);
843 } else {
844 handle->status = TFM_HANDLE_STATUS_IDLE;
845 }
846
Mingyang Sun620c8562021-11-10 11:44:58 +0800847 return ret;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800848}
849
Kevin Peng613b4172022-02-15 14:41:44 +0800850#if CONFIG_TFM_DOORBELL_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800851void tfm_spm_partition_psa_notify(int32_t partition_id)
852{
Ken Liu5d73c872021-08-19 19:23:17 +0800853 struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id);
854
855 spm_assert_signal(p_pt, PSA_DOORBELL);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800856}
857
858void tfm_spm_partition_psa_clear(void)
859{
Ken Liu92ede9f2021-10-20 09:35:00 +0800860 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800861 struct partition_t *partition = NULL;
862
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800863 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800864
865 /*
866 * It is a fatal error if the Secure Partition's doorbell signal is not
867 * currently asserted.
868 */
869 if ((partition->signals_asserted & PSA_DOORBELL) == 0) {
870 tfm_core_panic();
871 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800872
873 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800874 partition->signals_asserted &= ~PSA_DOORBELL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800875 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800876}
Kevin Peng613b4172022-02-15 14:41:44 +0800877#endif /* CONFIG_TFM_DOORBELL_API == 1 */
Mingyang Sunb26b2802021-07-07 11:25:00 +0800878
Mingyang Sunb26b2802021-07-07 11:25:00 +0800879void tfm_spm_partition_psa_panic(void)
880{
Joakim Andersson97cef7e2022-03-18 10:22:00 +0100881#ifdef CONFIG_TFM_HALT_ON_CORE_PANIC
882 tfm_hal_system_halt();
883#else
Mingyang Sunb26b2802021-07-07 11:25:00 +0800884 /*
885 * PSA FF recommends that the SPM causes the system to restart when a secure
886 * partition panics.
887 */
888 tfm_hal_system_reset();
Joakim Andersson97cef7e2022-03-18 10:22:00 +0100889#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800890}
891
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800892/* psa_set_rhandle is only needed by connection-based services */
893#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
894
895void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle)
896{
Mingyang Suna09adda2022-02-16 18:11:33 +0800897 struct conn_handle_t *handle;
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800898
899 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800900 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800901 if (!handle) {
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800902 tfm_core_panic();
903 }
904
905 /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */
Mingyang Suna09adda2022-02-16 18:11:33 +0800906 if (SERVICE_IS_STATELESS(handle->service->p_ldinf->flags)) {
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800907 tfm_core_panic();
908 }
909
Mingyang Suna09adda2022-02-16 18:11:33 +0800910 handle->msg.rhandle = rhandle;
911 handle->rhandle = rhandle;
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800912}
913
914#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
915
916#if CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1
Kevin Peng67a89fd2021-11-25 11:22:02 +0800917void tfm_spm_partition_psa_irq_enable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800918{
919 struct partition_t *partition;
920 struct irq_load_info_t *irq_info;
921
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800922 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800923
924 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
925 if (!irq_info) {
926 tfm_core_panic();
927 }
928
Kevin Pengd399a1f2021-09-08 15:33:14 +0800929 tfm_hal_irq_enable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800930}
931
Kevin Peng67a89fd2021-11-25 11:22:02 +0800932psa_irq_status_t tfm_spm_partition_psa_irq_disable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800933{
934 struct partition_t *partition;
935 struct irq_load_info_t *irq_info;
936
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800937 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800938
939 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
940 if (!irq_info) {
941 tfm_core_panic();
942 }
943
Kevin Pengd399a1f2021-09-08 15:33:14 +0800944 tfm_hal_irq_disable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800945
946 return 1;
947}
948
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800949/* This API is only used for FLIH. */
950#if CONFIG_TFM_FLIH_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800951void tfm_spm_partition_psa_reset_signal(psa_signal_t irq_signal)
952{
Ken Liu92ede9f2021-10-20 09:35:00 +0800953 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800954 struct irq_load_info_t *irq_info;
955 struct partition_t *partition;
956
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800957 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800958
959 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
960 if (!irq_info) {
961 tfm_core_panic();
962 }
963
964 if (!irq_info->flih_func) {
965 /* This API is for FLIH IRQs only */
966 tfm_core_panic();
967 }
968
969 if ((partition->signals_asserted & irq_signal) == 0) {
970 /* The signal is not asserted */
971 tfm_core_panic();
972 }
973
Ken Liu92ede9f2021-10-20 09:35:00 +0800974 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800975 partition->signals_asserted &= ~irq_signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800976 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800977}
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800978#endif
Shawn Shan038348e2021-09-08 17:11:04 +0800979
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800980/* This API is only used for SLIH. */
981#if CONFIG_TFM_SLIH_API == 1
982void tfm_spm_partition_psa_eoi(psa_signal_t irq_signal)
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800983{
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800984 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
985 struct irq_load_info_t *irq_info = NULL;
986 struct partition_t *partition = NULL;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800987
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800988 partition = GET_CURRENT_COMPONENT();
989
990 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
991 /* It is a fatal error if passed signal is not an interrupt signal. */
992 if (!irq_info) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800993 tfm_core_panic();
994 }
995
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800996 if (irq_info->flih_func) {
997 /* This API is for SLIH IRQs only */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800998 tfm_core_panic();
999 }
1000
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001001 /* It is a fatal error if passed signal is not currently asserted */
1002 if ((partition->signals_asserted & irq_signal) == 0) {
1003 tfm_core_panic();
1004 }
1005
1006 CRITICAL_SECTION_ENTER(cs_assert);
1007 partition->signals_asserted &= ~irq_signal;
1008 CRITICAL_SECTION_LEAVE(cs_assert);
1009
1010 tfm_hal_irq_clear_pending(irq_info->source);
1011 tfm_hal_irq_enable(irq_info->source);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001012}
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001013#endif
1014#endif /* CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001015
Shawn Shan038348e2021-09-08 17:11:04 +08001016#if PSA_FRAMEWORK_HAS_MM_IOVEC
1017
1018const void *tfm_spm_partition_psa_map_invec(psa_handle_t msg_handle,
1019 uint32_t invec_idx)
1020{
Mingyang Suna09adda2022-02-16 18:11:33 +08001021 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001022 struct partition_t *partition = NULL;
1023
1024 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001025 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001026 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001027 tfm_core_panic();
1028 }
1029
Mingyang Suna09adda2022-02-16 18:11:33 +08001030 partition = handle->service->partition;
Shawn Shan038348e2021-09-08 17:11:04 +08001031
1032 /*
1033 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1034 * Service that received the message.
1035 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001036 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001037 tfm_core_panic();
1038 }
1039
1040 /*
1041 * It is a fatal error if message handle does not refer to a request
1042 * message.
1043 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001044 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001045 tfm_core_panic();
1046 }
1047
1048 /*
1049 * It is a fatal error if invec_idx is equal to or greater than
1050 * PSA_MAX_IOVEC.
1051 */
1052 if (invec_idx >= PSA_MAX_IOVEC) {
1053 tfm_core_panic();
1054 }
1055
1056 /* It is a fatal error if the input vector has length zero. */
Mingyang Suna09adda2022-02-16 18:11:33 +08001057 if (handle->msg.in_size[invec_idx] == 0) {
Shawn Shan038348e2021-09-08 17:11:04 +08001058 tfm_core_panic();
1059 }
1060
1061 /*
1062 * It is a fatal error if the input vector has already been mapped using
1063 * psa_map_invec().
1064 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001065 if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001066 tfm_core_panic();
1067 }
1068
1069 /*
1070 * It is a fatal error if the input vector has already been accessed
1071 * using psa_read() or psa_skip().
1072 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001073 if (IOVEC_IS_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001074 tfm_core_panic();
1075 }
1076
1077 /*
1078 * It is a fatal error if the memory reference for the wrap input vector is
1079 * invalid or not readable.
1080 */
Summer Qin56725eb2022-05-06 15:23:40 +08001081 if (tfm_hal_memory_check(partition->boundary,
1082 (uintptr_t)handle->invec[invec_idx].base,
1083 handle->invec[invec_idx].len,
1084 TFM_HAL_ACCESS_READABLE) != PSA_SUCCESS) {
Shawn Shan038348e2021-09-08 17:11:04 +08001085 tfm_core_panic();
1086 }
1087
Mingyang Suna09adda2022-02-16 18:11:33 +08001088 SET_IOVEC_MAPPED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001089
Mingyang Suna09adda2022-02-16 18:11:33 +08001090 return handle->invec[invec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001091}
1092
1093void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle,
1094 uint32_t invec_idx)
1095{
Mingyang Suna09adda2022-02-16 18:11:33 +08001096 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001097
1098 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001099 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001100 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001101 tfm_core_panic();
1102 }
1103
1104 /*
1105 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1106 * Service that received the message.
1107 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001108 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001109 tfm_core_panic();
1110 }
1111
1112 /*
1113 * It is a fatal error if message handle does not refer to a request
1114 * message.
1115 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001116 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001117 tfm_core_panic();
1118 }
1119
1120 /*
1121 * It is a fatal error if invec_idx is equal to or greater than
1122 * PSA_MAX_IOVEC.
1123 */
1124 if (invec_idx >= PSA_MAX_IOVEC) {
1125 tfm_core_panic();
1126 }
1127
1128 /*
1129 * It is a fatal error if The input vector has not been mapped by a call to
1130 * psa_map_invec().
1131 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001132 if (!IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001133 tfm_core_panic();
1134 }
1135
1136 /*
1137 * It is a fatal error if the input vector has already been unmapped by a
1138 * call to psa_unmap_invec().
1139 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001140 if (IOVEC_IS_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001141 tfm_core_panic();
1142 }
1143
Mingyang Suna09adda2022-02-16 18:11:33 +08001144 SET_IOVEC_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001145}
1146
1147void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle,
1148 uint32_t outvec_idx)
1149{
Mingyang Suna09adda2022-02-16 18:11:33 +08001150 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001151 uint32_t privileged;
1152 struct partition_t *partition = NULL;
1153
1154 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001155 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001156 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001157 tfm_core_panic();
1158 }
1159
Mingyang Suna09adda2022-02-16 18:11:33 +08001160 partition = handle->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001161 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001162
1163 /*
1164 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1165 * Service that received the message.
1166 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001167 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001168 tfm_core_panic();
1169 }
1170
1171 /*
1172 * It is a fatal error if message handle does not refer to a request
1173 * message.
1174 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001175 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001176 tfm_core_panic();
1177 }
1178
1179 /*
1180 * It is a fatal error if outvec_idx is equal to or greater than
1181 * PSA_MAX_IOVEC.
1182 */
1183 if (outvec_idx >= PSA_MAX_IOVEC) {
1184 tfm_core_panic();
1185 }
1186
1187 /* It is a fatal error if the output vector has length zero. */
Mingyang Suna09adda2022-02-16 18:11:33 +08001188 if (handle->msg.out_size[outvec_idx] == 0) {
Shawn Shan038348e2021-09-08 17:11:04 +08001189 tfm_core_panic();
1190 }
1191
1192 /*
1193 * It is a fatal error if the output vector has already been mapped using
1194 * psa_map_outvec().
1195 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001196 if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001197 tfm_core_panic();
1198 }
1199
1200 /*
1201 * It is a fatal error if the output vector has already been accessed
1202 * using psa_write().
1203 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001204 if (IOVEC_IS_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001205 tfm_core_panic();
1206 }
1207
1208 /*
1209 * It is a fatal error if the output vector is invalid or not read-write.
1210 */
Summer Qin56725eb2022-05-06 15:23:40 +08001211 if (tfm_hal_memory_check(partition->boundary,
1212 (uintptr_t)handle->outvec[outvec_idx].base,
1213 handle->outvec[outvec_idx].len,
1214 TFM_HAL_ACCESS_READWRITE) != PSA_SUCCESS) {
Shawn Shan038348e2021-09-08 17:11:04 +08001215 tfm_core_panic();
1216 }
Mingyang Suna09adda2022-02-16 18:11:33 +08001217 SET_IOVEC_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001218
Mingyang Suna09adda2022-02-16 18:11:33 +08001219 return handle->outvec[outvec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001220}
1221
1222void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle,
1223 uint32_t outvec_idx, size_t len)
1224{
Mingyang Suna09adda2022-02-16 18:11:33 +08001225 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001226
1227 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001228 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001229 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001230 tfm_core_panic();
1231 }
1232
1233 /*
1234 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1235 * Service that received the message.
1236 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001237 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001238 tfm_core_panic();
1239 }
1240
1241 /*
1242 * It is a fatal error if message handle does not refer to a request
1243 * message.
1244 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001245 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001246 tfm_core_panic();
1247 }
1248
1249 /*
1250 * It is a fatal error if outvec_idx is equal to or greater than
1251 * PSA_MAX_IOVEC.
1252 */
1253 if (outvec_idx >= PSA_MAX_IOVEC) {
1254 tfm_core_panic();
1255 }
1256
1257 /*
1258 * It is a fatal error if len is greater than the output vector size.
1259 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001260 if (len > handle->msg.out_size[outvec_idx]) {
Shawn Shan038348e2021-09-08 17:11:04 +08001261 tfm_core_panic();
1262 }
1263
1264 /*
1265 * It is a fatal error if The output vector has not been mapped by a call to
1266 * psa_map_outvec().
1267 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001268 if (!IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001269 tfm_core_panic();
1270 }
1271
1272 /*
1273 * It is a fatal error if the output vector has already been unmapped by a
1274 * call to psa_unmap_outvec().
1275 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001276 if (IOVEC_IS_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001277 tfm_core_panic();
1278 }
1279
Mingyang Suna09adda2022-02-16 18:11:33 +08001280 SET_IOVEC_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001281
1282 /* Update the write number */
Mingyang Suna09adda2022-02-16 18:11:33 +08001283 handle->outvec[outvec_idx].len = len;
Shawn Shan038348e2021-09-08 17:11:04 +08001284}
1285
1286#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */