I will describe the answer to your specific question, but the same approach applies to other modes.
You will need to change the processor mode by changing the mode bits in CPSR in system mode. This will give you access to the SP / LR user mode (R13 and R14). Remember that system mode is privileged, but its R13 and R14 are the same as user mode R13 and R14.
Once you are in system mode, read R13 and R14 and place them where you want. Then just switch the mode bit back to the previous mode (I believe it was the supervisor mode in your example), and you are good to go.
Please note that we did not switch from supervisor to user mode. If you switched from supervisor to user, you could not return to supervisor mode . (Otherwise, there will be no protection against elevation of privileges of the user code). That's why we used system mode - system mode has the privilege, but the registers are the same as in user mode.
You can switch between any privileged modes as you wish by manipulating the mode bits in CPSR. I think they are the lower 5 bits? I am on the road and I have no information at my fingertips. Otherwise, I would provide you with assembly code for what I described above. In fact, if you want to put some hair on your chest, take what I gave you, sell, test and publish here.: - D
(One thing I have to add for the “general case” (yours is very specific) - you can study SPSR to see “where you came from” - and use this to determine which mode you need to switch to.)
By the way, I just recently did this for one of my clients ... a small world, I think.
Dan
source share