Confused about IRQ, ISR and all that stuff :D

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ostylk
Posts: 9
Joined: Fri Feb 13, 2015 12:38 pm

Confused about IRQ, ISR and all that stuff :D

Post by ostylk »

Hey,
I started a few days ago creating my os. I've successfully implemented my GDT and IDT. For the IDT i've set up 2 test interrupt handler in my idt.c.

Code: Select all

	idt_set_gate(0, (uint32_t) int_haendler1, 0x08, 0x8E);
	idt_set_gate(1, (uint32_t) int_haendler2, 0x08, 0x8E);
Then I tested it out with inline assembler:

Code: Select all

asm("int 0x0");
and it works perfectly.
Then I wanted to implement keyboard support and started readig about ISR, IRQ all that keyboard stuff. Now I'm very confused how it is working...
My keyboard is sending a signal to my PIC and then? Sends the PIC an interrupt to the CPU/my OS or can I modify it? Or is it important which entry at the IDT handles my PIC/keyboard interrupt? I'm really confused.. :D
Which entries do I need in my IDT? Is there a specific order? How does my PIC know to which interrupt handler it needs to send the signal..
(my english isn't the best :D)

Thanks for any help.
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Confused about IRQ, ISR and all that stuff :D

Post by JAAman »

ostylk wrote:Hey,
I started a few days ago creating my os. I've successfully implemented my GDT and IDT. For the IDT i've set up 2 test interrupt handler in my idt.c.

Code: Select all

	idt_set_gate(0, (uint32_t) int_haendler1, 0x08, 0x8E);
	idt_set_gate(1, (uint32_t) int_haendler2, 0x08, 0x8E);
Then I tested it out with inline assembler:

Code: Select all

asm("int 0x0");
never use 0! the first 32 interrupts are reserved for use by the CPU (check Intel manual 3A: chapter 5), 0 specifically means #DE (Divide Error) usually means you attempted to divide by 0
and it works perfectly.
Then I wanted to implement keyboard support and started readig about ISR, IRQ all that keyboard stuff. Now I'm very confused how it is working...
My keyboard is sending a signal to my PIC and then?
yes, the keyboard tells the PIC it wants an interrupt, and the PIC signals the CPU... what interrupt it sends to the CPU, is dependent on how the PIC is programmed... by default on PC's, it starts at 8... as noted above, this is a reserved, and should never be used, so you must remap it... look up the PIC in the wiki (link at the top of this page and in my signature) should give you all the information you need
User avatar
bace
Member
Member
Posts: 34
Joined: Fri Jan 16, 2015 10:41 am
Location: United Kingdom

Re: Confused about IRQ, ISR and all that stuff :D

Post by bace »

When a device wants to signal something to the CPU, it tells the PIC. The PIC (programmable interrupt controller) will work out which interrupt is should send to the CPU (you can change these by setting a offset which is added to the interrupt, e.g. PS2 keyboard interrupt = 1 + 32 (offset) = 33). These external interrupts are called IRQs. The CPU will decide where it should go to based on your IDT.

Interrupts can also be local to your processor, such as when your code generates a fault/exception (called a ISR) or just calls an interrupt (e.g. int 0x80). The ISRs on a x86 processor range from interrupts 0 to 31, so you'll probably change your PIC interrupt offset to 32 during initialization, so that there are no collisions. This also means you'll end up using something like 0x80 as your system call interrupt, because it is far away from the software exception/fault interrupts and the external hardware ones.

So, here's the steps you'd need to implement a PS2 keyboard driver:
  • 1. Remap the PIC's offset to 32
  • 2. Set an entry in your IDT for interrupt 33
  • 3. When the interrupt occurs,
    • 1. Save the processor state
    • 2. Get the information from the keyboard and put it in a buffer or something
    • 3. Tell the PIC you've handled the interrupt
    • 4. Restore the previous state, and iret
"for example, turning off the system’s power through the movement of a large red switch" - the Advanced Configuration and Power Interface Specification
ostylk
Posts: 9
Joined: Fri Feb 13, 2015 12:38 pm

Re: Confused about IRQ, ISR and all that stuff :D

Post by ostylk »

Sorry for late answer but my laptop crashed and won't start. I've found out that my hard disk was corrupted(don't know why). Fortunately I have the code saved in gitlab :). I hope I have it tomorrow fixed.
Post Reply