Fix free secure context for Cortex-M23 ports

Update the branching condition to correctly free secure context when
there is one.

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
diff --git a/portable/ARMv8M/non_secure/portable/GCC/ARM_CM23/portasm.c b/portable/ARMv8M/non_secure/portable/GCC/ARM_CM23/portasm.c
index 9dab039..b82bd63 100644
--- a/portable/ARMv8M/non_secure/portable/GCC/ARM_CM23/portasm.c
+++ b/portable/ARMv8M/non_secure/portable/GCC/ARM_CM23/portasm.c
@@ -449,7 +449,7 @@
         "	ldr r2, [r0]									\n"/* The first item in the TCB is the top of the stack. */

         "	ldr r1, [r2]									\n"/* The first item on the stack is the task's xSecureContext. */

         "	cmp r1, #0										\n"/* Raise svc if task's xSecureContext is not NULL. */

-        "	beq free_secure_context							\n"

+        "	bne free_secure_context							\n"/* Branch if r1 != 0. */

         "	bx lr											\n"/* There is no secure context (xSecureContext is NULL). */

         " free_secure_context:								\n"

         "	svc %0											\n"/* Secure context is freed in the supervisor call. */

diff --git a/portable/ARMv8M/non_secure/portable/IAR/ARM_CM23/portasm.s b/portable/ARMv8M/non_secure/portable/IAR/ARM_CM23/portasm.s
index 0f2c838..705889e 100644
--- a/portable/ARMv8M/non_secure/portable/IAR/ARM_CM23/portasm.s
+++ b/portable/ARMv8M/non_secure/portable/IAR/ARM_CM23/portasm.s
@@ -381,7 +381,7 @@
 	ldr r2, [r0]							/* The first item in the TCB is the top of the stack. */

 	ldr r1, [r2]							/* The first item on the stack is the task's xSecureContext. */

 	cmp r1, #0								/* Raise svc if task's xSecureContext is not NULL. */

-	beq free_secure_context

+	bne free_secure_context					/* Branch if r1 != 0. */

 	bx lr									/* There is no secure context (xSecureContext is NULL). */

 	free_secure_context:

 		svc 1								/* Secure context is freed in the supervisor call. portSVC_FREE_SECURE_CONTEXT = 1. */

diff --git a/portable/GCC/ARM_CM23/non_secure/portasm.c b/portable/GCC/ARM_CM23/non_secure/portasm.c
index 9dab039..b82bd63 100644
--- a/portable/GCC/ARM_CM23/non_secure/portasm.c
+++ b/portable/GCC/ARM_CM23/non_secure/portasm.c
@@ -449,7 +449,7 @@
         "	ldr r2, [r0]									\n"/* The first item in the TCB is the top of the stack. */

         "	ldr r1, [r2]									\n"/* The first item on the stack is the task's xSecureContext. */

         "	cmp r1, #0										\n"/* Raise svc if task's xSecureContext is not NULL. */

-        "	beq free_secure_context							\n"

+        "	bne free_secure_context							\n"/* Branch if r1 != 0. */

         "	bx lr											\n"/* There is no secure context (xSecureContext is NULL). */

         " free_secure_context:								\n"

         "	svc %0											\n"/* Secure context is freed in the supervisor call. */

diff --git a/portable/IAR/ARM_CM23/non_secure/portasm.s b/portable/IAR/ARM_CM23/non_secure/portasm.s
index 0f2c838..705889e 100644
--- a/portable/IAR/ARM_CM23/non_secure/portasm.s
+++ b/portable/IAR/ARM_CM23/non_secure/portasm.s
@@ -381,7 +381,7 @@
 	ldr r2, [r0]							/* The first item in the TCB is the top of the stack. */

 	ldr r1, [r2]							/* The first item on the stack is the task's xSecureContext. */

 	cmp r1, #0								/* Raise svc if task's xSecureContext is not NULL. */

-	beq free_secure_context

+	bne free_secure_context					/* Branch if r1 != 0. */

 	bx lr									/* There is no secure context (xSecureContext is NULL). */

 	free_secure_context:

 		svc 1								/* Secure context is freed in the supervisor call. portSVC_FREE_SECURE_CONTEXT = 1. */

diff --git a/tasks.c b/tasks.c
index 91af833..895675f 100644
--- a/tasks.c
+++ b/tasks.c
@@ -1219,7 +1219,6 @@
             {

                 --uxCurrentNumberOfTasks;

                 traceTASK_DELETE( pxTCB );

-                prvDeleteTCB( pxTCB );

 

                 /* Reset the next expected unblock time in case it referred to

                  * the task that has just been deleted. */

@@ -1228,6 +1227,14 @@
         }

         taskEXIT_CRITICAL();

 

+        /* If the task is not deleting itself, call prvDeleteTCB from outside of

+         * critical section. If a task deletes itself, prvDeleteTCB is called

+         * from prvCheckTasksWaitingTermination which is called from Idle task. */

+        if( pxTCB != pxCurrentTCB )

+        {

+            prvDeleteTCB( pxTCB );

+        }

+

         /* Force a reschedule if it is the currently running task that has just

          * been deleted. */

         if( xSchedulerRunning != pdFALSE )