Skip to content

Commit 4dc072c

Browse files
CopilotBernardXiong
andcommitted
[libcpu][cortex-a] Improve FPU stack initialization implementation
Co-authored-by: BernardXiong <[email protected]>
1 parent 3241912 commit 4dc072c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

libcpu/arm/cortex-a/cpuport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ struct rt_hw_stack
7272
#define E_Bit (1<<9)
7373
#define J_Bit (1<<24)
7474

75+
/* VFP/NEON register count for FPU context */
76+
#ifndef VFP_DATA_NR
77+
#define VFP_DATA_NR 64 /* 32 double-precision registers = 64 words */
78+
#endif
79+
7580
#ifdef RT_USING_SMP
7681
typedef union {
7782
unsigned long slock;

libcpu/arm/cortex-a/stack.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
3131
rt_uint8_t *stack_addr, void *texit)
3232
{
3333
rt_uint32_t *stk;
34+
#ifdef RT_USING_FPU
35+
rt_uint32_t i;
36+
#endif
3437

3538
stack_addr += sizeof(rt_uint32_t);
3639
stack_addr = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stack_addr, 8);
@@ -61,7 +64,15 @@ rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
6164
*(--stk) = 0; /* user sp*/
6265
#endif
6366
#ifdef RT_USING_FPU
64-
*(--stk) = 0; /* not use fpu*/
67+
/* FPU context initialization matches context_gcc.S restore order:
68+
* Stack layout (high to low): FPEXC -> FPSCR -> D16-D31 -> D0-D15
69+
*/
70+
for (i = 0; i < VFP_DATA_NR; i++)
71+
{
72+
*(--stk) = 0; /* Initialize D0-D31 (64 words for 32 double regs) */
73+
}
74+
*(--stk) = 0; /* FPSCR: Floating-Point Status and Control Register */
75+
*(--stk) = 0x40000000; /* FPEXC: Enable FPU (bit 30 = EN) */
6576
#endif
6677

6778
/* return task's current stack address */

0 commit comments

Comments
 (0)