Invalid OpCode Exception - misconfigured GDT/IDT?!

Programming, for all ages and all languages.
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Invalid OpCode Exception - misconfigured GDT/IDT?!

Post 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
Attachments
os.rar
(6.95 KiB) Downloaded 66 times
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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>
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

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

Post 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
Attachments
os.tar
(39 KiB) Downloaded 89 times
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

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

Post 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.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

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

Post 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
Developer of tyndur - community OS of Lowlevel (German)
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

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

Post 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
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

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

Post 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.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

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

Post 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.
Every good solution is obvious once you've found it.
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

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

Post 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!! :)
Attachments
complete.part2.zip
The complete kernel code (part 2)
(11.35 KiB) Downloaded 37 times
complete.part1.zip
The complete kernel code (part 1)
(60.71 KiB) Downloaded 60 times
small.zip
The important sources
(2.81 KiB) Downloaded 41 times
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

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

Post 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?
Developer of tyndur - community OS of Lowlevel (German)
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

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

Post 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??
Last edited by max on Fri Mar 09, 2012 2:12 pm, edited 2 times in total.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

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

Post 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.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

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

Post by max »

Damnit, that explains quite a lot..
Thank you very much, Kevin :)
Last edited by max on Fri Jul 26, 2013 6:05 am, edited 1 time in total.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

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

Post by Kevin »

Block lists considered harmful. Better use a proper file system or qemu -kernel.
Developer of tyndur - community OS of Lowlevel (German)
Post Reply