calling asm function from c

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

calling asm function from c

Post 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
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Post 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?
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Post 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
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post 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.
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Post 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
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Hi,

Posted code will probably help.

Cheers,

James
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Post 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...
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:

Post 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 :)
"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
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Every good solution is obvious once you've found it.
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Post 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
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

The bochs debugger shall reveal all.
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Post 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
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Post 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...
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:

Post by Combuster »

That's not bochs' debugger. You only told it to print more to the logfile.
"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 ]
Post Reply