Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is calling a function considered a context switching?

When I call a function as below

void main(void){
    Function();
}

Is it considered context switching because I save registers before go to the function ?

like image 300
Amr Tarek Avatar asked Dec 31 '22 01:12

Amr Tarek


2 Answers

No, context switch occurs when the kernel swaps a process. calling a user space function will leave you inside the same process, and therefore wont be context switch. calling a kernel function however, requires an operation from the kernel and therefore it is a context switch to the kernel.

like image 83
nir shahar Avatar answered Jan 13 '23 17:01

nir shahar


Before we ask ourselves whether we consider this "context switch", we should clear up what it means, or rather what it can mean.

When we are talking about the OS kernel and processes, we can "switch" between multiple things:

  1. Privilege levels/Rings/Modes
  2. CPU Register Set
  3. Virtual Memory Areas

Often, not just one of these happens alone. A mode switch is something that is rather frequent, supervisor calls / exceptions or hardware interrupts (e.g. a timer for scheduling or an external device) necessarily trigger these. In almost every scenario we need to acquire higher privileges to actually "do something".

Storing all of the CPUs registers is needed when we have some kind of asynchronous interruption of the execution. This is not necessarily needed for supervisor calls, as there we can define a calling-convention between the caller and the callee, which registers who is responsible to backup before executing the instruction1. This is simply not possible with hardware interrupts, as we do not know when they will trigger: How should a process be able to foresee that it will be interrupted in the next t nanoseconds and has to backup all used registers?

A switch between virtual memory areas is needed when we e.g. switch between processes (every process has their own address space) or between a process and the kernel address space (and so does the kernel). However, it's often not needed for supervisor calls as these are usually mapped into the processes address space and thus only execute with higher privilege (kernel mode) without "leaving the process" (processes' address space).2

While switches between privilege levels are rather cheap (performance wise), switching out the complete CPU register set is already quite expensive. However, unloading the complete page table, flushing the TLB etc. is the most expensive and we try to keep this down as much as possible. This is also a reason why threads are far more performant (in general) than processes: Switching between them only needs a switch between CPU registers (and, depending on the implementation, a short mode switch) but not a switch between address spaces!

When we read about "context switch" in literature, it's unfortunately not always clear what of these three, or which combination, is actually referred to as "switching the context". From what I can gather, the term comes from a very simplified machine model that has no distinction between interrupts, exceptions / supervisor calls, nor between switching CPU registers or address spaces. In this model we only see "the kernel [mode/space/...]" and "the user [mode/space/...]" and any switch between these is considered a "context switch".

Fortunately, your posted scenario does not fit into any of these switches as it is a synchronous, unprivileged, function call within the same address space, thus not needing any switch but a simple calling convention that only saves those registers that need saving suffices.


1 But this might have security implications, as correctly pointed out by @BenVoigt. This is no issue for calls like gettimeofday() (cf. this LWN.net article), but in other cases this might result into spilling confidential data to the user process.

2 Some microkernel designs dispatch a process which actually handles the supervisor call implementation, thus making a switch to this processes address space necessary.

like image 28
ljrk Avatar answered Jan 13 '23 17:01

ljrk