18
votes

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?

4
So is it really a typo ?denis631

4 Answers

32
votes

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.

6
votes

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.

3
votes

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".

1
votes

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.