Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 811
punstress Avatar asked Sep 27 '13 18:09

punstress


People also ask

Is it possible to transition from user mode to kernel mode?

The system is in user mode when the operating system is running a user application such as handling a text editor. The transition from user mode to kernel mode occurs when the application requests the help of operating system or an interrupt or a system call occurs.

Is switching from user to kernel mode privileged?

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

What causes a switch to kernel mode?

The transition is usually caused by one of the following: Fault (e.g. a page fault or some other exception caused by executing an instruction) Interrupt (e.g. a keyboard interrupt or I/O finishing) Trap (e.g. a system call)

What is difference between kernel mode and user mode?

In kernel mode, the program has direct and unrestricted access to system resources. In user mode, the application program executes and starts. In user mode, a single process fails if an interrupt occurs. Kernel mode is also known as the master mode, privileged mode, or system mode.


3 Answers

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.

like image 198
Philipp Avatar answered Oct 08 '22 19:10

Philipp


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.

like image 38
user832146 Avatar answered Oct 08 '22 19:10

user832146


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

like image 29
asantaballa Avatar answered Oct 08 '22 20:10

asantaballa