OS get frozen when installing IDT

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
deleted8917
Member
Member
Posts: 119
Joined: Wed Dec 12, 2018 12:16 pm

OS get frozen when installing IDT

Post 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. ;)
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: OS get frozen when installing IDT

Post 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.
deleted8917
Member
Member
Posts: 119
Joined: Wed Dec 12, 2018 12:16 pm

Re: OS get frozen when installing IDT

Post 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!
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: OS get frozen when installing IDT

Post 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.
deleted8917
Member
Member
Posts: 119
Joined: Wed Dec 12, 2018 12:16 pm

Re: OS get frozen when installing IDT

Post 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...
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: OS get frozen when installing IDT

Post 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.
Post Reply