]> begriffs open source - freertos/blob - portable/GCC/RISC-V/portContext.h
FreeRTOS MPU: Remove MPU region number check (#1261)
[freertos] / portable / GCC / RISC-V / portContext.h
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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 #ifndef PORTCONTEXT_H
30 #define PORTCONTEXT_H
31
32 #ifndef configENABLE_FPU
33     #define configENABLE_FPU 0
34 #endif
35
36 #if __riscv_xlen == 64
37     #define portWORD_SIZE    8
38     #define store_x          sd
39     #define load_x           ld
40 #elif __riscv_xlen == 32
41     #define store_x          sw
42     #define load_x           lw
43     #define portWORD_SIZE    4
44 #else
45     #error Assembler did not define __riscv_xlen
46 #endif
47
48 #include "freertos_risc_v_chip_specific_extensions.h"
49
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. */
55 #ifdef __riscv_32e
56     #define portCONTEXT_SIZE               ( 15 * portWORD_SIZE )
57     #define portCRITICAL_NESTING_OFFSET    14
58 #else
59     #define portCONTEXT_SIZE               ( 31 * portWORD_SIZE )
60     #define portCRITICAL_NESTING_OFFSET    30
61 #endif
62
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.
70      */
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
76
77     #ifdef __riscv_fdiv
78         #if __riscv_flen == 32
79             #define load_f                  flw
80             #define store_f                 fsw
81         #elif __riscv_flen == 64
82             #define load_f                  fld
83             #define store_f                 fsd
84         #else
85             #error Assembler did not define __riscv_flen
86         #endif
87
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 )
92     #else
93         #error configENABLE_FPU must not be set to 1 if the hardwar does not have FPU
94     #endif
95 #endif
96 /*-----------------------------------------------------------*/
97
98 .extern pxCurrentTCB
99 .extern xISRStackTop
100 .extern xCriticalNesting
101 .extern pxCriticalNesting
102 /*-----------------------------------------------------------*/
103
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 )
139 csrr t0, fcsr
140 store_x t0,  portFPU_REG_OFFSET( 32 )( sp )
141     .endm
142 /*-----------------------------------------------------------*/
143
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 )
179 csrw fcsr, t0
180 addi sp, sp, ( portFPU_CONTEXT_SIZE )
181     .endm
182 /*-----------------------------------------------------------*/
183
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 )
198 #ifndef __riscv_32e
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 */
216
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. */
219
220 #if( configENABLE_FPU == 1 )
221     csrr t0, mstatus
222     srl t1, t0, MSTATUS_FS_OFFSET
223     andi t1, t1, 3
224     addi t2, x0, 3
225     bne t1, t2, 1f /* If FPU status is not dirty, do not save FPU registers. */
226
227     portcontexSAVE_FPU_CONTEXT
228 1:
229 #endif
230
231 portasmSAVE_ADDITIONAL_REGISTERS /* Defined in freertos_risc_v_chip_specific_extensions.h to save any registers unique to the RISC-V implementation. */
232
233 csrr t0, mstatus
234 store_x t0, 1 * portWORD_SIZE( sp )
235
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
239     andi t1, t1, 3
240     addi t2, x0, 3
241     bne t1, t2, 2f
242
243     li t1, ~MSTATUS_FS_MASK
244     and t0, t0, t1
245     li t1, MSTATUS_FS_CLEAN
246     or t0, t0, t1
247     csrw mstatus, t0
248 2:
249 #endif
250
251 load_x t0, pxCurrentTCB          /* Load pxCurrentTCB. */
252 store_x sp, 0 ( t0 )             /* Write sp to first TCB member. */
253
254    .endm
255 /*-----------------------------------------------------------*/
256
257    .macro portcontextSAVE_EXCEPTION_CONTEXT
258 portcontextSAVE_CONTEXT_INTERNAL
259 csrr a0, mcause
260 csrr a1, mepc
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. */
264    .endm
265 /*-----------------------------------------------------------*/
266
267    .macro portcontextSAVE_INTERRUPT_CONTEXT
268 portcontextSAVE_CONTEXT_INTERNAL
269 csrr a0, mcause
270 csrr a1, mepc
271 store_x a1, 0 ( sp )    /* Asynchronous interrupt so save unmodified exception return address. */
272 load_x sp, xISRStackTop /* Switch to ISR stack. */
273    .endm
274 /*-----------------------------------------------------------*/
275
276    .macro portcontextRESTORE_CONTEXT
277 load_x t1, pxCurrentTCB /* Load pxCurrentTCB. */
278 load_x sp, 0 ( t1 )     /* Read sp from first TCB member. */
279
280 /* Load mepc with the address of the instruction in the task to run next. */
281 load_x t0, 0 ( sp )
282 csrw mepc, t0
283
284 /* Restore mstatus register. */
285 load_x t0, 1 * portWORD_SIZE( sp )
286 csrw mstatus, t0
287
288 /* Defined in freertos_risc_v_chip_specific_extensions.h to restore any registers unique to the RISC-V implementation. */
289 portasmRESTORE_ADDITIONAL_REGISTERS
290
291 #if( configENABLE_FPU == 1 )
292     csrr t0, mstatus
293     srl t1, t0, MSTATUS_FS_OFFSET
294     andi t1, t1, 3
295     addi t2, x0, 3
296     bne t1, t2, 3f /* If FPU status is not dirty, do not restore FPU registers. */
297
298     portcontextRESTORE_FPU_CONTEXT
299 3:
300 #endif /* ifdef portasmSTORE_FPU_CONTEXT */
301
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. */
305
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 )
318 #ifndef __riscv_32e
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
337
338 mret
339    .endm
340 /*-----------------------------------------------------------*/
341
342 #endif /* PORTCONTEXT_H */