blob: ff2e3b07be79fed0ee89ec09c713c877921aba14 [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"
Ken Liu92ede9f2021-10-20 09:35:00 +080010#include "critical_section.h"
Mingyang Suneeca4652021-07-15 15:19:16 +080011#include "psa/lifecycle.h"
David Hu733d8f92019-09-23 15:32:40 +080012#include "psa/service.h"
Kevin Peng3f67b2e2021-10-18 17:47:27 +080013#include "interrupt.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080014#include "spm_ipc.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080015#include "tfm_arch.h"
David Hu733d8f92019-09-23 15:32:40 +080016#include "tfm_core_utils.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080017#include "load/partition_defs.h"
Mingyang Sun2b352662021-04-21 11:35:43 +080018#include "load/service_defs.h"
Ken Liu3dd92562021-08-17 16:22:54 +080019#include "load/interrupt_defs.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080020#include "ffm/psa_api.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080021#include "utilities.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080022#include "ffm/backend.h"
Ken Liue07c3b72021-10-14 16:19:13 +080023#include "ffm/psa_api.h"
Ken Liubcae38b2021-01-20 15:47:44 +080024#include "ffm/spm_error_base.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080025#include "tfm_rpc.h"
26#include "tfm_spm_hal.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080027#include "tfm_hal_interrupt.h"
Mingyang Sunb26b2802021-07-07 11:25:00 +080028#include "tfm_hal_platform.h"
Ken Liu82e3eac2021-10-14 16:19:13 +080029#include "tfm_psa_call_pack.h"
David Hu733d8f92019-09-23 15:32:40 +080030
Ken Liub3b2cb62021-05-22 00:39:28 +080031#define GET_STATELESS_SERVICE(index) (stateless_services_ref_tbl[index])
Xinyu Zhanga38e9b52021-06-02 17:48:01 +080032extern struct service_t *stateless_services_ref_tbl[];
Mingyang Suncb6f70e2021-03-05 23:30:25 +080033
Shawn Shan038348e2021-09-08 17:11:04 +080034#if PSA_FRAMEWORK_HAS_MM_IOVEC
35
36/*
37 * The MM-IOVEC status
38 * The max total number of invec and outvec is 8.
39 * Each invec/outvec takes 4 bit, 32 bits in total.
40 *
41 * The encoding format of the MM-IOVEC status:
42 *--------------------------------------------------------------
43 *| Bit | 31 - 28 | 27 - 24 | ... | 7 - 4 | 3 - 0 |
44 *--------------------------------------------------------------
45 *| Vector | outvec[3] | outvec[2] | ... | invec[1] | invec[0] |
46 *--------------------------------------------------------------
47 *
48 * Take invec[0] as an example:
49 *
50 * bit 0: whether invec[0] has been mapped.
51 * bit 1: whether invec[0] has been unmapped.
52 * bit 2: whether invec[0] has been accessed using psa_read(), psa_skip() or
53 * psa_write().
54 * bit 3: reserved for invec[0].
55 */
56
57#define IOVEC_STATUS_BITS 4 /* Each vector occupies 4 bits. */
58#define OUTVEC_IDX_BASE 4 /*
59 * Base index of outvec.
60 * There are four invecs in front of
61 * outvec.
62 */
63#define INVEC_IDX_BASE 0 /* Base index of invec. */
64
65#define IOVEC_MAPPED_BIT (1U << 0)
66#define IOVEC_UNMAPPED_BIT (1U << 1)
67#define IOVEC_ACCESSED_BIT (1U << 2)
68
69#define IOVEC_IS_MAPPED(msg, iovec_idx) \
70 ((((msg)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
71 IOVEC_MAPPED_BIT)
72#define IOVEC_IS_UNMAPPED(msg, iovec_idx) \
73 ((((msg)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
74 IOVEC_UNMAPPED_BIT)
75#define IOVEC_IS_ACCESSED(msg, iovec_idx) \
76 ((((msg)->iovec_status) >> ((iovec_idx) * IOVEC_STATUS_BITS)) & \
77 IOVEC_ACCESSED_BIT)
78#define SET_IOVEC_MAPPED(msg, iovec_idx) \
79 (((msg)->iovec_status) |= (IOVEC_MAPPED_BIT << \
80 ((iovec_idx) * IOVEC_STATUS_BITS)))
81#define SET_IOVEC_UNMAPPED(msg, iovec_idx) \
82 (((msg)->iovec_status) |= (IOVEC_UNMAPPED_BIT << \
83 ((iovec_idx) * IOVEC_STATUS_BITS)))
84#define SET_IOVEC_ACCESSED(msg, iovec_idx) \
85 (((msg)->iovec_status) |= (IOVEC_ACCESSED_BIT << \
86 ((iovec_idx) * IOVEC_STATUS_BITS)))
87
88#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */
89
Xinyu Zhangb287ef82021-11-03 18:38:50 +080090void spm_handle_programmer_errors(psa_status_t status)
91{
92 if (status == PSA_ERROR_PROGRAMMER_ERROR ||
93 status == PSA_ERROR_CONNECTION_REFUSED ||
94 status == PSA_ERROR_CONNECTION_BUSY) {
95 if (!tfm_spm_is_ns_caller()) {
96 tfm_core_panic();
97 }
98 }
99}
100
Mingyang Suneeca4652021-07-15 15:19:16 +0800101uint32_t tfm_spm_get_lifecycle_state(void)
102{
103 /*
104 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
105 * implemented in the future.
106 */
107 return PSA_LIFECYCLE_UNKNOWN;
108}
109
110/* PSA Client API function body */
111
Mingyang Sund44522a2020-01-16 16:48:37 +0800112uint32_t tfm_spm_client_psa_framework_version(void)
David Hu733d8f92019-09-23 15:32:40 +0800113{
114 return PSA_FRAMEWORK_VERSION;
115}
116
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800117uint32_t tfm_spm_client_psa_version(uint32_t sid)
David Hu733d8f92019-09-23 15:32:40 +0800118{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800119 struct service_t *service;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800120 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800121
122 /*
123 * It should return PSA_VERSION_NONE if the RoT Service is not
124 * implemented.
125 */
126 service = tfm_spm_get_service_by_sid(sid);
127 if (!service) {
128 return PSA_VERSION_NONE;
129 }
130
131 /*
Shawn Shan2365c902019-12-19 18:35:36 +0800132 * It should return PSA_VERSION_NONE if the caller is not authorized
133 * to access the RoT Service.
David Hu733d8f92019-09-23 15:32:40 +0800134 */
Ken Liubcae38b2021-01-20 15:47:44 +0800135 if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) {
Shawn Shan2365c902019-12-19 18:35:36 +0800136 return PSA_VERSION_NONE;
David Hu733d8f92019-09-23 15:32:40 +0800137 }
138
Ken Liuacd2a572021-05-12 16:19:04 +0800139 return service->p_ldinf->version;
David Hu733d8f92019-09-23 15:32:40 +0800140}
141
Mingyang Suneeca4652021-07-15 15:19:16 +0800142psa_status_t tfm_spm_client_psa_call(psa_handle_t handle,
143 uint32_t ctrl_param,
144 const psa_invec *inptr,
145 psa_outvec *outptr)
David Hu733d8f92019-09-23 15:32:40 +0800146{
147 psa_invec invecs[PSA_MAX_IOVEC];
148 psa_outvec outvecs[PSA_MAX_IOVEC];
Summer Qin630c76b2020-05-20 10:32:58 +0800149 struct tfm_conn_handle_t *conn_handle;
Mingyang Sun783a59b2021-04-20 15:52:18 +0800150 struct service_t *service;
David Hu733d8f92019-09-23 15:32:40 +0800151 struct tfm_msg_body_t *msg;
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 Liuf39d8eb2021-10-07 12:55:33 +0800241 service = conn_handle->internal_msg.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
316 /*
317 * FixMe: Need to check if the message is unrecognized by the RoT
318 * Service or incorrectly formatted.
319 */
Kevin Pengdf6aa292021-03-11 17:58:50 +0800320 msg = tfm_spm_get_msg_buffer_from_conn_handle(conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800321
Ken Liu505b1702020-05-29 13:19:58 +0800322 tfm_spm_fill_msg(msg, service, handle, type, client_id,
Summer Qin630c76b2020-05-20 10:32:58 +0800323 invecs, in_num, outvecs, out_num, outptr);
David Hu733d8f92019-09-23 15:32:40 +0800324
Mingyang Sundeae45d2021-09-06 15:31:07 +0800325 return backend_instance.messaging(service, msg);
David Hu733d8f92019-09-23 15:32:40 +0800326}
327
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800328/* Following PSA APIs are only needed by connection-based services */
329#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
330
331psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version)
332{
333 struct service_t *service;
334 struct tfm_msg_body_t *msg;
335 struct tfm_conn_handle_t *connect_handle;
336 int32_t client_id;
337 psa_handle_t handle;
338 bool ns_caller = tfm_spm_is_ns_caller();
339 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
340
341 /*
342 * It is a PROGRAMMER ERROR if the RoT Service does not exist on the
343 * platform.
344 */
345 service = tfm_spm_get_service_by_sid(sid);
346 if (!service) {
347 return PSA_ERROR_CONNECTION_REFUSED;
348 }
349
350 /* It is a PROGRAMMER ERROR if connecting to a stateless service. */
351 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
352 return PSA_ERROR_PROGRAMMER_ERROR;
353 }
354
355 /*
356 * It is a PROGRAMMER ERROR if the caller is not authorized to access the
357 * RoT Service.
358 */
359 if (tfm_spm_check_authorization(sid, service, ns_caller) != SPM_SUCCESS) {
360 return PSA_ERROR_CONNECTION_REFUSED;
361 }
362
363 /*
364 * It is a PROGRAMMER ERROR if the version of the RoT Service requested is
365 * not supported on the platform.
366 */
367 if (tfm_spm_check_client_version(service, version) != SPM_SUCCESS) {
368 return PSA_ERROR_CONNECTION_REFUSED;
369 }
370
371 client_id = tfm_spm_get_client_id(ns_caller);
372
373 /*
374 * Create connection handle here since it is possible to return the error
375 * code to client when creation fails.
376 */
377 CRITICAL_SECTION_ENTER(cs_assert);
378 connect_handle = tfm_spm_create_conn_handle(service, client_id);
379 CRITICAL_SECTION_LEAVE(cs_assert);
380 if (!connect_handle) {
381 return PSA_ERROR_CONNECTION_BUSY;
382 }
383
384 msg = tfm_spm_get_msg_buffer_from_conn_handle(connect_handle);
385
386 handle = tfm_spm_to_user_handle(connect_handle);
387 /* No input or output needed for connect message */
388 tfm_spm_fill_msg(msg, service, handle, PSA_IPC_CONNECT,
389 client_id, NULL, 0, NULL, 0, NULL);
390
391 return backend_instance.messaging(service, msg);
392}
393
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800394psa_status_t tfm_spm_client_psa_close(psa_handle_t handle)
David Hu733d8f92019-09-23 15:32:40 +0800395{
Mingyang Sun783a59b2021-04-20 15:52:18 +0800396 struct service_t *service;
David Hu733d8f92019-09-23 15:32:40 +0800397 struct tfm_msg_body_t *msg;
Summer Qin630c76b2020-05-20 10:32:58 +0800398 struct tfm_conn_handle_t *conn_handle;
Summer Qin1ce712a2019-10-14 18:04:05 +0800399 int32_t client_id;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800400 bool ns_caller = tfm_spm_is_ns_caller();
David Hu733d8f92019-09-23 15:32:40 +0800401
402 /* It will have no effect if called with the NULL handle */
403 if (handle == PSA_NULL_HANDLE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800404 return PSA_SUCCESS;
David Hu733d8f92019-09-23 15:32:40 +0800405 }
406
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800407 /* It is a PROGRAMMER ERROR if called with a stateless handle. */
Mingyang Sune8d38082021-03-30 18:34:40 +0800408 if (IS_STATIC_HANDLE(handle)) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800409 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sun00cef5e2021-03-04 13:41:56 +0800410 }
411
Kevin Peng385fda82021-08-18 10:41:19 +0800412 client_id = tfm_spm_get_client_id(ns_caller);
Summer Qin1ce712a2019-10-14 18:04:05 +0800413
Summer Qin373feb12020-03-27 15:35:33 +0800414 conn_handle = tfm_spm_to_handle_instance(handle);
David Hu733d8f92019-09-23 15:32:40 +0800415 /*
Shawn Shanb222d892021-01-04 17:41:48 +0800416 * It is a PROGRAMMER ERROR if an invalid handle was provided that is not
417 * the null handle.
David Hu733d8f92019-09-23 15:32:40 +0800418 */
Ken Liubcae38b2021-01-20 15:47:44 +0800419 if (tfm_spm_validate_conn_handle(conn_handle, client_id) != SPM_SUCCESS) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800420 return PSA_ERROR_PROGRAMMER_ERROR;
Summer Qin1ce712a2019-10-14 18:04:05 +0800421 }
Shawn Shanb222d892021-01-04 17:41:48 +0800422
Ken Liuf39d8eb2021-10-07 12:55:33 +0800423 service = conn_handle->internal_msg.service;
David Hu733d8f92019-09-23 15:32:40 +0800424 if (!service) {
425 /* FixMe: Need to implement one mechanism to resolve this failure. */
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800426 return PSA_ERROR_PROGRAMMER_ERROR;
David Hu733d8f92019-09-23 15:32:40 +0800427 }
428
Kevin Pengdf6aa292021-03-11 17:58:50 +0800429 msg = tfm_spm_get_msg_buffer_from_conn_handle(conn_handle);
David Hu733d8f92019-09-23 15:32:40 +0800430
Shawn Shanb222d892021-01-04 17:41:48 +0800431 /*
432 * It is a PROGRAMMER ERROR if the connection is currently handling a
433 * request.
434 */
Summer Qin630c76b2020-05-20 10:32:58 +0800435 if (conn_handle->status == TFM_HANDLE_STATUS_ACTIVE) {
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800436 return PSA_ERROR_PROGRAMMER_ERROR;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800437 }
438
David Hu733d8f92019-09-23 15:32:40 +0800439 /* No input or output needed for close message */
Ken Liu505b1702020-05-29 13:19:58 +0800440 tfm_spm_fill_msg(msg, service, handle, PSA_IPC_DISCONNECT, client_id,
David Hu733d8f92019-09-23 15:32:40 +0800441 NULL, 0, NULL, 0, NULL);
442
Xinyu Zhangb287ef82021-11-03 18:38:50 +0800443 return backend_instance.messaging(service, msg);
David Hu733d8f92019-09-23 15:32:40 +0800444}
Mingyang Sunb26b2802021-07-07 11:25:00 +0800445
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800446#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
447
Mingyang Suneeca4652021-07-15 15:19:16 +0800448/* PSA Partition API function body */
449
Mingyang Sunb26b2802021-07-07 11:25:00 +0800450psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask,
451 uint32_t timeout)
452{
453 struct partition_t *partition = NULL;
454
455 /*
456 * Timeout[30:0] are reserved for future use.
457 * SPM must ignore the value of RES.
458 */
459 timeout &= PSA_TIMEOUT_MASK;
460
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800461 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800462
463 /*
464 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
465 * signals.
466 */
467 if ((partition->signals_allowed & signal_mask) == 0) {
468 tfm_core_panic();
469 }
470
471 /*
Ken Liu5d73c872021-08-19 19:23:17 +0800472 * thrd_wait_on() blocks the caller thread if no signals are available.
Mingyang Sunb26b2802021-07-07 11:25:00 +0800473 * In this case, the return value of this function is temporary set into
474 * runtime context. After new signal(s) are available, the return value
475 * is updated with the available signal(s) and blocked thread gets to run.
476 */
477 if (timeout == PSA_BLOCK &&
478 (partition->signals_asserted & signal_mask) == 0) {
479 partition->signals_waiting = signal_mask;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800480 thrd_wait_on(&partition->waitobj, CURRENT_THREAD);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800481 }
482
483 return partition->signals_asserted & signal_mask;
484}
485
486psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg)
487{
488 struct tfm_msg_body_t *tmp_msg = NULL;
489 struct partition_t *partition = NULL;
490 uint32_t privileged;
491
492 /*
493 * Only one message could be retrieved every time for psa_get(). It is a
494 * fatal error if the input signal has more than a signal bit set.
495 */
496 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
497 tfm_core_panic();
498 }
499
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800500 partition = GET_CURRENT_COMPONENT();
501
Kevin Penga40d29f2022-01-19 14:44:34 +0800502 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800503
504 /*
505 * Write the message to the service buffer. It is a fatal error if the
506 * input msg pointer is not a valid memory reference or not read-write.
507 */
508 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
509 privileged) != SPM_SUCCESS) {
510 tfm_core_panic();
511 }
512
513 /*
514 * It is a fatal error if the caller call psa_get() when no message has
515 * been set. The caller must call this function after an RoT Service signal
516 * is returned by psa_wait().
517 */
518 if (partition->signals_asserted == 0) {
519 tfm_core_panic();
520 }
521
522 /*
523 * It is a fatal error if the RoT Service signal is not currently asserted.
524 */
525 if ((partition->signals_asserted & signal) == 0) {
526 tfm_core_panic();
527 }
528
529 /*
530 * Get message by signal from partition. It is a fatal error if getting
531 * failed, which means the input signal is not correspond to an RoT service.
532 */
533 tmp_msg = tfm_spm_get_msg_by_signal(partition, signal);
534 if (!tmp_msg) {
535 return PSA_ERROR_DOES_NOT_EXIST;
536 }
537
538 (TO_CONTAINER(tmp_msg,
539 struct tfm_conn_handle_t,
540 internal_msg))->status = TFM_HANDLE_STATUS_ACTIVE;
541
542 spm_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
543
544 return PSA_SUCCESS;
545}
546
Mingyang Sunb26b2802021-07-07 11:25:00 +0800547size_t tfm_spm_partition_psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
548 void *buffer, size_t num_bytes)
549{
550 size_t bytes;
551 struct tfm_msg_body_t *msg = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800552 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800553
554 /* It is a fatal error if message handle is invalid */
555 msg = tfm_spm_get_msg_from_handle(msg_handle);
556 if (!msg) {
557 tfm_core_panic();
558 }
559
Kevin Penga40d29f2022-01-19 14:44:34 +0800560 priv_mode = GET_PARTITION_PRIVILEGED_MODE(msg->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800561
562 /*
563 * It is a fatal error if message handle does not refer to a request
564 * message
565 */
566 if (msg->msg.type < PSA_IPC_CALL) {
567 tfm_core_panic();
568 }
569
570 /*
571 * It is a fatal error if invec_idx is equal to or greater than
572 * PSA_MAX_IOVEC
573 */
574 if (invec_idx >= PSA_MAX_IOVEC) {
575 tfm_core_panic();
576 }
577
578 /* There was no remaining data in this input vector */
579 if (msg->msg.in_size[invec_idx] == 0) {
580 return 0;
581 }
582
Shawn Shan038348e2021-09-08 17:11:04 +0800583#if PSA_FRAMEWORK_HAS_MM_IOVEC
584 /*
585 * It is a fatal error if the input vector has already been mapped using
586 * psa_map_invec().
587 */
588 if (IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
589 tfm_core_panic();
590 }
591
592 SET_IOVEC_ACCESSED(msg, (invec_idx + INVEC_IDX_BASE));
593#endif
594
Mingyang Sunb26b2802021-07-07 11:25:00 +0800595 /*
596 * Copy the client data to the service buffer. It is a fatal error
597 * if the memory reference for buffer is invalid or not read-write.
598 */
599 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800600 TFM_MEMORY_ACCESS_RW, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800601 tfm_core_panic();
602 }
603
604 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
605 msg->msg.in_size[invec_idx] : num_bytes;
606
607 spm_memcpy(buffer, msg->invec[invec_idx].base, bytes);
608
609 /* There maybe some remaining data */
610 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
611 msg->msg.in_size[invec_idx] -= bytes;
612
613 return bytes;
614}
615
616size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx,
617 size_t num_bytes)
618{
619 struct tfm_msg_body_t *msg = NULL;
620
621 /* It is a fatal error if message handle is invalid */
622 msg = tfm_spm_get_msg_from_handle(msg_handle);
623 if (!msg) {
624 tfm_core_panic();
625 }
626
627 /*
628 * It is a fatal error if message handle does not refer to a request
629 * message
630 */
631 if (msg->msg.type < PSA_IPC_CALL) {
632 tfm_core_panic();
633 }
634
635 /*
636 * It is a fatal error if invec_idx is equal to or greater than
637 * PSA_MAX_IOVEC
638 */
639 if (invec_idx >= PSA_MAX_IOVEC) {
640 tfm_core_panic();
641 }
642
643 /* There was no remaining data in this input vector */
644 if (msg->msg.in_size[invec_idx] == 0) {
645 return 0;
646 }
647
Shawn Shan038348e2021-09-08 17:11:04 +0800648#if PSA_FRAMEWORK_HAS_MM_IOVEC
649 /*
650 * It is a fatal error if the input vector has already been mapped using
651 * psa_map_invec().
652 */
653 if (IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
654 tfm_core_panic();
655 }
656
657 SET_IOVEC_ACCESSED(msg, (invec_idx + INVEC_IDX_BASE));
658#endif
659
Mingyang Sunb26b2802021-07-07 11:25:00 +0800660 /*
661 * If num_bytes is greater than the remaining size of the input vector then
662 * the remaining size of the input vector is used.
663 */
664 if (num_bytes > msg->msg.in_size[invec_idx]) {
665 num_bytes = msg->msg.in_size[invec_idx];
666 }
667
668 /* There maybe some remaining data */
669 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
670 num_bytes;
671 msg->msg.in_size[invec_idx] -= num_bytes;
672
673 return num_bytes;
674}
675
676void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
677 const void *buffer, size_t num_bytes)
678{
679 struct tfm_msg_body_t *msg = NULL;
Kevin Penga40d29f2022-01-19 14:44:34 +0800680 uint32_t priv_mode;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800681
682 /* It is a fatal error if message handle is invalid */
683 msg = tfm_spm_get_msg_from_handle(msg_handle);
684 if (!msg) {
685 tfm_core_panic();
686 }
687
Kevin Penga40d29f2022-01-19 14:44:34 +0800688 priv_mode = GET_PARTITION_PRIVILEGED_MODE(msg->service->partition->p_ldinf);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800689
690 /*
691 * It is a fatal error if message handle does not refer to a request
692 * message
693 */
694 if (msg->msg.type < PSA_IPC_CALL) {
695 tfm_core_panic();
696 }
697
698 /*
699 * It is a fatal error if outvec_idx is equal to or greater than
700 * PSA_MAX_IOVEC
701 */
702 if (outvec_idx >= PSA_MAX_IOVEC) {
703 tfm_core_panic();
704 }
705
706 /*
707 * It is a fatal error if the call attempts to write data past the end of
708 * the client output vector
709 */
710 if (num_bytes > msg->msg.out_size[outvec_idx] -
711 msg->outvec[outvec_idx].len) {
712 tfm_core_panic();
713 }
714
Shawn Shan038348e2021-09-08 17:11:04 +0800715#if PSA_FRAMEWORK_HAS_MM_IOVEC
716 /*
717 * It is a fatal error if the output vector has already been mapped using
718 * psa_map_outvec().
719 */
720 if (IOVEC_IS_MAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
721 tfm_core_panic();
722 }
723
724 SET_IOVEC_ACCESSED(msg, (outvec_idx + OUTVEC_IDX_BASE));
725#endif
726
Mingyang Sunb26b2802021-07-07 11:25:00 +0800727 /*
728 * Copy the service buffer to client outvecs. It is a fatal error
729 * if the memory reference for buffer is invalid or not readable.
730 */
731 if (tfm_memory_check(buffer, num_bytes, false,
Kevin Penga40d29f2022-01-19 14:44:34 +0800732 TFM_MEMORY_ACCESS_RO, priv_mode) != SPM_SUCCESS) {
Mingyang Sunb26b2802021-07-07 11:25:00 +0800733 tfm_core_panic();
734 }
735
736 spm_memcpy((char *)msg->outvec[outvec_idx].base +
737 msg->outvec[outvec_idx].len, buffer, num_bytes);
738
739 /* Update the write number */
740 msg->outvec[outvec_idx].len += num_bytes;
741}
742
Ken Liuf39d8eb2021-10-07 12:55:33 +0800743int32_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle,
744 psa_status_t status)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800745{
746 struct service_t *service = NULL;
747 struct tfm_msg_body_t *msg = NULL;
748 int32_t ret = PSA_SUCCESS;
749 struct tfm_conn_handle_t *conn_handle;
Mingyang Sun620c8562021-11-10 11:44:58 +0800750 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800751
752 /* It is a fatal error if message handle is invalid */
753 msg = tfm_spm_get_msg_from_handle(msg_handle);
754 if (!msg) {
755 tfm_core_panic();
756 }
757
758 /*
759 * RoT Service information is needed in this function, stored it in message
760 * body structure. Only two parameters are passed in this function: handle
761 * and status, so it is useful and simply to do like this.
762 */
763 service = msg->service;
764 if (!service) {
765 tfm_core_panic();
766 }
767
768 /*
769 * Three type of message are passed in this function: CONNECTION, REQUEST,
770 * DISCONNECTION. It needs to process differently for each type.
771 */
772 conn_handle = tfm_spm_to_handle_instance(msg_handle);
773 switch (msg->msg.type) {
774 case PSA_IPC_CONNECT:
775 /*
776 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
777 * input status is PSA_SUCCESS. Others return values are based on the
778 * input status.
779 */
780 if (status == PSA_SUCCESS) {
781 ret = msg_handle;
782 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
783 /* Refuse the client connection, indicating a permanent error. */
784 tfm_spm_free_conn_handle(service, conn_handle);
785 ret = PSA_ERROR_CONNECTION_REFUSED;
786 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
787 /* Fail the client connection, indicating a transient error. */
788 ret = PSA_ERROR_CONNECTION_BUSY;
789 } else {
790 tfm_core_panic();
791 }
792 break;
793 case PSA_IPC_DISCONNECT:
794 /* Service handle is not used anymore */
795 tfm_spm_free_conn_handle(service, conn_handle);
796
797 /*
798 * If the message type is PSA_IPC_DISCONNECT, then the status code is
799 * ignored
800 */
801 break;
802 default:
803 if (msg->msg.type >= PSA_IPC_CALL) {
Shawn Shan038348e2021-09-08 17:11:04 +0800804
805#if PSA_FRAMEWORK_HAS_MM_IOVEC
806
807 /*
808 * If the unmapped function is not called for an input/output vector
809 * that has been mapped, the framework will remove the mapping.
810 */
811 int i;
812
813 for (i = 0; i < PSA_MAX_IOVEC * 2; i++) {
814 if (IOVEC_IS_MAPPED(msg, i) && (!IOVEC_IS_UNMAPPED(msg, i))) {
815 SET_IOVEC_UNMAPPED(msg, i);
816 /*
817 * Any output vectors that are still mapped will report that
818 * zero bytes have been written.
819 */
820 if (i >= OUTVEC_IDX_BASE) {
821 msg->outvec[i - OUTVEC_IDX_BASE].len = 0;
822 }
823 }
824 }
825
826#endif
Mingyang Sunb26b2802021-07-07 11:25:00 +0800827 /* Reply to a request message. Return values are based on status */
828 ret = status;
829 /*
830 * The total number of bytes written to a single parameter must be
831 * reported to the client by updating the len member of the
832 * psa_outvec structure for the parameter before returning from
833 * psa_call().
834 */
835 update_caller_outvec_len(msg);
836 if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
837 tfm_spm_free_conn_handle(service, conn_handle);
838 }
839 } else {
840 tfm_core_panic();
841 }
842 }
843
844 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
845 /*
846 * If the source of the programmer error is a Secure Partition, the SPM
847 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
848 */
849 if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) {
850 conn_handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
851 } else {
852 tfm_core_panic();
853 }
854 } else {
855 conn_handle->status = TFM_HANDLE_STATUS_IDLE;
856 }
857
Mingyang Sun620c8562021-11-10 11:44:58 +0800858 /*
859 * TODO: It can be optimized further by moving critical section protection
860 * to mailbox. Also need to check implementation when secure context is
861 * involved.
862 */
863 CRITICAL_SECTION_ENTER(cs_assert);
864 ret = backend_instance.replying(msg, ret);
865 CRITICAL_SECTION_LEAVE(cs_assert);
866
867 return ret;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800868}
869
870void tfm_spm_partition_psa_notify(int32_t partition_id)
871{
Ken Liu5d73c872021-08-19 19:23:17 +0800872 struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id);
873
874 spm_assert_signal(p_pt, PSA_DOORBELL);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800875}
876
877void tfm_spm_partition_psa_clear(void)
878{
Ken Liu92ede9f2021-10-20 09:35:00 +0800879 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800880 struct partition_t *partition = NULL;
881
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800882 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800883
884 /*
885 * It is a fatal error if the Secure Partition's doorbell signal is not
886 * currently asserted.
887 */
888 if ((partition->signals_asserted & PSA_DOORBELL) == 0) {
889 tfm_core_panic();
890 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800891
892 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800893 partition->signals_asserted &= ~PSA_DOORBELL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800894 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800895}
896
897void tfm_spm_partition_psa_eoi(psa_signal_t irq_signal)
898{
Ken Liu92ede9f2021-10-20 09:35:00 +0800899 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800900 struct irq_load_info_t *irq_info = NULL;
901 struct partition_t *partition = NULL;
902
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800903 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800904
905 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
906 /* It is a fatal error if passed signal is not an interrupt signal. */
907 if (!irq_info) {
908 tfm_core_panic();
909 }
910
911 if (irq_info->flih_func) {
912 /* This API is for SLIH IRQs only */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800913 tfm_core_panic();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800914 }
915
916 /* It is a fatal error if passed signal is not currently asserted */
917 if ((partition->signals_asserted & irq_signal) == 0) {
918 tfm_core_panic();
919 }
920
Ken Liu92ede9f2021-10-20 09:35:00 +0800921 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800922 partition->signals_asserted &= ~irq_signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800923 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800924
Kevin Pengd399a1f2021-09-08 15:33:14 +0800925 tfm_hal_irq_clear_pending(irq_info->source);
926 tfm_hal_irq_enable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800927}
928
929void tfm_spm_partition_psa_panic(void)
930{
931 /*
932 * PSA FF recommends that the SPM causes the system to restart when a secure
933 * partition panics.
934 */
935 tfm_hal_system_reset();
936}
937
Kevin Peng67a89fd2021-11-25 11:22:02 +0800938void tfm_spm_partition_psa_irq_enable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800939{
940 struct partition_t *partition;
941 struct irq_load_info_t *irq_info;
942
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800943 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800944
945 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
946 if (!irq_info) {
947 tfm_core_panic();
948 }
949
Kevin Pengd399a1f2021-09-08 15:33:14 +0800950 tfm_hal_irq_enable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800951}
952
Kevin Peng67a89fd2021-11-25 11:22:02 +0800953psa_irq_status_t tfm_spm_partition_psa_irq_disable(psa_signal_t irq_signal)
Mingyang Sunb26b2802021-07-07 11:25:00 +0800954{
955 struct partition_t *partition;
956 struct irq_load_info_t *irq_info;
957
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800958 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800959
960 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
961 if (!irq_info) {
962 tfm_core_panic();
963 }
964
Kevin Pengd399a1f2021-09-08 15:33:14 +0800965 tfm_hal_irq_disable(irq_info->source);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800966
967 return 1;
968}
969
970void tfm_spm_partition_psa_reset_signal(psa_signal_t irq_signal)
971{
Ken Liu92ede9f2021-10-20 09:35:00 +0800972 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sunb26b2802021-07-07 11:25:00 +0800973 struct irq_load_info_t *irq_info;
974 struct partition_t *partition;
975
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800976 partition = GET_CURRENT_COMPONENT();
Mingyang Sunb26b2802021-07-07 11:25:00 +0800977
978 irq_info = get_irq_info_for_signal(partition->p_ldinf, irq_signal);
979 if (!irq_info) {
980 tfm_core_panic();
981 }
982
983 if (!irq_info->flih_func) {
984 /* This API is for FLIH IRQs only */
985 tfm_core_panic();
986 }
987
988 if ((partition->signals_asserted & irq_signal) == 0) {
989 /* The signal is not asserted */
990 tfm_core_panic();
991 }
992
Ken Liu92ede9f2021-10-20 09:35:00 +0800993 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800994 partition->signals_asserted &= ~irq_signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800995 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sunb26b2802021-07-07 11:25:00 +0800996}
Shawn Shan038348e2021-09-08 17:11:04 +0800997
Xinyu Zhang2bc4d572021-12-27 16:37:46 +0800998/* psa_set_rhandle is only needed by connection-based services */
999#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
1000
1001void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle)
1002{
1003 struct tfm_msg_body_t *msg = NULL;
1004 struct tfm_conn_handle_t *conn_handle;
1005
1006 /* It is a fatal error if message handle is invalid */
1007 msg = tfm_spm_get_msg_from_handle(msg_handle);
1008 if (!msg) {
1009 tfm_core_panic();
1010 }
1011
1012 /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */
1013 if (SERVICE_IS_STATELESS(msg->service->p_ldinf->flags)) {
1014 tfm_core_panic();
1015 }
1016
1017 msg->msg.rhandle = rhandle;
1018 conn_handle = tfm_spm_to_handle_instance(msg_handle);
1019
1020 /* Store reverse handle for following client calls. */
1021 tfm_spm_set_rhandle(msg->service, conn_handle, rhandle);
1022}
1023
1024#endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
1025
Shawn Shan038348e2021-09-08 17:11:04 +08001026#if PSA_FRAMEWORK_HAS_MM_IOVEC
1027
1028const void *tfm_spm_partition_psa_map_invec(psa_handle_t msg_handle,
1029 uint32_t invec_idx)
1030{
1031 struct tfm_msg_body_t *msg = NULL;
1032 uint32_t privileged;
1033 struct partition_t *partition = NULL;
1034
1035 /* It is a fatal error if message handle is invalid */
1036 msg = tfm_spm_get_msg_from_handle(msg_handle);
1037 if (!msg) {
1038 tfm_core_panic();
1039 }
1040
1041 partition = msg->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001042 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001043
1044 /*
1045 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1046 * Service that received the message.
1047 */
1048 if (!SERVICE_ENABLED_MM_IOVEC(msg->service->p_ldinf->flags)) {
1049 tfm_core_panic();
1050 }
1051
1052 /*
1053 * It is a fatal error if message handle does not refer to a request
1054 * message.
1055 */
1056 if (msg->msg.type < PSA_IPC_CALL) {
1057 tfm_core_panic();
1058 }
1059
1060 /*
1061 * It is a fatal error if invec_idx is equal to or greater than
1062 * PSA_MAX_IOVEC.
1063 */
1064 if (invec_idx >= PSA_MAX_IOVEC) {
1065 tfm_core_panic();
1066 }
1067
1068 /* It is a fatal error if the input vector has length zero. */
1069 if (msg->msg.in_size[invec_idx] == 0) {
1070 tfm_core_panic();
1071 }
1072
1073 /*
1074 * It is a fatal error if the input vector has already been mapped using
1075 * psa_map_invec().
1076 */
1077 if (IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
1078 tfm_core_panic();
1079 }
1080
1081 /*
1082 * It is a fatal error if the input vector has already been accessed
1083 * using psa_read() or psa_skip().
1084 */
1085 if (IOVEC_IS_ACCESSED(msg, (invec_idx + INVEC_IDX_BASE))) {
1086 tfm_core_panic();
1087 }
1088
1089 /*
1090 * It is a fatal error if the memory reference for the wrap input vector is
1091 * invalid or not readable.
1092 */
1093 if (tfm_memory_check(msg->invec[invec_idx].base, msg->invec[invec_idx].len,
1094 false, TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
1095 tfm_core_panic();
1096 }
1097
1098 SET_IOVEC_MAPPED(msg, (invec_idx + INVEC_IDX_BASE));
1099
1100 return msg->invec[invec_idx].base;
1101}
1102
1103void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle,
1104 uint32_t invec_idx)
1105{
1106 struct tfm_msg_body_t *msg = NULL;
1107
1108 /* It is a fatal error if message handle is invalid */
1109 msg = tfm_spm_get_msg_from_handle(msg_handle);
1110 if (!msg) {
1111 tfm_core_panic();
1112 }
1113
1114 /*
1115 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1116 * Service that received the message.
1117 */
1118 if (!SERVICE_ENABLED_MM_IOVEC(msg->service->p_ldinf->flags)) {
1119 tfm_core_panic();
1120 }
1121
1122 /*
1123 * It is a fatal error if message handle does not refer to a request
1124 * message.
1125 */
1126 if (msg->msg.type < PSA_IPC_CALL) {
1127 tfm_core_panic();
1128 }
1129
1130 /*
1131 * It is a fatal error if invec_idx is equal to or greater than
1132 * PSA_MAX_IOVEC.
1133 */
1134 if (invec_idx >= PSA_MAX_IOVEC) {
1135 tfm_core_panic();
1136 }
1137
1138 /*
1139 * It is a fatal error if The input vector has not been mapped by a call to
1140 * psa_map_invec().
1141 */
1142 if (!IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
1143 tfm_core_panic();
1144 }
1145
1146 /*
1147 * It is a fatal error if the input vector has already been unmapped by a
1148 * call to psa_unmap_invec().
1149 */
1150 if (IOVEC_IS_UNMAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
1151 tfm_core_panic();
1152 }
1153
1154 SET_IOVEC_UNMAPPED(msg, (invec_idx + INVEC_IDX_BASE));
1155}
1156
1157void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle,
1158 uint32_t outvec_idx)
1159{
1160 struct tfm_msg_body_t *msg = NULL;
1161 uint32_t privileged;
1162 struct partition_t *partition = NULL;
1163
1164 /* It is a fatal error if message handle is invalid */
1165 msg = tfm_spm_get_msg_from_handle(msg_handle);
1166 if (!msg) {
1167 tfm_core_panic();
1168 }
1169
1170 partition = msg->service->partition;
Kevin Penga40d29f2022-01-19 14:44:34 +08001171 privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
Shawn Shan038348e2021-09-08 17:11:04 +08001172
1173 /*
1174 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1175 * Service that received the message.
1176 */
1177 if (!SERVICE_ENABLED_MM_IOVEC(msg->service->p_ldinf->flags)) {
1178 tfm_core_panic();
1179 }
1180
1181 /*
1182 * It is a fatal error if message handle does not refer to a request
1183 * message.
1184 */
1185 if (msg->msg.type < PSA_IPC_CALL) {
1186 tfm_core_panic();
1187 }
1188
1189 /*
1190 * It is a fatal error if outvec_idx is equal to or greater than
1191 * PSA_MAX_IOVEC.
1192 */
1193 if (outvec_idx >= PSA_MAX_IOVEC) {
1194 tfm_core_panic();
1195 }
1196
1197 /* It is a fatal error if the output vector has length zero. */
1198 if (msg->msg.out_size[outvec_idx] == 0) {
1199 tfm_core_panic();
1200 }
1201
1202 /*
1203 * It is a fatal error if the output vector has already been mapped using
1204 * psa_map_outvec().
1205 */
1206 if (IOVEC_IS_MAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
1207 tfm_core_panic();
1208 }
1209
1210 /*
1211 * It is a fatal error if the output vector has already been accessed
1212 * using psa_write().
1213 */
1214 if (IOVEC_IS_ACCESSED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
1215 tfm_core_panic();
1216 }
1217
1218 /*
1219 * It is a fatal error if the output vector is invalid or not read-write.
1220 */
1221 if (tfm_memory_check(msg->outvec[outvec_idx].base,
1222 msg->outvec[outvec_idx].len, false,
1223 TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
1224 tfm_core_panic();
1225 }
1226 SET_IOVEC_MAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE));
1227
1228 return msg->outvec[outvec_idx].base;
1229}
1230
1231void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle,
1232 uint32_t outvec_idx, size_t len)
1233{
1234 struct tfm_msg_body_t *msg = NULL;
1235
1236 /* It is a fatal error if message handle is invalid */
1237 msg = tfm_spm_get_msg_from_handle(msg_handle);
1238 if (!msg) {
1239 tfm_core_panic();
1240 }
1241
1242 /*
1243 * It is a fatal error if MM-IOVEC has not been enabled for the RoT
1244 * Service that received the message.
1245 */
1246 if (!SERVICE_ENABLED_MM_IOVEC(msg->service->p_ldinf->flags)) {
1247 tfm_core_panic();
1248 }
1249
1250 /*
1251 * It is a fatal error if message handle does not refer to a request
1252 * message.
1253 */
1254 if (msg->msg.type < PSA_IPC_CALL) {
1255 tfm_core_panic();
1256 }
1257
1258 /*
1259 * It is a fatal error if outvec_idx is equal to or greater than
1260 * PSA_MAX_IOVEC.
1261 */
1262 if (outvec_idx >= PSA_MAX_IOVEC) {
1263 tfm_core_panic();
1264 }
1265
1266 /*
1267 * It is a fatal error if len is greater than the output vector size.
1268 */
1269 if (len > msg->msg.out_size[outvec_idx]) {
1270 tfm_core_panic();
1271 }
1272
1273 /*
1274 * It is a fatal error if The output vector has not been mapped by a call to
1275 * psa_map_outvec().
1276 */
1277 if (!IOVEC_IS_MAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
1278 tfm_core_panic();
1279 }
1280
1281 /*
1282 * It is a fatal error if the output vector has already been unmapped by a
1283 * call to psa_unmap_outvec().
1284 */
1285 if (IOVEC_IS_UNMAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
1286 tfm_core_panic();
1287 }
1288
1289 SET_IOVEC_UNMAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE));
1290
1291 /* Update the write number */
1292 msg->outvec[outvec_idx].len = len;
1293}
1294
1295#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */