# Do system calls always means a context switch?

When a system call takes place some kernel code must be executed. If some kernel code is being executed it means it can alter register values. So before this code gets executed the old process state must be saved. This is called context switch. Now Wikipedia says that not all system calls need context switch. How is this possible?

Depends on how you define a context switch.

In the traditional sense it means saving all registers/cpu state changing the mmu state and then going elsewhere to answer the call and after it's finished restore everything.

It is not necessary to save all state for all operation. For example a mutex lock needs to check no other thread/process is using the mutex and then set it marked.

In a single-core cpu you can do that by ensuring that no interrupts happen during the mutex operation and then by virtue of being passed the point of interrupts being disabled you know you are the only one touching the mutex at that point. The only way another thread could be in the middle of the lock operation is if it re-enabled interrupts or it context switched out because the mutex was already taken. Both scenarios and where in the code they can happen are under full control of the kernel code.

Having said all that, saving the context isn't that expensive. The more expensive part is all the cache misses that will happen as the instruction flow goes to cold memory.

• You mean to say that part of the process state may be saved but other expensive things such as TLB flush may not be done. – Kishan Kumar Oct 31 '17 at 16:14
• @KishanKumar yes depending on the operation and how likely a true context switch is necessary. – ratchet freak Oct 31 '17 at 16:15

A system call does not necessarily require a context switch in general, but rather a privilege switch. This is because the kernel memory is mapped in each process memory. The user process cannot access the kernel's memory because the memory mapping indicates which part are for the user and which parts are for the system. Thus a system call is really just a change of privilege, not of process. On some systems, like micro-kernels, a system call may involve a context switch because drivers are in a different process. But on monolithic systems like Linux this is not necessary. Linux also supports what is called the vDSO to implement some system calls entirely in userspace.

EDIT: attached a picture

• You didn't answer what I asked for. How can the system not context switch if its running different code. Please provide an example that would suffice. – Kishan Kumar Oct 31 '17 at 14:59
• Imagine for a moment that privilege separation did not exist: the kernel and user space were running in the same memory context and the userspace can access kernel's memory. Then a system call would just be a jump to some function. Now with privilege separation this is the same thing except the jump is replaced by a special instruction or trap. The CPU then switches to privilege mode and jumps to a special location in the kernel. It works because the kernel code is mapped in the process memory but is only accessible in privilege mode. The details are very architecture dependent. – Amaury Pouly Oct 31 '17 at 15:11
• I added picture, I hope it makes things clearer. – Amaury Pouly Oct 31 '17 at 15:37
• Ok I understand everything that you say. But where is the guarantee that kernel code would not alter the register values that the process was using. You mentioned treating the system call as a sort of jump. However in normal code when we jump, the instructions that are executed by jump are written by the same programmer who has written the part before the jump. So the programmer know what resources would be altered and accordingly he programs. But in this case the jump is made to kernel code and the programmer has no idea what resources that code uses. – Kishan Kumar Oct 31 '17 at 15:56
• @KishanKumar The code jumped to is kernel code. It is set in the MMU as not executable unless under elevated privilege. The <setup args> bit will form the registers and stack so the kernel can understand what the user program wants according to a specific protocol – ratchet freak Oct 31 '17 at 15:58