Page 1 of 1

OS get frozen when installing IDT

Posted: Fri Jan 25, 2019 6:52 pm
by deleted8917
Hello again, I've been coding an IDT, ISR and an IRQ. But when the IDT is installed, my OS is frozen.
I've been following this tutorial to do it: http://www.osdever.net/bkerndev/Docs/idt.htm
Here's an extract of kmain.c:

Code: Select all

    idt_install(); // <- Gets stuck here
    isrs_install();
    irq_install();
    __asm__ __volatile__ ("sti"); 
    init_serial();
    init_video();
Here is the complete code of my OS: https://gitlab.com/hextakatt/experimentalos
Please be patient, especially with my horrible code. ;)

Re: OS get frozen when installing IDT

Posted: Fri Jan 25, 2019 8:06 pm
by MichaelPetch
At first glance. In boot2.asm you have this:

Code: Select all

bits 16
%include "gdt.inc"
%include "a20.inc"
%include "idt.inc"
idt.inc is being included under a bits 16 directive so everything in idt.inc will be encoded as 16-bit instructions (your idt.inc doesn't specify any bits directives so it takes on whatever bits directives were specified before it while being included in boot2.asm). That won't work when running in 32-bit protected mode. As an experiment what happens if you do:

Code: Select all

bits 16
%include "gdt.inc"
%include "a20.inc"
bits 32
%include "idt.inc"
bits 16
Preferably your INC files should be using the appropriate BITS directive.

Re: OS get frozen when installing IDT

Posted: Fri Jan 25, 2019 8:26 pm
by deleted8917
Oh, what a silly error I made!
Now works, but, seems that something is messing with my VRAM (the green dot):
Image
That green dot is not only green, it changes to all the 16 colors constantly (the entire ASCII charset that you see on the screen is not related, is intentionally produced by me)
Serial communication does not work, and ISR does not work, I tried to divide by zero, the ISR should trigger, which should make the screen appear "Division by zero", but that doesn't happen!

Re: OS get frozen when installing IDT

Posted: Fri Jan 25, 2019 9:21 pm
by MichaelPetch
In idt,h you have:

Code: Select all

void idt_set_gate(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags)
{
   idt[num].base_lo = (unsigned char) (base & 0xFF);
   idt[num].base_hi = (unsigned char) ((base >> 16) & 0xFF);
   idt[num].sel = sel;
   idt[num].always0 = 0;
   idt[num].flags = flags;
}
It is unclear why you are casting to an unsigned char.lo and hi are unsigned shorts.You are alsomasking off too many bits with &0xff. Pretty sure you want 0xFFFFF. I think it should look like:

Code: Select all

void idt_set_gate(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags)
{
   idt[num].base_lo = (unsigned short) (base & 0xFFFF);
   idt[num].base_hi = (unsigned short) ((base >> 16) & 0xFFFF);
   idt[num].sel = sel;
   idt[num].always0 = 0;
   idt[num].flags = flags;
}
Your version is truncating the pointers to the interrupt handlers and sending the CPU off into neverland when interrupts occur. You may also wish to review the function irq_install. I don't believe the majority of the calls to idt_set_gate are using the correct first parameter.
I also have a recommendation. A serious one. You should stop including non static functions as code from in header files. Non static functions should be placed in separate .c files and you compile them just like you did with kmain.c and then you add the extra objects to your linker line.

Re: OS get frozen when installing IDT

Posted: Sat Jan 26, 2019 9:50 pm
by deleted8917
Still working, but when I press a key, system crashes with General protection fault. I think that is some problem with the IDT again...
But for my surprise, my entire OS has no code of any keyboard communication! or drivers...

Re: OS get frozen when installing IDT

Posted: Sat Jan 26, 2019 9:56 pm
by MichaelPetch
I mentioned (a hint) in my previous comment about irq_install routine? You happened to map most of the IRQs to a single vector.

Code: Select all

idt_set_gate(32, (unsigned)irq0, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq1, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq2, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq3, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq4, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq5, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq6, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq7, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq8, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq9, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq10, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq11, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq12, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq13, 0x08, 0x8E);
    idt_set_gate(32, (unsigned)irq14, 0x08, 0x8E);
    idt_set_gate(47, (unsigned)irq15, 0x08, 0x8E);
Notice how the first parameter is all 32 except for the last one. IMHO They should be numbered 32 through 47. You've effectively overwritten entry 32 many times leaving most interrupts with no valid interrupt handler. Effectively iRQ1 through IRQ14 will fault when they occur. IRQ1 is the keyboard handler. IRQ0 and IRQ15 won't fault because they were actually initialised.