# ARMv8-M in non secure context

**URL:** <https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954>\
**Category:** Kernel\
**Created:** [November 25, 2021, 12:40pm UTC](https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954 "2021-11-25T12:40:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![OliM](https://avatars.discourse-cdn.com/v4/letter/o/f04885/32.png) [@OliM](https://forums.freertos.org/u/OliM)\
**Post date:** [November 25, 2021, 12:40pm UTC](https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954/1 "2021-11-25T12:40:20Z")

</div>

I am a bit confused by the effect of the configENABLE\_TRUSTZONE flag.  
According to this (sorry for the spaces but apparently even internal links are blocked for new users):  
https: // www .freertos.org /2020/04/ using-freertos-on-armv8-m-microcontrollers .html  
I should set this also to enabled if I want to run FreeRTOS in the nonsecure world but be able to call functions from an RTOS-less(!) secure world.  
But what I get (in V10.4.x) are dependencies on secure\_context and secure\_init, which from my understanding consist of function entry points to be defined in the secure world (since they use the cmse\_nonsecure\_entry attribute).  
So do I have to put those c-files in my secure project even though I do not want to have FreeRTOS there or should I actually disable the configENABLE\_TRUSTZONE flag for my usecase?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 25, 2021, 7:35pm UTC](https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954/2 "2021-11-25T19:35:07Z")

</div>

The build process on ARMv8-M is somewhat tricky compared to ARMv7-M, so I recommend reading some of the articles provided by Arm or tool vendors to understand how to create your secure and non secure projects so that one can call into the other. I’m not sure which tools you are using, but some include built-in workflows that make it simpler.

Most use cases will run FreeRTOS on the non-secure side and non RTOS code on the secure side. For example: [https://freertos.org/2020/07/security-for-arm-cortex-m-devices-with-freertos.html](https://freertos.org/2020/07/security-for-arm-cortex-m-devices-with-freertos.html). To do that you must have configENABLE\_TRUSTZONE set to 1. The hardware then enforces security by only allowing calls from non secure code to secure code through the secure gateway instruction - by design there is no software way around that so you need to see how your tools enable you to create those secure gateway entry points and place them in the correct place in memory.

The FreeRTOS download contains a few examples - which tools are you using?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 25, 2021, 10:37pm UTC](https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954/3 "2021-11-25T22:37:30Z")

</div>

> [@OliM](#):
>
> So do I have to put those c-files in my secure project even though I do not want to have FreeRTOS there or should I actually disable the configENABLE\_TRUSTZONE flag for my usecase?

If you want to run FreeRTOS on the non-secure side and want to call the functions exported from the secure side, you need to set `configENABLE_TRUSTZONE` to 1. Assuming that you are using GCC and Cortex-M33, you need to compile the following files:

- Secure Project - [https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM\_CM33/secure](https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM_CM33/secure)
- Non-Secure Project - [https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM\_CM33/non\_secure](https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM_CM33/non_secure)

On the other hand, if you do not want to use TrustZone to divide the environment into secure and non-secure, you have the following 2 options:

1. Disable the TrustZone in hardware so that the MCU boots as non-secure and always remains non-secure. In this case, you need to compile the following files: [https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM\_CM33\_NTZ/non\_secure](https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM_CM33_NTZ/non_secure)
2. Do not disable TrustZone in hardware but instead run the FreeRTOS on the secure side and never jump to non-secure side. This way the MCU boots as secure and always remains secure. You will need to set configRUN\_FREERTOS\_SECURE\_ONLY to 1 and compile the same files as above: [https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM\_CM33\_NTZ/non\_secure](https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM_CM33_NTZ/non_secure)

Thanks.

---

<div class="post-metadata">

**Author:** ![OliM](https://avatars.discourse-cdn.com/v4/letter/o/f04885/32.png) [@OliM](https://forums.freertos.org/u/OliM)\
**Post date:** [November 26, 2021, 7:35am UTC](https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954/4 "2021-11-26T07:35:26Z")

</div>

I am using an STM32L5 in CubeIDE with GCC. What I wanted to confirm, and as I understand it that’s what you did confirm @aggarg, is that I actually need to build the secure part into the secure project. Even though I do not want to have FreeRTOS tasks in the secure space but only call separate secure functions from a non secure FreeRTOS task, which outside of FreeRTOS is normally possible with the cmse\_nonsecure\_entry bridge alone.  
I guess this is about stable context switching inside FreeRTOS when preemption and/or TimeSlicing are active? What about a theoretical setup where FreeRTOS runs in non secure world but never needs to access the secure world at all(secure boot only)?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 26, 2021, 8:08am UTC](https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954/5 "2021-11-26T08:08:20Z")

</div>

> [@OliM](#):
>
> I guess this is about stable context switching inside FreeRTOS when preemption and/or TimeSlicing are active?

Yes, that is correct. You need to compile those files in the secure project so that FreeRTOS can manage secure context associated with a task (a task might be preempted when it was running on the secure side).

> [@OliM](#):
>
> What about a theoretical setup where FreeRTOS runs in non secure world but never needs to access the secure world at all(secure boot only)?

This is essentially the case of [using FreeRTOS without TrustZone support](https://www.freertos.org/2020/04/using-freertos-on-armv8-m-microcontrollers.html#FREERTOS_WITHOUT_TRUSTZONE). You need to compile the following files in the non-secure project - [https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM\_CM33\_NTZ/non\_secure](https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM_CM33_NTZ/non_secure)

Thanks.

---

<div class="post-metadata">

**Author:** ![OliM](https://avatars.discourse-cdn.com/v4/letter/o/f04885/32.png) [@OliM](https://forums.freertos.org/u/OliM)\
**Post date:** [November 26, 2021, 8:22am UTC](https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954/6 "2021-11-26T08:22:37Z")

</div>

(that link limit is really getting on my nerves, so a quote without links)

> [@aggarg](#):
>
> This is essentially the case of using FreeRTOS without TrustZone support. You need to compile the following files in the non-secure project: ARM\_CM33\_NTZ/non\_secure

That’s what I expected as well. I think than I’ve got it figured out now 🙂

Thank you for the fast support by the way.

---

<div class="post-metadata">

**Author:** ![OliM](https://avatars.discourse-cdn.com/v4/letter/o/f04885/32.png) [@OliM](https://forums.freertos.org/u/OliM)\
**Post date:** [December 10, 2021, 1:04pm UTC](https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954/7 "2021-12-10T13:04:45Z")

</div>

Now I’ve finally come around to have my bootloader in a state that I am actually doing the context switch etc. and can boot from secure to unsecure.  
But the FreeRTOS enabled application, which runs ok with disabled Trustzone, crashes while calling svc to start up the scheduler.

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/7/7f2dde2d015b54a41af942bfa68b9735927f7f66.png)  
The following HardFault exception I get is back in secure state, which makes no sense at all to me since svc should be banked. Any ideas what I am missing?

EDIT: Ok, looks like this was a side effect from having the interrupts globally disabled before jumping to the application. Interestingly enough this was not a problem while starting from unsecure to unsecure. ( [someone had a similar problem 10 month ago](https://community.arm.com/support-forums/f/architectures-and-processors-forum/48668/cortex-m33---svc-call-from-non-secure-code-does-not-trigger-non-secure-svc-exception) )

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [December 11, 2021, 9:08pm UTC](https://forums.freertos.org/t/armv8-m-in-non-secure-context/13954/8 "2021-12-11T21:08:04Z")

</div>

Glad that it worked for you. Thank you for taking time to report your solution.
