Core: Context structure refine

- Change the architecture context name to 'struct tfm_arch_ctx_t'.
- Rename EXC_RETURN value in LR to Macro 'EXC_RETURN_THREAD_S_PSP'.

Change-Id: Ia13f3ae5c477e4ad4dc092f11bdba1f2b32dd00b
Signed-off-by: Summer Qin <summer.qin@arm.com>
diff --git a/secure_fw/core/arch/include/tfm_arch.h b/secure_fw/core/arch/include/tfm_arch.h
index c65eb21..0ad1ff0 100644
--- a/secure_fw/core/arch/include/tfm_arch.h
+++ b/secure_fw/core/arch/include/tfm_arch.h
@@ -38,13 +38,11 @@
     uint32_t    xpsr;
 };
 
-/* Architecture specific thread context extension */
-struct tfm_state_context_ext;
+/* Architecture specific thread context */
+struct tfm_arch_ctx_t;
 
-#define TFM_STATE_1ST_ARG(ctx)     \
-                    (((struct tfm_state_context_t *)(ctx)->ctxb.sp)->r0)
 #define TFM_STATE_RET_VAL(ctx)     \
-                    (((struct tfm_state_context_t *)(ctx)->ctxb.sp)->r0)
+                    (((struct tfm_state_context_t *)((ctx)->sp))->r0)
 
 __attribute__ ((always_inline))
 __STATIC_INLINE void tfm_arch_trigger_pendsv(void)
@@ -89,7 +87,7 @@
 /*
  * Initialize CPU architecture specific thread context extension
  */
-void tfm_arch_initialize_ctx_ext(struct tfm_state_context_ext *p_ctxb,
+void tfm_arch_initialize_ctx_ext(struct tfm_arch_ctx_t *p_actx,
                                  uint32_t sp, uint32_t sp_limit);
 
 /*
diff --git a/secure_fw/core/arch/include/tfm_arch_v6m_v7m.h b/secure_fw/core/arch/include/tfm_arch_v6m_v7m.h
index eafb69b..380df16 100644
--- a/secure_fw/core/arch/include/tfm_arch_v6m_v7m.h
+++ b/secure_fw/core/arch/include/tfm_arch_v6m_v7m.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019, Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -20,9 +20,9 @@
 #endif
 
 /* Initial EXC_RETURN value in LR when a thread is loaded at the first time */
-#define INIT_LR_UNPRIVILEGED                    0xFFFFFFFD
+#define EXC_RETURN_THREAD_S_PSP                 0xFFFFFFFD
 
-struct tfm_state_context_ext {
+struct tfm_arch_ctx_t {
     uint32_t    r8;
     uint32_t    r9;
     uint32_t    r10;
@@ -97,13 +97,13 @@
 }
 
 /**
- * \brief Update context value into hardware
+ * \brief Update architecture context value into hardware
  *
- * \param[in] pctx        Pointer of context data
+ * \param[in] p_actx        Pointer of context data
  */
-__STATIC_INLINE void tfm_arch_update_ctx(struct tfm_state_context_ext *pctx)
+__STATIC_INLINE void tfm_arch_update_ctx(struct tfm_arch_ctx_t *p_actx)
 {
-    __set_PSP(pctx->sp);
+    __set_PSP(p_actx->sp);
 }
 
 /**
diff --git a/secure_fw/core/arch/include/tfm_arch_v8m.h b/secure_fw/core/arch/include/tfm_arch_v8m.h
index 429eacd..94a2e4f 100644
--- a/secure_fw/core/arch/include/tfm_arch_v8m.h
+++ b/secure_fw/core/arch/include/tfm_arch_v8m.h
@@ -12,19 +12,26 @@
 
 #include "cmsis_compiler.h"
 
-#define EXC_RETURN_INDICATOR                    (0xF << 28)
-#define EXC_RETURN_SECURITY_STACK_STATUS_MASK   (0x3 << 5)
+#define EXC_RETURN_INDICATOR                    (0xFF << 24)
+#define EXC_RETURN_RES1                         (0x1FFFF << 7)
 #define EXC_RETURN_SECURE_STACK                 (1 << 6)
+#define EXC_RETURN_STACK_RULE                   (1 << 5)
 #define EXC_RETURN_FPU_FRAME_BASIC              (1 << 4)
 #define EXC_RETURN_MODE_THREAD                  (1 << 3)
 #define EXC_RETURN_STACK_PROCESS                (1 << 2)
+#define EXC_RETURN_RES0                         (0 << 1)
 #define EXC_RETURN_EXC_SECURE                   (1)
 
 /* Initial EXC_RETURN value in LR when a thread is loaded at the first time */
-#define INIT_LR_UNPRIVILEGED                    0xFFFFFFFD
+#define EXC_RETURN_THREAD_S_PSP                                 \
+        EXC_RETURN_INDICATOR | EXC_RETURN_RES1 |                \
+        EXC_RETURN_SECURE_STACK | EXC_RETURN_STACK_RULE |       \
+        EXC_RETURN_FPU_FRAME_BASIC | EXC_RETURN_MODE_THREAD |   \
+        EXC_RETURN_STACK_PROCESS | EXC_RETURN_RES0 |            \
+        EXC_RETURN_EXC_SECURE
 
 #if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
-struct tfm_state_context_ext {
+struct tfm_arch_ctx_t {
     uint32_t    r4;
     uint32_t    r5;
     uint32_t    r6;
@@ -39,7 +46,7 @@
     uint32_t    lr;
 };
 #elif defined(__ARM_ARCH_8M_BASE__)
-struct tfm_state_context_ext {
+struct tfm_arch_ctx_t {
     uint32_t    r8;
     uint32_t    r9;
     uint32_t    r10;
@@ -101,14 +108,14 @@
 }
 
 /**
- * \brief Update context value into hardware
+ * \brief Update architecture context value into hardware
  *
- * \param[in] pctx        Pointer of context data
+ * \param[in] p_actx        Pointer of context data
  */
-__STATIC_INLINE void tfm_arch_update_ctx(struct tfm_state_context_ext *pctx)
+__STATIC_INLINE void tfm_arch_update_ctx(struct tfm_arch_ctx_t *p_actx)
 {
-    __set_PSP(pctx->sp);
-    __set_PSPLIM(pctx->sp_limit);
+    __set_PSP(p_actx->sp);
+    __set_PSPLIM(p_actx->sp_limit);
 }
 
 /**
diff --git a/secure_fw/core/arch/tfm_arch_v6m_v7m.c b/secure_fw/core/arch/tfm_arch_v6m_v7m.c
index 5cb111d..99ee9db 100644
--- a/secure_fw/core/arch/tfm_arch_v6m_v7m.c
+++ b/secure_fw/core/arch/tfm_arch_v6m_v7m.c
@@ -70,13 +70,13 @@
     );
 }
 
-void tfm_arch_initialize_ctx_ext(struct tfm_state_context_ext *p_ctxb,
+void tfm_arch_initialize_ctx_ext(struct tfm_arch_ctx_t *p_actx,
                                  uint32_t sp, uint32_t sp_limit)
 {
     (void)sp_limit;
 
-    p_ctxb->sp = sp;
-    p_ctxb->lr = INIT_LR_UNPRIVILEGED;
+    p_actx->sp = sp;
+    p_actx->lr = EXC_RETURN_THREAD_S_PSP;
 }
 
 __attribute__((naked)) void SVC_Handler(void)
diff --git a/secure_fw/core/arch/tfm_arch_v8m_base.c b/secure_fw/core/arch/tfm_arch_v8m_base.c
index d04d498..ec091f3 100644
--- a/secure_fw/core/arch/tfm_arch_v8m_base.c
+++ b/secure_fw/core/arch/tfm_arch_v8m_base.c
@@ -75,12 +75,12 @@
     );
 }
 
-void tfm_arch_initialize_ctx_ext(struct tfm_state_context_ext *p_ctxb,
+void tfm_arch_initialize_ctx_ext(struct tfm_arch_ctx_t *p_actx,
                                  uint32_t sp, uint32_t sp_limit)
 {
-    p_ctxb->sp = sp;
-    p_ctxb->sp_limit = sp_limit;
-    p_ctxb->lr = INIT_LR_UNPRIVILEGED;
+    p_actx->sp = sp;
+    p_actx->sp_limit = sp_limit;
+    p_actx->lr = EXC_RETURN_THREAD_S_PSP;
 }
 #else
 __attribute__((section("SFN"), naked))
diff --git a/secure_fw/core/arch/tfm_arch_v8m_main.c b/secure_fw/core/arch/tfm_arch_v8m_main.c
index 503e9c1..5e82bb6 100644
--- a/secure_fw/core/arch/tfm_arch_v8m_main.c
+++ b/secure_fw/core/arch/tfm_arch_v8m_main.c
@@ -78,12 +78,12 @@
     );
 }
 
-void tfm_arch_initialize_ctx_ext(struct tfm_state_context_ext *p_ctxb,
+void tfm_arch_initialize_ctx_ext(struct tfm_arch_ctx_t *p_actx,
                                  uint32_t sp, uint32_t sp_limit)
 {
-    p_ctxb->sp = sp;
-    p_ctxb->sp_limit = sp_limit;
-    p_ctxb->lr = INIT_LR_UNPRIVILEGED;
+    p_actx->sp = sp;
+    p_actx->sp_limit = sp_limit;
+    p_actx->lr = EXC_RETURN_THREAD_S_PSP;
 }
 #else
 __attribute__((section("SFN"), naked))
diff --git a/secure_fw/core/ipc/include/tfm_thread.h b/secure_fw/core/ipc/include/tfm_thread.h
index e5c1e03..f99f5ef 100644
--- a/secure_fw/core/ipc/include/tfm_thread.h
+++ b/secure_fw/core/ipc/include/tfm_thread.h
@@ -37,10 +37,6 @@
 /* Thread entry function type */
 typedef void *(*tfm_core_thrd_entry_t)(void *);
 
-struct tfm_state_context {
-    struct tfm_state_context_ext  ctxb;
-};
-
 /* Thread context */
 struct tfm_core_thread_t {
     tfm_core_thrd_entry_t pfn;          /* entry function               */
@@ -50,7 +46,7 @@
     uint32_t        prior;              /* priority                     */
     uint32_t        state;              /* state                        */
 
-    struct tfm_state_context state_ctx; /* State context                */
+    struct tfm_arch_ctx_t    arch_ctx;  /* State context                */
     struct tfm_core_thread_t *next;     /* next thread in list          */
 };
 
@@ -150,7 +146,7 @@
 void __STATIC_INLINE tfm_core_thrd_set_retval(struct tfm_core_thread_t *pth,
                                               uint32_t retval)
 {
-    TFM_STATE_RET_VAL(&pth->state_ctx) = retval;
+    TFM_STATE_RET_VAL(&pth->arch_ctx) = retval;
 }
 
 /*
@@ -206,17 +202,17 @@
 void tfm_core_thrd_activate_schedule(void);
 
 /*
- * Save current context into 'prev' thread and switch to 'next'.
+ * Save current architecture context into 'prev' thread and switch to 'next'.
  *
  * Parameters :
- *  ctxb        -     latest caller context
+ *  p_actx      -     latest caller context
  *  prev        -     previous thread to be switched out
  *  next        -     thread to be run
  *
  * Notes :
  *  This function could be called multiple times before scheduling.
  */
-void tfm_core_thrd_switch_context(struct tfm_state_context_ext *ctxb,
+void tfm_core_thrd_switch_context(struct tfm_arch_ctx_t *p_actx,
                                   struct tfm_core_thread_t *prev,
                                   struct tfm_core_thread_t *next);
 
diff --git a/secure_fw/core/ipc/tfm_thread.c b/secure_fw/core/ipc/tfm_thread.c
index 686961e..fbbe5c0 100644
--- a/secure_fw/core/ipc/tfm_thread.c
+++ b/secure_fw/core/ipc/tfm_thread.c
@@ -101,7 +101,7 @@
     tfm_core_thrd_exit();
 }
 
-static void tfm_thrd_initialize_context(struct tfm_state_context *ctx,
+static void tfm_thrd_initialize_context(struct tfm_arch_ctx_t *p_actx,
                                         void *param, uintptr_t pfn,
                                         uintptr_t stk_btm, uintptr_t stk_top)
 {
@@ -109,25 +109,25 @@
      * For security consideration, set unused registers into ZERO;
      * and only necessary registers are set here.
      */
-    struct tfm_state_context_t *p_ctxa =
+    struct tfm_state_context_t *p_stat_ctx =
                             (struct tfm_state_context_t *)stk_top;
 
     /*
      * Shift back SP to leave space for holding base context
      * since thread is kicked off through exception return.
      */
-    p_ctxa--;
+    p_stat_ctx--;
 
     /* Basic context is considerate at thread start.*/
-    tfm_core_util_memset(p_ctxa, 0, sizeof(*p_ctxa));
-    p_ctxa->r0 = (uint32_t)param;
-    p_ctxa->ra = (uint32_t)pfn;
-    p_ctxa->lr = (uint32_t)exit_zone;
-    p_ctxa->xpsr = XPSR_T32;
+    tfm_core_util_memset(p_stat_ctx, 0, sizeof(*p_stat_ctx));
+    p_stat_ctx->r0 = (uint32_t)param;
+    p_stat_ctx->ra = (uint32_t)pfn;
+    p_stat_ctx->lr = (uint32_t)exit_zone;
+    p_stat_ctx->xpsr = XPSR_T32;
 
-    tfm_core_util_memset(ctx, 0, sizeof(*ctx));
+    tfm_core_util_memset(p_actx, 0, sizeof(*p_actx));
 
-    tfm_arch_initialize_ctx_ext(&ctx->ctxb, (uint32_t)p_ctxa,
+    tfm_arch_initialize_ctx_ext(p_actx, (uint32_t)p_stat_ctx,
                                 (uint32_t)stk_btm);
 }
 
@@ -142,7 +142,7 @@
     }
 
     /* Thread management runs in handler mode; set context for thread mode. */
-    tfm_thrd_initialize_context(&pth->state_ctx,
+    tfm_thrd_initialize_context(&pth->arch_ctx,
                                 pth->param, (uintptr_t)pth->pfn,
                                 pth->stk_btm, pth->stk_top);
 
@@ -179,9 +179,9 @@
      */
     TFM_CORE_ASSERT(CURR_THRD == NULL);
     TFM_CORE_ASSERT(pth != NULL);
-    TFM_CORE_ASSERT(pth->state_ctx.ctxb.sp != 0);
+    TFM_CORE_ASSERT(pth->arch_ctx.sp != 0);
 
-    tfm_arch_update_ctx(&pth->state_ctx.ctxb);
+    tfm_arch_update_ctx(&pth->arch_ctx);
 
     CURR_THRD = pth;
 
@@ -204,7 +204,7 @@
     }
 }
 
-void tfm_core_thrd_switch_context(struct tfm_state_context_ext *ctxb,
+void tfm_core_thrd_switch_context(struct tfm_arch_ctx_t *p_actx,
                                   struct tfm_core_thread_t *prev,
                                   struct tfm_core_thread_t *next)
 {
@@ -215,9 +215,8 @@
      * First, update latest context into the current thread context.
      * Then, update background context with next thread's context.
      */
-    tfm_core_util_memcpy(&prev->state_ctx.ctxb, ctxb, sizeof(*ctxb));
-    tfm_core_util_memcpy(ctxb, &next->state_ctx.ctxb,
-                         sizeof(next->state_ctx.ctxb));
+    tfm_core_util_memcpy(&prev->arch_ctx, p_actx, sizeof(*p_actx));
+    tfm_core_util_memcpy(p_actx, &next->arch_ctx, sizeof(next->arch_ctx));
 
     /* Update current thread indicator */
     CURR_THRD = next;
diff --git a/secure_fw/spm/spm_api.h b/secure_fw/spm/spm_api.h
index a99033a..56a112a 100644
--- a/secure_fw/spm/spm_api.h
+++ b/secure_fw/spm/spm_api.h
@@ -669,13 +669,13 @@
  * PendSV specified function.
  *
  * Parameters :
- *  ctxb        -    State context storage pointer
+ *  p_actx        -    Architecture context storage pointer
  *
  * Notes:
  *  This is a staging API. Scheduler should be called in SPM finally and
  *  this function will be obsoleted later.
  */
-void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb);
+void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx);
 
 /**
  * \brief                      SPM initialization implementation
diff --git a/secure_fw/spm/spm_api_ipc.c b/secure_fw/spm/spm_api_ipc.c
index ec25b41..340c778 100644
--- a/secure_fw/spm/spm_api_ipc.c
+++ b/secure_fw/spm/spm_api_ipc.c
@@ -616,10 +616,10 @@
      */
     tfm_core_thrd_start_scheduler(p_ns_entry_thread);
 
-    return p_ns_entry_thread->state_ctx.ctxb.lr;
+    return p_ns_entry_thread->arch_ctx.lr;
 }
 
-void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb)
+void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
 {
 #if TFM_LVL == 2
     struct spm_partition_desc_t *p_next_partition;
@@ -648,7 +648,7 @@
         tfm_spm_partition_change_privilege(is_privileged);
 #endif
 
-        tfm_core_thrd_switch_context(ctxb, pth_curr, pth_next);
+        tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
     }
 
     /*