2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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 /******************************************************************/
36 .macro portSAVE_CONTEXT
38 /* Make room for the context. First save the current status so it can be
39 manipulated, and the cause and EPC registers so their original values are
42 addiu sp, sp, -portCONTEXT_SIZE
45 /* Also save s6 and s5 so they can be used. Any nesting interrupts should
46 maintain the values of these registers across the ISR. */
49 sw k1, portSTATUS_STACK_LOCATION(sp)
51 /* Prepare to enable interrupts above the current priority. */
56 /* s5 is used as the frame pointer. */
59 /* Check the nesting count value. */
60 la k0, uxInterruptNesting
63 /* If the nesting count is 0 then swap to the the system stack, otherwise
64 the system stack is already being used. */
68 /* Swap to the system stack. */
72 /* Increment and save the nesting count. */
76 /* s6 holds the EPC value, this is saved after interrupts are re-enabled. */
79 /* Re-enable interrupts. */
82 /* Save the context into the space just created. s6 is saved again
83 here as it now contains the EPC value. No other s registers need be
103 sw s6, portEPC_STACK_LOCATION(s5)
106 /* s6 is used as a scratch register. */
112 /* Update the task stack pointer value if nesting is zero. */
113 la s6, uxInterruptNesting
119 /* Save the stack pointer. */
120 la s6, uxSavedTaskStackPointer
125 /******************************************************************/
126 .macro portRESTORE_CONTEXT
128 /* Restore the stack pointer from the TCB. This is only done if the
129 nesting count is 1. */
130 la s6, uxInterruptNesting
135 la s6, uxSavedTaskStackPointer
138 /* Restore the context. */
144 /* s6 is loaded as it was used as a scratch register and therefore saved
145 as part of the interrupt context. */
166 /* Protect access to the k registers, and others. */
170 /* Decrement the nesting count. */
171 la k0, uxInterruptNesting
176 lw k0, portSTATUS_STACK_LOCATION(s5)
177 lw k1, portEPC_STACK_LOCATION(s5)
179 /* Leave the stack in its original state. First load sp from s5, then
180 restore s5 from the stack. */
183 addiu sp, sp, portCONTEXT_SIZE