Search code examples
androidmultithreadingarmneoncontext-switching

Saving ARM NEON registers while context switching in Android


At the time of context switching Android saves all ARM general registers on stack and restore it after.

Does Android OS stores NEON registers on stack?

If not, how to make a safe usage of NEON in multi-threading and multi-tasking environment?


Solution

  • If the kernel is built with NEON/VFP support then it will indeed preserve the NEON state for a task along with the rest as part of the context switch (provided that the task has actually touched NEON/VFP beforehand, so there is some state to preserve).

    If the kernel doesn't have NEON/VFP support, then as you guess there is simply no safe way of using it - any task could freely corrupt any other task as they'd all be sharing the same registers. Fortunately that problem doesn't exist in practice because a kernel which isn't aware of NEON also won't enable access to the hardware in the first place. In that case any NEON instructions would be treated as undefined, and signals sent back to the task as appropriate.

    The fact that there are tons of Android applications out there with NEON code in them should be a hint as to which of those configurations is relevant here ;)