Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicitly accessing banked registers on ARM

Tags:

arm

According to the ARM manual, it should be possible to access the banked registers for a specific CPU mode as, for instance, "r13_svc". When I try to do this gcc yells at me with the following error:

immediate expression requires a # prefix -- `mov r2,sp_svc'

What's wrong?

Update. The following text from the ARM Architecture Reference Manual for ARMv5 and ARMv6 led me to believe that it is possible, section A2.4.2:

Registers R13 and R14 have six banked physical registers each. One is used in User and System modes, and each of the remaining five is used in one of the five exception modes. Where it is necessary to be specific about which version is being referred to, you use names of the form: R13_mode R14_mode where mode is the appropriate one of usr, svc (for Supervisor mode), abt, und, irq and fiq.

like image 975
Demiurg Avatar asked May 06 '10 22:05

Demiurg


People also ask

What are ARM banked registers?

ARM processors have 37 registers. The registers are arranged in partially overlapping banks. There is a different register bank for each processor mode. The banked registers give rapid context switching for dealing with processor exceptions and privileged operations.

What does it mean to bank a register?

Register banking refers to providing multiple copies of a register at the same address. Taken from section 1.4.6 of the arm docs. The term is referring to a solution for the problem that not all registers can be seen at once. There is a different register bank for each processor mode.

What are banked registers used for?

Explanation: when switching from one mode to another, instead of storing the register contents somewhere else it'll be kept in the duplicate registers and the new values are stored in the actual registers.

Which register is accessible in all processor mode?

In all ARM processors, the following registers are available and accessible in any processor mode: 13 general-purpose registers R0-R12. One Stack Pointer (SP). One Link Register (LR).


2 Answers

You use mrs and msr to change modes by changing bits in the cpsr then use r13 normally.

From the arm arm

MRS R0,CPSR
BIC R0,R0,#0x1F
ORR R0,R0,#0x13
MSR CPSR_c,R0

then

mov sp,#0x10000000

or if you need more bits in the immediate

ldr sp,=0x12345600

or if you dont want the assembler placing your data, you can place it yourself.

ldr sp,svc_stack
b 1f
svc_stack: .word 0x12345600
1:

You will see typical arm startup code, where the application is going to support interrupts, aborts and other exceptions, to set all of your stack pointers that you are going to need, change mode, set sp, change mode, set sp, change mode ...

like image 130
old_timer Avatar answered Oct 10 '22 16:10

old_timer


I don't think that's possible with the mov instruction; at least according to the ARM Architecture Reference Manual I'm reading. What document do you have? There are is a variant of ldm that can load user mode registers from a privileged mode (using ^). Your only other option is to switch to SVC mode, do mov r2, sp, and then switch back to whatever other mode you were using.

The error you're getting is because it doesn't understand sp_svc, so it thinks you're trying to do an immediate mov, which would look like:

mov r2, #0x14

So that's why it says "requires a # prefix".

like image 35
Carl Norum Avatar answered Oct 10 '22 17:10

Carl Norum