1 ;/*
2 ; * FreeRTOS Kernel V11.1.0
3 ; * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 ; *
5 ; * SPDX-License-Identifier: MIT
6 ; *
7 ; * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 ; * this software and associated documentation files (the "Software"), to deal in
9 ; * the Software without restriction, including without limitation the rights to
10 ; * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 ; * the Software, and to permit persons to whom the Software is furnished to do so,
12 ; * subject to the following conditions:
13 ; *
14 ; * The above copyright notice and this permission notice shall be included in all
15 ; * copies or substantial portions of the Software.
16 ; *
17 ; * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 ; * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 ; * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 ; * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 ; * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 ; * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 ; *
24 ; * https://www.FreeRTOS.org
25 ; * https://github.com/FreeRTOS
26 ; *
27 ; */
28 
29     EXTERN  vTaskSwitchContext
30     EXTERN  ulCriticalNesting
31     EXTERN  pxCurrentTCB
32     EXTERN  ulPortTaskHasFPUContext
33     EXTERN  ulAsmAPIPriorityMask
34 
35 portSAVE_CONTEXT macro
36 
37     ; Save the LR and SPSR onto the system mode stack before switching to
38     ; system mode to save the remaining system mode registers
39     SRSDB   sp!, #SYS_MODE
40     CPS     #SYS_MODE
41     PUSH    {R0-R12, R14}
42 
43     ; Push the critical nesting count
44     LDR     R2, =ulCriticalNesting
45     LDR     R1, [R2]
46     PUSH    {R1}
47 
48     ; Does the task have a floating point context that needs saving?  If
49     ; ulPortTaskHasFPUContext is 0 then no.
50     LDR     R2, =ulPortTaskHasFPUContext
51     LDR     R3, [R2]
52     CMP     R3, #0
53 
54     ; Save the floating point context, if any
55     FMRXNE  R1,  FPSCR
56     VPUSHNE {D0-D15}
57     VPUSHNE {D16-D31}
58     PUSHNE  {R1}
59 
60     ; Save ulPortTaskHasFPUContext itself
61     PUSH    {R3}
62 
63     ; Save the stack pointer in the TCB
64     LDR     R0, =pxCurrentTCB
65     LDR     R1, [R0]
66     STR     SP, [R1]
67 
68     endm
69 
70 ; /**********************************************************************/
71 
72 portRESTORE_CONTEXT macro
73 
74     ; Set the SP to point to the stack of the task being restored.
75     LDR     R0, =pxCurrentTCB
76     LDR     R1, [R0]
77     LDR     SP, [R1]
78 
79     ; Is there a floating point context to restore?  If the restored
80     ; ulPortTaskHasFPUContext is zero then no.
81     LDR     R0, =ulPortTaskHasFPUContext
82     POP     {R1}
83     STR     R1, [R0]
84     CMP     R1, #0
85 
86     ; Restore the floating point context, if any
87     POPNE   {R0}
88     VPOPNE  {D16-D31}
89     VPOPNE  {D0-D15}
90     VMSRNE  FPSCR, R0
91 
92     ; Restore the critical section nesting depth
93     LDR     R0, =ulCriticalNesting
94     POP     {R1}
95     STR     R1, [R0]
96 
97     ; Ensure the priority mask is correct for the critical nesting depth
98     LDR     R2, =portICCPMR_PRIORITY_MASK_REGISTER_ADDRESS
99     CMP     R1, #0
100     MOVEQ   R4, #255
101     LDRNE   R4, =( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT )
102     STR     R4, [r2]
103 
104     ; Restore all system mode registers other than the SP (which is already
105     ; being used)
106     POP     {R0-R12, R14}
107 
108     ; Return to the task code, loading CPSR on the way.
109     RFEIA   sp!
110 
111     endm
112