7

To compare ARM processor modes with x86 modes of operation (ring0 to ring 3), user mode looks just like ring3, in which user space programs run. However I am not able to relate ring0 with either system mode or supervisor mode. Depending on the source of information, it seems that both modes can very well do the job of running a kernel in privileged mode. The only differences between the two modes that I could find out are the follwoing:

  1. registers 13 and 14 are banked in supervisor mode, whereas for system mode, all 15 registers are same.
  2. System mode cannot be entered directly on an exception, while supervisor mode can.
  3. System mode somehow prevents corruption of link registers .

can you please explain me the differences between the modes, which a person coming from x86 background can understand ?

Also how does the subtle architectural differences between the modes, like number of banked registers, make one better than the other?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Sahil Singh
  • 3,352
  • 39
  • 62
  • While your question makes sense an answer is only useful to someone coding an ARM OS. You need to read many books before you could write and design such an OS. So while someone can answer this question, I would question the worth of the answer to someone asking this question? Briefly, the modes are not like **rings**; forget that knowledge/concept as you read about the ARM. Go study some source and ask questions. How to use the modes is a design choice of the OS and related to context switches. You have the minor technical details right. – artless noise Apr 20 '15 at 15:12
  • Right now i am doing a literature survey of hypervisors for ARM, and while reading one of the papers I stumbled upon this concept. The online ARM reference doesn't say much. – Sahil Singh Apr 20 '15 at 15:18
  • For a 'hypervisor', you need to save/restore all banked registers on an guest OS switch; especially if not para-virtualized. The same is true for [tag:trust-zone]. Peruse [questions on 'arm banked'](http://stackoverflow.com/search?tab=votes&q=%5Barm%5D%20banked%20is%3aquestion) and maybe [this answer](http://stackoverflow.com/questions/2784978/explicitly-accessing-banked-registers-on-arm/24707254?s=1|13.7935#24707254) and [this quesiton](http://stackoverflow.com/questions/20315788/state-of-ttbr0-1-wrt-to-multiple-guests-in-case-of-virtualization-in-arm) may be useful for your subject. – artless noise Apr 20 '15 at 16:24
  • 1
    To expand on why the banked register question answers this, consider taking a system call: you need a stack to save working registers before you can do anything, but you can't use the user stack because a) it may not be mapped in the privileged page tables, and b) you can't blindly trust an unprivileged address anyway, so kernel mode needs its own private stack. Now, say the kernel needs to do some privileged operation, but in user context - it can't see the whole user context because its banked registers are in the way! Thus you need some way around that - each mode has its specific purpose. – Notlikethat Apr 20 '15 at 20:06

1 Answers1

4

I think the ARM ARM makes it pretty clear (see below), dont think X86 just think about what this processors modes allow you to do or not do. And what you would need in an operating system and which modes are useful or not.

You have user and system and then the exception modes. Their restrictions are documented AFAIK. The newer ARMs have even more features/restrictions/protections, etc.

From the ARM ARM

Most application programs execute in User mode. When the processor is in User mode, the program being executed is unable to access some protected system resources or to change mode, other than by causing an exception to occur (see Exceptions on page A2-16). This allows a suitably-written operating system to control the use of system resources. The modes other than User mode are known as privileged modes. They have full access to system resources and can change mode freely. Five of them are known as exception modes:

-FIQ

-IRQ

-Supervisor

-Abort

-Undefined.

These are entered when specific exceptions occur. Each of them has some additional registers to avoid corrupting User mode state when the exception occurs (see Registers on page A2-4 for details).

The remaining mode is System mode, which is not entered by any exception and has exactly the same registers available as User mode. However, it is a privileged mode and is therefore not subject to the User mode restrictions. It is intended for use by operating system tasks that need access to system resources, but wish to avoid using the additional registers associated with the exception modes. Avoiding such use ensures that the task state is not corrupted by the occurrence of any exception.

Superviser mode is what you hit when you make the svc or sys call (same instruction I think they changed the name from svc). Similar to an int 21h in the dos days, this is how you, from user mode without any permissions, ask the operating system do do something. That switches control to supervisor mode then once in supervisor mode you can handle it there or switch modes, etc...Once you switch to user though you cant switch out. So for example if you want to setup the user stack you cant easily do that in user mode and then get back to operating system tasks. so you need a privileged mode that if nothing else has user register access.

Community
  • 1
  • 1
old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Can you please some examples where I would strictly prefer one mode over the other. For example if in supervisor there is a possibility of corruption of link registers, then how does an OS running in that mode avoid corruption, are all interrupts disabled on entry to supervisor mode? Also, what happens when an interrupt in system mode occurs, the first few registers are common across all modes, and the interrupt handler, if it desires , can still write to these, thus corrupting task state? – Sahil Singh May 01 '15 at 03:00
  • The operating system is trusted, so if the bugs or problems are there, then the system is vulnerable or crashes. I assume the way interrupt handling works is the os makes a note of the interrupt in that mode, then cleans up and in another mode calls the handler, which is probably first off in the os also but then may call user code in user mode. – old_timer May 01 '15 at 14:58
  • If user mode trashes its registers oh well, supervisor if concerned can save and restore state with its own stack. if user mode as a result executes undefined instructions or goes out of its allowed address space it will get caught by a handler, handled by the OS, but that by itself doesnt take down the os. – old_timer May 01 '15 at 15:01
  • system vs user? not sure. I would have to read up more on the differences there. I think if there was a massive gap in ARM's design we would have one in almost every device with a power switch or battery, in particular our mobile phones. – old_timer May 01 '15 at 15:07