This is my understanding:

  1. When in user mode and a trap/system call occurs, registers are saved on to the kernel stack, switch is made to kernel mode, and trap is handled and returned to user mode.
  2. When in user mode and the timer interrupt goes off, again the registers are saved, switched to kernel mode, and another process is scheduled according to the scheduler, and it comes back to user mode now to run the second process.

But what if process 1 is already in Kernel mode, and then the timer interrupt goes off? What happens then?


Generally speaking (I dont know how the linux kernel implements this exactly) If any iterrupt is triggered, any code that runs is stopped, all registers are saved and the CPU runs the interupt service routine that handles the interrupts. Afterwards the registers are restored and code execution continues.

The only thing you could do to prevent this is disable interrupts, which is usually done on microcontrollers to handle atomic operations. Usually the first thing the interrupt service routine does is disable interrupts (so itself cant be interrupted) and after doing its tasks the last thing it does is reenable interrupts.

I am again not familiar with the exact implementation of interrupt service routines in the linux kernel but i would expect them to work the same way.

As far as mode switches from user to kernel mode in x86 architecture goes, I think it is happening independent from this. Its save to assume that the ISR runs in kernel mode so the CPU has to switch to it. If its already in kernel mode it just has to save the registers and jump to the ISR. Afterwards it resumes whatever code was executed prior to the interrupt in whatever mode that code ran on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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