1

Is there a better way to access the User-Mode Stack pointer (R13_usr) from within an exception than this

STM SP,{SP}^
NOP
LDM SP,{SP}

note: the nop is for back/forward compatibility reason, according to ARMv5 ARM

mjjoker
  • 119
  • 1
  • 6
  • As currently asked, this question can not be answered. **better way** is completely subjective. This is not to say you don't have the genesis of a good question, but you really need to think about what you want. Some context on why the `sp_usr` is needed might help. – artless noise Jun 17 '15 at 14:43

1 Answers1

1

ARMv4 and later have System mode, the whole purpose of which is to be a privileged mode with a full view of user registers, so unless you want to be compatible with truly ancient hardware (Acorn Archimedes, anyone?) you have the option of just switching modes and doing stuff directly in user context. For simply retrieving SP it's a little busy, but it is an option to avoid touching memory:

mrs r0, cpsr
orr r1, r0, #0xf
msr r1, cpsr
mov r2, sp
msr r0, cpsr    ; back to whatever the previous mode was with user SP in r2
mov sp, r2

On ARMv7 with the virtualisation extensions (Cortex-A7/A15/A17) there are new banked register access instructions, so things become trivial:

.arch_extension virt
mrs sp, r13_usr
Notlikethat
  • 20,095
  • 3
  • 40
  • 77
  • This is not what I meant. Was question was a bit misleading. The Idea is that I'm in an Interrupt Service Routine and want to save the running task on its own stack. It's ok for me to "loose" the IRQ-stack, so overriding it with the USER-SP and then saving the context on it. – mjjoker Jun 17 '15 at 10:02
  • @mjjoker Yeah, that's not at all clear, and frankly I'm wondering how it could work at all - What if the user stack address isn't mapped in the privileged page tables? What if the user code has corrupted its own stack pointer? What if you take an interrupt from SVC or some other non-user mode when no valid user context exists? There would appear to be an awful lot of non-obvious assumptions unstated here, and it does sound like you're trying to do things in a way the architecture wasn't really designed for. – Notlikethat Jun 17 '15 at 11:56
  • - The corruption of stacks is checked and always a problem - I don't do interrupt nesting, so only one Service Routine at a time and always a valid user context - I don't use MMU/MPU so no page table and/or mapping These are all valid points, but I hope I thought about everything – mjjoker Jun 17 '15 at 12:15
  • @mjoker Put the two concepts together. Use your `stm sp,{sp}^` (and probably you need to save more registers) and then transition to the system mode. There are many ways to do this. On some ARM CPUs, they have `mov r0,sp_usr`. Frankly, there are too many answers and you don't seem to want to accept anything but a single op-code; that only exists for a few ARM CPU types. – artless noise Jun 17 '15 at 14:41
  • See: [this question](http://stackoverflow.com/questions/2784978/explicitly-accessing-banked-registers-on-arm) for use of `mrs r0,sp_usr`, etc... – artless noise Jun 18 '15 at 20:23