Skip to content

Commit 45701e6

Browse files
gramsay0carlescufi
authored andcommitted
kernel: sched: Disable FPU context when thread ends
When `CONFIG_FPU_SHARING` is enabled each `k_thread` struct has a saved floating point context (`saved_fp_context`). During a context switch, the current FPU owner's (`_current_cpu->arch.fpu_owner`) registers are saved to its `saved_fp_context`, and the destination threads FPU registers are loaded from its `saved_fp_context`. When a thread ends, it does not release ownership of the FPU (`_current_cpu->arch.fpu_owner`). This is problematic if the `k_thread` struct was allocated on the stack. The next context switch will save the FPU registers into `k_thread -> saved_fp_context` which may now be out of scope. This will likely (but not always) result in a crash. Adding `arch_float_disable(thread);` when a thread ends disables preservation of floating point context information, fixing this issue Signed-off-by: Grant Ramsay <[email protected]>
1 parent 993cb30 commit 45701e6

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

kernel/sched.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,10 @@ static void end_thread(struct k_thread *thread)
17141714
unpend_all(&thread->join_queue);
17151715
update_cache(1);
17161716

1717+
#if defined(CONFIG_FPU) && defined(CONFIG_FPU_SHARING)
1718+
arch_float_disable(thread);
1719+
#endif
1720+
17171721
SYS_PORT_TRACING_FUNC(k_thread, sched_abort, thread);
17181722

17191723
z_thread_monitor_exit(thread);

0 commit comments

Comments
 (0)