Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* All the APIs defined in this file are used for library model. */ |
| 9 | |
| 10 | #include <inttypes.h> |
| 11 | #include <limits.h> |
| 12 | #include <stdbool.h> |
| 13 | #include <stdlib.h> |
| 14 | #include "tfm_utils.h" |
| 15 | #include "tfm_spm_hal.h" |
| 16 | #include "spm_api.h" |
| 17 | #include "spm_db.h" |
| 18 | #include "region_defs.h" |
| 19 | #include "tfm_nspm.h" |
| 20 | #include "tfm_memory_utils.h" |
| 21 | #include "tfm_internal.h" |
| 22 | |
| 23 | extern struct spm_partition_db_t g_spm_partition_db; |
| 24 | |
| 25 | typedef enum { |
| 26 | TFM_INIT_FAILURE, |
| 27 | } sp_error_type_t; |
| 28 | |
| 29 | /* |
| 30 | * This function is called when a secure partition causes an error. |
| 31 | * In case of an error in the error handling, a non-zero value have to be |
| 32 | * returned. |
| 33 | */ |
| 34 | static void tfm_spm_partition_err_handler( |
| 35 | const struct spm_partition_desc_t *partition, |
| 36 | sp_error_type_t err_type, |
| 37 | int32_t err_code) |
| 38 | { |
| 39 | #ifdef TFM_CORE_DEBUG |
| 40 | if (err_type == TFM_INIT_FAILURE) { |
| 41 | printf("Partition init failed for partition id 0x%08X\r\n", |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 42 | partition->static_data->partition_id); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 43 | } else { |
| 44 | printf( |
| 45 | "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n", |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 46 | err_type, err_code, partition->static_data->partition_id); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 47 | } |
| 48 | #else |
| 49 | (void)err_type; |
| 50 | (void)err_code; |
| 51 | #endif |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 52 | tfm_spm_partition_set_state(partition->static_data->partition_id, |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 53 | SPM_PARTITION_STATE_CLOSED); |
| 54 | } |
| 55 | |
| 56 | enum spm_err_t tfm_spm_partition_init(void) |
| 57 | { |
| 58 | struct spm_partition_desc_t *part; |
| 59 | struct tfm_sfn_req_s desc; |
| 60 | int32_t args[4] = {0}; |
| 61 | int32_t fail_cnt = 0; |
| 62 | uint32_t idx; |
| 63 | |
| 64 | /* Call the init function for each partition */ |
| 65 | for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) { |
| 66 | part = &g_spm_partition_db.partitions[idx]; |
| 67 | tfm_spm_hal_configure_default_isolation(part->platform_data); |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 68 | if (part->static_data->partition_init == NULL) { |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 69 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
| 70 | tfm_spm_partition_set_caller_partition_idx(idx, |
| 71 | SPM_INVALID_PARTITION_IDX); |
| 72 | } else { |
| 73 | int32_t res; |
| 74 | |
| 75 | desc.args = args; |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame^] | 76 | desc.ns_caller = false; |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 77 | desc.sfn = (sfn_t)part->static_data->partition_init; |
| 78 | desc.sp_id = part->static_data->partition_id; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 79 | res = tfm_core_sfn_request(&desc); |
| 80 | if (res == TFM_SUCCESS) { |
| 81 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
| 82 | } else { |
| 83 | tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res); |
| 84 | fail_cnt++; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | tfm_secure_api_init_done(); |
| 90 | |
| 91 | if (fail_cnt == 0) { |
| 92 | return SPM_ERR_OK; |
| 93 | } else { |
| 94 | return SPM_ERR_PARTITION_NOT_AVAILABLE; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx) |
| 99 | { |
| 100 | struct spm_partition_runtime_data_t *runtime_data = |
| 101 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 102 | struct interrupted_ctx_stack_frame_t *stack_frame = |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 103 | (struct interrupted_ctx_stack_frame_t *)runtime_data->ctx_stack_ptr; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 104 | |
| 105 | stack_frame->partition_state = runtime_data->partition_state; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx) |
| 109 | { |
| 110 | struct spm_partition_runtime_data_t *runtime_data = |
| 111 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 112 | struct interrupted_ctx_stack_frame_t *stack_frame; |
| 113 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 114 | stack_frame = (struct interrupted_ctx_stack_frame_t *) |
| 115 | runtime_data->ctx_stack_ptr; |
| 116 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 117 | stack_frame->partition_state = 0; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx) |
| 121 | { |
| 122 | struct spm_partition_runtime_data_t *runtime_data = |
| 123 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 124 | struct handler_ctx_stack_frame_t *stack_frame = |
| 125 | (struct handler_ctx_stack_frame_t *) |
| 126 | runtime_data->ctx_stack_ptr; |
| 127 | |
| 128 | stack_frame->partition_state = runtime_data->partition_state; |
| 129 | stack_frame->caller_partition_idx = runtime_data->caller_partition_idx; |
| 130 | |
| 131 | runtime_data->ctx_stack_ptr += |
| 132 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 133 | } |
| 134 | |
| 135 | void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx) |
| 136 | { |
| 137 | struct spm_partition_runtime_data_t *runtime_data = |
| 138 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 139 | struct handler_ctx_stack_frame_t *stack_frame; |
| 140 | |
| 141 | runtime_data->ctx_stack_ptr -= |
| 142 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 143 | |
| 144 | stack_frame = (struct handler_ctx_stack_frame_t *) |
| 145 | runtime_data->ctx_stack_ptr; |
| 146 | |
| 147 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 148 | stack_frame->partition_state = 0; |
| 149 | tfm_spm_partition_set_caller_partition_idx( |
| 150 | partition_idx, stack_frame->caller_partition_idx); |
| 151 | stack_frame->caller_partition_idx = 0; |
| 152 | } |
| 153 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 154 | void tfm_spm_partition_store_context(uint32_t partition_idx, |
| 155 | uint32_t stack_ptr, uint32_t lr) |
| 156 | { |
| 157 | g_spm_partition_db.partitions[partition_idx]. |
| 158 | runtime_data.stack_ptr = stack_ptr; |
| 159 | g_spm_partition_db.partitions[partition_idx]. |
| 160 | runtime_data.lr = lr; |
| 161 | } |
| 162 | |
| 163 | const struct spm_partition_runtime_data_t * |
| 164 | tfm_spm_partition_get_runtime_data(uint32_t partition_idx) |
| 165 | { |
| 166 | return &(g_spm_partition_db.partitions[partition_idx].runtime_data); |
| 167 | } |
| 168 | |
| 169 | void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state) |
| 170 | { |
| 171 | g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state = |
| 172 | state; |
| 173 | if (state == SPM_PARTITION_STATE_RUNNING || |
| 174 | state == SPM_PARTITION_STATE_HANDLING_IRQ) { |
| 175 | g_spm_partition_db.running_partition_idx = partition_idx; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx, |
| 180 | uint32_t caller_partition_idx) |
| 181 | { |
| 182 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 183 | caller_partition_idx = caller_partition_idx; |
| 184 | } |
| 185 | |
| 186 | void tfm_spm_partition_set_signal_mask(uint32_t partition_idx, |
| 187 | uint32_t signal_mask) |
| 188 | { |
| 189 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 190 | signal_mask = signal_mask; |
| 191 | } |
| 192 | |
| 193 | void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx, |
| 194 | int32_t caller_client_id) |
| 195 | { |
| 196 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 197 | caller_client_id = caller_client_id; |
| 198 | } |
| 199 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 200 | enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx, |
| 201 | const int32_t *args) |
| 202 | { |
| 203 | struct spm_partition_runtime_data_t *runtime_data = |
| 204 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 205 | size_t i; |
| 206 | |
| 207 | if ((args[1] < 0) || (args[3] < 0)) { |
| 208 | return SPM_ERR_INVALID_PARAMETER; |
| 209 | } |
| 210 | |
| 211 | runtime_data->iovec_args.in_len = (size_t)args[1]; |
| 212 | for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) { |
| 213 | runtime_data->iovec_args.in_vec[i].base = |
| 214 | ((psa_invec *)args[0])[i].base; |
| 215 | runtime_data->iovec_args.in_vec[i].len = ((psa_invec *)args[0])[i].len; |
| 216 | } |
| 217 | runtime_data->iovec_args.out_len = (size_t)args[3]; |
| 218 | for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) { |
| 219 | runtime_data->iovec_args.out_vec[i].base = |
| 220 | ((psa_outvec *)args[2])[i].base; |
| 221 | runtime_data->iovec_args.out_vec[i].len = |
| 222 | ((psa_outvec *)args[2])[i].len; |
| 223 | } |
| 224 | runtime_data->orig_outvec = (psa_outvec *)args[2]; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 225 | |
| 226 | return SPM_ERR_OK; |
| 227 | } |
| 228 | |
| 229 | uint32_t tfm_spm_partition_get_running_partition_idx(void) |
| 230 | { |
| 231 | return g_spm_partition_db.running_partition_idx; |
| 232 | } |
| 233 | |
| 234 | void tfm_spm_partition_cleanup_context(uint32_t partition_idx) |
| 235 | { |
| 236 | struct spm_partition_desc_t *partition = |
| 237 | &(g_spm_partition_db.partitions[partition_idx]); |
| 238 | int32_t i; |
| 239 | |
| 240 | partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 241 | partition->runtime_data.iovec_args.in_len = 0; |
| 242 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 243 | partition->runtime_data.iovec_args.in_vec[i].base = 0; |
| 244 | partition->runtime_data.iovec_args.in_vec[i].len = 0; |
| 245 | } |
| 246 | partition->runtime_data.iovec_args.out_len = 0; |
| 247 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 248 | partition->runtime_data.iovec_args.out_vec[i].base = 0; |
| 249 | partition->runtime_data.iovec_args.out_vec[i].len = 0; |
| 250 | } |
| 251 | partition->runtime_data.orig_outvec = 0; |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 252 | } |