foss-for-synopsys-dwc-arc-processors/toolchain

GCC support for ARCv2 ftrace in linux kernel (_mcount)

Opened this issue · 0 comments

As per foss-for-synopsys-dwc-arc-processors/linux#144 (comment), in order to fully support ftrace the linux kernel needs to change the return address of a function, from inside the nested call to _mcount and therefore requires extra help from the compiler.

Problem brief

Following the ARCv2 ABI, the function prologue stores the blink register before storing the 'callee stored registers' and after storing the necessary 'parameter registers'. Since there can be a variable amount of each, we can't infer where in the stack was the blink stored.

This is a requirement, as it is what allows the Linux Kernel to, via a call to _mcount, replace the return address with a hook that will record the function exit.

This problem does not happen for ARCv3 (return address location is easily found via fp), and this features' implementation is in an open PR (foss-for-synopsys-dwc-arc-processors/linux#146)

Patch

@claziss provided a quick proof of concept patch that makes GCC also pass a second argument with the delta between the stack pointer at _mcount entry, and at blink push.

This solution works most of the time, only being detected as not working in variadic functions.

I provide here an example setup that showcases the problem (run ./run.sh to see where we succeed and fail to find the blink in stack).

This problem with the GCC patch is currently being investigated.

Extra consideration

A loop that looks for the blink value in memory could be a solution, except it is very easy to stumble into false positives.