Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 7 | |
| 8 | /* All the APIs defined in this file are used for IPC model. */ |
| 9 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 10 | #include <inttypes.h> |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 11 | #include <limits.h> |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 12 | #include <stdbool.h> |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 13 | #include <stdlib.h> |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 14 | #include "psa/client.h" |
| 15 | #include "psa/service.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 16 | #include "tfm_utils.h" |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 17 | #include "tfm_spm_hal.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 18 | #include "spm_api.h" |
| 19 | #include "spm_db.h" |
David Hu | 326f397 | 2019-08-06 14:16:51 +0800 | [diff] [blame] | 20 | #include "tfm_core_mem_check.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 21 | #include "tfm_internal_defines.h" |
| 22 | #include "tfm_wait.h" |
| 23 | #include "tfm_message_queue.h" |
| 24 | #include "tfm_list.h" |
| 25 | #include "tfm_pools.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 26 | #include "tfm_thread.h" |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 27 | #include "region_defs.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 28 | #include "tfm_nspm.h" |
Edison Ai | 807fedb | 2019-03-07 11:22:03 +0800 | [diff] [blame] | 29 | #include "tfm_memory_utils.h" |
Mingyang Sun | 94b1b41 | 2019-09-20 15:11:14 +0800 | [diff] [blame] | 30 | #include "tfm_core_utils.h" |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 31 | #include "tfm_rpc.h" |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 32 | #include "tfm_irq_list.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 33 | |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 34 | #include "secure_fw/services/tfm_service_list.inc" |
| 35 | |
| 36 | /* Extern service variable */ |
| 37 | extern struct tfm_spm_service_t service[]; |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 38 | extern const struct tfm_spm_service_db_t service_db[]; |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 39 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 40 | /* Extern SPM variable */ |
| 41 | extern struct spm_partition_db_t g_spm_partition_db; |
| 42 | |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 43 | /* Extern secure lock variable */ |
| 44 | extern int32_t tfm_secure_lock; |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 45 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 46 | /* Pools */ |
| 47 | TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t), |
| 48 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 49 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 50 | /********************** SPM functions for handler mode ***********************/ |
| 51 | |
| 52 | /* Service handle management functions */ |
| 53 | psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service) |
| 54 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 55 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 56 | |
| 57 | TFM_ASSERT(service); |
| 58 | |
| 59 | /* Get buffer for handle list structure from handle pool */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 60 | p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool); |
| 61 | if (!p_handle) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 62 | return PSA_NULL_HANDLE; |
| 63 | } |
| 64 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 65 | p_handle->service = service; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 66 | |
| 67 | /* Add handle node to list for next psa functions */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 68 | tfm_list_add_tail(&service->handle_list, &p_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 69 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 70 | return (psa_handle_t)p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static struct tfm_conn_handle_t * |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 74 | tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service, |
| 75 | psa_handle_t conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 76 | { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 77 | TFM_ASSERT(service); |
| 78 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 79 | return (struct tfm_conn_handle_t *)conn_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service, |
| 83 | psa_handle_t conn_handle) |
| 84 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 85 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 86 | |
| 87 | TFM_ASSERT(service); |
| 88 | |
| 89 | /* There are many handles for each RoT Service */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 90 | p_handle = tfm_spm_find_conn_handle_node(service, conn_handle); |
| 91 | if (!p_handle) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 92 | tfm_panic(); |
| 93 | } |
| 94 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 95 | /* Clear magic as the handler is not used anymore */ |
| 96 | p_handle->internal_msg.magic = 0; |
| 97 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 98 | /* Remove node from handle list */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 99 | tfm_list_del_node(&p_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 100 | |
| 101 | /* Back handle buffer to pool */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 102 | tfm_pool_free(p_handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 103 | return IPC_SUCCESS; |
| 104 | } |
| 105 | |
| 106 | int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service, |
| 107 | psa_handle_t conn_handle, |
| 108 | void *rhandle) |
| 109 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 110 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 111 | |
| 112 | TFM_ASSERT(service); |
| 113 | /* Set reverse handle value only be allowed for a connected handle */ |
| 114 | TFM_ASSERT(conn_handle != PSA_NULL_HANDLE); |
| 115 | |
| 116 | /* There are many handles for each RoT Service */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 117 | p_handle = tfm_spm_find_conn_handle_node(service, conn_handle); |
| 118 | if (!p_handle) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 119 | tfm_panic(); |
| 120 | } |
| 121 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 122 | p_handle->rhandle = rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 123 | return IPC_SUCCESS; |
| 124 | } |
| 125 | |
| 126 | void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service, |
| 127 | psa_handle_t conn_handle) |
| 128 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 129 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 130 | |
| 131 | TFM_ASSERT(service); |
| 132 | /* Get reverse handle value only be allowed for a connected handle */ |
| 133 | TFM_ASSERT(conn_handle != PSA_NULL_HANDLE); |
| 134 | |
| 135 | /* There are many handles for each RoT Service */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 136 | p_handle = tfm_spm_find_conn_handle_node(service, conn_handle); |
| 137 | if (!p_handle) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 138 | tfm_panic(); |
| 139 | } |
| 140 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 141 | return p_handle->rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* Partition management functions */ |
| 145 | struct tfm_spm_service_t * |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 146 | tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition, |
| 147 | psa_signal_t signal) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 148 | { |
| 149 | struct tfm_list_node_t *node, *head; |
| 150 | struct tfm_spm_service_t *service; |
| 151 | |
| 152 | TFM_ASSERT(partition); |
| 153 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 154 | if (tfm_list_is_empty(&partition->runtime_data.service_list)) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 155 | tfm_panic(); |
| 156 | } |
| 157 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 158 | head = &partition->runtime_data.service_list; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 159 | TFM_LIST_FOR_EACH(node, head) { |
| 160 | service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list); |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 161 | if (service->service_db->signal == signal) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 162 | return service; |
| 163 | } |
| 164 | } |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid) |
| 169 | { |
| 170 | uint32_t i; |
| 171 | struct tfm_list_node_t *node, *head; |
| 172 | struct tfm_spm_service_t *service; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 173 | struct spm_partition_desc_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 174 | |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 175 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 176 | partition = &g_spm_partition_db.partitions[i]; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 177 | /* Skip partition without IPC flag */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 178 | if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 179 | continue; |
| 180 | } |
| 181 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 182 | if (tfm_list_is_empty(&partition->runtime_data.service_list)) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 183 | continue; |
| 184 | } |
| 185 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 186 | head = &partition->runtime_data.service_list; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 187 | TFM_LIST_FOR_EACH(node, head) { |
| 188 | service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, |
| 189 | list); |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 190 | if (service->service_db->sid == sid) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 191 | return service; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | struct tfm_spm_service_t * |
| 199 | tfm_spm_get_service_by_handle(psa_handle_t conn_handle) |
| 200 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 201 | return ((struct tfm_conn_handle_t *)conn_handle)->service; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 204 | struct spm_partition_desc_t *tfm_spm_get_partition_by_id(int32_t partition_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 205 | { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 206 | uint32_t idx = get_partition_idx(partition_id); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 207 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 208 | if (idx != SPM_INVALID_PARTITION_IDX) { |
| 209 | return &(g_spm_partition_db.partitions[idx]); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 210 | } |
| 211 | return NULL; |
| 212 | } |
| 213 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 214 | struct spm_partition_desc_t *tfm_spm_get_running_partition(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 215 | { |
| 216 | uint32_t spid; |
| 217 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 218 | spid = tfm_spm_partition_get_running_partition_id(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 219 | |
| 220 | return tfm_spm_get_partition_by_id(spid); |
| 221 | } |
| 222 | |
| 223 | int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service, |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 224 | uint32_t version) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 225 | { |
| 226 | TFM_ASSERT(service); |
| 227 | |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 228 | switch (service->service_db->version_policy) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 229 | case TFM_VERSION_POLICY_RELAXED: |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 230 | if (version > service->service_db->version) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 231 | return IPC_ERROR_VERSION; |
| 232 | } |
| 233 | break; |
| 234 | case TFM_VERSION_POLICY_STRICT: |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 235 | if (version != service->service_db->version) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 236 | return IPC_ERROR_VERSION; |
| 237 | } |
| 238 | break; |
| 239 | default: |
| 240 | return IPC_ERROR_VERSION; |
| 241 | } |
| 242 | return IPC_SUCCESS; |
| 243 | } |
| 244 | |
| 245 | /* Message functions */ |
| 246 | struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle) |
| 247 | { |
| 248 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 249 | * The message handler passed by the caller is considered invalid in the |
| 250 | * following cases: |
| 251 | * 1. Not a valid message handle. (The address of a message is not the |
| 252 | * address of a possible handle from the pool |
| 253 | * 2. Handle not belongs to the caller partition (The handle is either |
| 254 | * unused, or owned by anither partition) |
| 255 | * Check the conditions above |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 256 | */ |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 257 | struct tfm_conn_handle_t *connection_handle_address; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 258 | struct tfm_msg_body_t *msg; |
| 259 | uint32_t partition_id; |
| 260 | |
| 261 | msg = (struct tfm_msg_body_t *)msg_handle; |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 262 | |
| 263 | connection_handle_address = |
| 264 | TFM_GET_CONTAINER_PTR(msg, struct tfm_conn_handle_t, internal_msg); |
| 265 | |
| 266 | if (is_valid_chunk_data_in_pool( |
| 267 | conn_handle_pool, (uint8_t *)connection_handle_address) != 1) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 272 | * Check that the magic number is correct. This proves that the message |
| 273 | * structure contains an active message. |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 274 | */ |
| 275 | if (msg->magic != TFM_MSG_MAGIC) { |
| 276 | return NULL; |
| 277 | } |
| 278 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 279 | /* Check that the running partition owns the message */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 280 | partition_id = tfm_spm_partition_get_running_partition_id(); |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 281 | if (partition_id != msg->service->partition->static_data->partition_id) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
| 284 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 285 | /* |
| 286 | * FixMe: For condition 1 it should be checked whether the message belongs |
| 287 | * to the service. Skipping this check isn't a security risk as even if the |
| 288 | * message belongs to another service, the handle belongs to the calling |
| 289 | * partition. |
| 290 | */ |
| 291 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 292 | return msg; |
| 293 | } |
| 294 | |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 295 | struct tfm_msg_body_t * |
| 296 | tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 297 | { |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 298 | TFM_ASSERT(conn_handle != PSA_NULL_HANDLE); |
| 299 | |
| 300 | return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg); |
| 301 | } |
| 302 | |
| 303 | void tfm_spm_fill_msg(struct tfm_msg_body_t *msg, |
| 304 | struct tfm_spm_service_t *service, |
| 305 | psa_handle_t handle, |
| 306 | int32_t type, int32_t ns_caller, |
| 307 | psa_invec *invec, size_t in_len, |
| 308 | psa_outvec *outvec, size_t out_len, |
| 309 | psa_outvec *caller_outvec) |
| 310 | { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 311 | uint32_t i; |
| 312 | |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 313 | TFM_ASSERT(msg); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 314 | TFM_ASSERT(service); |
| 315 | TFM_ASSERT(!(invec == NULL && in_len != 0)); |
| 316 | TFM_ASSERT(!(outvec == NULL && out_len != 0)); |
| 317 | TFM_ASSERT(in_len <= PSA_MAX_IOVEC); |
| 318 | TFM_ASSERT(out_len <= PSA_MAX_IOVEC); |
| 319 | TFM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC); |
| 320 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 321 | /* Clear message buffer before using it */ |
Mingyang Sun | 94b1b41 | 2019-09-20 15:11:14 +0800 | [diff] [blame] | 322 | tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 323 | |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 324 | tfm_event_init(&msg->ack_evnt); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 325 | msg->magic = TFM_MSG_MAGIC; |
| 326 | msg->service = service; |
| 327 | msg->handle = handle; |
| 328 | msg->caller_outvec = caller_outvec; |
| 329 | /* Get current partition id */ |
| 330 | if (ns_caller) { |
| 331 | msg->msg.client_id = tfm_nspm_get_current_client_id(); |
| 332 | } else { |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 333 | msg->msg.client_id = tfm_spm_partition_get_running_partition_id(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Copy contents */ |
| 337 | msg->msg.type = type; |
| 338 | |
| 339 | for (i = 0; i < in_len; i++) { |
| 340 | msg->msg.in_size[i] = invec[i].len; |
| 341 | msg->invec[i].base = invec[i].base; |
| 342 | } |
| 343 | |
| 344 | for (i = 0; i < out_len; i++) { |
| 345 | msg->msg.out_size[i] = outvec[i].len; |
| 346 | msg->outvec[i].base = outvec[i].base; |
| 347 | /* Out len is used to record the writed number, set 0 here again */ |
| 348 | msg->outvec[i].len = 0; |
| 349 | } |
| 350 | |
| 351 | /* Use message address as handle */ |
| 352 | msg->msg.handle = (psa_handle_t)msg; |
| 353 | |
| 354 | /* For connected handle, set rhandle to every message */ |
| 355 | if (handle != PSA_NULL_HANDLE) { |
| 356 | msg->msg.rhandle = tfm_spm_get_rhandle(service, handle); |
| 357 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | int32_t tfm_spm_send_event(struct tfm_spm_service_t *service, |
| 361 | struct tfm_msg_body_t *msg) |
| 362 | { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 363 | struct spm_partition_runtime_data_t *p_runtime_data = |
| 364 | &service->partition->runtime_data; |
| 365 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 366 | TFM_ASSERT(service); |
| 367 | TFM_ASSERT(msg); |
| 368 | |
| 369 | /* Enqueue message to service message queue */ |
| 370 | if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) { |
| 371 | return IPC_ERROR_GENERIC; |
| 372 | } |
| 373 | |
| 374 | /* Messages put. Update signals */ |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 375 | p_runtime_data->signals |= service->service_db->signal; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 376 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 377 | tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals & |
| 378 | p_runtime_data->signal_mask)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 379 | |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 380 | /* |
| 381 | * If it is a NS request via RPC, it is unnecessary to block current |
| 382 | * thread. |
| 383 | */ |
| 384 | if (!is_tfm_rpc_msg(msg)) { |
| 385 | tfm_event_wait(&msg->ack_evnt); |
| 386 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 387 | |
| 388 | return IPC_SUCCESS; |
| 389 | } |
| 390 | |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 391 | uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx) |
| 392 | { |
| 393 | return g_spm_partition_db.partitions[partition_idx]. |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 394 | memory_data->stack_bottom; |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx) |
| 398 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 399 | return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top; |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 400 | } |
| 401 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 402 | uint32_t tfm_spm_partition_get_running_partition_id(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 403 | { |
| 404 | struct tfm_thrd_ctx *pth = tfm_thrd_curr_thread(); |
| 405 | struct spm_partition_desc_t *partition; |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 406 | struct spm_partition_runtime_data_t *r_data; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 407 | |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 408 | r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t, |
| 409 | sp_thrd); |
| 410 | partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t, |
| 411 | runtime_data); |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 412 | return partition->static_data->partition_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static struct tfm_thrd_ctx * |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 416 | tfm_spm_partition_get_thread_info(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 417 | { |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 418 | return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 419 | } |
| 420 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 421 | static tfm_thrd_func_t |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 422 | tfm_spm_partition_get_init_func(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 423 | { |
| 424 | return (tfm_thrd_func_t)(g_spm_partition_db.partitions[partition_idx]. |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 425 | static_data->partition_init); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 426 | } |
| 427 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 428 | static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 429 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 430 | return g_spm_partition_db.partitions[partition_idx].static_data-> |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 431 | partition_priority; |
| 432 | } |
| 433 | |
David Hu | cb05d97 | 2019-08-06 18:10:11 +0800 | [diff] [blame] | 434 | int32_t tfm_memory_check(const void *buffer, size_t len, int32_t ns_caller, |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 435 | enum tfm_memory_access_e access, |
| 436 | uint32_t privileged) |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 437 | { |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 438 | enum tfm_status_e err; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 439 | |
| 440 | /* If len is zero, this indicates an empty buffer and base is ignored */ |
| 441 | if (len == 0) { |
| 442 | return IPC_SUCCESS; |
| 443 | } |
| 444 | |
| 445 | if (!buffer) { |
| 446 | return IPC_ERROR_BAD_PARAMETERS; |
| 447 | } |
| 448 | |
| 449 | if ((uintptr_t)buffer > (UINTPTR_MAX - len)) { |
| 450 | return IPC_ERROR_MEMORY_CHECK; |
| 451 | } |
| 452 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 453 | if (access == TFM_MEMORY_ACCESS_RW) { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 454 | err = tfm_core_has_write_access_to_region(buffer, len, ns_caller, |
| 455 | privileged); |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 456 | } else { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 457 | err = tfm_core_has_read_access_to_region(buffer, len, ns_caller, |
| 458 | privileged); |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 459 | } |
Summer Qin | 0fc3f59 | 2019-04-11 16:00:10 +0800 | [diff] [blame] | 460 | if (err == TFM_SUCCESS) { |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 461 | return IPC_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | return IPC_ERROR_MEMORY_CHECK; |
| 465 | } |
| 466 | |
Summer Qin | 75f0d75 | 2019-08-27 14:38:20 +0800 | [diff] [blame] | 467 | uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags) |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 468 | { |
Summer Qin | 75f0d75 | 2019-08-27 14:38:20 +0800 | [diff] [blame] | 469 | if (partition_flags & SPM_PART_FLAG_PSA_ROT) { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 470 | return TFM_PARTITION_PRIVILEGED_MODE; |
| 471 | } else { |
| 472 | return TFM_PARTITION_UNPRIVILEGED_MODE; |
| 473 | } |
| 474 | } |
| 475 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 476 | /********************** SPM functions for thread mode ************************/ |
| 477 | |
| 478 | void tfm_spm_init(void) |
| 479 | { |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 480 | uint32_t i, j, num; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 481 | struct spm_partition_desc_t *partition; |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 482 | struct tfm_thrd_ctx *pth, this_thrd; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 483 | |
| 484 | tfm_pool_init(conn_handle_pool, |
| 485 | POOL_BUFFER_SIZE(conn_handle_pool), |
| 486 | sizeof(struct tfm_conn_handle_t), |
| 487 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 488 | |
| 489 | /* Init partition first for it will be used when init service */ |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 490 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 491 | partition = &g_spm_partition_db.partitions[i]; |
Edison Ai | f050170 | 2019-10-11 14:36:42 +0800 | [diff] [blame^] | 492 | |
| 493 | /* Check if the PSA framework version matches. */ |
| 494 | if (partition->static_data->psa_framework_version != |
| 495 | PSA_FRAMEWORK_VERSION) { |
| 496 | ERROR_MSG("Warning: PSA Framework Verison is not matched!"); |
| 497 | continue; |
| 498 | } |
| 499 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 500 | tfm_spm_hal_configure_default_isolation(partition->platform_data); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 501 | if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) { |
| 502 | continue; |
| 503 | } |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 504 | |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 505 | /* TODO: This can be optimized by generating the assigned signal |
| 506 | * in code generation time. |
| 507 | */ |
| 508 | for (j = 0; j < tfm_core_irq_signals_count; ++j) { |
| 509 | if (tfm_core_irq_signals[j].partition_id == |
| 510 | partition->static_data->partition_id) { |
| 511 | partition->runtime_data.assigned_signals |= |
| 512 | tfm_core_irq_signals[j].signal_value; |
| 513 | } |
| 514 | } |
| 515 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 516 | tfm_event_init(&partition->runtime_data.signal_evnt); |
| 517 | tfm_list_init(&partition->runtime_data.service_list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 518 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 519 | pth = tfm_spm_partition_get_thread_info(i); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 520 | if (!pth) { |
| 521 | tfm_panic(); |
| 522 | } |
| 523 | |
| 524 | tfm_thrd_init(pth, |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 525 | tfm_spm_partition_get_init_func(i), |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 526 | NULL, |
Ken Liu | 5a2b905 | 2019-08-15 19:03:29 +0800 | [diff] [blame] | 527 | (uintptr_t)tfm_spm_partition_get_stack_top(i), |
| 528 | (uintptr_t)tfm_spm_partition_get_stack_bottom(i)); |
Edison Ai | 788bae2 | 2019-02-18 17:38:59 +0800 | [diff] [blame] | 529 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 530 | pth->prior = tfm_spm_partition_get_priority(i); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 531 | |
| 532 | /* Kick off */ |
| 533 | if (tfm_thrd_start(pth) != THRD_SUCCESS) { |
| 534 | tfm_panic(); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /* Init Service */ |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 539 | num = sizeof(service) / sizeof(struct tfm_spm_service_t); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 540 | for (i = 0; i < num; i++) { |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 541 | service[i].service_db = &service_db[i]; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 542 | partition = |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 543 | tfm_spm_get_partition_by_id(service[i].service_db->partition_id); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 544 | if (!partition) { |
| 545 | tfm_panic(); |
| 546 | } |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 547 | service[i].partition = partition; |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 548 | partition->runtime_data.assigned_signals |= service->service_db->signal; |
| 549 | |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 550 | tfm_list_init(&service[i].handle_list); |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 551 | tfm_list_add_tail(&partition->runtime_data.service_list, |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 552 | &service[i].list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 553 | } |
| 554 | |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 555 | /* |
| 556 | * All threads initialized, start the scheduler. |
| 557 | * |
| 558 | * NOTE: |
| 559 | * Here is the booting privileged thread mode, and will never |
| 560 | * return to this place after scheduler is started. The start |
| 561 | * function has to save current runtime context to act as a |
| 562 | * 'current thread' to avoid repeating NULL 'current thread' |
| 563 | * checking while context switching. This saved context is worthy |
| 564 | * of being saved somewhere if there are potential usage purpose. |
| 565 | * Let's save this context in a local variable 'this_thrd' at |
| 566 | * current since there is no usage for it. |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 567 | * Also set tfm_nspm_thread_entry as pfn for this thread to |
| 568 | * use in detecting NS/S thread scheduling changes. |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 569 | */ |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 570 | this_thrd.pfn = (tfm_thrd_func_t)tfm_nspm_thread_entry; |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 571 | tfm_thrd_start_scheduler(&this_thrd); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 572 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 573 | |
| 574 | void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb) |
| 575 | { |
| 576 | #if TFM_LVL == 2 |
| 577 | struct spm_partition_desc_t *p_next_partition; |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 578 | struct spm_partition_runtime_data_t *r_data; |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 579 | uint32_t is_privileged; |
| 580 | #endif |
| 581 | struct tfm_thrd_ctx *pth_next = tfm_thrd_next_thread(); |
| 582 | struct tfm_thrd_ctx *pth_curr = tfm_thrd_curr_thread(); |
| 583 | |
Mate Toth-Pal | 32b2ccd | 2019-04-26 10:00:16 +0200 | [diff] [blame] | 584 | if (pth_next != NULL && pth_curr != pth_next) { |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 585 | #if TFM_LVL == 2 |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 586 | r_data = TFM_GET_CONTAINER_PTR(pth_next, |
| 587 | struct spm_partition_runtime_data_t, |
| 588 | sp_thrd); |
| 589 | p_next_partition = TFM_GET_CONTAINER_PTR(r_data, |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 590 | struct spm_partition_desc_t, |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 591 | runtime_data); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 592 | |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 593 | if (p_next_partition->static_data->partition_flags & |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 594 | SPM_PART_FLAG_PSA_ROT) { |
| 595 | is_privileged = TFM_PARTITION_PRIVILEGED_MODE; |
| 596 | } else { |
| 597 | is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE; |
| 598 | } |
| 599 | |
| 600 | tfm_spm_partition_change_privilege(is_privileged); |
| 601 | #endif |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 602 | /* Increase the secure lock, if we enter secure from non-secure */ |
| 603 | if ((void *)pth_curr->pfn == (void *)tfm_nspm_thread_entry) { |
| 604 | ++tfm_secure_lock; |
| 605 | } |
| 606 | /* Decrease the secure lock, if we return from secure to non-secure */ |
| 607 | if ((void *)pth_next->pfn == (void *)tfm_nspm_thread_entry) { |
| 608 | --tfm_secure_lock; |
| 609 | } |
| 610 | |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 611 | tfm_thrd_context_switch(ctxb, pth_curr, pth_next); |
| 612 | } |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | * Handle pending mailbox message from NS in multi-core topology. |
| 616 | * Empty operation on single Armv8-M platform. |
| 617 | */ |
| 618 | tfm_rpc_client_call_handler(); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 619 | } |