Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which stack is used by Interrupt Handler - Linux

In a multitasking system when any hardware generates a interrupt to a particular CPU, where CPU can be performing either of below cases unless it is already serving a ISR:

  1. User mode process is executing on CPU
  2. Kernel mode process is executing on CPU

Would like to know which stack is used by interrupt handler in above two situations and why ?

like image 690
Sunil Bojanapally Avatar asked Feb 27 '15 06:02

Sunil Bojanapally


People also ask

What stack do interrupts use?

The hard IRQ stack is used when handling interrupts. There is one hard IRQ stack for each CPU in the system, and each stack is contained in a single page frame.

How stack is used in interrupt handling?

Whenever a hardware interrupt occurs (or a softIRQ is processed), the kernel needs to switch to the appropriate stack. Historically, interrupt handlers did not receive their own stacks. Instead, interrupt handlers would share the stack of the running process, they interrupted.

What is interrupt handler in Linux?

An interrupt is simply a signal that the hardware can send when it wants the processor's attention. Linux handles interrupts in much the same way that it handles signals in user space. For the most part, a driver need only register a handler for its device's interrupts, and handle them properly when they arrive.

Where are interrupt handlers stored?

Interrupt handlers are stored in memory, like all executable code has to be. In a full multi-tasking OS, it's in the kernel. Under something like DOS, programs could "terminate and stay resident", leaving their interrupt handlers hooked into the interrupt table.


2 Answers

All interrupts are handled by kernel. That is done by interrupt handler written for that particular interrupt. For Interrupt handler there is IRQ stack. The setup of an interrupt handler’s stacks is configuration option. The size of the kernel stack might not always be enough for the kernel work and the space required by IRQ processing routines. Hence 2 stack comes into picture.

  1. Hardware IRQ Stack.
  2. Software IRQ Stack.

In contrast to the regular kernel stack that is allocated per process, the two additional stacks are allocated per CPU. Whenever a hardware interrupt occurs (or a softIRQ is processed), the kernel needs to switch to the appropriate stack. Historically, interrupt handlers did not receive their own stacks. Instead, interrupt handlers would share the stack of the running process, they interrupted. The kernel stack is two pages in size; typically, that is 8KB on 32-bit architectures and 16KB on 64-bit architectures. Because in this setup interrupt handlers share the stack, they must be exceptionally frugal with what data they allocate there. Of course, the kernel stack is limited to begin with, so all kernel code should be cautious.

like image 134
skaushal Avatar answered Sep 20 '22 18:09

skaushal


Interrupts are only handled by the kernel. So it is some kernel stack which is used (in both cases).

Interrupts do not affect (directly) user processes.

Processes may get signals, but these are not interrupts. See signal(7)...

like image 44
Basile Starynkevitch Avatar answered Sep 19 '22 18:09

Basile Starynkevitch