Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Tamas Ban | 8bd24b7 | 2019-02-19 12:13:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, Arm Limited. All rights reserved. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 8 | /* This file contains the APIs exported by the SPM to tfm core */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 9 | |
| 10 | #include <stdio.h> |
Mate Toth-Pal | 7345a4b | 2018-03-08 16:10:28 +0100 | [diff] [blame] | 11 | #include <string.h> |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 12 | #include "spm_api.h" |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 13 | #include "tfm_spm_hal.h" |
Tamas Ban | 8bd24b7 | 2019-02-19 12:13:13 +0000 | [diff] [blame] | 14 | #include "tfm_memory_utils.h" |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 15 | #include "spm_db.h" |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 16 | #include "tfm_internal.h" |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 17 | #include "tfm_api.h" |
Mate Toth-Pal | ce61afa | 2018-08-03 13:51:01 +0200 | [diff] [blame] | 18 | #include "tfm_nspm.h" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 19 | #include "secure_fw/core/tfm_core.h" |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 20 | #include "tfm_peripherals_def.h" |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 21 | #include "spm_partition_defs.h" |
| 22 | |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 23 | #define NON_SECURE_INTERNAL_PARTITION_DB_IDX 0 |
| 24 | #define TFM_CORE_INTERNAL_PARTITION_DB_IDX 1 |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 25 | |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 26 | #ifndef TFM_PSA_API |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 27 | typedef enum { |
| 28 | TFM_INIT_FAILURE, |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 29 | } sp_error_type_t; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 30 | |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 31 | /* The size of this struct must be multiple of 4 bytes as it is stacked to an |
| 32 | * uint32_t[] array |
| 33 | */ |
| 34 | struct interrupted_ctx_stack_frame_t { |
| 35 | #if TFM_LVL != 1 |
| 36 | uint32_t stack_ptr; |
| 37 | #endif |
| 38 | uint32_t partition_state; |
| 39 | }; |
| 40 | |
| 41 | /* The size of this struct must be multiple of 4 bytes as it is stacked to an |
| 42 | * uint32_t[] array |
| 43 | */ |
| 44 | struct handler_ctx_stack_frame_t { |
| 45 | uint32_t partition_state; |
| 46 | uint32_t caller_partition_idx; |
| 47 | }; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 48 | #endif /* !defined(TFM_PSA_API) */ |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 49 | |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 50 | /* Define SPM DB structure */ |
| 51 | #include "secure_fw/services/tfm_spm_db.inc" |
| 52 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 53 | /* |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 54 | * This function is called when a secure partition causes an error. |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 55 | * In case of an error in the error handling, a non-zero value have to be |
| 56 | * returned. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 57 | */ |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 58 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 59 | static void tfm_spm_partition_err_handler( |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 60 | const struct spm_partition_desc_t *partition, |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 61 | sp_error_type_t err_type, |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 62 | int32_t err_code) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 63 | { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 64 | #ifdef TFM_CORE_DEBUG |
| 65 | if (err_type == TFM_INIT_FAILURE) { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 66 | printf("Partition init failed for partition id 0x%08X\r\n", |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 67 | partition->static_data.partition_id); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 68 | } else { |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 69 | printf( |
| 70 | "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n", |
| 71 | err_type, err_code, partition->static_data.partition_id); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 72 | } |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 73 | #else |
| 74 | (void)err_type; |
| 75 | (void)err_code; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 76 | #endif |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 77 | tfm_spm_partition_set_state(partition->static_data.partition_id, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 78 | SPM_PARTITION_STATE_CLOSED); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 79 | } |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 80 | #endif /* !defined(TFM_PSA_API) */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 81 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 82 | uint32_t get_partition_idx(uint32_t partition_id) |
| 83 | { |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 84 | uint32_t i; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 85 | |
| 86 | if (partition_id == INVALID_PARTITION_ID) { |
| 87 | return SPM_INVALID_PARTITION_IDX; |
| 88 | } |
| 89 | |
| 90 | for (i = 0; i < g_spm_partition_db.partition_count; ++i) { |
| 91 | if (g_spm_partition_db.partitions[i].static_data.partition_id == |
| 92 | partition_id) { |
| 93 | return i; |
| 94 | } |
| 95 | } |
| 96 | return SPM_INVALID_PARTITION_IDX; |
| 97 | } |
| 98 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 99 | enum spm_err_t tfm_spm_db_init(void) |
| 100 | { |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 101 | struct spm_partition_desc_t *part_ptr; |
| 102 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 103 | static uint32_t ns_interrupt_ctx_stack[ |
| 104 | sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0}; |
| 105 | static uint32_t tfm_core_interrupt_ctx_stack[ |
| 106 | sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0}; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 107 | #endif /* !defined(TFM_PSA_API) */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 108 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 109 | /* This function initialises partition db */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 110 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 111 | /* There are a few partitions that are used by TF-M internally. |
| 112 | * These are explicitly added to the partition db here. |
| 113 | */ |
| 114 | |
| 115 | /* For the non secure Execution environment */ |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 116 | #if (TFM_LVL != 1) || defined(TFM_PSA_API) |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 117 | extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[]; |
| 118 | extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[]; |
| 119 | uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base; |
| 120 | uint32_t psp_stack_top = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit; |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 121 | #endif |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 122 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 123 | part_ptr = &(g_spm_partition_db.partitions[ |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 124 | NON_SECURE_INTERNAL_PARTITION_DB_IDX]); |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 125 | part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID; |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 126 | #ifdef TFM_PSA_API |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 127 | part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT | |
| 128 | SPM_PART_FLAG_IPC; |
| 129 | part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW; |
| 130 | part_ptr->static_data.partition_init = tfm_nspm_thread_entry; |
| 131 | #else |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 132 | part_ptr->static_data.partition_flags = 0; |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 133 | #endif |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 134 | |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 135 | #if (TFM_LVL != 1) || defined(TFM_PSA_API) |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 136 | part_ptr->memory_data.stack_bottom = psp_stack_bottom; |
| 137 | part_ptr->memory_data.stack_top = psp_stack_top; |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 138 | /* Since RW, ZI and stack are configured as one MPU region, configure |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 139 | * RW start address to psp_stack_bottom to get RW access to stack |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 140 | */ |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 141 | part_ptr->memory_data.rw_start = psp_stack_bottom; |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 142 | #endif |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 143 | |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 144 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 145 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 146 | part_ptr->runtime_data.ctx_stack_ptr = ns_interrupt_ctx_stack; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 147 | #endif /* !defined(TFM_PSA_API) */ |
| 148 | |
Miklos Balint | 12735bc | 2018-08-01 15:45:18 +0200 | [diff] [blame] | 149 | tfm_nspm_configure_clients(); |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 150 | |
| 151 | /* For the TF-M core environment itself */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 152 | part_ptr = &(g_spm_partition_db.partitions[ |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 153 | TFM_CORE_INTERNAL_PARTITION_DB_IDX]); |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 154 | part_ptr->static_data.partition_id = TFM_SP_CORE_ID; |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 155 | part_ptr->static_data.partition_flags = |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 156 | SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 157 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 158 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 159 | part_ptr->runtime_data.ctx_stack_ptr = tfm_core_interrupt_ctx_stack; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 160 | #endif /* !defined(TFM_PSA_API) */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 161 | |
Mate Toth-Pal | 7345a4b | 2018-03-08 16:10:28 +0100 | [diff] [blame] | 162 | g_spm_partition_db.is_init = 1; |
| 163 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 164 | return SPM_ERR_OK; |
| 165 | } |
| 166 | |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 167 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 168 | enum spm_err_t tfm_spm_partition_init(void) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 169 | { |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 170 | struct spm_partition_desc_t *part; |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 171 | struct tfm_sfn_req_s desc; |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 172 | int32_t args[4] = {0}; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 173 | int32_t fail_cnt = 0; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 174 | uint32_t idx; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 175 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 176 | /* Call the init function for each partition */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 177 | for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) { |
| 178 | part = &g_spm_partition_db.partitions[idx]; |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 179 | tfm_spm_hal_configure_default_isolation(part->platform_data); |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 180 | if (part->static_data.partition_init == NULL) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 181 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 182 | tfm_spm_partition_set_caller_partition_idx(idx, |
| 183 | SPM_INVALID_PARTITION_IDX); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 184 | } else { |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 185 | int32_t res; |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 186 | |
| 187 | desc.args = args; |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 188 | desc.ns_caller = 0U; |
Mate Toth-Pal | b8ce0dd | 2018-07-25 10:18:34 +0200 | [diff] [blame] | 189 | desc.iovec_api = TFM_SFN_API_IOVEC; |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 190 | desc.sfn = (sfn_t)part->static_data.partition_init; |
| 191 | desc.sp_id = part->static_data.partition_id; |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 192 | res = tfm_core_sfn_request(&desc); |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 193 | if (res == (int32_t)TFM_SUCCESS) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 194 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 195 | } else { |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 196 | tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 197 | fail_cnt++; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 202 | tfm_secure_api_init_done(); |
| 203 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 204 | if (fail_cnt == 0) { |
| 205 | return SPM_ERR_OK; |
| 206 | } else { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 207 | return SPM_ERR_PARTITION_NOT_AVAILABLE; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 210 | |
| 211 | void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx) |
| 212 | { |
| 213 | struct spm_partition_runtime_data_t *runtime_data = |
| 214 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 215 | struct interrupted_ctx_stack_frame_t *stack_frame = |
| 216 | (struct interrupted_ctx_stack_frame_t *) |
| 217 | runtime_data->ctx_stack_ptr; |
| 218 | |
| 219 | stack_frame->partition_state = runtime_data->partition_state; |
| 220 | #if TFM_LVL != 1 |
| 221 | stack_frame->stack_ptr = runtime_data->stack_ptr; |
| 222 | #endif |
| 223 | runtime_data->ctx_stack_ptr += |
| 224 | sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t); |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx) |
| 228 | { |
| 229 | struct spm_partition_runtime_data_t *runtime_data = |
| 230 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 231 | struct interrupted_ctx_stack_frame_t *stack_frame; |
| 232 | |
| 233 | runtime_data->ctx_stack_ptr -= |
| 234 | sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t); |
| 235 | stack_frame = (struct interrupted_ctx_stack_frame_t *) |
| 236 | runtime_data->ctx_stack_ptr; |
| 237 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 238 | stack_frame->partition_state = 0; |
| 239 | #if TFM_LVL != 1 |
| 240 | tfm_spm_partition_set_stack(partition_idx, stack_frame->stack_ptr); |
| 241 | stack_frame->stack_ptr = 0; |
| 242 | #endif |
| 243 | } |
| 244 | |
| 245 | void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx) |
| 246 | { |
| 247 | struct spm_partition_runtime_data_t *runtime_data = |
| 248 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 249 | struct handler_ctx_stack_frame_t *stack_frame = |
| 250 | (struct handler_ctx_stack_frame_t *) |
| 251 | runtime_data->ctx_stack_ptr; |
| 252 | |
| 253 | stack_frame->partition_state = runtime_data->partition_state; |
| 254 | stack_frame->caller_partition_idx = runtime_data->caller_partition_idx; |
| 255 | |
| 256 | runtime_data->ctx_stack_ptr += |
| 257 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 258 | } |
| 259 | |
| 260 | void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx) |
| 261 | { |
| 262 | struct spm_partition_runtime_data_t *runtime_data = |
| 263 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 264 | struct handler_ctx_stack_frame_t *stack_frame; |
| 265 | |
| 266 | runtime_data->ctx_stack_ptr -= |
| 267 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 268 | |
| 269 | stack_frame = (struct handler_ctx_stack_frame_t *) |
| 270 | runtime_data->ctx_stack_ptr; |
| 271 | |
| 272 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 273 | stack_frame->partition_state = 0; |
| 274 | tfm_spm_partition_set_caller_partition_idx( |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 275 | partition_idx, stack_frame->caller_partition_idx); |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 276 | stack_frame->caller_partition_idx = 0; |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 279 | #endif /* !defined(TFM_PSA_API) */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 280 | |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 281 | #if (TFM_LVL != 1) || defined(TFM_PSA_API) |
Summer Qin | d00e4db | 2019-05-09 18:03:52 +0800 | [diff] [blame] | 282 | uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx) |
| 283 | { |
| 284 | return g_spm_partition_db.partitions[partition_idx]. |
| 285 | memory_data.stack_bottom; |
| 286 | } |
| 287 | |
| 288 | uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx) |
| 289 | { |
| 290 | return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top; |
| 291 | } |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 292 | #endif |
Summer Qin | d00e4db | 2019-05-09 18:03:52 +0800 | [diff] [blame] | 293 | |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 294 | #if (TFM_LVL != 1) && !defined(TFM_PSA_API) |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 295 | enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 296 | { |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 297 | struct spm_partition_desc_t *part; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 298 | if (!g_spm_partition_db.is_init) { |
| 299 | return SPM_ERR_PARTITION_DB_NOT_INIT; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 302 | part = &g_spm_partition_db.partitions[partition_idx]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 303 | |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 304 | return tfm_spm_hal_partition_sandbox_config(&(part->memory_data), |
| 305 | part->platform_data); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 308 | enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 309 | { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 310 | /* This function takes a partition id and disables the |
| 311 | * SPM partition for that partition |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 312 | */ |
| 313 | |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 314 | struct spm_partition_desc_t *part; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 315 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 316 | part = &g_spm_partition_db.partitions[partition_idx]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 317 | |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 318 | return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data), |
| 319 | part->platform_data); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Mate Toth-Pal | 21a74c9 | 2018-04-13 14:05:41 +0200 | [diff] [blame] | 322 | uint32_t tfm_spm_partition_get_zi_start(uint32_t partition_idx) |
| 323 | { |
| 324 | return g_spm_partition_db.partitions[partition_idx]. |
| 325 | memory_data.zi_start; |
| 326 | } |
| 327 | |
| 328 | uint32_t tfm_spm_partition_get_zi_limit(uint32_t partition_idx) |
| 329 | { |
| 330 | return g_spm_partition_db.partitions[partition_idx]. |
| 331 | memory_data.zi_limit; |
| 332 | } |
| 333 | |
| 334 | uint32_t tfm_spm_partition_get_rw_start(uint32_t partition_idx) |
| 335 | { |
| 336 | return g_spm_partition_db.partitions[partition_idx]. |
| 337 | memory_data.rw_start; |
| 338 | } |
| 339 | |
| 340 | uint32_t tfm_spm_partition_get_rw_limit(uint32_t partition_idx) |
| 341 | { |
| 342 | return g_spm_partition_db.partitions[partition_idx]. |
| 343 | memory_data.rw_limit; |
| 344 | } |
| 345 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 346 | void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 347 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 348 | g_spm_partition_db.partitions[partition_idx]. |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 349 | runtime_data.stack_ptr = stack_ptr; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 350 | } |
| 351 | #endif |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 352 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 353 | uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 354 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 355 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 356 | partition_id; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 359 | uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx) |
| 360 | { |
| 361 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 362 | partition_flags; |
| 363 | } |
| 364 | |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 365 | #ifndef TFM_PSA_API |
| 366 | void tfm_spm_partition_store_context(uint32_t partition_idx, |
| 367 | uint32_t stack_ptr, uint32_t lr) |
| 368 | { |
| 369 | g_spm_partition_db.partitions[partition_idx]. |
| 370 | runtime_data.stack_ptr = stack_ptr; |
| 371 | g_spm_partition_db.partitions[partition_idx]. |
| 372 | runtime_data.lr = lr; |
| 373 | } |
| 374 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 375 | const struct spm_partition_runtime_data_t * |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 376 | tfm_spm_partition_get_runtime_data(uint32_t partition_idx) |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 377 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 378 | return &(g_spm_partition_db.partitions[partition_idx].runtime_data); |
| 379 | } |
| 380 | |
| 381 | void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state) |
| 382 | { |
| 383 | g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state = |
| 384 | state; |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 385 | if (state == SPM_PARTITION_STATE_RUNNING || |
| 386 | state == SPM_PARTITION_STATE_HANDLING_IRQ) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 387 | g_spm_partition_db.running_partition_idx = partition_idx; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 391 | void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx, |
| 392 | uint32_t caller_partition_idx) |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 393 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 394 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 395 | caller_partition_idx = caller_partition_idx; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 398 | void tfm_spm_partition_set_signal_mask(uint32_t partition_idx, |
| 399 | uint32_t signal_mask) |
| 400 | { |
| 401 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 402 | signal_mask = signal_mask; |
| 403 | } |
| 404 | |
Mate Toth-Pal | 21a74c9 | 2018-04-13 14:05:41 +0200 | [diff] [blame] | 405 | void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx, |
| 406 | int32_t caller_client_id) |
| 407 | { |
| 408 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 409 | caller_client_id = caller_client_id; |
| 410 | } |
| 411 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 412 | enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 413 | uint32_t share) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 414 | { |
| 415 | enum spm_err_t ret = SPM_ERR_OK; |
| 416 | |
| 417 | #if TFM_LVL != 1 |
| 418 | /* Only need to set configuration on levels higher than 1 */ |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 419 | ret = tfm_spm_hal_set_share_region(share); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 420 | #endif |
| 421 | |
| 422 | if (ret == SPM_ERR_OK) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 423 | g_spm_partition_db.partitions[partition_idx].runtime_data.share = share; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 424 | } |
| 425 | return ret; |
| 426 | } |
| 427 | |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 428 | enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx, |
| 429 | const int32_t *args) |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 430 | { |
| 431 | struct spm_partition_runtime_data_t *runtime_data = |
| 432 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 433 | size_t i; |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 434 | |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 435 | if ((args[1] < 0) || (args[3] < 0)) { |
| 436 | return SPM_ERR_INVALID_PARAMETER; |
| 437 | } |
| 438 | |
| 439 | runtime_data->iovec_args.in_len = (size_t)args[1]; |
| 440 | for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) { |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 441 | runtime_data->iovec_args.in_vec[i].base = |
| 442 | ((psa_invec *)args[0])[i].base; |
| 443 | runtime_data->iovec_args.in_vec[i].len = ((psa_invec *)args[0])[i].len; |
| 444 | } |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 445 | runtime_data->iovec_args.out_len = (size_t)args[3]; |
| 446 | for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) { |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 447 | runtime_data->iovec_args.out_vec[i].base = |
| 448 | ((psa_outvec *)args[2])[i].base; |
| 449 | runtime_data->iovec_args.out_vec[i].len = |
| 450 | ((psa_outvec *)args[2])[i].len; |
| 451 | } |
| 452 | runtime_data->orig_outvec = (psa_outvec *)args[2]; |
Mate Toth-Pal | 2a6f8c2 | 2018-12-13 16:37:17 +0100 | [diff] [blame] | 453 | runtime_data->iovec_api = 1; |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 454 | |
| 455 | return SPM_ERR_OK; |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 458 | uint32_t tfm_spm_partition_get_running_partition_idx(void) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 459 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 460 | return g_spm_partition_db.running_partition_idx; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 463 | void tfm_spm_partition_cleanup_context(uint32_t partition_idx) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 464 | { |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 465 | struct spm_partition_desc_t *partition = |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 466 | &(g_spm_partition_db.partitions[partition_idx]); |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 467 | int32_t i; |
| 468 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 469 | partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 470 | partition->runtime_data.share = 0; |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 471 | partition->runtime_data.iovec_args.in_len = 0; |
| 472 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 473 | partition->runtime_data.iovec_args.in_vec[i].base = 0; |
| 474 | partition->runtime_data.iovec_args.in_vec[i].len = 0; |
| 475 | } |
| 476 | partition->runtime_data.iovec_args.out_len = 0; |
| 477 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 478 | partition->runtime_data.iovec_args.out_vec[i].base = 0; |
| 479 | partition->runtime_data.iovec_args.out_vec[i].len = 0; |
| 480 | } |
| 481 | partition->runtime_data.orig_outvec = 0; |
Mate Toth-Pal | 2a6f8c2 | 2018-12-13 16:37:17 +0100 | [diff] [blame] | 482 | partition->runtime_data.iovec_api = 0; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 483 | } |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 484 | #endif /* !defined(TFM_PSA_API) */ |
Edison Ai | b557135 | 2019-03-22 10:49:52 +0800 | [diff] [blame] | 485 | |
| 486 | __attribute__((section("SFN"))) |
| 487 | void tfm_spm_partition_change_privilege(uint32_t privileged) |
| 488 | { |
| 489 | CONTROL_Type ctrl; |
| 490 | |
| 491 | ctrl.w = __get_CONTROL(); |
| 492 | |
| 493 | if (privileged == TFM_PARTITION_PRIVILEGED_MODE) { |
| 494 | ctrl.b.nPRIV = 0; |
| 495 | } else { |
| 496 | ctrl.b.nPRIV = 1; |
| 497 | } |
| 498 | |
| 499 | __set_CONTROL(ctrl.w); |
| 500 | } |