TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
spm_ipc.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef __SPM_IPC_H__
9 #define __SPM_IPC_H__
10 
11 #include <stdint.h>
12 #include "spm_partition_defs.h"
13 #include "tfm_arch.h"
14 #include "tfm_list.h"
15 #include "tfm_wait.h"
16 #include "tfm_secure_api.h"
17 #include "tfm_thread.h"
18 #include "psa/service.h"
19 
20 #define TFM_VERSION_POLICY_RELAXED 0
21 #define TFM_VERSION_POLICY_STRICT 1
22 
23 #define TFM_HANDLE_STATUS_IDLE 0
24 #define TFM_HANDLE_STATUS_ACTIVE 1
25 #define TFM_HANDLE_STATUS_CONNECT_ERROR 2
26 
27 #define PART_REGION_ADDR(partition, region) \
28  (uint32_t)&REGION_NAME(Image$$, partition, region)
29 
30 #define TFM_CONN_HANDLE_MAX_NUM 16
31 
32 #define SPM_INVALID_PARTITION_IDX (~0U)
33 
34 /* Privileged definitions for partition thread mode */
35 #define TFM_PARTITION_UNPRIVILEGED_MODE 0
36 #define TFM_PARTITION_PRIVILEGED_MODE 1
37 
38 #define SPM_PART_FLAG_APP_ROT 0x01
39 #define SPM_PART_FLAG_PSA_ROT 0x02
40 #define SPM_PART_FLAG_IPC 0x04
41 
42 #define TFM_PRIORITY_HIGH THRD_PRIOR_HIGHEST
43 #define TFM_PRIORITY_NORMAL THRD_PRIOR_MEDIUM
44 #define TFM_PRIORITY_LOW THRD_PRIOR_LOWEST
45 #define TFM_PRIORITY(LEVEL) TFM_PRIORITY_##LEVEL
46 
47 #define TFM_MSG_MAGIC 0x15154343
48 
49 enum spm_err_t {
56 };
57 
58 /* Message struct to collect parameter from client */
60  int32_t magic;
61  struct tfm_spm_service_t *service; /* RoT service pointer */
62  struct tfm_event_t ack_evnt; /* Event for ack reponse */
63  psa_msg_t msg; /* PSA message body */
64  psa_invec invec[PSA_MAX_IOVEC]; /* Put in/out vectors in msg body */
67  * Save caller outvec pointer for
68  * write length update
69  */
70 #ifdef TFM_MULTI_CORE_TOPOLOGY
71  const void *caller_data; /*
72  * Pointer to the private data of the
73  * caller. It identifies the NSPE PSA
74  * client calls in multi-core topology
75  */
76 #endif
77  struct tfm_list_node_t msg_node; /* For list operators */
78 };
79 
87  uint32_t partition_id;
88  uint32_t partition_flags;
91  uintptr_t stack_base;
92  size_t stack_size;
93  uintptr_t heap_base;
94  size_t heap_size;
95  uint32_t dependencies_num;
96  uint32_t *p_dependencies;
97 };
98 
103 struct partition_t {
105  void *p_platform;
107  void *p_metadata;
111  uint32_t signals_allowed;
112  uint32_t signals_waiting;
115  const struct tfm_spm_partition_platform_data_t **platform_data_list;
116  const struct tfm_spm_partition_memory_data_t *memory_data;
117 };
118 
119 struct spm_partition_db_t {
120  uint32_t is_init;
121  uint32_t partition_count;
123 };
124 
125 /* Service database defined by manifest */
127  char *name; /* Service name */
128  uint32_t partition_id; /* Partition ID which service belong to */
129  psa_signal_t signal; /* Service signal */
130  uint32_t sid; /* Service identifier */
131  bool non_secure_client; /* If can be called by non secure client */
132  uint32_t version; /* Service version */
133  uint32_t version_policy; /* Service version policy */
134 };
135 
136 /* RoT Service data */
138  const struct tfm_spm_service_db_t *service_db;/* Service database pointer */
139  struct partition_t *partition; /*
140  * Point to secure partition
141  * data
142  */
143  struct tfm_list_node_t handle_list; /* Service handle list */
144  struct tfm_list_node_t list; /* For list operation */
145 };
146 
147 /* RoT connection handle list */
149  void *rhandle; /* Reverse handle value */
150  uint32_t status; /*
151  * Status of handle, three valid
152  * options:
153  * TFM_HANDLE_STATUS_ACTIVE,
154  * TFM_HANDLE_STATUS_IDLE and
155  * TFM_HANDLE_STATUS_CONNECT_ERROR
156  */
157  int32_t client_id; /*
158  * Partition ID of the sender of the
159  * message:
160  * - secure partition id;
161  * - non secure client endpoint id.
162  */
163  struct tfm_msg_body_t internal_msg; /* Internal message for message queue */
164  struct tfm_spm_service_t *service; /* RoT service pointer */
165  struct tfm_list_node_t list; /* list node */
166 };
167 
171 };
172 
178 enum spm_err_t tfm_spm_db_init(void);
179 
188 uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags);
189 
194 void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx);
195 
202 
203 /******************** Service handle management functions ********************/
204 
215  struct tfm_spm_service_t *service,
216  int32_t client_id);
217 
228  const struct tfm_conn_handle_t *conn_handle,
229  int32_t client_id);
230 
243  struct tfm_conn_handle_t *conn_handle);
244 
245 /******************** Partition management functions *************************/
246 
259 struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
260  psa_signal_t signal);
261 
270 
280 struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid);
281 
282 /************************ Message functions **********************************/
283 
294 
303 struct tfm_msg_body_t *
305 
322 void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
323  struct tfm_spm_service_t *service,
324  psa_handle_t handle,
325  int32_t type, int32_t client_id,
326  psa_invec *invec, size_t in_len,
327  psa_outvec *outvec, size_t out_len,
329 
345  struct tfm_msg_body_t *msg);
346 
360  uint32_t version);
361 
373 int32_t tfm_spm_check_authorization(uint32_t sid,
374  struct tfm_spm_service_t *service,
375  bool ns_caller);
376 
393 int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
394  enum tfm_memory_access_e access,
395  uint32_t privileged);
396 
397 /*
398  * PendSV specified function.
399  *
400  * Parameters :
401  * p_actx - Architecture context storage pointer
402  *
403  * Notes:
404  * This is a staging API. Scheduler should be called in SPM finally and
405  * this function will be obsoleted later.
406  */
407 void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx);
408 
417 uint32_t tfm_spm_init(void);
418 
430 void tfm_spm_enable_irq(uint32_t *args);
431 
443 void tfm_spm_disable_irq(uint32_t *args);
444 
456 void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx,
457  uint32_t exc_return, bool ns_caller);
458 
462 psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance);
463 
468 
472 void tfm_core_handler_mode(void);
473 
487  struct tfm_conn_handle_t *conn_handle,
488  void *rhandle);
489 
490 void update_caller_outvec_len(struct tfm_msg_body_t *msg);
491 
502 void notify_with_signal(int32_t partition_id, psa_signal_t signal);
503 
517 int32_t get_irq_line_for_signal(int32_t partition_id,
518  psa_signal_t signal,
519  IRQn_Type *irq_line);
520 
521 #endif /* __SPM_IPC_H__ */
struct tfm_list_node_t list
Definition: spm_ipc.h:165
void * p_metadata
Definition: spm_ipc.h:107
struct tfm_conn_handle_t * tfm_spm_create_conn_handle(struct tfm_spm_service_t *service, int32_t client_id)
Create connection handle for client connect.
Definition: spm_ipc.c:137
void(* sp_entry_point)(void)
int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller, enum tfm_memory_access_e access, uint32_t privileged)
Check the memory reference is valid.
Definition: spm_ipc.c:596
uint32_t psa_signal_t
Definition: service.h:50
tfm_memory_access_e
Definition: spm_ipc.h:168
int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service, struct tfm_conn_handle_t *conn_handle, void *rhandle)
Set reverse handle value for connection.
Definition: spm_ipc.c:196
struct tfm_msg_body_t * tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Get message context by message handle.
Definition: spm_ipc.c:450
int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service, uint32_t version)
Check the client version according to version policy.
Definition: spm_ipc.c:394
struct tfm_spm_service_t * tfm_spm_get_service_by_sid(uint32_t sid)
Get the service context by service ID.
Definition: spm_ipc.c:351
struct tfm_list_node_t list
Definition: spm_ipc.h:144
uintptr_t stack_base
Definition: spm_ipc.h:91
struct tfm_spm_service_t service[]
void tfm_spm_fill_msg(struct tfm_msg_body_t *msg, struct tfm_spm_service_t *service, psa_handle_t handle, int32_t type, int32_t client_id, psa_invec *invec, size_t in_len, psa_outvec *outvec, size_t out_len, psa_outvec *caller_outvec)
Fill the message for PSA client call.
Definition: spm_ipc.c:498
void * rhandle
Definition: spm_ipc.h:149
struct tfm_list_node_t msg_node
Definition: spm_ipc.h:77
void tfm_core_handler_mode(void)
Move to handler mode by a SVC for specific purpose.
uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
Get the current partition mode.
Definition: spm_func.c:713
struct tfm_list_node_t msg_list
Definition: spm_ipc.h:110
psa_outvec outvec[PSA_MAX_IOVEC]
Definition: spm_ipc.h:65
struct tfm_list_node_t handle_list
Definition: spm_ipc.h:143
struct tfm_spm_service_t * service
Definition: spm_ipc.h:164
psa_outvec * caller_outvec
Definition: spm_ipc.h:66
uint32_t version_policy
Definition: spm_ipc.h:133
struct tfm_msg_body_t internal_msg
Definition: spm_ipc.h:163
void notify_with_signal(int32_t partition_id, psa_signal_t signal)
notify the partition with the signal.
Definition: spm_ipc.c:831
psa_invec invec[PSA_MAX_IOVEC]
Definition: spm_ipc.h:64
uint32_t partition_flags
Definition: spm_ipc.h:88
void * p_interrupts
Definition: spm_ipc.h:106
#define PSA_MAX_IOVEC
Definition: client.h:54
void tfm_spm_disable_irq(uint32_t *args)
SVC handler of disabling irq_line of the specified irq_signal.
Definition: spm_ipc.c:925
void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx, uint32_t exc_return, bool ns_caller)
Validate the whether NS caller re-enter.
Definition: spm_ipc.c:953
int32_t get_irq_line_for_signal(int32_t partition_id, psa_signal_t signal, IRQn_Type *irq_line)
Return the IRQ line number associated with a signal.
Definition: spm_ipc.c:881
void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Definition: spm_ipc.c:807
struct partition_t * partition
Definition: spm_ipc.h:139
const struct tfm_spm_partition_platform_data_t ** platform_data_list
Definition: spm_ipc.h:115
uint32_t partition_count
Definition: spm_func.h:127
uint32_t signals_waiting
Definition: spm_ipc.h:112
struct tfm_msg_body_t * tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Get message context by connect handle.
Definition: spm_ipc.c:491
struct tfm_event_t event
Definition: spm_ipc.h:109
psa_msg_t msg
Definition: spm_ipc.h:63
uint32_t partition_id
Definition: spm_ipc.h:87
size_t heap_size
Definition: spm_ipc.h:94
uintptr_t heap_base
Definition: spm_ipc.h:93
sp_entry_point partition_init
Definition: spm_ipc.h:90
spm_err_t
Definition: spm_func.h:44
uint32_t * p_dependencies
Definition: spm_ipc.h:96
int32_t tfm_spm_check_authorization(uint32_t sid, struct tfm_spm_service_t *service, bool ns_caller)
Check the client access authorization.
Definition: spm_ipc.c:416
void tfm_spm_enable_irq(uint32_t *args)
SVC handler of enabling irq_line of the specified irq_signal.
Definition: spm_ipc.c:897
int32_t tfm_spm_validate_conn_handle(const struct tfm_conn_handle_t *conn_handle, int32_t client_id)
Validate connection handle for client connect.
Definition: spm_ipc.c:161
struct tfm_spm_service_t * service
Definition: spm_ipc.h:61
int32_t tfm_spm_send_event(struct tfm_spm_service_t *service, struct tfm_msg_body_t *msg)
Send message and wake up the SP who is waiting on message queue, block the current thread and schedul...
Definition: spm_ipc.c:556
int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service, struct tfm_conn_handle_t *conn_handle)
Free connection handle which not used anymore.
Definition: spm_ipc.c:179
uint32_t signals_allowed
Definition: spm_ipc.h:111
struct tfm_conn_handle_t * tfm_spm_to_handle_instance(psa_handle_t user_handle)
Converts a user handle into a corresponded handle instance.
Definition: spm_ipc.c:121
uint32_t signals_asserted
Definition: spm_ipc.h:113
size_t stack_size
Definition: spm_ipc.h:92
uint32_t tfm_spm_init(void)
SPM initialization implementation.
Definition: spm_ipc.c:641
uint32_t dependencies_num
Definition: spm_ipc.h:95
uint32_t psa_framework_version
Definition: spm_ipc.h:86
void * p_platform
Definition: spm_ipc.h:105
enum spm_err_t tfm_spm_db_init(void)
Initialize partition database.
Definition: spm_func.c:1415
uint32_t tfm_spm_partition_get_running_partition_id(void)
Get the running partition ID.
Definition: spm_ipc.c:584
int32_t psa_handle_t
Definition: client.h:61
void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Definition: spm_ipc.c:756
psa_signal_t signal
Definition: spm_ipc.h:129
uint32_t version
Definition: spm_ipc.h:132
const struct partition_static_t * static_data
Definition: spm_ipc.h:104
int32_t client_id
Definition: spm_ipc.h:157
void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
Handle an SPM request by a secure service.
Definition: spm_func.c:1382
uint32_t is_init
Definition: spm_func.h:126
struct partition_t * partitions
Definition: spm_ipc.h:122
struct tfm_event_t ack_evnt
Definition: spm_ipc.h:62
uint32_t partition_id
Definition: spm_ipc.h:128
struct tfm_core_thread_t sp_thread
Definition: spm_ipc.h:108
const struct tfm_spm_partition_memory_data_t * memory_data
Definition: spm_ipc.h:116
const struct tfm_spm_service_db_t * service_db
Definition: spm_ipc.h:138
psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Converts a handle instance into a corresponded user handle.
Definition: spm_ipc.c:93
struct partition_t * tfm_spm_get_running_partition(void)
Get current running partition context.
Definition: spm_ipc.c:384
uint32_t status
Definition: spm_ipc.h:150
struct tfm_msg_body_t * tfm_spm_get_msg_by_signal(struct partition_t *partition, psa_signal_t signal)
Get the msg context by signal.
Definition: spm_ipc.c:233
uint32_t partition_priority
Definition: spm_ipc.h:89
int32_t magic
Definition: spm_ipc.h:60