2 * FreeRTOS Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
28 /* Secure context includes. */
29 #include "secure_context.h"
31 /* Secure port macros. */
32 #include "secure_port_macros.h"
34 secureportNON_SECURE_CALLABLE void SecureContext_LoadContext( SecureContextHandle_t xSecureContextHandle )
36 /* xSecureContextHandle value is in r0. */
41 " mrs r1, ipsr \n"/* r1 = IPSR. */
42 " cbz r1, load_ctx_therad_mode \n"/* Do nothing if the processor is running in the Thread Mode. */
43 " ldmia r0!, {r1, r2} \n"/* r1 = xSecureContextHandle->pucCurrentStackPointer, r2 = xSecureContextHandle->pucStackLimit. */
44 #if ( configENABLE_MPU == 1 )
45 " ldmia r1!, {r3} \n"/* Read CONTROL register value from task's stack. r3 = CONTROL. */
46 " msr control, r3 \n"/* CONTROL = r3. */
47 #endif /* configENABLE_MPU */
48 " msr psplim, r2 \n"/* PSPLIM = r2. */
49 " msr psp, r1 \n"/* PSP = r1. */
51 " load_ctx_therad_mode: \n"
57 /*-----------------------------------------------------------*/
59 secureportNON_SECURE_CALLABLE void SecureContext_SaveContext( SecureContextHandle_t xSecureContextHandle )
61 /* xSecureContextHandle value is in r0. */
66 " mrs r1, ipsr \n"/* r1 = IPSR. */
67 " cbz r1, save_ctx_therad_mode \n"/* Do nothing if the processor is running in the Thread Mode. */
68 " mrs r1, psp \n"/* r1 = PSP. */
69 #if ( configENABLE_FPU == 1 )
70 " vstmdb r1!, {s0} \n"/* Trigger the defferred stacking of FPU registers. */
71 " vldmia r1!, {s0} \n"/* Nullify the effect of the pervious statement. */
72 #endif /* configENABLE_FPU */
73 #if ( configENABLE_MPU == 1 )
74 " mrs r2, control \n"/* r2 = CONTROL. */
75 " stmdb r1!, {r2} \n"/* Store CONTROL value on the stack. */
76 #endif /* configENABLE_MPU */
77 " str r1, [r0] \n"/* Save the top of stack in context. xSecureContextHandle->pucCurrentStackPointer = r1. */
78 " movs r1, %0 \n"/* r1 = securecontextNO_STACK. */
79 " msr psplim, r1 \n"/* PSPLIM = securecontextNO_STACK. */
80 " msr psp, r1 \n"/* PSP = securecontextNO_STACK i.e. No stack for thread mode until next task's context is loaded. */
82 " save_ctx_therad_mode: \n"
85 ::"i" ( securecontextNO_STACK ) : "r1", "memory"
88 /*-----------------------------------------------------------*/