Page 1 of 1

IDT problems

Posted: Fri Jul 05, 2013 4:12 am
by josephks
I got some problems with IDT and ISRs. My ISR routines are not getting called after installing the IDTs. My source code

IDT structure : idt.h

Code: Select all

struct idt_entry
{
    uint16_t base_lo;
    uint16_t sel;
    uint8_t always0;
    uint8_t flags;
    uint16_t base_hi;
} __attribute__((packed));

struct idt_entry
{
    uint16_t base_lo;
    uint16_t sel;
    uint8_t always0;
    uint8_t flags;
    uint16_t base_hi;
} __attribute__((packed));

idt.c : setting the data

Code: Select all

void idt_set_gate(uint8_t num, uint64_t base, uint16_t sel, uint8_t flags)
{
    idt[num].base_lo = (base & 0xFFFF);
    idt[num].base_hi = (base >> 16) & 0xFFFF ;

    idt[num].sel = sel;
    idt[num].always0 = 0;
    idt[num].flags = flags;
}

void idt_install()
{
    idtp.limit = (sizeof(struct idt_entry) * 256) - 1;
    idtp.base = &idt;

    memset(&idt, 0, sizeof(struct idt_entry) * 256);

    idt_load();
}

asm.as

Code: Select all

.global idt_load                                                                                        
idt_load:                                                                                                 
    lidt idtp                                                                                          
    ret  

isr0:
   cli
   push 0
   push 0
   jmp isr_common_stub
.....
.extern fault_handler

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
isrs.c :Fault handler and isrs install

Code: Select all

void isrs_install()
{
    idt_set_gate(0, (unsigned)isr0, 0x08, 0x8E);
    ......
}
void fault_handler(struct regs *r)
{
        
    if(r->int_no < 32)
    {
        terminal_writestring(exception_messages[r->int_no]);  // array is defined in isrs.c
        terminal_writestring("Exception. System Halted!\n");
        for(;;);
    }

}

Kernel main:

Code: Select all

void kernel_main()
{

   gdt_install();
    idt_install();    
   isrs_install();


    terminal_initialize();
    terminal_writestring("Hello, kernel World!\n");
 

   int a = 5/0;  // Not printing the exception error here
    terminal_writestring("FFTHourtchSThirdecond line : Hello, kernel World!");

}


Re: IDT problems

Posted: Fri Jul 05, 2013 4:45 am
by josephks
Continue from my last message:
Using GNU toolchain for i586 (GCC, LD, AS)
Qemu simulator

Re: IDT problems

Posted: Fri Jul 05, 2013 2:14 pm
by Nable
You can use `edit' instead of creating additional messages (especially if there were no answers since your prev. message).

> asm.as
It seems that you're using AT&T syntax (AFAIK, GNU as uses AT&T). And if it's true then you're doing it wrong.

Code: Select all

"mov %ax, 0x10" == "mov [0x10], ax" != "mov $0x10, %ax" == "mov ax, 0x10"
"mov %eax, fault_handler" == "mov [fault_handler], eax" ...
I hope that sence of my `==' and `!=' usage is clear.
Note: it seems that there are more (than 2) such mistakes in your code.
Note2: did I something bad (when I didn't tell him to learn his tools and try some debugging) ?

Re: IDT problems

Posted: Mon Jul 08, 2013 9:54 pm
by josephks
Thanks, that solved the problem and also little modification in the exception creation code in kernel main(changed the variable[a] type to volatile, otherwise compiler is optimizing it ).