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
32 #ifndef configENABLE_FPU
33 #define configENABLE_FPU 0
36 #if __riscv_xlen == 64
37 #define portWORD_SIZE 8
40 #elif __riscv_xlen == 32
43 #define portWORD_SIZE 4
45 #error Assembler did not define __riscv_xlen
48 #include "freertos_risc_v_chip_specific_extensions.h"
50 /* Only the standard core registers are stored by default. Any additional
51 * registers must be saved by the portasmSAVE_ADDITIONAL_REGISTERS and
52 * portasmRESTORE_ADDITIONAL_REGISTERS macros - which can be defined in a chip
53 * specific version of freertos_risc_v_chip_specific_extensions.h. See the
54 * notes at the top of portASM.S file. */
56 #define portCONTEXT_SIZE ( 15 * portWORD_SIZE )
57 #define portCRITICAL_NESTING_OFFSET 14
59 #define portCONTEXT_SIZE ( 31 * portWORD_SIZE )
60 #define portCRITICAL_NESTING_OFFSET 30
63 #if ( configENABLE_FPU == 1 )
64 /* Bit [14:13] in the mstatus encode the status of FPU state which is one of
65 * the following values:
66 * 1. Value: 0, Meaning: Off.
67 * 2. Value: 1, Meaning: Initial.
68 * 3. Value: 2, Meaning: Clean.
69 * 4. Value: 3, Meaning: Dirty.
71 #define MSTATUS_FS_MASK 0x6000
72 #define MSTATUS_FS_INITIAL 0x2000
73 #define MSTATUS_FS_CLEAN 0x4000
74 #define MSTATUS_FS_DIRTY 0x6000
75 #define MSTATUS_FS_OFFSET 13
78 #if __riscv_flen == 32
81 #elif __riscv_flen == 64
85 #error Assembler did not define __riscv_flen
88 #define portFPU_REG_SIZE ( __riscv_flen / 8 )
89 #define portFPU_REG_COUNT 33 /* 32 Floating point registers plus one CSR. */
90 #define portFPU_REG_OFFSET( regIndex ) ( ( 2 * portWORD_SIZE ) + ( regIndex * portFPU_REG_SIZE ) )
91 #define portFPU_CONTEXT_SIZE ( portFPU_REG_SIZE * portFPU_REG_COUNT )
93 #error configENABLE_FPU must not be set to 1 if the hardwar does not have FPU
96 /*-----------------------------------------------------------*/
100 .extern xCriticalNesting
101 .extern pxCriticalNesting
102 /*-----------------------------------------------------------*/
104 .macro portcontexSAVE_FPU_CONTEXT
105 addi sp, sp, -( portFPU_CONTEXT_SIZE )
106 /* Store the FPU registers. */
107 store_f f0, portFPU_REG_OFFSET( 0 )( sp )
108 store_f f1, portFPU_REG_OFFSET( 1 )( sp )
109 store_f f2, portFPU_REG_OFFSET( 2 )( sp )
110 store_f f3, portFPU_REG_OFFSET( 3 )( sp )
111 store_f f4, portFPU_REG_OFFSET( 4 )( sp )
112 store_f f5, portFPU_REG_OFFSET( 5 )( sp )
113 store_f f6, portFPU_REG_OFFSET( 6 )( sp )
114 store_f f7, portFPU_REG_OFFSET( 7 )( sp )
115 store_f f8, portFPU_REG_OFFSET( 8 )( sp )
116 store_f f9, portFPU_REG_OFFSET( 9 )( sp )
117 store_f f10, portFPU_REG_OFFSET( 10 )( sp )
118 store_f f11, portFPU_REG_OFFSET( 11 )( sp )
119 store_f f12, portFPU_REG_OFFSET( 12 )( sp )
120 store_f f13, portFPU_REG_OFFSET( 13 )( sp )
121 store_f f14, portFPU_REG_OFFSET( 14 )( sp )
122 store_f f15, portFPU_REG_OFFSET( 15 )( sp )
123 store_f f16, portFPU_REG_OFFSET( 16 )( sp )
124 store_f f17, portFPU_REG_OFFSET( 17 )( sp )
125 store_f f18, portFPU_REG_OFFSET( 18 )( sp )
126 store_f f19, portFPU_REG_OFFSET( 19 )( sp )
127 store_f f20, portFPU_REG_OFFSET( 20 )( sp )
128 store_f f21, portFPU_REG_OFFSET( 21 )( sp )
129 store_f f22, portFPU_REG_OFFSET( 22 )( sp )
130 store_f f23, portFPU_REG_OFFSET( 23 )( sp )
131 store_f f24, portFPU_REG_OFFSET( 24 )( sp )
132 store_f f25, portFPU_REG_OFFSET( 25 )( sp )
133 store_f f26, portFPU_REG_OFFSET( 26 )( sp )
134 store_f f27, portFPU_REG_OFFSET( 27 )( sp )
135 store_f f28, portFPU_REG_OFFSET( 28 )( sp )
136 store_f f29, portFPU_REG_OFFSET( 29 )( sp )
137 store_f f30, portFPU_REG_OFFSET( 30 )( sp )
138 store_f f31, portFPU_REG_OFFSET( 31 )( sp )
140 store_x t0, portFPU_REG_OFFSET( 32 )( sp )
142 /*-----------------------------------------------------------*/
144 .macro portcontextRESTORE_FPU_CONTEXT
145 /* Restore the FPU registers. */
146 load_f f0, portFPU_REG_OFFSET( 0 )( sp )
147 load_f f1, portFPU_REG_OFFSET( 1 )( sp )
148 load_f f2, portFPU_REG_OFFSET( 2 )( sp )
149 load_f f3, portFPU_REG_OFFSET( 3 )( sp )
150 load_f f4, portFPU_REG_OFFSET( 4 )( sp )
151 load_f f5, portFPU_REG_OFFSET( 5 )( sp )
152 load_f f6, portFPU_REG_OFFSET( 6 )( sp )
153 load_f f7, portFPU_REG_OFFSET( 7 )( sp )
154 load_f f8, portFPU_REG_OFFSET( 8 )( sp )
155 load_f f9, portFPU_REG_OFFSET( 9 )( sp )
156 load_f f10, portFPU_REG_OFFSET( 10 )( sp )
157 load_f f11, portFPU_REG_OFFSET( 11 )( sp )
158 load_f f12, portFPU_REG_OFFSET( 12 )( sp )
159 load_f f13, portFPU_REG_OFFSET( 13 )( sp )
160 load_f f14, portFPU_REG_OFFSET( 14 )( sp )
161 load_f f15, portFPU_REG_OFFSET( 15 )( sp )
162 load_f f16, portFPU_REG_OFFSET( 16 )( sp )
163 load_f f17, portFPU_REG_OFFSET( 17 )( sp )
164 load_f f18, portFPU_REG_OFFSET( 18 )( sp )
165 load_f f19, portFPU_REG_OFFSET( 19 )( sp )
166 load_f f20, portFPU_REG_OFFSET( 20 )( sp )
167 load_f f21, portFPU_REG_OFFSET( 21 )( sp )
168 load_f f22, portFPU_REG_OFFSET( 22 )( sp )
169 load_f f23, portFPU_REG_OFFSET( 23 )( sp )
170 load_f f24, portFPU_REG_OFFSET( 24 )( sp )
171 load_f f25, portFPU_REG_OFFSET( 25 )( sp )
172 load_f f26, portFPU_REG_OFFSET( 26 )( sp )
173 load_f f27, portFPU_REG_OFFSET( 27 )( sp )
174 load_f f28, portFPU_REG_OFFSET( 28 )( sp )
175 load_f f29, portFPU_REG_OFFSET( 29 )( sp )
176 load_f f30, portFPU_REG_OFFSET( 30 )( sp )
177 load_f f31, portFPU_REG_OFFSET( 31 )( sp )
178 load_x t0, portFPU_REG_OFFSET( 32 )( sp )
180 addi sp, sp, ( portFPU_CONTEXT_SIZE )
182 /*-----------------------------------------------------------*/
184 .macro portcontextSAVE_CONTEXT_INTERNAL
185 addi sp, sp, -portCONTEXT_SIZE
186 store_x x1, 2 * portWORD_SIZE( sp )
187 store_x x5, 3 * portWORD_SIZE( sp )
188 store_x x6, 4 * portWORD_SIZE( sp )
189 store_x x7, 5 * portWORD_SIZE( sp )
190 store_x x8, 6 * portWORD_SIZE( sp )
191 store_x x9, 7 * portWORD_SIZE( sp )
192 store_x x10, 8 * portWORD_SIZE( sp )
193 store_x x11, 9 * portWORD_SIZE( sp )
194 store_x x12, 10 * portWORD_SIZE( sp )
195 store_x x13, 11 * portWORD_SIZE( sp )
196 store_x x14, 12 * portWORD_SIZE( sp )
197 store_x x15, 13 * portWORD_SIZE( sp )
199 store_x x16, 14 * portWORD_SIZE( sp )
200 store_x x17, 15 * portWORD_SIZE( sp )
201 store_x x18, 16 * portWORD_SIZE( sp )
202 store_x x19, 17 * portWORD_SIZE( sp )
203 store_x x20, 18 * portWORD_SIZE( sp )
204 store_x x21, 19 * portWORD_SIZE( sp )
205 store_x x22, 20 * portWORD_SIZE( sp )
206 store_x x23, 21 * portWORD_SIZE( sp )
207 store_x x24, 22 * portWORD_SIZE( sp )
208 store_x x25, 23 * portWORD_SIZE( sp )
209 store_x x26, 24 * portWORD_SIZE( sp )
210 store_x x27, 25 * portWORD_SIZE( sp )
211 store_x x28, 26 * portWORD_SIZE( sp )
212 store_x x29, 27 * portWORD_SIZE( sp )
213 store_x x30, 28 * portWORD_SIZE( sp )
214 store_x x31, 29 * portWORD_SIZE( sp )
215 #endif /* ifndef __riscv_32e */
217 load_x t0, xCriticalNesting /* Load the value of xCriticalNesting into t0. */
218 store_x t0, portCRITICAL_NESTING_OFFSET * portWORD_SIZE( sp ) /* Store the critical nesting value to the stack. */
220 #if( configENABLE_FPU == 1 )
222 srl t1, t0, MSTATUS_FS_OFFSET
225 bne t1, t2, 1f /* If FPU status is not dirty, do not save FPU registers. */
227 portcontexSAVE_FPU_CONTEXT
231 portasmSAVE_ADDITIONAL_REGISTERS /* Defined in freertos_risc_v_chip_specific_extensions.h to save any registers unique to the RISC-V implementation. */
234 store_x t0, 1 * portWORD_SIZE( sp )
236 #if( configENABLE_FPU == 1 )
237 /* Mark the FPU as clean, if it was dirty and we saved FPU registers. */
238 srl t1, t0, MSTATUS_FS_OFFSET
243 li t1, ~MSTATUS_FS_MASK
245 li t1, MSTATUS_FS_CLEAN
251 load_x t0, pxCurrentTCB /* Load pxCurrentTCB. */
252 store_x sp, 0 ( t0 ) /* Write sp to first TCB member. */
255 /*-----------------------------------------------------------*/
257 .macro portcontextSAVE_EXCEPTION_CONTEXT
258 portcontextSAVE_CONTEXT_INTERNAL
261 addi a1, a1, 4 /* Synchronous so update exception return address to the instruction after the instruction that generated the exception. */
262 store_x a1, 0 ( sp ) /* Save updated exception return address. */
263 load_x sp, xISRStackTop /* Switch to ISR stack. */
265 /*-----------------------------------------------------------*/
267 .macro portcontextSAVE_INTERRUPT_CONTEXT
268 portcontextSAVE_CONTEXT_INTERNAL
271 store_x a1, 0 ( sp ) /* Asynchronous interrupt so save unmodified exception return address. */
272 load_x sp, xISRStackTop /* Switch to ISR stack. */
274 /*-----------------------------------------------------------*/
276 .macro portcontextRESTORE_CONTEXT
277 load_x t1, pxCurrentTCB /* Load pxCurrentTCB. */
278 load_x sp, 0 ( t1 ) /* Read sp from first TCB member. */
280 /* Load mepc with the address of the instruction in the task to run next. */
284 /* Restore mstatus register. */
285 load_x t0, 1 * portWORD_SIZE( sp )
288 /* Defined in freertos_risc_v_chip_specific_extensions.h to restore any registers unique to the RISC-V implementation. */
289 portasmRESTORE_ADDITIONAL_REGISTERS
291 #if( configENABLE_FPU == 1 )
293 srl t1, t0, MSTATUS_FS_OFFSET
296 bne t1, t2, 3f /* If FPU status is not dirty, do not restore FPU registers. */
298 portcontextRESTORE_FPU_CONTEXT
300 #endif /* ifdef portasmSTORE_FPU_CONTEXT */
302 load_x t0, portCRITICAL_NESTING_OFFSET * portWORD_SIZE( sp ) /* Obtain xCriticalNesting value for this task from task's stack. */
303 load_x t1, pxCriticalNesting /* Load the address of xCriticalNesting into t1. */
304 store_x t0, 0 ( t1 ) /* Restore the critical nesting value for this task. */
306 load_x x1, 2 * portWORD_SIZE( sp )
307 load_x x5, 3 * portWORD_SIZE( sp )
308 load_x x6, 4 * portWORD_SIZE( sp )
309 load_x x7, 5 * portWORD_SIZE( sp )
310 load_x x8, 6 * portWORD_SIZE( sp )
311 load_x x9, 7 * portWORD_SIZE( sp )
312 load_x x10, 8 * portWORD_SIZE( sp )
313 load_x x11, 9 * portWORD_SIZE( sp )
314 load_x x12, 10 * portWORD_SIZE( sp )
315 load_x x13, 11 * portWORD_SIZE( sp )
316 load_x x14, 12 * portWORD_SIZE( sp )
317 load_x x15, 13 * portWORD_SIZE( sp )
319 load_x x16, 14 * portWORD_SIZE( sp )
320 load_x x17, 15 * portWORD_SIZE( sp )
321 load_x x18, 16 * portWORD_SIZE( sp )
322 load_x x19, 17 * portWORD_SIZE( sp )
323 load_x x20, 18 * portWORD_SIZE( sp )
324 load_x x21, 19 * portWORD_SIZE( sp )
325 load_x x22, 20 * portWORD_SIZE( sp )
326 load_x x23, 21 * portWORD_SIZE( sp )
327 load_x x24, 22 * portWORD_SIZE( sp )
328 load_x x25, 23 * portWORD_SIZE( sp )
329 load_x x26, 24 * portWORD_SIZE( sp )
330 load_x x27, 25 * portWORD_SIZE( sp )
331 load_x x28, 26 * portWORD_SIZE( sp )
332 load_x x29, 27 * portWORD_SIZE( sp )
333 load_x x30, 28 * portWORD_SIZE( sp )
334 load_x x31, 29 * portWORD_SIZE( sp )
335 #endif /* ifndef __riscv_32e */
336 addi sp, sp, portCONTEXT_SIZE
340 /*-----------------------------------------------------------*/
342 #endif /* PORTCONTEXT_H */