blob: 810f9bd7a2bb31f193b98ffa59b10fb7149e9fe9 [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
70#define IOVEC_IS_MAPPED(msg, iovec_idx) \
71 ((((msg)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
72 IOVEC_MAPPED_BIT)
73#define IOVEC_IS_UNMAPPED(msg, iovec_idx) \
74 ((((msg)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
75 IOVEC_UNMAPPED_BIT)
76#define IOVEC_IS_ACCESSED(msg, iovec_idx) \
77 ((((msg)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
78 IOVEC_ACCESSED_BIT)
79#define SET_IOVEC_MAPPED(msg, iovec_idx) \
80 (((msg)->iovec_status) |= (IOVEC_MAPPED_BIT << \
81 ((iovec_idx) * IOVEC_STATUS_BITS)))
82#define SET_IOVEC_UNMAPPED(msg, iovec_idx) \
83 (((msg)->iovec_status) |= (IOVEC_UNMAPPED_BIT << \
84 ((iovec_idx) * IOVEC_STATUS_BITS)))
85#define SET_IOVEC_ACCESSED(msg, iovec_idx) \
86 (((msg)->iovec_status) |= (IOVEC_ACCESSED_BIT << \
87 ((iovec_idx) * IOVEC_STATUS_BITS)))
88
89#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */
90
Xinyu Zhangb287ef82021-11-03 18:38:50 +080091void spm_handle_programmer_errors(psa_status_t status)
92{
93 if (status == PSA_ERROR_PROGRAMMER_ERROR ||
94 status == PSA_ERROR_CONNECTION_REFUSED ||
95 status == PSA_ERROR_CONNECTION_BUSY) {
96 if (!tfm_spm_is_ns_caller()) {
97 tfm_core_panic();
98 }
99 }
100}
101
Mingyang Suneeca4652021-07-15 15:19:16 +0800102uint32_t tfm_spm_get_lifecycle_state(void)
103{
104 /*
105 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
106 * implemented in the future.
107 */
108 return PSA_LIFECYCLE_UNKNOWN;
109}
110
111/* PSA Client API function body */
112
Mingyang Sund44522a2020-01-16 16:48:37 +0800113uint32_t tfm_spm_client_psa_framework_version(void)
David Hu733d8f92019-09-23 15:32:40 +0800114{
115 return PSA_FRAMEWORK_VERSION;
116}
117
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800118uint32_t tfm_spm_client_psa_version(uint32_t sid)
David Hu733d8f92019-09-23 15:32:40 +0800119{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800120 struct service_t *service;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800121 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800122
123 /*
124 * It should return PSA_VERSION_NONE if the RoT Service is not
125 * implemented.
126 */
127 service = tfm_spm_get_service_by_sid(sid);
128 if (!service) {
129 return PSA_VERSION_NONE;
130 }
131
132 /*
Shawn Shan2365c902019-12-19 18:35:36 +0800133 * It should return PSA_VERSION_NONE if the caller is not authorized
134 * to access the RoT Service.
David Hu733d8f92019-09-23 15:32:40 +0800135 */
Ken Liubcae38b2021-01-20 15:47:44 +0800136 if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) {
Shawn Shan2365c902019-12-19 18:35:36 +0800137 return PSA_VERSION_NONE;
David Hu733d8f92019-09-23 15:32:40 +0800138 }
139
Ken Liuacd2a572021-05-12 16:19:04 +0800140 return service->p_ldinf->version;
David Hu733d8f92019-09-23 15:32:40 +0800141}
142
Mingyang Suneeca4652021-07-15 15:19:16 +0800143psa_status_t tfm_spm_client_psa_call(psa_handle_t handle,
144 uint32_t ctrl_param,
145 const psa_invec *inptr,
146 psa_outvec *outptr)
David Hu733d8f92019-09-23 15:32:40 +0800147{
148 psa_invec invecs[PSA_MAX_IOVEC];
149 psa_outvec outvecs[PSA_MAX_IOVEC];
Ken Liu0bed7e02022-02-10 12:38:07 +0800150 struct conn_handle_t *conn_handle;
Mingyang Sun783a59b2021-04-20 15:52:18 +0800151 struct service_t *service;
Summer Qinba2346e2019-11-12 16:26:31 +0800152 int i, j;
Summer Qin1ce712a2019-10-14 18:04:05 +0800153 int32_t client_id;
Mingyang Sun453ad402021-03-17 17:58:33 +0800154 uint32_t sid, version, index;
Mingyang Sune529e3b2021-07-12 14:46:30 +0800155 uint32_t privileged;
Mingyang Sun620c8562021-11-10 11:44:58 +0800156 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800157 bool ns_caller = tfm_spm_is_ns_caller();
Mingyang Suneeca4652021-07-15 15:19:16 +0800158 int32_t type = (int32_t)(int16_t)((ctrl_param & TYPE_MASK) >> TYPE_OFFSET);
159 size_t in_num = (size_t)((ctrl_param & IN_LEN_MASK) >> IN_LEN_OFFSET);
160 size_t out_num = (size_t)((ctrl_param & OUT_LEN_MASK) >> OUT_LEN_OFFSET);
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800161
162 /* The request type must be zero or positive. */
163 if (type < 0) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800164 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800165 }
David Hu733d8f92019-09-23 15:32:40 +0800166
Shawn Shanb222d892021-01-04 17:41:48 +0800167 /* It is a PROGRAMMER ERROR if in_len + out_len > PSA_MAX_IOVEC. */
David Hu733d8f92019-09-23 15:32:40 +0800168 if ((in_num > PSA_MAX_IOVEC) ||
169 (out_num > PSA_MAX_IOVEC) ||
170 (in_num + out_num > PSA_MAX_IOVEC)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800171 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800172 }
173
Kevin Peng385fda82021-08-18 10:41:19 +0800174 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800175
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800176 /* Allocate space from handle pool for static handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800177 if (IS_STATIC_HANDLE(handle)) {
Mingyang Sun453ad402021-03-17 17:58:33 +0800178 index = GET_INDEX_FROM_STATIC_HANDLE(handle);
Mingyang Sune8d38082021-03-30 18:34:40 +0800179
180 if (!IS_VALID_STATIC_HANDLE_IDX(index)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800181 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sune8d38082021-03-30 18:34:40 +0800182 }
183
Mingyang Sun453ad402021-03-17 17:58:33 +0800184 service = GET_STATELESS_SERVICE(index);
Mingyang Sun86213242021-07-14 10:26:43 +0800185 if (!service) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800186 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun86213242021-07-14 10:26:43 +0800187 }
188
Ken Liub3b2cb62021-05-22 00:39:28 +0800189 sid = service->p_ldinf->sid;
Mingyang Sun453ad402021-03-17 17:58:33 +0800190
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800191 /*
192 * It is a PROGRAMMER ERROR if the caller is not authorized to access
193 * the RoT Service.
194 */
195 if (tfm_spm_check_authorization(sid, service, ns_caller)
196 != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800197 return PSA_ERROR_CONNECTION_REFUSED;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800198 }
199
Mingyang Sun453ad402021-03-17 17:58:33 +0800200 version = GET_VERSION_FROM_STATIC_HANDLE(handle);
201
202 if (tfm_spm_check_client_version(service, version) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800203 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun453ad402021-03-17 17:58:33 +0800204 }
205
Mingyang Sun620c8562021-11-10 11:44:58 +0800206 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800207 conn_handle = tfm_spm_create_conn_handle(service, client_id);
Mingyang Sun620c8562021-11-10 11:44:58 +0800208 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800209
210 if (!conn_handle) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800211 return PSA_ERROR_CONNECTION_BUSY;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800212 }
213
Mingyang Sun6d5dc3d2021-03-15 15:34:44 +0800214 conn_handle->rhandle = NULL;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800215 handle = tfm_spm_to_user_handle(conn_handle);
216 } else {
Sherry Zhanga4575f32022-02-23 15:45:51 +0800217#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800218 conn_handle = tfm_spm_to_handle_instance(handle);
219
220 /* It is a PROGRAMMER ERROR if an invalid handle was passed. */
221 if (tfm_spm_validate_conn_handle(conn_handle, client_id)
222 != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800223 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800224 }
225
226 /*
227 * It is a PROGRAMMER ERROR if the connection is currently
228 * handling a request.
229 */
230 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800231 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800232 }
233
234 /*
235 * Return PSA_ERROR_PROGRAMMER_ERROR immediately for the connection
236 * has been terminated by the RoT Service.
237 */
238 if (conn_handle->status == TFM_HANDLE_STATUS_CONNECT_ERROR) {
239 return PSA_ERROR_PROGRAMMER_ERROR;
240 }
241
Ken Liu0bed7e02022-02-10 12:38:07 +0800242 service = conn_handle->service;
Shawn Shanb222d892021-01-04 17:41:48 +0800243
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800244 if (!service) {
245 /* FixMe: Need to implement a mechanism to resolve this failure. */
246 return PSA_ERROR_PROGRAMMER_ERROR;
247 }
Sherry Zhanga4575f32022-02-23 15:45:51 +0800248#else
249 return PSA_ERROR_PROGRAMMER_ERROR;
250#endif
David Hu733d8f92019-09-23 15:32:40 +0800251 }
252
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800253 privileged = GET_CURRENT_PARTITION_PRIVILEGED_MODE();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800254
Kevin Pengedb8ee42021-03-09 16:50:11 +0800255 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800256 * Read client invecs from the wrap input vector. It is a PROGRAMMER ERROR
David Hu733d8f92019-09-23 15:32:40 +0800257 * if the memory reference for the wrap input vector is invalid or not
258 * readable.
259 */
260 if (tfm_memory_check(inptr, in_num * sizeof(psa_invec), ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800261 TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800262 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800263 }
Summer Qinba2346e2019-11-12 16:26:31 +0800264
David Hu733d8f92019-09-23 15:32:40 +0800265 /*
266 * Read client outvecs from the wrap output vector and will update the
Shawn Shanb222d892021-01-04 17:41:48 +0800267 * actual length later. It is a PROGRAMMER ERROR if the memory reference for
David Hu733d8f92019-09-23 15:32:40 +0800268 * the wrap output vector is invalid or not read-write.
269 */
270 if (tfm_memory_check(outptr, out_num * sizeof(psa_outvec), ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800271 TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800272 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800273 }
274
Summer Qinf24dbb52020-07-23 14:53:54 +0800275 spm_memset(invecs, 0, sizeof(invecs));
276 spm_memset(outvecs, 0, sizeof(outvecs));
David Hu733d8f92019-09-23 15:32:40 +0800277
278 /* Copy the address out to avoid TOCTOU attacks. */
Summer Qinf24dbb52020-07-23 14:53:54 +0800279 spm_memcpy(invecs, inptr, in_num * sizeof(psa_invec));
280 spm_memcpy(outvecs, outptr, out_num * sizeof(psa_outvec));
David Hu733d8f92019-09-23 15:32:40 +0800281
282 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800283 * For client input vector, it is a PROGRAMMER ERROR if the provided payload
David Hu733d8f92019-09-23 15:32:40 +0800284 * memory reference was invalid or not readable.
285 */
286 for (i = 0; i < in_num; i++) {
287 if (tfm_memory_check(invecs[i].base, invecs[i].len, ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800288 TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800289 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800290 }
291 }
Summer Qinba2346e2019-11-12 16:26:31 +0800292
293 /*
294 * Clients must never overlap input parameters because of the risk of a
295 * double-fetch inconsistency.
296 * Overflow is checked in tfm_memory_check functions.
297 */
298 for (i = 0; i + 1 < in_num; i++) {
299 for (j = i+1; j < in_num; j++) {
TTornblom83d96372019-11-19 12:53:16 +0100300 if (!((char *) invecs[j].base + invecs[j].len <=
301 (char *) invecs[i].base ||
302 (char *) invecs[j].base >=
303 (char *) invecs[i].base + invecs[i].len)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800304 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qinba2346e2019-11-12 16:26:31 +0800305 }
306 }
307 }
308
David Hu733d8f92019-09-23 15:32:40 +0800309 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800310 * For client output vector, it is a PROGRAMMER ERROR if the provided
311 * payload memory reference was invalid or not read-write.
David Hu733d8f92019-09-23 15:32:40 +0800312 */
313 for (i = 0; i < out_num; i++) {
314 if (tfm_memory_check(outvecs[i].base, outvecs[i].len,
Ken Liubcae38b2021-01-20 15:47:44 +0800315 ns_caller, TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800316 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800317 }
318 }
319
Ken Liu0bed7e02022-02-10 12:38:07 +0800320 spm_fill_message(conn_handle, service, handle, type, client_id,
Summer Qin630c76b2020-05-20 10:32:58 +0800321 invecs, in_num, outvecs, out_num, outptr);
David Hu733d8f92019-09-23 15:32:40 +0800322
Ken Liu0bed7e02022-02-10 12:38:07 +0800323 return backend_instance.messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800324}
325
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800326/* Following PSA APIs are only needed by connection-based services */
327#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
328
329psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version)
330{
331 struct service_t *service;
Ken Liu0bed7e02022-02-10 12:38:07 +0800332 struct conn_handle_t *connect_handle;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800333 int32_t client_id;
334 psa_handle_t handle;
335 bool ns_caller = tfm_spm_is_ns_caller();
336 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
337
338 /*
339 * It is a PROGRAMMER ERROR if the RoT Service does not exist on the
340 * platform.
341 */
342 service = tfm_spm_get_service_by_sid(sid);
343 if (!service) {
344 return PSA_ERROR_CONNECTION_REFUSED;
345 }
346
347 /* It is a PROGRAMMER ERROR if connecting to a stateless service. */
348 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
349 return PSA_ERROR_PROGRAMMER_ERROR;
350 }
351
352 /*
353 * It is a PROGRAMMER ERROR if the caller is not authorized to access the
354 * RoT Service.
355 */
356 if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) {
357 return PSA_ERROR_CONNECTION_REFUSED;
358 }
359
360 /*
361 * It is a PROGRAMMER ERROR if the version of the RoT Service requested is
362 * not supported on the platform.
363 */
364 if (tfm_spm_check_client_version(service, version) != SPM_SUCCESS) {
365 return PSA_ERROR_CONNECTION_REFUSED;
366 }
367
368 client_id = tfm_spm_get_client_id(ns_caller);
369
370 /*
371 * Create connection handle here since it is possible to return the error
372 * code to client when creation fails.
373 */
374 CRITICAL_SECTION_ENTER(cs_assert);
375 connect_handle = tfm_spm_create_conn_handle(service, client_id);
376 CRITICAL_SECTION_LEAVE(cs_assert);
377 if (!connect_handle) {
378 return PSA_ERROR_CONNECTION_BUSY;
379 }
380
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800381 handle = tfm_spm_to_user_handle(connect_handle);
382 /* No input or output needed for connect message */
Ken Liu0bed7e02022-02-10 12:38:07 +0800383 spm_fill_message(connect_handle, service, handle, PSA_IPC_CONNECT,
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800384 client_id, NULL, 0, NULL, 0, NULL);
385
Ken Liu0bed7e02022-02-10 12:38:07 +0800386 return backend_instance.messaging(service, connect_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800387}
388
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800389psa_status_t tfm_spm_client_psa_close(psa_handle_t handle)
David Hu733d8f92019-09-23 15:32:40 +0800390{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800391 struct service_t *service;
Ken Liu0bed7e02022-02-10 12:38:07 +0800392 struct conn_handle_t *conn_handle;
Summer Qin1ce712a2019-10-14 18:04:05 +0800393 int32_t client_id;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800394 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800395
396 /* It will have no effect if called with the NULL handle */
397 if (handle == PSA_NULL_HANDLE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800398 return PSA_SUCCESS;
David Hu733d8f92019-09-23 15:32:40 +0800399 }
400
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800401 /* It is a PROGRAMMER ERROR if called with a stateless handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800402 if (IS_STATIC_HANDLE(handle)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800403 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800404 }
405
Kevin Peng385fda82021-08-18 10:41:19 +0800406 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800407
Summer Qin373feb12020-03-27 15:35:33 +0800408 conn_handle = tfm_spm_to_handle_instance(handle);
David Hu733d8f92019-09-23 15:32:40 +0800409 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800410 * It is a PROGRAMMER ERROR if an invalid handle was provided that is not
411 * the null handle.
David Hu733d8f92019-09-23 15:32:40 +0800412 */
Ken Liubcae38b2021-01-20 15:47:44 +0800413 if (tfm_spm_validate_conn_handle(conn_handle, client_id) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800414 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qin1ce712a2019-10-14 18:04:05 +0800415 }
Shawn Shanb222d892021-01-04 17:41:48 +0800416
Ken Liu0bed7e02022-02-10 12:38:07 +0800417 service = conn_handle->service;
David Hu733d8f92019-09-23 15:32:40 +0800418 if (!service) {
419 /* FixMe: Need to implement one mechanism to resolve this failure. */
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800420 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800421 }
422
Shawn Shanb222d892021-01-04 17:41:48 +0800423 /*
424 * It is a PROGRAMMER ERROR if the connection is currently handling a
425 * request.
426 */
Summer Qin630c76b2020-05-20 10:32:58 +0800427 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800428 return PSA_ERROR_PROGRAMMER_ERROR;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800429 }
430
David Hu733d8f92019-09-23 15:32:40 +0800431 /* No input or output needed for close message */
Ken Liu0bed7e02022-02-10 12:38:07 +0800432 spm_fill_message(conn_handle, service, handle, PSA_IPC_DISCONNECT,
433 client_id, NULL, 0, NULL, 0, NULL);
David Hu733d8f92019-09-23 15:32:40 +0800434
Ken Liu0bed7e02022-02-10 12:38:07 +0800435 return backend_instance.messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800436}
Mingyang Sunb26b2802021-07-07 11:25:00 +0800437
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800438#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
439
Mingyang Suneeca4652021-07-15 15:19:16 +0800440/* PSA Partition API function body */
441
Kevin Peng613b4172022-02-15 14:41:44 +0800442#if CONFIG_TFM_SPM_BACKEND_IPC == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800443psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask,
444 uint32_t timeout)
445{
446 struct partition_t *partition = NULL;
447
448 /*
449 * Timeout[30:0] are reserved for future use.
450 * SPM must ignore the value of RES.
451 */
452 timeout &= PSA_TIMEOUT_MASK;
453
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800454 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800455
456 /*
457 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
458 * signals.
459 */
460 if ((partition->signals_allowed & signal_mask) == 0) {
461 tfm_core_panic();
462 }
463
464 /*
Ken Liu5d73c872021-08-19 19:23:17 +0800465 * thrd_wait_on() blocks the caller thread if no signals are available.
Mingyang Sunb26b2802021-07-07 11:25:00 +0800466 * In this case, the return value of this function is temporary set into
467 * runtime context. After new signal(s) are available, the return value
468 * is updated with the available signal(s) and blocked thread gets to run.
469 */
470 if (timeout == PSA_BLOCK &&
471 (partition->signals_asserted & signal_mask) == 0) {
472 partition->signals_waiting = signal_mask;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800473 thrd_wait_on(&partition->waitobj, CURRENT_THREAD);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800474 }
475
476 return partition->signals_asserted & signal_mask;
477}
478
479psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg)
480{
Ken Liu0bed7e02022-02-10 12:38:07 +0800481 struct conn_handle_t *tmp_msg = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800482 struct partition_t *partition = NULL;
483 uint32_t privileged;
484
485 /*
486 * Only one message could be retrieved every time for psa_get(). It is a
487 * fatal error if the input signal has more than a signal bit set.
488 */
489 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
490 tfm_core_panic();
491 }
492
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800493 partition = GET_CURRENT_COMPONENT();
494
Kevin Penga40d29f2022-01-19 14:44:34 +0800495 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800496
497 /*
498 * Write the message to the service buffer. It is a fatal error if the
499 * input msg pointer is not a valid memory reference or not read-write.
500 */
501 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
502 privileged) != SPM_SUCCESS) {
503 tfm_core_panic();
504 }
505
506 /*
507 * It is a fatal error if the caller call psa_get() when no message has
508 * been set. The caller must call this function after an RoT Service signal
509 * is returned by psa_wait().
510 */
511 if (partition->signals_asserted == 0) {
512 tfm_core_panic();
513 }
514
515 /*
516 * It is a fatal error if the RoT Service signal is not currently asserted.
517 */
518 if ((partition->signals_asserted & signal) == 0) {
519 tfm_core_panic();
520 }
521
522 /*
523 * Get message by signal from partition. It is a fatal error if getting
524 * failed, which means the input signal is not correspond to an RoT service.
525 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800526 tmp_msg = spm_get_handle_by_signal(partition, signal);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800527 if (!tmp_msg) {
528 return PSA_ERROR_DOES_NOT_EXIST;
529 }
530
Ken Liu0bed7e02022-02-10 12:38:07 +0800531 tmp_msg->status = TFM_HANDLE_STATUS_ACTIVE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800532
533 spm_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
534
535 return PSA_SUCCESS;
536}
Mingyang Sun4609ac72022-02-08 18:56:35 +0800537#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800538
Mingyang Sunb26b2802021-07-07 11:25:00 +0800539size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
540 void *buffer, size_t num_bytes)
541{
542 size_t bytes;
Ken Liu0bed7e02022-02-10 12:38:07 +0800543 struct conn_handle_t *msg = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800544 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800545
546 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +0800547 msg = spm_get_handle_by_user_handle(msg_handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800548 if (!msg) {
549 tfm_core_panic();
550 }
551
Kevin Penga40d29f2022-01-19 14:44:34 +0800552 priv_mode = GET_PARTITION_PRIVILEGED_MODE(msg->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800553
554 /*
555 * It is a fatal error if message handle does not refer to a request
556 * message
557 */
558 if (msg->msg.type < PSA_IPC_CALL) {
559 tfm_core_panic();
560 }
561
562 /*
563 * It is a fatal error if invec_idx is equal to or greater than
564 * PSA_MAX_IOVEC
565 */
566 if (invec_idx >= PSA_MAX_IOVEC) {
567 tfm_core_panic();
568 }
569
570 /* There was no remaining data in this input vector */
571 if (msg->msg.in_size[invec_idx] == 0) {
572 return 0;
573 }
574
Shawn Shan038348e2021-09-08 17:11:04 +0800575#if PSA_FRAMEWORK_HAS_MM_IOVEC
576 /*
577 * It is a fatal error if the input vector has already been mapped using
578 * psa_map_invec().
579 */
580 if (IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
581 tfm_core_panic();
582 }
583
584 SET_IOVEC_ACCESSED(msg, (invec_idx + INVEC_IDX_BASE));
585#endif
586
Mingyang Sunb26b2802021-07-07 11:25:00 +0800587 /*
588 * Copy the client data to the service buffer. It is a fatal error
589 * if the memory reference for buffer is invalid or not read-write.
590 */
591 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800592 TFM_MEMORY_ACCESS_RW, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800593 tfm_core_panic();
594 }
595
596 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
597 msg->msg.in_size[invec_idx] : num_bytes;
598
599 spm_memcpy(buffer, msg->invec[invec_idx].base, bytes);
600
601 /* There maybe some remaining data */
602 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
603 msg->msg.in_size[invec_idx] -= bytes;
604
605 return bytes;
606}
607
608size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx,
609 size_t num_bytes)
610{
Ken Liu0bed7e02022-02-10 12:38:07 +0800611 struct conn_handle_t *msg = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800612
613 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +0800614 msg = spm_get_handle_by_user_handle(msg_handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800615 if (!msg) {
616 tfm_core_panic();
617 }
618
619 /*
620 * It is a fatal error if message handle does not refer to a request
621 * message
622 */
623 if (msg->msg.type < PSA_IPC_CALL) {
624 tfm_core_panic();
625 }
626
627 /*
628 * It is a fatal error if invec_idx is equal to or greater than
629 * PSA_MAX_IOVEC
630 */
631 if (invec_idx >= PSA_MAX_IOVEC) {
632 tfm_core_panic();
633 }
634
635 /* There was no remaining data in this input vector */
636 if (msg->msg.in_size[invec_idx] == 0) {
637 return 0;
638 }
639
Shawn Shan038348e2021-09-08 17:11:04 +0800640#if PSA_FRAMEWORK_HAS_MM_IOVEC
641 /*
642 * It is a fatal error if the input vector has already been mapped using
643 * psa_map_invec().
644 */
645 if (IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
646 tfm_core_panic();
647 }
648
649 SET_IOVEC_ACCESSED(msg, (invec_idx + INVEC_IDX_BASE));
650#endif
651
Mingyang Sunb26b2802021-07-07 11:25:00 +0800652 /*
653 * If num_bytes is greater than the remaining size of the input vector then
654 * the remaining size of the input vector is used.
655 */
656 if (num_bytes > msg->msg.in_size[invec_idx]) {
657 num_bytes = msg->msg.in_size[invec_idx];
658 }
659
660 /* There maybe some remaining data */
661 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
662 num_bytes;
663 msg->msg.in_size[invec_idx] -= num_bytes;
664
665 return num_bytes;
666}
667
668void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
669 const void *buffer, size_t num_bytes)
670{
Ken Liu0bed7e02022-02-10 12:38:07 +0800671 struct conn_handle_t *msg = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800672 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800673
674 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +0800675 msg = spm_get_handle_by_user_handle(msg_handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800676 if (!msg) {
677 tfm_core_panic();
678 }
679
Kevin Penga40d29f2022-01-19 14:44:34 +0800680 priv_mode = GET_PARTITION_PRIVILEGED_MODE(msg->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800681
682 /*
683 * It is a fatal error if message handle does not refer to a request
684 * message
685 */
686 if (msg->msg.type < PSA_IPC_CALL) {
687 tfm_core_panic();
688 }
689
690 /*
691 * It is a fatal error if outvec_idx is equal to or greater than
692 * PSA_MAX_IOVEC
693 */
694 if (outvec_idx >= PSA_MAX_IOVEC) {
695 tfm_core_panic();
696 }
697
698 /*
699 * It is a fatal error if the call attempts to write data past the end of
700 * the client output vector
701 */
702 if (num_bytes > msg->msg.out_size[outvec_idx] -
703 msg->outvec[outvec_idx].len) {
704 tfm_core_panic();
705 }
706
Shawn Shan038348e2021-09-08 17:11:04 +0800707#if PSA_FRAMEWORK_HAS_MM_IOVEC
708 /*
709 * It is a fatal error if the output vector has already been mapped using
710 * psa_map_outvec().
711 */
712 if (IOVEC_IS_MAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
713 tfm_core_panic();
714 }
715
716 SET_IOVEC_ACCESSED(msg, (outvec_idx + OUTVEC_IDX_BASE));
717#endif
718
Mingyang Sunb26b2802021-07-07 11:25:00 +0800719 /*
720 * Copy the service buffer to client outvecs. It is a fatal error
721 * if the memory reference for buffer is invalid or not readable.
722 */
723 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800724 TFM_MEMORY_ACCESS_RO, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800725 tfm_core_panic();
726 }
727
728 spm_memcpy((char *)msg->outvec[outvec_idx].base +
729 msg->outvec[outvec_idx].len, buffer, num_bytes);
730
731 /* Update the write number */
732 msg->outvec[outvec_idx].len += num_bytes;
733}
734
Ken Liuf8c7e532022-02-10 15:03:04 +0800735psa_status_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle,
736 psa_status_t status)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800737{
Ken Liu0bed7e02022-02-10 12:38:07 +0800738 struct service_t *service;
739 struct conn_handle_t *hdl;
Ken Liuf8c7e532022-02-10 15:03:04 +0800740 psa_status_t ret = PSA_SUCCESS;
Mingyang Sun620c8562021-11-10 11:44:58 +0800741 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800742
743 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +0800744 hdl = spm_get_handle_by_user_handle(msg_handle);
745 if (!hdl) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800746 tfm_core_panic();
747 }
748
749 /*
750 * RoT Service information is needed in this function, stored it in message
751 * body structure. Only two parameters are passed in this function: handle
752 * and status, so it is useful and simply to do like this.
753 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800754 service = hdl->service;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800755 if (!service) {
756 tfm_core_panic();
757 }
758
Ken Liu0bed7e02022-02-10 12:38:07 +0800759 switch (hdl->msg.type) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800760 case PSA_IPC_CONNECT:
761 /*
762 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
763 * input status is PSA_SUCCESS. Others return values are based on the
764 * input status.
765 */
766 if (status == PSA_SUCCESS) {
767 ret = msg_handle;
768 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
769 /* Refuse the client connection, indicating a permanent error. */
Ken Liu0bed7e02022-02-10 12:38:07 +0800770 tfm_spm_free_conn_handle(service, hdl);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800771 ret = PSA_ERROR_CONNECTION_REFUSED;
772 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
773 /* Fail the client connection, indicating a transient error. */
774 ret = PSA_ERROR_CONNECTION_BUSY;
775 } else {
776 tfm_core_panic();
777 }
778 break;
779 case PSA_IPC_DISCONNECT:
780 /* Service handle is not used anymore */
Ken Liu0bed7e02022-02-10 12:38:07 +0800781 tfm_spm_free_conn_handle(service, hdl);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800782
783 /*
784 * If the message type is PSA_IPC_DISCONNECT, then the status code is
785 * ignored
786 */
787 break;
788 default:
Ken Liu0bed7e02022-02-10 12:38:07 +0800789 if (hdl->msg.type >= PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +0800790
791#if PSA_FRAMEWORK_HAS_MM_IOVEC
792
793 /*
794 * If the unmapped function is not called for an input/output vector
795 * that has been mapped, the framework will remove the mapping.
796 */
797 int i;
798
799 for (i = 0; i < PSA_MAX_IOVEC * 2; i++) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800800 if (IOVEC_IS_MAPPED(hdl, i) && (!IOVEC_IS_UNMAPPED(hdl, i))) {
801 SET_IOVEC_UNMAPPED(hdl, i);
Shawn Shan038348e2021-09-08 17:11:04 +0800802 /*
803 * Any output vectors that are still mapped will report that
804 * zero bytes have been written.
805 */
806 if (i >= OUTVEC_IDX_BASE) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800807 hdl->outvec[i - OUTVEC_IDX_BASE].len = 0;
Shawn Shan038348e2021-09-08 17:11:04 +0800808 }
809 }
810 }
811
812#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800813 /* Reply to a request message. Return values are based on status */
814 ret = status;
815 /*
816 * The total number of bytes written to a single parameter must be
817 * reported to the client by updating the len member of the
818 * psa_outvec structure for the parameter before returning from
819 * psa_call().
820 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800821 update_caller_outvec_len(hdl);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800822 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800823 tfm_spm_free_conn_handle(service, hdl);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800824 }
825 } else {
826 tfm_core_panic();
827 }
828 }
829
830 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
831 /*
832 * If the source of the programmer error is a Secure Partition, the SPM
833 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
834 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800835 if (TFM_CLIENT_ID_IS_NS(hdl->msg.client_id)) {
836 hdl->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800837 } else {
838 tfm_core_panic();
839 }
840 } else {
Ken Liu0bed7e02022-02-10 12:38:07 +0800841 hdl->status = TFM_HANDLE_STATUS_IDLE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800842 }
843
Mingyang Sun620c8562021-11-10 11:44:58 +0800844 /*
845 * TODO: It can be optimized further by moving critical section protection
846 * to mailbox. Also need to check implementation when secure context is
847 * involved.
848 */
849 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu0bed7e02022-02-10 12:38:07 +0800850 ret = backend_instance.replying(hdl, ret);
Mingyang Sun620c8562021-11-10 11:44:58 +0800851 CRITICAL_SECTION_LEAVE(cs_assert);
852
853 return ret;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800854}
855
Kevin Peng613b4172022-02-15 14:41:44 +0800856#if CONFIG_TFM_DOORBELL_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800857void tfm_spm_partition_psa_notify(int32_t partition_id)
858{
Ken Liu5d73c872021-08-19 19:23:17 +0800859 struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id);
860
861 spm_assert_signal(p_pt, PSA_DOORBELL);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800862}
863
864void tfm_spm_partition_psa_clear(void)
865{
Ken Liu92ede9f2021-10-20 09:35:00 +0800866 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800867 struct partition_t *partition = NULL;
868
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800869 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800870
871 /*
872 * It is a fatal error if the Secure Partition's doorbell signal is not
873 * currently asserted.
874 */
875 if ((partition->signals_asserted & PSA_DOORBELL) == 0) {
876 tfm_core_panic();
877 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800878
879 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800880 partition->signals_asserted &= ~PSA_DOORBELL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800881 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800882}
Kevin Peng613b4172022-02-15 14:41:44 +0800883#endif /* CONFIG_TFM_DOORBELL_API == 1 */
Mingyang Sunb26b2802021-07-07 11:25:00 +0800884
Mingyang Sunb26b2802021-07-07 11:25:00 +0800885void tfm_spm_partition_psa_panic(void)
886{
887 /*
888 * PSA FF recommends that the SPM causes the system to restart when a secure
889 * partition panics.
890 */
891 tfm_hal_system_reset();
892}
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{
899 struct conn_handle_t *hdl;
900
901 /* It is a fatal error if message handle is invalid */
902 hdl = spm_get_handle_by_user_handle(msg_handle);
903 if (!hdl) {
904 tfm_core_panic();
905 }
906
907 /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */
908 if (SERVICE_IS_STATELESS(hdl->service->p_ldinf->flags)) {
909 tfm_core_panic();
910 }
911
912 hdl->msg.rhandle = rhandle;
913 hdl->rhandle = rhandle;
914}
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{
Ken Liu0bed7e02022-02-10 12:38:07 +08001023 struct conn_handle_t *hdl;
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 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001028 hdl = spm_get_handle_by_user_handle(msg_handle);
1029 if (!hdl) {
Shawn Shan038348e2021-09-08 17:11:04 +08001030 tfm_core_panic();
1031 }
1032
Ken Liu0bed7e02022-02-10 12:38:07 +08001033 partition = hdl->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 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001040 if (!SERVICE_ENABLED_MM_IOVEC(hdl->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 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001048 if (hdl->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. */
Ken Liu0bed7e02022-02-10 12:38:07 +08001061 if (hdl->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 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001069 if (IOVEC_IS_MAPPED(hdl, (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 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001077 if (IOVEC_IS_ACCESSED(hdl, (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 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001085 if (tfm_memory_check(hdl->invec[invec_idx].base, hdl->invec[invec_idx].len,
Shawn Shan038348e2021-09-08 17:11:04 +08001086 false, TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
1087 tfm_core_panic();
1088 }
1089
Ken Liu0bed7e02022-02-10 12:38:07 +08001090 SET_IOVEC_MAPPED(hdl, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001091
Ken Liu0bed7e02022-02-10 12:38:07 +08001092 return hdl->invec[invec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001093}
1094
1095void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle,
1096 uint32_t invec_idx)
1097{
Ken Liu0bed7e02022-02-10 12:38:07 +08001098 struct conn_handle_t *hdl;
Shawn Shan038348e2021-09-08 17:11:04 +08001099
1100 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +08001101 hdl = spm_get_handle_by_user_handle(msg_handle);
1102 if (!hdl) {
Shawn Shan038348e2021-09-08 17:11:04 +08001103 tfm_core_panic();
1104 }
1105
1106 /*
1107 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1108 * Service that received the message.
1109 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001110 if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001111 tfm_core_panic();
1112 }
1113
1114 /*
1115 * It is a fatal error if message handle does not refer to a request
1116 * message.
1117 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001118 if (hdl->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001119 tfm_core_panic();
1120 }
1121
1122 /*
1123 * It is a fatal error if invec_idx is equal to or greater than
1124 * PSA_MAX_IOVEC.
1125 */
1126 if (invec_idx >= PSA_MAX_IOVEC) {
1127 tfm_core_panic();
1128 }
1129
1130 /*
1131 * It is a fatal error if The input vector has not been mapped by a call to
1132 * psa_map_invec().
1133 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001134 if (!IOVEC_IS_MAPPED(hdl, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001135 tfm_core_panic();
1136 }
1137
1138 /*
1139 * It is a fatal error if the input vector has already been unmapped by a
1140 * call to psa_unmap_invec().
1141 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001142 if (IOVEC_IS_UNMAPPED(hdl, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001143 tfm_core_panic();
1144 }
1145
Ken Liu0bed7e02022-02-10 12:38:07 +08001146 SET_IOVEC_UNMAPPED(hdl, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001147}
1148
1149void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle,
1150 uint32_t outvec_idx)
1151{
Ken Liu0bed7e02022-02-10 12:38:07 +08001152 struct conn_handle_t *hdl;
Shawn Shan038348e2021-09-08 17:11:04 +08001153 uint32_t privileged;
1154 struct partition_t *partition = NULL;
1155
1156 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +08001157 hdl = spm_get_handle_by_user_handle(msg_handle);
1158 if (!hdl) {
Shawn Shan038348e2021-09-08 17:11:04 +08001159 tfm_core_panic();
1160 }
1161
Ken Liu0bed7e02022-02-10 12:38:07 +08001162 partition = hdl->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001163 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001164
1165 /*
1166 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1167 * Service that received the message.
1168 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001169 if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001170 tfm_core_panic();
1171 }
1172
1173 /*
1174 * It is a fatal error if message handle does not refer to a request
1175 * message.
1176 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001177 if (hdl->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001178 tfm_core_panic();
1179 }
1180
1181 /*
1182 * It is a fatal error if outvec_idx is equal to or greater than
1183 * PSA_MAX_IOVEC.
1184 */
1185 if (outvec_idx >= PSA_MAX_IOVEC) {
1186 tfm_core_panic();
1187 }
1188
1189 /* It is a fatal error if the output vector has length zero. */
Ken Liu0bed7e02022-02-10 12:38:07 +08001190 if (hdl->msg.out_size[outvec_idx] == 0) {
Shawn Shan038348e2021-09-08 17:11:04 +08001191 tfm_core_panic();
1192 }
1193
1194 /*
1195 * It is a fatal error if the output vector has already been mapped using
1196 * psa_map_outvec().
1197 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001198 if (IOVEC_IS_MAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001199 tfm_core_panic();
1200 }
1201
1202 /*
1203 * It is a fatal error if the output vector has already been accessed
1204 * using psa_write().
1205 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001206 if (IOVEC_IS_ACCESSED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001207 tfm_core_panic();
1208 }
1209
1210 /*
1211 * It is a fatal error if the output vector is invalid or not read-write.
1212 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001213 if (tfm_memory_check(hdl->outvec[outvec_idx].base,
1214 hdl->outvec[outvec_idx].len, false,
Shawn Shan038348e2021-09-08 17:11:04 +08001215 TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
1216 tfm_core_panic();
1217 }
Ken Liu0bed7e02022-02-10 12:38:07 +08001218 SET_IOVEC_MAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001219
Ken Liu0bed7e02022-02-10 12:38:07 +08001220 return hdl->outvec[outvec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001221}
1222
1223void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle,
1224 uint32_t outvec_idx, size_t len)
1225{
Ken Liu0bed7e02022-02-10 12:38:07 +08001226 struct conn_handle_t *hdl;
Shawn Shan038348e2021-09-08 17:11:04 +08001227
1228 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +08001229 hdl = spm_get_handle_by_user_handle(msg_handle);
1230 if (!hdl) {
Shawn Shan038348e2021-09-08 17:11:04 +08001231 tfm_core_panic();
1232 }
1233
1234 /*
1235 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1236 * Service that received the message.
1237 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001238 if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001239 tfm_core_panic();
1240 }
1241
1242 /*
1243 * It is a fatal error if message handle does not refer to a request
1244 * message.
1245 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001246 if (hdl->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001247 tfm_core_panic();
1248 }
1249
1250 /*
1251 * It is a fatal error if outvec_idx is equal to or greater than
1252 * PSA_MAX_IOVEC.
1253 */
1254 if (outvec_idx >= PSA_MAX_IOVEC) {
1255 tfm_core_panic();
1256 }
1257
1258 /*
1259 * It is a fatal error if len is greater than the output vector size.
1260 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001261 if (len > hdl->msg.out_size[outvec_idx]) {
Shawn Shan038348e2021-09-08 17:11:04 +08001262 tfm_core_panic();
1263 }
1264
1265 /*
1266 * It is a fatal error if The output vector has not been mapped by a call to
1267 * psa_map_outvec().
1268 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001269 if (!IOVEC_IS_MAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001270 tfm_core_panic();
1271 }
1272
1273 /*
1274 * It is a fatal error if the output vector has already been unmapped by a
1275 * call to psa_unmap_outvec().
1276 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001277 if (IOVEC_IS_UNMAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001278 tfm_core_panic();
1279 }
1280
Ken Liu0bed7e02022-02-10 12:38:07 +08001281 SET_IOVEC_UNMAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001282
1283 /* Update the write number */
Ken Liu0bed7e02022-02-10 12:38:07 +08001284 hdl->outvec[outvec_idx].len = len;
Shawn Shan038348e2021-09-08 17:11:04 +08001285}
1286
1287#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */