Skip to content

Commit 4d9cd90

Browse files
authoredMar 6, 2025
RISCV Add FPU context save (FreeRTOS#1250)
* port: riscv: Split the number of registers and the size of the context * port: riscv: Create some macros for the FPU context * port: riscv: Add a couple of macros that store fpu context * port: riscv: Update the stack init function to include the fpu context size * port: riscv: Add a chip_specific_extensions file that includes the F extension * Update dictionary to include some risc-v instructions * port: riscv: Fix a few typos * port: riscv: Apply @aggarg's sugestions
1 parent 742729e commit 4d9cd90

File tree

4 files changed

+287
-123
lines changed

4 files changed

+287
-123
lines changed
 

‎.github/.cSpellWords.txt

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ AIRCR
2323
ALMIEN
2424
ALMV
2525
ANDC
26+
andi
2627
ANDCCR
2728
APIC
2829
APROCFREQ
@@ -47,6 +48,7 @@ bcpc
4748
BCPC
4849
beevt
4950
BEEVT
51+
beqz
5052
BERR
5153
bfextu
5254
Biagioni
@@ -298,6 +300,7 @@ FADD
298300
FCMD
299301
fcolor
300302
FCSE
303+
fcsr
301304
fdiagnostics
302305
fdiv
303306
FDIV

‎portable/GCC/RISC-V/chip_specific_extensions/Pulpino_Vega_RV32M1RM/freertos_risc_v_chip_specific_extensions.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,22 @@ csrr t2, lpcount0
8080
csrr t3, lpstart1
8181
csrr t4, lpend1
8282
csrr t5, lpcount1
83-
sw t0, 1 * portWORD_SIZE( sp )
84-
sw t1, 2 * portWORD_SIZE( sp )
85-
sw t2, 3 * portWORD_SIZE( sp )
86-
sw t3, 4 * portWORD_SIZE( sp )
87-
sw t4, 5 * portWORD_SIZE( sp )
88-
sw t5, 6 * portWORD_SIZE( sp )
83+
sw t0, 2 * portWORD_SIZE( sp )
84+
sw t1, 3 * portWORD_SIZE( sp )
85+
sw t2, 4 * portWORD_SIZE( sp )
86+
sw t3, 5 * portWORD_SIZE( sp )
87+
sw t4, 6 * portWORD_SIZE( sp )
88+
sw t5, 7 * portWORD_SIZE( sp )
8989
.endm
9090

9191
/* Restore the additional registers found on the Pulpino. */
9292
.macro portasmRESTORE_ADDITIONAL_REGISTERS
93-
lw t0, 1 * portWORD_SIZE( sp ) /* Load additional registers into accessible temporary registers. */
94-
lw t1, 2 * portWORD_SIZE( sp )
95-
lw t2, 3 * portWORD_SIZE( sp )
96-
lw t3, 4 * portWORD_SIZE( sp )
97-
lw t4, 5 * portWORD_SIZE( sp )
98-
lw t5, 6 * portWORD_SIZE( sp )
93+
lw t0, 2 * portWORD_SIZE( sp ) /* Load additional registers into accessible temporary registers. */
94+
lw t1, 3 * portWORD_SIZE( sp )
95+
lw t2, 4 * portWORD_SIZE( sp )
96+
lw t3, 5 * portWORD_SIZE( sp )
97+
lw t4, 6 * portWORD_SIZE( sp )
98+
lw t5, 7 * portWORD_SIZE( sp )
9999
csrw lpstart0, t0
100100
csrw lpend0, t1
101101
csrw lpcount0, t2

‎portable/GCC/RISC-V/portASM.S

+54-42
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ definitions. */
162162
* where the global and thread pointers are currently assumed to be constant so
163163
* are not saved:
164164
*
165-
* mstatus
166165
* xCriticalNesting
167166
* x31
168167
* x30
@@ -192,18 +191,12 @@ definitions. */
192191
* x6
193192
* x5
194193
* portTASK_RETURN_ADDRESS
194+
* [FPU registers (when enabled/available) go here]
195195
* [chip specific registers go here]
196+
* mstatus
196197
* pxCode
197198
*/
198199
pxPortInitialiseStack:
199-
csrr t0, mstatus /* Obtain current mstatus value. */
200-
andi t0, t0, ~0x8 /* Ensure interrupts are disabled when the stack is restored within an ISR. Required when a task is created after the schedulre has been started, otherwise interrupts would be disabled anyway. */
201-
addi t1, x0, 0x188 /* Generate the value 0x1880, which are the MPIE and MPP bits to set in mstatus. */
202-
slli t1, t1, 4
203-
or t0, t0, t1 /* Set MPIE and MPP bits in mstatus value. */
204-
205-
addi a0, a0, -portWORD_SIZE
206-
store_x t0, 0(a0) /* mstatus onto the stack. */
207200
addi a0, a0, -portWORD_SIZE /* Space for critical nesting count. */
208201
store_x x0, 0(a0) /* Critical nesting count starts at 0 for every task. */
209202

@@ -212,10 +205,12 @@ pxPortInitialiseStack:
212205
#else
213206
addi a0, a0, -(22 * portWORD_SIZE) /* Space for registers x10-x31. */
214207
#endif
215-
store_x a2, 0(a0) /* Task parameters (pvParameters parameter) goes into register X10/a0 on the stack. */
216-
addi a0, a0, -(6 * portWORD_SIZE) /* Space for registers x5-x9 + taskReturnAddress. */
208+
store_x a2, 0(a0) /* Task parameters (pvParameters parameter) goes into register x10/a0 on the stack. */
209+
210+
addi a0, a0, -(6 * portWORD_SIZE) /* Space for registers x5-x9 + taskReturnAddress (register x1). */
217211
load_x t0, xTaskReturnAddress
218212
store_x t0, 0(a0) /* Return address onto the stack. */
213+
219214
addi t0, x0, portasmADDITIONAL_CONTEXT_SIZE /* The number of chip specific additional registers. */
220215
chip_specific_stack_frame: /* First add any chip specific registers to the stack frame being created. */
221216
beq t0, x0, 1f /* No more chip specific registers to save. */
@@ -224,6 +219,23 @@ chip_specific_stack_frame: /* First add any chip specific registers
224219
addi t0, t0, -1 /* Decrement the count of chip specific registers remaining. */
225220
j chip_specific_stack_frame /* Until no more chip specific registers. */
226221
1:
222+
csrr t0, mstatus /* Obtain current mstatus value. */
223+
andi t0, t0, ~0x8 /* Ensure interrupts are disabled when the stack is restored within an ISR. Required when a task is created after the scheduler has been started, otherwise interrupts would be disabled anyway. */
224+
addi t1, x0, 0x188 /* Generate the value 0x1880, which are the MPIE=1 and MPP=M_Mode in mstatus. */
225+
slli t1, t1, 4
226+
or t0, t0, t1 /* Set MPIE and MPP bits in mstatus value. */
227+
228+
#if( configENABLE_FPU == 1 )
229+
/* Mark the FPU as clean in the mstatus value. */
230+
li t1, ~MSTATUS_FS_MASK
231+
and t0, t0, t1
232+
li t1, MSTATUS_FS_CLEAN
233+
or t0, t0, t1
234+
#endif
235+
236+
addi a0, a0, -portWORD_SIZE
237+
store_x t0, 0(a0) /* mstatus onto the stack. */
238+
227239
addi a0, a0, -portWORD_SIZE
228240
store_x a1, 0(a0) /* mret value (pxCode parameter) onto the stack. */
229241
ret
@@ -235,46 +247,46 @@ xPortStartFirstTask:
235247

236248
load_x x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */
237249

250+
load_x x5, 1 * portWORD_SIZE( sp ) /* Initial mstatus into x5 (t0). */
251+
addi x5, x5, 0x08 /* Set MIE bit so the first task starts with interrupts enabled - required as returns with ret not eret. */
252+
csrw mstatus, x5 /* Interrupts enabled from here! */
253+
238254
portasmRESTORE_ADDITIONAL_REGISTERS /* Defined in freertos_risc_v_chip_specific_extensions.h to restore any registers unique to the RISC-V implementation. */
239255

240-
load_x x7, 4 * portWORD_SIZE( sp ) /* t2 */
241-
load_x x8, 5 * portWORD_SIZE( sp ) /* s0/fp */
242-
load_x x9, 6 * portWORD_SIZE( sp ) /* s1 */
243-
load_x x10, 7 * portWORD_SIZE( sp ) /* a0 */
244-
load_x x11, 8 * portWORD_SIZE( sp ) /* a1 */
245-
load_x x12, 9 * portWORD_SIZE( sp ) /* a2 */
246-
load_x x13, 10 * portWORD_SIZE( sp ) /* a3 */
247-
load_x x14, 11 * portWORD_SIZE( sp ) /* a4 */
248-
load_x x15, 12 * portWORD_SIZE( sp ) /* a5 */
256+
load_x x7, 5 * portWORD_SIZE( sp ) /* t2 */
257+
load_x x8, 6 * portWORD_SIZE( sp ) /* s0/fp */
258+
load_x x9, 7 * portWORD_SIZE( sp ) /* s1 */
259+
load_x x10, 8 * portWORD_SIZE( sp ) /* a0 */
260+
load_x x11, 9 * portWORD_SIZE( sp ) /* a1 */
261+
load_x x12, 10 * portWORD_SIZE( sp ) /* a2 */
262+
load_x x13, 11 * portWORD_SIZE( sp ) /* a3 */
263+
load_x x14, 12 * portWORD_SIZE( sp ) /* a4 */
264+
load_x x15, 13 * portWORD_SIZE( sp ) /* a5 */
249265
#ifndef __riscv_32e
250-
load_x x16, 13 * portWORD_SIZE( sp ) /* a6 */
251-
load_x x17, 14 * portWORD_SIZE( sp ) /* a7 */
252-
load_x x18, 15 * portWORD_SIZE( sp ) /* s2 */
253-
load_x x19, 16 * portWORD_SIZE( sp ) /* s3 */
254-
load_x x20, 17 * portWORD_SIZE( sp ) /* s4 */
255-
load_x x21, 18 * portWORD_SIZE( sp ) /* s5 */
256-
load_x x22, 19 * portWORD_SIZE( sp ) /* s6 */
257-
load_x x23, 20 * portWORD_SIZE( sp ) /* s7 */
258-
load_x x24, 21 * portWORD_SIZE( sp ) /* s8 */
259-
load_x x25, 22 * portWORD_SIZE( sp ) /* s9 */
260-
load_x x26, 23 * portWORD_SIZE( sp ) /* s10 */
261-
load_x x27, 24 * portWORD_SIZE( sp ) /* s11 */
262-
load_x x28, 25 * portWORD_SIZE( sp ) /* t3 */
263-
load_x x29, 26 * portWORD_SIZE( sp ) /* t4 */
264-
load_x x30, 27 * portWORD_SIZE( sp ) /* t5 */
265-
load_x x31, 28 * portWORD_SIZE( sp ) /* t6 */
266+
load_x x16, 14 * portWORD_SIZE( sp ) /* a6 */
267+
load_x x17, 15 * portWORD_SIZE( sp ) /* a7 */
268+
load_x x18, 16 * portWORD_SIZE( sp ) /* s2 */
269+
load_x x19, 17 * portWORD_SIZE( sp ) /* s3 */
270+
load_x x20, 18 * portWORD_SIZE( sp ) /* s4 */
271+
load_x x21, 19 * portWORD_SIZE( sp ) /* s5 */
272+
load_x x22, 20 * portWORD_SIZE( sp ) /* s6 */
273+
load_x x23, 21 * portWORD_SIZE( sp ) /* s7 */
274+
load_x x24, 22 * portWORD_SIZE( sp ) /* s8 */
275+
load_x x25, 23 * portWORD_SIZE( sp ) /* s9 */
276+
load_x x26, 24 * portWORD_SIZE( sp ) /* s10 */
277+
load_x x27, 25 * portWORD_SIZE( sp ) /* s11 */
278+
load_x x28, 26 * portWORD_SIZE( sp ) /* t3 */
279+
load_x x29, 27 * portWORD_SIZE( sp ) /* t4 */
280+
load_x x30, 28 * portWORD_SIZE( sp ) /* t5 */
281+
load_x x31, 29 * portWORD_SIZE( sp ) /* t6 */
266282
#endif
267283

268284
load_x x5, portCRITICAL_NESTING_OFFSET * portWORD_SIZE( sp ) /* Obtain xCriticalNesting value for this task from task's stack. */
269285
load_x x6, pxCriticalNesting /* Load the address of xCriticalNesting into x6. */
270286
store_x x5, 0( x6 ) /* Restore the critical nesting value for this task. */
271287

272-
load_x x5, portMSTATUS_OFFSET * portWORD_SIZE( sp ) /* Initial mstatus into x5 (t0). */
273-
addi x5, x5, 0x08 /* Set MIE bit so the first task starts with interrupts enabled - required as returns with ret not eret. */
274-
csrrw x0, mstatus, x5 /* Interrupts enabled from here! */
275-
276-
load_x x5, 2 * portWORD_SIZE( sp ) /* Initial x5 (t0) value. */
277-
load_x x6, 3 * portWORD_SIZE( sp ) /* Initial x6 (t1) value. */
288+
load_x x5, 3 * portWORD_SIZE( sp ) /* Initial x5 (t0) value. */
289+
load_x x6, 4 * portWORD_SIZE( sp ) /* Initial x6 (t1) value. */
278290

279291
addi sp, sp, portCONTEXT_SIZE
280292
ret

0 commit comments

Comments
 (0)
Please sign in to comment.