Page 1 of 2

Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Wed Mar 07, 2012 1:40 am
by max
Hello guys! :)

First of all, thank you all for collecting so much information about os development! :)

Well, I am currently developing my own x86 os kernel with c++ and bits of assembler. Now I'm getting a Invalid OpCode Exception, and I've been searching for a solution for hours, this might happen because of a misconfigured GDT or IDT or something more trivial but I just dont get it.
When I start the kernel, the printed message shortly comes up, but then it goes away and my panic handler prints the exception code. Could this be caused by the PIT??

I would be very thankful if you could take a look at the relevant shortened sources I've attached.

File structure:
  • extra
    • linker.ld linker script
    src
    • boot
      • Loader.asm the bootloader
      gdt
      • Gdt.cpp the GDT code
        GdtMounter.asm the GDT mounter
      idt
      • Idt.cpp the IDT code
        IdtMounter.asm the IDT mounter
        InterruptRequests.asm
        InterruptRequests.cpp
        InterruptRoutines.asm
        InterruptRoutines.cpp
      system
      • handler
        • Keyboard.cpp interrupt handler
          Timer.cpp interrupt handler
      Kernel.cpp main method


Thank you! :)
Max

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Wed Mar 07, 2012 1:53 am
by Combuster
Invalid opcode generally means jump to garbage. You can use the (bochs') debugger to find out which instruction is causing that jump. The fact that you get your panic handler called means the IDT or GDT is more likely to be correct.


Also, RAR files stink</troll>

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Wed Mar 07, 2012 2:37 am
by max
Hey :)

Thanks for the tip, currently I'm using QEMU but I will try bochs today.
Okay that sounds goood. :)

.tarball in your face! :D

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Wed Mar 07, 2012 2:45 am
by bluemoon
You can use qemu-gdb stub and do break point and single step as well. Anyway it is worth to give bochs a try.
Check the wiki for more.

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Wed Mar 07, 2012 4:14 am
by Kevin
max wrote:.tarball in your face! :D
Seems you forgot to add the Makefile? But anyway, it can't compile as it is, you forgot all headers as well.

I don't really feel like finding your bug just by code inspection, but I can have a look if you repost something that I can compile without much effort. Or have a look at qemu's -d option, -d int,in_asm should show you where things are starting to go wrong.

Code: Select all

char readKey() {
         ....
        // Returnen
        return ret;
}
Now that's a useful comment! :D

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Wed Mar 07, 2012 5:21 am
by max
Hey Kevin :D

I am not compiling with make ;)
The attachment's intention was not to be compilable, but to show you the important parts of code to see if you find any obvious errors I overlooked :)

I'll try the "-d" option and take a look at bochs ;)

For sure it is xD

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Wed Mar 07, 2012 6:29 am
by Kevin
max wrote:I am not compiling with make ;)
Right, so you already found the first thing to fix. ;)
The attachment's intention was not to be compilable, but to show you the important parts of code to see if you find any obvious errors I overlooked :)
Could really be anything, that's why having something that you can run and observe helps a lot. It shows you where to look.

The thing to look for with an unexpected exception is where eip points, and if the address doesn't make sense, the sequence of instructions (or interrupts) that led to it. Once you have this information, finding the bug often isn't that hard any more.

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Wed Mar 07, 2012 8:19 am
by Solar
max wrote:.tarball in your face! :D
Take a machete and trim it down.

Show us the minimal code required to display the problem. Try to make the snippet as small as possible. Assume that no-one here has the patience to download an attachment, or look at more than one or two screens worth of code.

And I give you a chance of >80% that you will find the error yourself in the process.

As for the other 20%, your chances of actually getting a useful reply are inversely proportional to the size of your example code.

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Fri Mar 09, 2012 1:33 pm
by max
Hey guys,

I've found some time to do a complete refactoring. Now I'm still getting the exception, and I know where it occurs.
I am doing the following things:

- Setup and mount the GDT
- Setup and mount the IDT
- Install interrupt routines

Now when setting up the interrupt requests, I try to create the IDT gate for index 41, and there I get a invalid opcode exception. I read that this request (number 9) is free for peripherals/legacy scsi/nic, so why does it crash right there?!

The registers are set like this:
Image

In the attachments theres
- the important sources containing the setup of the IDT and the interrupt requests setup
- a fully runnable version with all sources

I think that theres something more basic setup wrong, because when I am trying to give a string literal to a method accepting a "const char[]" (like the Video::print method) the value at this address is 0 when accessing it. You can see an example for this problem in the interrupt handler when trying to print the error message.

Do you have any idea? Thank you so much for your help!! :)

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Fri Mar 09, 2012 1:52 pm
by Combuster
- a fully runnable version with all sources
That's not what Solar's Machete(tm) is about. It's about providing a fully runnable version with the minimal amount of sources that demonstrates the bug.

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Fri Mar 09, 2012 1:55 pm
by Kevin
In any case it's more helpful than what he posted initially.

However, I guess you won't like my results, max: It just works for me. ;) How do you boot your kernel? Using a block list in the GRUB shell?

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Fri Mar 09, 2012 1:58 pm
by max
Well "all sources" is only the sources for the things i described here (no additional functionalities except GDT, IDT, ISRs and IRQs). The only unnecessary thing is the paging folder because its not used yet ;)


EDIT: @Kevin - what the hack? in GRUB i am typing "kernel 200+18" and then "boot". how did you do it??

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Fri Mar 09, 2012 2:04 pm
by Kevin
I would guess the difference is that I loaded the whole kernel instead of just the first 18 sectors. At least my kernel.bin is 22 kB, YMMV.

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Fri Mar 09, 2012 2:14 pm
by max
Damnit, that explains quite a lot..
Thank you very much, Kevin :)

Re: Invalid OpCode Exception - misconfigured GDT/IDT?!

Posted: Fri Mar 09, 2012 2:15 pm
by Kevin
Block lists considered harmful. Better use a proper file system or qemu -kernel.