Hi,
I have the following code
FUNC(uint32,OS_CODE) Os_PlatformGetCoreIdSyscaller( void )
{
OS_ASM(" push {lr} "); /* OSASMEXT COV VIOLATION */
OS_ASM(" swi #1 "); /* OSASMEXT COV VIOLATION */
OS_ASM(" pop {lr} "); /* OSASMEXT COV VIOLATION */
/* Return value in R0 */
}
This function calls systemcall which in turn jumps into my systemcall handler:
Os_SysCall:
CPSID if
// Store LR and SPSR of the Current mode
//( Current mode here will be SVC mode as a result of SVC call )
// to SYS stack through SRSDB( Store Return State Decrement Before ) instruction.
// Check NOTE_1 above for details.
SRSDB sp!, #MODE_SYS
MRC p15, #0, r0, c0, c0, #5 // Read MPIDR since it is a getCoreId-Syscall
CPS #MODE_SYS
RFEIA sp!
In my system call handler Os_SysCall I provide the core id from MPIDR register and put it into R0 register (the return register)
So when I call Os_PlatformGetCoreIdSyscaller() like the following it returns what is stored in MPIDR register.
targetId = Os_PlatformGetCoreIdSyscaller()
But there was a problem in the behavior and when I debugged the problem I noticed that TIARMCLANG compiler injects assembly code for Os_PlatformGetCoreIdSyscaller function which mess with R0 value causing the returned vale in targetId is incorrect
Note the following screenshot:

Note that at the end of the function the compiler added "ldr r0, [r13]" which overrided the R0 value which I have put in the systemcall handler
What this instruction is really doing and how to avoid this situation ?