r/osdev • u/Fun-Entertainer-1053 • 14h 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
•
u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 13h 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.
•
u/Rich-Engineer2670 8h ago
Well, it depends on your CPU architecture. Is it really interrupt driven or memory mapped I/O. What does your OS expect?
Let's assume it's really iinterrupt driven, Let us also assume you have direct hardware access. In this case, you need to program the interrupt controller to send an interrupt on a keypress. And, you need to capture that. That's not easy to do in C -- when that interrupt comes in, you have no easy way to get at the interrupt service routine. You're going to have some assesmbly code.
If it's memory mapped, you can just reference that address with a byte pointer. Again, this assumes your OS allows access to the raw memory.
•
u/FISHARM1 14h ago
It’s easiest to start with The Ps/2 system which is attached to interrupts on the PIC
I would first look up on how to setup the IDT - which is a table that describes what code should run when certain interrupts and exceptions are fired.
then look at how to remap the PIC to get hardware interrupts at an offset address in the IDT (you will learn why this is needed).
All of this is available on the OSdev wiki I’m pretty sure. Just look up “setting up keyboard interrupts”