views:

1669

answers:

2

I have a few questions on the user-mode and supervisor-mode on Unix-like machines.

What is the difference between user-mode and supervisor-mode? I know that the user processes cannot access all memory and hardware and execute all instructions. Is there more to this?

What are the advantages of having different modes?

What are the steps involved when one switches from the user-mode to the supervisor mode?

When a system call is made by a user-program, the mode has to change from user-mode to supervisor mode. I have read elsewhere that this is achieved on x86 machines by using an int x80. So how is a mode-switch different from interrupt handling?

How is it different from a context-switch?

How are supervisor modes implemented in different architectures?

Any answers or pointers will be appreciated!

+1  A: 

The CPU will not physically allow access to the areas which are determined as "privileged". Because this is enforced in hardware, it gives your operating system the capability to protect itself. Without this mechanism there would be no "security" in an operating system, as the most obscure piece of code could simply access kernel memory and read all the passwords for instance.

User mode to supervisor mode switch is expensive because it is a context switch, and for security purposes the cache must be flushed (otherwise you might be able to access something that you weren't meant to.

As for a context switch, this inherently involves a switch to kernel mode to perform a task. When the CPU Scheduler timer interrupt fires, it switches into kernel mode, selects the next task to execute, and then switches back to user mode with the next task to resume.

Spence
A: 

Two concepts exist:

  • software user/kernel modes, which are switched from each other when performing a system call or a return form system call,
  • hardware user/supervisor modes, which are switched from each other on interrupts.

Very few code is executed in HW supervisor mode, mainly interrupt routines at low level and the very beginning of startup. Even most of SW kernel mode is executed in HW user mode.

mouviciel