Page 1 of 1

BxError: instruction with op1=0xff

Posted: Sun Oct 09, 2005 12:02 pm
by Guest
whenever I call a C function from my C kernel code I get the following Bochs Error:

Code: Select all

[CPU   ] BxError: instruction with op1=0xff
[CPU   ] mod was c0, nnn was 7, rm was 7
 [CPU  ] WARNING: Encountered an unknown instruction (signalling illegal instruction)
[SYS  ] bx_pc_system_c::Reset(SOFTWARE) called
I did some checking and it turns out that whenever i call a function from kmain(), I get the error.

i can link my different files together, and it works fine, it compiles fine as well. But it screws up whenever i actually call the function...

Here's my build script, maybe something is wrong with the compiler/linker options?

gcc -ffreestanding -c kmain.c -o kmain.o
gcc -o ports.o -c ports.c
ld -e kmain -Ttext 0x100000 -o kernel.o kmain.o ports.o
ld -i -e kmain -Ttext 0x100000 -o kernel.o kmain.o ports.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin


amd are ports.c and kmain.c:

Code: Select all

void kmain() {
   /*
    * A Pointer to the VGA Text Buffer at 0xB8000
    * an a lopping integer
    */
   unsigned char *vgat = (unsigned char *)0xB8000;
   unsigned short int i;
   
   /* Write "Hi" in black on white */
   *vgat++ = 'H';
   *vgat++ = 0x0F;
   *vgat++ = 'i';
   *vgat++ = 0x0F;
   
   func();
   
   /* Clear the rest of the screen */
   for(i = 2; i < 2000; i++) {
      *vgat++ = 0x20;
      *vgat++ = 0x0F;
   }
   
   /* Loop forever */
   for(;;);
}

Code: Select all

void out(unsigned short _port, unsigned char _data) {
   __asm__ __volatile__ ("out %%al, %%dx" : : "a" (_data), "d" (_port));
   return;
}

unsigned char in(unsigned short _port) {
   unsigned char ret;
   __asm__ __volatile__ ("in %%dx, %%al" : "=a" (ret) : "d" (_port));
   return ret;
}

void func() {
   
}
it crashes when I call func() from kmain()

Re:BxError: instruction with op1=0xff

Posted: Sun Oct 09, 2005 12:26 pm
by AR
You're double linking the kernel, doing it twice is unnecessary.

I recommend creating a linkscript, you are only setting the Text segment start address, not any of the other 3 critical sections.

Re:BxError: instruction with op1=0xff

Posted: Sun Oct 09, 2005 12:56 pm
by Guest
I double link the kernel because the incremental build doesn't really report any errors. I'll look into that linker script too.

Re:BxError: instruction with op1=0xff

Posted: Sun Oct 09, 2005 1:16 pm
by Pype.Clicker
does this happen _before_ or _after_ the function you call executes ?

btw, what seems odd to me is that you use incremental linking _after_ proper linking and that you use twice "-Ttext=100000" doing it only on the final link should be enough, imho.

I'd suggest you get a look at the output of objdump -drS kernel.o and see if all the pointers looks correct.

Re:BxError: instruction with op1=0xff

Posted: Sun Oct 09, 2005 5:03 pm
by Guest
well i checked it out, and everything before the call executes, but nothing inside the function executes, so I have the slight suspicion that it jumps into some random memory.

The call from the objdump of kernel.o 's kmain() looks like this:

Code: Select all

 100039:       e8 fc ff ff ff          call   10003a <kmain+0x3a>
                        10003a: R_386_PC32      func
however func is at:

Code: Select all

 
001000a8 <func>:
Anyone know why that might be??

Re:BxError: instruction with op1=0xff

Posted: Sun Oct 09, 2005 10:44 pm
by AR
Because func is in another file, the pre-linked object code doesn't know where the function is at the time, only the finished binary will provide a reliable result.

Re:BxError: instruction with op1=0xff

Posted: Mon Oct 10, 2005 2:45 am
by Pype.Clicker
what seems odd to me is that when a symbol is missing, the linker usually leave it pointing to the NULL address or something alike. Here, the call is branching towards the middle of an instruction, which is quite unusual even for unresolved references.

Honnestly, i think that you should re-think your building process, see if you _really_ need that incremental linking or not, but imho, you shouldn't do it.

Code: Select all

ld -i kmain.o ports.o -o kernel.o
ld -i something.o else.o -o somethingelse.o
ld kernel.o somethingelse.o
would make sense, but giving twice the same objects to the linker is very unlikely to produce anything good.

Re:BxError: instruction with op1=0xff

Posted: Mon Oct 10, 2005 3:36 am
by Candy
Pype.Clicker wrote: what seems odd to me is that when a symbol is missing, the linker usually leave it pointing to the NULL address or something alike. Here, the call is branching towards the middle of an instruction, which is quite unusual even for unresolved references.
The linker points it to -4 actually, which is compared to the end of the instruction. The linker expects all offsets to be compared to the beginning of the offset, so if you substract four from that you end up correctly. The value is just the value the linker needs to add to the calculated offset to make it match (symbol_location - relocation_location + skew_value).
Honnestly, i think that you should re-think your building process, see if you _really_ need that incremental linking or not, but imho, you shouldn't do it.
If you remove the second build step kernel.o will not be an object file but an executable elf file, which is what you want (I think). There's no point in overwriting it with an unlinked version afterwards.

Re:BxError: instruction with op1=0xff

Posted: Mon Oct 10, 2005 3:56 am
by Pype.Clicker
well, the only advantage i can see of using incremental linking for the final stuff is that incremental-linked stuff still contains relocation entries, so if you want to run your own loader that supports relocation of code at load time, that's nice.

Most of the time, for a kernel, you don't mind: you enforce text=some_address_known_to_be_available and you load stuff at that address. To my knowledge, Clicker might be the only system not working that way ;)

Re:BxError: instruction with op1=0xff

Posted: Mon Oct 10, 2005 5:03 am
by Candy
Pype.Clicker wrote: Most of the time, for a kernel, you don't mind: you enforce text=some_address_known_to_be_available and you load stuff at that address. To my knowledge, Clicker might be the only system not working that way ;)
Right now, atlantisos is being modified to support relocating of modules that are loaded, including linking them to kernel symbols. So no, you're not the only one :).

Re:BxError: instruction with op1=0xff

Posted: Mon Oct 10, 2005 5:23 pm
by Guest
Fixed.