punstress
punstress

Reputation: 1196

Switching from user mode to kernel mode

In my operating systems class, I'm asked whether switching from user to kernel mode is privileged. This is not OS-specific. At first I thought yes, but it seems like a big Catch 22. I referred to my textbook:

The hardware allows privileged instructions to be executed only in kernel mode. ...

The instruction to switch to kernel mode is an example of a privileged instruction.

Gagne, Greg; Abraham Silberschatz; Peter B. Galvin (2010-01-26). Operating System Concepts (p. 22). Wiley Higher Ed. Kindle Edition.

So we start in user mode. To switch to kernel mode requires a privileged instruction. A privileged instruction must be done in kernel mode, therefore we must switch to kernel mode to enable switching to kernel mode.

I'm thinking the system does not allow a user to switch itself to kernel mode directly, but that it is done by the kernel when the user seeks to execute another privileged instruction. Is that correct?

Upvotes: 20

Views: 25743

Answers (5)

JimJ
JimJ

Reputation: 11

I've always found the author's answer to this question confusing and would not put such a question on an OS exam (but its fine for discussion problems). In olden days on the IBM 360/370 mainframe, the PSW (program status word) included the pointer to the next instruction (a.k.a., PC), the mode bit (=1 for priv), and other state registers. The current PSW was in a hardware register. There was a Load PSW (LPSW) instruction to load a new PSW into this register, which was privileged since it could change the mode bit and more. An interrupt was a hardware mechanism that saved the current PSW (typically in user mode=0) and loaded the PSW needed to run an interrupt handler in privileged mode. So this was a hardware induced switch rather than by execution of a LPSW instruction. The handler itself would execute the LPSW to load one of the saved user PSWs. So the catch-22 concern gets answered in that the hardware mechanism got us into priv mode, and thereafter the privileged LPSW instruction could be run to get back to user mode later. Later OS's for processors invented for mini- and micro- computers (i.e., today's workstations and PCs) use different mechanisms for switching. In all OS mechanisms that I am aware of, the user can indirectly go to privileged mode by issuing any system call, but that is a voluntary way to relinquish control back to the OS which will be in priv mode (a.k.a., supervisor or kernel mode). In short, users should NOT be allowed to directly change the mode-bit since that is yet another hardware resource that the OS manages, not the user.

Upvotes: 1

Philipp
Philipp

Reputation: 598

In user mode you cannot just switch to kernel mode. Interaction between user and kernel is done via system-calls. Each system-call is providing one defined service. The user sends the service name (usually a number) and the required parameters. Here is a real world example how this is done. It's x86 AT&T style assembler.

It moves the system-call name into the EAX register, the pointer to the parameters into the EBX register of the CPU and then emits the software interrupt number 42. The interrupt handling will do the switch to kernel mode. The interrupt number is looked up in the Interrupt Descriptor Table (IDT) and invokes the function that's registered there, the syscall handler. This handler executes in kernel mode. On return to the user mode the code will move the content of EAX into the variable ret.

pok_ret_t pok_do_syscall (pok_syscall_id_t syscall_id, pok_syscall_args_t* args)
{
  pok_ret_t ret;
  uint32_t  args_addr;
  uint32_t  id;

  args_addr = (uint32_t) args;
  id        = (uint32_t) syscall_id;

  asm volatile ( "movl %1,%%eax  \n\t"
                 "movl %2,%%ebx  \n\t"
                 "int $42        \n\t"
                 "movl %%eax, %0 \n\t"
                 :"=g"(ret)
                 :"g"(id), "g"(args_addr)
                 : "%eax" , "%ebx"
               );
  return ret;
}

The OS Dev wiki is a good point to read more about this.

So you don't just switch to the kernel, but you can ask the kernel to do something for you. And then the kernel is telling you if it was done or not.

Upvotes: 33

user832146
user832146

Reputation: 150

That is a typo introduced in the 8th edition and kept in the 9th. In the seventh edition, page 19, it says instead:

"The instruction to switch to user mode is an example of a privileged instruction."

Which clearly makes a lot more sense.

Upvotes: 7

emsworth
emsworth

Reputation: 1171

In user land, you request privileged operations via system calls to the kernel, which performs the switch to kernel mode as necessary. The user is using an API, the kernel is performing the privileged operations.

Upvotes: 1

asantaballa
asantaballa

Reputation: 4038

There are usually as set of instructions that are not really to switch to kernel mode in s general way, but to request system services. So these switch to kernel mode, but only in the context of calling some piece of functionality which was set up by the operation system for the purpose of being called by user code.

In most modern systems, even this is hidden by an API layer that implements a specific function part of which may be doing an operating system call as above.

But in general it is true that user code cannot do the equivalent of saying "from this point on, I want to be running in kernel mode".

Upvotes: 3

Related Questions