Search code examples
kernelinterrupt-handlingirq

Although my ISR handler works perfectly, IRQ handler does not work


I am studying interrupt hendling. My interrupt descriptor table is as follows:

[EXTERN _isr_handler]
[extern _irq_handler]

%macro ISR_NOERRCODE 1  
 _isr%1:
 cli
 push byte 0
 push byte %1
 jmp isr_common_stub
%endmacro

%macro ISR_ERRCODE 1
 _isr%1:
 cli
 push byte %1
 jmp isr_common_stub
%endmacro

%macro IRQ 2
 irq%1:
 cli
 push byte 0
 push byte %2
 jmp irq_common_stub
%endmacro

%macro IDT_ENTRY 1
 dw _isr%1
 dw 0x08
 db 0x0
 db 0x8E
 dw 0x0000;
%endmacro

%macro IDT_IRQ_ENTRY 1
dw irq%1
dw 0x08
db 0x0
db 0x8E
dw 0x0000
%endmacro


ISR_NOERRCODE 0
ISR_NOERRCODE 1
...
ISR_NOERRCODE 31

IRQ 0, 32
IRQ 1, 33
...
IRQ 15, 47

idt_start:
IDT_ENTRY 0
IDT_ENTRY 1
...
IDT_ENTRY 31
IDT_IRQ_ENTRY 0
IDT_IRQ_ENTRY 1
...
IDT_IRQ_ENTRY 15
idt_end:
idt_descriptor:
dw idt_end - idt_start - 1
dd idt_start


isr_common_stub:
pusha                    

mov ax, ds               
push eax                

mov ax, 0x10  
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax

call _isr_handler

pop eax        
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax

popa                     
add esp, 8     
sti
iret 

irq_common_stub:
pusha                    

mov ax, ds               
push eax                

mov ax, 0x10 
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax

call _irq_handler

pop ebx        
mov ds, bx
mov es, bx
mov fs, bx
mov gs, bx

popa                     
add esp, 8     
sti
iret

And idt loading procedure is as follows:

mov al, 0x11
out 0x20, al     
out 0xA0, al     

mov al, 0x20
out 0x21, al     
mov al, 0x28
out 0xA1, al    

mov al, 0x04
out 0x21, al     
mov al, 0x02
out 0xA1, al

mov al, 0x01
out 0x21, al
out 0xA1, al

mov al, 0x00
out 0x21, al
out 0xA1, al
lidt [idt_descriptor]

My isr_handler C function gets called. However, irq_handler C function never gets called. I don't understand why only the irq handling part doesn't work. Please help.


Solution

  • I found the problem: in the idt loading section right before lidt command, I forgot the sti command.