blob: 32ece5542f3688eeaffb89f99891df388bc6541c [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"
David Hu733d8f92019-09-23 15:32:40 +080017#include "tfm_core_utils.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080018#include "load/partition_defs.h"
Mingyang Sun2b352662021-04-21 11:35:43 +080019#include "load/service_defs.h"
Ken Liu3dd92562021-08-17 16:22:54 +080020#include "load/interrupt_defs.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080021#include "ffm/psa_api.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080022#include "utilities.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080023#include "ffm/backend.h"
Ken Liue07c3b72021-10-14 16:19:13 +080024#include "ffm/psa_api.h"
Ken Liubcae38b2021-01-20 15:47:44 +080025#include "ffm/spm_error_base.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080026#include "tfm_rpc.h"
27#include "tfm_spm_hal.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080028#include "tfm_hal_interrupt.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080029#include "tfm_hal_platform.h"
Ken Liu82e3eac2021-10-14 16:19:13 +080030#include "tfm_psa_call_pack.h"
David Hu733d8f92019-09-23 15:32:40 +080031
Ken Liub3b2cb62021-05-22 00:39:28 +080032#define GET_STATELESS_SERVICE(index) (stateless_services_ref_tbl[index])
Xinyu Zhanga38e9b52021-06-02 17:48:01 +080033extern struct service_t *stateless_services_ref_tbl[];
Mingyang Suncb6f70e2021-03-05 23:30:25 +080034
Shawn Shan038348e2021-09-08 17:11:04 +080035#if PSA_FRAMEWORK_HAS_MM_IOVEC
36
37/*
38 * The MM-IOVEC status
39 * The max total number of invec and outvec is 8.
40 * Each invec/outvec takes 4 bit, 32 bits in total.
41 *
42 * The encoding format of the MM-IOVEC status:
43 *--------------------------------------------------------------
44 *| Bit | 31 - 28 | 27 - 24 | ... | 7 - 4 | 3 - 0 |
45 *--------------------------------------------------------------
46 *| Vector | outvec[3] | outvec[2] | ... | invec[1] | invec[0] |
47 *--------------------------------------------------------------
48 *
49 * Take invec[0] as an example:
50 *
51 * bit 0: whether invec[0] has been mapped.
52 * bit 1: whether invec[0] has been unmapped.
53 * bit 2: whether invec[0] has been accessed using psa_read(), psa_skip() or
54 * psa_write().
55 * bit 3: reserved for invec[0].
56 */
57
58#define IOVEC_STATUS_BITS 4 /* Each vector occupies 4 bits. */
59#define OUTVEC_IDX_BASE 4 /*
60 * Base index of outvec.
61 * There are four invecs in front of
62 * outvec.
63 */
64#define INVEC_IDX_BASE 0 /* Base index of invec. */
65
66#define IOVEC_MAPPED_BIT (1U << 0)
67#define IOVEC_UNMAPPED_BIT (1U << 1)
68#define IOVEC_ACCESSED_BIT (1U << 2)
69
Mingyang Suna09adda2022-02-16 18:11:33 +080070#define IOVEC_IS_MAPPED(handle, iovec_idx) \
71 ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
Shawn Shan038348e2021-09-08 17:11:04 +080072 IOVEC_MAPPED_BIT)
Mingyang Suna09adda2022-02-16 18:11:33 +080073#define IOVEC_IS_UNMAPPED(handle, iovec_idx) \
74 ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
Shawn Shan038348e2021-09-08 17:11:04 +080075 IOVEC_UNMAPPED_BIT)
Mingyang Suna09adda2022-02-16 18:11:33 +080076#define IOVEC_IS_ACCESSED(handle, iovec_idx) \
77 ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
Shawn Shan038348e2021-09-08 17:11:04 +080078 IOVEC_ACCESSED_BIT)
Mingyang Suna09adda2022-02-16 18:11:33 +080079#define SET_IOVEC_MAPPED(handle, iovec_idx) \
80 (((handle)->iovec_status) |= (IOVEC_MAPPED_BIT << \
Shawn Shan038348e2021-09-08 17:11:04 +080081 ((iovec_idx) * IOVEC_STATUS_BITS)))
Mingyang Suna09adda2022-02-16 18:11:33 +080082#define SET_IOVEC_UNMAPPED(handle, iovec_idx) \
83 (((handle)->iovec_status) |= (IOVEC_UNMAPPED_BIT << \
Shawn Shan038348e2021-09-08 17:11:04 +080084 ((iovec_idx) * IOVEC_STATUS_BITS)))
Mingyang Suna09adda2022-02-16 18:11:33 +080085#define SET_IOVEC_ACCESSED(handle, iovec_idx) \
86 (((handle)->iovec_status) |= (IOVEC_ACCESSED_BIT << \
Shawn Shan038348e2021-09-08 17:11:04 +080087 ((iovec_idx) * IOVEC_STATUS_BITS)))
88
89#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */
90
Xinyu Zhangb287ef82021-11-03 18:38:50 +080091void spm_handle_programmer_errors(psa_status_t status)
92{
93 if (status == PSA_ERROR_PROGRAMMER_ERROR ||
94 status == PSA_ERROR_CONNECTION_REFUSED ||
95 status == PSA_ERROR_CONNECTION_BUSY) {
96 if (!tfm_spm_is_ns_caller()) {
97 tfm_core_panic();
98 }
99 }
100}
101
Mingyang Suneeca4652021-07-15 15:19:16 +0800102uint32_t tfm_spm_get_lifecycle_state(void)
103{
104 /*
105 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
106 * implemented in the future.
107 */
108 return PSA_LIFECYCLE_UNKNOWN;
109}
110
111/* PSA Client API function body */
112
Mingyang Sund44522a2020-01-16 16:48:37 +0800113uint32_t tfm_spm_client_psa_framework_version(void)
David Hu733d8f92019-09-23 15:32:40 +0800114{
115 return PSA_FRAMEWORK_VERSION;
116}
117
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800118uint32_t tfm_spm_client_psa_version(uint32_t sid)
David Hu733d8f92019-09-23 15:32:40 +0800119{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800120 struct service_t *service;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800121 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800122
123 /*
124 * It should return PSA_VERSION_NONE if the RoT Service is not
125 * implemented.
126 */
127 service = tfm_spm_get_service_by_sid(sid);
128 if (!service) {
129 return PSA_VERSION_NONE;
130 }
131
132 /*
Shawn Shan2365c902019-12-19 18:35:36 +0800133 * It should return PSA_VERSION_NONE if the caller is not authorized
134 * to access the RoT Service.
David Hu733d8f92019-09-23 15:32:40 +0800135 */
Ken Liubcae38b2021-01-20 15:47:44 +0800136 if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) {
Shawn Shan2365c902019-12-19 18:35:36 +0800137 return PSA_VERSION_NONE;
David Hu733d8f92019-09-23 15:32:40 +0800138 }
139
Ken Liuacd2a572021-05-12 16:19:04 +0800140 return service->p_ldinf->version;
David Hu733d8f92019-09-23 15:32:40 +0800141}
142
Mingyang Suneeca4652021-07-15 15:19:16 +0800143psa_status_t tfm_spm_client_psa_call(psa_handle_t handle,
144 uint32_t ctrl_param,
145 const psa_invec *inptr,
146 psa_outvec *outptr)
David Hu733d8f92019-09-23 15:32:40 +0800147{
148 psa_invec invecs[PSA_MAX_IOVEC];
149 psa_outvec outvecs[PSA_MAX_IOVEC];
Ken Liu0bed7e02022-02-10 12:38:07 +0800150 struct conn_handle_t *conn_handle;
Mingyang Sun783a59b2021-04-20 15:52:18 +0800151 struct service_t *service;
Summer Qinba2346e2019-11-12 16:26:31 +0800152 int i, j;
Summer Qin1ce712a2019-10-14 18:04:05 +0800153 int32_t client_id;
Mingyang Sun453ad402021-03-17 17:58:33 +0800154 uint32_t sid, version, index;
Mingyang Sune529e3b2021-07-12 14:46:30 +0800155 uint32_t privileged;
Mingyang Sun620c8562021-11-10 11:44:58 +0800156 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800157 bool ns_caller = tfm_spm_is_ns_caller();
Mingyang Suneeca4652021-07-15 15:19:16 +0800158 int32_t type = (int32_t)(int16_t)((ctrl_param & TYPE_MASK) >> TYPE_OFFSET);
159 size_t in_num = (size_t)((ctrl_param & IN_LEN_MASK) >> IN_LEN_OFFSET);
160 size_t out_num = (size_t)((ctrl_param & OUT_LEN_MASK) >> OUT_LEN_OFFSET);
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800161
162 /* The request type must be zero or positive. */
163 if (type < 0) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800164 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800165 }
David Hu733d8f92019-09-23 15:32:40 +0800166
Shawn Shanb222d892021-01-04 17:41:48 +0800167 /* It is a PROGRAMMER ERROR if in_len + out_len > PSA_MAX_IOVEC. */
David Hu733d8f92019-09-23 15:32:40 +0800168 if ((in_num > PSA_MAX_IOVEC) ||
169 (out_num > PSA_MAX_IOVEC) ||
170 (in_num + out_num > PSA_MAX_IOVEC)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800171 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800172 }
173
Kevin Peng385fda82021-08-18 10:41:19 +0800174 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800175
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800176 /* Allocate space from handle pool for static handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800177 if (IS_STATIC_HANDLE(handle)) {
Mingyang Sun453ad402021-03-17 17:58:33 +0800178 index = GET_INDEX_FROM_STATIC_HANDLE(handle);
Mingyang Sune8d38082021-03-30 18:34:40 +0800179
180 if (!IS_VALID_STATIC_HANDLE_IDX(index)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800181 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sune8d38082021-03-30 18:34:40 +0800182 }
183
Mingyang Sun453ad402021-03-17 17:58:33 +0800184 service = GET_STATELESS_SERVICE(index);
Mingyang Sun86213242021-07-14 10:26:43 +0800185 if (!service) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800186 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun86213242021-07-14 10:26:43 +0800187 }
188
Ken Liub3b2cb62021-05-22 00:39:28 +0800189 sid = service->p_ldinf->sid;
Mingyang Sun453ad402021-03-17 17:58:33 +0800190
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800191 /*
192 * It is a PROGRAMMER ERROR if the caller is not authorized to access
193 * the RoT Service.
194 */
195 if (tfm_spm_check_authorization(sid, service, ns_caller)
196 != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800197 return PSA_ERROR_CONNECTION_REFUSED;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800198 }
199
Mingyang Sun453ad402021-03-17 17:58:33 +0800200 version = GET_VERSION_FROM_STATIC_HANDLE(handle);
201
202 if (tfm_spm_check_client_version(service, version) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800203 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun453ad402021-03-17 17:58:33 +0800204 }
205
Mingyang Sun620c8562021-11-10 11:44:58 +0800206 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800207 conn_handle = tfm_spm_create_conn_handle(service, client_id);
Mingyang Sun620c8562021-11-10 11:44:58 +0800208 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800209
210 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800211 return PSA_ERROR_CONNECTION_BUSY;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800212 }
213
Mingyang Sun6d5dc3d2021-03-15 15:34:44 +0800214 conn_handle->rhandle = NULL;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800215 handle = tfm_spm_to_user_handle(conn_handle);
216 } else {
Sherry Zhanga4575f32022-02-23 15:45:51 +0800217#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800218 conn_handle = tfm_spm_to_handle_instance(handle);
219
220 /* It is a PROGRAMMER ERROR if an invalid handle was passed. */
221 if (tfm_spm_validate_conn_handle(conn_handle, client_id)
222 != SPM_SUCCESS) {
Xinyu 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 */
230 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800231 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800232 }
233
234 /*
235 * Return PSA_ERROR_PROGRAMMER_ERROR immediately for the connection
236 * has been terminated by the RoT Service.
237 */
238 if (conn_handle->status == TFM_HANDLE_STATUS_CONNECT_ERROR) {
239 return PSA_ERROR_PROGRAMMER_ERROR;
240 }
241
Ken Liu0bed7e02022-02-10 12:38:07 +0800242 service = conn_handle->service;
Shawn Shanb222d892021-01-04 17:41:48 +0800243
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800244 if (!service) {
245 /* FixMe: Need to implement a mechanism to resolve this failure. */
246 return PSA_ERROR_PROGRAMMER_ERROR;
247 }
Sherry Zhanga4575f32022-02-23 15:45:51 +0800248#else
249 return PSA_ERROR_PROGRAMMER_ERROR;
250#endif
David Hu733d8f92019-09-23 15:32:40 +0800251 }
252
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800253 privileged = GET_CURRENT_PARTITION_PRIVILEGED_MODE();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800254
Kevin Pengedb8ee42021-03-09 16:50:11 +0800255 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800256 * Read client invecs from the wrap input vector. It is a PROGRAMMER ERROR
David Hu733d8f92019-09-23 15:32:40 +0800257 * if the memory reference for the wrap input vector is invalid or not
258 * readable.
259 */
260 if (tfm_memory_check(inptr, in_num * sizeof(psa_invec), ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800261 TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800262 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800263 }
Summer Qinba2346e2019-11-12 16:26:31 +0800264
David Hu733d8f92019-09-23 15:32:40 +0800265 /*
266 * Read client outvecs from the wrap output vector and will update the
Shawn Shanb222d892021-01-04 17:41:48 +0800267 * actual length later. It is a PROGRAMMER ERROR if the memory reference for
David Hu733d8f92019-09-23 15:32:40 +0800268 * the wrap output vector is invalid or not read-write.
269 */
270 if (tfm_memory_check(outptr, out_num * sizeof(psa_outvec), ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800271 TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800272 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800273 }
274
Summer Qinf24dbb52020-07-23 14:53:54 +0800275 spm_memset(invecs, 0, sizeof(invecs));
276 spm_memset(outvecs, 0, sizeof(outvecs));
David Hu733d8f92019-09-23 15:32:40 +0800277
278 /* Copy the address out to avoid TOCTOU attacks. */
Summer Qinf24dbb52020-07-23 14:53:54 +0800279 spm_memcpy(invecs, inptr, in_num * sizeof(psa_invec));
280 spm_memcpy(outvecs, outptr, out_num * sizeof(psa_outvec));
David Hu733d8f92019-09-23 15:32:40 +0800281
282 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800283 * For client input vector, it is a PROGRAMMER ERROR if the provided payload
David Hu733d8f92019-09-23 15:32:40 +0800284 * memory reference was invalid or not readable.
285 */
286 for (i = 0; i < in_num; i++) {
287 if (tfm_memory_check(invecs[i].base, invecs[i].len, ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800288 TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800289 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800290 }
291 }
Summer Qinba2346e2019-11-12 16:26:31 +0800292
293 /*
294 * Clients must never overlap input parameters because of the risk of a
295 * double-fetch inconsistency.
296 * Overflow is checked in tfm_memory_check functions.
297 */
298 for (i = 0; i + 1 < in_num; i++) {
299 for (j = i+1; j < in_num; j++) {
TTornblom83d96372019-11-19 12:53:16 +0100300 if (!((char *) invecs[j].base + invecs[j].len <=
301 (char *) invecs[i].base ||
302 (char *) invecs[j].base >=
303 (char *) invecs[i].base + invecs[i].len)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800304 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qinba2346e2019-11-12 16:26:31 +0800305 }
306 }
307 }
308
David Hu733d8f92019-09-23 15:32:40 +0800309 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800310 * For client output vector, it is a PROGRAMMER ERROR if the provided
311 * payload memory reference was invalid or not read-write.
David Hu733d8f92019-09-23 15:32:40 +0800312 */
313 for (i = 0; i < out_num; i++) {
314 if (tfm_memory_check(outvecs[i].base, outvecs[i].len,
Ken Liubcae38b2021-01-20 15:47:44 +0800315 ns_caller, TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800316 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800317 }
318 }
319
Ken Liu0bed7e02022-02-10 12:38:07 +0800320 spm_fill_message(conn_handle, service, handle, type, client_id,
Summer Qin630c76b2020-05-20 10:32:58 +0800321 invecs, in_num, outvecs, out_num, outptr);
David Hu733d8f92019-09-23 15:32:40 +0800322
Ken Liu0bed7e02022-02-10 12:38:07 +0800323 return backend_instance.messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800324}
325
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800326/* Following PSA APIs are only needed by connection-based services */
327#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
328
329psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version)
330{
331 struct service_t *service;
Mingyang Suna09adda2022-02-16 18:11:33 +0800332 struct conn_handle_t *conn_handle;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800333 int32_t client_id;
334 psa_handle_t handle;
335 bool ns_caller = tfm_spm_is_ns_caller();
336 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
337
338 /*
339 * It is a PROGRAMMER ERROR if the RoT Service does not exist on the
340 * platform.
341 */
342 service = tfm_spm_get_service_by_sid(sid);
343 if (!service) {
344 return PSA_ERROR_CONNECTION_REFUSED;
345 }
346
347 /* It is a PROGRAMMER ERROR if connecting to a stateless service. */
348 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
349 return PSA_ERROR_PROGRAMMER_ERROR;
350 }
351
352 /*
353 * It is a PROGRAMMER ERROR if the caller is not authorized to access the
354 * RoT Service.
355 */
356 if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) {
357 return PSA_ERROR_CONNECTION_REFUSED;
358 }
359
360 /*
361 * It is a PROGRAMMER ERROR if the version of the RoT Service requested is
362 * not supported on the platform.
363 */
364 if (tfm_spm_check_client_version(service, version) != SPM_SUCCESS) {
365 return PSA_ERROR_CONNECTION_REFUSED;
366 }
367
368 client_id = tfm_spm_get_client_id(ns_caller);
369
370 /*
371 * Create connection handle here since it is possible to return the error
372 * code to client when creation fails.
373 */
374 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Suna09adda2022-02-16 18:11:33 +0800375 conn_handle = tfm_spm_create_conn_handle(service, client_id);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800376 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Suna09adda2022-02-16 18:11:33 +0800377 if (!conn_handle) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800378 return PSA_ERROR_CONNECTION_BUSY;
379 }
380
Mingyang Suna09adda2022-02-16 18:11:33 +0800381 handle = tfm_spm_to_user_handle(conn_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800382 /* No input or output needed for connect message */
Mingyang Suna09adda2022-02-16 18:11:33 +0800383 spm_fill_message(conn_handle, service, handle, PSA_IPC_CONNECT,
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800384 client_id, NULL, 0, NULL, 0, NULL);
385
Mingyang Suna09adda2022-02-16 18:11:33 +0800386 return backend_instance.messaging(service, conn_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800387}
388
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800389psa_status_t tfm_spm_client_psa_close(psa_handle_t handle)
David Hu733d8f92019-09-23 15:32:40 +0800390{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800391 struct service_t *service;
Ken Liu0bed7e02022-02-10 12:38:07 +0800392 struct conn_handle_t *conn_handle;
Summer Qin1ce712a2019-10-14 18:04:05 +0800393 int32_t client_id;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800394 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800395
396 /* It will have no effect if called with the NULL handle */
397 if (handle == PSA_NULL_HANDLE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800398 return PSA_SUCCESS;
David Hu733d8f92019-09-23 15:32:40 +0800399 }
400
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800401 /* It is a PROGRAMMER ERROR if called with a stateless handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800402 if (IS_STATIC_HANDLE(handle)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800403 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800404 }
405
Kevin Peng385fda82021-08-18 10:41:19 +0800406 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800407
Summer Qin373feb12020-03-27 15:35:33 +0800408 conn_handle = tfm_spm_to_handle_instance(handle);
David Hu733d8f92019-09-23 15:32:40 +0800409 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800410 * It is a PROGRAMMER ERROR if an invalid handle was provided that is not
411 * the null handle.
David Hu733d8f92019-09-23 15:32:40 +0800412 */
Ken Liubcae38b2021-01-20 15:47:44 +0800413 if (tfm_spm_validate_conn_handle(conn_handle, client_id) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800414 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qin1ce712a2019-10-14 18:04:05 +0800415 }
Shawn Shanb222d892021-01-04 17:41:48 +0800416
Ken Liu0bed7e02022-02-10 12:38:07 +0800417 service = conn_handle->service;
David Hu733d8f92019-09-23 15:32:40 +0800418 if (!service) {
419 /* FixMe: Need to implement one mechanism to resolve this failure. */
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800420 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800421 }
422
Shawn Shanb222d892021-01-04 17:41:48 +0800423 /*
424 * It is a PROGRAMMER ERROR if the connection is currently handling a
425 * request.
426 */
Summer Qin630c76b2020-05-20 10:32:58 +0800427 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800428 return PSA_ERROR_PROGRAMMER_ERROR;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800429 }
430
David Hu733d8f92019-09-23 15:32:40 +0800431 /* No input or output needed for close message */
Ken Liu0bed7e02022-02-10 12:38:07 +0800432 spm_fill_message(conn_handle, service, handle, PSA_IPC_DISCONNECT,
433 client_id, NULL, 0, NULL, 0, NULL);
David Hu733d8f92019-09-23 15:32:40 +0800434
Ken Liu0bed7e02022-02-10 12:38:07 +0800435 return backend_instance.messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800436}
Mingyang Sunb26b2802021-07-07 11:25:00 +0800437
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800438#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
439
Mingyang Suneeca4652021-07-15 15:19:16 +0800440/* PSA Partition API function body */
441
Kevin Peng613b4172022-02-15 14:41:44 +0800442#if CONFIG_TFM_SPM_BACKEND_IPC == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800443psa_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 Pengcdf3ae22022-01-19 14:53:52 +0800454 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800455
456 /*
457 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
458 * signals.
459 */
460 if ((partition->signals_allowed & signal_mask) == 0) {
461 tfm_core_panic();
462 }
463
464 /*
Ken Liu5d73c872021-08-19 19:23:17 +0800465 * thrd_wait_on() blocks the caller thread if no signals are available.
Mingyang Sunb26b2802021-07-07 11:25:00 +0800466 * In this case, the return value of this function is temporary set into
467 * runtime context. After new signal(s) are available, the return value
468 * is updated with the available signal(s) and blocked thread gets to run.
469 */
470 if (timeout == PSA_BLOCK &&
471 (partition->signals_asserted & signal_mask) == 0) {
472 partition->signals_waiting = signal_mask;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800473 thrd_wait_on(&partition->waitobj, CURRENT_THREAD);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800474 }
475
476 return partition->signals_asserted & signal_mask;
477}
478
479psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg)
480{
Mingyang Suna09adda2022-02-16 18:11:33 +0800481 struct conn_handle_t *handle = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800482 struct partition_t *partition = NULL;
483 uint32_t privileged;
484
485 /*
486 * Only one message could be retrieved every time for psa_get(). It is a
487 * fatal error if the input signal has more than a signal bit set.
488 */
489 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
490 tfm_core_panic();
491 }
492
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800493 partition = GET_CURRENT_COMPONENT();
494
Kevin Penga40d29f2022-01-19 14:44:34 +0800495 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800496
497 /*
498 * Write the message to the service buffer. It is a fatal error if the
499 * input msg pointer is not a valid memory reference or not read-write.
500 */
501 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
502 privileged) != SPM_SUCCESS) {
503 tfm_core_panic();
504 }
505
506 /*
507 * It is a fatal error if the caller call psa_get() when no message has
508 * been set. The caller must call this function after an RoT Service signal
509 * is returned by psa_wait().
510 */
511 if (partition->signals_asserted == 0) {
512 tfm_core_panic();
513 }
514
515 /*
516 * It is a fatal error if the RoT Service signal is not currently asserted.
517 */
518 if ((partition->signals_asserted & signal) == 0) {
519 tfm_core_panic();
520 }
521
522 /*
523 * Get message by signal from partition. It is a fatal error if getting
524 * failed, which means the input signal is not correspond to an RoT service.
525 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800526 handle = spm_get_handle_by_signal(partition, signal);
527 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800528 return PSA_ERROR_DOES_NOT_EXIST;
529 }
530
Mingyang Suna09adda2022-02-16 18:11:33 +0800531 handle->status = TFM_HANDLE_STATUS_ACTIVE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800532
Mingyang Suna09adda2022-02-16 18:11:33 +0800533 spm_memcpy(msg, &handle->msg, sizeof(psa_msg_t));
Mingyang Sunb26b2802021-07-07 11:25:00 +0800534
535 return PSA_SUCCESS;
536}
Mingyang Sun4609ac72022-02-08 18:56:35 +0800537#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800538
Mingyang Sunb26b2802021-07-07 11:25:00 +0800539size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
540 void *buffer, size_t num_bytes)
541{
542 size_t bytes;
Mingyang Suna09adda2022-02-16 18:11:33 +0800543 struct conn_handle_t *handle = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800544 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800545
546 /* It is a fatal error if message handle is invalid */
Mingyang Suna09adda2022-02-16 18:11:33 +0800547 handle = spm_get_handle_by_user_handle(msg_handle);
548 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800549 tfm_core_panic();
550 }
551
Mingyang Suna09adda2022-02-16 18:11:33 +0800552 priv_mode = GET_PARTITION_PRIVILEGED_MODE(
553 handle->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800554
555 /*
556 * It is a fatal error if message handle does not refer to a request
557 * message
558 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800559 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800560 tfm_core_panic();
561 }
562
563 /*
564 * It is a fatal error if invec_idx is equal to or greater than
565 * PSA_MAX_IOVEC
566 */
567 if (invec_idx >= PSA_MAX_IOVEC) {
568 tfm_core_panic();
569 }
570
571 /* There was no remaining data in this input vector */
Mingyang Suna09adda2022-02-16 18:11:33 +0800572 if (handle->msg.in_size[invec_idx] == 0) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800573 return 0;
574 }
575
Shawn Shan038348e2021-09-08 17:11:04 +0800576#if PSA_FRAMEWORK_HAS_MM_IOVEC
577 /*
578 * It is a fatal error if the input vector has already been mapped using
579 * psa_map_invec().
580 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800581 if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800582 tfm_core_panic();
583 }
584
Mingyang Suna09adda2022-02-16 18:11:33 +0800585 SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800586#endif
587
Mingyang Sunb26b2802021-07-07 11:25:00 +0800588 /*
589 * Copy the client data to the service buffer. It is a fatal error
590 * if the memory reference for buffer is invalid or not read-write.
591 */
592 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800593 TFM_MEMORY_ACCESS_RW, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800594 tfm_core_panic();
595 }
596
Mingyang Suna09adda2022-02-16 18:11:33 +0800597 bytes = num_bytes > handle->msg.in_size[invec_idx] ?
598 handle->msg.in_size[invec_idx] : num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800599
Mingyang Suna09adda2022-02-16 18:11:33 +0800600 spm_memcpy(buffer, handle->invec[invec_idx].base, bytes);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800601
602 /* There maybe some remaining data */
Mingyang Suna09adda2022-02-16 18:11:33 +0800603 handle->invec[invec_idx].base =
604 (char *)handle->invec[invec_idx].base + bytes;
605 handle->msg.in_size[invec_idx] -= bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800606
607 return bytes;
608}
609
610size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx,
611 size_t num_bytes)
612{
Mingyang Suna09adda2022-02-16 18:11:33 +0800613 struct conn_handle_t *handle = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800614
615 /* It is a fatal error if message handle is invalid */
Mingyang Suna09adda2022-02-16 18:11:33 +0800616 handle = spm_get_handle_by_user_handle(msg_handle);
617 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800618 tfm_core_panic();
619 }
620
621 /*
622 * It is a fatal error if message handle does not refer to a request
623 * message
624 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800625 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800626 tfm_core_panic();
627 }
628
629 /*
630 * It is a fatal error if invec_idx is equal to or greater than
631 * PSA_MAX_IOVEC
632 */
633 if (invec_idx >= PSA_MAX_IOVEC) {
634 tfm_core_panic();
635 }
636
637 /* There was no remaining data in this input vector */
Mingyang Suna09adda2022-02-16 18:11:33 +0800638 if (handle->msg.in_size[invec_idx] == 0) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800639 return 0;
640 }
641
Shawn Shan038348e2021-09-08 17:11:04 +0800642#if PSA_FRAMEWORK_HAS_MM_IOVEC
643 /*
644 * It is a fatal error if the input vector has already been mapped using
645 * psa_map_invec().
646 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800647 if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800648 tfm_core_panic();
649 }
650
Mingyang Suna09adda2022-02-16 18:11:33 +0800651 SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800652#endif
653
Mingyang Sunb26b2802021-07-07 11:25:00 +0800654 /*
655 * If num_bytes is greater than the remaining size of the input vector then
656 * the remaining size of the input vector is used.
657 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800658 if (num_bytes > handle->msg.in_size[invec_idx]) {
659 num_bytes = handle->msg.in_size[invec_idx];
Mingyang Sunb26b2802021-07-07 11:25:00 +0800660 }
661
662 /* There maybe some remaining data */
Mingyang Suna09adda2022-02-16 18:11:33 +0800663 handle->invec[invec_idx].base =
664 (char *)handle->invec[invec_idx].base + num_bytes;
665 handle->msg.in_size[invec_idx] -= num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800666
667 return num_bytes;
668}
669
670void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
671 const void *buffer, size_t num_bytes)
672{
Mingyang Suna09adda2022-02-16 18:11:33 +0800673 struct conn_handle_t *handle = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800674 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800675
676 /* It is a fatal error if message handle is invalid */
Mingyang Suna09adda2022-02-16 18:11:33 +0800677 handle = spm_get_handle_by_user_handle(msg_handle);
678 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800679 tfm_core_panic();
680 }
681
Mingyang Suna09adda2022-02-16 18:11:33 +0800682 priv_mode = GET_PARTITION_PRIVILEGED_MODE(
683 handle->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800684
685 /*
686 * It is a fatal error if message handle does not refer to a request
687 * message
688 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800689 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800690 tfm_core_panic();
691 }
692
693 /*
694 * It is a fatal error if outvec_idx is equal to or greater than
695 * PSA_MAX_IOVEC
696 */
697 if (outvec_idx >= PSA_MAX_IOVEC) {
698 tfm_core_panic();
699 }
700
701 /*
702 * It is a fatal error if the call attempts to write data past the end of
703 * the client output vector
704 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800705 if (num_bytes > handle->msg.out_size[outvec_idx] -
706 handle->outvec[outvec_idx].len) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800707 tfm_core_panic();
708 }
709
Shawn Shan038348e2021-09-08 17:11:04 +0800710#if PSA_FRAMEWORK_HAS_MM_IOVEC
711 /*
712 * It is a fatal error if the output vector has already been mapped using
713 * psa_map_outvec().
714 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800715 if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800716 tfm_core_panic();
717 }
718
Mingyang Suna09adda2022-02-16 18:11:33 +0800719 SET_IOVEC_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800720#endif
721
Mingyang Sunb26b2802021-07-07 11:25:00 +0800722 /*
723 * Copy the service buffer to client outvecs. It is a fatal error
724 * if the memory reference for buffer is invalid or not readable.
725 */
726 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800727 TFM_MEMORY_ACCESS_RO, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800728 tfm_core_panic();
729 }
730
Mingyang Suna09adda2022-02-16 18:11:33 +0800731 spm_memcpy((char *)handle->outvec[outvec_idx].base +
732 handle->outvec[outvec_idx].len, buffer, num_bytes);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800733
734 /* Update the write number */
Mingyang Suna09adda2022-02-16 18:11:33 +0800735 handle->outvec[outvec_idx].len += num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800736}
737
Ken Liuf8c7e532022-02-10 15:03:04 +0800738psa_status_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle,
739 psa_status_t status)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800740{
Ken Liu0bed7e02022-02-10 12:38:07 +0800741 struct service_t *service;
Mingyang Suna09adda2022-02-16 18:11:33 +0800742 struct conn_handle_t *handle;
Ken Liuf8c7e532022-02-10 15:03:04 +0800743 psa_status_t ret = PSA_SUCCESS;
Mingyang Sun620c8562021-11-10 11:44:58 +0800744 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800745
746 /* It is a fatal error if message handle is invalid */
Mingyang Suna09adda2022-02-16 18:11:33 +0800747 handle = spm_get_handle_by_user_handle(msg_handle);
748 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800749 tfm_core_panic();
750 }
751
752 /*
753 * RoT Service information is needed in this function, stored it in message
754 * body structure. Only two parameters are passed in this function: handle
755 * and status, so it is useful and simply to do like this.
756 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800757 service = handle->service;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800758 if (!service) {
759 tfm_core_panic();
760 }
761
Mingyang Suna09adda2022-02-16 18:11:33 +0800762 switch (handle->msg.type) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800763 case PSA_IPC_CONNECT:
764 /*
765 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
766 * input status is PSA_SUCCESS. Others return values are based on the
767 * input status.
768 */
769 if (status == PSA_SUCCESS) {
770 ret = msg_handle;
771 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
772 /* Refuse the client connection, indicating a permanent error. */
Mingyang Suna09adda2022-02-16 18:11:33 +0800773 tfm_spm_free_conn_handle(service, handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800774 ret = PSA_ERROR_CONNECTION_REFUSED;
775 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
776 /* Fail the client connection, indicating a transient error. */
777 ret = PSA_ERROR_CONNECTION_BUSY;
778 } else {
779 tfm_core_panic();
780 }
781 break;
782 case PSA_IPC_DISCONNECT:
783 /* Service handle is not used anymore */
Mingyang Suna09adda2022-02-16 18:11:33 +0800784 tfm_spm_free_conn_handle(service, handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800785
786 /*
787 * If the message type is PSA_IPC_DISCONNECT, then the status code is
788 * ignored
789 */
790 break;
791 default:
Mingyang Suna09adda2022-02-16 18:11:33 +0800792 if (handle->msg.type >= PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +0800793
794#if PSA_FRAMEWORK_HAS_MM_IOVEC
795
796 /*
797 * If the unmapped function is not called for an input/output vector
798 * that has been mapped, the framework will remove the mapping.
799 */
800 int i;
801
802 for (i = 0; i < PSA_MAX_IOVEC * 2; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800803 if (IOVEC_IS_MAPPED(handle, i) &&
804 (!IOVEC_IS_UNMAPPED(handle, i))) {
805 SET_IOVEC_UNMAPPED(handle, i);
Shawn Shan038348e2021-09-08 17:11:04 +0800806 /*
807 * Any output vectors that are still mapped will report that
808 * zero bytes have been written.
809 */
810 if (i >= OUTVEC_IDX_BASE) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800811 handle->outvec[i - OUTVEC_IDX_BASE].len = 0;
Shawn Shan038348e2021-09-08 17:11:04 +0800812 }
813 }
814 }
815
816#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800817 /* Reply to a request message. Return values are based on status */
818 ret = status;
819 /*
820 * The total number of bytes written to a single parameter must be
821 * reported to the client by updating the len member of the
822 * psa_outvec structure for the parameter before returning from
823 * psa_call().
824 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800825 update_caller_outvec_len(handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800826 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800827 tfm_spm_free_conn_handle(service, handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800828 }
829 } else {
830 tfm_core_panic();
831 }
832 }
833
834 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
835 /*
836 * If the source of the programmer error is a Secure Partition, the SPM
837 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
838 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800839 if (TFM_CLIENT_ID_IS_NS(handle->msg.client_id)) {
840 handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800841 } else {
842 tfm_core_panic();
843 }
844 } else {
Mingyang Suna09adda2022-02-16 18:11:33 +0800845 handle->status = TFM_HANDLE_STATUS_IDLE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800846 }
847
Mingyang Sun620c8562021-11-10 11:44:58 +0800848 /*
849 * TODO: It can be optimized further by moving critical section protection
850 * to mailbox. Also need to check implementation when secure context is
851 * involved.
852 */
853 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Suna09adda2022-02-16 18:11:33 +0800854 ret = backend_instance.replying(handle, ret);
Mingyang Sun620c8562021-11-10 11:44:58 +0800855 CRITICAL_SECTION_LEAVE(cs_assert);
856
857 return ret;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800858}
859
Kevin Peng613b4172022-02-15 14:41:44 +0800860#if CONFIG_TFM_DOORBELL_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800861void tfm_spm_partition_psa_notify(int32_t partition_id)
862{
Ken Liu5d73c872021-08-19 19:23:17 +0800863 struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id);
864
865 spm_assert_signal(p_pt, PSA_DOORBELL);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800866}
867
868void tfm_spm_partition_psa_clear(void)
869{
Ken Liu92ede9f2021-10-20 09:35:00 +0800870 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800871 struct partition_t *partition = NULL;
872
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800873 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800874
875 /*
876 * It is a fatal error if the Secure Partition's doorbell signal is not
877 * currently asserted.
878 */
879 if ((partition->signals_asserted & PSA_DOORBELL) == 0) {
880 tfm_core_panic();
881 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800882
883 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800884 partition->signals_asserted &= ~PSA_DOORBELL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800885 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800886}
Kevin Peng613b4172022-02-15 14:41:44 +0800887#endif /* CONFIG_TFM_DOORBELL_API == 1 */
Mingyang Sunb26b2802021-07-07 11:25:00 +0800888
Mingyang Sunb26b2802021-07-07 11:25:00 +0800889void tfm_spm_partition_psa_panic(void)
890{
891 /*
892 * PSA FF recommends that the SPM causes the system to restart when a secure
893 * partition panics.
894 */
895 tfm_hal_system_reset();
896}
897
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800898/* psa_set_rhandle is only needed by connection-based services */
899#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
900
901void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle)
902{
Mingyang Suna09adda2022-02-16 18:11:33 +0800903 struct conn_handle_t *handle;
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800904
905 /* It is a fatal error if message handle is invalid */
Mingyang Suna09adda2022-02-16 18:11:33 +0800906 handle = spm_get_handle_by_user_handle(msg_handle);
907 if (!handle) {
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800908 tfm_core_panic();
909 }
910
911 /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */
Mingyang Suna09adda2022-02-16 18:11:33 +0800912 if (SERVICE_IS_STATELESS(handle->service->p_ldinf->flags)) {
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800913 tfm_core_panic();
914 }
915
Mingyang Suna09adda2022-02-16 18:11:33 +0800916 handle->msg.rhandle = rhandle;
917 handle->rhandle = rhandle;
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800918}
919
920#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
921
922#if CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1
Kevin Peng67a89fd2021-11-25 11:22:02 +0800923void tfm_spm_partition_psa_irq_enable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800924{
925 struct partition_t *partition;
926 struct irq_load_info_t *irq_info;
927
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800928 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800929
930 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
931 if (!irq_info) {
932 tfm_core_panic();
933 }
934
Kevin Pengd399a1f2021-09-08 15:33:14 +0800935 tfm_hal_irq_enable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800936}
937
Kevin Peng67a89fd2021-11-25 11:22:02 +0800938psa_irq_status_t tfm_spm_partition_psa_irq_disable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800939{
940 struct partition_t *partition;
941 struct irq_load_info_t *irq_info;
942
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800943 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800944
945 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
946 if (!irq_info) {
947 tfm_core_panic();
948 }
949
Kevin Pengd399a1f2021-09-08 15:33:14 +0800950 tfm_hal_irq_disable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800951
952 return 1;
953}
954
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800955/* This API is only used for FLIH. */
956#if CONFIG_TFM_FLIH_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800957void tfm_spm_partition_psa_reset_signal(psa_signal_t irq_signal)
958{
Ken Liu92ede9f2021-10-20 09:35:00 +0800959 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800960 struct irq_load_info_t *irq_info;
961 struct partition_t *partition;
962
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800963 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800964
965 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
966 if (!irq_info) {
967 tfm_core_panic();
968 }
969
970 if (!irq_info->flih_func) {
971 /* This API is for FLIH IRQs only */
972 tfm_core_panic();
973 }
974
975 if ((partition->signals_asserted & irq_signal) == 0) {
976 /* The signal is not asserted */
977 tfm_core_panic();
978 }
979
Ken Liu92ede9f2021-10-20 09:35:00 +0800980 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800981 partition->signals_asserted &= ~irq_signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800982 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800983}
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800984#endif
Shawn Shan038348e2021-09-08 17:11:04 +0800985
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800986/* This API is only used for SLIH. */
987#if CONFIG_TFM_SLIH_API == 1
988void tfm_spm_partition_psa_eoi(psa_signal_t irq_signal)
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800989{
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800990 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
991 struct irq_load_info_t *irq_info = NULL;
992 struct partition_t *partition = NULL;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800993
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800994 partition = GET_CURRENT_COMPONENT();
995
996 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
997 /* It is a fatal error if passed signal is not an interrupt signal. */
998 if (!irq_info) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800999 tfm_core_panic();
1000 }
1001
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001002 if (irq_info->flih_func) {
1003 /* This API is for SLIH IRQs only */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001004 tfm_core_panic();
1005 }
1006
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001007 /* It is a fatal error if passed signal is not currently asserted */
1008 if ((partition->signals_asserted & irq_signal) == 0) {
1009 tfm_core_panic();
1010 }
1011
1012 CRITICAL_SECTION_ENTER(cs_assert);
1013 partition->signals_asserted &= ~irq_signal;
1014 CRITICAL_SECTION_LEAVE(cs_assert);
1015
1016 tfm_hal_irq_clear_pending(irq_info->source);
1017 tfm_hal_irq_enable(irq_info->source);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001018}
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001019#endif
1020#endif /* CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001021
Shawn Shan038348e2021-09-08 17:11:04 +08001022#if PSA_FRAMEWORK_HAS_MM_IOVEC
1023
1024const void *tfm_spm_partition_psa_map_invec(psa_handle_t msg_handle,
1025 uint32_t invec_idx)
1026{
Mingyang Suna09adda2022-02-16 18:11:33 +08001027 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001028 uint32_t privileged;
1029 struct partition_t *partition = NULL;
1030
1031 /* It is a fatal error if message handle is invalid */
Mingyang Suna09adda2022-02-16 18:11:33 +08001032 handle = spm_get_handle_by_user_handle(msg_handle);
1033 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001034 tfm_core_panic();
1035 }
1036
Mingyang Suna09adda2022-02-16 18:11:33 +08001037 partition = handle->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001038 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001039
1040 /*
1041 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1042 * Service that received the message.
1043 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001044 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001045 tfm_core_panic();
1046 }
1047
1048 /*
1049 * It is a fatal error if message handle does not refer to a request
1050 * message.
1051 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001052 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001053 tfm_core_panic();
1054 }
1055
1056 /*
1057 * It is a fatal error if invec_idx is equal to or greater than
1058 * PSA_MAX_IOVEC.
1059 */
1060 if (invec_idx >= PSA_MAX_IOVEC) {
1061 tfm_core_panic();
1062 }
1063
1064 /* It is a fatal error if the input vector has length zero. */
Mingyang Suna09adda2022-02-16 18:11:33 +08001065 if (handle->msg.in_size[invec_idx] == 0) {
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 mapped using
1071 * psa_map_invec().
1072 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001073 if (IOVEC_IS_MAPPED(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 input vector has already been accessed
1079 * using psa_read() or psa_skip().
1080 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001081 if (IOVEC_IS_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001082 tfm_core_panic();
1083 }
1084
1085 /*
1086 * It is a fatal error if the memory reference for the wrap input vector is
1087 * invalid or not readable.
1088 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001089 if (tfm_memory_check(handle->invec[invec_idx].base,
1090 handle->invec[invec_idx].len,
1091 false, TFM_MEMORY_ACCESS_RO,
1092 privileged) != SPM_SUCCESS) {
Shawn Shan038348e2021-09-08 17:11:04 +08001093 tfm_core_panic();
1094 }
1095
Mingyang Suna09adda2022-02-16 18:11:33 +08001096 SET_IOVEC_MAPPED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001097
Mingyang Suna09adda2022-02-16 18:11:33 +08001098 return handle->invec[invec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001099}
1100
1101void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle,
1102 uint32_t invec_idx)
1103{
Mingyang Suna09adda2022-02-16 18:11:33 +08001104 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001105
1106 /* It is a fatal error if message handle is invalid */
Mingyang Suna09adda2022-02-16 18:11:33 +08001107 handle = spm_get_handle_by_user_handle(msg_handle);
1108 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001109 tfm_core_panic();
1110 }
1111
1112 /*
1113 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1114 * Service that received the message.
1115 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001116 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001117 tfm_core_panic();
1118 }
1119
1120 /*
1121 * It is a fatal error if message handle does not refer to a request
1122 * message.
1123 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001124 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001125 tfm_core_panic();
1126 }
1127
1128 /*
1129 * It is a fatal error if invec_idx is equal to or greater than
1130 * PSA_MAX_IOVEC.
1131 */
1132 if (invec_idx >= PSA_MAX_IOVEC) {
1133 tfm_core_panic();
1134 }
1135
1136 /*
1137 * It is a fatal error if The input vector has not been mapped by a call to
1138 * psa_map_invec().
1139 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001140 if (!IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001141 tfm_core_panic();
1142 }
1143
1144 /*
1145 * It is a fatal error if the input vector has already been unmapped by a
1146 * call to psa_unmap_invec().
1147 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001148 if (IOVEC_IS_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001149 tfm_core_panic();
1150 }
1151
Mingyang Suna09adda2022-02-16 18:11:33 +08001152 SET_IOVEC_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001153}
1154
1155void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle,
1156 uint32_t outvec_idx)
1157{
Mingyang Suna09adda2022-02-16 18:11:33 +08001158 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001159 uint32_t privileged;
1160 struct partition_t *partition = NULL;
1161
1162 /* It is a fatal error if message handle is invalid */
Mingyang Suna09adda2022-02-16 18:11:33 +08001163 handle = spm_get_handle_by_user_handle(msg_handle);
1164 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001165 tfm_core_panic();
1166 }
1167
Mingyang Suna09adda2022-02-16 18:11:33 +08001168 partition = handle->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001169 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001170
1171 /*
1172 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1173 * Service that received the message.
1174 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001175 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001176 tfm_core_panic();
1177 }
1178
1179 /*
1180 * It is a fatal error if message handle does not refer to a request
1181 * message.
1182 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001183 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001184 tfm_core_panic();
1185 }
1186
1187 /*
1188 * It is a fatal error if outvec_idx is equal to or greater than
1189 * PSA_MAX_IOVEC.
1190 */
1191 if (outvec_idx >= PSA_MAX_IOVEC) {
1192 tfm_core_panic();
1193 }
1194
1195 /* It is a fatal error if the output vector has length zero. */
Mingyang Suna09adda2022-02-16 18:11:33 +08001196 if (handle->msg.out_size[outvec_idx] == 0) {
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 mapped using
1202 * psa_map_outvec().
1203 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001204 if (IOVEC_IS_MAPPED(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 has already been accessed
1210 * using psa_write().
1211 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001212 if (IOVEC_IS_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001213 tfm_core_panic();
1214 }
1215
1216 /*
1217 * It is a fatal error if the output vector is invalid or not read-write.
1218 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001219 if (tfm_memory_check(handle->outvec[outvec_idx].base,
1220 handle->outvec[outvec_idx].len, false,
Shawn Shan038348e2021-09-08 17:11:04 +08001221 TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
1222 tfm_core_panic();
1223 }
Mingyang Suna09adda2022-02-16 18:11:33 +08001224 SET_IOVEC_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001225
Mingyang Suna09adda2022-02-16 18:11:33 +08001226 return handle->outvec[outvec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001227}
1228
1229void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle,
1230 uint32_t outvec_idx, size_t len)
1231{
Mingyang Suna09adda2022-02-16 18:11:33 +08001232 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001233
1234 /* It is a fatal error if message handle is invalid */
Mingyang Suna09adda2022-02-16 18:11:33 +08001235 handle = spm_get_handle_by_user_handle(msg_handle);
1236 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001237 tfm_core_panic();
1238 }
1239
1240 /*
1241 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1242 * Service that received the message.
1243 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001244 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001245 tfm_core_panic();
1246 }
1247
1248 /*
1249 * It is a fatal error if message handle does not refer to a request
1250 * message.
1251 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001252 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001253 tfm_core_panic();
1254 }
1255
1256 /*
1257 * It is a fatal error if outvec_idx is equal to or greater than
1258 * PSA_MAX_IOVEC.
1259 */
1260 if (outvec_idx >= PSA_MAX_IOVEC) {
1261 tfm_core_panic();
1262 }
1263
1264 /*
1265 * It is a fatal error if len is greater than the output vector size.
1266 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001267 if (len > handle->msg.out_size[outvec_idx]) {
Shawn Shan038348e2021-09-08 17:11:04 +08001268 tfm_core_panic();
1269 }
1270
1271 /*
1272 * It is a fatal error if The output vector has not been mapped by a call to
1273 * psa_map_outvec().
1274 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001275 if (!IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001276 tfm_core_panic();
1277 }
1278
1279 /*
1280 * It is a fatal error if the output vector has already been unmapped by a
1281 * call to psa_unmap_outvec().
1282 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001283 if (IOVEC_IS_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001284 tfm_core_panic();
1285 }
1286
Mingyang Suna09adda2022-02-16 18:11:33 +08001287 SET_IOVEC_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001288
1289 /* Update the write number */
Mingyang Suna09adda2022-02-16 18:11:33 +08001290 handle->outvec[outvec_idx].len = len;
Shawn Shan038348e2021-09-08 17:11:04 +08001291}
1292
1293#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */