[Fwd: Modes, stack setup for "no-OS" ARM app]

Nick Clifton nickc@redhat.com
Tue Apr 20 12:18:00 GMT 2004

Hi Toralf,

> I've been wondering about how I should handle processor modes and set up
> stack pointers etc. for my newlib-based ARM application running on a
> "bare" system (no OS, no debug monitor.) Unlike the default crt0, mine
> will definitely have to set up stack pointers for more than one mode,
> since I use interrupts actively. 

(Starting from a privileged mode) use the MSR instruction to write to 
the CPSR to select the privileged mode you want.  Then initialise that 
modes stack pointer.  Then move on to the next mode.

> Also, I'm not quite sure what mode the
> application itself ought to run under. I need to access "privileged"
> registers etc. during the init phase at least, so some bits have to run
> in Supervisor or System, but do I want to do *everything* in one of
> those modes?

No - if at all possible run in User mode.  This will give you the most 
protection from bugs in your overwriting important registers and memory 

> Some issues related to this (in no particular order):
>   1. Once User mode is entered, there is no easy way back, as far as I
>      know, so I can't simply switch to that mode e.g. to set up the
>      stack, if I want SVC or System later.

Correct.  The only way back is via an interrupt, (hard or soft) which is 
supposed to be vectored into OS code which decides if the process can 
have its privileges escalated.  In your case you should be able to 
install a vector into 0x00000008 and then use the SWI instruction to 
start that code executing in Supervisor mode.

>   2. System is ARM version 4 and above, so perhaps I shouldn't use that
>      in my crt0, which I intend to be very general.

There are very few ARMv3 and earlier systems left these days, so you 
probably can safely ignore them.  Using ARMv4 as the basis of your crt0 
should be quite safe.

>   3. As far as I can tell, there is no way to access the user mode
>      stack pointer e.g. from supervisor mode, at least when using gas.

Check out the LDM(2) and STM(2) instructions.  (You do have a copy of 
the ARM Architecture Reference Manual right ?).  They will allow user 
mode general purpose registers to be accessed when running in a 
privileged mode.


More information about the Newlib mailing list