Przeglądaj źródła

Updated template for Context Management for ARMv8-M TrustZone (tz_context.c)

Robert Rostohar 9 lat temu
rodzic
commit
71f4dcf9e4
2 zmienionych plików z 26 dodań i 7 usunięć
  1. 2 1
      ARM.CMSIS.pdsc
  2. 24 6
      CMSIS/Core/Template/ARMv8-M/tz_context.c

+ 2 - 1
ARM.CMSIS.pdsc

@@ -12,6 +12,7 @@
       CMSIS_Core:
        - Updated cmsis_armcc.h: corrected macro __ARM_ARCH_6M__
        - Updated template for secure main function (main_s.c) 
+       - Updated template for Context Management for ARMv8-M TrustZone (tz_context.c)
       CMSIS-RTOS2:
        - RTX 5.0.1 (see revision history for details) 
     </release>
@@ -1756,7 +1757,7 @@ ARMv8-M Mainline based device with TrustZone
         <file category="header"  name="CMSIS/Include/tz_context.h" condition="ARMv8-M TZ Device"/>
         <!-- Code template -->
         <file category="sourceC" attr="template" condition="ARMv8-M TZ Device" name="CMSIS/Core/Template/ARMv8-M/main_s.c"     version="1.1.0" select="Secure mode 'main' module for ARMv8-M"/>
-        <file category="sourceC" attr="template" condition="ARMv8-M TZ Device" name="CMSIS/Core/Template/ARMv8-M/tz_context.c" select="RTOS Context Management (TrustZone for ARMv8-M)" />
+        <file category="sourceC" attr="template" condition="ARMv8-M TZ Device" name="CMSIS/Core/Template/ARMv8-M/tz_context.c" version="1.1.0" select="RTOS Context Management (TrustZone for ARMv8-M)" />
       </files>
     </component>
 

+ 24 - 6
CMSIS/Core/Template/ARMv8-M/tz_context.c

@@ -17,14 +17,12 @@
  *
  * ----------------------------------------------------------------------------
  *
- * $Date:        21. September 2016
- * $Revision:    V1.0
+ * $Date:        15. October 2016
+ * $Revision:    1.1.0
  *
  * Project:      TrustZone for ARMv8-M
  * Title:        Context Management for ARMv8-M TrustZone - Sample implementation
  *
- * Version 1.0
- *    Initial Release
  *---------------------------------------------------------------------------*/
  
 #include "RTE_Components.h"
@@ -58,9 +56,10 @@ __attribute__((cmse_nonsecure_entry))
 uint32_t TZ_InitContextSystem_S (void) {
   uint32_t n;
 
-  if ((__get_CONTROL() & 2U) == 0U)  {
-    return 0U;  // Not using PSP for threads
+  if (__get_IPSR() == 0U) {
+    return 0U;  // Thread Mode
   }
+
   for (n = 0U; n < TZ_PROCESS_STACK_SLOTS; n++) {
     ProcessStackInfo[n].sp = 0U;
     ProcessStackInfo[n].sp_limit = (uint32_t)&ProcessStackMemory[n];
@@ -71,6 +70,13 @@ uint32_t TZ_InitContextSystem_S (void) {
 
   ProcessStackFreeSlot = 0U;
 
+  // Default process stack pointer and stack limit
+  __set_PSPLIM((uint32_t)ProcessStackMemory);
+  __set_PSP   ((uint32_t)ProcessStackMemory);
+
+  // Privileged Thread Mode using PSP
+  __set_CONTROL(0x02U);
+
   return 1U;    // Success
 }
 
@@ -85,6 +91,10 @@ TZ_MemoryId_t TZ_AllocModuleContext_S (TZ_ModuleId_t module) {
 
   (void)module; // Ignore (fixed Stack size)
 
+  if (__get_IPSR() == 0U) {
+    return 0U;  // Thread Mode
+  }
+
   if (ProcessStackFreeSlot == 0xFFFFFFFFU) {
     return 0U;  // No slot available
   }
@@ -105,6 +115,10 @@ __attribute__((cmse_nonsecure_entry))
 uint32_t TZ_FreeModuleContext_S (TZ_MemoryId_t id) {
   uint32_t slot;
 
+  if (__get_IPSR() == 0U) {
+    return 0U;  // Thread Mode
+  }
+
   if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
     return 0U;  // Invalid ID
   }
@@ -181,5 +195,9 @@ uint32_t TZ_StoreContext_S (TZ_MemoryId_t id) {
   }
   ProcessStackInfo[slot].sp = sp;
 
+  // Default process stack pointer and stack limit
+  __set_PSPLIM((uint32_t)ProcessStackMemory);
+  __set_PSP   ((uint32_t)ProcessStackMemory);
+
   return 1U;    // Success
 }