ISR handler not being called

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
User avatar
0xY
Posts: 13
Joined: Thu Sep 29, 2022 6:12 pm
Libera.chat IRC: 0xY

ISR handler not being called

Post by 0xY »

Hi, I'm getting started in OS Dev and I just got to implementing ISR's. My problem is that the code in my ISR handler function seemingly isn't called. Here is my assembly file (boot header omitted):

Code: Select all

.section .bss
.align 16
stack_bottom:
.skip 16384 # 16 KiB
stack_top:

.section .text
.global _start
.type _start, @function
_start:
	mov $stack_top, %esp
	call kernel_main

.global _gdt_flush
.extern _gp
.type _gdt_flush, @function
_gdt_flush:
	lgdt  (_gp)
	mov %ax, 0x10      
    mov %ds, %ax
    mov %es, %ax
    mov %fs, %ax
    mov %gs, %ax
    mov %ss, %ax
    jmp $0x08, $flush2
flush2:
    ret

.global _idt_load
.extern _idtp
.type _idt_load, @function
_idt_load:
	lidt (_idtp)
	ret               

.global _isr0
/* _isr1, isr2 etc... */

.type _isr0, @function
_isr0:
	cli
	push 0 
	push 0
	jmp isr_common_stub

.type _isr1, @function
_isr1:
    cli
    push 0
    push 1
    jmp isr_common_stub

/* _isr2, isr3 etc... */

.global isr_common_stub
.extern _fault_handler
.type isr_common_stub, @function
isr_common_stub:
	pusha
	push %ds
	push %es
	push %fs
	push %gs

	mov %ax, 0x10
	mov %ds, %ax
	mov %es, %ax	
	mov %fs, %ax
    mov %gs, %ax
    mov %eax, %esp  
    push %eax
    mov %eax, _fault_handler
    call *%eax     
    pop %eax
    pop %gs
    pop %fs
    pop %es
    pop %ds
    popa
    add %esp, 8     
    iret          

.size _start, . - _start
And here is my ISR code

Code: Select all

struct regs{
    unsigned int gs, fs, es, ds;      
    unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;  
    unsigned int int_no, err_code;   
    unsigned int eip, cs, eflags, useresp, ss;  
};

extern void _isr0();
/* _isr1, isr2 etc... */

void isrs_install(){
    idt_set_gate(0, (unsigned)_isr0, 0x08, 0x8E);
    /* _isr1, isr2 etc... */
}

void _fault_handler(struct regs *r){
	printf("Exception!\n");	
}

I assume that the ISR handling works to some capacity because if I omit the isrs_install function then the kernel crashes if it encounters an interrupt like this

Code: Select all

asm("int $0x10");
or when I divide an int by zero.

I have read the wiki page on this issue but I'm not sure if any of the listed potential problems apply to me. Can anyone point me in the right direction? Thanks.
[/size]
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: ISR handler not being called

Post by Octocontrabass »

Your code was originally written in Intel or NASM syntax and it wasn't correctly translated to AT&T syntax.
User avatar
0xY
Posts: 13
Joined: Thu Sep 29, 2022 6:12 pm
Libera.chat IRC: 0xY

Re: ISR handler not being called

Post by 0xY »

Yes the ISR tutorial I followed had a different asm syntax but I tried my best to convert it. Since it compiled without errors I assumed it was correct. Do you mind pointing out with parts specifically should be different? Thanks.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: ISR handler not being called

Post by Octocontrabass »

0xY wrote:Do you mind pointing out with parts specifically should be different?
Almost all of it, since most instructions have the operands backwards. In Intel/NASM syntax, the first operand is the destination. In AT&T syntax, the first operand is the source.

Code: Select all

	lgdt  (_gp)
The parentheses are unnecessary. A symbol without a prefix is an offset. In AT&T syntax, memory operands are typically written in the form "offset(base,index,scale)".

Code: Select all

	mov %ax, 0x10
You need a $ prefix for immediate values. (You also have the operands backwards, as I mentioned above.)

Code: Select all

    mov %eax, _fault_handler
    call *%eax
You can just call the function. Why are you trying to use an indirect call?
User avatar
0xY
Posts: 13
Joined: Thu Sep 29, 2022 6:12 pm
Libera.chat IRC: 0xY

Re: ISR handler not being called

Post by 0xY »

Got it working thanks to your help :D . I wasn't aware of those differences in the syntax.
Post Reply