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?


2 Answers 2


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.

  • $\begingroup$ 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. $\endgroup$ Commented Oct 31, 2017 at 16:14
  • $\begingroup$ @KishanKumar yes depending on the operation and how likely a true context switch is necessary. $\endgroup$ Commented Oct 31, 2017 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 pictureenter image description here

  • 1
    $\begingroup$ 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. $\endgroup$ Commented Oct 31, 2017 at 14:59
  • 5
    $\begingroup$ 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. $\endgroup$ Commented Oct 31, 2017 at 15:11
  • $\begingroup$ I added picture, I hope it makes things clearer. $\endgroup$ Commented Oct 31, 2017 at 15:37
  • 1
    $\begingroup$ That part is quite processor specific. On ARM for example, the trap instruction will switch to a different stack and a different set of registers (banked registers). The kernel code will then use special instructions to read/write user mode registers. That way the user code does not interfere with the kernel's code. But in any case, the user code has to follow some protocol to put the arguments in the right registers. See this post for more details. $\endgroup$ Commented Oct 31, 2017 at 16:07
  • 3
    $\begingroup$ Just as a comment, "saving registers" is not magic; it even happens when calling a function entirely in user-space. Most platforms (i.e. the combination of CPU and OS) specify that some registers are "caller-save" (that is, the value in that register is not guaranteed to be preserved if you call a function, so should be saved by the caller) and some are "callee-save". A system call is callee-save. $\endgroup$
    – Pseudonym
    Commented Nov 1, 2017 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.