Page 1 of 1

Invalid Opcode

Posted: Thu Nov 03, 2011 11:28 am
by MollenOS
Hey! I'm a long time stalker here, working on my own OS.

Anyway, I have an consistent issue that happens on all our pc's in my house (6 pc's), on every after bootloader jumps to my kernel, i get an Invalid Opcode Exception. However my kernel works just fine in VMWare Player, Bochs, VirtualPC.

I've read several topics here by searching that this might be caused by me having Optimization set to /Ox in visual studio. (Kernel is in VC++).

Now here is my question, if this might be the case, how important is it to actually have the max optimization included? I'm thinking about turning it completely off to avoid any surprises. But i would also be willingly to debug out which instruction it is that causes this if it is smart to enable max Optimization level.

Thanks, Philip.

Re: Invalid Opcode

Posted: Thu Nov 03, 2011 12:45 pm
by xenos
Did you figure out which instruction is causing the invalid opcode exception? Which CPUs do you use for testing?

I don't know much about VC++, so I cannot say much about its optimization. I use GCC with optimization level -O2, and there is a compiler switch which allows choosing a specific target CPU. For example, -march=pentium will generate only instructions that a Pentium CPU knows about. I don't know whether VC++ has a similar option, but this is what I would try to figure out.

Re: Invalid Opcode

Posted: Thu Nov 03, 2011 12:49 pm
by NickJohnson
Since it is failing on real hardware but not emulators, it's more likely that the problem is being caused by improperly initialized memory. Emulators tend to zero out all of their memory, which means that uninitialized memory is set to zero and may behave properly when you really should get undefined behavior; on real hardware, the contents of memory at boot are pretty much random, so uninitialized memory will likely cause problems.

Also, if your code is failing due to optimizations, you should always try to fix it. Optimizations never create bugs, they only reveal existing ones.

Re: Invalid Opcode

Posted: Thu Nov 03, 2011 12:57 pm
by MollenOS
Thanks for the suggestions so far guys.

@XenOS
I won't be able to do more debugging till i get home in some hours, but it obviously happens after setting up exceptions :P
And I actually know Visual Studio has an option for target cpu. All the CPU's I've tested on are Intel.

@NickJohnson
You may be correct here, it could be memory initialization that is incorrect. I'll have to look into that once i get home.
And that answer was in the alley I were looking for (about optimizations). I assume then it's smarter to have optimizations enabled than disabling them and just working around it.

I'll still appreciate more comments to this!

Thanks, Philip.

Re: Invalid Opcode

Posted: Thu Nov 03, 2011 3:30 pm
by MollenOS
Haha so found out what the problem was. Really stupid mistake.

Was using a signed int when looping through available pages instead of an unsigned int.

I have no idea how the emulators allowed that =/

Thanks for suggestions again!