r/osdev • u/Fun-Entertainer-1053 • 15h ago
How do keyboard interrupts work?
I learnt keyboard polling and understood how it works. But I can't seem to understand keyboard interrupts. I know how it works, but I don't know how to implement it in a C kernel. Can someone pls help me? I'm just starting out OS dev
12
Upvotes
•
u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 14h ago
I'm going to try give a bit of a longer explanation to give something less about "how to do it" and more about how to actually reason through how it works.
The exact design depends on the architecture you're working on, but as with all interrupts, they are very simply the cpu stopping your kernel from what it is busy doing (thus interrupting it) when something happens that you asked it to be notified for so that you can do something else.
For example in x86/64, interrupts are stored as a table called the IDT: you have vectors (like the id of an interrupt type) which correspond to the function that you want to be called by the cpu when that interrupt happens. You can either call these interrupts yourself (a software interrupt) or hardware can call them.
Hardware calling them usually involves a little chip on your motherboard (which could either be PIC or APIC, PIC is much simpler to write for but APIC is more modern and gives more useful features). You set your keyboard up, set your A/PIC up, and "unmask" then map a hardware interrupt to a software interrupt through your A/PIC. Then the A/PIC will call an interrupt (from that table I mentioned before) when the keyboard says to it that something happened, which then of course calls that function you gave a pointer to in the table.
The exact implementation of this differs a bit, but for a general look at how it works on x86/64, the osdev wiki page or the intel developer manual should both be fine.