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 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 8 | /* All the APIs defined in this file are common for library and IPC model. */ |
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 | |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 26 | /* Define SPM DB structure */ |
| 27 | #include "secure_fw/services/tfm_spm_db.inc" |
| 28 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 29 | uint32_t get_partition_idx(uint32_t partition_id) |
| 30 | { |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 31 | uint32_t i; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 32 | |
| 33 | if (partition_id == INVALID_PARTITION_ID) { |
| 34 | return SPM_INVALID_PARTITION_IDX; |
| 35 | } |
| 36 | |
| 37 | for (i = 0; i < g_spm_partition_db.partition_count; ++i) { |
| 38 | if (g_spm_partition_db.partitions[i].static_data.partition_id == |
| 39 | partition_id) { |
| 40 | return i; |
| 41 | } |
| 42 | } |
| 43 | return SPM_INVALID_PARTITION_IDX; |
| 44 | } |
| 45 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 46 | enum spm_err_t tfm_spm_db_init(void) |
| 47 | { |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 48 | struct spm_partition_desc_t *part_ptr; |
| 49 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 50 | static uint32_t ns_interrupt_ctx_stack[ |
| 51 | sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0}; |
| 52 | static uint32_t tfm_core_interrupt_ctx_stack[ |
| 53 | sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0}; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 54 | #endif /* !defined(TFM_PSA_API) */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 55 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 56 | /* This function initialises partition db */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 57 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 58 | /* There are a few partitions that are used by TF-M internally. |
| 59 | * These are explicitly added to the partition db here. |
| 60 | */ |
| 61 | |
| 62 | /* For the non secure Execution environment */ |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 63 | #if (TFM_LVL != 1) || defined(TFM_PSA_API) |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 64 | extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[]; |
| 65 | extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[]; |
| 66 | uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base; |
| 67 | 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] | 68 | #endif |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 69 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 70 | part_ptr = &(g_spm_partition_db.partitions[ |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 71 | NON_SECURE_INTERNAL_PARTITION_DB_IDX]); |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 72 | part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID; |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 73 | #ifdef TFM_PSA_API |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 74 | part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT | |
| 75 | SPM_PART_FLAG_IPC; |
| 76 | part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW; |
| 77 | part_ptr->static_data.partition_init = tfm_nspm_thread_entry; |
| 78 | #else |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 79 | part_ptr->static_data.partition_flags = 0; |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 80 | #endif |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 81 | |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 82 | #if (TFM_LVL != 1) || defined(TFM_PSA_API) |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 83 | part_ptr->memory_data.stack_bottom = psp_stack_bottom; |
| 84 | part_ptr->memory_data.stack_top = psp_stack_top; |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 85 | /* Since RW, ZI and stack are configured as one MPU region, configure |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 86 | * 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] | 87 | */ |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 88 | part_ptr->memory_data.rw_start = psp_stack_bottom; |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 89 | #endif |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 90 | |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 91 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 92 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 93 | part_ptr->runtime_data.ctx_stack_ptr = ns_interrupt_ctx_stack; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 94 | #endif /* !defined(TFM_PSA_API) */ |
| 95 | |
Miklos Balint | 12735bc | 2018-08-01 15:45:18 +0200 | [diff] [blame] | 96 | tfm_nspm_configure_clients(); |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 97 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 98 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 99 | /* For the TF-M core environment itself */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 100 | part_ptr = &(g_spm_partition_db.partitions[ |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 101 | TFM_CORE_INTERNAL_PARTITION_DB_IDX]); |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 102 | part_ptr->static_data.partition_id = TFM_SP_CORE_ID; |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 103 | part_ptr->static_data.partition_flags = |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 104 | SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 105 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 106 | part_ptr->runtime_data.ctx_stack_ptr = tfm_core_interrupt_ctx_stack; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 107 | #endif /* !defined(TFM_PSA_API) */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 108 | |
Mate Toth-Pal | 7345a4b | 2018-03-08 16:10:28 +0100 | [diff] [blame] | 109 | g_spm_partition_db.is_init = 1; |
| 110 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 111 | return SPM_ERR_OK; |
| 112 | } |
| 113 | |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 114 | #if (TFM_LVL != 1) || defined(TFM_PSA_API) |
Summer Qin | d00e4db | 2019-05-09 18:03:52 +0800 | [diff] [blame] | 115 | uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx) |
| 116 | { |
| 117 | return g_spm_partition_db.partitions[partition_idx]. |
| 118 | memory_data.stack_bottom; |
| 119 | } |
| 120 | |
| 121 | uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx) |
| 122 | { |
| 123 | return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top; |
| 124 | } |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 125 | #endif |
Summer Qin | d00e4db | 2019-05-09 18:03:52 +0800 | [diff] [blame] | 126 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 127 | 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] | 128 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 129 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 130 | partition_id; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 133 | uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx) |
| 134 | { |
| 135 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 136 | partition_flags; |
| 137 | } |
| 138 | |
Edison Ai | b557135 | 2019-03-22 10:49:52 +0800 | [diff] [blame] | 139 | __attribute__((section("SFN"))) |
| 140 | void tfm_spm_partition_change_privilege(uint32_t privileged) |
| 141 | { |
| 142 | CONTROL_Type ctrl; |
| 143 | |
| 144 | ctrl.w = __get_CONTROL(); |
| 145 | |
| 146 | if (privileged == TFM_PARTITION_PRIVILEGED_MODE) { |
| 147 | ctrl.b.nPRIV = 0; |
| 148 | } else { |
| 149 | ctrl.b.nPRIV = 1; |
| 150 | } |
| 151 | |
| 152 | __set_CONTROL(ctrl.w); |
| 153 | } |