Core: Minimize the memory usage for partition list

Split the rodata, data and bss of the partition list.

Change-Id: I1b7e876ceee38b65b871b6be664d46bc7bd49af3
Signed-off-by: Summer Qin <summer.qin@arm.com>
diff --git a/secure_fw/spm/spm_api.c b/secure_fw/spm/spm_api.c
index b8af146..51bce88 100644
--- a/secure_fw/spm/spm_api.c
+++ b/secure_fw/spm/spm_api.c
@@ -35,7 +35,7 @@
     }
 
     for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
-        if (g_spm_partition_db.partitions[i].static_data.partition_id ==
+        if (g_spm_partition_db.partitions[i].static_data->partition_id ==
                 partition_id) {
             return i;
         }
@@ -45,65 +45,36 @@
 
 enum spm_err_t tfm_spm_db_init(void)
 {
-    struct spm_partition_desc_t *part_ptr;
-#ifndef TFM_PSA_API
-    static uint32_t ns_interrupt_ctx_stack[
-           sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
-    static uint32_t tfm_core_interrupt_ctx_stack[
-           sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
-#endif /* !defined(TFM_PSA_API) */
+    uint32_t i;
 
     /* This function initialises partition db */
 
-    /* There are a few partitions that are used by TF-M internally.
-     * These are explicitly added to the partition db here.
-     */
-
     /* For the non secure Execution environment */
-#ifdef TFM_PSA_API
-    extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
-    extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[];
-    uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
-    uint32_t psp_stack_top    = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit;
-#endif
-
-    part_ptr = &(g_spm_partition_db.partitions[
-                                         NON_SECURE_INTERNAL_PARTITION_DB_IDX]);
-    part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
-#ifdef TFM_PSA_API
-    part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT |
-                                            SPM_PART_FLAG_IPC;
-    part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW;
-    part_ptr->static_data.partition_init = tfm_nspm_thread_entry;
-#else
-    part_ptr->static_data.partition_flags = 0;
-#endif
-
-#ifdef TFM_PSA_API
-    part_ptr->memory_data.stack_bottom = psp_stack_bottom;
-    part_ptr->memory_data.stack_top    = psp_stack_top;
-    /* Since RW, ZI and stack are configured as one MPU region, configure
-     * RW start address to psp_stack_bottom to get RW access to stack
-     */
-    part_ptr->memory_data.rw_start     = psp_stack_bottom;
-#endif
-
 #ifndef TFM_PSA_API
-    part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
-    part_ptr->runtime_data.ctx_stack_ptr = ns_interrupt_ctx_stack;
-
     tfm_nspm_configure_clients();
+#endif
 
-    /* For the TF-M core environment itself */
-    part_ptr = &(g_spm_partition_db.partitions[
-                                           TFM_CORE_INTERNAL_PARTITION_DB_IDX]);
-    part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
-    part_ptr->static_data.partition_flags =
-                    SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT;
-    part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
-    part_ptr->runtime_data.ctx_stack_ptr = tfm_core_interrupt_ctx_stack;
+    for (i = 0; i < g_spm_partition_db.partition_count; i++) {
+#ifndef TFM_PSA_API
+        g_spm_partition_db.partitions[i].runtime_data.partition_state =
+            SPM_PARTITION_STATE_UNINIT;
+        g_spm_partition_db.partitions[i].runtime_data.caller_partition_idx =
+            SPM_INVALID_PARTITION_IDX;
+        g_spm_partition_db.partitions[i].runtime_data.caller_client_id =
+            TFM_INVALID_CLIENT_ID;
+        g_spm_partition_db.partitions[i].runtime_data.share =
+            TFM_BUFFER_SHARE_DISABLE;
+        g_spm_partition_db.partitions[i].runtime_data.iovec_api =
+            TFM_SFN_API_IOVEC;
+        g_spm_partition_db.partitions[i].runtime_data.ctx_stack_ptr =
+            ctx_stack_list[i];
 #endif /* !defined(TFM_PSA_API) */
-
+        g_spm_partition_db.partitions[i].static_data = &static_data_list[i];
+        g_spm_partition_db.partitions[i].platform_data = platform_data_list[i];
+#ifdef TFM_PSA_API
+        g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i];
+#endif
+    }
     g_spm_partition_db.is_init = 1;
 
     return SPM_ERR_OK;
@@ -111,13 +82,13 @@
 
 uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
 {
-    return g_spm_partition_db.partitions[partition_idx].static_data.
+    return g_spm_partition_db.partitions[partition_idx].static_data->
             partition_id;
 }
 
 uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
 {
-    return g_spm_partition_db.partitions[partition_idx].static_data.
+    return g_spm_partition_db.partitions[partition_idx].static_data->
             partition_flags;
 }
 
diff --git a/secure_fw/spm/spm_api.h b/secure_fw/spm/spm_api.h
index 99d35a2..75b921c 100644
--- a/secure_fw/spm/spm_api.h
+++ b/secure_fw/spm/spm_api.h
@@ -106,6 +106,7 @@
                                          * Service signal mask passed by
                                          * psa_wait()
                                          */
+    uint32_t index;                     /* Partition index */
 };
 
 #ifdef TFM_PSA_API
diff --git a/secure_fw/spm/spm_api_func.c b/secure_fw/spm/spm_api_func.c
index 763a543..f425679 100644
--- a/secure_fw/spm/spm_api_func.c
+++ b/secure_fw/spm/spm_api_func.c
@@ -39,17 +39,17 @@
 #ifdef TFM_CORE_DEBUG
     if (err_type == TFM_INIT_FAILURE) {
         printf("Partition init failed for partition id 0x%08X\r\n",
-               partition->static_data.partition_id);
+               partition->static_data->partition_id);
     } else {
         printf(
             "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n",
-            err_type, err_code, partition->static_data.partition_id);
+            err_type, err_code, partition->static_data->partition_id);
     }
 #else
     (void)err_type;
     (void)err_code;
 #endif
-    tfm_spm_partition_set_state(partition->static_data.partition_id,
+    tfm_spm_partition_set_state(partition->static_data->partition_id,
                                 SPM_PARTITION_STATE_CLOSED);
 }
 
@@ -65,7 +65,7 @@
     for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
         part = &g_spm_partition_db.partitions[idx];
         tfm_spm_hal_configure_default_isolation(part->platform_data);
-        if (part->static_data.partition_init == NULL) {
+        if (part->static_data->partition_init == NULL) {
             tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
             tfm_spm_partition_set_caller_partition_idx(idx,
                                                        SPM_INVALID_PARTITION_IDX);
@@ -75,8 +75,8 @@
             desc.args = args;
             desc.ns_caller = 0U;
             desc.iovec_api = TFM_SFN_API_IOVEC;
-            desc.sfn = (sfn_t)part->static_data.partition_init;
-            desc.sp_id = part->static_data.partition_id;
+            desc.sfn = (sfn_t)part->static_data->partition_init;
+            desc.sp_id = part->static_data->partition_id;
             res = tfm_core_sfn_request(&desc);
             if (res == TFM_SUCCESS) {
                 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
@@ -261,4 +261,4 @@
     }
     partition->runtime_data.orig_outvec = 0;
     partition->runtime_data.iovec_api = 0;
-}
\ No newline at end of file
+}
diff --git a/secure_fw/spm/spm_api_ipc.c b/secure_fw/spm/spm_api_ipc.c
index 3db55b5..7f773d2 100644
--- a/secure_fw/spm/spm_api_ipc.c
+++ b/secure_fw/spm/spm_api_ipc.c
@@ -264,7 +264,7 @@
 
     /* For condition 2: check if the partition ID is same */
     partition_id = tfm_spm_partition_get_running_partition_id();
-    if (partition_id != msg->service->partition->static_data.partition_id) {
+    if (partition_id != msg->service->partition->static_data->partition_id) {
         return NULL;
     }
 
@@ -364,12 +364,12 @@
 uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
 {
     return g_spm_partition_db.partitions[partition_idx].
-            memory_data.stack_bottom;
+            memory_data->stack_bottom;
 }
 
 uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
 {
-    return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
+    return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top;
 }
 
 uint32_t tfm_spm_partition_get_running_partition_id(void)
@@ -379,7 +379,7 @@
 
     partition = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_desc_t,
                                       sp_thrd);
-    return partition->static_data.partition_id;
+    return partition->static_data->partition_id;
 }
 
 static struct tfm_thrd_ctx *
@@ -392,12 +392,12 @@
     tfm_spm_partition_get_init_func(uint32_t partition_idx)
 {
     return (tfm_thrd_func_t)(g_spm_partition_db.partitions[partition_idx].
-                             static_data.partition_init);
+                             static_data->partition_init);
 }
 
 static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
 {
-    return g_spm_partition_db.partitions[partition_idx].static_data.
+    return g_spm_partition_db.partitions[partition_idx].static_data->
                     partition_priority;
 }
 
@@ -460,7 +460,7 @@
     for (i = 0; i < g_spm_partition_db.partition_count; i++) {
         partition = &g_spm_partition_db.partitions[i];
         tfm_spm_hal_configure_default_isolation(partition->platform_data);
-        partition->static_data.index = i;
+        partition->runtime_data.index = i;
         if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
             continue;
         }
@@ -536,7 +536,7 @@
                                                  struct spm_partition_desc_t,
                                                  sp_thrd);
 
-        if (p_next_partition->static_data.partition_flags &
+        if (p_next_partition->static_data->partition_flags &
             SPM_PART_FLAG_PSA_ROT) {
             is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
         } else {
diff --git a/secure_fw/spm/spm_db.h b/secure_fw/spm/spm_db.h
index 2ee69f6..90e071c 100644
--- a/secure_fw/spm/spm_db.h
+++ b/secure_fw/spm/spm_db.h
@@ -39,7 +39,6 @@
  * phase.
  */
 struct spm_partition_static_data_t {
-    int32_t index;                      /* Partition index */
     uint32_t partition_id;
     uint32_t partition_flags;
     uint32_t partition_priority;
@@ -51,11 +50,11 @@
  * divided to structures, to keep the related fields close to each other.
  */
 struct spm_partition_desc_t {
-    struct spm_partition_static_data_t static_data;
     struct spm_partition_runtime_data_t runtime_data;
-    struct tfm_spm_partition_platform_data_t *platform_data;
+    const struct spm_partition_static_data_t *static_data;
+    const struct tfm_spm_partition_platform_data_t *platform_data;
 #if TFM_PSA_API
-    struct tfm_spm_partition_memory_data_t memory_data;
+    const struct tfm_spm_partition_memory_data_t *memory_data;
     struct tfm_thrd_ctx sp_thrd;
 #endif
 };