Skip to content

Commit 56be5ab

Browse files
maribuderMihai
andcommitted
cpu/native: fix FPU/SSE register related context switching issues
When the `float_math` feature is not in used, this adds CFLAGS to instruct the compiler to not make use of FPU/SSE registers, fixing issues (random crashes, floating point exceptions, stack corruptions, incorrect computations) due to incorrect back and restore of those registers on context switching. If `float_math` is used, the buggy behavior as before is produced. But since `float_math` is not actually provided by the native CPU, one would have to compile `CONTINUE_ON_EXPECTED_ERRORS=1` to actually get into that situation. Co-authored-by: Mihai Renea <[email protected]>
1 parent f2efe72 commit 56be5ab

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

cpu/native/Makefile.include

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,22 @@ ifeq ($(USE_LIBUCONTEXT),1)
3737
CFLAGS += $(pkg-config libucontext --cflags) -DUSE_LIBUCONTEXT=1
3838
LINKFLAGS += $(shell pkg-config libucontext --libs)
3939
endif
40+
41+
ifeq (x86_64,$(OS_ARCH))
42+
# Disable use of FPU/SSE registers if FPU is not used. This prevents radom
43+
# stack corruptions and floating point exceptions to not occur during
44+
# context switching.
45+
ifeq (,$(filter float_math,$(FEATURES_USED)))
46+
# FPU/SSE registers are not reliably saved and restored in glibc's ucontext
47+
# implementation (see https://github.com/RIOT-OS/RIOT/issues/495) and not at
48+
# all in libucontext's (but there at least this is documented). So we force
49+
# the compiler to only use registers known to be properly backed up and
50+
# restored during context switching
51+
CFLAGS += -mgeneral-regs-only
52+
53+
# Since we disabled the use of non-general registers (such as FPU registers),
54+
# we need to use a soft FPU
55+
CFLAGS += -msoft-float
56+
LINKFLAGS += -msoft-float
57+
endif
58+
endif

0 commit comments

Comments
 (0)