blob: 52e9ffc0d997b7bc17ad23ed3cce5ea6599002be [file] [log] [blame]
David Hu733d8f92019-09-23 15:32:40 +08001/*
Mingyang Sunbb4a42a2021-12-14 15:18:52 +08002 * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
David Hu733d8f92019-09-23 15:32:40 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Mingyang Suneeca4652021-07-15 15:19:16 +08008#include <stdint.h>
Mingyang Sunb26b2802021-07-07 11:25:00 +08009#include "bitops.h"
Mingyang Sun4609ac72022-02-08 18:56:35 +080010#include "config_impl.h"
Ken Liu92ede9f2021-10-20 09:35:00 +080011#include "critical_section.h"
Mingyang Suneeca4652021-07-15 15:19:16 +080012#include "psa/lifecycle.h"
David Hu733d8f92019-09-23 15:32:40 +080013#include "psa/service.h"
Kevin Peng3f67b2e2021-10-18 17:47:27 +080014#include "interrupt.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080015#include "spm_ipc.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080016#include "tfm_arch.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080017#include "load/partition_defs.h"
Mingyang Sun2b352662021-04-21 11:35:43 +080018#include "load/service_defs.h"
Ken Liu3dd92562021-08-17 16:22:54 +080019#include "load/interrupt_defs.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080020#include "ffm/psa_api.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080021#include "utilities.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080022#include "ffm/backend.h"
Ken Liue07c3b72021-10-14 16:19:13 +080023#include "ffm/psa_api.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080024#include "tfm_rpc.h"
25#include "tfm_spm_hal.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080026#include "tfm_hal_interrupt.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080027#include "tfm_hal_platform.h"
Ken Liu82e3eac2021-10-14 16:19:13 +080028#include "tfm_psa_call_pack.h"
David Hu733d8f92019-09-23 15:32:40 +080029
Ken Liub3b2cb62021-05-22 00:39:28 +080030#define GET_STATELESS_SERVICE(index) (stateless_services_ref_tbl[index])
Xinyu Zhanga38e9b52021-06-02 17:48:01 +080031extern struct service_t *stateless_services_ref_tbl[];
Mingyang Suncb6f70e2021-03-05 23:30:25 +080032
Shawn Shan038348e2021-09-08 17:11:04 +080033#if PSA_FRAMEWORK_HAS_MM_IOVEC
34
35/*
36 * The MM-IOVEC status
37 * The max total number of invec and outvec is 8.
38 * Each invec/outvec takes 4 bit, 32 bits in total.
39 *
40 * The encoding format of the MM-IOVEC status:
41 *--------------------------------------------------------------
42 *| Bit | 31 - 28 | 27 - 24 | ... | 7 - 4 | 3 - 0 |
43 *--------------------------------------------------------------
44 *| Vector | outvec[3] | outvec[2] | ... | invec[1] | invec[0] |
45 *--------------------------------------------------------------
46 *
47 * Take invec[0] as an example:
48 *
49 * bit 0: whether invec[0] has been mapped.
50 * bit 1: whether invec[0] has been unmapped.
51 * bit 2: whether invec[0] has been accessed using psa_read(), psa_skip() or
52 * psa_write().
53 * bit 3: reserved for invec[0].
54 */
55
56#define IOVEC_STATUS_BITS 4 /* Each vector occupies 4 bits. */
57#define OUTVEC_IDX_BASE 4 /*
58 * Base index of outvec.
59 * There are four invecs in front of
60 * outvec.
61 */
62#define INVEC_IDX_BASE 0 /* Base index of invec. */
63
64#define IOVEC_MAPPED_BIT (1U << 0)
65#define IOVEC_UNMAPPED_BIT (1U << 1)
66#define IOVEC_ACCESSED_BIT (1U << 2)
67
Mingyang Suna09adda2022-02-16 18:11:33 +080068#define IOVEC_IS_MAPPED(handle, iovec_idx) \
69 ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
Shawn Shan038348e2021-09-08 17:11:04 +080070 IOVEC_MAPPED_BIT)
Mingyang Suna09adda2022-02-16 18:11:33 +080071#define IOVEC_IS_UNMAPPED(handle, iovec_idx) \
72 ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
Shawn Shan038348e2021-09-08 17:11:04 +080073 IOVEC_UNMAPPED_BIT)
Mingyang Suna09adda2022-02-16 18:11:33 +080074#define IOVEC_IS_ACCESSED(handle, iovec_idx) \
75 ((((handle)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
Shawn Shan038348e2021-09-08 17:11:04 +080076 IOVEC_ACCESSED_BIT)
Mingyang Suna09adda2022-02-16 18:11:33 +080077#define SET_IOVEC_MAPPED(handle, iovec_idx) \
78 (((handle)->iovec_status) |= (IOVEC_MAPPED_BIT << \
Shawn Shan038348e2021-09-08 17:11:04 +080079 ((iovec_idx) * IOVEC_STATUS_BITS)))
Mingyang Suna09adda2022-02-16 18:11:33 +080080#define SET_IOVEC_UNMAPPED(handle, iovec_idx) \
81 (((handle)->iovec_status) |= (IOVEC_UNMAPPED_BIT << \
Shawn Shan038348e2021-09-08 17:11:04 +080082 ((iovec_idx) * IOVEC_STATUS_BITS)))
Mingyang Suna09adda2022-02-16 18:11:33 +080083#define SET_IOVEC_ACCESSED(handle, iovec_idx) \
84 (((handle)->iovec_status) |= (IOVEC_ACCESSED_BIT << \
Shawn Shan038348e2021-09-08 17:11:04 +080085 ((iovec_idx) * IOVEC_STATUS_BITS)))
86
87#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */
88
Xinyu Zhangb287ef82021-11-03 18:38:50 +080089void spm_handle_programmer_errors(psa_status_t status)
90{
Summer Qind9d497b2022-04-14 14:39:26 +080091 if (status == PSA_ERROR_PROGRAMMER_ERROR ||
92 status == PSA_ERROR_CONNECTION_REFUSED) {
93 if (!tfm_spm_is_ns_caller()) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +080094 tfm_core_panic();
Summer Qind9d497b2022-04-14 14:39:26 +080095 }
Xinyu Zhangb287ef82021-11-03 18:38:50 +080096 }
97}
98
Mingyang Suneeca4652021-07-15 15:19:16 +080099uint32_t tfm_spm_get_lifecycle_state(void)
100{
101 /*
102 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
103 * implemented in the future.
104 */
105 return PSA_LIFECYCLE_UNKNOWN;
106}
107
108/* PSA Client API function body */
109
Mingyang Sund44522a2020-01-16 16:48:37 +0800110uint32_t tfm_spm_client_psa_framework_version(void)
David Hu733d8f92019-09-23 15:32:40 +0800111{
112 return PSA_FRAMEWORK_VERSION;
113}
114
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800115uint32_t tfm_spm_client_psa_version(uint32_t sid)
David Hu733d8f92019-09-23 15:32:40 +0800116{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800117 struct service_t *service;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800118 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800119
120 /*
121 * It should return PSA_VERSION_NONE if the RoT Service is not
122 * implemented.
123 */
124 service = tfm_spm_get_service_by_sid(sid);
125 if (!service) {
126 return PSA_VERSION_NONE;
127 }
128
129 /*
Shawn Shan2365c902019-12-19 18:35:36 +0800130 * It should return PSA_VERSION_NONE if the caller is not authorized
131 * to access the RoT Service.
David Hu733d8f92019-09-23 15:32:40 +0800132 */
Ken Liuad3f3e62022-03-04 22:23:52 +0800133 if (tfm_spm_check_authorization(sid, service, ns_caller) != PSA_SUCCESS) {
Shawn Shan2365c902019-12-19 18:35:36 +0800134 return PSA_VERSION_NONE;
David Hu733d8f92019-09-23 15:32:40 +0800135 }
136
Ken Liuacd2a572021-05-12 16:19:04 +0800137 return service->p_ldinf->version;
David Hu733d8f92019-09-23 15:32:40 +0800138}
139
Mingyang Suneeca4652021-07-15 15:19:16 +0800140psa_status_t tfm_spm_client_psa_call(psa_handle_t handle,
141 uint32_t ctrl_param,
142 const psa_invec *inptr,
143 psa_outvec *outptr)
David Hu733d8f92019-09-23 15:32:40 +0800144{
145 psa_invec invecs[PSA_MAX_IOVEC];
146 psa_outvec outvecs[PSA_MAX_IOVEC];
Ken Liu0bed7e02022-02-10 12:38:07 +0800147 struct conn_handle_t *conn_handle;
Mingyang Sun783a59b2021-04-20 15:52:18 +0800148 struct service_t *service;
Summer Qinba2346e2019-11-12 16:26:31 +0800149 int i, j;
Summer Qin1ce712a2019-10-14 18:04:05 +0800150 int32_t client_id;
Mingyang Sun453ad402021-03-17 17:58:33 +0800151 uint32_t sid, version, index;
Mingyang Sune529e3b2021-07-12 14:46:30 +0800152 uint32_t privileged;
Mingyang Sun620c8562021-11-10 11:44:58 +0800153 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800154 bool ns_caller = tfm_spm_is_ns_caller();
Mingyang Suneeca4652021-07-15 15:19:16 +0800155 int32_t type = (int32_t)(int16_t)((ctrl_param & TYPE_MASK) >> TYPE_OFFSET);
156 size_t in_num = (size_t)((ctrl_param & IN_LEN_MASK) >> IN_LEN_OFFSET);
157 size_t out_num = (size_t)((ctrl_param & OUT_LEN_MASK) >> OUT_LEN_OFFSET);
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800158
159 /* The request type must be zero or positive. */
160 if (type < 0) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800161 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800162 }
David Hu733d8f92019-09-23 15:32:40 +0800163
Shawn Shanb222d892021-01-04 17:41:48 +0800164 /* It is a PROGRAMMER ERROR if in_len + out_len > PSA_MAX_IOVEC. */
Summer Qin22c301a2022-04-27 17:36:00 +0800165 if ((in_num > SIZE_MAX - out_num) ||
David Hu733d8f92019-09-23 15:32:40 +0800166 (in_num + out_num > PSA_MAX_IOVEC)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800167 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800168 }
169
Mingyang Suncf0b1a52022-03-28 17:29:03 +0800170 /* It is a PROGRAMMER ERROR if the handle is a null handle. */
171 if (handle == PSA_NULL_HANDLE) {
172 return PSA_ERROR_PROGRAMMER_ERROR;
173 }
174
Kevin Peng385fda82021-08-18 10:41:19 +0800175 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800176
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800177 /* Allocate space from handle pool for static handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800178 if (IS_STATIC_HANDLE(handle)) {
Mingyang Sun453ad402021-03-17 17:58:33 +0800179 index = GET_INDEX_FROM_STATIC_HANDLE(handle);
Mingyang Sune8d38082021-03-30 18:34:40 +0800180
181 if (!IS_VALID_STATIC_HANDLE_IDX(index)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800182 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sune8d38082021-03-30 18:34:40 +0800183 }
184
Mingyang Sun453ad402021-03-17 17:58:33 +0800185 service = GET_STATELESS_SERVICE(index);
Mingyang Sun86213242021-07-14 10:26:43 +0800186 if (!service) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800187 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun86213242021-07-14 10:26:43 +0800188 }
189
Ken Liub3b2cb62021-05-22 00:39:28 +0800190 sid = service->p_ldinf->sid;
Mingyang Sun453ad402021-03-17 17:58:33 +0800191
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800192 /*
193 * It is a PROGRAMMER ERROR if the caller is not authorized to access
194 * the RoT Service.
195 */
196 if (tfm_spm_check_authorization(sid, service, ns_caller)
Ken Liuad3f3e62022-03-04 22:23:52 +0800197 != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800198 return PSA_ERROR_CONNECTION_REFUSED;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800199 }
200
Mingyang Sun453ad402021-03-17 17:58:33 +0800201 version = GET_VERSION_FROM_STATIC_HANDLE(handle);
202
Ken Liuad3f3e62022-03-04 22:23:52 +0800203 if (tfm_spm_check_client_version(service, version) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800204 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun453ad402021-03-17 17:58:33 +0800205 }
206
Mingyang Sun620c8562021-11-10 11:44:58 +0800207 CRITICAL_SECTION_ENTER(cs_assert);
Kevin Penge61e7052022-01-27 14:57:06 +0800208 conn_handle = tfm_spm_create_conn_handle();
Mingyang Sun620c8562021-11-10 11:44:58 +0800209 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800210
211 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800212 return PSA_ERROR_CONNECTION_BUSY;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800213 }
214
Mingyang Sun6d5dc3d2021-03-15 15:34:44 +0800215 conn_handle->rhandle = NULL;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800216 handle = tfm_spm_to_user_handle(conn_handle);
217 } else {
Sherry Zhanga4575f32022-02-23 15:45:51 +0800218#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800219 /* It is a PROGRAMMER ERROR if an invalid handle was passed. */
Kevin Penge61e7052022-01-27 14:57:06 +0800220 conn_handle = spm_get_handle_by_client_handle(handle, client_id);
221 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800222 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800223 }
224
225 /*
226 * It is a PROGRAMMER ERROR if the connection is currently
227 * handling a request.
228 */
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800229 if (conn_handle->status != TFM_HANDLE_STATUS_IDLE) {
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800230 return PSA_ERROR_PROGRAMMER_ERROR;
231 }
232
Ken Liu0bed7e02022-02-10 12:38:07 +0800233 service = conn_handle->service;
Shawn Shanb222d892021-01-04 17:41:48 +0800234
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800235 if (!service) {
236 /* FixMe: Need to implement a mechanism to resolve this failure. */
237 return PSA_ERROR_PROGRAMMER_ERROR;
238 }
Sherry Zhanga4575f32022-02-23 15:45:51 +0800239#else
240 return PSA_ERROR_PROGRAMMER_ERROR;
241#endif
David Hu733d8f92019-09-23 15:32:40 +0800242 }
243
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800244 privileged = GET_CURRENT_PARTITION_PRIVILEGED_MODE();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800245
Kevin Pengedb8ee42021-03-09 16:50:11 +0800246 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800247 * Read client invecs from the wrap input vector. It is a PROGRAMMER ERROR
David Hu733d8f92019-09-23 15:32:40 +0800248 * if the memory reference for the wrap input vector is invalid or not
249 * readable.
250 */
251 if (tfm_memory_check(inptr, in_num * sizeof(psa_invec), ns_caller,
Ken Liuad3f3e62022-03-04 22:23:52 +0800252 TFM_MEMORY_ACCESS_RO, privileged) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800253 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800254 }
Summer Qinba2346e2019-11-12 16:26:31 +0800255
David Hu733d8f92019-09-23 15:32:40 +0800256 /*
257 * Read client outvecs from the wrap output vector and will update the
Shawn Shanb222d892021-01-04 17:41:48 +0800258 * actual length later. It is a PROGRAMMER ERROR if the memory reference for
David Hu733d8f92019-09-23 15:32:40 +0800259 * the wrap output vector is invalid or not read-write.
260 */
261 if (tfm_memory_check(outptr, out_num * sizeof(psa_outvec), ns_caller,
Ken Liuad3f3e62022-03-04 22:23:52 +0800262 TFM_MEMORY_ACCESS_RW, privileged) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800263 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800264 }
265
Summer Qinf24dbb52020-07-23 14:53:54 +0800266 spm_memset(invecs, 0, sizeof(invecs));
267 spm_memset(outvecs, 0, sizeof(outvecs));
David Hu733d8f92019-09-23 15:32:40 +0800268
269 /* Copy the address out to avoid TOCTOU attacks. */
Summer Qinf24dbb52020-07-23 14:53:54 +0800270 spm_memcpy(invecs, inptr, in_num * sizeof(psa_invec));
271 spm_memcpy(outvecs, outptr, out_num * sizeof(psa_outvec));
David Hu733d8f92019-09-23 15:32:40 +0800272
273 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800274 * For client input vector, it is a PROGRAMMER ERROR if the provided payload
David Hu733d8f92019-09-23 15:32:40 +0800275 * memory reference was invalid or not readable.
276 */
277 for (i = 0; i < in_num; i++) {
278 if (tfm_memory_check(invecs[i].base, invecs[i].len, ns_caller,
Ken Liuad3f3e62022-03-04 22:23:52 +0800279 TFM_MEMORY_ACCESS_RO, privileged) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800280 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800281 }
282 }
Summer Qinba2346e2019-11-12 16:26:31 +0800283
284 /*
285 * Clients must never overlap input parameters because of the risk of a
286 * double-fetch inconsistency.
287 * Overflow is checked in tfm_memory_check functions.
288 */
289 for (i = 0; i + 1 < in_num; i++) {
290 for (j = i+1; j < in_num; j++) {
TTornblom83d96372019-11-19 12:53:16 +0100291 if (!((char *) invecs[j].base + invecs[j].len <=
292 (char *) invecs[i].base ||
293 (char *) invecs[j].base >=
294 (char *) invecs[i].base + invecs[i].len)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800295 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qinba2346e2019-11-12 16:26:31 +0800296 }
297 }
298 }
299
David Hu733d8f92019-09-23 15:32:40 +0800300 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800301 * For client output vector, it is a PROGRAMMER ERROR if the provided
302 * payload memory reference was invalid or not read-write.
David Hu733d8f92019-09-23 15:32:40 +0800303 */
304 for (i = 0; i < out_num; i++) {
305 if (tfm_memory_check(outvecs[i].base, outvecs[i].len,
Ken Liuad3f3e62022-03-04 22:23:52 +0800306 ns_caller, TFM_MEMORY_ACCESS_RW, privileged) != PSA_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800307 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800308 }
309 }
310
Ken Liu0bed7e02022-02-10 12:38:07 +0800311 spm_fill_message(conn_handle, service, handle, type, client_id,
Summer Qin630c76b2020-05-20 10:32:58 +0800312 invecs, in_num, outvecs, out_num, outptr);
David Hu733d8f92019-09-23 15:32:40 +0800313
Ken Liu995a9742022-05-18 19:28:30 +0800314 return backend_messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800315}
316
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800317/* Following PSA APIs are only needed by connection-based services */
318#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
319
320psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version)
321{
322 struct service_t *service;
Mingyang Suna09adda2022-02-16 18:11:33 +0800323 struct conn_handle_t *conn_handle;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800324 int32_t client_id;
325 psa_handle_t handle;
326 bool ns_caller = tfm_spm_is_ns_caller();
327 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
328
329 /*
330 * It is a PROGRAMMER ERROR if the RoT Service does not exist on the
331 * platform.
332 */
333 service = tfm_spm_get_service_by_sid(sid);
334 if (!service) {
335 return PSA_ERROR_CONNECTION_REFUSED;
336 }
337
338 /* It is a PROGRAMMER ERROR if connecting to a stateless service. */
339 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
340 return PSA_ERROR_PROGRAMMER_ERROR;
341 }
342
343 /*
344 * It is a PROGRAMMER ERROR if the caller is not authorized to access the
345 * RoT Service.
346 */
Ken Liuad3f3e62022-03-04 22:23:52 +0800347 if (tfm_spm_check_authorization(sid, service, ns_caller) != PSA_SUCCESS) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800348 return PSA_ERROR_CONNECTION_REFUSED;
349 }
350
351 /*
352 * It is a PROGRAMMER ERROR if the version of the RoT Service requested is
353 * not supported on the platform.
354 */
Ken Liuad3f3e62022-03-04 22:23:52 +0800355 if (tfm_spm_check_client_version(service, version) != PSA_SUCCESS) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800356 return PSA_ERROR_CONNECTION_REFUSED;
357 }
358
359 client_id = tfm_spm_get_client_id(ns_caller);
360
361 /*
362 * Create connection handle here since it is possible to return the error
363 * code to client when creation fails.
364 */
365 CRITICAL_SECTION_ENTER(cs_assert);
Kevin Penge61e7052022-01-27 14:57:06 +0800366 conn_handle = tfm_spm_create_conn_handle();
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800367 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Suna09adda2022-02-16 18:11:33 +0800368 if (!conn_handle) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800369 return PSA_ERROR_CONNECTION_BUSY;
370 }
371
Mingyang Suna09adda2022-02-16 18:11:33 +0800372 handle = tfm_spm_to_user_handle(conn_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800373 /* No input or output needed for connect message */
Mingyang Suna09adda2022-02-16 18:11:33 +0800374 spm_fill_message(conn_handle, service, handle, PSA_IPC_CONNECT,
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800375 client_id, NULL, 0, NULL, 0, NULL);
376
Ken Liu995a9742022-05-18 19:28:30 +0800377 return backend_messaging(service, conn_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800378}
379
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800380psa_status_t tfm_spm_client_psa_close(psa_handle_t handle)
David Hu733d8f92019-09-23 15:32:40 +0800381{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800382 struct service_t *service;
Ken Liu0bed7e02022-02-10 12:38:07 +0800383 struct conn_handle_t *conn_handle;
Summer Qin1ce712a2019-10-14 18:04:05 +0800384 int32_t client_id;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800385 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800386
387 /* It will have no effect if called with the NULL handle */
388 if (handle == PSA_NULL_HANDLE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800389 return PSA_SUCCESS;
David Hu733d8f92019-09-23 15:32:40 +0800390 }
391
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800392 /* It is a PROGRAMMER ERROR if called with a stateless handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800393 if (IS_STATIC_HANDLE(handle)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800394 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800395 }
396
Kevin Peng385fda82021-08-18 10:41:19 +0800397 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800398
David Hu733d8f92019-09-23 15:32:40 +0800399 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800400 * It is a PROGRAMMER ERROR if an invalid handle was provided that is not
401 * the null handle.
David Hu733d8f92019-09-23 15:32:40 +0800402 */
Kevin Penge61e7052022-01-27 14:57:06 +0800403 conn_handle = spm_get_handle_by_client_handle(handle, client_id);
404 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800405 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qin1ce712a2019-10-14 18:04:05 +0800406 }
Shawn Shanb222d892021-01-04 17:41:48 +0800407
Ken Liu0bed7e02022-02-10 12:38:07 +0800408 service = conn_handle->service;
David Hu733d8f92019-09-23 15:32:40 +0800409 if (!service) {
410 /* FixMe: Need to implement one mechanism to resolve this failure. */
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800411 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800412 }
413
Shawn Shanb222d892021-01-04 17:41:48 +0800414 /*
415 * It is a PROGRAMMER ERROR if the connection is currently handling a
416 * request.
417 */
Summer Qin630c76b2020-05-20 10:32:58 +0800418 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800419 return PSA_ERROR_PROGRAMMER_ERROR;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800420 }
421
David Hu733d8f92019-09-23 15:32:40 +0800422 /* No input or output needed for close message */
Ken Liu0bed7e02022-02-10 12:38:07 +0800423 spm_fill_message(conn_handle, service, handle, PSA_IPC_DISCONNECT,
424 client_id, NULL, 0, NULL, 0, NULL);
David Hu733d8f92019-09-23 15:32:40 +0800425
Ken Liu995a9742022-05-18 19:28:30 +0800426 return backend_messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800427}
Mingyang Sunb26b2802021-07-07 11:25:00 +0800428
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800429#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
430
Mingyang Suneeca4652021-07-15 15:19:16 +0800431/* PSA Partition API function body */
432
Kevin Pengdef92de2021-11-10 16:14:48 +0800433#if CONFIG_TFM_SPM_BACKEND_IPC == 1 \
434 || CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800435psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask,
436 uint32_t timeout)
437{
438 struct partition_t *partition = NULL;
439
440 /*
441 * Timeout[30:0] are reserved for future use.
442 * SPM must ignore the value of RES.
443 */
444 timeout &= PSA_TIMEOUT_MASK;
445
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800446 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800447
448 /*
Ruchika Guptad1834172022-06-15 12:03:08 +0530449 * signals_allowed can be 0 for TF-M internal partitions for special usages.
450 * Regular Secure Partitions should have at least one signal.
451 * This is gauranteed by the manifest tool.
Mingyang Sunb26b2802021-07-07 11:25:00 +0800452 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
453 * signals.
454 */
Ruchika Guptad1834172022-06-15 12:03:08 +0530455 if ((partition->signals_allowed) &&
456 (partition->signals_allowed & signal_mask) == 0) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800457 tfm_core_panic();
458 }
459
460 /*
Ken Liu995a9742022-05-18 19:28:30 +0800461 * backend_wake_up() blocks the caller thread if no signals are
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800462 * available. In this case, the return value of this function is temporary
463 * set into runtime context. After new signal(s) are available, the return
464 * value is updated with the available signal(s) and blocked thread gets
465 * to run.
Mingyang Sunb26b2802021-07-07 11:25:00 +0800466 */
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800467 if (timeout == PSA_BLOCK) {
Ken Liu995a9742022-05-18 19:28:30 +0800468 return backend_wait(partition, signal_mask);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800469 }
470
471 return partition->signals_asserted & signal_mask;
472}
Kevin Pengdef92de2021-11-10 16:14:48 +0800473#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800474
Kevin Pengdef92de2021-11-10 16:14:48 +0800475#if CONFIG_TFM_SPM_BACKEND_IPC == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800476psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg)
477{
Mingyang Suna09adda2022-02-16 18:11:33 +0800478 struct conn_handle_t *handle = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800479 struct partition_t *partition = NULL;
480 uint32_t privileged;
481
482 /*
483 * Only one message could be retrieved every time for psa_get(). It is a
484 * fatal error if the input signal has more than a signal bit set.
485 */
486 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
487 tfm_core_panic();
488 }
489
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800490 partition = GET_CURRENT_COMPONENT();
491
Kevin Penga40d29f2022-01-19 14:44:34 +0800492 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800493
494 /*
495 * Write the message to the service buffer. It is a fatal error if the
496 * input msg pointer is not a valid memory reference or not read-write.
497 */
498 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
Ken Liuad3f3e62022-03-04 22:23:52 +0800499 privileged) != PSA_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800500 tfm_core_panic();
501 }
502
503 /*
504 * It is a fatal error if the caller call psa_get() when no message has
505 * been set. The caller must call this function after an RoT Service signal
506 * is returned by psa_wait().
507 */
508 if (partition->signals_asserted == 0) {
509 tfm_core_panic();
510 }
511
512 /*
513 * It is a fatal error if the RoT Service signal is not currently asserted.
514 */
515 if ((partition->signals_asserted & signal) == 0) {
516 tfm_core_panic();
517 }
518
519 /*
520 * Get message by signal from partition. It is a fatal error if getting
521 * failed, which means the input signal is not correspond to an RoT service.
522 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800523 handle = spm_get_handle_by_signal(partition, signal);
524 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800525 return PSA_ERROR_DOES_NOT_EXIST;
526 }
527
Mingyang Suna09adda2022-02-16 18:11:33 +0800528 spm_memcpy(msg, &handle->msg, sizeof(psa_msg_t));
Mingyang Sunb26b2802021-07-07 11:25:00 +0800529
530 return PSA_SUCCESS;
531}
Mingyang Sun4609ac72022-02-08 18:56:35 +0800532#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800533
Mingyang Sunb26b2802021-07-07 11:25:00 +0800534size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
535 void *buffer, size_t num_bytes)
536{
537 size_t bytes;
Mingyang Suna09adda2022-02-16 18:11:33 +0800538 struct conn_handle_t *handle = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800539 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800540
541 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800542 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800543 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800544 tfm_core_panic();
545 }
546
Mingyang Suna09adda2022-02-16 18:11:33 +0800547 priv_mode = GET_PARTITION_PRIVILEGED_MODE(
548 handle->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800549
550 /*
551 * It is a fatal error if message handle does not refer to a request
552 * message
553 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800554 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800555 tfm_core_panic();
556 }
557
558 /*
559 * It is a fatal error if invec_idx is equal to or greater than
560 * PSA_MAX_IOVEC
561 */
562 if (invec_idx >= PSA_MAX_IOVEC) {
563 tfm_core_panic();
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
Kevin Peng016b0e42022-04-15 11:28:48 +0800578 /* There was no remaining data in this input vector */
579 if (handle->msg.in_size[invec_idx] == 0) {
580 return 0;
581 }
582
Mingyang Sunb26b2802021-07-07 11:25:00 +0800583 /*
584 * Copy the client data to the service buffer. It is a fatal error
585 * if the memory reference for buffer is invalid or not read-write.
586 */
587 if (tfm_memory_check(buffer, num_bytes, false,
Ken Liuad3f3e62022-03-04 22:23:52 +0800588 TFM_MEMORY_ACCESS_RW, priv_mode) != PSA_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800589 tfm_core_panic();
590 }
591
Mingyang Suna09adda2022-02-16 18:11:33 +0800592 bytes = num_bytes > handle->msg.in_size[invec_idx] ?
593 handle->msg.in_size[invec_idx] : num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800594
Mingyang Suna09adda2022-02-16 18:11:33 +0800595 spm_memcpy(buffer, handle->invec[invec_idx].base, bytes);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800596
597 /* There maybe some remaining data */
Mingyang Suna09adda2022-02-16 18:11:33 +0800598 handle->invec[invec_idx].base =
599 (char *)handle->invec[invec_idx].base + bytes;
600 handle->msg.in_size[invec_idx] -= bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800601
602 return bytes;
603}
604
605size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx,
606 size_t num_bytes)
607{
Mingyang Suna09adda2022-02-16 18:11:33 +0800608 struct conn_handle_t *handle = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800609
610 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800611 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800612 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800613 tfm_core_panic();
614 }
615
616 /*
617 * It is a fatal error if message handle does not refer to a request
618 * message
619 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800620 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800621 tfm_core_panic();
622 }
623
624 /*
625 * It is a fatal error if invec_idx is equal to or greater than
626 * PSA_MAX_IOVEC
627 */
628 if (invec_idx >= PSA_MAX_IOVEC) {
629 tfm_core_panic();
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
Kevin Peng016b0e42022-04-15 11:28:48 +0800644 /* There was no remaining data in this input vector */
645 if (handle->msg.in_size[invec_idx] == 0) {
646 return 0;
647 }
648
Mingyang Sunb26b2802021-07-07 11:25:00 +0800649 /*
650 * If num_bytes is greater than the remaining size of the input vector then
651 * the remaining size of the input vector is used.
652 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800653 if (num_bytes > handle->msg.in_size[invec_idx]) {
654 num_bytes = handle->msg.in_size[invec_idx];
Mingyang Sunb26b2802021-07-07 11:25:00 +0800655 }
656
657 /* There maybe some remaining data */
Mingyang Suna09adda2022-02-16 18:11:33 +0800658 handle->invec[invec_idx].base =
659 (char *)handle->invec[invec_idx].base + num_bytes;
660 handle->msg.in_size[invec_idx] -= num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800661
662 return num_bytes;
663}
664
665void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
666 const void *buffer, size_t num_bytes)
667{
Mingyang Suna09adda2022-02-16 18:11:33 +0800668 struct conn_handle_t *handle = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800669 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800670
671 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800672 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800673 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800674 tfm_core_panic();
675 }
676
Mingyang Suna09adda2022-02-16 18:11:33 +0800677 priv_mode = GET_PARTITION_PRIVILEGED_MODE(
678 handle->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800679
680 /*
681 * It is a fatal error if message handle does not refer to a request
682 * message
683 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800684 if (handle->msg.type < PSA_IPC_CALL) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800685 tfm_core_panic();
686 }
687
688 /*
689 * It is a fatal error if outvec_idx is equal to or greater than
690 * PSA_MAX_IOVEC
691 */
692 if (outvec_idx >= PSA_MAX_IOVEC) {
693 tfm_core_panic();
694 }
695
696 /*
697 * It is a fatal error if the call attempts to write data past the end of
698 * the client output vector
699 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800700 if (num_bytes > handle->msg.out_size[outvec_idx] -
701 handle->outvec[outvec_idx].len) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800702 tfm_core_panic();
703 }
704
Shawn Shan038348e2021-09-08 17:11:04 +0800705#if PSA_FRAMEWORK_HAS_MM_IOVEC
706 /*
707 * It is a fatal error if the output vector has already been mapped using
708 * psa_map_outvec().
709 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800710 if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +0800711 tfm_core_panic();
712 }
713
Mingyang Suna09adda2022-02-16 18:11:33 +0800714 SET_IOVEC_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +0800715#endif
716
Mingyang Sunb26b2802021-07-07 11:25:00 +0800717 /*
718 * Copy the service buffer to client outvecs. It is a fatal error
719 * if the memory reference for buffer is invalid or not readable.
720 */
721 if (tfm_memory_check(buffer, num_bytes, false,
Ken Liuad3f3e62022-03-04 22:23:52 +0800722 TFM_MEMORY_ACCESS_RO, priv_mode) != PSA_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800723 tfm_core_panic();
724 }
725
Mingyang Suna09adda2022-02-16 18:11:33 +0800726 spm_memcpy((char *)handle->outvec[outvec_idx].base +
727 handle->outvec[outvec_idx].len, buffer, num_bytes);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800728
729 /* Update the write number */
Mingyang Suna09adda2022-02-16 18:11:33 +0800730 handle->outvec[outvec_idx].len += num_bytes;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800731}
732
Ken Liuf8c7e532022-02-10 15:03:04 +0800733psa_status_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle,
734 psa_status_t status)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800735{
Ken Liu0bed7e02022-02-10 12:38:07 +0800736 struct service_t *service;
Mingyang Suna09adda2022-02-16 18:11:33 +0800737 struct conn_handle_t *handle;
Ken Liuf8c7e532022-02-10 15:03:04 +0800738 psa_status_t ret = PSA_SUCCESS;
Mingyang Sun620c8562021-11-10 11:44:58 +0800739 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800740
741 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800742 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800743 if (!handle) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800744 tfm_core_panic();
745 }
746
747 /*
748 * RoT Service information is needed in this function, stored it in message
749 * body structure. Only two parameters are passed in this function: handle
750 * and status, so it is useful and simply to do like this.
751 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800752 service = handle->service;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800753 if (!service) {
754 tfm_core_panic();
755 }
756
Mingyang Suna09adda2022-02-16 18:11:33 +0800757 switch (handle->msg.type) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800758 case PSA_IPC_CONNECT:
759 /*
760 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
761 * input status is PSA_SUCCESS. Others return values are based on the
762 * input status.
763 */
764 if (status == PSA_SUCCESS) {
765 ret = msg_handle;
766 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
767 /* Refuse the client connection, indicating a permanent error. */
Mingyang Sunb26b2802021-07-07 11:25:00 +0800768 ret = PSA_ERROR_CONNECTION_REFUSED;
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800769 handle->status = TFM_HANDLE_STATUS_TO_FREE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800770 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
771 /* Fail the client connection, indicating a transient error. */
772 ret = PSA_ERROR_CONNECTION_BUSY;
773 } else {
774 tfm_core_panic();
775 }
776 break;
777 case PSA_IPC_DISCONNECT:
778 /* Service handle is not used anymore */
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800779 handle->status = TFM_HANDLE_STATUS_TO_FREE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800780
781 /*
782 * If the message type is PSA_IPC_DISCONNECT, then the status code is
783 * ignored
784 */
785 break;
786 default:
Mingyang Suna09adda2022-02-16 18:11:33 +0800787 if (handle->msg.type >= PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +0800788
789#if PSA_FRAMEWORK_HAS_MM_IOVEC
790
791 /*
792 * If the unmapped function is not called for an input/output vector
793 * that has been mapped, the framework will remove the mapping.
794 */
795 int i;
796
797 for (i = 0; i < PSA_MAX_IOVEC * 2; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800798 if (IOVEC_IS_MAPPED(handle, i) &&
799 (!IOVEC_IS_UNMAPPED(handle, i))) {
800 SET_IOVEC_UNMAPPED(handle, i);
Shawn Shan038348e2021-09-08 17:11:04 +0800801 /*
802 * Any output vectors that are still mapped will report that
803 * zero bytes have been written.
804 */
805 if (i >= OUTVEC_IDX_BASE) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800806 handle->outvec[i - OUTVEC_IDX_BASE].len = 0;
Shawn Shan038348e2021-09-08 17:11:04 +0800807 }
808 }
809 }
810
811#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800812 /* Reply to a request message. Return values are based on status */
813 ret = status;
814 /*
815 * The total number of bytes written to a single parameter must be
816 * reported to the client by updating the len member of the
817 * psa_outvec structure for the parameter before returning from
818 * psa_call().
819 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800820 update_caller_outvec_len(handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800821 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800822 handle->status = TFM_HANDLE_STATUS_TO_FREE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800823 }
824 } else {
825 tfm_core_panic();
826 }
827 }
828
829 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
830 /*
831 * If the source of the programmer error is a Secure Partition, the SPM
832 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
833 */
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800834 if (!TFM_CLIENT_ID_IS_NS(handle->msg.client_id)) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800835 tfm_core_panic();
836 }
Mingyang Sunb26b2802021-07-07 11:25:00 +0800837 }
838
Mingyang Sun620c8562021-11-10 11:44:58 +0800839 /*
840 * TODO: It can be optimized further by moving critical section protection
841 * to mailbox. Also need to check implementation when secure context is
842 * involved.
843 */
844 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu995a9742022-05-18 19:28:30 +0800845 ret = backend_replying(handle, ret);
Mingyang Sun620c8562021-11-10 11:44:58 +0800846 CRITICAL_SECTION_LEAVE(cs_assert);
847
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800848 if (handle->status == TFM_HANDLE_STATUS_TO_FREE) {
849 tfm_spm_free_conn_handle(handle);
850 } else {
851 handle->status = TFM_HANDLE_STATUS_IDLE;
852 }
853
Mingyang Sun620c8562021-11-10 11:44:58 +0800854 return ret;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800855}
856
Kevin Peng613b4172022-02-15 14:41:44 +0800857#if CONFIG_TFM_DOORBELL_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800858void tfm_spm_partition_psa_notify(int32_t partition_id)
859{
Ken Liu5d73c872021-08-19 19:23:17 +0800860 struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id);
861
862 spm_assert_signal(p_pt, PSA_DOORBELL);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800863}
864
865void tfm_spm_partition_psa_clear(void)
866{
Ken Liu92ede9f2021-10-20 09:35:00 +0800867 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800868 struct partition_t *partition = NULL;
869
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800870 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800871
872 /*
873 * It is a fatal error if the Secure Partition's doorbell signal is not
874 * currently asserted.
875 */
876 if ((partition->signals_asserted & PSA_DOORBELL) == 0) {
877 tfm_core_panic();
878 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800879
880 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800881 partition->signals_asserted &= ~PSA_DOORBELL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800882 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800883}
Kevin Peng613b4172022-02-15 14:41:44 +0800884#endif /* CONFIG_TFM_DOORBELL_API == 1 */
Mingyang Sunb26b2802021-07-07 11:25:00 +0800885
Mingyang Sunb26b2802021-07-07 11:25:00 +0800886void tfm_spm_partition_psa_panic(void)
887{
Joakim Andersson97cef7e2022-03-18 10:22:00 +0100888#ifdef CONFIG_TFM_HALT_ON_CORE_PANIC
889 tfm_hal_system_halt();
890#else
Mingyang Sunb26b2802021-07-07 11:25:00 +0800891 /*
892 * PSA FF recommends that the SPM causes the system to restart when a secure
893 * partition panics.
894 */
895 tfm_hal_system_reset();
Joakim Andersson97cef7e2022-03-18 10:22:00 +0100896#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800897}
898
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800899/* psa_set_rhandle is only needed by connection-based services */
900#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
901
902void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle)
903{
Mingyang Suna09adda2022-02-16 18:11:33 +0800904 struct conn_handle_t *handle;
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800905
906 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +0800907 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +0800908 if (!handle) {
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800909 tfm_core_panic();
910 }
911
912 /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */
Mingyang Suna09adda2022-02-16 18:11:33 +0800913 if (SERVICE_IS_STATELESS(handle->service->p_ldinf->flags)) {
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800914 tfm_core_panic();
915 }
916
Mingyang Suna09adda2022-02-16 18:11:33 +0800917 handle->msg.rhandle = rhandle;
918 handle->rhandle = rhandle;
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800919}
920
921#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
922
923#if CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1
Kevin Peng67a89fd2021-11-25 11:22:02 +0800924void tfm_spm_partition_psa_irq_enable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800925{
926 struct partition_t *partition;
927 struct irq_load_info_t *irq_info;
928
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800929 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800930
931 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
932 if (!irq_info) {
933 tfm_core_panic();
934 }
935
Kevin Pengd399a1f2021-09-08 15:33:14 +0800936 tfm_hal_irq_enable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800937}
938
Kevin Peng67a89fd2021-11-25 11:22:02 +0800939psa_irq_status_t tfm_spm_partition_psa_irq_disable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800940{
941 struct partition_t *partition;
942 struct irq_load_info_t *irq_info;
943
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800944 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800945
946 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
947 if (!irq_info) {
948 tfm_core_panic();
949 }
950
Kevin Pengd399a1f2021-09-08 15:33:14 +0800951 tfm_hal_irq_disable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800952
953 return 1;
954}
955
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800956/* This API is only used for FLIH. */
957#if CONFIG_TFM_FLIH_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800958void tfm_spm_partition_psa_reset_signal(psa_signal_t irq_signal)
959{
Ken Liu92ede9f2021-10-20 09:35:00 +0800960 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800961 struct irq_load_info_t *irq_info;
962 struct partition_t *partition;
963
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800964 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800965
966 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
967 if (!irq_info) {
968 tfm_core_panic();
969 }
970
971 if (!irq_info->flih_func) {
972 /* This API is for FLIH IRQs only */
973 tfm_core_panic();
974 }
975
976 if ((partition->signals_asserted & irq_signal) == 0) {
977 /* The signal is not asserted */
978 tfm_core_panic();
979 }
980
Ken Liu92ede9f2021-10-20 09:35:00 +0800981 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800982 partition->signals_asserted &= ~irq_signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800983 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800984}
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800985#endif
Shawn Shan038348e2021-09-08 17:11:04 +0800986
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800987/* This API is only used for SLIH. */
988#if CONFIG_TFM_SLIH_API == 1
989void tfm_spm_partition_psa_eoi(psa_signal_t irq_signal)
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800990{
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800991 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
992 struct irq_load_info_t *irq_info = NULL;
993 struct partition_t *partition = NULL;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800994
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800995 partition = GET_CURRENT_COMPONENT();
996
997 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
998 /* It is a fatal error if passed signal is not an interrupt signal. */
999 if (!irq_info) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001000 tfm_core_panic();
1001 }
1002
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001003 if (irq_info->flih_func) {
1004 /* This API is for SLIH IRQs only */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001005 tfm_core_panic();
1006 }
1007
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001008 /* It is a fatal error if passed signal is not currently asserted */
1009 if ((partition->signals_asserted & irq_signal) == 0) {
1010 tfm_core_panic();
1011 }
1012
1013 CRITICAL_SECTION_ENTER(cs_assert);
1014 partition->signals_asserted &= ~irq_signal;
1015 CRITICAL_SECTION_LEAVE(cs_assert);
1016
1017 tfm_hal_irq_clear_pending(irq_info->source);
1018 tfm_hal_irq_enable(irq_info->source);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001019}
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001020#endif
1021#endif /* CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001022
Shawn Shan038348e2021-09-08 17:11:04 +08001023#if PSA_FRAMEWORK_HAS_MM_IOVEC
1024
1025const void *tfm_spm_partition_psa_map_invec(psa_handle_t msg_handle,
1026 uint32_t invec_idx)
1027{
Mingyang Suna09adda2022-02-16 18:11:33 +08001028 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001029 uint32_t privileged;
1030 struct partition_t *partition = NULL;
1031
1032 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001033 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001034 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001035 tfm_core_panic();
1036 }
1037
Mingyang Suna09adda2022-02-16 18:11:33 +08001038 partition = handle->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001039 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001040
1041 /*
1042 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1043 * Service that received the message.
1044 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001045 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001046 tfm_core_panic();
1047 }
1048
1049 /*
1050 * It is a fatal error if message handle does not refer to a request
1051 * message.
1052 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001053 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001054 tfm_core_panic();
1055 }
1056
1057 /*
1058 * It is a fatal error if invec_idx is equal to or greater than
1059 * PSA_MAX_IOVEC.
1060 */
1061 if (invec_idx >= PSA_MAX_IOVEC) {
1062 tfm_core_panic();
1063 }
1064
1065 /* It is a fatal error if the input vector has length zero. */
Mingyang Suna09adda2022-02-16 18:11:33 +08001066 if (handle->msg.in_size[invec_idx] == 0) {
Shawn Shan038348e2021-09-08 17:11:04 +08001067 tfm_core_panic();
1068 }
1069
1070 /*
1071 * It is a fatal error if the input vector has already been mapped using
1072 * psa_map_invec().
1073 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001074 if (IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001075 tfm_core_panic();
1076 }
1077
1078 /*
1079 * It is a fatal error if the input vector has already been accessed
1080 * using psa_read() or psa_skip().
1081 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001082 if (IOVEC_IS_ACCESSED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001083 tfm_core_panic();
1084 }
1085
1086 /*
1087 * It is a fatal error if the memory reference for the wrap input vector is
1088 * invalid or not readable.
1089 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001090 if (tfm_memory_check(handle->invec[invec_idx].base,
1091 handle->invec[invec_idx].len,
1092 false, TFM_MEMORY_ACCESS_RO,
Ken Liuad3f3e62022-03-04 22:23:52 +08001093 privileged) != PSA_SUCCESS) {
Shawn Shan038348e2021-09-08 17:11:04 +08001094 tfm_core_panic();
1095 }
1096
Mingyang Suna09adda2022-02-16 18:11:33 +08001097 SET_IOVEC_MAPPED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001098
Mingyang Suna09adda2022-02-16 18:11:33 +08001099 return handle->invec[invec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001100}
1101
1102void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle,
1103 uint32_t invec_idx)
1104{
Mingyang Suna09adda2022-02-16 18:11:33 +08001105 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001106
1107 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001108 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001109 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001110 tfm_core_panic();
1111 }
1112
1113 /*
1114 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1115 * Service that received the message.
1116 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001117 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001118 tfm_core_panic();
1119 }
1120
1121 /*
1122 * It is a fatal error if message handle does not refer to a request
1123 * message.
1124 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001125 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001126 tfm_core_panic();
1127 }
1128
1129 /*
1130 * It is a fatal error if invec_idx is equal to or greater than
1131 * PSA_MAX_IOVEC.
1132 */
1133 if (invec_idx >= PSA_MAX_IOVEC) {
1134 tfm_core_panic();
1135 }
1136
1137 /*
1138 * It is a fatal error if The input vector has not been mapped by a call to
1139 * psa_map_invec().
1140 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001141 if (!IOVEC_IS_MAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001142 tfm_core_panic();
1143 }
1144
1145 /*
1146 * It is a fatal error if the input vector has already been unmapped by a
1147 * call to psa_unmap_invec().
1148 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001149 if (IOVEC_IS_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001150 tfm_core_panic();
1151 }
1152
Mingyang Suna09adda2022-02-16 18:11:33 +08001153 SET_IOVEC_UNMAPPED(handle, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001154}
1155
1156void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle,
1157 uint32_t outvec_idx)
1158{
Mingyang Suna09adda2022-02-16 18:11:33 +08001159 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001160 uint32_t privileged;
1161 struct partition_t *partition = NULL;
1162
1163 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001164 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001165 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001166 tfm_core_panic();
1167 }
1168
Mingyang Suna09adda2022-02-16 18:11:33 +08001169 partition = handle->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001170 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001171
1172 /*
1173 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1174 * Service that received the message.
1175 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001176 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001177 tfm_core_panic();
1178 }
1179
1180 /*
1181 * It is a fatal error if message handle does not refer to a request
1182 * message.
1183 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001184 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001185 tfm_core_panic();
1186 }
1187
1188 /*
1189 * It is a fatal error if outvec_idx is equal to or greater than
1190 * PSA_MAX_IOVEC.
1191 */
1192 if (outvec_idx >= PSA_MAX_IOVEC) {
1193 tfm_core_panic();
1194 }
1195
1196 /* It is a fatal error if the output vector has length zero. */
Mingyang Suna09adda2022-02-16 18:11:33 +08001197 if (handle->msg.out_size[outvec_idx] == 0) {
Shawn Shan038348e2021-09-08 17:11:04 +08001198 tfm_core_panic();
1199 }
1200
1201 /*
1202 * It is a fatal error if the output vector has already been mapped using
1203 * psa_map_outvec().
1204 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001205 if (IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001206 tfm_core_panic();
1207 }
1208
1209 /*
1210 * It is a fatal error if the output vector has already been accessed
1211 * using psa_write().
1212 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001213 if (IOVEC_IS_ACCESSED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001214 tfm_core_panic();
1215 }
1216
1217 /*
1218 * It is a fatal error if the output vector is invalid or not read-write.
1219 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001220 if (tfm_memory_check(handle->outvec[outvec_idx].base,
1221 handle->outvec[outvec_idx].len, false,
Ken Liuad3f3e62022-03-04 22:23:52 +08001222 TFM_MEMORY_ACCESS_RW, privileged) != PSA_SUCCESS) {
Shawn Shan038348e2021-09-08 17:11:04 +08001223 tfm_core_panic();
1224 }
Mingyang Suna09adda2022-02-16 18:11:33 +08001225 SET_IOVEC_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001226
Mingyang Suna09adda2022-02-16 18:11:33 +08001227 return handle->outvec[outvec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001228}
1229
1230void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle,
1231 uint32_t outvec_idx, size_t len)
1232{
Mingyang Suna09adda2022-02-16 18:11:33 +08001233 struct conn_handle_t *handle;
Shawn Shan038348e2021-09-08 17:11:04 +08001234
1235 /* It is a fatal error if message handle is invalid */
Kevin Penge61e7052022-01-27 14:57:06 +08001236 handle = spm_get_handle_by_msg_handle(msg_handle);
Mingyang Suna09adda2022-02-16 18:11:33 +08001237 if (!handle) {
Shawn Shan038348e2021-09-08 17:11:04 +08001238 tfm_core_panic();
1239 }
1240
1241 /*
1242 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1243 * Service that received the message.
1244 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001245 if (!SERVICE_ENABLED_MM_IOVEC(handle->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001246 tfm_core_panic();
1247 }
1248
1249 /*
1250 * It is a fatal error if message handle does not refer to a request
1251 * message.
1252 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001253 if (handle->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001254 tfm_core_panic();
1255 }
1256
1257 /*
1258 * It is a fatal error if outvec_idx is equal to or greater than
1259 * PSA_MAX_IOVEC.
1260 */
1261 if (outvec_idx >= PSA_MAX_IOVEC) {
1262 tfm_core_panic();
1263 }
1264
1265 /*
1266 * It is a fatal error if len is greater than the output vector size.
1267 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001268 if (len > handle->msg.out_size[outvec_idx]) {
Shawn Shan038348e2021-09-08 17:11:04 +08001269 tfm_core_panic();
1270 }
1271
1272 /*
1273 * It is a fatal error if The output vector has not been mapped by a call to
1274 * psa_map_outvec().
1275 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001276 if (!IOVEC_IS_MAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001277 tfm_core_panic();
1278 }
1279
1280 /*
1281 * It is a fatal error if the output vector has already been unmapped by a
1282 * call to psa_unmap_outvec().
1283 */
Mingyang Suna09adda2022-02-16 18:11:33 +08001284 if (IOVEC_IS_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001285 tfm_core_panic();
1286 }
1287
Mingyang Suna09adda2022-02-16 18:11:33 +08001288 SET_IOVEC_UNMAPPED(handle, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001289
1290 /* Update the write number */
Mingyang Suna09adda2022-02-16 18:11:33 +08001291 handle->outvec[outvec_idx].len = len;
Shawn Shan038348e2021-09-08 17:11:04 +08001292}
1293
1294#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */