Page 1 of 2

calling asm function from c

Posted: Sun May 25, 2008 12:31 pm
by suthers
I'm using gcc as a compiler, nasm as an assembler and ld as a linker.
When I call an asm function from my c code the code craches.
I use bochs as an emulator and it says: "00000576062p[CPU ] >>PANIC<< prefetch: running in bogus memory" end the EIP is: 1012492f, which is way outside of the amount of allocated memory, also when I disassemble my kernel with ndisasm, I can see some calls which are outside of the bounds of my kernel. Anybody know what i'm doing wrong and how to fic this?
Thanks in advance,

Jules

Posted: Sun May 25, 2008 12:48 pm
by Korona
Are you sure the function call is the problem? Did you put a cli hlt at the beginning of your assembly function to make sure it does not get called? Are you sure the assembly function is placed into the right segment of your kernel binary and that it is loaded properly?

Posted: Sun May 25, 2008 1:03 pm
by suthers
What would be the point of putting cli hlt in front of the function, that would stop the proc every time I called the function.
Its in the text segment of my kernel and it is properly loaded...
Any body know whats wrong?
Thanks in advance,

Jules

Posted: Sun May 25, 2008 1:57 pm
by iammisc
He is telling you to put a cli hlt so that if nothing goes wrong with the hlt in place, you know that it isn't the function call. That's one of the first things to do when debugging a kernel.

When I read your post I immediately thought that it was some bug with the linker. Is your linker script correct.

Posted: Mon May 26, 2008 1:06 pm
by suthers
I have put a cli, hlt infront of the function and still got the same error, so its probably an error with the function call and I've put the function in the .text section and my linker script seems correct though simple.
Here it is:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(main)
SECTIONS
{
  .text  0x100000 : {
    *(.text)
  }
  .data  : {
    *(.data)
  }
  .bss  :
  { 					
    *(.bss)
  }
}
I get no compile or assemble warnings/errors.
Anybody see any problems?
Thanks in advance,

Jules

Posted: Mon May 26, 2008 2:15 pm
by JamesM
Hi,

Posted code will probably help.

Cheers,

James

Posted: Mon May 26, 2008 3:55 pm
by suthers
Yah sorry, should have done so at the beginning...
I'm calling this function:

Code: Select all

_isr0:
	cli
	hlt
	pusha 
	push es
	push ds
	push fs
	push gs
	mov eax, cr2
	push eax
	call _int_dev_0
	call _hlt_cpu
	pop eax
	pop s
	pop fs
	pop ds
	pop es
	popa
	iret
its a basic isr from c with a simple call:

Code: Select all

isr0()
I define the like this:

Code: Select all

extern void isr0()
and in asm:

Code: Select all

[global _isr0]
Anybody see any problems?
Thanks in advance,

Jules

edit: I think it might be because i'm calling it as an function thought it is an isr and using iret though the CPU didn't call from an interupt... therefore the SS, EIP, ESP and CS are not poped to the stack, but the CPU pops them back at the end because of the iret even though there not on the stack...

Posted: Mon May 26, 2008 4:03 pm
by Combuster
suthers wrote:edit: I think it might be because i'm calling it as an function thought it is an isr and using iret though the CPU didn't call from an interupt... therefore the SS, EIP, ESP and CS are not poped to the stack, but the CPU pops them back at the end because of the iret even though there not on the stack...
I was going to suggest the same :)

Posted: Mon May 26, 2008 4:15 pm
by suthers
I'm an idiot and I answered my own question...
:lol:
Jules
edit: So when it does the iret the CPU pops bogus values into the EIP, etc... so it restarts executing at the wrong address

Posted: Tue May 27, 2008 1:46 am
by Solar
suthers wrote:...and my linker script seems correct though simple.
Here it is:
Missing a .rodata section, you will run into problems once you add string literals to your C code.

Posted: Tue May 27, 2008 4:47 am
by suthers
I didn't have access to my code when i posted my last post, but when I replaced iret by ret, I still get the same problem, I should of known as I still got the problem when I added cli, hlt to the beginning of my isr. So I still don't see what I'm doing wrong, anybody know what's happening?
Also, where would I normally add the .rodata section in the linker script (I've seen it after the .text section, would that be ok?)?
Thanks in advance,

Jules

Posted: Tue May 27, 2008 4:57 am
by JamesM
The bochs debugger shall reveal all.

Posted: Tue May 27, 2008 6:17 am
by suthers
I noticed the error changed to >>PANIC<< exception(): 3rd (13) exception with no resolution and the final EIP = 00203cb4, which is slightly less crazy than before (this is weird as i haven't really changed any code...)
Ill use the bochs debugger to find the error, but I've never used it before so I'm a bit clueless, I'll post what I've found as soon as I figure it out...
Thanks,

Jules

Posted: Tue May 27, 2008 6:51 am
by suthers
Ok here are the three exceptions that cause bochs to stop execution:

Code: Select all

00000582862d[CPU  ] interrupt(): vector = 8, INT = 0, EXT = 1
00000582862d[CPU  ] interrupt(): gate descriptor is not valid sys seg
00000582862d[CPU  ] exception(0d h)
00000582862d[CPU  ] interrupt(): vector = 13, INT = 0, EXT = 1
00000582862d[CPU  ] interrupt(): gate descriptor is not valid sys seg
00000582862d[CPU  ] exception(0d h)
00000582862d[CPU  ] interrupt(): vector = 8, INT = 0, EXT = 1
00000582862d[CPU  ] interrupt(): gate descriptor is not valid sys seg
00000582862d[CPU  ] exception(0d h)
According to this post: http://www.osdev.org/phpBB2/viewtopic.p ... d9eeaa7818
The vector indicates what interrupt number it is, so there all CPU interrupts caused by exceptions...
So we have a double fault, then a general protection exception, then another double fault...
INT is always 0 (I presume indicating whether the interrupt flag is on or off)
and EXT apparently indicates that its external, but since i haven't activated any devices yet, this is weird, anybody know whats wrong?
Thanks in advance,

Jules

Edit: Also there is the EIP which is way out of the range of memory were I have any instructions... and the fact that when I dissemble it, I see some jumps that seem to be out of the range of instructions loaded in memory...

Posted: Tue May 27, 2008 7:50 am
by Combuster
That's not bochs' debugger. You only told it to print more to the logfile.