Stack Segment Fault

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.
Post Reply
petrusss

Stack Segment Fault

Post by petrusss »

I have some code that works well, but when I add "char strBuffer[512]" it causes a stack segment fault.
What may be wrong?

Hmm, it seems that I can only access 1.5 KB RAM (in my kernel, like "char strBuffer[1536]") and then bochs crashes.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Stack Segment Fault

Post by Pype.Clicker »

well, it basically depend on your stack segment setup, but if you have a very small and close to segment limit stack, having ESP going beyond SS.limit will trigger this exception.
petrusss

Re:Stack Segment Fault

Post by petrusss »

OK.
I don't get Stack Segment Fault anymore, but it crashes when I use more than 1.5 KB ram in the kernel.

Hmm. How do I resize the stack?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Stack Segment Fault

Post by Pype.Clicker »

the real question is 'where does your stack come from' ...

actually, it may be :
- the BIOS bootstrap stack, which is very small and at an unknown address. The first thing your bootloader should do (after ensuring it knows the 'right' CS:IP values) is set up a bootloader stack with something like

Code: Select all

   cli
   mov ss,0x0
   mov sp,0xfffe
   sti
[/close]
This leaves most of the 'conventional' memory free for loading the kernel and reserve the lowest 64K for the bootloader's own purpose. Within those 64K, you have the 0x7e00 .. 0xffff range that will hold the stack. It should be large enough (about 32K) for most loading purpose even if you do quite complex stuff, as long as you don't have endless function call loops.

Another possibility is that your stack has been set up by a 3rd party bootloader (grub). It may occur that your kernel was loaded too close of this stack. Once again the best thing here is to set up a new bootloader-independent stack when initializing your kernel
section .bss
align 4
bottom_of_stack:
resd 2047
top_of_stack:
resd 1

section .text
_start:
; // interrupts are usually still disabled at this point
mov SS, DATA_SELECTOR
mov esp, top_of_stack
call main
cli ; // just in case the kernel had enabled interrupts
hlt

Code: Select all


Post Reply