blob: c8ede674f897b025d0cf88785ff808d925a37f8d [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 {
217 conn_handle = tfm_spm_to_handle_instance(handle);
218
219 /* It is a PROGRAMMER ERROR if an invalid handle was passed. */
220 if (tfm_spm_validate_conn_handle(conn_handle, client_id)
221 != SPM_SUCCESS) {
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 */
229 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800230 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Suncb6f70e2021-03-05 23:30:25 +0800231 }
232
233 /*
234 * Return PSA_ERROR_PROGRAMMER_ERROR immediately for the connection
235 * has been terminated by the RoT Service.
236 */
237 if (conn_handle->status == TFM_HANDLE_STATUS_CONNECT_ERROR) {
238 return PSA_ERROR_PROGRAMMER_ERROR;
239 }
240
Ken Liu0bed7e02022-02-10 12:38:07 +0800241 service = conn_handle->service;
Shawn Shanb222d892021-01-04 17:41:48 +0800242
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800243 if (!service) {
244 /* FixMe: Need to implement a mechanism to resolve this failure. */
245 return PSA_ERROR_PROGRAMMER_ERROR;
246 }
David Hu733d8f92019-09-23 15:32:40 +0800247 }
248
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800249 privileged = GET_CURRENT_PARTITION_PRIVILEGED_MODE();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800250
Kevin Pengedb8ee42021-03-09 16:50:11 +0800251 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800252 * Read client invecs from the wrap input vector. It is a PROGRAMMER ERROR
David Hu733d8f92019-09-23 15:32:40 +0800253 * if the memory reference for the wrap input vector is invalid or not
254 * readable.
255 */
256 if (tfm_memory_check(inptr, in_num * sizeof(psa_invec), ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800257 TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800258 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800259 }
Summer Qinba2346e2019-11-12 16:26:31 +0800260
David Hu733d8f92019-09-23 15:32:40 +0800261 /*
262 * Read client outvecs from the wrap output vector and will update the
Shawn Shanb222d892021-01-04 17:41:48 +0800263 * actual length later. It is a PROGRAMMER ERROR if the memory reference for
David Hu733d8f92019-09-23 15:32:40 +0800264 * the wrap output vector is invalid or not read-write.
265 */
266 if (tfm_memory_check(outptr, out_num * sizeof(psa_outvec), ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800267 TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800268 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800269 }
270
Summer Qinf24dbb52020-07-23 14:53:54 +0800271 spm_memset(invecs, 0, sizeof(invecs));
272 spm_memset(outvecs, 0, sizeof(outvecs));
David Hu733d8f92019-09-23 15:32:40 +0800273
274 /* Copy the address out to avoid TOCTOU attacks. */
Summer Qinf24dbb52020-07-23 14:53:54 +0800275 spm_memcpy(invecs, inptr, in_num * sizeof(psa_invec));
276 spm_memcpy(outvecs, outptr, out_num * sizeof(psa_outvec));
David Hu733d8f92019-09-23 15:32:40 +0800277
278 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800279 * For client input vector, it is a PROGRAMMER ERROR if the provided payload
David Hu733d8f92019-09-23 15:32:40 +0800280 * memory reference was invalid or not readable.
281 */
282 for (i = 0; i < in_num; i++) {
283 if (tfm_memory_check(invecs[i].base, invecs[i].len, ns_caller,
Ken Liubcae38b2021-01-20 15:47:44 +0800284 TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800285 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800286 }
287 }
Summer Qinba2346e2019-11-12 16:26:31 +0800288
289 /*
290 * Clients must never overlap input parameters because of the risk of a
291 * double-fetch inconsistency.
292 * Overflow is checked in tfm_memory_check functions.
293 */
294 for (i = 0; i + 1 < in_num; i++) {
295 for (j = i+1; j < in_num; j++) {
TTornblom83d96372019-11-19 12:53:16 +0100296 if (!((char *) invecs[j].base + invecs[j].len <=
297 (char *) invecs[i].base ||
298 (char *) invecs[j].base >=
299 (char *) invecs[i].base + invecs[i].len)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800300 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qinba2346e2019-11-12 16:26:31 +0800301 }
302 }
303 }
304
David Hu733d8f92019-09-23 15:32:40 +0800305 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800306 * For client output vector, it is a PROGRAMMER ERROR if the provided
307 * payload memory reference was invalid or not read-write.
David Hu733d8f92019-09-23 15:32:40 +0800308 */
309 for (i = 0; i < out_num; i++) {
310 if (tfm_memory_check(outvecs[i].base, outvecs[i].len,
Ken Liubcae38b2021-01-20 15:47:44 +0800311 ns_caller, TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800312 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800313 }
314 }
315
Ken Liu0bed7e02022-02-10 12:38:07 +0800316 spm_fill_message(conn_handle, service, handle, type, client_id,
Summer Qin630c76b2020-05-20 10:32:58 +0800317 invecs, in_num, outvecs, out_num, outptr);
David Hu733d8f92019-09-23 15:32:40 +0800318
Ken Liu0bed7e02022-02-10 12:38:07 +0800319 return backend_instance.messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800320}
321
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800322/* Following PSA APIs are only needed by connection-based services */
323#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
324
325psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version)
326{
327 struct service_t *service;
Ken Liu0bed7e02022-02-10 12:38:07 +0800328 struct conn_handle_t *connect_handle;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800329 int32_t client_id;
330 psa_handle_t handle;
331 bool ns_caller = tfm_spm_is_ns_caller();
332 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
333
334 /*
335 * It is a PROGRAMMER ERROR if the RoT Service does not exist on the
336 * platform.
337 */
338 service = tfm_spm_get_service_by_sid(sid);
339 if (!service) {
340 return PSA_ERROR_CONNECTION_REFUSED;
341 }
342
343 /* It is a PROGRAMMER ERROR if connecting to a stateless service. */
344 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
345 return PSA_ERROR_PROGRAMMER_ERROR;
346 }
347
348 /*
349 * It is a PROGRAMMER ERROR if the caller is not authorized to access the
350 * RoT Service.
351 */
352 if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) {
353 return PSA_ERROR_CONNECTION_REFUSED;
354 }
355
356 /*
357 * It is a PROGRAMMER ERROR if the version of the RoT Service requested is
358 * not supported on the platform.
359 */
360 if (tfm_spm_check_client_version(service, version) != SPM_SUCCESS) {
361 return PSA_ERROR_CONNECTION_REFUSED;
362 }
363
364 client_id = tfm_spm_get_client_id(ns_caller);
365
366 /*
367 * Create connection handle here since it is possible to return the error
368 * code to client when creation fails.
369 */
370 CRITICAL_SECTION_ENTER(cs_assert);
371 connect_handle = tfm_spm_create_conn_handle(service, client_id);
372 CRITICAL_SECTION_LEAVE(cs_assert);
373 if (!connect_handle) {
374 return PSA_ERROR_CONNECTION_BUSY;
375 }
376
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800377 handle = tfm_spm_to_user_handle(connect_handle);
378 /* No input or output needed for connect message */
Ken Liu0bed7e02022-02-10 12:38:07 +0800379 spm_fill_message(connect_handle, service, handle, PSA_IPC_CONNECT,
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800380 client_id, NULL, 0, NULL, 0, NULL);
381
Ken Liu0bed7e02022-02-10 12:38:07 +0800382 return backend_instance.messaging(service, connect_handle);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800383}
384
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800385psa_status_t tfm_spm_client_psa_close(psa_handle_t handle)
David Hu733d8f92019-09-23 15:32:40 +0800386{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800387 struct service_t *service;
Ken Liu0bed7e02022-02-10 12:38:07 +0800388 struct conn_handle_t *conn_handle;
Summer Qin1ce712a2019-10-14 18:04:05 +0800389 int32_t client_id;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800390 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800391
392 /* It will have no effect if called with the NULL handle */
393 if (handle == PSA_NULL_HANDLE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800394 return PSA_SUCCESS;
David Hu733d8f92019-09-23 15:32:40 +0800395 }
396
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800397 /* It is a PROGRAMMER ERROR if called with a stateless handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800398 if (IS_STATIC_HANDLE(handle)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800399 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800400 }
401
Kevin Peng385fda82021-08-18 10:41:19 +0800402 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800403
Summer Qin373feb12020-03-27 15:35:33 +0800404 conn_handle = tfm_spm_to_handle_instance(handle);
David Hu733d8f92019-09-23 15:32:40 +0800405 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800406 * It is a PROGRAMMER ERROR if an invalid handle was provided that is not
407 * the null handle.
David Hu733d8f92019-09-23 15:32:40 +0800408 */
Ken Liubcae38b2021-01-20 15:47:44 +0800409 if (tfm_spm_validate_conn_handle(conn_handle, client_id) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800410 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qin1ce712a2019-10-14 18:04:05 +0800411 }
Shawn Shanb222d892021-01-04 17:41:48 +0800412
Ken Liu0bed7e02022-02-10 12:38:07 +0800413 service = conn_handle->service;
David Hu733d8f92019-09-23 15:32:40 +0800414 if (!service) {
415 /* FixMe: Need to implement one mechanism to resolve this failure. */
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800416 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800417 }
418
Shawn Shanb222d892021-01-04 17:41:48 +0800419 /*
420 * It is a PROGRAMMER ERROR if the connection is currently handling a
421 * request.
422 */
Summer Qin630c76b2020-05-20 10:32:58 +0800423 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800424 return PSA_ERROR_PROGRAMMER_ERROR;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800425 }
426
David Hu733d8f92019-09-23 15:32:40 +0800427 /* No input or output needed for close message */
Ken Liu0bed7e02022-02-10 12:38:07 +0800428 spm_fill_message(conn_handle, service, handle, PSA_IPC_DISCONNECT,
429 client_id, NULL, 0, NULL, 0, NULL);
David Hu733d8f92019-09-23 15:32:40 +0800430
Ken Liu0bed7e02022-02-10 12:38:07 +0800431 return backend_instance.messaging(service, conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800432}
Mingyang Sunb26b2802021-07-07 11:25:00 +0800433
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800434#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
435
Mingyang Suneeca4652021-07-15 15:19:16 +0800436/* PSA Partition API function body */
437
Mingyang Sunb26b2802021-07-07 11:25:00 +0800438psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask,
439 uint32_t timeout)
440{
441 struct partition_t *partition = NULL;
442
443 /*
444 * Timeout[30:0] are reserved for future use.
445 * SPM must ignore the value of RES.
446 */
447 timeout &= PSA_TIMEOUT_MASK;
448
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800449 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800450
451 /*
452 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
453 * signals.
454 */
455 if ((partition->signals_allowed & signal_mask) == 0) {
456 tfm_core_panic();
457 }
458
459 /*
Ken Liu5d73c872021-08-19 19:23:17 +0800460 * thrd_wait_on() blocks the caller thread if no signals are available.
Mingyang Sunb26b2802021-07-07 11:25:00 +0800461 * In this case, the return value of this function is temporary set into
462 * runtime context. After new signal(s) are available, the return value
463 * is updated with the available signal(s) and blocked thread gets to run.
464 */
465 if (timeout == PSA_BLOCK &&
466 (partition->signals_asserted & signal_mask) == 0) {
467 partition->signals_waiting = signal_mask;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800468 thrd_wait_on(&partition->waitobj, CURRENT_THREAD);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800469 }
470
471 return partition->signals_asserted & signal_mask;
472}
473
Mingyang Sun4609ac72022-02-08 18:56:35 +0800474/* This API is only used in IPC backend. */
475#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{
Ken Liu0bed7e02022-02-10 12:38:07 +0800478 struct conn_handle_t *tmp_msg = 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,
499 privileged) != SPM_SUCCESS) {
500 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 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800523 tmp_msg = spm_get_handle_by_signal(partition, signal);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800524 if (!tmp_msg) {
525 return PSA_ERROR_DOES_NOT_EXIST;
526 }
527
Ken Liu0bed7e02022-02-10 12:38:07 +0800528 tmp_msg->status = TFM_HANDLE_STATUS_ACTIVE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800529
530 spm_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
531
532 return PSA_SUCCESS;
533}
Mingyang Sun4609ac72022-02-08 18:56:35 +0800534#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800535
Mingyang Sunb26b2802021-07-07 11:25:00 +0800536size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
537 void *buffer, size_t num_bytes)
538{
539 size_t bytes;
Ken Liu0bed7e02022-02-10 12:38:07 +0800540 struct conn_handle_t *msg = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800541 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800542
543 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +0800544 msg = spm_get_handle_by_user_handle(msg_handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800545 if (!msg) {
546 tfm_core_panic();
547 }
548
Kevin Penga40d29f2022-01-19 14:44:34 +0800549 priv_mode = GET_PARTITION_PRIVILEGED_MODE(msg->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800550
551 /*
552 * It is a fatal error if message handle does not refer to a request
553 * message
554 */
555 if (msg->msg.type < PSA_IPC_CALL) {
556 tfm_core_panic();
557 }
558
559 /*
560 * It is a fatal error if invec_idx is equal to or greater than
561 * PSA_MAX_IOVEC
562 */
563 if (invec_idx >= PSA_MAX_IOVEC) {
564 tfm_core_panic();
565 }
566
567 /* There was no remaining data in this input vector */
568 if (msg->msg.in_size[invec_idx] == 0) {
569 return 0;
570 }
571
Shawn Shan038348e2021-09-08 17:11:04 +0800572#if PSA_FRAMEWORK_HAS_MM_IOVEC
573 /*
574 * It is a fatal error if the input vector has already been mapped using
575 * psa_map_invec().
576 */
577 if (IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
578 tfm_core_panic();
579 }
580
581 SET_IOVEC_ACCESSED(msg, (invec_idx + INVEC_IDX_BASE));
582#endif
583
Mingyang Sunb26b2802021-07-07 11:25:00 +0800584 /*
585 * Copy the client data to the service buffer. It is a fatal error
586 * if the memory reference for buffer is invalid or not read-write.
587 */
588 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800589 TFM_MEMORY_ACCESS_RW, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800590 tfm_core_panic();
591 }
592
593 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
594 msg->msg.in_size[invec_idx] : num_bytes;
595
596 spm_memcpy(buffer, msg->invec[invec_idx].base, bytes);
597
598 /* There maybe some remaining data */
599 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
600 msg->msg.in_size[invec_idx] -= bytes;
601
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{
Ken Liu0bed7e02022-02-10 12:38:07 +0800608 struct conn_handle_t *msg = NULL;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800609
610 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +0800611 msg = spm_get_handle_by_user_handle(msg_handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800612 if (!msg) {
613 tfm_core_panic();
614 }
615
616 /*
617 * It is a fatal error if message handle does not refer to a request
618 * message
619 */
620 if (msg->msg.type < PSA_IPC_CALL) {
621 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
632 /* There was no remaining data in this input vector */
633 if (msg->msg.in_size[invec_idx] == 0) {
634 return 0;
635 }
636
Shawn Shan038348e2021-09-08 17:11:04 +0800637#if PSA_FRAMEWORK_HAS_MM_IOVEC
638 /*
639 * It is a fatal error if the input vector has already been mapped using
640 * psa_map_invec().
641 */
642 if (IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
643 tfm_core_panic();
644 }
645
646 SET_IOVEC_ACCESSED(msg, (invec_idx + INVEC_IDX_BASE));
647#endif
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 */
653 if (num_bytes > msg->msg.in_size[invec_idx]) {
654 num_bytes = msg->msg.in_size[invec_idx];
655 }
656
657 /* There maybe some remaining data */
658 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
659 num_bytes;
660 msg->msg.in_size[invec_idx] -= num_bytes;
661
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{
Ken Liu0bed7e02022-02-10 12:38:07 +0800668 struct conn_handle_t *msg = 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 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800672 msg = spm_get_handle_by_user_handle(msg_handle);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800673 if (!msg) {
674 tfm_core_panic();
675 }
676
Kevin Penga40d29f2022-01-19 14:44:34 +0800677 priv_mode = GET_PARTITION_PRIVILEGED_MODE(msg->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800678
679 /*
680 * It is a fatal error if message handle does not refer to a request
681 * message
682 */
683 if (msg->msg.type < PSA_IPC_CALL) {
684 tfm_core_panic();
685 }
686
687 /*
688 * It is a fatal error if outvec_idx is equal to or greater than
689 * PSA_MAX_IOVEC
690 */
691 if (outvec_idx >= PSA_MAX_IOVEC) {
692 tfm_core_panic();
693 }
694
695 /*
696 * It is a fatal error if the call attempts to write data past the end of
697 * the client output vector
698 */
699 if (num_bytes > msg->msg.out_size[outvec_idx] -
700 msg->outvec[outvec_idx].len) {
701 tfm_core_panic();
702 }
703
Shawn Shan038348e2021-09-08 17:11:04 +0800704#if PSA_FRAMEWORK_HAS_MM_IOVEC
705 /*
706 * It is a fatal error if the output vector has already been mapped using
707 * psa_map_outvec().
708 */
709 if (IOVEC_IS_MAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
710 tfm_core_panic();
711 }
712
713 SET_IOVEC_ACCESSED(msg, (outvec_idx + OUTVEC_IDX_BASE));
714#endif
715
Mingyang Sunb26b2802021-07-07 11:25:00 +0800716 /*
717 * Copy the service buffer to client outvecs. It is a fatal error
718 * if the memory reference for buffer is invalid or not readable.
719 */
720 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800721 TFM_MEMORY_ACCESS_RO, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800722 tfm_core_panic();
723 }
724
725 spm_memcpy((char *)msg->outvec[outvec_idx].base +
726 msg->outvec[outvec_idx].len, buffer, num_bytes);
727
728 /* Update the write number */
729 msg->outvec[outvec_idx].len += num_bytes;
730}
731
Ken Liuf8c7e532022-02-10 15:03:04 +0800732psa_status_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle,
733 psa_status_t status)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800734{
Ken Liu0bed7e02022-02-10 12:38:07 +0800735 struct service_t *service;
736 struct conn_handle_t *hdl;
Ken Liuf8c7e532022-02-10 15:03:04 +0800737 psa_status_t ret = PSA_SUCCESS;
Mingyang Sun620c8562021-11-10 11:44:58 +0800738 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800739
740 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +0800741 hdl = spm_get_handle_by_user_handle(msg_handle);
742 if (!hdl) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800743 tfm_core_panic();
744 }
745
746 /*
747 * RoT Service information is needed in this function, stored it in message
748 * body structure. Only two parameters are passed in this function: handle
749 * and status, so it is useful and simply to do like this.
750 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800751 service = hdl->service;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800752 if (!service) {
753 tfm_core_panic();
754 }
755
Ken Liu0bed7e02022-02-10 12:38:07 +0800756 switch (hdl->msg.type) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800757 case PSA_IPC_CONNECT:
758 /*
759 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
760 * input status is PSA_SUCCESS. Others return values are based on the
761 * input status.
762 */
763 if (status == PSA_SUCCESS) {
764 ret = msg_handle;
765 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
766 /* Refuse the client connection, indicating a permanent error. */
Ken Liu0bed7e02022-02-10 12:38:07 +0800767 tfm_spm_free_conn_handle(service, hdl);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800768 ret = PSA_ERROR_CONNECTION_REFUSED;
769 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
770 /* Fail the client connection, indicating a transient error. */
771 ret = PSA_ERROR_CONNECTION_BUSY;
772 } else {
773 tfm_core_panic();
774 }
775 break;
776 case PSA_IPC_DISCONNECT:
777 /* Service handle is not used anymore */
Ken Liu0bed7e02022-02-10 12:38:07 +0800778 tfm_spm_free_conn_handle(service, hdl);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800779
780 /*
781 * If the message type is PSA_IPC_DISCONNECT, then the status code is
782 * ignored
783 */
784 break;
785 default:
Ken Liu0bed7e02022-02-10 12:38:07 +0800786 if (hdl->msg.type >= PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +0800787
788#if PSA_FRAMEWORK_HAS_MM_IOVEC
789
790 /*
791 * If the unmapped function is not called for an input/output vector
792 * that has been mapped, the framework will remove the mapping.
793 */
794 int i;
795
796 for (i = 0; i < PSA_MAX_IOVEC * 2; i++) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800797 if (IOVEC_IS_MAPPED(hdl, i) && (!IOVEC_IS_UNMAPPED(hdl, i))) {
798 SET_IOVEC_UNMAPPED(hdl, i);
Shawn Shan038348e2021-09-08 17:11:04 +0800799 /*
800 * Any output vectors that are still mapped will report that
801 * zero bytes have been written.
802 */
803 if (i >= OUTVEC_IDX_BASE) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800804 hdl->outvec[i - OUTVEC_IDX_BASE].len = 0;
Shawn Shan038348e2021-09-08 17:11:04 +0800805 }
806 }
807 }
808
809#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800810 /* Reply to a request message. Return values are based on status */
811 ret = status;
812 /*
813 * The total number of bytes written to a single parameter must be
814 * reported to the client by updating the len member of the
815 * psa_outvec structure for the parameter before returning from
816 * psa_call().
817 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800818 update_caller_outvec_len(hdl);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800819 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800820 tfm_spm_free_conn_handle(service, hdl);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800821 }
822 } else {
823 tfm_core_panic();
824 }
825 }
826
827 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
828 /*
829 * If the source of the programmer error is a Secure Partition, the SPM
830 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
831 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800832 if (TFM_CLIENT_ID_IS_NS(hdl->msg.client_id)) {
833 hdl->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800834 } else {
835 tfm_core_panic();
836 }
837 } else {
Ken Liu0bed7e02022-02-10 12:38:07 +0800838 hdl->status = TFM_HANDLE_STATUS_IDLE;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800839 }
840
Mingyang Sun620c8562021-11-10 11:44:58 +0800841 /*
842 * TODO: It can be optimized further by moving critical section protection
843 * to mailbox. Also need to check implementation when secure context is
844 * involved.
845 */
846 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu0bed7e02022-02-10 12:38:07 +0800847 ret = backend_instance.replying(hdl, ret);
Mingyang Sun620c8562021-11-10 11:44:58 +0800848 CRITICAL_SECTION_LEAVE(cs_assert);
849
850 return ret;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800851}
852
853void tfm_spm_partition_psa_notify(int32_t partition_id)
854{
Ken Liu5d73c872021-08-19 19:23:17 +0800855 struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id);
856
857 spm_assert_signal(p_pt, PSA_DOORBELL);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800858}
859
860void tfm_spm_partition_psa_clear(void)
861{
Ken Liu92ede9f2021-10-20 09:35:00 +0800862 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800863 struct partition_t *partition = NULL;
864
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800865 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800866
867 /*
868 * It is a fatal error if the Secure Partition's doorbell signal is not
869 * currently asserted.
870 */
871 if ((partition->signals_asserted & PSA_DOORBELL) == 0) {
872 tfm_core_panic();
873 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800874
875 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800876 partition->signals_asserted &= ~PSA_DOORBELL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800877 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800878}
879
Mingyang Sunb26b2802021-07-07 11:25:00 +0800880void tfm_spm_partition_psa_panic(void)
881{
882 /*
883 * PSA FF recommends that the SPM causes the system to restart when a secure
884 * partition panics.
885 */
886 tfm_hal_system_reset();
887}
888
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800889/* psa_set_rhandle is only needed by connection-based services */
890#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
891
892void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle)
893{
894 struct conn_handle_t *hdl;
895
896 /* It is a fatal error if message handle is invalid */
897 hdl = spm_get_handle_by_user_handle(msg_handle);
898 if (!hdl) {
899 tfm_core_panic();
900 }
901
902 /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */
903 if (SERVICE_IS_STATELESS(hdl->service->p_ldinf->flags)) {
904 tfm_core_panic();
905 }
906
907 hdl->msg.rhandle = rhandle;
908 hdl->rhandle = rhandle;
909}
910
911#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
912
913#if CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1
Kevin Peng67a89fd2021-11-25 11:22:02 +0800914void tfm_spm_partition_psa_irq_enable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800915{
916 struct partition_t *partition;
917 struct irq_load_info_t *irq_info;
918
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800919 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800920
921 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
922 if (!irq_info) {
923 tfm_core_panic();
924 }
925
Kevin Pengd399a1f2021-09-08 15:33:14 +0800926 tfm_hal_irq_enable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800927}
928
Kevin Peng67a89fd2021-11-25 11:22:02 +0800929psa_irq_status_t tfm_spm_partition_psa_irq_disable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800930{
931 struct partition_t *partition;
932 struct irq_load_info_t *irq_info;
933
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800934 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800935
936 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
937 if (!irq_info) {
938 tfm_core_panic();
939 }
940
Kevin Pengd399a1f2021-09-08 15:33:14 +0800941 tfm_hal_irq_disable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800942
943 return 1;
944}
945
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800946/* This API is only used for FLIH. */
947#if CONFIG_TFM_FLIH_API == 1
Mingyang Sunb26b2802021-07-07 11:25:00 +0800948void tfm_spm_partition_psa_reset_signal(psa_signal_t irq_signal)
949{
Ken Liu92ede9f2021-10-20 09:35:00 +0800950 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800951 struct irq_load_info_t *irq_info;
952 struct partition_t *partition;
953
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800954 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800955
956 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
957 if (!irq_info) {
958 tfm_core_panic();
959 }
960
961 if (!irq_info->flih_func) {
962 /* This API is for FLIH IRQs only */
963 tfm_core_panic();
964 }
965
966 if ((partition->signals_asserted & irq_signal) == 0) {
967 /* The signal is not asserted */
968 tfm_core_panic();
969 }
970
Ken Liu92ede9f2021-10-20 09:35:00 +0800971 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800972 partition->signals_asserted &= ~irq_signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800973 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800974}
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800975#endif
Shawn Shan038348e2021-09-08 17:11:04 +0800976
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800977/* This API is only used for SLIH. */
978#if CONFIG_TFM_SLIH_API == 1
979void tfm_spm_partition_psa_eoi(psa_signal_t irq_signal)
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800980{
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800981 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
982 struct irq_load_info_t *irq_info = NULL;
983 struct partition_t *partition = NULL;
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800984
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800985 partition = GET_CURRENT_COMPONENT();
986
987 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
988 /* It is a fatal error if passed signal is not an interrupt signal. */
989 if (!irq_info) {
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800990 tfm_core_panic();
991 }
992
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800993 if (irq_info->flih_func) {
994 /* This API is for SLIH IRQs only */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800995 tfm_core_panic();
996 }
997
Mingyang Suned5fe7b2022-02-10 17:33:21 +0800998 /* It is a fatal error if passed signal is not currently asserted */
999 if ((partition->signals_asserted & irq_signal) == 0) {
1000 tfm_core_panic();
1001 }
1002
1003 CRITICAL_SECTION_ENTER(cs_assert);
1004 partition->signals_asserted &= ~irq_signal;
1005 CRITICAL_SECTION_LEAVE(cs_assert);
1006
1007 tfm_hal_irq_clear_pending(irq_info->source);
1008 tfm_hal_irq_enable(irq_info->source);
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001009}
Mingyang Suned5fe7b2022-02-10 17:33:21 +08001010#endif
1011#endif /* CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1 */
Xinyu Zhang2bc4d572021-12-27 16:37:46 +08001012
Shawn Shan038348e2021-09-08 17:11:04 +08001013#if PSA_FRAMEWORK_HAS_MM_IOVEC
1014
1015const void *tfm_spm_partition_psa_map_invec(psa_handle_t msg_handle,
1016 uint32_t invec_idx)
1017{
Ken Liu0bed7e02022-02-10 12:38:07 +08001018 struct conn_handle_t *hdl;
Shawn Shan038348e2021-09-08 17:11:04 +08001019 uint32_t privileged;
1020 struct partition_t *partition = NULL;
1021
1022 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +08001023 hdl = spm_get_handle_by_user_handle(msg_handle);
1024 if (!hdl) {
Shawn Shan038348e2021-09-08 17:11:04 +08001025 tfm_core_panic();
1026 }
1027
Ken Liu0bed7e02022-02-10 12:38:07 +08001028 partition = hdl->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001029 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001030
1031 /*
1032 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1033 * Service that received the message.
1034 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001035 if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001036 tfm_core_panic();
1037 }
1038
1039 /*
1040 * It is a fatal error if message handle does not refer to a request
1041 * message.
1042 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001043 if (hdl->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001044 tfm_core_panic();
1045 }
1046
1047 /*
1048 * It is a fatal error if invec_idx is equal to or greater than
1049 * PSA_MAX_IOVEC.
1050 */
1051 if (invec_idx >= PSA_MAX_IOVEC) {
1052 tfm_core_panic();
1053 }
1054
1055 /* It is a fatal error if the input vector has length zero. */
Ken Liu0bed7e02022-02-10 12:38:07 +08001056 if (hdl->msg.in_size[invec_idx] == 0) {
Shawn Shan038348e2021-09-08 17:11:04 +08001057 tfm_core_panic();
1058 }
1059
1060 /*
1061 * It is a fatal error if the input vector has already been mapped using
1062 * psa_map_invec().
1063 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001064 if (IOVEC_IS_MAPPED(hdl, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001065 tfm_core_panic();
1066 }
1067
1068 /*
1069 * It is a fatal error if the input vector has already been accessed
1070 * using psa_read() or psa_skip().
1071 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001072 if (IOVEC_IS_ACCESSED(hdl, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001073 tfm_core_panic();
1074 }
1075
1076 /*
1077 * It is a fatal error if the memory reference for the wrap input vector is
1078 * invalid or not readable.
1079 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001080 if (tfm_memory_check(hdl->invec[invec_idx].base, hdl->invec[invec_idx].len,
Shawn Shan038348e2021-09-08 17:11:04 +08001081 false, TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
1082 tfm_core_panic();
1083 }
1084
Ken Liu0bed7e02022-02-10 12:38:07 +08001085 SET_IOVEC_MAPPED(hdl, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001086
Ken Liu0bed7e02022-02-10 12:38:07 +08001087 return hdl->invec[invec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001088}
1089
1090void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle,
1091 uint32_t invec_idx)
1092{
Ken Liu0bed7e02022-02-10 12:38:07 +08001093 struct conn_handle_t *hdl;
Shawn Shan038348e2021-09-08 17:11:04 +08001094
1095 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +08001096 hdl = spm_get_handle_by_user_handle(msg_handle);
1097 if (!hdl) {
Shawn Shan038348e2021-09-08 17:11:04 +08001098 tfm_core_panic();
1099 }
1100
1101 /*
1102 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1103 * Service that received the message.
1104 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001105 if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001106 tfm_core_panic();
1107 }
1108
1109 /*
1110 * It is a fatal error if message handle does not refer to a request
1111 * message.
1112 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001113 if (hdl->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001114 tfm_core_panic();
1115 }
1116
1117 /*
1118 * It is a fatal error if invec_idx is equal to or greater than
1119 * PSA_MAX_IOVEC.
1120 */
1121 if (invec_idx >= PSA_MAX_IOVEC) {
1122 tfm_core_panic();
1123 }
1124
1125 /*
1126 * It is a fatal error if The input vector has not been mapped by a call to
1127 * psa_map_invec().
1128 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001129 if (!IOVEC_IS_MAPPED(hdl, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001130 tfm_core_panic();
1131 }
1132
1133 /*
1134 * It is a fatal error if the input vector has already been unmapped by a
1135 * call to psa_unmap_invec().
1136 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001137 if (IOVEC_IS_UNMAPPED(hdl, (invec_idx + INVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001138 tfm_core_panic();
1139 }
1140
Ken Liu0bed7e02022-02-10 12:38:07 +08001141 SET_IOVEC_UNMAPPED(hdl, (invec_idx + INVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001142}
1143
1144void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle,
1145 uint32_t outvec_idx)
1146{
Ken Liu0bed7e02022-02-10 12:38:07 +08001147 struct conn_handle_t *hdl;
Shawn Shan038348e2021-09-08 17:11:04 +08001148 uint32_t privileged;
1149 struct partition_t *partition = NULL;
1150
1151 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +08001152 hdl = spm_get_handle_by_user_handle(msg_handle);
1153 if (!hdl) {
Shawn Shan038348e2021-09-08 17:11:04 +08001154 tfm_core_panic();
1155 }
1156
Ken Liu0bed7e02022-02-10 12:38:07 +08001157 partition = hdl->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001158 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001159
1160 /*
1161 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1162 * Service that received the message.
1163 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001164 if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001165 tfm_core_panic();
1166 }
1167
1168 /*
1169 * It is a fatal error if message handle does not refer to a request
1170 * message.
1171 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001172 if (hdl->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001173 tfm_core_panic();
1174 }
1175
1176 /*
1177 * It is a fatal error if outvec_idx is equal to or greater than
1178 * PSA_MAX_IOVEC.
1179 */
1180 if (outvec_idx >= PSA_MAX_IOVEC) {
1181 tfm_core_panic();
1182 }
1183
1184 /* It is a fatal error if the output vector has length zero. */
Ken Liu0bed7e02022-02-10 12:38:07 +08001185 if (hdl->msg.out_size[outvec_idx] == 0) {
Shawn Shan038348e2021-09-08 17:11:04 +08001186 tfm_core_panic();
1187 }
1188
1189 /*
1190 * It is a fatal error if the output vector has already been mapped using
1191 * psa_map_outvec().
1192 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001193 if (IOVEC_IS_MAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001194 tfm_core_panic();
1195 }
1196
1197 /*
1198 * It is a fatal error if the output vector has already been accessed
1199 * using psa_write().
1200 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001201 if (IOVEC_IS_ACCESSED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001202 tfm_core_panic();
1203 }
1204
1205 /*
1206 * It is a fatal error if the output vector is invalid or not read-write.
1207 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001208 if (tfm_memory_check(hdl->outvec[outvec_idx].base,
1209 hdl->outvec[outvec_idx].len, false,
Shawn Shan038348e2021-09-08 17:11:04 +08001210 TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
1211 tfm_core_panic();
1212 }
Ken Liu0bed7e02022-02-10 12:38:07 +08001213 SET_IOVEC_MAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001214
Ken Liu0bed7e02022-02-10 12:38:07 +08001215 return hdl->outvec[outvec_idx].base;
Shawn Shan038348e2021-09-08 17:11:04 +08001216}
1217
1218void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle,
1219 uint32_t outvec_idx, size_t len)
1220{
Ken Liu0bed7e02022-02-10 12:38:07 +08001221 struct conn_handle_t *hdl;
Shawn Shan038348e2021-09-08 17:11:04 +08001222
1223 /* It is a fatal error if message handle is invalid */
Ken Liu0bed7e02022-02-10 12:38:07 +08001224 hdl = spm_get_handle_by_user_handle(msg_handle);
1225 if (!hdl) {
Shawn Shan038348e2021-09-08 17:11:04 +08001226 tfm_core_panic();
1227 }
1228
1229 /*
1230 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1231 * Service that received the message.
1232 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001233 if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
Shawn Shan038348e2021-09-08 17:11:04 +08001234 tfm_core_panic();
1235 }
1236
1237 /*
1238 * It is a fatal error if message handle does not refer to a request
1239 * message.
1240 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001241 if (hdl->msg.type < PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +08001242 tfm_core_panic();
1243 }
1244
1245 /*
1246 * It is a fatal error if outvec_idx is equal to or greater than
1247 * PSA_MAX_IOVEC.
1248 */
1249 if (outvec_idx >= PSA_MAX_IOVEC) {
1250 tfm_core_panic();
1251 }
1252
1253 /*
1254 * It is a fatal error if len is greater than the output vector size.
1255 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001256 if (len > hdl->msg.out_size[outvec_idx]) {
Shawn Shan038348e2021-09-08 17:11:04 +08001257 tfm_core_panic();
1258 }
1259
1260 /*
1261 * It is a fatal error if The output vector has not been mapped by a call to
1262 * psa_map_outvec().
1263 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001264 if (!IOVEC_IS_MAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001265 tfm_core_panic();
1266 }
1267
1268 /*
1269 * It is a fatal error if the output vector has already been unmapped by a
1270 * call to psa_unmap_outvec().
1271 */
Ken Liu0bed7e02022-02-10 12:38:07 +08001272 if (IOVEC_IS_UNMAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
Shawn Shan038348e2021-09-08 17:11:04 +08001273 tfm_core_panic();
1274 }
1275
Ken Liu0bed7e02022-02-10 12:38:07 +08001276 SET_IOVEC_UNMAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE));
Shawn Shan038348e2021-09-08 17:11:04 +08001277
1278 /* Update the write number */
Ken Liu0bed7e02022-02-10 12:38:07 +08001279 hdl->outvec[outvec_idx].len = len;
Shawn Shan038348e2021-09-08 17:11:04 +08001280}
1281
1282#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */