I'm currently learning about the Linux Kernel and OSes in general, and while I have found many great resources concerning IRQs, Drivers, Scheduling and other important OS concepts, as well as keyboard-related resources, I am having a difficult time putting together a comprehensive overview of how the Linux Kernel handles a button press on a keyboard. I'm not trying to understand every single detail at this stage, but am rather trying to connect concepts, somewhat comprehensively.
I have the following scenario in mind:
- I'm on a x64 machine with a single processor.
- There're a couple of processes running, notably the Editor
VIM
(Process #1
) and sayLibreOffice
(Process #2
). - I'm inside
VIM
and press thea
-key. However, the process that's currently running isProcess #2
(withVIM
being scheduled next).
This is how I imagine things to go down right now:
- The keyboard, through a series of steps, generates an electrical signal (USB Protocol Encoding) that it sends down the USB wire.
- The signal gets processed by a USB-Controller, and is send through PCI-e (and possibly other controllers / buses?) to the Interrupt Controller (
APIC
). TheAPIC
triggers theINT Pin
of the processor. - The processor switches to
Kernel Mode
and request anIRQ-Number
from theAPIC
, which it uses as an offset into theInterrupt Descriptor Table Register
(IDTR
). A descriptor is obtained, that is then used to obtain the address of the interrupt handler routine. As I understand it, this interrupt handler was initially registered by the keyboard driver? - The interrupt handler routine (in this case a keyboard handler routine) is invoked.
This brings me to my main question: By which mechanism does the interrupt handler routine communicate the pressed key to the correct Process (Process #1
)? Does it actually do that, or does it simply write the pressed key into a buffer (available through a char-device
?), that is read-only to one process at a time (and currently "attached" to Process #1
)? I don't understand at which time Process #1
receives the key. Does it process the data immediately, as the interrupt handler schedules the process immediately, or does it process the key data the next time that the scheduler schedules it?
- When this handler returns (
IRET
), the context is switched back to the previously executing process (Process #2
).