2 * FreeRTOS Kernel V11.2.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 #include "FreeRTOSConfig.h"
31 #define portCONTEXT_SIZE 132
32 #define portEPC_STACK_LOCATION 124
33 #define portSTATUS_STACK_LOCATION 128
35 #ifdef __LANGUAGE_ASSEMBLY__
37 /******************************************************************/
38 .macro portSAVE_CONTEXT
40 /* Make room for the context. First save the current status so it can be
41 manipulated, and the cause and EPC registers so their original values are
44 addiu sp, sp, -portCONTEXT_SIZE
47 /* Also save s6 and s5 so they can be used. Any nesting interrupts should
48 maintain the values of these registers across the ISR. */
51 sw k1, portSTATUS_STACK_LOCATION(sp)
53 /* Prepare to enable interrupts above the current priority.
54 k0 = k0 >> 10. Moves RIPL[17:10] to [7:0] */
57 /* Insert bit field. 7 bits k0[6:0] to k1[16:10] */
60 /* Sets CP0.Status.IPL = CP0.Cause.RIPL
61 Copy the MSB of the IPL, but it would be an error if it was set anyway. */
64 /* MSB of IPL is bit[18] of CP0.Status */
67 /* CP0.Status[5:1] = 0 b[5]=Rsvd, b[4]=UM,
68 b[3]=Rsvd, b[2]=ERL, b[1]=EXL
69 Setting EXL=0 allows higher priority interrupts
70 to preempt this handler */
74 /* s5 is used as the frame pointer. */
77 /* Check the nesting count value. */
78 la k0, uxInterruptNesting
81 /* If the nesting count is 0 then swap to the the system stack, otherwise
82 the system stack is already being used. */
86 /* Swap to the system stack. */
90 /* Increment and save the nesting count. */
94 /* s6 holds the EPC value, this is saved after interrupts are re-enabled. */
97 /* Re-enable interrupts. */
100 /* Save the context into the space just created. s6 is saved again
101 here as it now contains the EPC value. No other s registers need be
103 sw ra, 120(s5) /* Return address (RA=R31) */
104 sw s8, 116(s5) /* Frame Pointer (FP=R30) */
121 sw s6, portEPC_STACK_LOCATION(s5)
124 /* MEC14xx does not have DSP, removed 7 words */
130 /* Update the task stack pointer value if nesting is zero. */
131 la s6, uxInterruptNesting
137 /* Save the stack pointer. */
138 la s6, uxSavedTaskStackPointer
143 /******************************************************************/
144 .macro portRESTORE_CONTEXT
146 /* Restore the stack pointer from the TCB. This is only done if the
147 nesting count is 1. */
148 la s6, uxInterruptNesting
153 la s6, uxSavedTaskStackPointer
156 /* Restore the context.
157 MCHP MEC14xx does not include DSP */
165 /* s6 is loaded as it was used as a scratch register and therefore saved
166 as part of the interrupt context. */
187 /* Protect access to the k registers, and others. */
191 /* Decrement the nesting count. */
192 la k0, uxInterruptNesting
197 lw k0, portSTATUS_STACK_LOCATION(s5)
198 lw k1, portEPC_STACK_LOCATION(s5)
200 /* Leave the stack in its original state. First load sp from s5, then
201 restore s5 from the stack. */
204 addiu sp, sp, portCONTEXT_SIZE
214 #endif /* #ifdef __LANGUAGE_ASSEMBLY__ */