blob: 98c3824b805bf2bc2f9a1bfcc869a01cb0ee1865 [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);
Kevin Penge61e7052022-01-27 14:57:06 +0800207 conn_handle = tfm_spm_create_conn_handle();
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 /* It is a PROGRAMMER ERROR if an invalid handle was passed. */
Kevin Penge61e7052022-01-27 14:57:06 +0800219 conn_handle = spm_get_handle_by_client_handle(handle, client_id);
220 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800221 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800222 }
223
224 /*
225 * It is a PROGRAMMER ERROR if the connection is currently
226 * handling a request.
227 */
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800228 if (conn_handle->status != TFM_HANDLE_STATUS_IDLE) {
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800229 return PSA_ERROR_PROGRAMMER_ERROR;
230 }
231
Ken Liu0bed7e02022-02-10 12:38:07 +0800232 service = conn_handle->service;
Shawn Shanb222d892021-01-04 17:41:48 +0800233
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800234 if (!service) {
235 /* FixMe: Need to implement a mechanism to resolve this failure. */
236 return PSA_ERROR_PROGRAMMER_ERROR;
237 }
Sherry Zhanga4575f32022-02-23 15:45:51 +0800238#else
239 return PSA_ERROR_PROGRAMMER_ERROR;
240#endif
David Hu733d8f92019-09-23 15:32:40 +0800241 }
242
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800243 privileged = GET_CURRENT_PARTITION_PRIVILEGED_MODE();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800244
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 */
250 if (tfm_memory_check(inptr, in_num * sizeof(psa_invec), ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800251 TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800252 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800253 }
Summer Qinba2346e2019-11-12 16:26:31 +0800254
David Hu733d8f92019-09-23 15:32:40 +0800255 /*
256 * Read client outvecs from the wrap output vector and will update the
Shawn Shanb222d892021-01-04 17:41:48 +0800257 * actual length later. It is a PROGRAMMER ERROR if the memory reference for
David Hu733d8f92019-09-23 15:32:40 +0800258 * the wrap output vector is invalid or not read-write.
259 */
260 if (tfm_memory_check(outptr, out_num * sizeof(psa_outvec), ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800261 TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800262 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800263 }
264
Summer Qinf24dbb52020-07-23 14:53:54 +0800265 spm_memset(invecs, 0, sizeof(invecs));
266 spm_memset(outvecs, 0, sizeof(outvecs));
David Hu733d8f92019-09-23 15:32:40 +0800267
268 /* Copy the address out to avoid TOCTOU attacks. */
Summer Qinf24dbb52020-07-23 14:53:54 +0800269 spm_memcpy(invecs, inptr, in_num * sizeof(psa_invec));
270 spm_memcpy(outvecs, outptr, out_num * sizeof(psa_outvec));
David Hu733d8f92019-09-23 15:32:40 +0800271
272 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800273 * For client input vector, it is a PROGRAMMER ERROR if the provided payload
David Hu733d8f92019-09-23 15:32:40 +0800274 * memory reference was invalid or not readable.
275 */
276 for (i = 0; i < in_num; i++) {
277 if (tfm_memory_check(invecs[i].base, invecs[i].len, ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800278 TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800279 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800280 }
281 }
Summer Qinba2346e2019-11-12 16:26:31 +0800282
283 /*
284 * Clients must never overlap input parameters because of the risk of a
285 * double-fetch inconsistency.
286 * Overflow is checked in tfm_memory_check functions.
287 */
288 for (i = 0; i + 1 < in_num; i++) {
289 for (j = i+1; j < in_num; j++) {
TTornblom83d96372019-11-19 12:53:16 +0100290 if (!((char *) invecs[j].base + invecs[j].len <=
291 (char *) invecs[i].base ||
292 (char *) invecs[j].base >=
293 (char *) invecs[i].base + invecs[i].len)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800294 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qinba2346e2019-11-12 16:26:31 +0800295 }
296 }
297 }
298
David Hu733d8f92019-09-23 15:32:40 +0800299 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800300 * For client output vector, it is a PROGRAMMER ERROR if the provided
301 * payload memory reference was invalid or not read-write.
David Hu733d8f92019-09-23 15:32:40 +0800302 */
303 for (i = 0; i < out_num; i++) {
304 if (tfm_memory_check(outvecs[i].base, outvecs[i].len,
Ken Liubcae38b2021-01-20 15:47:44 +0800305 ns_caller, TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800306 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800307 }
308 }
309
Ken Liu0bed7e02022-02-10 12:38:07 +0800310 spm_fill_message(conn_handle, service, handle, type, client_id,
Summer Qin630c76b2020-05-20 10:32:58 +0800311 invecs, in_num, outvecs, out_num, outptr);
David Hu733d8f92019-09-23 15:32:40 +0800312
Ken Liu0bed7e02022-02-10 12:38:07 +0800313 return backend_instance.messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800314}
315
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800316/* Following PSA APIs are only needed by connection-based services */
317#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
318
319psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version)
320{
321 struct service_t *service;
Mingyang Suna09adda2022-02-16 18:11:33 +0800322 struct conn_handle_t *conn_handle;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800323 int32_t client_id;
324 psa_handle_t handle;
325 bool ns_caller = tfm_spm_is_ns_caller();
326 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
327
328 /*
329 * It is a PROGRAMMER ERROR if the RoT Service does not exist on the
330 * platform.
331 */
332 service = tfm_spm_get_service_by_sid(sid);
333 if (!service) {
334 return PSA_ERROR_CONNECTION_REFUSED;
335 }
336
337 /* It is a PROGRAMMER ERROR if connecting to a stateless service. */
338 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
339 return PSA_ERROR_PROGRAMMER_ERROR;
340 }
341
342 /*
343 * It is a PROGRAMMER ERROR if the caller is not authorized to access the
344 * RoT Service.
345 */
346 if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) {
347 return PSA_ERROR_CONNECTION_REFUSED;
348 }
349
350 /*
351 * It is a PROGRAMMER ERROR if the version of the RoT Service requested is
352 * not supported on the platform.
353 */
354 if (tfm_spm_check_client_version(service, version) != SPM_SUCCESS) {
355 return PSA_ERROR_CONNECTION_REFUSED;
356 }
357
358 client_id = tfm_spm_get_client_id(ns_caller);
359
360 /*
361 * Create connection handle here since it is possible to return the error
362 * code to client when creation fails.
363 */
364 CRITICAL_SECTION_ENTER(cs_assert);
Kevin Penge61e7052022-01-27 14:57:06 +0800365 conn_handle = tfm_spm_create_conn_handle();
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800366 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Suna09adda2022-02-16 18:11:33 +0800367 if (!conn_handle) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800368 return PSA_ERROR_CONNECTION_BUSY;
369 }
370
Mingyang Suna09adda2022-02-16 18:11:33 +0800371 handle = tfm_spm_to_user_handle(conn_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800372 /* No input or output needed for connect message */
Mingyang Suna09adda2022-02-16 18:11:33 +0800373 spm_fill_message(conn_handle, service, handle, PSA_IPC_CONNECT,
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800374 client_id, NULL, 0, NULL, 0, NULL);
375
Mingyang Suna09adda2022-02-16 18:11:33 +0800376 return backend_instance.messaging(service, conn_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800377}
378
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800379psa_status_t tfm_spm_client_psa_close(psa_handle_t handle)
David Hu733d8f92019-09-23 15:32:40 +0800380{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800381 struct service_t *service;
Ken Liu0bed7e02022-02-10 12:38:07 +0800382 struct conn_handle_t *conn_handle;
Summer Qin1ce712a2019-10-14 18:04:05 +0800383 int32_t client_id;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800384 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800385
386 /* It will have no effect if called with the NULL handle */
387 if (handle == PSA_NULL_HANDLE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800388 return PSA_SUCCESS;
David Hu733d8f92019-09-23 15:32:40 +0800389 }
390
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800391 /* It is a PROGRAMMER ERROR if called with a stateless handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800392 if (IS_STATIC_HANDLE(handle)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800393 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800394 }
395
Kevin Peng385fda82021-08-18 10:41:19 +0800396 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800397
David Hu733d8f92019-09-23 15:32:40 +0800398 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800399 * It is a PROGRAMMER ERROR if an invalid handle was provided that is not
400 * the null handle.
David Hu733d8f92019-09-23 15:32:40 +0800401 */
Kevin Penge61e7052022-01-27 14:57:06 +0800402 conn_handle = spm_get_handle_by_client_handle(handle, client_id);
403 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800404 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qin1ce712a2019-10-14 18:04:05 +0800405 }
Shawn Shanb222d892021-01-04 17:41:48 +0800406
Ken Liu0bed7e02022-02-10 12:38:07 +0800407 service = conn_handle->service;
David Hu733d8f92019-09-23 15:32:40 +0800408 if (!service) {
409 /* FixMe: Need to implement one mechanism to resolve this failure. */
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800410 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800411 }
412
Shawn Shanb222d892021-01-04 17:41:48 +0800413 /*
414 * It is a PROGRAMMER ERROR if the connection is currently handling a
415 * request.
416 */
Summer Qin630c76b2020-05-20 10:32:58 +0800417 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800418 return PSA_ERROR_PROGRAMMER_ERROR;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800419 }
420
David Hu733d8f92019-09-23 15:32:40 +0800421 /* No input or output needed for close message */
Ken Liu0bed7e02022-02-10 12:38:07 +0800422 spm_fill_message(conn_handle, service, handle, PSA_IPC_DISCONNECT,
423 client_id, NULL, 0, NULL, 0, NULL);
David Hu733d8f92019-09-23 15:32:40 +0800424
Ken Liu0bed7e02022-02-10 12:38:07 +0800425 return backend_instance.messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800426}
Mingyang Sunb26b2802021-07-07 11:25:00 +0800427
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800428#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
429
Mingyang Suneeca4652021-07-15 15:19:16 +0800430/* PSA Partition API function body */
431
Kevin Pengdef92de2021-11-10 16:14:48 +0800432#if CONFIG_TFM_SPM_BACKEND_IPC == 1 \
433 || CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800434psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask,
435 uint32_t timeout)
436{
437 struct partition_t *partition = NULL;
438
439 /*
440 * Timeout[30:0] are reserved for future use.
441 * SPM must ignore the value of RES.
442 */
443 timeout &= PSA_TIMEOUT_MASK;
444
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800445 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800446
447 /*
448 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
449 * signals.
450 */
451 if ((partition->signals_allowed & signal_mask) == 0) {
452 tfm_core_panic();
453 }
454
455 /*
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800456 * backend_instance.wait() blocks the caller thread if no signals are
457 * available. In this case, the return value of this function is temporary
458 * set into runtime context. After new signal(s) are available, the return
459 * value is updated with the available signal(s) and blocked thread gets
460 * to run.
Mingyang Sunb26b2802021-07-07 11:25:00 +0800461 */
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800462 if (timeout == PSA_BLOCK) {
463 return backend_instance.wait(partition, signal_mask);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800464 }
465
466 return partition->signals_asserted & signal_mask;
467}
Kevin Pengdef92de2021-11-10 16:14:48 +0800468#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800469
Kevin Pengdef92de2021-11-10 16:14:48 +0800470#if CONFIG_TFM_SPM_BACKEND_IPC == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800471psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg)
472{
Mingyang Suna09adda2022-02-16 18:11:33 +0800473 struct conn_handle_t *handle = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800474 struct partition_t *partition = NULL;
475 uint32_t privileged;
476
477 /*
478 * Only one message could be retrieved every time for psa_get(). It is a
479 * fatal error if the input signal has more than a signal bit set.
480 */
481 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
482 tfm_core_panic();
483 }
484
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800485 partition = GET_CURRENT_COMPONENT();
486
Kevin Penga40d29f2022-01-19 14:44:34 +0800487 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800488
489 /*
490 * Write the message to the service buffer. It is a fatal error if the
491 * input msg pointer is not a valid memory reference or not read-write.
492 */
493 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
494 privileged) != SPM_SUCCESS) {
495 tfm_core_panic();
496 }
497
498 /*
499 * It is a fatal error if the caller call psa_get() when no message has
500 * been set. The caller must call this function after an RoT Service signal
501 * is returned by psa_wait().
502 */
503 if (partition->signals_asserted == 0) {
504 tfm_core_panic();
505 }
506
507 /*
508 * It is a fatal error if the RoT Service signal is not currently asserted.
509 */
510 if ((partition->signals_asserted & signal) == 0) {
511 tfm_core_panic();
512 }
513
514 /*
515 * Get message by signal from partition. It is a fatal error if getting
516 * failed, which means the input signal is not correspond to an RoT service.
517 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800518 handle = spm_get_handle_by_signal(partition, signal);
519 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800520 return PSA_ERROR_DOES_NOT_EXIST;
521 }
522
Mingyang Suna09adda2022-02-16 18:11:33 +0800523 spm_memcpy(msg, &handle->msg, sizeof(psa_msg_t));
Mingyang Sunb26b2802021-07-07 11:25:00 +0800524
525 return PSA_SUCCESS;
526}
Mingyang Sun4609ac72022-02-08 18:56:35 +0800527#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800528
Mingyang Sunb26b2802021-07-07 11:25:00 +0800529size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
530 void *buffer, size_t num_bytes)
531{
532 size_t bytes;
Mingyang Suna09adda2022-02-16 18:11:33 +0800533 struct conn_handle_t *handle = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800534 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800535
536 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800537 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800538 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800539 tfm_core_panic();
540 }
541
Mingyang Suna09adda2022-02-16 18:11:33 +0800542 priv_mode = GET_PARTITION_PRIVILEGED_MODE(
543 handle->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800544
545 /*
546 * It is a fatal error if message handle does not refer to a request
547 * message
548 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800549 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800550 tfm_core_panic();
551 }
552
553 /*
554 * It is a fatal error if invec_idx is equal to or greater than
555 * PSA_MAX_IOVEC
556 */
557 if (invec_idx >= PSA_MAX_IOVEC) {
558 tfm_core_panic();
559 }
560
561 /* There was no remaining data in this input vector */
Mingyang Suna09adda2022-02-16 18:11:33 +0800562 if (handle->msg.in_size[invec_idx] == 0) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800563 return 0;
564 }
565
Shawn Shan038348e2021-09-08 17:11:04 +0800566#if PSA_FRAMEWORK_HAS_MM_IOVEC
567 /*
568 * It is a fatal error if the input vector has already been mapped using
569 * psa_map_invec().
570 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800571 if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800572 tfm_core_panic();
573 }
574
Mingyang Suna09adda2022-02-16 18:11:33 +0800575 SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800576#endif
577
Mingyang Sunb26b2802021-07-07 11:25:00 +0800578 /*
579 * Copy the client data to the service buffer. It is a fatal error
580 * if the memory reference for buffer is invalid or not read-write.
581 */
582 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800583 TFM_MEMORY_ACCESS_RW, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800584 tfm_core_panic();
585 }
586
Mingyang Suna09adda2022-02-16 18:11:33 +0800587 bytes = num_bytes > handle->msg.in_size[invec_idx] ?
588 handle->msg.in_size[invec_idx] : num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800589
Mingyang Suna09adda2022-02-16 18:11:33 +0800590 spm_memcpy(buffer, handle->invec[invec_idx].base, bytes);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800591
592 /* There maybe some remaining data */
Mingyang Suna09adda2022-02-16 18:11:33 +0800593 handle->invec[invec_idx].base =
594 (char *)handle->invec[invec_idx].base + bytes;
595 handle->msg.in_size[invec_idx] -= bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800596
597 return bytes;
598}
599
600size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx,
601 size_t num_bytes)
602{
Mingyang Suna09adda2022-02-16 18:11:33 +0800603 struct conn_handle_t *handle = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800604
605 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800606 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800607 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800608 tfm_core_panic();
609 }
610
611 /*
612 * It is a fatal error if message handle does not refer to a request
613 * message
614 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800615 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800616 tfm_core_panic();
617 }
618
619 /*
620 * It is a fatal error if invec_idx is equal to or greater than
621 * PSA_MAX_IOVEC
622 */
623 if (invec_idx >= PSA_MAX_IOVEC) {
624 tfm_core_panic();
625 }
626
627 /* There was no remaining data in this input vector */
Mingyang Suna09adda2022-02-16 18:11:33 +0800628 if (handle->msg.in_size[invec_idx] == 0) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800629 return 0;
630 }
631
Shawn Shan038348e2021-09-08 17:11:04 +0800632#if PSA_FRAMEWORK_HAS_MM_IOVEC
633 /*
634 * It is a fatal error if the input vector has already been mapped using
635 * psa_map_invec().
636 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800637 if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800638 tfm_core_panic();
639 }
640
Mingyang Suna09adda2022-02-16 18:11:33 +0800641 SET_IOVEC_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800642#endif
643
Mingyang Sunb26b2802021-07-07 11:25:00 +0800644 /*
645 * If num_bytes is greater than the remaining size of the input vector then
646 * the remaining size of the input vector is used.
647 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800648 if (num_bytes > handle->msg.in_size[invec_idx]) {
649 num_bytes = handle->msg.in_size[invec_idx];
Mingyang Sunb26b2802021-07-07 11:25:00 +0800650 }
651
652 /* There maybe some remaining data */
Mingyang Suna09adda2022-02-16 18:11:33 +0800653 handle->invec[invec_idx].base =
654 (char *)handle->invec[invec_idx].base + num_bytes;
655 handle->msg.in_size[invec_idx] -= num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800656
657 return num_bytes;
658}
659
660void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
661 const void *buffer, size_t num_bytes)
662{
Mingyang Suna09adda2022-02-16 18:11:33 +0800663 struct conn_handle_t *handle = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800664 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800665
666 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800667 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800668 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800669 tfm_core_panic();
670 }
671
Mingyang Suna09adda2022-02-16 18:11:33 +0800672 priv_mode = GET_PARTITION_PRIVILEGED_MODE(
673 handle->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800674
675 /*
676 * It is a fatal error if message handle does not refer to a request
677 * message
678 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800679 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800680 tfm_core_panic();
681 }
682
683 /*
684 * It is a fatal error if outvec_idx is equal to or greater than
685 * PSA_MAX_IOVEC
686 */
687 if (outvec_idx >= PSA_MAX_IOVEC) {
688 tfm_core_panic();
689 }
690
691 /*
692 * It is a fatal error if the call attempts to write data past the end of
693 * the client output vector
694 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800695 if (num_bytes > handle->msg.out_size[outvec_idx] -
696 handle->outvec[outvec_idx].len) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800697 tfm_core_panic();
698 }
699
Shawn Shan038348e2021-09-08 17:11:04 +0800700#if PSA_FRAMEWORK_HAS_MM_IOVEC
701 /*
702 * It is a fatal error if the output vector has already been mapped using
703 * psa_map_outvec().
704 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800705 if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800706 tfm_core_panic();
707 }
708
Mingyang Suna09adda2022-02-16 18:11:33 +0800709 SET_IOVEC_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800710#endif
711
Mingyang Sunb26b2802021-07-07 11:25:00 +0800712 /*
713 * Copy the service buffer to client outvecs. It is a fatal error
714 * if the memory reference for buffer is invalid or not readable.
715 */
716 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800717 TFM_MEMORY_ACCESS_RO, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800718 tfm_core_panic();
719 }
720
Mingyang Suna09adda2022-02-16 18:11:33 +0800721 spm_memcpy((char *)handle->outvec[outvec_idx].base +
722 handle->outvec[outvec_idx].len, buffer, num_bytes);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800723
724 /* Update the write number */
Mingyang Suna09adda2022-02-16 18:11:33 +0800725 handle->outvec[outvec_idx].len += num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800726}
727
Ken Liuf8c7e532022-02-10 15:03:04 +0800728psa_status_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle,
729 psa_status_t status)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800730{
Ken Liu0bed7e02022-02-10 12:38:07 +0800731 struct service_t *service;
Mingyang Suna09adda2022-02-16 18:11:33 +0800732 struct conn_handle_t *handle;
Ken Liuf8c7e532022-02-10 15:03:04 +0800733 psa_status_t ret = PSA_SUCCESS;
Mingyang Sun620c8562021-11-10 11:44:58 +0800734 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800735
736 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800737 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800738 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800739 tfm_core_panic();
740 }
741
742 /*
743 * RoT Service information is needed in this function, stored it in message
744 * body structure. Only two parameters are passed in this function: handle
745 * and status, so it is useful and simply to do like this.
746 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800747 service = handle->service;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800748 if (!service) {
749 tfm_core_panic();
750 }
751
Mingyang Suna09adda2022-02-16 18:11:33 +0800752 switch (handle->msg.type) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800753 case PSA_IPC_CONNECT:
754 /*
755 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
756 * input status is PSA_SUCCESS. Others return values are based on the
757 * input status.
758 */
759 if (status == PSA_SUCCESS) {
760 ret = msg_handle;
761 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
762 /* Refuse the client connection, indicating a permanent error. */
Mingyang Sunb26b2802021-07-07 11:25:00 +0800763 ret = PSA_ERROR_CONNECTION_REFUSED;
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800764 handle->status = TFM_HANDLE_STATUS_TO_FREE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800765 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
766 /* Fail the client connection, indicating a transient error. */
767 ret = PSA_ERROR_CONNECTION_BUSY;
768 } else {
769 tfm_core_panic();
770 }
771 break;
772 case PSA_IPC_DISCONNECT:
773 /* Service handle is not used anymore */
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800774 handle->status = TFM_HANDLE_STATUS_TO_FREE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800775
776 /*
777 * If the message type is PSA_IPC_DISCONNECT, then the status code is
778 * ignored
779 */
780 break;
781 default:
Mingyang Suna09adda2022-02-16 18:11:33 +0800782 if (handle->msg.type >= PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +0800783
784#if PSA_FRAMEWORK_HAS_MM_IOVEC
785
786 /*
787 * If the unmapped function is not called for an input/output vector
788 * that has been mapped, the framework will remove the mapping.
789 */
790 int i;
791
792 for (i = 0; i < PSA_MAX_IOVEC * 2; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800793 if (IOVEC_IS_MAPPED(handle, i) &&
794 (!IOVEC_IS_UNMAPPED(handle, i))) {
795 SET_IOVEC_UNMAPPED(handle, i);
Shawn Shan038348e2021-09-08 17:11:04 +0800796 /*
797 * Any output vectors that are still mapped will report that
798 * zero bytes have been written.
799 */
800 if (i >= OUTVEC_IDX_BASE) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800801 handle->outvec[i - OUTVEC_IDX_BASE].len = 0;
Shawn Shan038348e2021-09-08 17:11:04 +0800802 }
803 }
804 }
805
806#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800807 /* Reply to a request message. Return values are based on status */
808 ret = status;
809 /*
810 * The total number of bytes written to a single parameter must be
811 * reported to the client by updating the len member of the
812 * psa_outvec structure for the parameter before returning from
813 * psa_call().
814 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800815 update_caller_outvec_len(handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800816 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800817 handle->status = TFM_HANDLE_STATUS_TO_FREE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800818 }
819 } else {
820 tfm_core_panic();
821 }
822 }
823
824 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
825 /*
826 * If the source of the programmer error is a Secure Partition, the SPM
827 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
828 */
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800829 if (!TFM_CLIENT_ID_IS_NS(handle->msg.client_id)) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800830 tfm_core_panic();
831 }
Mingyang Sunb26b2802021-07-07 11:25:00 +0800832 }
833
Mingyang Sun620c8562021-11-10 11:44:58 +0800834 /*
835 * TODO: It can be optimized further by moving critical section protection
836 * to mailbox. Also need to check implementation when secure context is
837 * involved.
838 */
839 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Suna09adda2022-02-16 18:11:33 +0800840 ret = backend_instance.replying(handle, ret);
Mingyang Sun620c8562021-11-10 11:44:58 +0800841 CRITICAL_SECTION_LEAVE(cs_assert);
842
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800843 if (handle->status == TFM_HANDLE_STATUS_TO_FREE) {
844 tfm_spm_free_conn_handle(handle);
845 } else {
846 handle->status = TFM_HANDLE_STATUS_IDLE;
847 }
848
Mingyang Sun620c8562021-11-10 11:44:58 +0800849 return ret;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800850}
851
Kevin Peng613b4172022-02-15 14:41:44 +0800852#if CONFIG_TFM_DOORBELL_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800853void tfm_spm_partition_psa_notify(int32_t partition_id)
854{
Ken Liu5d73c872021-08-19 19:23:17 +0800855 struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id);
856
857 spm_assert_signal(p_pt, PSA_DOORBELL);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800858}
859
860void tfm_spm_partition_psa_clear(void)
861{
Ken Liu92ede9f2021-10-20 09:35:00 +0800862 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800863 struct partition_t *partition = NULL;
864
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800865 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800866
867 /*
868 * It is a fatal error if the Secure Partition's doorbell signal is not
869 * currently asserted.
870 */
871 if ((partition->signals_asserted & PSA_DOORBELL) == 0) {
872 tfm_core_panic();
873 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800874
875 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800876 partition->signals_asserted &= ~PSA_DOORBELL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800877 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800878}
Kevin Peng613b4172022-02-15 14:41:44 +0800879#endif /* CONFIG_TFM_DOORBELL_API == 1 */
Mingyang Sunb26b2802021-07-07 11:25:00 +0800880
Mingyang Sunb26b2802021-07-07 11:25:00 +0800881void tfm_spm_partition_psa_panic(void)
882{
Joakim Andersson97cef7e2022-03-18 10:22:00 +0100883#ifdef CONFIG_TFM_HALT_ON_CORE_PANIC
884 tfm_hal_system_halt();
885#else
Mingyang Sunb26b2802021-07-07 11:25:00 +0800886 /*
887 * PSA FF recommends that the SPM causes the system to restart when a secure
888 * partition panics.
889 */
890 tfm_hal_system_reset();
Joakim Andersson97cef7e2022-03-18 10:22:00 +0100891#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800892}
893
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800894/* psa_set_rhandle is only needed by connection-based services */
895#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
896
897void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle)
898{
Mingyang Suna09adda2022-02-16 18:11:33 +0800899 struct conn_handle_t *handle;
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800900
901 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800902 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800903 if (!handle) {
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800904 tfm_core_panic();
905 }
906
907 /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */
Mingyang Suna09adda2022-02-16 18:11:33 +0800908 if (SERVICE_IS_STATELESS(handle->service->p_ldinf->flags)) {
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800909 tfm_core_panic();
910 }
911
Mingyang Suna09adda2022-02-16 18:11:33 +0800912 handle->msg.rhandle = rhandle;
913 handle->rhandle = rhandle;
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800914}
915
916#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
917
918#if CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1
Kevin Peng67a89fd2021-11-25 11:22:02 +0800919void tfm_spm_partition_psa_irq_enable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800920{
921 struct partition_t *partition;
922 struct irq_load_info_t *irq_info;
923
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800924 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800925
926 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
927 if (!irq_info) {
928 tfm_core_panic();
929 }
930
Kevin Pengd399a1f2021-09-08 15:33:14 +0800931 tfm_hal_irq_enable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800932}
933
Kevin Peng67a89fd2021-11-25 11:22:02 +0800934psa_irq_status_t tfm_spm_partition_psa_irq_disable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800935{
936 struct partition_t *partition;
937 struct irq_load_info_t *irq_info;
938
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800939 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800940
941 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
942 if (!irq_info) {
943 tfm_core_panic();
944 }
945
Kevin Pengd399a1f2021-09-08 15:33:14 +0800946 tfm_hal_irq_disable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800947
948 return 1;
949}
950
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800951/* This API is only used for FLIH. */
952#if CONFIG_TFM_FLIH_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800953void tfm_spm_partition_psa_reset_signal(psa_signal_t irq_signal)
954{
Ken Liu92ede9f2021-10-20 09:35:00 +0800955 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800956 struct irq_load_info_t *irq_info;
957 struct partition_t *partition;
958
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800959 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800960
961 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
962 if (!irq_info) {
963 tfm_core_panic();
964 }
965
966 if (!irq_info->flih_func) {
967 /* This API is for FLIH IRQs only */
968 tfm_core_panic();
969 }
970
971 if ((partition->signals_asserted & irq_signal) == 0) {
972 /* The signal is not asserted */
973 tfm_core_panic();
974 }
975
Ken Liu92ede9f2021-10-20 09:35:00 +0800976 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800977 partition->signals_asserted &= ~irq_signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800978 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800979}
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800980#endif
Shawn Shan038348e2021-09-08 17:11:04 +0800981
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800982/* This API is only used for SLIH. */
983#if CONFIG_TFM_SLIH_API == 1
984void tfm_spm_partition_psa_eoi(psa_signal_t irq_signal)
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800985{
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800986 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
987 struct irq_load_info_t *irq_info = NULL;
988 struct partition_t *partition = NULL;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800989
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800990 partition = GET_CURRENT_COMPONENT();
991
992 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
993 /* It is a fatal error if passed signal is not an interrupt signal. */
994 if (!irq_info) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800995 tfm_core_panic();
996 }
997
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800998 if (irq_info->flih_func) {
999 /* This API is for SLIH IRQs only */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001000 tfm_core_panic();
1001 }
1002
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001003 /* It is a fatal error if passed signal is not currently asserted */
1004 if ((partition->signals_asserted & irq_signal) == 0) {
1005 tfm_core_panic();
1006 }
1007
1008 CRITICAL_SECTION_ENTER(cs_assert);
1009 partition->signals_asserted &= ~irq_signal;
1010 CRITICAL_SECTION_LEAVE(cs_assert);
1011
1012 tfm_hal_irq_clear_pending(irq_info->source);
1013 tfm_hal_irq_enable(irq_info->source);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001014}
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001015#endif
1016#endif /* CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001017
Shawn Shan038348e2021-09-08 17:11:04 +08001018#if PSA_FRAMEWORK_HAS_MM_IOVEC
1019
1020const void *tfm_spm_partition_psa_map_invec(psa_handle_t msg_handle,
1021 uint32_t invec_idx)
1022{
Mingyang Suna09adda2022-02-16 18:11:33 +08001023 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001024 uint32_t privileged;
1025 struct partition_t *partition = NULL;
1026
1027 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001028 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001029 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001030 tfm_core_panic();
1031 }
1032
Mingyang Suna09adda2022-02-16 18:11:33 +08001033 partition = handle->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001034 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001035
1036 /*
1037 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1038 * Service that received the message.
1039 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001040 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001041 tfm_core_panic();
1042 }
1043
1044 /*
1045 * It is a fatal error if message handle does not refer to a request
1046 * message.
1047 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001048 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001049 tfm_core_panic();
1050 }
1051
1052 /*
1053 * It is a fatal error if invec_idx is equal to or greater than
1054 * PSA_MAX_IOVEC.
1055 */
1056 if (invec_idx >= PSA_MAX_IOVEC) {
1057 tfm_core_panic();
1058 }
1059
1060 /* It is a fatal error if the input vector has length zero. */
Mingyang Suna09adda2022-02-16 18:11:33 +08001061 if (handle->msg.in_size[invec_idx] == 0) {
Shawn Shan038348e2021-09-08 17:11:04 +08001062 tfm_core_panic();
1063 }
1064
1065 /*
1066 * It is a fatal error if the input vector has already been mapped using
1067 * psa_map_invec().
1068 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001069 if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001070 tfm_core_panic();
1071 }
1072
1073 /*
1074 * It is a fatal error if the input vector has already been accessed
1075 * using psa_read() or psa_skip().
1076 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001077 if (IOVEC_IS_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001078 tfm_core_panic();
1079 }
1080
1081 /*
1082 * It is a fatal error if the memory reference for the wrap input vector is
1083 * invalid or not readable.
1084 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001085 if (tfm_memory_check(handle->invec[invec_idx].base,
1086 handle->invec[invec_idx].len,
1087 false, TFM_MEMORY_ACCESS_RO,
1088 privileged) != SPM_SUCCESS) {
Shawn Shan038348e2021-09-08 17:11:04 +08001089 tfm_core_panic();
1090 }
1091
Mingyang Suna09adda2022-02-16 18:11:33 +08001092 SET_IOVEC_MAPPED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001093
Mingyang Suna09adda2022-02-16 18:11:33 +08001094 return handle->invec[invec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001095}
1096
1097void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle,
1098 uint32_t invec_idx)
1099{
Mingyang Suna09adda2022-02-16 18:11:33 +08001100 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001101
1102 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001103 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001104 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001105 tfm_core_panic();
1106 }
1107
1108 /*
1109 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1110 * Service that received the message.
1111 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001112 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001113 tfm_core_panic();
1114 }
1115
1116 /*
1117 * It is a fatal error if message handle does not refer to a request
1118 * message.
1119 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001120 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001121 tfm_core_panic();
1122 }
1123
1124 /*
1125 * It is a fatal error if invec_idx is equal to or greater than
1126 * PSA_MAX_IOVEC.
1127 */
1128 if (invec_idx >= PSA_MAX_IOVEC) {
1129 tfm_core_panic();
1130 }
1131
1132 /*
1133 * It is a fatal error if The input vector has not been mapped by a call to
1134 * psa_map_invec().
1135 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001136 if (!IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001137 tfm_core_panic();
1138 }
1139
1140 /*
1141 * It is a fatal error if the input vector has already been unmapped by a
1142 * call to psa_unmap_invec().
1143 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001144 if (IOVEC_IS_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001145 tfm_core_panic();
1146 }
1147
Mingyang Suna09adda2022-02-16 18:11:33 +08001148 SET_IOVEC_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001149}
1150
1151void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle,
1152 uint32_t outvec_idx)
1153{
Mingyang Suna09adda2022-02-16 18:11:33 +08001154 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001155 uint32_t privileged;
1156 struct partition_t *partition = NULL;
1157
1158 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001159 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001160 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001161 tfm_core_panic();
1162 }
1163
Mingyang Suna09adda2022-02-16 18:11:33 +08001164 partition = handle->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001165 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001166
1167 /*
1168 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1169 * Service that received the message.
1170 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001171 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001172 tfm_core_panic();
1173 }
1174
1175 /*
1176 * It is a fatal error if message handle does not refer to a request
1177 * message.
1178 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001179 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001180 tfm_core_panic();
1181 }
1182
1183 /*
1184 * It is a fatal error if outvec_idx is equal to or greater than
1185 * PSA_MAX_IOVEC.
1186 */
1187 if (outvec_idx >= PSA_MAX_IOVEC) {
1188 tfm_core_panic();
1189 }
1190
1191 /* It is a fatal error if the output vector has length zero. */
Mingyang Suna09adda2022-02-16 18:11:33 +08001192 if (handle->msg.out_size[outvec_idx] == 0) {
Shawn Shan038348e2021-09-08 17:11:04 +08001193 tfm_core_panic();
1194 }
1195
1196 /*
1197 * It is a fatal error if the output vector has already been mapped using
1198 * psa_map_outvec().
1199 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001200 if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001201 tfm_core_panic();
1202 }
1203
1204 /*
1205 * It is a fatal error if the output vector has already been accessed
1206 * using psa_write().
1207 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001208 if (IOVEC_IS_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001209 tfm_core_panic();
1210 }
1211
1212 /*
1213 * It is a fatal error if the output vector is invalid or not read-write.
1214 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001215 if (tfm_memory_check(handle->outvec[outvec_idx].base,
1216 handle->outvec[outvec_idx].len, false,
Shawn Shan038348e2021-09-08 17:11:04 +08001217 TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
1218 tfm_core_panic();
1219 }
Mingyang Suna09adda2022-02-16 18:11:33 +08001220 SET_IOVEC_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001221
Mingyang Suna09adda2022-02-16 18:11:33 +08001222 return handle->outvec[outvec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001223}
1224
1225void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle,
1226 uint32_t outvec_idx, size_t len)
1227{
Mingyang Suna09adda2022-02-16 18:11:33 +08001228 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001229
1230 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001231 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001232 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001233 tfm_core_panic();
1234 }
1235
1236 /*
1237 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1238 * Service that received the message.
1239 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001240 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001241 tfm_core_panic();
1242 }
1243
1244 /*
1245 * It is a fatal error if message handle does not refer to a request
1246 * message.
1247 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001248 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001249 tfm_core_panic();
1250 }
1251
1252 /*
1253 * It is a fatal error if outvec_idx is equal to or greater than
1254 * PSA_MAX_IOVEC.
1255 */
1256 if (outvec_idx >= PSA_MAX_IOVEC) {
1257 tfm_core_panic();
1258 }
1259
1260 /*
1261 * It is a fatal error if len is greater than the output vector size.
1262 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001263 if (len > handle->msg.out_size[outvec_idx]) {
Shawn Shan038348e2021-09-08 17:11:04 +08001264 tfm_core_panic();
1265 }
1266
1267 /*
1268 * It is a fatal error if The output vector has not been mapped by a call to
1269 * psa_map_outvec().
1270 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001271 if (!IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001272 tfm_core_panic();
1273 }
1274
1275 /*
1276 * It is a fatal error if the output vector has already been unmapped by a
1277 * call to psa_unmap_outvec().
1278 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001279 if (IOVEC_IS_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001280 tfm_core_panic();
1281 }
1282
Mingyang Suna09adda2022-02-16 18:11:33 +08001283 SET_IOVEC_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001284
1285 /* Update the write number */
Mingyang Suna09adda2022-02-16 18:11:33 +08001286 handle->outvec[outvec_idx].len = len;
Shawn Shan038348e2021-09-08 17:11:04 +08001287}
1288
1289#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */